summaryrefslogtreecommitdiff
path: root/ext/json/json.c
diff options
context:
space:
mode:
authorRemi Collet <remi@php.net>2012-11-26 13:05:37 +0100
committerRemi Collet <remi@php.net>2012-11-26 13:05:37 +0100
commit495ff0964d6c24f04f1d705334bb3a5ce49ea8cb (patch)
treea4c82af687db769c3a0af86a6d1bec34589b1cad /ext/json/json.c
parentdfa5ca2b8dbaf0e4400dce8bcb156821aec384ce (diff)
parent7751a6882485de64365661f4e7918535c3992982 (diff)
downloadphp-git-495ff0964d6c24f04f1d705334bb3a5ce49ea8cb.tar.gz
Merge branch 'PHP-5.4' into PHP-5.5
* PHP-5.4: Related bug #63588 fix length computation + optimize for speed
Diffstat (limited to 'ext/json/json.c')
-rw-r--r--ext/json/json.c39
1 files changed, 22 insertions, 17 deletions
diff --git a/ext/json/json.c b/ext/json/json.c
index 0393be3fc1..cf2b1edc00 100644
--- a/ext/json/json.c
+++ b/ext/json/json.c
@@ -363,12 +363,13 @@ 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) {
+ if (utf16) {
+ /* really convert the utf8 string */
+ for (j=0 ; pos < len ; j++) {
+ us = php_next_utf8_char((const unsigned char *)utf8, len, &pos, &status);
+ if (status != SUCCESS) {
+ return -1;
+ }
/* From http://en.wikipedia.org/wiki/UTF16 */
if (us >= 0x10000) {
us -= 0x10000;
@@ -378,14 +379,23 @@ static int json_utf8_to_utf16(unsigned short *utf16, char utf8[], int len) /* {{
utf16[j] = (unsigned short)us;
}
}
+ } else {
+ /* Only check if utf8 string is valid, and compute utf16 lenght */
+ for (j=0 ; pos < len ; j++) {
+ us = php_next_utf8_char((const unsigned char *)utf8, len, &pos, &status);
+ if (status != SUCCESS) {
+ return -1;
+ }
+ if (us >= 0x10000) {
+ j++;
+ }
+ }
}
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) /* {{{ */
{
int pos = 0, ulen = 0;
@@ -527,15 +537,10 @@ static void json_escape_string(smart_str *buf, char *s, int len, int options TSR
smart_str_appendc(buf, (unsigned char) us);
} else {
smart_str_appendl(buf, "\\u", 2);
- us = REVERSE16(us);
-
- smart_str_appendc(buf, digits[us & ((1 << 4) - 1)]);
- us >>= 4;
- smart_str_appendc(buf, digits[us & ((1 << 4) - 1)]);
- us >>= 4;
- smart_str_appendc(buf, digits[us & ((1 << 4) - 1)]);
- us >>= 4;
- smart_str_appendc(buf, digits[us & ((1 << 4) - 1)]);
+ smart_str_appendc(buf, digits[(us & 0xf000) >> 12]);
+ smart_str_appendc(buf, digits[(us & 0xf00) >> 8]);
+ smart_str_appendc(buf, digits[(us & 0xf0) >> 4]);
+ smart_str_appendc(buf, digits[(us & 0xf)]);
}
break;
}