From f4c4b783c8dbe2fe8e7e6f6b5f19f0d44b489c9a Mon Sep 17 00:00:00 2001 From: Zach C Date: Sun, 31 Aug 2008 11:25:22 -0700 Subject: Added binary-plist support (tweaked slightly to move stuff around) Signed-off-by: Matt Colyer fix makefile to take correct main function into account --- src/plist.c | 284 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 284 insertions(+) (limited to 'src/plist.c') diff --git a/src/plist.c b/src/plist.c index c4d6bfa..0024577 100644 --- a/src/plist.c +++ b/src/plist.c @@ -23,8 +23,10 @@ #include #include #include +#include "utils.h" #include "plist.h" + const char *plist_base = "\n\ \n\ \n\ @@ -243,3 +245,285 @@ void free_dictionary(char **dictionary) free(dictionary); } + +/* + * Binary propertylist code follows + */ + + +/* + * This is how parsing a bplist is going to have to work: + * - The entire binary plist is going to have to be in memory. + * - A function, parse_nodes(), will have to be a recursive function + * which iterates over the binary plist and reads in elements into bplist_node structs + * and handles them accordingly. The end result should be a somewhat-hierarchical layout + * of bplist_nodes. + * - parse_nodes() will return the first node it encounters, which is usually the "root" node. + */ + +uint32_t uipow(uint32_t value, uint32_t power) { + if (!power) return 1; + int i = 0, oVal = value; + for (i = 1; i < power; i++) { + value *= oVal; + } + return value; +} + +void byte_convert(char *address, size_t size) { + int i = 0, j = 0; + char tmp = '\0'; + + for (i = 0; i < (size / 2); i++) { + tmp = address[i]; + j = ((size-1) + 0) - i; + address[i] = address[j]; + address[j] = tmp; + } +} + +bplist_node *parse_raw_node(const char *bpbuffer, uint32_t bplength, uint32_t *position, uint8_t ref_size) { + if (!position || !bpbuffer || !bplength) return NULL; + + uint8_t modifier = 0; + bplist_node *new_node = (bplist_node*)malloc(sizeof(bplist_node)); + bplist_node *length_stupidity = NULL; + memset(new_node, 0, sizeof(bplist_node)); // initialize the new struct + + int myPos = *position; + if (myPos == bplength || (myPos+1) == bplength) { free(new_node); return NULL; } // end of string + + uint32_t length = 0; + if (!myPos) { + if (strncmp(bpbuffer, "bplist00", strlen("bplist00"))) { + return NULL; // badness! + } + myPos += strlen("bplist00"); + } + + // Get the node's type. + if (bpbuffer[myPos] == BPLIST_DATE) { // handle date separately, but do it as a real + // better handling of date; basically interpret as real or double + new_node->type = BPLIST_DATE; + new_node->length = 8; // always 8 for "date" (Apple intended it, not me) + myPos++; + memcpy(&new_node->realval, bpbuffer+myPos, sizeof(new_node->realval)); + byte_convert(&new_node->realval, sizeof(new_node->realval)); + myPos += new_node->length; + *position = myPos; + return new_node; + } + + new_node->type = bpbuffer[myPos] & BPLIST_MASK; + new_node->length = bpbuffer[myPos] & BPLIST_FILL; + if (!new_node->type) { + // what? check if it's a boolean. + if (bpbuffer[myPos] == BPLIST_TRUE || bpbuffer[myPos] == BPLIST_FALSE) { + // okay, so it is. Carry on. + new_node->type = bpbuffer[myPos]; + new_node->length = 0; + } else { + // er, what? we have a bad type here. Return NULL. + free(new_node); + //printf("parse_raw_node: lol type: type given %x\n", bpbuffer[myPos]); + return NULL; + } + } + + myPos++; // puts us in the data. + if (new_node->length == BPLIST_FILL) { // Data happens to contain length... + // what? you're going to make me parse an int for the length. You suck. + *position = myPos; + length_stupidity = parse_raw_node(bpbuffer, bplength, &myPos, ref_size); + switch (length_stupidity->length) { + case sizeof(uint8_t): + new_node->length = length_stupidity->intval8; + break; + case sizeof(uint16_t): + new_node->length = length_stupidity->intval16; + break; + case sizeof(uint32_t): + new_node->length = length_stupidity->intval32; + break; + case sizeof(uint64_t): + new_node->length = length_stupidity->intval64; + break; + default: + free(new_node); + free(length_stupidity); + return NULL; + } + // There, we have our fucking length now. + *position = myPos; + free(length_stupidity); // cleanup + } + + // Now we're in the data. + // Error-checking sorta + if ((myPos + new_node->length) >= bplength) { + new_node->length = bplength - myPos; // truncate the object + } + + // And now for the greatest show on earth: the giant fucking switch statement. + switch (new_node->type) { + case BPLIST_INT: + new_node->length = uipow(2, new_node->length); // make length less misleading + switch (new_node->length) { + case sizeof(uint8_t): + new_node->intval8 = bpbuffer[myPos]; + break; + case sizeof(uint16_t): + memcpy(&new_node->intval16, bpbuffer+myPos, sizeof(uint16_t)); + new_node->intval16 = ntohs(new_node->intval16); + break; + case sizeof(uint32_t): + memcpy(&new_node->intval32, bpbuffer+myPos, sizeof(uint32_t)); + new_node->intval32 = ntohl(new_node->intval32); + break; + case sizeof(uint64_t): + memcpy(&new_node->intval64, bpbuffer+myPos, sizeof(uint64_t)); + byte_convert(&new_node->intval64, sizeof(uint64_t)); + break; + default: + free(new_node); + printf("parse_raw_node: lol: invalid int: size given %i\n", new_node->length); + printf("parse_raw_node: lol: by the way sizeof(uint64) = %i\n", sizeof(uint64_t)); + return NULL; + } + break; + + case BPLIST_REAL: + new_node->length = uipow(2, new_node->length); + memcpy(&new_node->realval, bpbuffer+myPos, new_node->length); // XXX: probable buffer overflow here + //new_node->realval = bpbuffer[myPos]; // why not + byte_convert(&new_node->realval, sizeof(double)); + break; + + case BPLIST_DICT: /* returning a raw dict, it forward-references, so. */ + new_node->length = new_node->length * 2; // dicts lie + case BPLIST_ARRAY: /* returning a raw array, it forward-references, so. */ + new_node->intval8 = ref_size; // in arrays and dicts, the "ref size" alluded to in the trailer applies, and should be stored in intval8 so as to save space. + case BPLIST_STRING: + case BPLIST_DATA: + default: /* made to hold raw data. */ + modifier = (new_node->intval8 > 0) ? new_node->intval8 : 1; + new_node->strval = (char*)malloc(sizeof(char) * (new_node->length * modifier)); + memcpy(new_node->strval, bpbuffer+myPos, (new_node->length * modifier)); + break; + + case BPLIST_UNICODE: + new_node->unicodeval = (wchar_t*)malloc(sizeof(wchar_t) * new_node->length); + memcpy(new_node->unicodeval, bpbuffer+myPos, new_node->length); + break; + } + + myPos += new_node->length; + *position = myPos; + return new_node; +} + +void print_bytes(char *val, size_t size) { + int i = 0; + for (i = 0; i < size; i++) { + printf("Byte %i: 0x%x\n", i, val[i]); + } +} + +bplist_node *parse_nodes(const char *bpbuffer, uint32_t bplength, uint32_t *position) { + bplist_node **nodeslist = NULL, **newaddr = NULL; + bplist_node *new_node = NULL, *root_node = NULL; + + uint32_t nodeslength = 0; + uint8_t offset_size = 0, dict_param_size = 0; + offset_size = bpbuffer[bplength-26]; + dict_param_size = bpbuffer[bplength-25]; + uint64_t current_offset = 0; + //uint64_t num_objects = *(bpbuffer+(bplength-24)), root_object = *(bpbuffer+(bplength-16)), offset_table_index = *(bpbuffer+(bplength-8)); + uint64_t num_objects = 0, root_object = 0, offset_table_index = 0; + memcpy(&num_objects, bpbuffer+bplength-24, sizeof(uint64_t)); + memcpy(&root_object, bpbuffer+bplength-16, sizeof(uint64_t)); + memcpy(&offset_table_index, bpbuffer+bplength-8, sizeof(uint64_t)); + byte_convert(&num_objects, sizeof(uint64_t)); + byte_convert(&root_object, sizeof(uint64_t)); + byte_convert(&offset_table_index, sizeof(uint64_t)); + + log_debug_msg("Offset size: %i\nGiven: %i\n", offset_size, bpbuffer[bplength-26]); + log_debug_msg("Ref size: %i\nGiven: %i\n", dict_param_size, bpbuffer[bplength-25]); + log_debug_msg("Number of objects: %lli\nGiven: %llu\n", num_objects, *(bpbuffer+bplength-24)); + log_debug_msg("Root object index: %lli\nGiven: %llu\n", root_object, *(bpbuffer+bplength-16)); + log_debug_msg("Offset table index: %lli\nGiven: %llu\n", offset_table_index, *(bpbuffer+bplength-8)); + log_debug_msg("Size of uint64: %i\n", sizeof(uint64_t)); + + int i = 0, j = 0, k = 0, str_i = 0, str_j = 0; + uint32_t index1 = 0, index2 = 0; + + nodeslist = (bplist_node**)malloc(sizeof(bplist_node*) * num_objects); + if (!nodeslist) return NULL; + + for (i = 0; i < num_objects; i++) { + memcpy(¤t_offset, bpbuffer+(offset_table_index+(i*offset_size)), offset_size); + //current_offset = (offset_size == 2) ? ntohs(current_offset) : (offset_size == 4) ? ntohl(current_offset) : current_offset; + //if (offset_size == 8) byte_convert(¤t_offset, 8); + byte_convert(¤t_offset, (offset_size <= sizeof(current_offset)) ? offset_size : sizeof(current_offset)); + log_debug_msg("parse_nodes: current_offset = %x\n", current_offset); + nodeslist[i] = parse_raw_node(bpbuffer, bplength, ¤t_offset, dict_param_size); + log_debug_msg("parse_nodes: parse_raw_node done\n"); + } + + + for (i = 0; i < num_objects; i++) { + // set elements for dicts and arrays and leave the rest alone + log_debug_msg("parse_nodes: on node %i\n", i); + switch (nodeslist[i]->type) { + case BPLIST_DICT: + log_debug_msg("parse_nodes: dictionary found\n"); + nodeslist[i]->subnodes = (bplist_node*)malloc(sizeof(bplist_node) * nodeslist[i]->length); + for (j = 0; j < (nodeslist[i]->length / 2); j++) { + str_i = j * nodeslist[i]->intval8; + str_j = (j + (nodeslist[i]->length / 2)) * nodeslist[i]->intval8; + + memcpy(&index1, nodeslist[i]->strval+str_i, nodeslist[i]->intval8); + memcpy(&index2, nodeslist[i]->strval+str_j, nodeslist[i]->intval8); + //index1 = (dict_param_size == 1) ? index1 : (dict_param_size == 2) ? ntohs(index1) : (dict_param_size == 4) ? ntohl(index1) : index1; + //index2 = (dict_param_size == 1) ? index2 : (dict_param_size == 2) ? ntohs(index2) : (dict_param_size == 4) ? ntohl(index2) : index2; + byte_convert(&index1, (dict_param_size <= sizeof(index1)) ? dict_param_size : sizeof(index2)); + byte_convert(&index2, (dict_param_size <= sizeof(index2)) ? dict_param_size : sizeof(index2)); + //printf("parse_nodes: key index %i value %i\n", index1, index2); + //printf("parse_nodes: key type %x and length %i\n", nodeslist[index1]->type, nodeslist[index1]->length); + //printf("parse_nodes: value type %x and length %i\n", nodeslist[index2]->type, nodeslist[index2]->length); + nodeslist[i]->subnodes[k++] = nodeslist[index1]; + nodeslist[i]->subnodes[k++] = nodeslist[index2]; + } + + nodeslist[i]->length = nodeslist[i]->length / 2; + free(nodeslist[i]->strval); + k = 0; + break; + + case BPLIST_ARRAY: + log_debug_msg("parse_nodes: array found\n"); + nodeslist[i]->subnodes = (bplist_node*)malloc(sizeof(bplist_node) * nodeslist[i]->length); // memory allocation helps a lot when storing data + + for (j = 0; j < nodeslist[i]->length; j++) { + log_debug_msg("parse_nodes: array index %i\n", j); + str_j = j * nodeslist[i]->intval8; + //index1 = nodeslist[i]->strval[j]; + memcpy(&index1, nodeslist[i]->strval+str_j, nodeslist[i]->intval8); + log_debug_msg("parse_nodes: post-memcpy\n"); + //index1 = (dict_param_size == 1) ? index1 : (dict_param_size == 2) ? ntohs(index1) : (dict_param_size == 4) ? ntohl(index1) : index1; + byte_convert(&index1, (dict_param_size <= sizeof(index1)) ? dict_param_size : sizeof(index1)); + log_debug_msg("parse_nodes: post-ntohl\nindex1 = %i\n", index1); + nodeslist[i]->subnodes[j] = nodeslist[index1]; + log_debug_msg("parse_nodes: post-assignment\n"); + } + free(nodeslist[i]->strval); + break; + default: + //printf("lol... type %x\n", nodeslist[i]->type); + break; + } // those are the only two we need to correct for. + } + + root_node = nodeslist[root_object]; + return root_node; +} -- cgit v1.1-32-gdbae From aed2c025f6e47dc769675e564cc574adc496a88a Mon Sep 17 00:00:00 2001 From: Jonathan Beck Date: Tue, 25 Nov 2008 19:14:27 +0100 Subject: fix some warnings and indent --- src/plist.c | 335 +++++++++++++++++++++++++++++++----------------------------- 1 file changed, 172 insertions(+), 163 deletions(-) (limited to 'src/plist.c') diff --git a/src/plist.c b/src/plist.c index 0024577..7a09b4d 100644 --- a/src/plist.c +++ b/src/plist.c @@ -261,8 +261,10 @@ void free_dictionary(char **dictionary) * - parse_nodes() will return the first node it encounters, which is usually the "root" node. */ -uint32_t uipow(uint32_t value, uint32_t power) { - if (!power) return 1; +uint32_t uipow(uint32_t value, uint32_t power) +{ + if (!power) + return 1; int i = 0, oVal = value; for (i = 1; i < power; i++) { value *= oVal; @@ -270,50 +272,55 @@ uint32_t uipow(uint32_t value, uint32_t power) { return value; } -void byte_convert(char *address, size_t size) { +void byte_convert(char *address, size_t size) +{ int i = 0, j = 0; char tmp = '\0'; - + for (i = 0; i < (size / 2); i++) { tmp = address[i]; - j = ((size-1) + 0) - i; + j = ((size - 1) + 0) - i; address[i] = address[j]; address[j] = tmp; } } - -bplist_node *parse_raw_node(const char *bpbuffer, uint32_t bplength, uint32_t *position, uint8_t ref_size) { - if (!position || !bpbuffer || !bplength) return NULL; - + +bplist_node *parse_raw_node(const char *bpbuffer, uint32_t bplength, uint32_t * position, uint8_t ref_size) +{ + if (!position || !bpbuffer || !bplength) + return NULL; + uint8_t modifier = 0; - bplist_node *new_node = (bplist_node*)malloc(sizeof(bplist_node)); + bplist_node *new_node = (bplist_node *) malloc(sizeof(bplist_node)); bplist_node *length_stupidity = NULL; - memset(new_node, 0, sizeof(bplist_node)); // initialize the new struct - + memset(new_node, 0, sizeof(bplist_node)); // initialize the new struct + int myPos = *position; - if (myPos == bplength || (myPos+1) == bplength) { free(new_node); return NULL; } // end of string - + if (myPos == bplength || (myPos + 1) == bplength) { + free(new_node); + return NULL; + } // end of string + uint32_t length = 0; if (!myPos) { if (strncmp(bpbuffer, "bplist00", strlen("bplist00"))) { - return NULL; // badness! + return NULL; // badness! } myPos += strlen("bplist00"); } - // Get the node's type. - if (bpbuffer[myPos] == BPLIST_DATE) { // handle date separately, but do it as a real + if (bpbuffer[myPos] == BPLIST_DATE) { // handle date separately, but do it as a real // better handling of date; basically interpret as real or double new_node->type = BPLIST_DATE; - new_node->length = 8; // always 8 for "date" (Apple intended it, not me) + new_node->length = 8; // always 8 for "date" (Apple intended it, not me) myPos++; - memcpy(&new_node->realval, bpbuffer+myPos, sizeof(new_node->realval)); - byte_convert(&new_node->realval, sizeof(new_node->realval)); + memcpy(&new_node->realval, bpbuffer + myPos, sizeof(new_node->realval)); + byte_convert((char *) &new_node->realval, sizeof(new_node->realval)); myPos += new_node->length; *position = myPos; return new_node; } - + new_node->type = bpbuffer[myPos] & BPLIST_MASK; new_node->length = bpbuffer[myPos] & BPLIST_FILL; if (!new_node->type) { @@ -322,151 +329,153 @@ bplist_node *parse_raw_node(const char *bpbuffer, uint32_t bplength, uint32_t *p // okay, so it is. Carry on. new_node->type = bpbuffer[myPos]; new_node->length = 0; - } else { + } else { // er, what? we have a bad type here. Return NULL. free(new_node); //printf("parse_raw_node: lol type: type given %x\n", bpbuffer[myPos]); return NULL; } } - - myPos++; // puts us in the data. - if (new_node->length == BPLIST_FILL) { // Data happens to contain length... + + myPos++; // puts us in the data. + if (new_node->length == BPLIST_FILL) { // Data happens to contain length... // what? you're going to make me parse an int for the length. You suck. *position = myPos; length_stupidity = parse_raw_node(bpbuffer, bplength, &myPos, ref_size); switch (length_stupidity->length) { - case sizeof(uint8_t): - new_node->length = length_stupidity->intval8; - break; - case sizeof(uint16_t): - new_node->length = length_stupidity->intval16; - break; - case sizeof(uint32_t): - new_node->length = length_stupidity->intval32; - break; - case sizeof(uint64_t): - new_node->length = length_stupidity->intval64; - break; - default: - free(new_node); - free(length_stupidity); - return NULL; + case sizeof(uint8_t): + new_node->length = length_stupidity->intval8; + break; + case sizeof(uint16_t): + new_node->length = length_stupidity->intval16; + break; + case sizeof(uint32_t): + new_node->length = length_stupidity->intval32; + break; + case sizeof(uint64_t): + new_node->length = length_stupidity->intval64; + break; + default: + free(new_node); + free(length_stupidity); + return NULL; } // There, we have our fucking length now. *position = myPos; - free(length_stupidity); // cleanup + free(length_stupidity); // cleanup } - // Now we're in the data. // Error-checking sorta if ((myPos + new_node->length) >= bplength) { - new_node->length = bplength - myPos; // truncate the object + new_node->length = bplength - myPos; // truncate the object } - // And now for the greatest show on earth: the giant fucking switch statement. switch (new_node->type) { - case BPLIST_INT: - new_node->length = uipow(2, new_node->length); // make length less misleading - switch (new_node->length) { - case sizeof(uint8_t): - new_node->intval8 = bpbuffer[myPos]; - break; - case sizeof(uint16_t): - memcpy(&new_node->intval16, bpbuffer+myPos, sizeof(uint16_t)); - new_node->intval16 = ntohs(new_node->intval16); - break; - case sizeof(uint32_t): - memcpy(&new_node->intval32, bpbuffer+myPos, sizeof(uint32_t)); - new_node->intval32 = ntohl(new_node->intval32); - break; - case sizeof(uint64_t): - memcpy(&new_node->intval64, bpbuffer+myPos, sizeof(uint64_t)); - byte_convert(&new_node->intval64, sizeof(uint64_t)); - break; - default: - free(new_node); - printf("parse_raw_node: lol: invalid int: size given %i\n", new_node->length); - printf("parse_raw_node: lol: by the way sizeof(uint64) = %i\n", sizeof(uint64_t)); - return NULL; - } + case BPLIST_INT: + new_node->length = uipow(2, new_node->length); // make length less misleading + switch (new_node->length) { + case sizeof(uint8_t): + new_node->intval8 = bpbuffer[myPos]; break; - - case BPLIST_REAL: - new_node->length = uipow(2, new_node->length); - memcpy(&new_node->realval, bpbuffer+myPos, new_node->length); // XXX: probable buffer overflow here - //new_node->realval = bpbuffer[myPos]; // why not - byte_convert(&new_node->realval, sizeof(double)); + case sizeof(uint16_t): + memcpy(&new_node->intval16, bpbuffer + myPos, sizeof(uint16_t)); + new_node->intval16 = ntohs(new_node->intval16); break; - - case BPLIST_DICT: /* returning a raw dict, it forward-references, so. */ - new_node->length = new_node->length * 2; // dicts lie - case BPLIST_ARRAY: /* returning a raw array, it forward-references, so. */ - new_node->intval8 = ref_size; // in arrays and dicts, the "ref size" alluded to in the trailer applies, and should be stored in intval8 so as to save space. - case BPLIST_STRING: - case BPLIST_DATA: - default: /* made to hold raw data. */ - modifier = (new_node->intval8 > 0) ? new_node->intval8 : 1; - new_node->strval = (char*)malloc(sizeof(char) * (new_node->length * modifier)); - memcpy(new_node->strval, bpbuffer+myPos, (new_node->length * modifier)); + case sizeof(uint32_t): + memcpy(&new_node->intval32, bpbuffer + myPos, sizeof(uint32_t)); + new_node->intval32 = ntohl(new_node->intval32); break; - - case BPLIST_UNICODE: - new_node->unicodeval = (wchar_t*)malloc(sizeof(wchar_t) * new_node->length); - memcpy(new_node->unicodeval, bpbuffer+myPos, new_node->length); + case sizeof(uint64_t): + memcpy(&new_node->intval64, bpbuffer + myPos, sizeof(uint64_t)); + byte_convert((char *) &new_node->intval64, sizeof(uint64_t)); break; + default: + free(new_node); + printf("parse_raw_node: lol: invalid int: size given %lu\n", (long unsigned int) new_node->length); + printf("parse_raw_node: lol: by the way sizeof(uint64) = %i\n", sizeof(uint64_t)); + return NULL; + } + break; + + case BPLIST_REAL: + new_node->length = uipow(2, new_node->length); + memcpy(&new_node->realval, bpbuffer + myPos, new_node->length); // XXX: probable buffer overflow here + //new_node->realval = bpbuffer[myPos]; // why not + byte_convert((char *) &new_node->realval, sizeof(double)); + break; + + case BPLIST_DICT: /* returning a raw dict, it forward-references, so. */ + new_node->length = new_node->length * 2; // dicts lie + case BPLIST_ARRAY: /* returning a raw array, it forward-references, so. */ + new_node->intval8 = ref_size; // in arrays and dicts, the "ref size" alluded to in the trailer applies, and should be stored in intval8 so as to save space. + case BPLIST_STRING: + case BPLIST_DATA: + default: /* made to hold raw data. */ + modifier = (new_node->intval8 > 0) ? new_node->intval8 : 1; + new_node->strval = (char *) malloc(sizeof(char) * (new_node->length * modifier)); + memcpy(new_node->strval, bpbuffer + myPos, (new_node->length * modifier)); + break; + + case BPLIST_UNICODE: + new_node->unicodeval = (wchar_t *) malloc(sizeof(wchar_t) * new_node->length); + memcpy(new_node->unicodeval, bpbuffer + myPos, new_node->length); + break; } - + myPos += new_node->length; *position = myPos; return new_node; } -void print_bytes(char *val, size_t size) { +void print_bytes(char *val, size_t size) +{ int i = 0; for (i = 0; i < size; i++) { printf("Byte %i: 0x%x\n", i, val[i]); } } -bplist_node *parse_nodes(const char *bpbuffer, uint32_t bplength, uint32_t *position) { +bplist_node *parse_nodes(const char *bpbuffer, uint32_t bplength, uint32_t * position) +{ bplist_node **nodeslist = NULL, **newaddr = NULL; bplist_node *new_node = NULL, *root_node = NULL; - + uint32_t nodeslength = 0; uint8_t offset_size = 0, dict_param_size = 0; - offset_size = bpbuffer[bplength-26]; - dict_param_size = bpbuffer[bplength-25]; + offset_size = bpbuffer[bplength - 26]; + dict_param_size = bpbuffer[bplength - 25]; uint64_t current_offset = 0; //uint64_t num_objects = *(bpbuffer+(bplength-24)), root_object = *(bpbuffer+(bplength-16)), offset_table_index = *(bpbuffer+(bplength-8)); uint64_t num_objects = 0, root_object = 0, offset_table_index = 0; - memcpy(&num_objects, bpbuffer+bplength-24, sizeof(uint64_t)); - memcpy(&root_object, bpbuffer+bplength-16, sizeof(uint64_t)); - memcpy(&offset_table_index, bpbuffer+bplength-8, sizeof(uint64_t)); - byte_convert(&num_objects, sizeof(uint64_t)); - byte_convert(&root_object, sizeof(uint64_t)); - byte_convert(&offset_table_index, sizeof(uint64_t)); - - log_debug_msg("Offset size: %i\nGiven: %i\n", offset_size, bpbuffer[bplength-26]); - log_debug_msg("Ref size: %i\nGiven: %i\n", dict_param_size, bpbuffer[bplength-25]); - log_debug_msg("Number of objects: %lli\nGiven: %llu\n", num_objects, *(bpbuffer+bplength-24)); - log_debug_msg("Root object index: %lli\nGiven: %llu\n", root_object, *(bpbuffer+bplength-16)); - log_debug_msg("Offset table index: %lli\nGiven: %llu\n", offset_table_index, *(bpbuffer+bplength-8)); + memcpy(&num_objects, bpbuffer + bplength - 24, sizeof(uint64_t)); + memcpy(&root_object, bpbuffer + bplength - 16, sizeof(uint64_t)); + memcpy(&offset_table_index, bpbuffer + bplength - 8, sizeof(uint64_t)); + byte_convert((char *) &num_objects, sizeof(uint64_t)); + byte_convert((char *) &root_object, sizeof(uint64_t)); + byte_convert((char *) &offset_table_index, sizeof(uint64_t)); + + log_debug_msg("Offset size: %i\nGiven: %i\n", offset_size, bpbuffer[bplength - 26]); + log_debug_msg("Ref size: %i\nGiven: %i\n", dict_param_size, bpbuffer[bplength - 25]); + log_debug_msg("Number of objects: %lli\nGiven: %llu\n", num_objects, *(bpbuffer + bplength - 24)); + log_debug_msg("Root object index: %lli\nGiven: %llu\n", root_object, *(bpbuffer + bplength - 16)); + log_debug_msg("Offset table index: %lli\nGiven: %llu\n", offset_table_index, *(bpbuffer + bplength - 8)); log_debug_msg("Size of uint64: %i\n", sizeof(uint64_t)); - + int i = 0, j = 0, k = 0, str_i = 0, str_j = 0; uint32_t index1 = 0, index2 = 0; - - nodeslist = (bplist_node**)malloc(sizeof(bplist_node*) * num_objects); - if (!nodeslist) return NULL; + + nodeslist = (bplist_node **) malloc(sizeof(bplist_node *) * num_objects); + if (!nodeslist) + return NULL; for (i = 0; i < num_objects; i++) { - memcpy(¤t_offset, bpbuffer+(offset_table_index+(i*offset_size)), offset_size); + memcpy(¤t_offset, bpbuffer + (offset_table_index + (i * offset_size)), offset_size); //current_offset = (offset_size == 2) ? ntohs(current_offset) : (offset_size == 4) ? ntohl(current_offset) : current_offset; //if (offset_size == 8) byte_convert(¤t_offset, 8); - byte_convert(¤t_offset, (offset_size <= sizeof(current_offset)) ? offset_size : sizeof(current_offset)); + byte_convert((char *) ¤t_offset, + (offset_size <= sizeof(current_offset)) ? offset_size : sizeof(current_offset)); log_debug_msg("parse_nodes: current_offset = %x\n", current_offset); - nodeslist[i] = parse_raw_node(bpbuffer, bplength, ¤t_offset, dict_param_size); + nodeslist[i] = parse_raw_node(bpbuffer, bplength, (uint32_t *) & current_offset, dict_param_size); log_debug_msg("parse_nodes: parse_raw_node done\n"); } @@ -475,55 +484,55 @@ bplist_node *parse_nodes(const char *bpbuffer, uint32_t bplength, uint32_t *posi // set elements for dicts and arrays and leave the rest alone log_debug_msg("parse_nodes: on node %i\n", i); switch (nodeslist[i]->type) { - case BPLIST_DICT: - log_debug_msg("parse_nodes: dictionary found\n"); - nodeslist[i]->subnodes = (bplist_node*)malloc(sizeof(bplist_node) * nodeslist[i]->length); - for (j = 0; j < (nodeslist[i]->length / 2); j++) { - str_i = j * nodeslist[i]->intval8; - str_j = (j + (nodeslist[i]->length / 2)) * nodeslist[i]->intval8; - - memcpy(&index1, nodeslist[i]->strval+str_i, nodeslist[i]->intval8); - memcpy(&index2, nodeslist[i]->strval+str_j, nodeslist[i]->intval8); - //index1 = (dict_param_size == 1) ? index1 : (dict_param_size == 2) ? ntohs(index1) : (dict_param_size == 4) ? ntohl(index1) : index1; - //index2 = (dict_param_size == 1) ? index2 : (dict_param_size == 2) ? ntohs(index2) : (dict_param_size == 4) ? ntohl(index2) : index2; - byte_convert(&index1, (dict_param_size <= sizeof(index1)) ? dict_param_size : sizeof(index2)); - byte_convert(&index2, (dict_param_size <= sizeof(index2)) ? dict_param_size : sizeof(index2)); - //printf("parse_nodes: key index %i value %i\n", index1, index2); - //printf("parse_nodes: key type %x and length %i\n", nodeslist[index1]->type, nodeslist[index1]->length); - //printf("parse_nodes: value type %x and length %i\n", nodeslist[index2]->type, nodeslist[index2]->length); - nodeslist[i]->subnodes[k++] = nodeslist[index1]; - nodeslist[i]->subnodes[k++] = nodeslist[index2]; - } - - nodeslist[i]->length = nodeslist[i]->length / 2; - free(nodeslist[i]->strval); - k = 0; - break; - - case BPLIST_ARRAY: - log_debug_msg("parse_nodes: array found\n"); - nodeslist[i]->subnodes = (bplist_node*)malloc(sizeof(bplist_node) * nodeslist[i]->length); // memory allocation helps a lot when storing data - - for (j = 0; j < nodeslist[i]->length; j++) { - log_debug_msg("parse_nodes: array index %i\n", j); - str_j = j * nodeslist[i]->intval8; - //index1 = nodeslist[i]->strval[j]; - memcpy(&index1, nodeslist[i]->strval+str_j, nodeslist[i]->intval8); - log_debug_msg("parse_nodes: post-memcpy\n"); - //index1 = (dict_param_size == 1) ? index1 : (dict_param_size == 2) ? ntohs(index1) : (dict_param_size == 4) ? ntohl(index1) : index1; - byte_convert(&index1, (dict_param_size <= sizeof(index1)) ? dict_param_size : sizeof(index1)); - log_debug_msg("parse_nodes: post-ntohl\nindex1 = %i\n", index1); - nodeslist[i]->subnodes[j] = nodeslist[index1]; - log_debug_msg("parse_nodes: post-assignment\n"); - } - free(nodeslist[i]->strval); - break; - default: - //printf("lol... type %x\n", nodeslist[i]->type); - break; - } // those are the only two we need to correct for. + case BPLIST_DICT: + log_debug_msg("parse_nodes: dictionary found\n"); + nodeslist[i]->subnodes = (bplist_node **) malloc(sizeof(bplist_node) * nodeslist[i]->length); + for (j = 0; j < (nodeslist[i]->length / 2); j++) { + str_i = j * nodeslist[i]->intval8; + str_j = (j + (nodeslist[i]->length / 2)) * nodeslist[i]->intval8; + + memcpy(&index1, nodeslist[i]->strval + str_i, nodeslist[i]->intval8); + memcpy(&index2, nodeslist[i]->strval + str_j, nodeslist[i]->intval8); + //index1 = (dict_param_size == 1) ? index1 : (dict_param_size == 2) ? ntohs(index1) : (dict_param_size == 4) ? ntohl(index1) : index1; + //index2 = (dict_param_size == 1) ? index2 : (dict_param_size == 2) ? ntohs(index2) : (dict_param_size == 4) ? ntohl(index2) : index2; + byte_convert((char *) &index1, (dict_param_size <= sizeof(index1)) ? dict_param_size : sizeof(index2)); + byte_convert((char *) &index2, (dict_param_size <= sizeof(index2)) ? dict_param_size : sizeof(index2)); + //printf("parse_nodes: key index %i value %i\n", index1, index2); + //printf("parse_nodes: key type %x and length %i\n", nodeslist[index1]->type, nodeslist[index1]->length); + //printf("parse_nodes: value type %x and length %i\n", nodeslist[index2]->type, nodeslist[index2]->length); + nodeslist[i]->subnodes[k++] = nodeslist[index1]; + nodeslist[i]->subnodes[k++] = nodeslist[index2]; + } + + nodeslist[i]->length = nodeslist[i]->length / 2; + free(nodeslist[i]->strval); + k = 0; + break; + + case BPLIST_ARRAY: + log_debug_msg("parse_nodes: array found\n"); + nodeslist[i]->subnodes = (bplist_node **) malloc(sizeof(bplist_node) * nodeslist[i]->length); // memory allocation helps a lot when storing data + + for (j = 0; j < nodeslist[i]->length; j++) { + log_debug_msg("parse_nodes: array index %i\n", j); + str_j = j * nodeslist[i]->intval8; + //index1 = nodeslist[i]->strval[j]; + memcpy(&index1, nodeslist[i]->strval + str_j, nodeslist[i]->intval8); + log_debug_msg("parse_nodes: post-memcpy\n"); + //index1 = (dict_param_size == 1) ? index1 : (dict_param_size == 2) ? ntohs(index1) : (dict_param_size == 4) ? ntohl(index1) : index1; + byte_convert((char *) &index1, (dict_param_size <= sizeof(index1)) ? dict_param_size : sizeof(index1)); + log_debug_msg("parse_nodes: post-ntohl\nindex1 = %i\n", index1); + nodeslist[i]->subnodes[j] = nodeslist[index1]; + log_debug_msg("parse_nodes: post-assignment\n"); + } + free(nodeslist[i]->strval); + break; + default: + //printf("lol... type %x\n", nodeslist[i]->type); + break; + } // those are the only two we need to correct for. } - + root_node = nodeslist[root_object]; return root_node; } -- cgit v1.1-32-gdbae From 5811f92943fa81b6266e0b57d95824d1efa17120 Mon Sep 17 00:00:00 2001 From: Jonathan Beck Date: Fri, 28 Nov 2008 23:19:17 +0100 Subject: Start an abstraction of xml and binary plist --- src/plist.c | 279 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 279 insertions(+) (limited to 'src/plist.c') diff --git a/src/plist.c b/src/plist.c index 7a09b4d..1553c1c 100644 --- a/src/plist.c +++ b/src/plist.c @@ -536,3 +536,282 @@ bplist_node *parse_nodes(const char *bpbuffer, uint32_t bplength, uint32_t * pos root_node = nodeslist[root_object]; return root_node; } + +struct plist_data { + union { + char boolval; + uint8_t intval8; + uint16_t intval16; + uint32_t intval32; + uint64_t intval64; + float realval32; + double realval64; + char *strval; + wchar_t *unicodeval; + char *buff; + }; + int index; + plist_type type; +}; + +void plist_new_plist(plist_t* plist) +{ + if (*plist != NULL) return; + struct plist_data* data = (struct plist_data*)calloc(sizeof(struct plist_data), 1); + data->type = PLIST_PLIST; + *plist = g_node_new (data); +} + +void plist_new_dict_in_plist(plist_t plist, dict_t* dict) +{ + if (!plist || *dict) return; + + struct plist_data* data = (struct plist_data*)calloc(sizeof(struct plist_data), 1); + data->type = PLIST_DICT; + *dict = g_node_new (data); + g_node_append(plist, *dict); +} + +void plist_new_array_in_plist(plist_t plist, int length, plist_type type, void** values, array_t* array) +{ +} + +void plist_add_dict_element(dict_t dict, char* key, plist_type type, void* value) +{ + if (!dict || !key || !value) return; + + struct plist_data* data = (struct plist_data*)calloc(sizeof(struct plist_data), 1); + data->type = PLIST_KEY; + data->strval = strdup(key); + GNode* keynode = g_node_new (data); + g_node_append(dict, keynode); + + //now handle value + struct plist_data* val = (struct plist_data*)calloc(sizeof(struct plist_data), 1); + val->type = type; + + switch (type) { + case PLIST_BOOLEAN : val->boolval = *((char*)value); break; + case PLIST_UINT8 : val->intval8 = *((uint8_t*)value); break; + case PLIST_UINT16 : val->intval16 = *((uint16_t*)value); break; + case PLIST_UINT32 : val->intval32 = *((uint32_t*)value); break; + case PLIST_UINT64 : val->intval64 = *((uint64_t*)value); break; + case PLIST_FLOAT32 : val->realval32 = *((float*)value); break; + case PLIST_FLOAT64 : val->realval64 = *((double*)value); break; + case PLIST_STRING : val->strval = strdup((char*) value); break; + case PLIST_UNICODE : val->unicodeval = wcsdup((wchar_t*) value); break; + case PLIST_DATA : val->buff = strdup((char*) value); break; + case PLIST_ARRAY : + case PLIST_DICT : + case PLIST_DATE : + case PLIST_PLIST : + default: + break; + } + GNode* valnode = g_node_new (val); + g_node_append(dict, valnode); +} + +void plist_free(plist_t plist) +{ + g_node_destroy(plist); +} + +void node_to_xml (GNode *node, gpointer data) +{ + if (!node) return; + + struct plist_data* node_data = (struct plist_data*)node->data; + + xmlNodePtr child_node = NULL; + char isStruct = FALSE; + + gchar* tag = NULL; + gchar* val = NULL; + + switch (node_data->type) { + case PLIST_BOOLEAN : + { + if (node_data->boolval) + tag = "true"; + else + tag = "false"; + } + break; + + case PLIST_UINT8 : + tag = "integer"; + val = g_strdup_printf("%u", node_data->intval8); + break; + + case PLIST_UINT16 : + tag = "integer"; + val = g_strdup_printf("%u", node_data->intval16); + break; + + case PLIST_UINT32 : + tag = "integer"; + val = g_strdup_printf("%u", node_data->intval32); + break; + + case PLIST_UINT64 : + tag = "integer"; + val = g_strdup_printf("%lu", (long unsigned int)node_data->intval64); + break; + + case PLIST_FLOAT32 : + tag = "real"; + val = g_strdup_printf("%f", node_data->realval32); + break; + + case PLIST_FLOAT64 : + tag = "real"; + val = g_strdup_printf("%Lf", (long double)node_data->intval64); + break; + + case PLIST_STRING : + tag = "string"; + val = g_strdup(node_data->strval); + break; + + case PLIST_UNICODE : + tag = "string"; + val = g_strdup((gchar*)node_data->unicodeval); + break; + + case PLIST_KEY : + tag = "key"; + val = g_strdup((gchar*)node_data->strval); + break; + + case PLIST_DATA : + tag = "data"; + val = format_string(node_data->buff, 60, 0); + break; + case PLIST_ARRAY : + tag = "array"; + isStruct = TRUE; + break; + case PLIST_DICT : + tag = "dict"; + isStruct = TRUE; + break; + case PLIST_PLIST : + tag = "plist"; + isStruct = TRUE; + break; + case PLIST_DATE : //TODO : handle date tag + default: + break; + } + return; + + child_node = xmlNewChild(data, NULL, tag, val); + gfree(val); + + if (isStruct) + g_node_children_foreach(node, G_TRAVERSE_ALL, node_to_xml, child_node); + + return; +} + +void xml_to_node (xmlNodePtr xml_node, GNode *plist_node) +{ + xmlNodePtr node = NULL; + struct plist_data* data = (struct plist_data*)calloc(sizeof(struct plist_data), 1); + GNode* subnode = g_node_new (data); + g_node_append(plist_node, subnode); + + for (node = xml_node->children; node; node = node->next) { + + if (!xmlStrcmp(node->name, "true")) { + data->boolval = 1; + data->type = PLIST_BOOLEAN; + continue; + } + + if (!xmlStrcmp(node->name, "false")) { + data->boolval = 0; + data->type = PLIST_BOOLEAN; + continue; + } + + if (!xmlStrcmp(node->name, "integer")) { + char* strval = xmlNodeGetContent(node); + data->intval64 = atoi(strval); + data->type = PLIST_UINT64; + continue; + } + + if (!xmlStrcmp(node->name, "real")){ + char* strval = xmlNodeGetContent(node); + data->realval64 = atof(strval); + data->type = PLIST_FLOAT64; + continue; + } + + if (!xmlStrcmp(node->name, "date")) + continue;//TODO : handle date tag + + if (!xmlStrcmp(node->name, "string")) { + data->strval = strdup(xmlNodeGetContent(node)); + data->type = PLIST_STRING; + continue; + } + + if (!xmlStrcmp(node->name, "key")) { + data->strval = strdup(xmlNodeGetContent(node)); + data->type = PLIST_KEY; + continue; + } + + if (!xmlStrcmp(node->name, "data")) { + data->buff = strdup(xmlNodeGetContent(node)); + data->type = PLIST_DATA; + continue; + } + + if (!xmlStrcmp(node->name, "array")) { + data->type = PLIST_ARRAY; + xml_to_node (node, subnode); + continue; + } + + if (!xmlStrcmp(node->name, "dict")) { + data->type = PLIST_DICT; + xml_to_node (node, subnode); + continue; + } + } +} + +void plist_to_xml(plist_t plist, char** plist_xml) +{ + if (!plist || !plist_xml || *plist_xml) return; + xmlDocPtr plist_doc = new_plist(); + xmlNodePtr root_node = xmlDocGetRootElement(plist_doc); + g_node_children_foreach(plist, G_TRAVERSE_ALL, node_to_xml, root_node); + int size = 0; + xmlDocDumpMemory (plist_doc, (xmlChar**)plist_xml, &size); +} + + +void plist_to_bin(plist_t plist, char** plist_bin) +{ +} + +void xml_to_plist(const char* plist_xml, plist_t* plist) +{ + xmlDocPtr plist_doc = xmlReadMemory(plist_xml, strlen(plist_xml), NULL, NULL, 0); + xmlNodePtr root_node = xmlDocGetRootElement(plist_doc); + + struct plist_data* data = (struct plist_data*)calloc(sizeof(struct plist_data), 1); + *plist = g_node_new (data); + data->type = PLIST_PLIST; + xml_to_node (root_node, *plist); + +} + +void bin_to_plist(const char* plist_bin, plist_t* plist) +{ +} -- cgit v1.1-32-gdbae From 889cb32a1231c41762d7e2bbe6c891bd3a6c9a7d Mon Sep 17 00:00:00 2001 From: Jonathan Beck Date: Sun, 30 Nov 2008 16:36:18 +0100 Subject: Continue abstraction of xml and binary plist. --- src/plist.c | 799 +++++++++++++++++++++++++++++++++--------------------------- 1 file changed, 437 insertions(+), 362 deletions(-) (limited to 'src/plist.c') diff --git a/src/plist.c b/src/plist.c index 1553c1c..377646d 100644 --- a/src/plist.c +++ b/src/plist.c @@ -25,6 +25,7 @@ #include #include "utils.h" #include "plist.h" +#include const char *plist_base = "\n\ @@ -285,147 +286,7 @@ void byte_convert(char *address, size_t size) } } -bplist_node *parse_raw_node(const char *bpbuffer, uint32_t bplength, uint32_t * position, uint8_t ref_size) -{ - if (!position || !bpbuffer || !bplength) - return NULL; - - uint8_t modifier = 0; - bplist_node *new_node = (bplist_node *) malloc(sizeof(bplist_node)); - bplist_node *length_stupidity = NULL; - memset(new_node, 0, sizeof(bplist_node)); // initialize the new struct - - int myPos = *position; - if (myPos == bplength || (myPos + 1) == bplength) { - free(new_node); - return NULL; - } // end of string - - uint32_t length = 0; - if (!myPos) { - if (strncmp(bpbuffer, "bplist00", strlen("bplist00"))) { - return NULL; // badness! - } - myPos += strlen("bplist00"); - } - // Get the node's type. - if (bpbuffer[myPos] == BPLIST_DATE) { // handle date separately, but do it as a real - // better handling of date; basically interpret as real or double - new_node->type = BPLIST_DATE; - new_node->length = 8; // always 8 for "date" (Apple intended it, not me) - myPos++; - memcpy(&new_node->realval, bpbuffer + myPos, sizeof(new_node->realval)); - byte_convert((char *) &new_node->realval, sizeof(new_node->realval)); - myPos += new_node->length; - *position = myPos; - return new_node; - } - - new_node->type = bpbuffer[myPos] & BPLIST_MASK; - new_node->length = bpbuffer[myPos] & BPLIST_FILL; - if (!new_node->type) { - // what? check if it's a boolean. - if (bpbuffer[myPos] == BPLIST_TRUE || bpbuffer[myPos] == BPLIST_FALSE) { - // okay, so it is. Carry on. - new_node->type = bpbuffer[myPos]; - new_node->length = 0; - } else { - // er, what? we have a bad type here. Return NULL. - free(new_node); - //printf("parse_raw_node: lol type: type given %x\n", bpbuffer[myPos]); - return NULL; - } - } - - myPos++; // puts us in the data. - if (new_node->length == BPLIST_FILL) { // Data happens to contain length... - // what? you're going to make me parse an int for the length. You suck. - *position = myPos; - length_stupidity = parse_raw_node(bpbuffer, bplength, &myPos, ref_size); - switch (length_stupidity->length) { - case sizeof(uint8_t): - new_node->length = length_stupidity->intval8; - break; - case sizeof(uint16_t): - new_node->length = length_stupidity->intval16; - break; - case sizeof(uint32_t): - new_node->length = length_stupidity->intval32; - break; - case sizeof(uint64_t): - new_node->length = length_stupidity->intval64; - break; - default: - free(new_node); - free(length_stupidity); - return NULL; - } - // There, we have our fucking length now. - *position = myPos; - free(length_stupidity); // cleanup - } - // Now we're in the data. - // Error-checking sorta - if ((myPos + new_node->length) >= bplength) { - new_node->length = bplength - myPos; // truncate the object - } - // And now for the greatest show on earth: the giant fucking switch statement. - switch (new_node->type) { - case BPLIST_INT: - new_node->length = uipow(2, new_node->length); // make length less misleading - switch (new_node->length) { - case sizeof(uint8_t): - new_node->intval8 = bpbuffer[myPos]; - break; - case sizeof(uint16_t): - memcpy(&new_node->intval16, bpbuffer + myPos, sizeof(uint16_t)); - new_node->intval16 = ntohs(new_node->intval16); - break; - case sizeof(uint32_t): - memcpy(&new_node->intval32, bpbuffer + myPos, sizeof(uint32_t)); - new_node->intval32 = ntohl(new_node->intval32); - break; - case sizeof(uint64_t): - memcpy(&new_node->intval64, bpbuffer + myPos, sizeof(uint64_t)); - byte_convert((char *) &new_node->intval64, sizeof(uint64_t)); - break; - default: - free(new_node); - printf("parse_raw_node: lol: invalid int: size given %lu\n", (long unsigned int) new_node->length); - printf("parse_raw_node: lol: by the way sizeof(uint64) = %i\n", sizeof(uint64_t)); - return NULL; - } - break; - case BPLIST_REAL: - new_node->length = uipow(2, new_node->length); - memcpy(&new_node->realval, bpbuffer + myPos, new_node->length); // XXX: probable buffer overflow here - //new_node->realval = bpbuffer[myPos]; // why not - byte_convert((char *) &new_node->realval, sizeof(double)); - break; - - case BPLIST_DICT: /* returning a raw dict, it forward-references, so. */ - new_node->length = new_node->length * 2; // dicts lie - case BPLIST_ARRAY: /* returning a raw array, it forward-references, so. */ - new_node->intval8 = ref_size; // in arrays and dicts, the "ref size" alluded to in the trailer applies, and should be stored in intval8 so as to save space. - case BPLIST_STRING: - case BPLIST_DATA: - default: /* made to hold raw data. */ - modifier = (new_node->intval8 > 0) ? new_node->intval8 : 1; - new_node->strval = (char *) malloc(sizeof(char) * (new_node->length * modifier)); - memcpy(new_node->strval, bpbuffer + myPos, (new_node->length * modifier)); - break; - - case BPLIST_UNICODE: - new_node->unicodeval = (wchar_t *) malloc(sizeof(wchar_t) * new_node->length); - memcpy(new_node->unicodeval, bpbuffer + myPos, new_node->length); - break; - } - - myPos += new_node->length; - *position = myPos; - return new_node; -} void print_bytes(char *val, size_t size) { @@ -435,180 +296,123 @@ void print_bytes(char *val, size_t size) } } -bplist_node *parse_nodes(const char *bpbuffer, uint32_t bplength, uint32_t * position) -{ - bplist_node **nodeslist = NULL, **newaddr = NULL; - bplist_node *new_node = NULL, *root_node = NULL; - - uint32_t nodeslength = 0; - uint8_t offset_size = 0, dict_param_size = 0; - offset_size = bpbuffer[bplength - 26]; - dict_param_size = bpbuffer[bplength - 25]; - uint64_t current_offset = 0; - //uint64_t num_objects = *(bpbuffer+(bplength-24)), root_object = *(bpbuffer+(bplength-16)), offset_table_index = *(bpbuffer+(bplength-8)); - uint64_t num_objects = 0, root_object = 0, offset_table_index = 0; - memcpy(&num_objects, bpbuffer + bplength - 24, sizeof(uint64_t)); - memcpy(&root_object, bpbuffer + bplength - 16, sizeof(uint64_t)); - memcpy(&offset_table_index, bpbuffer + bplength - 8, sizeof(uint64_t)); - byte_convert((char *) &num_objects, sizeof(uint64_t)); - byte_convert((char *) &root_object, sizeof(uint64_t)); - byte_convert((char *) &offset_table_index, sizeof(uint64_t)); - - log_debug_msg("Offset size: %i\nGiven: %i\n", offset_size, bpbuffer[bplength - 26]); - log_debug_msg("Ref size: %i\nGiven: %i\n", dict_param_size, bpbuffer[bplength - 25]); - log_debug_msg("Number of objects: %lli\nGiven: %llu\n", num_objects, *(bpbuffer + bplength - 24)); - log_debug_msg("Root object index: %lli\nGiven: %llu\n", root_object, *(bpbuffer + bplength - 16)); - log_debug_msg("Offset table index: %lli\nGiven: %llu\n", offset_table_index, *(bpbuffer + bplength - 8)); - log_debug_msg("Size of uint64: %i\n", sizeof(uint64_t)); - - int i = 0, j = 0, k = 0, str_i = 0, str_j = 0; - uint32_t index1 = 0, index2 = 0; - - nodeslist = (bplist_node **) malloc(sizeof(bplist_node *) * num_objects); - if (!nodeslist) - return NULL; - - for (i = 0; i < num_objects; i++) { - memcpy(¤t_offset, bpbuffer + (offset_table_index + (i * offset_size)), offset_size); - //current_offset = (offset_size == 2) ? ntohs(current_offset) : (offset_size == 4) ? ntohl(current_offset) : current_offset; - //if (offset_size == 8) byte_convert(¤t_offset, 8); - byte_convert((char *) ¤t_offset, - (offset_size <= sizeof(current_offset)) ? offset_size : sizeof(current_offset)); - log_debug_msg("parse_nodes: current_offset = %x\n", current_offset); - nodeslist[i] = parse_raw_node(bpbuffer, bplength, (uint32_t *) & current_offset, dict_param_size); - log_debug_msg("parse_nodes: parse_raw_node done\n"); - } - - - for (i = 0; i < num_objects; i++) { - // set elements for dicts and arrays and leave the rest alone - log_debug_msg("parse_nodes: on node %i\n", i); - switch (nodeslist[i]->type) { - case BPLIST_DICT: - log_debug_msg("parse_nodes: dictionary found\n"); - nodeslist[i]->subnodes = (bplist_node **) malloc(sizeof(bplist_node) * nodeslist[i]->length); - for (j = 0; j < (nodeslist[i]->length / 2); j++) { - str_i = j * nodeslist[i]->intval8; - str_j = (j + (nodeslist[i]->length / 2)) * nodeslist[i]->intval8; - - memcpy(&index1, nodeslist[i]->strval + str_i, nodeslist[i]->intval8); - memcpy(&index2, nodeslist[i]->strval + str_j, nodeslist[i]->intval8); - //index1 = (dict_param_size == 1) ? index1 : (dict_param_size == 2) ? ntohs(index1) : (dict_param_size == 4) ? ntohl(index1) : index1; - //index2 = (dict_param_size == 1) ? index2 : (dict_param_size == 2) ? ntohs(index2) : (dict_param_size == 4) ? ntohl(index2) : index2; - byte_convert((char *) &index1, (dict_param_size <= sizeof(index1)) ? dict_param_size : sizeof(index2)); - byte_convert((char *) &index2, (dict_param_size <= sizeof(index2)) ? dict_param_size : sizeof(index2)); - //printf("parse_nodes: key index %i value %i\n", index1, index2); - //printf("parse_nodes: key type %x and length %i\n", nodeslist[index1]->type, nodeslist[index1]->length); - //printf("parse_nodes: value type %x and length %i\n", nodeslist[index2]->type, nodeslist[index2]->length); - nodeslist[i]->subnodes[k++] = nodeslist[index1]; - nodeslist[i]->subnodes[k++] = nodeslist[index2]; - } - - nodeslist[i]->length = nodeslist[i]->length / 2; - free(nodeslist[i]->strval); - k = 0; - break; - - case BPLIST_ARRAY: - log_debug_msg("parse_nodes: array found\n"); - nodeslist[i]->subnodes = (bplist_node **) malloc(sizeof(bplist_node) * nodeslist[i]->length); // memory allocation helps a lot when storing data - - for (j = 0; j < nodeslist[i]->length; j++) { - log_debug_msg("parse_nodes: array index %i\n", j); - str_j = j * nodeslist[i]->intval8; - //index1 = nodeslist[i]->strval[j]; - memcpy(&index1, nodeslist[i]->strval + str_j, nodeslist[i]->intval8); - log_debug_msg("parse_nodes: post-memcpy\n"); - //index1 = (dict_param_size == 1) ? index1 : (dict_param_size == 2) ? ntohs(index1) : (dict_param_size == 4) ? ntohl(index1) : index1; - byte_convert((char *) &index1, (dict_param_size <= sizeof(index1)) ? dict_param_size : sizeof(index1)); - log_debug_msg("parse_nodes: post-ntohl\nindex1 = %i\n", index1); - nodeslist[i]->subnodes[j] = nodeslist[index1]; - log_debug_msg("parse_nodes: post-assignment\n"); - } - free(nodeslist[i]->strval); - break; - default: - //printf("lol... type %x\n", nodeslist[i]->type); - break; - } // those are the only two we need to correct for. - } - root_node = nodeslist[root_object]; - return root_node; -} struct plist_data { union { - char boolval; - uint8_t intval8; + char boolval; + uint8_t intval8; uint16_t intval16; uint32_t intval32; uint64_t intval64; - float realval32; - double realval64; - char *strval; + float realval32; + double realval64; + char *strval; wchar_t *unicodeval; - char *buff; + struct { + char *buff; + uint8_t ref_size; + }; }; - int index; + uint64_t length; plist_type type; }; -void plist_new_plist(plist_t* plist) +enum { + BPLIST_TRUE = 0x08, + BPLIST_FALSE = 0x09, + BPLIST_FILL = 0x0F, /* will be used for length grabbing */ + BPLIST_INT = 0x10, + BPLIST_REAL = 0x20, + BPLIST_DATE = 0x33, + BPLIST_DATA = 0x40, + BPLIST_STRING = 0x50, + BPLIST_UNICODE = 0x60, + BPLIST_UID = 0x70, + BPLIST_ARRAY = 0xA0, + BPLIST_SET = 0xC0, + BPLIST_DICT = 0xD0, + BPLIST_MASK = 0xF0 +}; + +void plist_new_plist(plist_t * plist) { - if (*plist != NULL) return; - struct plist_data* data = (struct plist_data*)calloc(sizeof(struct plist_data), 1); + if (*plist != NULL) + return; + struct plist_data *data = (struct plist_data *) calloc(sizeof(struct plist_data), 1); data->type = PLIST_PLIST; - *plist = g_node_new (data); + *plist = g_node_new(data); } -void plist_new_dict_in_plist(plist_t plist, dict_t* dict) +void plist_new_dict_in_plist(plist_t plist, dict_t * dict) { - if (!plist || *dict) return; + if (!plist || *dict) + return; - struct plist_data* data = (struct plist_data*)calloc(sizeof(struct plist_data), 1); + struct plist_data *data = (struct plist_data *) calloc(sizeof(struct plist_data), 1); data->type = PLIST_DICT; - *dict = g_node_new (data); + *dict = g_node_new(data); g_node_append(plist, *dict); } -void plist_new_array_in_plist(plist_t plist, int length, plist_type type, void** values, array_t* array) +void plist_new_array_in_plist(plist_t plist, int length, plist_type type, void **values, array_t * array) { } -void plist_add_dict_element(dict_t dict, char* key, plist_type type, void* value) +void plist_add_dict_element(dict_t dict, char *key, plist_type type, void *value) { - if (!dict || !key || !value) return; + if (!dict || !key || !value) + return; - struct plist_data* data = (struct plist_data*)calloc(sizeof(struct plist_data), 1); + struct plist_data *data = (struct plist_data *) calloc(sizeof(struct plist_data), 1); data->type = PLIST_KEY; data->strval = strdup(key); - GNode* keynode = g_node_new (data); + GNode *keynode = g_node_new(data); g_node_append(dict, keynode); //now handle value - struct plist_data* val = (struct plist_data*)calloc(sizeof(struct plist_data), 1); + struct plist_data *val = (struct plist_data *) calloc(sizeof(struct plist_data), 1); val->type = type; switch (type) { - case PLIST_BOOLEAN : val->boolval = *((char*)value); break; - case PLIST_UINT8 : val->intval8 = *((uint8_t*)value); break; - case PLIST_UINT16 : val->intval16 = *((uint16_t*)value); break; - case PLIST_UINT32 : val->intval32 = *((uint32_t*)value); break; - case PLIST_UINT64 : val->intval64 = *((uint64_t*)value); break; - case PLIST_FLOAT32 : val->realval32 = *((float*)value); break; - case PLIST_FLOAT64 : val->realval64 = *((double*)value); break; - case PLIST_STRING : val->strval = strdup((char*) value); break; - case PLIST_UNICODE : val->unicodeval = wcsdup((wchar_t*) value); break; - case PLIST_DATA : val->buff = strdup((char*) value); break; - case PLIST_ARRAY : - case PLIST_DICT : - case PLIST_DATE : - case PLIST_PLIST : - default: - break; + case PLIST_BOOLEAN: + val->boolval = *((char *) value); + break; + case PLIST_UINT8: + val->intval8 = *((uint8_t *) value); + break; + case PLIST_UINT16: + val->intval16 = *((uint16_t *) value); + break; + case PLIST_UINT32: + val->intval32 = *((uint32_t *) value); + break; + case PLIST_UINT64: + val->intval64 = *((uint64_t *) value); + break; + case PLIST_FLOAT32: + val->realval32 = *((float *) value); + break; + case PLIST_FLOAT64: + val->realval64 = *((double *) value); + break; + case PLIST_STRING: + val->strval = strdup((char *) value); + break; + case PLIST_UNICODE: + val->unicodeval = wcsdup((wchar_t *) value); + break; + case PLIST_DATA: + val->buff = strdup((char *) value); + break; + case PLIST_ARRAY: + case PLIST_DICT: + case PLIST_DATE: + case PLIST_PLIST: + default: + break; } - GNode* valnode = g_node_new (val); + GNode *valnode = g_node_new(val); g_node_append(dict, valnode); } @@ -617,97 +421,98 @@ void plist_free(plist_t plist) g_node_destroy(plist); } -void node_to_xml (GNode *node, gpointer data) +void node_to_xml(GNode * node, gpointer data) { - if (!node) return; + if (!node) + return; - struct plist_data* node_data = (struct plist_data*)node->data; + struct plist_data *node_data = (struct plist_data *) node->data; xmlNodePtr child_node = NULL; char isStruct = FALSE; - gchar* tag = NULL; - gchar* val = NULL; + gchar *tag = NULL; + gchar *val = NULL; switch (node_data->type) { - case PLIST_BOOLEAN : - { + case PLIST_BOOLEAN: + { if (node_data->boolval) tag = "true"; else tag = "false"; - } - break; + } + break; - case PLIST_UINT8 : - tag = "integer"; - val = g_strdup_printf("%u", node_data->intval8); - break; + case PLIST_UINT8: + tag = "integer"; + val = g_strdup_printf("%u", node_data->intval8); + break; - case PLIST_UINT16 : - tag = "integer"; - val = g_strdup_printf("%u", node_data->intval16); - break; + case PLIST_UINT16: + tag = "integer"; + val = g_strdup_printf("%u", node_data->intval16); + break; - case PLIST_UINT32 : - tag = "integer"; - val = g_strdup_printf("%u", node_data->intval32); - break; + case PLIST_UINT32: + tag = "integer"; + val = g_strdup_printf("%u", node_data->intval32); + break; - case PLIST_UINT64 : - tag = "integer"; - val = g_strdup_printf("%lu", (long unsigned int)node_data->intval64); - break; + case PLIST_UINT64: + tag = "integer"; + val = g_strdup_printf("%lu", (long unsigned int) node_data->intval64); + break; - case PLIST_FLOAT32 : - tag = "real"; - val = g_strdup_printf("%f", node_data->realval32); - break; + case PLIST_FLOAT32: + tag = "real"; + val = g_strdup_printf("%f", node_data->realval32); + break; - case PLIST_FLOAT64 : - tag = "real"; - val = g_strdup_printf("%Lf", (long double)node_data->intval64); - break; + case PLIST_FLOAT64: + tag = "real"; + val = g_strdup_printf("%Lf", (long double) node_data->intval64); + break; - case PLIST_STRING : - tag = "string"; - val = g_strdup(node_data->strval); - break; + case PLIST_STRING: + tag = "string"; + val = g_strdup(node_data->strval); + break; - case PLIST_UNICODE : - tag = "string"; - val = g_strdup((gchar*)node_data->unicodeval); - break; + case PLIST_UNICODE: + tag = "string"; + val = g_strdup((gchar *) node_data->unicodeval); + break; - case PLIST_KEY : - tag = "key"; - val = g_strdup((gchar*)node_data->strval); - break; + case PLIST_KEY: + tag = "key"; + val = g_strdup((gchar *) node_data->strval); + break; - case PLIST_DATA : - tag = "data"; - val = format_string(node_data->buff, 60, 0); - break; - case PLIST_ARRAY : - tag = "array"; - isStruct = TRUE; - break; - case PLIST_DICT : - tag = "dict"; - isStruct = TRUE; - break; - case PLIST_PLIST : - tag = "plist"; - isStruct = TRUE; - break; - case PLIST_DATE : //TODO : handle date tag - default: - break; + case PLIST_DATA: + tag = "data"; + val = format_string(node_data->buff, 60, 0); + break; + case PLIST_ARRAY: + tag = "array"; + isStruct = TRUE; + break; + case PLIST_DICT: + tag = "dict"; + isStruct = TRUE; + break; + case PLIST_PLIST: + tag = "plist"; + isStruct = TRUE; + break; + case PLIST_DATE: //TODO : handle date tag + default: + break; } - return; child_node = xmlNewChild(data, NULL, tag, val); - gfree(val); + xmlNodeAddContent(child_node, "\n"); + g_free(val); if (isStruct) g_node_children_foreach(node, G_TRAVERSE_ALL, node_to_xml, child_node); @@ -715,11 +520,11 @@ void node_to_xml (GNode *node, gpointer data) return; } -void xml_to_node (xmlNodePtr xml_node, GNode *plist_node) +void xml_to_node(xmlNodePtr xml_node, GNode * plist_node) { xmlNodePtr node = NULL; - struct plist_data* data = (struct plist_data*)calloc(sizeof(struct plist_data), 1); - GNode* subnode = g_node_new (data); + struct plist_data *data = (struct plist_data *) calloc(sizeof(struct plist_data), 1); + GNode *subnode = g_node_new(data); g_node_append(plist_node, subnode); for (node = xml_node->children; node; node = node->next) { @@ -737,21 +542,21 @@ void xml_to_node (xmlNodePtr xml_node, GNode *plist_node) } if (!xmlStrcmp(node->name, "integer")) { - char* strval = xmlNodeGetContent(node); + char *strval = xmlNodeGetContent(node); data->intval64 = atoi(strval); data->type = PLIST_UINT64; continue; } - if (!xmlStrcmp(node->name, "real")){ - char* strval = xmlNodeGetContent(node); + if (!xmlStrcmp(node->name, "real")) { + char *strval = xmlNodeGetContent(node); data->realval64 = atof(strval); data->type = PLIST_FLOAT64; continue; } if (!xmlStrcmp(node->name, "date")) - continue;//TODO : handle date tag + continue; //TODO : handle date tag if (!xmlStrcmp(node->name, "string")) { data->strval = strdup(xmlNodeGetContent(node)); @@ -773,45 +578,315 @@ void xml_to_node (xmlNodePtr xml_node, GNode *plist_node) if (!xmlStrcmp(node->name, "array")) { data->type = PLIST_ARRAY; - xml_to_node (node, subnode); + xml_to_node(node, subnode); continue; } if (!xmlStrcmp(node->name, "dict")) { data->type = PLIST_DICT; - xml_to_node (node, subnode); + xml_to_node(node, subnode); continue; } } } -void plist_to_xml(plist_t plist, char** plist_xml) +void plist_to_xml(plist_t plist, char **plist_xml) { - if (!plist || !plist_xml || *plist_xml) return; + if (!plist || !plist_xml || *plist_xml) + return; xmlDocPtr plist_doc = new_plist(); xmlNodePtr root_node = xmlDocGetRootElement(plist_doc); g_node_children_foreach(plist, G_TRAVERSE_ALL, node_to_xml, root_node); int size = 0; - xmlDocDumpMemory (plist_doc, (xmlChar**)plist_xml, &size); + xmlDocDumpMemory(plist_doc, (xmlChar **) plist_xml, &size); } +GNode *parse_raw_node(const char *bpbuffer, uint32_t bplength, uint32_t * position, uint8_t ref_size) +{ + if (!position || !bpbuffer || !bplength) + return NULL; + + uint8_t modifier = 0; + struct plist_data *data = (struct plist_data *) calloc(sizeof(struct plist_data), 1); + GNode *new_node = g_node_new(data); + GNode *length_stupidity = NULL; + + int myPos = *position; + if (myPos == bplength || (myPos + 1) == bplength) { + g_node_destroy(new_node); + return NULL; + } // end of string + + uint32_t length = 0; + if (!myPos) { + if (strncmp(bpbuffer, "bplist00", strlen("bplist00"))) { + return NULL; // badness! + } + myPos += strlen("bplist00"); + } + // Get the node's type. + if (bpbuffer[myPos] == BPLIST_DATE) { // handle date separately, but do it as a real + // better handling of date; basically interpret as real or double + data->type = BPLIST_DATE; + length = 8; // always 8 for "date" (Apple intended it, not me) + myPos++; + memcpy(&data->realval64, bpbuffer + myPos, sizeof(data->realval64)); + byte_convert((char *) &data->realval64, sizeof(data->realval64)); + myPos += length; + *position = myPos; + return new_node; + } + + int type = bpbuffer[myPos] & BPLIST_MASK; + data->length = bpbuffer[myPos] & BPLIST_FILL; + + if (!type) { + // what? check if it's a boolean. + if (bpbuffer[myPos] == BPLIST_TRUE || bpbuffer[myPos] == BPLIST_FALSE) { + // okay, so it is. Carry on. + data->type = PLIST_BOOLEAN; + data->boolval = TRUE; + data->length = 0; + } else { + // er, what? we have a bad type here. Return NULL. + g_node_destroy(new_node); + //printf("parse_raw_node: lol type: type given %x\n", bpbuffer[myPos]); + return NULL; + } + } + + myPos++; // puts us in the data. + if (length == BPLIST_FILL) { // Data happens to contain length... + // what? you're going to make me parse an int for the length. You suck. + *position = myPos; + length_stupidity = parse_raw_node(bpbuffer, bplength, &myPos, ref_size); + switch (((struct plist_data *) length_stupidity->data)->type) { + case PLIST_UINT8: + data->length = ((struct plist_data *) length_stupidity->data)->intval8; + break; + case PLIST_UINT16: + data->length = ((struct plist_data *) length_stupidity->data)->intval16; + break; + case PLIST_UINT32: + data->length = ((struct plist_data *) length_stupidity->data)->intval32; + break; + case PLIST_UINT64: + data->length = ((struct plist_data *) length_stupidity->data)->intval64; + break; + default: + g_node_destroy(new_node); + g_node_destroy(length_stupidity); + return NULL; + } + // There, we have our fucking length now. + *position = myPos; + g_node_destroy(length_stupidity); // cleanup + } + // Now we're in the data. + // Error-checking sorta + if ((myPos + data->length) >= bplength) { + data->length = bplength - myPos; // truncate the object + } + // And now for the greatest show on earth: the giant fucking switch statement. + switch (type) { + case BPLIST_INT: + data->length = uipow(2, data->length); // make length less misleading + switch (data->length) { + case sizeof(uint8_t): + data->type = PLIST_UINT8; + data->intval8 = bpbuffer[myPos]; + break; + case sizeof(uint16_t): + data->type = PLIST_UINT16; + memcpy(&data->intval16, bpbuffer + myPos, sizeof(uint16_t)); + data->intval16 = ntohs(data->intval16); + break; + case sizeof(uint32_t): + data->type = PLIST_UINT32; + memcpy(&data->intval32, bpbuffer + myPos, sizeof(uint32_t)); + data->intval32 = ntohl(data->intval32); + break; + case sizeof(uint64_t): + data->type = PLIST_UINT64; + memcpy(&data->intval64, bpbuffer + myPos, sizeof(uint64_t)); + byte_convert((char *) &data->intval64, sizeof(uint64_t)); + break; + default: + g_node_destroy(new_node); + printf("parse_raw_node: lol: invalid int: size given %lu\n", (long unsigned int) length); + printf("parse_raw_node: lol: by the way sizeof(uint64) = %i\n", sizeof(uint64_t)); + return NULL; + } + break; + + case BPLIST_REAL: + data->length = uipow(2, data->length); + switch (data->length) { + case sizeof(float): + data->type = PLIST_FLOAT32; + memcpy(&data->realval32, bpbuffer + myPos, data->length); + byte_convert((char *) &data->realval32, sizeof(float)); //necessary ?? + break; + case sizeof(double): + data->type = PLIST_FLOAT64; + memcpy(&data->realval64, bpbuffer + myPos, data->length); + byte_convert((char *) &data->realval64, sizeof(double)); + break; + default: + g_node_destroy(new_node); + printf("parse_raw_node: lol: invalid real: size given %lu\n", (long unsigned int) length); + printf("parse_raw_node: lol: by the way sizeof(uint64) = %i\n", sizeof(uint64_t)); + return NULL; + } + break; + + case BPLIST_STRING: + data->type = PLIST_STRING; + data->strval = (char *) malloc(sizeof(char) * data->length); + memcpy(data->strval, bpbuffer + myPos, data->length); + break; + + case BPLIST_UNICODE: + data->type = PLIST_UNICODE; + data->unicodeval = (wchar_t *) malloc(sizeof(wchar_t) * data->length); + memcpy(data->unicodeval, bpbuffer + myPos, data->length); + break; + + case BPLIST_DICT: /* returning a raw dict, it forward-references, so. */ + data->length = data->length * 2; // dicts lie + data->type = PLIST_DICT; + + case BPLIST_ARRAY: /* returning a raw array, it forward-references, so. */ + data->ref_size = ref_size; // in arrays and dicts, the "ref size" alluded to in the trailer applies, and should be stored in intval8 so as to save space. + if (data->type == 0) + data->type = PLIST_ARRAY; + case BPLIST_DATA: + if (data->type == 0) + data->type = PLIST_DATA; + default: /* made to hold raw data. */ + modifier = (data->ref_size > 0) ? data->ref_size : 1; + data->buff = (char *) malloc(sizeof(char) * (data->length * modifier)); + memcpy(data->buff, bpbuffer + myPos, (data->length * modifier)); + break; + + } + + myPos += data->length; + *position = myPos; + return new_node; +} + +plist_t parse_nodes(const char *bpbuffer, uint32_t bplength, uint32_t * position) +{ + plist_t *nodeslist = NULL, *newaddr = NULL; + plist_t new_node = NULL, root_node = NULL; + + uint32_t nodeslength = 0; + uint8_t offset_size = 0, dict_param_size = 0; + offset_size = bpbuffer[bplength - 26]; + dict_param_size = bpbuffer[bplength - 25]; + uint64_t current_offset = 0; + uint64_t num_objects = 0, root_object = 0, offset_table_index = 0; + memcpy(&num_objects, bpbuffer + bplength - 24, sizeof(uint64_t)); + memcpy(&root_object, bpbuffer + bplength - 16, sizeof(uint64_t)); + memcpy(&offset_table_index, bpbuffer + bplength - 8, sizeof(uint64_t)); + byte_convert((char *) &num_objects, sizeof(uint64_t)); + byte_convert((char *) &root_object, sizeof(uint64_t)); + byte_convert((char *) &offset_table_index, sizeof(uint64_t)); + + log_debug_msg("Offset size: %i\nGiven: %i\n", offset_size, bpbuffer[bplength - 26]); + log_debug_msg("Ref size: %i\nGiven: %i\n", dict_param_size, bpbuffer[bplength - 25]); + log_debug_msg("Number of objects: %lli\nGiven: %llu\n", num_objects, *(bpbuffer + bplength - 24)); + log_debug_msg("Root object index: %lli\nGiven: %llu\n", root_object, *(bpbuffer + bplength - 16)); + log_debug_msg("Offset table index: %lli\nGiven: %llu\n", offset_table_index, *(bpbuffer + bplength - 8)); + log_debug_msg("Size of uint64: %i\n", sizeof(uint64_t)); + + int i = 0, j = 0, k = 0, str_i = 0, str_j = 0; + uint32_t index1 = 0, index2 = 0; + + nodeslist = (plist_t *) malloc(sizeof(plist_t) * num_objects); + if (!nodeslist) + return NULL; + + for (i = 0; i < num_objects; i++) { + memcpy(¤t_offset, bpbuffer + (offset_table_index + (i * offset_size)), offset_size); + byte_convert((char *) ¤t_offset, + (offset_size <= sizeof(current_offset)) ? offset_size : sizeof(current_offset)); + log_debug_msg("parse_nodes: current_offset = %x\n", current_offset); + nodeslist[i] = parse_raw_node(bpbuffer, bplength, (uint32_t *) & current_offset, dict_param_size); + log_debug_msg("parse_nodes: parse_raw_node done\n"); + } + + + for (i = 0; i < num_objects; i++) { + // set elements for dicts and arrays and leave the rest alone + log_debug_msg("parse_nodes: on node %i\n", i); + struct plist_data *data = (struct plist_data *) nodeslist[i]->data; + + switch (data->type) { + case PLIST_DICT: + log_debug_msg("parse_nodes: dictionary found\n"); + for (j = 0; j < (data->length / 2); j++) { + str_i = j * data->ref_size; + str_j = (j + (data->length / 2)) * data->ref_size; + + memcpy(&index1, data->buff + str_i, data->ref_size); + memcpy(&index2, data->buff + str_j, data->ref_size); + + byte_convert((char *) &index1, (dict_param_size <= sizeof(index1)) ? dict_param_size : sizeof(index2)); + byte_convert((char *) &index2, (dict_param_size <= sizeof(index2)) ? dict_param_size : sizeof(index2)); + + g_node_append(nodeslist[i], nodeslist[index1]); + g_node_append(nodeslist[i], nodeslist[index2]); + } + + data->length = data->length / 2; + free(data->buff); + k = 0; + break; + + case PLIST_ARRAY: + log_debug_msg("parse_nodes: array found\n"); + for (j = 0; j < data->length; j++) { + log_debug_msg("parse_nodes: array index %i\n", j); + str_j = j * data->ref_size; + memcpy(&index1, data->buff + str_j, data->ref_size); + log_debug_msg("parse_nodes: post-memcpy\n"); + byte_convert((char *) &index1, (dict_param_size <= sizeof(index1)) ? dict_param_size : sizeof(index1)); + log_debug_msg("parse_nodes: post-ntohl\nindex1 = %i\n", index1); + g_node_append(nodeslist[i], nodeslist[index1]); + log_debug_msg("parse_nodes: post-assignment\n"); + } + free(data->buff); + break; + default: + //printf("lol... type %x\n", nodeslist[i]->type); + break; + } // those are the only two we need to correct for. + } + + root_node = nodeslist[root_object]; + return root_node; +} -void plist_to_bin(plist_t plist, char** plist_bin) +void plist_to_bin(plist_t plist, char **plist_bin, int *length) { } -void xml_to_plist(const char* plist_xml, plist_t* plist) +void xml_to_plist(const char *plist_xml, plist_t * plist) { xmlDocPtr plist_doc = xmlReadMemory(plist_xml, strlen(plist_xml), NULL, NULL, 0); xmlNodePtr root_node = xmlDocGetRootElement(plist_doc); - struct plist_data* data = (struct plist_data*)calloc(sizeof(struct plist_data), 1); - *plist = g_node_new (data); + struct plist_data *data = (struct plist_data *) calloc(sizeof(struct plist_data), 1); + *plist = g_node_new(data); data->type = PLIST_PLIST; - xml_to_node (root_node, *plist); + xml_to_node(root_node, *plist); } -void bin_to_plist(const char* plist_bin, plist_t* plist) +void bin_to_plist(const char *plist_bin, int length, plist_t * plist) { + uint32_t pos = 0; + *plist = parse_nodes(plist_bin, length, &pos); } -- cgit v1.1-32-gdbae From d560cf5a15d1aef74e95b208ed69b7d324d94354 Mon Sep 17 00:00:00 2001 From: Jonathan Beck Date: Sun, 30 Nov 2008 21:49:56 +0100 Subject: complete xml plist abstraction and migrate lockdownd_hello to new plist API. --- src/plist.c | 141 ++++++++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 123 insertions(+), 18 deletions(-) (limited to 'src/plist.c') diff --git a/src/plist.c b/src/plist.c index 377646d..5b61570 100644 --- a/src/plist.c +++ b/src/plist.c @@ -286,8 +286,6 @@ void byte_convert(char *address, size_t size) } } - - void print_bytes(char *val, size_t size) { int i = 0; @@ -421,11 +419,17 @@ void plist_free(plist_t plist) g_node_destroy(plist); } -void node_to_xml(GNode * node, gpointer data) +struct xml_node { + xmlNodePtr xml; + uint32_t depth; +}; + +void node_to_xml(GNode * node, gpointer xml_struct) { if (!node) return; + struct xml_node *xstruct = (struct xml_node *) xml_struct; struct plist_data *node_data = (struct plist_data *) node->data; xmlNodePtr child_node = NULL; @@ -510,12 +514,31 @@ void node_to_xml(GNode * node, gpointer data) break; } - child_node = xmlNewChild(data, NULL, tag, val); - xmlNodeAddContent(child_node, "\n"); + int i = 0; + for (i = 0; i < xstruct->depth; i++) { + xmlNodeAddContent(xstruct->xml, "\t"); + } + child_node = xmlNewChild(xstruct->xml, NULL, tag, val); + xmlNodeAddContent(xstruct->xml, "\n"); g_free(val); - if (isStruct) - g_node_children_foreach(node, G_TRAVERSE_ALL, node_to_xml, child_node); + //add return for structured types + if (node_data->type == PLIST_ARRAY || + node_data->type == PLIST_DICT || node_data->type == PLIST_DATA || node_data->type == PLIST_PLIST) + xmlNodeAddContent(child_node, "\n"); + + if (isStruct) { + struct xml_node child = { child_node, xstruct->depth + 1 }; + g_node_children_foreach(node, G_TRAVERSE_ALL, node_to_xml, &child); + } + //fix indent for structured types + if (node_data->type == PLIST_ARRAY || + node_data->type == PLIST_DICT || node_data->type == PLIST_DATA || node_data->type == PLIST_PLIST) { + + for (i = 0; i < xstruct->depth; i++) { + xmlNodeAddContent(child_node, "\t"); + } + } return; } @@ -523,12 +546,18 @@ void node_to_xml(GNode * node, gpointer data) void xml_to_node(xmlNodePtr xml_node, GNode * plist_node) { xmlNodePtr node = NULL; - struct plist_data *data = (struct plist_data *) calloc(sizeof(struct plist_data), 1); - GNode *subnode = g_node_new(data); - g_node_append(plist_node, subnode); for (node = xml_node->children; node; node = node->next) { + while (node && !xmlStrcmp(node->name, "text")) + node = node->next; + if (!node) + break; + + struct plist_data *data = (struct plist_data *) calloc(sizeof(struct plist_data), 1); + GNode *subnode = g_node_new(data); + g_node_append(plist_node, subnode); + if (!xmlStrcmp(node->name, "true")) { data->boolval = 1; data->type = PLIST_BOOLEAN; @@ -590,15 +619,15 @@ void xml_to_node(xmlNodePtr xml_node, GNode * plist_node) } } -void plist_to_xml(plist_t plist, char **plist_xml) +void plist_to_xml(plist_t plist, char **plist_xml, uint32_t * length) { if (!plist || !plist_xml || *plist_xml) return; xmlDocPtr plist_doc = new_plist(); xmlNodePtr root_node = xmlDocGetRootElement(plist_doc); - g_node_children_foreach(plist, G_TRAVERSE_ALL, node_to_xml, root_node); - int size = 0; - xmlDocDumpMemory(plist_doc, (xmlChar **) plist_xml, &size); + struct xml_node root = { root_node, 0 }; + g_node_children_foreach(plist, G_TRAVERSE_ALL, node_to_xml, &root); + xmlDocDumpMemory(plist_doc, (xmlChar **) plist_xml, length); } GNode *parse_raw_node(const char *bpbuffer, uint32_t bplength, uint32_t * position, uint8_t ref_size) @@ -869,13 +898,13 @@ plist_t parse_nodes(const char *bpbuffer, uint32_t bplength, uint32_t * position return root_node; } -void plist_to_bin(plist_t plist, char **plist_bin, int *length) +void plist_to_bin(plist_t plist, char **plist_bin, uint32_t * length) { } -void xml_to_plist(const char *plist_xml, plist_t * plist) +void xml_to_plist(const char *plist_xml, uint32_t length, plist_t * plist) { - xmlDocPtr plist_doc = xmlReadMemory(plist_xml, strlen(plist_xml), NULL, NULL, 0); + xmlDocPtr plist_doc = xmlReadMemory(plist_xml, length, NULL, NULL, 0); xmlNodePtr root_node = xmlDocGetRootElement(plist_doc); struct plist_data *data = (struct plist_data *) calloc(sizeof(struct plist_data), 1); @@ -885,8 +914,84 @@ void xml_to_plist(const char *plist_xml, plist_t * plist) } -void bin_to_plist(const char *plist_bin, int length, plist_t * plist) +void bin_to_plist(const char *plist_bin, uint32_t length, plist_t * plist) { uint32_t pos = 0; *plist = parse_nodes(plist_bin, length, &pos); } + + +GNode *find_query_node(plist_t plist, char *key, char *request) +{ + if (!plist) + return NULL; + + GNode *current = NULL; + for (current = plist->children; current; current = current->next) { + + struct plist_data *data = (struct plist_data *) current->data; + + if (data->type == PLIST_KEY && !strcmp(data->strval, key) && current->next) { + + data = (struct plist_data *) current->next->data; + if (data->type == PLIST_STRING && !strcmp(data->strval, request)) + return current->next; + } + if (data->type == PLIST_DICT || data->type == PLIST_ARRAY || data->type == PLIST_PLIST) { + GNode *sub = find_query_node(current, key, request); + if (sub) + return sub; + } + } + return NULL; +} + +void get_type_and_value(GNode * node, plist_type * type, void *value) +{ + if (!node) + return; + + struct plist_data *data = (struct plist_data *) node->data; + + *type = data->type; + + switch (*type) { + case PLIST_BOOLEAN: + *((char *) value) = data->boolval; + break; + case PLIST_UINT8: + *((uint8_t *) value) = data->intval8; + break; + case PLIST_UINT16: + *((uint16_t *) value) = data->intval16; + break; + case PLIST_UINT32: + *((uint32_t *) value) = data->intval32; + break; + case PLIST_UINT64: + *((uint64_t *) value) = data->intval64; + break; + case PLIST_FLOAT32: + *((float *) value) = data->realval32; + break; + case PLIST_FLOAT64: + *((double *) value) = data->realval64; + break; + case PLIST_STRING: + *((char **) value) = strdup(data->strval); + break; + case PLIST_UNICODE: + *((wchar_t **) value) = wcsdup(data->unicodeval); + break; + case PLIST_KEY: + *((char **) value) = strdup(data->strval); + break; + case PLIST_DATA: + case PLIST_ARRAY: + case PLIST_DICT: + case PLIST_DATE: + case PLIST_PLIST: + default: + break; + } +} -- cgit v1.1-32-gdbae From 505c97582b53ed406169f931a49ee6f678b19b52 Mon Sep 17 00:00:00 2001 From: Jonathan Beck Date: Mon, 1 Dec 2008 20:25:12 +0100 Subject: continue migration to new plist API. --- src/plist.c | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) (limited to 'src/plist.c') diff --git a/src/plist.c b/src/plist.c index 5b61570..1c00cc6 100644 --- a/src/plist.c +++ b/src/plist.c @@ -946,6 +946,73 @@ GNode *find_query_node(plist_t plist, char *key, char *request) return NULL; } +char compare_node_value(plist_type type, struct plist_data *data, void *value) +{ + char res = FALSE; + switch (type) { + case PLIST_BOOLEAN: + res = data->boolval == *((char *) value) ? TRUE : FALSE; + break; + case PLIST_UINT8: + res = data->intval8 == *((uint8_t *) value) ? TRUE : FALSE; + break; + case PLIST_UINT16: + res = data->intval16 == *((uint16_t *) value) ? TRUE : FALSE; + break; + case PLIST_UINT32: + res = data->intval32 == *((uint32_t *) value) ? TRUE : FALSE; + break; + case PLIST_UINT64: + res = data->intval64 == *((uint64_t *) value) ? TRUE : FALSE; + break; + case PLIST_FLOAT32: + res = data->realval32 == *((float *) value) ? TRUE : FALSE; + break; + case PLIST_FLOAT64: + res = data->realval64 == *((double *) value) ? TRUE : FALSE; + break; + case PLIST_KEY: + case PLIST_STRING: + res = !strcmp(data->strval, ((char *) value)); + break; + case PLIST_UNICODE: + res = !wcscmp(data->unicodeval, ((wchar_t *) value)); + break; + case PLIST_DATA: + res = !strcmp(data->buff, ((char *) value)); + break; + case PLIST_ARRAY: + case PLIST_DICT: + case PLIST_DATE: + case PLIST_PLIST: + default: + break; + } + return res; +} + +GNode *find_node(plist_t plist, plist_type type, void *value) +{ + if (!plist) + return NULL; + + GNode *current = NULL; + for (current = plist->children; current; current = current->next) { + + struct plist_data *data = (struct plist_data *) current->data; + + if (data->type == type && compare_node_value(type, data, value)) { + return current; + } + if (data->type == PLIST_DICT || data->type == PLIST_ARRAY || data->type == PLIST_PLIST) { + GNode *sub = find_node(current, type, value); + if (sub) + return sub; + } + } + return NULL; +} + void get_type_and_value(GNode * node, plist_type * type, void *value) { if (!node) -- cgit v1.1-32-gdbae From 7563917755cf58cee80fbd5bc56a1ab0f563963a Mon Sep 17 00:00:00 2001 From: Jonathan Beck Date: Mon, 1 Dec 2008 21:23:58 +0100 Subject: cleanup unused functions. --- src/plist.c | 153 ++++-------------------------------------------------------- 1 file changed, 8 insertions(+), 145 deletions(-) (limited to 'src/plist.c') diff --git a/src/plist.c b/src/plist.c index 1c00cc6..5d8fc0e 100644 --- a/src/plist.c +++ b/src/plist.c @@ -101,151 +101,6 @@ void free_plist(xmlDocPtr plist) xmlFreeDoc(plist); } -/** Adds a new node as a child to a given node. - * - * This is a lower level function so you probably want to use - * add_key_str_dict_element, add_key_dict_node or add_key_data_dict_element - * instead. - * - * @param plist The plist XML document to which the to_node belongs. - * @param name The name of the new node. - * @param content The string containing the text node of the new node. - * @param to_node The node to attach the child node to. If none is given, the - * root node of the given document is used. - * @param depth The number of tabs to indent the new node. - * - * @return The newly created node. - */ -xmlNode *add_child_to_plist(xmlDocPtr plist, const char *name, const char *content, xmlNode * to_node, int depth) -{ - int i = 0; - xmlNode *child; - - if (!plist) - return NULL; - assert(depth >= 0); - if (!to_node) - to_node = xmlDocGetRootElement(plist); - - for (i = 0; i < depth; i++) { - xmlNodeAddContent(to_node, "\t"); - } - child = xmlNewChild(to_node, NULL, name, content); - xmlNodeAddContent(to_node, "\n"); - - return child; -} - -/** Adds a string key-pair to a plist XML document. - * - * @param plist The plist XML document to add the new node to. - * @param dict The dictionary node within the plist XML document to add the new node to. - * @param key The string containing the key value. - * @param value The string containing the value. - * @param depth The number of tabs to indent the new node. - * - * @return The newly created key node. - */ -xmlNode *add_key_str_dict_element(xmlDocPtr plist, xmlNode * dict, const char *key, const char *value, int depth) -{ - xmlNode *keyPtr; - - keyPtr = add_child_to_plist(plist, "key", key, dict, depth); - add_child_to_plist(plist, "string", value, dict, depth); - - return keyPtr; -} - -/** Adds a new dictionary key-pair to a plist XML document. - * - * @param plist The plist XML document to add the new node to. - * @param dict The dictionary node within the plist XML document to add the new node to. - * @param key The string containing the key value. - * @param value The string containing the value. - * @param depth The number of tabs to indent the new node. - * - * @return The newly created dict node. - */ -xmlNode *add_key_dict_node(xmlDocPtr plist, xmlNode * dict, const char *key, const char *value, int depth) -{ - xmlNode *child; - - add_child_to_plist(plist, "key", key, dict, depth); - child = add_child_to_plist(plist, "dict", value, dict, depth); - - return child; -} - -/** Adds a new data dictionary key-pair to a plist XML document. - * - * @param plist The plist XML document to add the new node to. - * @param dict The dictionary node within the plist XML document to add the new node to. - * @param key The string containing the key value. - * @param value The string containing the value. - * @param depth The number of tabs to indent the new node. - * - * @return The newly created key node. - */ -xmlNode *add_key_data_dict_element(xmlDocPtr plist, xmlNode * dict, const char *key, const char *value, int depth) -{ - xmlNode *keyPtr; - - keyPtr = add_child_to_plist(plist, "key", key, dict, depth); - add_child_to_plist(plist, "data", format_string(value, 60, depth), dict, depth); - - return keyPtr; -} - -/** Reads a set of keys and strings into an array from a plist XML document. - * - * @param dict The root XMLNode of a plist XML document to be read. - * - * @return An array where each even number is a key and the odd numbers are - * values. If the odd number is \0, that's the end of the list. - */ -char **read_dict_element_strings(xmlNode * dict) -{ - char **return_me = NULL, **old = NULL; - int current_length = 0; - int current_pos = 0; - xmlNode *dict_walker; - - for (dict_walker = dict->children; dict_walker; dict_walker = dict_walker->next) { - if (!xmlStrcmp(dict_walker->name, "key")) { - current_length += 2; - old = return_me; - return_me = realloc(return_me, sizeof(char *) * current_length); - if (!return_me) { - free(old); - return NULL; - } - return_me[current_pos++] = xmlNodeGetContent(dict_walker); - return_me[current_pos++] = xmlNodeGetContent(dict_walker->next->next); - } - } - - old = return_me; - return_me = realloc(return_me, sizeof(char *) * (current_length + 1)); - return_me[current_pos] = NULL; - - return return_me; -} - -/** Destroys a dictionary as returned by read_dict_element_strings - */ -void free_dictionary(char **dictionary) -{ - int i = 0; - - if (!dictionary) - return; - - for (i = 0; dictionary[i]; i++) { - free(dictionary[i]); - } - - free(dictionary); -} /* * Binary propertylist code follows @@ -357,6 +212,14 @@ void plist_new_array_in_plist(plist_t plist, int length, plist_type type, void * { } +/** Adds a new key pair to a dict. + * + * @param dict The dict node in the plist. + * @param key the key name of the key pair. + * @param type The the type of the value in the key pair. + * @param value a pointer to the actual buffer containing the value. WARNING : the buffer is supposed to match the type of the value + * + */ void plist_add_dict_element(dict_t dict, char *key, plist_type type, void *value) { if (!dict || !key || !value) -- cgit v1.1-32-gdbae From 1a06347d27ca51283de3a9ff21e138a3ea9ba9b6 Mon Sep 17 00:00:00 2001 From: Jonathan Beck Date: Mon, 8 Dec 2008 22:47:02 +0100 Subject: cleanup binary parsing and move stuff around. --- src/plist.c | 854 +++++++++++++++++++++++++++++++++--------------------------- 1 file changed, 471 insertions(+), 383 deletions(-) (limited to 'src/plist.c') diff --git a/src/plist.c b/src/plist.c index 5d8fc0e..c691c16 100644 --- a/src/plist.c +++ b/src/plist.c @@ -19,19 +19,24 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ -#include -#include + #include #include #include "utils.h" #include "plist.h" #include +/********************************************** +* * +* Abstract Plist stuff * +* * +**********************************************/ + + + + + -const char *plist_base = "\n\ -\n\ -\n\ -\0"; /** Formats a block of text to be a given indentation and width. * @@ -72,34 +77,7 @@ char *format_string(const char *buf, int cols, int depth) return new_buf; } -/** Creates a new plist XML document. - * - * @return The plist XML document. - */ -xmlDocPtr new_plist() -{ - char *plist = strdup(plist_base); - xmlDocPtr plist_xml = xmlReadMemory(plist, strlen(plist), NULL, NULL, 0); - - if (!plist_xml) - return NULL; - - free(plist); - - return plist_xml; -} - -/** Destroys a previously created XML document. - * - * @param plist The XML document to destroy. - */ -void free_plist(xmlDocPtr plist) -{ - if (!plist) - return; - xmlFreeDoc(plist); -} /* @@ -117,29 +95,7 @@ void free_plist(xmlDocPtr plist) * - parse_nodes() will return the first node it encounters, which is usually the "root" node. */ -uint32_t uipow(uint32_t value, uint32_t power) -{ - if (!power) - return 1; - int i = 0, oVal = value; - for (i = 1; i < power; i++) { - value *= oVal; - } - return value; -} -void byte_convert(char *address, size_t size) -{ - int i = 0, j = 0; - char tmp = '\0'; - - for (i = 0; i < (size / 2); i++) { - tmp = address[i]; - j = ((size - 1) + 0) - i; - address[i] = address[j]; - address[j] = tmp; - } -} void print_bytes(char *val, size_t size) { @@ -154,39 +110,17 @@ void print_bytes(char *val, size_t size) struct plist_data { union { char boolval; - uint8_t intval8; - uint16_t intval16; - uint32_t intval32; - uint64_t intval64; - float realval32; - double realval64; + uint64_t intval; + double realval; char *strval; wchar_t *unicodeval; - struct { - char *buff; - uint8_t ref_size; - }; + char *buff; }; uint64_t length; plist_type type; }; -enum { - BPLIST_TRUE = 0x08, - BPLIST_FALSE = 0x09, - BPLIST_FILL = 0x0F, /* will be used for length grabbing */ - BPLIST_INT = 0x10, - BPLIST_REAL = 0x20, - BPLIST_DATE = 0x33, - BPLIST_DATA = 0x40, - BPLIST_STRING = 0x50, - BPLIST_UNICODE = 0x60, - BPLIST_UID = 0x70, - BPLIST_ARRAY = 0xA0, - BPLIST_SET = 0xC0, - BPLIST_DICT = 0xD0, - BPLIST_MASK = 0xF0 -}; + void plist_new_plist(plist_t * plist) { @@ -239,23 +173,11 @@ void plist_add_dict_element(dict_t dict, char *key, plist_type type, void *value case PLIST_BOOLEAN: val->boolval = *((char *) value); break; - case PLIST_UINT8: - val->intval8 = *((uint8_t *) value); - break; - case PLIST_UINT16: - val->intval16 = *((uint16_t *) value); - break; - case PLIST_UINT32: - val->intval32 = *((uint32_t *) value); - break; - case PLIST_UINT64: - val->intval64 = *((uint64_t *) value); + case PLIST_UINT: + val->intval = *((uint64_t *) value); break; - case PLIST_FLOAT32: - val->realval32 = *((float *) value); - break; - case PLIST_FLOAT64: - val->realval64 = *((double *) value); + case PLIST_REAL: + val->realval = *((double *) value); break; case PLIST_STRING: val->strval = strdup((char *) value); @@ -282,11 +204,55 @@ void plist_free(plist_t plist) g_node_destroy(plist); } +/********************************************** +* * +* Xml Plist stuff * +* * +**********************************************/ + +#include +#include + + +const char *plist_base = "\n\ +\n\ +\n\ +\0"; + struct xml_node { xmlNodePtr xml; uint32_t depth; }; +/** Creates a new plist XML document. + * + * @return The plist XML document. + */ +xmlDocPtr new_plist() +{ + char *plist = strdup(plist_base); + xmlDocPtr plist_xml = xmlReadMemory(plist, strlen(plist), NULL, NULL, 0); + + if (!plist_xml) + return NULL; + + free(plist); + + return plist_xml; +} + +/** Destroys a previously created XML document. + * + * @param plist The XML document to destroy. + */ +void free_plist(xmlDocPtr plist) +{ + if (!plist) + return; + + xmlFreeDoc(plist); +} + void node_to_xml(GNode * node, gpointer xml_struct) { if (!node) @@ -311,34 +277,14 @@ void node_to_xml(GNode * node, gpointer xml_struct) } break; - case PLIST_UINT8: - tag = "integer"; - val = g_strdup_printf("%u", node_data->intval8); - break; - - case PLIST_UINT16: - tag = "integer"; - val = g_strdup_printf("%u", node_data->intval16); - break; - - case PLIST_UINT32: - tag = "integer"; - val = g_strdup_printf("%u", node_data->intval32); - break; - - case PLIST_UINT64: + case PLIST_UINT: tag = "integer"; - val = g_strdup_printf("%lu", (long unsigned int) node_data->intval64); - break; - - case PLIST_FLOAT32: - tag = "real"; - val = g_strdup_printf("%f", node_data->realval32); + val = g_strdup_printf("%lu", (long unsigned int) node_data->intval); break; - case PLIST_FLOAT64: + case PLIST_REAL: tag = "real"; - val = g_strdup_printf("%Lf", (long double) node_data->intval64); + val = g_strdup_printf("%Lf", (long double) node_data->realval); break; case PLIST_STRING: @@ -358,7 +304,7 @@ void node_to_xml(GNode * node, gpointer xml_struct) case PLIST_DATA: tag = "data"; - val = format_string(node_data->buff, 60, 0); + val = format_string(node_data->buff, 60, xstruct->depth); break; case PLIST_ARRAY: tag = "array"; @@ -435,15 +381,15 @@ void xml_to_node(xmlNodePtr xml_node, GNode * plist_node) if (!xmlStrcmp(node->name, "integer")) { char *strval = xmlNodeGetContent(node); - data->intval64 = atoi(strval); - data->type = PLIST_UINT64; + data->intval = atoi(strval); + data->type = PLIST_UINT; continue; } if (!xmlStrcmp(node->name, "real")) { char *strval = xmlNodeGetContent(node); - data->realval64 = atof(strval); - data->type = PLIST_FLOAT64; + data->realval = atof(strval); + data->type = PLIST_REAL; continue; } @@ -493,294 +439,460 @@ void plist_to_xml(plist_t plist, char **plist_xml, uint32_t * length) xmlDocDumpMemory(plist_doc, (xmlChar **) plist_xml, length); } -GNode *parse_raw_node(const char *bpbuffer, uint32_t bplength, uint32_t * position, uint8_t ref_size) +void xml_to_plist(const char *plist_xml, uint32_t length, plist_t * plist) { - if (!position || !bpbuffer || !bplength) - return NULL; + xmlDocPtr plist_doc = xmlReadMemory(plist_xml, length, NULL, NULL, 0); + xmlNodePtr root_node = xmlDocGetRootElement(plist_doc); - uint8_t modifier = 0; struct plist_data *data = (struct plist_data *) calloc(sizeof(struct plist_data), 1); - GNode *new_node = g_node_new(data); - GNode *length_stupidity = NULL; + *plist = g_node_new(data); + data->type = PLIST_PLIST; + xml_to_node(root_node, *plist); +} - int myPos = *position; - if (myPos == bplength || (myPos + 1) == bplength) { - g_node_destroy(new_node); - return NULL; - } // end of string - uint32_t length = 0; - if (!myPos) { - if (strncmp(bpbuffer, "bplist00", strlen("bplist00"))) { - return NULL; // badness! - } - myPos += strlen("bplist00"); - } - // Get the node's type. - if (bpbuffer[myPos] == BPLIST_DATE) { // handle date separately, but do it as a real - // better handling of date; basically interpret as real or double - data->type = BPLIST_DATE; - length = 8; // always 8 for "date" (Apple intended it, not me) - myPos++; - memcpy(&data->realval64, bpbuffer + myPos, sizeof(data->realval64)); - byte_convert((char *) &data->realval64, sizeof(data->realval64)); - myPos += length; - *position = myPos; - return new_node; - } - int type = bpbuffer[myPos] & BPLIST_MASK; - data->length = bpbuffer[myPos] & BPLIST_FILL; +/********************************************** +* * +* Binary Plist stuff * +* * +**********************************************/ - if (!type) { - // what? check if it's a boolean. - if (bpbuffer[myPos] == BPLIST_TRUE || bpbuffer[myPos] == BPLIST_FALSE) { - // okay, so it is. Carry on. - data->type = PLIST_BOOLEAN; - data->boolval = TRUE; - data->length = 0; - } else { - // er, what? we have a bad type here. Return NULL. - g_node_destroy(new_node); - //printf("parse_raw_node: lol type: type given %x\n", bpbuffer[myPos]); - return NULL; - } - } +/* Magic marker and size. */ +#define BPLIST_MAGIC "bplist" +#define BPLIST_MAGIC_SIZE 6 - myPos++; // puts us in the data. - if (length == BPLIST_FILL) { // Data happens to contain length... - // what? you're going to make me parse an int for the length. You suck. - *position = myPos; - length_stupidity = parse_raw_node(bpbuffer, bplength, &myPos, ref_size); - switch (((struct plist_data *) length_stupidity->data)->type) { - case PLIST_UINT8: - data->length = ((struct plist_data *) length_stupidity->data)->intval8; - break; - case PLIST_UINT16: - data->length = ((struct plist_data *) length_stupidity->data)->intval16; - break; - case PLIST_UINT32: - data->length = ((struct plist_data *) length_stupidity->data)->intval32; - break; - case PLIST_UINT64: - data->length = ((struct plist_data *) length_stupidity->data)->intval64; - break; - default: - g_node_destroy(new_node); - g_node_destroy(length_stupidity); - return NULL; - } - // There, we have our fucking length now. - *position = myPos; - g_node_destroy(length_stupidity); // cleanup +#define BPLIST_VERSION "00" +#define BPLIST_VERSION_SIZE 2 + + +#define BPLIST_TRL_SIZE 26 +#define BPLIST_TRL_OFFSIZE_IDX 0 +#define BPLIST_TRL_PARMSIZE_IDX 1 +#define BPLIST_TRL_NUMOBJ_IDX 2 +#define BPLIST_TRL_ROOTOBJ_IDX 10 +#define BPLIST_TRL_OFFTAB_IDX 18 + +enum { + BPLIST_NULL = 0x00, + BPLIST_TRUE = 0x08, + BPLIST_FALSE = 0x09, + BPLIST_FILL = 0x0F, /* will be used for length grabbing */ + BPLIST_UINT = 0x10, + BPLIST_REAL = 0x20, + BPLIST_DATE = 0x30, + BPLIST_DATA = 0x40, + BPLIST_STRING = 0x50, + BPLIST_UNICODE = 0x60, + BPLIST_UID = 0x70, + BPLIST_ARRAY = 0xA0, + BPLIST_SET = 0xC0, + BPLIST_DICT = 0xD0, + BPLIST_MASK = 0xF0 +}; + +void byte_convert(char *address, size_t size) +{ + int i = 0, j = 0; + char tmp = '\0'; + + for (i = 0; i < (size / 2); i++) { + tmp = address[i]; + j = ((size - 1) + 0) - i; + address[i] = address[j]; + address[j] = tmp; } - // Now we're in the data. - // Error-checking sorta - if ((myPos + data->length) >= bplength) { - data->length = bplength - myPos; // truncate the object +} + +#include +#define swap_n_bytes(x, n) \ + n == 8 ? bswap_64(*(uint64_t *)(x)) : \ + (n == 4 ? bswap_32(*(uint32_t *)(x)) : \ + (n == 2 ? bswap_16(*(uint16_t *)(x)) : *(x) )) + +#define be64dec(x) bswap_64( *(uint64_t*)(x) ) + +GNode *parse_uint_node(char *bnode, uint8_t size, char **next_object) +{ + struct plist_data *data = (struct plist_data *) calloc(sizeof(struct plist_data), 1); + + size = 1 << size; // make length less misleading + switch (size) { + case sizeof(uint8_t): + data->intval = bnode[0]; + break; + case sizeof(uint16_t): + memcpy(&data->intval, bnode, size); + data->intval = ntohs(data->intval); + break; + case sizeof(uint32_t): + memcpy(&data->intval, bnode, size); + data->intval = ntohl(data->intval); + break; + case sizeof(uint64_t): + memcpy(&data->intval, bnode, size); + byte_convert((char *) &data->intval, size); + break; + default: + free(data); + return NULL; + }; + + *next_object = bnode + size; + data->type = PLIST_UINT; + return g_node_new(data); +} + +GNode *parse_real_node(char *bnode, uint8_t size) +{ + struct plist_data *data = (struct plist_data *) calloc(sizeof(struct plist_data), 1); + + size = 1 << size; // make length less misleading + switch (size) { + case sizeof(float): + memcpy(&data->realval, bnode, size); + byte_convert((char *) &data->realval, size); + break; + case sizeof(double): + memcpy(&data->realval, bnode, size); + byte_convert((char *) &data->realval, size); + break; + default: + free(data); + return NULL; } - // And now for the greatest show on earth: the giant fucking switch statement. + data->type = PLIST_REAL; + return g_node_new(data); +} + +GNode *parse_string_node(char *bnode, uint8_t size) +{ + struct plist_data *data = (struct plist_data *) calloc(sizeof(struct plist_data), 1); + + data->type = PLIST_STRING; + data->strval = (char *) malloc(sizeof(char) * (size + 1)); + memcpy(data->strval, bnode, size); + data->strval[size] = '\0'; + + return g_node_new(data); +} + +GNode *parse_unicode_node(char *bnode, uint8_t size) +{ + struct plist_data *data = (struct plist_data *) calloc(sizeof(struct plist_data), 1); + + data->type = PLIST_UNICODE; + data->unicodeval = (wchar_t *) malloc(sizeof(wchar_t) * (size + 1)); + memcpy(data->unicodeval, bnode, size); + data->unicodeval[size] = '\0'; + + return g_node_new(data); +} + +GNode *parse_data_node(char *bnode, uint64_t size, uint32_t ref_size) +{ + struct plist_data *data = (struct plist_data *) calloc(sizeof(struct plist_data), 1); + + data->type = PLIST_DATA; + data->length = size; + data->buff = (char *) malloc(sizeof(char) * size); + memcpy(data->buff, bnode, sizeof(char) * size); + + return g_node_new(data); +} + +GNode *parse_dict_node(char *bnode, uint64_t size, uint32_t ref_size) +{ + struct plist_data *data = (struct plist_data *) calloc(sizeof(struct plist_data), 1); + + data->type = PLIST_DICT; + data->length = size; + data->buff = (char *) malloc(sizeof(char) * size * ref_size * 2); + memcpy(data->buff, bnode, sizeof(char) * size * ref_size * 2); + + return g_node_new(data); +} + +GNode *parse_array_node(char *bnode, uint64_t size, uint32_t ref_size) +{ + struct plist_data *data = (struct plist_data *) calloc(sizeof(struct plist_data), 1); + + data->type = PLIST_ARRAY; + data->length = size; + data->buff = (char *) malloc(sizeof(char) * size * ref_size); + memcpy(data->buff, bnode, sizeof(char) * size * ref_size); + + return g_node_new(data); +} + +plist_type plist_get_node_type(plist_t node) +{ + return ((struct plist_data *) node->data)->type; +} + +uint64_t plist_get_node_uint_val(plist_t node) +{ + if (PLIST_UINT == plist_get_node_type(node)) + return ((struct plist_data *) node->data)->intval; + else + return 0; +} + + +GNode *parse_bin_node(char *object, uint8_t dict_size, char **next_object) +{ + if (!object) + return NULL; + + uint16_t type = *object & 0xF0; + uint64_t size = *object & 0x0F; + object++; + switch (type) { - case BPLIST_INT: - data->length = uipow(2, data->length); // make length less misleading - switch (data->length) { - case sizeof(uint8_t): - data->type = PLIST_UINT8; - data->intval8 = bpbuffer[myPos]; - break; - case sizeof(uint16_t): - data->type = PLIST_UINT16; - memcpy(&data->intval16, bpbuffer + myPos, sizeof(uint16_t)); - data->intval16 = ntohs(data->intval16); - break; - case sizeof(uint32_t): - data->type = PLIST_UINT32; - memcpy(&data->intval32, bpbuffer + myPos, sizeof(uint32_t)); - data->intval32 = ntohl(data->intval32); - break; - case sizeof(uint64_t): - data->type = PLIST_UINT64; - memcpy(&data->intval64, bpbuffer + myPos, sizeof(uint64_t)); - byte_convert((char *) &data->intval64, sizeof(uint64_t)); - break; + + case BPLIST_NULL: + switch (size) { + + case BPLIST_TRUE: + { + struct plist_data *data = (struct plist_data *) calloc(sizeof(struct plist_data), 1); + data->type = PLIST_BOOLEAN; + data->boolval = TRUE; + return g_node_new(data); + } + + case BPLIST_FALSE: + { + struct plist_data *data = (struct plist_data *) calloc(sizeof(struct plist_data), 1); + data->type = PLIST_BOOLEAN; + data->boolval = FALSE; + return g_node_new(data); + } + + case BPLIST_NULL: default: - g_node_destroy(new_node); - printf("parse_raw_node: lol: invalid int: size given %lu\n", (long unsigned int) length); - printf("parse_raw_node: lol: by the way sizeof(uint64) = %i\n", sizeof(uint64_t)); return NULL; } - break; + + case BPLIST_UINT: + return parse_uint_node(object, size, next_object); case BPLIST_REAL: - data->length = uipow(2, data->length); - switch (data->length) { - case sizeof(float): - data->type = PLIST_FLOAT32; - memcpy(&data->realval32, bpbuffer + myPos, data->length); - byte_convert((char *) &data->realval32, sizeof(float)); //necessary ?? - break; - case sizeof(double): - data->type = PLIST_FLOAT64; - memcpy(&data->realval64, bpbuffer + myPos, data->length); - byte_convert((char *) &data->realval64, sizeof(double)); - break; - default: - g_node_destroy(new_node); - printf("parse_raw_node: lol: invalid real: size given %lu\n", (long unsigned int) length); - printf("parse_raw_node: lol: by the way sizeof(uint64) = %i\n", sizeof(uint64_t)); + return parse_real_node(object, size); + + case BPLIST_DATE: + if (3 != size) return NULL; + else + return parse_real_node(object, size); + + case BPLIST_DATA: + if (0x0F == size) { + plist_t size_node = parse_bin_node(object, dict_size, &object); + if (plist_get_node_type(size_node) != PLIST_UINT) + return NULL; + size = plist_get_node_uint_val(size_node); } - break; + return parse_data_node(object, size, dict_size); case BPLIST_STRING: - data->type = PLIST_STRING; - data->strval = (char *) malloc(sizeof(char) * data->length); - memcpy(data->strval, bpbuffer + myPos, data->length); - break; + if (0x0F == size) { + plist_t size_node = parse_bin_node(object, dict_size, &object); + if (plist_get_node_type(size_node) != PLIST_UINT) + return NULL; + size = plist_get_node_uint_val(size_node); + } + return parse_string_node(object, size); case BPLIST_UNICODE: - data->type = PLIST_UNICODE; - data->unicodeval = (wchar_t *) malloc(sizeof(wchar_t) * data->length); - memcpy(data->unicodeval, bpbuffer + myPos, data->length); - break; + if (0x0F == size) { + plist_t size_node = parse_bin_node(object, dict_size, &object); + if (plist_get_node_type(size_node) != PLIST_UINT) + return NULL; + size = plist_get_node_uint_val(size_node); + } + return parse_unicode_node(object, size); + + case BPLIST_UID: + case BPLIST_ARRAY: + if (0x0F == size) { + plist_t size_node = parse_bin_node(object, dict_size, &object); + if (plist_get_node_type(size_node) != PLIST_UINT) + return NULL; + size = plist_get_node_uint_val(size_node); + } + return parse_array_node(object, size, dict_size); + + case BPLIST_SET: + case BPLIST_DICT: + if (0x0F == size) { + plist_t size_node = parse_bin_node(object, dict_size, &object); + if (plist_get_node_type(size_node) != PLIST_UINT) + return NULL; + object++; + size = plist_get_node_uint_val(size_node); + } + return parse_dict_node(object, size, dict_size); - case BPLIST_DICT: /* returning a raw dict, it forward-references, so. */ - data->length = data->length * 2; // dicts lie - data->type = PLIST_DICT; + } + return NULL; +} - case BPLIST_ARRAY: /* returning a raw array, it forward-references, so. */ - data->ref_size = ref_size; // in arrays and dicts, the "ref size" alluded to in the trailer applies, and should be stored in intval8 so as to save space. - if (data->type == 0) - data->type = PLIST_ARRAY; - case BPLIST_DATA: - if (data->type == 0) - data->type = PLIST_DATA; - default: /* made to hold raw data. */ - modifier = (data->ref_size > 0) ? data->ref_size : 1; - data->buff = (char *) malloc(sizeof(char) * (data->length * modifier)); - memcpy(data->buff, bpbuffer + myPos, (data->length * modifier)); +void plist_to_bin(plist_t plist, char **plist_bin, uint32_t * length) +{ + uint64_t num_objects = g_node_n_nodes(plist, G_TRAVERSE_ALL); +} + + + +gpointer copy_plist_data(gconstpointer src, gpointer data) +{ + struct plist_data *srcdata = (struct plist_data *) src; + struct plist_data *dstdata = (struct plist_data *) calloc(sizeof(struct plist_data), 1); + + dstdata->type = srcdata->type; + dstdata->length = srcdata->length; + switch (dstdata->type) { + case PLIST_BOOLEAN: + dstdata->boolval = srcdata->boolval; + break; + case PLIST_UINT: + dstdata->intval = srcdata->intval; + break; + case PLIST_DATE: + case PLIST_REAL: + dstdata->realval = srcdata->realval; + break; + case PLIST_KEY: + case PLIST_STRING: + dstdata->strval = strdup(srcdata->strval); + break; + case PLIST_UNICODE: + dstdata->unicodeval = wcsdup(srcdata->unicodeval); + break; + case PLIST_PLIST: + case PLIST_DATA: + case PLIST_ARRAY: + case PLIST_DICT: + dstdata->buff = (char *) malloc(sizeof(char *) * srcdata->length); + memcpy(dstdata->buff, srcdata->buff, sizeof(char *) * srcdata->length); break; + default: + break; } - myPos += data->length; - *position = myPos; - return new_node; + return dstdata; } -plist_t parse_nodes(const char *bpbuffer, uint32_t bplength, uint32_t * position) +void bin_to_plist(const char *plist_bin, uint32_t length, plist_t * plist) { - plist_t *nodeslist = NULL, *newaddr = NULL; - plist_t new_node = NULL, root_node = NULL; + //first check we have enough data + if (!(length >= BPLIST_MAGIC_SIZE + BPLIST_VERSION_SIZE + BPLIST_TRL_SIZE)) + return; + //check that plist_bin in actually a plist + if (memcmp(plist_bin, BPLIST_MAGIC, BPLIST_MAGIC_SIZE) != 0) + return; + //check for known version + if (memcmp(plist_bin + BPLIST_MAGIC_SIZE, BPLIST_VERSION, BPLIST_VERSION_SIZE) != 0) + return; - uint32_t nodeslength = 0; - uint8_t offset_size = 0, dict_param_size = 0; - offset_size = bpbuffer[bplength - 26]; - dict_param_size = bpbuffer[bplength - 25]; - uint64_t current_offset = 0; - uint64_t num_objects = 0, root_object = 0, offset_table_index = 0; - memcpy(&num_objects, bpbuffer + bplength - 24, sizeof(uint64_t)); - memcpy(&root_object, bpbuffer + bplength - 16, sizeof(uint64_t)); - memcpy(&offset_table_index, bpbuffer + bplength - 8, sizeof(uint64_t)); - byte_convert((char *) &num_objects, sizeof(uint64_t)); - byte_convert((char *) &root_object, sizeof(uint64_t)); - byte_convert((char *) &offset_table_index, sizeof(uint64_t)); - - log_debug_msg("Offset size: %i\nGiven: %i\n", offset_size, bpbuffer[bplength - 26]); - log_debug_msg("Ref size: %i\nGiven: %i\n", dict_param_size, bpbuffer[bplength - 25]); - log_debug_msg("Number of objects: %lli\nGiven: %llu\n", num_objects, *(bpbuffer + bplength - 24)); - log_debug_msg("Root object index: %lli\nGiven: %llu\n", root_object, *(bpbuffer + bplength - 16)); - log_debug_msg("Offset table index: %lli\nGiven: %llu\n", offset_table_index, *(bpbuffer + bplength - 8)); - log_debug_msg("Size of uint64: %i\n", sizeof(uint64_t)); - - int i = 0, j = 0, k = 0, str_i = 0, str_j = 0; - uint32_t index1 = 0, index2 = 0; + //now parse trailer + const char *trailer = plist_bin + (length - BPLIST_TRL_SIZE); + + uint8_t offset_size = trailer[BPLIST_TRL_OFFSIZE_IDX]; + uint8_t dict_param_size = trailer[BPLIST_TRL_PARMSIZE_IDX]; + uint64_t num_objects = be64dec(trailer + BPLIST_TRL_NUMOBJ_IDX); + uint64_t root_object = be64dec(trailer + BPLIST_TRL_ROOTOBJ_IDX); + uint64_t offset_table_index = be64dec(trailer + BPLIST_TRL_OFFTAB_IDX); + log_debug_msg("Offset size: %i\n", offset_size); + log_debug_msg("Ref size: %i\n", dict_param_size); + log_debug_msg("Number of objects: %lli\n", num_objects); + log_debug_msg("Root object index: %lli\n", root_object); + log_debug_msg("Offset table index: %lli\n", offset_table_index); + + if (num_objects == 0) + return; + + //allocate serialized array of nodes + plist_t *nodeslist = NULL; nodeslist = (plist_t *) malloc(sizeof(plist_t) * num_objects); + if (!nodeslist) - return NULL; + return; + //parse serialized nodes + uint64_t i = 0; + uint64_t current_offset = 0; + const char *offset_table = plist_bin + offset_table_index; for (i = 0; i < num_objects; i++) { - memcpy(¤t_offset, bpbuffer + (offset_table_index + (i * offset_size)), offset_size); - byte_convert((char *) ¤t_offset, - (offset_size <= sizeof(current_offset)) ? offset_size : sizeof(current_offset)); - log_debug_msg("parse_nodes: current_offset = %x\n", current_offset); - nodeslist[i] = parse_raw_node(bpbuffer, bplength, (uint32_t *) & current_offset, dict_param_size); + current_offset = swap_n_bytes(offset_table + i * offset_size, offset_size); + + log_debug_msg("parse_nodes: current_offset = %i\n", current_offset); + char *obj = plist_bin + current_offset; + nodeslist[i] = parse_bin_node(obj, dict_param_size, &obj); log_debug_msg("parse_nodes: parse_raw_node done\n"); } + //setup children for structured types + int j = 0, str_i = 0, str_j = 0; + uint32_t index1 = 0, index2 = 0; for (i = 0; i < num_objects; i++) { - // set elements for dicts and arrays and leave the rest alone + log_debug_msg("parse_nodes: on node %i\n", i); struct plist_data *data = (struct plist_data *) nodeslist[i]->data; switch (data->type) { case PLIST_DICT: log_debug_msg("parse_nodes: dictionary found\n"); - for (j = 0; j < (data->length / 2); j++) { - str_i = j * data->ref_size; - str_j = (j + (data->length / 2)) * data->ref_size; - - memcpy(&index1, data->buff + str_i, data->ref_size); - memcpy(&index2, data->buff + str_j, data->ref_size); - - byte_convert((char *) &index1, (dict_param_size <= sizeof(index1)) ? dict_param_size : sizeof(index2)); - byte_convert((char *) &index2, (dict_param_size <= sizeof(index2)) ? dict_param_size : sizeof(index2)); - - g_node_append(nodeslist[i], nodeslist[index1]); - g_node_append(nodeslist[i], nodeslist[index2]); + for (j = 0; j < data->length; j++) { + str_i = j * dict_param_size; + str_j = (j + data->length) * dict_param_size; + + index1 = swap_n_bytes(data->buff + str_i, dict_param_size); + index2 = swap_n_bytes(data->buff + str_j, dict_param_size); + + //first one is actually a key + ((struct plist_data *) nodeslist[index1]->data)->type = PLIST_KEY; + //g_node_append(nodeslist[i], nodeslist[index1]); + //g_node_append(nodeslist[i], nodeslist[index2]); + + if (G_NODE_IS_ROOT(nodeslist[index1])) + g_node_append(nodeslist[i], nodeslist[index1]); + else + g_node_append(nodeslist[i], g_node_copy_deep(nodeslist[index1], copy_plist_data, NULL)); + + if (G_NODE_IS_ROOT(nodeslist[index2])) + g_node_append(nodeslist[i], nodeslist[index2]); + else + g_node_append(nodeslist[i], g_node_copy_deep(nodeslist[index2], copy_plist_data, NULL)); } - data->length = data->length / 2; free(data->buff); - k = 0; break; case PLIST_ARRAY: log_debug_msg("parse_nodes: array found\n"); for (j = 0; j < data->length; j++) { - log_debug_msg("parse_nodes: array index %i\n", j); - str_j = j * data->ref_size; - memcpy(&index1, data->buff + str_j, data->ref_size); - log_debug_msg("parse_nodes: post-memcpy\n"); - byte_convert((char *) &index1, (dict_param_size <= sizeof(index1)) ? dict_param_size : sizeof(index1)); - log_debug_msg("parse_nodes: post-ntohl\nindex1 = %i\n", index1); - g_node_append(nodeslist[i], nodeslist[index1]); - log_debug_msg("parse_nodes: post-assignment\n"); + str_j = j * dict_param_size; + index1 = swap_n_bytes(data->buff + str_j, dict_param_size); + + //g_node_append(nodeslist[i], nodeslist[index1]); + if (G_NODE_IS_ROOT(nodeslist[index1])) + g_node_append(nodeslist[i], nodeslist[index1]); + else + g_node_append(nodeslist[i], g_node_copy_deep(nodeslist[index1], copy_plist_data, NULL)); } free(data->buff); break; default: - //printf("lol... type %x\n", nodeslist[i]->type); break; - } // those are the only two we need to correct for. + } } - root_node = nodeslist[root_object]; - return root_node; -} - -void plist_to_bin(plist_t plist, char **plist_bin, uint32_t * length) -{ -} - -void xml_to_plist(const char *plist_xml, uint32_t length, plist_t * plist) -{ - xmlDocPtr plist_doc = xmlReadMemory(plist_xml, length, NULL, NULL, 0); - xmlNodePtr root_node = xmlDocGetRootElement(plist_doc); - - struct plist_data *data = (struct plist_data *) calloc(sizeof(struct plist_data), 1); - *plist = g_node_new(data); - data->type = PLIST_PLIST; - xml_to_node(root_node, *plist); - -} - -void bin_to_plist(const char *plist_bin, uint32_t length, plist_t * plist) -{ - uint32_t pos = 0; - *plist = parse_nodes(plist_bin, length, &pos); + *plist = nodeslist[root_object]; } @@ -816,23 +928,11 @@ char compare_node_value(plist_type type, struct plist_data *data, void *value) case PLIST_BOOLEAN: res = data->boolval == *((char *) value) ? TRUE : FALSE; break; - case PLIST_UINT8: - res = data->intval8 == *((uint8_t *) value) ? TRUE : FALSE; - break; - case PLIST_UINT16: - res = data->intval16 == *((uint16_t *) value) ? TRUE : FALSE; - break; - case PLIST_UINT32: - res = data->intval32 == *((uint32_t *) value) ? TRUE : FALSE; - break; - case PLIST_UINT64: - res = data->intval64 == *((uint64_t *) value) ? TRUE : FALSE; + case PLIST_UINT: + res = data->intval == *((uint64_t *) value) ? TRUE : FALSE; break; - case PLIST_FLOAT32: - res = data->realval32 == *((float *) value) ? TRUE : FALSE; - break; - case PLIST_FLOAT64: - res = data->realval64 == *((double *) value) ? TRUE : FALSE; + case PLIST_REAL: + res = data->realval == *((double *) value) ? TRUE : FALSE; break; case PLIST_KEY: case PLIST_STRING: @@ -889,23 +989,11 @@ void get_type_and_value(GNode * node, plist_type * type, void *value) case PLIST_BOOLEAN: *((char *) value) = data->boolval; break; - case PLIST_UINT8: - *((uint8_t *) value) = data->intval8; - break; - case PLIST_UINT16: - *((uint16_t *) value) = data->intval16; - break; - case PLIST_UINT32: - *((uint32_t *) value) = data->intval32; - break; - case PLIST_UINT64: - *((uint64_t *) value) = data->intval64; - break; - case PLIST_FLOAT32: - *((float *) value) = data->realval32; + case PLIST_UINT: + *((uint64_t *) value) = data->intval; break; - case PLIST_FLOAT64: - *((double *) value) = data->realval64; + case PLIST_REAL: + *((double *) value) = data->realval; break; case PLIST_STRING: *((char **) value) = strdup(data->strval); -- cgit v1.1-32-gdbae From 625633203a27f569bea8890cb269132fea83b497 Mon Sep 17 00:00:00 2001 From: Jonathan Beck Date: Wed, 10 Dec 2008 23:22:12 +0100 Subject: add bplist writting capability. --- src/plist.c | 383 +++++++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 356 insertions(+), 27 deletions(-) (limited to 'src/plist.c') diff --git a/src/plist.c b/src/plist.c index c691c16..431c64a 100644 --- a/src/plist.c +++ b/src/plist.c @@ -25,6 +25,8 @@ #include "utils.h" #include "plist.h" #include +#include +#include /********************************************** * * @@ -127,7 +129,7 @@ void plist_new_plist(plist_t * plist) if (*plist != NULL) return; struct plist_data *data = (struct plist_data *) calloc(sizeof(struct plist_data), 1); - data->type = PLIST_PLIST; + data->type = PLIST_DICT; *plist = g_node_new(data); } @@ -191,7 +193,6 @@ void plist_add_dict_element(dict_t dict, char *key, plist_type type, void *value case PLIST_ARRAY: case PLIST_DICT: case PLIST_DATE: - case PLIST_PLIST: default: break; } @@ -314,10 +315,6 @@ void node_to_xml(GNode * node, gpointer xml_struct) tag = "dict"; isStruct = TRUE; break; - case PLIST_PLIST: - tag = "plist"; - isStruct = TRUE; - break; case PLIST_DATE: //TODO : handle date tag default: break; @@ -332,8 +329,7 @@ void node_to_xml(GNode * node, gpointer xml_struct) g_free(val); //add return for structured types - if (node_data->type == PLIST_ARRAY || - node_data->type == PLIST_DICT || node_data->type == PLIST_DATA || node_data->type == PLIST_PLIST) + if (node_data->type == PLIST_ARRAY || node_data->type == PLIST_DICT || node_data->type == PLIST_DATA) xmlNodeAddContent(child_node, "\n"); if (isStruct) { @@ -341,8 +337,7 @@ void node_to_xml(GNode * node, gpointer xml_struct) g_node_children_foreach(node, G_TRAVERSE_ALL, node_to_xml, &child); } //fix indent for structured types - if (node_data->type == PLIST_ARRAY || - node_data->type == PLIST_DICT || node_data->type == PLIST_DATA || node_data->type == PLIST_PLIST) { + if (node_data->type == PLIST_ARRAY || node_data->type == PLIST_DICT || node_data->type == PLIST_DATA) { for (i = 0; i < xstruct->depth; i++) { xmlNodeAddContent(child_node, "\t"); @@ -446,7 +441,7 @@ void xml_to_plist(const char *plist_xml, uint32_t length, plist_t * plist) struct plist_data *data = (struct plist_data *) calloc(sizeof(struct plist_data), 1); *plist = g_node_new(data); - data->type = PLIST_PLIST; + data->type = PLIST_DICT; xml_to_node(root_node, *plist); } @@ -512,6 +507,9 @@ void byte_convert(char *address, size_t size) #define be64dec(x) bswap_64( *(uint64_t*)(x) ) +#define get_needed_bytes(x) (x <= 1<<8 ? 1 : ( x <= 1<<16 ? 2 : ( x <= 1<<32 ? 4 : 8))) +#define get_real_bytes(x) (x >> 32 ? 4 : 8) + GNode *parse_uint_node(char *bnode, uint8_t size, char **next_object) { struct plist_data *data = (struct plist_data *) calloc(sizeof(struct plist_data), 1); @@ -638,7 +636,6 @@ uint64_t plist_get_node_uint_val(plist_t node) return 0; } - GNode *parse_bin_node(char *object, uint8_t dict_size, char **next_object) { if (!object) @@ -738,13 +735,6 @@ GNode *parse_bin_node(char *object, uint8_t dict_size, char **next_object) return NULL; } -void plist_to_bin(plist_t plist, char **plist_bin, uint32_t * length) -{ - uint64_t num_objects = g_node_n_nodes(plist, G_TRAVERSE_ALL); -} - - - gpointer copy_plist_data(gconstpointer src, gpointer data) { struct plist_data *srcdata = (struct plist_data *) src; @@ -770,7 +760,6 @@ gpointer copy_plist_data(gconstpointer src, gpointer data) case PLIST_UNICODE: dstdata->unicodeval = wcsdup(srcdata->unicodeval); break; - case PLIST_PLIST: case PLIST_DATA: case PLIST_ARRAY: case PLIST_DICT: @@ -856,8 +845,6 @@ void bin_to_plist(const char *plist_bin, uint32_t length, plist_t * plist) //first one is actually a key ((struct plist_data *) nodeslist[index1]->data)->type = PLIST_KEY; - //g_node_append(nodeslist[i], nodeslist[index1]); - //g_node_append(nodeslist[i], nodeslist[index2]); if (G_NODE_IS_ROOT(nodeslist[index1])) g_node_append(nodeslist[i], nodeslist[index1]); @@ -895,7 +882,6 @@ void bin_to_plist(const char *plist_bin, uint32_t length, plist_t * plist) *plist = nodeslist[root_object]; } - GNode *find_query_node(plist_t plist, char *key, char *request) { if (!plist) @@ -912,7 +898,7 @@ GNode *find_query_node(plist_t plist, char *key, char *request) if (data->type == PLIST_STRING && !strcmp(data->strval, request)) return current->next; } - if (data->type == PLIST_DICT || data->type == PLIST_ARRAY || data->type == PLIST_PLIST) { + if (data->type == PLIST_DICT || data->type == PLIST_ARRAY) { GNode *sub = find_query_node(current, key, request); if (sub) return sub; @@ -947,7 +933,6 @@ char compare_node_value(plist_type type, struct plist_data *data, void *value) case PLIST_ARRAY: case PLIST_DICT: case PLIST_DATE: - case PLIST_PLIST: default: break; } @@ -967,7 +952,7 @@ GNode *find_node(plist_t plist, plist_type type, void *value) if (data->type == type && compare_node_value(type, data, value)) { return current; } - if (data->type == PLIST_DICT || data->type == PLIST_ARRAY || data->type == PLIST_PLIST) { + if (data->type == PLIST_DICT || data->type == PLIST_ARRAY) { GNode *sub = find_node(current, type, value); if (sub) return sub; @@ -1008,8 +993,352 @@ void get_type_and_value(GNode * node, plist_type * type, void *value) case PLIST_ARRAY: case PLIST_DICT: case PLIST_DATE: - case PLIST_PLIST: default: break; } } + +guint plist_data_hash(gconstpointer key) +{ + struct plist_data *data = (struct plist_data *) ((GNode *) key)->data; + + guint hash = data->type; + guint i = 0; + + char *buff = NULL; + guint size = 0; + + switch (data->type) { + case PLIST_BOOLEAN: + case PLIST_UINT: + case PLIST_REAL: + buff = (char *) &data->intval; + size = 8; + + case PLIST_KEY: + case PLIST_STRING: + buff = data->strval; + size = strlen(buff); + + case PLIST_UNICODE: + buff = data->unicodeval; + size = strlen(buff) * sizeof(wchar_t); + + case PLIST_DATA: + case PLIST_ARRAY: + case PLIST_DICT: + //for these types only hash pointer + buff = &key; + size = sizeof(gconstpointer); + break; + case PLIST_DATE: + default: + break; + } + + //now perform hash + for (i = 0; i < size; buff++, i++) + hash = hash << 7 ^ (*buff); + + return hash; +} + +gboolean plist_data_compare(gconstpointer a, gconstpointer b) +{ + if (!a || !b) + return FALSE; + + if (!((GNode *) a)->data || !((GNode *) b)->data) + return FALSE; + + struct plist_data *val_a = (struct plist_data *) ((GNode *) a)->data; + struct plist_data *val_b = (struct plist_data *) ((GNode *) b)->data; + + if (val_a->type != val_b->type) + return FALSE; + + switch (val_a->type) { + case PLIST_BOOLEAN: + case PLIST_UINT: + case PLIST_REAL: + if (val_a->intval == val_b->intval) //it is an union so this is sufficient + return TRUE; + else + return FALSE; + + case PLIST_KEY: + case PLIST_STRING: + if (!strcmp(val_a->strval, val_b->strval)) + return TRUE; + else + return FALSE; + case PLIST_UNICODE: + if (!strcmp(val_a->unicodeval, val_b->unicodeval)) + return TRUE; + else + return FALSE; + + case PLIST_DATA: + case PLIST_ARRAY: + case PLIST_DICT: + //compare pointer + if (a == b) + return TRUE; + else + return FALSE; + break; + case PLIST_DATE: + default: + break; + } + return FALSE; +} + +struct serialize_s { + GPtrArray *objects; + GHashTable *ref_table; +}; + +void serialize_plist(GNode * node, gpointer data) +{ + struct serialize_s *ser = (struct serialize_s *) data; + uint64_t current_index = ser->objects->len; + + //first check that node is not yet in objects + gpointer val = g_hash_table_lookup(ser->ref_table, node); + if (val) { + //data is already in table + return; + } + //insert new ref + g_hash_table_insert(ser->ref_table, node, GUINT_TO_POINTER(current_index)); + + //now append current node to object array + g_ptr_array_add(ser->objects, node); + + //now recurse on children + g_node_children_foreach(node, G_TRAVERSE_ALL, serialize_plist, data); + return; +} + + + +void write_int(GByteArray * bplist, uint64_t val) +{ + uint64_t size = get_needed_bytes(val); + uint8_t *buff = (uint8_t *) malloc(sizeof(uint8_t) + size); + buff[0] = BPLIST_UINT | size >> 1; + memcpy(buff + 1, &val, size); + swap_n_bytes(buff + 1, size); + g_byte_array_append(bplist, buff, sizeof(uint8_t) + size); + free(buff); +} + +void write_real(GByteArray * bplist, double val) +{ + uint64_t size = get_real_bytes(*((uint64_t *) & val)); //cheat to know used space + uint8_t *buff = (uint8_t *) malloc(sizeof(uint8_t) + size); + buff[0] = BPLIST_REAL | size >> 1; + memcpy(buff + 1, &val, size); + swap_n_bytes(buff + 1, size); + g_byte_array_append(bplist, buff, sizeof(uint8_t) + size); + free(buff); +} + +void write_raw_data(GByteArray * bplist, uint8_t mark, uint8_t * val, uint64_t size) +{ + uint8_t marker = mark | (size < 15 ? size : 0xf); + g_byte_array_append(bplist, &marker, sizeof(uint8_t)); + if (size >= 15) { + GByteArray *int_buff = g_byte_array_new(); + write_int(int_buff, size); + g_byte_array_append(bplist, int_buff->data, int_buff->len); + g_byte_array_free(int_buff, TRUE); + } + uint8_t *buff = (uint8_t *) malloc(size); + memcpy(buff, val, size); + g_byte_array_append(bplist, buff, size); + free(buff); +} + +void write_data(GByteArray * bplist, uint8_t * val, uint64_t size) +{ + write_raw_data(bplist, BPLIST_DATA, val, size); +} + +void write_string(GByteArray * bplist, char *val) +{ + uint64_t size = strlen(val); + write_raw_data(bplist, BPLIST_STRING, val, size); +} + +void write_array(GByteArray * bplist, GNode * node, GHashTable * ref_table, uint8_t dict_param_size) +{ + uint64_t size = g_node_n_children(node); + uint8_t marker = BPLIST_ARRAY | (size < 15 ? size : 0xf); + g_byte_array_append(bplist, &marker, sizeof(uint8_t)); + if (size >= 15) { + GByteArray *int_buff = g_byte_array_new(); + write_int(int_buff, size); + g_byte_array_append(bplist, int_buff->data, int_buff->len); + g_byte_array_free(int_buff, TRUE); + } + + uint64_t idx = 0; + uint8_t *buff = (uint8_t *) malloc(size * dict_param_size); + + GNode *cur = NULL; + int i = 0; + for (i = 0, cur = node->children; cur && i < size; cur = cur->next, i++) { + idx = GPOINTER_TO_UINT(g_hash_table_lookup(ref_table, cur)); + memcpy(buff + i * dict_param_size, &idx, dict_param_size); + swap_n_bytes(buff + i * dict_param_size, dict_param_size); + } + + //now append to bplist + g_byte_array_append(bplist, buff, size * dict_param_size); + free(buff); + +} + +void write_dict(GByteArray * bplist, GNode * node, GHashTable * ref_table, uint8_t dict_param_size) +{ + uint64_t size = g_node_n_children(node) / 2; + uint8_t marker = BPLIST_ARRAY | (size < 15 ? size : 0xf); + g_byte_array_append(bplist, &marker, sizeof(uint8_t)); + if (size >= 15) { + GByteArray *int_buff = g_byte_array_new(); + write_int(int_buff, size); + g_byte_array_append(bplist, int_buff->data, int_buff->len); + g_byte_array_free(int_buff, TRUE); + } + + uint64_t idx1 = 0; + uint64_t idx2 = 0; + uint8_t *buff = (uint8_t *) malloc(size * 2 * dict_param_size); + + GNode *cur = NULL; + int i = 0; + for (i = 0, cur = node->children; cur && i < size; cur = cur->next->next, i++) { + idx1 = GPOINTER_TO_UINT(g_hash_table_lookup(ref_table, cur)); + memcpy(buff + i * dict_param_size, &idx1, dict_param_size); + swap_n_bytes(buff + i * dict_param_size, dict_param_size); + + idx2 = GPOINTER_TO_UINT(g_hash_table_lookup(ref_table, cur->next)); + memcpy(buff + (i + size) * dict_param_size, &idx2, dict_param_size); + swap_n_bytes(buff + (i + size) * dict_param_size, dict_param_size); + } + + //now append to bplist + g_byte_array_append(bplist, buff, size * dict_param_size); + free(buff); + +} + +void plist_to_bin(plist_t plist, char **plist_bin, uint32_t * length) +{ + //first serialize tree + + //list of objects + GPtrArray *objects = g_ptr_array_new(); + //hashtable to write only once same nodes + GHashTable *ref_table = g_hash_table_new(plist_data_hash, plist_data_compare); + + //serialize plist + struct serialize_s ser_s = { objects, ref_table }; + g_node_children_foreach(plist, G_TRAVERSE_ALL, serialize_plist, &ser_s); + + //now stream to output buffer + uint8_t offset_size = 0; //unknown yet + uint8_t dict_param_size = get_needed_bytes(objects->len); + uint64_t num_objects = objects->len; + uint64_t root_object = 0; //root is first in list + uint64_t offset_table_index = 0; //unknown yet + + //setup a dynamic bytes array to store bplist in + GByteArray *bplist_buff = g_byte_array_new(); + + //set magic number and version + g_byte_array_append(bplist_buff, BPLIST_MAGIC, BPLIST_MAGIC_SIZE); + g_byte_array_append(bplist_buff, BPLIST_VERSION, BPLIST_VERSION_SIZE); + + //write objects and table + int i = 0; + uint8_t *buff = NULL; + uint8_t size = 0; + uint64_t offsets[num_objects]; + for (i = 0; i <= num_objects; i++) { + + offsets[i] = bplist_buff->len; + struct plist_data *data = (struct plist_data *) ((GNode *) g_ptr_array_index(objects, i))->data; + + switch (data->type) { + case PLIST_BOOLEAN: + buff = (uint8_t *) malloc(sizeof(uint8_t)); + buff[0] = data->boolval ? BPLIST_TRUE : BPLIST_FALSE; + g_byte_array_append(bplist_buff, buff, sizeof(uint8_t)); + free(buff); + break; + + case PLIST_UINT: + write_int(bplist_buff, data->intval); + break; + + case PLIST_REAL: + write_real(bplist_buff, data->realval); + break; + + case PLIST_KEY: + case PLIST_STRING: + write_string(bplist_buff, data->strval); + break; + case PLIST_UNICODE: + //TODO + break; + case PLIST_DATA: + write_data(bplist_buff, data->strval, data->length); + case PLIST_ARRAY: + write_array(bplist_buff, g_ptr_array_index(objects, i), ref_table, dict_param_size); + break; + case PLIST_DICT: + write_dict(bplist_buff, g_ptr_array_index(objects, i), ref_table, dict_param_size); + break; + case PLIST_DATE: + //TODO + break; + default: + break; + } + } + + //write offsets + offset_size = get_needed_bytes(bplist_buff->len); + for (i = 0; i <= num_objects; i++) { + uint8_t *buff = (uint8_t *) malloc(offset_size); + memcpy(buff, offsets + i, offset_size); + swap_n_bytes(buff, offset_size); + g_byte_array_append(bplist_buff, buff, offset_size); + free(buff); + } + + //setup trailer + num_objects = bswap_64(num_objects); + root_object = bswap_64(root_object); + offset_table_index = bswap_64(offset_table_index); + + char trailer[BPLIST_TRL_SIZE]; + memcpy(trailer + BPLIST_TRL_OFFSIZE_IDX, &offset_size, sizeof(uint8_t)); + memcpy(trailer + BPLIST_TRL_PARMSIZE_IDX, &dict_param_size, sizeof(uint8_t)); + memcpy(trailer + BPLIST_TRL_NUMOBJ_IDX, &num_objects, sizeof(uint64_t)); + memcpy(trailer + BPLIST_TRL_ROOTOBJ_IDX, &root_object, sizeof(uint64_t)); + memcpy(trailer + BPLIST_TRL_OFFTAB_IDX, &offset_table_index, sizeof(uint64_t)); + + g_byte_array_append(bplist_buff, trailer, BPLIST_TRL_SIZE); + + //duplicate buffer + *plist_bin = (char *) malloc(bplist_buff->len); + memcpy(*plist_bin, bplist_buff->data, bplist_buff->len); + *length = bplist_buff->len; + + g_byte_array_free(bplist_buff, TRUE); +} -- cgit v1.1-32-gdbae From cd95e9bc6e23949b5cef3996132b79bd8803467a Mon Sep 17 00:00:00 2001 From: Jonathan Beck Date: Wed, 10 Dec 2008 23:42:21 +0100 Subject: fix minor programming erro plus enhance plutil to convert bin to xml and xml to bin. --- src/plist.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'src/plist.c') diff --git a/src/plist.c b/src/plist.c index 431c64a..2a6d4bc 100644 --- a/src/plist.c +++ b/src/plist.c @@ -1014,16 +1014,16 @@ guint plist_data_hash(gconstpointer key) case PLIST_REAL: buff = (char *) &data->intval; size = 8; - + break; case PLIST_KEY: case PLIST_STRING: buff = data->strval; size = strlen(buff); - + break; case PLIST_UNICODE: buff = data->unicodeval; size = strlen(buff) * sizeof(wchar_t); - + break; case PLIST_DATA: case PLIST_ARRAY: case PLIST_DICT: @@ -1267,7 +1267,7 @@ void plist_to_bin(plist_t plist, char **plist_bin, uint32_t * length) uint8_t *buff = NULL; uint8_t size = 0; uint64_t offsets[num_objects]; - for (i = 0; i <= num_objects; i++) { + for (i = 0; i < num_objects; i++) { offsets[i] = bplist_buff->len; struct plist_data *data = (struct plist_data *) ((GNode *) g_ptr_array_index(objects, i))->data; -- cgit v1.1-32-gdbae From 18d1ee3b0f17325fdffe0cf3e2770a3f0f45a1b9 Mon Sep 17 00:00:00 2001 From: Jonathan Beck Date: Thu, 11 Dec 2008 23:03:21 +0100 Subject: move stuff around to make code more organized. --- src/plist.c | 1118 +---------------------------------------------------------- 1 file changed, 7 insertions(+), 1111 deletions(-) (limited to 'src/plist.c') diff --git a/src/plist.c b/src/plist.c index 2a6d4bc..76ae954 100644 --- a/src/plist.c +++ b/src/plist.c @@ -28,101 +28,6 @@ #include #include -/********************************************** -* * -* Abstract Plist stuff * -* * -**********************************************/ - - - - - - - -/** Formats a block of text to be a given indentation and width. - * - * The total width of the return string will be depth + cols. - * - * @param buf The string to format. - * @param cols The number of text columns for returned block of text. - * @param depth The number of tabs to indent the returned block of text. - * - * @return The formatted string. - */ -char *format_string(const char *buf, int cols, int depth) -{ - int colw = depth + cols + 1; - int len = strlen(buf); - int nlines = len / cols + 1; - char *new_buf = (char *) malloc(nlines * colw + depth + 1); - int i = 0; - int j = 0; - - assert(cols >= 0); - assert(depth >= 0); - - // Inserts new lines and tabs at appropriate locations - for (i = 0; i < nlines; i++) { - new_buf[i * colw] = '\n'; - for (j = 0; j < depth; j++) - new_buf[i * colw + 1 + j] = '\t'; - memcpy(new_buf + i * colw + 1 + depth, buf + i * cols, cols); - } - new_buf[len + (1 + depth) * nlines] = '\n'; - - // Inserts final row of indentation and termination character - for (j = 0; j < depth; j++) - new_buf[len + (1 + depth) * nlines + 1 + j] = '\t'; - new_buf[len + (1 + depth) * nlines + depth + 1] = '\0'; - - return new_buf; -} - - - - -/* - * Binary propertylist code follows - */ - - -/* - * This is how parsing a bplist is going to have to work: - * - The entire binary plist is going to have to be in memory. - * - A function, parse_nodes(), will have to be a recursive function - * which iterates over the binary plist and reads in elements into bplist_node structs - * and handles them accordingly. The end result should be a somewhat-hierarchical layout - * of bplist_nodes. - * - parse_nodes() will return the first node it encounters, which is usually the "root" node. - */ - - - -void print_bytes(char *val, size_t size) -{ - int i = 0; - for (i = 0; i < size; i++) { - printf("Byte %i: 0x%x\n", i, val[i]); - } -} - - - -struct plist_data { - union { - char boolval; - uint64_t intval; - double realval; - char *strval; - wchar_t *unicodeval; - char *buff; - }; - uint64_t length; - plist_type type; -}; - - void plist_new_plist(plist_t * plist) { @@ -205,683 +110,6 @@ void plist_free(plist_t plist) g_node_destroy(plist); } -/********************************************** -* * -* Xml Plist stuff * -* * -**********************************************/ - -#include -#include - - -const char *plist_base = "\n\ -\n\ -\n\ -\0"; - -struct xml_node { - xmlNodePtr xml; - uint32_t depth; -}; - -/** Creates a new plist XML document. - * - * @return The plist XML document. - */ -xmlDocPtr new_plist() -{ - char *plist = strdup(plist_base); - xmlDocPtr plist_xml = xmlReadMemory(plist, strlen(plist), NULL, NULL, 0); - - if (!plist_xml) - return NULL; - - free(plist); - - return plist_xml; -} - -/** Destroys a previously created XML document. - * - * @param plist The XML document to destroy. - */ -void free_plist(xmlDocPtr plist) -{ - if (!plist) - return; - - xmlFreeDoc(plist); -} - -void node_to_xml(GNode * node, gpointer xml_struct) -{ - if (!node) - return; - - struct xml_node *xstruct = (struct xml_node *) xml_struct; - struct plist_data *node_data = (struct plist_data *) node->data; - - xmlNodePtr child_node = NULL; - char isStruct = FALSE; - - gchar *tag = NULL; - gchar *val = NULL; - - switch (node_data->type) { - case PLIST_BOOLEAN: - { - if (node_data->boolval) - tag = "true"; - else - tag = "false"; - } - break; - - case PLIST_UINT: - tag = "integer"; - val = g_strdup_printf("%lu", (long unsigned int) node_data->intval); - break; - - case PLIST_REAL: - tag = "real"; - val = g_strdup_printf("%Lf", (long double) node_data->realval); - break; - - case PLIST_STRING: - tag = "string"; - val = g_strdup(node_data->strval); - break; - - case PLIST_UNICODE: - tag = "string"; - val = g_strdup((gchar *) node_data->unicodeval); - break; - - case PLIST_KEY: - tag = "key"; - val = g_strdup((gchar *) node_data->strval); - break; - - case PLIST_DATA: - tag = "data"; - val = format_string(node_data->buff, 60, xstruct->depth); - break; - case PLIST_ARRAY: - tag = "array"; - isStruct = TRUE; - break; - case PLIST_DICT: - tag = "dict"; - isStruct = TRUE; - break; - case PLIST_DATE: //TODO : handle date tag - default: - break; - } - - int i = 0; - for (i = 0; i < xstruct->depth; i++) { - xmlNodeAddContent(xstruct->xml, "\t"); - } - child_node = xmlNewChild(xstruct->xml, NULL, tag, val); - xmlNodeAddContent(xstruct->xml, "\n"); - g_free(val); - - //add return for structured types - if (node_data->type == PLIST_ARRAY || node_data->type == PLIST_DICT || node_data->type == PLIST_DATA) - xmlNodeAddContent(child_node, "\n"); - - if (isStruct) { - struct xml_node child = { child_node, xstruct->depth + 1 }; - g_node_children_foreach(node, G_TRAVERSE_ALL, node_to_xml, &child); - } - //fix indent for structured types - if (node_data->type == PLIST_ARRAY || node_data->type == PLIST_DICT || node_data->type == PLIST_DATA) { - - for (i = 0; i < xstruct->depth; i++) { - xmlNodeAddContent(child_node, "\t"); - } - } - - return; -} - -void xml_to_node(xmlNodePtr xml_node, GNode * plist_node) -{ - xmlNodePtr node = NULL; - - for (node = xml_node->children; node; node = node->next) { - - while (node && !xmlStrcmp(node->name, "text")) - node = node->next; - if (!node) - break; - - struct plist_data *data = (struct plist_data *) calloc(sizeof(struct plist_data), 1); - GNode *subnode = g_node_new(data); - g_node_append(plist_node, subnode); - - if (!xmlStrcmp(node->name, "true")) { - data->boolval = 1; - data->type = PLIST_BOOLEAN; - continue; - } - - if (!xmlStrcmp(node->name, "false")) { - data->boolval = 0; - data->type = PLIST_BOOLEAN; - continue; - } - - if (!xmlStrcmp(node->name, "integer")) { - char *strval = xmlNodeGetContent(node); - data->intval = atoi(strval); - data->type = PLIST_UINT; - continue; - } - - if (!xmlStrcmp(node->name, "real")) { - char *strval = xmlNodeGetContent(node); - data->realval = atof(strval); - data->type = PLIST_REAL; - continue; - } - - if (!xmlStrcmp(node->name, "date")) - continue; //TODO : handle date tag - - if (!xmlStrcmp(node->name, "string")) { - data->strval = strdup(xmlNodeGetContent(node)); - data->type = PLIST_STRING; - continue; - } - - if (!xmlStrcmp(node->name, "key")) { - data->strval = strdup(xmlNodeGetContent(node)); - data->type = PLIST_KEY; - continue; - } - - if (!xmlStrcmp(node->name, "data")) { - data->buff = strdup(xmlNodeGetContent(node)); - data->type = PLIST_DATA; - continue; - } - - if (!xmlStrcmp(node->name, "array")) { - data->type = PLIST_ARRAY; - xml_to_node(node, subnode); - continue; - } - - if (!xmlStrcmp(node->name, "dict")) { - data->type = PLIST_DICT; - xml_to_node(node, subnode); - continue; - } - } -} - -void plist_to_xml(plist_t plist, char **plist_xml, uint32_t * length) -{ - if (!plist || !plist_xml || *plist_xml) - return; - xmlDocPtr plist_doc = new_plist(); - xmlNodePtr root_node = xmlDocGetRootElement(plist_doc); - struct xml_node root = { root_node, 0 }; - g_node_children_foreach(plist, G_TRAVERSE_ALL, node_to_xml, &root); - xmlDocDumpMemory(plist_doc, (xmlChar **) plist_xml, length); -} - -void xml_to_plist(const char *plist_xml, uint32_t length, plist_t * plist) -{ - xmlDocPtr plist_doc = xmlReadMemory(plist_xml, length, NULL, NULL, 0); - xmlNodePtr root_node = xmlDocGetRootElement(plist_doc); - - struct plist_data *data = (struct plist_data *) calloc(sizeof(struct plist_data), 1); - *plist = g_node_new(data); - data->type = PLIST_DICT; - xml_to_node(root_node, *plist); -} - - - -/********************************************** -* * -* Binary Plist stuff * -* * -**********************************************/ - -/* Magic marker and size. */ -#define BPLIST_MAGIC "bplist" -#define BPLIST_MAGIC_SIZE 6 - -#define BPLIST_VERSION "00" -#define BPLIST_VERSION_SIZE 2 - - -#define BPLIST_TRL_SIZE 26 -#define BPLIST_TRL_OFFSIZE_IDX 0 -#define BPLIST_TRL_PARMSIZE_IDX 1 -#define BPLIST_TRL_NUMOBJ_IDX 2 -#define BPLIST_TRL_ROOTOBJ_IDX 10 -#define BPLIST_TRL_OFFTAB_IDX 18 - -enum { - BPLIST_NULL = 0x00, - BPLIST_TRUE = 0x08, - BPLIST_FALSE = 0x09, - BPLIST_FILL = 0x0F, /* will be used for length grabbing */ - BPLIST_UINT = 0x10, - BPLIST_REAL = 0x20, - BPLIST_DATE = 0x30, - BPLIST_DATA = 0x40, - BPLIST_STRING = 0x50, - BPLIST_UNICODE = 0x60, - BPLIST_UID = 0x70, - BPLIST_ARRAY = 0xA0, - BPLIST_SET = 0xC0, - BPLIST_DICT = 0xD0, - BPLIST_MASK = 0xF0 -}; - -void byte_convert(char *address, size_t size) -{ - int i = 0, j = 0; - char tmp = '\0'; - - for (i = 0; i < (size / 2); i++) { - tmp = address[i]; - j = ((size - 1) + 0) - i; - address[i] = address[j]; - address[j] = tmp; - } -} - -#include -#define swap_n_bytes(x, n) \ - n == 8 ? bswap_64(*(uint64_t *)(x)) : \ - (n == 4 ? bswap_32(*(uint32_t *)(x)) : \ - (n == 2 ? bswap_16(*(uint16_t *)(x)) : *(x) )) - -#define be64dec(x) bswap_64( *(uint64_t*)(x) ) - -#define get_needed_bytes(x) (x <= 1<<8 ? 1 : ( x <= 1<<16 ? 2 : ( x <= 1<<32 ? 4 : 8))) -#define get_real_bytes(x) (x >> 32 ? 4 : 8) - -GNode *parse_uint_node(char *bnode, uint8_t size, char **next_object) -{ - struct plist_data *data = (struct plist_data *) calloc(sizeof(struct plist_data), 1); - - size = 1 << size; // make length less misleading - switch (size) { - case sizeof(uint8_t): - data->intval = bnode[0]; - break; - case sizeof(uint16_t): - memcpy(&data->intval, bnode, size); - data->intval = ntohs(data->intval); - break; - case sizeof(uint32_t): - memcpy(&data->intval, bnode, size); - data->intval = ntohl(data->intval); - break; - case sizeof(uint64_t): - memcpy(&data->intval, bnode, size); - byte_convert((char *) &data->intval, size); - break; - default: - free(data); - return NULL; - }; - - *next_object = bnode + size; - data->type = PLIST_UINT; - return g_node_new(data); -} - -GNode *parse_real_node(char *bnode, uint8_t size) -{ - struct plist_data *data = (struct plist_data *) calloc(sizeof(struct plist_data), 1); - - size = 1 << size; // make length less misleading - switch (size) { - case sizeof(float): - memcpy(&data->realval, bnode, size); - byte_convert((char *) &data->realval, size); - break; - case sizeof(double): - memcpy(&data->realval, bnode, size); - byte_convert((char *) &data->realval, size); - break; - default: - free(data); - return NULL; - } - data->type = PLIST_REAL; - return g_node_new(data); -} - -GNode *parse_string_node(char *bnode, uint8_t size) -{ - struct plist_data *data = (struct plist_data *) calloc(sizeof(struct plist_data), 1); - - data->type = PLIST_STRING; - data->strval = (char *) malloc(sizeof(char) * (size + 1)); - memcpy(data->strval, bnode, size); - data->strval[size] = '\0'; - - return g_node_new(data); -} - -GNode *parse_unicode_node(char *bnode, uint8_t size) -{ - struct plist_data *data = (struct plist_data *) calloc(sizeof(struct plist_data), 1); - - data->type = PLIST_UNICODE; - data->unicodeval = (wchar_t *) malloc(sizeof(wchar_t) * (size + 1)); - memcpy(data->unicodeval, bnode, size); - data->unicodeval[size] = '\0'; - - return g_node_new(data); -} - -GNode *parse_data_node(char *bnode, uint64_t size, uint32_t ref_size) -{ - struct plist_data *data = (struct plist_data *) calloc(sizeof(struct plist_data), 1); - - data->type = PLIST_DATA; - data->length = size; - data->buff = (char *) malloc(sizeof(char) * size); - memcpy(data->buff, bnode, sizeof(char) * size); - - return g_node_new(data); -} - -GNode *parse_dict_node(char *bnode, uint64_t size, uint32_t ref_size) -{ - struct plist_data *data = (struct plist_data *) calloc(sizeof(struct plist_data), 1); - - data->type = PLIST_DICT; - data->length = size; - data->buff = (char *) malloc(sizeof(char) * size * ref_size * 2); - memcpy(data->buff, bnode, sizeof(char) * size * ref_size * 2); - - return g_node_new(data); -} - -GNode *parse_array_node(char *bnode, uint64_t size, uint32_t ref_size) -{ - struct plist_data *data = (struct plist_data *) calloc(sizeof(struct plist_data), 1); - - data->type = PLIST_ARRAY; - data->length = size; - data->buff = (char *) malloc(sizeof(char) * size * ref_size); - memcpy(data->buff, bnode, sizeof(char) * size * ref_size); - - return g_node_new(data); -} - -plist_type plist_get_node_type(plist_t node) -{ - return ((struct plist_data *) node->data)->type; -} - -uint64_t plist_get_node_uint_val(plist_t node) -{ - if (PLIST_UINT == plist_get_node_type(node)) - return ((struct plist_data *) node->data)->intval; - else - return 0; -} - -GNode *parse_bin_node(char *object, uint8_t dict_size, char **next_object) -{ - if (!object) - return NULL; - - uint16_t type = *object & 0xF0; - uint64_t size = *object & 0x0F; - object++; - - switch (type) { - - case BPLIST_NULL: - switch (size) { - - case BPLIST_TRUE: - { - struct plist_data *data = (struct plist_data *) calloc(sizeof(struct plist_data), 1); - data->type = PLIST_BOOLEAN; - data->boolval = TRUE; - return g_node_new(data); - } - - case BPLIST_FALSE: - { - struct plist_data *data = (struct plist_data *) calloc(sizeof(struct plist_data), 1); - data->type = PLIST_BOOLEAN; - data->boolval = FALSE; - return g_node_new(data); - } - - case BPLIST_NULL: - default: - return NULL; - } - - case BPLIST_UINT: - return parse_uint_node(object, size, next_object); - - case BPLIST_REAL: - return parse_real_node(object, size); - - case BPLIST_DATE: - if (3 != size) - return NULL; - else - return parse_real_node(object, size); - - case BPLIST_DATA: - if (0x0F == size) { - plist_t size_node = parse_bin_node(object, dict_size, &object); - if (plist_get_node_type(size_node) != PLIST_UINT) - return NULL; - size = plist_get_node_uint_val(size_node); - } - return parse_data_node(object, size, dict_size); - - case BPLIST_STRING: - if (0x0F == size) { - plist_t size_node = parse_bin_node(object, dict_size, &object); - if (plist_get_node_type(size_node) != PLIST_UINT) - return NULL; - size = plist_get_node_uint_val(size_node); - } - return parse_string_node(object, size); - - case BPLIST_UNICODE: - if (0x0F == size) { - plist_t size_node = parse_bin_node(object, dict_size, &object); - if (plist_get_node_type(size_node) != PLIST_UINT) - return NULL; - size = plist_get_node_uint_val(size_node); - } - return parse_unicode_node(object, size); - - case BPLIST_UID: - case BPLIST_ARRAY: - if (0x0F == size) { - plist_t size_node = parse_bin_node(object, dict_size, &object); - if (plist_get_node_type(size_node) != PLIST_UINT) - return NULL; - size = plist_get_node_uint_val(size_node); - } - return parse_array_node(object, size, dict_size); - - case BPLIST_SET: - case BPLIST_DICT: - if (0x0F == size) { - plist_t size_node = parse_bin_node(object, dict_size, &object); - if (plist_get_node_type(size_node) != PLIST_UINT) - return NULL; - object++; - size = plist_get_node_uint_val(size_node); - } - return parse_dict_node(object, size, dict_size); - - } - return NULL; -} - -gpointer copy_plist_data(gconstpointer src, gpointer data) -{ - struct plist_data *srcdata = (struct plist_data *) src; - struct plist_data *dstdata = (struct plist_data *) calloc(sizeof(struct plist_data), 1); - - dstdata->type = srcdata->type; - dstdata->length = srcdata->length; - switch (dstdata->type) { - case PLIST_BOOLEAN: - dstdata->boolval = srcdata->boolval; - break; - case PLIST_UINT: - dstdata->intval = srcdata->intval; - break; - case PLIST_DATE: - case PLIST_REAL: - dstdata->realval = srcdata->realval; - break; - case PLIST_KEY: - case PLIST_STRING: - dstdata->strval = strdup(srcdata->strval); - break; - case PLIST_UNICODE: - dstdata->unicodeval = wcsdup(srcdata->unicodeval); - break; - case PLIST_DATA: - case PLIST_ARRAY: - case PLIST_DICT: - dstdata->buff = (char *) malloc(sizeof(char *) * srcdata->length); - memcpy(dstdata->buff, srcdata->buff, sizeof(char *) * srcdata->length); - break; - - default: - break; - } - - return dstdata; -} - -void bin_to_plist(const char *plist_bin, uint32_t length, plist_t * plist) -{ - //first check we have enough data - if (!(length >= BPLIST_MAGIC_SIZE + BPLIST_VERSION_SIZE + BPLIST_TRL_SIZE)) - return; - //check that plist_bin in actually a plist - if (memcmp(plist_bin, BPLIST_MAGIC, BPLIST_MAGIC_SIZE) != 0) - return; - //check for known version - if (memcmp(plist_bin + BPLIST_MAGIC_SIZE, BPLIST_VERSION, BPLIST_VERSION_SIZE) != 0) - return; - - //now parse trailer - const char *trailer = plist_bin + (length - BPLIST_TRL_SIZE); - - uint8_t offset_size = trailer[BPLIST_TRL_OFFSIZE_IDX]; - uint8_t dict_param_size = trailer[BPLIST_TRL_PARMSIZE_IDX]; - uint64_t num_objects = be64dec(trailer + BPLIST_TRL_NUMOBJ_IDX); - uint64_t root_object = be64dec(trailer + BPLIST_TRL_ROOTOBJ_IDX); - uint64_t offset_table_index = be64dec(trailer + BPLIST_TRL_OFFTAB_IDX); - - log_debug_msg("Offset size: %i\n", offset_size); - log_debug_msg("Ref size: %i\n", dict_param_size); - log_debug_msg("Number of objects: %lli\n", num_objects); - log_debug_msg("Root object index: %lli\n", root_object); - log_debug_msg("Offset table index: %lli\n", offset_table_index); - - if (num_objects == 0) - return; - - //allocate serialized array of nodes - plist_t *nodeslist = NULL; - nodeslist = (plist_t *) malloc(sizeof(plist_t) * num_objects); - - if (!nodeslist) - return; - - //parse serialized nodes - uint64_t i = 0; - uint64_t current_offset = 0; - const char *offset_table = plist_bin + offset_table_index; - for (i = 0; i < num_objects; i++) { - current_offset = swap_n_bytes(offset_table + i * offset_size, offset_size); - - log_debug_msg("parse_nodes: current_offset = %i\n", current_offset); - char *obj = plist_bin + current_offset; - nodeslist[i] = parse_bin_node(obj, dict_param_size, &obj); - log_debug_msg("parse_nodes: parse_raw_node done\n"); - } - - //setup children for structured types - int j = 0, str_i = 0, str_j = 0; - uint32_t index1 = 0, index2 = 0; - - for (i = 0; i < num_objects; i++) { - - log_debug_msg("parse_nodes: on node %i\n", i); - struct plist_data *data = (struct plist_data *) nodeslist[i]->data; - - switch (data->type) { - case PLIST_DICT: - log_debug_msg("parse_nodes: dictionary found\n"); - for (j = 0; j < data->length; j++) { - str_i = j * dict_param_size; - str_j = (j + data->length) * dict_param_size; - - index1 = swap_n_bytes(data->buff + str_i, dict_param_size); - index2 = swap_n_bytes(data->buff + str_j, dict_param_size); - - //first one is actually a key - ((struct plist_data *) nodeslist[index1]->data)->type = PLIST_KEY; - - if (G_NODE_IS_ROOT(nodeslist[index1])) - g_node_append(nodeslist[i], nodeslist[index1]); - else - g_node_append(nodeslist[i], g_node_copy_deep(nodeslist[index1], copy_plist_data, NULL)); - - if (G_NODE_IS_ROOT(nodeslist[index2])) - g_node_append(nodeslist[i], nodeslist[index2]); - else - g_node_append(nodeslist[i], g_node_copy_deep(nodeslist[index2], copy_plist_data, NULL)); - } - - free(data->buff); - break; - - case PLIST_ARRAY: - log_debug_msg("parse_nodes: array found\n"); - for (j = 0; j < data->length; j++) { - str_j = j * dict_param_size; - index1 = swap_n_bytes(data->buff + str_j, dict_param_size); - - //g_node_append(nodeslist[i], nodeslist[index1]); - if (G_NODE_IS_ROOT(nodeslist[index1])) - g_node_append(nodeslist[i], nodeslist[index1]); - else - g_node_append(nodeslist[i], g_node_copy_deep(nodeslist[index1], copy_plist_data, NULL)); - } - free(data->buff); - break; - default: - break; - } - } - - *plist = nodeslist[root_object]; -} - GNode *find_query_node(plist_t plist, char *key, char *request) { if (!plist) @@ -998,347 +226,15 @@ void get_type_and_value(GNode * node, plist_type * type, void *value) } } -guint plist_data_hash(gconstpointer key) -{ - struct plist_data *data = (struct plist_data *) ((GNode *) key)->data; - - guint hash = data->type; - guint i = 0; - - char *buff = NULL; - guint size = 0; - - switch (data->type) { - case PLIST_BOOLEAN: - case PLIST_UINT: - case PLIST_REAL: - buff = (char *) &data->intval; - size = 8; - break; - case PLIST_KEY: - case PLIST_STRING: - buff = data->strval; - size = strlen(buff); - break; - case PLIST_UNICODE: - buff = data->unicodeval; - size = strlen(buff) * sizeof(wchar_t); - break; - case PLIST_DATA: - case PLIST_ARRAY: - case PLIST_DICT: - //for these types only hash pointer - buff = &key; - size = sizeof(gconstpointer); - break; - case PLIST_DATE: - default: - break; - } - - //now perform hash - for (i = 0; i < size; buff++, i++) - hash = hash << 7 ^ (*buff); - - return hash; -} - -gboolean plist_data_compare(gconstpointer a, gconstpointer b) -{ - if (!a || !b) - return FALSE; - - if (!((GNode *) a)->data || !((GNode *) b)->data) - return FALSE; - - struct plist_data *val_a = (struct plist_data *) ((GNode *) a)->data; - struct plist_data *val_b = (struct plist_data *) ((GNode *) b)->data; - - if (val_a->type != val_b->type) - return FALSE; - - switch (val_a->type) { - case PLIST_BOOLEAN: - case PLIST_UINT: - case PLIST_REAL: - if (val_a->intval == val_b->intval) //it is an union so this is sufficient - return TRUE; - else - return FALSE; - - case PLIST_KEY: - case PLIST_STRING: - if (!strcmp(val_a->strval, val_b->strval)) - return TRUE; - else - return FALSE; - case PLIST_UNICODE: - if (!strcmp(val_a->unicodeval, val_b->unicodeval)) - return TRUE; - else - return FALSE; - - case PLIST_DATA: - case PLIST_ARRAY: - case PLIST_DICT: - //compare pointer - if (a == b) - return TRUE; - else - return FALSE; - break; - case PLIST_DATE: - default: - break; - } - return FALSE; -} - -struct serialize_s { - GPtrArray *objects; - GHashTable *ref_table; -}; - -void serialize_plist(GNode * node, gpointer data) -{ - struct serialize_s *ser = (struct serialize_s *) data; - uint64_t current_index = ser->objects->len; - - //first check that node is not yet in objects - gpointer val = g_hash_table_lookup(ser->ref_table, node); - if (val) { - //data is already in table - return; - } - //insert new ref - g_hash_table_insert(ser->ref_table, node, GUINT_TO_POINTER(current_index)); - - //now append current node to object array - g_ptr_array_add(ser->objects, node); - - //now recurse on children - g_node_children_foreach(node, G_TRAVERSE_ALL, serialize_plist, data); - return; -} - - - -void write_int(GByteArray * bplist, uint64_t val) -{ - uint64_t size = get_needed_bytes(val); - uint8_t *buff = (uint8_t *) malloc(sizeof(uint8_t) + size); - buff[0] = BPLIST_UINT | size >> 1; - memcpy(buff + 1, &val, size); - swap_n_bytes(buff + 1, size); - g_byte_array_append(bplist, buff, sizeof(uint8_t) + size); - free(buff); -} - -void write_real(GByteArray * bplist, double val) -{ - uint64_t size = get_real_bytes(*((uint64_t *) & val)); //cheat to know used space - uint8_t *buff = (uint8_t *) malloc(sizeof(uint8_t) + size); - buff[0] = BPLIST_REAL | size >> 1; - memcpy(buff + 1, &val, size); - swap_n_bytes(buff + 1, size); - g_byte_array_append(bplist, buff, sizeof(uint8_t) + size); - free(buff); -} - -void write_raw_data(GByteArray * bplist, uint8_t mark, uint8_t * val, uint64_t size) -{ - uint8_t marker = mark | (size < 15 ? size : 0xf); - g_byte_array_append(bplist, &marker, sizeof(uint8_t)); - if (size >= 15) { - GByteArray *int_buff = g_byte_array_new(); - write_int(int_buff, size); - g_byte_array_append(bplist, int_buff->data, int_buff->len); - g_byte_array_free(int_buff, TRUE); - } - uint8_t *buff = (uint8_t *) malloc(size); - memcpy(buff, val, size); - g_byte_array_append(bplist, buff, size); - free(buff); -} - -void write_data(GByteArray * bplist, uint8_t * val, uint64_t size) -{ - write_raw_data(bplist, BPLIST_DATA, val, size); -} - -void write_string(GByteArray * bplist, char *val) -{ - uint64_t size = strlen(val); - write_raw_data(bplist, BPLIST_STRING, val, size); -} - -void write_array(GByteArray * bplist, GNode * node, GHashTable * ref_table, uint8_t dict_param_size) -{ - uint64_t size = g_node_n_children(node); - uint8_t marker = BPLIST_ARRAY | (size < 15 ? size : 0xf); - g_byte_array_append(bplist, &marker, sizeof(uint8_t)); - if (size >= 15) { - GByteArray *int_buff = g_byte_array_new(); - write_int(int_buff, size); - g_byte_array_append(bplist, int_buff->data, int_buff->len); - g_byte_array_free(int_buff, TRUE); - } - - uint64_t idx = 0; - uint8_t *buff = (uint8_t *) malloc(size * dict_param_size); - - GNode *cur = NULL; - int i = 0; - for (i = 0, cur = node->children; cur && i < size; cur = cur->next, i++) { - idx = GPOINTER_TO_UINT(g_hash_table_lookup(ref_table, cur)); - memcpy(buff + i * dict_param_size, &idx, dict_param_size); - swap_n_bytes(buff + i * dict_param_size, dict_param_size); - } - - //now append to bplist - g_byte_array_append(bplist, buff, size * dict_param_size); - free(buff); - -} - -void write_dict(GByteArray * bplist, GNode * node, GHashTable * ref_table, uint8_t dict_param_size) +plist_type plist_get_node_type(plist_t node) { - uint64_t size = g_node_n_children(node) / 2; - uint8_t marker = BPLIST_ARRAY | (size < 15 ? size : 0xf); - g_byte_array_append(bplist, &marker, sizeof(uint8_t)); - if (size >= 15) { - GByteArray *int_buff = g_byte_array_new(); - write_int(int_buff, size); - g_byte_array_append(bplist, int_buff->data, int_buff->len); - g_byte_array_free(int_buff, TRUE); - } - - uint64_t idx1 = 0; - uint64_t idx2 = 0; - uint8_t *buff = (uint8_t *) malloc(size * 2 * dict_param_size); - - GNode *cur = NULL; - int i = 0; - for (i = 0, cur = node->children; cur && i < size; cur = cur->next->next, i++) { - idx1 = GPOINTER_TO_UINT(g_hash_table_lookup(ref_table, cur)); - memcpy(buff + i * dict_param_size, &idx1, dict_param_size); - swap_n_bytes(buff + i * dict_param_size, dict_param_size); - - idx2 = GPOINTER_TO_UINT(g_hash_table_lookup(ref_table, cur->next)); - memcpy(buff + (i + size) * dict_param_size, &idx2, dict_param_size); - swap_n_bytes(buff + (i + size) * dict_param_size, dict_param_size); - } - - //now append to bplist - g_byte_array_append(bplist, buff, size * dict_param_size); - free(buff); - + return ((struct plist_data *) node->data)->type; } -void plist_to_bin(plist_t plist, char **plist_bin, uint32_t * length) +uint64_t plist_get_node_uint_val(plist_t node) { - //first serialize tree - - //list of objects - GPtrArray *objects = g_ptr_array_new(); - //hashtable to write only once same nodes - GHashTable *ref_table = g_hash_table_new(plist_data_hash, plist_data_compare); - - //serialize plist - struct serialize_s ser_s = { objects, ref_table }; - g_node_children_foreach(plist, G_TRAVERSE_ALL, serialize_plist, &ser_s); - - //now stream to output buffer - uint8_t offset_size = 0; //unknown yet - uint8_t dict_param_size = get_needed_bytes(objects->len); - uint64_t num_objects = objects->len; - uint64_t root_object = 0; //root is first in list - uint64_t offset_table_index = 0; //unknown yet - - //setup a dynamic bytes array to store bplist in - GByteArray *bplist_buff = g_byte_array_new(); - - //set magic number and version - g_byte_array_append(bplist_buff, BPLIST_MAGIC, BPLIST_MAGIC_SIZE); - g_byte_array_append(bplist_buff, BPLIST_VERSION, BPLIST_VERSION_SIZE); - - //write objects and table - int i = 0; - uint8_t *buff = NULL; - uint8_t size = 0; - uint64_t offsets[num_objects]; - for (i = 0; i < num_objects; i++) { - - offsets[i] = bplist_buff->len; - struct plist_data *data = (struct plist_data *) ((GNode *) g_ptr_array_index(objects, i))->data; - - switch (data->type) { - case PLIST_BOOLEAN: - buff = (uint8_t *) malloc(sizeof(uint8_t)); - buff[0] = data->boolval ? BPLIST_TRUE : BPLIST_FALSE; - g_byte_array_append(bplist_buff, buff, sizeof(uint8_t)); - free(buff); - break; - - case PLIST_UINT: - write_int(bplist_buff, data->intval); - break; - - case PLIST_REAL: - write_real(bplist_buff, data->realval); - break; - - case PLIST_KEY: - case PLIST_STRING: - write_string(bplist_buff, data->strval); - break; - case PLIST_UNICODE: - //TODO - break; - case PLIST_DATA: - write_data(bplist_buff, data->strval, data->length); - case PLIST_ARRAY: - write_array(bplist_buff, g_ptr_array_index(objects, i), ref_table, dict_param_size); - break; - case PLIST_DICT: - write_dict(bplist_buff, g_ptr_array_index(objects, i), ref_table, dict_param_size); - break; - case PLIST_DATE: - //TODO - break; - default: - break; - } - } - - //write offsets - offset_size = get_needed_bytes(bplist_buff->len); - for (i = 0; i <= num_objects; i++) { - uint8_t *buff = (uint8_t *) malloc(offset_size); - memcpy(buff, offsets + i, offset_size); - swap_n_bytes(buff, offset_size); - g_byte_array_append(bplist_buff, buff, offset_size); - free(buff); - } - - //setup trailer - num_objects = bswap_64(num_objects); - root_object = bswap_64(root_object); - offset_table_index = bswap_64(offset_table_index); - - char trailer[BPLIST_TRL_SIZE]; - memcpy(trailer + BPLIST_TRL_OFFSIZE_IDX, &offset_size, sizeof(uint8_t)); - memcpy(trailer + BPLIST_TRL_PARMSIZE_IDX, &dict_param_size, sizeof(uint8_t)); - memcpy(trailer + BPLIST_TRL_NUMOBJ_IDX, &num_objects, sizeof(uint64_t)); - memcpy(trailer + BPLIST_TRL_ROOTOBJ_IDX, &root_object, sizeof(uint64_t)); - memcpy(trailer + BPLIST_TRL_OFFTAB_IDX, &offset_table_index, sizeof(uint64_t)); - - g_byte_array_append(bplist_buff, trailer, BPLIST_TRL_SIZE); - - //duplicate buffer - *plist_bin = (char *) malloc(bplist_buff->len); - memcpy(*plist_bin, bplist_buff->data, bplist_buff->len); - *length = bplist_buff->len; - - g_byte_array_free(bplist_buff, TRUE); + if (PLIST_UINT == plist_get_node_type(node)) + return ((struct plist_data *) node->data)->intval; + else + return 0; } -- cgit v1.1-32-gdbae From 9ca887308d59e6cb5bf684f9f3bd968118e8014f Mon Sep 17 00:00:00 2001 From: Jonathan Beck Date: Fri, 12 Dec 2008 22:05:44 +0100 Subject: Fix some bugs in binary plist generation. --- src/plist.c | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) (limited to 'src/plist.c') diff --git a/src/plist.c b/src/plist.c index 76ae954..66a74c3 100644 --- a/src/plist.c +++ b/src/plist.c @@ -29,7 +29,7 @@ #include -void plist_new_plist(plist_t * plist) +void plist_new_dict(plist_t * plist) { if (*plist != NULL) return; @@ -38,7 +38,16 @@ void plist_new_plist(plist_t * plist) *plist = g_node_new(data); } -void plist_new_dict_in_plist(plist_t plist, dict_t * dict) +void plist_new_array(plist_t * plist) +{ + if (*plist != NULL) + return; + struct plist_data *data = (struct plist_data *) calloc(sizeof(struct plist_data), 1); + data->type = PLIST_ARRAY; + *plist = g_node_new(data); +} + +void plist_new_dict_in_plist(plist_t plist, plist_t * dict) { if (!plist || *dict) return; @@ -49,9 +58,6 @@ void plist_new_dict_in_plist(plist_t plist, dict_t * dict) g_node_append(plist, *dict); } -void plist_new_array_in_plist(plist_t plist, int length, plist_type type, void **values, array_t * array) -{ -} /** Adds a new key pair to a dict. * @@ -61,7 +67,7 @@ void plist_new_array_in_plist(plist_t plist, int length, plist_type type, void * * @param value a pointer to the actual buffer containing the value. WARNING : the buffer is supposed to match the type of the value * */ -void plist_add_dict_element(dict_t dict, char *key, plist_type type, void *value) +void plist_add_dict_element(plist_t dict, char *key, plist_type type, void *value) { if (!dict || !key || !value) return; @@ -110,7 +116,7 @@ void plist_free(plist_t plist) g_node_destroy(plist); } -GNode *find_query_node(plist_t plist, char *key, char *request) +plist_t find_query_node(plist_t plist, char *key, char *request) { if (!plist) return NULL; @@ -167,7 +173,7 @@ char compare_node_value(plist_type type, struct plist_data *data, void *value) return res; } -GNode *find_node(plist_t plist, plist_type type, void *value) +plist_t find_node(plist_t plist, plist_type type, void *value) { if (!plist) return NULL; @@ -228,7 +234,10 @@ void get_type_and_value(GNode * node, plist_type * type, void *value) plist_type plist_get_node_type(plist_t node) { - return ((struct plist_data *) node->data)->type; + if (node && node->data) + return ((struct plist_data *) node->data)->type; + else + return PLIST_NONE; } uint64_t plist_get_node_uint_val(plist_t node) -- cgit v1.1-32-gdbae From 3d8ba053deeacd74e621469d3d45d1db38ee411a Mon Sep 17 00:00:00 2001 From: Jonathan Beck Date: Fri, 12 Dec 2008 23:39:33 +0100 Subject: Change from Base64 encoded buffers to real buffers. Base64 decoding/encoding only happens in xml plists. --- src/plist.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'src/plist.c') diff --git a/src/plist.c b/src/plist.c index 66a74c3..932ea5e 100644 --- a/src/plist.c +++ b/src/plist.c @@ -67,7 +67,7 @@ void plist_new_dict_in_plist(plist_t plist, plist_t * dict) * @param value a pointer to the actual buffer containing the value. WARNING : the buffer is supposed to match the type of the value * */ -void plist_add_dict_element(plist_t dict, char *key, plist_type type, void *value) +void plist_add_dict_element(plist_t dict, char *key, plist_type type, void *value, uint64_t length) { if (!dict || !key || !value) return; @@ -81,6 +81,7 @@ void plist_add_dict_element(plist_t dict, char *key, plist_type type, void *valu //now handle value struct plist_data *val = (struct plist_data *) calloc(sizeof(struct plist_data), 1); val->type = type; + val->length = length; switch (type) { case PLIST_BOOLEAN: @@ -99,7 +100,7 @@ void plist_add_dict_element(plist_t dict, char *key, plist_type type, void *valu val->unicodeval = wcsdup((wchar_t *) value); break; case PLIST_DATA: - val->buff = strdup((char *) value); + memcpy(val->buff, value, length); break; case PLIST_ARRAY: case PLIST_DICT: @@ -195,7 +196,7 @@ plist_t find_node(plist_t plist, plist_type type, void *value) return NULL; } -void get_type_and_value(GNode * node, plist_type * type, void *value) +void get_type_and_value(GNode * node, plist_type * type, void *value, uint64_t * length) { if (!node) return; @@ -203,6 +204,7 @@ void get_type_and_value(GNode * node, plist_type * type, void *value) struct plist_data *data = (struct plist_data *) node->data; *type = data->type; + *length = data->length; switch (*type) { case PLIST_BOOLEAN: -- cgit v1.1-32-gdbae From 4301ef9bb8e9d06ffa4e9172191d58ede5e16f5d Mon Sep 17 00:00:00 2001 From: Jonathan Beck Date: Sat, 13 Dec 2008 18:12:46 +0100 Subject: fork out plist stuff in libplist and migrate libiphone to use it. --- src/plist.c | 251 ------------------------------------------------------------ 1 file changed, 251 deletions(-) delete mode 100644 src/plist.c (limited to 'src/plist.c') diff --git a/src/plist.c b/src/plist.c deleted file mode 100644 index 932ea5e..0000000 --- a/src/plist.c +++ /dev/null @@ -1,251 +0,0 @@ -/* - * plist.c - * Builds plist XML structures. - * - * Copyright (c) 2008 Zach C. All Rights Reserved. - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - */ - - -#include -#include -#include "utils.h" -#include "plist.h" -#include -#include -#include - - -void plist_new_dict(plist_t * plist) -{ - if (*plist != NULL) - return; - struct plist_data *data = (struct plist_data *) calloc(sizeof(struct plist_data), 1); - data->type = PLIST_DICT; - *plist = g_node_new(data); -} - -void plist_new_array(plist_t * plist) -{ - if (*plist != NULL) - return; - struct plist_data *data = (struct plist_data *) calloc(sizeof(struct plist_data), 1); - data->type = PLIST_ARRAY; - *plist = g_node_new(data); -} - -void plist_new_dict_in_plist(plist_t plist, plist_t * dict) -{ - if (!plist || *dict) - return; - - struct plist_data *data = (struct plist_data *) calloc(sizeof(struct plist_data), 1); - data->type = PLIST_DICT; - *dict = g_node_new(data); - g_node_append(plist, *dict); -} - - -/** Adds a new key pair to a dict. - * - * @param dict The dict node in the plist. - * @param key the key name of the key pair. - * @param type The the type of the value in the key pair. - * @param value a pointer to the actual buffer containing the value. WARNING : the buffer is supposed to match the type of the value - * - */ -void plist_add_dict_element(plist_t dict, char *key, plist_type type, void *value, uint64_t length) -{ - if (!dict || !key || !value) - return; - - struct plist_data *data = (struct plist_data *) calloc(sizeof(struct plist_data), 1); - data->type = PLIST_KEY; - data->strval = strdup(key); - GNode *keynode = g_node_new(data); - g_node_append(dict, keynode); - - //now handle value - struct plist_data *val = (struct plist_data *) calloc(sizeof(struct plist_data), 1); - val->type = type; - val->length = length; - - switch (type) { - case PLIST_BOOLEAN: - val->boolval = *((char *) value); - break; - case PLIST_UINT: - val->intval = *((uint64_t *) value); - break; - case PLIST_REAL: - val->realval = *((double *) value); - break; - case PLIST_STRING: - val->strval = strdup((char *) value); - break; - case PLIST_UNICODE: - val->unicodeval = wcsdup((wchar_t *) value); - break; - case PLIST_DATA: - memcpy(val->buff, value, length); - break; - case PLIST_ARRAY: - case PLIST_DICT: - case PLIST_DATE: - default: - break; - } - GNode *valnode = g_node_new(val); - g_node_append(dict, valnode); -} - -void plist_free(plist_t plist) -{ - g_node_destroy(plist); -} - -plist_t find_query_node(plist_t plist, char *key, char *request) -{ - if (!plist) - return NULL; - - GNode *current = NULL; - for (current = plist->children; current; current = current->next) { - - struct plist_data *data = (struct plist_data *) current->data; - - if (data->type == PLIST_KEY && !strcmp(data->strval, key) && current->next) { - - data = (struct plist_data *) current->next->data; - if (data->type == PLIST_STRING && !strcmp(data->strval, request)) - return current->next; - } - if (data->type == PLIST_DICT || data->type == PLIST_ARRAY) { - GNode *sub = find_query_node(current, key, request); - if (sub) - return sub; - } - } - return NULL; -} - -char compare_node_value(plist_type type, struct plist_data *data, void *value) -{ - char res = FALSE; - switch (type) { - case PLIST_BOOLEAN: - res = data->boolval == *((char *) value) ? TRUE : FALSE; - break; - case PLIST_UINT: - res = data->intval == *((uint64_t *) value) ? TRUE : FALSE; - break; - case PLIST_REAL: - res = data->realval == *((double *) value) ? TRUE : FALSE; - break; - case PLIST_KEY: - case PLIST_STRING: - res = !strcmp(data->strval, ((char *) value)); - break; - case PLIST_UNICODE: - res = !wcscmp(data->unicodeval, ((wchar_t *) value)); - break; - case PLIST_DATA: - res = !strcmp(data->buff, ((char *) value)); - break; - case PLIST_ARRAY: - case PLIST_DICT: - case PLIST_DATE: - default: - break; - } - return res; -} - -plist_t find_node(plist_t plist, plist_type type, void *value) -{ - if (!plist) - return NULL; - - GNode *current = NULL; - for (current = plist->children; current; current = current->next) { - - struct plist_data *data = (struct plist_data *) current->data; - - if (data->type == type && compare_node_value(type, data, value)) { - return current; - } - if (data->type == PLIST_DICT || data->type == PLIST_ARRAY) { - GNode *sub = find_node(current, type, value); - if (sub) - return sub; - } - } - return NULL; -} - -void get_type_and_value(GNode * node, plist_type * type, void *value, uint64_t * length) -{ - if (!node) - return; - - struct plist_data *data = (struct plist_data *) node->data; - - *type = data->type; - *length = data->length; - - switch (*type) { - case PLIST_BOOLEAN: - *((char *) value) = data->boolval; - break; - case PLIST_UINT: - *((uint64_t *) value) = data->intval; - break; - case PLIST_REAL: - *((double *) value) = data->realval; - break; - case PLIST_STRING: - *((char **) value) = strdup(data->strval); - break; - case PLIST_UNICODE: - *((wchar_t **) value) = wcsdup(data->unicodeval); - break; - case PLIST_KEY: - *((char **) value) = strdup(data->strval); - break; - case PLIST_DATA: - case PLIST_ARRAY: - case PLIST_DICT: - case PLIST_DATE: - default: - break; - } -} - -plist_type plist_get_node_type(plist_t node) -{ - if (node && node->data) - return ((struct plist_data *) node->data)->type; - else - return PLIST_NONE; -} - -uint64_t plist_get_node_uint_val(plist_t node) -{ - if (PLIST_UINT == plist_get_node_type(node)) - return ((struct plist_data *) node->data)->intval; - else - return 0; -} -- cgit v1.1-32-gdbae