-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
420 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
#include "xf.h" | ||
|
||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <inttypes.h> | ||
|
||
/** | ||
* Given an instance of `xf_blob_t` (such as `j_drec_val_t::xfields` or | ||
* `j_inode_val_t::xfields`), get an array of instances of `xf_pair_t`, which | ||
* allows you to easily access the xfield value for a given xfield key/type, | ||
* since they are then stored directly alongside each other. | ||
* | ||
* xfields: | ||
* A pointer to an instance of `xf_blob_t`, including the data in its | ||
* `xf_data` field. | ||
* | ||
* RETURN VALUE: | ||
* A pointer to the head of an array of pointers to instances of | ||
* `xf_pair_t`. The length of this array is not returned to the caller, | ||
* but the last element of this array will be a NULL pointer. If an error | ||
* occurs, NULL is returned. | ||
* | ||
* When the data in the array is no longer needed, it is the caller's | ||
* responsibility to free the associated memory by passing the pointer | ||
* that was returned by this function to `free_xf_pairs_array()`. | ||
*/ | ||
xf_pair_t** get_xf_pairs_array(xf_blob_t* xfields) { | ||
/* | ||
* Create the array to return. We use `calloc()` here so that early return | ||
* in case of error does not cause our call to `free_xf_pairs_array()` to | ||
* `free()` arbitrary pointers. In other words, it makes sure that | ||
* `xf_pairs_array` is always NULL-terminated regardless of how many | ||
* entries it has been populated with so far. | ||
*/ | ||
xf_pair_t** xf_pairs_array = calloc(xfields->xf_num_exts + 1, sizeof(xf_pair_t*)); | ||
if (!xf_pairs_array) { | ||
fprintf(stderr, "\nERROR: %s: Couldn't create `xf_pairs_array`.\n", __func__); | ||
return NULL; | ||
} | ||
|
||
x_field_t* xf_keys = xfields->xf_data; | ||
uint8_t* xf_values = xf_keys + xfields->xf_num_exts; | ||
|
||
/* | ||
* Use this value to keep track of how many bytes into `xf_values` the | ||
* current xfield's value starts. This will always be a multiple of 8 before | ||
* we copy a value, since the values are 8-byte aligned. | ||
*/ | ||
uint16_t xf_value_cursor_index = 0; | ||
|
||
// Populate the array | ||
for (uint16_t i = 0; i < xfields->xf_num_exts; i++) { | ||
// Move to the start of the next 8-byte aligned segment. | ||
uint16_t remainder = xf_value_cursor_index % 8; | ||
if (remainder != 0) { | ||
xf_value_cursor_index += 8 - remainder; | ||
} | ||
|
||
xf_pairs_array[i] = malloc(sizeof(x_field_t) + xf_keys[i].x_size); | ||
if (!xf_pairs_array[i]) { | ||
fprintf(stderr, "\nERROR: %s: Couldn't create `xf_pairs_array[%"PRIu16"].`\n", __func__, i); | ||
free_xf_pairs_array(xf_pairs_array); | ||
return NULL; | ||
} | ||
|
||
memcpy(xf_pairs_array[i], xf_keys + i, sizeof(x_field_t)); | ||
memcpy(&(xf_pairs_array[i]->value), xf_values + xf_value_cursor_index, xf_keys[i].x_size); | ||
|
||
xf_value_cursor_index += xf_keys[i].x_size; | ||
} | ||
xf_pairs_array[xfields->xf_num_exts] = NULL; | ||
|
||
return xf_pairs_array; | ||
} | ||
|
||
/** | ||
* Free memory allocated for a file-system records array that | ||
* was created by a call to `get_xf_pairs_array()`. | ||
* | ||
* xf_pairs_array: | ||
* A pointer to an array of pointers to instances of `xf_pair_t`, as | ||
* returned by a call to `get_xfields_array()`. | ||
*/ | ||
void free_xf_pairs_array(xf_pair_t** xf_pairs_array) { | ||
if (!xf_pairs_array) { | ||
return; | ||
} | ||
|
||
for (xf_pair_t** cursor = xf_pairs_array; *cursor; cursor++) { | ||
free(*cursor); | ||
} | ||
free(xf_pairs_array); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#ifndef DRAT_FUNC_XF_H | ||
#define DRAT_FUNC_XF_H | ||
|
||
#include <apfs/xf.h> | ||
|
||
/** | ||
* Data structure used to collect an xfield's key (an instance of `x_field_t`, | ||
* which contains its type, flags, and the size of its value) and its value | ||
* together. The function `get_xfields_array()`, which returns a pointer | ||
* to an array of instance of this data structure, allows us to more easily | ||
* access xfields without needing to do cumbersome arithmetic to find the xfield | ||
* value in an instance of `xf_blob_t`. | ||
*/ | ||
typedef struct { | ||
x_field_t key; | ||
uint8_t value[]; | ||
} xf_pair_t; | ||
|
||
xf_pair_t** get_xf_pairs_array(xf_blob_t* xfields); | ||
|
||
void free_xf_pairs_array(xf_pair_t** xfields_array); | ||
|
||
#endif // DRAT_FUNC_XF_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#include "dstream.h" | ||
|
||
#include <inttypes.h> | ||
#include <stdio.h> | ||
|
||
void print_j_dstream(j_dstream_t* dstream) { | ||
printf( | ||
"Size: %"PRIu64" bytes\n" | ||
"Allocated size: %"PRIu64" bytes\n" | ||
"Default crypto ID: %#"PRIx64"\n" | ||
"Total bytes written: %"PRIu64" bytes\n" | ||
"Total bytes read: %"PRIu64" bytes\n", | ||
|
||
dstream->size, | ||
dstream->alloced_size, | ||
dstream->default_crypto_id, | ||
dstream->total_bytes_written, | ||
dstream->total_bytes_read | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#ifndef DRAT_STRING_DSTREAM_H | ||
#define DRAT_STRING_DSTREAM_H | ||
|
||
#include <apfs/dstream.h> | ||
|
||
void print_j_dstream(j_dstream_t* dstream); | ||
|
||
#endif // DRAT_STRING_DSTREAM_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#include "general.h" | ||
|
||
#include <stdio.h> | ||
#include <stdint.h> | ||
#include <inttypes.h> | ||
|
||
void print_uuid(uuid_t uuid) { | ||
for (int i = 0; i < 16; i++) { | ||
switch (i) { | ||
case 4: case 6: case 8: case 10: | ||
printf("-"); | ||
// fall through | ||
default: | ||
printf("%02X", uuid[i]); | ||
break; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#ifndef DRAT_STRING_GENERAL_H | ||
#define DRAT_STRING_GENERAL_H | ||
|
||
#include <apfs/general.h> | ||
|
||
void print_uuid(uuid_t uuid); | ||
|
||
#endif // DRAT_STRING_GENERAL_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.