summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Jonathan Beck2009-01-22 22:18:43 +0100
committerGravatar Jonathan Beck2009-01-22 22:18:43 +0100
commitbfd57eb1cf738ed62e17941e855347772ce48ef2 (patch)
tree45eda77468ff586bdd64fd6afcb5452cb0407bad
parent8da4dac2e65778d1f562f4502c09b16590cfd8e1 (diff)
downloadlibplist-bfd57eb1cf738ed62e17941e855347772ce48ef2.tar.gz
libplist-bfd57eb1cf738ed62e17941e855347772ce48ef2.tar.bz2
Add Unicode support.
-rw-r--r--src/Makefile.am2
-rw-r--r--src/bplist.c38
-rw-r--r--src/plist.c9
-rw-r--r--src/plist.h3
-rw-r--r--src/utils.c3
-rw-r--r--src/xplist.c33
6 files changed, 59 insertions, 29 deletions
diff --git a/src/Makefile.am b/src/Makefile.am
index 3178d8a..eb9cd08 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -1,6 +1,6 @@
1INCLUDES = -I$(top_srcdir)/include 1INCLUDES = -I$(top_srcdir)/include
2 2
3AM_CFLAGS = $(GLOBAL_CFLAGS) $(libxml2_CFLAGS) $(libglib2_CFLAGS) -D_GNU_SOURCE 3AM_CFLAGS = $(GLOBAL_CFLAGS) $(libxml2_CFLAGS) $(libglib2_CFLAGS)
4AM_LDFLAGS = $(libxml2_LIBS) $(libglib2_LIBS) 4AM_LDFLAGS = $(libxml2_LIBS) $(libglib2_LIBS)
5 5
6lib_LTLIBRARIES = libplist.la 6lib_LTLIBRARIES = libplist.la
diff --git a/src/bplist.c b/src/bplist.c
index fd0280c..67d7f72 100644
--- a/src/bplist.c
+++ b/src/bplist.c
@@ -21,7 +21,6 @@
21 21
22 22
23#include "plist.h" 23#include "plist.h"
24#include <wchar.h>
25#include <stdlib.h> 24#include <stdlib.h>
26#include <stdio.h> 25#include <stdio.h>
27#include <string.h> 26#include <string.h>
@@ -152,7 +151,7 @@ static plist_t parse_date_node(char *bnode, uint8_t size)
152 return node; 151 return node;
153} 152}
154 153
155static plist_t parse_string_node(char *bnode, uint8_t size) 154static plist_t parse_string_node(char *bnode, uint64_t size)
156{ 155{
157 plist_data_t data = plist_new_plist_data(); 156 plist_data_t data = plist_new_plist_data();
158 157
@@ -165,16 +164,17 @@ static plist_t parse_string_node(char *bnode, uint8_t size)
165 return g_node_new(data); 164 return g_node_new(data);
166} 165}
167 166
168static plist_t parse_unicode_node(char *bnode, uint8_t size) 167static plist_t parse_unicode_node(char *bnode, uint64_t size)
169{ 168{
170 plist_data_t data = plist_new_plist_data(); 169 plist_data_t data = plist_new_plist_data();
171 170 uint64_t i = 0;
172 data->type = PLIST_UNICODE; 171 data->type = PLIST_UNICODE;
173 data->unicodeval = (wchar_t *) malloc(sizeof(wchar_t) * (size + 1)); 172 data->unicodeval = (gunichar2 *) malloc(sizeof(gunichar2) * (size + 1));
174 memcpy(data->unicodeval, bnode, size); 173 memcpy(data->unicodeval, bnode, sizeof(gunichar2) * size);
175 data->unicodeval[size] = '\0'; 174 data->unicodeval[sizeof(gunichar2) * size] = '\0';
176 data->length = wcslen(data->unicodeval); 175 data->length = size;
177 176 for (i = 0; i <= size; i++)
177 byte_convert(data->unicodeval + i, sizeof(gunichar2));
178 return g_node_new(data); 178 return g_node_new(data);
179} 179}
180 180
@@ -338,7 +338,8 @@ static gpointer copy_plist_data(gconstpointer src, gpointer data)
338 dstdata->strval = strdup(srcdata->strval); 338 dstdata->strval = strdup(srcdata->strval);
339 break; 339 break;
340 case PLIST_UNICODE: 340 case PLIST_UNICODE:
341 dstdata->unicodeval = wcsdup(srcdata->unicodeval); 341 dstdata->unicodeval = (gunichar2*) malloc(srcdata->length * sizeof(gunichar2));
342 memcpy(dstdata->unicodeval, srcdata->unicodeval, srcdata->length * sizeof(gunichar2));
342 break; 343 break;
343 case PLIST_DATA: 344 case PLIST_DATA:
344 case PLIST_ARRAY: 345 case PLIST_ARRAY:
@@ -493,7 +494,7 @@ static guint plist_data_hash(gconstpointer key)
493 break; 494 break;
494 case PLIST_UNICODE: 495 case PLIST_UNICODE:
495 buff = (char *) data->unicodeval; 496 buff = (char *) data->unicodeval;
496 size = strlen(buff) * sizeof(wchar_t); 497 size = data->length;
497 break; 498 break;
498 case PLIST_DATA: 499 case PLIST_DATA:
499 case PLIST_ARRAY: 500 case PLIST_ARRAY:
@@ -544,7 +545,7 @@ static gboolean plist_data_compare(gconstpointer a, gconstpointer b)
544 else 545 else
545 return FALSE; 546 return FALSE;
546 case PLIST_UNICODE: 547 case PLIST_UNICODE:
547 if (!wcscmp(val_a->unicodeval, val_b->unicodeval)) 548 if (!memcmp(val_a->unicodeval, val_b->unicodeval,val_a->length))
548 return TRUE; 549 return TRUE;
549 else 550 else
550 return FALSE; 551 return FALSE;
@@ -662,6 +663,17 @@ static void write_string(GByteArray * bplist, char *val)
662 write_raw_data(bplist, BPLIST_STRING, (uint8_t *) val, size); 663 write_raw_data(bplist, BPLIST_STRING, (uint8_t *) val, size);
663} 664}
664 665
666static void write_unicode(GByteArray * bplist, gunichar2 *val, uint64_t size)
667{
668 uint64_t i = 0;
669 uint64_t size2 = size * sizeof(gunichar2);
670 uint8_t* buff = (uint8_t*) malloc(size2);
671 memcpy(buff, val, size2);
672 for (i = 0; i < size; i++)
673 byte_convert(buff + i * sizeof(gunichar2), sizeof(gunichar2));
674 write_raw_data(bplist, BPLIST_STRING, buff, size2);
675}
676
665static void write_array(GByteArray * bplist, GNode * node, GHashTable * ref_table, uint8_t dict_param_size) 677static void write_array(GByteArray * bplist, GNode * node, GHashTable * ref_table, uint8_t dict_param_size)
666{ 678{
667 uint64_t size = g_node_n_children(node); 679 uint64_t size = g_node_n_children(node);
@@ -784,7 +796,7 @@ void plist_to_bin(plist_t plist, char **plist_bin, uint32_t * length)
784 write_string(bplist_buff, data->strval); 796 write_string(bplist_buff, data->strval);
785 break; 797 break;
786 case PLIST_UNICODE: 798 case PLIST_UNICODE:
787 //TODO 799 write_unicode(bplist_buff, data->unicodeval, data->length);
788 break; 800 break;
789 case PLIST_DATA: 801 case PLIST_DATA:
790 write_data(bplist_buff, data->buff, data->length); 802 write_data(bplist_buff, data->buff, data->length);
diff --git a/src/plist.c b/src/plist.c
index e5cf2db..74868cc 100644
--- a/src/plist.c
+++ b/src/plist.c
@@ -24,7 +24,6 @@
24#include <assert.h> 24#include <assert.h>
25#include "utils.h" 25#include "utils.h"
26#include "plist.h" 26#include "plist.h"
27#include <wchar.h>
28#include <stdlib.h> 27#include <stdlib.h>
29#include <stdio.h> 28#include <stdio.h>
30 29
@@ -99,7 +98,8 @@ plist_t plist_add_sub_element(plist_t node, plist_type type, void *value, uint64
99 data->strval = strdup((char *) value); 98 data->strval = strdup((char *) value);
100 break; 99 break;
101 case PLIST_UNICODE: 100 case PLIST_UNICODE:
102 data->unicodeval = wcsdup((wchar_t *) value); 101 data->unicodeval = (gunichar2*) malloc(length * sizeof(gunichar2));
102 memcpy(data->unicodeval, value, length * sizeof(gunichar2));
103 break; 103 break;
104 case PLIST_DATA: 104 case PLIST_DATA:
105 memcpy(data->buff, value, length); 105 memcpy(data->buff, value, length);
@@ -158,7 +158,7 @@ static char compare_node_value(plist_type type, plist_data_t data, void *value,
158 res = !strcmp(data->strval, ((char *) value)); 158 res = !strcmp(data->strval, ((char *) value));
159 break; 159 break;
160 case PLIST_UNICODE: 160 case PLIST_UNICODE:
161 res = !wcscmp(data->unicodeval, ((wchar_t *) value)); 161 res = !memcpy(data->unicodeval, value, length);
162 break; 162 break;
163 case PLIST_DATA: 163 case PLIST_DATA:
164 res = memcmp(data->buff, (char *) value, length); 164 res = memcmp(data->buff, (char *) value, length);
@@ -229,7 +229,8 @@ void plist_get_type_and_value(plist_t node, plist_type * type, void *value, uint
229 *((char **) value) = strdup(data->strval); 229 *((char **) value) = strdup(data->strval);
230 break; 230 break;
231 case PLIST_UNICODE: 231 case PLIST_UNICODE:
232 *((wchar_t **) value) = wcsdup(data->unicodeval); 232 *((gunichar2 **) value) = malloc (*length * sizeof(gunichar2));
233 memcpy(*((gunichar2 **) value), data->unicodeval, *length * sizeof(gunichar2));
233 break; 234 break;
234 case PLIST_DATA: 235 case PLIST_DATA:
235 case PLIST_ARRAY: 236 case PLIST_ARRAY:
diff --git a/src/plist.h b/src/plist.h
index 449a3f7..80a3f7f 100644
--- a/src/plist.h
+++ b/src/plist.h
@@ -25,7 +25,6 @@
25#include "plist/plist.h" 25#include "plist/plist.h"
26 26
27#include <stdint.h> 27#include <stdint.h>
28#include <wchar.h>
29 28
30#include <sys/types.h> 29#include <sys/types.h>
31#include <sys/stat.h> 30#include <sys/stat.h>
@@ -42,7 +41,7 @@ struct plist_data_s {
42 uint64_t intval; 41 uint64_t intval;
43 double realval; 42 double realval;
44 char *strval; 43 char *strval;
45 wchar_t *unicodeval; 44 gunichar2 *unicodeval;
46 uint8_t *buff; 45 uint8_t *buff;
47 GTimeVal timeval; 46 GTimeVal timeval;
48 }; 47 };
diff --git a/src/utils.c b/src/utils.c
index ceb1f5d..920b347 100644
--- a/src/utils.c
+++ b/src/utils.c
@@ -35,8 +35,6 @@ void iphone_set_debug(int level)
35 toto_debug = level; 35 toto_debug = level;
36} 36}
37 37
38
39
40void log_debug_msg(const char *format, ...) 38void log_debug_msg(const char *format, ...)
41{ 39{
42#ifndef STRIP_DEBUG_CODE 40#ifndef STRIP_DEBUG_CODE
@@ -60,7 +58,6 @@ inline void log_debug_buffer(const char *data, const int length)
60 /* run the real fprintf */ 58 /* run the real fprintf */
61 if (toto_debug) 59 if (toto_debug)
62 fwrite(data, 1, length, stderr); 60 fwrite(data, 1, length, stderr);
63
64#endif 61#endif
65} 62}
66 63
diff --git a/src/xplist.c b/src/xplist.c
index 0b803cf..e61797c 100644
--- a/src/xplist.c
+++ b/src/xplist.c
@@ -24,7 +24,6 @@
24#include <assert.h> 24#include <assert.h>
25#include "utils.h" 25#include "utils.h"
26#include "plist.h" 26#include "plist.h"
27#include <wchar.h>
28#include <stdlib.h> 27#include <stdlib.h>
29#include <stdio.h> 28#include <stdio.h>
30 29
@@ -139,6 +138,12 @@ static void node_to_xml(GNode * node, gpointer xml_struct)
139 const xmlChar *tag = NULL; 138 const xmlChar *tag = NULL;
140 gchar *val = NULL; 139 gchar *val = NULL;
141 140
141 //for unicode
142 glong len = 0;
143 glong items_read = 0;
144 glong items_written = 0;
145 GError *error = NULL;
146
142 switch (node_data->type) { 147 switch (node_data->type) {
143 case PLIST_BOOLEAN: 148 case PLIST_BOOLEAN:
144 { 149 {
@@ -166,7 +171,8 @@ static void node_to_xml(GNode * node, gpointer xml_struct)
166 171
167 case PLIST_UNICODE: 172 case PLIST_UNICODE:
168 tag = XPLIST_STRING; 173 tag = XPLIST_STRING;
169 val = g_strdup((gchar *) node_data->unicodeval); 174 len = node_data->length;
175 val = g_utf16_to_utf8(node_data->unicodeval, len, &items_read, &items_written, &error);
170 break; 176 break;
171 177
172 case PLIST_KEY: 178 case PLIST_KEY:
@@ -278,13 +284,28 @@ static void xml_to_node(xmlNodePtr xml_node, plist_t * plist_node)
278 g_time_val_from_iso8601((char *) xmlNodeGetContent(node), &data->timeval); 284 g_time_val_from_iso8601((char *) xmlNodeGetContent(node), &data->timeval);
279 data->type = PLIST_DATE; 285 data->type = PLIST_DATE;
280 data->length = sizeof(GTimeVal); 286 data->length = sizeof(GTimeVal);
281 continue; //TODO : handle date tag 287 continue;
282 } 288 }
283 289
284 if (!xmlStrcmp(node->name, XPLIST_STRING)) { 290 if (!xmlStrcmp(node->name, XPLIST_STRING)) {
285 data->strval = strdup((char *) xmlNodeGetContent(node)); 291
286 data->type = PLIST_STRING; 292 unsigned char *tmp = xmlNodeGetContent(node);
287 data->length = strlen(data->strval); 293 glong len = strlen((char*)tmp);
294 glong items_read = 0;
295 glong items_written = 0;
296 GError *error = NULL;
297 int type = xmlDetectCharEncoding(tmp, len);
298
299 if (XML_CHAR_ENCODING_UTF8 == type) {
300 data->unicodeval = g_utf8_to_utf16((char*) tmp, len, &items_read, &items_written, &error);
301 data->type = PLIST_UNICODE;
302 data->length = items_written;
303 }
304 else if (XML_CHAR_ENCODING_ASCII == type || XML_CHAR_ENCODING_NONE == type) {
305 data->strval = strdup((char *) xmlNodeGetContent(node));
306 data->type = PLIST_STRING;
307 data->length = strlen(data->strval);
308 }
288 continue; 309 continue;
289 } 310 }
290 311