diff options
| author | 2017-01-19 15:49:50 +0100 | |
|---|---|---|
| committer | 2017-01-19 15:49:50 +0100 | |
| commit | 3955a2815e2ca90d285d42c13ee644a58d1b6701 (patch) | |
| tree | b7442bf7cedf96da850b41b4ed4772ed67043e31 /src/bplist.c | |
| parent | 26061aac4ec75e7a4469a9aab9a424716223e5c4 (diff) | |
| download | libplist-3955a2815e2ca90d285d42c13ee644a58d1b6701.tar.gz libplist-3955a2815e2ca90d285d42c13ee644a58d1b6701.tar.bz2 | |
bplist: Use proper struct for binary plist trailer
Diffstat (limited to 'src/bplist.c')
| -rw-r--r-- | src/bplist.c | 78 |
1 files changed, 31 insertions, 47 deletions
diff --git a/src/bplist.c b/src/bplist.c index 4a00683..29900e8 100644 --- a/src/bplist.c +++ b/src/bplist.c | |||
| @@ -40,19 +40,20 @@ | |||
| 40 | #include <node_iterator.h> | 40 | #include <node_iterator.h> |
| 41 | 41 | ||
| 42 | /* Magic marker and size. */ | 42 | /* Magic marker and size. */ |
| 43 | #define BPLIST_MAGIC ((uint8_t*)"bplist") | 43 | #define BPLIST_MAGIC ((uint8_t*)"bplist") |
| 44 | #define BPLIST_MAGIC_SIZE 6 | 44 | #define BPLIST_MAGIC_SIZE 6 |
| 45 | 45 | ||
| 46 | #define BPLIST_VERSION ((uint8_t*)"00") | 46 | #define BPLIST_VERSION ((uint8_t*)"00") |
| 47 | #define BPLIST_VERSION_SIZE 2 | 47 | #define BPLIST_VERSION_SIZE 2 |
| 48 | 48 | ||
| 49 | 49 | typedef struct __attribute__((packed)) { | |
| 50 | #define BPLIST_TRL_SIZE 26 | 50 | uint8_t unused[6]; |
| 51 | #define BPLIST_TRL_OFFSIZE_IDX 0 | 51 | uint8_t offset_size; |
| 52 | #define BPLIST_TRL_PARMSIZE_IDX 1 | 52 | uint8_t ref_size; |
| 53 | #define BPLIST_TRL_NUMOBJ_IDX 2 | 53 | uint64_t num_objects; |
| 54 | #define BPLIST_TRL_ROOTOBJ_IDX 10 | 54 | uint64_t root_object_index; |
| 55 | #define BPLIST_TRL_OFFTAB_IDX 18 | 55 | uint64_t offset_table_offset; |
| 56 | } bplist_trailer_t; | ||
| 56 | 57 | ||
| 57 | enum | 58 | enum |
| 58 | { | 59 | { |
| @@ -179,13 +180,6 @@ static void byte_convert(uint8_t * address, size_t size) | |||
| 179 | )))); \ | 180 | )))); \ |
| 180 | }) | 181 | }) |
| 181 | 182 | ||
| 182 | #define be64dec(x) \ | ||
| 183 | ({ \ | ||
| 184 | union plist_uint_ptr __up; \ | ||
| 185 | __up.src = x; \ | ||
| 186 | be64toh( get_unaligned(__up.u64ptr) ); \ | ||
| 187 | }) | ||
| 188 | |||
| 189 | #define get_needed_bytes(x) \ | 183 | #define get_needed_bytes(x) \ |
| 190 | ( ((uint64_t)x) < (1ULL << 8) ? 1 : \ | 184 | ( ((uint64_t)x) < (1ULL << 8) ? 1 : \ |
| 191 | ( ((uint64_t)x) < (1ULL << 16) ? 2 : \ | 185 | ( ((uint64_t)x) < (1ULL << 16) ? 2 : \ |
| @@ -701,17 +695,15 @@ static plist_t parse_bin_node_at_index(struct bplist_data *bplist, uint32_t node | |||
| 701 | 695 | ||
| 702 | PLIST_API void plist_from_bin(const char *plist_bin, uint32_t length, plist_t * plist) | 696 | PLIST_API void plist_from_bin(const char *plist_bin, uint32_t length, plist_t * plist) |
| 703 | { | 697 | { |
| 704 | char *trailer = NULL; | 698 | bplist_trailer_t *trailer = NULL; |
| 705 | |||
| 706 | uint8_t offset_size = 0; | 699 | uint8_t offset_size = 0; |
| 707 | uint8_t dict_size = 0; | 700 | uint8_t ref_size = 0; |
| 708 | uint64_t num_objects = 0; | 701 | uint64_t num_objects = 0; |
| 709 | uint64_t root_object = 0; | 702 | uint64_t root_object = 0; |
| 710 | uint64_t offset_table_index = 0; | ||
| 711 | char *offset_table = NULL; | 703 | char *offset_table = NULL; |
| 712 | 704 | ||
| 713 | //first check we have enough data | 705 | //first check we have enough data |
| 714 | if (!(length >= BPLIST_MAGIC_SIZE + BPLIST_VERSION_SIZE + BPLIST_TRL_SIZE)) | 706 | if (!(length >= BPLIST_MAGIC_SIZE + BPLIST_VERSION_SIZE + sizeof(bplist_trailer_t))) |
| 715 | return; | 707 | return; |
| 716 | //check that plist_bin in actually a plist | 708 | //check that plist_bin in actually a plist |
| 717 | if (memcmp(plist_bin, BPLIST_MAGIC, BPLIST_MAGIC_SIZE) != 0) | 709 | if (memcmp(plist_bin, BPLIST_MAGIC, BPLIST_MAGIC_SIZE) != 0) |
| @@ -721,14 +713,13 @@ PLIST_API void plist_from_bin(const char *plist_bin, uint32_t length, plist_t * | |||
| 721 | return; | 713 | return; |
| 722 | 714 | ||
| 723 | //now parse trailer | 715 | //now parse trailer |
| 724 | trailer = (char *) (plist_bin + (length - BPLIST_TRL_SIZE)); | 716 | trailer = (bplist_trailer_t*)(plist_bin + (length - sizeof(bplist_trailer_t))); |
| 725 | 717 | ||
| 726 | offset_size = trailer[BPLIST_TRL_OFFSIZE_IDX]; | 718 | offset_size = trailer->offset_size; |
| 727 | dict_size = trailer[BPLIST_TRL_PARMSIZE_IDX]; | 719 | ref_size = trailer->ref_size; |
| 728 | num_objects = be64dec(trailer + BPLIST_TRL_NUMOBJ_IDX); | 720 | num_objects = be64toh(trailer->num_objects); |
| 729 | root_object = be64dec(trailer + BPLIST_TRL_ROOTOBJ_IDX); | 721 | root_object = be64toh(trailer->root_object_index); |
| 730 | offset_table_index = be64dec(trailer + BPLIST_TRL_OFFTAB_IDX); | 722 | offset_table = (char *)(plist_bin + be64toh(trailer->offset_table_offset)); |
| 731 | offset_table = (char *) (plist_bin + offset_table_index); | ||
| 732 | 723 | ||
| 733 | if (num_objects == 0) | 724 | if (num_objects == 0) |
| 734 | return; | 725 | return; |
| @@ -752,7 +743,7 @@ PLIST_API void plist_from_bin(const char *plist_bin, uint32_t length, plist_t * | |||
| 752 | bplist.data = plist_bin; | 743 | bplist.data = plist_bin; |
| 753 | bplist.size = length; | 744 | bplist.size = length; |
| 754 | bplist.num_objects = num_objects; | 745 | bplist.num_objects = num_objects; |
| 755 | bplist.dict_size = dict_size; | 746 | bplist.dict_size = ref_size; |
| 756 | bplist.offset_size = offset_size; | 747 | bplist.offset_size = offset_size; |
| 757 | bplist.offset_table = offset_table; | 748 | bplist.offset_table = offset_table; |
| 758 | bplist.level = 0; | 749 | bplist.level = 0; |
| @@ -1142,8 +1133,7 @@ PLIST_API void plist_to_bin(plist_t plist, char **plist_bin, uint32_t * length) | |||
| 1142 | uint64_t i = 0; | 1133 | uint64_t i = 0; |
| 1143 | uint8_t *buff = NULL; | 1134 | uint8_t *buff = NULL; |
| 1144 | uint64_t *offsets = NULL; | 1135 | uint64_t *offsets = NULL; |
| 1145 | uint8_t pad[6] = { 0, 0, 0, 0, 0, 0 }; | 1136 | bplist_trailer_t trailer; |
| 1146 | uint8_t trailer[BPLIST_TRL_SIZE]; | ||
| 1147 | //for string | 1137 | //for string |
| 1148 | long len = 0; | 1138 | long len = 0; |
| 1149 | long items_read = 0; | 1139 | long items_read = 0; |
| @@ -1265,21 +1255,15 @@ PLIST_API void plist_to_bin(plist_t plist, char **plist_bin, uint32_t * length) | |||
| 1265 | free(offsetbuff); | 1255 | free(offsetbuff); |
| 1266 | } | 1256 | } |
| 1267 | 1257 | ||
| 1268 | //experimental pad to reflect apple's files | ||
| 1269 | byte_array_append(bplist_buff, pad, 6); | ||
| 1270 | |||
| 1271 | //setup trailer | 1258 | //setup trailer |
| 1272 | num_objects = be64toh(num_objects); | 1259 | memset(trailer.unused, '\0', sizeof(trailer.unused)); |
| 1273 | root_object = be64toh(root_object); | 1260 | trailer.offset_size = offset_size; |
| 1274 | offset_table_index = be64toh(offset_table_index); | 1261 | trailer.ref_size = dict_param_size; |
| 1275 | 1262 | trailer.num_objects = be64toh(num_objects); | |
| 1276 | memcpy(trailer + BPLIST_TRL_OFFSIZE_IDX, &offset_size, sizeof(uint8_t)); | 1263 | trailer.root_object_index = be64toh(root_object); |
| 1277 | memcpy(trailer + BPLIST_TRL_PARMSIZE_IDX, &dict_param_size, sizeof(uint8_t)); | 1264 | trailer.offset_table_offset = be64toh(offset_table_index); |
| 1278 | memcpy(trailer + BPLIST_TRL_NUMOBJ_IDX, &num_objects, sizeof(uint64_t)); | 1265 | |
| 1279 | memcpy(trailer + BPLIST_TRL_ROOTOBJ_IDX, &root_object, sizeof(uint64_t)); | 1266 | byte_array_append(bplist_buff, &trailer, sizeof(bplist_trailer_t)); |
| 1280 | memcpy(trailer + BPLIST_TRL_OFFTAB_IDX, &offset_table_index, sizeof(uint64_t)); | ||
| 1281 | |||
| 1282 | byte_array_append(bplist_buff, trailer, BPLIST_TRL_SIZE); | ||
| 1283 | 1267 | ||
| 1284 | //duplicate buffer | 1268 | //duplicate buffer |
| 1285 | *plist_bin = (char *) malloc(bplist_buff->len); | 1269 | *plist_bin = (char *) malloc(bplist_buff->len); |
