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