php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Return to Bug #53946
Patch json_unescaped_unicode revision 2011-08-29 14:04 UTC by gwynne@php.net
revision 2011-08-29 12:57 UTC by gwynne@php.net
revision 2011-07-19 16:01 UTC by irker@php.net
Patch json_encode_doc.patch revision 2011-08-29 09:01 UTC by irker@php.net
revision 2011-08-29 08:59 UTC by irker@php.net
Patch bug53946.phpt revision 2011-07-25 06:18 UTC by irker@php.net
revision 2011-07-19 16:02 UTC by irker@php.net
Patch json_unescaped_unicode.patch revision 2011-07-19 16:02 UTC by irker@php.net

Patch json_unescaped_unicode for JSON related Bug #53946

Patch version 2011-08-29 12:57 UTC

Return to Bug #53946 | Download this patch
This patch is obsolete

Obsoleted by patches:

This patch renders other patches obsolete

Obsolete patches:

Patch Revisions: 2011-08-29 14:04 UTC | 2011-08-29 12:57 UTC | 2011-07-19 16:01 UTC

Developer: gwynne@php.net



  +	REGISTER_LONG_CONSTANT("JSON_UNESCAPED_UNICODE", PHP_JSON_UNESCAPED_UNICODE, CONST_CS | CONST_PERSISTENT);
   
   	REGISTER_LONG_CONSTANT("JSON_ERROR_NONE", PHP_JSON_ERROR_NONE, CONST_CS | CONST_PERSISTENT);
   	REGISTER_LONG_CONSTANT("JSON_ERROR_DEPTH", PHP_JSON_ERROR_DEPTH, CONST_CS | CONST_PERSISTENT);
 @@ -346,7 +347,7 @@
  
  static void json_escape_string(smart_str *buf, char *s, int len, int options TSRMLS_DC) /* {{{ */
  {
 -	int pos = 0;
 +	int pos = 0, ulen = 0;
  	unsigned short us;
  	unsigned short *utf16;
  
 @@ -378,15 +379,14 @@
 @@ -378,31 +379,32 @@
   		}
   		
   	}
  -
  -	utf16 = (unsigned short *) safe_emalloc(len, sizeof(unsigned short), 0);
  -
  -	len = utf8_to_utf16(utf16, s, len);
  -	if (len <= 0) {
 -		if (utf16) {
 -			efree(utf16);
 -		}
 -		if (len < 0) {
 -			JSON_G(error_code) = PHP_JSON_ERROR_UTF8;
 -			if (!PG(display_errors)) {
 -				php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid UTF-8 sequence in argument");
  +	
 +	utf16 = (options & PHP_JSON_UNESCAPED_UNICODE) ? NULL : (unsigned short *) safe_emalloc(len, sizeof(unsigned short), 0);
 +	ulen = utf8_to_utf16(utf16, s, len);
 +	if (ulen <= 0) {
  		if (utf16) {
  			efree(utf16);
 +	if (!(options & PHP_JSON_UNESCAPED_UNICODE)) {
 +		utf16 = (unsigned short *) safe_emalloc(len, sizeof(unsigned short), 0);
 +		len = utf8_to_utf16(utf16, s, len);
 +		if (len <= 0) {
 +			if (utf16) {
 +				efree(utf16);
  			}
 -			smart_str_appendl(buf, "null", 4);
 -		} else {
 -			smart_str_appendl(buf, "\"\"", 2);
 +			if (len < 0) {
 +				JSON_G(error_code) = PHP_JSON_ERROR_UTF8;
 +				if (!PG(display_errors)) {
 +					php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid UTF-8 sequence in argument");
 +				}
 +				smart_str_appendl(buf, "null", 4);
 +			} else {
 +				smart_str_appendl(buf, "\"\"", 2);
 +			}
 +			return;
   		}
 -		if (len < 0) {
 +		if (ulen < 0) {
  			JSON_G(error_code) = PHP_JSON_ERROR_UTF8;
  			if (!PG(display_errors)) {
  				php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid UTF-8 sequence in argument");
 @@ -397,12 +397,15 @@
  		}
  		return;
 -		return;
   	}
 +	if (!(options & PHP_JSON_UNESCAPED_UNICODE)) {
 +		len = ulen;
 +	}
   
   	smart_str_appendc(buf, '"');
   
   	while (pos < len)
Line 56 (now 59), was 73 lines, now 28 lines

  +		us = (options & PHP_JSON_UNESCAPED_UNICODE) ? s[pos++] : utf16[pos++];
   
   		switch (us)
   		{
 @@ -479,7 +482,7 @@
 @@ -479,7 +481,7 @@
   				break;
   
   			default:
  -				if (us >= ' ' && (us & 127) == us) {
  +				if (us >= ' ' && ((options & PHP_JSON_UNESCAPED_UNICODE) || (us & 127) == us)) {
   					smart_str_appendc(buf, (unsigned char) us);
   				} else {
   					smart_str_appendl(buf, "\\u", 2);
 @@ -498,7 +501,9 @@
 @@ -498,7 +500,9 @@
   	}
   
   	smart_str_appendc(buf, '"');
  -	efree(utf16);
 +	if (utf16) {
 +	if (!(options & PHP_JSON_UNESCAPED_UNICODE)) {
  +		efree(utf16);
  +	}
   }
   /* }}} */
   
 Index: ext/json/utf8_to_utf16.c
 ===================================================================
 --- ext/json/utf8_to_utf16.c	(revision 315701)
 +++ ext/json/utf8_to_utf16.c	(working copy)
 @@ -30,7 +30,7 @@
  #include "utf8_decode.h"
  
  int 
 -utf8_to_utf16(unsigned short w[], char p[], int length) 
 +utf8_to_utf16(unsigned short *w, char p[], int length) 
  {
      int c;
      int the_index = 0;
 @@ -43,14 +43,17 @@
              return (c == UTF8_END) ? the_index : UTF8_ERROR;
          }
          if (c < 0x10000) {
 -            w[the_index] = (unsigned short)c;
 +            if (w) {
 +                w[the_index] = (unsigned short)c;
 +            }
              the_index += 1;
          } else {
              c -= 0x10000;
 -            w[the_index] = (unsigned short)(0xD800 | (c >> 10));
 -            the_index += 1;
 -            w[the_index] = (unsigned short)(0xDC00 | (c & 0x3FF));
 -            the_index += 1;
 +            if (w) {
 +                w[the_index] = (unsigned short)(0xD800 | (c >> 10));
 +                w[the_index + 1] = (unsigned short)(0xDC00 | (c & 0x3FF));
 +            }
 +            the_index += 2;
          }
      }
  }
 Index: ext/json/utf8_to_utf16.h
 ===================================================================
 --- ext/json/utf8_to_utf16.h	(revision 315701)
 +++ ext/json/utf8_to_utf16.h	(working copy)
 @@ -1,3 +1,3 @@
  /* utf8_to_utf16.h */
  
 -extern int utf8_to_utf16(unsigned short w[], char p[], int length);
 +extern int utf8_to_utf16(unsigned short *w, char p[], int length);
  Index: ext/json/php_json.h
  ===================================================================
  --- ext/json/php_json.h	(revision 315701)
  +++ ext/json/php_json.h	(working copy)
 
PHP Copyright © 2001-2014 The PHP Group
All rights reserved.
Last updated: Wed Apr 23 14:02:33 2014 UTC