@@ -95,6 +95,7 @@ static PHP_MINIT_FUNCTION(json)
95
95
REGISTER_LONG_CONSTANT ("JSON_NUMERIC_CHECK" , PHP_JSON_NUMERIC_CHECK , CONST_CS | CONST_PERSISTENT );
96
96
REGISTER_LONG_CONSTANT ("JSON_UNESCAPED_SLASHES" , PHP_JSON_UNESCAPED_SLASHES , CONST_CS | CONST_PERSISTENT );
97
97
REGISTER_LONG_CONSTANT ("JSON_PRETTY_PRINT" , PHP_JSON_PRETTY_PRINT , CONST_CS | CONST_PERSISTENT );
98
+ REGISTER_LONG_CONSTANT ("JSON_UNESCAPED_UNICODE" , PHP_JSON_UNESCAPED_UNICODE , CONST_CS | CONST_PERSISTENT );
98
99
99
100
REGISTER_LONG_CONSTANT ("JSON_ERROR_NONE" , PHP_JSON_ERROR_NONE , CONST_CS | CONST_PERSISTENT );
100
101
REGISTER_LONG_CONSTANT ("JSON_ERROR_DEPTH" , PHP_JSON_ERROR_DEPTH , CONST_CS | CONST_PERSISTENT );
@@ -346,7 +347,7 @@ static void json_encode_array(smart_str *buf, zval **val, int options TSRMLS_DC)
346
347
347
348
static void json_escape_string (smart_str * buf , char * s , int len , int options TSRMLS_DC ) /* {{{ */
348
349
{
349
- int pos = 0 ;
350
+ int pos = 0 , ulen = 0 ;
350
351
unsigned short us ;
351
352
unsigned short * utf16 ;
352
353
@@ -378,15 +379,14 @@ static void json_escape_string(smart_str *buf, char *s, int len, int options TSR
378
379
}
379
380
380
381
}
381
-
382
- utf16 = (unsigned short * ) safe_emalloc (len , sizeof (unsigned short ), 0 );
383
-
384
- len = utf8_to_utf16 (utf16 , s , len );
385
- if (len <= 0 ) {
382
+
383
+ utf16 = (options & PHP_JSON_UNESCAPED_UNICODE ) ? NULL : (unsigned short * ) safe_emalloc (len , sizeof (unsigned short ), 0 );
384
+ ulen = utf8_to_utf16 (utf16 , s , len );
385
+ if (ulen <= 0 ) {
386
386
if (utf16 ) {
387
387
efree (utf16 );
388
388
}
389
- if (len < 0 ) {
389
+ if (ulen < 0 ) {
390
390
JSON_G (error_code ) = PHP_JSON_ERROR_UTF8 ;
391
391
if (!PG (display_errors )) {
392
392
php_error_docref (NULL TSRMLS_CC , E_WARNING , "Invalid UTF-8 sequence in argument" );
@@ -397,12 +397,15 @@ static void json_escape_string(smart_str *buf, char *s, int len, int options TSR
397
397
}
398
398
return ;
399
399
}
400
+ if (!(options & PHP_JSON_UNESCAPED_UNICODE )) {
401
+ len = ulen ;
402
+ }
400
403
401
404
smart_str_appendc (buf , '"' );
402
405
403
406
while (pos < len )
404
407
{
405
- us = utf16 [pos ++ ];
408
+ us = ( options & PHP_JSON_UNESCAPED_UNICODE ) ? s [ pos ++ ] : utf16 [pos ++ ];
406
409
407
410
switch (us )
408
411
{
@@ -479,7 +482,7 @@ static void json_escape_string(smart_str *buf, char *s, int len, int options TSR
479
482
break ;
480
483
481
484
default :
482
- if (us >= ' ' && (us & 127 ) == us ) {
485
+ if (us >= ' ' && (( options & PHP_JSON_UNESCAPED_UNICODE ) || ( us & 127 ) == us ) ) {
483
486
smart_str_appendc (buf , (unsigned char ) us );
484
487
} else {
485
488
smart_str_appendl (buf , "\\u" , 2 );
@@ -498,7 +501,9 @@ static void json_escape_string(smart_str *buf, char *s, int len, int options TSR
498
501
}
499
502
500
503
smart_str_appendc (buf , '"' );
501
- efree (utf16 );
504
+ if (utf16 ) {
505
+ efree (utf16 );
506
+ }
502
507
}
503
508
/* }}} */
504
509
0 commit comments