summaryrefslogtreecommitdiffstats
path: root/src/bplist.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/bplist.c')
-rw-r--r--src/bplist.c793
1 files changed, 0 insertions, 793 deletions
diff --git a/src/bplist.c b/src/bplist.c
deleted file mode 100644
index a5b1c9b..0000000
--- a/src/bplist.c
+++ /dev/null
@@ -1,793 +0,0 @@
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#define Log2(x) (x == 8 ? 3 : (x == 4 ? 2 : (x == 2 ? 1 : 0)))
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 | Log2(size);
577 memcpy(buff + 1, &val, size);
578 byte_convert(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 | Log2(size);
588 memcpy(buff + 1, &val, size);
589 byte_convert(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 byte_convert(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_DICT | (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 byte_convert(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 byte_convert(buff + (i + size) * dict_param_size, dict_param_size);
676 }
677
678 //now append to bplist
679 g_byte_array_append(bplist, buff, size * 2 * dict_param_size);
680 free(buff);
681
682}
683
684void plist_to_bin(plist_t plist, char **plist_bin, uint32_t * length)
685{
686 //check for valid input
687 if (!plist || !plist_bin || *plist_bin || !length)
688 return;
689
690 //list of objects
691 GPtrArray *objects = g_ptr_array_new();
692 //hashtable to write only once same nodes
693 GHashTable *ref_table = g_hash_table_new(plist_data_hash, plist_data_compare);
694
695 //serialize plist
696 struct serialize_s ser_s = { objects, ref_table };
697 serialize_plist(plist, &ser_s);
698
699 //now stream to output buffer
700 uint8_t offset_size = 0; //unknown yet
701 uint8_t dict_param_size = get_needed_bytes(objects->len);
702 uint64_t num_objects = objects->len;
703 uint64_t root_object = 0; //root is first in list
704 uint64_t offset_table_index = 0; //unknown yet
705
706 //setup a dynamic bytes array to store bplist in
707 GByteArray *bplist_buff = g_byte_array_new();
708
709 //set magic number and version
710 g_byte_array_append(bplist_buff, BPLIST_MAGIC, BPLIST_MAGIC_SIZE);
711 g_byte_array_append(bplist_buff, BPLIST_VERSION, BPLIST_VERSION_SIZE);
712
713 //write objects and table
714 int i = 0;
715 uint8_t *buff = NULL;
716 uint8_t size = 0;
717 uint64_t offsets[num_objects];
718 for (i = 0; i < num_objects; i++) {
719
720 offsets[i] = bplist_buff->len;
721 struct plist_data *data = (struct plist_data *) ((GNode *) g_ptr_array_index(objects, i))->data;
722
723 switch (data->type) {
724 case PLIST_BOOLEAN:
725 buff = (uint8_t *) malloc(sizeof(uint8_t));
726 buff[0] = data->boolval ? BPLIST_TRUE : BPLIST_FALSE;
727 g_byte_array_append(bplist_buff, buff, sizeof(uint8_t));
728 free(buff);
729 break;
730
731 case PLIST_UINT:
732 write_int(bplist_buff, data->intval);
733 break;
734
735 case PLIST_REAL:
736 write_real(bplist_buff, data->realval);
737 break;
738
739 case PLIST_KEY:
740 case PLIST_STRING:
741 write_string(bplist_buff, data->strval);
742 break;
743 case PLIST_UNICODE:
744 //TODO
745 break;
746 case PLIST_DATA:
747 write_data(bplist_buff, data->strval, data->length);
748 case PLIST_ARRAY:
749 write_array(bplist_buff, g_ptr_array_index(objects, i), ref_table, dict_param_size);
750 break;
751 case PLIST_DICT:
752 write_dict(bplist_buff, g_ptr_array_index(objects, i), ref_table, dict_param_size);
753 break;
754 case PLIST_DATE:
755 //TODO
756 break;
757 default:
758 break;
759 }
760 }
761
762 //write offsets
763 offset_size = get_needed_bytes(bplist_buff->len);
764 offset_table_index = bplist_buff->len;
765 for (i = 0; i <= num_objects; i++) {
766 uint8_t *buff = (uint8_t *) malloc(offset_size);
767 memcpy(buff, offsets + i, offset_size);
768 byte_convert(buff, offset_size);
769 g_byte_array_append(bplist_buff, buff, offset_size);
770 free(buff);
771 }
772
773 //setup trailer
774 num_objects = bswap_64(num_objects);
775 root_object = bswap_64(root_object);
776 offset_table_index = bswap_64(offset_table_index);
777
778 char trailer[BPLIST_TRL_SIZE];
779 memcpy(trailer + BPLIST_TRL_OFFSIZE_IDX, &offset_size, sizeof(uint8_t));
780 memcpy(trailer + BPLIST_TRL_PARMSIZE_IDX, &dict_param_size, sizeof(uint8_t));
781 memcpy(trailer + BPLIST_TRL_NUMOBJ_IDX, &num_objects, sizeof(uint64_t));
782 memcpy(trailer + BPLIST_TRL_ROOTOBJ_IDX, &root_object, sizeof(uint64_t));
783 memcpy(trailer + BPLIST_TRL_OFFTAB_IDX, &offset_table_index, sizeof(uint64_t));
784
785 g_byte_array_append(bplist_buff, trailer, BPLIST_TRL_SIZE);
786
787 //duplicate buffer
788 *plist_bin = (char *) malloc(bplist_buff->len);
789 memcpy(*plist_bin, bplist_buff->data, bplist_buff->len);
790 *length = bplist_buff->len;
791
792 g_byte_array_free(bplist_buff, TRUE);
793}