summaryrefslogtreecommitdiffstats
path: root/src/bplist.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/bplist.c')
-rw-r--r--src/bplist.c66
1 files changed, 39 insertions, 27 deletions
diff --git a/src/bplist.c b/src/bplist.c
index ed381db..7a7225d 100644
--- a/src/bplist.c
+++ b/src/bplist.c
@@ -27,10 +27,10 @@
27#include <string.h> 27#include <string.h>
28 28
29/* Magic marker and size. */ 29/* Magic marker and size. */
30#define BPLIST_MAGIC "bplist" 30#define BPLIST_MAGIC ((uint8_t*)"bplist")
31#define BPLIST_MAGIC_SIZE 6 31#define BPLIST_MAGIC_SIZE 6
32 32
33#define BPLIST_VERSION "00" 33#define BPLIST_VERSION ((uint8_t*)"00")
34#define BPLIST_VERSION_SIZE 2 34#define BPLIST_VERSION_SIZE 2
35 35
36 36
@@ -59,11 +59,11 @@ enum {
59 BPLIST_MASK = 0xF0 59 BPLIST_MASK = 0xF0
60}; 60};
61 61
62static void byte_convert(char *address, size_t size) 62static void byte_convert(uint8_t *address, size_t size)
63{ 63{
64#if G_BYTE_ORDER == G_LITTLE_ENDIAN 64#if G_BYTE_ORDER == G_LITTLE_ENDIAN
65 uint8_t i = 0, j = 0; 65 uint8_t i = 0, j = 0;
66 char tmp = '\0'; 66 uint8_t tmp = 0;
67 67
68 for (i = 0; i < (size / 2); i++) { 68 for (i = 0; i < (size / 2); i++) {
69 tmp = address[i]; 69 tmp = address[i];
@@ -75,13 +75,18 @@ static void byte_convert(char *address, size_t size)
75} 75}
76 76
77#define swap_n_bytes(x, n) \ 77#define swap_n_bytes(x, n) \
78 n == 8 ? GUINT64_FROM_BE( *(uint64_t *)(x) ) : \ 78 (n == 8 ? GUINT64_FROM_BE( *(uint64_t *)(x) ) : \
79 (n == 4 ? GUINT32_FROM_BE( *(uint32_t *)(x) ) : \ 79 (n == 4 ? GUINT32_FROM_BE( *(uint32_t *)(x) ) : \
80 (n == 2 ? GUINT16_FROM_BE( *(uint16_t *)(x) ) : *(uint8_t *)(x) )) 80 (n == 2 ? GUINT16_FROM_BE( *(uint16_t *)(x) ) : \
81 *(uint8_t *)(x) )))
81 82
82#define be64dec(x) GUINT64_FROM_BE( *(uint64_t*)(x) ) 83#define be64dec(x) GUINT64_FROM_BE( *(uint64_t*)(x) )
83 84
84#define get_needed_bytes(x) (x <= 1<<8 ? 1 : ( x <= 1<<16 ? 2 : ( x <= (uint64_t)1<<32 ? 4 : 8))) 85#define get_needed_bytes(x) \
86 ( ((uint64_t)x) < (1ULL << 8) ? 1 : \
87 ( ((uint64_t)x) < (1ULL << 16) ? 2 : \
88 ( ((uint64_t)x) < (1ULL << 32) ? 4 : 8)))
89
85#define get_real_bytes(x) (x >> 32 ? 4 : 8) 90#define get_real_bytes(x) (x >> 32 ? 4 : 8)
86 91
87 92
@@ -118,12 +123,8 @@ static plist_t parse_real_node(char *bnode, uint8_t size)
118 size = 1 << size; // make length less misleading 123 size = 1 << size; // make length less misleading
119 switch (size) { 124 switch (size) {
120 case sizeof(float): 125 case sizeof(float):
121 memcpy(&data->realval, bnode, size);
122 byte_convert((char *) &data->realval, size);
123 break;
124 case sizeof(double): 126 case sizeof(double):
125 memcpy(&data->realval, bnode, size); 127 data->realval = swap_n_bytes(bnode, size);
126 byte_convert((char *) &data->realval, size);
127 break; 128 break;
128 default: 129 default:
129 free(data); 130 free(data);
@@ -163,8 +164,8 @@ static plist_t parse_data_node(char *bnode, uint64_t size)
163 164
164 data->type = PLIST_DATA; 165 data->type = PLIST_DATA;
165 data->length = size; 166 data->length = size;
166 data->buff = (char *) malloc(sizeof(char) * size); 167 data->buff = (uint8_t *) malloc(sizeof(uint8_t) * size);
167 memcpy(data->buff, bnode, sizeof(char) * size); 168 memcpy(data->buff, bnode, sizeof(uint8_t) * size);
168 169
169 return g_node_new(data); 170 return g_node_new(data);
170} 171}
@@ -175,8 +176,8 @@ static plist_t parse_dict_node(char *bnode, uint64_t size, uint32_t ref_size)
175 176
176 data->type = PLIST_DICT; 177 data->type = PLIST_DICT;
177 data->length = size; 178 data->length = size;
178 data->buff = (char *) malloc(sizeof(char) * size * ref_size * 2); 179 data->buff = (uint8_t *) malloc(sizeof(uint8_t) * size * ref_size * 2);
179 memcpy(data->buff, bnode, sizeof(char) * size * ref_size * 2); 180 memcpy(data->buff, bnode, sizeof(uint8_t) * size * ref_size * 2);
180 181
181 return g_node_new(data); 182 return g_node_new(data);
182} 183}
@@ -187,8 +188,8 @@ static plist_t parse_array_node(char *bnode, uint64_t size, uint32_t ref_size)
187 188
188 data->type = PLIST_ARRAY; 189 data->type = PLIST_ARRAY;
189 data->length = size; 190 data->length = size;
190 data->buff = (char *) malloc(sizeof(char) * size * ref_size); 191 data->buff = (uint8_t *) malloc(sizeof(uint8_t) * size * ref_size);
191 memcpy(data->buff, bnode, sizeof(char) * size * ref_size); 192 memcpy(data->buff, bnode, sizeof(uint8_t) * size * ref_size);
192 193
193 return g_node_new(data); 194 return g_node_new(data);
194} 195}
@@ -321,12 +322,12 @@ static gpointer copy_plist_data(gconstpointer src, gpointer data)
321 break; 322 break;
322 case PLIST_DATA: 323 case PLIST_DATA:
323 case PLIST_ARRAY: 324 case PLIST_ARRAY:
324 dstdata->buff = (char *) malloc(sizeof(char *) * srcdata->length); 325 dstdata->buff = (uint8_t *) malloc(sizeof(uint8_t *) * srcdata->length);
325 memcpy(dstdata->buff, srcdata->buff, sizeof(char *) * srcdata->length); 326 memcpy(dstdata->buff, srcdata->buff, sizeof(uint8_t *) * srcdata->length);
326 break; 327 break;
327 case PLIST_DICT: 328 case PLIST_DICT:
328 dstdata->buff = (char *) malloc(sizeof(char *) * srcdata->length * 2); 329 dstdata->buff = (uint8_t *) malloc(sizeof(uint8_t *) * srcdata->length * 2);
329 memcpy(dstdata->buff, srcdata->buff, sizeof(char *) * srcdata->length * 2); 330 memcpy(dstdata->buff, srcdata->buff, sizeof(uint8_t *) * srcdata->length * 2);
330 break; 331 break;
331 default: 332 default:
332 break; 333 break;
@@ -561,7 +562,9 @@ static void serialize_plist(GNode * node, gpointer data)
561 return; 562 return;
562 } 563 }
563 //insert new ref 564 //insert new ref
564 g_hash_table_insert(ser->ref_table, node, GUINT_TO_POINTER(current_index)); 565 uint64_t* index_val = (uint64_t*) malloc(sizeof(uint64_t));
566 *index_val = current_index;
567 g_hash_table_insert(ser->ref_table, node, index_val);
565 568
566 //now append current node to object array 569 //now append current node to object array
567 g_ptr_array_add(ser->objects, node); 570 g_ptr_array_add(ser->objects, node);
@@ -571,6 +574,12 @@ static void serialize_plist(GNode * node, gpointer data)
571 return; 574 return;
572} 575}
573 576
577static gboolean free_index (gpointer key, gpointer value, gpointer user_data)
578{
579 free((uint64_t*)value);
580 return TRUE;
581}
582
574#define Log2(x) (x == 8 ? 3 : (x == 4 ? 2 : (x == 2 ? 1 : 0))) 583#define Log2(x) (x == 8 ? 3 : (x == 4 ? 2 : (x == 2 ? 1 : 0)))
575 584
576static void write_int(GByteArray * bplist, uint64_t val) 585static void write_int(GByteArray * bplist, uint64_t val)
@@ -619,7 +628,7 @@ static void write_data(GByteArray * bplist, uint8_t * val, uint64_t size)
619static void write_string(GByteArray * bplist, char *val) 628static void write_string(GByteArray * bplist, char *val)
620{ 629{
621 uint64_t size = strlen(val); 630 uint64_t size = strlen(val);
622 write_raw_data(bplist, BPLIST_STRING, val, size); 631 write_raw_data(bplist, BPLIST_STRING, (uint8_t*) val, size);
623} 632}
624 633
625static void write_array(GByteArray * bplist, GNode * node, GHashTable * ref_table, uint8_t dict_param_size) 634static void write_array(GByteArray * bplist, GNode * node, GHashTable * ref_table, uint8_t dict_param_size)
@@ -640,7 +649,7 @@ static void write_array(GByteArray * bplist, GNode * node, GHashTable * ref_tabl
640 GNode *cur = NULL; 649 GNode *cur = NULL;
641 uint64_t i = 0; 650 uint64_t i = 0;
642 for (i = 0, cur = node->children; cur && i < size; cur = cur->next, i++) { 651 for (i = 0, cur = node->children; cur && i < size; cur = cur->next, i++) {
643 idx = GPOINTER_TO_UINT(g_hash_table_lookup(ref_table, cur)); 652 idx = *(uint64_t*)(g_hash_table_lookup(ref_table, cur));
644 memcpy(buff + i * dict_param_size, &idx, dict_param_size); 653 memcpy(buff + i * dict_param_size, &idx, dict_param_size);
645 byte_convert(buff + i * dict_param_size, dict_param_size); 654 byte_convert(buff + i * dict_param_size, dict_param_size);
646 } 655 }
@@ -747,7 +756,7 @@ void plist_to_bin(plist_t plist, char **plist_bin, uint32_t * length)
747 //TODO 756 //TODO
748 break; 757 break;
749 case PLIST_DATA: 758 case PLIST_DATA:
750 write_data(bplist_buff, data->strval, data->length); 759 write_data(bplist_buff, data->buff, data->length);
751 case PLIST_ARRAY: 760 case PLIST_ARRAY:
752 write_array(bplist_buff, g_ptr_array_index(objects, i), ref_table, dict_param_size); 761 write_array(bplist_buff, g_ptr_array_index(objects, i), ref_table, dict_param_size);
753 break; 762 break;
@@ -762,6 +771,9 @@ void plist_to_bin(plist_t plist, char **plist_bin, uint32_t * length)
762 } 771 }
763 } 772 }
764 773
774 //free intermediate objects
775 g_hash_table_foreach_remove (ref_table, free_index, NULL);
776
765 //write offsets 777 //write offsets
766 offset_size = get_needed_bytes(bplist_buff->len); 778 offset_size = get_needed_bytes(bplist_buff->len);
767 offset_table_index = bplist_buff->len; 779 offset_table_index = bplist_buff->len;
@@ -778,7 +790,7 @@ void plist_to_bin(plist_t plist, char **plist_bin, uint32_t * length)
778 root_object = GUINT64_FROM_BE(root_object); 790 root_object = GUINT64_FROM_BE(root_object);
779 offset_table_index = GUINT64_FROM_BE(offset_table_index); 791 offset_table_index = GUINT64_FROM_BE(offset_table_index);
780 792
781 char trailer[BPLIST_TRL_SIZE]; 793 uint8_t trailer[BPLIST_TRL_SIZE];
782 memcpy(trailer + BPLIST_TRL_OFFSIZE_IDX, &offset_size, sizeof(uint8_t)); 794 memcpy(trailer + BPLIST_TRL_OFFSIZE_IDX, &offset_size, sizeof(uint8_t));
783 memcpy(trailer + BPLIST_TRL_PARMSIZE_IDX, &dict_param_size, sizeof(uint8_t)); 795 memcpy(trailer + BPLIST_TRL_PARMSIZE_IDX, &dict_param_size, sizeof(uint8_t));
784 memcpy(trailer + BPLIST_TRL_NUMOBJ_IDX, &num_objects, sizeof(uint64_t)); 796 memcpy(trailer + BPLIST_TRL_NUMOBJ_IDX, &num_objects, sizeof(uint64_t));