summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorGravatar Jonathan Beck2008-12-08 22:47:02 +0100
committerGravatar Jonathan Beck2008-12-08 22:47:02 +0100
commit1a06347d27ca51283de3a9ff21e138a3ea9ba9b6 (patch)
treeaa863b9df8557c8dc04da2b6e6f982640bc3ac26 /src
parent7563917755cf58cee80fbd5bc56a1ab0f563963a (diff)
downloadlibimobiledevice-1a06347d27ca51283de3a9ff21e138a3ea9ba9b6.tar.gz
libimobiledevice-1a06347d27ca51283de3a9ff21e138a3ea9ba9b6.tar.bz2
cleanup binary parsing and move stuff around.
Diffstat (limited to 'src')
-rw-r--r--src/lockdown.c2
-rw-r--r--src/plist.c854
-rw-r--r--src/plist.h18
3 files changed, 489 insertions, 385 deletions
diff --git a/src/lockdown.c b/src/lockdown.c
index 55e2e65..ae077b7 100644
--- a/src/lockdown.c
+++ b/src/lockdown.c
@@ -963,7 +963,7 @@ iphone_error_t iphone_lckd_start_service(iphone_lckd_client_t client, const char
963 if (result_key_type == PLIST_KEY && 963 if (result_key_type == PLIST_KEY &&
964 result_value_type == PLIST_STRING && 964 result_value_type == PLIST_STRING &&
965 port_key_type == PLIST_KEY && 965 port_key_type == PLIST_KEY &&
966 port_value_type == PLIST_UINT64 && 966 port_value_type == PLIST_UINT &&
967 !strcmp(result_key, "Result") && !strcmp(result_value, "Success") && !strcmp(port_key, "Port")) { 967 !strcmp(result_key, "Result") && !strcmp(result_value, "Success") && !strcmp(port_key, "Port")) {
968 port_loc = port_value; 968 port_loc = port_value;
969 ret = IPHONE_E_SUCCESS; 969 ret = IPHONE_E_SUCCESS;
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 @@
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */ 20 */
21 21
22#include <libxml/parser.h> 22
23#include <libxml/tree.h>
24#include <string.h> 23#include <string.h>
25#include <assert.h> 24#include <assert.h>
26#include "utils.h" 25#include "utils.h"
27#include "plist.h" 26#include "plist.h"
28#include <wchar.h> 27#include <wchar.h>
29 28
29/**********************************************
30* *
31* Abstract Plist stuff *
32* *
33**********************************************/
34
35
36
37
38
30 39
31const char *plist_base = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
32<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n\
33<plist version=\"1.0\">\n\
34</plist>\0";
35 40
36/** Formats a block of text to be a given indentation and width. 41/** Formats a block of text to be a given indentation and width.
37 * 42 *
@@ -72,34 +77,7 @@ char *format_string(const char *buf, int cols, int depth)
72 return new_buf; 77 return new_buf;
73} 78}
74 79
75/** Creates a new plist XML document.
76 *
77 * @return The plist XML document.
78 */
79xmlDocPtr new_plist()
80{
81 char *plist = strdup(plist_base);
82 xmlDocPtr plist_xml = xmlReadMemory(plist, strlen(plist), NULL, NULL, 0);
83
84 if (!plist_xml)
85 return NULL;
86
87 free(plist);
88
89 return plist_xml;
90}
91
92/** Destroys a previously created XML document.
93 *
94 * @param plist The XML document to destroy.
95 */
96void free_plist(xmlDocPtr plist)
97{
98 if (!plist)
99 return;
100 80
101 xmlFreeDoc(plist);
102}
103 81
104 82
105/* 83/*
@@ -117,29 +95,7 @@ void free_plist(xmlDocPtr plist)
117 * - parse_nodes() will return the first node it encounters, which is usually the "root" node. 95 * - parse_nodes() will return the first node it encounters, which is usually the "root" node.
118 */ 96 */
119 97
120uint32_t uipow(uint32_t value, uint32_t power)
121{
122 if (!power)
123 return 1;
124 int i = 0, oVal = value;
125 for (i = 1; i < power; i++) {
126 value *= oVal;
127 }
128 return value;
129}
130 98
131void byte_convert(char *address, size_t size)
132{
133 int i = 0, j = 0;
134 char tmp = '\0';
135
136 for (i = 0; i < (size / 2); i++) {
137 tmp = address[i];
138 j = ((size - 1) + 0) - i;
139 address[i] = address[j];
140 address[j] = tmp;
141 }
142}
143 99
144void print_bytes(char *val, size_t size) 100void print_bytes(char *val, size_t size)
145{ 101{
@@ -154,39 +110,17 @@ void print_bytes(char *val, size_t size)
154struct plist_data { 110struct plist_data {
155 union { 111 union {
156 char boolval; 112 char boolval;
157 uint8_t intval8; 113 uint64_t intval;
158 uint16_t intval16; 114 double realval;
159 uint32_t intval32;
160 uint64_t intval64;
161 float realval32;
162 double realval64;
163 char *strval; 115 char *strval;
164 wchar_t *unicodeval; 116 wchar_t *unicodeval;
165 struct { 117 char *buff;
166 char *buff;
167 uint8_t ref_size;
168 };
169 }; 118 };
170 uint64_t length; 119 uint64_t length;
171 plist_type type; 120 plist_type type;
172}; 121};
173 122
174enum { 123
175 BPLIST_TRUE = 0x08,
176 BPLIST_FALSE = 0x09,
177 BPLIST_FILL = 0x0F, /* will be used for length grabbing */
178 BPLIST_INT = 0x10,
179 BPLIST_REAL = 0x20,
180 BPLIST_DATE = 0x33,
181 BPLIST_DATA = 0x40,
182 BPLIST_STRING = 0x50,
183 BPLIST_UNICODE = 0x60,
184 BPLIST_UID = 0x70,
185 BPLIST_ARRAY = 0xA0,
186 BPLIST_SET = 0xC0,
187 BPLIST_DICT = 0xD0,
188 BPLIST_MASK = 0xF0
189};
190 124
191void plist_new_plist(plist_t * plist) 125void plist_new_plist(plist_t * plist)
192{ 126{
@@ -239,23 +173,11 @@ void plist_add_dict_element(dict_t dict, char *key, plist_type type, void *value
239 case PLIST_BOOLEAN: 173 case PLIST_BOOLEAN:
240 val->boolval = *((char *) value); 174 val->boolval = *((char *) value);
241 break; 175 break;
242 case PLIST_UINT8: 176 case PLIST_UINT:
243 val->intval8 = *((uint8_t *) value); 177 val->intval = *((uint64_t *) value);
244 break;
245 case PLIST_UINT16:
246 val->intval16 = *((uint16_t *) value);
247 break;
248 case PLIST_UINT32:
249 val->intval32 = *((uint32_t *) value);
250 break;
251 case PLIST_UINT64:
252 val->intval64 = *((uint64_t *) value);
253 break; 178 break;
254 case PLIST_FLOAT32: 179 case PLIST_REAL:
255 val->realval32 = *((float *) value); 180 val->realval = *((double *) value);
256 break;
257 case PLIST_FLOAT64:
258 val->realval64 = *((double *) value);
259 break; 181 break;
260 case PLIST_STRING: 182 case PLIST_STRING:
261 val->strval = strdup((char *) value); 183 val->strval = strdup((char *) value);
@@ -282,11 +204,55 @@ void plist_free(plist_t plist)
282 g_node_destroy(plist); 204 g_node_destroy(plist);
283} 205}
284 206
207/**********************************************
208* *
209* Xml Plist stuff *
210* *
211**********************************************/
212
213#include <libxml/parser.h>
214#include <libxml/tree.h>
215
216
217const char *plist_base = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
218<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n\
219<plist version=\"1.0\">\n\
220</plist>\0";
221
285struct xml_node { 222struct xml_node {
286 xmlNodePtr xml; 223 xmlNodePtr xml;
287 uint32_t depth; 224 uint32_t depth;
288}; 225};
289 226
227/** Creates a new plist XML document.
228 *
229 * @return The plist XML document.
230 */
231xmlDocPtr new_plist()
232{
233 char *plist = strdup(plist_base);
234 xmlDocPtr plist_xml = xmlReadMemory(plist, strlen(plist), NULL, NULL, 0);
235
236 if (!plist_xml)
237 return NULL;
238
239 free(plist);
240
241 return plist_xml;
242}
243
244/** Destroys a previously created XML document.
245 *
246 * @param plist The XML document to destroy.
247 */
248void free_plist(xmlDocPtr plist)
249{
250 if (!plist)
251 return;
252
253 xmlFreeDoc(plist);
254}
255
290void node_to_xml(GNode * node, gpointer xml_struct) 256void node_to_xml(GNode * node, gpointer xml_struct)
291{ 257{
292 if (!node) 258 if (!node)
@@ -311,34 +277,14 @@ void node_to_xml(GNode * node, gpointer xml_struct)
311 } 277 }
312 break; 278 break;
313 279
314 case PLIST_UINT8: 280 case PLIST_UINT:
315 tag = "integer";
316 val = g_strdup_printf("%u", node_data->intval8);
317 break;
318
319 case PLIST_UINT16:
320 tag = "integer";
321 val = g_strdup_printf("%u", node_data->intval16);
322 break;
323
324 case PLIST_UINT32:
325 tag = "integer";
326 val = g_strdup_printf("%u", node_data->intval32);
327 break;
328
329 case PLIST_UINT64:
330 tag = "integer"; 281 tag = "integer";
331 val = g_strdup_printf("%lu", (long unsigned int) node_data->intval64); 282 val = g_strdup_printf("%lu", (long unsigned int) node_data->intval);
332 break;
333
334 case PLIST_FLOAT32:
335 tag = "real";
336 val = g_strdup_printf("%f", node_data->realval32);
337 break; 283 break;
338 284
339 case PLIST_FLOAT64: 285 case PLIST_REAL:
340 tag = "real"; 286 tag = "real";
341 val = g_strdup_printf("%Lf", (long double) node_data->intval64); 287 val = g_strdup_printf("%Lf", (long double) node_data->realval);
342 break; 288 break;
343 289
344 case PLIST_STRING: 290 case PLIST_STRING:
@@ -358,7 +304,7 @@ void node_to_xml(GNode * node, gpointer xml_struct)
358 304
359 case PLIST_DATA: 305 case PLIST_DATA:
360 tag = "data"; 306 tag = "data";
361 val = format_string(node_data->buff, 60, 0); 307 val = format_string(node_data->buff, 60, xstruct->depth);
362 break; 308 break;
363 case PLIST_ARRAY: 309 case PLIST_ARRAY:
364 tag = "array"; 310 tag = "array";
@@ -435,15 +381,15 @@ void xml_to_node(xmlNodePtr xml_node, GNode * plist_node)
435 381
436 if (!xmlStrcmp(node->name, "integer")) { 382 if (!xmlStrcmp(node->name, "integer")) {
437 char *strval = xmlNodeGetContent(node); 383 char *strval = xmlNodeGetContent(node);
438 data->intval64 = atoi(strval); 384 data->intval = atoi(strval);
439 data->type = PLIST_UINT64; 385 data->type = PLIST_UINT;
440 continue; 386 continue;
441 } 387 }
442 388
443 if (!xmlStrcmp(node->name, "real")) { 389 if (!xmlStrcmp(node->name, "real")) {
444 char *strval = xmlNodeGetContent(node); 390 char *strval = xmlNodeGetContent(node);
445 data->realval64 = atof(strval); 391 data->realval = atof(strval);
446 data->type = PLIST_FLOAT64; 392 data->type = PLIST_REAL;
447 continue; 393 continue;
448 } 394 }
449 395
@@ -493,294 +439,460 @@ void plist_to_xml(plist_t plist, char **plist_xml, uint32_t * length)
493 xmlDocDumpMemory(plist_doc, (xmlChar **) plist_xml, length); 439 xmlDocDumpMemory(plist_doc, (xmlChar **) plist_xml, length);
494} 440}
495 441
496GNode *parse_raw_node(const char *bpbuffer, uint32_t bplength, uint32_t * position, uint8_t ref_size) 442void xml_to_plist(const char *plist_xml, uint32_t length, plist_t * plist)
497{ 443{
498 if (!position || !bpbuffer || !bplength) 444 xmlDocPtr plist_doc = xmlReadMemory(plist_xml, length, NULL, NULL, 0);
499 return NULL; 445 xmlNodePtr root_node = xmlDocGetRootElement(plist_doc);
500 446
501 uint8_t modifier = 0;
502 struct plist_data *data = (struct plist_data *) calloc(sizeof(struct plist_data), 1); 447 struct plist_data *data = (struct plist_data *) calloc(sizeof(struct plist_data), 1);
503 GNode *new_node = g_node_new(data); 448 *plist = g_node_new(data);
504 GNode *length_stupidity = NULL; 449 data->type = PLIST_PLIST;
450 xml_to_node(root_node, *plist);
451}
505 452
506 int myPos = *position;
507 if (myPos == bplength || (myPos + 1) == bplength) {
508 g_node_destroy(new_node);
509 return NULL;
510 } // end of string
511 453
512 uint32_t length = 0;
513 if (!myPos) {
514 if (strncmp(bpbuffer, "bplist00", strlen("bplist00"))) {
515 return NULL; // badness!
516 }
517 myPos += strlen("bplist00");
518 }
519 // Get the node's type.
520 if (bpbuffer[myPos] == BPLIST_DATE) { // handle date separately, but do it as a real
521 // better handling of date; basically interpret as real or double
522 data->type = BPLIST_DATE;
523 length = 8; // always 8 for "date" (Apple intended it, not me)
524 myPos++;
525 memcpy(&data->realval64, bpbuffer + myPos, sizeof(data->realval64));
526 byte_convert((char *) &data->realval64, sizeof(data->realval64));
527 myPos += length;
528 *position = myPos;
529 return new_node;
530 }
531 454
532 int type = bpbuffer[myPos] & BPLIST_MASK; 455/**********************************************
533 data->length = bpbuffer[myPos] & BPLIST_FILL; 456* *
457* Binary Plist stuff *
458* *
459**********************************************/
534 460
535 if (!type) { 461/* Magic marker and size. */
536 // what? check if it's a boolean. 462#define BPLIST_MAGIC "bplist"
537 if (bpbuffer[myPos] == BPLIST_TRUE || bpbuffer[myPos] == BPLIST_FALSE) { 463#define BPLIST_MAGIC_SIZE 6
538 // okay, so it is. Carry on.
539 data->type = PLIST_BOOLEAN;
540 data->boolval = TRUE;
541 data->length = 0;
542 } else {
543 // er, what? we have a bad type here. Return NULL.
544 g_node_destroy(new_node);
545 //printf("parse_raw_node: lol type: type given %x\n", bpbuffer[myPos]);
546 return NULL;
547 }
548 }
549 464
550 myPos++; // puts us in the data. 465#define BPLIST_VERSION "00"
551 if (length == BPLIST_FILL) { // Data happens to contain length... 466#define BPLIST_VERSION_SIZE 2
552 // what? you're going to make me parse an int for the length. You suck. 467
553 *position = myPos; 468
554 length_stupidity = parse_raw_node(bpbuffer, bplength, &myPos, ref_size); 469#define BPLIST_TRL_SIZE 26
555 switch (((struct plist_data *) length_stupidity->data)->type) { 470#define BPLIST_TRL_OFFSIZE_IDX 0
556 case PLIST_UINT8: 471#define BPLIST_TRL_PARMSIZE_IDX 1
557 data->length = ((struct plist_data *) length_stupidity->data)->intval8; 472#define BPLIST_TRL_NUMOBJ_IDX 2
558 break; 473#define BPLIST_TRL_ROOTOBJ_IDX 10
559 case PLIST_UINT16: 474#define BPLIST_TRL_OFFTAB_IDX 18
560 data->length = ((struct plist_data *) length_stupidity->data)->intval16; 475
561 break; 476enum {
562 case PLIST_UINT32: 477 BPLIST_NULL = 0x00,
563 data->length = ((struct plist_data *) length_stupidity->data)->intval32; 478 BPLIST_TRUE = 0x08,
564 break; 479 BPLIST_FALSE = 0x09,
565 case PLIST_UINT64: 480 BPLIST_FILL = 0x0F, /* will be used for length grabbing */
566 data->length = ((struct plist_data *) length_stupidity->data)->intval64; 481 BPLIST_UINT = 0x10,
567 break; 482 BPLIST_REAL = 0x20,
568 default: 483 BPLIST_DATE = 0x30,
569 g_node_destroy(new_node); 484 BPLIST_DATA = 0x40,
570 g_node_destroy(length_stupidity); 485 BPLIST_STRING = 0x50,
571 return NULL; 486 BPLIST_UNICODE = 0x60,
572 } 487 BPLIST_UID = 0x70,
573 // There, we have our fucking length now. 488 BPLIST_ARRAY = 0xA0,
574 *position = myPos; 489 BPLIST_SET = 0xC0,
575 g_node_destroy(length_stupidity); // cleanup 490 BPLIST_DICT = 0xD0,
491 BPLIST_MASK = 0xF0
492};
493
494void byte_convert(char *address, size_t size)
495{
496 int i = 0, j = 0;
497 char tmp = '\0';
498
499 for (i = 0; i < (size / 2); i++) {
500 tmp = address[i];
501 j = ((size - 1) + 0) - i;
502 address[i] = address[j];
503 address[j] = tmp;
576 } 504 }
577 // Now we're in the data. 505}
578 // Error-checking sorta 506
579 if ((myPos + data->length) >= bplength) { 507#include <byteswap.h>
580 data->length = bplength - myPos; // truncate the object 508#define swap_n_bytes(x, n) \
509 n == 8 ? bswap_64(*(uint64_t *)(x)) : \
510 (n == 4 ? bswap_32(*(uint32_t *)(x)) : \
511 (n == 2 ? bswap_16(*(uint16_t *)(x)) : *(x) ))
512
513#define be64dec(x) bswap_64( *(uint64_t*)(x) )
514
515GNode *parse_uint_node(char *bnode, uint8_t size, char **next_object)
516{
517 struct plist_data *data = (struct plist_data *) calloc(sizeof(struct plist_data), 1);
518
519 size = 1 << size; // make length less misleading
520 switch (size) {
521 case sizeof(uint8_t):
522 data->intval = bnode[0];
523 break;
524 case sizeof(uint16_t):
525 memcpy(&data->intval, bnode, size);
526 data->intval = ntohs(data->intval);
527 break;
528 case sizeof(uint32_t):
529 memcpy(&data->intval, bnode, size);
530 data->intval = ntohl(data->intval);
531 break;
532 case sizeof(uint64_t):
533 memcpy(&data->intval, bnode, size);
534 byte_convert((char *) &data->intval, size);
535 break;
536 default:
537 free(data);
538 return NULL;
539 };
540
541 *next_object = bnode + size;
542 data->type = PLIST_UINT;
543 return g_node_new(data);
544}
545
546GNode *parse_real_node(char *bnode, uint8_t size)
547{
548 struct plist_data *data = (struct plist_data *) calloc(sizeof(struct plist_data), 1);
549
550 size = 1 << size; // make length less misleading
551 switch (size) {
552 case sizeof(float):
553 memcpy(&data->realval, bnode, size);
554 byte_convert((char *) &data->realval, size);
555 break;
556 case sizeof(double):
557 memcpy(&data->realval, bnode, size);
558 byte_convert((char *) &data->realval, size);
559 break;
560 default:
561 free(data);
562 return NULL;
581 } 563 }
582 // And now for the greatest show on earth: the giant fucking switch statement. 564 data->type = PLIST_REAL;
565 return g_node_new(data);
566}
567
568GNode *parse_string_node(char *bnode, uint8_t size)
569{
570 struct plist_data *data = (struct plist_data *) calloc(sizeof(struct plist_data), 1);
571
572 data->type = PLIST_STRING;
573 data->strval = (char *) malloc(sizeof(char) * (size + 1));
574 memcpy(data->strval, bnode, size);
575 data->strval[size] = '\0';
576
577 return g_node_new(data);
578}
579
580GNode *parse_unicode_node(char *bnode, uint8_t size)
581{
582 struct plist_data *data = (struct plist_data *) calloc(sizeof(struct plist_data), 1);
583
584 data->type = PLIST_UNICODE;
585 data->unicodeval = (wchar_t *) malloc(sizeof(wchar_t) * (size + 1));
586 memcpy(data->unicodeval, bnode, size);
587 data->unicodeval[size] = '\0';
588
589 return g_node_new(data);
590}
591
592GNode *parse_data_node(char *bnode, uint64_t size, uint32_t ref_size)
593{
594 struct plist_data *data = (struct plist_data *) calloc(sizeof(struct plist_data), 1);
595
596 data->type = PLIST_DATA;
597 data->length = size;
598 data->buff = (char *) malloc(sizeof(char) * size);
599 memcpy(data->buff, bnode, sizeof(char) * size);
600
601 return g_node_new(data);
602}
603
604GNode *parse_dict_node(char *bnode, uint64_t size, uint32_t ref_size)
605{
606 struct plist_data *data = (struct plist_data *) calloc(sizeof(struct plist_data), 1);
607
608 data->type = PLIST_DICT;
609 data->length = size;
610 data->buff = (char *) malloc(sizeof(char) * size * ref_size * 2);
611 memcpy(data->buff, bnode, sizeof(char) * size * ref_size * 2);
612
613 return g_node_new(data);
614}
615
616GNode *parse_array_node(char *bnode, uint64_t size, uint32_t ref_size)
617{
618 struct plist_data *data = (struct plist_data *) calloc(sizeof(struct plist_data), 1);
619
620 data->type = PLIST_ARRAY;
621 data->length = size;
622 data->buff = (char *) malloc(sizeof(char) * size * ref_size);
623 memcpy(data->buff, bnode, sizeof(char) * size * ref_size);
624
625 return g_node_new(data);
626}
627
628plist_type plist_get_node_type(plist_t node)
629{
630 return ((struct plist_data *) node->data)->type;
631}
632
633uint64_t plist_get_node_uint_val(plist_t node)
634{
635 if (PLIST_UINT == plist_get_node_type(node))
636 return ((struct plist_data *) node->data)->intval;
637 else
638 return 0;
639}
640
641
642GNode *parse_bin_node(char *object, uint8_t dict_size, char **next_object)
643{
644 if (!object)
645 return NULL;
646
647 uint16_t type = *object & 0xF0;
648 uint64_t size = *object & 0x0F;
649 object++;
650
583 switch (type) { 651 switch (type) {
584 case BPLIST_INT: 652
585 data->length = uipow(2, data->length); // make length less misleading 653 case BPLIST_NULL:
586 switch (data->length) { 654 switch (size) {
587 case sizeof(uint8_t): 655
588 data->type = PLIST_UINT8; 656 case BPLIST_TRUE:
589 data->intval8 = bpbuffer[myPos]; 657 {
590 break; 658 struct plist_data *data = (struct plist_data *) calloc(sizeof(struct plist_data), 1);
591 case sizeof(uint16_t): 659 data->type = PLIST_BOOLEAN;
592 data->type = PLIST_UINT16; 660 data->boolval = TRUE;
593 memcpy(&data->intval16, bpbuffer + myPos, sizeof(uint16_t)); 661 return g_node_new(data);
594 data->intval16 = ntohs(data->intval16); 662 }
595 break; 663
596 case sizeof(uint32_t): 664 case BPLIST_FALSE:
597 data->type = PLIST_UINT32; 665 {
598 memcpy(&data->intval32, bpbuffer + myPos, sizeof(uint32_t)); 666 struct plist_data *data = (struct plist_data *) calloc(sizeof(struct plist_data), 1);
599 data->intval32 = ntohl(data->intval32); 667 data->type = PLIST_BOOLEAN;
600 break; 668 data->boolval = FALSE;
601 case sizeof(uint64_t): 669 return g_node_new(data);
602 data->type = PLIST_UINT64; 670 }
603 memcpy(&data->intval64, bpbuffer + myPos, sizeof(uint64_t)); 671
604 byte_convert((char *) &data->intval64, sizeof(uint64_t)); 672 case BPLIST_NULL:
605 break;
606 default: 673 default:
607 g_node_destroy(new_node);
608 printf("parse_raw_node: lol: invalid int: size given %lu\n", (long unsigned int) length);
609 printf("parse_raw_node: lol: by the way sizeof(uint64) = %i\n", sizeof(uint64_t));
610 return NULL; 674 return NULL;
611 } 675 }
612 break; 676
677 case BPLIST_UINT:
678 return parse_uint_node(object, size, next_object);
613 679
614 case BPLIST_REAL: 680 case BPLIST_REAL:
615 data->length = uipow(2, data->length); 681 return parse_real_node(object, size);
616 switch (data->length) { 682
617 case sizeof(float): 683 case BPLIST_DATE:
618 data->type = PLIST_FLOAT32; 684 if (3 != size)
619 memcpy(&data->realval32, bpbuffer + myPos, data->length);
620 byte_convert((char *) &data->realval32, sizeof(float)); //necessary ??
621 break;
622 case sizeof(double):
623 data->type = PLIST_FLOAT64;
624 memcpy(&data->realval64, bpbuffer + myPos, data->length);
625 byte_convert((char *) &data->realval64, sizeof(double));
626 break;
627 default:
628 g_node_destroy(new_node);
629 printf("parse_raw_node: lol: invalid real: size given %lu\n", (long unsigned int) length);
630 printf("parse_raw_node: lol: by the way sizeof(uint64) = %i\n", sizeof(uint64_t));
631 return NULL; 685 return NULL;
686 else
687 return parse_real_node(object, size);
688
689 case BPLIST_DATA:
690 if (0x0F == size) {
691 plist_t size_node = parse_bin_node(object, dict_size, &object);
692 if (plist_get_node_type(size_node) != PLIST_UINT)
693 return NULL;
694 size = plist_get_node_uint_val(size_node);
632 } 695 }
633 break; 696 return parse_data_node(object, size, dict_size);
634 697
635 case BPLIST_STRING: 698 case BPLIST_STRING:
636 data->type = PLIST_STRING; 699 if (0x0F == size) {
637 data->strval = (char *) malloc(sizeof(char) * data->length); 700 plist_t size_node = parse_bin_node(object, dict_size, &object);
638 memcpy(data->strval, bpbuffer + myPos, data->length); 701 if (plist_get_node_type(size_node) != PLIST_UINT)
639 break; 702 return NULL;
703 size = plist_get_node_uint_val(size_node);
704 }
705 return parse_string_node(object, size);
640 706
641 case BPLIST_UNICODE: 707 case BPLIST_UNICODE:
642 data->type = PLIST_UNICODE; 708 if (0x0F == size) {
643 data->unicodeval = (wchar_t *) malloc(sizeof(wchar_t) * data->length); 709 plist_t size_node = parse_bin_node(object, dict_size, &object);
644 memcpy(data->unicodeval, bpbuffer + myPos, data->length); 710 if (plist_get_node_type(size_node) != PLIST_UINT)
645 break; 711 return NULL;
712 size = plist_get_node_uint_val(size_node);
713 }
714 return parse_unicode_node(object, size);
715
716 case BPLIST_UID:
717 case BPLIST_ARRAY:
718 if (0x0F == size) {
719 plist_t size_node = parse_bin_node(object, dict_size, &object);
720 if (plist_get_node_type(size_node) != PLIST_UINT)
721 return NULL;
722 size = plist_get_node_uint_val(size_node);
723 }
724 return parse_array_node(object, size, dict_size);
725
726 case BPLIST_SET:
727 case BPLIST_DICT:
728 if (0x0F == size) {
729 plist_t size_node = parse_bin_node(object, dict_size, &object);
730 if (plist_get_node_type(size_node) != PLIST_UINT)
731 return NULL;
732 object++;
733 size = plist_get_node_uint_val(size_node);
734 }
735 return parse_dict_node(object, size, dict_size);
646 736
647 case BPLIST_DICT: /* returning a raw dict, it forward-references, so. */ 737 }
648 data->length = data->length * 2; // dicts lie 738 return NULL;
649 data->type = PLIST_DICT; 739}
650 740
651 case BPLIST_ARRAY: /* returning a raw array, it forward-references, so. */ 741void plist_to_bin(plist_t plist, char **plist_bin, uint32_t * length)
652 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. 742{
653 if (data->type == 0) 743 uint64_t num_objects = g_node_n_nodes(plist, G_TRAVERSE_ALL);
654 data->type = PLIST_ARRAY; 744}
655 case BPLIST_DATA: 745
656 if (data->type == 0) 746
657 data->type = PLIST_DATA; 747
658 default: /* made to hold raw data. */ 748gpointer copy_plist_data(gconstpointer src, gpointer data)
659 modifier = (data->ref_size > 0) ? data->ref_size : 1; 749{
660 data->buff = (char *) malloc(sizeof(char) * (data->length * modifier)); 750 struct plist_data *srcdata = (struct plist_data *) src;
661 memcpy(data->buff, bpbuffer + myPos, (data->length * modifier)); 751 struct plist_data *dstdata = (struct plist_data *) calloc(sizeof(struct plist_data), 1);
752
753 dstdata->type = srcdata->type;
754 dstdata->length = srcdata->length;
755 switch (dstdata->type) {
756 case PLIST_BOOLEAN:
757 dstdata->boolval = srcdata->boolval;
758 break;
759 case PLIST_UINT:
760 dstdata->intval = srcdata->intval;
761 break;
762 case PLIST_DATE:
763 case PLIST_REAL:
764 dstdata->realval = srcdata->realval;
765 break;
766 case PLIST_KEY:
767 case PLIST_STRING:
768 dstdata->strval = strdup(srcdata->strval);
769 break;
770 case PLIST_UNICODE:
771 dstdata->unicodeval = wcsdup(srcdata->unicodeval);
772 break;
773 case PLIST_PLIST:
774 case PLIST_DATA:
775 case PLIST_ARRAY:
776 case PLIST_DICT:
777 dstdata->buff = (char *) malloc(sizeof(char *) * srcdata->length);
778 memcpy(dstdata->buff, srcdata->buff, sizeof(char *) * srcdata->length);
662 break; 779 break;
663 780
781 default:
782 break;
664 } 783 }
665 784
666 myPos += data->length; 785 return dstdata;
667 *position = myPos;
668 return new_node;
669} 786}
670 787
671plist_t parse_nodes(const char *bpbuffer, uint32_t bplength, uint32_t * position) 788void bin_to_plist(const char *plist_bin, uint32_t length, plist_t * plist)
672{ 789{
673 plist_t *nodeslist = NULL, *newaddr = NULL; 790 //first check we have enough data
674 plist_t new_node = NULL, root_node = NULL; 791 if (!(length >= BPLIST_MAGIC_SIZE + BPLIST_VERSION_SIZE + BPLIST_TRL_SIZE))
792 return;
793 //check that plist_bin in actually a plist
794 if (memcmp(plist_bin, BPLIST_MAGIC, BPLIST_MAGIC_SIZE) != 0)
795 return;
796 //check for known version
797 if (memcmp(plist_bin + BPLIST_MAGIC_SIZE, BPLIST_VERSION, BPLIST_VERSION_SIZE) != 0)
798 return;
675 799
676 uint32_t nodeslength = 0; 800 //now parse trailer
677 uint8_t offset_size = 0, dict_param_size = 0; 801 const char *trailer = plist_bin + (length - BPLIST_TRL_SIZE);
678 offset_size = bpbuffer[bplength - 26]; 802
679 dict_param_size = bpbuffer[bplength - 25]; 803 uint8_t offset_size = trailer[BPLIST_TRL_OFFSIZE_IDX];
680 uint64_t current_offset = 0; 804 uint8_t dict_param_size = trailer[BPLIST_TRL_PARMSIZE_IDX];
681 uint64_t num_objects = 0, root_object = 0, offset_table_index = 0; 805 uint64_t num_objects = be64dec(trailer + BPLIST_TRL_NUMOBJ_IDX);
682 memcpy(&num_objects, bpbuffer + bplength - 24, sizeof(uint64_t)); 806 uint64_t root_object = be64dec(trailer + BPLIST_TRL_ROOTOBJ_IDX);
683 memcpy(&root_object, bpbuffer + bplength - 16, sizeof(uint64_t)); 807 uint64_t offset_table_index = be64dec(trailer + BPLIST_TRL_OFFTAB_IDX);
684 memcpy(&offset_table_index, bpbuffer + bplength - 8, sizeof(uint64_t));
685 byte_convert((char *) &num_objects, sizeof(uint64_t));
686 byte_convert((char *) &root_object, sizeof(uint64_t));
687 byte_convert((char *) &offset_table_index, sizeof(uint64_t));
688
689 log_debug_msg("Offset size: %i\nGiven: %i\n", offset_size, bpbuffer[bplength - 26]);
690 log_debug_msg("Ref size: %i\nGiven: %i\n", dict_param_size, bpbuffer[bplength - 25]);
691 log_debug_msg("Number of objects: %lli\nGiven: %llu\n", num_objects, *(bpbuffer + bplength - 24));
692 log_debug_msg("Root object index: %lli\nGiven: %llu\n", root_object, *(bpbuffer + bplength - 16));
693 log_debug_msg("Offset table index: %lli\nGiven: %llu\n", offset_table_index, *(bpbuffer + bplength - 8));
694 log_debug_msg("Size of uint64: %i\n", sizeof(uint64_t));
695
696 int i = 0, j = 0, k = 0, str_i = 0, str_j = 0;
697 uint32_t index1 = 0, index2 = 0;
698 808
809 log_debug_msg("Offset size: %i\n", offset_size);
810 log_debug_msg("Ref size: %i\n", dict_param_size);
811 log_debug_msg("Number of objects: %lli\n", num_objects);
812 log_debug_msg("Root object index: %lli\n", root_object);
813 log_debug_msg("Offset table index: %lli\n", offset_table_index);
814
815 if (num_objects == 0)
816 return;
817
818 //allocate serialized array of nodes
819 plist_t *nodeslist = NULL;
699 nodeslist = (plist_t *) malloc(sizeof(plist_t) * num_objects); 820 nodeslist = (plist_t *) malloc(sizeof(plist_t) * num_objects);
821
700 if (!nodeslist) 822 if (!nodeslist)
701 return NULL; 823 return;
702 824
825 //parse serialized nodes
826 uint64_t i = 0;
827 uint64_t current_offset = 0;
828 const char *offset_table = plist_bin + offset_table_index;
703 for (i = 0; i < num_objects; i++) { 829 for (i = 0; i < num_objects; i++) {
704 memcpy(&current_offset, bpbuffer + (offset_table_index + (i * offset_size)), offset_size); 830 current_offset = swap_n_bytes(offset_table + i * offset_size, offset_size);
705 byte_convert((char *) &current_offset, 831
706 (offset_size <= sizeof(current_offset)) ? offset_size : sizeof(current_offset)); 832 log_debug_msg("parse_nodes: current_offset = %i\n", current_offset);
707 log_debug_msg("parse_nodes: current_offset = %x\n", current_offset); 833 char *obj = plist_bin + current_offset;
708 nodeslist[i] = parse_raw_node(bpbuffer, bplength, (uint32_t *) & current_offset, dict_param_size); 834 nodeslist[i] = parse_bin_node(obj, dict_param_size, &obj);
709 log_debug_msg("parse_nodes: parse_raw_node done\n"); 835 log_debug_msg("parse_nodes: parse_raw_node done\n");
710 } 836 }
711 837
838 //setup children for structured types
839 int j = 0, str_i = 0, str_j = 0;
840 uint32_t index1 = 0, index2 = 0;
712 841
713 for (i = 0; i < num_objects; i++) { 842 for (i = 0; i < num_objects; i++) {
714 // set elements for dicts and arrays and leave the rest alone 843
715 log_debug_msg("parse_nodes: on node %i\n", i); 844 log_debug_msg("parse_nodes: on node %i\n", i);
716 struct plist_data *data = (struct plist_data *) nodeslist[i]->data; 845 struct plist_data *data = (struct plist_data *) nodeslist[i]->data;
717 846
718 switch (data->type) { 847 switch (data->type) {
719 case PLIST_DICT: 848 case PLIST_DICT:
720 log_debug_msg("parse_nodes: dictionary found\n"); 849 log_debug_msg("parse_nodes: dictionary found\n");
721 for (j = 0; j < (data->length / 2); j++) { 850 for (j = 0; j < data->length; j++) {
722 str_i = j * data->ref_size; 851 str_i = j * dict_param_size;
723 str_j = (j + (data->length / 2)) * data->ref_size; 852 str_j = (j + data->length) * dict_param_size;
724 853
725 memcpy(&index1, data->buff + str_i, data->ref_size); 854 index1 = swap_n_bytes(data->buff + str_i, dict_param_size);
726 memcpy(&index2, data->buff + str_j, data->ref_size); 855 index2 = swap_n_bytes(data->buff + str_j, dict_param_size);
727 856
728 byte_convert((char *) &index1, (dict_param_size <= sizeof(index1)) ? dict_param_size : sizeof(index2)); 857 //first one is actually a key
729 byte_convert((char *) &index2, (dict_param_size <= sizeof(index2)) ? dict_param_size : sizeof(index2)); 858 ((struct plist_data *) nodeslist[index1]->data)->type = PLIST_KEY;
730 859 //g_node_append(nodeslist[i], nodeslist[index1]);
731 g_node_append(nodeslist[i], nodeslist[index1]); 860 //g_node_append(nodeslist[i], nodeslist[index2]);
732 g_node_append(nodeslist[i], nodeslist[index2]); 861
862 if (G_NODE_IS_ROOT(nodeslist[index1]))
863 g_node_append(nodeslist[i], nodeslist[index1]);
864 else
865 g_node_append(nodeslist[i], g_node_copy_deep(nodeslist[index1], copy_plist_data, NULL));
866
867 if (G_NODE_IS_ROOT(nodeslist[index2]))
868 g_node_append(nodeslist[i], nodeslist[index2]);
869 else
870 g_node_append(nodeslist[i], g_node_copy_deep(nodeslist[index2], copy_plist_data, NULL));
733 } 871 }
734 872
735 data->length = data->length / 2;
736 free(data->buff); 873 free(data->buff);
737 k = 0;
738 break; 874 break;
739 875
740 case PLIST_ARRAY: 876 case PLIST_ARRAY:
741 log_debug_msg("parse_nodes: array found\n"); 877 log_debug_msg("parse_nodes: array found\n");
742 for (j = 0; j < data->length; j++) { 878 for (j = 0; j < data->length; j++) {
743 log_debug_msg("parse_nodes: array index %i\n", j); 879 str_j = j * dict_param_size;
744 str_j = j * data->ref_size; 880 index1 = swap_n_bytes(data->buff + str_j, dict_param_size);
745 memcpy(&index1, data->buff + str_j, data->ref_size); 881
746 log_debug_msg("parse_nodes: post-memcpy\n"); 882 //g_node_append(nodeslist[i], nodeslist[index1]);
747 byte_convert((char *) &index1, (dict_param_size <= sizeof(index1)) ? dict_param_size : sizeof(index1)); 883 if (G_NODE_IS_ROOT(nodeslist[index1]))
748 log_debug_msg("parse_nodes: post-ntohl\nindex1 = %i\n", index1); 884 g_node_append(nodeslist[i], nodeslist[index1]);
749 g_node_append(nodeslist[i], nodeslist[index1]); 885 else
750 log_debug_msg("parse_nodes: post-assignment\n"); 886 g_node_append(nodeslist[i], g_node_copy_deep(nodeslist[index1], copy_plist_data, NULL));
751 } 887 }
752 free(data->buff); 888 free(data->buff);
753 break; 889 break;
754 default: 890 default:
755 //printf("lol... type %x\n", nodeslist[i]->type);
756 break; 891 break;
757 } // those are the only two we need to correct for. 892 }
758 } 893 }
759 894
760 root_node = nodeslist[root_object]; 895 *plist = nodeslist[root_object];
761 return root_node;
762}
763
764void plist_to_bin(plist_t plist, char **plist_bin, uint32_t * length)
765{
766}
767
768void xml_to_plist(const char *plist_xml, uint32_t length, plist_t * plist)
769{
770 xmlDocPtr plist_doc = xmlReadMemory(plist_xml, length, NULL, NULL, 0);
771 xmlNodePtr root_node = xmlDocGetRootElement(plist_doc);
772
773 struct plist_data *data = (struct plist_data *) calloc(sizeof(struct plist_data), 1);
774 *plist = g_node_new(data);
775 data->type = PLIST_PLIST;
776 xml_to_node(root_node, *plist);
777
778}
779
780void bin_to_plist(const char *plist_bin, uint32_t length, plist_t * plist)
781{
782 uint32_t pos = 0;
783 *plist = parse_nodes(plist_bin, length, &pos);
784} 896}
785 897
786 898
@@ -816,23 +928,11 @@ char compare_node_value(plist_type type, struct plist_data *data, void *value)
816 case PLIST_BOOLEAN: 928 case PLIST_BOOLEAN:
817 res = data->boolval == *((char *) value) ? TRUE : FALSE; 929 res = data->boolval == *((char *) value) ? TRUE : FALSE;
818 break; 930 break;
819 case PLIST_UINT8: 931 case PLIST_UINT:
820 res = data->intval8 == *((uint8_t *) value) ? TRUE : FALSE; 932 res = data->intval == *((uint64_t *) value) ? TRUE : FALSE;
821 break;
822 case PLIST_UINT16:
823 res = data->intval16 == *((uint16_t *) value) ? TRUE : FALSE;
824 break;
825 case PLIST_UINT32:
826 res = data->intval32 == *((uint32_t *) value) ? TRUE : FALSE;
827 break;
828 case PLIST_UINT64:
829 res = data->intval64 == *((uint64_t *) value) ? TRUE : FALSE;
830 break; 933 break;
831 case PLIST_FLOAT32: 934 case PLIST_REAL:
832 res = data->realval32 == *((float *) value) ? TRUE : FALSE; 935 res = data->realval == *((double *) value) ? TRUE : FALSE;
833 break;
834 case PLIST_FLOAT64:
835 res = data->realval64 == *((double *) value) ? TRUE : FALSE;
836 break; 936 break;
837 case PLIST_KEY: 937 case PLIST_KEY:
838 case PLIST_STRING: 938 case PLIST_STRING:
@@ -889,23 +989,11 @@ void get_type_and_value(GNode * node, plist_type * type, void *value)
889 case PLIST_BOOLEAN: 989 case PLIST_BOOLEAN:
890 *((char *) value) = data->boolval; 990 *((char *) value) = data->boolval;
891 break; 991 break;
892 case PLIST_UINT8: 992 case PLIST_UINT:
893 *((uint8_t *) value) = data->intval8; 993 *((uint64_t *) value) = data->intval;
894 break;
895 case PLIST_UINT16:
896 *((uint16_t *) value) = data->intval16;
897 break;
898 case PLIST_UINT32:
899 *((uint32_t *) value) = data->intval32;
900 break;
901 case PLIST_UINT64:
902 *((uint64_t *) value) = data->intval64;
903 break;
904 case PLIST_FLOAT32:
905 *((float *) value) = data->realval32;
906 break; 994 break;
907 case PLIST_FLOAT64: 995 case PLIST_REAL:
908 *((double *) value) = data->realval64; 996 *((double *) value) = data->realval;
909 break; 997 break;
910 case PLIST_STRING: 998 case PLIST_STRING:
911 *((char **) value) = strdup(data->strval); 999 *((char **) value) = strdup(data->strval);
diff --git a/src/plist.h b/src/plist.h
index ed3d2b2..df1d3e4 100644
--- a/src/plist.h
+++ b/src/plist.h
@@ -35,7 +35,7 @@ char *format_string(const char *buf, int cols, int depth);
35 35
36/* Binary plist stuff */ 36/* Binary plist stuff */
37 37
38 38/*
39typedef enum { 39typedef enum {
40 PLIST_BOOLEAN, 40 PLIST_BOOLEAN,
41 PLIST_UINT8, 41 PLIST_UINT8,
@@ -53,6 +53,22 @@ typedef enum {
53 PLIST_PLIST, 53 PLIST_PLIST,
54 PLIST_KEY, 54 PLIST_KEY,
55} plist_type; 55} plist_type;
56*/
57
58typedef enum {
59 PLIST_BOOLEAN,
60 PLIST_UINT,
61 PLIST_REAL,
62 PLIST_STRING,
63 PLIST_UNICODE,
64 PLIST_ARRAY,
65 PLIST_DICT,
66 PLIST_DATE,
67 PLIST_DATA,
68 PLIST_PLIST,
69 PLIST_KEY,
70} plist_type;
71
56 72
57 73
58typedef GNode *plist_t; 74typedef GNode *plist_t;