/src/php-src/ext/json/json_encoder.c
Line | Count | Source |
1 | | /* |
2 | | +----------------------------------------------------------------------+ |
3 | | | Copyright © The PHP Group and Contributors. | |
4 | | +----------------------------------------------------------------------+ |
5 | | | This source file is subject to the Modified BSD License that is | |
6 | | | bundled with this package in the file LICENSE, and is available | |
7 | | | through the World Wide Web at <https://www.php.net/license/>. | |
8 | | | | |
9 | | | SPDX-License-Identifier: BSD-3-Clause | |
10 | | +----------------------------------------------------------------------+ |
11 | | | Author: Omar Kilani <omar@php.net> | |
12 | | | Jakub Zelenka <bukka@php.net> | |
13 | | +----------------------------------------------------------------------+ |
14 | | */ |
15 | | |
16 | | #ifdef HAVE_CONFIG_H |
17 | | #include <config.h> |
18 | | #endif |
19 | | |
20 | | #include "php.h" |
21 | | #include "ext/standard/html.h" |
22 | | #include "zend_smart_str.h" |
23 | | #include "php_json.h" |
24 | | #include "php_json_encoder.h" |
25 | | #include "zend_portability.h" |
26 | | #include <zend_exceptions.h> |
27 | | #include "zend_enum.h" |
28 | | #include "zend_property_hooks.h" |
29 | | #include "zend_lazy_objects.h" |
30 | | |
31 | | static const char digits[] = "0123456789abcdef"; |
32 | | |
33 | | static zend_always_inline bool php_json_check_stack_limit(void) |
34 | 386 | { |
35 | 386 | #ifdef ZEND_CHECK_STACK_LIMIT |
36 | 386 | return zend_call_stack_overflowed(EG(stack_limit)); |
37 | | #else |
38 | | return false; |
39 | | #endif |
40 | 386 | } |
41 | | |
42 | | /* {{{ Pretty printing support functions */ |
43 | | |
44 | | static inline void php_json_pretty_print_char(smart_str *buf, int options, char c) /* {{{ */ |
45 | 2.54k | { |
46 | 2.54k | if (options & PHP_JSON_PRETTY_PRINT) { |
47 | 0 | smart_str_appendc(buf, c); |
48 | 0 | } |
49 | 2.54k | } |
50 | | /* }}} */ |
51 | | |
52 | | static inline void php_json_pretty_print_indent(smart_str *buf, int options, const php_json_encoder *encoder) /* {{{ */ |
53 | 2.08k | { |
54 | 2.08k | if (options & PHP_JSON_PRETTY_PRINT) { |
55 | 0 | smart_str_alloc(buf, encoder->depth * 4, 0); |
56 | 0 | for (int i = 0; i < encoder->depth; ++i) { |
57 | 0 | smart_str_appendl(buf, " ", 4); |
58 | 0 | } |
59 | 0 | } |
60 | 2.08k | } |
61 | | /* }}} */ |
62 | | |
63 | | /* }}} */ |
64 | | |
65 | | static |
66 | | #if defined(_MSC_VER) && defined(_M_ARM64) |
67 | | // MSVC bug: https://developercommunity.visualstudio.com/t/corrupt-optimization-on-arm64-with-Ox-/10102551 |
68 | | zend_never_inline |
69 | | #else |
70 | | inline |
71 | | #endif |
72 | | bool php_json_is_valid_double(double d) /* {{{ */ |
73 | 166 | { |
74 | 166 | return !zend_isinf(d) && !zend_isnan(d); |
75 | 166 | } |
76 | | /* }}} */ |
77 | | |
78 | | static inline void php_json_encode_double(smart_str *buf, double d, int options) /* {{{ */ |
79 | 147 | { |
80 | 147 | size_t len; |
81 | 147 | char num[ZEND_DOUBLE_MAX_LENGTH]; |
82 | | |
83 | 147 | zend_gcvt(d, (int)PG(serialize_precision), '.', 'e', num); |
84 | 147 | len = strlen(num); |
85 | 147 | if (options & PHP_JSON_PRESERVE_ZERO_FRACTION && strchr(num, '.') == NULL && len < ZEND_DOUBLE_MAX_LENGTH - 2) { |
86 | 101 | num[len++] = '.'; |
87 | 101 | num[len++] = '0'; |
88 | 101 | num[len] = '\0'; |
89 | 101 | } |
90 | 147 | smart_str_appendl(buf, num, len); |
91 | 147 | } |
92 | | /* }}} */ |
93 | | |
94 | | #define PHP_JSON_HASH_PROTECT_RECURSION(_tmp_ht) \ |
95 | 386 | do { \ |
96 | 386 | if (_tmp_ht) { \ |
97 | 378 | GC_TRY_PROTECT_RECURSION(_tmp_ht); \ |
98 | 378 | } \ |
99 | 386 | } while (0) |
100 | | |
101 | | #define PHP_JSON_HASH_UNPROTECT_RECURSION(_tmp_ht) \ |
102 | 386 | do { \ |
103 | 386 | if (_tmp_ht) { \ |
104 | 378 | GC_TRY_UNPROTECT_RECURSION(_tmp_ht); \ |
105 | 378 | } \ |
106 | 386 | } while (0) |
107 | | |
108 | | static zend_result php_json_encode_array(smart_str *buf, zval *val, int options, php_json_encoder *encoder) /* {{{ */ |
109 | 386 | { |
110 | 386 | bool encode_as_object = options & PHP_JSON_FORCE_OBJECT; |
111 | 386 | HashTable *myht, *prop_ht; |
112 | 386 | zend_refcounted *recursion_rc; |
113 | | |
114 | 386 | if (php_json_check_stack_limit()) { |
115 | 0 | encoder->error_code = PHP_JSON_ERROR_DEPTH; |
116 | 0 | if (options & PHP_JSON_PARTIAL_OUTPUT_ON_ERROR) { |
117 | 0 | smart_str_appendl(buf, "null", 4); |
118 | 0 | } |
119 | 0 | return FAILURE; |
120 | 0 | } |
121 | | |
122 | 386 | if (Z_TYPE_P(val) == IS_ARRAY) { |
123 | 123 | myht = Z_ARRVAL_P(val); |
124 | 123 | recursion_rc = (zend_refcounted *)myht; |
125 | 123 | prop_ht = NULL; |
126 | 123 | encode_as_object = encode_as_object || !zend_array_is_list(myht); |
127 | 263 | } else if (Z_OBJ_P(val)->properties == NULL |
128 | 98 | && Z_OBJ_HT_P(val)->get_properties_for == NULL |
129 | 90 | && Z_OBJ_HT_P(val)->get_properties == zend_std_get_properties |
130 | 90 | && Z_OBJ_P(val)->ce->num_hooked_props == 0 |
131 | 38 | && !zend_object_is_lazy(Z_OBJ_P(val))) { |
132 | | /* Optimized version without rebuilding properties HashTable */ |
133 | 2 | zend_object *obj = Z_OBJ_P(val); |
134 | 2 | const zend_class_entry *ce = obj->ce; |
135 | | |
136 | 2 | if (GC_IS_RECURSIVE(obj)) { |
137 | 0 | encoder->error_code = PHP_JSON_ERROR_RECURSION; |
138 | 0 | smart_str_appendl(buf, "null", 4); |
139 | 0 | return FAILURE; |
140 | 0 | } |
141 | | |
142 | 2 | PHP_JSON_HASH_PROTECT_RECURSION(obj); |
143 | | |
144 | 2 | smart_str_appendc(buf, '{'); |
145 | | |
146 | 2 | ++encoder->depth; |
147 | | |
148 | 2 | for (int i = 0; i < ce->default_properties_count; i++) { |
149 | 0 | zend_property_info *prop_info = ce->properties_info_table[i]; |
150 | 0 | if (!prop_info) { |
151 | 0 | continue; |
152 | 0 | } |
153 | 0 | if (ZSTR_VAL(prop_info->name)[0] == '\0' && ZSTR_LEN(prop_info->name) > 0) { |
154 | | /* Skip protected and private members. */ |
155 | 0 | continue; |
156 | 0 | } |
157 | 0 | zval *prop = OBJ_PROP(obj, prop_info->offset); |
158 | 0 | if (Z_TYPE_P(prop) == IS_UNDEF) { |
159 | 0 | continue; |
160 | 0 | } |
161 | | |
162 | 0 | php_json_pretty_print_char(buf, options, '\n'); |
163 | 0 | php_json_pretty_print_indent(buf, options, encoder); |
164 | |
|
165 | 0 | if (php_json_escape_string(buf, ZSTR_VAL(prop_info->name), ZSTR_LEN(prop_info->name), |
166 | 0 | options & ~PHP_JSON_NUMERIC_CHECK, encoder) == FAILURE && |
167 | 0 | (options & PHP_JSON_PARTIAL_OUTPUT_ON_ERROR) && |
168 | 0 | buf->s) { |
169 | 0 | ZSTR_LEN(buf->s) -= 4; |
170 | 0 | smart_str_appendl(buf, "\"\"", 2); |
171 | 0 | } |
172 | |
|
173 | 0 | smart_str_appendc(buf, ':'); |
174 | 0 | php_json_pretty_print_char(buf, options, ' '); |
175 | |
|
176 | 0 | if (php_json_encode_zval(buf, prop, options, encoder) == FAILURE && |
177 | 0 | !(options & PHP_JSON_PARTIAL_OUTPUT_ON_ERROR)) { |
178 | 0 | PHP_JSON_HASH_UNPROTECT_RECURSION(obj); |
179 | 0 | return FAILURE; |
180 | 0 | } |
181 | | |
182 | 0 | smart_str_appendc(buf, ','); |
183 | 0 | } |
184 | | |
185 | 2 | bool empty = ZSTR_VAL(buf->s)[ZSTR_LEN(buf->s) - 1] != ','; |
186 | 2 | if (!empty) { |
187 | | /* Drop the trailing comma. */ |
188 | 0 | ZSTR_LEN(buf->s)--; |
189 | 0 | } |
190 | | |
191 | 2 | PHP_JSON_HASH_UNPROTECT_RECURSION(obj); |
192 | 2 | if (encoder->depth > encoder->max_depth) { |
193 | 0 | encoder->error_code = PHP_JSON_ERROR_DEPTH; |
194 | 0 | if (!(options & PHP_JSON_PARTIAL_OUTPUT_ON_ERROR)) { |
195 | 0 | return FAILURE; |
196 | 0 | } |
197 | 0 | } |
198 | 2 | --encoder->depth; |
199 | | |
200 | 2 | if (!empty) { |
201 | 0 | php_json_pretty_print_char(buf, options, '\n'); |
202 | 0 | php_json_pretty_print_indent(buf, options, encoder); |
203 | 0 | } |
204 | 2 | smart_str_appendc(buf, '}'); |
205 | 2 | return SUCCESS; |
206 | 261 | } else { |
207 | 261 | zend_object *obj = Z_OBJ_P(val); |
208 | 261 | prop_ht = myht = zend_get_properties_for(val, ZEND_PROP_PURPOSE_JSON); |
209 | 261 | if (obj->ce->num_hooked_props == 0) { |
210 | 70 | recursion_rc = (zend_refcounted *)prop_ht; |
211 | 191 | } else { |
212 | | /* Protecting the object itself is fine here because myht is temporary and can't be |
213 | | * referenced from a different place in the object graph. */ |
214 | 191 | recursion_rc = (zend_refcounted *)obj; |
215 | 191 | } |
216 | 261 | encode_as_object = true; |
217 | 261 | } |
218 | | |
219 | 384 | if (recursion_rc && GC_IS_RECURSIVE(recursion_rc)) { |
220 | 0 | encoder->error_code = PHP_JSON_ERROR_RECURSION; |
221 | 0 | smart_str_appendl(buf, "null", 4); |
222 | 0 | zend_release_properties(prop_ht); |
223 | 0 | return FAILURE; |
224 | 0 | } |
225 | | |
226 | 384 | PHP_JSON_HASH_PROTECT_RECURSION(recursion_rc); |
227 | | |
228 | 384 | if (!encode_as_object) { |
229 | 116 | smart_str_appendc(buf, '['); |
230 | 268 | } else { |
231 | 268 | smart_str_appendc(buf, '{'); |
232 | 268 | } |
233 | | |
234 | 384 | ++encoder->depth; |
235 | | |
236 | 384 | uint32_t i = myht ? zend_hash_num_elements(myht) : 0; |
237 | | |
238 | 384 | bool empty = true; |
239 | 384 | if (i > 0) { |
240 | 325 | zend_string *key; |
241 | 325 | zval *data; |
242 | 325 | zend_ulong index; |
243 | | |
244 | 4.45k | ZEND_HASH_FOREACH_KEY_VAL_IND(myht, index, key, data) { |
245 | 4.45k | bool need_dtor = false; |
246 | 4.45k | zval tmp; |
247 | 4.45k | ZVAL_UNDEF(&tmp); |
248 | | |
249 | 4.45k | if (!encode_as_object) { |
250 | 1.33k | ZEND_ASSERT(Z_TYPE_P(data) != IS_PTR); |
251 | | |
252 | 1.33k | php_json_pretty_print_char(buf, options, '\n'); |
253 | 1.33k | php_json_pretty_print_indent(buf, options, encoder); |
254 | 1.33k | } else { |
255 | 726 | if (key) { |
256 | 726 | if (ZSTR_VAL(key)[0] == '\0' && ZSTR_LEN(key) > 0 && Z_TYPE_P(val) == IS_OBJECT) { |
257 | | /* Skip protected and private members. */ |
258 | 193 | continue; |
259 | 193 | } |
260 | | |
261 | | /* data is IS_PTR for properties with hooks. */ |
262 | 533 | if (UNEXPECTED(Z_TYPE_P(data) == IS_PTR)) { |
263 | 363 | zend_property_info *prop_info = Z_PTR_P(data); |
264 | 363 | if ((prop_info->flags & ZEND_ACC_VIRTUAL) && !prop_info->hooks[ZEND_PROPERTY_HOOK_GET]) { |
265 | 68 | continue; |
266 | 68 | } |
267 | 295 | need_dtor = true; |
268 | 295 | data = zend_read_property_ex(prop_info->ce, Z_OBJ_P(val), prop_info->name, /* silent */ true, &tmp); |
269 | 295 | if (EG(exception)) { |
270 | 5 | PHP_JSON_HASH_UNPROTECT_RECURSION(recursion_rc); |
271 | 5 | zend_release_properties(prop_ht); |
272 | 5 | zval_ptr_dtor(&tmp); |
273 | 5 | return FAILURE; |
274 | 5 | } |
275 | 295 | } |
276 | | |
277 | | |
278 | 460 | php_json_pretty_print_char(buf, options, '\n'); |
279 | 460 | php_json_pretty_print_indent(buf, options, encoder); |
280 | | |
281 | 460 | if (php_json_escape_string(buf, ZSTR_VAL(key), ZSTR_LEN(key), |
282 | 460 | options & ~PHP_JSON_NUMERIC_CHECK, encoder) == FAILURE && |
283 | 5 | (options & PHP_JSON_PARTIAL_OUTPUT_ON_ERROR) && |
284 | 0 | buf->s) { |
285 | 0 | ZSTR_LEN(buf->s) -= 4; |
286 | 0 | smart_str_appendl(buf, "\"\"", 2); |
287 | 0 | } |
288 | 460 | } else { |
289 | 0 | php_json_pretty_print_char(buf, options, '\n'); |
290 | 0 | php_json_pretty_print_indent(buf, options, encoder); |
291 | |
|
292 | 0 | smart_str_appendc(buf, '"'); |
293 | 0 | smart_str_append_long(buf, (zend_long) index); |
294 | 0 | smart_str_appendc(buf, '"'); |
295 | 0 | } |
296 | | |
297 | 460 | smart_str_appendc(buf, ':'); |
298 | 460 | php_json_pretty_print_char(buf, options, ' '); |
299 | 460 | } |
300 | | |
301 | 1.79k | if (php_json_encode_zval(buf, data, options, encoder) == FAILURE && |
302 | 13 | !(options & PHP_JSON_PARTIAL_OUTPUT_ON_ERROR)) { |
303 | 13 | PHP_JSON_HASH_UNPROTECT_RECURSION(recursion_rc); |
304 | 13 | zend_release_properties(prop_ht); |
305 | 13 | zval_ptr_dtor(&tmp); |
306 | 13 | return FAILURE; |
307 | 13 | } |
308 | 1.77k | if (UNEXPECTED(need_dtor)) { |
309 | 284 | zval_ptr_dtor(&tmp); |
310 | 284 | } |
311 | | |
312 | 1.77k | smart_str_appendc(buf, ','); |
313 | 1.77k | } ZEND_HASH_FOREACH_END(); |
314 | | |
315 | 307 | empty = ZSTR_VAL(buf->s)[ZSTR_LEN(buf->s) - 1] != ','; |
316 | 307 | if (!empty) { |
317 | | /* Drop the trailing comma. */ |
318 | 294 | ZSTR_LEN(buf->s)--; |
319 | 294 | } |
320 | 307 | } |
321 | | |
322 | 366 | PHP_JSON_HASH_UNPROTECT_RECURSION(recursion_rc); |
323 | | |
324 | 366 | if (encoder->depth > encoder->max_depth) { |
325 | 0 | encoder->error_code = PHP_JSON_ERROR_DEPTH; |
326 | 0 | if (!(options & PHP_JSON_PARTIAL_OUTPUT_ON_ERROR)) { |
327 | 0 | zend_release_properties(prop_ht); |
328 | 0 | return FAILURE; |
329 | 0 | } |
330 | 0 | } |
331 | 366 | --encoder->depth; |
332 | | |
333 | | /* Only keep closing bracket on same line for empty arrays/objects */ |
334 | 366 | if (!empty) { |
335 | 294 | php_json_pretty_print_char(buf, options, '\n'); |
336 | 294 | php_json_pretty_print_indent(buf, options, encoder); |
337 | 294 | } |
338 | | |
339 | 366 | if (!encode_as_object) { |
340 | 116 | smart_str_appendc(buf, ']'); |
341 | 250 | } else { |
342 | 250 | smart_str_appendc(buf, '}'); |
343 | 250 | } |
344 | | |
345 | 366 | zend_release_properties(prop_ht); |
346 | 366 | return SUCCESS; |
347 | 366 | } |
348 | | /* }}} */ |
349 | | |
350 | | zend_result php_json_escape_string( |
351 | | smart_str *buf, const char *s, size_t len, |
352 | | int options, php_json_encoder *encoder) /* {{{ */ |
353 | 1.55k | { |
354 | 1.55k | size_t pos, checkpoint; |
355 | 1.55k | char *dst; |
356 | | |
357 | 1.55k | if (len == 0) { |
358 | 57 | smart_str_appendl(buf, "\"\"", 2); |
359 | 57 | return SUCCESS; |
360 | 57 | } |
361 | | |
362 | 1.49k | if (options & PHP_JSON_NUMERIC_CHECK) { |
363 | 0 | double d; |
364 | 0 | int type; |
365 | 0 | zend_long p; |
366 | |
|
367 | 0 | if ((type = is_numeric_string(s, len, &p, &d, 0)) != 0) { |
368 | 0 | if (type == IS_LONG) { |
369 | 0 | smart_str_append_long(buf, p); |
370 | 0 | return SUCCESS; |
371 | 0 | } else if (type == IS_DOUBLE && php_json_is_valid_double(d)) { |
372 | 0 | php_json_encode_double(buf, d, options); |
373 | 0 | return SUCCESS; |
374 | 0 | } |
375 | 0 | } |
376 | |
|
377 | 0 | } |
378 | 1.49k | checkpoint = buf->s ? ZSTR_LEN(buf->s) : 0; |
379 | | |
380 | | /* pre-allocate for string length plus 2 quotes */ |
381 | 1.49k | smart_str_alloc(buf, len+2, 0); |
382 | 1.49k | smart_str_appendc(buf, '"'); |
383 | | |
384 | 1.49k | pos = 0; |
385 | | |
386 | 68.9k | do { |
387 | 68.9k | static const uint32_t charmap[8] = { |
388 | 68.9k | 0xffffffff, 0x500080c4, 0x10000000, 0x00000000, |
389 | 68.9k | 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff}; |
390 | | |
391 | 68.9k | unsigned int us = (unsigned char)s[pos]; |
392 | 68.9k | if (EXPECTED(!ZEND_BIT_TEST(charmap, us))) { |
393 | 42.0k | pos++; |
394 | 42.0k | len--; |
395 | 42.0k | if (len == 0) { |
396 | 1.44k | smart_str_appendl(buf, s, pos); |
397 | 1.44k | break; |
398 | 1.44k | } |
399 | 42.0k | } else { |
400 | 26.8k | if (pos) { |
401 | 1.95k | smart_str_appendl(buf, s, pos); |
402 | 1.95k | s += pos; |
403 | 1.95k | pos = 0; |
404 | 1.95k | } |
405 | 26.8k | us = (unsigned char)s[0]; |
406 | 26.8k | if (UNEXPECTED(us >= 0x80)) { |
407 | 9.41k | zend_result status; |
408 | 9.41k | us = php_next_utf8_char((unsigned char *)s, len, &pos, &status); |
409 | | |
410 | | /* check whether UTF8 character is correct */ |
411 | 9.41k | if (UNEXPECTED(status != SUCCESS)) { |
412 | 9.35k | if (options & PHP_JSON_INVALID_UTF8_IGNORE) { |
413 | | /* ignore invalid UTF8 character */ |
414 | 9.30k | } else if (options & PHP_JSON_INVALID_UTF8_SUBSTITUTE) { |
415 | | /* Use Unicode character 'REPLACEMENT CHARACTER' (U+FFFD) */ |
416 | 0 | if (options & PHP_JSON_UNESCAPED_UNICODE) { |
417 | 0 | smart_str_appendl(buf, "\xef\xbf\xbd", 3); |
418 | 0 | } else { |
419 | 0 | smart_str_appendl(buf, "\\ufffd", 6); |
420 | 0 | } |
421 | 49 | } else { |
422 | 49 | ZSTR_LEN(buf->s) = checkpoint; |
423 | 49 | encoder->error_code = PHP_JSON_ERROR_UTF8; |
424 | 49 | if (options & PHP_JSON_PARTIAL_OUTPUT_ON_ERROR) { |
425 | 0 | smart_str_appendl(buf, "null", 4); |
426 | 0 | } |
427 | 49 | return FAILURE; |
428 | 49 | } |
429 | | |
430 | | /* Escape U+2028/U+2029 line terminators, UNLESS both |
431 | | JSON_UNESCAPED_UNICODE and |
432 | | JSON_UNESCAPED_LINE_TERMINATORS were provided */ |
433 | 9.35k | } else if ((options & PHP_JSON_UNESCAPED_UNICODE) |
434 | 64 | && ((options & PHP_JSON_UNESCAPED_LINE_TERMINATORS) |
435 | 64 | || us < 0x2028 || us > 0x2029)) { |
436 | 64 | smart_str_appendl(buf, s, pos); |
437 | 64 | } else { |
438 | | /* From http://en.wikipedia.org/wiki/UTF16 */ |
439 | 0 | if (us >= 0x10000) { |
440 | 0 | unsigned int next_us; |
441 | |
|
442 | 0 | us -= 0x10000; |
443 | 0 | next_us = (unsigned short)((us & 0x3ff) | 0xdc00); |
444 | 0 | us = (unsigned short)((us >> 10) | 0xd800); |
445 | 0 | dst = smart_str_extend(buf, 6); |
446 | 0 | dst[0] = '\\'; |
447 | 0 | dst[1] = 'u'; |
448 | 0 | dst[2] = digits[(us >> 12) & 0xf]; |
449 | 0 | dst[3] = digits[(us >> 8) & 0xf]; |
450 | 0 | dst[4] = digits[(us >> 4) & 0xf]; |
451 | 0 | dst[5] = digits[us & 0xf]; |
452 | 0 | us = next_us; |
453 | 0 | } |
454 | 0 | dst = smart_str_extend(buf, 6); |
455 | 0 | dst[0] = '\\'; |
456 | 0 | dst[1] = 'u'; |
457 | 0 | dst[2] = digits[(us >> 12) & 0xf]; |
458 | 0 | dst[3] = digits[(us >> 8) & 0xf]; |
459 | 0 | dst[4] = digits[(us >> 4) & 0xf]; |
460 | 0 | dst[5] = digits[us & 0xf]; |
461 | 0 | } |
462 | 9.36k | s += pos; |
463 | 9.36k | len -= pos; |
464 | 9.36k | pos = 0; |
465 | 17.4k | } else { |
466 | 17.4k | s++; |
467 | 17.4k | switch (us) { |
468 | 230 | case '"': |
469 | 230 | if (options & PHP_JSON_HEX_QUOT) { |
470 | 0 | smart_str_appendl(buf, "\\u0022", 6); |
471 | 230 | } else { |
472 | 230 | smart_str_appendl(buf, "\\\"", 2); |
473 | 230 | } |
474 | 230 | break; |
475 | | |
476 | 85 | case '\\': |
477 | 85 | smart_str_appendl(buf, "\\\\", 2); |
478 | 85 | break; |
479 | | |
480 | 155 | case '/': |
481 | 155 | if (options & PHP_JSON_UNESCAPED_SLASHES) { |
482 | 151 | smart_str_appendc(buf, '/'); |
483 | 151 | } else { |
484 | 4 | smart_str_appendl(buf, "\\/", 2); |
485 | 4 | } |
486 | 155 | break; |
487 | | |
488 | 26 | case '\b': |
489 | 26 | smart_str_appendl(buf, "\\b", 2); |
490 | 26 | break; |
491 | | |
492 | 42 | case '\f': |
493 | 42 | smart_str_appendl(buf, "\\f", 2); |
494 | 42 | break; |
495 | | |
496 | 246 | case '\n': |
497 | 246 | smart_str_appendl(buf, "\\n", 2); |
498 | 246 | break; |
499 | | |
500 | 1.75k | case '\r': |
501 | 1.75k | smart_str_appendl(buf, "\\r", 2); |
502 | 1.75k | break; |
503 | | |
504 | 45 | case '\t': |
505 | 45 | smart_str_appendl(buf, "\\t", 2); |
506 | 45 | break; |
507 | | |
508 | 8.27k | case '<': |
509 | 8.27k | if (options & PHP_JSON_HEX_TAG) { |
510 | 4.01k | smart_str_appendl(buf, "\\u003C", 6); |
511 | 4.26k | } else { |
512 | 4.26k | smart_str_appendc(buf, '<'); |
513 | 4.26k | } |
514 | 8.27k | break; |
515 | | |
516 | 76 | case '>': |
517 | 76 | if (options & PHP_JSON_HEX_TAG) { |
518 | 60 | smart_str_appendl(buf, "\\u003E", 6); |
519 | 60 | } else { |
520 | 16 | smart_str_appendc(buf, '>'); |
521 | 16 | } |
522 | 76 | break; |
523 | | |
524 | 24 | case '&': |
525 | 24 | if (options & PHP_JSON_HEX_AMP) { |
526 | 20 | smart_str_appendl(buf, "\\u0026", 6); |
527 | 20 | } else { |
528 | 4 | smart_str_appendc(buf, '&'); |
529 | 4 | } |
530 | 24 | break; |
531 | | |
532 | 4 | case '\'': |
533 | 4 | if (options & PHP_JSON_HEX_APOS) { |
534 | 0 | smart_str_appendl(buf, "\\u0027", 6); |
535 | 4 | } else { |
536 | 4 | smart_str_appendc(buf, '\''); |
537 | 4 | } |
538 | 4 | break; |
539 | | |
540 | 6.46k | default: |
541 | 6.46k | ZEND_ASSERT(us < ' '); |
542 | 6.46k | dst = smart_str_extend(buf, 6); |
543 | 6.46k | dst[0] = '\\'; |
544 | 6.46k | dst[1] = 'u'; |
545 | 6.46k | dst[2] = '0'; |
546 | 6.46k | dst[3] = '0'; |
547 | 6.46k | dst[4] = digits[(us >> 4) & 0xf]; |
548 | 6.46k | dst[5] = digits[us & 0xf]; |
549 | 6.46k | break; |
550 | 17.4k | } |
551 | 17.4k | len--; |
552 | 17.4k | } |
553 | 26.8k | } |
554 | 68.9k | } while (len); |
555 | | |
556 | 1.44k | smart_str_appendc(buf, '"'); |
557 | | |
558 | 1.44k | return SUCCESS; |
559 | 1.49k | } |
560 | | /* }}} */ |
561 | | |
562 | | static zend_result php_json_encode_serializable_object(smart_str *buf, zend_object *obj, int options, php_json_encoder *encoder) |
563 | 37 | { |
564 | 37 | zend_class_entry *ce = obj->ce; |
565 | 37 | uint32_t *guard = zend_get_recursion_guard(obj); |
566 | 37 | zval retval; |
567 | 37 | zend_result return_code; |
568 | | |
569 | 37 | ZEND_ASSERT(guard != NULL); |
570 | | |
571 | 37 | if (ZEND_GUARD_IS_RECURSIVE(guard, JSON)) { |
572 | 0 | encoder->error_code = PHP_JSON_ERROR_RECURSION; |
573 | 0 | if (options & PHP_JSON_PARTIAL_OUTPUT_ON_ERROR) { |
574 | 0 | smart_str_appendl(buf, "null", 4); |
575 | 0 | } |
576 | 0 | return FAILURE; |
577 | 0 | } |
578 | | |
579 | 37 | ZEND_GUARD_PROTECT_RECURSION(guard, JSON); |
580 | | |
581 | | /* jsonSerialize() may drop the last reference to the object, e.g. by |
582 | | * nulling a reference that aliases the encoded array slot; keep it alive |
583 | | * so the recursion guard and the identity check below stay valid. */ |
584 | 37 | GC_ADDREF(obj); |
585 | | |
586 | 37 | zend_function *json_serialize_method = zend_hash_str_find_ptr(&ce->function_table, ZEND_STRL("jsonserialize")); |
587 | 37 | ZEND_ASSERT(json_serialize_method != NULL && "This should be guaranteed prior to calling this function"); |
588 | 37 | zend_call_known_function(json_serialize_method, obj, ce, &retval, 0, NULL, NULL); |
589 | | /* An exception has occurred */ |
590 | 37 | if (Z_TYPE(retval) == IS_UNDEF) { |
591 | 13 | if (options & PHP_JSON_PARTIAL_OUTPUT_ON_ERROR) { |
592 | 0 | smart_str_appendl(buf, "null", 4); |
593 | 0 | } |
594 | 13 | ZEND_GUARD_UNPROTECT_RECURSION(guard, JSON); |
595 | 13 | OBJ_RELEASE(obj); |
596 | 13 | return FAILURE; |
597 | 13 | } |
598 | | |
599 | 24 | if (Z_TYPE(retval) == IS_OBJECT && Z_OBJ(retval) == obj) { |
600 | | /* Handle the case where jsonSerialize does: return $this; by going straight to encode array */ |
601 | 0 | ZEND_GUARD_UNPROTECT_RECURSION(guard, JSON); |
602 | 0 | return_code = php_json_encode_array(buf, &retval, options, encoder); |
603 | 24 | } else { |
604 | | /* All other types, encode as normal */ |
605 | 24 | return_code = php_json_encode_zval(buf, &retval, options, encoder); |
606 | 24 | ZEND_GUARD_UNPROTECT_RECURSION(guard, JSON); |
607 | 24 | } |
608 | | |
609 | 24 | zval_ptr_dtor(&retval); |
610 | 24 | OBJ_RELEASE(obj); |
611 | | |
612 | 24 | return return_code; |
613 | 37 | } |
614 | | |
615 | | static zend_result php_json_encode_serializable_enum(smart_str *buf, zval *val, int options, php_json_encoder *encoder) |
616 | 157 | { |
617 | 157 | const zend_class_entry *ce = Z_OBJCE_P(val); |
618 | 157 | if (ce->enum_backing_type == IS_UNDEF) { |
619 | 65 | encoder->error_code = PHP_JSON_ERROR_NON_BACKED_ENUM; |
620 | 65 | smart_str_appendc(buf, '0'); |
621 | 65 | return FAILURE; |
622 | 65 | } |
623 | | |
624 | 92 | zval *value_zv = zend_enum_fetch_case_value(Z_OBJ_P(val)); |
625 | 92 | return php_json_encode_zval(buf, value_zv, options, encoder); |
626 | 157 | } |
627 | | |
628 | | zend_result php_json_encode_zval(smart_str *buf, zval *val, int options, php_json_encoder *encoder) /* {{{ */ |
629 | 4.69k | { |
630 | 4.72k | again: |
631 | 4.72k | switch (Z_TYPE_P(val)) |
632 | 4.72k | { |
633 | 297 | case IS_NULL: |
634 | 297 | smart_str_appendl(buf, "null", 4); |
635 | 297 | break; |
636 | | |
637 | 309 | case IS_TRUE: |
638 | 309 | smart_str_appendl(buf, "true", 4); |
639 | 309 | break; |
640 | 261 | case IS_FALSE: |
641 | 261 | smart_str_appendl(buf, "false", 5); |
642 | 261 | break; |
643 | | |
644 | 1.99k | case IS_LONG: |
645 | 1.99k | smart_str_append_long(buf, Z_LVAL_P(val)); |
646 | 1.99k | break; |
647 | | |
648 | 166 | case IS_DOUBLE: |
649 | 166 | if (php_json_is_valid_double(Z_DVAL_P(val))) { |
650 | 147 | php_json_encode_double(buf, Z_DVAL_P(val), options); |
651 | 147 | } else { |
652 | 19 | encoder->error_code = PHP_JSON_ERROR_INF_OR_NAN; |
653 | 19 | smart_str_appendc(buf, '0'); |
654 | 19 | } |
655 | 166 | break; |
656 | | |
657 | 1.09k | case IS_STRING: |
658 | 1.09k | return php_json_escape_string(buf, Z_STRVAL_P(val), Z_STRLEN_P(val), options, encoder); |
659 | | |
660 | 457 | case IS_OBJECT: |
661 | 457 | if (instanceof_function(Z_OBJCE_P(val), php_json_serializable_ce)) { |
662 | 37 | return php_json_encode_serializable_object(buf, Z_OBJ_P(val), options, encoder); |
663 | 37 | } |
664 | 420 | if (Z_OBJCE_P(val)->ce_flags & ZEND_ACC_ENUM) { |
665 | 157 | return php_json_encode_serializable_enum(buf, val, options, encoder); |
666 | 157 | } |
667 | | /* fallthrough -- Non-serializable object */ |
668 | 263 | ZEND_FALLTHROUGH; |
669 | 386 | case IS_ARRAY: { |
670 | | /* Avoid modifications (and potential freeing) of the array through a reference when a |
671 | | * jsonSerialize() method is invoked. */ |
672 | 386 | zval zv; |
673 | 386 | zend_result res; |
674 | 386 | ZVAL_COPY(&zv, val); |
675 | 386 | res = php_json_encode_array(buf, &zv, options, encoder); |
676 | 386 | zval_ptr_dtor_nogc(&zv); |
677 | 386 | return res; |
678 | 263 | } |
679 | | |
680 | 29 | case IS_REFERENCE: |
681 | 29 | val = Z_REFVAL_P(val); |
682 | 29 | goto again; |
683 | | |
684 | 0 | default: |
685 | 0 | encoder->error_code = PHP_JSON_ERROR_UNSUPPORTED_TYPE; |
686 | 0 | if (options & PHP_JSON_PARTIAL_OUTPUT_ON_ERROR) { |
687 | 0 | smart_str_appendl(buf, "null", 4); |
688 | 0 | } |
689 | 0 | return FAILURE; |
690 | 4.72k | } |
691 | | |
692 | 3.02k | return SUCCESS; |
693 | 4.72k | } |
694 | | /* }}} */ |