diff options
author | Remi Collet <remi@php.net> | 2012-11-25 08:14:23 +0100 |
---|---|---|
committer | Remi Collet <remi@php.net> | 2012-11-25 08:14:23 +0100 |
commit | 289bb339c9e722ff3017ec58fdef7300f39b8e6d (patch) | |
tree | d4a123d1c318b8e1339a4cf2ed1d32564ef8f1e5 /ext/json/json.c | |
parent | 8d2c44b59dfc6c5c608121546edac5840def3f9a (diff) | |
download | php-git-289bb339c9e722ff3017ec58fdef7300f39b8e6d.tar.gz |
Fixed bug #63588 Duplicate implementation of php_next_utf8_char
Json use an utf8 parser from a third party library, switch to
our implementation of php_next_utf8_char.
This also helps on solving #63520. All the unit tests succeed.
Our implementation also seems a little faster.
json.dsp need to be regenerated.
Diffstat (limited to 'ext/json/json.c')
-rw-r--r-- | ext/json/json.c | 32 |
1 files changed, 29 insertions, 3 deletions
diff --git a/ext/json/json.c b/ext/json/json.c index 786b21ac91..270c7cb346 100644 --- a/ext/json/json.c +++ b/ext/json/json.c @@ -25,8 +25,8 @@ #include "php.h" #include "php_ini.h" #include "ext/standard/info.h" +#include "ext/standard/html.h" #include "ext/standard/php_smart_str.h" -#include "utf8_to_utf16.h" #include "JSON_parser.h" #include "php_json.h" #include <zend_exceptions.h> @@ -344,6 +344,32 @@ static void json_encode_array(smart_str *buf, zval **val, int options TSRMLS_DC) } /* }}} */ +static int json_utf8_to_utf16(unsigned short *utf16, char utf8[], int len) /* {{{ */ +{ + size_t pos = 0, us; + int j, status; + + for (j=0 ; pos < len ; j++) { + us = php_next_utf8_char((const unsigned char *)utf8, len, &pos, &status); + if (status != SUCCESS) { + return -1; + } + if (utf16) { + /* From http://en.wikipedia.org/wiki/UTF16 */ + if (us >= 0x10000) { + us -= 0x10000; + utf16[j++] = (unsigned short)((us >> 10) | 0xd800); + utf16[j] = (unsigned short)((us & 0x3ff) | 0xdc00); + } else { + utf16[j] = (unsigned short)us; + } + } + } + return j; +} +/* }}} */ + + #define REVERSE16(us) (((us & 0xf) << 12) | (((us >> 4) & 0xf) << 8) | (((us >> 8) & 0xf) << 4) | ((us >> 12) & 0xf)) static void json_escape_string(smart_str *buf, char *s, int len, int options TSRMLS_DC) /* {{{ */ @@ -383,7 +409,7 @@ static void json_escape_string(smart_str *buf, char *s, int len, int options TSR } utf16 = (options & PHP_JSON_UNESCAPED_UNICODE) ? NULL : (unsigned short *) safe_emalloc(len, sizeof(unsigned short), 0); - ulen = utf8_to_utf16(utf16, s, len); + ulen = json_utf8_to_utf16(utf16, s, len); if (ulen <= 0) { if (utf16) { efree(utf16); @@ -628,7 +654,7 @@ PHP_JSON_API void php_json_decode_ex(zval *return_value, char *str, int str_len, utf16 = (unsigned short *) safe_emalloc((str_len+1), sizeof(unsigned short), 1); - utf16_len = utf8_to_utf16(utf16, str, str_len); + utf16_len = json_utf8_to_utf16(utf16, str, str_len); if (utf16_len <= 0) { if (utf16) { efree(utf16); |