/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 | 189 | { |
35 | 189 | #ifdef ZEND_CHECK_STACK_LIMIT |
36 | 189 | return zend_call_stack_overflowed(EG(stack_limit)); |
37 | | #else |
38 | | return false; |
39 | | #endif |
40 | 189 | } |
41 | | |
42 | | /* {{{ Pretty printing support functions */ |
43 | | |
44 | | static inline void php_json_pretty_print_char(smart_str *buf, int options, char c) /* {{{ */ |
45 | 1.04k | { |
46 | 1.04k | if (options & PHP_JSON_PRETTY_PRINT) { |
47 | 0 | smart_str_appendc(buf, c); |
48 | 0 | } |
49 | 1.04k | } |
50 | | /* }}} */ |
51 | | |
52 | | static inline void php_json_pretty_print_indent(smart_str *buf, int options, const php_json_encoder *encoder) /* {{{ */ |
53 | 782 | { |
54 | 782 | 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 | 782 | } |
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 | 68 | { |
74 | 68 | return !zend_isinf(d) && !zend_isnan(d); |
75 | 68 | } |
76 | | /* }}} */ |
77 | | |
78 | | static inline void php_json_encode_double(smart_str *buf, double d, int options) /* {{{ */ |
79 | 68 | { |
80 | 68 | size_t len; |
81 | 68 | char num[ZEND_DOUBLE_MAX_LENGTH]; |
82 | | |
83 | 68 | zend_gcvt(d, (int)PG(serialize_precision), '.', 'e', num); |
84 | 68 | len = strlen(num); |
85 | 68 | if (options & PHP_JSON_PRESERVE_ZERO_FRACTION && strchr(num, '.') == NULL && len < ZEND_DOUBLE_MAX_LENGTH - 2) { |
86 | 56 | num[len++] = '.'; |
87 | 56 | num[len++] = '0'; |
88 | 56 | num[len] = '\0'; |
89 | 56 | } |
90 | 68 | smart_str_appendl(buf, num, len); |
91 | 68 | } |
92 | | /* }}} */ |
93 | | |
94 | | #define PHP_JSON_HASH_PROTECT_RECURSION(_tmp_ht) \ |
95 | 189 | do { \ |
96 | 189 | if (_tmp_ht) { \ |
97 | 186 | GC_TRY_PROTECT_RECURSION(_tmp_ht); \ |
98 | 186 | } \ |
99 | 189 | } while (0) |
100 | | |
101 | | #define PHP_JSON_HASH_UNPROTECT_RECURSION(_tmp_ht) \ |
102 | 189 | do { \ |
103 | 189 | if (_tmp_ht) { \ |
104 | 186 | GC_TRY_UNPROTECT_RECURSION(_tmp_ht); \ |
105 | 186 | } \ |
106 | 189 | } while (0) |
107 | | |
108 | | static zend_result php_json_encode_array(smart_str *buf, zval *val, int options, php_json_encoder *encoder) /* {{{ */ |
109 | 189 | { |
110 | 189 | bool encode_as_object = options & PHP_JSON_FORCE_OBJECT; |
111 | 189 | HashTable *myht, *prop_ht; |
112 | 189 | zend_refcounted *recursion_rc; |
113 | | |
114 | 189 | 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 | 189 | if (Z_TYPE_P(val) == IS_ARRAY) { |
123 | 45 | myht = Z_ARRVAL_P(val); |
124 | 45 | recursion_rc = (zend_refcounted *)myht; |
125 | 45 | prop_ht = NULL; |
126 | 45 | encode_as_object = encode_as_object || !zend_array_is_list(myht); |
127 | 144 | } else if (Z_OBJ_P(val)->properties == NULL |
128 | 54 | && Z_OBJ_HT_P(val)->get_properties_for == NULL |
129 | 51 | && Z_OBJ_HT_P(val)->get_properties == zend_std_get_properties |
130 | 51 | && Z_OBJ_P(val)->ce->num_hooked_props == 0 |
131 | 18 | && !zend_object_is_lazy(Z_OBJ_P(val))) { |
132 | | /* Optimized version without rebuilding properties HashTable */ |
133 | 0 | zend_object *obj = Z_OBJ_P(val); |
134 | 0 | const zend_class_entry *ce = obj->ce; |
135 | |
|
136 | 0 | 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 | 0 | PHP_JSON_HASH_PROTECT_RECURSION(obj); |
143 | |
|
144 | 0 | smart_str_appendc(buf, '{'); |
145 | |
|
146 | 0 | ++encoder->depth; |
147 | |
|
148 | 0 | 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 | 0 | bool empty = ZSTR_VAL(buf->s)[ZSTR_LEN(buf->s) - 1] != ','; |
186 | 0 | if (!empty) { |
187 | | /* Drop the trailing comma. */ |
188 | 0 | ZSTR_LEN(buf->s)--; |
189 | 0 | } |
190 | |
|
191 | 0 | PHP_JSON_HASH_UNPROTECT_RECURSION(obj); |
192 | 0 | 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 | 0 | --encoder->depth; |
199 | |
|
200 | 0 | 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 | 0 | smart_str_appendc(buf, '}'); |
205 | 0 | return SUCCESS; |
206 | 144 | } else { |
207 | 144 | zend_object *obj = Z_OBJ_P(val); |
208 | 144 | prop_ht = myht = zend_get_properties_for(val, ZEND_PROP_PURPOSE_JSON); |
209 | 144 | if (obj->ce->num_hooked_props == 0) { |
210 | 33 | recursion_rc = (zend_refcounted *)prop_ht; |
211 | 111 | } 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 | 111 | recursion_rc = (zend_refcounted *)obj; |
215 | 111 | } |
216 | 144 | encode_as_object = true; |
217 | 144 | } |
218 | | |
219 | 189 | 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 | 189 | PHP_JSON_HASH_PROTECT_RECURSION(recursion_rc); |
227 | | |
228 | 189 | if (!encode_as_object) { |
229 | 42 | smart_str_appendc(buf, '['); |
230 | 147 | } else { |
231 | 147 | smart_str_appendc(buf, '{'); |
232 | 147 | } |
233 | | |
234 | 189 | ++encoder->depth; |
235 | | |
236 | 189 | uint32_t i = myht ? zend_hash_num_elements(myht) : 0; |
237 | | |
238 | 189 | bool empty = true; |
239 | 189 | if (i > 0) { |
240 | 153 | zend_string *key; |
241 | 153 | zval *data; |
242 | 153 | zend_ulong index; |
243 | | |
244 | 1.82k | ZEND_HASH_FOREACH_KEY_VAL_IND(myht, index, key, data) { |
245 | 1.82k | bool need_dtor = false; |
246 | 1.82k | zval tmp; |
247 | 1.82k | ZVAL_UNDEF(&tmp); |
248 | | |
249 | 1.82k | if (!encode_as_object) { |
250 | 377 | ZEND_ASSERT(Z_TYPE_P(data) != IS_PTR); |
251 | | |
252 | 377 | php_json_pretty_print_char(buf, options, '\n'); |
253 | 377 | php_json_pretty_print_indent(buf, options, encoder); |
254 | 456 | } else { |
255 | 456 | if (key) { |
256 | 456 | if (ZSTR_VAL(key)[0] == '\0' && ZSTR_LEN(key) > 0 && Z_TYPE_P(val) == IS_OBJECT) { |
257 | | /* Skip protected and private members. */ |
258 | 141 | continue; |
259 | 141 | } |
260 | | |
261 | | /* data is IS_PTR for properties with hooks. */ |
262 | 315 | if (UNEXPECTED(Z_TYPE_P(data) == IS_PTR)) { |
263 | 240 | zend_property_info *prop_info = Z_PTR_P(data); |
264 | 240 | if ((prop_info->flags & ZEND_ACC_VIRTUAL) && !prop_info->hooks[ZEND_PROPERTY_HOOK_GET]) { |
265 | 51 | continue; |
266 | 51 | } |
267 | 189 | need_dtor = true; |
268 | 189 | data = zend_read_property_ex(prop_info->ce, Z_OBJ_P(val), prop_info->name, /* silent */ true, &tmp); |
269 | 189 | if (EG(exception)) { |
270 | 0 | PHP_JSON_HASH_UNPROTECT_RECURSION(recursion_rc); |
271 | 0 | zend_release_properties(prop_ht); |
272 | 0 | return FAILURE; |
273 | 0 | } |
274 | 189 | } |
275 | | |
276 | | |
277 | 264 | php_json_pretty_print_char(buf, options, '\n'); |
278 | 264 | php_json_pretty_print_indent(buf, options, encoder); |
279 | | |
280 | 264 | if (php_json_escape_string(buf, ZSTR_VAL(key), ZSTR_LEN(key), |
281 | 264 | options & ~PHP_JSON_NUMERIC_CHECK, encoder) == FAILURE && |
282 | 0 | (options & PHP_JSON_PARTIAL_OUTPUT_ON_ERROR) && |
283 | 0 | buf->s) { |
284 | 0 | ZSTR_LEN(buf->s) -= 4; |
285 | 0 | smart_str_appendl(buf, "\"\"", 2); |
286 | 0 | } |
287 | 264 | } else { |
288 | 0 | php_json_pretty_print_char(buf, options, '\n'); |
289 | 0 | php_json_pretty_print_indent(buf, options, encoder); |
290 | |
|
291 | 0 | smart_str_appendc(buf, '"'); |
292 | 0 | smart_str_append_long(buf, (zend_long) index); |
293 | 0 | smart_str_appendc(buf, '"'); |
294 | 0 | } |
295 | | |
296 | 264 | smart_str_appendc(buf, ':'); |
297 | 264 | php_json_pretty_print_char(buf, options, ' '); |
298 | 264 | } |
299 | | |
300 | 641 | if (php_json_encode_zval(buf, data, options, encoder) == FAILURE && |
301 | 3 | !(options & PHP_JSON_PARTIAL_OUTPUT_ON_ERROR)) { |
302 | 3 | PHP_JSON_HASH_UNPROTECT_RECURSION(recursion_rc); |
303 | 3 | zend_release_properties(prop_ht); |
304 | 3 | zval_ptr_dtor(&tmp); |
305 | 3 | return FAILURE; |
306 | 3 | } |
307 | 638 | if (UNEXPECTED(need_dtor)) { |
308 | 189 | zval_ptr_dtor(&tmp); |
309 | 189 | } |
310 | | |
311 | 638 | smart_str_appendc(buf, ','); |
312 | 638 | } ZEND_HASH_FOREACH_END(); |
313 | | |
314 | 150 | empty = ZSTR_VAL(buf->s)[ZSTR_LEN(buf->s) - 1] != ','; |
315 | 150 | if (!empty) { |
316 | | /* Drop the trailing comma. */ |
317 | 141 | ZSTR_LEN(buf->s)--; |
318 | 141 | } |
319 | 150 | } |
320 | | |
321 | 186 | PHP_JSON_HASH_UNPROTECT_RECURSION(recursion_rc); |
322 | | |
323 | 186 | if (encoder->depth > encoder->max_depth) { |
324 | 0 | encoder->error_code = PHP_JSON_ERROR_DEPTH; |
325 | 0 | if (!(options & PHP_JSON_PARTIAL_OUTPUT_ON_ERROR)) { |
326 | 0 | zend_release_properties(prop_ht); |
327 | 0 | return FAILURE; |
328 | 0 | } |
329 | 0 | } |
330 | 186 | --encoder->depth; |
331 | | |
332 | | /* Only keep closing bracket on same line for empty arrays/objects */ |
333 | 186 | if (!empty) { |
334 | 141 | php_json_pretty_print_char(buf, options, '\n'); |
335 | 141 | php_json_pretty_print_indent(buf, options, encoder); |
336 | 141 | } |
337 | | |
338 | 186 | if (!encode_as_object) { |
339 | 42 | smart_str_appendc(buf, ']'); |
340 | 144 | } else { |
341 | 144 | smart_str_appendc(buf, '}'); |
342 | 144 | } |
343 | | |
344 | 186 | zend_release_properties(prop_ht); |
345 | 186 | return SUCCESS; |
346 | 186 | } |
347 | | /* }}} */ |
348 | | |
349 | | zend_result php_json_escape_string( |
350 | | smart_str *buf, const char *s, size_t len, |
351 | | int options, php_json_encoder *encoder) /* {{{ */ |
352 | 750 | { |
353 | 750 | size_t pos, checkpoint; |
354 | 750 | char *dst; |
355 | | |
356 | 750 | if (len == 0) { |
357 | 40 | smart_str_appendl(buf, "\"\"", 2); |
358 | 40 | return SUCCESS; |
359 | 40 | } |
360 | | |
361 | 710 | if (options & PHP_JSON_NUMERIC_CHECK) { |
362 | 0 | double d; |
363 | 0 | int type; |
364 | 0 | zend_long p; |
365 | |
|
366 | 0 | if ((type = is_numeric_string(s, len, &p, &d, 0)) != 0) { |
367 | 0 | if (type == IS_LONG) { |
368 | 0 | smart_str_append_long(buf, p); |
369 | 0 | return SUCCESS; |
370 | 0 | } else if (type == IS_DOUBLE && php_json_is_valid_double(d)) { |
371 | 0 | php_json_encode_double(buf, d, options); |
372 | 0 | return SUCCESS; |
373 | 0 | } |
374 | 0 | } |
375 | |
|
376 | 0 | } |
377 | 710 | checkpoint = buf->s ? ZSTR_LEN(buf->s) : 0; |
378 | | |
379 | | /* pre-allocate for string length plus 2 quotes */ |
380 | 710 | smart_str_alloc(buf, len+2, 0); |
381 | 710 | smart_str_appendc(buf, '"'); |
382 | | |
383 | 710 | pos = 0; |
384 | | |
385 | 3.95k | do { |
386 | 3.95k | static const uint32_t charmap[8] = { |
387 | 3.95k | 0xffffffff, 0x500080c4, 0x10000000, 0x00000000, |
388 | 3.95k | 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff}; |
389 | | |
390 | 3.95k | unsigned int us = (unsigned char)s[pos]; |
391 | 3.95k | if (EXPECTED(!ZEND_BIT_TEST(charmap, us))) { |
392 | 3.91k | pos++; |
393 | 3.91k | len--; |
394 | 3.91k | if (len == 0) { |
395 | 694 | smart_str_appendl(buf, s, pos); |
396 | 694 | break; |
397 | 694 | } |
398 | 3.91k | } else { |
399 | 37 | if (pos) { |
400 | 25 | smart_str_appendl(buf, s, pos); |
401 | 25 | s += pos; |
402 | 25 | pos = 0; |
403 | 25 | } |
404 | 37 | us = (unsigned char)s[0]; |
405 | 37 | if (UNEXPECTED(us >= 0x80)) { |
406 | 13 | zend_result status; |
407 | 13 | us = php_next_utf8_char((unsigned char *)s, len, &pos, &status); |
408 | | |
409 | | /* check whether UTF8 character is correct */ |
410 | 13 | if (UNEXPECTED(status != SUCCESS)) { |
411 | 13 | if (options & PHP_JSON_INVALID_UTF8_IGNORE) { |
412 | | /* ignore invalid UTF8 character */ |
413 | 13 | } else if (options & PHP_JSON_INVALID_UTF8_SUBSTITUTE) { |
414 | | /* Use Unicode character 'REPLACEMENT CHARACTER' (U+FFFD) */ |
415 | 0 | if (options & PHP_JSON_UNESCAPED_UNICODE) { |
416 | 0 | smart_str_appendl(buf, "\xef\xbf\xbd", 3); |
417 | 0 | } else { |
418 | 0 | smart_str_appendl(buf, "\\ufffd", 6); |
419 | 0 | } |
420 | 13 | } else { |
421 | 13 | ZSTR_LEN(buf->s) = checkpoint; |
422 | 13 | encoder->error_code = PHP_JSON_ERROR_UTF8; |
423 | 13 | if (options & PHP_JSON_PARTIAL_OUTPUT_ON_ERROR) { |
424 | 0 | smart_str_appendl(buf, "null", 4); |
425 | 0 | } |
426 | 13 | return FAILURE; |
427 | 13 | } |
428 | | |
429 | | /* Escape U+2028/U+2029 line terminators, UNLESS both |
430 | | JSON_UNESCAPED_UNICODE and |
431 | | JSON_UNESCAPED_LINE_TERMINATORS were provided */ |
432 | 13 | } else if ((options & PHP_JSON_UNESCAPED_UNICODE) |
433 | 0 | && ((options & PHP_JSON_UNESCAPED_LINE_TERMINATORS) |
434 | 0 | || us < 0x2028 || us > 0x2029)) { |
435 | 0 | smart_str_appendl(buf, s, pos); |
436 | 0 | } else { |
437 | | /* From http://en.wikipedia.org/wiki/UTF16 */ |
438 | 0 | if (us >= 0x10000) { |
439 | 0 | unsigned int next_us; |
440 | |
|
441 | 0 | us -= 0x10000; |
442 | 0 | next_us = (unsigned short)((us & 0x3ff) | 0xdc00); |
443 | 0 | us = (unsigned short)((us >> 10) | 0xd800); |
444 | 0 | dst = smart_str_extend(buf, 6); |
445 | 0 | dst[0] = '\\'; |
446 | 0 | dst[1] = 'u'; |
447 | 0 | dst[2] = digits[(us >> 12) & 0xf]; |
448 | 0 | dst[3] = digits[(us >> 8) & 0xf]; |
449 | 0 | dst[4] = digits[(us >> 4) & 0xf]; |
450 | 0 | dst[5] = digits[us & 0xf]; |
451 | 0 | us = next_us; |
452 | 0 | } |
453 | 0 | dst = smart_str_extend(buf, 6); |
454 | 0 | dst[0] = '\\'; |
455 | 0 | dst[1] = 'u'; |
456 | 0 | dst[2] = digits[(us >> 12) & 0xf]; |
457 | 0 | dst[3] = digits[(us >> 8) & 0xf]; |
458 | 0 | dst[4] = digits[(us >> 4) & 0xf]; |
459 | 0 | dst[5] = digits[us & 0xf]; |
460 | 0 | } |
461 | 0 | s += pos; |
462 | 0 | len -= pos; |
463 | 0 | pos = 0; |
464 | 24 | } else { |
465 | 24 | s++; |
466 | 24 | switch (us) { |
467 | 3 | case '"': |
468 | 3 | if (options & PHP_JSON_HEX_QUOT) { |
469 | 0 | smart_str_appendl(buf, "\\u0022", 6); |
470 | 3 | } else { |
471 | 3 | smart_str_appendl(buf, "\\\"", 2); |
472 | 3 | } |
473 | 3 | break; |
474 | | |
475 | 0 | case '\\': |
476 | 0 | smart_str_appendl(buf, "\\\\", 2); |
477 | 0 | break; |
478 | | |
479 | 0 | case '/': |
480 | 0 | if (options & PHP_JSON_UNESCAPED_SLASHES) { |
481 | 0 | smart_str_appendc(buf, '/'); |
482 | 0 | } else { |
483 | 0 | smart_str_appendl(buf, "\\/", 2); |
484 | 0 | } |
485 | 0 | break; |
486 | | |
487 | 3 | case '\b': |
488 | 3 | smart_str_appendl(buf, "\\b", 2); |
489 | 3 | break; |
490 | | |
491 | 0 | case '\f': |
492 | 0 | smart_str_appendl(buf, "\\f", 2); |
493 | 0 | break; |
494 | | |
495 | 0 | case '\n': |
496 | 0 | smart_str_appendl(buf, "\\n", 2); |
497 | 0 | break; |
498 | | |
499 | 0 | case '\r': |
500 | 0 | smart_str_appendl(buf, "\\r", 2); |
501 | 0 | break; |
502 | | |
503 | 0 | case '\t': |
504 | 0 | smart_str_appendl(buf, "\\t", 2); |
505 | 0 | break; |
506 | | |
507 | 0 | case '<': |
508 | 0 | if (options & PHP_JSON_HEX_TAG) { |
509 | 0 | smart_str_appendl(buf, "\\u003C", 6); |
510 | 0 | } else { |
511 | 0 | smart_str_appendc(buf, '<'); |
512 | 0 | } |
513 | 0 | break; |
514 | | |
515 | 0 | case '>': |
516 | 0 | if (options & PHP_JSON_HEX_TAG) { |
517 | 0 | smart_str_appendl(buf, "\\u003E", 6); |
518 | 0 | } else { |
519 | 0 | smart_str_appendc(buf, '>'); |
520 | 0 | } |
521 | 0 | break; |
522 | | |
523 | 0 | case '&': |
524 | 0 | if (options & PHP_JSON_HEX_AMP) { |
525 | 0 | smart_str_appendl(buf, "\\u0026", 6); |
526 | 0 | } else { |
527 | 0 | smart_str_appendc(buf, '&'); |
528 | 0 | } |
529 | 0 | break; |
530 | | |
531 | 0 | case '\'': |
532 | 0 | if (options & PHP_JSON_HEX_APOS) { |
533 | 0 | smart_str_appendl(buf, "\\u0027", 6); |
534 | 0 | } else { |
535 | 0 | smart_str_appendc(buf, '\''); |
536 | 0 | } |
537 | 0 | break; |
538 | | |
539 | 18 | default: |
540 | 18 | ZEND_ASSERT(us < ' '); |
541 | 18 | dst = smart_str_extend(buf, 6); |
542 | 18 | dst[0] = '\\'; |
543 | 18 | dst[1] = 'u'; |
544 | 18 | dst[2] = '0'; |
545 | 18 | dst[3] = '0'; |
546 | 18 | dst[4] = digits[(us >> 4) & 0xf]; |
547 | 18 | dst[5] = digits[us & 0xf]; |
548 | 18 | break; |
549 | 24 | } |
550 | 24 | len--; |
551 | 24 | } |
552 | 37 | } |
553 | 3.95k | } while (len); |
554 | | |
555 | 697 | smart_str_appendc(buf, '"'); |
556 | | |
557 | 697 | return SUCCESS; |
558 | 710 | } |
559 | | /* }}} */ |
560 | | |
561 | | static zend_result php_json_encode_serializable_object(smart_str *buf, zend_object *obj, int options, php_json_encoder *encoder) |
562 | 12 | { |
563 | 12 | zend_class_entry *ce = obj->ce; |
564 | 12 | uint32_t *guard = zend_get_recursion_guard(obj); |
565 | 12 | zval retval; |
566 | 12 | zend_result return_code; |
567 | | |
568 | 12 | ZEND_ASSERT(guard != NULL); |
569 | | |
570 | 12 | if (ZEND_GUARD_IS_RECURSIVE(guard, JSON)) { |
571 | 0 | encoder->error_code = PHP_JSON_ERROR_RECURSION; |
572 | 0 | if (options & PHP_JSON_PARTIAL_OUTPUT_ON_ERROR) { |
573 | 0 | smart_str_appendl(buf, "null", 4); |
574 | 0 | } |
575 | 0 | return FAILURE; |
576 | 0 | } |
577 | | |
578 | 12 | ZEND_GUARD_PROTECT_RECURSION(guard, JSON); |
579 | | |
580 | 12 | zend_function *json_serialize_method = zend_hash_str_find_ptr(&ce->function_table, ZEND_STRL("jsonserialize")); |
581 | 12 | ZEND_ASSERT(json_serialize_method != NULL && "This should be guaranteed prior to calling this function"); |
582 | 12 | zend_call_known_function(json_serialize_method, obj, ce, &retval, 0, NULL, NULL); |
583 | | /* An exception has occurred */ |
584 | 12 | if (Z_TYPE(retval) == IS_UNDEF) { |
585 | 0 | if (options & PHP_JSON_PARTIAL_OUTPUT_ON_ERROR) { |
586 | 0 | smart_str_appendl(buf, "null", 4); |
587 | 0 | } |
588 | 0 | ZEND_GUARD_UNPROTECT_RECURSION(guard, JSON); |
589 | 0 | return FAILURE; |
590 | 0 | } |
591 | | |
592 | 12 | if (Z_TYPE(retval) == IS_OBJECT && Z_OBJ(retval) == obj) { |
593 | | /* Handle the case where jsonSerialize does: return $this; by going straight to encode array */ |
594 | 0 | ZEND_GUARD_UNPROTECT_RECURSION(guard, JSON); |
595 | 0 | return_code = php_json_encode_array(buf, &retval, options, encoder); |
596 | 12 | } else { |
597 | | /* All other types, encode as normal */ |
598 | 12 | return_code = php_json_encode_zval(buf, &retval, options, encoder); |
599 | 12 | ZEND_GUARD_UNPROTECT_RECURSION(guard, JSON); |
600 | 12 | } |
601 | | |
602 | 12 | zval_ptr_dtor(&retval); |
603 | | |
604 | 12 | return return_code; |
605 | 12 | } |
606 | | |
607 | | static zend_result php_json_encode_serializable_enum(smart_str *buf, zval *val, int options, php_json_encoder *encoder) |
608 | 48 | { |
609 | 48 | const zend_class_entry *ce = Z_OBJCE_P(val); |
610 | 48 | if (ce->enum_backing_type == IS_UNDEF) { |
611 | 24 | encoder->error_code = PHP_JSON_ERROR_NON_BACKED_ENUM; |
612 | 24 | smart_str_appendc(buf, '0'); |
613 | 24 | return FAILURE; |
614 | 24 | } |
615 | | |
616 | 24 | zval *value_zv = zend_enum_fetch_case_value(Z_OBJ_P(val)); |
617 | 24 | return php_json_encode_zval(buf, value_zv, options, encoder); |
618 | 48 | } |
619 | | |
620 | | zend_result php_json_encode_zval(smart_str *buf, zval *val, int options, php_json_encoder *encoder) /* {{{ */ |
621 | 1.76k | { |
622 | 1.76k | again: |
623 | 1.76k | switch (Z_TYPE_P(val)) |
624 | 1.76k | { |
625 | 123 | case IS_NULL: |
626 | 123 | smart_str_appendl(buf, "null", 4); |
627 | 123 | break; |
628 | | |
629 | 99 | case IS_TRUE: |
630 | 99 | smart_str_appendl(buf, "true", 4); |
631 | 99 | break; |
632 | 94 | case IS_FALSE: |
633 | 94 | smart_str_appendl(buf, "false", 5); |
634 | 94 | break; |
635 | | |
636 | 642 | case IS_LONG: |
637 | 642 | smart_str_append_long(buf, Z_LVAL_P(val)); |
638 | 642 | break; |
639 | | |
640 | 68 | case IS_DOUBLE: |
641 | 68 | if (php_json_is_valid_double(Z_DVAL_P(val))) { |
642 | 68 | php_json_encode_double(buf, Z_DVAL_P(val), options); |
643 | 68 | } else { |
644 | 0 | encoder->error_code = PHP_JSON_ERROR_INF_OR_NAN; |
645 | 0 | smart_str_appendc(buf, '0'); |
646 | 0 | } |
647 | 68 | break; |
648 | | |
649 | 486 | case IS_STRING: |
650 | 486 | return php_json_escape_string(buf, Z_STRVAL_P(val), Z_STRLEN_P(val), options, encoder); |
651 | | |
652 | 204 | case IS_OBJECT: |
653 | 204 | if (instanceof_function(Z_OBJCE_P(val), php_json_serializable_ce)) { |
654 | 12 | return php_json_encode_serializable_object(buf, Z_OBJ_P(val), options, encoder); |
655 | 12 | } |
656 | 192 | if (Z_OBJCE_P(val)->ce_flags & ZEND_ACC_ENUM) { |
657 | 48 | return php_json_encode_serializable_enum(buf, val, options, encoder); |
658 | 48 | } |
659 | | /* fallthrough -- Non-serializable object */ |
660 | 144 | ZEND_FALLTHROUGH; |
661 | 189 | case IS_ARRAY: { |
662 | | /* Avoid modifications (and potential freeing) of the array through a reference when a |
663 | | * jsonSerialize() method is invoked. */ |
664 | 189 | zval zv; |
665 | 189 | zend_result res; |
666 | 189 | ZVAL_COPY(&zv, val); |
667 | 189 | res = php_json_encode_array(buf, &zv, options, encoder); |
668 | 189 | zval_ptr_dtor_nogc(&zv); |
669 | 189 | return res; |
670 | 144 | } |
671 | | |
672 | 3 | case IS_REFERENCE: |
673 | 3 | val = Z_REFVAL_P(val); |
674 | 3 | goto again; |
675 | | |
676 | 0 | default: |
677 | 0 | encoder->error_code = PHP_JSON_ERROR_UNSUPPORTED_TYPE; |
678 | 0 | if (options & PHP_JSON_PARTIAL_OUTPUT_ON_ERROR) { |
679 | 0 | smart_str_appendl(buf, "null", 4); |
680 | 0 | } |
681 | 0 | return FAILURE; |
682 | 1.76k | } |
683 | | |
684 | 1.02k | return SUCCESS; |
685 | 1.76k | } |
686 | | /* }}} */ |