summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Jonathan Beck2009-01-04 14:56:30 +0100
committerGravatar Jonathan Beck2009-01-04 14:56:30 +0100
commit66df32eec203568c3a17eb0d066d3ac81be267af (patch)
tree81c9706f41e47f6476d09d0ec67dd9297a6229bb
parentca40090460a6e43112c1f9e9d414727c29a9847d (diff)
downloadlibplist-66df32eec203568c3a17eb0d066d3ac81be267af.tar.gz
libplist-66df32eec203568c3a17eb0d066d3ac81be267af.tar.bz2
fix some warnings and correct binary tag enum (false and true were inverted).
-rw-r--r--src/bplist.c73
-rw-r--r--src/plist.h2
-rw-r--r--src/xplist.c18
3 files changed, 46 insertions, 47 deletions
diff --git a/src/bplist.c b/src/bplist.c
index 94d7458..187d516 100644
--- a/src/bplist.c
+++ b/src/bplist.c
@@ -43,8 +43,8 @@
43 43
44enum { 44enum {
45 BPLIST_NULL = 0x00, 45 BPLIST_NULL = 0x00,
46 BPLIST_TRUE = 0x08, 46 BPLIST_FALSE = 0x08,
47 BPLIST_FALSE = 0x09, 47 BPLIST_TRUE = 0x09,
48 BPLIST_FILL = 0x0F, /* will be used for length grabbing */ 48 BPLIST_FILL = 0x0F, /* will be used for length grabbing */
49 BPLIST_UINT = 0x10, 49 BPLIST_UINT = 0x10,
50 BPLIST_REAL = 0x20, 50 BPLIST_REAL = 0x20,
@@ -61,6 +61,7 @@ enum {
61 61
62static void byte_convert(char *address, size_t size) 62static void byte_convert(char *address, size_t size)
63{ 63{
64#if G_BYTE_ORDER == G_LITTLE_ENDIAN
64 uint8_t i = 0, j = 0; 65 uint8_t i = 0, j = 0;
65 char tmp = '\0'; 66 char tmp = '\0';
66 67
@@ -70,19 +71,23 @@ static void byte_convert(char *address, size_t size)
70 address[i] = address[j]; 71 address[i] = address[j];
71 address[j] = tmp; 72 address[j] = tmp;
72 } 73 }
74#endif
73} 75}
74 76
75#include <byteswap.h>
76#define swap_n_bytes(x, n) \ 77#define swap_n_bytes(x, n) \
77 n == 8 ? bswap_64(*(uint64_t *)(x)) : \ 78 n == 8 ? GUINT64_FROM_BE( *(uint64_t *)(x) ) : \
78 (n == 4 ? bswap_32(*(uint32_t *)(x)) : \ 79 (n == 4 ? GUINT32_FROM_BE( *(uint32_t *)(x) ) : \
79 (n == 2 ? bswap_16(*(uint16_t *)(x)) : *(x) )) 80 (n == 2 ? GUINT16_FROM_BE( *(uint16_t *)(x) ) : *(uint8_t *)(x) ))
80 81
81#define be64dec(x) bswap_64( *(uint64_t*)(x) ) 82#define be64dec(x) GUINT64_FROM_BE( *(uint64_t*)(x) )
82 83
83#define get_needed_bytes(x) (x <= 1<<8 ? 1 : ( x <= 1<<16 ? 2 : ( x <= (uint64_t)1<<32 ? 4 : 8))) 84#define get_needed_bytes(x) (x <= 1<<8 ? 1 : ( x <= 1<<16 ? 2 : ( x <= (uint64_t)1<<32 ? 4 : 8)))
84#define get_real_bytes(x) (x >> 32 ? 4 : 8) 85#define get_real_bytes(x) (x >> 32 ? 4 : 8)
85 86
87
88
89
90
86static plist_t parse_uint_node(char *bnode, uint8_t size, char **next_object) 91static plist_t parse_uint_node(char *bnode, uint8_t size, char **next_object)
87{ 92{
88 plist_data_t data = plist_new_plist_data(); 93 plist_data_t data = plist_new_plist_data();
@@ -90,19 +95,11 @@ static plist_t parse_uint_node(char *bnode, uint8_t size, char **next_object)
90 size = 1 << size; // make length less misleading 95 size = 1 << size; // make length less misleading
91 switch (size) { 96 switch (size) {
92 case sizeof(uint8_t): 97 case sizeof(uint8_t):
93 data->intval = bnode[0];
94 break;
95 case sizeof(uint16_t): 98 case sizeof(uint16_t):
96 memcpy(&data->intval, bnode, size);
97 data->intval = ntohs(data->intval);
98 break;
99 case sizeof(uint32_t): 99 case sizeof(uint32_t):
100 memcpy(&data->intval, bnode, size);
101 data->intval = ntohl(data->intval);
102 break;
103 case sizeof(uint64_t): 100 case sizeof(uint64_t):
104 memcpy(&data->intval, bnode, size); 101 memcpy(&data->intval, bnode, size);
105 byte_convert((char *) &data->intval, size); 102 data->intval = swap_n_bytes(&data->intval, size);
106 break; 103 break;
107 default: 104 default:
108 free(data); 105 free(data);
@@ -160,7 +157,7 @@ static plist_t parse_unicode_node(char *bnode, uint8_t size)
160 return g_node_new(data); 157 return g_node_new(data);
161} 158}
162 159
163static plist_t parse_data_node(char *bnode, uint64_t size, uint32_t ref_size) 160static plist_t parse_data_node(char *bnode, uint64_t size)
164{ 161{
165 plist_data_t data = plist_new_plist_data(); 162 plist_data_t data = plist_new_plist_data();
166 163
@@ -252,7 +249,7 @@ static plist_t parse_bin_node(char *object, uint8_t dict_size, char **next_objec
252 return NULL; 249 return NULL;
253 size = plist_get_node_uint_val(size_node); 250 size = plist_get_node_uint_val(size_node);
254 } 251 }
255 return parse_data_node(object, size, dict_size); 252 return parse_data_node(object, size);
256 253
257 case BPLIST_STRING: 254 case BPLIST_STRING:
258 if (0x0F == size) { 255 if (0x0F == size) {
@@ -292,7 +289,8 @@ static plist_t parse_bin_node(char *object, uint8_t dict_size, char **next_objec
292 size = plist_get_node_uint_val(size_node); 289 size = plist_get_node_uint_val(size_node);
293 } 290 }
294 return parse_dict_node(object, size, dict_size); 291 return parse_dict_node(object, size, dict_size);
295 292 default:
293 return NULL;
296 } 294 }
297 return NULL; 295 return NULL;
298} 296}
@@ -324,11 +322,13 @@ static gpointer copy_plist_data(gconstpointer src, gpointer data)
324 break; 322 break;
325 case PLIST_DATA: 323 case PLIST_DATA:
326 case PLIST_ARRAY: 324 case PLIST_ARRAY:
327 case PLIST_DICT:
328 dstdata->buff = (char *) malloc(sizeof(char *) * srcdata->length); 325 dstdata->buff = (char *) malloc(sizeof(char *) * srcdata->length);
329 memcpy(dstdata->buff, srcdata->buff, sizeof(char *) * srcdata->length); 326 memcpy(dstdata->buff, srcdata->buff, sizeof(char *) * srcdata->length);
330 break; 327 break;
331 328 case PLIST_DICT:
329 dstdata->buff = (char *) malloc(sizeof(char *) * srcdata->length * 2);
330 memcpy(dstdata->buff, srcdata->buff, sizeof(char *) * srcdata->length * 2);
331 break;
332 default: 332 default:
333 break; 333 break;
334 } 334 }
@@ -408,14 +408,14 @@ void plist_from_bin(const char *plist_bin, uint32_t length, plist_t * plist)
408 //first one is actually a key 408 //first one is actually a key
409 plist_get_data(nodeslist[index1])->type = PLIST_KEY; 409 plist_get_data(nodeslist[index1])->type = PLIST_KEY;
410 410
411 if (index1 >= 0 && index1 < num_objects) { 411 if (index1 < num_objects) {
412 if (G_NODE_IS_ROOT(nodeslist[index1])) 412 if (G_NODE_IS_ROOT(nodeslist[index1]))
413 g_node_append(nodeslist[i], nodeslist[index1]); 413 g_node_append(nodeslist[i], nodeslist[index1]);
414 else 414 else
415 g_node_append(nodeslist[i], g_node_copy_deep(nodeslist[index1], copy_plist_data, NULL)); 415 g_node_append(nodeslist[i], g_node_copy_deep(nodeslist[index1], copy_plist_data, NULL));
416 } 416 }
417 417
418 if (index2 >= 0 && index2 < num_objects) { 418 if (index2 < num_objects) {
419 if (G_NODE_IS_ROOT(nodeslist[index2])) 419 if (G_NODE_IS_ROOT(nodeslist[index2]))
420 g_node_append(nodeslist[i], nodeslist[index2]); 420 g_node_append(nodeslist[i], nodeslist[index2]);
421 else 421 else
@@ -432,7 +432,7 @@ void plist_from_bin(const char *plist_bin, uint32_t length, plist_t * plist)
432 str_j = j * dict_param_size; 432 str_j = j * dict_param_size;
433 index1 = swap_n_bytes(data->buff + str_j, dict_param_size); 433 index1 = swap_n_bytes(data->buff + str_j, dict_param_size);
434 434
435 if (index1 >= 0 && index1 < num_objects) { 435 if (index1 < num_objects) {
436 if (G_NODE_IS_ROOT(nodeslist[index1])) 436 if (G_NODE_IS_ROOT(nodeslist[index1]))
437 g_node_append(nodeslist[i], nodeslist[index1]); 437 g_node_append(nodeslist[i], nodeslist[index1]);
438 else 438 else
@@ -574,7 +574,7 @@ static void serialize_plist(GNode * node, gpointer data)
574 574
575#define Log2(x) (x == 8 ? 3 : (x == 4 ? 2 : (x == 2 ? 1 : 0))) 575#define Log2(x) (x == 8 ? 3 : (x == 4 ? 2 : (x == 2 ? 1 : 0)))
576 576
577void write_int(GByteArray * bplist, uint64_t val) 577static void write_int(GByteArray * bplist, uint64_t val)
578{ 578{
579 uint64_t size = get_needed_bytes(val); 579 uint64_t size = get_needed_bytes(val);
580 uint8_t *buff = (uint8_t *) malloc(sizeof(uint8_t) + size); 580 uint8_t *buff = (uint8_t *) malloc(sizeof(uint8_t) + size);
@@ -639,7 +639,7 @@ static void write_array(GByteArray * bplist, GNode * node, GHashTable * ref_tabl
639 uint8_t *buff = (uint8_t *) malloc(size * dict_param_size); 639 uint8_t *buff = (uint8_t *) malloc(size * dict_param_size);
640 640
641 GNode *cur = NULL; 641 GNode *cur = NULL;
642 int i = 0; 642 uint64_t i = 0;
643 for (i = 0, cur = node->children; cur && i < size; cur = cur->next, i++) { 643 for (i = 0, cur = node->children; cur && i < size; cur = cur->next, i++) {
644 idx = GPOINTER_TO_UINT(g_hash_table_lookup(ref_table, cur)); 644 idx = GPOINTER_TO_UINT(g_hash_table_lookup(ref_table, cur));
645 memcpy(buff + i * dict_param_size, &idx, dict_param_size); 645 memcpy(buff + i * dict_param_size, &idx, dict_param_size);
@@ -669,7 +669,7 @@ static void write_dict(GByteArray * bplist, GNode * node, GHashTable * ref_table
669 uint8_t *buff = (uint8_t *) malloc(size * 2 * dict_param_size); 669 uint8_t *buff = (uint8_t *) malloc(size * 2 * dict_param_size);
670 670
671 GNode *cur = NULL; 671 GNode *cur = NULL;
672 int i = 0; 672 uint64_t i = 0;
673 for (i = 0, cur = node->children; cur && i < size; cur = cur->next->next, i++) { 673 for (i = 0, cur = node->children; cur && i < size; cur = cur->next->next, i++) {
674 idx1 = GPOINTER_TO_UINT(g_hash_table_lookup(ref_table, cur)); 674 idx1 = GPOINTER_TO_UINT(g_hash_table_lookup(ref_table, cur));
675 memcpy(buff + i * dict_param_size, &idx1, dict_param_size); 675 memcpy(buff + i * dict_param_size, &idx1, dict_param_size);
@@ -716,9 +716,8 @@ void plist_to_bin(plist_t plist, char **plist_bin, uint32_t * length)
716 g_byte_array_append(bplist_buff, BPLIST_VERSION, BPLIST_VERSION_SIZE); 716 g_byte_array_append(bplist_buff, BPLIST_VERSION, BPLIST_VERSION_SIZE);
717 717
718 //write objects and table 718 //write objects and table
719 int i = 0; 719 uint64_t i = 0;
720 uint8_t *buff = NULL; 720 uint8_t *buff = NULL;
721 uint8_t size = 0;
722 uint64_t offsets[num_objects]; 721 uint64_t offsets[num_objects];
723 for (i = 0; i < num_objects; i++) { 722 for (i = 0; i < num_objects; i++) {
724 723
@@ -768,17 +767,17 @@ void plist_to_bin(plist_t plist, char **plist_bin, uint32_t * length)
768 offset_size = get_needed_bytes(bplist_buff->len); 767 offset_size = get_needed_bytes(bplist_buff->len);
769 offset_table_index = bplist_buff->len; 768 offset_table_index = bplist_buff->len;
770 for (i = 0; i <= num_objects; i++) { 769 for (i = 0; i <= num_objects; i++) {
771 uint8_t *buff = (uint8_t *) malloc(offset_size); 770 uint8_t *offsetbuff = (uint8_t *) malloc(offset_size);
772 memcpy(buff, offsets + i, offset_size); 771 memcpy(offsetbuff, offsets + i, offset_size);
773 byte_convert(buff, offset_size); 772 byte_convert(offsetbuff, offset_size);
774 g_byte_array_append(bplist_buff, buff, offset_size); 773 g_byte_array_append(bplist_buff, offsetbuff, offset_size);
775 free(buff); 774 free(offsetbuff);
776 } 775 }
777 776
778 //setup trailer 777 //setup trailer
779 num_objects = bswap_64(num_objects); 778 num_objects = GUINT64_FROM_BE(num_objects);
780 root_object = bswap_64(root_object); 779 root_object = GUINT64_FROM_BE(root_object);
781 offset_table_index = bswap_64(offset_table_index); 780 offset_table_index = GUINT64_FROM_BE(offset_table_index);
782 781
783 char trailer[BPLIST_TRL_SIZE]; 782 char trailer[BPLIST_TRL_SIZE];
784 memcpy(trailer + BPLIST_TRL_OFFSIZE_IDX, &offset_size, sizeof(uint8_t)); 783 memcpy(trailer + BPLIST_TRL_OFFSIZE_IDX, &offset_size, sizeof(uint8_t));
diff --git a/src/plist.h b/src/plist.h
index 3f4036f..a637118 100644
--- a/src/plist.h
+++ b/src/plist.h
@@ -31,7 +31,7 @@
31#include <sys/stat.h> 31#include <sys/stat.h>
32#include <unistd.h> 32#include <unistd.h>
33#include <glib.h> 33#include <glib.h>
34 34#include "utils.h"
35 35
36 36
37 37
diff --git a/src/xplist.c b/src/xplist.c
index 9a5698c..abc448d 100644
--- a/src/xplist.c
+++ b/src/xplist.c
@@ -49,7 +49,7 @@ const char *plist_base = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
49 * 49 *
50 * @return The formatted string. 50 * @return The formatted string.
51 */ 51 */
52char *format_string(const char *buf, int cols, int depth) 52static char *format_string(const char *buf, int cols, int depth)
53{ 53{
54 int colw = depth + cols + 1; 54 int colw = depth + cols + 1;
55 int len = strlen(buf); 55 int len = strlen(buf);
@@ -89,7 +89,7 @@ struct xml_node {
89 * 89 *
90 * @return The plist XML document. 90 * @return The plist XML document.
91 */ 91 */
92xmlDocPtr new_xml_plist() 92static xmlDocPtr new_xml_plist()
93{ 93{
94 char *plist = strdup(plist_base); 94 char *plist = strdup(plist_base);
95 xmlDocPtr plist_xml = xmlReadMemory(plist, strlen(plist), NULL, NULL, 0); 95 xmlDocPtr plist_xml = xmlReadMemory(plist, strlen(plist), NULL, NULL, 0);
@@ -106,7 +106,7 @@ xmlDocPtr new_xml_plist()
106 * 106 *
107 * @param plist The XML document to destroy. 107 * @param plist The XML document to destroy.
108 */ 108 */
109void free_plist(xmlDocPtr plist) 109static void free_plist(xmlDocPtr plist)
110{ 110{
111 if (!plist) 111 if (!plist)
112 return; 112 return;
@@ -114,7 +114,7 @@ void free_plist(xmlDocPtr plist)
114 xmlFreeDoc(plist); 114 xmlFreeDoc(plist);
115} 115}
116 116
117void node_to_xml(GNode * node, gpointer xml_struct) 117static void node_to_xml(GNode * node, gpointer xml_struct)
118{ 118{
119 if (!node) 119 if (!node)
120 return; 120 return;
@@ -125,8 +125,8 @@ void node_to_xml(GNode * node, gpointer xml_struct)
125 xmlNodePtr child_node = NULL; 125 xmlNodePtr child_node = NULL;
126 char isStruct = FALSE; 126 char isStruct = FALSE;
127 127
128 gchar *tag = NULL; 128 const gchar *tag = NULL;
129 gchar *val = NULL; 129 const gchar *val = NULL;
130 130
131 switch (node_data->type) { 131 switch (node_data->type) {
132 case PLIST_BOOLEAN: 132 case PLIST_BOOLEAN:
@@ -166,7 +166,7 @@ void node_to_xml(GNode * node, gpointer xml_struct)
166 case PLIST_DATA: 166 case PLIST_DATA:
167 tag = "data"; 167 tag = "data";
168 gchar *valtmp = g_base64_encode(node_data->buff, node_data->length); 168 gchar *valtmp = g_base64_encode(node_data->buff, node_data->length);
169 val = format_string(valtmp, 60, xstruct->depth); 169 val = format_string(valtmp, 68, xstruct->depth);
170 g_free(valtmp); 170 g_free(valtmp);
171 break; 171 break;
172 case PLIST_ARRAY: 172 case PLIST_ARRAY:
@@ -191,7 +191,7 @@ void node_to_xml(GNode * node, gpointer xml_struct)
191 g_free(val); 191 g_free(val);
192 192
193 //add return for structured types 193 //add return for structured types
194 if (node_data->type == PLIST_ARRAY || node_data->type == PLIST_DICT || node_data->type == PLIST_DATA) 194 if (node_data->type == PLIST_ARRAY || node_data->type == PLIST_DICT)
195 xmlNodeAddContent(child_node, "\n"); 195 xmlNodeAddContent(child_node, "\n");
196 196
197 if (isStruct) { 197 if (isStruct) {
@@ -199,7 +199,7 @@ void node_to_xml(GNode * node, gpointer xml_struct)
199 g_node_children_foreach(node, G_TRAVERSE_ALL, node_to_xml, &child); 199 g_node_children_foreach(node, G_TRAVERSE_ALL, node_to_xml, &child);
200 } 200 }
201 //fix indent for structured types 201 //fix indent for structured types
202 if (node_data->type == PLIST_ARRAY || node_data->type == PLIST_DICT || node_data->type == PLIST_DATA) { 202 if (node_data->type == PLIST_ARRAY || node_data->type == PLIST_DICT) {
203 203
204 for (i = 0; i < xstruct->depth; i++) { 204 for (i = 0; i < xstruct->depth; i++) {
205 xmlNodeAddContent(child_node, "\t"); 205 xmlNodeAddContent(child_node, "\t");