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