]> granicus.if.org Git - php/commitdiff
Fixed bug #41067 (json_encode() problem with UTF-16 input).
authorIlia Alshanetsky <iliaa@php.net>
Mon, 16 Apr 2007 22:31:05 +0000 (22:31 +0000)
committerIlia Alshanetsky <iliaa@php.net>
Mon, 16 Apr 2007 22:31:05 +0000 (22:31 +0000)
NEWS
ext/json/JSON_parser.c
ext/json/tests/bug41067.phpt [new file with mode: 0644]

diff --git a/NEWS b/NEWS
index 399147ab9b9e5a52e76add8f6b258b5956554fb9..13b831bd0009883e9545c5cd2be22530121178f1 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -1,10 +1,13 @@
 PHP                                                                        NEWS
 |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
 ?? Apr 2007, PHP 5.2.2RC2
+- Fixed bug #41093 (magic_quotes_gpc ignores first arrays keys). (Arpad, Ilia)
 - Fixed bug #41083 (mysql_ping() requires MYSQL_OPT_RECONNECT to be set since 
   MySQL 5.0.13). (xiaojb at gmail dot com, Tony)
 - Fixed bug #41075 (memleak when creating default object caused exception). 
   (Dmitry)
+- Fixed bug #41067 (json_encode() problem with UTF-16 input). (jp at df5ea
+  dot net. Ilia)
 - Fixed bug #41063 (chdir doesn't like root paths). (Dmitry)
 - Fixed bug #41061 ("visibility error" in ReflectionFunction::export()).
   (Johannes)
index 0fbdb1add1d22d27da7d017ebd5e31961139f61b..b57d65174eb234f3c003474c200c0a9946b81e0b 100644 (file)
@@ -316,6 +316,25 @@ static void utf16_to_utf8(smart_str *buf, unsigned short utf16)
         smart_str_appendc(buf, 0xc0 | (utf16 >> 6));
         smart_str_appendc(buf, 0x80 | (utf16 & 0x3f));
     }
+    else if ((utf16 & 0xfc00) == 0xdc00
+                && buf->len >= 3
+                && ((unsigned char) buf->c[buf->len - 3]) == 0xed
+                && ((unsigned char) buf->c[buf->len - 2] & 0xf0) == 0xa0
+                && ((unsigned char) buf->c[buf->len - 1] & 0xc0) == 0x80)
+    {
+        /* found surrogate pair */
+        unsigned long utf32;
+
+        utf32 = (((buf->c[buf->len - 2] & 0xf) << 16)
+                    | ((buf->c[buf->len - 1] & 0x3f) << 10)
+                    | (utf16 & 0x3ff)) + 0x10000;
+        buf->len -= 3;
+
+        smart_str_appendc(buf, 0xf0 | (utf32 >> 18));
+        smart_str_appendc(buf, 0x80 | ((utf32 >> 12) & 0x3f));
+        smart_str_appendc(buf, 0x80 | ((utf32 >> 6) & 0x3f));
+        smart_str_appendc(buf, 0x80 | (utf32 & 0x3f));
+    }
     else
     {
         smart_str_appendc(buf, 0xe0 | (utf16 >> 12));
diff --git a/ext/json/tests/bug41067.phpt b/ext/json/tests/bug41067.phpt
new file mode 100644 (file)
index 0000000..b20e6e1
--- /dev/null
@@ -0,0 +1,23 @@
+--TEST--
+Bug #41067 (json_encode() problem with UTF-16 input)
+--SKIPIF--
+<?php if (!extension_loaded("json")) print "skip"; ?>
+--FILE--
+<?php
+$single_barline = "\360\235\204\200";
+$array = array($single_barline);
+print bin2hex($single_barline) . "\n";
+// print $single_barline . "\n\n";
+$json = json_encode($array);
+print $json . "\n\n";
+$json_decoded = json_decode($json, true);
+// print $json_decoded[0] . "\n";
+print bin2hex($json_decoded[0]) . "\n";
+print "END\n";
+?>
+--EXPECT--
+f09d8480
+["\ud834\udd00"]
+
+f09d8480
+END