summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--include/plist/plist.h80
-rw-r--r--src/bplist.c23
-rw-r--r--src/plist.c119
-rw-r--r--src/xplist.c10
4 files changed, 211 insertions, 21 deletions
diff --git a/include/plist/plist.h b/include/plist/plist.h
index c289158..a075fc6 100644
--- a/include/plist/plist.h
+++ b/include/plist/plist.h
@@ -109,6 +109,15 @@ extern "C" {
109 */ 109 */
110 PLIST_API void plist_free(plist_t plist); 110 PLIST_API void plist_free(plist_t plist);
111 111
112/**
113 * Return a copy of passed node and it's children
114 *
115 * @param plist the plist to copy
116 * @return copied plist
117 */
118 PLIST_API plist_t plist_copy(plist_t node);
119
120
112/******************************************** 121/********************************************
113 * * 122 * *
114 * Tree navigation * 123 * Tree navigation *
@@ -321,6 +330,77 @@ extern "C" {
321 PLIST_API void plist_get_date_val(plist_t node, int32_t * sec, int32_t * usec); 330 PLIST_API void plist_get_date_val(plist_t node, int32_t * sec, int32_t * usec);
322 331
323 332
333/********************************************
334 * *
335 * Setters *
336 * *
337 ********************************************/
338
339/**
340 * Set the value of a node.
341 * Forces type of node to #PLIST_KEY
342 *
343 * @param node the node
344 * @param val the key value
345 */
346 PLIST_API void plist_set_key_val(plist_t node, const char *val);
347
348/**
349 * Set the value of a node.
350 * Forces type of node to #PLIST_STRING
351 *
352 * @param node the node
353 * @param val the string value
354 */
355 PLIST_API void plist_set_string_val(plist_t node, const char *val);
356
357/**
358 * Set the value of a node.
359 * Forces type of node to #PLIST_BOOLEAN
360 *
361 * @param node the node
362 * @param val the boolean value
363 */
364 PLIST_API void plist_set_bool_val(plist_t node, uint8_t val);
365
366/**
367 * Set the value of a node.
368 * Forces type of node to #PLIST_UINT
369 *
370 * @param node the node
371 * @param val the unsigned integer value
372 */
373 PLIST_API void plist_set_uint_val(plist_t node, uint64_t val);
374
375/**
376 * Set the value of a node.
377 * Forces type of node to #PLIST_REAL
378 *
379 * @param node the node
380 * @param val the real value
381 */
382 PLIST_API void plist_set_real_val(plist_t node, double val);
383
384/**
385 * Set the value of a node.
386 * Forces type of node to #PLIST_DATA
387 *
388 * @param node the node
389 * @param val the binary buffer
390 * @param length the length of the buffer
391 */
392 PLIST_API void plist_set_data_val(plist_t node, const char *val, uint64_t length);
393
394/**
395 * Set the value of a node.
396 * Forces type of node to #PLIST_DATE
397 *
398 * @param node the node
399 * @param sec the number of seconds since 01/01/2001
400 * @param usec the number of microseconds
401 */
402 PLIST_API void plist_set_date_val(plist_t node, int32_t sec, int32_t usec);
403
324 404
325/******************************************** 405/********************************************
326 * * 406 * *
diff --git a/src/bplist.c b/src/bplist.c
index 6b2d2f3..f993d9e 100644
--- a/src/bplist.c
+++ b/src/bplist.c
@@ -80,7 +80,7 @@ static void byte_convert(uint8_t * address, size_t size)
80static uint32_t uint24_from_be(char *buff) 80static uint32_t uint24_from_be(char *buff)
81{ 81{
82 uint32_t ret = 0; 82 uint32_t ret = 0;
83 char *tmp = (char*) &ret; 83 char *tmp = (char *) &ret;
84 memcpy(tmp + 1, buff, 3 * sizeof(char)); 84 memcpy(tmp + 1, buff, 3 * sizeof(char));
85 byte_convert(tmp, sizeof(uint32_t)); 85 byte_convert(tmp, sizeof(uint32_t));
86 return ret; 86 return ret;
@@ -137,13 +137,13 @@ static plist_t parse_real_node(char *bnode, uint8_t size)
137 size = 1 << size; // make length less misleading 137 size = 1 << size; // make length less misleading
138 switch (size) { 138 switch (size) {
139 case sizeof(float): 139 case sizeof(float):
140 floatval = *(float*)bnode; 140 floatval = *(float *) bnode;
141 byte_convert((uint8_t*)&floatval, sizeof(float)); 141 byte_convert((uint8_t *) & floatval, sizeof(float));
142 data->realval = floatval; 142 data->realval = floatval;
143 break; 143 break;
144 case sizeof(double): 144 case sizeof(double):
145 data->realval = *(double*)bnode; 145 data->realval = *(double *) bnode;
146 byte_convert((uint8_t*)&(data->realval), sizeof(double)); 146 byte_convert((uint8_t *) & (data->realval), sizeof(double));
147 break; 147 break;
148 default: 148 default:
149 free(data); 149 free(data);
@@ -197,7 +197,7 @@ static plist_t parse_unicode_node(char *bnode, uint64_t size)
197 unicodestr = (gunichar2 *) malloc(sizeof(gunichar2) * size); 197 unicodestr = (gunichar2 *) malloc(sizeof(gunichar2) * size);
198 memcpy(unicodestr, bnode, sizeof(gunichar2) * size); 198 memcpy(unicodestr, bnode, sizeof(gunichar2) * size);
199 for (i = 0; i < size; i++) 199 for (i = 0; i < size; i++)
200 byte_convert((uint8_t*)(unicodestr + i), sizeof(gunichar2)); 200 byte_convert((uint8_t *) (unicodestr + i), sizeof(gunichar2));
201 201
202 tmpstr = g_utf16_to_utf8(unicodestr, size, &items_read, &items_written, &error); 202 tmpstr = g_utf16_to_utf8(unicodestr, size, &items_read, &items_written, &error);
203 free(unicodestr); 203 free(unicodestr);
@@ -426,7 +426,7 @@ void plist_from_bin(const char *plist_bin, uint32_t length, plist_t * plist)
426 return; 426 return;
427 427
428 //now parse trailer 428 //now parse trailer
429 trailer = (char*)(plist_bin + (length - BPLIST_TRL_SIZE)); 429 trailer = (char *) (plist_bin + (length - BPLIST_TRL_SIZE));
430 430
431 offset_size = trailer[BPLIST_TRL_OFFSIZE_IDX]; 431 offset_size = trailer[BPLIST_TRL_OFFSIZE_IDX];
432 dict_param_size = trailer[BPLIST_TRL_PARMSIZE_IDX]; 432 dict_param_size = trailer[BPLIST_TRL_PARMSIZE_IDX];
@@ -444,12 +444,12 @@ void plist_from_bin(const char *plist_bin, uint32_t length, plist_t * plist)
444 return; 444 return;
445 445
446 //parse serialized nodes 446 //parse serialized nodes
447 offset_table = (char*)(plist_bin + offset_table_index); 447 offset_table = (char *) (plist_bin + offset_table_index);
448 for (i = 0; i < num_objects; i++) { 448 for (i = 0; i < num_objects; i++) {
449 char *obj = NULL; 449 char *obj = NULL;
450 current_offset = UINT_TO_HOST(offset_table + i * offset_size, offset_size); 450 current_offset = UINT_TO_HOST(offset_table + i * offset_size, offset_size);
451 451
452 obj = (char*)(plist_bin + current_offset); 452 obj = (char *) (plist_bin + current_offset);
453 nodeslist[i] = parse_bin_node(obj, dict_param_size, &obj); 453 nodeslist[i] = parse_bin_node(obj, dict_param_size, &obj);
454 } 454 }
455 455
@@ -734,7 +734,7 @@ static void write_dict(GByteArray * bplist, GNode * node, GHashTable * ref_table
734 memcpy(buff + i * dict_param_size, &idx1, dict_param_size); 734 memcpy(buff + i * dict_param_size, &idx1, dict_param_size);
735 byte_convert(buff + i * dict_param_size, dict_param_size); 735 byte_convert(buff + i * dict_param_size, dict_param_size);
736 736
737 idx2 = *(uint64_t *)(g_hash_table_lookup(ref_table, cur->next)); 737 idx2 = *(uint64_t *) (g_hash_table_lookup(ref_table, cur->next));
738 memcpy(buff + (i + size) * dict_param_size, &idx2, dict_param_size); 738 memcpy(buff + (i + size) * dict_param_size, &idx2, dict_param_size);
739 byte_convert(buff + (i + size) * dict_param_size, dict_param_size); 739 byte_convert(buff + (i + size) * dict_param_size, dict_param_size);
740 } 740 }
@@ -828,8 +828,7 @@ void plist_to_bin(plist_t plist, char **plist_bin, uint32_t * length)
828 unicodestr = g_utf8_to_utf16(data->strval, len, &items_read, &items_written, &error); 828 unicodestr = g_utf8_to_utf16(data->strval, len, &items_read, &items_written, &error);
829 write_unicode(bplist_buff, unicodestr, items_written); 829 write_unicode(bplist_buff, unicodestr, items_written);
830 g_free(unicodestr); 830 g_free(unicodestr);
831 } 831 } else if (XML_CHAR_ENCODING_ASCII == type || XML_CHAR_ENCODING_NONE == type) {
832 else if (XML_CHAR_ENCODING_ASCII == type || XML_CHAR_ENCODING_NONE == type) {
833 write_string(bplist_buff, data->strval); 832 write_string(bplist_buff, data->strval);
834 } 833 }
835 break; 834 break;
diff --git a/src/plist.c b/src/plist.c
index 7949bce..e43d50d 100644
--- a/src/plist.c
+++ b/src/plist.c
@@ -109,7 +109,7 @@ static plist_t plist_add_sub_element(plist_t node, plist_type type, const void *
109 data->strval = strdup((char *) value); 109 data->strval = strdup((char *) value);
110 break; 110 break;
111 case PLIST_DATA: 111 case PLIST_DATA:
112 data->buff = (uint8_t*)malloc(length); 112 data->buff = (uint8_t *) malloc(length);
113 memcpy(data->buff, value, length); 113 memcpy(data->buff, value, length);
114 break; 114 break;
115 case PLIST_DATE: 115 case PLIST_DATE:
@@ -138,6 +138,28 @@ void plist_free(plist_t plist)
138 g_node_destroy(plist); 138 g_node_destroy(plist);
139} 139}
140 140
141static void plist_copy_node(GNode * node, gpointer parent_node)
142{
143 plist_t newnode = NULL;
144 plist_data_t data = plist_get_data(node);
145 plist_data_t newdata = plist_new_plist_data();
146
147 assert(data); // plist should always have data
148
149 memcpy(newdata, data, sizeof(struct plist_data_s));
150 newnode = plist_new_node(newdata);
151
152 if (parent_node) {
153 g_node_append(parent_node, newnode);
154 }
155 g_node_children_foreach(node, G_TRAVERSE_ALL, plist_copy_node, newnode);
156}
157
158plist_t plist_copy(plist_t node)
159{
160 plist_copy_node(node, NULL);
161}
162
141plist_t plist_get_first_child(plist_t node) 163plist_t plist_get_first_child(plist_t node)
142{ 164{
143 return (plist_t) g_node_first_child((GNode *) node); 165 return (plist_t) g_node_first_child((GNode *) node);
@@ -160,7 +182,7 @@ plist_t plist_get_array_nth_el(plist_t node, uint32_t n)
160 uint32_t i = 0; 182 uint32_t i = 0;
161 plist_t temp = plist_get_first_child(node); 183 plist_t temp = plist_get_first_child(node);
162 184
163 while ( i <= n && temp) { 185 while (i <= n && temp) {
164 if (i == n) 186 if (i == n)
165 ret = temp; 187 ret = temp;
166 temp = plist_get_next_sibling(temp); 188 temp = plist_get_next_sibling(temp);
@@ -465,5 +487,96 @@ gboolean plist_data_compare(gconstpointer a, gconstpointer b)
465 487
466char plist_compare_node_value(plist_t node_l, plist_t node_r) 488char plist_compare_node_value(plist_t node_l, plist_t node_r)
467{ 489{
468 return plist_data_compare( node_l , node_r ); 490 return plist_data_compare(node_l, node_r);
491}
492
493static plist_t plist_set_element_val(plist_t node, plist_type type, const void *value, uint64_t length)
494{
495 //free previous allocated buffer
496 plist_data_t data = plist_get_data(node);
497 assert(data); // a node should always have data attached
498
499 switch (data->type) {
500 case PLIST_KEY:
501 case PLIST_STRING:
502 free(data->strval);
503 data->strval = NULL;
504 break;
505 case PLIST_DATA:
506 free(data->buff);
507 data->buff = NULL;
508 break;
509 default:
510 break;
511 }
512
513 //now handle value
514
515 data->type = type;
516 data->length = length;
517
518 switch (type) {
519 case PLIST_BOOLEAN:
520 data->boolval = *((char *) value);
521 break;
522 case PLIST_UINT:
523 data->intval = *((uint64_t *) value);
524 break;
525 case PLIST_REAL:
526 data->realval = *((double *) value);
527 break;
528 case PLIST_KEY:
529 case PLIST_STRING:
530 data->strval = strdup((char *) value);
531 break;
532 case PLIST_DATA:
533 data->buff = (uint8_t *) malloc(length);
534 memcpy(data->buff, value, length);
535 break;
536 case PLIST_DATE:
537 data->timeval.tv_sec = ((GTimeVal *) value)->tv_sec;
538 data->timeval.tv_usec = ((GTimeVal *) value)->tv_usec;
539 break;
540 case PLIST_ARRAY:
541 case PLIST_DICT:
542 default:
543 break;
544 }
545}
546
547
548void plist_set_key_val(plist_t node, const char *val)
549{
550 plist_set_element_val(node, PLIST_KEY, val, strlen(val));
551}
552
553void plist_set_string_val(plist_t node, const char *val)
554{
555 plist_set_element_val(node, PLIST_STRING, val, strlen(val));
556}
557
558void plist_set_bool_val(plist_t node, uint8_t val)
559{
560 plist_set_element_val(node, PLIST_BOOLEAN, &val, sizeof(uint8_t));
561}
562
563void plist_set_uint_val(plist_t node, uint64_t val)
564{
565 plist_set_element_val(node, PLIST_UINT, &val, sizeof(uint64_t));
566}
567
568void plist_set_real_val(plist_t node, double val)
569{
570 plist_set_element_val(node, PLIST_REAL, &val, sizeof(double));
571}
572
573void plist_set_data_val(plist_t node, const char *val, uint64_t length)
574{
575 plist_set_element_val(node, PLIST_DATA, val, length);
576}
577
578void plist_set_date_val(plist_t node, int32_t sec, int32_t usec)
579{
580 GTimeVal val = { sec, usec };
581 plist_set_element_val(node, PLIST_DATE, &val, sizeof(GTimeVal));
469} 582}
diff --git a/src/xplist.c b/src/xplist.c
index 38cc4fe..490367e 100644
--- a/src/xplist.c
+++ b/src/xplist.c
@@ -257,7 +257,7 @@ static void xml_to_node(xmlNodePtr xml_node, plist_t * plist_node)
257 257
258 if (!xmlStrcmp(node->name, XPLIST_INT)) { 258 if (!xmlStrcmp(node->name, XPLIST_INT)) {
259 xmlChar *strval = xmlNodeGetContent(node); 259 xmlChar *strval = xmlNodeGetContent(node);
260 data->intval = g_ascii_strtoull((char *)strval, NULL, 0); 260 data->intval = g_ascii_strtoull((char *) strval, NULL, 0);
261 data->type = PLIST_UINT; 261 data->type = PLIST_UINT;
262 data->length = 8; 262 data->length = 8;
263 xmlFree(strval); 263 xmlFree(strval);
@@ -266,7 +266,7 @@ static void xml_to_node(xmlNodePtr xml_node, plist_t * plist_node)
266 266
267 if (!xmlStrcmp(node->name, XPLIST_REAL)) { 267 if (!xmlStrcmp(node->name, XPLIST_REAL)) {
268 xmlChar *strval = xmlNodeGetContent(node); 268 xmlChar *strval = xmlNodeGetContent(node);
269 data->realval = atof((char *)strval); 269 data->realval = atof((char *) strval);
270 data->type = PLIST_REAL; 270 data->type = PLIST_REAL;
271 data->length = 8; 271 data->length = 8;
272 xmlFree(strval); 272 xmlFree(strval);
@@ -287,9 +287,7 @@ static void xml_to_node(xmlNodePtr xml_node, plist_t * plist_node)
287 len = strlen((char *) strval); 287 len = strlen((char *) strval);
288 type = xmlDetectCharEncoding(strval, len); 288 type = xmlDetectCharEncoding(strval, len);
289 289
290 if (XML_CHAR_ENCODING_UTF8 == type || 290 if (XML_CHAR_ENCODING_UTF8 == type || XML_CHAR_ENCODING_ASCII == type || XML_CHAR_ENCODING_NONE == type) {
291 XML_CHAR_ENCODING_ASCII == type ||
292 XML_CHAR_ENCODING_NONE == type) {
293 data->strval = strdup((char *) strval); 291 data->strval = strdup((char *) strval);
294 data->type = PLIST_STRING; 292 data->type = PLIST_STRING;
295 data->length = strlen(data->strval); 293 data->length = strlen(data->strval);
@@ -311,7 +309,7 @@ static void xml_to_node(xmlNodePtr xml_node, plist_t * plist_node)
311 xmlChar *strval = xmlNodeGetContent(node); 309 xmlChar *strval = xmlNodeGetContent(node);
312 gsize size = 0; 310 gsize size = 0;
313 guchar *dec = g_base64_decode((char *) strval, &size); 311 guchar *dec = g_base64_decode((char *) strval, &size);
314 data->buff = (uint8_t*) malloc( size * sizeof(uint8_t)); 312 data->buff = (uint8_t *) malloc(size * sizeof(uint8_t));
315 memcpy(data->buff, dec, size * sizeof(uint8_t)); 313 memcpy(data->buff, dec, size * sizeof(uint8_t));
316 g_free(dec); 314 g_free(dec);
317 data->length = size; 315 data->length = size;