diff options
| author | 2010-07-29 18:40:41 +0200 | |
|---|---|---|
| committer | 2010-07-29 18:40:41 +0200 | |
| commit | bd6ce8830f699f890e20faa4c16e24106ee59506 (patch) | |
| tree | cfacd89e8aa74ac90a73139769fbfcad1f2a661c /src | |
| parent | a1ced5fb61b8383038930ac7d6b3b19884e7721f (diff) | |
| download | libplist-bd6ce8830f699f890e20faa4c16e24106ee59506.tar.gz libplist-bd6ce8830f699f890e20faa4c16e24106ee59506.tar.bz2 | |
Fix unicode binary writing.
Diffstat (limited to 'src')
| -rw-r--r-- | src/bplist.c | 30 |
1 files changed, 23 insertions, 7 deletions
diff --git a/src/bplist.c b/src/bplist.c index 0e248f4..794d04f 100644 --- a/src/bplist.c +++ b/src/bplist.c | |||
| @@ -25,6 +25,7 @@ | |||
| 25 | #include <string.h> | 25 | #include <string.h> |
| 26 | 26 | ||
| 27 | #include <libxml/encoding.h> | 27 | #include <libxml/encoding.h> |
| 28 | #include <ctype.h> | ||
| 28 | 29 | ||
| 29 | #include <plist/plist.h> | 30 | #include <plist/plist.h> |
| 30 | #include "plist.h" | 31 | #include "plist.h" |
| @@ -732,6 +733,8 @@ static void write_raw_data(GByteArray * bplist, uint8_t mark, uint8_t * val, uin | |||
| 732 | g_byte_array_append(bplist, int_buff->data, int_buff->len); | 733 | g_byte_array_append(bplist, int_buff->data, int_buff->len); |
| 733 | g_byte_array_free(int_buff, TRUE); | 734 | g_byte_array_free(int_buff, TRUE); |
| 734 | } | 735 | } |
| 736 | //stupid unicode buffer length | ||
| 737 | if (BPLIST_UNICODE==mark) size *= 2; | ||
| 735 | buff = (uint8_t *) malloc(size); | 738 | buff = (uint8_t *) malloc(size); |
| 736 | memcpy(buff, val, size); | 739 | memcpy(buff, val, size); |
| 737 | g_byte_array_append(bplist, buff, size); | 740 | g_byte_array_append(bplist, buff, size); |
| @@ -757,7 +760,7 @@ static void write_unicode(GByteArray * bplist, gunichar2 * val, uint64_t size) | |||
| 757 | memcpy(buff, val, size2); | 760 | memcpy(buff, val, size2); |
| 758 | for (i = 0; i < size; i++) | 761 | for (i = 0; i < size; i++) |
| 759 | byte_convert(buff + i * sizeof(gunichar2), sizeof(gunichar2)); | 762 | byte_convert(buff + i * sizeof(gunichar2), sizeof(gunichar2)); |
| 760 | write_raw_data(bplist, BPLIST_STRING, buff, size2); | 763 | write_raw_data(bplist, BPLIST_UNICODE, buff, size); |
| 761 | } | 764 | } |
| 762 | 765 | ||
| 763 | static void write_array(GByteArray * bplist, GNode * node, GHashTable * ref_table, uint8_t dict_param_size) | 766 | static void write_array(GByteArray * bplist, GNode * node, GHashTable * ref_table, uint8_t dict_param_size) |
| @@ -842,6 +845,20 @@ static void write_dict(GByteArray * bplist, GNode * node, GHashTable * ref_table | |||
| 842 | 845 | ||
| 843 | } | 846 | } |
| 844 | 847 | ||
| 848 | static int is_ascii_string(char* s, int len) | ||
| 849 | { | ||
| 850 | int ret = 1, i = 0; | ||
| 851 | for(i = 0; i < len; i++) | ||
| 852 | { | ||
| 853 | if ( !isascii( s[i] ) ) | ||
| 854 | { | ||
| 855 | ret = 0; | ||
| 856 | break; | ||
| 857 | } | ||
| 858 | } | ||
| 859 | return ret; | ||
| 860 | } | ||
| 861 | |||
| 845 | void plist_to_bin(plist_t plist, char **plist_bin, uint32_t * length) | 862 | void plist_to_bin(plist_t plist, char **plist_bin, uint32_t * length) |
| 846 | { | 863 | { |
| 847 | GPtrArray *objects = NULL; | 864 | GPtrArray *objects = NULL; |
| @@ -922,17 +939,16 @@ void plist_to_bin(plist_t plist, char **plist_bin, uint32_t * length) | |||
| 922 | case PLIST_KEY: | 939 | case PLIST_KEY: |
| 923 | case PLIST_STRING: | 940 | case PLIST_STRING: |
| 924 | len = strlen(data->strval); | 941 | len = strlen(data->strval); |
| 925 | type = xmlDetectCharEncoding((const unsigned char *)data->strval, len); | 942 | if ( is_ascii_string(data->strval, len) ) |
| 926 | if (XML_CHAR_ENCODING_UTF8 == type) | 943 | { |
| 944 | write_string(bplist_buff, data->strval); | ||
| 945 | } | ||
| 946 | else | ||
| 927 | { | 947 | { |
| 928 | unicodestr = g_utf8_to_utf16(data->strval, len, &items_read, &items_written, &error); | 948 | unicodestr = g_utf8_to_utf16(data->strval, len, &items_read, &items_written, &error); |
| 929 | write_unicode(bplist_buff, unicodestr, items_written); | 949 | write_unicode(bplist_buff, unicodestr, items_written); |
| 930 | g_free(unicodestr); | 950 | g_free(unicodestr); |
| 931 | } | 951 | } |
| 932 | else if (XML_CHAR_ENCODING_ASCII == type || XML_CHAR_ENCODING_NONE == type) | ||
| 933 | { | ||
| 934 | write_string(bplist_buff, data->strval); | ||
| 935 | } | ||
| 936 | break; | 952 | break; |
| 937 | case PLIST_DATA: | 953 | case PLIST_DATA: |
| 938 | write_data(bplist_buff, data->buff, data->length); | 954 | write_data(bplist_buff, data->buff, data->length); |
