/src/php-src/ext/standard/var.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 | | | Authors: Jani Lehtimäki <jkl@njet.net> | |
12 | | | Thies C. Arntzen <thies@thieso.net> | |
13 | | | Sascha Schumann <sascha@schumann.cx> | |
14 | | +----------------------------------------------------------------------+ |
15 | | */ |
16 | | |
17 | | /* {{{ includes */ |
18 | | #include <stdio.h> |
19 | | #include <stdlib.h> |
20 | | #include <errno.h> |
21 | | #include "php.h" |
22 | | #include "php_string.h" |
23 | | #include "php_var.h" |
24 | | #include "zend_lazy_objects.h" |
25 | | #include "zend_smart_str.h" |
26 | | #include "basic_functions.h" |
27 | | #include "php_incomplete_class.h" |
28 | | #include "zend_enum.h" |
29 | | #include "zend_exceptions.h" |
30 | | #include "zend_types.h" |
31 | | /* }}} */ |
32 | | |
33 | | struct php_serialize_data { |
34 | | HashTable ht; |
35 | | uint32_t n; |
36 | | }; |
37 | | |
38 | 0 | #define COMMON (is_ref ? "&" : "") |
39 | | |
40 | | static void php_array_element_dump(zval *zv, zend_ulong index, zend_string *key, int level) /* {{{ */ |
41 | 0 | { |
42 | 0 | if (key == NULL) { /* numeric key */ |
43 | 0 | php_printf("%*c[" ZEND_LONG_FMT "]=>\n", level + 1, ' ', index); |
44 | 0 | } else { /* string key */ |
45 | 0 | php_printf("%*c[\"", level + 1, ' '); |
46 | 0 | PHPWRITE(ZSTR_VAL(key), ZSTR_LEN(key)); |
47 | 0 | php_printf("\"]=>\n"); |
48 | 0 | } |
49 | 0 | php_var_dump(zv, level + 2); |
50 | 0 | } |
51 | | /* }}} */ |
52 | | |
53 | | static void php_object_property_dump(zend_property_info *prop_info, zval *zv, zend_ulong index, zend_string *key, int level) /* {{{ */ |
54 | 0 | { |
55 | 0 | const char *prop_name, *class_name; |
56 | |
|
57 | 0 | #ifdef ZEND_CHECK_STACK_LIMIT |
58 | 0 | if (UNEXPECTED(zend_call_stack_overflowed(EG(stack_limit)))) { |
59 | 0 | php_printf("%*cnesting level too deep", level + 1, ' '); |
60 | 0 | return; |
61 | 0 | } |
62 | 0 | #endif |
63 | 0 | if (key == NULL) { /* numeric key */ |
64 | 0 | php_printf("%*c[" ZEND_LONG_FMT "]=>\n", level + 1, ' ', index); |
65 | 0 | } else { /* string key */ |
66 | 0 | int unmangle = zend_unmangle_property_name(key, &class_name, &prop_name); |
67 | 0 | php_printf("%*c[", level + 1, ' '); |
68 | |
|
69 | 0 | if (class_name && unmangle == SUCCESS) { |
70 | 0 | if (class_name[0] == '*') { |
71 | 0 | php_printf("\"%s\":protected", prop_name); |
72 | 0 | } else { |
73 | 0 | php_printf("\"%s\":\"%s\":private", prop_name, class_name); |
74 | 0 | } |
75 | 0 | } else { |
76 | 0 | php_printf("\""); |
77 | 0 | PHPWRITE(ZSTR_VAL(key), ZSTR_LEN(key)); |
78 | 0 | php_printf("\""); |
79 | 0 | } |
80 | 0 | ZEND_PUTS("]=>\n"); |
81 | 0 | } |
82 | |
|
83 | 0 | if (Z_TYPE_P(zv) == IS_UNDEF) { |
84 | 0 | ZEND_ASSERT(ZEND_TYPE_IS_SET(prop_info->type)); |
85 | 0 | zend_string *type_str = zend_type_to_string(prop_info->type); |
86 | 0 | php_printf("%*cuninitialized(%s)\n", |
87 | 0 | level + 1, ' ', ZSTR_VAL(type_str)); |
88 | 0 | zend_string_release(type_str); |
89 | 0 | } else { |
90 | 0 | php_var_dump(zv, level + 2); |
91 | 0 | } |
92 | 0 | } |
93 | | /* }}} */ |
94 | | |
95 | 0 | static const char *php_var_dump_object_prefix(zend_object *obj) { |
96 | 0 | if (EXPECTED(!zend_object_is_lazy(obj))) { |
97 | 0 | return ""; |
98 | 0 | } |
99 | | |
100 | 0 | if (zend_object_is_lazy_proxy(obj)) { |
101 | 0 | return "lazy proxy "; |
102 | 0 | } |
103 | | |
104 | 0 | return "lazy ghost "; |
105 | 0 | } |
106 | | |
107 | | PHPAPI void php_var_dump(zval *struc, int level) /* {{{ */ |
108 | 0 | { |
109 | 0 | HashTable *myht; |
110 | 0 | zend_string *class_name; |
111 | 0 | int is_ref = 0; |
112 | 0 | zend_ulong num; |
113 | 0 | zend_string *key; |
114 | 0 | zval *val; |
115 | 0 | uint32_t count; |
116 | |
|
117 | 0 | if (level > 1) { |
118 | 0 | php_printf("%*c", level - 1, ' '); |
119 | 0 | } |
120 | |
|
121 | 0 | again: |
122 | 0 | switch (Z_TYPE_P(struc)) { |
123 | 0 | case IS_FALSE: |
124 | 0 | php_printf("%sbool(false)\n", COMMON); |
125 | 0 | break; |
126 | 0 | case IS_TRUE: |
127 | 0 | php_printf("%sbool(true)\n", COMMON); |
128 | 0 | break; |
129 | 0 | case IS_NULL: |
130 | 0 | php_printf("%sNULL\n", COMMON); |
131 | 0 | break; |
132 | 0 | case IS_LONG: |
133 | 0 | php_printf("%sint(" ZEND_LONG_FMT ")\n", COMMON, Z_LVAL_P(struc)); |
134 | 0 | break; |
135 | 0 | case IS_DOUBLE: |
136 | 0 | php_printf_unchecked("%sfloat(%.*H)\n", COMMON, (int) PG(serialize_precision), Z_DVAL_P(struc)); |
137 | 0 | break; |
138 | 0 | case IS_STRING: |
139 | 0 | php_printf("%sstring(%zd) \"", COMMON, Z_STRLEN_P(struc)); |
140 | 0 | PHPWRITE(Z_STRVAL_P(struc), Z_STRLEN_P(struc)); |
141 | 0 | PUTS("\"\n"); |
142 | 0 | break; |
143 | 0 | case IS_ARRAY: |
144 | 0 | myht = Z_ARRVAL_P(struc); |
145 | 0 | if (!(GC_FLAGS(myht) & GC_IMMUTABLE)) { |
146 | 0 | if (GC_IS_RECURSIVE(myht)) { |
147 | 0 | PUTS("*RECURSION*\n"); |
148 | 0 | return; |
149 | 0 | } |
150 | 0 | GC_ADDREF(myht); |
151 | 0 | GC_PROTECT_RECURSION(myht); |
152 | 0 | } |
153 | 0 | count = zend_hash_num_elements(myht); |
154 | 0 | php_printf("%sarray(%d) {\n", COMMON, count); |
155 | 0 | ZEND_HASH_FOREACH_KEY_VAL(myht, num, key, val) { |
156 | 0 | php_array_element_dump(val, num, key, level); |
157 | 0 | } ZEND_HASH_FOREACH_END(); |
158 | 0 | if (!(GC_FLAGS(myht) & GC_IMMUTABLE)) { |
159 | 0 | GC_UNPROTECT_RECURSION(myht); |
160 | 0 | GC_DTOR_NO_REF(myht); |
161 | 0 | } |
162 | 0 | if (level > 1) { |
163 | 0 | php_printf("%*c", level-1, ' '); |
164 | 0 | } |
165 | 0 | PUTS("}\n"); |
166 | 0 | break; |
167 | 0 | case IS_OBJECT: { |
168 | 0 | zend_class_entry *ce = Z_OBJCE_P(struc); |
169 | 0 | if ((ce->ce_flags & ZEND_ACC_ENUM) && ce->__debugInfo == NULL) { |
170 | 0 | zval *case_name_zval = zend_enum_fetch_case_name(Z_OBJ_P(struc)); |
171 | 0 | php_printf("%senum(%s::%s)\n", COMMON, ZSTR_VAL(ce->name), Z_STRVAL_P(case_name_zval)); |
172 | 0 | return; |
173 | 0 | } |
174 | 0 | zend_object *zobj = Z_OBJ_P(struc); |
175 | 0 | uint32_t *guard = zend_get_recursion_guard(zobj); |
176 | 0 | if (ZEND_GUARD_OR_GC_IS_RECURSIVE(guard, DEBUG, zobj)) { |
177 | 0 | PUTS("*RECURSION*\n"); |
178 | 0 | return; |
179 | 0 | } |
180 | 0 | ZEND_GUARD_OR_GC_PROTECT_RECURSION(guard, DEBUG, zobj); |
181 | |
|
182 | 0 | myht = zend_get_properties_for(struc, ZEND_PROP_PURPOSE_DEBUG); |
183 | 0 | if (ce->ce_flags & ZEND_ACC_ENUM) { |
184 | 0 | zval *case_name_zval = zend_enum_fetch_case_name(Z_OBJ_P(struc)); |
185 | 0 | php_printf("%senum(%s::%s) (%d) {\n", COMMON, ZSTR_VAL(ce->name), Z_STRVAL_P(case_name_zval), myht ? zend_array_count(myht) : 0); |
186 | 0 | } else { |
187 | 0 | class_name = Z_OBJ_HANDLER_P(struc, get_class_name)(Z_OBJ_P(struc)); |
188 | 0 | const char *prefix = php_var_dump_object_prefix(Z_OBJ_P(struc)); |
189 | |
|
190 | 0 | php_printf("%s%sobject(%s)#%d (%d) {\n", COMMON, prefix, ZSTR_VAL(class_name), Z_OBJ_HANDLE_P(struc), myht ? zend_array_count(myht) : 0); |
191 | 0 | zend_string_release_ex(class_name, 0); |
192 | 0 | } |
193 | |
|
194 | 0 | if (myht) { |
195 | 0 | zend_ulong num; |
196 | 0 | zend_string *key; |
197 | 0 | zval *val; |
198 | |
|
199 | 0 | ZEND_HASH_FOREACH_KEY_VAL(myht, num, key, val) { |
200 | 0 | zend_property_info *prop_info = NULL; |
201 | |
|
202 | 0 | if (Z_TYPE_P(val) == IS_INDIRECT) { |
203 | 0 | val = Z_INDIRECT_P(val); |
204 | 0 | if (key) { |
205 | 0 | prop_info = zend_get_typed_property_info_for_slot(Z_OBJ_P(struc), val); |
206 | 0 | } |
207 | 0 | } |
208 | |
|
209 | 0 | if (!Z_ISUNDEF_P(val) || prop_info) { |
210 | 0 | php_object_property_dump(prop_info, val, num, key, level); |
211 | 0 | } |
212 | 0 | } ZEND_HASH_FOREACH_END(); |
213 | 0 | zend_release_properties(myht); |
214 | 0 | } |
215 | 0 | if (level > 1) { |
216 | 0 | php_printf("%*c", level-1, ' '); |
217 | 0 | } |
218 | 0 | PUTS("}\n"); |
219 | 0 | ZEND_GUARD_OR_GC_UNPROTECT_RECURSION(guard, DEBUG, zobj); |
220 | 0 | break; |
221 | 0 | } |
222 | 0 | case IS_RESOURCE: { |
223 | 0 | const char *type_name = zend_rsrc_list_get_rsrc_type(Z_RES_P(struc)); |
224 | 0 | php_printf("%sresource(" ZEND_LONG_FMT ") of type (%s)\n", COMMON, Z_RES_P(struc)->handle, type_name ? type_name : "Unknown"); |
225 | 0 | break; |
226 | 0 | } |
227 | 0 | case IS_REFERENCE: |
228 | | //??? hide references with refcount==1 (for compatibility) |
229 | 0 | if (Z_REFCOUNT_P(struc) > 1) { |
230 | 0 | is_ref = 1; |
231 | 0 | } |
232 | 0 | struc = Z_REFVAL_P(struc); |
233 | 0 | goto again; |
234 | 0 | default: |
235 | 0 | php_printf("%sUNKNOWN:0\n", COMMON); |
236 | 0 | break; |
237 | 0 | } |
238 | 0 | } |
239 | | /* }}} */ |
240 | | |
241 | | /* {{{ Dumps a string representation of variable to output */ |
242 | | PHP_FUNCTION(var_dump) |
243 | 0 | { |
244 | 0 | zval *args; |
245 | 0 | int argc; |
246 | 0 | int i; |
247 | |
|
248 | 0 | ZEND_PARSE_PARAMETERS_START(1, -1) |
249 | 0 | Z_PARAM_VARIADIC('+', args, argc) |
250 | 0 | ZEND_PARSE_PARAMETERS_END(); |
251 | | |
252 | 0 | for (i = 0; i < argc; i++) { |
253 | 0 | php_var_dump(&args[i], 1); |
254 | 0 | } |
255 | 0 | } |
256 | | /* }}} */ |
257 | | |
258 | | static void zval_array_element_dump(zval *zv, zend_ulong index, zend_string *key, int level) /* {{{ */ |
259 | 0 | { |
260 | 0 | if (key == NULL) { /* numeric key */ |
261 | 0 | php_printf("%*c[" ZEND_LONG_FMT "]=>\n", level + 1, ' ', index); |
262 | 0 | } else { /* string key */ |
263 | 0 | php_printf("%*c[\"", level + 1, ' '); |
264 | 0 | PHPWRITE(ZSTR_VAL(key), ZSTR_LEN(key)); |
265 | 0 | php_printf("\"]=>\n"); |
266 | 0 | } |
267 | 0 | php_debug_zval_dump(zv, level + 2); |
268 | 0 | } |
269 | | /* }}} */ |
270 | | |
271 | | static void zval_object_property_dump(zend_property_info *prop_info, zval *zv, zend_ulong index, zend_string *key, int level) /* {{{ */ |
272 | 0 | { |
273 | 0 | const char *prop_name, *class_name; |
274 | |
|
275 | 0 | if (key == NULL) { /* numeric key */ |
276 | 0 | php_printf("%*c[" ZEND_LONG_FMT "]=>\n", level + 1, ' ', index); |
277 | 0 | } else { /* string key */ |
278 | 0 | zend_unmangle_property_name(key, &class_name, &prop_name); |
279 | 0 | php_printf("%*c[", level + 1, ' '); |
280 | |
|
281 | 0 | if (class_name) { |
282 | 0 | if (class_name[0] == '*') { |
283 | 0 | php_printf("\"%s\":protected", prop_name); |
284 | 0 | } else { |
285 | 0 | php_printf("\"%s\":\"%s\":private", prop_name, class_name); |
286 | 0 | } |
287 | 0 | } else { |
288 | 0 | php_printf("\"%s\"", prop_name); |
289 | 0 | } |
290 | 0 | ZEND_PUTS("]=>\n"); |
291 | 0 | } |
292 | 0 | if (prop_info && Z_TYPE_P(zv) == IS_UNDEF) { |
293 | 0 | zend_string *type_str = zend_type_to_string(prop_info->type); |
294 | 0 | php_printf("%*cuninitialized(%s)\n", |
295 | 0 | level + 1, ' ', ZSTR_VAL(type_str)); |
296 | 0 | zend_string_release(type_str); |
297 | 0 | } else { |
298 | 0 | php_debug_zval_dump(zv, level + 2); |
299 | 0 | } |
300 | 0 | } |
301 | | /* }}} */ |
302 | | |
303 | | PHPAPI void php_debug_zval_dump(zval *struc, int level) /* {{{ */ |
304 | 0 | { |
305 | 0 | HashTable *myht = NULL; |
306 | 0 | zend_string *class_name; |
307 | 0 | zend_ulong index; |
308 | 0 | zend_string *key; |
309 | 0 | zval *val; |
310 | 0 | uint32_t count; |
311 | 0 | char *packed; |
312 | |
|
313 | 0 | if (level > 1) { |
314 | 0 | php_printf("%*c", level - 1, ' '); |
315 | 0 | } |
316 | |
|
317 | 0 | switch (Z_TYPE_P(struc)) { |
318 | 0 | case IS_FALSE: |
319 | 0 | PUTS("bool(false)\n"); |
320 | 0 | break; |
321 | 0 | case IS_TRUE: |
322 | 0 | PUTS("bool(true)\n"); |
323 | 0 | break; |
324 | 0 | case IS_NULL: |
325 | 0 | PUTS("NULL\n"); |
326 | 0 | break; |
327 | 0 | case IS_LONG: |
328 | 0 | php_printf("int(" ZEND_LONG_FMT ")\n", Z_LVAL_P(struc)); |
329 | 0 | break; |
330 | 0 | case IS_DOUBLE: |
331 | 0 | php_printf_unchecked("float(%.*H)\n", (int) PG(serialize_precision), Z_DVAL_P(struc)); |
332 | 0 | break; |
333 | 0 | case IS_STRING: |
334 | 0 | php_printf("string(%zd) \"", Z_STRLEN_P(struc)); |
335 | 0 | PHPWRITE(Z_STRVAL_P(struc), Z_STRLEN_P(struc)); |
336 | 0 | if (Z_REFCOUNTED_P(struc)) { |
337 | 0 | php_printf("\" refcount(%u)\n", Z_REFCOUNT_P(struc)); |
338 | 0 | } else { |
339 | 0 | PUTS("\" interned\n"); |
340 | 0 | } |
341 | 0 | break; |
342 | 0 | case IS_ARRAY: |
343 | 0 | myht = Z_ARRVAL_P(struc); |
344 | 0 | if (!(GC_FLAGS(myht) & GC_IMMUTABLE)) { |
345 | 0 | if (GC_IS_RECURSIVE(myht)) { |
346 | 0 | PUTS("*RECURSION*\n"); |
347 | 0 | return; |
348 | 0 | } |
349 | 0 | GC_ADDREF(myht); |
350 | 0 | GC_PROTECT_RECURSION(myht); |
351 | 0 | } |
352 | 0 | count = zend_hash_num_elements(myht); |
353 | 0 | packed = HT_IS_PACKED(myht) ? "packed " : ""; |
354 | 0 | if (Z_REFCOUNTED_P(struc)) { |
355 | | /* -1 because of ADDREF above. */ |
356 | 0 | php_printf("array(%d) %srefcount(%u){\n", count, packed, Z_REFCOUNT_P(struc) - 1); |
357 | 0 | } else { |
358 | 0 | php_printf("array(%d) %sinterned {\n", count, packed); |
359 | 0 | } |
360 | 0 | ZEND_HASH_FOREACH_KEY_VAL(myht, index, key, val) { |
361 | 0 | zval_array_element_dump(val, index, key, level); |
362 | 0 | } ZEND_HASH_FOREACH_END(); |
363 | 0 | if (!(GC_FLAGS(myht) & GC_IMMUTABLE)) { |
364 | 0 | GC_UNPROTECT_RECURSION(myht); |
365 | 0 | GC_DTOR_NO_REF(myht); |
366 | 0 | } |
367 | 0 | if (level > 1) { |
368 | 0 | php_printf("%*c", level - 1, ' '); |
369 | 0 | } |
370 | 0 | PUTS("}\n"); |
371 | 0 | break; |
372 | 0 | case IS_OBJECT: { |
373 | | /* Check if this is already recursing on the object before calling zend_get_properties_for, |
374 | | * to allow infinite recursion detection to work even if classes return temporary arrays, |
375 | | * and to avoid the need to update the properties table in place to reflect the state |
376 | | * if the result won't be used. (https://github.com/php/php-src/issues/8044) */ |
377 | 0 | zend_object *zobj = Z_OBJ_P(struc); |
378 | 0 | uint32_t *guard = zend_get_recursion_guard(zobj); |
379 | 0 | if (ZEND_GUARD_OR_GC_IS_RECURSIVE(guard, DEBUG, zobj)) { |
380 | 0 | PUTS("*RECURSION*\n"); |
381 | 0 | return; |
382 | 0 | } |
383 | 0 | ZEND_GUARD_OR_GC_PROTECT_RECURSION(guard, DEBUG, zobj); |
384 | |
|
385 | 0 | myht = zend_get_properties_for(struc, ZEND_PROP_PURPOSE_DEBUG); |
386 | 0 | class_name = Z_OBJ_HANDLER_P(struc, get_class_name)(Z_OBJ_P(struc)); |
387 | 0 | const char *prefix = php_var_dump_object_prefix(Z_OBJ_P(struc)); |
388 | |
|
389 | 0 | php_printf("%sobject(%s)#%d (%d) refcount(%u){\n", prefix, ZSTR_VAL(class_name), Z_OBJ_HANDLE_P(struc), myht ? zend_array_count(myht) : 0, Z_REFCOUNT_P(struc)); |
390 | 0 | zend_string_release_ex(class_name, 0); |
391 | 0 | if (myht) { |
392 | 0 | ZEND_HASH_FOREACH_KEY_VAL(myht, index, key, val) { |
393 | 0 | zend_property_info *prop_info = NULL; |
394 | |
|
395 | 0 | if (Z_TYPE_P(val) == IS_INDIRECT) { |
396 | 0 | val = Z_INDIRECT_P(val); |
397 | 0 | if (key) { |
398 | 0 | prop_info = zend_get_typed_property_info_for_slot(Z_OBJ_P(struc), val); |
399 | 0 | } |
400 | 0 | } |
401 | |
|
402 | 0 | if (!Z_ISUNDEF_P(val) || prop_info) { |
403 | 0 | zval_object_property_dump(prop_info, val, index, key, level); |
404 | 0 | } |
405 | 0 | } ZEND_HASH_FOREACH_END(); |
406 | 0 | zend_release_properties(myht); |
407 | 0 | } |
408 | 0 | if (level > 1) { |
409 | 0 | php_printf("%*c", level - 1, ' '); |
410 | 0 | } |
411 | 0 | PUTS("}\n"); |
412 | 0 | ZEND_GUARD_OR_GC_UNPROTECT_RECURSION(guard, DEBUG, zobj); |
413 | 0 | break; |
414 | 0 | } |
415 | 0 | case IS_RESOURCE: { |
416 | 0 | const char *type_name = zend_rsrc_list_get_rsrc_type(Z_RES_P(struc)); |
417 | 0 | php_printf("resource(" ZEND_LONG_FMT ") of type (%s) refcount(%u)\n", Z_RES_P(struc)->handle, type_name ? type_name : "Unknown", Z_REFCOUNT_P(struc)); |
418 | 0 | break; |
419 | 0 | } |
420 | 0 | case IS_REFERENCE: |
421 | 0 | php_printf("reference refcount(%u) {\n", Z_REFCOUNT_P(struc)); |
422 | 0 | php_debug_zval_dump(Z_REFVAL_P(struc), level + 2); |
423 | 0 | if (level > 1) { |
424 | 0 | php_printf("%*c", level - 1, ' '); |
425 | 0 | } |
426 | 0 | PUTS("}\n"); |
427 | 0 | break; |
428 | 0 | default: |
429 | 0 | PUTS("UNKNOWN:0\n"); |
430 | 0 | break; |
431 | 0 | } |
432 | 0 | } |
433 | | /* }}} */ |
434 | | |
435 | | /* {{{ Dumps a string representation of an internal zend value to output. */ |
436 | | PHP_FUNCTION(debug_zval_dump) |
437 | 0 | { |
438 | 0 | zval *args; |
439 | 0 | int argc; |
440 | 0 | int i; |
441 | |
|
442 | 0 | ZEND_PARSE_PARAMETERS_START(1, -1) |
443 | 0 | Z_PARAM_VARIADIC('+', args, argc) |
444 | 0 | ZEND_PARSE_PARAMETERS_END(); |
445 | | |
446 | 0 | for (i = 0; i < argc; i++) { |
447 | 0 | php_debug_zval_dump(&args[i], 1); |
448 | 0 | } |
449 | 0 | } |
450 | | /* }}} */ |
451 | | |
452 | | #define buffer_append_spaces(buf, num_spaces) \ |
453 | 0 | do { \ |
454 | 0 | char *tmp_spaces; \ |
455 | 0 | size_t tmp_spaces_len; \ |
456 | 0 | tmp_spaces_len = spprintf(&tmp_spaces, 0,"%*c", num_spaces, ' '); \ |
457 | 0 | smart_str_appendl(buf, tmp_spaces, tmp_spaces_len); \ |
458 | 0 | efree(tmp_spaces); \ |
459 | 0 | } while(0); |
460 | | |
461 | | static zend_result php_array_element_export(zval *zv, zend_ulong index, zend_string *key, int level, smart_str *buf) /* {{{ */ |
462 | 0 | { |
463 | 0 | if (key == NULL) { /* numeric key */ |
464 | 0 | buffer_append_spaces(buf, level+1); |
465 | 0 | smart_str_append_long(buf, (zend_long) index); |
466 | 0 | smart_str_appendl(buf, " => ", 4); |
467 | |
|
468 | 0 | } else { /* string key */ |
469 | 0 | zend_string *tmp_str; |
470 | 0 | zend_string *ckey = php_addcslashes(key, "'\\", 2); |
471 | 0 | tmp_str = php_str_to_str(ZSTR_VAL(ckey), ZSTR_LEN(ckey), "\0", 1, "' . \"\\0\" . '", 12); |
472 | |
|
473 | 0 | buffer_append_spaces(buf, level + 1); |
474 | |
|
475 | 0 | smart_str_appendc(buf, '\''); |
476 | 0 | smart_str_append(buf, tmp_str); |
477 | 0 | smart_str_appendl(buf, "' => ", 5); |
478 | |
|
479 | 0 | zend_string_free(ckey); |
480 | 0 | zend_string_free(tmp_str); |
481 | 0 | } |
482 | 0 | zend_result result = php_var_export_ex(zv, level + 2, buf); |
483 | |
|
484 | 0 | smart_str_appendc(buf, ','); |
485 | 0 | smart_str_appendc(buf, '\n'); |
486 | |
|
487 | 0 | return result; |
488 | 0 | } |
489 | | /* }}} */ |
490 | | |
491 | | static zend_result php_object_element_export(zval *zv, zend_ulong index, zend_string *key, int level, smart_str *buf) /* {{{ */ |
492 | 0 | { |
493 | 0 | buffer_append_spaces(buf, level + 2); |
494 | 0 | if (key != NULL) { |
495 | 0 | const char *class_name, *prop_name; |
496 | 0 | size_t prop_name_len; |
497 | 0 | zend_string *pname_esc; |
498 | |
|
499 | 0 | zend_unmangle_property_name_ex(key, &class_name, &prop_name, &prop_name_len); |
500 | 0 | pname_esc = php_addcslashes_str(prop_name, prop_name_len, "'\\", 2); |
501 | |
|
502 | 0 | smart_str_appendc(buf, '\''); |
503 | 0 | smart_str_append(buf, pname_esc); |
504 | 0 | smart_str_appendc(buf, '\''); |
505 | 0 | zend_string_release_ex(pname_esc, 0); |
506 | 0 | } else { |
507 | 0 | smart_str_append_long(buf, (zend_long) index); |
508 | 0 | } |
509 | 0 | smart_str_appendl(buf, " => ", 4); |
510 | 0 | zend_result result = php_var_export_ex(zv, level + 2, buf); |
511 | 0 | smart_str_appendc(buf, ','); |
512 | 0 | smart_str_appendc(buf, '\n'); |
513 | |
|
514 | 0 | return result; |
515 | 0 | } |
516 | | /* }}} */ |
517 | | |
518 | | PHPAPI zend_result php_var_export_ex(zval *struc, int level, smart_str *buf) /* {{{ */ |
519 | 0 | { |
520 | 0 | HashTable *myht; |
521 | 0 | zend_string *ztmp, *ztmp2; |
522 | 0 | zend_ulong index; |
523 | 0 | zend_string *key; |
524 | 0 | zval *val; |
525 | |
|
526 | 0 | again: |
527 | 0 | switch (Z_TYPE_P(struc)) { |
528 | 0 | case IS_FALSE: |
529 | 0 | smart_str_appendl(buf, "false", 5); |
530 | 0 | break; |
531 | 0 | case IS_TRUE: |
532 | 0 | smart_str_appendl(buf, "true", 4); |
533 | 0 | break; |
534 | 0 | case IS_NULL: |
535 | 0 | smart_str_appendl(buf, "NULL", 4); |
536 | 0 | break; |
537 | 0 | case IS_LONG: |
538 | | /* INT_MIN as a literal will be parsed as a float. Emit something like |
539 | | * -9223372036854775807-1 to avoid this. */ |
540 | 0 | if (Z_LVAL_P(struc) == ZEND_LONG_MIN) { |
541 | 0 | smart_str_append_long(buf, ZEND_LONG_MIN+1); |
542 | 0 | smart_str_appends(buf, "-1"); |
543 | 0 | break; |
544 | 0 | } |
545 | 0 | smart_str_append_long(buf, Z_LVAL_P(struc)); |
546 | 0 | break; |
547 | 0 | case IS_DOUBLE: |
548 | 0 | smart_str_append_double( |
549 | 0 | buf, Z_DVAL_P(struc), (int) PG(serialize_precision), /* zero_fraction */ true); |
550 | 0 | break; |
551 | 0 | case IS_STRING: |
552 | 0 | ztmp = php_addcslashes(Z_STR_P(struc), "'\\", 2); |
553 | 0 | ztmp2 = php_str_to_str(ZSTR_VAL(ztmp), ZSTR_LEN(ztmp), "\0", 1, "' . \"\\0\" . '", 12); |
554 | |
|
555 | 0 | smart_str_appendc(buf, '\''); |
556 | 0 | smart_str_append(buf, ztmp2); |
557 | 0 | smart_str_appendc(buf, '\''); |
558 | |
|
559 | 0 | zend_string_free(ztmp); |
560 | 0 | zend_string_free(ztmp2); |
561 | 0 | break; |
562 | 0 | case IS_ARRAY: |
563 | 0 | myht = Z_ARRVAL_P(struc); |
564 | 0 | if (!(GC_FLAGS(myht) & GC_IMMUTABLE)) { |
565 | 0 | if (GC_IS_RECURSIVE(myht)) { |
566 | 0 | smart_str_appendl(buf, "NULL", 4); |
567 | 0 | zend_error(E_WARNING, "var_export does not handle circular references"); |
568 | 0 | return SUCCESS; |
569 | 0 | } |
570 | 0 | GC_ADDREF(myht); |
571 | 0 | GC_PROTECT_RECURSION(myht); |
572 | 0 | } |
573 | 0 | if (level > 1) { |
574 | 0 | smart_str_appendc(buf, '\n'); |
575 | 0 | buffer_append_spaces(buf, level - 1); |
576 | 0 | } |
577 | 0 | smart_str_appendl(buf, "array (\n", 8); |
578 | 0 | ZEND_HASH_FOREACH_KEY_VAL(myht, index, key, val) { |
579 | 0 | if (php_array_element_export(val, index, key, level, buf) == FAILURE) { |
580 | 0 | if (!(GC_FLAGS(myht) & GC_IMMUTABLE)) { |
581 | 0 | GC_UNPROTECT_RECURSION(myht); |
582 | 0 | GC_DELREF(myht); |
583 | 0 | } |
584 | 0 | return FAILURE; |
585 | 0 | } |
586 | 0 | } ZEND_HASH_FOREACH_END(); |
587 | 0 | if (!(GC_FLAGS(myht) & GC_IMMUTABLE)) { |
588 | 0 | GC_UNPROTECT_RECURSION(myht); |
589 | 0 | GC_DELREF(myht); |
590 | 0 | } |
591 | 0 | if (level > 1) { |
592 | 0 | buffer_append_spaces(buf, level - 1); |
593 | 0 | } |
594 | 0 | smart_str_appendc(buf, ')'); |
595 | |
|
596 | 0 | break; |
597 | | |
598 | 0 | case IS_OBJECT: { |
599 | | /* Check if this is already recursing on the object before calling zend_get_properties_for, |
600 | | * to allow infinite recursion detection to work even if classes return temporary arrays, |
601 | | * and to avoid the need to update the properties table in place to reflect the state |
602 | | * if the result won't be used. (https://github.com/php/php-src/issues/8044) */ |
603 | 0 | zend_object *zobj = Z_OBJ_P(struc); |
604 | 0 | uint32_t *guard = zend_get_recursion_guard(zobj); |
605 | 0 | if (ZEND_GUARD_OR_GC_IS_RECURSIVE(guard, EXPORT, zobj)) { |
606 | 0 | smart_str_appendl(buf, "NULL", 4); |
607 | 0 | zend_error(E_WARNING, "var_export does not handle circular references"); |
608 | 0 | return SUCCESS; |
609 | 0 | } |
610 | 0 | ZEND_GUARD_OR_GC_PROTECT_RECURSION(guard, EXPORT, zobj); |
611 | 0 | myht = zend_get_properties_for(struc, ZEND_PROP_PURPOSE_VAR_EXPORT); |
612 | 0 | if (level > 1) { |
613 | 0 | smart_str_appendc(buf, '\n'); |
614 | 0 | buffer_append_spaces(buf, level - 1); |
615 | 0 | } |
616 | |
|
617 | 0 | zend_class_entry *ce = Z_OBJCE_P(struc); |
618 | 0 | bool is_enum = ce->ce_flags & ZEND_ACC_ENUM; |
619 | | |
620 | | /* stdClass has no __set_state method, but can be casted to */ |
621 | 0 | if (ce == zend_standard_class_def) { |
622 | 0 | smart_str_appendl(buf, "(object) array(\n", 16); |
623 | 0 | } else { |
624 | 0 | smart_str_appendc(buf, '\\'); |
625 | 0 | smart_str_append(buf, ce->name); |
626 | 0 | if (is_enum) { |
627 | 0 | zend_object *zobj = Z_OBJ_P(struc); |
628 | 0 | zval *case_name_zval = zend_enum_fetch_case_name(zobj); |
629 | 0 | smart_str_appendl(buf, "::", 2); |
630 | 0 | smart_str_append(buf, Z_STR_P(case_name_zval)); |
631 | 0 | } else { |
632 | 0 | smart_str_appendl(buf, "::__set_state(array(\n", 21); |
633 | 0 | } |
634 | 0 | } |
635 | |
|
636 | 0 | if (myht) { |
637 | 0 | if (!is_enum) { |
638 | 0 | ZEND_HASH_FOREACH_KEY_VAL_IND(myht, index, key, val) { |
639 | | /* data is IS_PTR for properties with hooks. */ |
640 | 0 | zval tmp; |
641 | 0 | if (UNEXPECTED(Z_TYPE_P(val) == IS_PTR)) { |
642 | 0 | zend_property_info *prop_info = Z_PTR_P(val); |
643 | 0 | if ((prop_info->flags & ZEND_ACC_VIRTUAL) && !prop_info->hooks[ZEND_PROPERTY_HOOK_GET]) { |
644 | 0 | continue; |
645 | 0 | } |
646 | 0 | const char *unmangled_name_cstr = zend_get_unmangled_property_name(prop_info->name); |
647 | 0 | zend_string *unmangled_name = zend_string_init(unmangled_name_cstr, strlen(unmangled_name_cstr), false); |
648 | 0 | val = zend_read_property_ex(prop_info->ce, zobj, unmangled_name, /* silent */ true, &tmp); |
649 | 0 | zend_string_release_ex(unmangled_name, false); |
650 | 0 | if (EG(exception)) { |
651 | 0 | ZEND_GUARD_OR_GC_UNPROTECT_RECURSION(guard, EXPORT, zobj); |
652 | 0 | zend_release_properties(myht); |
653 | 0 | return FAILURE; |
654 | 0 | } |
655 | 0 | } |
656 | 0 | php_object_element_export(val, index, key, level, buf); |
657 | 0 | if (val == &tmp) { |
658 | 0 | zval_ptr_dtor(val); |
659 | 0 | } |
660 | 0 | } ZEND_HASH_FOREACH_END(); |
661 | 0 | } |
662 | 0 | zend_release_properties(myht); |
663 | 0 | } |
664 | 0 | ZEND_GUARD_OR_GC_UNPROTECT_RECURSION(guard, EXPORT, zobj); |
665 | 0 | if (level > 1 && !is_enum) { |
666 | 0 | buffer_append_spaces(buf, level - 1); |
667 | 0 | } |
668 | 0 | if (ce == zend_standard_class_def) { |
669 | 0 | smart_str_appendc(buf, ')'); |
670 | 0 | } else if (!is_enum) { |
671 | 0 | smart_str_appendl(buf, "))", 2); |
672 | 0 | } |
673 | |
|
674 | 0 | break; |
675 | 0 | } |
676 | 0 | case IS_REFERENCE: |
677 | 0 | struc = Z_REFVAL_P(struc); |
678 | 0 | goto again; |
679 | 0 | default: |
680 | 0 | smart_str_appendl(buf, "NULL", 4); |
681 | 0 | break; |
682 | 0 | } |
683 | | |
684 | 0 | return SUCCESS; |
685 | 0 | } |
686 | | /* }}} */ |
687 | | |
688 | | /* FOR BC reasons, this will always perform and then print */ |
689 | | PHPAPI void php_var_export(zval *struc, int level) /* {{{ */ |
690 | 0 | { |
691 | 0 | smart_str buf = {0}; |
692 | 0 | zend_result result = php_var_export_ex(struc, level, &buf); |
693 | 0 | smart_str_0(&buf); |
694 | 0 | if (result == SUCCESS) { |
695 | 0 | PHPWRITE(ZSTR_VAL(buf.s), ZSTR_LEN(buf.s)); |
696 | 0 | } |
697 | 0 | smart_str_free(&buf); |
698 | 0 | } |
699 | | /* }}} */ |
700 | | |
701 | | /* {{{ Outputs or returns a string representation of a variable */ |
702 | | PHP_FUNCTION(var_export) |
703 | 0 | { |
704 | 0 | zval *var; |
705 | 0 | bool return_output = 0; |
706 | 0 | smart_str buf = {0}; |
707 | |
|
708 | 0 | ZEND_PARSE_PARAMETERS_START(1, 2) |
709 | 0 | Z_PARAM_ZVAL(var) |
710 | 0 | Z_PARAM_OPTIONAL |
711 | 0 | Z_PARAM_BOOL(return_output) |
712 | 0 | ZEND_PARSE_PARAMETERS_END(); |
713 | | |
714 | 0 | zend_result result = php_var_export_ex(var, 1, &buf); |
715 | 0 | smart_str_0 (&buf); |
716 | |
|
717 | 0 | if (result == FAILURE) { |
718 | 0 | smart_str_free(&buf); |
719 | 0 | } else if (return_output) { |
720 | 0 | RETURN_STR(smart_str_extract(&buf)); |
721 | 0 | } else { |
722 | 0 | PHPWRITE(ZSTR_VAL(buf.s), ZSTR_LEN(buf.s)); |
723 | 0 | smart_str_free(&buf); |
724 | 0 | } |
725 | 0 | } |
726 | | /* }}} */ |
727 | | |
728 | | static void php_var_serialize_intern(smart_str *buf, zval *struc, php_serialize_data_t var_hash, bool in_rcn_array, bool is_root); |
729 | | |
730 | | /** |
731 | | * @param bool in_rcn_array Whether the element appears in a potentially nested array with RC > 1. |
732 | | */ |
733 | | static inline zend_long php_add_var_hash(php_serialize_data_t data, zval *var, bool in_rcn_array) /* {{{ */ |
734 | 0 | { |
735 | 0 | zval *zv; |
736 | 0 | zend_ulong key; |
737 | 0 | bool is_ref = Z_ISREF_P(var); |
738 | |
|
739 | 0 | data->n += 1; |
740 | |
|
741 | 0 | if (is_ref) { |
742 | | /* pass */ |
743 | 0 | } else if (Z_TYPE_P(var) != IS_OBJECT) { |
744 | 0 | return 0; |
745 | 0 | } else if (!in_rcn_array |
746 | 0 | && Z_REFCOUNT_P(var) == 1 |
747 | 0 | && (Z_OBJ_P(var)->properties == NULL || GC_REFCOUNT(Z_OBJ_P(var)->properties) == 1) |
748 | | /* __serialize and __sleep may arbitrarily increase the refcount */ |
749 | 0 | && Z_OBJCE_P(var)->__serialize == NULL |
750 | 0 | && zend_hash_find_known_hash(&Z_OBJCE_P(var)->function_table, ZSTR_KNOWN(ZEND_STR_SLEEP)) == NULL) { |
751 | 0 | return 0; |
752 | 0 | } |
753 | | |
754 | | /* References to objects are treated as if the reference didn't exist */ |
755 | 0 | if (is_ref && Z_TYPE_P(Z_REFVAL_P(var)) == IS_OBJECT) { |
756 | 0 | var = Z_REFVAL_P(var); |
757 | 0 | } |
758 | | |
759 | | /* Index for the variable is stored using the numeric value of the pointer to |
760 | | * the zend_refcounted struct */ |
761 | 0 | key = (zend_ulong) (uintptr_t) Z_COUNTED_P(var); |
762 | 0 | zv = zend_hash_index_find(&data->ht, key); |
763 | |
|
764 | 0 | if (zv) { |
765 | | /* References are only counted once, undo the data->n increment above */ |
766 | 0 | if (is_ref && Z_LVAL_P(zv) != -1) { |
767 | 0 | data->n -= 1; |
768 | 0 | } |
769 | |
|
770 | 0 | return Z_LVAL_P(zv); |
771 | 0 | } else { |
772 | 0 | zval zv_n; |
773 | 0 | ZVAL_LONG(&zv_n, data->n); |
774 | 0 | zend_hash_index_add_new(&data->ht, key, &zv_n); |
775 | | |
776 | | /* Additionally to the index, we also store the variable, to ensure that it is |
777 | | * not destroyed during serialization and its pointer reused. The variable is |
778 | | * stored at the numeric value of the pointer + 1, which cannot be the location |
779 | | * of another zend_refcounted structure. */ |
780 | 0 | zend_hash_index_add_new(&data->ht, key + 1, var); |
781 | 0 | Z_ADDREF_P(var); |
782 | |
|
783 | 0 | return 0; |
784 | 0 | } |
785 | 0 | } |
786 | | /* }}} */ |
787 | | |
788 | | static inline void php_var_serialize_long(smart_str *buf, zend_long val) /* {{{ */ |
789 | 0 | { |
790 | 0 | char b[32]; |
791 | 0 | char *s = zend_print_long_to_buf(b + sizeof(b) - 1, val); |
792 | 0 | size_t l = b + sizeof(b) - 1 - s; |
793 | 0 | char *res = smart_str_extend(buf, 2 + l + 1); |
794 | 0 | res = zend_mempcpy(res, "i:", 2); |
795 | 0 | memcpy(res, s, l); |
796 | 0 | res[l] = ';'; |
797 | 0 | } |
798 | | /* }}} */ |
799 | | |
800 | | static inline void php_var_serialize_string(smart_str *buf, char *str, size_t len) /* {{{ */ |
801 | 0 | { |
802 | 0 | char b[32]; |
803 | 0 | char *s = zend_print_long_to_buf(b + sizeof(b) - 1, len); |
804 | 0 | size_t l = b + sizeof(b) - 1 - s; |
805 | 0 | char *res = smart_str_extend(buf, 2 + l + 2 + len + 2); |
806 | 0 | res = zend_mempcpy(res, "s:", 2); |
807 | 0 | res = zend_mempcpy(res, s, l); |
808 | 0 | res = zend_mempcpy(res, ":\"", 2); |
809 | 0 | res = zend_mempcpy(res, str, len); |
810 | 0 | memcpy(res, "\";", 2); |
811 | 0 | } |
812 | | /* }}} */ |
813 | | |
814 | | static inline bool php_var_serialize_class_name(smart_str *buf, zval *struc) /* {{{ */ |
815 | 0 | { |
816 | 0 | char b[32]; |
817 | 0 | PHP_CLASS_ATTRIBUTES; |
818 | |
|
819 | 0 | PHP_SET_CLASS_ATTRIBUTES(struc); |
820 | 0 | size_t class_name_len = ZSTR_LEN(class_name); |
821 | 0 | char *s = zend_print_long_to_buf(b + sizeof(b) - 1, class_name_len); |
822 | 0 | size_t l = b + sizeof(b) - 1 - s; |
823 | 0 | char *res = smart_str_extend(buf, 2 + l + 2 + class_name_len + 2); |
824 | 0 | res = zend_mempcpy(res, "O:", 2); |
825 | 0 | res = zend_mempcpy(res, s, l); |
826 | 0 | res = zend_mempcpy(res, ":\"", 2); |
827 | 0 | res = zend_mempcpy(res, ZSTR_VAL(class_name), class_name_len); |
828 | 0 | memcpy(res, "\":", 2); |
829 | 0 | PHP_CLEANUP_CLASS_ATTRIBUTES(); |
830 | 0 | return incomplete_class; |
831 | 0 | } |
832 | | /* }}} */ |
833 | | |
834 | | static HashTable* php_var_serialize_call_sleep(zend_object *obj, zend_function *fn) /* {{{ */ |
835 | 0 | { |
836 | 0 | zval retval; |
837 | |
|
838 | 0 | BG(serialize_lock)++; |
839 | 0 | zend_call_known_instance_method(fn, obj, &retval, /* param_count */ 0, /* params */ NULL); |
840 | 0 | BG(serialize_lock)--; |
841 | |
|
842 | 0 | if (Z_ISUNDEF(retval) || EG(exception)) { |
843 | 0 | zval_ptr_dtor(&retval); |
844 | 0 | return NULL; |
845 | 0 | } |
846 | | |
847 | 0 | if (Z_TYPE(retval) != IS_ARRAY) { |
848 | 0 | zval_ptr_dtor(&retval); |
849 | 0 | php_error_docref(NULL, E_WARNING, "%s::__sleep() should return an array only containing the names of instance-variables to serialize", ZSTR_VAL(obj->ce->name)); |
850 | 0 | return NULL; |
851 | 0 | } |
852 | | |
853 | 0 | return Z_ARRVAL(retval); |
854 | 0 | } |
855 | | /* }}} */ |
856 | | |
857 | | static int php_var_serialize_call_magic_serialize(zval *retval, zval *obj) /* {{{ */ |
858 | 0 | { |
859 | 0 | BG(serialize_lock)++; |
860 | 0 | zend_call_known_instance_method_with_0_params( |
861 | 0 | Z_OBJCE_P(obj)->__serialize, Z_OBJ_P(obj), retval); |
862 | 0 | BG(serialize_lock)--; |
863 | |
|
864 | 0 | if (EG(exception)) { |
865 | 0 | zval_ptr_dtor(retval); |
866 | 0 | return FAILURE; |
867 | 0 | } |
868 | | |
869 | 0 | if (Z_TYPE_P(retval) != IS_ARRAY) { |
870 | 0 | zval_ptr_dtor(retval); |
871 | 0 | zend_type_error("%s::__serialize() must return an array", ZSTR_VAL(Z_OBJCE_P(obj)->name)); |
872 | 0 | return FAILURE; |
873 | 0 | } |
874 | | |
875 | 0 | return SUCCESS; |
876 | 0 | } |
877 | | /* }}} */ |
878 | | |
879 | | static int php_var_serialize_try_add_sleep_prop( |
880 | | HashTable *ht, HashTable *props, zend_string *name, zend_string *error_name, zval *struc) /* {{{ */ |
881 | 0 | { |
882 | 0 | zval *val = zend_hash_find(props, name); |
883 | 0 | if (val == NULL) { |
884 | 0 | return FAILURE; |
885 | 0 | } |
886 | | |
887 | 0 | if (Z_TYPE_P(val) == IS_INDIRECT) { |
888 | 0 | val = Z_INDIRECT_P(val); |
889 | 0 | if (Z_TYPE_P(val) == IS_UNDEF) { |
890 | 0 | zend_property_info *info = zend_get_typed_property_info_for_slot(Z_OBJ_P(struc), val); |
891 | 0 | if (info) { |
892 | 0 | return SUCCESS; |
893 | 0 | } |
894 | 0 | return FAILURE; |
895 | 0 | } |
896 | 0 | } |
897 | | |
898 | 0 | if (!zend_hash_add(ht, name, val)) { |
899 | 0 | php_error_docref(NULL, E_WARNING, |
900 | 0 | "\"%s\" is returned from __sleep() multiple times", ZSTR_VAL(error_name)); |
901 | 0 | return SUCCESS; |
902 | 0 | } |
903 | | |
904 | 0 | Z_TRY_ADDREF_P(val); |
905 | 0 | return SUCCESS; |
906 | 0 | } |
907 | | /* }}} */ |
908 | | |
909 | | static int php_var_serialize_get_sleep_props( |
910 | | HashTable *ht, zval *struc, HashTable *sleep_retval) /* {{{ */ |
911 | 0 | { |
912 | 0 | zend_class_entry *ce = Z_OBJCE_P(struc); |
913 | 0 | HashTable *props = zend_get_properties_for(struc, ZEND_PROP_PURPOSE_SERIALIZE); |
914 | 0 | zval *name_val; |
915 | 0 | int retval = SUCCESS; |
916 | |
|
917 | 0 | zend_hash_init(ht, zend_hash_num_elements(sleep_retval), NULL, ZVAL_PTR_DTOR, 0); |
918 | | /* TODO: Rewrite this by fetching the property info instead of trying out different |
919 | | * name manglings? */ |
920 | 0 | ZEND_HASH_FOREACH_VAL_IND(sleep_retval, name_val) { |
921 | 0 | zend_string *name, *tmp_name, *priv_name, *prot_name; |
922 | |
|
923 | 0 | ZVAL_DEREF(name_val); |
924 | 0 | if (Z_TYPE_P(name_val) != IS_STRING) { |
925 | 0 | php_error_docref(NULL, E_WARNING, |
926 | 0 | "%s::__sleep() should return an array only containing the names of instance-variables to serialize", |
927 | 0 | ZSTR_VAL(ce->name)); |
928 | 0 | } |
929 | |
|
930 | 0 | name = zval_get_tmp_string(name_val, &tmp_name); |
931 | 0 | if (php_var_serialize_try_add_sleep_prop(ht, props, name, name, struc) == SUCCESS) { |
932 | 0 | zend_tmp_string_release(tmp_name); |
933 | 0 | continue; |
934 | 0 | } |
935 | | |
936 | 0 | if (EG(exception)) { |
937 | 0 | zend_tmp_string_release(tmp_name); |
938 | 0 | retval = FAILURE; |
939 | 0 | break; |
940 | 0 | } |
941 | | |
942 | 0 | priv_name = zend_mangle_property_name( |
943 | 0 | ZSTR_VAL(ce->name), ZSTR_LEN(ce->name), |
944 | 0 | ZSTR_VAL(name), ZSTR_LEN(name), ce->type == ZEND_INTERNAL_CLASS); |
945 | 0 | if (php_var_serialize_try_add_sleep_prop(ht, props, priv_name, name, struc) == SUCCESS) { |
946 | 0 | zend_tmp_string_release(tmp_name); |
947 | 0 | zend_string_release(priv_name); |
948 | 0 | continue; |
949 | 0 | } |
950 | 0 | zend_string_release(priv_name); |
951 | |
|
952 | 0 | if (EG(exception)) { |
953 | 0 | zend_tmp_string_release(tmp_name); |
954 | 0 | retval = FAILURE; |
955 | 0 | break; |
956 | 0 | } |
957 | | |
958 | 0 | prot_name = zend_mangle_property_name( |
959 | 0 | "*", 1, ZSTR_VAL(name), ZSTR_LEN(name), ce->type == ZEND_INTERNAL_CLASS); |
960 | 0 | if (php_var_serialize_try_add_sleep_prop(ht, props, prot_name, name, struc) == SUCCESS) { |
961 | 0 | zend_tmp_string_release(tmp_name); |
962 | 0 | zend_string_release(prot_name); |
963 | 0 | continue; |
964 | 0 | } |
965 | 0 | zend_string_release(prot_name); |
966 | |
|
967 | 0 | if (EG(exception)) { |
968 | 0 | zend_tmp_string_release(tmp_name); |
969 | 0 | retval = FAILURE; |
970 | 0 | break; |
971 | 0 | } |
972 | | |
973 | 0 | php_error_docref(NULL, E_WARNING, |
974 | 0 | "\"%s\" returned as member variable from __sleep() but does not exist", ZSTR_VAL(name)); |
975 | 0 | zend_tmp_string_release(tmp_name); |
976 | 0 | } ZEND_HASH_FOREACH_END(); |
977 | |
|
978 | 0 | zend_release_properties(props); |
979 | 0 | return retval; |
980 | 0 | } |
981 | | /* }}} */ |
982 | | |
983 | | static void php_var_serialize_nested_data(smart_str *buf, zval *struc, HashTable *ht, uint32_t count, bool incomplete_class, php_serialize_data_t var_hash, bool in_rcn_array) /* {{{ */ |
984 | 0 | { |
985 | 0 | smart_str_append_unsigned(buf, count); |
986 | 0 | smart_str_appendl(buf, ":{", 2); |
987 | 0 | if (count > 0) { |
988 | 0 | zend_string *key; |
989 | 0 | zval *data; |
990 | 0 | zend_ulong index; |
991 | |
|
992 | 0 | ZEND_HASH_FOREACH_KEY_VAL_IND(ht, index, key, data) { |
993 | 0 | if (incomplete_class && zend_string_equals_literal(key, MAGIC_MEMBER)) { |
994 | 0 | incomplete_class = false; |
995 | 0 | continue; |
996 | 0 | } |
997 | | |
998 | 0 | if (!key) { |
999 | 0 | php_var_serialize_long(buf, index); |
1000 | 0 | } else { |
1001 | 0 | php_var_serialize_string(buf, ZSTR_VAL(key), ZSTR_LEN(key)); |
1002 | 0 | } |
1003 | |
|
1004 | 0 | if (Z_ISREF_P(data) && Z_REFCOUNT_P(data) == 1) { |
1005 | 0 | data = Z_REFVAL_P(data); |
1006 | 0 | } |
1007 | | |
1008 | | /* we should still add element even if it's not OK, |
1009 | | * since we already wrote the length of the array before */ |
1010 | 0 | if (Z_TYPE_P(data) == IS_ARRAY) { |
1011 | 0 | if (UNEXPECTED(Z_TYPE_P(struc) == IS_ARRAY && Z_ARR_P(data) == Z_ARR_P(struc))) { |
1012 | 0 | php_add_var_hash(var_hash, struc, in_rcn_array); |
1013 | 0 | smart_str_appendl(buf, "N;", 2); |
1014 | 0 | } else { |
1015 | 0 | php_var_serialize_intern(buf, data, var_hash, in_rcn_array, false); |
1016 | 0 | } |
1017 | 0 | } else { |
1018 | 0 | php_var_serialize_intern(buf, data, var_hash, in_rcn_array, false); |
1019 | 0 | } |
1020 | 0 | } ZEND_HASH_FOREACH_END(); |
1021 | 0 | } |
1022 | 0 | smart_str_appendc(buf, '}'); |
1023 | 0 | } |
1024 | | /* }}} */ |
1025 | | |
1026 | | static void php_var_serialize_class(smart_str *buf, zval *struc, HashTable *ht, php_serialize_data_t var_hash) /* {{{ */ |
1027 | 0 | { |
1028 | 0 | HashTable props; |
1029 | |
|
1030 | 0 | if (php_var_serialize_get_sleep_props(&props, struc, ht) == SUCCESS) { |
1031 | 0 | php_var_serialize_class_name(buf, struc); |
1032 | 0 | php_var_serialize_nested_data( |
1033 | 0 | buf, struc, &props, zend_hash_num_elements(&props), /* incomplete_class */ false, var_hash, |
1034 | 0 | GC_REFCOUNT(&props) > 1); |
1035 | 0 | } |
1036 | 0 | zend_hash_destroy(&props); |
1037 | 0 | } |
1038 | | /* }}} */ |
1039 | | |
1040 | | static zend_always_inline bool php_serialize_check_stack_limit(void) |
1041 | 0 | { |
1042 | 0 | #ifdef ZEND_CHECK_STACK_LIMIT |
1043 | 0 | if (UNEXPECTED(zend_call_stack_overflowed(EG(stack_limit)))) { |
1044 | 0 | zend_call_stack_size_error(); |
1045 | 0 | return true; |
1046 | 0 | } |
1047 | 0 | #endif |
1048 | 0 | return false; |
1049 | 0 | } |
1050 | | |
1051 | | static void php_var_serialize_intern(smart_str *buf, zval *struc, php_serialize_data_t var_hash, bool in_rcn_array, bool is_root) /* {{{ */ |
1052 | 0 | { |
1053 | 0 | zend_long var_already; |
1054 | 0 | HashTable *myht; |
1055 | |
|
1056 | 0 | if (EG(exception)) { |
1057 | 0 | return; |
1058 | 0 | } |
1059 | | |
1060 | 0 | if (UNEXPECTED(php_serialize_check_stack_limit())) { |
1061 | 0 | return; |
1062 | 0 | } |
1063 | | |
1064 | 0 | if (var_hash && (var_already = php_add_var_hash(var_hash, struc, in_rcn_array))) { |
1065 | 0 | if (var_already == -1) { |
1066 | | /* Reference to an object that failed to serialize, replace with null. */ |
1067 | 0 | smart_str_appendl(buf, "N;", 2); |
1068 | 0 | return; |
1069 | 0 | } else if (Z_ISREF_P(struc)) { |
1070 | 0 | smart_str_appendl(buf, "R:", 2); |
1071 | 0 | smart_str_append_long(buf, var_already); |
1072 | 0 | smart_str_appendc(buf, ';'); |
1073 | 0 | return; |
1074 | 0 | } else if (Z_TYPE_P(struc) == IS_OBJECT) { |
1075 | 0 | smart_str_appendl(buf, "r:", 2); |
1076 | 0 | smart_str_append_long(buf, var_already); |
1077 | 0 | smart_str_appendc(buf, ';'); |
1078 | 0 | return; |
1079 | 0 | } |
1080 | 0 | } |
1081 | | |
1082 | 0 | again: |
1083 | 0 | switch (Z_TYPE_P(struc)) { |
1084 | 0 | case IS_FALSE: |
1085 | 0 | smart_str_appendl(buf, "b:0;", 4); |
1086 | 0 | return; |
1087 | | |
1088 | 0 | case IS_TRUE: |
1089 | 0 | smart_str_appendl(buf, "b:1;", 4); |
1090 | 0 | return; |
1091 | | |
1092 | 0 | case IS_NULL: |
1093 | 0 | smart_str_appendl(buf, "N;", 2); |
1094 | 0 | return; |
1095 | | |
1096 | 0 | case IS_LONG: |
1097 | 0 | php_var_serialize_long(buf, Z_LVAL_P(struc)); |
1098 | 0 | return; |
1099 | | |
1100 | 0 | case IS_DOUBLE: { |
1101 | 0 | char tmp_str[ZEND_DOUBLE_MAX_LENGTH]; |
1102 | 0 | zend_gcvt(Z_DVAL_P(struc), (int)PG(serialize_precision), '.', 'E', tmp_str); |
1103 | |
|
1104 | 0 | size_t len = strlen(tmp_str); |
1105 | 0 | char *res = smart_str_extend(buf, 2 + len + 1); |
1106 | 0 | res = zend_mempcpy(res, "d:", 2); |
1107 | 0 | memcpy(res, tmp_str, len); |
1108 | 0 | res[len] = ';'; |
1109 | 0 | return; |
1110 | 0 | } |
1111 | | |
1112 | 0 | case IS_STRING: |
1113 | 0 | php_var_serialize_string(buf, Z_STRVAL_P(struc), Z_STRLEN_P(struc)); |
1114 | 0 | return; |
1115 | | |
1116 | 0 | case IS_OBJECT: { |
1117 | 0 | zend_class_entry *ce = Z_OBJCE_P(struc); |
1118 | 0 | bool incomplete_class; |
1119 | 0 | uint32_t count; |
1120 | |
|
1121 | 0 | if (ce->ce_flags & ZEND_ACC_NOT_SERIALIZABLE) { |
1122 | 0 | zend_throw_exception_ex(NULL, 0, "Serialization of '%s' is not allowed", |
1123 | 0 | ZSTR_VAL(ce->name)); |
1124 | 0 | return; |
1125 | 0 | } |
1126 | | |
1127 | 0 | if (ce->ce_flags & ZEND_ACC_ENUM) { |
1128 | 0 | PHP_CLASS_ATTRIBUTES; |
1129 | |
|
1130 | 0 | zval *case_name_zval = zend_enum_fetch_case_name(Z_OBJ_P(struc)); |
1131 | |
|
1132 | 0 | PHP_SET_CLASS_ATTRIBUTES(struc); |
1133 | 0 | smart_str_appendl(buf, "E:", 2); |
1134 | 0 | smart_str_append_unsigned(buf, ZSTR_LEN(class_name) + strlen(":") + Z_STRLEN_P(case_name_zval)); |
1135 | 0 | smart_str_appendl(buf, ":\"", 2); |
1136 | 0 | smart_str_append(buf, class_name); |
1137 | 0 | smart_str_appendc(buf, ':'); |
1138 | 0 | smart_str_append(buf, Z_STR_P(case_name_zval)); |
1139 | 0 | smart_str_appendl(buf, "\";", 2); |
1140 | 0 | PHP_CLEANUP_CLASS_ATTRIBUTES(); |
1141 | 0 | return; |
1142 | 0 | } |
1143 | | |
1144 | 0 | if (ce->__serialize) { |
1145 | 0 | zval retval, obj; |
1146 | 0 | zend_string *key; |
1147 | 0 | zval *data; |
1148 | 0 | zend_ulong index; |
1149 | |
|
1150 | 0 | ZVAL_OBJ_COPY(&obj, Z_OBJ_P(struc)); |
1151 | 0 | if (php_var_serialize_call_magic_serialize(&retval, &obj) == FAILURE) { |
1152 | 0 | if (!EG(exception)) { |
1153 | 0 | smart_str_appendl(buf, "N;", 2); |
1154 | 0 | } |
1155 | 0 | zval_ptr_dtor(&obj); |
1156 | 0 | return; |
1157 | 0 | } |
1158 | | |
1159 | 0 | php_var_serialize_class_name(buf, &obj); |
1160 | 0 | smart_str_append_unsigned(buf, zend_hash_num_elements(Z_ARRVAL(retval))); |
1161 | 0 | smart_str_appendl(buf, ":{", 2); |
1162 | 0 | ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL(retval), index, key, data) { |
1163 | 0 | if (!key) { |
1164 | 0 | php_var_serialize_long(buf, index); |
1165 | 0 | } else { |
1166 | 0 | php_var_serialize_string(buf, ZSTR_VAL(key), ZSTR_LEN(key)); |
1167 | 0 | } |
1168 | |
|
1169 | 0 | if (Z_ISREF_P(data) && Z_REFCOUNT_P(data) == 1) { |
1170 | 0 | data = Z_REFVAL_P(data); |
1171 | 0 | } |
1172 | 0 | php_var_serialize_intern(buf, data, var_hash, Z_REFCOUNT(retval) > 1, false); |
1173 | 0 | } ZEND_HASH_FOREACH_END(); |
1174 | 0 | smart_str_appendc(buf, '}'); |
1175 | |
|
1176 | 0 | zval_ptr_dtor(&obj); |
1177 | 0 | zval_ptr_dtor(&retval); |
1178 | 0 | return; |
1179 | 0 | } |
1180 | | |
1181 | 0 | if (ce->serialize != NULL) { |
1182 | | /* has custom handler */ |
1183 | 0 | unsigned char *serialized_data = NULL; |
1184 | 0 | size_t serialized_length; |
1185 | |
|
1186 | 0 | if (ce->serialize(struc, &serialized_data, &serialized_length, (zend_serialize_data *)var_hash) == SUCCESS) { |
1187 | 0 | char b1[32], b2[32]; |
1188 | 0 | char *s1 = zend_print_long_to_buf(b1 + sizeof(b1) - 1, ZSTR_LEN(Z_OBJCE_P(struc)->name)); |
1189 | 0 | size_t l1 = b1 + sizeof(b1) - 1 - s1; |
1190 | 0 | char *s2 = zend_print_long_to_buf(b2 + sizeof(b2) - 1, serialized_length); |
1191 | 0 | size_t l2 = b2 + sizeof(b2) - 1 - s2; |
1192 | 0 | char *res = smart_str_extend(buf, 2 + l1 + 2 + ZSTR_LEN(Z_OBJCE_P(struc)->name) + 2 + l2 + 2 + serialized_length + 1); |
1193 | 0 | res = zend_mempcpy(res, "C:", 2); |
1194 | 0 | res = zend_mempcpy(res, s1, l1); |
1195 | 0 | res = zend_mempcpy(res, ":\"", 2); |
1196 | 0 | res = zend_mempcpy(res, ZSTR_VAL(Z_OBJCE_P(struc)->name), ZSTR_LEN(Z_OBJCE_P(struc)->name)); |
1197 | 0 | res = zend_mempcpy(res, "\":", 2); |
1198 | 0 | res = zend_mempcpy(res, s2, l2); |
1199 | 0 | res = zend_mempcpy(res, ":{", 2); |
1200 | 0 | memcpy(res, (char *) serialized_data, serialized_length); |
1201 | 0 | res[serialized_length] = '}'; |
1202 | 0 | } else { |
1203 | | /* Mark this value in the var_hash, to avoid creating references to it. */ |
1204 | 0 | zval *var_idx = zend_hash_index_find(&var_hash->ht, |
1205 | 0 | (zend_ulong) (uintptr_t) Z_COUNTED_P(struc)); |
1206 | 0 | if (var_idx) { |
1207 | 0 | ZVAL_LONG(var_idx, -1); |
1208 | 0 | } |
1209 | 0 | smart_str_appendl(buf, "N;", 2); |
1210 | 0 | } |
1211 | 0 | if (serialized_data) { |
1212 | 0 | efree(serialized_data); |
1213 | 0 | } |
1214 | 0 | return; |
1215 | 0 | } |
1216 | | |
1217 | 0 | if (ce != PHP_IC_ENTRY) { |
1218 | 0 | zval *zv = zend_hash_find_known_hash(&ce->function_table, ZSTR_KNOWN(ZEND_STR_SLEEP)); |
1219 | |
|
1220 | 0 | if (zv) { |
1221 | 0 | HashTable *ht; |
1222 | 0 | zval tmp; |
1223 | |
|
1224 | 0 | ZVAL_OBJ_COPY(&tmp, Z_OBJ_P(struc)); |
1225 | 0 | if (!(ht = php_var_serialize_call_sleep(Z_OBJ(tmp), Z_FUNC_P(zv)))) { |
1226 | 0 | if (!EG(exception)) { |
1227 | | /* we should still add element even if it's not OK, |
1228 | | * since we already wrote the length of the array before */ |
1229 | 0 | smart_str_appendl(buf, "N;", 2); |
1230 | 0 | } |
1231 | 0 | OBJ_RELEASE(Z_OBJ(tmp)); |
1232 | 0 | return; |
1233 | 0 | } |
1234 | | |
1235 | 0 | php_var_serialize_class(buf, &tmp, ht, var_hash); |
1236 | 0 | zend_array_release(ht); |
1237 | 0 | OBJ_RELEASE(Z_OBJ(tmp)); |
1238 | 0 | return; |
1239 | 0 | } |
1240 | 0 | } |
1241 | | |
1242 | 0 | incomplete_class = php_var_serialize_class_name(buf, struc); |
1243 | |
|
1244 | 0 | if (Z_OBJ_P(struc)->properties == NULL |
1245 | 0 | && Z_OBJ_HT_P(struc)->get_properties_for == NULL |
1246 | 0 | && Z_OBJ_HT_P(struc)->get_properties == zend_std_get_properties |
1247 | 0 | && !zend_object_is_lazy(Z_OBJ_P(struc))) { |
1248 | | /* Optimized version without rebuilding properties HashTable */ |
1249 | 0 | zend_object *obj = Z_OBJ_P(struc); |
1250 | 0 | zend_class_entry *ce = obj->ce; |
1251 | 0 | zend_property_info *prop_info; |
1252 | 0 | zval *prop; |
1253 | 0 | int i; |
1254 | |
|
1255 | 0 | count = ce->default_properties_count; |
1256 | 0 | for (i = 0; i < ce->default_properties_count; i++) { |
1257 | 0 | prop_info = ce->properties_info_table[i]; |
1258 | 0 | if (!prop_info) { |
1259 | 0 | count--; |
1260 | 0 | continue; |
1261 | 0 | } |
1262 | 0 | prop = OBJ_PROP(obj, prop_info->offset); |
1263 | 0 | if (Z_TYPE_P(prop) == IS_UNDEF) { |
1264 | 0 | count--; |
1265 | 0 | continue; |
1266 | 0 | } |
1267 | 0 | } |
1268 | 0 | if (count) { |
1269 | 0 | smart_str_append_unsigned(buf, count); |
1270 | 0 | smart_str_appendl(buf, ":{", 2); |
1271 | 0 | for (i = 0; i < ce->default_properties_count; i++) { |
1272 | 0 | prop_info = ce->properties_info_table[i]; |
1273 | 0 | if (!prop_info) { |
1274 | 0 | continue; |
1275 | 0 | } |
1276 | 0 | prop = OBJ_PROP(obj, prop_info->offset); |
1277 | 0 | if (Z_TYPE_P(prop) == IS_UNDEF) { |
1278 | 0 | continue; |
1279 | 0 | } |
1280 | | |
1281 | 0 | php_var_serialize_string(buf, ZSTR_VAL(prop_info->name), ZSTR_LEN(prop_info->name)); |
1282 | |
|
1283 | 0 | if (Z_ISREF_P(prop) && Z_REFCOUNT_P(prop) == 1) { |
1284 | 0 | prop = Z_REFVAL_P(prop); |
1285 | 0 | } |
1286 | |
|
1287 | 0 | php_var_serialize_intern(buf, prop, var_hash, false, false); |
1288 | 0 | } |
1289 | 0 | smart_str_appendc(buf, '}'); |
1290 | 0 | } else { |
1291 | 0 | smart_str_appendl(buf, "0:{}", 4); |
1292 | 0 | } |
1293 | 0 | return; |
1294 | 0 | } |
1295 | 0 | myht = zend_get_properties_for(struc, ZEND_PROP_PURPOSE_SERIALIZE); |
1296 | | /* count after serializing name, since php_var_serialize_class_name |
1297 | | * changes the count if the variable is incomplete class */ |
1298 | 0 | count = zend_array_count(myht); |
1299 | 0 | if (count > 0 && incomplete_class) { |
1300 | 0 | --count; |
1301 | 0 | } |
1302 | 0 | php_var_serialize_nested_data(buf, struc, myht, count, incomplete_class, var_hash, GC_REFCOUNT(myht) > 1); |
1303 | 0 | zend_release_properties(myht); |
1304 | 0 | return; |
1305 | 0 | } |
1306 | 0 | case IS_ARRAY: |
1307 | 0 | smart_str_appendl(buf, "a:", 2); |
1308 | 0 | myht = Z_ARRVAL_P(struc); |
1309 | 0 | php_var_serialize_nested_data( |
1310 | 0 | buf, struc, myht, zend_array_count(myht), /* incomplete_class */ false, var_hash, |
1311 | 0 | !is_root && (in_rcn_array || GC_REFCOUNT(myht) > 1)); |
1312 | 0 | return; |
1313 | 0 | case IS_REFERENCE: |
1314 | 0 | struc = Z_REFVAL_P(struc); |
1315 | 0 | goto again; |
1316 | 0 | default: |
1317 | 0 | smart_str_appendl(buf, "i:0;", 4); |
1318 | 0 | return; |
1319 | 0 | } |
1320 | 0 | } |
1321 | | /* }}} */ |
1322 | | |
1323 | | PHPAPI void php_var_serialize(smart_str *buf, zval *struc, php_serialize_data_t *data) /* {{{ */ |
1324 | 0 | { |
1325 | 0 | php_var_serialize_intern(buf, struc, *data, false, true); |
1326 | 0 | smart_str_0(buf); |
1327 | 0 | } |
1328 | | /* }}} */ |
1329 | | |
1330 | 0 | PHPAPI php_serialize_data_t php_var_serialize_init(void) { |
1331 | 0 | struct php_serialize_data *d; |
1332 | | /* fprintf(stderr, "SERIALIZE_INIT == lock: %u, level: %u\n", BG(serialize_lock), BG(serialize).level); */ |
1333 | 0 | if (BG(serialize_lock) || !BG(serialize).level) { |
1334 | 0 | d = emalloc(sizeof(struct php_serialize_data)); |
1335 | 0 | zend_hash_init(&d->ht, 16, NULL, ZVAL_PTR_DTOR, 0); |
1336 | 0 | d->n = 0; |
1337 | 0 | if (!BG(serialize_lock)) { |
1338 | 0 | BG(serialize).data = d; |
1339 | 0 | BG(serialize).level = 1; |
1340 | 0 | } |
1341 | 0 | } else { |
1342 | 0 | d = BG(serialize).data; |
1343 | 0 | ++BG(serialize).level; |
1344 | 0 | } |
1345 | 0 | return d; |
1346 | 0 | } |
1347 | | |
1348 | 0 | PHPAPI void php_var_serialize_destroy(php_serialize_data_t d) { |
1349 | | /* fprintf(stderr, "SERIALIZE_DESTROY == lock: %u, level: %u\n", BG(serialize_lock), BG(serialize).level); */ |
1350 | 0 | if (BG(serialize_lock) || BG(serialize).level == 1) { |
1351 | 0 | zend_hash_destroy(&d->ht); |
1352 | 0 | efree(d); |
1353 | 0 | } |
1354 | 0 | if (!BG(serialize_lock) && !--BG(serialize).level) { |
1355 | 0 | BG(serialize).data = NULL; |
1356 | 0 | } |
1357 | 0 | } |
1358 | | |
1359 | | /* {{{ Returns a string representation of variable (which can later be unserialized) */ |
1360 | | PHP_FUNCTION(serialize) |
1361 | 0 | { |
1362 | 0 | zval *struc; |
1363 | 0 | php_serialize_data_t var_hash; |
1364 | 0 | smart_str buf = {0}; |
1365 | |
|
1366 | 0 | ZEND_PARSE_PARAMETERS_START(1, 1) |
1367 | 0 | Z_PARAM_ZVAL(struc) |
1368 | 0 | ZEND_PARSE_PARAMETERS_END(); |
1369 | | |
1370 | 0 | PHP_VAR_SERIALIZE_INIT(var_hash); |
1371 | 0 | php_var_serialize(&buf, struc, &var_hash); |
1372 | 0 | PHP_VAR_SERIALIZE_DESTROY(var_hash); |
1373 | |
|
1374 | 0 | if (EG(exception)) { |
1375 | 0 | smart_str_free(&buf); |
1376 | 0 | RETURN_THROWS(); |
1377 | 0 | } |
1378 | | |
1379 | 0 | RETURN_STR(smart_str_extract(&buf)); |
1380 | 0 | } |
1381 | | /* }}} */ |
1382 | | |
1383 | | /* {{{ Takes a string representation of variable and recreates it, subject to the optional unserialize options HashTable */ |
1384 | | PHPAPI void php_unserialize_with_options(zval *return_value, const char *buf, const size_t buf_len, HashTable *options, const char* function_name) |
1385 | 0 | { |
1386 | 0 | const unsigned char *p; |
1387 | 0 | php_unserialize_data_t var_hash; |
1388 | 0 | zval *retval; |
1389 | 0 | HashTable *class_hash = NULL, *prev_class_hash; |
1390 | 0 | zend_long prev_max_depth, prev_cur_depth; |
1391 | |
|
1392 | 0 | if (buf_len == 0) { |
1393 | 0 | RETURN_FALSE; |
1394 | 0 | } |
1395 | | |
1396 | 0 | p = (const unsigned char*) buf; |
1397 | 0 | PHP_VAR_UNSERIALIZE_INIT(var_hash); |
1398 | |
|
1399 | 0 | prev_class_hash = php_var_unserialize_get_allowed_classes(var_hash); |
1400 | 0 | prev_max_depth = php_var_unserialize_get_max_depth(var_hash); |
1401 | 0 | prev_cur_depth = php_var_unserialize_get_cur_depth(var_hash); |
1402 | 0 | if (options != NULL) { |
1403 | 0 | zval *classes, *max_depth; |
1404 | |
|
1405 | 0 | classes = zend_hash_str_find_deref(options, "allowed_classes", sizeof("allowed_classes")-1); |
1406 | 0 | if (classes && Z_TYPE_P(classes) != IS_ARRAY && Z_TYPE_P(classes) != IS_TRUE && Z_TYPE_P(classes) != IS_FALSE) { |
1407 | 0 | zend_type_error("%s(): Option \"allowed_classes\" must be of type array|bool, %s given", function_name, zend_zval_value_name(classes)); |
1408 | 0 | goto cleanup; |
1409 | 0 | } |
1410 | | |
1411 | 0 | if (classes && (Z_TYPE_P(classes) == IS_ARRAY || !zend_is_true(classes))) { |
1412 | 0 | ALLOC_HASHTABLE(class_hash); |
1413 | 0 | zend_hash_init(class_hash, (Z_TYPE_P(classes) == IS_ARRAY)?zend_hash_num_elements(Z_ARRVAL_P(classes)):0, NULL, NULL, 0); |
1414 | 0 | } |
1415 | 0 | if (class_hash && Z_TYPE_P(classes) == IS_ARRAY) { |
1416 | 0 | zval *entry; |
1417 | |
|
1418 | 0 | ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(classes), entry) { |
1419 | 0 | ZVAL_DEREF(entry); |
1420 | 0 | if (UNEXPECTED(Z_TYPE_P(entry) != IS_STRING && Z_TYPE_P(entry) != IS_OBJECT)) { |
1421 | 0 | zend_type_error("%s(): Option \"allowed_classes\" must be an array of class names, %s given", |
1422 | 0 | function_name, zend_zval_value_name(entry)); |
1423 | 0 | goto cleanup; |
1424 | 0 | } |
1425 | 0 | zend_string *tmp_str; |
1426 | 0 | zend_string *name = zval_try_get_tmp_string(entry, &tmp_str); |
1427 | 0 | if (UNEXPECTED(name == NULL)) { |
1428 | 0 | goto cleanup; |
1429 | 0 | } |
1430 | 0 | if (UNEXPECTED(!zend_is_valid_class_name(name))) { |
1431 | 0 | zend_value_error("%s(): Option \"allowed_classes\" must be an array of class names, \"%s\" given", function_name, ZSTR_VAL(name)); |
1432 | 0 | zend_tmp_string_release(tmp_str); |
1433 | 0 | goto cleanup; |
1434 | 0 | } |
1435 | 0 | zend_string *lcname = zend_string_tolower(name); |
1436 | 0 | zend_hash_add_empty_element(class_hash, lcname); |
1437 | 0 | zend_string_release_ex(lcname, false); |
1438 | 0 | zend_tmp_string_release(tmp_str); |
1439 | 0 | } ZEND_HASH_FOREACH_END(); |
1440 | 0 | } |
1441 | 0 | php_var_unserialize_set_allowed_classes(var_hash, class_hash); |
1442 | |
|
1443 | 0 | max_depth = zend_hash_str_find_deref(options, "max_depth", sizeof("max_depth") - 1); |
1444 | 0 | if (max_depth) { |
1445 | 0 | if (Z_TYPE_P(max_depth) != IS_LONG) { |
1446 | 0 | zend_type_error("%s(): Option \"max_depth\" must be of type int, %s given", function_name, zend_zval_value_name(max_depth)); |
1447 | 0 | goto cleanup; |
1448 | 0 | } |
1449 | 0 | if (Z_LVAL_P(max_depth) < 0) { |
1450 | 0 | zend_value_error("%s(): Option \"max_depth\" must be greater than or equal to 0", function_name); |
1451 | 0 | goto cleanup; |
1452 | 0 | } |
1453 | | |
1454 | 0 | php_var_unserialize_set_max_depth(var_hash, Z_LVAL_P(max_depth)); |
1455 | | /* If the max_depth for a nested unserialize() call has been overridden, |
1456 | | * start counting from zero again (for the nested call only). */ |
1457 | 0 | php_var_unserialize_set_cur_depth(var_hash, 0); |
1458 | 0 | } |
1459 | 0 | } |
1460 | | |
1461 | 0 | if (BG(unserialize).level > 1) { |
1462 | 0 | retval = var_tmp_var(&var_hash); |
1463 | 0 | } else { |
1464 | 0 | retval = return_value; |
1465 | 0 | } |
1466 | 0 | if (!php_var_unserialize(retval, &p, p + buf_len, &var_hash)) { |
1467 | 0 | if (!EG(exception)) { |
1468 | 0 | php_error_docref(NULL, E_WARNING, "Error at offset " ZEND_LONG_FMT " of %zd bytes", |
1469 | 0 | (zend_long)((char*)p - buf), buf_len); |
1470 | 0 | } |
1471 | 0 | if (BG(unserialize).level <= 1) { |
1472 | 0 | zval_ptr_dtor(return_value); |
1473 | 0 | } |
1474 | 0 | RETVAL_FALSE; |
1475 | 0 | } else { |
1476 | 0 | if ((char*)p < buf + buf_len) { |
1477 | 0 | if (!EG(exception)) { |
1478 | 0 | php_error_docref(NULL, E_WARNING, "Extra data starting at offset " ZEND_LONG_FMT " of %zd bytes", |
1479 | 0 | (zend_long)((char*)p - buf), buf_len); |
1480 | 0 | } |
1481 | 0 | } |
1482 | 0 | if (BG(unserialize).level > 1) { |
1483 | 0 | ZVAL_COPY(return_value, retval); |
1484 | 0 | } else if (Z_REFCOUNTED_P(return_value)) { |
1485 | 0 | zend_refcounted *ref = Z_COUNTED_P(return_value); |
1486 | 0 | gc_check_possible_root(ref); |
1487 | 0 | } |
1488 | 0 | } |
1489 | |
|
1490 | 0 | cleanup: |
1491 | 0 | if (class_hash) { |
1492 | 0 | zend_hash_destroy(class_hash); |
1493 | 0 | FREE_HASHTABLE(class_hash); |
1494 | 0 | } |
1495 | | |
1496 | | /* Reset to previous options in case this is a nested call */ |
1497 | 0 | php_var_unserialize_set_allowed_classes(var_hash, prev_class_hash); |
1498 | 0 | php_var_unserialize_set_max_depth(var_hash, prev_max_depth); |
1499 | 0 | php_var_unserialize_set_cur_depth(var_hash, prev_cur_depth); |
1500 | 0 | PHP_VAR_UNSERIALIZE_DESTROY(var_hash); |
1501 | | |
1502 | | /* Per calling convention we must not return a reference here, so unwrap. We're doing this at |
1503 | | * the very end, because __wakeup() calls performed during UNSERIALIZE_DESTROY might affect |
1504 | | * the value we unwrap here. This is compatible with behavior in PHP <=7.0. */ |
1505 | 0 | if (Z_ISREF_P(return_value)) { |
1506 | 0 | zend_unwrap_reference(return_value); |
1507 | 0 | } |
1508 | 0 | } |
1509 | | /* }}} */ |
1510 | | |
1511 | | /* {{{ Takes a string representation of variable and recreates it */ |
1512 | | PHP_FUNCTION(unserialize) |
1513 | 0 | { |
1514 | 0 | char *buf = NULL; |
1515 | 0 | size_t buf_len; |
1516 | 0 | HashTable *options = NULL; |
1517 | |
|
1518 | 0 | ZEND_PARSE_PARAMETERS_START(1, 2) |
1519 | 0 | Z_PARAM_STRING(buf, buf_len) |
1520 | 0 | Z_PARAM_OPTIONAL |
1521 | 0 | Z_PARAM_ARRAY_HT(options) |
1522 | 0 | ZEND_PARSE_PARAMETERS_END(); |
1523 | | |
1524 | 0 | php_unserialize_with_options(return_value, buf, buf_len, options, "unserialize"); |
1525 | 0 | } |
1526 | | /* }}} */ |
1527 | | |
1528 | | /* {{{ Returns the allocated by PHP memory */ |
1529 | 0 | PHP_FUNCTION(memory_get_usage) { |
1530 | 0 | bool real_usage = 0; |
1531 | |
|
1532 | 0 | ZEND_PARSE_PARAMETERS_START(0, 1) |
1533 | 0 | Z_PARAM_OPTIONAL |
1534 | 0 | Z_PARAM_BOOL(real_usage) |
1535 | 0 | ZEND_PARSE_PARAMETERS_END(); |
1536 | | |
1537 | 0 | RETURN_LONG(zend_memory_usage(real_usage)); |
1538 | 0 | } |
1539 | | /* }}} */ |
1540 | | |
1541 | | /* {{{ Returns the peak allocated by PHP memory */ |
1542 | 0 | PHP_FUNCTION(memory_get_peak_usage) { |
1543 | 0 | bool real_usage = 0; |
1544 | |
|
1545 | 0 | ZEND_PARSE_PARAMETERS_START(0, 1) |
1546 | 0 | Z_PARAM_OPTIONAL |
1547 | 0 | Z_PARAM_BOOL(real_usage) |
1548 | 0 | ZEND_PARSE_PARAMETERS_END(); |
1549 | | |
1550 | 0 | RETURN_LONG(zend_memory_peak_usage(real_usage)); |
1551 | 0 | } |
1552 | | /* }}} */ |
1553 | | |
1554 | | /* {{{ Resets the peak PHP memory usage */ |
1555 | 0 | PHP_FUNCTION(memory_reset_peak_usage) { |
1556 | 0 | ZEND_PARSE_PARAMETERS_NONE(); |
1557 | | |
1558 | 0 | zend_memory_reset_peak_usage(); |
1559 | 0 | } |
1560 | | /* }}} */ |
1561 | | |
1562 | | PHP_INI_BEGIN() |
1563 | | STD_PHP_INI_ENTRY("unserialize_max_depth", "4096", PHP_INI_ALL, OnUpdateLong, unserialize_max_depth, php_basic_globals, basic_globals) |
1564 | | PHP_INI_END() |
1565 | | |
1566 | | PHP_MINIT_FUNCTION(var) |
1567 | 2 | { |
1568 | 2 | REGISTER_INI_ENTRIES(); |
1569 | 2 | return SUCCESS; |
1570 | 2 | } |