Coverage Report

Created: 2026-02-14 06:52

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