summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--include/plist/plist.h25
-rw-r--r--src/bplist.c60
-rw-r--r--src/plist.c43
-rw-r--r--src/plist.h1
-rw-r--r--src/xplist.c27
-rw-r--r--swig/plist.i1
6 files changed, 46 insertions, 111 deletions
diff --git a/include/plist/plist.h b/include/plist/plist.h
index b7b0fa4..c289158 100644
--- a/include/plist/plist.h
+++ b/include/plist/plist.h
@@ -71,8 +71,6 @@ extern "C" {
71 PLIST_REAL, /**< Real, scalar type */ 71 PLIST_REAL, /**< Real, scalar type */
72 PLIST_STRING, 72 PLIST_STRING,
73 /**< ASCII string, scalar type */ 73 /**< ASCII string, scalar type */
74 PLIST_UNICODE,
75 /**< Unicode strin, scalar type */
76 PLIST_ARRAY,/**< Ordered array, structured type */ 74 PLIST_ARRAY,/**< Ordered array, structured type */
77 PLIST_DICT, /**< Unordered dictionary (key/value pair), structured type */ 75 PLIST_DICT, /**< Unordered dictionary (key/value pair), structured type */
78 PLIST_DATE, /**< Date, scalar type */ 76 PLIST_DATE, /**< Date, scalar type */
@@ -189,7 +187,7 @@ extern "C" {
189 * (ie #PLIST_DICT or #PLIST_ARRAY). 187 * (ie #PLIST_DICT or #PLIST_ARRAY).
190 * 188 *
191 * @param node the node to add a children to 189 * @param node the node to add a children to
192 * @param val the string value encoded as an ASCII string (must be null terminated) 190 * @param val the string value encoded as an ASCII or UTF-8 string (must be null terminated)
193 */ 191 */
194 PLIST_API void plist_add_sub_string_el(plist_t node, const char *val); 192 PLIST_API void plist_add_sub_string_el(plist_t node, const char *val);
195 193
@@ -231,15 +229,6 @@ extern "C" {
231 PLIST_API void plist_add_sub_data_el(plist_t node, const char *val, uint64_t length); 229 PLIST_API void plist_add_sub_data_el(plist_t node, const char *val, uint64_t length);
232 230
233/** 231/**
234 * Add a subnode of type #PLIST_UNICODE to a node. The node must be of a structured type
235 * (ie #PLIST_DICT or #PLIST_ARRAY).
236 *
237 * @param node the node to add a children to
238 * @param val the unicode string encoded in UTF-8 (must be null terminated)
239 */
240 PLIST_API void plist_add_sub_unicode_el(plist_t node, const char *val);
241
242/**
243 * Add a subnode of type #PLIST_DATE to a node. The node must be of a structured type 232 * Add a subnode of type #PLIST_DATE to a node. The node must be of a structured type
244 * (ie #PLIST_DICT or #PLIST_ARRAY). 233 * (ie #PLIST_DICT or #PLIST_ARRAY).
245 * 234 *
@@ -280,7 +269,7 @@ extern "C" {
280 * 269 *
281 * @param node the node 270 * @param node the node
282 * @param val a pointer to a C-string. This function allocates the memory, 271 * @param val a pointer to a C-string. This function allocates the memory,
283 * caller is responsible for freeing it. 272 * caller is responsible for freeing it. Data is UTF-8 encoded.
284 */ 273 */
285 PLIST_API void plist_get_string_val(plist_t node, char **val); 274 PLIST_API void plist_get_string_val(plist_t node, char **val);
286 275
@@ -322,16 +311,6 @@ extern "C" {
322 PLIST_API void plist_get_data_val(plist_t node, char **val, uint64_t * length); 311 PLIST_API void plist_get_data_val(plist_t node, char **val, uint64_t * length);
323 312
324/** 313/**
325 * Get the value of a #PLIST_UNICODE node.
326 * This function does nothing if node is not of type #PLIST_UNICODE
327 *
328 * @param node the node
329 * @param val a pointer to a C-string. This function allocates the memory,
330 * caller is responsible for freeing it. Data is UTF-8 encoded.
331 */
332 PLIST_API void plist_get_unicode_val(plist_t node, char **val);
333
334/**
335 * Get the value of a #PLIST_DATE node. 314 * Get the value of a #PLIST_DATE node.
336 * This function does nothing if node is not of type #PLIST_DATE 315 * This function does nothing if node is not of type #PLIST_DATE
337 * 316 *
diff --git a/src/bplist.c b/src/bplist.c
index 2a82b9f..6b2d2f3 100644
--- a/src/bplist.c
+++ b/src/bplist.c
@@ -24,6 +24,8 @@
24#include <stdio.h> 24#include <stdio.h>
25#include <string.h> 25#include <string.h>
26 26
27#include <libxml/tree.h>
28
27#include <plist/plist.h> 29#include <plist/plist.h>
28#include "plist.h" 30#include "plist.h"
29 31
@@ -184,13 +186,28 @@ static plist_t parse_unicode_node(char *bnode, uint64_t size)
184{ 186{
185 plist_data_t data = plist_new_plist_data(); 187 plist_data_t data = plist_new_plist_data();
186 uint64_t i = 0; 188 uint64_t i = 0;
187 data->type = PLIST_UNICODE; 189 gunichar2 *unicodestr = NULL;
188 data->unicodeval = (gunichar2 *) malloc(sizeof(gunichar2) * (size + 1)); 190 gchar *tmpstr = NULL;
189 memcpy(data->unicodeval, bnode, sizeof(gunichar2) * size); 191 int type = 0;
190 data->unicodeval[sizeof(gunichar2) * size] = '\0'; 192 glong items_read = 0;
191 data->length = size; 193 glong items_written = 0;
192 for (i = 0; i <= size; i++) 194 GError *error = NULL;
193 byte_convert((uint8_t*)(data->unicodeval + i), sizeof(gunichar2)); 195
196 data->type = PLIST_STRING;
197 unicodestr = (gunichar2 *) malloc(sizeof(gunichar2) * size);
198 memcpy(unicodestr, bnode, sizeof(gunichar2) * size);
199 for (i = 0; i < size; i++)
200 byte_convert((uint8_t*)(unicodestr + i), sizeof(gunichar2));
201
202 tmpstr = g_utf16_to_utf8(unicodestr, size, &items_read, &items_written, &error);
203 free(unicodestr);
204
205 data->type = PLIST_STRING;
206 data->strval = (char *) malloc(sizeof(char) * (items_written + 1));
207 memcpy(data->strval, tmpstr, items_written);
208 data->strval[items_written] = '\0';
209 data->length = strlen(data->strval);
210 g_free(tmpstr);
194 return g_node_new(data); 211 return g_node_new(data);
195} 212}
196 213
@@ -364,10 +381,6 @@ static gpointer copy_plist_data(gconstpointer src, gpointer data)
364 case PLIST_STRING: 381 case PLIST_STRING:
365 dstdata->strval = strdup(srcdata->strval); 382 dstdata->strval = strdup(srcdata->strval);
366 break; 383 break;
367 case PLIST_UNICODE:
368 dstdata->unicodeval = (gunichar2 *) malloc(srcdata->length * sizeof(gunichar2));
369 memcpy(dstdata->unicodeval, srcdata->unicodeval, srcdata->length * sizeof(gunichar2));
370 break;
371 case PLIST_DATA: 384 case PLIST_DATA:
372 case PLIST_ARRAY: 385 case PLIST_ARRAY:
373 dstdata->buff = (uint8_t *) malloc(sizeof(uint8_t *) * srcdata->length); 386 dstdata->buff = (uint8_t *) malloc(sizeof(uint8_t *) * srcdata->length);
@@ -520,10 +533,6 @@ static guint plist_data_hash(gconstpointer key)
520 buff = data->strval; 533 buff = data->strval;
521 size = strlen(buff); 534 size = strlen(buff);
522 break; 535 break;
523 case PLIST_UNICODE:
524 buff = (char *) data->unicodeval;
525 size = data->length;
526 break;
527 case PLIST_DATA: 536 case PLIST_DATA:
528 case PLIST_ARRAY: 537 case PLIST_ARRAY:
529 case PLIST_DICT: 538 case PLIST_DICT:
@@ -752,6 +761,13 @@ void plist_to_bin(plist_t plist, char **plist_bin, uint32_t * length)
752 uint64_t *offsets = NULL; 761 uint64_t *offsets = NULL;
753 uint8_t pad[6] = { 0, 0, 0, 0, 0, 0 }; 762 uint8_t pad[6] = { 0, 0, 0, 0, 0, 0 };
754 uint8_t trailer[BPLIST_TRL_SIZE]; 763 uint8_t trailer[BPLIST_TRL_SIZE];
764 //for string
765 glong len = 0;
766 int type = 0;
767 glong items_read = 0;
768 glong items_written = 0;
769 GError *error = NULL;
770 gunichar2 *unicodestr = NULL;
755 771
756 //check for valid input 772 //check for valid input
757 if (!plist || !plist_bin || *plist_bin || !length) 773 if (!plist || !plist_bin || *plist_bin || !length)
@@ -806,10 +822,16 @@ void plist_to_bin(plist_t plist, char **plist_bin, uint32_t * length)
806 822
807 case PLIST_KEY: 823 case PLIST_KEY:
808 case PLIST_STRING: 824 case PLIST_STRING:
809 write_string(bplist_buff, data->strval); 825 len = strlen(data->strval);
810 break; 826 type = xmlDetectCharEncoding(data->strval, len);
811 case PLIST_UNICODE: 827 if (XML_CHAR_ENCODING_UTF8 == type) {
812 write_unicode(bplist_buff, data->unicodeval, data->length); 828 unicodestr = g_utf8_to_utf16(data->strval, len, &items_read, &items_written, &error);
829 write_unicode(bplist_buff, unicodestr, items_written);
830 g_free(unicodestr);
831 }
832 else if (XML_CHAR_ENCODING_ASCII == type || XML_CHAR_ENCODING_NONE == type) {
833 write_string(bplist_buff, data->strval);
834 }
813 break; 835 break;
814 case PLIST_DATA: 836 case PLIST_DATA:
815 write_data(bplist_buff, data->buff, data->length); 837 write_data(bplist_buff, data->buff, data->length);
diff --git a/src/plist.c b/src/plist.c
index 4fc2780..7949bce 100644
--- a/src/plist.c
+++ b/src/plist.c
@@ -53,9 +53,6 @@ static void plist_free_node(GNode * node, gpointer none)
53 case PLIST_STRING: 53 case PLIST_STRING:
54 free(data->strval); 54 free(data->strval);
55 break; 55 break;
56 case PLIST_UNICODE:
57 free(data->unicodeval);
58 break;
59 case PLIST_DATA: 56 case PLIST_DATA:
60 free(data->buff); 57 free(data->buff);
61 break; 58 break;
@@ -90,10 +87,6 @@ static plist_t plist_add_sub_element(plist_t node, plist_type type, const void *
90 //only structured types are allowed to have nulll value 87 //only structured types are allowed to have nulll value
91 if (value || (!value && (type == PLIST_DICT || type == PLIST_ARRAY))) { 88 if (value || (!value && (type == PLIST_DICT || type == PLIST_ARRAY))) {
92 89
93 glong len = 0;
94 glong items_read = 0;
95 glong items_written = 0;
96 GError *error = NULL;
97 plist_t subnode = NULL; 90 plist_t subnode = NULL;
98 91
99 //now handle value 92 //now handle value
@@ -115,11 +108,6 @@ static plist_t plist_add_sub_element(plist_t node, plist_type type, const void *
115 case PLIST_STRING: 108 case PLIST_STRING:
116 data->strval = strdup((char *) value); 109 data->strval = strdup((char *) value);
117 break; 110 break;
118 case PLIST_UNICODE:
119 len = strlen((char *) value);
120 data->unicodeval = g_utf8_to_utf16((char *) value, len, &items_read, &items_written, &error);
121 data->length = items_written;
122 break;
123 case PLIST_DATA: 111 case PLIST_DATA:
124 data->buff = (uint8_t*)malloc(length); 112 data->buff = (uint8_t*)malloc(length);
125 memcpy(data->buff, value, length); 113 memcpy(data->buff, value, length);
@@ -210,9 +198,6 @@ static char compare_node_value(plist_type type, plist_data_t data, const void *v
210 case PLIST_STRING: 198 case PLIST_STRING:
211 res = !strcmp(data->strval, ((char *) value)); 199 res = !strcmp(data->strval, ((char *) value));
212 break; 200 break;
213 case PLIST_UNICODE:
214 res = !memcpy(data->unicodeval, value, length);
215 break;
216 case PLIST_DATA: 201 case PLIST_DATA:
217 res = !memcmp(data->buff, (char *) value, length); 202 res = !memcmp(data->buff, (char *) value, length);
218 break; 203 break;
@@ -262,11 +247,6 @@ plist_t plist_find_node_by_string(plist_t plist, const char *value)
262 247
263static void plist_get_type_and_value(plist_t node, plist_type * type, void *value, uint64_t * length) 248static void plist_get_type_and_value(plist_t node, plist_type * type, void *value, uint64_t * length)
264{ 249{
265 //for unicode
266 glong len = 0;
267 glong items_read = 0;
268 glong items_written = 0;
269 GError *error = NULL;
270 plist_data_t data = NULL; 250 plist_data_t data = NULL;
271 251
272 if (!node) 252 if (!node)
@@ -291,10 +271,6 @@ static void plist_get_type_and_value(plist_t node, plist_type * type, void *valu
291 case PLIST_STRING: 271 case PLIST_STRING:
292 *((char **) value) = strdup(data->strval); 272 *((char **) value) = strdup(data->strval);
293 break; 273 break;
294 case PLIST_UNICODE:
295 len = data->length;
296 *((char **) value) = g_utf16_to_utf8(data->unicodeval, len, &items_read, &items_written, &error);
297 break;
298 case PLIST_DATA: 274 case PLIST_DATA:
299 *((uint8_t **) value) = (uint8_t *) malloc(*length * sizeof(uint8_t)); 275 *((uint8_t **) value) = (uint8_t *) malloc(*length * sizeof(uint8_t));
300 memcpy(*((uint8_t **) value), data->buff, *length * sizeof(uint8_t)); 276 memcpy(*((uint8_t **) value), data->buff, *length * sizeof(uint8_t));
@@ -360,11 +336,6 @@ void plist_add_sub_data_el(plist_t node, const char *val, uint64_t length)
360 plist_add_sub_element(node, PLIST_DATA, val, length); 336 plist_add_sub_element(node, PLIST_DATA, val, length);
361} 337}
362 338
363void plist_add_sub_unicode_el(plist_t node, const char *val)
364{
365 plist_add_sub_element(node, PLIST_UNICODE, val, strlen(val));
366}
367
368void plist_add_sub_date_el(plist_t node, int32_t sec, int32_t usec) 339void plist_add_sub_date_el(plist_t node, int32_t sec, int32_t usec)
369{ 340{
370 GTimeVal val = { sec, usec }; 341 GTimeVal val = { sec, usec };
@@ -423,15 +394,6 @@ void plist_get_data_val(plist_t node, char **val, uint64_t * length)
423 plist_get_type_and_value(node, &type, (void *) val, length); 394 plist_get_type_and_value(node, &type, (void *) val, length);
424} 395}
425 396
426void plist_get_unicode_val(plist_t node, char **val)
427{
428 plist_type type = plist_get_node_type(node);
429 uint64_t length = 0;
430 if (PLIST_UNICODE == type)
431 plist_get_type_and_value(node, &type, (void *) val, &length);
432 assert(length == strlen(*val));
433}
434
435void plist_get_date_val(plist_t node, int32_t * sec, int32_t * usec) 397void plist_get_date_val(plist_t node, int32_t * sec, int32_t * usec)
436{ 398{
437 plist_type type = plist_get_node_type(node); 399 plist_type type = plist_get_node_type(node);
@@ -476,11 +438,6 @@ gboolean plist_data_compare(gconstpointer a, gconstpointer b)
476 return TRUE; 438 return TRUE;
477 else 439 else
478 return FALSE; 440 return FALSE;
479 case PLIST_UNICODE:
480 if (!memcmp(val_a->unicodeval, val_b->unicodeval, val_a->length))
481 return TRUE;
482 else
483 return FALSE;
484 441
485 case PLIST_DATA: 442 case PLIST_DATA:
486 if (!memcmp(val_a->buff, val_b->buff, val_a->length)) 443 if (!memcmp(val_a->buff, val_b->buff, val_a->length))
diff --git a/src/plist.h b/src/plist.h
index ebbb0b5..723ed87 100644
--- a/src/plist.h
+++ b/src/plist.h
@@ -36,7 +36,6 @@ struct plist_data_s {
36 uint64_t intval; 36 uint64_t intval;
37 double realval; 37 double realval;
38 char *strval; 38 char *strval;
39 gunichar2 *unicodeval;
40 uint8_t *buff; 39 uint8_t *buff;
41 GTimeVal timeval; 40 GTimeVal timeval;
42 }; 41 };
diff --git a/src/xplist.c b/src/xplist.c
index 2bee7c7..38cc4fe 100644
--- a/src/xplist.c
+++ b/src/xplist.c
@@ -126,12 +126,6 @@ static void node_to_xml(GNode * node, gpointer xml_struct)
126 //for base64 126 //for base64
127 gchar *valtmp = NULL; 127 gchar *valtmp = NULL;
128 128
129 //for unicode
130 glong len = 0;
131 glong items_read = 0;
132 glong items_written = 0;
133 GError *error = NULL;
134
135 uint32_t i = 0; 129 uint32_t i = 0;
136 130
137 if (!node) 131 if (!node)
@@ -165,12 +159,6 @@ static void node_to_xml(GNode * node, gpointer xml_struct)
165 val = g_strdup(node_data->strval); 159 val = g_strdup(node_data->strval);
166 break; 160 break;
167 161
168 case PLIST_UNICODE:
169 tag = XPLIST_STRING;
170 len = node_data->length;
171 val = g_utf16_to_utf8(node_data->unicodeval, len, &items_read, &items_written, &error);
172 break;
173
174 case PLIST_KEY: 162 case PLIST_KEY:
175 tag = XPLIST_KEY; 163 tag = XPLIST_KEY;
176 val = g_strdup((gchar *) node_data->strval); 164 val = g_strdup((gchar *) node_data->strval);
@@ -233,11 +221,7 @@ static void xml_to_node(xmlNodePtr xml_node, plist_t * plist_node)
233 plist_t subnode = NULL; 221 plist_t subnode = NULL;
234 222
235 //for string 223 //for string
236 unsigned char *tmp = NULL;
237 glong len = 0; 224 glong len = 0;
238 glong items_read = 0;
239 glong items_written = 0;
240 GError *error = NULL;
241 int type = 0; 225 int type = 0;
242 226
243 if (!xml_node) 227 if (!xml_node)
@@ -301,16 +285,11 @@ static void xml_to_node(xmlNodePtr xml_node, plist_t * plist_node)
301 if (!xmlStrcmp(node->name, XPLIST_STRING)) { 285 if (!xmlStrcmp(node->name, XPLIST_STRING)) {
302 xmlChar *strval = xmlNodeGetContent(node); 286 xmlChar *strval = xmlNodeGetContent(node);
303 len = strlen((char *) strval); 287 len = strlen((char *) strval);
304 items_read = 0;
305 items_written = 0;
306 error = NULL;
307 type = xmlDetectCharEncoding(strval, len); 288 type = xmlDetectCharEncoding(strval, len);
308 289
309 if (XML_CHAR_ENCODING_UTF8 == type) { 290 if (XML_CHAR_ENCODING_UTF8 == type ||
310 data->unicodeval = g_utf8_to_utf16((char *) strval, len, &items_read, &items_written, &error); 291 XML_CHAR_ENCODING_ASCII == type ||
311 data->type = PLIST_UNICODE; 292 XML_CHAR_ENCODING_NONE == type) {
312 data->length = items_written;
313 } else if (XML_CHAR_ENCODING_ASCII == type || XML_CHAR_ENCODING_NONE == type) {
314 data->strval = strdup((char *) strval); 293 data->strval = strdup((char *) strval);
315 data->type = PLIST_STRING; 294 data->type = PLIST_STRING;
316 data->length = strlen(data->strval); 295 data->length = strlen(data->strval);
diff --git a/swig/plist.i b/swig/plist.i
index 79298e6..b6f9f78 100644
--- a/swig/plist.i
+++ b/swig/plist.i
@@ -30,7 +30,6 @@ typedef enum {
30 PLIST_UINT, 30 PLIST_UINT,
31 PLIST_REAL, 31 PLIST_REAL,
32 PLIST_STRING, 32 PLIST_STRING,
33 PLIST_UNICODE,
34 PLIST_ARRAY, 33 PLIST_ARRAY,
35 PLIST_DICT, 34 PLIST_DICT,
36 PLIST_DATE, 35 PLIST_DATE,