Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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.
  • Loading branch information...
commit 289bb339c9e722ff3017ec58fdef7300f39b8e6d 1 parent 8d2c44b
@remicollet remicollet authored
View
4 NEWS
@@ -9,6 +9,10 @@ PHP NEWS
- Imap:
. Fixed Bug #63126 DISABLE_AUTHENTICATOR ignores array (Remi)
+- Json:
+ . Fixed bug #63588 use php_next_utf8_char and remove duplicate
+ implementation. (Remi)
+
- mysqli:
. Fixed bug #63361 missing header. (Remi)
View
2  ext/json/config.m4
@@ -9,7 +9,7 @@ if test "$PHP_JSON" != "no"; then
AC_DEFINE([HAVE_JSON],1 ,[whether to enable JavaScript Object Serialization support])
AC_HEADER_STDC
- PHP_NEW_EXTENSION(json, json.c utf8_to_utf16.c utf8_decode.c JSON_parser.c, $ext_shared)
+ PHP_NEW_EXTENSION(json, json.c utf8_decode.c JSON_parser.c, $ext_shared)
PHP_INSTALL_HEADERS([ext/json], [php_json.h])
PHP_SUBST(JSON_SHARED_LIBADD)
fi
View
2  ext/json/config.w32
@@ -5,7 +5,7 @@ ARG_ENABLE("json", "JavaScript Object Serialization support", "yes");
if (PHP_JSON != "no") {
EXTENSION('json', 'json.c', PHP_JSON_SHARED, "");
- ADD_SOURCES(configure_module_dirname, "JSON_parser.c utf8_decode.c utf8_to_utf16.c", "json");
+ ADD_SOURCES(configure_module_dirname, "JSON_parser.c utf8_decode.c", "json");
PHP_INSTALL_HEADERS("ext/json/", "php_json.h");
}
View
32 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);
View
59 ext/json/utf8_to_utf16.c
@@ -1,59 +0,0 @@
-/* utf8_to_utf16.c */
-
-/* 2005-12-25 */
-
-/*
-Copyright (c) 2005 JSON.org
-
-Permission is hereby granted, free of charge, to any person obtaining a copy
-of this software and associated documentation files (the "Software"), to deal
-in the Software without restriction, including without limitation the rights
-to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-copies of the Software, and to permit persons to whom the Software is
-furnished to do so, subject to the following conditions:
-
-The above copyright notice and this permission notice shall be included in all
-copies or substantial portions of the Software.
-
-The Software shall be used for Good, not Evil.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-SOFTWARE.
-*/
-
-#include "utf8_to_utf16.h"
-#include "utf8_decode.h"
-
-int
-utf8_to_utf16(unsigned short *w, char p[], int length)
-{
- int c;
- int the_index = 0;
- json_utf8_decode utf8;
-
- utf8_decode_init(&utf8, p, length);
- for (;;) {
- c = utf8_decode_next(&utf8);
- if (c < 0) {
- return (c == UTF8_END) ? the_index : UTF8_ERROR;
- }
- if (c < 0x10000) {
- if (w) {
- w[the_index] = (unsigned short)c;
- }
- the_index += 1;
- } else {
- c -= 0x10000;
- if (w) {
- w[the_index] = (unsigned short)(0xD800 | (c >> 10));
- w[the_index + 1] = (unsigned short)(0xDC00 | (c & 0x3FF));
- }
- the_index += 2;
- }
- }
-}
View
3  ext/json/utf8_to_utf16.h
@@ -1,3 +0,0 @@
-/* utf8_to_utf16.h */
-
-extern int utf8_to_utf16(unsigned short *w, char p[], int length);

0 comments on commit 289bb33

Please sign in to comment.
Something went wrong with that request. Please try again.