From 07b8041d44c6b43da1b0c55d140999cb3137d040 Mon Sep 17 00:00:00 2001 From: Jonathan Beck Date: Tue, 28 Apr 2009 22:15:39 +0200 Subject: Merge ascii and unicode handling in PLIST_STRING using UTF-8. Remove unicode related declaration in API (breaks API&ABI) --- include/plist/plist.h | 25 ++------------------- src/bplist.c | 60 +++++++++++++++++++++++++++++++++++---------------- src/plist.c | 43 ------------------------------------ src/plist.h | 1 - src/xplist.c | 27 +++-------------------- swig/plist.i | 1 - 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" { PLIST_REAL, /**< Real, scalar type */ PLIST_STRING, /**< ASCII string, scalar type */ - PLIST_UNICODE, - /**< Unicode strin, scalar type */ PLIST_ARRAY,/**< Ordered array, structured type */ PLIST_DICT, /**< Unordered dictionary (key/value pair), structured type */ PLIST_DATE, /**< Date, scalar type */ @@ -189,7 +187,7 @@ extern "C" { * (ie #PLIST_DICT or #PLIST_ARRAY). * * @param node the node to add a children to - * @param val the string value encoded as an ASCII string (must be null terminated) + * @param val the string value encoded as an ASCII or UTF-8 string (must be null terminated) */ PLIST_API void plist_add_sub_string_el(plist_t node, const char *val); @@ -230,15 +228,6 @@ extern "C" { */ PLIST_API void plist_add_sub_data_el(plist_t node, const char *val, uint64_t length); -/** - * Add a subnode of type #PLIST_UNICODE to a node. The node must be of a structured type - * (ie #PLIST_DICT or #PLIST_ARRAY). - * - * @param node the node to add a children to - * @param val the unicode string encoded in UTF-8 (must be null terminated) - */ - PLIST_API void plist_add_sub_unicode_el(plist_t node, const char *val); - /** * Add a subnode of type #PLIST_DATE to a node. The node must be of a structured type * (ie #PLIST_DICT or #PLIST_ARRAY). @@ -280,7 +269,7 @@ extern "C" { * * @param node the node * @param val a pointer to a C-string. This function allocates the memory, - * caller is responsible for freeing it. + * caller is responsible for freeing it. Data is UTF-8 encoded. */ PLIST_API void plist_get_string_val(plist_t node, char **val); @@ -321,16 +310,6 @@ extern "C" { */ PLIST_API void plist_get_data_val(plist_t node, char **val, uint64_t * length); -/** - * Get the value of a #PLIST_UNICODE node. - * This function does nothing if node is not of type #PLIST_UNICODE - * - * @param node the node - * @param val a pointer to a C-string. This function allocates the memory, - * caller is responsible for freeing it. Data is UTF-8 encoded. - */ - PLIST_API void plist_get_unicode_val(plist_t node, char **val); - /** * Get the value of a #PLIST_DATE node. * This function does nothing if node is not of type #PLIST_DATE 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 @@ #include #include +#include + #include #include "plist.h" @@ -184,13 +186,28 @@ static plist_t parse_unicode_node(char *bnode, uint64_t size) { plist_data_t data = plist_new_plist_data(); uint64_t i = 0; - data->type = PLIST_UNICODE; - data->unicodeval = (gunichar2 *) malloc(sizeof(gunichar2) * (size + 1)); - memcpy(data->unicodeval, bnode, sizeof(gunichar2) * size); - data->unicodeval[sizeof(gunichar2) * size] = '\0'; - data->length = size; - for (i = 0; i <= size; i++) - byte_convert((uint8_t*)(data->unicodeval + i), sizeof(gunichar2)); + gunichar2 *unicodestr = NULL; + gchar *tmpstr = NULL; + int type = 0; + glong items_read = 0; + glong items_written = 0; + GError *error = NULL; + + data->type = PLIST_STRING; + unicodestr = (gunichar2 *) malloc(sizeof(gunichar2) * size); + memcpy(unicodestr, bnode, sizeof(gunichar2) * size); + for (i = 0; i < size; i++) + byte_convert((uint8_t*)(unicodestr + i), sizeof(gunichar2)); + + tmpstr = g_utf16_to_utf8(unicodestr, size, &items_read, &items_written, &error); + free(unicodestr); + + data->type = PLIST_STRING; + data->strval = (char *) malloc(sizeof(char) * (items_written + 1)); + memcpy(data->strval, tmpstr, items_written); + data->strval[items_written] = '\0'; + data->length = strlen(data->strval); + g_free(tmpstr); return g_node_new(data); } @@ -364,10 +381,6 @@ static gpointer copy_plist_data(gconstpointer src, gpointer data) case PLIST_STRING: dstdata->strval = strdup(srcdata->strval); break; - case PLIST_UNICODE: - dstdata->unicodeval = (gunichar2 *) malloc(srcdata->length * sizeof(gunichar2)); - memcpy(dstdata->unicodeval, srcdata->unicodeval, srcdata->length * sizeof(gunichar2)); - break; case PLIST_DATA: case PLIST_ARRAY: dstdata->buff = (uint8_t *) malloc(sizeof(uint8_t *) * srcdata->length); @@ -520,10 +533,6 @@ static guint plist_data_hash(gconstpointer key) buff = data->strval; size = strlen(buff); break; - case PLIST_UNICODE: - buff = (char *) data->unicodeval; - size = data->length; - break; case PLIST_DATA: case PLIST_ARRAY: case PLIST_DICT: @@ -752,6 +761,13 @@ void plist_to_bin(plist_t plist, char **plist_bin, uint32_t * length) uint64_t *offsets = NULL; uint8_t pad[6] = { 0, 0, 0, 0, 0, 0 }; uint8_t trailer[BPLIST_TRL_SIZE]; + //for string + glong len = 0; + int type = 0; + glong items_read = 0; + glong items_written = 0; + GError *error = NULL; + gunichar2 *unicodestr = NULL; //check for valid input if (!plist || !plist_bin || *plist_bin || !length) @@ -806,10 +822,16 @@ void plist_to_bin(plist_t plist, char **plist_bin, uint32_t * length) case PLIST_KEY: case PLIST_STRING: - write_string(bplist_buff, data->strval); - break; - case PLIST_UNICODE: - write_unicode(bplist_buff, data->unicodeval, data->length); + len = strlen(data->strval); + type = xmlDetectCharEncoding(data->strval, len); + if (XML_CHAR_ENCODING_UTF8 == type) { + unicodestr = g_utf8_to_utf16(data->strval, len, &items_read, &items_written, &error); + write_unicode(bplist_buff, unicodestr, items_written); + g_free(unicodestr); + } + else if (XML_CHAR_ENCODING_ASCII == type || XML_CHAR_ENCODING_NONE == type) { + write_string(bplist_buff, data->strval); + } break; case PLIST_DATA: 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) case PLIST_STRING: free(data->strval); break; - case PLIST_UNICODE: - free(data->unicodeval); - break; case PLIST_DATA: free(data->buff); break; @@ -90,10 +87,6 @@ static plist_t plist_add_sub_element(plist_t node, plist_type type, const void * //only structured types are allowed to have nulll value if (value || (!value && (type == PLIST_DICT || type == PLIST_ARRAY))) { - glong len = 0; - glong items_read = 0; - glong items_written = 0; - GError *error = NULL; plist_t subnode = NULL; //now handle value @@ -115,11 +108,6 @@ static plist_t plist_add_sub_element(plist_t node, plist_type type, const void * case PLIST_STRING: data->strval = strdup((char *) value); break; - case PLIST_UNICODE: - len = strlen((char *) value); - data->unicodeval = g_utf8_to_utf16((char *) value, len, &items_read, &items_written, &error); - data->length = items_written; - break; case PLIST_DATA: data->buff = (uint8_t*)malloc(length); memcpy(data->buff, value, length); @@ -210,9 +198,6 @@ static char compare_node_value(plist_type type, plist_data_t data, const void *v case PLIST_STRING: res = !strcmp(data->strval, ((char *) value)); break; - case PLIST_UNICODE: - res = !memcpy(data->unicodeval, value, length); - break; case PLIST_DATA: res = !memcmp(data->buff, (char *) value, length); break; @@ -262,11 +247,6 @@ plist_t plist_find_node_by_string(plist_t plist, const char *value) static void plist_get_type_and_value(plist_t node, plist_type * type, void *value, uint64_t * length) { - //for unicode - glong len = 0; - glong items_read = 0; - glong items_written = 0; - GError *error = NULL; plist_data_t data = NULL; if (!node) @@ -291,10 +271,6 @@ static void plist_get_type_and_value(plist_t node, plist_type * type, void *valu case PLIST_STRING: *((char **) value) = strdup(data->strval); break; - case PLIST_UNICODE: - len = data->length; - *((char **) value) = g_utf16_to_utf8(data->unicodeval, len, &items_read, &items_written, &error); - break; case PLIST_DATA: *((uint8_t **) value) = (uint8_t *) malloc(*length * sizeof(uint8_t)); 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) plist_add_sub_element(node, PLIST_DATA, val, length); } -void plist_add_sub_unicode_el(plist_t node, const char *val) -{ - plist_add_sub_element(node, PLIST_UNICODE, val, strlen(val)); -} - void plist_add_sub_date_el(plist_t node, int32_t sec, int32_t usec) { GTimeVal val = { sec, usec }; @@ -423,15 +394,6 @@ void plist_get_data_val(plist_t node, char **val, uint64_t * length) plist_get_type_and_value(node, &type, (void *) val, length); } -void plist_get_unicode_val(plist_t node, char **val) -{ - plist_type type = plist_get_node_type(node); - uint64_t length = 0; - if (PLIST_UNICODE == type) - plist_get_type_and_value(node, &type, (void *) val, &length); - assert(length == strlen(*val)); -} - void plist_get_date_val(plist_t node, int32_t * sec, int32_t * usec) { plist_type type = plist_get_node_type(node); @@ -476,11 +438,6 @@ gboolean plist_data_compare(gconstpointer a, gconstpointer b) return TRUE; else return FALSE; - case PLIST_UNICODE: - if (!memcmp(val_a->unicodeval, val_b->unicodeval, val_a->length)) - return TRUE; - else - return FALSE; case PLIST_DATA: 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 { uint64_t intval; double realval; char *strval; - gunichar2 *unicodeval; uint8_t *buff; GTimeVal timeval; }; 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) //for base64 gchar *valtmp = NULL; - //for unicode - glong len = 0; - glong items_read = 0; - glong items_written = 0; - GError *error = NULL; - uint32_t i = 0; if (!node) @@ -165,12 +159,6 @@ static void node_to_xml(GNode * node, gpointer xml_struct) val = g_strdup(node_data->strval); break; - case PLIST_UNICODE: - tag = XPLIST_STRING; - len = node_data->length; - val = g_utf16_to_utf8(node_data->unicodeval, len, &items_read, &items_written, &error); - break; - case PLIST_KEY: tag = XPLIST_KEY; val = g_strdup((gchar *) node_data->strval); @@ -233,11 +221,7 @@ static void xml_to_node(xmlNodePtr xml_node, plist_t * plist_node) plist_t subnode = NULL; //for string - unsigned char *tmp = NULL; glong len = 0; - glong items_read = 0; - glong items_written = 0; - GError *error = NULL; int type = 0; if (!xml_node) @@ -301,16 +285,11 @@ static void xml_to_node(xmlNodePtr xml_node, plist_t * plist_node) if (!xmlStrcmp(node->name, XPLIST_STRING)) { xmlChar *strval = xmlNodeGetContent(node); len = strlen((char *) strval); - items_read = 0; - items_written = 0; - error = NULL; type = xmlDetectCharEncoding(strval, len); - if (XML_CHAR_ENCODING_UTF8 == type) { - data->unicodeval = g_utf8_to_utf16((char *) strval, len, &items_read, &items_written, &error); - data->type = PLIST_UNICODE; - data->length = items_written; - } else if (XML_CHAR_ENCODING_ASCII == type || XML_CHAR_ENCODING_NONE == type) { + if (XML_CHAR_ENCODING_UTF8 == type || + XML_CHAR_ENCODING_ASCII == type || + XML_CHAR_ENCODING_NONE == type) { data->strval = strdup((char *) strval); data->type = PLIST_STRING; 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 { PLIST_UINT, PLIST_REAL, PLIST_STRING, - PLIST_UNICODE, PLIST_ARRAY, PLIST_DICT, PLIST_DATE, -- cgit v1.1-32-gdbae