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