summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Makefile.am2
-rw-r--r--include/plist/plist.h21
-rw-r--r--src/bplist.c26
-rw-r--r--src/plist.c173
-rw-r--r--src/plist.h1
-rw-r--r--src/xplist.c14
6 files changed, 176 insertions, 61 deletions
diff --git a/Makefile.am b/Makefile.am
index e88e9be..264b0a3 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -9,4 +9,4 @@ doc:
9 doxygen doxygen.cfg 9 doxygen doxygen.cfg
10 10
11indent: 11indent:
12 indent -kr -ut -ts4 -l120 src/*.c src/*.h dev/*.c 12 indent -kr -ut -ts4 -l120 src/*.c src/*.h plutil/*.c
diff --git a/include/plist/plist.h b/include/plist/plist.h
index b6335f5..b496d6f 100644
--- a/include/plist/plist.h
+++ b/include/plist/plist.h
@@ -52,6 +52,17 @@ plist_t plist_new_array();
52//Plist edition, create a new root if node is NULL 52//Plist edition, create a new root if node is NULL
53plist_t plist_add_sub_element( plist_t node, plist_type type, void* value, uint64_t length); 53plist_t plist_add_sub_element( plist_t node, plist_type type, void* value, uint64_t length);
54 54
55//Plist edition, only work for dict and array node
56void plist_add_sub_node(plist_t node, plist_t subnode);
57
58void plist_add_sub_key_el(plist_t node, char* val);
59void plist_add_sub_string_el(plist_t node, char* val);
60void plist_add_sub_bool_el(plist_t node, uint8_t val);
61void plist_add_sub_uint_el(plist_t node, uint64_t val);
62void plist_add_sub_real_el(plist_t node, double val);
63void plist_add_sub_data_el(plist_t node, char* val, uint64_t length);
64
65
55//plist free 66//plist free
56void plist_free(plist_t plist); 67void plist_free(plist_t plist);
57 68
@@ -63,6 +74,16 @@ plist_t plist_get_prev_sibling(plist_t node);
63plist_t plist_find_node(plist_t plist, plist_type type, void *value, uint64_t length); 74plist_t plist_find_node(plist_t plist, plist_type type, void *value, uint64_t length);
64void plist_get_type_and_value(plist_t node, plist_type * type, void *value, uint64_t * length); 75void plist_get_type_and_value(plist_t node, plist_type * type, void *value, uint64_t * length);
65 76
77//Plist reading
78plist_type plist_get_node_type(plist_t node);
79
80void plist_get_key_val(plist_t node, char** val);
81void plist_get_string_val(plist_t node, char** val);
82void plist_get_bool_val(plist_t node, uint8_t* val);
83void plist_get_uint_val(plist_t node, uint64_t* val);
84void plist_get_real_val(plist_t node, double* val);
85void plist_get_data_val(plist_t node, char** val, uint64_t* length);
86
66//import and export functions 87//import and export functions
67void plist_to_xml(plist_t plist, char **plist_xml, uint32_t * length); 88void plist_to_xml(plist_t plist, char **plist_xml, uint32_t * length);
68void plist_to_bin(plist_t plist, char **plist_bin, uint32_t * length); 89void plist_to_bin(plist_t plist, char **plist_bin, uint32_t * length);
diff --git a/src/bplist.c b/src/bplist.c
index cbe53cd..b633535 100644
--- a/src/bplist.c
+++ b/src/bplist.c
@@ -59,7 +59,7 @@ enum {
59 BPLIST_MASK = 0xF0 59 BPLIST_MASK = 0xF0
60}; 60};
61 61
62static void byte_convert(uint8_t *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;
@@ -124,7 +124,7 @@ static plist_t parse_real_node(char *bnode, uint8_t size)
124 switch (size) { 124 switch (size) {
125 case sizeof(float): 125 case sizeof(float):
126 case sizeof(double): 126 case sizeof(double):
127 data->intval = UINT_TO_HOST(bnode, size); //use the fact that we have an union to cheat byte swapping 127 data->intval = UINT_TO_HOST(bnode, size); //use the fact that we have an union to cheat byte swapping
128 break; 128 break;
129 default: 129 default:
130 free(data); 130 free(data);
@@ -140,8 +140,8 @@ static plist_t parse_date_node(char *bnode, uint8_t size)
140 plist_data_t data = plist_get_data(node); 140 plist_data_t data = plist_get_data(node);
141 141
142 double time_real = data->realval; 142 double time_real = data->realval;
143 data->timeval.tv_sec = (glong)time_real; 143 data->timeval.tv_sec = (glong) time_real;
144 data->timeval.tv_usec = (time_real - (glong)time_real) * G_USEC_PER_SEC; 144 data->timeval.tv_usec = (time_real - (glong) time_real) * G_USEC_PER_SEC;
145 data->type = PLIST_DATE; 145 data->type = PLIST_DATE;
146 return node; 146 return node;
147} 147}
@@ -475,7 +475,7 @@ static guint plist_data_hash(gconstpointer key)
475 case PLIST_BOOLEAN: 475 case PLIST_BOOLEAN:
476 case PLIST_UINT: 476 case PLIST_UINT:
477 case PLIST_REAL: 477 case PLIST_REAL:
478 buff = (char *) &data->intval; //works also for real as we use an union 478 buff = (char *) &data->intval; //works also for real as we use an union
479 size = 8; 479 size = 8;
480 break; 480 break;
481 case PLIST_KEY: 481 case PLIST_KEY:
@@ -574,7 +574,7 @@ static void serialize_plist(GNode * node, gpointer data)
574 return; 574 return;
575 } 575 }
576 //insert new ref 576 //insert new ref
577 uint64_t* index_val = (uint64_t*) malloc(sizeof(uint64_t)); 577 uint64_t *index_val = (uint64_t *) malloc(sizeof(uint64_t));
578 *index_val = current_index; 578 *index_val = current_index;
579 g_hash_table_insert(ser->ref_table, node, index_val); 579 g_hash_table_insert(ser->ref_table, node, index_val);
580 580
@@ -586,9 +586,9 @@ static void serialize_plist(GNode * node, gpointer data)
586 return; 586 return;
587} 587}
588 588
589static gboolean free_index (gpointer key, gpointer value, gpointer user_data) 589static gboolean free_index(gpointer key, gpointer value, gpointer user_data)
590{ 590{
591 free((uint64_t*)value); 591 free((uint64_t *) value);
592 return TRUE; 592 return TRUE;
593} 593}
594 594
@@ -618,7 +618,7 @@ static void write_real(GByteArray * bplist, double val)
618 618
619static void write_date(GByteArray * bplist, double val) 619static void write_date(GByteArray * bplist, double val)
620{ 620{
621 uint64_t size = 8; //dates always use 8 bytes 621 uint64_t size = 8; //dates always use 8 bytes
622 uint8_t *buff = (uint8_t *) malloc(sizeof(uint8_t) + size); 622 uint8_t *buff = (uint8_t *) malloc(sizeof(uint8_t) + size);
623 buff[0] = BPLIST_DATE | Log2(size); 623 buff[0] = BPLIST_DATE | Log2(size);
624 memcpy(buff + 1, &val, size); 624 memcpy(buff + 1, &val, size);
@@ -651,7 +651,7 @@ static void write_data(GByteArray * bplist, uint8_t * val, uint64_t size)
651static void write_string(GByteArray * bplist, char *val) 651static void write_string(GByteArray * bplist, char *val)
652{ 652{
653 uint64_t size = strlen(val); 653 uint64_t size = strlen(val);
654 write_raw_data(bplist, BPLIST_STRING, (uint8_t*) val, size); 654 write_raw_data(bplist, BPLIST_STRING, (uint8_t *) val, size);
655} 655}
656 656
657static void write_array(GByteArray * bplist, GNode * node, GHashTable * ref_table, uint8_t dict_param_size) 657static void write_array(GByteArray * bplist, GNode * node, GHashTable * ref_table, uint8_t dict_param_size)
@@ -672,7 +672,7 @@ static void write_array(GByteArray * bplist, GNode * node, GHashTable * ref_tabl
672 GNode *cur = NULL; 672 GNode *cur = NULL;
673 uint64_t i = 0; 673 uint64_t i = 0;
674 for (i = 0, cur = node->children; cur && i < size; cur = cur->next, i++) { 674 for (i = 0, cur = node->children; cur && i < size; cur = cur->next, i++) {
675 idx = *(uint64_t*)(g_hash_table_lookup(ref_table, cur)); 675 idx = *(uint64_t *) (g_hash_table_lookup(ref_table, cur));
676 memcpy(buff + i * dict_param_size, &idx, dict_param_size); 676 memcpy(buff + i * dict_param_size, &idx, dict_param_size);
677 byte_convert(buff + i * dict_param_size, dict_param_size); 677 byte_convert(buff + i * dict_param_size, dict_param_size);
678 } 678 }
@@ -787,7 +787,7 @@ void plist_to_bin(plist_t plist, char **plist_bin, uint32_t * length)
787 write_dict(bplist_buff, g_ptr_array_index(objects, i), ref_table, dict_param_size); 787 write_dict(bplist_buff, g_ptr_array_index(objects, i), ref_table, dict_param_size);
788 break; 788 break;
789 case PLIST_DATE: 789 case PLIST_DATE:
790 write_date(bplist_buff, data->timeval.tv_sec + (double)data->timeval.tv_usec / G_USEC_PER_SEC ); 790 write_date(bplist_buff, data->timeval.tv_sec + (double) data->timeval.tv_usec / G_USEC_PER_SEC);
791 break; 791 break;
792 default: 792 default:
793 break; 793 break;
@@ -795,7 +795,7 @@ void plist_to_bin(plist_t plist, char **plist_bin, uint32_t * length)
795 } 795 }
796 796
797 //free intermediate objects 797 //free intermediate objects
798 g_hash_table_foreach_remove (ref_table, free_index, NULL); 798 g_hash_table_foreach_remove(ref_table, free_index, NULL);
799 799
800 //write offsets 800 //write offsets
801 offset_size = get_needed_bytes(bplist_buff->len); 801 offset_size = get_needed_bytes(bplist_buff->len);
diff --git a/src/plist.c b/src/plist.c
index d737ab8..640bbe5 100644
--- a/src/plist.c
+++ b/src/plist.c
@@ -74,46 +74,50 @@ plist_t plist_new_array()
74 74
75plist_t plist_add_sub_element(plist_t node, plist_type type, void *value, uint64_t length) 75plist_t plist_add_sub_element(plist_t node, plist_type type, void *value, uint64_t length)
76{ 76{
77 //only structured types are allowed to have nulll value 77 //only structured types can have children
78 if (value || (!value && (type == PLIST_DICT || type == PLIST_ARRAY))) { 78 plist_type node_type = plist_get_node_type(node);
79 //now handle value 79 if (node_type == PLIST_DICT || node_type == PLIST_ARRAY) {
80 plist_data_t data = plist_new_plist_data(); 80 //only structured types are allowed to have nulll value
81 data->type = type; 81 if (value || (!value && (type == PLIST_DICT || type == PLIST_ARRAY))) {
82 data->length = length; 82 //now handle value
83 83 plist_data_t data = plist_new_plist_data();
84 switch (type) { 84 data->type = type;
85 case PLIST_BOOLEAN: 85 data->length = length;
86 data->boolval = *((char *) value);
87 break;
88 case PLIST_UINT:
89 data->intval = *((uint64_t *) value);
90 break;
91 case PLIST_REAL:
92 data->realval = *((double *) value);
93 break;
94 case PLIST_KEY:
95 case PLIST_STRING:
96 data->strval = strdup((char *) value);
97 break;
98 case PLIST_UNICODE:
99 data->unicodeval = wcsdup((wchar_t *) value);
100 break;
101 case PLIST_DATA:
102 memcpy(data->buff, value, length);
103 break;
104 case PLIST_ARRAY:
105 case PLIST_DICT:
106 case PLIST_DATE:
107 default:
108 break;
109 }
110 86
111 plist_t subnode = plist_new_node(data); 87 switch (type) {
112 if (node) 88 case PLIST_BOOLEAN:
113 g_node_append(node, subnode); 89 data->boolval = *((char *) value);
114 return subnode; 90 break;
115 } else 91 case PLIST_UINT:
116 return NULL; 92 data->intval = *((uint64_t *) value);
93 break;
94 case PLIST_REAL:
95 data->realval = *((double *) value);
96 break;
97 case PLIST_KEY:
98 case PLIST_STRING:
99 data->strval = strdup((char *) value);
100 break;
101 case PLIST_UNICODE:
102 data->unicodeval = wcsdup((wchar_t *) value);
103 break;
104 case PLIST_DATA:
105 memcpy(data->buff, value, length);
106 break;
107 case PLIST_ARRAY:
108 case PLIST_DICT:
109 case PLIST_DATE:
110 default:
111 break;
112 }
113
114 plist_t subnode = plist_new_node(data);
115 if (node)
116 g_node_append(node, subnode);
117 return subnode;
118 } else
119 return NULL;
120 }
117} 121}
118 122
119void plist_free(plist_t plist) 123void plist_free(plist_t plist)
@@ -243,3 +247,94 @@ uint64_t plist_get_node_uint_val(plist_t node)
243 else 247 else
244 return 0; 248 return 0;
245} 249}
250
251void plist_add_sub_node(plist_t node, plist_t subnode)
252{
253 if (node && subnode) {
254 plist_type type = plist_get_node_type(node);
255 if (type == PLIST_DICT || type == PLIST_ARRAY)
256 g_node_append(node, subnode);
257 }
258}
259
260void plist_add_sub_key_el(plist_t node, char *val)
261{
262 plist_add_sub_element(node, PLIST_KEY, val, strlen(val));
263}
264
265void plist_add_sub_string_el(plist_t node, char *val)
266{
267 plist_add_sub_element(node, PLIST_STRING, val, strlen(val));
268}
269
270void plist_add_sub_bool_el(plist_t node, uint8_t val)
271{
272 plist_add_sub_element(node, PLIST_BOOLEAN, &val, sizeof(uint8_t));
273}
274
275void plist_add_sub_uint_el(plist_t node, uint64_t val)
276{
277 plist_add_sub_element(node, PLIST_UINT, &val, sizeof(uint64_t));
278}
279
280void plist_add_sub_real_el(plist_t node, double val)
281{
282 plist_add_sub_element(node, PLIST_REAL, &val, sizeof(double));
283}
284
285void plist_add_sub_data_el(plist_t node, char *val, uint64_t length)
286{
287 plist_add_sub_element(node, PLIST_DATA, val, length);
288}
289
290void plist_get_key_val(plist_t node, char **val)
291{
292 plist_type type = plist_get_node_type(node);
293 uint64_t length = 0;
294 if (PLIST_KEY == type)
295 plist_get_type_and_value(node, &type, (void *) val, &length);
296 assert(length == strlen(*val));
297}
298
299void plist_get_string_val(plist_t node, char **val)
300{
301 plist_type type = plist_get_node_type(node);
302 uint64_t length = 0;
303 if (PLIST_STRING == type)
304 plist_get_type_and_value(node, &type, (void *) val, &length);
305 assert(length == strlen(*val));
306}
307
308void plist_get_bool_val(plist_t node, uint8_t * val)
309{
310 plist_type type = plist_get_node_type(node);
311 uint64_t length = 0;
312 if (PLIST_BOOLEAN == type)
313 plist_get_type_and_value(node, &type, (void *) val, &length);
314 assert(length == sizeof(uint8_t));
315}
316
317void plist_get_uint_val(plist_t node, uint64_t * val)
318{
319 plist_type type = plist_get_node_type(node);
320 uint64_t length = 0;
321 if (PLIST_UINT == type)
322 plist_get_type_and_value(node, &type, (void *) val, &length);
323 assert(length == sizeof(uint64_t));
324}
325
326void plist_get_real_val(plist_t node, double *val)
327{
328 plist_type type = plist_get_node_type(node);
329 uint64_t length = 0;
330 if (PLIST_REAL == type)
331 plist_get_type_and_value(node, &type, (void *) val, &length);
332 assert(length == sizeof(double));
333}
334
335void plist_get_data_val(plist_t node, char **val, uint64_t * length)
336{
337 plist_type type = plist_get_node_type(node);
338 if (PLIST_UINT == type)
339 plist_get_type_and_value(node, &type, (void *) val, length);
340}
diff --git a/src/plist.h b/src/plist.h
index bd12e3a..449a3f7 100644
--- a/src/plist.h
+++ b/src/plist.h
@@ -56,7 +56,6 @@ plist_t plist_new_node(plist_data_t data);
56plist_data_t plist_get_data(const plist_t node); 56plist_data_t plist_get_data(const plist_t node);
57plist_data_t plist_new_plist_data(); 57plist_data_t plist_new_plist_data();
58void plist_free_plist_data(plist_data_t node); 58void plist_free_plist_data(plist_data_t node);
59plist_type plist_get_node_type(plist_t node);
60uint64_t plist_get_node_uint_val(plist_t node); 59uint64_t plist_get_node_uint_val(plist_t node);
61 60
62 61
diff --git a/src/xplist.c b/src/xplist.c
index 3a019a9..e0d3768 100644
--- a/src/xplist.c
+++ b/src/xplist.c
@@ -256,7 +256,7 @@ static void xml_to_node(xmlNodePtr xml_node, plist_t * plist_node)
256 } 256 }
257 257
258 if (!xmlStrcmp(node->name, XPLIST_INT)) { 258 if (!xmlStrcmp(node->name, XPLIST_INT)) {
259 char *strval = (char*)xmlNodeGetContent(node); 259 char *strval = (char *) xmlNodeGetContent(node);
260 data->intval = g_ascii_strtoull(strval, NULL, 0); 260 data->intval = g_ascii_strtoull(strval, NULL, 0);
261 data->type = PLIST_UINT; 261 data->type = PLIST_UINT;
262 data->length = 8; 262 data->length = 8;
@@ -264,7 +264,7 @@ static void xml_to_node(xmlNodePtr xml_node, plist_t * plist_node)
264 } 264 }
265 265
266 if (!xmlStrcmp(node->name, XPLIST_REAL)) { 266 if (!xmlStrcmp(node->name, XPLIST_REAL)) {
267 char *strval = (char*)xmlNodeGetContent(node); 267 char *strval = (char *) xmlNodeGetContent(node);
268 data->realval = atof(strval); 268 data->realval = atof(strval);
269 data->type = PLIST_REAL; 269 data->type = PLIST_REAL;
270 data->length = 8; 270 data->length = 8;
@@ -272,21 +272,21 @@ static void xml_to_node(xmlNodePtr xml_node, plist_t * plist_node)
272 } 272 }
273 273
274 if (!xmlStrcmp(node->name, XPLIST_DATE)) { 274 if (!xmlStrcmp(node->name, XPLIST_DATE)) {
275 g_time_val_from_iso8601((char*)xmlNodeGetContent(node), &data->timeval); 275 g_time_val_from_iso8601((char *) xmlNodeGetContent(node), &data->timeval);
276 data->type = PLIST_DATE; 276 data->type = PLIST_DATE;
277 data->length = sizeof(GTimeVal); 277 data->length = sizeof(GTimeVal);
278 continue; //TODO : handle date tag 278 continue; //TODO : handle date tag
279 } 279 }
280 280
281 if (!xmlStrcmp(node->name, XPLIST_STRING)) { 281 if (!xmlStrcmp(node->name, XPLIST_STRING)) {
282 data->strval = strdup( (char*) xmlNodeGetContent(node)); 282 data->strval = strdup((char *) xmlNodeGetContent(node));
283 data->type = PLIST_STRING; 283 data->type = PLIST_STRING;
284 data->length = strlen(data->strval); 284 data->length = strlen(data->strval);
285 continue; 285 continue;
286 } 286 }
287 287
288 if (!xmlStrcmp(node->name, XPLIST_KEY)) { 288 if (!xmlStrcmp(node->name, XPLIST_KEY)) {
289 data->strval = strdup( (char*) xmlNodeGetContent(node)); 289 data->strval = strdup((char *) xmlNodeGetContent(node));
290 data->type = PLIST_KEY; 290 data->type = PLIST_KEY;
291 data->length = strlen(data->strval); 291 data->length = strlen(data->strval);
292 continue; 292 continue;
@@ -294,7 +294,7 @@ static void xml_to_node(xmlNodePtr xml_node, plist_t * plist_node)
294 294
295 if (!xmlStrcmp(node->name, XPLIST_DATA)) { 295 if (!xmlStrcmp(node->name, XPLIST_DATA)) {
296 gsize size = 0; 296 gsize size = 0;
297 data->buff = g_base64_decode((char*)xmlNodeGetContent(node), &size); 297 data->buff = g_base64_decode((char *) xmlNodeGetContent(node), &size);
298 data->length = size; 298 data->length = size;
299 data->type = PLIST_DATA; 299 data->type = PLIST_DATA;
300 continue; 300 continue;
@@ -326,7 +326,7 @@ void plist_to_xml(plist_t plist, char **plist_xml, uint32_t * length)
326 326
327 int size = 0; 327 int size = 0;
328 xmlDocDumpMemory(plist_doc, (xmlChar **) plist_xml, &size); 328 xmlDocDumpMemory(plist_doc, (xmlChar **) plist_xml, &size);
329 if (size >=0 ) 329 if (size >= 0)
330 *length = size; 330 *length = size;
331 free_plist(plist_doc); 331 free_plist(plist_doc);
332} 332}