]> granicus.if.org Git - php/commitdiff
Fixed bug #63588 Duplicate implementation of php_next_utf8_char
authorRemi Collet <remi@php.net>
Sun, 25 Nov 2012 07:14:23 +0000 (08:14 +0100)
committerRemi Collet <remi@php.net>
Sun, 25 Nov 2012 07:14:23 +0000 (08:14 +0100)
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.

NEWS
ext/json/config.m4
ext/json/config.w32
ext/json/json.c
ext/json/utf8_to_utf16.c [deleted file]
ext/json/utf8_to_utf16.h [deleted file]

diff --git a/NEWS b/NEWS
index 4dde872f223d15c2e1cb7cfa39d6ce2fa3c6823d..d55c46794caefd604820c623020e149b5196f8ef 100644 (file)
--- a/NEWS
+++ b/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)
 
index b62e938421cc1f49a2d10cdb15d6b20523462e3c..26c43a0e3f0f41f16d9e9f31884516740534f38a 100644 (file)
@@ -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
index 06a48e05ed2e5b458f406a6991483c9ceb69dfec..cedbf428298a0beac40cb0592cf74f7e572de2e4 100644 (file)
@@ -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");
 }
 
index 786b21ac910242dddbd4ae84a40b9526338e40d9..270c7cb34682ed24816f05f9ec8adef1df41cb58 100644 (file)
@@ -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);
diff --git a/ext/json/utf8_to_utf16.c b/ext/json/utf8_to_utf16.c
deleted file mode 100644 (file)
index 508bc93..0000000
+++ /dev/null
@@ -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;
-        }
-    }
-}
diff --git a/ext/json/utf8_to_utf16.h b/ext/json/utf8_to_utf16.h
deleted file mode 100644 (file)
index 5c9685a..0000000
+++ /dev/null
@@ -1,3 +0,0 @@
-/* utf8_to_utf16.h */
-
-extern int utf8_to_utf16(unsigned short *w, char p[], int length);