1573b4602SAndi Gutmans/*
2573b4602SAndi Gutmans   +----------------------------------------------------------------------+
3573b4602SAndi Gutmans   | Zend Engine                                                          |
4573b4602SAndi Gutmans   +----------------------------------------------------------------------+
5a6519d05SXinchen Hui   | Copyright (c) 1998-2018 Zend Technologies Ltd. (http://www.zend.com) |
6573b4602SAndi Gutmans   +----------------------------------------------------------------------+
7d863d52aSSebastian Bergmann   | This source file is subject to version 2.00 of the Zend license,     |
8b5b11177SZeev Suraski   | that is bundled with this package in the file LICENSE, and is        |
9f68c7ff2SJames Cox   | available through the world-wide-web at the following url:           |
10d863d52aSSebastian Bergmann   | http://www.zend.com/license/2_00.txt.                                |
11b5b11177SZeev Suraski   | If you did not receive a copy of the Zend license and are unable to  |
12b5b11177SZeev Suraski   | obtain it through the world-wide-web, please send a note to          |
13b5b11177SZeev Suraski   | license@zend.com so we can mail you a copy immediately.              |
14573b4602SAndi Gutmans   +----------------------------------------------------------------------+
159afce019SZeev Suraski   | Authors: Andi Gutmans <andi@php.net>                                 |
169afce019SZeev Suraski   |          Zeev Suraski <zeev@php.net>                                 |
17573b4602SAndi Gutmans   +----------------------------------------------------------------------+
18573b4602SAndi Gutmans*/
19573b4602SAndi Gutmans
20573b4602SAndi Gutmans#include "zend.h"
21573b4602SAndi Gutmans#include "zend_extensions.h"
227080fa5bSAndi Gutmans#include "zend_modules.h"
23573b4602SAndi Gutmans#include "zend_constants.h"
24573b4602SAndi Gutmans#include "zend_list.h"
25da9faa2cSZeev Suraski#include "zend_API.h"
2600ed3badSZeev Suraski#include "zend_exceptions.h"
2713d840bcSZeev Suraski#include "zend_builtin_functions.h"
2854d53055SZeev Suraski#include "zend_ini.h"
29fcaa1c6dSDmitry Stogov#include "zend_vm.h"
3008b9fdc8SDavid Soria Parra#include "zend_dtrace.h"
31cf6ab0e9SAnatol Belski#include "zend_virtual_cwd.h"
321b29e0caSNikita Popov#include "zend_smart_str.h"
335fc9aa9aSNikita Popov#include "zend_smart_string.h"
3481132608SAnatol Belski#include "zend_cpuinfo.h"
35573b4602SAndi Gutmans
367a87fcbbSZeev Suraski#ifdef ZTS
37cd2b462aSAnatol BelskiZEND_API int compiler_globals_id;
38cd2b462aSAnatol BelskiZEND_API int executor_globals_id;
39cd2b462aSAnatol Belskistatic HashTable *global_function_table = NULL;
40cd2b462aSAnatol Belskistatic HashTable *global_class_table = NULL;
41cd2b462aSAnatol Belskistatic HashTable *global_constants_table = NULL;
42cd2b462aSAnatol Belskistatic HashTable *global_auto_globals_table = NULL;
43cd2b462aSAnatol Belskistatic HashTable *global_persistent_list = NULL;
44cd2b462aSAnatol BelskiZEND_TSRMLS_CACHE_DEFINE()
45b4892511SJani Taskinen# define GLOBAL_FUNCTION_TABLE		global_function_table
46b4892511SJani Taskinen# define GLOBAL_CLASS_TABLE			global_class_table
47b4892511SJani Taskinen# define GLOBAL_CONSTANTS_TABLE		global_constants_table
48b4892511SJani Taskinen# define GLOBAL_AUTO_GLOBALS_TABLE	global_auto_globals_table
497a87fcbbSZeev Suraski#else
50b4892511SJani Taskinen# define GLOBAL_FUNCTION_TABLE		CG(function_table)
51b4892511SJani Taskinen# define GLOBAL_CLASS_TABLE			CG(class_table)
52b4892511SJani Taskinen# define GLOBAL_AUTO_GLOBALS_TABLE	CG(auto_globals)
53b4892511SJani Taskinen# define GLOBAL_CONSTANTS_TABLE		EG(zend_constants)
547a87fcbbSZeev Suraski#endif
557a87fcbbSZeev Suraski
56cd2b462aSAnatol BelskiZEND_API zend_utility_values zend_uv;
57fa45f64bSRemi ColletZEND_API zend_bool zend_dtrace_enabled;
58cd2b462aSAnatol Belski
59cd2b462aSAnatol Belski/* version information */
60cd2b462aSAnatol Belskistatic char *zend_version_info;
61cd2b462aSAnatol Belskistatic uint32_t zend_version_info_length;
62a6519d05SXinchen Hui#define ZEND_CORE_VERSION_INFO	"Zend Engine v" ZEND_VERSION ", Copyright (c) 1998-2018 Zend Technologies\n"
63cd2b462aSAnatol Belski#define PRINT_ZVAL_INDENT 4
64cd2b462aSAnatol Belski
65573b4602SAndi Gutmans/* true multithread-shared globals */
6692dd5e61SStanislav MalyshevZEND_API zend_class_entry *zend_standard_class_def = NULL;
67c3e3c98eSAnatol BelskiZEND_API size_t (*zend_printf)(const char *format, ...);
685dba4774SZeev SuraskiZEND_API zend_write_func_t zend_write;
692fa8d67aSDmitry StogovZEND_API FILE *(*zend_fopen)(const char *filename, zend_string **opened_path);
70bdeb220fSAnatol BelskiZEND_API int (*zend_stream_open_function)(const char *filename, zend_file_handle *handle);
71bdeb220fSAnatol BelskiZEND_API void (*zend_ticks_function)(int ticks);
72d0460d8fSDmitry StogovZEND_API void (*zend_interrupt_function)(zend_execute_data *execute_data);
73b204b3abSAnatol BelskiZEND_API void (*zend_error_cb)(int type, const char *error_filename, const uint32_t error_lineno, const char *format, va_list args);
745fc9aa9aSNikita Popovvoid (*zend_printf_to_smart_string)(smart_string *buf, const char *format, va_list ap);
755fc9aa9aSNikita Popovvoid (*zend_printf_to_smart_str)(smart_str *buf, const char *format, va_list ap);
76bdeb220fSAnatol BelskiZEND_API char *(*zend_getenv)(char *name, size_t name_len);
7749d9b301SAnatol BelskiZEND_API zend_string *(*zend_resolve_path)(const char *filename, size_t filename_len);
78b4903aefSDmitry StogovZEND_API int (*zend_post_startup_cb)(void) = NULL;
79536221beSStanislav Malyshev
80bdeb220fSAnatol Belskivoid (*zend_on_timeout)(int seconds);
81bd115087SZeev Suraski
82bdeb220fSAnatol Belskistatic void (*zend_message_dispatcher_p)(zend_long message, const void *data);
8388d7ca44SDmitry Stogovstatic zval *(*zend_get_configuration_directive_p)(zend_string *name);
84d035af4aSZeev Suraski
8549ea143bSDmitry Stogov#if ZEND_RC_DEBUG
8649ea143bSDmitry StogovZEND_API zend_bool zend_rc_debug = 0;
8749ea143bSDmitry Stogov#endif
8849ea143bSDmitry Stogov
89b4892511SJani Taskinenstatic ZEND_INI_MH(OnUpdateErrorReporting) /* {{{ */
904be862b0SZeev Suraski{
914be862b0SZeev Suraski	if (!new_value) {
92d3e50265SMarcus Boerger		EG(error_reporting) = E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED;
934be862b0SZeev Suraski	} else {
947aa76271SDmitry Stogov		EG(error_reporting) = atoi(ZSTR_VAL(new_value));
954be862b0SZeev Suraski	}
964be862b0SZeev Suraski	return SUCCESS;
974be862b0SZeev Suraski}
98b4892511SJani Taskinen/* }}} */
994be862b0SZeev Suraski
10071592cecSDmitry Stogovstatic ZEND_INI_MH(OnUpdateGCEnabled) /* {{{ */
10171592cecSDmitry Stogov{
102baa98901SDmitry Stogov	zend_bool val;
103baa98901SDmitry Stogov
1046c035f53SDmitry Stogov	val = zend_ini_parse_bool(new_value);
105f844d40fSDmitry Stogov	gc_enable(val);
106baa98901SDmitry Stogov
10771592cecSDmitry Stogov	return SUCCESS;
10871592cecSDmitry Stogov}
10971592cecSDmitry Stogov/* }}} */
11071592cecSDmitry Stogov
111baa98901SDmitry Stogovstatic ZEND_INI_DISP(zend_gc_enabled_displayer_cb) /* {{{ */
112baa98901SDmitry Stogov{
113baa98901SDmitry Stogov	if (gc_enabled()) {
114baa98901SDmitry Stogov		ZEND_PUTS("On");
115baa98901SDmitry Stogov	} else {
116baa98901SDmitry Stogov		ZEND_PUTS("Off");
117baa98901SDmitry Stogov	}
118baa98901SDmitry Stogov}
119baa98901SDmitry Stogov/* }}} */
120baa98901SDmitry Stogov
121baa98901SDmitry Stogov
122bbf3d43cSMoriyoshi Koizumistatic ZEND_INI_MH(OnUpdateScriptEncoding) /* {{{ */
123bbf3d43cSMoriyoshi Koizumi{
124e46852d2SAnatol Belski	if (!CG(multibyte)) {
125bbf3d43cSMoriyoshi Koizumi		return FAILURE;
126bbf3d43cSMoriyoshi Koizumi	}
127bdeb220fSAnatol Belski	if (!zend_multibyte_get_functions()) {
128bbf3d43cSMoriyoshi Koizumi		return SUCCESS;
129bbf3d43cSMoriyoshi Koizumi	}
1307aa76271SDmitry Stogov	return zend_multibyte_set_script_encoding_by_string(new_value ? ZSTR_VAL(new_value) : NULL, new_value ? ZSTR_LEN(new_value) : 0);
131bbf3d43cSMoriyoshi Koizumi}
132bbf3d43cSMoriyoshi Koizumi/* }}} */
133bbf3d43cSMoriyoshi Koizumi
1349a20323eSDmitry Stogovstatic ZEND_INI_MH(OnUpdateAssertions) /* {{{ */
1359a20323eSDmitry Stogov{
1369a20323eSDmitry Stogov	zend_long *p, val;
1379a20323eSDmitry Stogov#ifndef ZTS
1389a20323eSDmitry Stogov	char *base = (char *) mh_arg2;
1399a20323eSDmitry Stogov#else
1409a20323eSDmitry Stogov	char *base;
1419a20323eSDmitry Stogov
1429a20323eSDmitry Stogov	base = (char *) ts_resource(*((int *) mh_arg2));
1439a20323eSDmitry Stogov#endif
1449a20323eSDmitry Stogov
1459a20323eSDmitry Stogov	p = (zend_long *) (base+(size_t) mh_arg1);
1469a20323eSDmitry Stogov
1470d1eeeb6SAnatol Belski	val = zend_atol(ZSTR_VAL(new_value), ZSTR_LEN(new_value));
1489a20323eSDmitry Stogov
1499a20323eSDmitry Stogov	if (stage != ZEND_INI_STAGE_STARTUP &&
1509a20323eSDmitry Stogov	    stage != ZEND_INI_STAGE_SHUTDOWN &&
1519a20323eSDmitry Stogov	    *p != val &&
1529a20323eSDmitry Stogov	    (*p < 0 || val < 0)) {
1539a20323eSDmitry Stogov		zend_error(E_WARNING, "zend.assertions may be completely enabled or disabled only in php.ini");
1549a20323eSDmitry Stogov		return FAILURE;
1559a20323eSDmitry Stogov	}
1569a20323eSDmitry Stogov
1579a20323eSDmitry Stogov	*p = val;
1589a20323eSDmitry Stogov	return SUCCESS;
1599a20323eSDmitry Stogov}
1609a20323eSDmitry Stogov/* }}} */
161bbf3d43cSMoriyoshi Koizumi
1624be862b0SZeev SuraskiZEND_INI_BEGIN()
163eb6fd52eSZeev Suraski	ZEND_INI_ENTRY("error_reporting",				NULL,		ZEND_INI_ALL,		OnUpdateErrorReporting)
164fb2c890cSRasmus Lerdorf	STD_ZEND_INI_ENTRY("zend.assertions",				"1",    ZEND_INI_ALL,       OnUpdateAssertions,           assertions,   zend_executor_globals,  executor_globals)
165baa98901SDmitry Stogov	ZEND_INI_ENTRY3_EX("zend.enable_gc",				"1",	ZEND_INI_ALL,		OnUpdateGCEnabled, NULL, NULL, NULL, zend_gc_enabled_displayer_cb)
166ab93d8c6SDmitry Stogov 	STD_ZEND_INI_BOOLEAN("zend.multibyte", "0", ZEND_INI_PERDIR, OnUpdateBool, multibyte,      zend_compiler_globals, compiler_globals)
167bbf3d43cSMoriyoshi Koizumi 	ZEND_INI_ENTRY("zend.script_encoding",			NULL,		ZEND_INI_ALL,		OnUpdateScriptEncoding)
168bbf3d43cSMoriyoshi Koizumi 	STD_ZEND_INI_BOOLEAN("zend.detect_unicode",			"1",	ZEND_INI_ALL,		OnUpdateBool, detect_unicode, zend_compiler_globals, compiler_globals)
16934d93f0cSIlia Alshanetsky#ifdef ZEND_SIGNALS
17034d93f0cSIlia Alshanetsky	STD_ZEND_INI_BOOLEAN("zend.signal_check", "0", ZEND_INI_SYSTEM, OnUpdateBool, check, zend_signal_globals_t, zend_signal_globals)
17134d93f0cSIlia Alshanetsky#endif
1724be862b0SZeev SuraskiZEND_INI_END()
1734be862b0SZeev Suraski
1745fc9aa9aSNikita PopovZEND_API size_t zend_vspprintf(char **pbuf, size_t max_len, const char *format, va_list ap) /* {{{ */
1755fc9aa9aSNikita Popov{
1765fc9aa9aSNikita Popov	smart_string buf = {0};
1775fc9aa9aSNikita Popov
1785fc9aa9aSNikita Popov	/* since there are places where (v)spprintf called without checking for null,
1795fc9aa9aSNikita Popov	   a bit of defensive coding here */
1805fc9aa9aSNikita Popov	if (!pbuf) {
1815fc9aa9aSNikita Popov		return 0;
1825fc9aa9aSNikita Popov	}
1835fc9aa9aSNikita Popov
1845fc9aa9aSNikita Popov	zend_printf_to_smart_string(&buf, format, ap);
1855fc9aa9aSNikita Popov
1865fc9aa9aSNikita Popov	if (max_len && buf.len > max_len) {
1875fc9aa9aSNikita Popov		buf.len = max_len;
1885fc9aa9aSNikita Popov	}
1895fc9aa9aSNikita Popov
1905fc9aa9aSNikita Popov	smart_string_0(&buf);
1915fc9aa9aSNikita Popov
1925fc9aa9aSNikita Popov	if (buf.c) {
1935fc9aa9aSNikita Popov		*pbuf = buf.c;
1945fc9aa9aSNikita Popov		return buf.len;
1955fc9aa9aSNikita Popov	} else {
1965fc9aa9aSNikita Popov		*pbuf = estrndup("", 0);
1975fc9aa9aSNikita Popov		return 0;
1985fc9aa9aSNikita Popov	}
1995fc9aa9aSNikita Popov}
2005fc9aa9aSNikita Popov/* }}} */
2015fc9aa9aSNikita Popov
202c7742e28SNikita PopovZEND_API size_t zend_spprintf(char **message, size_t max_len, const char *format, ...) /* {{{ */
203c7742e28SNikita Popov{
204c7742e28SNikita Popov	va_list arg;
205c7742e28SNikita Popov	size_t len;
206c7742e28SNikita Popov
207c7742e28SNikita Popov	va_start(arg, format);
208c7742e28SNikita Popov	len = zend_vspprintf(message, max_len, format, arg);
209c7742e28SNikita Popov	va_end(arg);
210c7742e28SNikita Popov	return len;
211c7742e28SNikita Popov}
212c7742e28SNikita Popov/* }}} */
213c7742e28SNikita Popov
21426f8fc83SNikita PopovZEND_API size_t zend_spprintf_unchecked(char **message, size_t max_len, const char *format, ...) /* {{{ */
21526f8fc83SNikita Popov{
21626f8fc83SNikita Popov	va_list arg;
21726f8fc83SNikita Popov	size_t len;
21826f8fc83SNikita Popov
21926f8fc83SNikita Popov	va_start(arg, format);
22026f8fc83SNikita Popov	len = zend_vspprintf(message, max_len, format, arg);
22126f8fc83SNikita Popov	va_end(arg);
22226f8fc83SNikita Popov	return len;
22326f8fc83SNikita Popov}
22426f8fc83SNikita Popov/* }}} */
22526f8fc83SNikita Popov
2265fc9aa9aSNikita PopovZEND_API zend_string *zend_vstrpprintf(size_t max_len, const char *format, va_list ap) /* {{{ */
2275fc9aa9aSNikita Popov{
2285fc9aa9aSNikita Popov	smart_str buf = {0};
2295fc9aa9aSNikita Popov
2305fc9aa9aSNikita Popov	zend_printf_to_smart_str(&buf, format, ap);
2315fc9aa9aSNikita Popov
2325fc9aa9aSNikita Popov	if (!buf.s) {
2335fc9aa9aSNikita Popov		return ZSTR_EMPTY_ALLOC();
2345fc9aa9aSNikita Popov	}
2355fc9aa9aSNikita Popov
2365fc9aa9aSNikita Popov	if (max_len && ZSTR_LEN(buf.s) > max_len) {
2375fc9aa9aSNikita Popov		ZSTR_LEN(buf.s) = max_len;
2385fc9aa9aSNikita Popov	}
2395fc9aa9aSNikita Popov
2405fc9aa9aSNikita Popov	smart_str_0(&buf);
2415fc9aa9aSNikita Popov	return buf.s;
2425fc9aa9aSNikita Popov}
2435fc9aa9aSNikita Popov/* }}} */
2445fc9aa9aSNikita Popov
245c7742e28SNikita PopovZEND_API zend_string *zend_strpprintf(size_t max_len, const char *format, ...) /* {{{ */
246c7742e28SNikita Popov{
247c7742e28SNikita Popov	va_list arg;
248c7742e28SNikita Popov	zend_string *str;
249c7742e28SNikita Popov
250c7742e28SNikita Popov	va_start(arg, format);
251c7742e28SNikita Popov	str = zend_vstrpprintf(max_len, format, arg);
252c7742e28SNikita Popov	va_end(arg);
253c7742e28SNikita Popov	return str;
254c7742e28SNikita Popov}
255c7742e28SNikita Popov/* }}} */
256c7742e28SNikita Popov
25726f8fc83SNikita PopovZEND_API zend_string *zend_strpprintf_unchecked(size_t max_len, const char *format, ...) /* {{{ */
25826f8fc83SNikita Popov{
25926f8fc83SNikita Popov	va_list arg;
26026f8fc83SNikita Popov	zend_string *str;
26126f8fc83SNikita Popov
26226f8fc83SNikita Popov	va_start(arg, format);
26326f8fc83SNikita Popov	str = zend_vstrpprintf(max_len, format, arg);
26426f8fc83SNikita Popov	va_end(arg);
26526f8fc83SNikita Popov	return str;
26626f8fc83SNikita Popov}
26726f8fc83SNikita Popov/* }}} */
26826f8fc83SNikita Popov
2691b29e0caSNikita Popovstatic void zend_print_zval_r_to_buf(smart_str *buf, zval *expr, int indent);
2701b29e0caSNikita Popov
2711b29e0caSNikita Popovstatic void print_hash(smart_str *buf, HashTable *ht, int indent, zend_bool is_object) /* {{{ */
272573b4602SAndi Gutmans{
273f4cfaf36SDmitry Stogov	zval *tmp;
274f4cfaf36SDmitry Stogov	zend_string *string_key;
275c3e3c98eSAnatol Belski	zend_ulong num_key;
276573b4602SAndi Gutmans	int i;
277573b4602SAndi Gutmans
278b4892511SJani Taskinen	for (i = 0; i < indent; i++) {
2791b29e0caSNikita Popov		smart_str_appendc(buf, ' ');
280573b4602SAndi Gutmans	}
2811b29e0caSNikita Popov	smart_str_appends(buf, "(\n");
28280bab9d9SAndrei Zmievski	indent += PRINT_ZVAL_INDENT;
2831b29e0caSNikita Popov	ZEND_HASH_FOREACH_KEY_VAL_IND(ht, num_key, string_key, tmp) {
284b4892511SJani Taskinen		for (i = 0; i < indent; i++) {
2851b29e0caSNikita Popov			smart_str_appendc(buf, ' ');
286573b4602SAndi Gutmans		}
2871b29e0caSNikita Popov		smart_str_appendc(buf, '[');
2887652a977SDmitry Stogov		if (string_key) {
2897652a977SDmitry Stogov			if (is_object) {
2907652a977SDmitry Stogov				const char *prop_name, *class_name;
291079409bbSNikita Popov				size_t prop_len;
292079409bbSNikita Popov				int mangled = zend_unmangle_property_name_ex(string_key, &class_name, &prop_name, &prop_len);
2937652a977SDmitry Stogov
2941b29e0caSNikita Popov				smart_str_appendl(buf, prop_name, prop_len);
2957652a977SDmitry Stogov				if (class_name && mangled == SUCCESS) {
2961b29e0caSNikita Popov					if (class_name[0] == '*') {
2971b29e0caSNikita Popov						smart_str_appends(buf, ":protected");
2987652a977SDmitry Stogov					} else {
2991b29e0caSNikita Popov						smart_str_appends(buf, ":");
3001b29e0caSNikita Popov						smart_str_appends(buf, class_name);
3011b29e0caSNikita Popov						smart_str_appends(buf, ":private");
302d329ce93SZeev Suraski					}
303d3d02fbcSDmitry Stogov				}
3047652a977SDmitry Stogov			} else {
3051b29e0caSNikita Popov				smart_str_append(buf, string_key);
3067652a977SDmitry Stogov			}
3077652a977SDmitry Stogov		} else {
3081b29e0caSNikita Popov			smart_str_append_long(buf, num_key);
309573b4602SAndi Gutmans		}
3101b29e0caSNikita Popov		smart_str_appends(buf, "] => ");
3111b29e0caSNikita Popov		zend_print_zval_r_to_buf(buf, tmp, indent+PRINT_ZVAL_INDENT);
3121b29e0caSNikita Popov		smart_str_appends(buf, "\n");
3137652a977SDmitry Stogov	} ZEND_HASH_FOREACH_END();
31480bab9d9SAndrei Zmievski	indent -= PRINT_ZVAL_INDENT;
315b4892511SJani Taskinen	for (i = 0; i < indent; i++) {
3161b29e0caSNikita Popov		smart_str_appendc(buf, ' ');
317573b4602SAndi Gutmans	}
3181b29e0caSNikita Popov	smart_str_appends(buf, ")\n");
319573b4602SAndi Gutmans}
320b4892511SJani Taskinen/* }}} */
321573b4602SAndi Gutmans
322bdeb220fSAnatol Belskistatic void print_flat_hash(HashTable *ht) /* {{{ */
32327de54b0SAndi Gutmans{
324f4cfaf36SDmitry Stogov	zval *tmp;
325f4cfaf36SDmitry Stogov	zend_string *string_key;
326c3e3c98eSAnatol Belski	zend_ulong num_key;
327b4892511SJani Taskinen	int i = 0;
328b4892511SJani Taskinen
3290ade00c8SNikita Popov	ZEND_HASH_FOREACH_KEY_VAL_IND(ht, num_key, string_key, tmp) {
330b4892511SJani Taskinen		if (i++ > 0) {
331b4892511SJani Taskinen			ZEND_PUTS(",");
332b4892511SJani Taskinen		}
333b4892511SJani Taskinen		ZEND_PUTS("[");
3347652a977SDmitry Stogov		if (string_key) {
3357aa76271SDmitry Stogov			ZEND_WRITE(ZSTR_VAL(string_key), ZSTR_LEN(string_key));
3367652a977SDmitry Stogov		} else {
3374d997f63SAnatol Belski			zend_printf(ZEND_ULONG_FMT, num_key);
338b4892511SJani Taskinen		}
339b4892511SJani Taskinen		ZEND_PUTS("] => ");
340bdeb220fSAnatol Belski		zend_print_flat_zval_r(tmp);
3417652a977SDmitry Stogov	} ZEND_HASH_FOREACH_END();
34227de54b0SAndi Gutmans}
343b4892511SJani Taskinen/* }}} */
3447a87fcbbSZeev Suraski
345bdeb220fSAnatol BelskiZEND_API int zend_make_printable_zval(zval *expr, zval *expr_copy) /* {{{ */
346573b4602SAndi Gutmans{
347f3a6c30cSXinchen Hui	if (Z_TYPE_P(expr) == IS_STRING) {
3489d6e45a0SDmitry Stogov		return 0;
34922fbfdfbSXinchen Hui	} else {
350ce18738aSDmitry Stogov		ZVAL_STR(expr_copy, zval_get_string_func(expr));
35122fbfdfbSXinchen Hui		return 1;
352da9faa2cSZeev Suraski	}
353da9faa2cSZeev Suraski}
354b4892511SJani Taskinen/* }}} */
355da9faa2cSZeev Suraski
356bdeb220fSAnatol BelskiZEND_API size_t zend_print_zval(zval *expr, int indent) /* {{{ */
357da9faa2cSZeev Suraski{
358ccc12b82SDmitry Stogov	zend_string *tmp_str;
359ccc12b82SDmitry Stogov	zend_string *str = zval_get_tmp_string(expr, &tmp_str);
3607aa76271SDmitry Stogov	size_t len = ZSTR_LEN(str);
361da9faa2cSZeev Suraski
362bda96e3cSNikita Popov	if (len != 0) {
3631b29e0caSNikita Popov		zend_write(ZSTR_VAL(str), len);
364573b4602SAndi Gutmans	}
365bda96e3cSNikita Popov
366ccc12b82SDmitry Stogov	zend_tmp_string_release(tmp_str);
367bda96e3cSNikita Popov	return len;
368573b4602SAndi Gutmans}
369b4892511SJani Taskinen/* }}} */
370573b4602SAndi Gutmans
371bdeb220fSAnatol BelskiZEND_API void zend_print_flat_zval_r(zval *expr) /* {{{ */
37227de54b0SAndi Gutmans{
373b4892511SJani Taskinen	switch (Z_TYPE_P(expr)) {
374b4892511SJani Taskinen		case IS_ARRAY:
375b4892511SJani Taskinen			ZEND_PUTS("Array (");
37654a58a73SXinchen Hui			if (!(GC_FLAGS(Z_ARRVAL_P(expr)) & GC_IMMUTABLE)) {
37754a58a73SXinchen Hui				if (GC_IS_RECURSIVE(Z_ARRVAL_P(expr))) {
378cb9d81efSDmitry Stogov					ZEND_PUTS(" *RECURSION*");
379cb9d81efSDmitry Stogov					return;
380cb9d81efSDmitry Stogov				}
38154a58a73SXinchen Hui				GC_PROTECT_RECURSION(Z_ARRVAL_P(expr));
382b4892511SJani Taskinen			}
383bdeb220fSAnatol Belski			print_flat_hash(Z_ARRVAL_P(expr));
384b4892511SJani Taskinen			ZEND_PUTS(")");
38554a58a73SXinchen Hui			if (!(GC_FLAGS(Z_ARRVAL_P(expr)) & GC_IMMUTABLE)) {
38654a58a73SXinchen Hui				GC_UNPROTECT_RECURSION(Z_ARRVAL_P(expr));
387b3b616cfSDmitry Stogov			}
388b4892511SJani Taskinen			break;
389b4892511SJani Taskinen		case IS_OBJECT:
390b4892511SJani Taskinen		{
3919bba373aSStanislav Malyshev			HashTable *properties = NULL;
392bdeb220fSAnatol Belski			zend_string *class_name = Z_OBJ_HANDLER_P(expr, get_class_name)(Z_OBJ_P(expr));
3937aa76271SDmitry Stogov			zend_printf("%s Object (", ZSTR_VAL(class_name));
3945eb1f92fSDmitry Stogov			zend_string_release_ex(class_name, 0);
3958ce1211aSMarcus Boerger
39654a58a73SXinchen Hui			if (GC_IS_RECURSIVE(Z_OBJ_P(expr))) {
397dc764bf6SNikita Popov				ZEND_PUTS(" *RECURSION*");
398dc764bf6SNikita Popov				return;
399dc764bf6SNikita Popov			}
400dc764bf6SNikita Popov
401f30f3590SStanislav Malyshev			if (Z_OBJ_HANDLER_P(expr, get_properties)) {
4029bba373aSStanislav Malyshev				properties = Z_OBJPROP_P(expr);
4039bba373aSStanislav Malyshev			}
404f30f3590SStanislav Malyshev			if (properties) {
40554a58a73SXinchen Hui				GC_PROTECT_RECURSION(Z_OBJ_P(expr));
406bdeb220fSAnatol Belski				print_flat_hash(properties);
40754a58a73SXinchen Hui				GC_UNPROTECT_RECURSION(Z_OBJ_P(expr));
408