/src/php-src/ext/standard/var.c
Line | Count | Source (jump to first uncovered line) |
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 | | | http://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_smart_str.h" |
27 | | #include "basic_functions.h" |
28 | | #include "php_incomplete_class.h" |
29 | | /* }}} */ |
30 | | |
31 | | struct php_serialize_data { |
32 | | HashTable ht; |
33 | | uint32_t n; |
34 | | }; |
35 | | |
36 | 756k | #define COMMON (is_ref ? "&" : "") |
37 | | |
38 | | static void php_array_element_dump(zval *zv, zend_ulong index, zend_string *key, int level) /* {{{ */ |
39 | 205k | { |
40 | 205k | if (key == NULL) { /* numeric key */ |
41 | 142k | php_printf("%*c[" ZEND_LONG_FMT "]=>\n", level + 1, ' ', index); |
42 | 63.3k | } else { /* string key */ |
43 | 63.3k | php_printf("%*c[\"", level + 1, ' '); |
44 | 63.3k | PHPWRITE(ZSTR_VAL(key), ZSTR_LEN(key)); |
45 | 63.3k | php_printf("\"]=>\n"); |
46 | 63.3k | } |
47 | 205k | php_var_dump(zv, level + 2); |
48 | 205k | } |
49 | | /* }}} */ |
50 | | |
51 | | static void php_object_property_dump(zend_property_info *prop_info, zval *zv, zend_ulong index, zend_string *key, int level) /* {{{ */ |
52 | 19.4k | { |
53 | 19.4k | const char *prop_name, *class_name; |
54 | | |
55 | 19.4k | if (key == NULL) { /* numeric key */ |
56 | 2.85k | php_printf("%*c[" ZEND_LONG_FMT "]=>\n", level + 1, ' ', index); |
57 | 16.6k | } else { /* string key */ |
58 | 16.6k | int unmangle = zend_unmangle_property_name(key, &class_name, &prop_name); |
59 | 16.6k | php_printf("%*c[", level + 1, ' '); |
60 | | |
61 | 16.6k | if (class_name && unmangle == SUCCESS) { |
62 | 2.47k | if (class_name[0] == '*') { |
63 | 547 | php_printf("\"%s\":protected", prop_name); |
64 | 1.92k | } else { |
65 | 1.92k | php_printf("\"%s\":\"%s\":private", prop_name, class_name); |
66 | 1.92k | } |
67 | 14.1k | } else { |
68 | 14.1k | php_printf("\""); |
69 | 14.1k | PHPWRITE(ZSTR_VAL(key), ZSTR_LEN(key)); |
70 | 14.1k | php_printf("\""); |
71 | 14.1k | } |
72 | 16.6k | ZEND_PUTS("]=>\n"); |
73 | 16.6k | } |
74 | | |
75 | 19.4k | if (Z_TYPE_P(zv) == IS_UNDEF) { |
76 | 597 | ZEND_ASSERT(ZEND_TYPE_IS_SET(prop_info->type)); |
77 | 597 | zend_string *type_str = zend_type_to_string(prop_info->type); |
78 | 597 | php_printf("%*cuninitialized(%s)\n", |
79 | 597 | level + 1, ' ', ZSTR_VAL(type_str)); |
80 | 597 | zend_string_release(type_str); |
81 | 18.8k | } else { |
82 | 18.8k | php_var_dump(zv, level + 2); |
83 | 18.8k | } |
84 | 19.4k | } |
85 | | /* }}} */ |
86 | | |
87 | | PHPAPI void php_var_dump(zval *struc, int level) /* {{{ */ |
88 | 757k | { |
89 | 757k | HashTable *myht; |
90 | 757k | zend_string *class_name; |
91 | 757k | int is_ref = 0; |
92 | 757k | zend_ulong num; |
93 | 757k | zend_string *key; |
94 | 757k | zval *val; |
95 | 757k | uint32_t count; |
96 | | |
97 | 757k | if (level > 1) { |
98 | 224k | php_printf("%*c", level - 1, ' '); |
99 | 224k | } |
100 | | |
101 | 768k | again: |
102 | 768k | switch (Z_TYPE_P(struc)) { |
103 | 62.9k | case IS_FALSE: |
104 | 62.9k | php_printf("%sbool(false)\n", COMMON); |
105 | 62.9k | break; |
106 | 47.5k | case IS_TRUE: |
107 | 47.5k | php_printf("%sbool(true)\n", COMMON); |
108 | 47.5k | break; |
109 | 43.8k | case IS_NULL: |
110 | 43.8k | php_printf("%sNULL\n", COMMON); |
111 | 43.8k | break; |
112 | 261k | case IS_LONG: |
113 | 261k | php_printf("%sint(" ZEND_LONG_FMT ")\n", COMMON, Z_LVAL_P(struc)); |
114 | 261k | break; |
115 | 22.8k | case IS_DOUBLE: |
116 | 22.8k | php_printf_unchecked("%sfloat(%.*H)\n", COMMON, (int) PG(serialize_precision), Z_DVAL_P(struc)); |
117 | 22.8k | break; |
118 | 186k | case IS_STRING: |
119 | 186k | php_printf("%sstring(%zd) \"", COMMON, Z_STRLEN_P(struc)); |
120 | 186k | PHPWRITE(Z_STRVAL_P(struc), Z_STRLEN_P(struc)); |
121 | 186k | PUTS("\"\n"); |
122 | 186k | break; |
123 | 89.4k | case IS_ARRAY: |
124 | 89.4k | myht = Z_ARRVAL_P(struc); |
125 | 89.4k | if (!(GC_FLAGS(myht) & GC_IMMUTABLE)) { |
126 | 84.6k | if (GC_IS_RECURSIVE(myht)) { |
127 | 263 | PUTS("*RECURSION*\n"); |
128 | 263 | return; |
129 | 263 | } |
130 | 84.4k | GC_ADDREF(myht); |
131 | 84.4k | GC_PROTECT_RECURSION(myht); |
132 | 84.4k | } |
133 | 89.1k | count = zend_array_count(myht); |
134 | 89.1k | php_printf("%sarray(%d) {\n", COMMON, count); |
135 | 504k | ZEND_HASH_FOREACH_KEY_VAL_IND(myht, num, key, val) { |
136 | 504k | php_array_element_dump(val, num, key, level); |
137 | 205k | } ZEND_HASH_FOREACH_END(); |
138 | 89.1k | if (!(GC_FLAGS(myht) & GC_IMMUTABLE)) { |
139 | 84.4k | GC_UNPROTECT_RECURSION(myht); |
140 | 84.4k | GC_DELREF(myht); |
141 | 84.4k | } |
142 | 89.1k | if (level > 1) { |
143 | 26.2k | php_printf("%*c", level-1, ' '); |
144 | 26.2k | } |
145 | 89.1k | PUTS("}\n"); |
146 | 89.1k | break; |
147 | 43.1k | case IS_OBJECT: |
148 | 43.1k | if (Z_IS_RECURSIVE_P(struc)) { |
149 | 1.02k | PUTS("*RECURSION*\n"); |
150 | 1.02k | return; |
151 | 1.02k | } |
152 | 42.1k | Z_PROTECT_RECURSION_P(struc); |
153 | | |
154 | 42.1k | myht = zend_get_properties_for(struc, ZEND_PROP_PURPOSE_DEBUG); |
155 | 42.1k | class_name = Z_OBJ_HANDLER_P(struc, get_class_name)(Z_OBJ_P(struc)); |
156 | 42.1k | php_printf("%sobject(%s)#%d (%d) {\n", COMMON, ZSTR_VAL(class_name), Z_OBJ_HANDLE_P(struc), myht ? zend_array_count(myht) : 0); |
157 | 42.1k | zend_string_release_ex(class_name, 0); |
158 | | |
159 | 42.1k | if (myht) { |
160 | 41.9k | zend_ulong num; |
161 | 41.9k | zend_string *key; |
162 | 41.9k | zval *val; |
163 | | |
164 | 80.8k | ZEND_HASH_FOREACH_KEY_VAL(myht, num, key, val) { |
165 | 80.8k | zend_property_info *prop_info = NULL; |
166 | | |
167 | 19.4k | if (Z_TYPE_P(val) == IS_INDIRECT) { |
168 | 5.39k | val = Z_INDIRECT_P(val); |
169 | 5.39k | if (key) { |
170 | 5.39k | prop_info = zend_get_typed_property_info_for_slot(Z_OBJ_P(struc), val); |
171 | 5.39k | } |
172 | 5.39k | } |
173 | | |
174 | 19.4k | if (!Z_ISUNDEF_P(val) || prop_info) { |
175 | 19.4k | php_object_property_dump(prop_info, val, num, key, level); |
176 | 19.4k | } |
177 | 19.4k | } ZEND_HASH_FOREACH_END(); |
178 | 41.9k | zend_release_properties(myht); |
179 | 41.9k | } |
180 | 42.1k | if (level > 1) { |
181 | 5.79k | php_printf("%*c", level-1, ' '); |
182 | 5.79k | } |
183 | 42.1k | PUTS("}\n"); |
184 | 42.1k | Z_UNPROTECT_RECURSION_P(struc); |
185 | 42.1k | break; |
186 | 0 | case IS_RESOURCE: { |
187 | 0 | const char *type_name = zend_rsrc_list_get_rsrc_type(Z_RES_P(struc)); |
188 | 0 | php_printf("%sresource(%d) of type (%s)\n", COMMON, Z_RES_P(struc)->handle, type_name ? type_name : "Unknown"); |
189 | 0 | break; |
190 | 42.1k | } |
191 | 10.5k | case IS_REFERENCE: |
192 | | //??? hide references with refcount==1 (for compatibility) |
193 | 10.5k | if (Z_REFCOUNT_P(struc) > 1) { |
194 | 6.32k | is_ref = 1; |
195 | 6.32k | } |
196 | 10.5k | struc = Z_REFVAL_P(struc); |
197 | 10.5k | goto again; |
198 | 0 | break; |
199 | 0 | default: |
200 | 0 | php_printf("%sUNKNOWN:0\n", COMMON); |
201 | 0 | break; |
202 | 768k | } |
203 | 768k | } |
204 | | /* }}} */ |
205 | | |
206 | | /* {{{ Dumps a string representation of variable to output */ |
207 | | PHP_FUNCTION(var_dump) |
208 | 497k | { |
209 | 497k | zval *args; |
210 | 497k | int argc; |
211 | 497k | int i; |
212 | | |
213 | 1.49M | ZEND_PARSE_PARAMETERS_START(1, -1) |
214 | 497k | Z_PARAM_VARIADIC('+', args, argc) |
215 | 497k | ZEND_PARSE_PARAMETERS_END(); |
216 | | |
217 | 1.03M | for (i = 0; i < argc; i++) { |
218 | 533k | php_var_dump(&args[i], 1); |
219 | 533k | } |
220 | 497k | } |
221 | | /* }}} */ |
222 | | |
223 | | static void zval_array_element_dump(zval *zv, zend_ulong index, zend_string *key, int level) /* {{{ */ |
224 | 0 | { |
225 | 0 | if (key == NULL) { /* numeric key */ |
226 | 0 | php_printf("%*c[" ZEND_LONG_FMT "]=>\n", level + 1, ' ', index); |
227 | 0 | } else { /* string key */ |
228 | 0 | php_printf("%*c[\"", level + 1, ' '); |
229 | 0 | PHPWRITE(ZSTR_VAL(key), ZSTR_LEN(key)); |
230 | 0 | php_printf("\"]=>\n"); |
231 | 0 | } |
232 | 0 | php_debug_zval_dump(zv, level + 2); |
233 | 0 | } |
234 | | /* }}} */ |
235 | | |
236 | | static void zval_object_property_dump(zend_property_info *prop_info, zval *zv, zend_ulong index, zend_string *key, int level) /* {{{ */ |
237 | 57 | { |
238 | 57 | const char *prop_name, *class_name; |
239 | | |
240 | 57 | if (key == NULL) { /* numeric key */ |
241 | 0 | php_printf("%*c[" ZEND_LONG_FMT "]=>\n", level + 1, ' ', index); |
242 | 57 | } else { /* string key */ |
243 | 57 | zend_unmangle_property_name(key, &class_name, &prop_name); |
244 | 57 | php_printf("%*c[", level + 1, ' '); |
245 | | |
246 | 57 | if (class_name) { |
247 | 38 | if (class_name[0] == '*') { |
248 | 19 | php_printf("\"%s\":protected", prop_name); |
249 | 19 | } else { |
250 | 19 | php_printf("\"%s\":\"%s\":private", prop_name, class_name); |
251 | 19 | } |
252 | 19 | } else { |
253 | 19 | php_printf("\"%s\"", prop_name); |
254 | 19 | } |
255 | 57 | ZEND_PUTS("]=>\n"); |
256 | 57 | } |
257 | 57 | if (prop_info && Z_TYPE_P(zv) == IS_UNDEF) { |
258 | 57 | zend_string *type_str = zend_type_to_string(prop_info->type); |
259 | 57 | php_printf("%*cuninitialized(%s)\n", |
260 | 57 | level + 1, ' ', ZSTR_VAL(type_str)); |
261 | 57 | zend_string_release(type_str); |
262 | 0 | } else { |
263 | 0 | php_debug_zval_dump(zv, level + 2); |
264 | 0 | } |
265 | 57 | } |
266 | | /* }}} */ |
267 | | |
268 | | PHPAPI void php_debug_zval_dump(zval *struc, int level) /* {{{ */ |
269 | 215 | { |
270 | 215 | HashTable *myht = NULL; |
271 | 215 | zend_string *class_name; |
272 | 215 | int is_ref = 0; |
273 | 215 | zend_ulong index; |
274 | 215 | zend_string *key; |
275 | 215 | zval *val; |
276 | 215 | uint32_t count; |
277 | | |
278 | 215 | if (level > 1) { |
279 | 0 | php_printf("%*c", level - 1, ' '); |
280 | 0 | } |
281 | | |
282 | 215 | again: |
283 | 215 | switch (Z_TYPE_P(struc)) { |
284 | 0 | case IS_FALSE: |
285 | 0 | php_printf("%sbool(false)\n", COMMON); |
286 | 0 | break; |
287 | 0 | case IS_TRUE: |
288 | 0 | php_printf("%sbool(true)\n", COMMON); |
289 | 0 | break; |
290 | 55 | case IS_NULL: |
291 | 55 | php_printf("%sNULL\n", COMMON); |
292 | 55 | break; |
293 | 0 | case IS_LONG: |
294 | 0 | php_printf("%sint(" ZEND_LONG_FMT ")\n", COMMON, Z_LVAL_P(struc)); |
295 | 0 | break; |
296 | 0 | case IS_DOUBLE: |
297 | 0 | php_printf_unchecked("%sfloat(%.*H)\n", COMMON, (int) PG(serialize_precision), Z_DVAL_P(struc)); |
298 | 0 | break; |
299 | 0 | case IS_STRING: |
300 | 0 | php_printf("%sstring(%zd) \"", COMMON, Z_STRLEN_P(struc)); |
301 | 0 | PHPWRITE(Z_STRVAL_P(struc), Z_STRLEN_P(struc)); |
302 | 0 | php_printf("\" refcount(%u)\n", Z_REFCOUNTED_P(struc) ? Z_REFCOUNT_P(struc) : 1); |
303 | 0 | break; |
304 | 0 | case IS_ARRAY: |
305 | 0 | myht = Z_ARRVAL_P(struc); |
306 | 0 | if (!(GC_FLAGS(myht) & GC_IMMUTABLE)) { |
307 | 0 | if (GC_IS_RECURSIVE(myht)) { |
308 | 0 | PUTS("*RECURSION*\n"); |
309 | 0 | return; |
310 | 0 | } |
311 | 0 | GC_ADDREF(myht); |
312 | 0 | GC_PROTECT_RECURSION(myht); |
313 | 0 | } |
314 | 0 | count = zend_array_count(myht); |
315 | 0 | php_printf("%sarray(%d) refcount(%u){\n", COMMON, count, Z_REFCOUNTED_P(struc) ? Z_REFCOUNT_P(struc) - 1 : 1); |
316 | 0 | ZEND_HASH_FOREACH_KEY_VAL_IND(myht, index, key, val) { |
317 | 0 | zval_array_element_dump(val, index, key, level); |
318 | 0 | } ZEND_HASH_FOREACH_END(); |
319 | 0 | if (!(GC_FLAGS(myht) & GC_IMMUTABLE)) { |
320 | 0 | GC_UNPROTECT_RECURSION(myht); |
321 | 0 | GC_DELREF(myht); |
322 | 0 | } |
323 | 0 | if (level > 1) { |
324 | 0 | php_printf("%*c", level - 1, ' '); |
325 | 0 | } |
326 | 0 | PUTS("}\n"); |
327 | 0 | break; |
328 | 160 | case IS_OBJECT: |
329 | 160 | myht = zend_get_properties_for(struc, ZEND_PROP_PURPOSE_DEBUG); |
330 | 160 | if (myht) { |
331 | 160 | if (GC_IS_RECURSIVE(myht)) { |
332 | 0 | PUTS("*RECURSION*\n"); |
333 | 0 | zend_release_properties(myht); |
334 | 0 | return; |
335 | 0 | } |
336 | 160 | GC_PROTECT_RECURSION(myht); |
337 | 160 | } |
338 | 160 | class_name = Z_OBJ_HANDLER_P(struc, get_class_name)(Z_OBJ_P(struc)); |
339 | 160 | php_printf("%sobject(%s)#%d (%d) refcount(%u){\n", COMMON, ZSTR_VAL(class_name), Z_OBJ_HANDLE_P(struc), myht ? zend_array_count(myht) : 0, Z_REFCOUNT_P(struc)); |
340 | 160 | zend_string_release_ex(class_name, 0); |
341 | 160 | if (myht) { |
342 | 274 | ZEND_HASH_FOREACH_KEY_VAL(myht, index, key, val) { |
343 | 274 | zend_property_info *prop_info = NULL; |
344 | | |
345 | 57 | if (Z_TYPE_P(val) == IS_INDIRECT) { |
346 | 57 | val = Z_INDIRECT_P(val); |
347 | 57 | if (key) { |
348 | 57 | prop_info = zend_get_typed_property_info_for_slot(Z_OBJ_P(struc), val); |
349 | 57 | } |
350 | 57 | } |
351 | | |
352 | 57 | if (!Z_ISUNDEF_P(val) || prop_info) { |
353 | 57 | zval_object_property_dump(prop_info, val, index, key, level); |
354 | 57 | } |
355 | 57 | } ZEND_HASH_FOREACH_END(); |
356 | 160 | GC_UNPROTECT_RECURSION(myht); |
357 | 160 | zend_release_properties(myht); |
358 | 160 | } |
359 | 160 | if (level > 1) { |
360 | 0 | php_printf("%*c", level - 1, ' '); |
361 | 0 | } |
362 | 160 | PUTS("}\n"); |
363 | 160 | break; |
364 | 0 | case IS_RESOURCE: { |
365 | 0 | const char *type_name = zend_rsrc_list_get_rsrc_type(Z_RES_P(struc)); |
366 | 0 | php_printf("%sresource(%d) of type (%s) refcount(%u)\n", COMMON, Z_RES_P(struc)->handle, type_name ? type_name : "Unknown", Z_REFCOUNT_P(struc)); |
367 | 0 | break; |
368 | 160 | } |
369 | 0 | case IS_REFERENCE: |
370 | | //??? hide references with refcount==1 (for compatibility) |
371 | 0 | if (Z_REFCOUNT_P(struc) > 1) { |
372 | 0 | is_ref = 1; |
373 | 0 | } |
374 | 0 | struc = Z_REFVAL_P(struc); |
375 | 0 | goto again; |
376 | 0 | default: |
377 | 0 | php_printf("%sUNKNOWN:0\n", COMMON); |
378 | 0 | break; |
379 | 215 | } |
380 | 215 | } |
381 | | /* }}} */ |
382 | | |
383 | | /* {{{ Dumps a string representation of an internal zend value to output. */ |
384 | | PHP_FUNCTION(debug_zval_dump) |
385 | 215 | { |
386 | 215 | zval *args; |
387 | 215 | int argc; |
388 | 215 | int i; |
389 | | |
390 | 645 | ZEND_PARSE_PARAMETERS_START(1, -1) |
391 | 215 | Z_PARAM_VARIADIC('+', args, argc) |
392 | 215 | ZEND_PARSE_PARAMETERS_END(); |
393 | | |
394 | 430 | for (i = 0; i < argc; i++) { |
395 | 215 | php_debug_zval_dump(&args[i], 1); |
396 | 215 | } |
397 | 215 | } |
398 | | /* }}} */ |
399 | | |
400 | | #define buffer_append_spaces(buf, num_spaces) \ |
401 | 2.67k | do { \ |
402 | 2.67k | char *tmp_spaces; \ |
403 | 2.67k | size_t tmp_spaces_len; \ |
404 | 2.67k | tmp_spaces_len = spprintf(&tmp_spaces, 0,"%*c", num_spaces, ' '); \ |
405 | 2.67k | smart_str_appendl(buf, tmp_spaces, tmp_spaces_len); \ |
406 | 2.67k | efree(tmp_spaces); \ |
407 | 2.67k | } while(0); |
408 | | |
409 | | static void php_array_element_export(zval *zv, zend_ulong index, zend_string *key, int level, smart_str *buf) /* {{{ */ |
410 | 2.35k | { |
411 | 2.35k | if (key == NULL) { /* numeric key */ |
412 | 639 | buffer_append_spaces(buf, level+1); |
413 | 639 | smart_str_append_long(buf, (zend_long) index); |
414 | 639 | smart_str_appendl(buf, " => ", 4); |
415 | | |
416 | 1.71k | } else { /* string key */ |
417 | 1.71k | zend_string *tmp_str; |
418 | 1.71k | zend_string *ckey = php_addcslashes(key, "'\\", 2); |
419 | 1.71k | tmp_str = php_str_to_str(ZSTR_VAL(ckey), ZSTR_LEN(ckey), "\0", 1, "' . \"\\0\" . '", 12); |
420 | | |
421 | 1.71k | buffer_append_spaces(buf, level + 1); |
422 | | |
423 | 1.71k | smart_str_appendc(buf, '\''); |
424 | 1.71k | smart_str_append(buf, tmp_str); |
425 | 1.71k | smart_str_appendl(buf, "' => ", 5); |
426 | | |
427 | 1.71k | zend_string_free(ckey); |
428 | 1.71k | zend_string_free(tmp_str); |
429 | 1.71k | } |
430 | 2.35k | php_var_export_ex(zv, level + 2, buf); |
431 | | |
432 | 2.35k | smart_str_appendc(buf, ','); |
433 | 2.35k | smart_str_appendc(buf, '\n'); |
434 | 2.35k | } |
435 | | /* }}} */ |
436 | | |
437 | | static void php_object_element_export(zval *zv, zend_ulong index, zend_string *key, int level, smart_str *buf) /* {{{ */ |
438 | 251 | { |
439 | 251 | buffer_append_spaces(buf, level + 2); |
440 | 251 | if (key != NULL) { |
441 | 245 | const char *class_name, *prop_name; |
442 | 245 | size_t prop_name_len; |
443 | 245 | zend_string *pname_esc; |
444 | | |
445 | 245 | zend_unmangle_property_name_ex(key, &class_name, &prop_name, &prop_name_len); |
446 | 245 | pname_esc = php_addcslashes_str(prop_name, prop_name_len, "'\\", 2); |
447 | | |
448 | 245 | smart_str_appendc(buf, '\''); |
449 | 245 | smart_str_append(buf, pname_esc); |
450 | 245 | smart_str_appendc(buf, '\''); |
451 | 245 | zend_string_release_ex(pname_esc, 0); |
452 | 6 | } else { |
453 | 6 | smart_str_append_long(buf, (zend_long) index); |
454 | 6 | } |
455 | 251 | smart_str_appendl(buf, " => ", 4); |
456 | 251 | php_var_export_ex(zv, level + 2, buf); |
457 | 251 | smart_str_appendc(buf, ','); |
458 | 251 | smart_str_appendc(buf, '\n'); |
459 | 251 | } |
460 | | /* }}} */ |
461 | | |
462 | | PHPAPI void php_var_export_ex(zval *struc, int level, smart_str *buf) /* {{{ */ |
463 | 3.34k | { |
464 | 3.34k | HashTable *myht; |
465 | 3.34k | char tmp_str[PHP_DOUBLE_MAX_LENGTH]; |
466 | 3.34k | zend_string *ztmp, *ztmp2; |
467 | 3.34k | zend_ulong index; |
468 | 3.34k | zend_string *key; |
469 | 3.34k | zval *val; |
470 | | |
471 | 3.34k | again: |
472 | 3.34k | switch (Z_TYPE_P(struc)) { |
473 | 68 | case IS_FALSE: |
474 | 68 | smart_str_appendl(buf, "false", 5); |
475 | 68 | break; |
476 | 4 | case IS_TRUE: |
477 | 4 | smart_str_appendl(buf, "true", 4); |
478 | 4 | break; |
479 | 43 | case IS_NULL: |
480 | 43 | smart_str_appendl(buf, "NULL", 4); |
481 | 43 | break; |
482 | 1.97k | case IS_LONG: |
483 | | /* INT_MIN as a literal will be parsed as a float. Emit something like |
484 | | * -9223372036854775807-1 to avoid this. */ |
485 | 1.97k | if (Z_LVAL_P(struc) == ZEND_LONG_MIN) { |
486 | 0 | smart_str_append_long(buf, ZEND_LONG_MIN+1); |
487 | 0 | smart_str_appends(buf, "-1"); |
488 | 0 | break; |
489 | 0 | } |
490 | 1.97k | smart_str_append_long(buf, Z_LVAL_P(struc)); |
491 | 1.97k | break; |
492 | 1 | case IS_DOUBLE: |
493 | 1 | php_gcvt(Z_DVAL_P(struc), (int)PG(serialize_precision), '.', 'E', tmp_str); |
494 | 1 | smart_str_appends(buf, tmp_str); |
495 | | /* Without a decimal point, PHP treats a number literal as an int. |
496 | | * This check even works for scientific notation, because the |
497 | | * mantissa always contains a decimal point. |
498 | | * We need to check for finiteness, because INF, -INF and NAN |
499 | | * must not have a decimal point added. |
500 | | */ |
501 | 1 | if (zend_finite(Z_DVAL_P(struc)) && NULL == strchr(tmp_str, '.')) { |
502 | 1 | smart_str_appendl(buf, ".0", 2); |
503 | 1 | } |
504 | 1 | break; |
505 | 499 | case IS_STRING: |
506 | 499 | ztmp = php_addcslashes(Z_STR_P(struc), "'\\", 2); |
507 | 499 | ztmp2 = php_str_to_str(ZSTR_VAL(ztmp), ZSTR_LEN(ztmp), "\0", 1, "' . \"\\0\" . '", 12); |
508 | | |
509 | 499 | smart_str_appendc(buf, '\''); |
510 | 499 | smart_str_append(buf, ztmp2); |
511 | 499 | smart_str_appendc(buf, '\''); |
512 | | |
513 | 499 | zend_string_free(ztmp); |
514 | 499 | zend_string_free(ztmp2); |
515 | 499 | break; |
516 | 707 | case IS_ARRAY: |
517 | 707 | myht = Z_ARRVAL_P(struc); |
518 | 707 | if (!(GC_FLAGS(myht) & GC_IMMUTABLE)) { |
519 | 703 | if (GC_IS_RECURSIVE(myht)) { |
520 | 0 | smart_str_appendl(buf, "NULL", 4); |
521 | 0 | zend_error(E_WARNING, "var_export does not handle circular references"); |
522 | 0 | return; |
523 | 0 | } |
524 | 703 | GC_ADDREF(myht); |
525 | 703 | GC_PROTECT_RECURSION(myht); |
526 | 703 | } |
527 | 707 | if (level > 1) { |
528 | 33 | smart_str_appendc(buf, '\n'); |
529 | 33 | buffer_append_spaces(buf, level - 1); |
530 | 33 | } |
531 | 707 | smart_str_appendl(buf, "array (\n", 8); |
532 | 5.50k | ZEND_HASH_FOREACH_KEY_VAL_IND(myht, index, key, val) { |
533 | 5.50k | php_array_element_export(val, index, key, level, buf); |
534 | 2.35k | } ZEND_HASH_FOREACH_END(); |
535 | 707 | if (!(GC_FLAGS(myht) & GC_IMMUTABLE)) { |
536 | 703 | GC_UNPROTECT_RECURSION(myht); |
537 | 703 | GC_DELREF(myht); |
538 | 703 | } |
539 | 707 | if (level > 1) { |
540 | 33 | buffer_append_spaces(buf, level - 1); |
541 | 33 | } |
542 | 707 | smart_str_appendc(buf, ')'); |
543 | | |
544 | 707 | break; |
545 | | |
546 | 47 | case IS_OBJECT: |
547 | 47 | myht = zend_get_properties_for(struc, ZEND_PROP_PURPOSE_VAR_EXPORT); |
548 | 47 | if (myht) { |
549 | 47 | if (GC_IS_RECURSIVE(myht)) { |
550 | 0 | smart_str_appendl(buf, "NULL", 4); |
551 | 0 | zend_error(E_WARNING, "var_export does not handle circular references"); |
552 | 0 | zend_release_properties(myht); |
553 | 0 | return; |
554 | 47 | } else { |
555 | 47 | GC_TRY_PROTECT_RECURSION(myht); |
556 | 47 | } |
557 | 47 | } |
558 | 47 | if (level > 1) { |
559 | 0 | smart_str_appendc(buf, '\n'); |
560 | 0 | buffer_append_spaces(buf, level - 1); |
561 | 0 | } |
562 | | |
563 | | /* stdClass has no __set_state method, but can be casted to */ |
564 | 47 | if (Z_OBJCE_P(struc) == zend_standard_class_def) { |
565 | 0 | smart_str_appendl(buf, "(object) array(\n", 16); |
566 | 47 | } else { |
567 | 47 | smart_str_append(buf, Z_OBJCE_P(struc)->name); |
568 | 47 | smart_str_appendl(buf, "::__set_state(array(\n", 21); |
569 | 47 | } |
570 | | |
571 | 47 | if (myht) { |
572 | 549 | ZEND_HASH_FOREACH_KEY_VAL_IND(myht, index, key, val) { |
573 | 549 | php_object_element_export(val, index, key, level, buf); |
574 | 251 | } ZEND_HASH_FOREACH_END(); |
575 | 47 | GC_TRY_UNPROTECT_RECURSION(myht); |
576 | 47 | zend_release_properties(myht); |
577 | 47 | } |
578 | 47 | if (level > 1) { |
579 | 0 | buffer_append_spaces(buf, level - 1); |
580 | 0 | } |
581 | 47 | if (Z_OBJCE_P(struc) == zend_standard_class_def) { |
582 | 0 | smart_str_appendc(buf, ')'); |
583 | 47 | } else { |
584 | 47 | smart_str_appendl(buf, "))", 2); |
585 | 47 | } |
586 | | |
587 | 47 | break; |
588 | 0 | case IS_REFERENCE: |
589 | 0 | struc = Z_REFVAL_P(struc); |
590 | 0 | goto again; |
591 | 0 | break; |
592 | 0 | default: |
593 | 0 | smart_str_appendl(buf, "NULL", 4); |
594 | 0 | break; |
595 | 3.34k | } |
596 | 3.34k | } |
597 | | /* }}} */ |
598 | | |
599 | | /* FOR BC reasons, this will always perform and then print */ |
600 | | PHPAPI void php_var_export(zval *struc, int level) /* {{{ */ |
601 | 0 | { |
602 | 0 | smart_str buf = {0}; |
603 | 0 | php_var_export_ex(struc, level, &buf); |
604 | 0 | smart_str_0(&buf); |
605 | 0 | PHPWRITE(ZSTR_VAL(buf.s), ZSTR_LEN(buf.s)); |
606 | 0 | smart_str_free(&buf); |
607 | 0 | } |
608 | | /* }}} */ |
609 | | |
610 | | /* {{{ Outputs or returns a string representation of a variable */ |
611 | | PHP_FUNCTION(var_export) |
612 | 737 | { |
613 | 737 | zval *var; |
614 | 737 | zend_bool return_output = 0; |
615 | 737 | smart_str buf = {0}; |
616 | | |
617 | 2.21k | ZEND_PARSE_PARAMETERS_START(1, 2) |
618 | 737 | Z_PARAM_ZVAL(var) |
619 | 737 | Z_PARAM_OPTIONAL |
620 | 0 | Z_PARAM_BOOL(return_output) |
621 | 737 | ZEND_PARSE_PARAMETERS_END(); |
622 | | |
623 | 737 | php_var_export_ex(var, 1, &buf); |
624 | 737 | smart_str_0 (&buf); |
625 | | |
626 | 737 | if (return_output) { |
627 | 0 | RETURN_NEW_STR(buf.s); |
628 | 737 | } else { |
629 | 737 | PHPWRITE(ZSTR_VAL(buf.s), ZSTR_LEN(buf.s)); |
630 | 737 | smart_str_free(&buf); |
631 | 737 | } |
632 | 737 | } |
633 | | /* }}} */ |
634 | | |
635 | | static void php_var_serialize_intern(smart_str *buf, zval *struc, php_serialize_data_t var_hash); |
636 | | |
637 | | static inline zend_long php_add_var_hash(php_serialize_data_t data, zval *var) /* {{{ */ |
638 | 17.2k | { |
639 | 17.2k | zval *zv; |
640 | 17.2k | zend_ulong key; |
641 | 17.2k | zend_bool is_ref = Z_ISREF_P(var); |
642 | | |
643 | 17.2k | data->n += 1; |
644 | | |
645 | 17.2k | if (!is_ref && Z_TYPE_P(var) != IS_OBJECT) { |
646 | 15.6k | return 0; |
647 | 15.6k | } |
648 | | |
649 | | /* References to objects are treated as if the reference didn't exist */ |
650 | 1.51k | if (is_ref && Z_TYPE_P(Z_REFVAL_P(var)) == IS_OBJECT) { |
651 | 0 | var = Z_REFVAL_P(var); |
652 | 0 | } |
653 | | |
654 | | /* Index for the variable is stored using the numeric value of the pointer to |
655 | | * the zend_refcounted struct */ |
656 | 1.51k | key = (zend_ulong) (zend_uintptr_t) Z_COUNTED_P(var); |
657 | 1.51k | zv = zend_hash_index_find(&data->ht, key); |
658 | | |
659 | 1.51k | if (zv) { |
660 | | /* References are only counted once, undo the data->n increment above */ |
661 | 144 | if (is_ref) { |
662 | 27 | data->n -= 1; |
663 | 27 | } |
664 | | |
665 | 144 | return Z_LVAL_P(zv); |
666 | 1.36k | } else { |
667 | 1.36k | zval zv_n; |
668 | 1.36k | ZVAL_LONG(&zv_n, data->n); |
669 | 1.36k | zend_hash_index_add_new(&data->ht, key, &zv_n); |
670 | | |
671 | | /* Additionally to the index, we also store the variable, to ensure that it is |
672 | | * not destroyed during serialization and its pointer reused. The variable is |
673 | | * stored at the numeric value of the pointer + 1, which cannot be the location |
674 | | * of another zend_refcounted structure. */ |
675 | 1.36k | zend_hash_index_add_new(&data->ht, key + 1, var); |
676 | 1.36k | Z_ADDREF_P(var); |
677 | | |
678 | 1.36k | return 0; |
679 | 1.36k | } |
680 | 1.51k | } |
681 | | /* }}} */ |
682 | | |
683 | | static inline void php_var_serialize_long(smart_str *buf, zend_long val) /* {{{ */ |
684 | 4.35k | { |
685 | 4.35k | smart_str_appendl(buf, "i:", 2); |
686 | 4.35k | smart_str_append_long(buf, val); |
687 | 4.35k | smart_str_appendc(buf, ';'); |
688 | 4.35k | } |
689 | | /* }}} */ |
690 | | |
691 | | static inline void php_var_serialize_string(smart_str *buf, char *str, size_t len) /* {{{ */ |
692 | 22.0k | { |
693 | 22.0k | smart_str_appendl(buf, "s:", 2); |
694 | 22.0k | smart_str_append_unsigned(buf, len); |
695 | 22.0k | smart_str_appendl(buf, ":\"", 2); |
696 | 22.0k | smart_str_appendl(buf, str, len); |
697 | 22.0k | smart_str_appendl(buf, "\";", 2); |
698 | 22.0k | } |
699 | | /* }}} */ |
700 | | |
701 | | static inline zend_bool php_var_serialize_class_name(smart_str *buf, zval *struc) /* {{{ */ |
702 | 1.03k | { |
703 | 1.03k | PHP_CLASS_ATTRIBUTES; |
704 | | |
705 | 1.03k | PHP_SET_CLASS_ATTRIBUTES(struc); |
706 | 1.03k | smart_str_appendl(buf, "O:", 2); |
707 | 1.03k | smart_str_append_unsigned(buf, ZSTR_LEN(class_name)); |
708 | 1.03k | smart_str_appendl(buf, ":\"", 2); |
709 | 1.03k | smart_str_append(buf, class_name); |
710 | 1.03k | smart_str_appendl(buf, "\":", 2); |
711 | 1.03k | PHP_CLEANUP_CLASS_ATTRIBUTES(); |
712 | 1.03k | return incomplete_class; |
713 | 1.03k | } |
714 | | /* }}} */ |
715 | | |
716 | | static int php_var_serialize_call_sleep(zval *retval, zval *struc) /* {{{ */ |
717 | 0 | { |
718 | 0 | zval fname; |
719 | 0 | int res; |
720 | |
|
721 | 0 | ZVAL_STRINGL(&fname, "__sleep", sizeof("__sleep") - 1); |
722 | 0 | BG(serialize_lock)++; |
723 | 0 | res = call_user_function(NULL, struc, &fname, retval, 0, 0); |
724 | 0 | BG(serialize_lock)--; |
725 | 0 | zval_ptr_dtor_str(&fname); |
726 | |
|
727 | 0 | if (res == FAILURE || Z_ISUNDEF_P(retval)) { |
728 | 0 | zval_ptr_dtor(retval); |
729 | 0 | return FAILURE; |
730 | 0 | } |
731 | | |
732 | 0 | if (!HASH_OF(retval)) { |
733 | 0 | zend_class_entry *ce; |
734 | 0 | ZEND_ASSERT(Z_TYPE_P(struc) == IS_OBJECT); |
735 | 0 | ce = Z_OBJCE_P(struc); |
736 | 0 | zval_ptr_dtor(retval); |
737 | 0 | php_error_docref(NULL, E_NOTICE, "%s::__sleep should return an array only containing the names of instance-variables to serialize", ZSTR_VAL(ce->name)); |
738 | 0 | return FAILURE; |
739 | 0 | } |
740 | | |
741 | 0 | return SUCCESS; |
742 | 0 | } |
743 | | /* }}} */ |
744 | | |
745 | | static int php_var_serialize_call_magic_serialize(zval *retval, zval *obj) /* {{{ */ |
746 | 0 | { |
747 | 0 | BG(serialize_lock)++; |
748 | 0 | zend_call_known_instance_method_with_0_params( |
749 | 0 | Z_OBJCE_P(obj)->__serialize, Z_OBJ_P(obj), retval); |
750 | 0 | BG(serialize_lock)--; |
751 | |
|
752 | 0 | if (EG(exception)) { |
753 | 0 | zval_ptr_dtor(retval); |
754 | 0 | return FAILURE; |
755 | 0 | } |
756 | | |
757 | 0 | if (Z_TYPE_P(retval) != IS_ARRAY) { |
758 | 0 | zval_ptr_dtor(retval); |
759 | 0 | zend_type_error("%s::__serialize() must return an array", ZSTR_VAL(Z_OBJCE_P(obj)->name)); |
760 | 0 | return FAILURE; |
761 | 0 | } |
762 | | |
763 | 0 | return SUCCESS; |
764 | 0 | } |
765 | | /* }}} */ |
766 | | |
767 | | static int php_var_serialize_try_add_sleep_prop( |
768 | | HashTable *ht, HashTable *props, zend_string *name, zend_string *error_name, zval *struc) /* {{{ */ |
769 | 0 | { |
770 | 0 | zval *val = zend_hash_find(props, name); |
771 | 0 | if (val == NULL) { |
772 | 0 | return FAILURE; |
773 | 0 | } |
774 | | |
775 | 0 | if (Z_TYPE_P(val) == IS_INDIRECT) { |
776 | 0 | val = Z_INDIRECT_P(val); |
777 | 0 | if (Z_TYPE_P(val) == IS_UNDEF) { |
778 | 0 | zend_property_info *info = zend_get_typed_property_info_for_slot(Z_OBJ_P(struc), val); |
779 | 0 | if (info) { |
780 | 0 | return SUCCESS; |
781 | 0 | } |
782 | 0 | return FAILURE; |
783 | 0 | } |
784 | 0 | } |
785 | | |
786 | 0 | if (!zend_hash_add(ht, name, val)) { |
787 | 0 | php_error_docref(NULL, E_NOTICE, |
788 | 0 | "\"%s\" is returned from __sleep multiple times", ZSTR_VAL(error_name)); |
789 | 0 | return SUCCESS; |
790 | 0 | } |
791 | | |
792 | 0 | Z_TRY_ADDREF_P(val); |
793 | 0 | return SUCCESS; |
794 | 0 | } |
795 | | /* }}} */ |
796 | | |
797 | | static int php_var_serialize_get_sleep_props( |
798 | | HashTable *ht, zval *struc, HashTable *sleep_retval) /* {{{ */ |
799 | 0 | { |
800 | 0 | zend_class_entry *ce = Z_OBJCE_P(struc); |
801 | 0 | HashTable *props = zend_get_properties_for(struc, ZEND_PROP_PURPOSE_SERIALIZE); |
802 | 0 | zval *name_val; |
803 | 0 | int retval = SUCCESS; |
804 | |
|
805 | 0 | zend_hash_init(ht, zend_hash_num_elements(sleep_retval), NULL, ZVAL_PTR_DTOR, 0); |
806 | | /* TODO: Rewrite this by fetching the property info instead of trying out different |
807 | | * name manglings? */ |
808 | 0 | ZEND_HASH_FOREACH_VAL(sleep_retval, name_val) { |
809 | 0 | zend_string *name, *tmp_name, *priv_name, *prot_name; |
810 | |
|
811 | 0 | ZVAL_DEREF(name_val); |
812 | 0 | if (Z_TYPE_P(name_val) != IS_STRING) { |
813 | 0 | php_error_docref(NULL, E_NOTICE, |
814 | 0 | "%s::__sleep should return an array only containing the names of instance-variables to serialize", |
815 | 0 | ZSTR_VAL(ce->name)); |
816 | 0 | } |
817 | |
|
818 | 0 | name = zval_get_tmp_string(name_val, &tmp_name); |
819 | 0 | if (php_var_serialize_try_add_sleep_prop(ht, props, name, name, struc) == SUCCESS) { |
820 | 0 | zend_tmp_string_release(tmp_name); |
821 | 0 | continue; |
822 | 0 | } |
823 | | |
824 | 0 | if (EG(exception)) { |
825 | 0 | zend_tmp_string_release(tmp_name); |
826 | 0 | retval = FAILURE; |
827 | 0 | break; |
828 | 0 | } |
829 | | |
830 | 0 | priv_name = zend_mangle_property_name( |
831 | 0 | ZSTR_VAL(ce->name), ZSTR_LEN(ce->name), |
832 | 0 | ZSTR_VAL(name), ZSTR_LEN(name), ce->type & ZEND_INTERNAL_CLASS); |
833 | 0 | if (php_var_serialize_try_add_sleep_prop(ht, props, priv_name, name, struc) == SUCCESS) { |
834 | 0 | zend_tmp_string_release(tmp_name); |
835 | 0 | zend_string_release(priv_name); |
836 | 0 | continue; |
837 | 0 | } |
838 | 0 | zend_string_release(priv_name); |
839 | |
|
840 | 0 | if (EG(exception)) { |
841 | 0 | zend_tmp_string_release(tmp_name); |
842 | 0 | retval = FAILURE; |
843 | 0 | break; |
844 | 0 | } |
845 | | |
846 | 0 | prot_name = zend_mangle_property_name( |
847 | 0 | "*", 1, ZSTR_VAL(name), ZSTR_LEN(name), ce->type & ZEND_INTERNAL_CLASS); |
848 | 0 | if (php_var_serialize_try_add_sleep_prop(ht, props, prot_name, name, struc) == SUCCESS) { |
849 | 0 | zend_tmp_string_release(tmp_name); |
850 | 0 | zend_string_release(prot_name); |
851 | 0 | continue; |
852 | 0 | } |
853 | 0 | zend_string_release(prot_name); |
854 | |
|
855 | 0 | if (EG(exception)) { |
856 | 0 | zend_tmp_string_release(tmp_name); |
857 | 0 | retval = FAILURE; |
858 | 0 | break; |
859 | 0 | } |
860 | | |
861 | 0 | php_error_docref(NULL, E_NOTICE, |
862 | 0 | "\"%s\" returned as member variable from __sleep() but does not exist", ZSTR_VAL(name)); |
863 | 0 | zend_tmp_string_release(tmp_name); |
864 | 0 | } ZEND_HASH_FOREACH_END(); |
865 | |
|
866 | 0 | zend_release_properties(props); |
867 | 0 | return retval; |
868 | 0 | } |
869 | | /* }}} */ |
870 | | |
871 | | static void php_var_serialize_nested_data(smart_str *buf, zval *struc, HashTable *ht, uint32_t count, zend_bool incomplete_class, php_serialize_data_t var_hash) /* {{{ */ |
872 | 5.32k | { |
873 | 5.32k | smart_str_append_unsigned(buf, count); |
874 | 5.32k | smart_str_appendl(buf, ":{", 2); |
875 | 5.32k | if (count > 0) { |
876 | 4.32k | zend_string *key; |
877 | 4.32k | zval *data; |
878 | 4.32k | zend_ulong index; |
879 | | |
880 | 35.7k | ZEND_HASH_FOREACH_KEY_VAL_IND(ht, index, key, data) { |
881 | 15.6k | if (incomplete_class && strcmp(ZSTR_VAL(key), MAGIC_MEMBER) == 0) { |
882 | 0 | continue; |
883 | 0 | } |
884 | | |
885 | 15.6k | if (!key) { |
886 | 779 | php_var_serialize_long(buf, index); |
887 | 14.8k | } else { |
888 | 14.8k | php_var_serialize_string(buf, ZSTR_VAL(key), ZSTR_LEN(key)); |
889 | 14.8k | } |
890 | | |
891 | 15.6k | if (Z_ISREF_P(data) && Z_REFCOUNT_P(data) == 1) { |
892 | 46 | data = Z_REFVAL_P(data); |
893 | 46 | } |
894 | | |
895 | | /* we should still add element even if it's not OK, |
896 | | * since we already wrote the length of the array before */ |
897 | 15.6k | if (Z_TYPE_P(data) == IS_ARRAY) { |
898 | 4.04k | if (UNEXPECTED(Z_IS_RECURSIVE_P(data)) |
899 | 4.04k | || UNEXPECTED(Z_TYPE_P(struc) == IS_ARRAY && Z_ARR_P(data) == Z_ARR_P(struc))) { |
900 | 46 | php_add_var_hash(var_hash, struc); |
901 | 46 | smart_str_appendl(buf, "N;", 2); |
902 | 4.00k | } else { |
903 | 4.00k | if (Z_REFCOUNTED_P(data)) { |
904 | 3.94k | Z_PROTECT_RECURSION_P(data); |
905 | 3.94k | } |
906 | 4.00k | php_var_serialize_intern(buf, data, var_hash); |
907 | 4.00k | if (Z_REFCOUNTED_P(data)) { |
908 | 3.94k | Z_UNPROTECT_RECURSION_P(data); |
909 | 3.94k | } |
910 | 4.00k | } |
911 | 11.5k | } else { |
912 | 11.5k | php_var_serialize_intern(buf, data, var_hash); |
913 | 11.5k | } |
914 | 15.6k | } ZEND_HASH_FOREACH_END(); |
915 | 4.32k | } |
916 | 5.32k | smart_str_appendc(buf, '}'); |
917 | 5.32k | } |
918 | | /* }}} */ |
919 | | |
920 | | static void php_var_serialize_class(smart_str *buf, zval *struc, zval *retval_ptr, php_serialize_data_t var_hash) /* {{{ */ |
921 | 0 | { |
922 | 0 | HashTable props; |
923 | 0 | if (php_var_serialize_get_sleep_props(&props, struc, HASH_OF(retval_ptr)) == SUCCESS) { |
924 | 0 | php_var_serialize_class_name(buf, struc); |
925 | 0 | php_var_serialize_nested_data( |
926 | 0 | buf, struc, &props, zend_hash_num_elements(&props), /* incomplete_class */ 0, var_hash); |
927 | 0 | } |
928 | 0 | zend_hash_destroy(&props); |
929 | 0 | } |
930 | | /* }}} */ |
931 | | |
932 | | static void php_var_serialize_intern(smart_str *buf, zval *struc, php_serialize_data_t var_hash) /* {{{ */ |
933 | 17.1k | { |
934 | 17.1k | zend_long var_already; |
935 | 17.1k | HashTable *myht; |
936 | | |
937 | 17.1k | if (EG(exception)) { |
938 | 0 | return; |
939 | 0 | } |
940 | | |
941 | 17.1k | if (var_hash && (var_already = php_add_var_hash(var_hash, struc))) { |
942 | 144 | if (var_already == -1) { |
943 | | /* Reference to an object that failed to serialize, replace with null. */ |
944 | 0 | smart_str_appendl(buf, "N;", 2); |
945 | 0 | return; |
946 | 144 | } else if (Z_ISREF_P(struc)) { |
947 | 27 | smart_str_appendl(buf, "R:", 2); |
948 | 27 | smart_str_append_long(buf, var_already); |
949 | 27 | smart_str_appendc(buf, ';'); |
950 | 27 | return; |
951 | 117 | } else if (Z_TYPE_P(struc) == IS_OBJECT) { |
952 | 117 | smart_str_appendl(buf, "r:", 2); |
953 | 117 | smart_str_append_long(buf, var_already); |
954 | 117 | smart_str_appendc(buf, ';'); |
955 | 117 | return; |
956 | 117 | } |
957 | 17.0k | } |
958 | | |
959 | 17.0k | again: |
960 | 17.0k | switch (Z_TYPE_P(struc)) { |
961 | 39 | case IS_FALSE: |
962 | 39 | smart_str_appendl(buf, "b:0;", 4); |
963 | 39 | return; |
964 | | |
965 | 22 | case IS_TRUE: |
966 | 22 | smart_str_appendl(buf, "b:1;", 4); |
967 | 22 | return; |
968 | | |
969 | 511 | case IS_NULL: |
970 | 511 | smart_str_appendl(buf, "N;", 2); |
971 | 511 | return; |
972 | | |
973 | 3.57k | case IS_LONG: |
974 | 3.57k | php_var_serialize_long(buf, Z_LVAL_P(struc)); |
975 | 3.57k | return; |
976 | | |
977 | 1 | case IS_DOUBLE: { |
978 | 1 | char tmp_str[PHP_DOUBLE_MAX_LENGTH]; |
979 | 1 | smart_str_appendl(buf, "d:", 2); |
980 | 1 | php_gcvt(Z_DVAL_P(struc), (int)PG(serialize_precision), '.', 'E', tmp_str); |
981 | 1 | smart_str_appends(buf, tmp_str); |
982 | 1 | smart_str_appendc(buf, ';'); |
983 | 1 | return; |
984 | 0 | } |
985 | | |
986 | 7.25k | case IS_STRING: |
987 | 7.25k | php_var_serialize_string(buf, Z_STRVAL_P(struc), Z_STRLEN_P(struc)); |
988 | 7.25k | return; |
989 | | |
990 | 1.33k | case IS_OBJECT: { |
991 | 1.33k | zend_class_entry *ce = Z_OBJCE_P(struc); |
992 | 1.33k | zend_bool incomplete_class; |
993 | 1.33k | uint32_t count; |
994 | | |
995 | 1.33k | if (ce->__serialize) { |
996 | 0 | zval retval, obj; |
997 | 0 | zend_string *key; |
998 | 0 | zval *data; |
999 | 0 | zend_ulong index; |
1000 | |
|
1001 | 0 | ZVAL_OBJ_COPY(&obj, Z_OBJ_P(struc)); |
1002 | 0 | if (php_var_serialize_call_magic_serialize(&retval, &obj) == FAILURE) { |
1003 | 0 | if (!EG(exception)) { |
1004 | 0 | smart_str_appendl(buf, "N;", 2); |
1005 | 0 | } |
1006 | 0 | zval_ptr_dtor(&obj); |
1007 | 0 | return; |
1008 | 0 | } |
1009 | | |
1010 | 0 | php_var_serialize_class_name(buf, &obj); |
1011 | 0 | smart_str_append_unsigned(buf, zend_array_count(Z_ARRVAL(retval))); |
1012 | 0 | smart_str_appendl(buf, ":{", 2); |
1013 | 0 | ZEND_HASH_FOREACH_KEY_VAL_IND(Z_ARRVAL(retval), index, key, data) { |
1014 | 0 | if (!key) { |
1015 | 0 | php_var_serialize_long(buf, index); |
1016 | 0 | } else { |
1017 | 0 | php_var_serialize_string(buf, ZSTR_VAL(key), ZSTR_LEN(key)); |
1018 | 0 | } |
1019 | |
|
1020 | 0 | if (Z_ISREF_P(data) && Z_REFCOUNT_P(data) == 1) { |
1021 | 0 | data = Z_REFVAL_P(data); |
1022 | 0 | } |
1023 | 0 | php_var_serialize_intern(buf, data, var_hash); |
1024 | 0 | } ZEND_HASH_FOREACH_END(); |
1025 | 0 | smart_str_appendc(buf, '}'); |
1026 | |
|
1027 | 0 | zval_ptr_dtor(&obj); |
1028 | 0 | zval_ptr_dtor(&retval); |
1029 | 0 | return; |
1030 | 0 | } |
1031 | | |
1032 | 1.33k | if (ce->serialize != NULL) { |
1033 | | /* has custom handler */ |
1034 | 295 | unsigned char *serialized_data = NULL; |
1035 | 295 | size_t serialized_length; |
1036 | | |
1037 | 295 | if (ce->serialize(struc, &serialized_data, &serialized_length, (zend_serialize_data *)var_hash) == SUCCESS) { |
1038 | 21 | smart_str_appendl(buf, "C:", 2); |
1039 | 21 | smart_str_append_unsigned(buf, ZSTR_LEN(Z_OBJCE_P(struc)->name)); |
1040 | 21 | smart_str_appendl(buf, ":\"", 2); |
1041 | 21 | smart_str_append(buf, Z_OBJCE_P(struc)->name); |
1042 | 21 | smart_str_appendl(buf, "\":", 2); |
1043 | | |
1044 | 21 | smart_str_append_unsigned(buf, serialized_length); |
1045 | 21 | smart_str_appendl(buf, ":{", 2); |
1046 | 21 | smart_str_appendl(buf, (char *) serialized_data, serialized_length); |
1047 | 21 | smart_str_appendc(buf, '}'); |
1048 | 274 | } else { |
1049 | | /* Mark this value in the var_hash, to avoid creating references to it. */ |
1050 | 274 | zval *var_idx = zend_hash_index_find(&var_hash->ht, |
1051 | 274 | (zend_ulong) (zend_uintptr_t) Z_COUNTED_P(struc)); |
1052 | 274 | ZVAL_LONG(var_idx, -1); |
1053 | 274 | smart_str_appendl(buf, "N;", 2); |
1054 | 274 | } |
1055 | 295 | if (serialized_data) { |
1056 | 21 | efree(serialized_data); |
1057 | 21 | } |
1058 | 295 | return; |
1059 | 295 | } |
1060 | | |
1061 | 1.03k | if (ce != PHP_IC_ENTRY && zend_hash_str_exists(&ce->function_table, "__sleep", sizeof("__sleep")-1)) { |
1062 | 0 | zval retval, tmp; |
1063 | |
|
1064 | 0 | ZVAL_OBJ_COPY(&tmp, Z_OBJ_P(struc)); |
1065 | |
|
1066 | 0 | if (php_var_serialize_call_sleep(&retval, &tmp) == FAILURE) { |
1067 | 0 | if (!EG(exception)) { |
1068 | | /* we should still add element even if it's not OK, |
1069 | | * since we already wrote the length of the array before */ |
1070 | 0 | smart_str_appendl(buf, "N;", 2); |
1071 | 0 | } |
1072 | 0 | zval_ptr_dtor(&tmp); |
1073 | 0 | return; |
1074 | 0 | } |
1075 | | |
1076 | 0 | php_var_serialize_class(buf, &tmp, &retval, var_hash); |
1077 | 0 | zval_ptr_dtor(&retval); |
1078 | 0 | zval_ptr_dtor(&tmp); |
1079 | 0 | return; |
1080 | 0 | } |
1081 | | |
1082 | 1.03k | incomplete_class = php_var_serialize_class_name(buf, struc); |
1083 | 1.03k | myht = zend_get_properties_for(struc, ZEND_PROP_PURPOSE_SERIALIZE); |
1084 | | /* count after serializing name, since php_var_serialize_class_name |
1085 | | * changes the count if the variable is incomplete class */ |
1086 | 1.03k | count = zend_array_count(myht); |
1087 | 1.03k | if (count > 0 && incomplete_class) { |
1088 | 0 | --count; |
1089 | 0 | } |
1090 | 1.03k | php_var_serialize_nested_data(buf, struc, myht, count, incomplete_class, var_hash); |
1091 | 1.03k | zend_release_properties(myht); |
1092 | 1.03k | return; |
1093 | 1.03k | } |
1094 | 4.28k | case IS_ARRAY: |
1095 | 4.28k | smart_str_appendl(buf, "a:", 2); |
1096 | 4.28k | myht = Z_ARRVAL_P(struc); |
1097 | 4.28k | php_var_serialize_nested_data( |
1098 | 4.28k | buf, struc, myht, zend_array_count(myht), /* incomplete_class */ 0, var_hash); |
1099 | 4.28k | return; |
1100 | 36 | case IS_REFERENCE: |
1101 | 36 | struc = Z_REFVAL_P(struc); |
1102 | 36 | goto again; |
1103 | 0 | default: |
1104 | 0 | smart_str_appendl(buf, "i:0;", 4); |
1105 | 0 | return; |
1106 | 17.0k | } |
1107 | 17.0k | } |
1108 | | /* }}} */ |
1109 | | |
1110 | | PHPAPI void php_var_serialize(smart_str *buf, zval *struc, php_serialize_data_t *data) /* {{{ */ |
1111 | 1.60k | { |
1112 | 1.60k | php_var_serialize_intern(buf, struc, *data); |
1113 | 1.60k | smart_str_0(buf); |
1114 | 1.60k | } |
1115 | | /* }}} */ |
1116 | | |
1117 | 1.60k | PHPAPI php_serialize_data_t php_var_serialize_init(void) { |
1118 | 1.60k | struct php_serialize_data *d; |
1119 | | /* fprintf(stderr, "SERIALIZE_INIT == lock: %u, level: %u\n", BG(serialize_lock), BG(serialize).level); */ |
1120 | 1.60k | if (BG(serialize_lock) || !BG(serialize).level) { |
1121 | 1.60k | d = emalloc(sizeof(struct php_serialize_data)); |
1122 | 1.60k | zend_hash_init(&d->ht, 16, NULL, ZVAL_PTR_DTOR, 0); |
1123 | 1.60k | d->n = 0; |
1124 | 1.60k | if (!BG(serialize_lock)) { |
1125 | 1.60k | BG(serialize).data = d; |
1126 | 1.60k | BG(serialize).level = 1; |
1127 | 1.60k | } |
1128 | 0 | } else { |
1129 | 0 | d = BG(serialize).data; |
1130 | 0 | ++BG(serialize).level; |
1131 | 0 | } |
1132 | 1.60k | return d; |
1133 | 1.60k | } |
1134 | | |
1135 | 1.60k | PHPAPI void php_var_serialize_destroy(php_serialize_data_t d) { |
1136 | | /* fprintf(stderr, "SERIALIZE_DESTROY == lock: %u, level: %u\n", BG(serialize_lock), BG(serialize).level); */ |
1137 | 1.60k | if (BG(serialize_lock) || BG(serialize).level == 1) { |
1138 | 1.60k | zend_hash_destroy(&d->ht); |
1139 | 1.60k | efree(d); |
1140 | 1.60k | } |
1141 | 1.60k | if (!BG(serialize_lock) && !--BG(serialize).level) { |
1142 | 1.60k | BG(serialize).data = NULL; |
1143 | 1.60k | } |
1144 | 1.60k | } |
1145 | | |
1146 | | /* {{{ Returns a string representation of variable (which can later be unserialized) */ |
1147 | | PHP_FUNCTION(serialize) |
1148 | 1.60k | { |
1149 | 1.60k | zval *struc; |
1150 | 1.60k | php_serialize_data_t var_hash; |
1151 | 1.60k | smart_str buf = {0}; |
1152 | | |
1153 | 4.82k | ZEND_PARSE_PARAMETERS_START(1, 1) |
1154 | 1.60k | Z_PARAM_ZVAL(struc) |
1155 | 1.60k | ZEND_PARSE_PARAMETERS_END(); |
1156 | | |
1157 | 1.60k | PHP_VAR_SERIALIZE_INIT(var_hash); |
1158 | 1.60k | php_var_serialize(&buf, struc, &var_hash); |
1159 | 1.60k | PHP_VAR_SERIALIZE_DESTROY(var_hash); |
1160 | | |
1161 | 1.60k | if (EG(exception)) { |
1162 | 273 | smart_str_free(&buf); |
1163 | 273 | RETURN_THROWS(); |
1164 | 273 | } |
1165 | | |
1166 | 1.33k | if (buf.s) { |
1167 | 1.33k | RETURN_NEW_STR(buf.s); |
1168 | 0 | } else { |
1169 | 0 | RETURN_EMPTY_STRING(); |
1170 | 0 | } |
1171 | 1.33k | } |
1172 | | /* }}} */ |
1173 | | |
1174 | | /* {{{ Takes a string representation of variable and recreates it, subject to the optional unserialize options HashTable */ |
1175 | | PHPAPI void php_unserialize_with_options(zval *return_value, const char *buf, const size_t buf_len, HashTable *options, const char* function_name) |
1176 | 2.56k | { |
1177 | 2.56k | const unsigned char *p; |
1178 | 2.56k | php_unserialize_data_t var_hash; |
1179 | 2.56k | zval *retval; |
1180 | 2.56k | HashTable *class_hash = NULL, *prev_class_hash; |
1181 | 2.56k | zend_long prev_max_depth, prev_cur_depth; |
1182 | | |
1183 | 2.56k | if (buf_len == 0) { |
1184 | 19 | RETURN_FALSE; |
1185 | 19 | } |
1186 | | |
1187 | 2.54k | p = (const unsigned char*) buf; |
1188 | 2.54k | PHP_VAR_UNSERIALIZE_INIT(var_hash); |
1189 | | |
1190 | 2.54k | prev_class_hash = php_var_unserialize_get_allowed_classes(var_hash); |
1191 | 2.54k | prev_max_depth = php_var_unserialize_get_max_depth(var_hash); |
1192 | 2.54k | prev_cur_depth = php_var_unserialize_get_cur_depth(var_hash); |
1193 | 2.54k | if (options != NULL) { |
1194 | 16 | zval *classes, *max_depth; |
1195 | | |
1196 | 16 | classes = zend_hash_str_find_deref(options, "allowed_classes", sizeof("allowed_classes")-1); |
1197 | 16 | if (classes && Z_TYPE_P(classes) != IS_ARRAY && Z_TYPE_P(classes) != IS_TRUE && Z_TYPE_P(classes) != IS_FALSE) { |
1198 | 0 | php_error_docref(NULL, E_WARNING, "allowed_classes option should be array or boolean"); |
1199 | 0 | RETVAL_FALSE; |
1200 | 0 | goto cleanup; |
1201 | 0 | } |
1202 | | |
1203 | 16 | if(classes && (Z_TYPE_P(classes) == IS_ARRAY || !zend_is_true(classes))) { |
1204 | 0 | ALLOC_HASHTABLE(class_hash); |
1205 | 0 | zend_hash_init(class_hash, (Z_TYPE_P(classes) == IS_ARRAY)?zend_hash_num_elements(Z_ARRVAL_P(classes)):0, NULL, NULL, 0); |
1206 | 0 | } |
1207 | 16 | if(class_hash && Z_TYPE_P(classes) == IS_ARRAY) { |
1208 | 0 | zval *entry; |
1209 | 0 | zend_string *lcname; |
1210 | |
|
1211 | 0 | ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(classes), entry) { |
1212 | 0 | convert_to_string_ex(entry); |
1213 | 0 | lcname = zend_string_tolower(Z_STR_P(entry)); |
1214 | 0 | zend_hash_add_empty_element(class_hash, lcname); |
1215 | 0 | zend_string_release_ex(lcname, 0); |
1216 | 0 | } ZEND_HASH_FOREACH_END(); |
1217 | | |
1218 | | /* Exception during string conversion. */ |
1219 | 0 | if (EG(exception)) { |
1220 | 0 | goto cleanup; |
1221 | 0 | } |
1222 | 16 | } |
1223 | 16 | php_var_unserialize_set_allowed_classes(var_hash, class_hash); |
1224 | | |
1225 | 16 | max_depth = zend_hash_str_find_deref(options, "max_depth", sizeof("max_depth") - 1); |
1226 | 16 | if (max_depth) { |
1227 | 0 | if (Z_TYPE_P(max_depth) != IS_LONG) { |
1228 | 0 | zend_type_error("%s(): \"max_depth\" option must be of type int, %s given", function_name, zend_zval_type_name(max_depth)); |
1229 | 0 | goto cleanup; |
1230 | 0 | } |
1231 | 0 | if (Z_LVAL_P(max_depth) < 0) { |
1232 | 0 | zend_value_error("%s(): \"max_depth\" option must be greater than or equal to 0", function_name); |
1233 | 0 | goto cleanup; |
1234 | 0 | } |
1235 | | |
1236 | 0 | php_var_unserialize_set_max_depth(var_hash, Z_LVAL_P(max_depth)); |
1237 | | /* If the max_depth for a nested unserialize() call has been overridden, |
1238 | | * start counting from zero again (for the nested call only). */ |
1239 | 0 | php_var_unserialize_set_cur_depth(var_hash, 0); |
1240 | 0 | } |
1241 | 16 | } |
1242 | | |
1243 | 2.54k | if (BG(unserialize).level > 1) { |
1244 | 0 | retval = var_tmp_var(&var_hash); |
1245 | 2.54k | } else { |
1246 | 2.54k | retval = return_value; |
1247 | 2.54k | } |
1248 | 2.54k | if (!php_var_unserialize(retval, &p, p + buf_len, &var_hash)) { |
1249 | 1.54k | if (!EG(exception)) { |
1250 | 1.21k | php_error_docref(NULL, E_NOTICE, "Error at offset " ZEND_LONG_FMT " of %zd bytes", |
1251 | 1.21k | (zend_long)((char*)p - buf), buf_len); |
1252 | 1.21k | } |
1253 | 1.54k | if (BG(unserialize).level <= 1) { |
1254 | 1.54k | zval_ptr_dtor(return_value); |
1255 | 1.54k | } |
1256 | 1.54k | RETVAL_FALSE; |
1257 | 1.00k | } else if (BG(unserialize).level > 1) { |
1258 | 0 | ZVAL_COPY(return_value, retval); |
1259 | 1.00k | } else if (Z_REFCOUNTED_P(return_value)) { |
1260 | 916 | zend_refcounted *ref = Z_COUNTED_P(return_value); |
1261 | 916 | gc_check_possible_root(ref); |
1262 | 916 | } |
1263 | | |
1264 | 2.54k | cleanup: |
1265 | 2.54k | if (class_hash) { |
1266 | 0 | zend_hash_destroy(class_hash); |
1267 | 0 | FREE_HASHTABLE(class_hash); |
1268 | 0 | } |
1269 | | |
1270 | | /* Reset to previous options in case this is a nested call */ |
1271 | 2.54k | php_var_unserialize_set_allowed_classes(var_hash, prev_class_hash); |
1272 | 2.54k | php_var_unserialize_set_max_depth(var_hash, prev_max_depth); |
1273 | 2.54k | php_var_unserialize_set_cur_depth(var_hash, prev_cur_depth); |
1274 | 2.54k | PHP_VAR_UNSERIALIZE_DESTROY(var_hash); |
1275 | | |
1276 | | /* Per calling convention we must not return a reference here, so unwrap. We're doing this at |
1277 | | * the very end, because __wakeup() calls performed during UNSERIALIZE_DESTROY might affect |
1278 | | * the value we unwrap here. This is compatible with behavior in PHP <=7.0. */ |
1279 | 2.54k | if (Z_ISREF_P(return_value)) { |
1280 | 3 | zend_unwrap_reference(return_value); |
1281 | 3 | } |
1282 | 2.54k | } |
1283 | | /* }}} */ |
1284 | | |
1285 | | /* {{{ Takes a string representation of variable and recreates it */ |
1286 | | PHP_FUNCTION(unserialize) |
1287 | 2.57k | { |
1288 | 2.57k | char *buf = NULL; |
1289 | 2.57k | size_t buf_len; |
1290 | 2.57k | HashTable *options = NULL; |
1291 | | |
1292 | 7.73k | ZEND_PARSE_PARAMETERS_START(1, 2) |
1293 | 2.57k | Z_PARAM_STRING(buf, buf_len) |
1294 | 2.57k | Z_PARAM_OPTIONAL |
1295 | 22 | Z_PARAM_ARRAY_HT(options) |
1296 | 2.57k | ZEND_PARSE_PARAMETERS_END(); |
1297 | | |
1298 | 2.56k | php_unserialize_with_options(return_value, buf, buf_len, options, "unserialize"); |
1299 | 2.56k | } |
1300 | | /* }}} */ |
1301 | | |
1302 | | /* {{{ Returns the allocated by PHP memory */ |
1303 | 181 | PHP_FUNCTION(memory_get_usage) { |
1304 | 181 | zend_bool real_usage = 0; |
1305 | | |
1306 | 543 | ZEND_PARSE_PARAMETERS_START(0, 1) |
1307 | 181 | Z_PARAM_OPTIONAL |
1308 | 71 | Z_PARAM_BOOL(real_usage) |
1309 | 181 | ZEND_PARSE_PARAMETERS_END(); |
1310 | | |
1311 | 181 | RETURN_LONG(zend_memory_usage(real_usage)); |
1312 | 181 | } |
1313 | | /* }}} */ |
1314 | | |
1315 | | /* {{{ Returns the peak allocated by PHP memory */ |
1316 | 0 | PHP_FUNCTION(memory_get_peak_usage) { |
1317 | 0 | zend_bool real_usage = 0; |
1318 | |
|
1319 | 0 | ZEND_PARSE_PARAMETERS_START(0, 1) |
1320 | 0 | Z_PARAM_OPTIONAL |
1321 | 0 | Z_PARAM_BOOL(real_usage) |
1322 | 0 | ZEND_PARSE_PARAMETERS_END(); |
1323 | |
|
1324 | 0 | RETURN_LONG(zend_memory_peak_usage(real_usage)); |
1325 | 0 | } |
1326 | | /* }}} */ |
1327 | | |
1328 | | PHP_INI_BEGIN() |
1329 | | STD_PHP_INI_ENTRY("unserialize_max_depth", "4096", PHP_INI_ALL, OnUpdateLong, unserialize_max_depth, php_basic_globals, basic_globals) |
1330 | | PHP_INI_END() |
1331 | | |
1332 | | PHP_MINIT_FUNCTION(var) |
1333 | 3.00k | { |
1334 | 3.00k | REGISTER_INI_ENTRIES(); |
1335 | 3.00k | return SUCCESS; |
1336 | 3.00k | } |