xref: /PHP-7.3/ext/json/json_encoder.c (revision 4831e150)
1 /*
2   +----------------------------------------------------------------------+
3   | PHP Version 7                                                        |
4   +----------------------------------------------------------------------+
5   | Copyright (c) 1997-2018 The PHP Group                                |
6   +----------------------------------------------------------------------+
7   | This source file is subject to version 3.01 of the PHP license,      |
8   | that is bundled with this package in the file LICENSE, and is        |
9   | available through the world-wide-web at the following url:           |
10   | http://www.php.net/license/3_01.txt                                  |
11   | If you did not receive a copy of the PHP license and are unable to   |
12   | obtain it through the world-wide-web, please send a note to          |
13   | license@php.net so we can mail you a copy immediately.               |
14   +----------------------------------------------------------------------+
15   | Author: Omar Kilani <omar@php.net>                                   |
16   |         Jakub Zelenka <bukka@php.net>                                |
17   +----------------------------------------------------------------------+
18 */
19 
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23 
24 #include "php.h"
25 #include "php_ini.h"
26 #include "ext/standard/info.h"
27 #include "ext/standard/html.h"
28 #include "zend_smart_str.h"
29 #include "php_json.h"
30 #include "php_json_encoder.h"
31 #include <zend_exceptions.h>
32 
33 static const char digits[] = "0123456789abcdef";
34 
35 static int php_json_escape_string(
36 		smart_str *buf,	const char *s, size_t len,
37 		int options, php_json_encoder *encoder);
38 
php_json_determine_array_type(zval * val)39 static int php_json_determine_array_type(zval *val) /* {{{ */
40 {
41 	int i;
42 	HashTable *myht = Z_ARRVAL_P(val);
43 
44 	i = myht ? zend_hash_num_elements(myht) : 0;
45 	if (i > 0) {
46 		zend_string *key;
47 		zend_ulong index, idx;
48 
49 		if (HT_IS_PACKED(myht) && HT_IS_WITHOUT_HOLES(myht)) {
50 			return PHP_JSON_OUTPUT_ARRAY;
51 		}
52 
53 		idx = 0;
54 		ZEND_HASH_FOREACH_KEY(myht, index, key) {
55 			if (key) {
56 				return PHP_JSON_OUTPUT_OBJECT;
57 			} else {
58 				if (index != idx) {
59 					return PHP_JSON_OUTPUT_OBJECT;
60 				}
61 			}
62 			idx++;
63 		} ZEND_HASH_FOREACH_END();
64 	}
65 
66 	return PHP_JSON_OUTPUT_ARRAY;
67 }
68 /* }}} */
69 
70 /* {{{ Pretty printing support functions */
71 
php_json_pretty_print_char(smart_str * buf,int options,char c)72 static inline void php_json_pretty_print_char(smart_str *buf, int options, char c) /* {{{ */
73 {
74 	if (options & PHP_JSON_PRETTY_PRINT) {
75 		smart_str_appendc(buf, c);
76 	}
77 }
78 /* }}} */
79 
php_json_pretty_print_indent(smart_str * buf,int options,php_json_encoder * encoder)80 static inline void php_json_pretty_print_indent(smart_str *buf, int options, php_json_encoder *encoder) /* {{{ */
81 {
82 	int i;
83 
84 	if (options & PHP_JSON_PRETTY_PRINT) {
85 		for (i = 0; i < encoder->depth; ++i) {
86 			smart_str_appendl(buf, "    ", 4);
87 		}
88 	}
89 }
90 /* }}} */
91 
92 /* }}} */
93 
php_json_is_valid_double(double d)94 static inline int php_json_is_valid_double(double d) /* {{{ */
95 {
96 	return !zend_isinf(d) && !zend_isnan(d);
97 }
98 /* }}} */
99 
php_json_encode_double(smart_str * buf,double d,int options)100 static inline void php_json_encode_double(smart_str *buf, double d, int options) /* {{{ */
101 {
102 	size_t len;
103 	char num[PHP_DOUBLE_MAX_LENGTH];
104 
105 	php_gcvt(d, (int)PG(serialize_precision), '.', 'e', num);
106 	len = strlen(num);
107 	if (options & PHP_JSON_PRESERVE_ZERO_FRACTION && strchr(num, '.') == NULL && len < PHP_DOUBLE_MAX_LENGTH - 2) {
108 		num[len++] = '.';
109 		num[len++] = '0';
110 		num[len] = '\0';
111 	}
112 	smart_str_appendl(buf, num, len);
113 }
114 /* }}} */
115 
116 #define PHP_JSON_HASH_PROTECT_RECURSION(_tmp_ht) \
117 	do { \
118 		if (_tmp_ht && !(GC_FLAGS(_tmp_ht) & GC_IMMUTABLE)) { \
119 			GC_PROTECT_RECURSION(_tmp_ht); \
120 		} \
121 	} while (0)
122 
123 #define PHP_JSON_HASH_UNPROTECT_RECURSION(_tmp_ht) \
124 	do { \
125 		if (_tmp_ht && !(GC_FLAGS(_tmp_ht) & GC_IMMUTABLE)) { \
126 			GC_UNPROTECT_RECURSION(_tmp_ht); \
127 		} \
128 	} while (0)
129 
php_json_encode_array(smart_str * buf,zval * val,int options,php_json_encoder * encoder)130 static int php_json_encode_array(smart_str *buf, zval *val, int options, php_json_encoder *encoder) /* {{{ */
131 {
132 	int i, r, need_comma = 0;
133 	HashTable *myht;
134 
135 	if (Z_TYPE_P(val) == IS_ARRAY) {
136 		myht = Z_ARRVAL_P(val);
137 		r = (options & PHP_JSON_FORCE_OBJECT) ? PHP_JSON_OUTPUT_OBJECT : php_json_determine_array_type(val);
138 	} else {
139 		myht = Z_OBJPROP_P(val);
140 		r = PHP_JSON_OUTPUT_OBJECT;
141 	}
142 
143 	if (myht && GC_IS_RECURSIVE(myht)) {
144 		encoder->error_code = PHP_JSON_ERROR_RECURSION;
145 		smart_str_appendl(buf, "null", 4);
146 		return FAILURE;
147 	}
148 
149 	PHP_JSON_HASH_PROTECT_RECURSION(myht);
150 
151 	if (r == PHP_JSON_OUTPUT_ARRAY) {
152 		smart_str_appendc(buf, '[');
153 	} else {
154 		smart_str_appendc(buf, '{');
155 	}
156 
157 	++encoder->depth;
158 
159 	i = myht ? zend_hash_num_elements(myht) : 0;
160 
161 	if (i > 0) {
162 		zend_string *key;
163 		zval *data;
164 		zend_ulong index;
165 
166 		ZEND_HASH_FOREACH_KEY_VAL_IND(myht, index, key, data) {
167 			if (r == PHP_JSON_OUTPUT_ARRAY) {
168 				if (need_comma) {
169 					smart_str_appendc(buf, ',');
170 				} else {
171 					need_comma = 1;
172 				}
173 
174 				php_json_pretty_print_char(buf, options, '\n');
175 				php_json_pretty_print_indent(buf, options, encoder);
176 			} else if (r == PHP_JSON_OUTPUT_OBJECT) {
177 				if (key) {
178 					if (ZSTR_VAL(key)[0] == '\0' && ZSTR_LEN(key) > 0 && Z_TYPE_P(val) == IS_OBJECT) {
179 						/* Skip protected and private members. */
180 						continue;
181 					}
182 
183 					if (need_comma) {
184 						smart_str_appendc(buf, ',');
185 					} else {
186 						need_comma = 1;
187 					}
188 
189 					php_json_pretty_print_char(buf, options, '\n');
190 					php_json_pretty_print_indent(buf, options, encoder);
191 
192 					if (php_json_escape_string(buf, ZSTR_VAL(key), ZSTR_LEN(key),
193 								options & ~PHP_JSON_NUMERIC_CHECK, encoder) == FAILURE &&
194 							(options & PHP_JSON_PARTIAL_OUTPUT_ON_ERROR) &&
195 							buf->s) {
196 						ZSTR_LEN(buf->s) -= 4;
197 						smart_str_appendl(buf, "\"\"", 2);
198 					}
199 				} else {
200 					if (need_comma) {
201 						smart_str_appendc(buf, ',');
202 					} else {
203 						need_comma = 1;
204 					}
205 
206 					php_json_pretty_print_char(buf, options, '\n');
207 					php_json_pretty_print_indent(buf, options, encoder);
208 
209 					smart_str_appendc(buf, '"');
210 					smart_str_append_long(buf, (zend_long) index);
211 					smart_str_appendc(buf, '"');
212 				}
213 
214 				smart_str_appendc(buf, ':');
215 				php_json_pretty_print_char(buf, options, ' ');
216 			}
217 
218 			if (php_json_encode_zval(buf, data, options, encoder) == FAILURE &&
219 					!(options & PHP_JSON_PARTIAL_OUTPUT_ON_ERROR)) {
220 				PHP_JSON_HASH_UNPROTECT_RECURSION(myht);
221 				return FAILURE;
222 			}
223 		} ZEND_HASH_FOREACH_END();
224 	}
225 
226 	PHP_JSON_HASH_UNPROTECT_RECURSION(myht);
227 
228 	if (encoder->depth > encoder->max_depth) {
229 		encoder->error_code = PHP_JSON_ERROR_DEPTH;
230 		if (!(options & PHP_JSON_PARTIAL_OUTPUT_ON_ERROR)) {
231 			return FAILURE;
232 		}
233 	}
234 	--encoder->depth;
235 
236 	/* Only keep closing bracket on same line for empty arrays/objects */
237 	if (need_comma) {
238 		php_json_pretty_print_char(buf, options, '\n');
239 		php_json_pretty_print_indent(buf, options, encoder);
240 	}
241 
242 	if (r == PHP_JSON_OUTPUT_ARRAY) {
243 		smart_str_appendc(buf, ']');
244 	} else {
245 		smart_str_appendc(buf, '}');
246 	}
247 
248 	return SUCCESS;
249 }
250 /* }}} */
251 
php_json_escape_string(smart_str * buf,const char * s,size_t len,int options,php_json_encoder * encoder)252 static int php_json_escape_string(
253 		smart_str *buf, const char *s, size_t len,
254 		int options, php_json_encoder *encoder) /* {{{ */
255 {
256 	int status;
257 	unsigned int us;
258 	size_t pos, checkpoint;
259 	char *dst;
260 
261 	if (len == 0) {
262 		smart_str_appendl(buf, "\"\"", 2);
263 		return SUCCESS;
264 	}
265 
266 	if (options & PHP_JSON_NUMERIC_CHECK) {
267 		double d;
268 		int type;
269 		zend_long p;
270 
271 		if ((type = is_numeric_string(s, len, &p, &d, 0)) != 0) {
272 			if (type == IS_LONG) {
273 				smart_str_append_long(buf, p);
274 				return SUCCESS;
275 			} else if (type == IS_DOUBLE && php_json_is_valid_double(d)) {
276 				php_json_encode_double(buf, d, options);
277 				return SUCCESS;
278 			}
279 		}
280 
281 	}
282 	pos = 0;
283 	checkpoint = buf->s ? ZSTR_LEN(buf->s) : 0;
284 
285 	/* pre-allocate for string length plus 2 quotes */
286 	smart_str_alloc(buf, len+2, 0);
287 	smart_str_appendc(buf, '"');
288 
289 	do {
290 		us = (unsigned char)s[pos];
291 		if (UNEXPECTED(us >= 0x80)) {
292 			if (pos) {
293 				smart_str_appendl(buf, s, pos);
294 				s += pos;
295 				pos = 0;
296 			}
297 			us = php_next_utf8_char((unsigned char *)s, len, &pos, &status);
298 			len -= pos;
299 
300 			/* check whether UTF8 character is correct */
301 			if (UNEXPECTED(status != SUCCESS)) {
302 				s += pos;
303 				pos = 0;
304 				if (options & PHP_JSON_INVALID_UTF8_IGNORE) {
305 					/* ignore invalid UTF8 character */
306 					continue;
307 				} else if (options & PHP_JSON_INVALID_UTF8_SUBSTITUTE) {
308 					/* Use Unicode character 'REPLACEMENT CHARACTER' (U+FFFD) */
309 					if (options & PHP_JSON_UNESCAPED_UNICODE) {
310 						smart_str_appendl(buf, "\xef\xbf\xbd", 3);
311 					} else {
312 						smart_str_appendl(buf, "\\ufffd", 6);
313 					}
314 					continue;
315 				} else {
316 					ZSTR_LEN(buf->s) = checkpoint;
317 					encoder->error_code = PHP_JSON_ERROR_UTF8;
318 					if (options & PHP_JSON_PARTIAL_OUTPUT_ON_ERROR) {
319 						smart_str_appendl(buf, "null", 4);
320 					}
321 					return FAILURE;
322 				}
323 
324 			/* Escape U+2028/U+2029 line terminators, UNLESS both
325 			   JSON_UNESCAPED_UNICODE and
326 			   JSON_UNESCAPED_LINE_TERMINATORS were provided */
327 			} else if ((options & PHP_JSON_UNESCAPED_UNICODE)
328 			    && ((options & PHP_JSON_UNESCAPED_LINE_TERMINATORS)
329 					|| us < 0x2028 || us > 0x2029)) {
330 				smart_str_appendl(buf, s, pos);
331 				s += pos;
332 				pos = 0;
333 				continue;
334 			}
335 			/* From http://en.wikipedia.org/wiki/UTF16 */
336 			if (us >= 0x10000) {
337 				unsigned int next_us;
338 
339 				us -= 0x10000;
340 				next_us = (unsigned short)((us & 0x3ff) | 0xdc00);
341 				us = (unsigned short)((us >> 10) | 0xd800);
342 				dst = smart_str_extend(buf, 6);
343 				dst[0] = '\\';
344 				dst[1] = 'u';
345 				dst[2] = digits[(us >> 12) & 0xf];
346 				dst[3] = digits[(us >> 8) & 0xf];
347 				dst[4] = digits[(us >> 4) & 0xf];
348 				dst[5] = digits[us & 0xf];
349 				us = next_us;
350 			}
351 			dst = smart_str_extend(buf, 6);
352 			dst[0] = '\\';
353 			dst[1] = 'u';
354 			dst[2] = digits[(us >> 12) & 0xf];
355 			dst[3] = digits[(us >> 8) & 0xf];
356 			dst[4] = digits[(us >> 4) & 0xf];
357 			dst[5] = digits[us & 0xf];
358 			s += pos;
359 			pos = 0;
360 		} else {
361 			static const uint32_t charmap[4] = {
362 				0xffffffff, 0x500080c4, 0x10000000, 0x00000000};
363 
364 			len--;
365 			if (EXPECTED(!ZEND_BIT_TEST(charmap, us))) {
366 				pos++;
367 			} else {
368 				if (pos) {
369 					smart_str_appendl(buf, s, pos);
370 					s += pos;
371 					pos = 0;
372 				}
373 				s++;
374 				switch (us) {
375 					case '"':
376 						if (options & PHP_JSON_HEX_QUOT) {
377 							smart_str_appendl(buf, "\\u0022", 6);
378 						} else {
379 							smart_str_appendl(buf, "\\\"", 2);
380 						}
381 						break;
382 
383 					case '\\':
384 						smart_str_appendl(buf, "\\\\", 2);
385 						break;
386 
387 					case '/':
388 						if (options & PHP_JSON_UNESCAPED_SLASHES) {
389 							smart_str_appendc(buf, '/');
390 						} else {
391 							smart_str_appendl(buf, "\\/", 2);
392 						}
393 						break;
394 
395 					case '\b':
396 						smart_str_appendl(buf, "\\b", 2);
397 						break;
398 
399 					case '\f':
400 						smart_str_appendl(buf, "\\f", 2);
401 						break;
402 
403 					case '\n':
404 						smart_str_appendl(buf, "\\n", 2);
405 						break;
406 
407 					case '\r':
408 						smart_str_appendl(buf, "\\r", 2);
409 						break;
410 
411 					case '\t':
412 						smart_str_appendl(buf, "\\t", 2);
413 						break;
414 
415 					case '<':
416 						if (options & PHP_JSON_HEX_TAG) {
417 							smart_str_appendl(buf, "\\u003C", 6);
418 						} else {
419 							smart_str_appendc(buf, '<');
420 						}
421 						break;
422 
423 					case '>':
424 						if (options & PHP_JSON_HEX_TAG) {
425 							smart_str_appendl(buf, "\\u003E", 6);
426 						} else {
427 							smart_str_appendc(buf, '>');
428 						}
429 						break;
430 
431 					case '&':
432 						if (options & PHP_JSON_HEX_AMP) {
433 							smart_str_appendl(buf, "\\u0026", 6);
434 						} else {
435 							smart_str_appendc(buf, '&');
436 						}
437 						break;
438 
439 					case '\'':
440 						if (options & PHP_JSON_HEX_APOS) {
441 							smart_str_appendl(buf, "\\u0027", 6);
442 						} else {
443 							smart_str_appendc(buf, '\'');
444 						}
445 						break;
446 
447 					default:
448 						ZEND_ASSERT(us < ' ');
449 						dst = smart_str_extend(buf, 6);
450 						dst[0] = '\\';
451 						dst[1] = 'u';
452 						dst[2] = '0';
453 						dst[3] = '0';
454 						dst[4] = digits[(us >> 4) & 0xf];
455 						dst[5] = digits[us & 0xf];
456 						break;
457 				}
458 			}
459 		}
460 	} while (len);
461 
462 	if (EXPECTED(pos)) {
463 		smart_str_appendl(buf, s, pos);
464 	}
465 	smart_str_appendc(buf, '"');
466 
467 	return SUCCESS;
468 }
469 /* }}} */
470 
php_json_encode_serializable_object(smart_str * buf,zval * val,int options,php_json_encoder * encoder)471 static int php_json_encode_serializable_object(smart_str *buf, zval *val, int options, php_json_encoder *encoder) /* {{{ */
472 {
473 	zend_class_entry *ce = Z_OBJCE_P(val);
474 	HashTable* myht = Z_OBJPROP_P(val);
475 	zval retval, fname;
476 	int return_code;
477 
478 	if (myht && GC_IS_RECURSIVE(myht)) {
479 		encoder->error_code = PHP_JSON_ERROR_RECURSION;
480 		if (options & PHP_JSON_PARTIAL_OUTPUT_ON_ERROR) {
481 			smart_str_appendl(buf, "null", 4);
482 		}
483 		return FAILURE;
484 	}
485 
486 	PHP_JSON_HASH_PROTECT_RECURSION(myht);
487 
488 	ZVAL_STRING(&fname, "jsonSerialize");
489 
490 	if (FAILURE == call_user_function(EG(function_table), val, &fname, &retval, 0, NULL) || Z_TYPE(retval) == IS_UNDEF) {
491 		if (!EG(exception)) {
492 			zend_throw_exception_ex(NULL, 0, "Failed calling %s::jsonSerialize()", ZSTR_VAL(ce->name));
493 		}
494 		zval_ptr_dtor(&fname);
495 
496 		if (options & PHP_JSON_PARTIAL_OUTPUT_ON_ERROR) {
497 			smart_str_appendl(buf, "null", 4);
498 		}
499 		PHP_JSON_HASH_UNPROTECT_RECURSION(myht);
500 		return FAILURE;
501 	}
502 
503 	if (EG(exception)) {
504 		/* Error already raised */
505 		zval_ptr_dtor(&retval);
506 		zval_ptr_dtor(&fname);
507 
508 		if (options & PHP_JSON_PARTIAL_OUTPUT_ON_ERROR) {
509 			smart_str_appendl(buf, "null", 4);
510 		}
511 		PHP_JSON_HASH_UNPROTECT_RECURSION(myht);
512 		return FAILURE;
513 	}
514 
515 	if ((Z_TYPE(retval) == IS_OBJECT) &&
516 		(Z_OBJ(retval) == Z_OBJ_P(val))) {
517 		/* Handle the case where jsonSerialize does: return $this; by going straight to encode array */
518 		PHP_JSON_HASH_UNPROTECT_RECURSION(myht);
519 		return_code = php_json_encode_array(buf, &retval, options, encoder);
520 	} else {
521 		/* All other types, encode as normal */
522 		return_code = php_json_encode_zval(buf, &retval, options, encoder);
523 		PHP_JSON_HASH_UNPROTECT_RECURSION(myht);
524 	}
525 
526 	zval_ptr_dtor(&retval);
527 	zval_ptr_dtor(&fname);
528 
529 	return return_code;
530 }
531 /* }}} */
532 
php_json_encode_zval(smart_str * buf,zval * val,int options,php_json_encoder * encoder)533 int php_json_encode_zval(smart_str *buf, zval *val, int options, php_json_encoder *encoder) /* {{{ */
534 {
535 again:
536 	switch (Z_TYPE_P(val))
537 	{
538 		case IS_NULL:
539 			smart_str_appendl(buf, "null", 4);
540 			break;
541 
542 		case IS_TRUE:
543 			smart_str_appendl(buf, "true", 4);
544 			break;
545 		case IS_FALSE:
546 			smart_str_appendl(buf, "false", 5);
547 			break;
548 
549 		case IS_LONG:
550 			smart_str_append_long(buf, Z_LVAL_P(val));
551 			break;
552 
553 		case IS_DOUBLE:
554 			if (php_json_is_valid_double(Z_DVAL_P(val))) {
555 				php_json_encode_double(buf, Z_DVAL_P(val), options);
556 			} else {
557 				encoder->error_code = PHP_JSON_ERROR_INF_OR_NAN;
558 				smart_str_appendc(buf, '0');
559 			}
560 			break;
561 
562 		case IS_STRING:
563 			return php_json_escape_string(buf, Z_STRVAL_P(val), Z_STRLEN_P(val), options, encoder);
564 
565 		case IS_OBJECT:
566 			if (instanceof_function(Z_OBJCE_P(val), php_json_serializable_ce)) {
567 				return php_json_encode_serializable_object(buf, val, options, encoder);
568 			}
569 			/* fallthrough -- Non-serializable object */
570 		case IS_ARRAY: {
571 			/* Avoid modifications (and potential freeing) of the array through a reference when a
572 			 * jsonSerialize() method is invoked. */
573 			zval zv;
574 			int res;
575 			ZVAL_COPY(&zv, val);
576 			res = php_json_encode_array(buf, &zv, options, encoder);
577 			zval_ptr_dtor_nogc(&zv);
578 			return res;
579 		}
580 
581 		case IS_REFERENCE:
582 			val = Z_REFVAL_P(val);
583 			goto again;
584 
585 		default:
586 			encoder->error_code = PHP_JSON_ERROR_UNSUPPORTED_TYPE;
587 			if (options & PHP_JSON_PARTIAL_OUTPUT_ON_ERROR) {
588 				smart_str_appendl(buf, "null", 4);
589 			}
590 			return FAILURE;
591 	}
592 
593 	return SUCCESS;
594 }
595 /* }}} */
596 
597 /*
598  * Local variables:
599  * tab-width: 4
600  * c-basic-offset: 4
601  * End:
602  * vim600: noet sw=4 ts=4 fdm=marker
603  * vim<600: noet sw=4 ts=4
604  */
605