php-src/ext/standard/var.c

1156 lines
32 KiB
C
Raw Normal View History

/*
+----------------------------------------------------------------------+
2014-09-19 16:33:14 +00:00
| PHP Version 7 |
+----------------------------------------------------------------------+
2017-01-02 15:30:12 +00:00
| Copyright (c) 1997-2017 The PHP Group |
+----------------------------------------------------------------------+
2006-01-01 12:51:34 +00:00
| This source file is subject to version 3.01 of the PHP license, |
1999-07-16 13:13:16 +00:00
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
2006-01-01 12:51:34 +00:00
| http://www.php.net/license/3_01.txt |
1999-07-16 13:13:16 +00:00
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Jani Lehtimäki <jkl@njet.net> |
2000-10-29 09:14:55 +00:00
| Thies C. Arntzen <thies@thieso.net> |
| Sascha Schumann <sascha@schumann.cx> |
+----------------------------------------------------------------------+
2000-12-11 12:52:52 +00:00
*/
2003-01-31 16:24:12 +00:00
/* $Id$ */
1999-09-15 11:09:32 +00:00
/* {{{ includes
*/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include "php.h"
#include "php_string.h"
#include "php_var.h"
#include "zend_smart_str.h"
#include "basic_functions.h"
#include "php_incomplete_class.h"
/* }}} */
struct php_serialize_data {
HashTable ht;
uint32_t n;
};
#define COMMON (is_ref ? "&" : "")
2014-12-13 22:06:14 +00:00
static void php_array_element_dump(zval *zv, zend_ulong index, zend_string *key, int level) /* {{{ */
{
2014-05-27 22:13:45 +00:00
if (key == NULL) { /* numeric key */
2014-08-25 18:22:49 +00:00
php_printf("%*c[" ZEND_LONG_FMT "]=>\n", level + 1, ' ', index);
} else { /* string key */
php_printf("%*c[\"", level + 1, ' ');
PHPWRITE(ZSTR_VAL(key), ZSTR_LEN(key));
php_printf("\"]=>\n");
}
2014-12-13 22:06:14 +00:00
php_var_dump(zv, level + 2);
}
/* }}} */
2014-12-13 22:06:14 +00:00
static void php_object_property_dump(zval *zv, zend_ulong index, zend_string *key, int level) /* {{{ */
{
const char *prop_name, *class_name;
2014-05-27 22:13:45 +00:00
if (key == NULL) { /* numeric key */
2014-08-25 18:22:49 +00:00
php_printf("%*c[" ZEND_LONG_FMT "]=>\n", level + 1, ' ', index);
} else { /* string key */
int unmangle = zend_unmangle_property_name(key, &class_name, &prop_name);
php_printf("%*c[", level + 1, ' ');
if (class_name && unmangle == SUCCESS) {
if (class_name[0] == '*') {
php_printf("\"%s\":protected", prop_name);
} else {
php_printf("\"%s\":\"%s\":private", prop_name, class_name);
}
} else {
php_printf("\"");
PHPWRITE(ZSTR_VAL(key), ZSTR_LEN(key));
php_printf("\"");
}
ZEND_PUTS("]=>\n");
}
2014-12-13 22:06:14 +00:00
php_var_dump(zv, level + 2);
}
/* }}} */
2014-12-13 22:06:14 +00:00
PHPAPI void php_var_dump(zval *struc, int level) /* {{{ */
{
HashTable *myht;
zend_string *class_name;
2007-10-11 01:03:19 +00:00
int is_temp;
int is_ref = 0;
2014-08-25 17:24:55 +00:00
zend_ulong num;
2014-05-27 22:13:45 +00:00
zend_string *key;
zval *val;
uint32_t count;
if (level > 1) {
php_printf("%*c", level - 1, ' ');
}
2014-03-09 12:03:24 +00:00
again:
switch (Z_TYPE_P(struc)) {
case IS_FALSE:
php_printf("%sbool(false)\n", COMMON);
break;
case IS_TRUE:
php_printf("%sbool(true)\n", COMMON);
2014-03-09 12:03:24 +00:00
break;
case IS_NULL:
php_printf("%sNULL\n", COMMON);
break;
2014-08-25 17:24:55 +00:00
case IS_LONG:
2014-08-25 18:22:49 +00:00
php_printf("%sint(" ZEND_LONG_FMT ")\n", COMMON, Z_LVAL_P(struc));
2014-03-09 12:03:24 +00:00
break;
case IS_DOUBLE:
php_printf("%sfloat(%.*G)\n", COMMON, (int) EG(precision), Z_DVAL_P(struc));
break;
case IS_STRING:
php_printf("%sstring(%zd) \"", COMMON, Z_STRLEN_P(struc));
2014-08-25 17:24:55 +00:00
PHPWRITE(Z_STRVAL_P(struc), Z_STRLEN_P(struc));
2014-03-09 12:03:24 +00:00
PUTS("\"\n");
break;
case IS_ARRAY:
myht = Z_ARRVAL_P(struc);
if (level > 1 && ZEND_HASH_APPLY_PROTECTION(myht) && ++myht->u.v.nApplyCount > 1) {
2014-03-09 12:03:24 +00:00
PUTS("*RECURSION*\n");
--myht->u.v.nApplyCount;
2014-03-09 12:03:24 +00:00
return;
}
count = zend_array_count(myht);
php_printf("%sarray(%d) {\n", COMMON, count);
2014-03-09 12:03:24 +00:00
is_temp = 0;
2014-05-27 22:13:45 +00:00
ZEND_HASH_FOREACH_KEY_VAL_IND(myht, num, key, val) {
2014-12-13 22:06:14 +00:00
php_array_element_dump(val, num, key, level);
2014-05-27 22:13:45 +00:00
} ZEND_HASH_FOREACH_END();
if (level > 1 && ZEND_HASH_APPLY_PROTECTION(myht)) {
--myht->u.v.nApplyCount;
}
2014-05-27 22:13:45 +00:00
if (is_temp) {
zend_hash_destroy(myht);
efree(myht);
}
if (level > 1) {
php_printf("%*c", level-1, ' ');
}
PUTS("}\n");
break;
2014-03-09 12:03:24 +00:00
case IS_OBJECT:
if (Z_OBJ_APPLY_COUNT_P(struc) > 0) {
2014-03-09 12:03:24 +00:00
PUTS("*RECURSION*\n");
return;
}
Z_OBJ_INC_APPLY_COUNT_P(struc);
myht = Z_OBJDEBUG_P(struc, is_temp);
2014-12-13 22:06:14 +00:00
class_name = Z_OBJ_HANDLER_P(struc, get_class_name)(Z_OBJ_P(struc));
php_printf("%sobject(%s)#%d (%d) {\n", COMMON, ZSTR_VAL(class_name), Z_OBJ_HANDLE_P(struc), myht ? zend_array_count(myht) : 0);
zend_string_release(class_name);
2014-03-09 12:03:24 +00:00
if (myht) {
2014-08-25 17:24:55 +00:00
zend_ulong num;
2014-05-27 22:13:45 +00:00
zend_string *key;
zval *val;
ZEND_HASH_FOREACH_KEY_VAL_IND(myht, num, key, val) {
2014-12-13 22:06:14 +00:00
php_object_property_dump(val, num, key, level);
2014-05-27 22:13:45 +00:00
} ZEND_HASH_FOREACH_END();
2014-03-09 12:03:24 +00:00
if (is_temp) {
zend_hash_destroy(myht);
efree(myht);
}
}
if (level > 1) {
php_printf("%*c", level-1, ' ');
}
PUTS("}\n");
Z_OBJ_DEC_APPLY_COUNT_P(struc);
2014-03-09 12:03:24 +00:00
break;
case IS_RESOURCE: {
2014-12-13 22:06:14 +00:00
const char *type_name = zend_rsrc_list_get_rsrc_type(Z_RES_P(struc));
php_printf("%sresource(%d) of type (%s)\n", COMMON, Z_RES_P(struc)->handle, type_name ? type_name : "Unknown");
2014-03-09 12:03:24 +00:00
break;
}
2014-03-09 12:03:24 +00:00
case IS_REFERENCE:
2014-04-15 17:56:30 +00:00
//??? hide references with refcount==1 (for compatibility)
2014-03-09 12:03:24 +00:00
if (Z_REFCOUNT_P(struc) > 1) {
is_ref = 1;
}
struc = Z_REFVAL_P(struc);
goto again;
break;
default:
php_printf("%sUNKNOWN:0\n", COMMON);
break;
}
}
/* }}} */
2000-05-17 19:42:08 +00:00
/* {{{ proto void var_dump(mixed var)
Dumps a string representation of variable to output */
PHP_FUNCTION(var_dump)
{
zval *args;
int argc;
int i;
2016-12-31 02:03:33 +00:00
ZEND_PARSE_PARAMETERS_START(1, -1)
Z_PARAM_VARIADIC('+', args, argc)
ZEND_PARSE_PARAMETERS_END();
for (i = 0; i < argc; i++) {
2014-12-13 22:06:14 +00:00
php_var_dump(&args[i], 1);
}
}
/* }}} */
2014-12-13 22:06:14 +00:00
static void zval_array_element_dump(zval *zv, zend_ulong index, zend_string *key, int level) /* {{{ */
{
2014-05-27 22:13:45 +00:00
if (key == NULL) { /* numeric key */
2014-08-25 18:22:49 +00:00
php_printf("%*c[" ZEND_LONG_FMT "]=>\n", level + 1, ' ', index);
} else { /* string key */
php_printf("%*c[\"", level + 1, ' ');
PHPWRITE(ZSTR_VAL(key), ZSTR_LEN(key));
php_printf("\"]=>\n");
}
2014-12-13 22:06:14 +00:00
php_debug_zval_dump(zv, level + 2);
}
/* }}} */
2014-12-13 22:06:14 +00:00
static void zval_object_property_dump(zval *zv, zend_ulong index, zend_string *key, int level) /* {{{ */
{
const char *prop_name, *class_name;
2014-05-27 22:13:45 +00:00
if (key == NULL) { /* numeric key */
2014-08-25 18:22:49 +00:00
php_printf("%*c[" ZEND_LONG_FMT "]=>\n", level + 1, ' ', index);
} else { /* string key */
zend_unmangle_property_name(key, &class_name, &prop_name);
php_printf("%*c[", level + 1, ' ');
if (class_name) {
if (class_name[0] == '*') {
php_printf("\"%s\":protected", prop_name);
} else {
php_printf("\"%s\":\"%s\":private", prop_name, class_name);
}
} else {
php_printf("\"%s\"", prop_name);
}
ZEND_PUTS("]=>\n");
}
2014-12-13 22:06:14 +00:00
php_debug_zval_dump(zv, level + 2);
}
/* }}} */
2014-12-13 22:06:14 +00:00
PHPAPI void php_debug_zval_dump(zval *struc, int level) /* {{{ */
{
2002-03-14 14:05:13 +00:00
HashTable *myht = NULL;
zend_string *class_name;
2007-11-01 16:54:43 +00:00
int is_temp = 0;
int is_ref = 0;
2014-08-25 17:24:55 +00:00
zend_ulong index;
2014-05-27 22:13:45 +00:00
zend_string *key;
zval *val;
uint32_t count;
if (level > 1) {
php_printf("%*c", level - 1, ' ');
}
again:
switch (Z_TYPE_P(struc)) {
case IS_FALSE:
php_printf("%sbool(false)\n", COMMON);
break;
case IS_TRUE:
php_printf("%sbool(true)\n", COMMON);
break;
case IS_NULL:
2014-02-21 16:35:40 +00:00
php_printf("%sNULL\n", COMMON);
break;
2014-08-25 17:24:55 +00:00
case IS_LONG:
php_printf("%sint(" ZEND_LONG_FMT ")\n", COMMON, Z_LVAL_P(struc));
break;
case IS_DOUBLE:
php_printf("%sfloat(%.*G)\n", COMMON, (int) EG(precision), Z_DVAL_P(struc));
break;
case IS_STRING:
php_printf("%sstring(%zd) \"", COMMON, Z_STRLEN_P(struc));
2014-08-25 17:24:55 +00:00
PHPWRITE(Z_STRVAL_P(struc), Z_STRLEN_P(struc));
php_printf("\" refcount(%u)\n", Z_REFCOUNTED_P(struc) ? Z_REFCOUNT_P(struc) : 1);
break;
case IS_ARRAY:
myht = Z_ARRVAL_P(struc);
if (level > 1 && ZEND_HASH_APPLY_PROTECTION(myht) && myht->u.v.nApplyCount++ > 1) {
2014-05-27 22:13:45 +00:00
myht->u.v.nApplyCount--;
2005-07-21 07:18:02 +00:00
PUTS("*RECURSION*\n");
return;
}
count = zend_array_count(myht);
php_printf("%sarray(%d) refcount(%u){\n", COMMON, count, Z_REFCOUNTED_P(struc) ? Z_REFCOUNT_P(struc) : 1);
2014-05-27 22:13:45 +00:00
ZEND_HASH_FOREACH_KEY_VAL_IND(myht, index, key, val) {
2014-12-13 22:06:14 +00:00
zval_array_element_dump(val, index, key, level);
2014-05-27 22:13:45 +00:00
} ZEND_HASH_FOREACH_END();
if (level > 1 && ZEND_HASH_APPLY_PROTECTION(myht)) {
myht->u.v.nApplyCount--;
}
2014-05-27 22:13:45 +00:00
if (is_temp) {
zend_hash_destroy(myht);
efree(myht);
}
if (level > 1) {
php_printf("%*c", level - 1, ' ');
}
PUTS("}\n");
break;
case IS_OBJECT:
myht = Z_OBJDEBUG_P(struc, is_temp);
2014-05-27 22:13:45 +00:00
if (myht) {
if (myht->u.v.nApplyCount > 1) {
PUTS("*RECURSION*\n");
return;
} else {
myht->u.v.nApplyCount++;
}
2005-07-21 07:18:02 +00:00
}
2014-12-13 22:06:14 +00:00
class_name = Z_OBJ_HANDLER_P(struc, get_class_name)(Z_OBJ_P(struc));
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));
2014-08-25 17:24:55 +00:00
zend_string_release(class_name);
if (myht) {
2014-05-27 22:13:45 +00:00
ZEND_HASH_FOREACH_KEY_VAL_IND(myht, index, key, val) {
2014-12-13 22:06:14 +00:00
zval_object_property_dump(val, index, key, level);
2014-05-27 22:13:45 +00:00
} ZEND_HASH_FOREACH_END();
myht->u.v.nApplyCount--;
2007-11-01 16:54:43 +00:00
if (is_temp) {
zend_hash_destroy(myht);
efree(myht);
}
}
if (level > 1) {
php_printf("%*c", level - 1, ' ');
}
PUTS("}\n");
break;
case IS_RESOURCE: {
2014-12-13 22:06:14 +00:00
const char *type_name = zend_rsrc_list_get_rsrc_type(Z_RES_P(struc));
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));
break;
}
case IS_REFERENCE:
2014-04-15 17:56:30 +00:00
//??? hide references with refcount==1 (for compatibility)
if (Z_REFCOUNT_P(struc) > 1) {
is_ref = 1;
}
struc = Z_REFVAL_P(struc);
goto again;
default:
2002-03-15 02:00:55 +00:00
php_printf("%sUNKNOWN:0\n", COMMON);
break;
}
}
/* }}} */
/* {{{ proto void debug_zval_dump(mixed var)
Dumps a string representation of an internal zend value to output. */
PHP_FUNCTION(debug_zval_dump)
{
zval *args;
int argc;
int i;
2016-12-31 02:03:33 +00:00
ZEND_PARSE_PARAMETERS_START(1, -1)
Z_PARAM_VARIADIC('+', args, argc)
ZEND_PARSE_PARAMETERS_END();
for (i = 0; i < argc; i++) {
2014-12-13 22:06:14 +00:00
php_debug_zval_dump(&args[i], 1);
}
}
/* }}} */
#define buffer_append_spaces(buf, num_spaces) \
do { \
char *tmp_spaces; \
2014-10-22 07:25:53 +00:00
size_t tmp_spaces_len; \
tmp_spaces_len = spprintf(&tmp_spaces, 0,"%*c", num_spaces, ' '); \
smart_str_appendl(buf, tmp_spaces, tmp_spaces_len); \
efree(tmp_spaces); \
} while(0);
2014-12-13 22:06:14 +00:00
static void php_array_element_export(zval *zv, zend_ulong index, zend_string *key, int level, smart_str *buf) /* {{{ */
{
2014-05-27 22:13:45 +00:00
if (key == NULL) { /* numeric key */
buffer_append_spaces(buf, level+1);
2014-08-25 17:24:55 +00:00
smart_str_append_long(buf, (zend_long) index);
smart_str_appendl(buf, " => ", 4);
} else { /* string key */
zend_string *tmp_str;
zend_string *ckey = php_addcslashes(key, 0, "'\\", 2);
tmp_str = php_str_to_str(ZSTR_VAL(ckey), ZSTR_LEN(ckey), "\0", 1, "' . \"\\0\" . '", 12);
buffer_append_spaces(buf, level + 1);
smart_str_appendc(buf, '\'');
smart_str_append(buf, tmp_str);
smart_str_appendl(buf, "' => ", 5);
2014-08-25 17:24:55 +00:00
zend_string_free(ckey);
zend_string_free(tmp_str);
}
2014-12-13 22:06:14 +00:00
php_var_export_ex(zv, level + 2, buf);
smart_str_appendc(buf, ',');
smart_str_appendc(buf, '\n');
}
/* }}} */
2014-12-13 22:06:14 +00:00
static void php_object_element_export(zval *zv, zend_ulong index, zend_string *key, int level, smart_str *buf) /* {{{ */
2002-03-14 18:41:35 +00:00
{
buffer_append_spaces(buf, level + 2);
2014-05-27 22:13:45 +00:00
if (key != NULL) {
const char *class_name, *prop_name;
size_t prop_name_len;
zend_string *pname_esc;
2015-01-03 09:22:58 +00:00
zend_unmangle_property_name_ex(key, &class_name, &prop_name, &prop_name_len);
pname_esc = php_addcslashes(zend_string_init(prop_name, prop_name_len, 0), 1, "'\\", 2);
smart_str_appendc(buf, '\'');
smart_str_append(buf, pname_esc);
smart_str_appendc(buf, '\'');
2014-08-25 17:24:55 +00:00
zend_string_release(pname_esc);
} else {
2014-08-25 17:24:55 +00:00
smart_str_append_long(buf, (zend_long) index);
2002-03-14 18:41:35 +00:00
}
smart_str_appendl(buf, " => ", 4);
2014-12-13 22:06:14 +00:00
php_var_export_ex(zv, level + 2, buf);
smart_str_appendc(buf, ',');
smart_str_appendc(buf, '\n');
2002-03-14 18:41:35 +00:00
}
/* }}} */
2002-03-14 18:41:35 +00:00
2014-12-13 22:06:14 +00:00
PHPAPI void php_var_export_ex(zval *struc, int level, smart_str *buf) /* {{{ */
{
HashTable *myht;
char tmp_str[PHP_DOUBLE_MAX_LENGTH];
zend_string *ztmp, *ztmp2;
2014-08-25 17:24:55 +00:00
zend_ulong index;
2014-05-27 22:13:45 +00:00
zend_string *key;
zval *val;
2001-12-08 23:46:19 +00:00
2014-03-05 04:10:43 +00:00
again:
switch (Z_TYPE_P(struc)) {
case IS_FALSE:
smart_str_appendl(buf, "false", 5);
break;
case IS_TRUE:
smart_str_appendl(buf, "true", 4);
2014-03-05 04:10:43 +00:00
break;
case IS_NULL:
smart_str_appendl(buf, "NULL", 4);
2014-03-05 04:10:43 +00:00
break;
2014-08-25 17:24:55 +00:00
case IS_LONG:
smart_str_append_long(buf, Z_LVAL_P(struc));
2014-03-05 04:10:43 +00:00
break;
case IS_DOUBLE:
php_gcvt(Z_DVAL_P(struc), (int)PG(serialize_precision), '.', 'E', tmp_str);
smart_str_appends(buf, tmp_str);
/* Without a decimal point, PHP treats a number literal as an int.
* This check even works for scientific notation, because the
* mantissa always contains a decimal point.
2016-01-08 17:20:52 +00:00
* We need to check for finiteness, because INF, -INF and NAN
* must not have a decimal point added.
*/
2016-01-08 17:20:52 +00:00
if (zend_finite(Z_DVAL_P(struc)) && NULL == strchr(tmp_str, '.')) {
smart_str_appendl(buf, ".0", 2);
}
2014-03-05 04:10:43 +00:00
break;
case IS_STRING:
ztmp = php_addcslashes(Z_STR_P(struc), 0, "'\\", 2);
ztmp2 = php_str_to_str(ZSTR_VAL(ztmp), ZSTR_LEN(ztmp), "\0", 1, "' . \"\\0\" . '", 12);
2014-03-05 04:10:43 +00:00
smart_str_appendc(buf, '\'');
smart_str_append(buf, ztmp2);
2014-03-05 04:10:43 +00:00
smart_str_appendc(buf, '\'');
2014-08-25 17:24:55 +00:00
zend_string_free(ztmp);
zend_string_free(ztmp2);
2014-03-05 04:10:43 +00:00
break;
case IS_ARRAY:
myht = Z_ARRVAL_P(struc);
if (ZEND_HASH_APPLY_PROTECTION(myht) && myht->u.v.nApplyCount++ > 0) {
2014-05-27 22:13:45 +00:00
myht->u.v.nApplyCount--;
2014-03-05 04:10:43 +00:00
smart_str_appendl(buf, "NULL", 4);
zend_error(E_WARNING, "var_export does not handle circular references");
return;
}
if (level > 1) {
smart_str_appendc(buf, '\n');
buffer_append_spaces(buf, level - 1);
}
smart_str_appendl(buf, "array (\n", 8);
2014-05-27 22:13:45 +00:00
ZEND_HASH_FOREACH_KEY_VAL_IND(myht, index, key, val) {
2014-12-13 22:06:14 +00:00
php_array_element_export(val, index, key, level, buf);
2014-05-27 22:13:45 +00:00
} ZEND_HASH_FOREACH_END();
if (ZEND_HASH_APPLY_PROTECTION(myht)) {
myht->u.v.nApplyCount--;
}
2014-03-05 04:10:43 +00:00
if (level > 1) {
buffer_append_spaces(buf, level - 1);
}
smart_str_appendc(buf, ')');
2015-01-03 09:22:58 +00:00
2014-03-05 04:10:43 +00:00
break;
case IS_OBJECT:
myht = Z_OBJPROP_P(struc);
2014-05-27 22:13:45 +00:00
if (myht) {
if (myht->u.v.nApplyCount > 0) {
smart_str_appendl(buf, "NULL", 4);
zend_error(E_WARNING, "var_export does not handle circular references");
return;
} else {
myht->u.v.nApplyCount++;
}
2014-03-05 04:10:43 +00:00
}
if (level > 1) {
smart_str_appendc(buf, '\n');
buffer_append_spaces(buf, level - 1);
}
smart_str_append(buf, Z_OBJCE_P(struc)->name);
2014-03-05 04:10:43 +00:00
smart_str_appendl(buf, "::__set_state(array(\n", 21);
2014-03-05 04:10:43 +00:00
if (myht) {
2014-05-27 22:13:45 +00:00
ZEND_HASH_FOREACH_KEY_VAL_IND(myht, index, key, val) {
2014-12-13 22:06:14 +00:00
php_object_element_export(val, index, key, level, buf);
2014-05-27 22:13:45 +00:00
} ZEND_HASH_FOREACH_END();
myht->u.v.nApplyCount--;
2014-03-05 04:10:43 +00:00
}
if (level > 1) {
buffer_append_spaces(buf, level - 1);
}
smart_str_appendl(buf, "))", 2);
break;
case IS_REFERENCE:
struc = Z_REFVAL_P(struc);
goto again;
break;
default:
smart_str_appendl(buf, "NULL", 4);
break;
}
}
/* }}} */
/* FOR BC reasons, this will always perform and then print */
2014-12-13 22:06:14 +00:00
PHPAPI void php_var_export(zval *struc, int level) /* {{{ */
{
smart_str buf = {0};
2014-12-13 22:06:14 +00:00
php_var_export_ex(struc, level, &buf);
2014-02-18 09:42:46 +00:00
smart_str_0(&buf);
PHPWRITE(ZSTR_VAL(buf.s), ZSTR_LEN(buf.s));
smart_str_free(&buf);
}
/* }}} */
2002-02-28 16:00:26 +00:00
/* {{{ proto mixed var_export(mixed var [, bool return])
2002-03-14 18:41:35 +00:00
Outputs or returns a string representation of a variable */
PHP_FUNCTION(var_export)
{
zval *var;
zend_bool return_output = 0;
smart_str buf = {0};
2016-12-31 02:03:33 +00:00
ZEND_PARSE_PARAMETERS_START(1, 2)
Z_PARAM_ZVAL_DEREF(var)
Z_PARAM_OPTIONAL
Z_PARAM_BOOL(return_output)
ZEND_PARSE_PARAMETERS_END();
2014-12-13 22:06:14 +00:00
php_var_export_ex(var, 1, &buf);
smart_str_0 (&buf);
if (return_output) {
RETURN_NEW_STR(buf.s);
} else {
PHPWRITE(ZSTR_VAL(buf.s), ZSTR_LEN(buf.s));
2014-02-18 09:42:46 +00:00
smart_str_free(&buf);
}
}
/* }}} */
2014-12-13 22:06:14 +00:00
static void php_var_serialize_intern(smart_str *buf, zval *struc, php_serialize_data_t var_hash);
2014-12-13 22:06:14 +00:00
static inline zend_long php_add_var_hash(php_serialize_data_t data, zval *var) /* {{{ */
{
zval *zv;
zend_ulong key;
zend_bool is_ref = Z_ISREF_P(var);
data->n += 1;
if (!is_ref && Z_TYPE_P(var) != IS_OBJECT) {
return 0;
}
/* References to objects are treated as if the reference didn't exist */
if (is_ref && Z_TYPE_P(Z_REFVAL_P(var)) == IS_OBJECT) {
var = Z_REFVAL_P(var);
}
/* Index for the variable is stored using the numeric value of the pointer to
* the zend_refcounted struct */
key = (zend_ulong) (zend_uintptr_t) Z_COUNTED_P(var);
zv = zend_hash_index_find(&data->ht, key);
if (zv) {
/* References are only counted once, undo the data->n increment above */
if (is_ref) {
data->n -= 1;
}
return Z_LVAL_P(zv);
} else {
zval zv_n;
ZVAL_LONG(&zv_n, data->n);
zend_hash_index_add_new(&data->ht, key, &zv_n);
/* Additionally to the index, we also store the variable, to ensure that it is
* not destroyed during serialization and its pointer reused. The variable is
* stored at the numeric value of the pointer + 1, which cannot be the location
* of another zend_refcounted structure. */
zend_hash_index_add_new(&data->ht, key + 1, var);
Z_ADDREF_P(var);
return 0;
}
}
/* }}} */
2014-08-25 17:24:55 +00:00
static inline void php_var_serialize_long(smart_str *buf, zend_long val) /* {{{ */
{
smart_str_appendl(buf, "i:", 2);
2014-08-25 17:24:55 +00:00
smart_str_append_long(buf, val);
smart_str_appendc(buf, ';');
}
/* }}} */
static inline void php_var_serialize_string(smart_str *buf, char *str, size_t len) /* {{{ */
{
smart_str_appendl(buf, "s:", 2);
smart_str_append_unsigned(buf, len);
smart_str_appendl(buf, ":\"", 2);
smart_str_appendl(buf, str, len);
smart_str_appendl(buf, "\";", 2);
}
/* }}} */
2014-12-13 22:06:14 +00:00
static inline zend_bool php_var_serialize_class_name(smart_str *buf, zval *struc) /* {{{ */
{
PHP_CLASS_ATTRIBUTES;
PHP_SET_CLASS_ATTRIBUTES(struc);
smart_str_appendl(buf, "O:", 2);
smart_str_append_unsigned(buf, ZSTR_LEN(class_name));
smart_str_appendl(buf, ":\"", 2);
smart_str_append(buf, class_name);
smart_str_appendl(buf, "\":", 2);
PHP_CLEANUP_CLASS_ATTRIBUTES();
return incomplete_class;
}
/* }}} */
2017-03-12 20:32:47 +00:00
static int php_var_serialize_call_sleep(zval *retval, zval *struc) /* {{{ */
{
zval fname;
int res;
ZVAL_STRINGL(&fname, "__sleep", sizeof("__sleep") - 1);
BG(serialize_lock)++;
res = call_user_function_ex(CG(function_table), struc, &fname, retval, 0, 0, 1, NULL);
BG(serialize_lock)--;
zval_dtor(&fname);
if (res == FAILURE || Z_ISUNDEF_P(retval)) {
zval_ptr_dtor(retval);
return FAILURE;
}
if (!HASH_OF(retval)) {
zval_ptr_dtor(retval);
php_error_docref(NULL, E_NOTICE, "__sleep should return an array only containing the names of instance-variables to serialize");
return FAILURE;
}
return SUCCESS;
}
/* }}} */
static void php_var_serialize_collect_names(HashTable *ht, HashTable *src) /* {{{ */
{
zval *val;
2017-03-12 20:32:47 +00:00
zend_string *name;
2017-03-12 20:32:47 +00:00
zend_hash_init(ht, zend_hash_num_elements(src), NULL, NULL, 0);
ZEND_HASH_FOREACH_VAL(src, val) {
if (Z_TYPE_P(val) != IS_STRING) {
php_error_docref(NULL, E_NOTICE,
"__sleep should return an array only containing the names of instance-variables to serialize.");
}
2017-03-12 20:32:47 +00:00
name = zval_get_string(val);
if (zend_hash_exists(ht, name)) {
php_error_docref(NULL, E_NOTICE,
"\"%s\" is returned from __sleep multiple times", ZSTR_VAL(name));
zend_string_release(name);
continue;
}
zend_hash_add_empty_element(ht, name);
zend_string_release(name);
} ZEND_HASH_FOREACH_END();
}
/* }}} */
2014-12-13 22:06:14 +00:00
static void php_var_serialize_class(smart_str *buf, zval *struc, zval *retval_ptr, php_serialize_data_t var_hash) /* {{{ */
{
2017-03-12 20:32:47 +00:00
zend_class_entry *ce = Z_OBJCE_P(struc);
HashTable names, *propers;
zval nval;
zend_string *name;
php_var_serialize_class_name(buf, struc);
php_var_serialize_collect_names(&names, HASH_OF(retval_ptr));
smart_str_append_unsigned(buf, zend_hash_num_elements(&names));
smart_str_appendl(buf, ":{", 2);
ZVAL_NULL(&nval);
propers = Z_OBJPROP_P(struc);
ZEND_HASH_FOREACH_STR_KEY(&names, name) {
zval *val = zend_hash_find(propers, name);
if (val != NULL) {
if (Z_TYPE_P(val) == IS_INDIRECT) {
val = Z_INDIRECT_P(val);
if (Z_TYPE_P(val) == IS_UNDEF) {
continue;
}
}
php_var_serialize_string(buf, ZSTR_VAL(name), ZSTR_LEN(name));
php_var_serialize_intern(buf, val, var_hash);
} else {
zend_string *prot_name, *priv_name;
priv_name = zend_mangle_property_name(
ZSTR_VAL(ce->name), ZSTR_LEN(ce->name), ZSTR_VAL(name), ZSTR_LEN(name), ce->type & ZEND_INTERNAL_CLASS);
val = zend_hash_find(propers, priv_name);
if (val != NULL) {
if (Z_TYPE_P(val) == IS_INDIRECT) {
val = Z_INDIRECT_P(val);
if (Z_ISUNDEF_P(val)) {
continue;
}
}
2017-03-12 20:32:47 +00:00
php_var_serialize_string(buf, ZSTR_VAL(priv_name), ZSTR_LEN(priv_name));
zend_string_free(priv_name);
php_var_serialize_intern(buf, val, var_hash);
continue;
}
zend_string_free(priv_name);
prot_name = zend_mangle_property_name(
"*", 1, ZSTR_VAL(name), ZSTR_LEN(name), ce->type & ZEND_INTERNAL_CLASS);
val = zend_hash_find(propers, prot_name);
if (val != NULL) {
if (Z_TYPE_P(val) == IS_INDIRECT) {
val = Z_INDIRECT_P(val);
if (Z_TYPE_P(val) == IS_UNDEF) {
2014-08-25 17:24:55 +00:00
zend_string_free(prot_name);
2017-03-12 20:32:47 +00:00
continue;
}
}
2017-03-12 20:32:47 +00:00
php_var_serialize_string(buf, ZSTR_VAL(prot_name), ZSTR_LEN(prot_name));
zend_string_free(prot_name);
php_var_serialize_intern(buf, val, var_hash);
continue;
}
2017-03-12 20:32:47 +00:00
zend_string_free(prot_name);
php_var_serialize_string(buf, ZSTR_VAL(name), ZSTR_LEN(name));
php_var_serialize_intern(buf, &nval, var_hash);
php_error_docref(NULL, E_NOTICE,
"\"%s\" returned as member variable from __sleep() but does not exist", ZSTR_VAL(name));
}
} ZEND_HASH_FOREACH_END();
smart_str_appendc(buf, '}');
2017-03-12 20:32:47 +00:00
zend_hash_destroy(&names);
}
/* }}} */
2014-12-13 22:06:14 +00:00
static void php_var_serialize_intern(smart_str *buf, zval *struc, php_serialize_data_t var_hash) /* {{{ */
{
2014-10-22 07:37:25 +00:00
zend_long var_already;
HashTable *myht;
if (EG(exception)) {
return;
}
2014-12-13 22:06:14 +00:00
if (var_hash && (var_already = php_add_var_hash(var_hash, struc))) {
if (Z_ISREF_P(struc)) {
smart_str_appendl(buf, "R:", 2);
smart_str_append_long(buf, var_already);
smart_str_appendc(buf, ';');
2004-02-05 09:07:46 +00:00
return;
} else if (Z_TYPE_P(struc) == IS_OBJECT) {
smart_str_appendl(buf, "r:", 2);
smart_str_append_long(buf, var_already);
smart_str_appendc(buf, ';');
2004-02-05 09:07:46 +00:00
return;
}
}
again:
switch (Z_TYPE_P(struc)) {
case IS_FALSE:
smart_str_appendl(buf, "b:0;", 4);
return;
case IS_TRUE:
smart_str_appendl(buf, "b:1;", 4);
1999-07-01 09:26:05 +00:00
return;
2000-01-04 13:22:58 +00:00
case IS_NULL:
smart_str_appendl(buf, "N;", 2);
return;
2014-08-25 17:24:55 +00:00
case IS_LONG:
php_var_serialize_long(buf, Z_LVAL_P(struc));
return;
case IS_DOUBLE: {
char tmp_str[PHP_DOUBLE_MAX_LENGTH];
smart_str_appendl(buf, "d:", 2);
php_gcvt(Z_DVAL_P(struc), (int)PG(serialize_precision), '.', 'E', tmp_str);
smart_str_appends(buf, tmp_str);
smart_str_appendc(buf, ';');
return;
}
case IS_STRING:
2014-08-25 17:24:55 +00:00
php_var_serialize_string(buf, Z_STRVAL_P(struc), Z_STRLEN_P(struc));
return;
case IS_OBJECT: {
zend_class_entry *ce = Z_OBJCE_P(struc);
if (ce->serialize != NULL) {
/* has custom handler */
unsigned char *serialized_data = NULL;
size_t serialized_length;
2014-12-13 22:06:14 +00:00
if (ce->serialize(struc, &serialized_data, &serialized_length, (zend_serialize_data *)var_hash) == SUCCESS) {
smart_str_appendl(buf, "C:", 2);
smart_str_append_unsigned(buf, ZSTR_LEN(Z_OBJCE_P(struc)->name));
smart_str_appendl(buf, ":\"", 2);
smart_str_append(buf, Z_OBJCE_P(struc)->name);
smart_str_appendl(buf, "\":", 2);
smart_str_append_unsigned(buf, serialized_length);
smart_str_appendl(buf, ":{", 2);
smart_str_appendl(buf, (char *) serialized_data, serialized_length);
smart_str_appendc(buf, '}');
} else {
smart_str_appendl(buf, "N;", 2);
}
if (serialized_data) {
efree(serialized_data);
}
return;
}
2016-03-10 07:26:15 +00:00
if (ce != PHP_IC_ENTRY && zend_hash_str_exists(&ce->function_table, "__sleep", sizeof("__sleep")-1)) {
2017-03-12 20:32:47 +00:00
zval retval, tmp;
ZVAL_COPY(&tmp, struc);
2017-03-12 20:32:47 +00:00
if (php_var_serialize_call_sleep(&retval, &tmp) == FAILURE) {
if (!EG(exception)) {
/* we should still add element even if it's not OK,
* since we already wrote the length of the array before */
smart_str_appendl(buf, "N;", 2);
}
zval_ptr_dtor(&tmp);
return;
}
2017-03-12 20:32:47 +00:00
php_var_serialize_class(buf, &tmp, &retval, var_hash);
2014-02-25 08:54:26 +00:00
zval_ptr_dtor(&retval);
zval_ptr_dtor(&tmp);
2017-03-12 20:32:47 +00:00
return;
}
/* fall-through */
}
case IS_ARRAY: {
uint32_t i;
zend_bool incomplete_class = 0;
if (Z_TYPE_P(struc) == IS_ARRAY) {
smart_str_appendl(buf, "a:", 2);
myht = Z_ARRVAL_P(struc);
i = zend_array_count(myht);
} else {
2014-12-13 22:06:14 +00:00
incomplete_class = php_var_serialize_class_name(buf, struc);
myht = Z_OBJPROP_P(struc);
/* count after serializing name, since php_var_serialize_class_name
* changes the count if the variable is incomplete class */
i = zend_array_count(myht);
if (i > 0 && incomplete_class) {
--i;
}
}
smart_str_append_unsigned(buf, i);
smart_str_appendl(buf, ":{", 2);
if (i > 0) {
zend_string *key;
zval *data;
2014-08-25 17:24:55 +00:00
zend_ulong index;
ZEND_HASH_FOREACH_KEY_VAL_IND(myht, index, key, data) {
if (incomplete_class && strcmp(ZSTR_VAL(key), MAGIC_MEMBER) == 0) {
continue;
}
if (!key) {
php_var_serialize_long(buf, index);
} else {
php_var_serialize_string(buf, ZSTR_VAL(key), ZSTR_LEN(key));
}
if (Z_ISREF_P(data) && Z_REFCOUNT_P(data) == 1) {
2016-05-17 10:07:20 +00:00
data = Z_REFVAL_P(data);
}
/* we should still add element even if it's not OK,
* since we already wrote the length of the array before */
if ((Z_TYPE_P(data) == IS_ARRAY && Z_TYPE_P(struc) == IS_ARRAY && Z_ARR_P(data) == Z_ARR_P(struc))
|| (Z_TYPE_P(data) == IS_ARRAY && Z_ARRVAL_P(data)->u.v.nApplyCount > 1)
) {
smart_str_appendl(buf, "N;", 2);
} else {
if (Z_TYPE_P(data) == IS_ARRAY && ZEND_HASH_APPLY_PROTECTION(Z_ARRVAL_P(data))) {
Z_ARRVAL_P(data)->u.v.nApplyCount++;
}
2014-12-13 22:06:14 +00:00
php_var_serialize_intern(buf, data, var_hash);
if (Z_TYPE_P(data) == IS_ARRAY && ZEND_HASH_APPLY_PROTECTION(Z_ARRVAL_P(data))) {
Z_ARRVAL_P(data)->u.v.nApplyCount--;
}
}
} ZEND_HASH_FOREACH_END();
}
smart_str_appendc(buf, '}');
return;
}
case IS_REFERENCE:
struc = Z_REFVAL_P(struc);
goto again;
default:
smart_str_appendl(buf, "i:0;", 4);
return;
}
}
/* }}} */
2014-12-13 22:06:14 +00:00
PHPAPI void php_var_serialize(smart_str *buf, zval *struc, php_serialize_data_t *data) /* {{{ */
{
2014-12-13 22:06:14 +00:00
php_var_serialize_intern(buf, struc, *data);
smart_str_0(buf);
}
/* }}} */
PHPAPI php_serialize_data_t php_var_serialize_init() {
struct php_serialize_data *d;
/* fprintf(stderr, "SERIALIZE_INIT == lock: %u, level: %u\n", BG(serialize_lock), BG(serialize).level); */
if (BG(serialize_lock) || !BG(serialize).level) {
d = emalloc(sizeof(struct php_serialize_data));
zend_hash_init(&d->ht, 16, NULL, ZVAL_PTR_DTOR, 0);
d->n = 0;
if (!BG(serialize_lock)) {
BG(serialize).data = d;
BG(serialize).level = 1;
}
} else {
d = BG(serialize).data;
++BG(serialize).level;
}
return d;
}
PHPAPI void php_var_serialize_destroy(php_serialize_data_t d) {
/* fprintf(stderr, "SERIALIZE_DESTROY == lock: %u, level: %u\n", BG(serialize_lock), BG(serialize).level); */
if (BG(serialize_lock) || BG(serialize).level == 1) {
zend_hash_destroy(&d->ht);
efree(d);
}
if (!BG(serialize_lock) && !--BG(serialize).level) {
BG(serialize).data = NULL;
}
}
/* {{{ proto string serialize(mixed variable)
Returns a string representation of variable (which can later be unserialized) */
PHP_FUNCTION(serialize)
{
zval *struc;
php_serialize_data_t var_hash;
smart_str buf = {0};
2016-12-31 02:03:33 +00:00
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_ZVAL_DEREF(struc)
ZEND_PARSE_PARAMETERS_END();
PHP_VAR_SERIALIZE_INIT(var_hash);
2014-12-13 22:06:14 +00:00
php_var_serialize(&buf, struc, &var_hash);
PHP_VAR_SERIALIZE_DESTROY(var_hash);
if (EG(exception)) {
smart_str_free(&buf);
RETURN_FALSE;
}
2014-02-18 09:42:46 +00:00
if (buf.s) {
RETURN_NEW_STR(buf.s);
} else {
RETURN_NULL();
}
}
/* }}} */
2007-06-26 11:51:14 +00:00
/* {{{ proto mixed unserialize(string variable_representation[, array allowed_classes])
Takes a string representation of variable and recreates it */
PHP_FUNCTION(unserialize)
{
char *buf = NULL;
size_t buf_len;
const unsigned char *p;
php_unserialize_data_t var_hash;
zval *options = NULL, *classes = NULL;
zval *retval;
HashTable *class_hash = NULL, *prev_class_hash;
2016-12-31 02:03:33 +00:00
ZEND_PARSE_PARAMETERS_START(1, 2)
Z_PARAM_STRING(buf, buf_len)
Z_PARAM_OPTIONAL
Z_PARAM_ARRAY(options)
ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
if (buf_len == 0) {
RETURN_FALSE;
}
1999-09-15 11:42:28 +00:00
p = (const unsigned char*) buf;
PHP_VAR_UNSERIALIZE_INIT(var_hash);
prev_class_hash = php_var_unserialize_get_allowed_classes(var_hash);
if (options != NULL) {
classes = zend_hash_str_find(Z_ARRVAL_P(options), "allowed_classes", sizeof("allowed_classes")-1);
if (classes && Z_TYPE_P(classes) != IS_ARRAY && Z_TYPE_P(classes) != IS_TRUE && Z_TYPE_P(classes) != IS_FALSE) {
php_error_docref(NULL, E_WARNING, "allowed_classes option should be array or boolean");
PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
RETURN_FALSE;
}
2014-12-13 22:06:14 +00:00
if(classes && (Z_TYPE_P(classes) == IS_ARRAY || !zend_is_true(classes))) {
ALLOC_HASHTABLE(class_hash);
zend_hash_init(class_hash, (Z_TYPE_P(classes) == IS_ARRAY)?zend_hash_num_elements(Z_ARRVAL_P(classes)):0, NULL, NULL, 0);
}
if(class_hash && Z_TYPE_P(classes) == IS_ARRAY) {
zval *entry;
zend_string *lcname;
ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(classes), entry) {
convert_to_string_ex(entry);
lcname = zend_string_tolower(Z_STR_P(entry));
zend_hash_add_empty_element(class_hash, lcname);
zend_string_release(lcname);
} ZEND_HASH_FOREACH_END();
}
php_var_unserialize_set_allowed_classes(var_hash, class_hash);
}
retval = var_tmp_var(&var_hash);
2016-11-05 22:10:19 +00:00
if (!php_var_unserialize(retval, &p, p + buf_len, &var_hash)) {
if (!EG(exception)) {
php_error_docref(NULL, E_NOTICE, "Error at offset " ZEND_LONG_FMT " of %zd bytes",
(zend_long)((char*)p - buf), buf_len);
}
RETVAL_FALSE;
} else {
2016-11-05 22:10:19 +00:00
ZVAL_COPY(return_value, retval);
}
2016-04-07 11:28:54 +00:00
if (class_hash) {
zend_hash_destroy(class_hash);
FREE_HASHTABLE(class_hash);
}
/* Reset to previous allowed_classes in case this is a nested call */
php_var_unserialize_set_allowed_classes(var_hash, prev_class_hash);
PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
/* Per calling convention we must not return a reference here, so unwrap. We're doing this at
* the very end, because __wakeup() calls performed during UNSERIALIZE_DESTROY might affect
* the value we unwrap here. This is compatible with behavior in PHP <=7.0. */
if (Z_ISREF_P(return_value)) {
zend_unwrap_reference(return_value);
}
}
/* }}} */
2015-06-23 21:46:07 +00:00
/* {{{ proto int memory_get_usage([bool real_usage])
Returns the allocated by PHP memory */
PHP_FUNCTION(memory_get_usage) {
zend_bool real_usage = 0;
2016-12-31 02:03:33 +00:00
ZEND_PARSE_PARAMETERS_START(0, 1)
Z_PARAM_OPTIONAL
Z_PARAM_BOOL(real_usage)
ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
2014-12-13 22:06:14 +00:00
RETURN_LONG(zend_memory_usage(real_usage));
}
/* }}} */
2015-06-23 21:46:07 +00:00
/* {{{ proto int memory_get_peak_usage([bool real_usage])
Returns the peak allocated by PHP memory */
PHP_FUNCTION(memory_get_peak_usage) {
zend_bool real_usage = 0;
2016-12-31 02:03:33 +00:00
ZEND_PARSE_PARAMETERS_START(0, 1)
Z_PARAM_OPTIONAL
Z_PARAM_BOOL(real_usage)
ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
2014-12-13 22:06:14 +00:00
RETURN_LONG(zend_memory_peak_usage(real_usage));
}
/* }}} */
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: sw=4 ts=4 fdm=marker
* vim<600: sw=4 ts=4
*/