Skip to content

Commit 289bb33

Browse files
committed
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.
1 parent 8d2c44b commit 289bb33

File tree

6 files changed

+35
-67
lines changed

6 files changed

+35
-67
lines changed

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ PHP NEWS
99
- Imap:
1010
. Fixed Bug #63126 DISABLE_AUTHENTICATOR ignores array (Remi)
1111

12+
- Json:
13+
. Fixed bug #63588 use php_next_utf8_char and remove duplicate
14+
implementation. (Remi)
15+
1216
- mysqli:
1317
. Fixed bug #63361 missing header. (Remi)
1418

ext/json/config.m4

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ if test "$PHP_JSON" != "no"; then
99
AC_DEFINE([HAVE_JSON],1 ,[whether to enable JavaScript Object Serialization support])
1010
AC_HEADER_STDC
1111

12-
PHP_NEW_EXTENSION(json, json.c utf8_to_utf16.c utf8_decode.c JSON_parser.c, $ext_shared)
12+
PHP_NEW_EXTENSION(json, json.c utf8_decode.c JSON_parser.c, $ext_shared)
1313
PHP_INSTALL_HEADERS([ext/json], [php_json.h])
1414
PHP_SUBST(JSON_SHARED_LIBADD)
1515
fi

ext/json/config.w32

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ ARG_ENABLE("json", "JavaScript Object Serialization support", "yes");
55

66
if (PHP_JSON != "no") {
77
EXTENSION('json', 'json.c', PHP_JSON_SHARED, "");
8-
ADD_SOURCES(configure_module_dirname, "JSON_parser.c utf8_decode.c utf8_to_utf16.c", "json");
8+
ADD_SOURCES(configure_module_dirname, "JSON_parser.c utf8_decode.c", "json");
99
PHP_INSTALL_HEADERS("ext/json/", "php_json.h");
1010
}
1111

ext/json/json.c

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
#include "php.h"
2626
#include "php_ini.h"
2727
#include "ext/standard/info.h"
28+
#include "ext/standard/html.h"
2829
#include "ext/standard/php_smart_str.h"
29-
#include "utf8_to_utf16.h"
3030
#include "JSON_parser.h"
3131
#include "php_json.h"
3232
#include <zend_exceptions.h>
@@ -344,6 +344,32 @@ static void json_encode_array(smart_str *buf, zval **val, int options TSRMLS_DC)
344344
}
345345
/* }}} */
346346

347+
static int json_utf8_to_utf16(unsigned short *utf16, char utf8[], int len) /* {{{ */
348+
{
349+
size_t pos = 0, us;
350+
int j, status;
351+
352+
for (j=0 ; pos < len ; j++) {
353+
us = php_next_utf8_char((const unsigned char *)utf8, len, &pos, &status);
354+
if (status != SUCCESS) {
355+
return -1;
356+
}
357+
if (utf16) {
358+
/* From http://en.wikipedia.org/wiki/UTF16 */
359+
if (us >= 0x10000) {
360+
us -= 0x10000;
361+
utf16[j++] = (unsigned short)((us >> 10) | 0xd800);
362+
utf16[j] = (unsigned short)((us & 0x3ff) | 0xdc00);
363+
} else {
364+
utf16[j] = (unsigned short)us;
365+
}
366+
}
367+
}
368+
return j;
369+
}
370+
/* }}} */
371+
372+
347373
#define REVERSE16(us) (((us & 0xf) << 12) | (((us >> 4) & 0xf) << 8) | (((us >> 8) & 0xf) << 4) | ((us >> 12) & 0xf))
348374

349375
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
383409
}
384410

385411
utf16 = (options & PHP_JSON_UNESCAPED_UNICODE) ? NULL : (unsigned short *) safe_emalloc(len, sizeof(unsigned short), 0);
386-
ulen = utf8_to_utf16(utf16, s, len);
412+
ulen = json_utf8_to_utf16(utf16, s, len);
387413
if (ulen <= 0) {
388414
if (utf16) {
389415
efree(utf16);
@@ -628,7 +654,7 @@ PHP_JSON_API void php_json_decode_ex(zval *return_value, char *str, int str_len,
628654

629655
utf16 = (unsigned short *) safe_emalloc((str_len+1), sizeof(unsigned short), 1);
630656

631-
utf16_len = utf8_to_utf16(utf16, str, str_len);
657+
utf16_len = json_utf8_to_utf16(utf16, str, str_len);
632658
if (utf16_len <= 0) {
633659
if (utf16) {
634660
efree(utf16);

ext/json/utf8_to_utf16.c

Lines changed: 0 additions & 59 deletions
This file was deleted.

ext/json/utf8_to_utf16.h

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)