@@ -252,7 +252,7 @@ static int php_json_escape_string(
252
252
{
253
253
int status ;
254
254
unsigned int us ;
255
- size_t prev_pos , pos , checkpoint ;
255
+ size_t pos , checkpoint ;
256
256
257
257
if (len == 0 ) {
258
258
smart_str_appendl (buf , "\"\"" , 2 );
@@ -283,27 +283,31 @@ static int php_json_escape_string(
283
283
smart_str_appendc (buf , '"' );
284
284
285
285
do {
286
- prev_pos = pos ;
287
- us = php_next_utf8_char ((unsigned char * )s , len , & pos , & status );
288
- /* check whether UTF8 character is correct */
289
- if (status != SUCCESS ) {
290
- if (buf -> s ) {
291
- ZSTR_LEN (buf -> s ) = checkpoint ;
292
- }
293
- encoder -> error_code = PHP_JSON_ERROR_UTF8 ;
294
- if (options & PHP_JSON_PARTIAL_OUTPUT_ON_ERROR ) {
295
- smart_str_appendl (buf , "null" , 4 );
286
+ us = (unsigned char )s [pos ];
287
+ if (us >= 0x80 ) {
288
+ size_t prev_pos = pos ;
289
+
290
+ us = php_next_utf8_char ((unsigned char * )s , len , & pos , & status );
291
+
292
+ /* check whether UTF8 character is correct */
293
+ if (status != SUCCESS ) {
294
+ if (buf -> s ) {
295
+ ZSTR_LEN (buf -> s ) = checkpoint ;
296
+ }
297
+ encoder -> error_code = PHP_JSON_ERROR_UTF8 ;
298
+ if (options & PHP_JSON_PARTIAL_OUTPUT_ON_ERROR ) {
299
+ smart_str_appendl (buf , "null" , 4 );
300
+ }
301
+ return FAILURE ;
296
302
}
297
- return FAILURE ;
298
- }
299
- if (us >= 0x80 && (!(options & PHP_JSON_UNESCAPED_UNICODE ) || (unsigned char )s [prev_pos ] == 0xE2 )) {
303
+
300
304
/* Escape U+2028/U+2029 line terminators, UNLESS both
301
305
JSON_UNESCAPED_UNICODE and
302
306
JSON_UNESCAPED_LINE_TERMINATORS were provided */
303
307
if ((options & PHP_JSON_UNESCAPED_UNICODE )
304
- && ((options & PHP_JSON_UNESCAPED_LINE_TERMINATORS )
308
+ && ((options & PHP_JSON_UNESCAPED_LINE_TERMINATORS )
305
309
|| us < 0x2028 || us > 0x2029 )) {
306
- smart_str_appendl (buf , & s [ prev_pos ], 3 );
310
+ smart_str_appendl (buf , s + prev_pos , pos - prev_pos );
307
311
continue ;
308
312
}
309
313
/* From http://en.wikipedia.org/wiki/UTF16 */
@@ -325,6 +329,8 @@ static int php_json_escape_string(
325
329
smart_str_appendc (buf , digits [(us & 0xf0 ) >> 4 ]);
326
330
smart_str_appendc (buf , digits [(us & 0xf )]);
327
331
} else {
332
+ pos ++ ;
333
+
328
334
switch (us ) {
329
335
case '"' :
330
336
if (options & PHP_JSON_HEX_QUOT ) {
@@ -400,7 +406,7 @@ static int php_json_escape_string(
400
406
401
407
default :
402
408
if (us >= ' ' ) {
403
- smart_str_appendl (buf , s + prev_pos , pos - prev_pos );
409
+ smart_str_appendc (buf , ( unsigned char ) us );
404
410
} else {
405
411
smart_str_appendl (buf , "\\u00" , sizeof ("\\u00" )- 1 );
406
412
smart_str_appendc (buf , digits [(us & 0xf0 ) >> 4 ]);
0 commit comments