summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--include/plist/plist.h8
-rw-r--r--src/bplist.c80
-rw-r--r--src/plist.c67
-rw-r--r--src/plist.h2
-rw-r--r--src/xplist.c4
-rw-r--r--test/CMakeLists.txt11
-rw-r--r--test/data/2.plist2
-rw-r--r--test/data/3.plist2
-rw-r--r--test/data/4.plist2
-rw-r--r--test/data/5.plist2
-rw-r--r--test/plist_cmp.c138
11 files changed, 246 insertions, 72 deletions
diff --git a/include/plist/plist.h b/include/plist/plist.h
index df30ac4..4547f18 100644
--- a/include/plist/plist.h
+++ b/include/plist/plist.h
@@ -394,6 +394,14 @@ extern "C" {
394 */ 394 */
395 PLIST_API plist_t plist_find_node_by_string(plist_t plist, const char *value); 395 PLIST_API plist_t plist_find_node_by_string(plist_t plist, const char *value);
396 396
397/**
398 * Compare two node values
399 *
400 * @param node_l left node to compare
401 * @param node_r rigth node to compare
402 * @return TRUE is type and value match, FALSE otherwise.
403 */
404 PLIST_API char plist_compare_node_value(plist_t node_l, plist_t node_r);
397 405
398/*@}*/ 406/*@}*/
399 407
diff --git a/src/bplist.c b/src/bplist.c
index 95070d7..cf9b9c6 100644
--- a/src/bplist.c
+++ b/src/bplist.c
@@ -87,7 +87,7 @@ static uint32_t uint24_from_be(char *buff)
87#define UINT_TO_HOST(x, n) \ 87#define UINT_TO_HOST(x, n) \
88 (n == 8 ? GUINT64_FROM_BE( *(uint64_t *)(x) ) : \ 88 (n == 8 ? GUINT64_FROM_BE( *(uint64_t *)(x) ) : \
89 (n == 4 ? GUINT32_FROM_BE( *(uint32_t *)(x) ) : \ 89 (n == 4 ? GUINT32_FROM_BE( *(uint32_t *)(x) ) : \
90 (n == 3 ? uint24_from_be( x ) : \ 90 (n == 3 ? uint24_from_be( (char*)x ) : \
91 (n == 2 ? GUINT16_FROM_BE( *(uint16_t *)(x) ) : \ 91 (n == 2 ? GUINT16_FROM_BE( *(uint16_t *)(x) ) : \
92 *(uint8_t *)(x) )))) 92 *(uint8_t *)(x) ))))
93 93
@@ -99,10 +99,7 @@ static uint32_t uint24_from_be(char *buff)
99 ( ((uint64_t)x) < (1ULL << 24) ? 3 : \ 99 ( ((uint64_t)x) < (1ULL << 24) ? 3 : \
100 ( ((uint64_t)x) < (1ULL << 32) ? 4 : 8)))) 100 ( ((uint64_t)x) < (1ULL << 32) ? 4 : 8))))
101 101
102#define get_real_bytes(x) (x >> 32 ? 4 : 8) 102#define get_real_bytes(x) (x == (float) x ? 4 : 8)
103
104
105
106 103
107 104
108static plist_t parse_uint_node(char *bnode, uint8_t size, char **next_object) 105static plist_t parse_uint_node(char *bnode, uint8_t size, char **next_object)
@@ -133,12 +130,18 @@ static plist_t parse_uint_node(char *bnode, uint8_t size, char **next_object)
133static plist_t parse_real_node(char *bnode, uint8_t size) 130static plist_t parse_real_node(char *bnode, uint8_t size)
134{ 131{
135 plist_data_t data = plist_new_plist_data(); 132 plist_data_t data = plist_new_plist_data();
133 float floatval = 0.0;
136 134
137 size = 1 << size; // make length less misleading 135 size = 1 << size; // make length less misleading
138 switch (size) { 136 switch (size) {
139 case sizeof(float): 137 case sizeof(float):
138 floatval = *(float*)bnode;
139 byte_convert((uint8_t*)&floatval, sizeof(float));
140 data->realval = floatval;
141 break;
140 case sizeof(double): 142 case sizeof(double):
141 data->intval = UINT_TO_HOST(bnode, size); //use the fact that we have an union to cheat byte swapping 143 data->realval = *(double*)bnode;
144 byte_convert((uint8_t*)&(data->realval), sizeof(double));
142 break; 145 break;
143 default: 146 default:
144 free(data); 147 free(data);
@@ -187,7 +190,7 @@ static plist_t parse_unicode_node(char *bnode, uint64_t size)
187 data->unicodeval[sizeof(gunichar2) * size] = '\0'; 190 data->unicodeval[sizeof(gunichar2) * size] = '\0';
188 data->length = size; 191 data->length = size;
189 for (i = 0; i <= size; i++) 192 for (i = 0; i <= size; i++)
190 byte_convert(data->unicodeval + i, sizeof(gunichar2)); 193 byte_convert((uint8_t*)(data->unicodeval + i), sizeof(gunichar2));
191 return g_node_new(data); 194 return g_node_new(data);
192} 195}
193 196
@@ -543,63 +546,7 @@ static guint plist_data_hash(gconstpointer key)
543 return hash; 546 return hash;
544} 547}
545 548
546static gboolean plist_data_compare(gconstpointer a, gconstpointer b)
547{
548 plist_data_t val_a = NULL;
549 plist_data_t val_b = NULL;
550 549
551 if (!a || !b)
552 return FALSE;
553
554 if (!((GNode *) a)->data || !((GNode *) b)->data)
555 return FALSE;
556
557 val_a = plist_get_data((plist_t) a);
558 val_b = plist_get_data((plist_t) b);
559
560 if (val_a->type != val_b->type)
561 return FALSE;
562
563 switch (val_a->type) {
564 case PLIST_BOOLEAN:
565 case PLIST_UINT:
566 case PLIST_REAL:
567 if (val_a->intval == val_b->intval) //it is an union so this is sufficient
568 return TRUE;
569 else
570 return FALSE;
571
572 case PLIST_KEY:
573 case PLIST_STRING:
574 if (!strcmp(val_a->strval, val_b->strval))
575 return TRUE;
576 else
577 return FALSE;
578 case PLIST_UNICODE:
579 if (!memcmp(val_a->unicodeval, val_b->unicodeval, val_a->length))
580 return TRUE;
581 else
582 return FALSE;
583
584 case PLIST_DATA:
585 case PLIST_ARRAY:
586 case PLIST_DICT:
587 //compare pointer
588 if (a == b)
589 return TRUE;
590 else
591 return FALSE;
592 break;
593 case PLIST_DATE:
594 if (!memcmp(&(val_a->timeval), &(val_b->timeval), sizeof(GTimeVal)))
595 return TRUE;
596 else
597 return FALSE;
598 default:
599 break;
600 }
601 return FALSE;
602}
603 550
604struct serialize_s { 551struct serialize_s {
605 GPtrArray *objects; 552 GPtrArray *objects;
@@ -655,7 +602,12 @@ static void write_real(GByteArray * bplist, double val)
655 uint64_t size = get_real_bytes(*((uint64_t *) & val)); //cheat to know used space 602 uint64_t size = get_real_bytes(*((uint64_t *) & val)); //cheat to know used space
656 uint8_t *buff = (uint8_t *) malloc(sizeof(uint8_t) + size); 603 uint8_t *buff = (uint8_t *) malloc(sizeof(uint8_t) + size);
657 buff[0] = BPLIST_REAL | Log2(size); 604 buff[0] = BPLIST_REAL | Log2(size);
658 memcpy(buff + 1, &val, size); 605 if (size == sizeof(double)) {
606 memcpy(buff + 1, &val, size);
607 } else if (size == sizeof(float)) {
608 float tmpval = (float) val;
609 memcpy(buff + 1, &tmpval, size);
610 }
659 byte_convert(buff + 1, size); 611 byte_convert(buff + 1, size);
660 g_byte_array_append(bplist, buff, sizeof(uint8_t) + size); 612 g_byte_array_append(bplist, buff, sizeof(uint8_t) + size);
661 free(buff); 613 free(buff);
diff --git a/src/plist.c b/src/plist.c
index 0e73c4b..e800b5c 100644
--- a/src/plist.c
+++ b/src/plist.c
@@ -414,3 +414,70 @@ void plist_get_date_val(plist_t node, int32_t * sec, int32_t * usec)
414 *sec = val.tv_sec; 414 *sec = val.tv_sec;
415 *usec = val.tv_usec; 415 *usec = val.tv_usec;
416} 416}
417
418gboolean plist_data_compare(gconstpointer a, gconstpointer b)
419{
420 plist_data_t val_a = NULL;
421 plist_data_t val_b = NULL;
422
423 if (!a || !b)
424 return FALSE;
425
426 if (!((GNode *) a)->data || !((GNode *) b)->data)
427 return FALSE;
428
429 val_a = plist_get_data((plist_t) a);
430 val_b = plist_get_data((plist_t) b);
431
432 if (val_a->type != val_b->type)
433 return FALSE;
434
435 switch (val_a->type) {
436 case PLIST_BOOLEAN:
437 case PLIST_UINT:
438 case PLIST_REAL:
439 if (val_a->intval == val_b->intval) //it is an union so this is sufficient
440 return TRUE;
441 else
442 return FALSE;
443
444 case PLIST_KEY:
445 case PLIST_STRING:
446 if (!strcmp(val_a->strval, val_b->strval))
447 return TRUE;
448 else
449 return FALSE;
450 case PLIST_UNICODE:
451 if (!memcmp(val_a->unicodeval, val_b->unicodeval, val_a->length))
452 return TRUE;
453 else
454 return FALSE;
455
456 case PLIST_DATA:
457 if (!memcmp(val_a->buff, val_b->buff, val_a->length))
458 return TRUE;
459 else
460 return FALSE;
461 case PLIST_ARRAY:
462 case PLIST_DICT:
463 //compare pointer
464 if (a == b)
465 return TRUE;
466 else
467 return FALSE;
468 break;
469 case PLIST_DATE:
470 if (!memcmp(&(val_a->timeval), &(val_b->timeval), sizeof(GTimeVal)))
471 return TRUE;
472 else
473 return FALSE;
474 default:
475 break;
476 }
477 return FALSE;
478}
479
480char plist_compare_node_value(plist_t node_l, plist_t node_r)
481{
482 return plist_data_compare( node_l , node_r );
483}
diff --git a/src/plist.h b/src/plist.h
index 4bb97e3..83c63bf 100644
--- a/src/plist.h
+++ b/src/plist.h
@@ -49,7 +49,7 @@ typedef struct plist_data_s *plist_data_t;
49plist_t plist_new_node(plist_data_t data); 49plist_t plist_new_node(plist_data_t data);
50plist_data_t plist_get_data(const plist_t node); 50plist_data_t plist_get_data(const plist_t node);
51plist_data_t plist_new_plist_data(); 51plist_data_t plist_new_plist_data();
52 52gboolean plist_data_compare(gconstpointer a, gconstpointer b);
53 53
54 54
55#endif 55#endif
diff --git a/src/xplist.c b/src/xplist.c
index e281b4f..c0849b6 100644
--- a/src/xplist.c
+++ b/src/xplist.c
@@ -152,12 +152,12 @@ static void node_to_xml(GNode * node, gpointer xml_struct)
152 152
153 case PLIST_UINT: 153 case PLIST_UINT:
154 tag = XPLIST_INT; 154 tag = XPLIST_INT;
155 val = g_strdup_printf("%lu", (long unsigned int) node_data->intval); 155 val = g_strdup_printf("%llu", node_data->intval);
156 break; 156 break;
157 157
158 case PLIST_REAL: 158 case PLIST_REAL:
159 tag = XPLIST_REAL; 159 tag = XPLIST_REAL;
160 val = g_strdup_printf("%Lf", (long double) node_data->realval); 160 val = g_strdup_printf("%f", node_data->realval);
161 break; 161 break;
162 162
163 case PLIST_STRING: 163 case PLIST_STRING:
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index b499018..40dc640 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -3,9 +3,12 @@
3 3
4SET(plist_test_SRC 4SET(plist_test_SRC
5 plist_test.c) 5 plist_test.c)
6SET(plist_cmp_SRC
7 plist_cmp.c)
6ADD_EXECUTABLE(plist_test ${plist_test_SRC}) 8ADD_EXECUTABLE(plist_test ${plist_test_SRC})
7TARGET_LINK_LIBRARIES(plist_test plist) 9TARGET_LINK_LIBRARIES(plist_test plist)
8 10ADD_EXECUTABLE(plist_cmp ${plist_cmp_SRC})
11TARGET_LINK_LIBRARIES(plist_cmp plist)
9 12
10INCLUDE(CTest) 13INCLUDE(CTest)
11 14
@@ -16,3 +19,9 @@ ADD_TEST(Large plist_test data/4.plist)
16ADD_TEST(Huge plist_test data/5.plist) 19ADD_TEST(Huge plist_test data/5.plist)
17ADD_TEST(Big_Array plist_test data/6.plist) 20ADD_TEST(Big_Array plist_test data/6.plist)
18 21
22ADD_TEST(EmptyCmp plist_cmp data/1.plist data/1.plist.out)
23ADD_TEST(SmallCmp plist_cmp data/2.plist data/2.plist.out)
24ADD_TEST(MediumCmp plist_cmp data/3.plist data/3.plist.out)
25ADD_TEST(LargeCmp plist_cmp data/4.plist data/4.plist.out)
26ADD_TEST(HugeCmp plist_cmp data/5.plist data/5.plist.out)
27ADD_TEST(Big_ArrayCmp plist_cmp data/6.plist data/6.plist.out)
diff --git a/test/data/2.plist b/test/data/2.plist
index 7586de0..169b822 100644
--- a/test/data/2.plist
+++ b/test/data/2.plist
@@ -23,7 +23,7 @@
23 <key>Some Int</key> 23 <key>Some Int</key>
24 <integer>32434543632</integer> 24 <integer>32434543632</integer>
25 <key>Some Real</key> 25 <key>Some Real</key>
26 <real>58654.3476563</real> 26 <real>58654.347656</real>
27 <key>Some Date</key> 27 <key>Some Date</key>
28 <date>2009-02-12T22:23:00Z</date> 28 <date>2009-02-12T22:23:00Z</date>
29 <key>Some Data</key> 29 <key>Some Data</key>
diff --git a/test/data/3.plist b/test/data/3.plist
index 30d5b3e..6d16191 100644
--- a/test/data/3.plist
+++ b/test/data/3.plist
@@ -23,7 +23,7 @@
23 <key>Some Int</key> 23 <key>Some Int</key>
24 <integer>32434543632</integer> 24 <integer>32434543632</integer>
25 <key>Some Real</key> 25 <key>Some Real</key>
26 <real>58654.3476563</real> 26 <real>58654.347656</real>
27 <key>Some Date</key> 27 <key>Some Date</key>
28 <date>2009-02-12T22:23:00Z</date> 28 <date>2009-02-12T22:23:00Z</date>
29 <key>Some Data</key> 29 <key>Some Data</key>
diff --git a/test/data/4.plist b/test/data/4.plist
index 87d6e4c..de7c488 100644
--- a/test/data/4.plist
+++ b/test/data/4.plist
@@ -23,7 +23,7 @@
23 <key>Some Int</key> 23 <key>Some Int</key>
24 <integer>32434543632</integer> 24 <integer>32434543632</integer>
25 <key>Some Real</key> 25 <key>Some Real</key>
26 <real>58654.3476563</real> 26 <real>58654.347656</real>
27 <key>Some Date</key> 27 <key>Some Date</key>
28 <date>2009-02-12T22:23:00Z</date> 28 <date>2009-02-12T22:23:00Z</date>
29 <key>Some Data</key> 29 <key>Some Data</key>
diff --git a/test/data/5.plist b/test/data/5.plist
index 388ff85..647c648 100644
--- a/test/data/5.plist
+++ b/test/data/5.plist
@@ -23,7 +23,7 @@
23 <key>Some Int</key> 23 <key>Some Int</key>
24 <integer>32434543632</integer> 24 <integer>32434543632</integer>
25 <key>Some Real</key> 25 <key>Some Real</key>
26 <real>58654.3476563</real> 26 <real>58654.347656</real>
27 <key>Some Date</key> 27 <key>Some Date</key>
28 <date>2009-02-12T22:23:00Z</date> 28 <date>2009-02-12T22:23:00Z</date>
29 <key>Some Data</key> 29 <key>Some Data</key>
diff --git a/test/plist_cmp.c b/test/plist_cmp.c
new file mode 100644
index 0000000..1a66119
--- /dev/null
+++ b/test/plist_cmp.c
@@ -0,0 +1,138 @@
1/*
2 * backup_test.c
3 * source libplist regression test
4 *
5 * Copyright (c) 2009 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/plist.h"
24
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <sys/stat.h>
29
30char compare_plist(plist_t node_l, plist_t node_r)
31{
32 plist_t cur_l = NULL;
33 plist_t cur_r = NULL;
34 int res = 1;
35
36 cur_l = plist_get_first_child(node_l);
37 cur_r = plist_get_first_child(node_r);
38
39 if ( (!cur_l && cur_r) || (cur_l && !cur_r))
40 return 0;
41
42 if ( !cur_l && !cur_r )
43 return plist_compare_node_value( node_l, node_r );
44
45 while(cur_l && cur_r && res) {
46
47 if (!(res = compare_plist(cur_l, cur_r)))
48 return res;
49
50 cur_l = plist_get_next_sibling(cur_l);
51 cur_r = plist_get_next_sibling(cur_r);
52 if ( (!cur_l && cur_r) || (cur_l && !cur_r))
53 return 0;
54 }
55
56 return res;
57}
58
59int main(int argc, char *argv[])
60{
61 FILE *iplist1 = NULL;
62 FILE *iplist2 = NULL;
63 plist_t root_node1 = NULL;
64 plist_t root_node2 = NULL;
65 char *plist_1 = NULL;
66 char *plist_2 = NULL;
67 int size_in1 = 0;
68 int size_in2 = 0;
69 char *file_in1 = NULL;
70 char *file_in2 = NULL;
71 int res = 0;
72
73 struct stat *filestats1 = (struct stat *) malloc(sizeof(struct stat));
74 struct stat *filestats2 = (struct stat *) malloc(sizeof(struct stat));
75
76 if (argc!= 3) {
77 printf("Wrong input\n");
78 return 1;
79 }
80
81 file_in1 = argv[1];
82 file_in2 = argv[2];
83
84 //read input file
85 iplist1 = fopen(file_in1, "rb");
86 iplist2 = fopen(file_in2, "rb");
87
88 if (!iplist1 || !iplist2) {
89 printf("File does not exists\n");
90 return 2;
91 }
92
93 stat(file_in1, filestats1);
94 stat(file_in2, filestats2);
95
96 size_in1 = filestats1->st_size;
97 size_in2 = filestats2->st_size;
98
99 plist_1 = (char *) malloc(sizeof(char) * (size_in1 + 1));
100 plist_2 = (char *) malloc(sizeof(char) * (size_in2 + 1));
101
102 fread(plist_1, sizeof(char), size_in1, iplist1);
103 fread(plist_2, sizeof(char), size_in2, iplist2);
104
105 fclose(iplist1);
106 fclose(iplist2);
107
108 if (memcmp(plist_1, "bplist00", 8) == 0)
109 plist_from_bin(plist_1, size_in1, &root_node1);
110 else
111 plist_from_xml(plist_1, size_in1, &root_node1);
112
113 if (memcmp(plist_2, "bplist00", 8) == 0)
114 plist_from_bin(plist_2, size_in2, &root_node2);
115 else
116 plist_from_xml(plist_2, size_in2, &root_node2);
117
118 if (!root_node1 || !root_node2) {
119 printf("PList parsing failed\n");
120 return 3;
121 }
122 else
123 printf("PList parsing succeeded\n");
124
125 res = compare_plist(root_node1, root_node2);
126
127
128 plist_free(root_node1);
129 plist_free(root_node2);
130
131 free(plist_1);
132 free(plist_2);
133 free(filestats1);
134 free(filestats2);
135
136 return !res;
137}
138