summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/bplist.c790
-rw-r--r--src/xplist.c306
2 files changed, 1096 insertions, 0 deletions
diff --git a/src/bplist.c b/src/bplist.c
new file mode 100644
index 0000000..6136fe9
--- /dev/null
+++ b/src/bplist.c
@@ -0,0 +1,790 @@
1/*
2 * plist.c
3 * Binary plist implementation
4 *
5 * Copyright (c) 2008 Jonathan Beck All Rights Reserved.
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22
23#include "plist.h"
24#include <wchar.h>
25#include <stdlib.h>
26#include <stdio.h>
27#include <string.h>
28
29/* Magic marker and size. */
30#define BPLIST_MAGIC "bplist"
31#define BPLIST_MAGIC_SIZE 6
32
33#define BPLIST_VERSION "00"
34#define BPLIST_VERSION_SIZE 2
35
36
37#define BPLIST_TRL_SIZE 26
38#define BPLIST_TRL_OFFSIZE_IDX 0
39#define BPLIST_TRL_PARMSIZE_IDX 1
40#define BPLIST_TRL_NUMOBJ_IDX 2
41#define BPLIST_TRL_ROOTOBJ_IDX 10
42#define BPLIST_TRL_OFFTAB_IDX 18
43
44enum {
45 BPLIST_NULL = 0x00,
46 BPLIST_TRUE = 0x08,
47 BPLIST_FALSE = 0x09,
48 BPLIST_FILL = 0x0F, /* will be used for length grabbing */
49 BPLIST_UINT = 0x10,
50 BPLIST_REAL = 0x20,
51 BPLIST_DATE = 0x30,
52 BPLIST_DATA = 0x40,
53 BPLIST_STRING = 0x50,
54 BPLIST_UNICODE = 0x60,
55 BPLIST_UID = 0x70,
56 BPLIST_ARRAY = 0xA0,
57 BPLIST_SET = 0xC0,
58 BPLIST_DICT = 0xD0,
59 BPLIST_MASK = 0xF0
60};
61
62void byte_convert(char *address, size_t size)
63{
64 int i = 0, j = 0;
65 char tmp = '\0';
66
67 for (i = 0; i < (size / 2); i++) {
68 tmp = address[i];
69 j = ((size - 1) + 0) - i;
70 address[i] = address[j];
71 address[j] = tmp;
72 }
73}
74
75#include <byteswap.h>
76#define swap_n_bytes(x, n) \
77 n == 8 ? bswap_64(*(uint64_t *)(x)) : \
78 (n == 4 ? bswap_32(*(uint32_t *)(x)) : \
79 (n == 2 ? bswap_16(*(uint16_t *)(x)) : *(x) ))
80
81#define be64dec(x) bswap_64( *(uint64_t*)(x) )
82
83#define get_needed_bytes(x) (x <= 1<<8 ? 1 : ( x <= 1<<16 ? 2 : ( x <= 1<<32 ? 4 : 8)))
84#define get_real_bytes(x) (x >> 32 ? 4 : 8)
85
86GNode *parse_uint_node(char *bnode, uint8_t size, char **next_object)
87{
88 struct plist_data *data = (struct plist_data *) calloc(sizeof(struct plist_data), 1);
89
90 size = 1 << size; // make length less misleading
91 switch (size) {
92 case sizeof(uint8_t):
93 data->intval = bnode[0];
94 break;
95 case sizeof(uint16_t):
96 memcpy(&data->intval, bnode, size);
97 data->intval = ntohs(data->intval);
98 break;
99 case sizeof(uint32_t):
100 memcpy(&data->intval, bnode, size);
101 data->intval = ntohl(data->intval);
102 break;
103 case sizeof(uint64_t):
104 memcpy(&data->intval, bnode, size);
105 byte_convert((char *) &data->intval, size);
106 break;
107 default:
108 free(data);
109 return NULL;
110 };
111
112 *next_object = bnode + size;
113 data->type = PLIST_UINT;
114 return g_node_new(data);
115}
116
117GNode *parse_real_node(char *bnode, uint8_t size)
118{
119 struct plist_data *data = (struct plist_data *) calloc(sizeof(struct plist_data), 1);
120
121 size = 1 << size; // make length less misleading
122 switch (size) {
123 case sizeof(float):
124 memcpy(&data->realval, bnode, size);
125 byte_convert((char *) &data->realval, size);
126 break;
127 case sizeof(double):
128 memcpy(&data->realval, bnode, size);
129 byte_convert((char *) &data->realval, size);
130 break;
131 default:
132 free(data);
133 return NULL;
134 }
135 data->type = PLIST_REAL;
136 return g_node_new(data);
137}
138
139GNode *parse_string_node(char *bnode, uint8_t size)
140{
141 struct plist_data *data = (struct plist_data *) calloc(sizeof(struct plist_data), 1);
142
143 data->type = PLIST_STRING;
144 data->strval = (char *) malloc(sizeof(char) * (size + 1));
145 memcpy(data->strval, bnode, size);
146 data->strval[size] = '\0';
147
148 return g_node_new(data);
149}
150
151GNode *parse_unicode_node(char *bnode, uint8_t size)
152{
153 struct plist_data *data = (struct plist_data *) calloc(sizeof(struct plist_data), 1);
154
155 data->type = PLIST_UNICODE;
156 data->unicodeval = (wchar_t *) malloc(sizeof(wchar_t) * (size + 1));
157 memcpy(data->unicodeval, bnode, size);
158 data->unicodeval[size] = '\0';
159
160 return g_node_new(data);
161}
162
163GNode *parse_data_node(char *bnode, uint64_t size, uint32_t ref_size)
164{
165 struct plist_data *data = (struct plist_data *) calloc(sizeof(struct plist_data), 1);
166
167 data->type = PLIST_DATA;
168 data->length = size;
169 data->buff = (char *) malloc(sizeof(char) * size);
170 memcpy(data->buff, bnode, sizeof(char) * size);
171
172 return g_node_new(data);
173}
174
175GNode *parse_dict_node(char *bnode, uint64_t size, uint32_t ref_size)
176{
177 struct plist_data *data = (struct plist_data *) calloc(sizeof(struct plist_data), 1);
178
179 data->type = PLIST_DICT;
180 data->length = size;
181 data->buff = (char *) malloc(sizeof(char) * size * ref_size * 2);
182 memcpy(data->buff, bnode, sizeof(char) * size * ref_size * 2);
183
184 return g_node_new(data);
185}
186
187GNode *parse_array_node(char *bnode, uint64_t size, uint32_t ref_size)
188{
189 struct plist_data *data = (struct plist_data *) calloc(sizeof(struct plist_data), 1);
190
191 data->type = PLIST_ARRAY;
192 data->length = size;
193 data->buff = (char *) malloc(sizeof(char) * size * ref_size);
194 memcpy(data->buff, bnode, sizeof(char) * size * ref_size);
195
196 return g_node_new(data);
197}
198
199
200
201GNode *parse_bin_node(char *object, uint8_t dict_size, char **next_object)
202{
203 if (!object)
204 return NULL;
205
206 uint16_t type = *object & 0xF0;
207 uint64_t size = *object & 0x0F;
208 object++;
209
210 switch (type) {
211
212 case BPLIST_NULL:
213 switch (size) {
214
215 case BPLIST_TRUE:
216 {
217 struct plist_data *data = (struct plist_data *) calloc(sizeof(struct plist_data), 1);
218 data->type = PLIST_BOOLEAN;
219 data->boolval = TRUE;
220 return g_node_new(data);
221 }
222
223 case BPLIST_FALSE:
224 {
225 struct plist_data *data = (struct plist_data *) calloc(sizeof(struct plist_data), 1);
226 data->type = PLIST_BOOLEAN;
227 data->boolval = FALSE;
228 return g_node_new(data);
229 }
230
231 case BPLIST_NULL:
232 default:
233 return NULL;
234 }
235
236 case BPLIST_UINT:
237 return parse_uint_node(object, size, next_object);
238
239 case BPLIST_REAL:
240 return parse_real_node(object, size);
241
242 case BPLIST_DATE:
243 if (3 != size)
244 return NULL;
245 else
246 return parse_real_node(object, size);
247
248 case BPLIST_DATA:
249 if (0x0F == size) {
250 plist_t size_node = parse_bin_node(object, dict_size, &object);
251 if (plist_get_node_type(size_node) != PLIST_UINT)
252 return NULL;
253 size = plist_get_node_uint_val(size_node);
254 }
255 return parse_data_node(object, size, dict_size);
256
257 case BPLIST_STRING:
258 if (0x0F == size) {
259 plist_t size_node = parse_bin_node(object, dict_size, &object);
260 if (plist_get_node_type(size_node) != PLIST_UINT)
261 return NULL;
262 size = plist_get_node_uint_val(size_node);
263 }
264 return parse_string_node(object, size);
265
266 case BPLIST_UNICODE:
267 if (0x0F == size) {
268 plist_t size_node = parse_bin_node(object, dict_size, &object);
269 if (plist_get_node_type(size_node) != PLIST_UINT)
270 return NULL;
271 size = plist_get_node_uint_val(size_node);
272 }
273 return parse_unicode_node(object, size);
274
275 case BPLIST_UID:
276 case BPLIST_ARRAY:
277 if (0x0F == size) {
278 plist_t size_node = parse_bin_node(object, dict_size, &object);
279 if (plist_get_node_type(size_node) != PLIST_UINT)
280 return NULL;
281 size = plist_get_node_uint_val(size_node);
282 }
283 return parse_array_node(object, size, dict_size);
284
285 case BPLIST_SET:
286 case BPLIST_DICT:
287 if (0x0F == size) {
288 plist_t size_node = parse_bin_node(object, dict_size, &object);
289 if (plist_get_node_type(size_node) != PLIST_UINT)
290 return NULL;
291 object++;
292 size = plist_get_node_uint_val(size_node);
293 }
294 return parse_dict_node(object, size, dict_size);
295
296 }
297 return NULL;
298}
299
300gpointer copy_plist_data(gconstpointer src, gpointer data)
301{
302 struct plist_data *srcdata = (struct plist_data *) src;
303 struct plist_data *dstdata = (struct plist_data *) calloc(sizeof(struct plist_data), 1);
304
305 dstdata->type = srcdata->type;
306 dstdata->length = srcdata->length;
307 switch (dstdata->type) {
308 case PLIST_BOOLEAN:
309 dstdata->boolval = srcdata->boolval;
310 break;
311 case PLIST_UINT:
312 dstdata->intval = srcdata->intval;
313 break;
314 case PLIST_DATE:
315 case PLIST_REAL:
316 dstdata->realval = srcdata->realval;
317 break;
318 case PLIST_KEY:
319 case PLIST_STRING:
320 dstdata->strval = strdup(srcdata->strval);
321 break;
322 case PLIST_UNICODE:
323 dstdata->unicodeval = wcsdup(srcdata->unicodeval);
324 break;
325 case PLIST_DATA:
326 case PLIST_ARRAY:
327 case PLIST_DICT:
328 dstdata->buff = (char *) malloc(sizeof(char *) * srcdata->length);
329 memcpy(dstdata->buff, srcdata->buff, sizeof(char *) * srcdata->length);
330 break;
331
332 default:
333 break;
334 }
335
336 return dstdata;
337}
338
339void bin_to_plist(const char *plist_bin, uint32_t length, plist_t * plist)
340{
341 //first check we have enough data
342 if (!(length >= BPLIST_MAGIC_SIZE + BPLIST_VERSION_SIZE + BPLIST_TRL_SIZE))
343 return;
344 //check that plist_bin in actually a plist
345 if (memcmp(plist_bin, BPLIST_MAGIC, BPLIST_MAGIC_SIZE) != 0)
346 return;
347 //check for known version
348 if (memcmp(plist_bin + BPLIST_MAGIC_SIZE, BPLIST_VERSION, BPLIST_VERSION_SIZE) != 0)
349 return;
350
351 //now parse trailer
352 const char *trailer = plist_bin + (length - BPLIST_TRL_SIZE);
353
354 uint8_t offset_size = trailer[BPLIST_TRL_OFFSIZE_IDX];
355 uint8_t dict_param_size = trailer[BPLIST_TRL_PARMSIZE_IDX];
356 uint64_t num_objects = be64dec(trailer + BPLIST_TRL_NUMOBJ_IDX);
357 uint64_t root_object = be64dec(trailer + BPLIST_TRL_ROOTOBJ_IDX);
358 uint64_t offset_table_index = be64dec(trailer + BPLIST_TRL_OFFTAB_IDX);
359
360 log_debug_msg("Offset size: %i\n", offset_size);
361 log_debug_msg("Ref size: %i\n", dict_param_size);
362 log_debug_msg("Number of objects: %lli\n", num_objects);
363 log_debug_msg("Root object index: %lli\n", root_object);
364 log_debug_msg("Offset table index: %lli\n", offset_table_index);
365
366 if (num_objects == 0)
367 return;
368
369 //allocate serialized array of nodes
370 plist_t *nodeslist = NULL;
371 nodeslist = (plist_t *) malloc(sizeof(plist_t) * num_objects);
372
373 if (!nodeslist)
374 return;
375
376 //parse serialized nodes
377 uint64_t i = 0;
378 uint64_t current_offset = 0;
379 const char *offset_table = plist_bin + offset_table_index;
380 for (i = 0; i < num_objects; i++) {
381 current_offset = swap_n_bytes(offset_table + i * offset_size, offset_size);
382
383 log_debug_msg("parse_nodes: current_offset = %i\n", current_offset);
384 char *obj = plist_bin + current_offset;
385 nodeslist[i] = parse_bin_node(obj, dict_param_size, &obj);
386 log_debug_msg("parse_nodes: parse_raw_node done\n");
387 }
388
389 //setup children for structured types
390 int j = 0, str_i = 0, str_j = 0;
391 uint32_t index1 = 0, index2 = 0;
392
393 for (i = 0; i < num_objects; i++) {
394
395 log_debug_msg("parse_nodes: on node %i\n", i);
396 struct plist_data *data = (struct plist_data *) nodeslist[i]->data;
397
398 switch (data->type) {
399 case PLIST_DICT:
400 log_debug_msg("parse_nodes: dictionary found\n");
401 for (j = 0; j < data->length; j++) {
402 str_i = j * dict_param_size;
403 str_j = (j + data->length) * dict_param_size;
404
405 index1 = swap_n_bytes(data->buff + str_i, dict_param_size);
406 index2 = swap_n_bytes(data->buff + str_j, dict_param_size);
407
408 //first one is actually a key
409 ((struct plist_data *) nodeslist[index1]->data)->type = PLIST_KEY;
410
411 if (G_NODE_IS_ROOT(nodeslist[index1]))
412 g_node_append(nodeslist[i], nodeslist[index1]);
413 else
414 g_node_append(nodeslist[i], g_node_copy_deep(nodeslist[index1], copy_plist_data, NULL));
415
416 if (G_NODE_IS_ROOT(nodeslist[index2]))
417 g_node_append(nodeslist[i], nodeslist[index2]);
418 else
419 g_node_append(nodeslist[i], g_node_copy_deep(nodeslist[index2], copy_plist_data, NULL));
420 }
421
422 free(data->buff);
423 break;
424
425 case PLIST_ARRAY:
426 log_debug_msg("parse_nodes: array found\n");
427 for (j = 0; j < data->length; j++) {
428 str_j = j * dict_param_size;
429 index1 = swap_n_bytes(data->buff + str_j, dict_param_size);
430
431 //g_node_append(nodeslist[i], nodeslist[index1]);
432 if (G_NODE_IS_ROOT(nodeslist[index1]))
433 g_node_append(nodeslist[i], nodeslist[index1]);
434 else
435 g_node_append(nodeslist[i], g_node_copy_deep(nodeslist[index1], copy_plist_data, NULL));
436 }
437 free(data->buff);
438 break;
439 default:
440 break;
441 }
442 }
443
444 *plist = nodeslist[root_object];
445}
446
447guint plist_data_hash(gconstpointer key)
448{
449 struct plist_data *data = (struct plist_data *) ((GNode *) key)->data;
450
451 guint hash = data->type;
452 guint i = 0;
453
454 char *buff = NULL;
455 guint size = 0;
456
457 switch (data->type) {
458 case PLIST_BOOLEAN:
459 case PLIST_UINT:
460 case PLIST_REAL:
461 buff = (char *) &data->intval;
462 size = 8;
463 break;
464 case PLIST_KEY:
465 case PLIST_STRING:
466 buff = data->strval;
467 size = strlen(buff);
468 break;
469 case PLIST_UNICODE:
470 buff = data->unicodeval;
471 size = strlen(buff) * sizeof(wchar_t);
472 break;
473 case PLIST_DATA:
474 case PLIST_ARRAY:
475 case PLIST_DICT:
476 //for these types only hash pointer
477 buff = &key;
478 size = sizeof(gconstpointer);
479 break;
480 case PLIST_DATE:
481 default:
482 break;
483 }
484
485 //now perform hash
486 for (i = 0; i < size; buff++, i++)
487 hash = hash << 7 ^ (*buff);
488
489 return hash;
490}
491
492gboolean plist_data_compare(gconstpointer a, gconstpointer b)
493{
494 if (!a || !b)
495 return FALSE;
496
497 if (!((GNode *) a)->data || !((GNode *) b)->data)
498 return FALSE;
499
500 struct plist_data *val_a = (struct plist_data *) ((GNode *) a)->data;
501 struct plist_data *val_b = (struct plist_data *) ((GNode *) b)->data;
502
503 if (val_a->type != val_b->type)
504 return FALSE;
505
506 switch (val_a->type) {
507 case PLIST_BOOLEAN:
508 case PLIST_UINT:
509 case PLIST_REAL:
510 if (val_a->intval == val_b->intval) //it is an union so this is sufficient
511 return TRUE;
512 else
513 return FALSE;
514
515 case PLIST_KEY:
516 case PLIST_STRING:
517 if (!strcmp(val_a->strval, val_b->strval))
518 return TRUE;
519 else
520 return FALSE;
521 case PLIST_UNICODE:
522 if (!strcmp(val_a->unicodeval, val_b->unicodeval))
523 return TRUE;
524 else
525 return FALSE;
526
527 case PLIST_DATA:
528 case PLIST_ARRAY:
529 case PLIST_DICT:
530 //compare pointer
531 if (a == b)
532 return TRUE;
533 else
534 return FALSE;
535 break;
536 case PLIST_DATE:
537 default:
538 break;
539 }
540 return FALSE;
541}
542
543struct serialize_s {
544 GPtrArray *objects;
545 GHashTable *ref_table;
546};
547
548void serialize_plist(GNode * node, gpointer data)
549{
550 struct serialize_s *ser = (struct serialize_s *) data;
551 uint64_t current_index = ser->objects->len;
552
553 //first check that node is not yet in objects
554 gpointer val = g_hash_table_lookup(ser->ref_table, node);
555 if (val) {
556 //data is already in table
557 return;
558 }
559 //insert new ref
560 g_hash_table_insert(ser->ref_table, node, GUINT_TO_POINTER(current_index));
561
562 //now append current node to object array
563 g_ptr_array_add(ser->objects, node);
564
565 //now recurse on children
566 g_node_children_foreach(node, G_TRAVERSE_ALL, serialize_plist, data);
567 return;
568}
569
570
571
572void write_int(GByteArray * bplist, uint64_t val)
573{
574 uint64_t size = get_needed_bytes(val);
575 uint8_t *buff = (uint8_t *) malloc(sizeof(uint8_t) + size);
576 buff[0] = BPLIST_UINT | size >> 1;
577 memcpy(buff + 1, &val, size);
578 swap_n_bytes(buff + 1, size);
579 g_byte_array_append(bplist, buff, sizeof(uint8_t) + size);
580 free(buff);
581}
582
583void write_real(GByteArray * bplist, double val)
584{
585 uint64_t size = get_real_bytes(*((uint64_t *) & val)); //cheat to know used space
586 uint8_t *buff = (uint8_t *) malloc(sizeof(uint8_t) + size);
587 buff[0] = BPLIST_REAL | size >> 1;
588 memcpy(buff + 1, &val, size);
589 swap_n_bytes(buff + 1, size);
590 g_byte_array_append(bplist, buff, sizeof(uint8_t) + size);
591 free(buff);
592}
593
594void write_raw_data(GByteArray * bplist, uint8_t mark, uint8_t * val, uint64_t size)
595{
596 uint8_t marker = mark | (size < 15 ? size : 0xf);
597 g_byte_array_append(bplist, &marker, sizeof(uint8_t));
598 if (size >= 15) {
599 GByteArray *int_buff = g_byte_array_new();
600 write_int(int_buff, size);
601 g_byte_array_append(bplist, int_buff->data, int_buff->len);
602 g_byte_array_free(int_buff, TRUE);
603 }
604 uint8_t *buff = (uint8_t *) malloc(size);
605 memcpy(buff, val, size);
606 g_byte_array_append(bplist, buff, size);
607 free(buff);
608}
609
610void write_data(GByteArray * bplist, uint8_t * val, uint64_t size)
611{
612 write_raw_data(bplist, BPLIST_DATA, val, size);
613}
614
615void write_string(GByteArray * bplist, char *val)
616{
617 uint64_t size = strlen(val);
618 write_raw_data(bplist, BPLIST_STRING, val, size);
619}
620
621void write_array(GByteArray * bplist, GNode * node, GHashTable * ref_table, uint8_t dict_param_size)
622{
623 uint64_t size = g_node_n_children(node);
624 uint8_t marker = BPLIST_ARRAY | (size < 15 ? size : 0xf);
625 g_byte_array_append(bplist, &marker, sizeof(uint8_t));
626 if (size >= 15) {
627 GByteArray *int_buff = g_byte_array_new();
628 write_int(int_buff, size);
629 g_byte_array_append(bplist, int_buff->data, int_buff->len);
630 g_byte_array_free(int_buff, TRUE);
631 }
632
633 uint64_t idx = 0;
634 uint8_t *buff = (uint8_t *) malloc(size * dict_param_size);
635
636 GNode *cur = NULL;
637 int i = 0;
638 for (i = 0, cur = node->children; cur && i < size; cur = cur->next, i++) {
639 idx = GPOINTER_TO_UINT(g_hash_table_lookup(ref_table, cur));
640 memcpy(buff + i * dict_param_size, &idx, dict_param_size);
641 swap_n_bytes(buff + i * dict_param_size, dict_param_size);
642 }
643
644 //now append to bplist
645 g_byte_array_append(bplist, buff, size * dict_param_size);
646 free(buff);
647
648}
649
650void write_dict(GByteArray * bplist, GNode * node, GHashTable * ref_table, uint8_t dict_param_size)
651{
652 uint64_t size = g_node_n_children(node) / 2;
653 uint8_t marker = BPLIST_ARRAY | (size < 15 ? size : 0xf);
654 g_byte_array_append(bplist, &marker, sizeof(uint8_t));
655 if (size >= 15) {
656 GByteArray *int_buff = g_byte_array_new();
657 write_int(int_buff, size);
658 g_byte_array_append(bplist, int_buff->data, int_buff->len);
659 g_byte_array_free(int_buff, TRUE);
660 }
661
662 uint64_t idx1 = 0;
663 uint64_t idx2 = 0;
664 uint8_t *buff = (uint8_t *) malloc(size * 2 * dict_param_size);
665
666 GNode *cur = NULL;
667 int i = 0;
668 for (i = 0, cur = node->children; cur && i < size; cur = cur->next->next, i++) {
669 idx1 = GPOINTER_TO_UINT(g_hash_table_lookup(ref_table, cur));
670 memcpy(buff + i * dict_param_size, &idx1, dict_param_size);
671 swap_n_bytes(buff + i * dict_param_size, dict_param_size);
672
673 idx2 = GPOINTER_TO_UINT(g_hash_table_lookup(ref_table, cur->next));
674 memcpy(buff + (i + size) * dict_param_size, &idx2, dict_param_size);
675 swap_n_bytes(buff + (i + size) * dict_param_size, dict_param_size);
676 }
677
678 //now append to bplist
679 g_byte_array_append(bplist, buff, size * dict_param_size);
680 free(buff);
681
682}
683
684void plist_to_bin(plist_t plist, char **plist_bin, uint32_t * length)
685{
686 //first serialize tree
687
688 //list of objects
689 GPtrArray *objects = g_ptr_array_new();
690 //hashtable to write only once same nodes
691 GHashTable *ref_table = g_hash_table_new(plist_data_hash, plist_data_compare);
692
693 //serialize plist
694 struct serialize_s ser_s = { objects, ref_table };
695 g_node_children_foreach(plist, G_TRAVERSE_ALL, serialize_plist, &ser_s);
696
697 //now stream to output buffer
698 uint8_t offset_size = 0; //unknown yet
699 uint8_t dict_param_size = get_needed_bytes(objects->len);
700 uint64_t num_objects = objects->len;
701 uint64_t root_object = 0; //root is first in list
702 uint64_t offset_table_index = 0; //unknown yet
703
704 //setup a dynamic bytes array to store bplist in
705 GByteArray *bplist_buff = g_byte_array_new();
706
707 //set magic number and version
708 g_byte_array_append(bplist_buff, BPLIST_MAGIC, BPLIST_MAGIC_SIZE);
709 g_byte_array_append(bplist_buff, BPLIST_VERSION, BPLIST_VERSION_SIZE);
710
711 //write objects and table
712 int i = 0;
713 uint8_t *buff = NULL;
714 uint8_t size = 0;
715 uint64_t offsets[num_objects];
716 for (i = 0; i < num_objects; i++) {
717
718 offsets[i] = bplist_buff->len;
719 struct plist_data *data = (struct plist_data *) ((GNode *) g_ptr_array_index(objects, i))->data;
720
721 switch (data->type) {
722 case PLIST_BOOLEAN:
723 buff = (uint8_t *) malloc(sizeof(uint8_t));
724 buff[0] = data->boolval ? BPLIST_TRUE : BPLIST_FALSE;
725 g_byte_array_append(bplist_buff, buff, sizeof(uint8_t));
726 free(buff);
727 break;
728
729 case PLIST_UINT:
730 write_int(bplist_buff, data->intval);
731 break;
732
733 case PLIST_REAL:
734 write_real(bplist_buff, data->realval);
735 break;
736
737 case PLIST_KEY:
738 case PLIST_STRING:
739 write_string(bplist_buff, data->strval);
740 break;
741 case PLIST_UNICODE:
742 //TODO
743 break;
744 case PLIST_DATA:
745 write_data(bplist_buff, data->strval, data->length);
746 case PLIST_ARRAY:
747 write_array(bplist_buff, g_ptr_array_index(objects, i), ref_table, dict_param_size);
748 break;
749 case PLIST_DICT:
750 write_dict(bplist_buff, g_ptr_array_index(objects, i), ref_table, dict_param_size);
751 break;
752 case PLIST_DATE:
753 //TODO
754 break;
755 default:
756 break;
757 }
758 }
759
760 //write offsets
761 offset_size = get_needed_bytes(bplist_buff->len);
762 for (i = 0; i <= num_objects; i++) {
763 uint8_t *buff = (uint8_t *) malloc(offset_size);
764 memcpy(buff, offsets + i, offset_size);
765 swap_n_bytes(buff, offset_size);
766 g_byte_array_append(bplist_buff, buff, offset_size);
767 free(buff);
768 }
769
770 //setup trailer
771 num_objects = bswap_64(num_objects);
772 root_object = bswap_64(root_object);
773 offset_table_index = bswap_64(offset_table_index);
774
775 char trailer[BPLIST_TRL_SIZE];
776 memcpy(trailer + BPLIST_TRL_OFFSIZE_IDX, &offset_size, sizeof(uint8_t));
777 memcpy(trailer + BPLIST_TRL_PARMSIZE_IDX, &dict_param_size, sizeof(uint8_t));
778 memcpy(trailer + BPLIST_TRL_NUMOBJ_IDX, &num_objects, sizeof(uint64_t));
779 memcpy(trailer + BPLIST_TRL_ROOTOBJ_IDX, &root_object, sizeof(uint64_t));
780 memcpy(trailer + BPLIST_TRL_OFFTAB_IDX, &offset_table_index, sizeof(uint64_t));
781
782 g_byte_array_append(bplist_buff, trailer, BPLIST_TRL_SIZE);
783
784 //duplicate buffer
785 *plist_bin = (char *) malloc(bplist_buff->len);
786 memcpy(*plist_bin, bplist_buff->data, bplist_buff->len);
787 *length = bplist_buff->len;
788
789 g_byte_array_free(bplist_buff, TRUE);
790}
diff --git a/src/xplist.c b/src/xplist.c
new file mode 100644
index 0000000..a87b259
--- /dev/null
+++ b/src/xplist.c
@@ -0,0 +1,306 @@
1/*
2 * plist.c
3 * XML plist implementation
4 *
5 * Copyright (c) 2008 Jonathan Beck All Rights Reserved.
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22
23#include <string.h>
24#include <assert.h>
25#include "utils.h"
26#include "plist.h"
27#include <wchar.h>
28#include <stdlib.h>
29#include <stdio.h>
30
31
32#include <libxml/parser.h>
33#include <libxml/tree.h>
34
35
36const char *plist_base = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
37<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n\
38<plist version=\"1.0\">\n\
39</plist>\0";
40
41
42/** Formats a block of text to be a given indentation and width.
43 *
44 * The total width of the return string will be depth + cols.
45 *
46 * @param buf The string to format.
47 * @param cols The number of text columns for returned block of text.
48 * @param depth The number of tabs to indent the returned block of text.
49 *
50 * @return The formatted string.
51 */
52char *format_string(const char *buf, int cols, int depth)
53{
54 int colw = depth + cols + 1;
55 int len = strlen(buf);
56 int nlines = len / cols + 1;
57 char *new_buf = (char *) malloc(nlines * colw + depth + 1);
58 int i = 0;
59 int j = 0;
60
61 assert(cols >= 0);
62 assert(depth >= 0);
63
64 // Inserts new lines and tabs at appropriate locations
65 for (i = 0; i < nlines; i++) {
66 new_buf[i * colw] = '\n';
67 for (j = 0; j < depth; j++)
68 new_buf[i * colw + 1 + j] = '\t';
69 memcpy(new_buf + i * colw + 1 + depth, buf + i * cols, cols);
70 }
71 new_buf[len + (1 + depth) * nlines] = '\n';
72
73 // Inserts final row of indentation and termination character
74 for (j = 0; j < depth; j++)
75 new_buf[len + (1 + depth) * nlines + 1 + j] = '\t';
76 new_buf[len + (1 + depth) * nlines + depth + 1] = '\0';
77
78 return new_buf;
79}
80
81
82
83struct xml_node {
84 xmlNodePtr xml;
85 uint32_t depth;
86};
87
88/** Creates a new plist XML document.
89 *
90 * @return The plist XML document.
91 */
92xmlDocPtr new_plist()
93{
94 char *plist = strdup(plist_base);
95 xmlDocPtr plist_xml = xmlReadMemory(plist, strlen(plist), NULL, NULL, 0);
96
97 if (!plist_xml)
98 return NULL;
99
100 free(plist);
101
102 return plist_xml;
103}
104
105/** Destroys a previously created XML document.
106 *
107 * @param plist The XML document to destroy.
108 */
109void free_plist(xmlDocPtr plist)
110{
111 if (!plist)
112 return;
113
114 xmlFreeDoc(plist);
115}
116
117void node_to_xml(GNode * node, gpointer xml_struct)
118{
119 if (!node)
120 return;
121
122 struct xml_node *xstruct = (struct xml_node *) xml_struct;
123 struct plist_data *node_data = (struct plist_data *) node->data;
124
125 xmlNodePtr child_node = NULL;
126 char isStruct = FALSE;
127
128 gchar *tag = NULL;
129 gchar *val = NULL;
130
131 switch (node_data->type) {
132 case PLIST_BOOLEAN:
133 {
134 if (node_data->boolval)
135 tag = "true";
136 else
137 tag = "false";
138 }
139 break;
140
141 case PLIST_UINT:
142 tag = "integer";
143 val = g_strdup_printf("%lu", (long unsigned int) node_data->intval);
144 break;
145
146 case PLIST_REAL:
147 tag = "real";
148 val = g_strdup_printf("%Lf", (long double) node_data->realval);
149 break;
150
151 case PLIST_STRING:
152 tag = "string";
153 val = g_strdup(node_data->strval);
154 break;
155
156 case PLIST_UNICODE:
157 tag = "string";
158 val = g_strdup((gchar *) node_data->unicodeval);
159 break;
160
161 case PLIST_KEY:
162 tag = "key";
163 val = g_strdup((gchar *) node_data->strval);
164 break;
165
166 case PLIST_DATA:
167 tag = "data";
168 val = format_string(node_data->buff, 60, xstruct->depth);
169 break;
170 case PLIST_ARRAY:
171 tag = "array";
172 isStruct = TRUE;
173 break;
174 case PLIST_DICT:
175 tag = "dict";
176 isStruct = TRUE;
177 break;
178 case PLIST_DATE: //TODO : handle date tag
179 default:
180 break;
181 }
182
183 int i = 0;
184 for (i = 0; i < xstruct->depth; i++) {
185 xmlNodeAddContent(xstruct->xml, "\t");
186 }
187 child_node = xmlNewChild(xstruct->xml, NULL, tag, val);
188 xmlNodeAddContent(xstruct->xml, "\n");
189 g_free(val);
190
191 //add return for structured types
192 if (node_data->type == PLIST_ARRAY || node_data->type == PLIST_DICT || node_data->type == PLIST_DATA)
193 xmlNodeAddContent(child_node, "\n");
194
195 if (isStruct) {
196 struct xml_node child = { child_node, xstruct->depth + 1 };
197 g_node_children_foreach(node, G_TRAVERSE_ALL, node_to_xml, &child);
198 }
199 //fix indent for structured types
200 if (node_data->type == PLIST_ARRAY || node_data->type == PLIST_DICT || node_data->type == PLIST_DATA) {
201
202 for (i = 0; i < xstruct->depth; i++) {
203 xmlNodeAddContent(child_node, "\t");
204 }
205 }
206
207 return;
208}
209
210void xml_to_node(xmlNodePtr xml_node, GNode * plist_node)
211{
212 xmlNodePtr node = NULL;
213
214 for (node = xml_node->children; node; node = node->next) {
215
216 while (node && !xmlStrcmp(node->name, "text"))
217 node = node->next;
218 if (!node)
219 break;
220
221 struct plist_data *data = (struct plist_data *) calloc(sizeof(struct plist_data), 1);
222 GNode *subnode = g_node_new(data);
223 g_node_append(plist_node, subnode);
224
225 if (!xmlStrcmp(node->name, "true")) {
226 data->boolval = 1;
227 data->type = PLIST_BOOLEAN;
228 continue;
229 }
230
231 if (!xmlStrcmp(node->name, "false")) {
232 data->boolval = 0;
233 data->type = PLIST_BOOLEAN;
234 continue;
235 }
236
237 if (!xmlStrcmp(node->name, "integer")) {
238 char *strval = xmlNodeGetContent(node);
239 data->intval = atoi(strval);
240 data->type = PLIST_UINT;
241 continue;
242 }
243
244 if (!xmlStrcmp(node->name, "real")) {
245 char *strval = xmlNodeGetContent(node);
246 data->realval = atof(strval);
247 data->type = PLIST_REAL;
248 continue;
249 }
250
251 if (!xmlStrcmp(node->name, "date"))
252 continue; //TODO : handle date tag
253
254 if (!xmlStrcmp(node->name, "string")) {
255 data->strval = strdup(xmlNodeGetContent(node));
256 data->type = PLIST_STRING;
257 continue;
258 }
259
260 if (!xmlStrcmp(node->name, "key")) {
261 data->strval = strdup(xmlNodeGetContent(node));
262 data->type = PLIST_KEY;
263 continue;
264 }
265
266 if (!xmlStrcmp(node->name, "data")) {
267 data->buff = strdup(xmlNodeGetContent(node));
268 data->type = PLIST_DATA;
269 continue;
270 }
271
272 if (!xmlStrcmp(node->name, "array")) {
273 data->type = PLIST_ARRAY;
274 xml_to_node(node, subnode);
275 continue;
276 }
277
278 if (!xmlStrcmp(node->name, "dict")) {
279 data->type = PLIST_DICT;
280 xml_to_node(node, subnode);
281 continue;
282 }
283 }
284}
285
286void plist_to_xml(plist_t plist, char **plist_xml, uint32_t * length)
287{
288 if (!plist || !plist_xml || *plist_xml)
289 return;
290 xmlDocPtr plist_doc = new_plist();
291 xmlNodePtr root_node = xmlDocGetRootElement(plist_doc);
292 struct xml_node root = { root_node, 0 };
293 g_node_children_foreach(plist, G_TRAVERSE_ALL, node_to_xml, &root);
294 xmlDocDumpMemory(plist_doc, (xmlChar **) plist_xml, length);
295}
296
297void xml_to_plist(const char *plist_xml, uint32_t length, plist_t * plist)
298{
299 xmlDocPtr plist_doc = xmlReadMemory(plist_xml, length, NULL, NULL, 0);
300 xmlNodePtr root_node = xmlDocGetRootElement(plist_doc);
301
302 struct plist_data *data = (struct plist_data *) calloc(sizeof(struct plist_data), 1);
303 *plist = g_node_new(data);
304 data->type = PLIST_DICT;
305 xml_to_node(root_node, *plist);
306}