1/*
2   +----------------------------------------------------------------------+
3   | Zend Engine                                                          |
4   +----------------------------------------------------------------------+
5   | Copyright (c) 1998-2016 Zend Technologies Ltd. (http://www.zend.com) |
6   +----------------------------------------------------------------------+
7   | This source file is subject to version 2.00 of the Zend license,     |
8   | that is bundled with this package in the file LICENSE, and is        |
9   | available through the world-wide-web at the following url:           |
10   | http://www.zend.com/license/2_00.txt.                                |
11   | If you did not receive a copy of the Zend license and are unable to  |
12   | obtain it through the world-wide-web, please send a note to          |
13   | license@zend.com so we can mail you a copy immediately.              |
14   +----------------------------------------------------------------------+
15   | Authors: Andi Gutmans <andi@zend.com>                                |
16   |          Zeev Suraski <zeev@zend.com>                                |
17   |          Andrei Zmievski <andrei@php.net>                            |
18   |          Dmitry Stogov <dmitry@zend.com>                             |
19   +----------------------------------------------------------------------+
20*/
21
22/* $Id$ */
23
24#include "zend.h"
25#include "zend_execute.h"
26#include "zend_API.h"
27#include "zend_modules.h"
28#include "zend_extensions.h"
29#include "zend_constants.h"
30#include "zend_exceptions.h"
31#include "zend_closures.h"
32#include "zend_inheritance.h"
33
34#ifdef HAVE_STDARG_H
35#include <stdarg.h>
36#endif
37
38/* these variables are true statics/globals, and have to be mutex'ed on every access */
39ZEND_API HashTable module_registry;
40
41static zend_module_entry **module_request_startup_handlers;
42static zend_module_entry **module_request_shutdown_handlers;
43static zend_module_entry **module_post_deactivate_handlers;
44
45static zend_class_entry  **class_cleanup_handlers;
46
47/* this function doesn't check for too many parameters */
48ZEND_API int zend_get_parameters(int ht, int param_count, ...) /* {{{ */
49{
50	int arg_count;
51	va_list ptr;
52	zval **param, *param_ptr;
53
54	param_ptr = ZEND_CALL_ARG(EG(current_execute_data), 1);
55	arg_count = ZEND_CALL_NUM_ARGS(EG(current_execute_data));
56
57	if (param_count>arg_count) {
58		return FAILURE;
59	}
60
61	va_start(ptr, param_count);
62
63	while (param_count-->0) {
64		param = va_arg(ptr, zval **);
65		if (!Z_ISREF_P(param_ptr) && Z_REFCOUNT_P(param_ptr) > 1) {
66			zval new_tmp;
67
68			ZVAL_DUP(&new_tmp, param_ptr);
69			Z_DELREF_P(param_ptr);
70			ZVAL_COPY_VALUE(param_ptr, &new_tmp);
71		}
72		*param = param_ptr;
73		param_ptr++;
74	}
75	va_end(ptr);
76
77	return SUCCESS;
78}
79/* }}} */
80
81/* Zend-optimized Extended functions */
82/* this function doesn't check for too many parameters */
83ZEND_API int zend_get_parameters_ex(int param_count, ...) /* {{{ */
84{
85	int arg_count;
86	va_list ptr;
87	zval **param, *param_ptr;
88
89	param_ptr = ZEND_CALL_ARG(EG(current_execute_data), 1);
90	arg_count = ZEND_CALL_NUM_ARGS(EG(current_execute_data));
91
92	if (param_count>arg_count) {
93		return FAILURE;
94	}
95
96	va_start(ptr, param_count);
97	while (param_count-->0) {
98		param = va_arg(ptr, zval **);
99		*param = param_ptr;
100		param_ptr++;
101	}
102	va_end(ptr);
103
104	return SUCCESS;
105}
106/* }}} */
107
108ZEND_API int _zend_get_parameters_array_ex(int param_count, zval *argument_array) /* {{{ */
109{
110	zval *param_ptr;
111	int arg_count;
112
113	param_ptr = ZEND_CALL_ARG(EG(current_execute_data), 1);
114	arg_count = ZEND_CALL_NUM_ARGS(EG(current_execute_data));
115
116	if (param_count>arg_count) {
117		return FAILURE;
118	}
119
120	while (param_count-->0) {
121		ZVAL_COPY_VALUE(argument_array, param_ptr);
122		argument_array++;
123		param_ptr++;
124	}
125
126	return SUCCESS;
127}
128/* }}} */
129
130ZEND_API int zend_copy_parameters_array(int param_count, zval *argument_array) /* {{{ */
131{
132	zval *param_ptr;
133	int arg_count;
134
135	param_ptr = ZEND_CALL_ARG(EG(current_execute_data), 1);
136	arg_count = ZEND_CALL_NUM_ARGS(EG(current_execute_data));
137
138	if (param_count>arg_count) {
139		return FAILURE;
140	}
141
142	while (param_count-->0) {
143		if (Z_REFCOUNTED_P(param_ptr)) {
144			Z_ADDREF_P(param_ptr);
145		}
146		zend_hash_next_index_insert_new(Z_ARRVAL_P(argument_array), param_ptr);
147		param_ptr++;
148	}
149
150	return SUCCESS;
151}
152/* }}} */
153
154ZEND_API ZEND_COLD void zend_wrong_param_count(void) /* {{{ */
155{
156	const char *space;
157	const char *class_name = get_active_class_name(&space);
158
159	zend_internal_type_error(ZEND_ARG_USES_STRICT_TYPES(), "Wrong parameter count for %s%s%s()", class_name, space, get_active_function_name());
160}
161/* }}} */
162
163/* Argument parsing API -- andrei */
164ZEND_API char *zend_get_type_by_const(int type) /* {{{ */
165{
166	switch(type) {
167		case IS_FALSE:
168		case IS_TRUE:
169		case _IS_BOOL:
170			return "boolean";
171		case IS_LONG:
172			return "integer";
173		case IS_DOUBLE:
174			return "float";
175		case IS_STRING:
176			return "string";
177		case IS_OBJECT:
178			return "object";
179		case IS_RESOURCE:
180			return "resource";
181		case IS_NULL:
182			return "null";
183		case IS_CALLABLE:
184			return "callable";
185		case IS_ARRAY:
186			return "array";
187		case IS_VOID:
188			return "void";
189		default:
190			return "unknown";
191	}
192}
193/* }}} */
194
195ZEND_API char *zend_zval_type_name(const zval *arg) /* {{{ */
196{
197	ZVAL_DEREF(arg);
198	return zend_get_type_by_const(Z_TYPE_P(arg));
199}
200/* }}} */
201
202#ifdef FAST_ZPP
203ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_parameters_count_error(int num_args, int min_num_args, int max_num_args) /* {{{ */
204{
205	zend_function *active_function = EG(current_execute_data)->func;
206	const char *class_name = active_function->common.scope ? ZSTR_VAL(active_function->common.scope->name) : "";
207
208	zend_internal_type_error(ZEND_ARG_USES_STRICT_TYPES(), "%s%s%s() expects %s %d parameter%s, %d given",
209		class_name, \
210		class_name[0] ? "::" : "", \
211		ZSTR_VAL(active_function->common.function_name),
212		min_num_args == max_num_args ? "exactly" : num_args < min_num_args ? "at least" : "at most",
213		num_args < min_num_args ? min_num_args : max_num_args,
214		(num_args < min_num_args ? min_num_args : max_num_args) == 1 ? "" : "s",
215		num_args);
216}
217/* }}} */
218
219ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_parameter_type_error(int num, zend_expected_type expected_type, zval *arg) /* {{{ */
220{
221	const char *space;
222	const char *class_name = get_active_class_name(&space);
223	static const char * const expected_error[] = {
224		Z_EXPECTED_TYPES(Z_EXPECTED_TYPE_STR)
225		NULL
226	};
227
228	zend_internal_type_error(ZEND_ARG_USES_STRICT_TYPES(), "%s%s%s() expects parameter %d to be %s, %s given",
229		class_name, space, get_active_function_name(), num, expected_error[expected_type], zend_zval_type_name(arg));
230}
231/* }}} */
232
233ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_parameter_class_error(int num, char *name, zval *arg) /* {{{ */
234{
235	const char *space;
236	const char *class_name = get_active_class_name(&space);
237
238	zend_internal_type_error(ZEND_ARG_USES_STRICT_TYPES(), "%s%s%s() expects parameter %d to be %s, %s given",
239		class_name, space, get_active_function_name(), num, name, zend_zval_type_name(arg));
240}
241/* }}} */
242
243ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_callback_error(int severity, int num, char *error) /* {{{ */
244{
245	const char *space;
246	const char *class_name = get_active_class_name(&space);
247
248	if (severity == E_WARNING) {
249		zend_internal_type_error(ZEND_ARG_USES_STRICT_TYPES(), "%s%s%s() expects parameter %d to be a valid callback, %s",
250			class_name, space, get_active_function_name(), num, error);
251	} else if (severity == E_ERROR) {
252		zend_throw_error(zend_ce_type_error, "%s%s%s() expects parameter %d to be a valid callback, %s",
253			class_name, space, get_active_function_name(), num, error);
254	} else {
255		zend_error(severity, "%s%s%s() expects parameter %d to be a valid callback, %s",
256			class_name, space, get_active_function_name(), num, error);
257	}
258	efree(error);
259}
260/* }}} */
261
262ZEND_API int ZEND_FASTCALL zend_parse_arg_class(zval *arg, zend_class_entry **pce, int num, int check_null) /* {{{ */
263{
264	zend_class_entry *ce_base = *pce;
265
266	if (check_null && Z_TYPE_P(arg) == IS_NULL) {
267		*pce = NULL;
268		return 1;
269	}
270	convert_to_string_ex(arg);
271	*pce = zend_lookup_class(Z_STR_P(arg));
272	if (ce_base) {
273		if ((!*pce || !instanceof_function(*pce, ce_base))) {
274			const char *space;
275			const char *class_name = get_active_class_name(&space);
276
277			zend_internal_type_error(ZEND_ARG_USES_STRICT_TYPES(), "%s%s%s() expects parameter %d to be a class name derived from %s, '%s' given",
278				class_name, space, get_active_function_name(), num,
279				ZSTR_VAL(ce_base->name), Z_STRVAL_P(arg));
280			*pce = NULL;
281			return 0;
282		}
283	}
284	if (!*pce) {
285		const char *space;
286		const char *class_name = get_active_class_name(&space);
287
288		zend_internal_type_error(ZEND_ARG_USES_STRICT_TYPES(), "%s%s%s() expects parameter %d to be a valid class name, '%s' given",
289			class_name, space, get_active_function_name(), num,
290			Z_STRVAL_P(arg));
291		return 0;
292	}
293	return 1;
294}
295/* }}} */
296#endif
297
298ZEND_API int ZEND_FASTCALL zend_parse_arg_bool_weak(zval *arg, zend_bool *dest) /* {{{ */
299{
300	if (EXPECTED(Z_TYPE_P(arg) <= IS_STRING)) {
301		*dest = zend_is_true(arg);
302	} else {
303		return 0;
304	}
305	return 1;
306}
307/* }}} */
308
309ZEND_API int ZEND_FASTCALL zend_parse_arg_bool_slow(zval *arg, zend_bool *dest) /* {{{ */
310{
311	if (UNEXPECTED(ZEND_ARG_USES_STRICT_TYPES())) {
312		return 0;
313	}
314	return zend_parse_arg_bool_weak(arg, dest);
315}
316/* }}} */
317
318ZEND_API int ZEND_FASTCALL zend_parse_arg_long_weak(zval *arg, zend_long *dest) /* {{{ */
319{
320	if (EXPECTED(Z_TYPE_P(arg) == IS_DOUBLE)) {
321		if (UNEXPECTED(zend_isnan(Z_DVAL_P(arg)))) {
322			return 0;
323		}
324		if (UNEXPECTED(!ZEND_DOUBLE_FITS_LONG(Z_DVAL_P(arg)))) {
325			return 0;
326		} else {
327			*dest = zend_dval_to_lval(Z_DVAL_P(arg));
328		}
329	} else if (EXPECTED(Z_TYPE_P(arg) == IS_STRING)) {
330		double d;
331		int type;
332
333		if (UNEXPECTED((type = is_numeric_str_function(Z_STR_P(arg), dest, &d)) != IS_LONG)) {
334			if (EXPECTED(type != 0)) {
335				if (UNEXPECTED(zend_isnan(d))) {
336					return 0;
337				}
338				if (UNEXPECTED(!ZEND_DOUBLE_FITS_LONG(d))) {
339					return 0;
340				} else {
341					*dest = zend_dval_to_lval(d);
342				}
343			} else {
344				return 0;
345			}
346		}
347	} else if (EXPECTED(Z_TYPE_P(arg) < IS_TRUE)) {
348		*dest = 0;
349	} else if (EXPECTED(Z_TYPE_P(arg) == IS_TRUE)) {
350		*dest = 1;
351	} else {
352		return 0;
353	}
354	return 1;
355}
356/* }}} */
357
358ZEND_API int ZEND_FASTCALL zend_parse_arg_long_slow(zval *arg, zend_long *dest) /* {{{ */
359{
360	if (UNEXPECTED(ZEND_ARG_USES_STRICT_TYPES())) {
361		return 0;
362	}
363	return zend_parse_arg_long_weak(arg, dest);
364}
365/* }}} */
366
367ZEND_API int ZEND_FASTCALL zend_parse_arg_long_cap_weak(zval *arg, zend_long *dest) /* {{{ */
368{
369	if (EXPECTED(Z_TYPE_P(arg) == IS_DOUBLE)) {
370		if (UNEXPECTED(zend_isnan(Z_DVAL_P(arg)))) {
371			return 0;
372		}
373		if (UNEXPECTED(!ZEND_DOUBLE_FITS_LONG(Z_DVAL_P(arg)))) {
374			*dest = (Z_DVAL_P(arg) > 0) ? ZEND_LONG_MAX : ZEND_LONG_MIN;
375		} else {
376			*dest = zend_dval_to_lval(Z_DVAL_P(arg));
377		}
378	} else if (EXPECTED(Z_TYPE_P(arg) == IS_STRING)) {
379		double d;
380		int type;
381
382		if (UNEXPECTED((type = is_numeric_str_function(Z_STR_P(arg), dest, &d)) != IS_LONG)) {
383			if (EXPECTED(type != 0)) {
384				if (UNEXPECTED(zend_isnan(d))) {
385					return 0;
386				}
387				if (UNEXPECTED(!ZEND_DOUBLE_FITS_LONG(d))) {
388					*dest = (d > 0) ? ZEND_LONG_MAX : ZEND_LONG_MIN;
389				} else {
390					*dest = zend_dval_to_lval(d);
391				}
392			} else {
393				return 0;
394			}
395		}
396	} else if (EXPECTED(Z_TYPE_P(arg) < IS_TRUE)) {
397		*dest = 0;
398	} else if (EXPECTED(Z_TYPE_P(arg) == IS_TRUE)) {
399		*dest = 1;
400	} else {
401		return 0;
402	}
403	return 1;
404}
405/* }}} */
406
407ZEND_API int ZEND_FASTCALL zend_parse_arg_long_cap_slow(zval *arg, zend_long *dest) /* {{{ */
408{
409	if (UNEXPECTED(ZEND_ARG_USES_STRICT_TYPES())) {
410		return 0;
411	}
412	return zend_parse_arg_long_cap_weak(arg, dest);
413}
414/* }}} */
415
416ZEND_API int ZEND_FASTCALL zend_parse_arg_double_weak(zval *arg, double *dest) /* {{{ */
417{
418	if (EXPECTED(Z_TYPE_P(arg) == IS_LONG)) {
419		*dest = (double)Z_LVAL_P(arg);
420	} else if (EXPECTED(Z_TYPE_P(arg) == IS_STRING)) {
421		zend_long l;
422		int type;
423
424		if (UNEXPECTED((type = is_numeric_str_function(Z_STR_P(arg), &l, dest)) != IS_DOUBLE)) {
425			if (EXPECTED(type != 0)) {
426				*dest = (double)(l);
427			} else {
428				return 0;
429			}
430		}
431	} else if (EXPECTED(Z_TYPE_P(arg) < IS_TRUE)) {
432		*dest = 0.0;
433	} else if (EXPECTED(Z_TYPE_P(arg) == IS_TRUE)) {
434		*dest = 1.0;
435	} else {
436		return 0;
437	}
438	return 1;
439}
440/* }}} */
441
442ZEND_API int ZEND_FASTCALL zend_parse_arg_double_slow(zval *arg, double *dest) /* {{{ */
443{
444	if (EXPECTED(Z_TYPE_P(arg) == IS_LONG)) {
445		/* SSTH Exception: IS_LONG may be accepted instead as IS_DOUBLE */
446		*dest = (double)Z_LVAL_P(arg);
447	} else if (UNEXPECTED(ZEND_ARG_USES_STRICT_TYPES())) {
448		return 0;
449	}
450	return zend_parse_arg_double_weak(arg, dest);
451}
452/* }}} */
453
454ZEND_API int ZEND_FASTCALL zend_parse_arg_str_weak(zval *arg, zend_string **dest) /* {{{ */
455{
456	if (EXPECTED(Z_TYPE_P(arg) < IS_STRING)) {
457		convert_to_string(arg);
458		*dest = Z_STR_P(arg);
459	} else if (UNEXPECTED(Z_TYPE_P(arg) == IS_OBJECT)) {
460		if (Z_OBJ_HANDLER_P(arg, cast_object)) {
461			zval obj;
462			if (Z_OBJ_HANDLER_P(arg, cast_object)(arg, &obj, IS_STRING) == SUCCESS) {
463				zval_ptr_dtor(arg);
464				ZVAL_COPY_VALUE(arg, &obj);
465				*dest = Z_STR_P(arg);
466				return 1;
467			}
468		} else if (Z_OBJ_HANDLER_P(arg, get)) {
469			zval rv;
470			zval *z = Z_OBJ_HANDLER_P(arg, get)(arg, &rv);
471
472			Z_ADDREF_P(z);
473			if (Z_TYPE_P(z) != IS_OBJECT) {
474				zval_dtor(arg);
475				ZVAL_NULL(arg);
476				if (!zend_make_printable_zval(z, arg)) {
477					ZVAL_COPY_VALUE(arg, z);
478				}
479				*dest = Z_STR_P(arg);
480				return 1;
481			}
482			zval_ptr_dtor(z);
483		}
484		return 0;
485	} else {
486		return 0;
487	}
488	return 1;
489}
490/* }}} */
491
492ZEND_API int ZEND_FASTCALL zend_parse_arg_str_slow(zval *arg, zend_string **dest) /* {{{ */
493{
494	if (UNEXPECTED(ZEND_ARG_USES_STRICT_TYPES())) {
495		return 0;
496	}
497	return zend_parse_arg_str_weak(arg, dest);
498}
499/* }}} */
500
501static const char *zend_parse_arg_impl(int arg_num, zval *arg, va_list *va, const char **spec, char **error, int *severity) /* {{{ */
502{
503	const char *spec_walk = *spec;
504	char c = *spec_walk++;
505	int check_null = 0;
506	zval *real_arg = arg;
507
508	/* scan through modifiers */
509	ZVAL_DEREF(arg);
510	while (1) {
511		if (*spec_walk == '/') {
512			SEPARATE_ZVAL_NOREF(arg);
513			real_arg = arg;
514		} else if (*spec_walk == '!') {
515			check_null = 1;
516		} else {
517			break;
518		}
519		spec_walk++;
520	}
521
522	switch (c) {
523		case 'l':
524		case 'L':
525			{
526				zend_long *p = va_arg(*va, zend_long *);
527				zend_bool *is_null = NULL;
528
529				if (check_null) {
530					is_null = va_arg(*va, zend_bool *);
531				}
532
533				if (!zend_parse_arg_long(arg, p, is_null, check_null, c == 'L')) {
534					return "integer";
535				}
536			}
537			break;
538
539		case 'd':
540			{
541				double *p = va_arg(*va, double *);
542				zend_bool *is_null = NULL;
543
544				if (check_null) {
545					is_null = va_arg(*va, zend_bool *);
546				}
547
548				if (!zend_parse_arg_double(arg, p, is_null, check_null)) {
549					return "float";
550				}
551			}
552			break;
553
554		case 's':
555			{
556				char **p = va_arg(*va, char **);
557				size_t *pl = va_arg(*va, size_t *);
558				if (!zend_parse_arg_string(arg, p, pl, check_null)) {
559					return "string";
560				}
561			}
562			break;
563
564		case 'p':
565			{
566				char **p = va_arg(*va, char **);
567				size_t *pl = va_arg(*va, size_t *);
568				if (!zend_parse_arg_path(arg, p, pl, check_null)) {
569					return "a valid path";
570				}
571			}
572			break;
573
574		case 'P':
575			{
576				zend_string **str = va_arg(*va, zend_string **);
577				if (!zend_parse_arg_path_str(arg, str, check_null)) {
578					return "a valid path";
579				}
580			}
581			break;
582
583		case 'S':
584			{
585				zend_string **str = va_arg(*va, zend_string **);
586				if (!zend_parse_arg_str(arg, str, check_null)) {
587					return "string";
588				}
589			}
590			break;
591
592		case 'b':
593			{
594				zend_bool *p = va_arg(*va, zend_bool *);
595				zend_bool *is_null = NULL;
596
597				if (check_null) {
598					is_null = va_arg(*va, zend_bool *);
599				}
600
601				if (!zend_parse_arg_bool(arg, p, is_null, check_null)) {
602					return "boolean";
603				}
604			}
605			break;
606
607		case 'r':
608			{
609				zval **p = va_arg(*va, zval **);
610
611				if (!zend_parse_arg_resource(arg, p, check_null)) {
612					return "resource";
613				}
614			}
615			break;
616
617		case 'A':
618		case 'a':
619			{
620				zval **p = va_arg(*va, zval **);
621
622				if (!zend_parse_arg_array(arg, p, check_null, c == 'A')) {
623					return "array";
624				}
625			}
626			break;
627
628		case 'H':
629		case 'h':
630			{
631				HashTable **p = va_arg(*va, HashTable **);
632
633				if (!zend_parse_arg_array_ht(arg, p, check_null, c == 'H')) {
634					return "array";
635				}
636			}
637			break;
638
639		case 'o':
640			{
641				zval **p = va_arg(*va, zval **);
642
643				if (!zend_parse_arg_object(arg, p, NULL, check_null)) {
644					return "object";
645				}
646			}
647			break;
648
649		case 'O':
650			{
651				zval **p = va_arg(*va, zval **);
652				zend_class_entry *ce = va_arg(*va, zend_class_entry *);
653
654				if (!zend_parse_arg_object(arg, p, ce, check_null)) {
655					if (ce) {
656						return ZSTR_VAL(ce->name);
657					} else {
658						return "object";
659					}
660				}
661			}
662			break;
663
664		case 'C':
665			{
666				zend_class_entry *lookup, **pce = va_arg(*va, zend_class_entry **);
667				zend_class_entry *ce_base = *pce;
668
669				if (check_null && Z_TYPE_P(arg) == IS_NULL) {
670					*pce = NULL;
671					break;
672				}
673				convert_to_string_ex(arg);
674				if ((lookup = zend_lookup_class(Z_STR_P(arg))) == NULL) {
675					*pce = NULL;
676				} else {
677					*pce = lookup;
678				}
679				if (ce_base) {
680					if ((!*pce || !instanceof_function(*pce, ce_base))) {
681						zend_spprintf(error, 0, "to be a class name derived from %s, '%s' given",
682							ZSTR_VAL(ce_base->name), Z_STRVAL_P(arg));
683						*pce = NULL;
684						return "";
685					}
686				}
687				if (!*pce) {
688					zend_spprintf(error, 0, "to be a valid class name, '%s' given",
689						Z_STRVAL_P(arg));
690					return "";
691				}
692				break;
693
694			}
695			break;
696
697		case 'f':
698			{
699				zend_fcall_info *fci = va_arg(*va, zend_fcall_info *);
700				zend_fcall_info_cache *fcc = va_arg(*va, zend_fcall_info_cache *);
701				char *is_callable_error = NULL;
702
703				if (check_null && Z_TYPE_P(arg) == IS_NULL) {
704					fci->size = 0;
705					fcc->initialized = 0;
706					break;
707				}
708
709				if (zend_fcall_info_init(arg, 0, fci, fcc, NULL, &is_callable_error) == SUCCESS) {
710					if (is_callable_error) {
711						*severity = E_DEPRECATED;
712						zend_spprintf(error, 0, "to be a valid callback, %s", is_callable_error);
713						efree(is_callable_error);
714						*spec = spec_walk;
715						return "";
716					}
717					break;
718				} else {
719					if (is_callable_error) {
720						*severity = E_ERROR;
721						zend_spprintf(error, 0, "to be a valid callback, %s", is_callable_error);
722						efree(is_callable_error);
723						return "";
724					} else {
725						return "valid callback";
726					}
727				}
728			}
729
730		case 'z':
731			{
732				zval **p = va_arg(*va, zval **);
733
734				zend_parse_arg_zval_deref(real_arg, p, check_null);
735			}
736			break;
737
738		case 'Z':
739			/* 'Z' iz not supported anymore and should be replaced with 'z' */
740			ZEND_ASSERT(c != 'Z');
741		default:
742			return "unknown";
743	}
744
745	*spec = spec_walk;
746
747	return NULL;
748}
749/* }}} */
750
751static int zend_parse_arg(int arg_num, zval *arg, va_list *va, const char **spec, int flags) /* {{{ */
752{
753	const char *expected_type = NULL;
754	char *error = NULL;
755	int severity = 0;
756
757	expected_type = zend_parse_arg_impl(arg_num, arg, va, spec, &error, &severity);
758	if (expected_type) {
759		if (!(flags & ZEND_PARSE_PARAMS_QUIET) && (*expected_type || error)) {
760			const char *space;
761			const char *class_name = get_active_class_name(&space);
762			zend_bool throw_exception =
763				ZEND_ARG_USES_STRICT_TYPES() || (flags & ZEND_PARSE_PARAMS_THROW);
764
765			if (error) {
766				zend_internal_type_error(throw_exception, "%s%s%s() expects parameter %d %s",
767						class_name, space, get_active_function_name(), arg_num, error);
768				efree(error);
769			} else {
770				zend_internal_type_error(throw_exception,
771						"%s%s%s() expects parameter %d to be %s, %s given",
772						class_name, space, get_active_function_name(), arg_num, expected_type,
773						zend_zval_type_name(arg));
774			}
775		}
776		if (severity != E_DEPRECATED) {
777			return FAILURE;
778		}
779	}
780
781	return SUCCESS;
782}
783/* }}} */
784
785ZEND_API int zend_parse_parameter(int flags, int arg_num, zval *arg, const char *spec, ...)
786{
787	va_list va;
788	int ret;
789
790	va_start(va, spec);
791	ret = zend_parse_arg(arg_num, arg, &va, &spec, flags);
792	va_end(va);
793
794	return ret;
795}
796
797static ZEND_COLD void zend_parse_parameters_debug_error(const char *msg) {
798	zend_function *active_function = EG(current_execute_data)->func;
799	const char *class_name = active_function->common.scope
800		? ZSTR_VAL(active_function->common.scope->name) : "";
801	zend_error_noreturn(E_CORE_ERROR, "%s%s%s(): %s",
802		class_name, class_name[0] ? "::" : "",
803		ZSTR_VAL(active_function->common.function_name), msg);
804}
805
806static int zend_parse_va_args(int num_args, const char *type_spec, va_list *va, int flags) /* {{{ */
807{
808	const  char *spec_walk;
809	int c, i;
810	int min_num_args = -1;
811	int max_num_args = 0;
812	int post_varargs = 0;
813	zval *arg;
814	int arg_count;
815	zend_bool have_varargs = 0;
816	zval **varargs = NULL;
817	int *n_varargs = NULL;
818
819	for (spec_walk = type_spec; *spec_walk; spec_walk++) {
820		c = *spec_walk;
821		switch (c) {
822			case 'l': case 'd':
823			case 's': case 'b':
824			case 'r': case 'a':
825			case 'o': case 'O':
826			case 'z': case 'Z':
827			case 'C': case 'h':
828			case 'f': case 'A':
829			case 'H': case 'p':
830			case 'S': case 'P':
831			case 'L':
832				max_num_args++;
833				break;
834
835			case '|':
836				min_num_args = max_num_args;
837				break;
838
839			case '/':
840			case '!':
841				/* Pass */
842				break;
843
844			case '*':
845			case '+':
846				if (have_varargs) {
847					zend_parse_parameters_debug_error(
848						"only one varargs specifier (* or +) is permitted");
849					return FAILURE;
850				}
851				have_varargs = 1;
852				/* we expect at least one parameter in varargs */
853				if (c == '+') {
854					max_num_args++;
855				}
856				/* mark the beginning of varargs */
857				post_varargs = max_num_args;
858				break;
859
860			default:
861				zend_parse_parameters_debug_error("bad type specifier while parsing parameters");
862				return FAILURE;
863		}
864	}
865
866	if (min_num_args < 0) {
867		min_num_args = max_num_args;
868	}
869
870	if (have_varargs) {
871		/* calculate how many required args are at the end of the specifier list */
872		post_varargs = max_num_args - post_varargs;
873		max_num_args = -1;
874	}
875
876	if (num_args < min_num_args || (num_args > max_num_args && max_num_args >= 0)) {
877		if (!(flags & ZEND_PARSE_PARAMS_QUIET)) {
878			zend_function *active_function = EG(current_execute_data)->func;
879			const char *class_name = active_function->common.scope ? ZSTR_VAL(active_function->common.scope->name) : "";
880			zend_bool throw_exception = ZEND_ARG_USES_STRICT_TYPES() || (flags & ZEND_PARSE_PARAMS_THROW);
881			zend_internal_type_error(throw_exception, "%s%s%s() expects %s %d parameter%s, %d given",
882					class_name,
883					class_name[0] ? "::" : "",
884					ZSTR_VAL(active_function->common.function_name),
885					min_num_args == max_num_args ? "exactly" : num_args < min_num_args ? "at least" : "at most",
886					num_args < min_num_args ? min_num_args : max_num_args,
887					(num_args < min_num_args ? min_num_args : max_num_args) == 1 ? "" : "s",
888					num_args);
889		}
890		return FAILURE;
891	}
892
893	arg_count = ZEND_CALL_NUM_ARGS(EG(current_execute_data));
894
895	if (num_args > arg_count) {
896		zend_parse_parameters_debug_error("could not obtain parameters for parsing");
897		return FAILURE;
898	}
899
900	i = 0;
901	while (num_args-- > 0) {
902		if (*type_spec == '|') {
903			type_spec++;
904		}
905
906		if (*type_spec == '*' || *type_spec == '+') {
907			int num_varargs = num_args + 1 - post_varargs;
908
909			/* eat up the passed in storage even if it won't be filled in with varargs */
910			varargs = va_arg(*va, zval **);
911			n_varargs = va_arg(*va, int *);
912			type_spec++;
913
914			if (num_varargs > 0) {
915				*n_varargs = num_varargs;
916				*varargs = ZEND_CALL_ARG(EG(current_execute_data), i + 1);
917				/* adjust how many args we have left and restart loop */
918				num_args += 1 - num_varargs;
919				i += num_varargs;
920				continue;
921			} else {
922				*varargs = NULL;
923				*n_varargs = 0;
924			}
925		}
926
927		arg = ZEND_CALL_ARG(EG(current_execute_data), i + 1);
928
929		if (zend_parse_arg(i+1, arg, va, &type_spec, flags) == FAILURE) {
930			/* clean up varargs array if it was used */
931			if (varargs && *varargs) {
932				*varargs = NULL;
933			}
934			return FAILURE;
935		}
936		i++;
937	}
938
939	return SUCCESS;
940}
941/* }}} */
942
943ZEND_API int zend_parse_parameters_ex(int flags, int num_args, const char *type_spec, ...) /* {{{ */
944{
945	va_list va;
946	int retval;
947
948	va_start(va, type_spec);
949	retval = zend_parse_va_args(num_args, type_spec, &va, flags);
950	va_end(va);
951
952	return retval;
953}
954/* }}} */
955
956ZEND_API int zend_parse_parameters(int num_args, const char *type_spec, ...) /* {{{ */
957{
958	va_list va;
959	int retval;
960	int flags = 0;
961
962	va_start(va, type_spec);
963	retval = zend_parse_va_args(num_args, type_spec, &va, flags);
964	va_end(va);
965
966	return retval;
967}
968/* }}} */
969
970ZEND_API int zend_parse_parameters_throw(int num_args, const char *type_spec, ...) /* {{{ */
971{
972	va_list va;
973	int retval;
974	int flags = ZEND_PARSE_PARAMS_THROW;
975
976	va_start(va, type_spec);
977	retval = zend_parse_va_args(num_args, type_spec, &va, flags);
978	va_end(va);
979
980	return retval;
981}
982/* }}} */
983
984ZEND_API int zend_parse_method_parameters(int num_args, zval *this_ptr, const char *type_spec, ...) /* {{{ */
985{
986	va_list va;
987	int retval;
988	int flags = 0;
989	const char *p = type_spec;
990	zval **object;
991	zend_class_entry *ce;
992
993	/* Just checking this_ptr is not enough, because fcall_common_helper does not set
994	 * Z_OBJ(EG(This)) to NULL when calling an internal function with common.scope == NULL.
995	 * In that case EG(This) would still be the $this from the calling code and we'd take the
996	 * wrong branch here. */
997	zend_bool is_method = EG(current_execute_data)->func->common.scope != NULL;
998
999	if (!is_method || !this_ptr || Z_TYPE_P(this_ptr) != IS_OBJECT) {
1000		va_start(va, type_spec);
1001		retval = zend_parse_va_args(num_args, type_spec, &va, flags);
1002		va_end(va);
1003	} else {
1004		p++;
1005
1006		va_start(va, type_spec);
1007
1008		object = va_arg(va, zval **);
1009		ce = va_arg(va, zend_class_entry *);
1010		*object = this_ptr;
1011
1012		if (ce && !instanceof_function(Z_OBJCE_P(this_ptr), ce)) {
1013			zend_error_noreturn(E_CORE_ERROR, "%s::%s() must be derived from %s::%s",
1014				ZSTR_VAL(Z_OBJCE_P(this_ptr)->name), get_active_function_name(), ZSTR_VAL(ce->name), get_active_function_name());
1015		}
1016
1017		retval = zend_parse_va_args(num_args, p, &va, flags);
1018		va_end(va);
1019	}
1020	return retval;
1021}
1022/* }}} */
1023
1024ZEND_API int zend_parse_method_parameters_ex(int flags, int num_args, zval *this_ptr, const char *type_spec, ...) /* {{{ */
1025{
1026	va_list va;
1027	int retval;
1028	const char *p = type_spec;
1029	zval **object;
1030	zend_class_entry *ce;
1031
1032	if (!this_ptr) {
1033		va_start(va, type_spec);
1034		retval = zend_parse_va_args(num_args, type_spec, &va, flags);
1035		va_end(va);
1036	} else {
1037		p++;
1038		va_start(va, type_spec);
1039
1040		object = va_arg(va, zval **);
1041		ce = va_arg(va, zend_class_entry *);
1042		*object = this_ptr;
1043
1044		if (ce && !instanceof_function(Z_OBJCE_P(this_ptr), ce)) {
1045			if (!(flags & ZEND_PARSE_PARAMS_QUIET)) {
1046				zend_error_noreturn(E_CORE_ERROR, "%s::%s() must be derived from %s::%s",
1047					ZSTR_VAL(ce->name), get_active_function_name(), ZSTR_VAL(Z_OBJCE_P(this_ptr)->name), get_active_function_name());
1048			}
1049			va_end(va);
1050			return FAILURE;
1051		}
1052
1053		retval = zend_parse_va_args(num_args, p, &va, flags);
1054		va_end(va);
1055	}
1056	return retval;
1057}
1058/* }}} */
1059
1060/* Argument parsing API -- andrei */
1061ZEND_API int _array_init(zval *arg, uint32_t size ZEND_FILE_LINE_DC) /* {{{ */
1062{
1063	ZVAL_NEW_ARR(arg);
1064	_zend_hash_init(Z_ARRVAL_P(arg), size, ZVAL_PTR_DTOR, 0 ZEND_FILE_LINE_RELAY_CC);
1065	return SUCCESS;
1066}
1067/* }}} */
1068
1069/* This function should be called after the constructor has been called
1070 * because it may call __set from the uninitialized object otherwise. */
1071ZEND_API void zend_merge_properties(zval *obj, HashTable *properties) /* {{{ */
1072{
1073	const zend_object_handlers *obj_ht = Z_OBJ_HT_P(obj);
1074	zend_class_entry *old_scope = EG(scope);
1075	zend_string *key;
1076	zval *value;
1077
1078	EG(scope) = Z_OBJCE_P(obj);
1079	ZEND_HASH_FOREACH_STR_KEY_VAL(properties, key, value) {
1080		if (key) {
1081			zval member;
1082
1083			ZVAL_STR(&member, key);
1084			obj_ht->write_property(obj, &member, value, NULL);
1085		}
1086	} ZEND_HASH_FOREACH_END();
1087	EG(scope) = old_scope;
1088}
1089/* }}} */
1090
1091ZEND_API int zend_update_class_constants(zend_class_entry *class_type) /* {{{ */
1092{
1093	if (!(class_type->ce_flags & ZEND_ACC_CONSTANTS_UPDATED)) {
1094		class_type->ce_flags |= ZEND_ACC_CONSTANTS_UPDATED;
1095
1096		if (class_type->parent) {
1097			if (UNEXPECTED(zend_update_class_constants(class_type->parent) != SUCCESS)) {
1098				return FAILURE;
1099			}
1100		}
1101
1102		if (!CE_STATIC_MEMBERS(class_type) && class_type->default_static_members_count) {
1103			/* initialize static members of internal class */
1104			int i;
1105			zval *p;
1106
1107#if ZTS
1108			CG(static_members_table)[(zend_intptr_t)(class_type->static_members_table)] = emalloc(sizeof(zval) * class_type->default_static_members_count);
1109#else
1110			class_type->static_members_table = emalloc(sizeof(zval) * class_type->default_static_members_count);
1111#endif
1112			for (i = 0; i < class_type->default_static_members_count; i++) {
1113				p = &class_type->default_static_members_table[i];
1114				if (Z_ISREF_P(p) &&
1115					class_type->parent &&
1116					i < class_type->parent->default_static_members_count &&
1117					p == &class_type->parent->default_static_members_table[i] &&
1118					Z_TYPE(CE_STATIC_MEMBERS(class_type->parent)[i]) != IS_UNDEF
1119				) {
1120					zval *q = &CE_STATIC_MEMBERS(class_type->parent)[i];
1121
1122					ZVAL_NEW_REF(q, q);
1123					ZVAL_COPY_VALUE(&CE_STATIC_MEMBERS(class_type)[i], q);
1124					Z_ADDREF_P(q);
1125				} else {
1126					ZVAL_DUP(&CE_STATIC_MEMBERS(class_type)[i], p);
1127				}
1128			}
1129		} else {
1130			zend_class_entry **scope = EG(current_execute_data) ? &EG(scope) : &CG(active_class_entry);
1131			zend_class_entry *old_scope = *scope;
1132			zend_class_entry *ce;
1133			zend_class_constant *c;
1134			zval *val;
1135			zend_property_info *prop_info;
1136
1137			*scope = class_type;
1138			ZEND_HASH_FOREACH_PTR(&class_type->constants_table, c) {
1139				val = &c->value;
1140				if (Z_CONSTANT_P(val)) {
1141					if (UNEXPECTED(zval_update_constant_ex(val, 1, class_type) != SUCCESS)) {
1142						return FAILURE;
1143					}
1144				}
1145			} ZEND_HASH_FOREACH_END();
1146
1147			ce = class_type;
1148			while (ce) {
1149				ZEND_HASH_FOREACH_PTR(&ce->properties_info, prop_info) {
1150					if (prop_info->ce == ce) {
1151						if (prop_info->flags & ZEND_ACC_STATIC) {
1152							val = CE_STATIC_MEMBERS(class_type) + prop_info->offset;
1153						} else {
1154							val = (zval*)((char*)class_type->default_properties_table + prop_info->offset - OBJ_PROP_TO_OFFSET(0));
1155						}
1156						ZVAL_DEREF(val);
1157						if (Z_CONSTANT_P(val)) {
1158							*scope = ce;
1159							if (UNEXPECTED(zval_update_constant_ex(val, 1, NULL) != SUCCESS)) {
1160								return FAILURE;
1161							}
1162						}
1163					}
1164				} ZEND_HASH_FOREACH_END();
1165				ce = ce->parent;
1166			}
1167
1168			*scope = old_scope;
1169		}
1170	}
1171	return SUCCESS;
1172}
1173/* }}} */
1174
1175ZEND_API void object_properties_init(zend_object *object, zend_class_entry *class_type) /* {{{ */
1176{
1177	if (class_type->default_properties_count) {
1178		zval *src = class_type->default_properties_table;
1179		zval *dst = object->properties_table;
1180		zval *end = src + class_type->default_properties_count;
1181
1182		do {
1183#if ZTS
1184			ZVAL_DUP(dst, src);
1185#else
1186			ZVAL_COPY(dst, src);
1187#endif
1188			src++;
1189			dst++;
1190		} while (src != end);
1191		object->properties = NULL;
1192	}
1193}
1194/* }}} */
1195
1196ZEND_API void object_properties_init_ex(zend_object *object, HashTable *properties) /* {{{ */
1197{
1198	object->properties = properties;
1199	if (object->ce->default_properties_count) {
1200	    zval *prop;
1201    	zend_string *key;
1202    	zend_property_info *property_info;
1203
1204    	ZEND_HASH_FOREACH_STR_KEY_VAL(properties, key, prop) {
1205			property_info = zend_get_property_info(object->ce, key, 1);
1206			if (property_info != ZEND_WRONG_PROPERTY_INFO &&
1207			    property_info &&
1208			    (property_info->flags & ZEND_ACC_STATIC) == 0) {
1209				zval *slot = OBJ_PROP(object, property_info->offset);
1210				ZVAL_COPY_VALUE(slot, prop);
1211				ZVAL_INDIRECT(prop, slot);
1212			}
1213		} ZEND_HASH_FOREACH_END();
1214	}
1215}
1216/* }}} */
1217
1218ZEND_API void object_properties_load(zend_object *object, HashTable *properties) /* {{{ */
1219{
1220    zval *prop, tmp;
1221   	zend_string *key;
1222   	zend_long h;
1223   	zend_property_info *property_info;
1224
1225   	ZEND_HASH_FOREACH_KEY_VAL(properties, h, key, prop) {
1226		if (key) {
1227			if (ZSTR_VAL(key)[0] == '\0') {
1228				const char *class_name, *prop_name;
1229				size_t prop_name_len;
1230				if (zend_unmangle_property_name_ex(key, &class_name, &prop_name, &prop_name_len) == SUCCESS) {
1231					zend_string *pname = zend_string_init(prop_name, prop_name_len, 0);
1232					property_info = zend_get_property_info(object->ce, pname, 1);
1233					zend_string_release(pname);
1234				} else {
1235					property_info = ZEND_WRONG_PROPERTY_INFO;
1236				}
1237			} else {
1238				property_info = zend_get_property_info(object->ce, key, 1);
1239			}
1240			if (property_info != ZEND_WRONG_PROPERTY_INFO &&
1241				property_info &&
1242				(property_info->flags & ZEND_ACC_STATIC) == 0) {
1243				zval *slot = OBJ_PROP(object, property_info->offset);
1244				zval_ptr_dtor(slot);
1245				ZVAL_COPY_VALUE(slot, prop);
1246				zval_add_ref(slot);
1247				if (object->properties) {
1248					ZVAL_INDIRECT(&tmp, slot);
1249					zend_hash_update(object->properties, key, &tmp);
1250				}
1251			} else {
1252				if (!object->properties) {
1253					rebuild_object_properties(object);
1254				}
1255				prop = zend_hash_update(object->properties, key, prop);
1256				zval_add_ref(prop);
1257			}
1258		} else {
1259			if (!object->properties) {
1260				rebuild_object_properties(object);
1261			}
1262			prop = zend_hash_index_update(object->properties, h, prop);
1263			zval_add_ref(prop);
1264		}
1265	} ZEND_HASH_FOREACH_END();
1266}
1267/* }}} */
1268
1269/* This function requires 'properties' to contain all props declared in the
1270 * class and all props being public. If only a subset is given or the class
1271 * has protected members then you need to merge the properties separately by
1272 * calling zend_merge_properties(). */
1273ZEND_API int _object_and_properties_init(zval *arg, zend_class_entry *class_type, HashTable *properties ZEND_FILE_LINE_DC) /* {{{ */
1274{
1275	if (UNEXPECTED(class_type->ce_flags & (ZEND_ACC_INTERFACE|ZEND_ACC_TRAIT|ZEND_ACC_IMPLICIT_ABSTRACT_CLASS|ZEND_ACC_EXPLICIT_ABSTRACT_CLASS))) {
1276		if (class_type->ce_flags & ZEND_ACC_INTERFACE) {
1277			zend_throw_error(NULL, "Cannot instantiate interface %s", ZSTR_VAL(class_type->name));
1278		} else if (class_type->ce_flags & ZEND_ACC_TRAIT) {
1279			zend_throw_error(NULL, "Cannot instantiate trait %s", ZSTR_VAL(class_type->name));
1280		} else {
1281			zend_throw_error(NULL, "Cannot instantiate abstract class %s", ZSTR_VAL(class_type->name));
1282		}
1283		ZVAL_NULL(arg);
1284		Z_OBJ_P(arg) = NULL;
1285		return FAILURE;
1286	}
1287
1288	if (UNEXPECTED(!(class_type->ce_flags & ZEND_ACC_CONSTANTS_UPDATED))) {
1289		if (UNEXPECTED(zend_update_class_constants(class_type) != SUCCESS)) {
1290			ZVAL_NULL(arg);
1291			Z_OBJ_P(arg) = NULL;
1292			return FAILURE;
1293		}
1294	}
1295
1296	if (class_type->create_object == NULL) {
1297		ZVAL_OBJ(arg, zend_objects_new(class_type));
1298		if (properties) {
1299			object_properties_init_ex(Z_OBJ_P(arg), properties);
1300		} else {
1301			object_properties_init(Z_OBJ_P(arg), class_type);
1302		}
1303	} else {
1304		ZVAL_OBJ(arg, class_type->create_object(class_type));
1305	}
1306	return SUCCESS;
1307}
1308/* }}} */
1309
1310ZEND_API int _object_init_ex(zval *arg, zend_class_entry *class_type ZEND_FILE_LINE_DC) /* {{{ */
1311{
1312	return _object_and_properties_init(arg, class_type, 0 ZEND_FILE_LINE_RELAY_CC);
1313}
1314/* }}} */
1315
1316ZEND_API int _object_init(zval *arg ZEND_FILE_LINE_DC) /* {{{ */
1317{
1318	return _object_init_ex(arg, zend_standard_class_def ZEND_FILE_LINE_RELAY_CC);
1319}
1320/* }}} */
1321
1322ZEND_API int add_assoc_long_ex(zval *arg, const char *key, size_t key_len, zend_long n) /* {{{ */
1323{
1324	zval *ret, tmp;
1325
1326	ZVAL_LONG(&tmp, n);
1327	ret = zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
1328	return ret ? SUCCESS : FAILURE;
1329}
1330/* }}} */
1331
1332ZEND_API int add_assoc_null_ex(zval *arg, const char *key, size_t key_len) /* {{{ */
1333{
1334	zval *ret, tmp;
1335
1336	ZVAL_NULL(&tmp);
1337	ret = zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
1338	return ret ? SUCCESS : FAILURE;
1339}
1340/* }}} */
1341
1342ZEND_API int add_assoc_bool_ex(zval *arg, const char *key, size_t key_len, int b) /* {{{ */
1343{
1344	zval *ret, tmp;
1345
1346	ZVAL_BOOL(&tmp, b);
1347	ret = zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
1348	return ret ? SUCCESS : FAILURE;
1349}
1350/* }}} */
1351
1352ZEND_API int add_assoc_resource_ex(zval *arg, const char *key, size_t key_len, zend_resource *r) /* {{{ */
1353{
1354	zval *ret, tmp;
1355
1356	ZVAL_RES(&tmp, r);
1357	ret = zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
1358	return ret ? SUCCESS : FAILURE;
1359}
1360/* }}} */
1361
1362ZEND_API int add_assoc_double_ex(zval *arg, const char *key, size_t key_len, double d) /* {{{ */
1363{
1364	zval *ret, tmp;
1365
1366	ZVAL_DOUBLE(&tmp, d);
1367	ret = zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
1368	return ret ? SUCCESS : FAILURE;
1369}
1370/* }}} */
1371
1372ZEND_API int add_assoc_str_ex(zval *arg, const char *key, size_t key_len, zend_string *str) /* {{{ */
1373{
1374	zval *ret, tmp;
1375
1376	ZVAL_STR(&tmp, str);
1377	ret = zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
1378	return ret ? SUCCESS : FAILURE;
1379}
1380/* }}} */
1381
1382ZEND_API int add_assoc_string_ex(zval *arg, const char *key, size_t key_len, char *str) /* {{{ */
1383{
1384	zval *ret, tmp;
1385
1386	ZVAL_STRING(&tmp, str);
1387	ret = zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
1388	return ret ? SUCCESS : FAILURE;
1389}
1390/* }}} */
1391
1392ZEND_API int add_assoc_stringl_ex(zval *arg, const char *key, size_t key_len, char *str, size_t length) /* {{{ */
1393{
1394	zval *ret, tmp;
1395
1396	ZVAL_STRINGL(&tmp, str, length);
1397	ret = zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
1398	return ret ? SUCCESS : FAILURE;
1399}
1400/* }}} */
1401
1402ZEND_API int add_assoc_zval_ex(zval *arg, const char *key, size_t key_len, zval *value) /* {{{ */
1403{
1404	zval *ret;
1405
1406	ret = zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, value);
1407	return ret ? SUCCESS : FAILURE;
1408}
1409/* }}} */
1410
1411ZEND_API int add_index_long(zval *arg, zend_ulong index, zend_long n) /* {{{ */
1412{
1413	zval tmp;
1414
1415	ZVAL_LONG(&tmp, n);
1416	return zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp) ? SUCCESS : FAILURE;
1417}
1418/* }}} */
1419
1420ZEND_API int add_index_null(zval *arg, zend_ulong index) /* {{{ */
1421{
1422	zval tmp;
1423
1424	ZVAL_NULL(&tmp);
1425	return zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp) ? SUCCESS : FAILURE;
1426}
1427/* }}} */
1428
1429ZEND_API int add_index_bool(zval *arg, zend_ulong index, int b) /* {{{ */
1430{
1431	zval tmp;
1432
1433	ZVAL_BOOL(&tmp, b);
1434	return zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp) ? SUCCESS : FAILURE;
1435}
1436/* }}} */
1437
1438ZEND_API int add_index_resource(zval *arg, zend_ulong index, zend_resource *r) /* {{{ */
1439{
1440	zval tmp;
1441
1442	ZVAL_RES(&tmp, r);
1443	return zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp) ? SUCCESS : FAILURE;
1444}
1445/* }}} */
1446
1447ZEND_API int add_index_double(zval *arg, zend_ulong index, double d) /* {{{ */
1448{
1449	zval tmp;
1450
1451	ZVAL_DOUBLE(&tmp, d);
1452	return zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp) ? SUCCESS : FAILURE;
1453}
1454/* }}} */
1455
1456ZEND_API int add_index_str(zval *arg, zend_ulong index, zend_string *str) /* {{{ */
1457{
1458	zval tmp;
1459
1460	ZVAL_STR(&tmp, str);
1461	return zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp) ? SUCCESS : FAILURE;
1462}
1463/* }}} */
1464
1465ZEND_API int add_index_string(zval *arg, zend_ulong index, const char *str) /* {{{ */
1466{
1467	zval tmp;
1468
1469	ZVAL_STRING(&tmp, str);
1470	return zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp) ? SUCCESS : FAILURE;
1471}
1472/* }}} */
1473
1474ZEND_API int add_index_stringl(zval *arg, zend_ulong index, const char *str, size_t length) /* {{{ */
1475{
1476	zval tmp;
1477
1478	ZVAL_STRINGL(&tmp, str, length);
1479	return zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp) ? SUCCESS : FAILURE;
1480}
1481/* }}} */
1482
1483ZEND_API int add_index_zval(zval *arg, zend_ulong index, zval *value) /* {{{ */
1484{
1485	return zend_hash_index_update(Z_ARRVAL_P(arg), index, value) ? SUCCESS : FAILURE;
1486}
1487/* }}} */
1488
1489ZEND_API int add_next_index_long(zval *arg, zend_long n) /* {{{ */
1490{
1491	zval tmp;
1492
1493	ZVAL_LONG(&tmp, n);
1494	return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE;
1495}
1496/* }}} */
1497
1498ZEND_API int add_next_index_null(zval *arg) /* {{{ */
1499{
1500	zval tmp;
1501
1502	ZVAL_NULL(&tmp);
1503	return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE;
1504}
1505/* }}} */
1506
1507ZEND_API int add_next_index_bool(zval *arg, int b) /* {{{ */
1508{
1509	zval tmp;
1510
1511	ZVAL_BOOL(&tmp, b);
1512	return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE;
1513}
1514/* }}} */
1515
1516ZEND_API int add_next_index_resource(zval *arg, zend_resource *r) /* {{{ */
1517{
1518	zval tmp;
1519
1520	ZVAL_RES(&tmp, r);
1521	return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE;
1522}
1523/* }}} */
1524
1525ZEND_API int add_next_index_double(zval *arg, double d) /* {{{ */
1526{
1527	zval tmp;
1528
1529	ZVAL_DOUBLE(&tmp, d);
1530	return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE;
1531}
1532/* }}} */
1533
1534ZEND_API int add_next_index_str(zval *arg, zend_string *str) /* {{{ */
1535{
1536	zval tmp;
1537
1538	ZVAL_STR(&tmp, str);
1539	return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE;
1540}
1541/* }}} */
1542
1543ZEND_API int add_next_index_string(zval *arg, const char *str) /* {{{ */
1544{
1545	zval tmp;
1546
1547	ZVAL_STRING(&tmp, str);
1548	return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE;
1549}
1550/* }}} */
1551
1552ZEND_API int add_next_index_stringl(zval *arg, const char *str, size_t length) /* {{{ */
1553{
1554	zval tmp;
1555
1556	ZVAL_STRINGL(&tmp, str, length);
1557	return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE;
1558}
1559/* }}} */
1560
1561ZEND_API int add_next_index_zval(zval *arg, zval *value) /* {{{ */
1562{
1563	return zend_hash_next_index_insert(Z_ARRVAL_P(arg), value) ? SUCCESS : FAILURE;
1564}
1565/* }}} */
1566
1567ZEND_API zval *add_get_assoc_string_ex(zval *arg, const char *key, uint key_len, const char *str) /* {{{ */
1568{
1569	zval tmp, *ret;
1570
1571	ZVAL_STRING(&tmp, str);
1572	ret = zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
1573	return ret;
1574}
1575/* }}} */
1576
1577ZEND_API zval *add_get_assoc_stringl_ex(zval *arg, const char *key, uint key_len, const char *str, size_t length) /* {{{ */
1578{
1579	zval tmp, *ret;
1580
1581	ZVAL_STRINGL(&tmp, str, length);
1582	ret = zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
1583	return ret;
1584}
1585/* }}} */
1586
1587ZEND_API zval *add_get_index_long(zval *arg, zend_ulong index, zend_long l) /* {{{ */
1588{
1589	zval tmp;
1590
1591	ZVAL_LONG(&tmp, l);
1592	return zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp);
1593}
1594/* }}} */
1595
1596ZEND_API zval *add_get_index_double(zval *arg, zend_ulong index, double d) /* {{{ */
1597{
1598	zval tmp;
1599
1600	ZVAL_DOUBLE(&tmp, d);
1601	return zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp);
1602}
1603/* }}} */
1604
1605ZEND_API zval *add_get_index_str(zval *arg, zend_ulong index, zend_string *str) /* {{{ */
1606{
1607	zval tmp;
1608
1609	ZVAL_STR(&tmp, str);
1610	return zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp);
1611}
1612/* }}} */
1613
1614ZEND_API zval *add_get_index_string(zval *arg, zend_ulong index, const char *str) /* {{{ */
1615{
1616	zval tmp;
1617
1618	ZVAL_STRING(&tmp, str);
1619	return zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp);
1620}
1621/* }}} */
1622
1623ZEND_API zval *add_get_index_stringl(zval *arg, zend_ulong index, const char *str, size_t length) /* {{{ */
1624{
1625	zval tmp;
1626
1627	ZVAL_STRINGL(&tmp, str, length);
1628	return zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp);
1629}
1630/* }}} */
1631
1632ZEND_API int array_set_zval_key(HashTable *ht, zval *key, zval *value) /* {{{ */
1633{
1634	zval *result;
1635
1636	switch (Z_TYPE_P(key)) {
1637		case IS_STRING:
1638			result = zend_symtable_update(ht, Z_STR_P(key), value);
1639			break;
1640		case IS_NULL:
1641			result = zend_symtable_update(ht, ZSTR_EMPTY_ALLOC(), value);
1642			break;
1643		case IS_RESOURCE:
1644			zend_error(E_NOTICE, "Resource ID#" ZEND_LONG_FMT " used as offset, casting to integer (%pd)", Z_RES_HANDLE_P(key), Z_RES_HANDLE_P(key));
1645			result = zend_hash_index_update(ht, Z_RES_HANDLE_P(key), value);
1646			break;
1647		case IS_FALSE:
1648			result = zend_hash_index_update(ht, 0, value);
1649			break;
1650		case IS_TRUE:
1651			result = zend_hash_index_update(ht, 1, value);
1652			break;
1653		case IS_LONG:
1654			result = zend_hash_index_update(ht, Z_LVAL_P(key), value);
1655			break;
1656		case IS_DOUBLE:
1657			result = zend_hash_index_update(ht, zend_dval_to_lval(Z_DVAL_P(key)), value);
1658			break;
1659		default:
1660			zend_error(E_WARNING, "Illegal offset type");
1661			result = NULL;
1662	}
1663
1664	if (result) {
1665		if (Z_REFCOUNTED_P(result)) {
1666			Z_ADDREF_P(result);
1667		}
1668		return SUCCESS;
1669	} else {
1670		return FAILURE;
1671	}
1672}
1673/* }}} */
1674
1675ZEND_API int add_property_long_ex(zval *arg, const char *key, size_t key_len, zend_long n) /* {{{ */
1676{
1677	zval tmp;
1678	zval z_key;
1679
1680	ZVAL_LONG(&tmp, n);
1681	ZVAL_STRINGL(&z_key, key, key_len);
1682	Z_OBJ_HANDLER_P(arg, write_property)(arg, &z_key, &tmp, NULL);
1683	zval_ptr_dtor(&tmp); /* write_property will add 1 to refcount */
1684	zval_ptr_dtor(&z_key);
1685	return SUCCESS;
1686}
1687/* }}} */
1688
1689ZEND_API int add_property_bool_ex(zval *arg, const char *key, size_t key_len, zend_long b) /* {{{ */
1690{
1691	zval tmp;
1692	zval z_key;
1693
1694	ZVAL_BOOL(&tmp, b);
1695	ZVAL_STRINGL(&z_key, key, key_len);
1696	Z_OBJ_HANDLER_P(arg, write_property)(arg, &z_key, &tmp, NULL);
1697	zval_ptr_dtor(&tmp); /* write_property will add 1 to refcount */
1698	zval_ptr_dtor(&z_key);
1699	return SUCCESS;
1700}
1701/* }}} */
1702
1703ZEND_API int add_property_null_ex(zval *arg, const char *key, size_t key_len) /* {{{ */
1704{
1705	zval tmp;
1706	zval z_key;
1707
1708	ZVAL_NULL(&tmp);
1709	ZVAL_STRINGL(&z_key, key, key_len);
1710	Z_OBJ_HANDLER_P(arg, write_property)(arg, &z_key, &tmp, NULL);
1711	zval_ptr_dtor(&tmp); /* write_property will add 1 to refcount */
1712	zval_ptr_dtor(&z_key);
1713	return SUCCESS;
1714}
1715/* }}} */
1716
1717ZEND_API int add_property_resource_ex(zval *arg, const char *key, size_t key_len, zend_resource *r) /* {{{ */
1718{
1719	zval tmp;
1720	zval z_key;
1721
1722	ZVAL_RES(&tmp, r);
1723	ZVAL_STRINGL(&z_key, key, key_len);
1724	Z_OBJ_HANDLER_P(arg, write_property)(arg, &z_key, &tmp, NULL);
1725	zval_ptr_dtor(&tmp); /* write_property will add 1 to refcount */
1726	zval_ptr_dtor(&z_key);
1727	return SUCCESS;
1728}
1729/* }}} */
1730
1731ZEND_API int add_property_double_ex(zval *arg, const char *key, size_t key_len, double d) /* {{{ */
1732{
1733	zval tmp;
1734	zval z_key;
1735
1736	ZVAL_DOUBLE(&tmp, d);
1737	ZVAL_STRINGL(&z_key, key, key_len);
1738	Z_OBJ_HANDLER_P(arg, write_property)(arg, &z_key, &tmp, NULL);
1739	zval_ptr_dtor(&tmp); /* write_property will add 1 to refcount */
1740	zval_ptr_dtor(&z_key);
1741	return SUCCESS;
1742}
1743/* }}} */
1744
1745ZEND_API int add_property_str_ex(zval *arg, const char *key, size_t key_len, zend_string *str) /* {{{ */
1746{
1747	zval tmp;
1748	zval z_key;
1749
1750	ZVAL_STR(&tmp, str);
1751	ZVAL_STRINGL(&z_key, key, key_len);
1752	Z_OBJ_HANDLER_P(arg, write_property)(arg, &z_key, &tmp, NULL);
1753	zval_ptr_dtor(&tmp); /* write_property will add 1 to refcount */
1754	zval_ptr_dtor(&z_key);
1755	return SUCCESS;
1756}
1757/* }}} */
1758
1759ZEND_API int add_property_string_ex(zval *arg, const char *key, size_t key_len, const char *str) /* {{{ */
1760{
1761	zval tmp;
1762	zval z_key;
1763
1764	ZVAL_STRING(&tmp, str);
1765	ZVAL_STRINGL(&z_key, key, key_len);
1766	Z_OBJ_HANDLER_P(arg, write_property)(arg, &z_key, &tmp, NULL);
1767	zval_ptr_dtor(&tmp); /* write_property will add 1 to refcount */
1768	zval_ptr_dtor(&z_key);
1769	return SUCCESS;
1770}
1771/* }}} */
1772
1773ZEND_API int add_property_stringl_ex(zval *arg, const char *key, size_t key_len, const char *str, size_t length) /* {{{ */
1774{
1775	zval tmp;
1776	zval z_key;
1777
1778	ZVAL_STRINGL(&tmp, str, length);
1779	ZVAL_STRINGL(&z_key, key, key_len);
1780	Z_OBJ_HANDLER_P(arg, write_property)(arg, &z_key, &tmp, NULL);
1781	zval_ptr_dtor(&tmp); /* write_property will add 1 to refcount */
1782	zval_ptr_dtor(&z_key);
1783	return SUCCESS;
1784}
1785/* }}} */
1786
1787ZEND_API int add_property_zval_ex(zval *arg, const char *key, size_t key_len, zval *value) /* {{{ */
1788{
1789	zval z_key;
1790
1791	ZVAL_STRINGL(&z_key, key, key_len);
1792	Z_OBJ_HANDLER_P(arg, write_property)(arg, &z_key, value, NULL);
1793	zval_ptr_dtor(&z_key);
1794	return SUCCESS;
1795}
1796/* }}} */
1797
1798ZEND_API int zend_startup_module_ex(zend_module_entry *module) /* {{{ */
1799{
1800	size_t name_len;
1801	zend_string *lcname;
1802
1803	if (module->module_started) {
1804		return SUCCESS;
1805	}
1806	module->module_started = 1;
1807
1808	/* Check module dependencies */
1809	if (module->deps) {
1810		const zend_module_dep *dep = module->deps;
1811
1812		while (dep->name) {
1813			if (dep->type == MODULE_DEP_REQUIRED) {
1814				zend_module_entry *req_mod;
1815
1816				name_len = strlen(dep->name);
1817				lcname = zend_string_alloc(name_len, 0);
1818				zend_str_tolower_copy(ZSTR_VAL(lcname), dep->name, name_len);
1819
1820				if ((req_mod = zend_hash_find_ptr(&module_registry, lcname)) == NULL || !req_mod->module_started) {
1821					zend_string_free(lcname);
1822					/* TODO: Check version relationship */
1823					zend_error(E_CORE_WARNING, "Cannot load module '%s' because required module '%s' is not loaded", module->name, dep->name);
1824					module->module_started = 0;
1825					return FAILURE;
1826				}
1827				zend_string_free(lcname);
1828			}
1829			++dep;
1830		}
1831	}
1832
1833	/* Initialize module globals */
1834	if (module->globals_size) {
1835#ifdef ZTS
1836		ts_allocate_id(module->globals_id_ptr, module->globals_size, (ts_allocate_ctor) module->globals_ctor, (ts_allocate_dtor) module->globals_dtor);
1837#else
1838		if (module->globals_ctor) {
1839			module->globals_ctor(module->globals_ptr);
1840		}
1841#endif
1842	}
1843	if (module->module_startup_func) {
1844		EG(current_module) = module;
1845		if (module->module_startup_func(module->type, module->module_number)==FAILURE) {
1846			zend_error_noreturn(E_CORE_ERROR,"Unable to start %s module", module->name);
1847			EG(current_module) = NULL;
1848			return FAILURE;
1849		}
1850		EG(current_module) = NULL;
1851	}
1852	return SUCCESS;
1853}
1854/* }}} */
1855
1856static int zend_startup_module_zval(zval *zv) /* {{{ */
1857{
1858	zend_module_entry *module = Z_PTR_P(zv);
1859
1860	return zend_startup_module_ex(module);
1861}
1862/* }}} */
1863
1864static void zend_sort_modules(void *base, size_t count, size_t siz, compare_func_t compare, swap_func_t swp) /* {{{ */
1865{
1866	Bucket *b1 = base;
1867	Bucket *b2;
1868	Bucket *end = b1 + count;
1869	Bucket tmp;
1870	zend_module_entry *m, *r;
1871
1872	while (b1 < end) {
1873try_again:
1874		m = (zend_module_entry*)Z_PTR(b1->val);
1875		if (!m->module_started && m->deps) {
1876			const zend_module_dep *dep = m->deps;
1877			while (dep->name) {
1878				if (dep->type == MODULE_DEP_REQUIRED || dep->type == MODULE_DEP_OPTIONAL) {
1879					b2 = b1 + 1;
1880					while (b2 < end) {
1881						r = (zend_module_entry*)Z_PTR(b2->val);
1882						if (strcasecmp(dep->name, r->name) == 0) {
1883							tmp = *b1;
1884							*b1 = *b2;
1885							*b2 = tmp;
1886							goto try_again;
1887						}
1888						b2++;
1889					}
1890				}
1891				dep++;
1892			}
1893		}
1894		b1++;
1895	}
1896}
1897/* }}} */
1898
1899ZEND_API void zend_collect_module_handlers(void) /* {{{ */
1900{
1901	zend_module_entry *module;
1902	int startup_count = 0;
1903	int shutdown_count = 0;
1904	int post_deactivate_count = 0;
1905	zend_class_entry *ce;
1906	int class_count = 0;
1907
1908	/* Collect extensions with request startup/shutdown handlers */
1909	ZEND_HASH_FOREACH_PTR(&module_registry, module) {
1910		if (module->request_startup_func) {
1911			startup_count++;
1912		}
1913		if (module->request_shutdown_func) {
1914			shutdown_count++;
1915		}
1916		if (module->post_deactivate_func) {
1917			post_deactivate_count++;
1918		}
1919	} ZEND_HASH_FOREACH_END();
1920	module_request_startup_handlers = (zend_module_entry**)malloc(
1921	    sizeof(zend_module_entry*) *
1922		(startup_count + 1 +
1923		 shutdown_count + 1 +
1924		 post_deactivate_count + 1));
1925	module_request_startup_handlers[startup_count] = NULL;
1926	module_request_shutdown_handlers = module_request_startup_handlers + startup_count + 1;
1927	module_request_shutdown_handlers[shutdown_count] = NULL;
1928	module_post_deactivate_handlers = module_request_shutdown_handlers + shutdown_count + 1;
1929	module_post_deactivate_handlers[post_deactivate_count] = NULL;
1930	startup_count = 0;
1931
1932	ZEND_HASH_FOREACH_PTR(&module_registry, module) {
1933		if (module->request_startup_func) {
1934			module_request_startup_handlers[startup_count++] = module;
1935		}
1936		if (module->request_shutdown_func) {
1937			module_request_shutdown_handlers[--shutdown_count] = module;
1938		}
1939		if (module->post_deactivate_func) {
1940			module_post_deactivate_handlers[--post_deactivate_count] = module;
1941		}
1942	} ZEND_HASH_FOREACH_END();
1943
1944	/* Collect internal classes with static members */
1945	ZEND_HASH_FOREACH_PTR(CG(class_table), ce) {
1946		if (ce->type == ZEND_INTERNAL_CLASS &&
1947		    ce->default_static_members_count > 0) {
1948		    class_count++;
1949		}
1950	} ZEND_HASH_FOREACH_END();
1951
1952	class_cleanup_handlers = (zend_class_entry**)malloc(
1953		sizeof(zend_class_entry*) *
1954		(class_count + 1));
1955	class_cleanup_handlers[class_count] = NULL;
1956
1957	if (class_count) {
1958		ZEND_HASH_FOREACH_PTR(CG(class_table), ce) {
1959			if (ce->type == ZEND_INTERNAL_CLASS &&
1960			    ce->default_static_members_count > 0) {
1961			    class_cleanup_handlers[--class_count] = ce;
1962			}
1963		} ZEND_HASH_FOREACH_END();
1964	}
1965}
1966/* }}} */
1967
1968ZEND_API int zend_startup_modules(void) /* {{{ */
1969{
1970	zend_hash_sort_ex(&module_registry, zend_sort_modules, NULL, 0);
1971	zend_hash_apply(&module_registry, zend_startup_module_zval);
1972	return SUCCESS;
1973}
1974/* }}} */
1975
1976ZEND_API void zend_destroy_modules(void) /* {{{ */
1977{
1978	free(class_cleanup_handlers);
1979	free(module_request_startup_handlers);
1980	zend_hash_graceful_reverse_destroy(&module_registry);
1981}
1982/* }}} */
1983
1984ZEND_API zend_module_entry* zend_register_module_ex(zend_module_entry *module) /* {{{ */
1985{
1986	size_t name_len;
1987	zend_string *lcname;
1988	zend_module_entry *module_ptr;
1989
1990	if (!module) {
1991		return NULL;
1992	}
1993
1994#if 0
1995	zend_printf("%s: Registering module %d\n", module->name, module->module_number);
1996#endif
1997
1998	/* Check module dependencies */
1999	if (module->deps) {
2000		const zend_module_dep *dep = module->deps;
2001
2002		while (dep->name) {
2003			if (dep->type == MODULE_DEP_CONFLICTS) {
2004				name_len = strlen(dep->name);
2005				lcname = zend_string_alloc(name_len, 0);
2006				zend_str_tolower_copy(ZSTR_VAL(lcname), dep->name, name_len);
2007
2008				if (zend_hash_exists(&module_registry, lcname) || zend_get_extension(dep->name)) {
2009					zend_string_free(lcname);
2010					/* TODO: Check version relationship */
2011					zend_error(E_CORE_WARNING, "Cannot load module '%s' because conflicting module '%s' is already loaded", module->name, dep->name);
2012					return NULL;
2013				}
2014				zend_string_free(lcname);
2015			}
2016			++dep;
2017		}
2018	}
2019
2020	name_len = strlen(module->name);
2021	lcname = zend_string_alloc(name_len, 1);
2022	zend_str_tolower_copy(ZSTR_VAL(lcname), module->name, name_len);
2023
2024	if ((module_ptr = zend_hash_add_mem(&module_registry, lcname, module, sizeof(zend_module_entry))) == NULL) {
2025		zend_error(E_CORE_WARNING, "Module '%s' already loaded", module->name);
2026		zend_string_release(lcname);
2027		return NULL;
2028	}
2029	zend_string_release(lcname);
2030	module = module_ptr;
2031	EG(current_module) = module;
2032
2033	if (module->functions && zend_register_functions(NULL, module->functions, NULL, module->type)==FAILURE) {
2034		EG(current_module) = NULL;
2035		zend_error(E_CORE_WARNING,"%s: Unable to register functions, unable to load", module->name);
2036		return NULL;
2037	}
2038
2039	EG(current_module) = NULL;
2040	return module;
2041}
2042/* }}} */
2043
2044ZEND_API zend_module_entry* zend_register_internal_module(zend_module_entry *module) /* {{{ */
2045{
2046	module->module_number = zend_next_free_module();
2047	module->type = MODULE_PERSISTENT;
2048	return zend_register_module_ex(module);
2049}
2050/* }}} */
2051
2052ZEND_API void zend_check_magic_method_implementation(const zend_class_entry *ce, const zend_function *fptr, int error_type) /* {{{ */
2053{
2054	char lcname[16];
2055	size_t name_len;
2056
2057	/* we don't care if the function name is longer, in fact lowercasing only
2058	 * the beginning of the name speeds up the check process */
2059	name_len = ZSTR_LEN(fptr->common.function_name);
2060	zend_str_tolower_copy(lcname, ZSTR_VAL(fptr->common.function_name), MIN(name_len, sizeof(lcname)-1));
2061	lcname[sizeof(lcname)-1] = '\0'; /* zend_str_tolower_copy won't necessarily set the zero byte */
2062
2063	if (name_len == sizeof(ZEND_DESTRUCTOR_FUNC_NAME) - 1 && !memcmp(lcname, ZEND_DESTRUCTOR_FUNC_NAME, sizeof(ZEND_DESTRUCTOR_FUNC_NAME) - 1) && fptr->common.num_args != 0) {
2064		zend_error(error_type, "Destructor %s::%s() cannot take arguments", ZSTR_VAL(ce->name), ZEND_DESTRUCTOR_FUNC_NAME);
2065	} else if (name_len == sizeof(ZEND_CLONE_FUNC_NAME) - 1 && !memcmp(lcname, ZEND_CLONE_FUNC_NAME, sizeof(ZEND_CLONE_FUNC_NAME) - 1) && fptr->common.num_args != 0) {
2066		zend_error(error_type, "Method %s::%s() cannot accept any arguments", ZSTR_VAL(ce->name), ZEND_CLONE_FUNC_NAME);
2067	} else if (name_len == sizeof(ZEND_GET_FUNC_NAME) - 1 && !memcmp(lcname, ZEND_GET_FUNC_NAME, sizeof(ZEND_GET_FUNC_NAME) - 1)) {
2068		if (fptr->common.num_args != 1) {
2069			zend_error(error_type, "Method %s::%s() must take exactly 1 argument", ZSTR_VAL(ce->name), ZEND_GET_FUNC_NAME);
2070		} else if (ARG_SHOULD_BE_SENT_BY_REF(fptr, 1)) {
2071			zend_error(error_type, "Method %s::%s() cannot take arguments by reference", ZSTR_VAL(ce->name), ZEND_GET_FUNC_NAME);
2072		}
2073	} else if (name_len == sizeof(ZEND_SET_FUNC_NAME) - 1 && !memcmp(lcname, ZEND_SET_FUNC_NAME, sizeof(ZEND_SET_FUNC_NAME) - 1)) {
2074		if (fptr->common.num_args != 2) {
2075			zend_error(error_type, "Method %s::%s() must take exactly 2 arguments", ZSTR_VAL(ce->name), ZEND_SET_FUNC_NAME);
2076		} else if (ARG_SHOULD_BE_SENT_BY_REF(fptr, 1) || ARG_SHOULD_BE_SENT_BY_REF(fptr, 2)) {
2077			zend_error(error_type, "Method %s::%s() cannot take arguments by reference", ZSTR_VAL(ce->name), ZEND_SET_FUNC_NAME);
2078		}
2079	} else if (name_len == sizeof(ZEND_UNSET_FUNC_NAME) - 1 && !memcmp(lcname, ZEND_UNSET_FUNC_NAME, sizeof(ZEND_UNSET_FUNC_NAME) - 1)) {
2080		if (fptr->common.num_args != 1) {
2081			zend_error(error_type, "Method %s::%s() must take exactly 1 argument", ZSTR_VAL(ce->name), ZEND_UNSET_FUNC_NAME);
2082		} else if (ARG_SHOULD_BE_SENT_BY_REF(fptr, 1)) {
2083			zend_error(error_type, "Method %s::%s() cannot take arguments by reference", ZSTR_VAL(ce->name), ZEND_UNSET_FUNC_NAME);
2084		}
2085	} else if (name_len == sizeof(ZEND_ISSET_FUNC_NAME) - 1 && !memcmp(lcname, ZEND_ISSET_FUNC_NAME, sizeof(ZEND_ISSET_FUNC_NAME) - 1)) {
2086		if (fptr->common.num_args != 1) {
2087			zend_error(error_type, "Method %s::%s() must take exactly 1 argument", ZSTR_VAL(ce->name), ZEND_ISSET_FUNC_NAME);
2088		} else if (ARG_SHOULD_BE_SENT_BY_REF(fptr, 1)) {
2089			zend_error(error_type, "Method %s::%s() cannot take arguments by reference", ZSTR_VAL(ce->name), ZEND_ISSET_FUNC_NAME);
2090		}
2091	} else if (name_len == sizeof(ZEND_CALL_FUNC_NAME) - 1 && !memcmp(lcname, ZEND_CALL_FUNC_NAME, sizeof(ZEND_CALL_FUNC_NAME) - 1)) {
2092		if (fptr->common.num_args != 2) {
2093			zend_error(error_type, "Method %s::%s() must take exactly 2 arguments", ZSTR_VAL(ce->name), ZEND_CALL_FUNC_NAME);
2094		} else if (ARG_SHOULD_BE_SENT_BY_REF(fptr, 1) || ARG_SHOULD_BE_SENT_BY_REF(fptr, 2)) {
2095			zend_error(error_type, "Method %s::%s() cannot take arguments by reference", ZSTR_VAL(ce->name), ZEND_CALL_FUNC_NAME);
2096		}
2097	} else if (name_len == sizeof(ZEND_CALLSTATIC_FUNC_NAME) - 1 &&
2098		!memcmp(lcname, ZEND_CALLSTATIC_FUNC_NAME, sizeof(ZEND_CALLSTATIC_FUNC_NAME)-1)
2099	) {
2100		if (fptr->common.num_args != 2) {
2101			zend_error(error_type, "Method %s::__callStatic() must take exactly 2 arguments", ZSTR_VAL(ce->name));
2102		} else if (ARG_SHOULD_BE_SENT_BY_REF(fptr, 1) || ARG_SHOULD_BE_SENT_BY_REF(fptr, 2)) {
2103			zend_error(error_type, "Method %s::__callStatic() cannot take arguments by reference", ZSTR_VAL(ce->name));
2104		}
2105 	} else if (name_len == sizeof(ZEND_TOSTRING_FUNC_NAME) - 1 &&
2106 		!memcmp(lcname, ZEND_TOSTRING_FUNC_NAME, sizeof(ZEND_TOSTRING_FUNC_NAME)-1) && fptr->common.num_args != 0
2107	) {
2108		zend_error(error_type, "Method %s::%s() cannot take arguments", ZSTR_VAL(ce->name), ZEND_TOSTRING_FUNC_NAME);
2109	} else if (name_len == sizeof(ZEND_DEBUGINFO_FUNC_NAME) - 1 &&
2110		!memcmp(lcname, ZEND_DEBUGINFO_FUNC_NAME, sizeof(ZEND_DEBUGINFO_FUNC_NAME)-1) && fptr->common.num_args != 0) {
2111		zend_error(error_type, "Method %s::%s() cannot take arguments", ZSTR_VAL(ce->name), ZEND_DEBUGINFO_FUNC_NAME);
2112	}
2113}
2114/* }}} */
2115
2116/* registers all functions in *library_functions in the function hash */
2117ZEND_API int zend_register_functions(zend_class_entry *scope, const zend_function_entry *functions, HashTable *function_table, int type) /* {{{ */
2118{
2119	const zend_function_entry *ptr = functions;
2120	zend_function function, *reg_function;
2121	zend_internal_function *internal_function = (zend_internal_function *)&function;
2122	int count=0, unload=0;
2123	HashTable *target_function_table = function_table;
2124	int error_type;
2125	zend_function *ctor = NULL, *dtor = NULL, *clone = NULL, *__get = NULL, *__set = NULL, *__unset = NULL, *__isset = NULL, *__call = NULL, *__callstatic = NULL, *__tostring = NULL, *__debugInfo = NULL;
2126	zend_string *lowercase_name;
2127	size_t fname_len;
2128	const char *lc_class_name = NULL;
2129	size_t class_name_len = 0;
2130
2131	if (type==MODULE_PERSISTENT) {
2132		error_type = E_CORE_WARNING;
2133	} else {
2134		error_type = E_WARNING;
2135	}
2136
2137	if (!target_function_table) {
2138		target_function_table = CG(function_table);
2139	}
2140	internal_function->type = ZEND_INTERNAL_FUNCTION;
2141	internal_function->module = EG(current_module);
2142	memset(internal_function->reserved, 0, ZEND_MAX_RESERVED_RESOURCES * sizeof(void*));
2143
2144	if (scope) {
2145		class_name_len = ZSTR_LEN(scope->name);
2146		if ((lc_class_name = zend_memrchr(ZSTR_VAL(scope->name), '\\', class_name_len))) {
2147			++lc_class_name;
2148			class_name_len -= (lc_class_name - ZSTR_VAL(scope->name));
2149			lc_class_name = zend_str_tolower_dup(lc_class_name, class_name_len);
2150		} else {
2151			lc_class_name = zend_str_tolower_dup(ZSTR_VAL(scope->name), class_name_len);
2152		}
2153	}
2154
2155	while (ptr->fname) {
2156		fname_len = strlen(ptr->fname);
2157		internal_function->handler = ptr->handler;
2158		internal_function->function_name = zend_new_interned_string(zend_string_init(ptr->fname, fname_len, 1));
2159		internal_function->scope = scope;
2160		internal_function->prototype = NULL;
2161		if (ptr->flags) {
2162			if (!(ptr->flags & ZEND_ACC_PPP_MASK)) {
2163				if (ptr->flags != ZEND_ACC_DEPRECATED || scope) {
2164					zend_error(error_type, "Invalid access level for %s%s%s() - access must be exactly one of public, protected or private", scope ? ZSTR_VAL(scope->name) : "", scope ? "::" : "", ptr->fname);
2165				}
2166				internal_function->fn_flags = ZEND_ACC_PUBLIC | ptr->flags;
2167			} else {
2168				internal_function->fn_flags = ptr->flags;
2169			}
2170		} else {
2171			internal_function->fn_flags = ZEND_ACC_PUBLIC;
2172		}
2173		if (ptr->arg_info) {
2174			zend_internal_function_info *info = (zend_internal_function_info*)ptr->arg_info;
2175
2176			internal_function->arg_info = (zend_internal_arg_info*)ptr->arg_info+1;
2177			internal_function->num_args = ptr->num_args;
2178			/* Currently you cannot denote that the function can accept less arguments than num_args */
2179			if (info->required_num_args == (zend_uintptr_t)-1) {
2180				internal_function->required_num_args = ptr->num_args;
2181			} else {
2182				internal_function->required_num_args = info->required_num_args;
2183			}
2184			if (info->return_reference) {
2185				internal_function->fn_flags |= ZEND_ACC_RETURN_REFERENCE;
2186			}
2187			if (ptr->arg_info[ptr->num_args].is_variadic) {
2188				internal_function->fn_flags |= ZEND_ACC_VARIADIC;
2189				/* Don't count the variadic argument */
2190				internal_function->num_args--;
2191			}
2192			if (info->type_hint) {
2193				if (info->class_name) {
2194					ZEND_ASSERT(info->type_hint == IS_OBJECT);
2195					if (!scope && (!strcasecmp(info->class_name, "self") || !strcasecmp(info->class_name, "parent"))) {
2196						zend_error_noreturn(E_CORE_ERROR, "Cannot declare a return type of %s outside of a class scope", info->class_name);
2197					}
2198				}
2199
2200				internal_function->fn_flags |= ZEND_ACC_HAS_RETURN_TYPE;
2201			}
2202		} else {
2203			internal_function->arg_info = NULL;
2204			internal_function->num_args = 0;
2205			internal_function->required_num_args = 0;
2206		}
2207		zend_set_function_arg_flags((zend_function*)internal_function);
2208		if (ptr->flags & ZEND_ACC_ABSTRACT) {
2209			if (scope) {
2210				/* This is a class that must be abstract itself. Here we set the check info. */
2211				scope->ce_flags |= ZEND_ACC_IMPLICIT_ABSTRACT_CLASS;
2212				if (!(scope->ce_flags & ZEND_ACC_INTERFACE)) {
2213					/* Since the class is not an interface it needs to be declared as a abstract class. */
2214					/* Since here we are handling internal functions only we can add the keyword flag. */
2215					/* This time we set the flag for the keyword 'abstract'. */
2216					scope->ce_flags |= ZEND_ACC_EXPLICIT_ABSTRACT_CLASS;
2217				}
2218			}
2219			if (ptr->flags & ZEND_ACC_STATIC && (!scope || !(scope->ce_flags & ZEND_ACC_INTERFACE))) {
2220				zend_error(error_type, "Static function %s%s%s() cannot be abstract", scope ? ZSTR_VAL(scope->name) : "", scope ? "::" : "", ptr->fname);
2221			}
2222		} else {
2223			if (scope && (scope->ce_flags & ZEND_ACC_INTERFACE)) {
2224				efree((char*)lc_class_name);
2225				zend_error(error_type, "Interface %s cannot contain non abstract method %s()", ZSTR_VAL(scope->name), ptr->fname);
2226				return FAILURE;
2227			}
2228			if (!internal_function->handler) {
2229				if (scope) {
2230					efree((char*)lc_class_name);
2231				}
2232				zend_error(error_type, "Method %s%s%s() cannot be a NULL function", scope ? ZSTR_VAL(scope->name) : "", scope ? "::" : "", ptr->fname);
2233				zend_unregister_functions(functions, count, target_function_table);
2234				return FAILURE;
2235			}
2236		}
2237		lowercase_name = zend_string_alloc(fname_len, 1);
2238		zend_str_tolower_copy(ZSTR_VAL(lowercase_name), ptr->fname, fname_len);
2239		lowercase_name = zend_new_interned_string(lowercase_name);
2240		reg_function = malloc(sizeof(zend_internal_function));
2241		memcpy(reg_function, &function, sizeof(zend_internal_function));
2242		if (zend_hash_add_ptr(target_function_table, lowercase_name, reg_function) == NULL) {
2243			unload=1;
2244			free(reg_function);
2245			zend_string_release(lowercase_name);
2246			break;
2247		}
2248
2249		/* If types of arguments have to be checked */
2250		if (reg_function->common.arg_info && reg_function->common.num_args) {
2251			uint32_t i;
2252			for (i = 0; i < reg_function->common.num_args; i++) {
2253				if (reg_function->common.arg_info[i].class_name ||
2254				    reg_function->common.arg_info[i].type_hint) {
2255				    reg_function->common.fn_flags |= ZEND_ACC_HAS_TYPE_HINTS;
2256					break;
2257				}
2258			}
2259		}
2260
2261		if (scope) {
2262			/* Look for ctor, dtor, clone
2263			 * If it's an old-style constructor, store it only if we don't have
2264			 * a constructor already.
2265			 */
2266			if ((fname_len == class_name_len) && !ctor && !memcmp(ZSTR_VAL(lowercase_name), lc_class_name, class_name_len+1)) {
2267				ctor = reg_function;
2268			} else if (zend_string_equals_literal(lowercase_name, ZEND_CONSTRUCTOR_FUNC_NAME)) {
2269				ctor = reg_function;
2270			} else if (zend_string_equals_literal(lowercase_name, ZEND_DESTRUCTOR_FUNC_NAME)) {
2271				dtor = reg_function;
2272				if (internal_function->num_args) {
2273					zend_error(error_type, "Destructor %s::%s() cannot take arguments", ZSTR_VAL(scope->name), ptr->fname);
2274				}
2275			} else if (zend_string_equals_literal(lowercase_name, ZEND_CLONE_FUNC_NAME)) {
2276				clone = reg_function;
2277			} else if (zend_string_equals_literal(lowercase_name, ZEND_CALL_FUNC_NAME)) {
2278				__call = reg_function;
2279			} else if (zend_string_equals_literal(lowercase_name, ZEND_CALLSTATIC_FUNC_NAME)) {
2280				__callstatic = reg_function;
2281			} else if (zend_string_equals_literal(lowercase_name, ZEND_TOSTRING_FUNC_NAME)) {
2282				__tostring = reg_function;
2283			} else if (zend_string_equals_literal(lowercase_name, ZEND_GET_FUNC_NAME)) {
2284				__get = reg_function;
2285				scope->ce_flags |= ZEND_ACC_USE_GUARDS;
2286			} else if (zend_string_equals_literal(lowercase_name, ZEND_SET_FUNC_NAME)) {
2287				__set = reg_function;
2288				scope->ce_flags |= ZEND_ACC_USE_GUARDS;
2289			} else if (zend_string_equals_literal(lowercase_name, ZEND_UNSET_FUNC_NAME)) {
2290				__unset = reg_function;
2291				scope->ce_flags |= ZEND_ACC_USE_GUARDS;
2292			} else if (zend_string_equals_literal(lowercase_name, ZEND_ISSET_FUNC_NAME)) {
2293				__isset = reg_function;
2294				scope->ce_flags |= ZEND_ACC_USE_GUARDS;
2295			} else if (zend_string_equals_literal(lowercase_name, ZEND_DEBUGINFO_FUNC_NAME)) {
2296				__debugInfo = reg_function;
2297			} else {
2298				reg_function = NULL;
2299			}
2300			if (reg_function) {
2301				zend_check_magic_method_implementation(scope, reg_function, error_type);
2302			}
2303		}
2304		ptr++;
2305		count++;
2306		zend_string_release(lowercase_name);
2307	}
2308	if (unload) { /* before unloading, display all remaining bad function in the module */
2309		if (scope) {
2310			efree((char*)lc_class_name);
2311		}
2312		while (ptr->fname) {
2313			fname_len = strlen(ptr->fname);
2314			lowercase_name = zend_string_alloc(fname_len, 0);
2315			zend_str_tolower_copy(ZSTR_VAL(lowercase_name), ptr->fname, fname_len);
2316			if (zend_hash_exists(target_function_table, lowercase_name)) {
2317				zend_error(error_type, "Function registration failed - duplicate name - %s%s%s", scope ? ZSTR_VAL(scope->name) : "", scope ? "::" : "", ptr->fname);
2318			}
2319			zend_string_free(lowercase_name);
2320			ptr++;
2321		}
2322		zend_unregister_functions(functions, count, target_function_table);
2323		return FAILURE;
2324	}
2325	if (scope) {
2326		scope->constructor = ctor;
2327		scope->destructor = dtor;
2328		scope->clone = clone;
2329		scope->__call = __call;
2330		scope->__callstatic = __callstatic;
2331		scope->__tostring = __tostring;
2332		scope->__get = __get;
2333		scope->__set = __set;
2334		scope->__unset = __unset;
2335		scope->__isset = __isset;
2336		scope->__debugInfo = __debugInfo;
2337		if (ctor) {
2338			ctor->common.fn_flags |= ZEND_ACC_CTOR;
2339			if (ctor->common.fn_flags & ZEND_ACC_STATIC) {
2340				zend_error(error_type, "Constructor %s::%s() cannot be static", ZSTR_VAL(scope->name), ZSTR_VAL(ctor->common.function_name));
2341			}
2342			ctor->common.fn_flags &= ~ZEND_ACC_ALLOW_STATIC;
2343		}
2344		if (dtor) {
2345			dtor->common.fn_flags |= ZEND_ACC_DTOR;
2346			if (dtor->common.fn_flags & ZEND_ACC_STATIC) {
2347				zend_error(error_type, "Destructor %s::%s() cannot be static", ZSTR_VAL(scope->name), ZSTR_VAL(dtor->common.function_name));
2348			}
2349			dtor->common.fn_flags &= ~ZEND_ACC_ALLOW_STATIC;
2350		}
2351		if (clone) {
2352			clone->common.fn_flags |= ZEND_ACC_CLONE;
2353			if (clone->common.fn_flags & ZEND_ACC_STATIC) {
2354				zend_error(error_type, "Constructor %s::%s() cannot be static", ZSTR_VAL(scope->name), ZSTR_VAL(clone->common.function_name));
2355			}
2356			clone->common.fn_flags &= ~ZEND_ACC_ALLOW_STATIC;
2357		}
2358		if (__call) {
2359			if (__call->common.fn_flags & ZEND_ACC_STATIC) {
2360				zend_error(error_type, "Method %s::%s() cannot be static", ZSTR_VAL(scope->name), ZSTR_VAL(__call->common.function_name));
2361			}
2362			__call->common.fn_flags &= ~ZEND_ACC_ALLOW_STATIC;
2363		}
2364		if (__callstatic) {
2365			if (!(__callstatic->common.fn_flags & ZEND_ACC_STATIC)) {
2366				zend_error(error_type, "Method %s::%s() must be static", ZSTR_VAL(scope->name), ZSTR_VAL(__callstatic->common.function_name));
2367			}
2368			__callstatic->common.fn_flags |= ZEND_ACC_STATIC;
2369		}
2370		if (__tostring) {
2371			if (__tostring->common.fn_flags & ZEND_ACC_STATIC) {
2372				zend_error(error_type, "Method %s::%s() cannot be static", ZSTR_VAL(scope->name), ZSTR_VAL(__tostring->common.function_name));
2373			}
2374			__tostring->common.fn_flags &= ~ZEND_ACC_ALLOW_STATIC;
2375		}
2376		if (__get) {
2377			if (__get->common.fn_flags & ZEND_ACC_STATIC) {
2378				zend_error(error_type, "Method %s::%s() cannot be static", ZSTR_VAL(scope->name), ZSTR_VAL(__get->common.function_name));
2379			}
2380			__get->common.fn_flags &= ~ZEND_ACC_ALLOW_STATIC;
2381		}
2382		if (__set) {
2383			if (__set->common.fn_flags & ZEND_ACC_STATIC) {
2384				zend_error(error_type, "Method %s::%s() cannot be static", ZSTR_VAL(scope->name), ZSTR_VAL(__set->common.function_name));
2385			}
2386			__set->common.fn_flags &= ~ZEND_ACC_ALLOW_STATIC;
2387		}
2388		if (__unset) {
2389			if (__unset->common.fn_flags & ZEND_ACC_STATIC) {
2390				zend_error(error_type, "Method %s::%s() cannot be static", ZSTR_VAL(scope->name), ZSTR_VAL(__unset->common.function_name));
2391			}
2392			__unset->common.fn_flags &= ~ZEND_ACC_ALLOW_STATIC;
2393		}
2394		if (__isset) {
2395			if (__isset->common.fn_flags & ZEND_ACC_STATIC) {
2396				zend_error(error_type, "Method %s::%s() cannot be static", ZSTR_VAL(scope->name), ZSTR_VAL(__isset->common.function_name));
2397			}
2398			__isset->common.fn_flags &= ~ZEND_ACC_ALLOW_STATIC;
2399		}
2400		if (__debugInfo) {
2401			if (__debugInfo->common.fn_flags & ZEND_ACC_STATIC) {
2402				zend_error(error_type, "Method %s::%s() cannot be static", ZSTR_VAL(scope->name), ZSTR_VAL(__debugInfo->common.function_name));
2403			}
2404		}
2405
2406		if (ctor && ctor->common.fn_flags & ZEND_ACC_HAS_RETURN_TYPE && ctor->common.fn_flags & ZEND_ACC_CTOR) {
2407			zend_error_noreturn(E_CORE_ERROR, "Constructor %s::%s() cannot declare a return type", ZSTR_VAL(scope->name), ZSTR_VAL(ctor->common.function_name));
2408		}
2409
2410		if (dtor && dtor->common.fn_flags & ZEND_ACC_HAS_RETURN_TYPE && dtor->common.fn_flags & ZEND_ACC_DTOR) {
2411			zend_error_noreturn(E_CORE_ERROR, "Destructor %s::%s() cannot declare a return type", ZSTR_VAL(scope->name), ZSTR_VAL(dtor->common.function_name));
2412		}
2413
2414		if (clone && clone->common.fn_flags & ZEND_ACC_HAS_RETURN_TYPE && dtor->common.fn_flags & ZEND_ACC_DTOR) {
2415			zend_error_noreturn(E_CORE_ERROR, "%s::%s() cannot declare a return type", ZSTR_VAL(scope->name), ZSTR_VAL(clone->common.function_name));
2416		}
2417		efree((char*)lc_class_name);
2418	}
2419	return SUCCESS;
2420}
2421/* }}} */
2422
2423/* count=-1 means erase all functions, otherwise,
2424 * erase the first count functions
2425 */
2426ZEND_API void zend_unregister_functions(const zend_function_entry *functions, int count, HashTable *function_table) /* {{{ */
2427{
2428	const zend_function_entry *ptr = functions;
2429	int i=0;
2430	HashTable *target_function_table = function_table;
2431	zend_string *lowercase_name;
2432	size_t fname_len;
2433
2434	if (!target_function_table) {
2435		target_function_table = CG(function_table);
2436	}
2437	while (ptr->fname) {
2438		if (count!=-1 && i>=count) {
2439			break;
2440		}
2441		fname_len = strlen(ptr->fname);
2442		lowercase_name = zend_string_alloc(fname_len, 0);
2443		zend_str_tolower_copy(ZSTR_VAL(lowercase_name), ptr->fname, fname_len);
2444		zend_hash_del(target_function_table, lowercase_name);
2445		zend_string_free(lowercase_name);
2446		ptr++;
2447		i++;
2448	}
2449}
2450/* }}} */
2451
2452ZEND_API int zend_startup_module(zend_module_entry *module) /* {{{ */
2453{
2454	if ((module = zend_register_internal_module(module)) != NULL && zend_startup_module_ex(module) == SUCCESS) {
2455		return SUCCESS;
2456	}
2457	return FAILURE;
2458}
2459/* }}} */
2460
2461ZEND_API int zend_get_module_started(const char *module_name) /* {{{ */
2462{
2463	zend_module_entry *module;
2464
2465	module = zend_hash_str_find_ptr(&module_registry, module_name, strlen(module_name));
2466	return (module && module->module_started) ? SUCCESS : FAILURE;
2467}
2468/* }}} */
2469
2470static int clean_module_class(zval *el, void *arg) /* {{{ */
2471{
2472	zend_class_entry *ce = (zend_class_entry *)Z_PTR_P(el);
2473	int module_number = *(int *)arg;
2474	if (ce->type == ZEND_INTERNAL_CLASS && ce->info.internal.module->module_number == module_number) {
2475		return ZEND_HASH_APPLY_REMOVE;
2476	} else {
2477		return ZEND_HASH_APPLY_KEEP;
2478	}
2479}
2480/* }}} */
2481
2482static void clean_module_classes(int module_number) /* {{{ */
2483{
2484	zend_hash_apply_with_argument(EG(class_table), clean_module_class, (void *) &module_number);
2485}
2486/* }}} */
2487
2488void module_destructor(zend_module_entry *module) /* {{{ */
2489{
2490
2491	if (module->type == MODULE_TEMPORARY) {
2492		zend_clean_module_rsrc_dtors(module->module_number);
2493		clean_module_constants(module->module_number);
2494		clean_module_classes(module->module_number);
2495	}
2496
2497	if (module->module_started && module->module_shutdown_func) {
2498#if 0
2499		zend_printf("%s: Module shutdown\n", module->name);
2500#endif
2501		module->module_shutdown_func(module->type, module->module_number);
2502	}
2503
2504	/* Deinitilaise module globals */
2505	if (module->globals_size) {
2506#ifdef ZTS
2507		if (*module->globals_id_ptr) {
2508			ts_free_id(*module->globals_id_ptr);
2509		}
2510#else
2511		if (module->globals_dtor) {
2512			module->globals_dtor(module->globals_ptr);
2513		}
2514#endif
2515	}
2516
2517	module->module_started=0;
2518	if (module->functions) {
2519		zend_unregister_functions(module->functions, -1, NULL);
2520	}
2521
2522#if HAVE_LIBDL
2523#if !(defined(NETWARE) && defined(APACHE_1_BUILD))
2524	if (module->handle && !getenv("ZEND_DONT_UNLOAD_MODULES")) {
2525		DL_UNLOAD(module->handle);
2526	}
2527#endif
2528#endif
2529}
2530/* }}} */
2531
2532ZEND_API void zend_activate_modules(void) /* {{{ */
2533{
2534	zend_module_entry **p = module_request_startup_handlers;
2535
2536	while (*p) {
2537		zend_module_entry *module = *p;
2538
2539		if (module->request_startup_func(module->type, module->module_number)==FAILURE) {
2540			zend_error(E_WARNING, "request_startup() for %s module failed", module->name);
2541			exit(1);
2542		}
2543		p++;
2544	}
2545}
2546/* }}} */
2547
2548/* call request shutdown for all modules */
2549static int module_registry_cleanup(zval *zv) /* {{{ */
2550{
2551	zend_module_entry *module = Z_PTR_P(zv);
2552
2553	if (module->request_shutdown_func) {
2554#if 0
2555		zend_printf("%s: Request shutdown\n", module->name);
2556#endif
2557		module->request_shutdown_func(module->type, module->module_number);
2558	}
2559	return 0;
2560}
2561/* }}} */
2562
2563ZEND_API void zend_deactivate_modules(void) /* {{{ */
2564{
2565	EG(current_execute_data) = NULL; /* we're no longer executing anything */
2566
2567	zend_try {
2568		if (EG(full_tables_cleanup)) {
2569			zend_hash_reverse_apply(&module_registry, module_registry_cleanup);
2570		} else {
2571			zend_module_entry **p = module_request_shutdown_handlers;
2572
2573			while (*p) {
2574				zend_module_entry *module = *p;
2575
2576				module->request_shutdown_func(module->type, module->module_number);
2577				p++;
2578			}
2579		}
2580	} zend_end_try();
2581}
2582/* }}} */
2583
2584ZEND_API void zend_cleanup_internal_classes(void) /* {{{ */
2585{
2586	zend_class_entry **p = class_cleanup_handlers;
2587
2588	while (*p) {
2589		zend_cleanup_internal_class_data(*p);
2590		p++;
2591	}
2592}
2593/* }}} */
2594
2595int module_registry_unload_temp(const zend_module_entry *module) /* {{{ */
2596{
2597	return (module->type == MODULE_TEMPORARY) ? ZEND_HASH_APPLY_REMOVE : ZEND_HASH_APPLY_STOP;
2598}
2599/* }}} */
2600
2601static int module_registry_unload_temp_wrapper(zval *el) /* {{{ */
2602{
2603	zend_module_entry *module = (zend_module_entry *)Z_PTR_P(el);
2604	return module_registry_unload_temp((const zend_module_entry *)module);
2605}
2606/* }}} */
2607
2608static int exec_done_cb(zval *el) /* {{{ */
2609{
2610	zend_module_entry *module = (zend_module_entry *)Z_PTR_P(el);
2611	if (module->post_deactivate_func) {
2612		module->post_deactivate_func();
2613	}
2614	return 0;
2615}
2616/* }}} */
2617
2618ZEND_API void zend_post_deactivate_modules(void) /* {{{ */
2619{
2620	if (EG(full_tables_cleanup)) {
2621		zend_hash_apply(&module_registry, exec_done_cb);
2622		zend_hash_reverse_apply(&module_registry, module_registry_unload_temp_wrapper);
2623	} else {
2624		zend_module_entry **p = module_post_deactivate_handlers;
2625
2626		while (*p) {
2627			zend_module_entry *module = *p;
2628
2629			module->post_deactivate_func();
2630			p++;
2631		}
2632	}
2633}
2634/* }}} */
2635
2636/* return the next free module number */
2637ZEND_API int zend_next_free_module(void) /* {{{ */
2638{
2639	return zend_hash_num_elements(&module_registry) + 1;
2640}
2641/* }}} */
2642
2643static zend_class_entry *do_register_internal_class(zend_class_entry *orig_class_entry, uint32_t ce_flags) /* {{{ */
2644{
2645	zend_class_entry *class_entry = malloc(sizeof(zend_class_entry));
2646	zend_string *lowercase_name = zend_string_alloc(ZSTR_LEN(orig_class_entry->name), 1);
2647	*class_entry = *orig_class_entry;
2648
2649	class_entry->type = ZEND_INTERNAL_CLASS;
2650	zend_initialize_class_data(class_entry, 0);
2651	class_entry->ce_flags = ce_flags | ZEND_ACC_CONSTANTS_UPDATED;
2652	class_entry->info.internal.module = EG(current_module);
2653
2654	if (class_entry->info.internal.builtin_functions) {
2655		zend_register_functions(class_entry, class_entry->info.internal.builtin_functions, &class_entry->function_table, MODULE_PERSISTENT);
2656	}
2657
2658	zend_str_tolower_copy(ZSTR_VAL(lowercase_name), ZSTR_VAL(orig_class_entry->name), ZSTR_LEN(class_entry->name));
2659	lowercase_name = zend_new_interned_string(lowercase_name);
2660	zend_hash_update_ptr(CG(class_table), lowercase_name, class_entry);
2661	zend_string_release(lowercase_name);
2662	return class_entry;
2663}
2664/* }}} */
2665
2666/* If parent_ce is not NULL then it inherits from parent_ce
2667 * If parent_ce is NULL and parent_name isn't then it looks for the parent and inherits from it
2668 * If both parent_ce and parent_name are NULL it does a regular class registration
2669 * If parent_name is specified but not found NULL is returned
2670 */
2671ZEND_API zend_class_entry *zend_register_internal_class_ex(zend_class_entry *class_entry, zend_class_entry *parent_ce) /* {{{ */
2672{
2673	zend_class_entry *register_class;
2674
2675	register_class = zend_register_internal_class(class_entry);
2676
2677	if (parent_ce) {
2678		zend_do_inheritance(register_class, parent_ce);
2679	}
2680	return register_class;
2681}
2682/* }}} */
2683
2684ZEND_API void zend_class_implements(zend_class_entry *class_entry, int num_interfaces, ...) /* {{{ */
2685{
2686	zend_class_entry *interface_entry;
2687	va_list interface_list;
2688	va_start(interface_list, num_interfaces);
2689
2690	while (num_interfaces--) {
2691		interface_entry = va_arg(interface_list, zend_class_entry *);
2692		zend_do_implement_interface(class_entry, interface_entry);
2693	}
2694
2695	va_end(interface_list);
2696}
2697/* }}} */
2698
2699/* A class that contains at least one abstract method automatically becomes an abstract class.
2700 */
2701ZEND_API zend_class_entry *zend_register_internal_class(zend_class_entry *orig_class_entry) /* {{{ */
2702{
2703	return do_register_internal_class(orig_class_entry, 0);
2704}
2705/* }}} */
2706
2707ZEND_API zend_class_entry *zend_register_internal_interface(zend_class_entry *orig_class_entry) /* {{{ */
2708{
2709	return do_register_internal_class(orig_class_entry, ZEND_ACC_INTERFACE);
2710}
2711/* }}} */
2712
2713ZEND_API int zend_register_class_alias_ex(const char *name, size_t name_len, zend_class_entry *ce) /* {{{ */
2714{
2715	zend_string *lcname;
2716
2717	if (name[0] == '\\') {
2718		lcname = zend_string_alloc(name_len-1, 1);
2719		zend_str_tolower_copy(ZSTR_VAL(lcname), name+1, name_len-1);
2720	} else {
2721		lcname = zend_string_alloc(name_len, 1);
2722		zend_str_tolower_copy(ZSTR_VAL(lcname), name, name_len);
2723	}
2724
2725	zend_assert_valid_class_name(lcname);
2726
2727	ce = zend_hash_add_ptr(CG(class_table), lcname, ce);
2728	zend_string_release(lcname);
2729	if (ce) {
2730		ce->refcount++;
2731		return SUCCESS;
2732	}
2733	return FAILURE;
2734}
2735/* }}} */
2736
2737ZEND_API int zend_set_hash_symbol(zval *symbol, const char *name, int name_length, zend_bool is_ref, int num_symbol_tables, ...) /* {{{ */
2738{
2739	HashTable *symbol_table;
2740	va_list symbol_table_list;
2741
2742	if (num_symbol_tables <= 0) return FAILURE;
2743
2744	if (is_ref) {
2745		ZVAL_MAKE_REF(symbol);
2746	}
2747
2748	va_start(symbol_table_list, num_symbol_tables);
2749	while (num_symbol_tables-- > 0) {
2750		symbol_table = va_arg(symbol_table_list, HashTable *);
2751		zend_hash_str_update(symbol_table, name, name_length, symbol);
2752		if (Z_REFCOUNTED_P(symbol)) {
2753			Z_ADDREF_P(symbol);
2754		}
2755	}
2756	va_end(symbol_table_list);
2757	return SUCCESS;
2758}
2759/* }}} */
2760
2761/* Disabled functions support */
2762
2763/* {{{ proto void display_disabled_function(void)
2764Dummy function which displays an error when a disabled function is called. */
2765ZEND_API ZEND_FUNCTION(display_disabled_function)
2766{
2767	zend_error(E_WARNING, "%s() has been disabled for security reasons", get_active_function_name());
2768}
2769/* }}} */
2770
2771ZEND_API int zend_disable_function(char *function_name, size_t function_name_length) /* {{{ */
2772{
2773	zend_internal_function *func;
2774	if ((func = zend_hash_str_find_ptr(CG(function_table), function_name, function_name_length))) {
2775	    func->fn_flags &= ~(ZEND_ACC_VARIADIC | ZEND_ACC_HAS_TYPE_HINTS);
2776		func->num_args = 0;
2777		func->arg_info = NULL;
2778		func->handler = ZEND_FN(display_disabled_function);
2779		return SUCCESS;
2780	}
2781	return FAILURE;
2782}
2783/* }}} */
2784
2785#ifdef ZEND_WIN32
2786#pragma optimize("", off)
2787#endif
2788static zend_object *display_disabled_class(zend_class_entry *class_type) /* {{{ */
2789{
2790	zend_object *intern;
2791
2792	intern = zend_objects_new(class_type);
2793	zend_error(E_WARNING, "%s() has been disabled for security reasons", ZSTR_VAL(class_type->name));
2794	return intern;
2795}
2796#ifdef ZEND_WIN32
2797#pragma optimize("", on)
2798#endif
2799/* }}} */
2800
2801static const zend_function_entry disabled_class_new[] = {
2802	ZEND_FE_END
2803};
2804
2805ZEND_API int zend_disable_class(char *class_name, size_t class_name_length) /* {{{ */
2806{
2807	zend_class_entry *disabled_class;
2808	zend_string *key;
2809
2810	key = zend_string_alloc(class_name_length, 0);
2811	zend_str_tolower_copy(ZSTR_VAL(key), class_name, class_name_length);
2812	disabled_class = zend_hash_find_ptr(CG(class_table), key);
2813	if (!disabled_class) {
2814		return FAILURE;
2815	}
2816	INIT_CLASS_ENTRY_INIT_METHODS((*disabled_class), disabled_class_new, NULL, NULL, NULL, NULL, NULL);
2817	disabled_class->create_object = display_disabled_class;
2818	zend_hash_clean(&disabled_class->function_table);
2819	return SUCCESS;
2820}
2821/* }}} */
2822
2823static int zend_is_callable_check_class(zend_string *name, zend_fcall_info_cache *fcc, int *strict_class, char **error) /* {{{ */
2824{
2825	int ret = 0;
2826	zend_class_entry *ce;
2827	size_t name_len = ZSTR_LEN(name);
2828	zend_string *lcname;
2829	ALLOCA_FLAG(use_heap);
2830
2831	ZSTR_ALLOCA_ALLOC(lcname, name_len, use_heap);
2832	zend_str_tolower_copy(ZSTR_VAL(lcname), ZSTR_VAL(name), name_len);
2833
2834	*strict_class = 0;
2835	if (zend_string_equals_literal(lcname, "self")) {
2836		if (!EG(scope)) {
2837			if (error) *error = estrdup("cannot access self:: when no class scope is active");
2838		} else {
2839			fcc->called_scope = zend_get_called_scope(EG(current_execute_data));
2840			fcc->calling_scope = EG(scope);
2841			if (!fcc->object) {
2842				fcc->object = zend_get_this_object(EG(current_execute_data));
2843			}
2844			ret = 1;
2845		}
2846	} else if (zend_string_equals_literal(lcname, "parent")) {
2847		if (!EG(scope)) {
2848			if (error) *error = estrdup("cannot access parent:: when no class scope is active");
2849		} else if (!EG(scope)->parent) {
2850			if (error) *error = estrdup("cannot access parent:: when current class scope has no parent");
2851		} else {
2852			fcc->called_scope = zend_get_called_scope(EG(current_execute_data));
2853			fcc->calling_scope = EG(scope)->parent;
2854			if (!fcc->object) {
2855				fcc->object = zend_get_this_object(EG(current_execute_data));
2856			}
2857			*strict_class = 1;
2858			ret = 1;
2859		}
2860	} else if (zend_string_equals_literal(lcname, "static")) {
2861		zend_class_entry *called_scope = zend_get_called_scope(EG(current_execute_data));
2862
2863		if (!called_scope) {
2864			if (error) *error = estrdup("cannot access static:: when no class scope is active");
2865		} else {
2866			fcc->called_scope = called_scope;
2867			fcc->calling_scope = called_scope;
2868			if (!fcc->object) {
2869				fcc->object = zend_get_this_object(EG(current_execute_data));
2870			}
2871			*strict_class = 1;
2872			ret = 1;
2873		}
2874	} else if ((ce = zend_lookup_class_ex(name, NULL, 1)) != NULL) {
2875		zend_class_entry *scope;
2876		zend_execute_data *ex = EG(current_execute_data);
2877
2878		while (ex && (!ex->func || !ZEND_USER_CODE(ex->func->type))) {
2879			ex = ex->prev_execute_data;
2880		}
2881		scope = ex ? ex->func->common.scope : NULL;
2882		fcc->calling_scope = ce;
2883		if (scope && !fcc->object) {
2884			zend_object *object = zend_get_this_object(EG(current_execute_data));
2885
2886			if (object &&
2887			    instanceof_function(object->ce, scope) &&
2888			    instanceof_function(scope, fcc->calling_scope)) {
2889				fcc->object = object;
2890				fcc->called_scope = object->ce;
2891			} else {
2892				fcc->called_scope = fcc->calling_scope;
2893			}
2894		} else {
2895			fcc->called_scope = fcc->object ? fcc->object->ce : fcc->calling_scope;
2896		}
2897		*strict_class = 1;
2898		ret = 1;
2899	} else {
2900		if (error) zend_spprintf(error, 0, "class '%.*s' not found", name_len, ZSTR_VAL(name));
2901	}
2902	ZSTR_ALLOCA_FREE(lcname, use_heap);
2903	return ret;
2904}
2905/* }}} */
2906
2907static int zend_is_callable_check_func(int check_flags, zval *callable, zend_fcall_info_cache *fcc, int strict_class, char **error) /* {{{ */
2908{
2909	zend_class_entry *ce_org = fcc->calling_scope;
2910	int retval = 0;
2911	zend_string *mname, *cname;
2912	zend_string *lmname;
2913	const char *colon;
2914	size_t clen, mlen;
2915	zend_class_entry *last_scope;
2916	HashTable *ftable;
2917	int call_via_handler = 0;
2918	ALLOCA_FLAG(use_heap)
2919
2920	if (error) {
2921		*error = NULL;
2922	}
2923
2924	fcc->calling_scope = NULL;
2925	fcc->function_handler = NULL;
2926
2927	if (!ce_org) {
2928		zend_string *lmname;
2929
2930		/* Skip leading \ */
2931		if (UNEXPECTED(Z_STRVAL_P(callable)[0] == '\\')) {
2932			ZSTR_ALLOCA_INIT(lmname, Z_STRVAL_P(callable) + 1, Z_STRLEN_P(callable) - 1, use_heap);
2933		} else {
2934			lmname = Z_STR_P(callable);
2935		}
2936		/* Check if function with given name exists.
2937		 * This may be a compound name that includes namespace name */
2938		if (EXPECTED((fcc->function_handler = zend_hash_find_ptr(EG(function_table), lmname)) != NULL)) {
2939			if (lmname != Z_STR_P(callable)) {
2940				ZSTR_ALLOCA_FREE(lmname, use_heap);
2941			}
2942			fcc->initialized = 1;
2943			return 1;
2944		} else {
2945			if (lmname == Z_STR_P(callable)) {
2946				ZSTR_ALLOCA_INIT(lmname, Z_STRVAL_P(callable), Z_STRLEN_P(callable), use_heap);
2947			} else {
2948				zend_string_forget_hash_val(lmname);
2949			}
2950			zend_str_tolower(ZSTR_VAL(lmname), ZSTR_LEN(lmname));
2951			if ((fcc->function_handler = zend_hash_find_ptr(EG(function_table), lmname)) != NULL) {
2952				ZSTR_ALLOCA_FREE(lmname, use_heap);
2953				fcc->initialized = 1;
2954				return 1;
2955			}
2956		}
2957		if (lmname != Z_STR_P(callable)) {
2958			ZSTR_ALLOCA_FREE(lmname, use_heap);
2959		}
2960	}
2961
2962	/* Split name into class/namespace and method/function names */
2963	if ((colon = zend_memrchr(Z_STRVAL_P(callable), ':', Z_STRLEN_P(callable))) != NULL &&
2964		colon > Z_STRVAL_P(callable) &&
2965		*(colon-1) == ':'
2966	) {
2967		colon--;
2968		clen = colon - Z_STRVAL_P(callable);
2969		mlen = Z_STRLEN_P(callable) - clen - 2;
2970
2971		if (colon == Z_STRVAL_P(callable)) {
2972			if (error) zend_spprintf(error, 0, "invalid function name");
2973			return 0;
2974		}
2975
2976		/* This is a compound name.
2977		 * Try to fetch class and then find static method. */
2978		last_scope = EG(scope);
2979		if (ce_org) {
2980			EG(scope) = ce_org;
2981		}
2982
2983		cname = zend_string_init(Z_STRVAL_P(callable), clen, 0);
2984		if (!zend_is_callable_check_class(cname, fcc, &strict_class, error)) {
2985			zend_string_release(cname);
2986			EG(scope) = last_scope;
2987			return 0;
2988		}
2989		zend_string_release(cname);
2990		EG(scope) = last_scope;
2991
2992		ftable = &fcc->calling_scope->function_table;
2993		if (ce_org && !instanceof_function(ce_org, fcc->calling_scope)) {
2994			if (error) zend_spprintf(error, 0, "class '%s' is not a subclass of '%s'", ZSTR_VAL(ce_org->name), ZSTR_VAL(fcc->calling_scope->name));
2995			return 0;
2996		}
2997		mname = zend_string_init(Z_STRVAL_P(callable) + clen + 2, mlen, 0);
2998	} else if (ce_org) {
2999		/* Try to fetch find static method of given class. */
3000		mlen = Z_STRLEN_P(callable);
3001		mname = Z_STR_P(callable);
3002		zend_string_addref(mname);
3003		ftable = &ce_org->function_table;
3004		fcc->calling_scope = ce_org;
3005	} else {
3006		/* We already checked for plain function before. */
3007		if (error && !(check_flags & IS_CALLABLE_CHECK_SILENT)) {
3008			zend_spprintf(error, 0, "function '%s' not found or invalid function name", Z_STRVAL_P(callable));
3009		}
3010		return 0;
3011	}
3012
3013	lmname = zend_string_tolower(mname);
3014	if (strict_class &&
3015	    fcc->calling_scope &&
3016		zend_string_equals_literal(lmname, ZEND_CONSTRUCTOR_FUNC_NAME)) {
3017		fcc->function_handler = fcc->calling_scope->constructor;
3018		if (fcc->function_handler) {
3019			retval = 1;
3020		}
3021	} else if ((fcc->function_handler = zend_hash_find_ptr(ftable, lmname)) != NULL) {
3022		retval = 1;
3023		if ((fcc->function_handler->op_array.fn_flags & ZEND_ACC_CHANGED) &&
3024		    !strict_class && EG(scope) &&
3025		    instanceof_function(fcc->function_handler->common.scope, EG(scope))) {
3026			zend_function *priv_fbc;
3027
3028			if ((priv_fbc = zend_hash_find_ptr(&EG(scope)->function_table, lmname)) != NULL
3029				&& priv_fbc->common.fn_flags & ZEND_ACC_PRIVATE
3030				&& priv_fbc->common.scope == EG(scope)) {
3031				fcc->function_handler = priv_fbc;
3032			}
3033		}
3034		if ((check_flags & IS_CALLABLE_CHECK_NO_ACCESS) == 0 &&
3035		    (fcc->calling_scope &&
3036		     ((fcc->object && fcc->calling_scope->__call) ||
3037		      (!fcc->object && fcc->calling_scope->__callstatic)))) {
3038			if (fcc->function_handler->op_array.fn_flags & ZEND_ACC_PRIVATE) {
3039				if (!zend_check_private(fcc->function_handler, fcc->object ? fcc->object->ce : EG(scope), lmname)) {
3040					retval = 0;
3041					fcc->function_handler = NULL;
3042					goto get_function_via_handler;
3043				}
3044			} else if (fcc->function_handler->common.fn_flags & ZEND_ACC_PROTECTED) {
3045				if (!zend_check_protected(fcc->function_handler->common.scope, EG(scope))) {
3046					retval = 0;
3047					fcc->function_handler = NULL;
3048					goto get_function_via_handler;
3049				}
3050			}
3051		}
3052	} else {
3053get_function_via_handler:
3054		if (fcc->object && fcc->calling_scope == ce_org) {
3055			if (strict_class && ce_org->__call) {
3056				fcc->function_handler = zend_get_call_trampoline_func(ce_org, mname, 0);
3057				call_via_handler = 1;
3058				retval = 1;
3059			} else if (fcc->object->handlers->get_method) {
3060				fcc->function_handler = fcc->object->handlers->get_method(&fcc->object, mname, NULL);
3061				if (fcc->function_handler) {
3062					if (strict_class &&
3063					    (!fcc->function_handler->common.scope ||
3064					     !instanceof_function(ce_org, fcc->function_handler->common.scope))) {
3065						if (fcc->function_handler->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE) {
3066							if (fcc->function_handler->type != ZEND_OVERLOADED_FUNCTION) {
3067								zend_string_release(fcc->function_handler->common.function_name);
3068							}
3069							zend_free_trampoline(fcc->function_handler);
3070						}
3071					} else {
3072						retval = 1;
3073						call_via_handler = (fcc->function_handler->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE) != 0;
3074					}
3075				}
3076			}
3077		} else if (fcc->calling_scope) {
3078			if (fcc->calling_scope->get_static_method) {
3079				fcc->function_handler = fcc->calling_scope->get_static_method(fcc->calling_scope, mname);
3080			} else {
3081				fcc->function_handler = zend_std_get_static_method(fcc->calling_scope, mname, NULL);
3082			}
3083			if (fcc->function_handler) {
3084				retval = 1;
3085				call_via_handler = (fcc->function_handler->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE) != 0;
3086				if (call_via_handler && !fcc->object) {
3087					zend_object *object = zend_get_this_object(EG(current_execute_data));
3088					if (object &&
3089					    instanceof_function(object->ce, fcc->calling_scope)) {
3090						fcc->object = object;
3091					}
3092				}
3093			}
3094		}
3095	}
3096
3097	if (retval) {
3098		if (fcc->calling_scope && !call_via_handler) {
3099			if (!fcc->object && (fcc->function_handler->common.fn_flags & ZEND_ACC_ABSTRACT)) {
3100				if (error) {
3101					zend_spprintf(error, 0, "cannot call abstract method %s::%s()", ZSTR_VAL(fcc->calling_scope->name), ZSTR_VAL(fcc->function_handler->common.function_name));
3102					retval = 0;
3103				} else {
3104					zend_throw_error(NULL, "Cannot call abstract method %s::%s()", ZSTR_VAL(fcc->calling_scope->name), ZSTR_VAL(fcc->function_handler->common.function_name));
3105					return 0;
3106				}
3107			} else if (!fcc->object && !(fcc->function_handler->common.fn_flags & ZEND_ACC_STATIC)) {
3108				int severity;
3109				char *verb;
3110				if (fcc->function_handler->common.fn_flags & ZEND_ACC_ALLOW_STATIC) {
3111					severity = E_DEPRECATED;
3112					verb = "should not";
3113				} else {
3114					/* An internal function assumes $this is present and won't check that. So PHP would crash by allowing the call. */
3115					severity = E_ERROR;
3116					verb = "cannot";
3117				}
3118				if ((check_flags & IS_CALLABLE_CHECK_IS_STATIC) != 0) {
3119					retval = 0;
3120				}
3121				if (error) {
3122					zend_spprintf(error, 0, "non-static method %s::%s() %s be called statically", ZSTR_VAL(fcc->calling_scope->name), ZSTR_VAL(fcc->function_handler->common.function_name), verb);
3123					if (severity != E_DEPRECATED) {
3124						retval = 0;
3125					}
3126				} else if (retval) {
3127					if (severity == E_ERROR) {
3128						zend_throw_error(NULL, "Non-static method %s::%s() %s be called statically", ZSTR_VAL(fcc->calling_scope->name), ZSTR_VAL(fcc->function_handler->common.function_name), verb);
3129					} else {
3130						zend_error(severity, "Non-static method %s::%s() %s be called statically", ZSTR_VAL(fcc->calling_scope->name), ZSTR_VAL(fcc->function_handler->common.function_name), verb);
3131					}
3132				}
3133			}
3134			if (retval && (check_flags & IS_CALLABLE_CHECK_NO_ACCESS) == 0) {
3135				if (fcc->function_handler->op_array.fn_flags & ZEND_ACC_PRIVATE) {
3136					if (!zend_check_private(fcc->function_handler, fcc->object ? fcc->object->ce : EG(scope), lmname)) {
3137						if (error) {
3138							if (*error) {
3139								efree(*error);
3140							}
3141							zend_spprintf(error, 0, "cannot access private method %s::%s()", ZSTR_VAL(fcc->calling_scope->name), ZSTR_VAL(fcc->function_handler->common.function_name));
3142						}
3143						retval = 0;
3144					}
3145				} else if ((fcc->function_handler->common.fn_flags & ZEND_ACC_PROTECTED)) {
3146					if (!zend_check_protected(fcc->function_handler->common.scope, EG(scope))) {
3147						if (error) {
3148							if (*error) {
3149								efree(*error);
3150							}
3151							zend_spprintf(error, 0, "cannot access protected method %s::%s()", ZSTR_VAL(fcc->calling_scope->name), ZSTR_VAL(fcc->function_handler->common.function_name));
3152						}
3153						retval = 0;
3154					}
3155				}
3156			}
3157		}
3158	} else if (error && !(check_flags & IS_CALLABLE_CHECK_SILENT)) {
3159		if (fcc->calling_scope) {
3160			if (error) zend_spprintf(error, 0, "class '%s' does not have a method '%s'", ZSTR_VAL(fcc->calling_scope->name), ZSTR_VAL(mname));
3161		} else {
3162			if (error) zend_spprintf(error, 0, "function '%s' does not exist", ZSTR_VAL(mname));
3163		}
3164	}
3165	zend_string_release(lmname);
3166	zend_string_release(mname);
3167
3168	if (fcc->object) {
3169		fcc->called_scope = fcc->object->ce;
3170	}
3171	if (retval) {
3172		fcc->initialized = 1;
3173	}
3174	return retval;
3175}
3176/* }}} */
3177
3178ZEND_API zend_bool zend_is_callable_ex(zval *callable, zend_object *object, uint check_flags, zend_string **callable_name, zend_fcall_info_cache *fcc, char **error) /* {{{ */
3179{
3180	zend_bool ret;
3181	zend_fcall_info_cache fcc_local;
3182
3183	if (callable_name) {
3184		*callable_name = NULL;
3185	}
3186	if (fcc == NULL) {
3187		fcc = &fcc_local;
3188	}
3189	if (error) {
3190		*error = NULL;
3191	}
3192
3193	fcc->initialized = 0;
3194	fcc->calling_scope = NULL;
3195	fcc->called_scope = NULL;
3196	fcc->function_handler = NULL;
3197	fcc->object = NULL;
3198
3199	if (object &&
3200	    (!EG(objects_store).object_buckets ||
3201	     !IS_OBJ_VALID(EG(objects_store).object_buckets[object->handle]))) {
3202		return 0;
3203	}
3204
3205again:
3206	switch (Z_TYPE_P(callable)) {
3207		case IS_STRING:
3208			if (object) {
3209				fcc->object = object;
3210				fcc->calling_scope = object->ce;
3211				if (callable_name) {
3212					char *ptr;
3213
3214					*callable_name = zend_string_alloc(ZSTR_LEN(fcc->calling_scope->name) + Z_STRLEN_P(callable) + sizeof("::") - 1, 0);
3215					ptr = ZSTR_VAL(*callable_name);
3216					memcpy(ptr, ZSTR_VAL(fcc->calling_scope->name), ZSTR_LEN(fcc->calling_scope->name));
3217					ptr += ZSTR_LEN(fcc->calling_scope->name);
3218					memcpy(ptr, "::", sizeof("::") - 1);
3219					ptr += sizeof("::") - 1;
3220					memcpy(ptr, Z_STRVAL_P(callable), Z_STRLEN_P(callable) + 1);
3221				}
3222			} else if (callable_name) {
3223				*callable_name = zend_string_copy(Z_STR_P(callable));
3224			}
3225			if (check_flags & IS_CALLABLE_CHECK_SYNTAX_ONLY) {
3226				fcc->called_scope = fcc->calling_scope;
3227				return 1;
3228			}
3229
3230			ret = zend_is_callable_check_func(check_flags, callable, fcc, 0, error);
3231			if (fcc == &fcc_local &&
3232			    fcc->function_handler &&
3233				((fcc->function_handler->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE) ||
3234			     fcc->function_handler->type == ZEND_OVERLOADED_FUNCTION_TEMPORARY ||
3235			     fcc->function_handler->type == ZEND_OVERLOADED_FUNCTION)) {
3236				if (fcc->function_handler->type != ZEND_OVERLOADED_FUNCTION) {
3237					zend_string_release(fcc->function_handler->common.function_name);
3238				}
3239				zend_free_trampoline(fcc->function_handler);
3240			}
3241			return ret;
3242
3243		case IS_ARRAY:
3244			{
3245				zval *method = NULL;
3246				zval *obj = NULL;
3247				int strict_class = 0;
3248
3249				if (zend_hash_num_elements(Z_ARRVAL_P(callable)) == 2) {
3250					obj = zend_hash_index_find(Z_ARRVAL_P(callable), 0);
3251					method = zend_hash_index_find(Z_ARRVAL_P(callable), 1);
3252				}
3253
3254				do {
3255					if (obj == NULL || method == NULL) {
3256						break;
3257					}
3258
3259					ZVAL_DEREF(method);
3260					if (Z_TYPE_P(method) != IS_STRING) {
3261						break;
3262					}
3263
3264					ZVAL_DEREF(obj);
3265					if (Z_TYPE_P(obj) == IS_STRING) {
3266						if (callable_name) {
3267							char *ptr;
3268
3269
3270							*callable_name = zend_string_alloc(Z_STRLEN_P(obj) + Z_STRLEN_P(method) + sizeof("::") - 1, 0);
3271							ptr = ZSTR_VAL(*callable_name);
3272							memcpy(ptr, Z_STRVAL_P(obj), Z_STRLEN_P(obj));
3273							ptr += Z_STRLEN_P(obj);
3274							memcpy(ptr, "::", sizeof("::") - 1);
3275							ptr += sizeof("::") - 1;
3276							memcpy(ptr, Z_STRVAL_P(method), Z_STRLEN_P(method) + 1);
3277						}
3278
3279						if (check_flags & IS_CALLABLE_CHECK_SYNTAX_ONLY) {
3280							return 1;
3281						}
3282
3283						if (!zend_is_callable_check_class(Z_STR_P(obj), fcc, &strict_class, error)) {
3284							return 0;
3285						}
3286
3287					} else if (Z_TYPE_P(obj) == IS_OBJECT) {
3288						if (!EG(objects_store).object_buckets ||
3289						    !IS_OBJ_VALID(EG(objects_store).object_buckets[Z_OBJ_HANDLE_P(obj)])) {
3290							return 0;
3291						}
3292
3293						fcc->calling_scope = Z_OBJCE_P(obj); /* TBFixed: what if it's overloaded? */
3294
3295						fcc->object = Z_OBJ_P(obj);
3296
3297						if (callable_name) {
3298							char *ptr;
3299
3300							*callable_name = zend_string_alloc(ZSTR_LEN(fcc->calling_scope->name) + Z_STRLEN_P(method) + sizeof("::") - 1, 0);
3301							ptr = ZSTR_VAL(*callable_name);
3302							memcpy(ptr, ZSTR_VAL(fcc->calling_scope->name), ZSTR_LEN(fcc->calling_scope->name));
3303							ptr += ZSTR_LEN(fcc->calling_scope->name);
3304							memcpy(ptr, "::", sizeof("::") - 1);
3305							ptr += sizeof("::") - 1;
3306							memcpy(ptr, Z_STRVAL_P(method), Z_STRLEN_P(method) + 1);
3307						}
3308
3309						if (check_flags & IS_CALLABLE_CHECK_SYNTAX_ONLY) {
3310							fcc->called_scope = fcc->calling_scope;
3311							return 1;
3312						}
3313					} else {
3314						break;
3315					}
3316
3317					ret = zend_is_callable_check_func(check_flags, method, fcc, strict_class, error);
3318					if (fcc == &fcc_local &&
3319					    fcc->function_handler &&
3320						((fcc->function_handler->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE) ||
3321					     fcc->function_handler->type == ZEND_OVERLOADED_FUNCTION_TEMPORARY ||
3322					     fcc->function_handler->type == ZEND_OVERLOADED_FUNCTION)) {
3323						if (fcc->function_handler->type != ZEND_OVERLOADED_FUNCTION) {
3324							zend_string_release(fcc->function_handler->common.function_name);
3325						}
3326						zend_free_trampoline(fcc->function_handler);
3327					}
3328					return ret;
3329
3330				} while (0);
3331				if (zend_hash_num_elements(Z_ARRVAL_P(callable)) == 2) {
3332					if (!obj || (!Z_ISREF_P(obj)?
3333								(Z_TYPE_P(obj) != IS_STRING && Z_TYPE_P(obj) != IS_OBJECT) :
3334								(Z_TYPE_P(Z_REFVAL_P(obj)) != IS_STRING && Z_TYPE_P(Z_REFVAL_P(obj)) != IS_OBJECT))) {
3335						if (error) zend_spprintf(error, 0, "first array member is not a valid class name or object");
3336					} else {
3337						if (error) zend_spprintf(error, 0, "second array member is not a valid method");
3338					}
3339				} else {
3340					if (error) zend_spprintf(error, 0, "array must have exactly two members");
3341				}
3342				if (callable_name) {
3343					*callable_name = zend_string_init("Array", sizeof("Array")-1, 0);
3344				}
3345			}
3346			return 0;
3347		case IS_OBJECT:
3348			if (Z_OBJ_HANDLER_P(callable, get_closure) && Z_OBJ_HANDLER_P(callable, get_closure)(callable, &fcc->calling_scope, &fcc->function_handler, &fcc->object) == SUCCESS) {
3349				fcc->called_scope = fcc->calling_scope;
3350				if (callable_name) {
3351					zend_class_entry *ce = Z_OBJCE_P(callable); /* TBFixed: what if it's overloaded? */
3352
3353					*callable_name = zend_string_alloc(ZSTR_LEN(ce->name) + sizeof("::__invoke") - 1, 0);
3354					memcpy(ZSTR_VAL(*callable_name), ZSTR_VAL(ce->name), ZSTR_LEN(ce->name));
3355					memcpy(ZSTR_VAL(*callable_name) + ZSTR_LEN(ce->name), "::__invoke", sizeof("::__invoke"));
3356				}
3357				return 1;
3358			}
3359			if (callable_name) {
3360				*callable_name = zval_get_string(callable);
3361			}
3362			if (error) zend_spprintf(error, 0, "no array or string given");
3363			return 0;
3364		case IS_REFERENCE:
3365			callable = Z_REFVAL_P(callable);
3366			goto again;
3367		default:
3368			if (callable_name) {
3369				*callable_name = zval_get_string(callable);
3370			}
3371			if (error) zend_spprintf(error, 0, "no array or string given");
3372			return 0;
3373	}
3374}
3375/* }}} */
3376
3377ZEND_API zend_bool zend_is_callable(zval *callable, uint check_flags, zend_string **callable_name) /* {{{ */
3378{
3379	return zend_is_callable_ex(callable, NULL, check_flags, callable_name, NULL, NULL);
3380}
3381/* }}} */
3382
3383ZEND_API zend_bool zend_make_callable(zval *callable, zend_string **callable_name) /* {{{ */
3384{
3385	zend_fcall_info_cache fcc;
3386
3387	if (zend_is_callable_ex(callable, NULL, IS_CALLABLE_STRICT, callable_name, &fcc, NULL)) {
3388		if (Z_TYPE_P(callable) == IS_STRING && fcc.calling_scope) {
3389			zval_dtor(callable);
3390			array_init(callable);
3391			add_next_index_str(callable, zend_string_copy(fcc.calling_scope->name));
3392			add_next_index_str(callable, zend_string_copy(fcc.function_handler->common.function_name));
3393		}
3394		if (fcc.function_handler &&
3395			((fcc.function_handler->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE) ||
3396		     fcc.function_handler->type == ZEND_OVERLOADED_FUNCTION_TEMPORARY ||
3397		     fcc.function_handler->type == ZEND_OVERLOADED_FUNCTION)) {
3398			if (fcc.function_handler->type != ZEND_OVERLOADED_FUNCTION) {
3399				zend_string_release(fcc.function_handler->common.function_name);
3400			}
3401			zend_free_trampoline(fcc.function_handler);
3402		}
3403		return 1;
3404	}
3405	return 0;
3406}
3407/* }}} */
3408
3409ZEND_API int zend_fcall_info_init(zval *callable, uint check_flags, zend_fcall_info *fci, zend_fcall_info_cache *fcc, zend_string **callable_name, char **error) /* {{{ */
3410{
3411	if (!zend_is_callable_ex(callable, NULL, check_flags, callable_name, fcc, error)) {
3412		return FAILURE;
3413	}
3414
3415	fci->size = sizeof(*fci);
3416	fci->function_table = fcc->calling_scope ? &fcc->calling_scope->function_table : EG(function_table);
3417	fci->object = fcc->object;
3418	ZVAL_COPY_VALUE(&fci->function_name, callable);
3419	fci->retval = NULL;
3420	fci->param_count = 0;
3421	fci->params = NULL;
3422	fci->no_separation = 1;
3423	fci->symbol_table = NULL;
3424
3425	return SUCCESS;
3426}
3427/* }}} */
3428
3429ZEND_API void zend_fcall_info_args_clear(zend_fcall_info *fci, int free_mem) /* {{{ */
3430{
3431	if (fci->params) {
3432		zval *p = fci->params;
3433		zval *end = p + fci->param_count;
3434
3435		while (p != end) {
3436			i_zval_ptr_dtor(p ZEND_FILE_LINE_CC);
3437			p++;
3438		}
3439		if (free_mem) {
3440			efree(fci->params);
3441			fci->params = NULL;
3442		}
3443	}
3444	fci->param_count = 0;
3445}
3446/* }}} */
3447
3448ZEND_API void zend_fcall_info_args_save(zend_fcall_info *fci, int *param_count, zval **params) /* {{{ */
3449{
3450	*param_count = fci->param_count;
3451	*params = fci->params;
3452	fci->param_count = 0;
3453	fci->params = NULL;
3454}
3455/* }}} */
3456
3457ZEND_API void zend_fcall_info_args_restore(zend_fcall_info *fci, int param_count, zval *params) /* {{{ */
3458{
3459	zend_fcall_info_args_clear(fci, 1);
3460	fci->param_count = param_count;
3461	fci->params = params;
3462}
3463/* }}} */
3464
3465ZEND_API int zend_fcall_info_args_ex(zend_fcall_info *fci, zend_function *func, zval *args) /* {{{ */
3466{
3467	zval *arg, *params;
3468	uint32_t n = 1;
3469
3470	zend_fcall_info_args_clear(fci, !args);
3471
3472	if (!args) {
3473		return SUCCESS;
3474	}
3475
3476	if (Z_TYPE_P(args) != IS_ARRAY) {
3477		return FAILURE;
3478	}
3479
3480	fci->param_count = zend_hash_num_elements(Z_ARRVAL_P(args));
3481	fci->params = params = (zval *) erealloc(fci->params, fci->param_count * sizeof(zval));
3482
3483	ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(args), arg) {
3484		if (func && !Z_ISREF_P(arg) && ARG_SHOULD_BE_SENT_BY_REF(func, n)) {
3485			ZVAL_NEW_REF(params, arg);
3486			if (Z_REFCOUNTED_P(arg)) {
3487				Z_ADDREF_P(arg);
3488			}
3489		} else {
3490			ZVAL_COPY(params, arg);
3491		}
3492		params++;
3493		n++;
3494	} ZEND_HASH_FOREACH_END();
3495
3496	return SUCCESS;
3497}
3498/* }}} */
3499
3500ZEND_API int zend_fcall_info_args(zend_fcall_info *fci, zval *args) /* {{{ */
3501{
3502	return zend_fcall_info_args_ex(fci, NULL, args);
3503}
3504/* }}} */
3505
3506ZEND_API int zend_fcall_info_argp(zend_fcall_info *fci, int argc, zval *argv) /* {{{ */
3507{
3508	int i;
3509
3510	if (argc < 0) {
3511		return FAILURE;
3512	}
3513
3514	zend_fcall_info_args_clear(fci, !argc);
3515
3516	if (argc) {
3517		fci->param_count = argc;
3518		fci->params = (zval *) erealloc(fci->params, fci->param_count * sizeof(zval));
3519
3520		for (i = 0; i < argc; ++i) {
3521			ZVAL_COPY(&fci->params[i], &argv[i]);
3522		}
3523	}
3524
3525	return SUCCESS;
3526}
3527/* }}} */
3528
3529ZEND_API int zend_fcall_info_argv(zend_fcall_info *fci, int argc, va_list *argv) /* {{{ */
3530{
3531	int i;
3532	zval *arg;
3533
3534	if (argc < 0) {
3535		return FAILURE;
3536	}
3537
3538	zend_fcall_info_args_clear(fci, !argc);
3539
3540	if (argc) {
3541		fci->param_count = argc;
3542		fci->params = (zval *) erealloc(fci->params, fci->param_count * sizeof(zval));
3543
3544		for (i = 0; i < argc; ++i) {
3545			arg = va_arg(*argv, zval *);
3546			ZVAL_COPY(&fci->params[i], arg);
3547		}
3548	}
3549
3550	return SUCCESS;
3551}
3552/* }}} */
3553
3554ZEND_API int zend_fcall_info_argn(zend_fcall_info *fci, int argc, ...) /* {{{ */
3555{
3556	int ret;
3557	va_list argv;
3558
3559	va_start(argv, argc);
3560	ret = zend_fcall_info_argv(fci, argc, &argv);
3561	va_end(argv);
3562
3563	return ret;
3564}
3565/* }}} */
3566
3567ZEND_API int zend_fcall_info_call(zend_fcall_info *fci, zend_fcall_info_cache *fcc, zval *retval_ptr, zval *args) /* {{{ */
3568{
3569	zval retval, *org_params = NULL;
3570	int result, org_count = 0;
3571
3572	fci->retval = retval_ptr ? retval_ptr : &retval;
3573	if (args) {
3574		zend_fcall_info_args_save(fci, &org_count, &org_params);
3575		zend_fcall_info_args(fci, args);
3576	}
3577	result = zend_call_function(fci, fcc);
3578
3579	if (!retval_ptr && Z_TYPE(retval) != IS_UNDEF) {
3580		zval_ptr_dtor(&retval);
3581	}
3582	if (args) {
3583		zend_fcall_info_args_restore(fci, org_count, org_params);
3584	}
3585	return result;
3586}
3587/* }}} */
3588
3589ZEND_API const char *zend_get_module_version(const char *module_name) /* {{{ */
3590{
3591	zend_string *lname;
3592	size_t name_len = strlen(module_name);
3593	zend_module_entry *module;
3594
3595	lname = zend_string_alloc(name_len, 0);
3596	zend_str_tolower_copy(ZSTR_VAL(lname), module_name, name_len);
3597	module = zend_hash_find_ptr(&module_registry, lname);
3598	zend_string_free(lname);
3599	return module ? module->version : NULL;
3600}
3601/* }}} */
3602
3603ZEND_API int zend_declare_property_ex(zend_class_entry *ce, zend_string *name, zval *property, int access_type, zend_string *doc_comment) /* {{{ */
3604{
3605	zend_property_info *property_info, *property_info_ptr;
3606
3607	if (ce->type == ZEND_INTERNAL_CLASS) {
3608		property_info = pemalloc(sizeof(zend_property_info), 1);
3609		if ((access_type & ZEND_ACC_STATIC) || Z_CONSTANT_P(property)) {
3610			ce->ce_flags &= ~ZEND_ACC_CONSTANTS_UPDATED;
3611		}
3612	} else {
3613		property_info = zend_arena_alloc(&CG(arena), sizeof(zend_property_info));
3614		if (Z_CONSTANT_P(property)) {
3615			ce->ce_flags &= ~ZEND_ACC_CONSTANTS_UPDATED;
3616		}
3617	}
3618
3619	if (!(access_type & ZEND_ACC_PPP_MASK)) {
3620		access_type |= ZEND_ACC_PUBLIC;
3621	}
3622	if (access_type & ZEND_ACC_STATIC) {
3623		if ((property_info_ptr = zend_hash_find_ptr(&ce->properties_info, name)) != NULL &&
3624		    (property_info_ptr->flags & ZEND_ACC_STATIC) != 0) {
3625			property_info->offset = property_info_ptr->offset;
3626			zval_ptr_dtor(&ce->default_static_members_table[property_info->offset]);
3627			zend_hash_del(&ce->properties_info, name);
3628		} else {
3629			property_info->offset = ce->default_static_members_count++;
3630			ce->default_static_members_table = perealloc(ce->default_static_members_table, sizeof(zval) * ce->default_static_members_count, ce->type == ZEND_INTERNAL_CLASS);
3631		}
3632		ZVAL_COPY_VALUE(&ce->default_static_members_table[property_info->offset], property);
3633		if (ce->type == ZEND_USER_CLASS) {
3634			ce->static_members_table = ce->default_static_members_table;
3635		}
3636	} else {
3637		if ((property_info_ptr = zend_hash_find_ptr(&ce->properties_info, name)) != NULL &&
3638		    (property_info_ptr->flags & ZEND_ACC_STATIC) == 0) {
3639			property_info->offset = property_info_ptr->offset;
3640			zval_ptr_dtor(&ce->default_properties_table[OBJ_PROP_TO_NUM(property_info->offset)]);
3641			zend_hash_del(&ce->properties_info, name);
3642		} else {
3643			property_info->offset = OBJ_PROP_TO_OFFSET(ce->default_properties_count);
3644			ce->default_properties_count++;
3645			ce->default_properties_table = perealloc(ce->default_properties_table, sizeof(zval) * ce->default_properties_count, ce->type == ZEND_INTERNAL_CLASS);
3646		}
3647		ZVAL_COPY_VALUE(&ce->default_properties_table[OBJ_PROP_TO_NUM(property_info->offset)], property);
3648	}
3649	if (ce->type & ZEND_INTERNAL_CLASS) {
3650		switch(Z_TYPE_P(property)) {
3651			case IS_ARRAY:
3652			case IS_OBJECT:
3653			case IS_RESOURCE:
3654				zend_error_noreturn(E_CORE_ERROR, "Internal zval's can't be arrays, objects or resources");
3655				break;
3656			default:
3657				break;
3658		}
3659	}
3660	if (access_type & ZEND_ACC_PUBLIC) {
3661		property_info->name = zend_string_copy(name);
3662	} else if (access_type & ZEND_ACC_PRIVATE) {
3663		property_info->name = zend_mangle_property_name(ZSTR_VAL(ce->name), ZSTR_LEN(ce->name), ZSTR_VAL(name), ZSTR_LEN(name), ce->type & ZEND_INTERNAL_CLASS);
3664	} else {
3665		ZEND_ASSERT(access_type & ZEND_ACC_PROTECTED);
3666		property_info->name = zend_mangle_property_name("*", 1, ZSTR_VAL(name), ZSTR_LEN(name), ce->type & ZEND_INTERNAL_CLASS);
3667	}
3668
3669	property_info->name = zend_new_interned_string(property_info->name);
3670	property_info->flags = access_type;
3671	property_info->doc_comment = doc_comment;
3672	property_info->ce = ce;
3673	zend_hash_update_ptr(&ce->properties_info, name, property_info);
3674
3675	return SUCCESS;
3676}
3677/* }}} */
3678
3679ZEND_API int zend_declare_property(zend_class_entry *ce, const char *name, size_t name_length, zval *property, int access_type) /* {{{ */
3680{
3681	zend_string *key = zend_string_init(name, name_length, ce->type & ZEND_INTERNAL_CLASS);
3682	int ret = zend_declare_property_ex(ce, key, property, access_type, NULL);
3683	zend_string_release(key);
3684	return ret;
3685}
3686/* }}} */
3687
3688ZEND_API int zend_declare_property_null(zend_class_entry *ce, const char *name, size_t name_length, int access_type) /* {{{ */
3689{
3690	zval property;
3691
3692	ZVAL_NULL(&property);
3693	return zend_declare_property(ce, name, name_length, &property, access_type);
3694}
3695/* }}} */
3696
3697ZEND_API int zend_declare_property_bool(zend_class_entry *ce, const char *name, size_t name_length, zend_long value, int access_type) /* {{{ */
3698{
3699	zval property;
3700
3701	ZVAL_BOOL(&property, value);
3702	return zend_declare_property(ce, name, name_length, &property, access_type);
3703}
3704/* }}} */
3705
3706ZEND_API int zend_declare_property_long(zend_class_entry *ce, const char *name, size_t name_length, zend_long value, int access_type) /* {{{ */
3707{
3708	zval property;
3709
3710	ZVAL_LONG(&property, value);
3711	return zend_declare_property(ce, name, name_length, &property, access_type);
3712}
3713/* }}} */
3714
3715ZEND_API int zend_declare_property_double(zend_class_entry *ce, const char *name, size_t name_length, double value, int access_type) /* {{{ */
3716{
3717	zval property;
3718
3719	ZVAL_DOUBLE(&property, value);
3720	return zend_declare_property(ce, name, name_length, &property, access_type);
3721}
3722/* }}} */
3723
3724ZEND_API int zend_declare_property_string(zend_class_entry *ce, const char *name, size_t name_length, const char *value, int access_type) /* {{{ */
3725{
3726	zval property;
3727
3728	ZVAL_NEW_STR(&property, zend_string_init(value, strlen(value), ce->type & ZEND_INTERNAL_CLASS));
3729	return zend_declare_property(ce, name, name_length, &property, access_type);
3730}
3731/* }}} */
3732
3733ZEND_API int zend_declare_property_stringl(zend_class_entry *ce, const char *name, size_t name_length, const char *value, size_t value_len, int access_type) /* {{{ */
3734{
3735	zval property;
3736
3737	ZVAL_NEW_STR(&property, zend_string_init(value, value_len, ce->type & ZEND_INTERNAL_CLASS));
3738	return zend_declare_property(ce, name, name_length, &property, access_type);
3739}
3740/* }}} */
3741
3742ZEND_API int zend_declare_class_constant_ex(zend_class_entry *ce, zend_string *name, zval *value, int access_type, zend_string *doc_comment) /* {{{ */
3743{
3744	zend_class_constant *c;
3745
3746	if (ce->ce_flags & ZEND_ACC_INTERFACE) {
3747		if (access_type != ZEND_ACC_PUBLIC) {
3748			zend_error_noreturn(E_COMPILE_ERROR, "Access type for interface constant %s::%s must be public", ZSTR_VAL(ce->name), ZSTR_VAL(name));
3749		}
3750	}
3751
3752	if (zend_string_equals_literal_ci(name, "class")) {
3753		zend_error_noreturn(ce->type == ZEND_INTERNAL_CLASS ? E_CORE_ERROR : E_COMPILE_ERROR,
3754				"A class constant must not be called 'class'; it is reserved for class name fetching");
3755	}
3756
3757	if (ce->type == ZEND_INTERNAL_CLASS) {
3758		c = pemalloc(sizeof(zend_class_constant), 1);
3759	} else {
3760		c = zend_arena_alloc(&CG(arena), sizeof(zend_class_constant));
3761	}
3762	ZVAL_COPY_VALUE(&c->value, value);
3763	Z_ACCESS_FLAGS(c->value) = access_type;
3764	c->doc_comment = doc_comment;
3765	c->ce = ce;
3766	if (Z_CONSTANT_P(value)) {
3767		ce->ce_flags &= ~ZEND_ACC_CONSTANTS_UPDATED;
3768	}
3769
3770	if (!zend_hash_add_ptr(&ce->constants_table, name, c)) {
3771		zend_error_noreturn(ce->type == ZEND_INTERNAL_CLASS ? E_CORE_ERROR : E_COMPILE_ERROR,
3772			"Cannot redefine class constant %s::%s", ZSTR_VAL(ce->name), ZSTR_VAL(name));
3773	}
3774
3775	return SUCCESS;
3776}
3777/* }}} */
3778
3779ZEND_API int zend_declare_class_constant(zend_class_entry *ce, const char *name, size_t name_length, zval *value) /* {{{ */
3780{
3781	int ret;
3782
3783	zend_string *key = zend_string_init(name, name_length, ce->type & ZEND_INTERNAL_CLASS);
3784	ret = zend_declare_class_constant_ex(ce, key, value, ZEND_ACC_PUBLIC, NULL);
3785	zend_string_release(key);
3786	return ret;
3787}
3788/* }}} */
3789
3790ZEND_API int zend_declare_class_constant_null(zend_class_entry *ce, const char *name, size_t name_length) /* {{{ */
3791{
3792	zval constant;
3793
3794	ZVAL_NULL(&constant);
3795	return zend_declare_class_constant(ce, name, name_length, &constant);
3796}
3797/* }}} */
3798
3799ZEND_API int zend_declare_class_constant_long(zend_class_entry *ce, const char *name, size_t name_length, zend_long value) /* {{{ */
3800{
3801	zval constant;
3802
3803	ZVAL_LONG(&constant, value);
3804	return zend_declare_class_constant(ce, name, name_length, &constant);
3805}
3806/* }}} */
3807
3808ZEND_API int zend_declare_class_constant_bool(zend_class_entry *ce, const char *name, size_t name_length, zend_bool value) /* {{{ */
3809{
3810	zval constant;
3811
3812	ZVAL_BOOL(&constant, value);
3813	return zend_declare_class_constant(ce, name, name_length, &constant);
3814}
3815/* }}} */
3816
3817ZEND_API int zend_declare_class_constant_double(zend_class_entry *ce, const char *name, size_t name_length, double value) /* {{{ */
3818{
3819	zval constant;
3820
3821	ZVAL_DOUBLE(&constant, value);
3822	return zend_declare_class_constant(ce, name, name_length, &constant);
3823}
3824/* }}} */
3825
3826ZEND_API int zend_declare_class_constant_stringl(zend_class_entry *ce, const char *name, size_t name_length, const char *value, size_t value_length) /* {{{ */
3827{
3828	zval constant;
3829
3830	ZVAL_NEW_STR(&constant, zend_string_init(value, value_length, ce->type & ZEND_INTERNAL_CLASS));
3831	return zend_declare_class_constant(ce, name, name_length, &constant);
3832}
3833/* }}} */
3834
3835ZEND_API int zend_declare_class_constant_string(zend_class_entry *ce, const char *name, size_t name_length, const char *value) /* {{{ */
3836{
3837	return zend_declare_class_constant_stringl(ce, name, name_length, value, strlen(value));
3838}
3839/* }}} */
3840
3841ZEND_API void zend_update_property_ex(zend_class_entry *scope, zval *object, zend_string *name, zval *value) /* {{{ */
3842{
3843	zval property;
3844	zend_class_entry *old_scope = EG(scope);
3845
3846	EG(scope) = scope;
3847
3848	if (!Z_OBJ_HT_P(object)->write_property) {
3849		zend_error_noreturn(E_CORE_ERROR, "Property %s of class %s cannot be updated", name, ZSTR_VAL(Z_OBJCE_P(object)->name));
3850	}
3851	ZVAL_STR(&property, name);
3852	Z_OBJ_HT_P(object)->write_property(object, &property, value, NULL);
3853
3854	EG(scope) = old_scope;
3855}
3856/* }}} */
3857
3858ZEND_API void zend_update_property(zend_class_entry *scope, zval *object, const char *name, size_t name_length, zval *value) /* {{{ */
3859{
3860	zval property;
3861	zend_class_entry *old_scope = EG(scope);
3862
3863	EG(scope) = scope;
3864
3865	if (!Z_OBJ_HT_P(object)->write_property) {
3866		zend_error_noreturn(E_CORE_ERROR, "Property %s of class %s cannot be updated", name, ZSTR_VAL(Z_OBJCE_P(object)->name));
3867	}
3868	ZVAL_STRINGL(&property, name, name_length);
3869	Z_OBJ_HT_P(object)->write_property(object, &property, value, NULL);
3870	zval_ptr_dtor(&property);
3871
3872	EG(scope) = old_scope;
3873}
3874/* }}} */
3875
3876ZEND_API void zend_update_property_null(zend_class_entry *scope, zval *object, const char *name, size_t name_length) /* {{{ */
3877{
3878	zval tmp;
3879
3880	ZVAL_NULL(&tmp);
3881	zend_update_property(scope, object, name, name_length, &tmp);
3882}
3883/* }}} */
3884
3885ZEND_API void zend_update_property_bool(zend_class_entry *scope, zval *object, const char *name, size_t name_length, zend_long value) /* {{{ */
3886{
3887	zval tmp;
3888
3889	ZVAL_BOOL(&tmp, value);
3890	zend_update_property(scope, object, name, name_length, &tmp);
3891}
3892/* }}} */
3893
3894ZEND_API void zend_update_property_long(zend_class_entry *scope, zval *object, const char *name, size_t name_length, zend_long value) /* {{{ */
3895{
3896	zval tmp;
3897
3898	ZVAL_LONG(&tmp, value);
3899	zend_update_property(scope, object, name, name_length, &tmp);
3900}
3901/* }}} */
3902
3903ZEND_API void zend_update_property_double(zend_class_entry *scope, zval *object, const char *name, size_t name_length, double value) /* {{{ */
3904{
3905	zval tmp;
3906
3907	ZVAL_DOUBLE(&tmp, value);
3908	zend_update_property(scope, object, name, name_length, &tmp);
3909}
3910/* }}} */
3911
3912ZEND_API void zend_update_property_str(zend_class_entry *scope, zval *object, const char *name, size_t name_length, zend_string *value) /* {{{ */
3913{
3914	zval tmp;
3915
3916	ZVAL_STR(&tmp, value);
3917	zend_update_property(scope, object, name, name_length, &tmp);
3918}
3919/* }}} */
3920
3921ZEND_API void zend_update_property_string(zend_class_entry *scope, zval *object, const char *name, size_t name_length, const char *value) /* {{{ */
3922{
3923	zval tmp;
3924
3925	ZVAL_STRING(&tmp, value);
3926	Z_SET_REFCOUNT(tmp, 0);
3927	zend_update_property(scope, object, name, name_length, &tmp);
3928}
3929/* }}} */
3930
3931ZEND_API void zend_update_property_stringl(zend_class_entry *scope, zval *object, const char *name, size_t name_length, const char *value, size_t value_len) /* {{{ */
3932{
3933	zval tmp;
3934
3935	ZVAL_STRINGL(&tmp, value, value_len);
3936	Z_SET_REFCOUNT(tmp, 0);
3937	zend_update_property(scope, object, name, name_length, &tmp);
3938}
3939/* }}} */
3940
3941ZEND_API int zend_update_static_property(zend_class_entry *scope, const char *name, size_t name_length, zval *value) /* {{{ */
3942{
3943	zval *property;
3944	zend_class_entry *old_scope = EG(scope);
3945	zend_string *key = zend_string_init(name, name_length, 0);
3946
3947	EG(scope) = scope;
3948	property = zend_std_get_static_property(scope, key, 0);
3949	EG(scope) = old_scope;
3950	zend_string_free(key);
3951	if (!property) {
3952		return FAILURE;
3953	} else {
3954		if (property != value) {
3955			if (Z_ISREF_P(property)) {
3956				zval_dtor(property);
3957				ZVAL_COPY_VALUE(property, value);
3958				if (Z_REFCOUNTED_P(value) && Z_REFCOUNT_P(value) > 0) {
3959					zval_opt_copy_ctor(property);
3960				}
3961			} else {
3962				zval garbage;
3963
3964				ZVAL_COPY_VALUE(&garbage, property);
3965				if (Z_REFCOUNTED_P(value)) {
3966					Z_ADDREF_P(value);
3967					if (Z_ISREF_P(value)) {
3968						SEPARATE_ZVAL(value);
3969					}
3970				}
3971				ZVAL_COPY_VALUE(property, value);
3972				zval_ptr_dtor(&garbage);
3973			}
3974		}
3975		return SUCCESS;
3976	}
3977}
3978/* }}} */
3979
3980ZEND_API int zend_update_static_property_null(zend_class_entry *scope, const char *name, size_t name_length) /* {{{ */
3981{
3982	zval tmp;
3983
3984	ZVAL_NULL(&tmp);
3985	return zend_update_static_property(scope, name, name_length, &tmp);
3986}
3987/* }}} */
3988
3989ZEND_API int zend_update_static_property_bool(zend_class_entry *scope, const char *name, size_t name_length, zend_long value) /* {{{ */
3990{
3991	zval tmp;
3992
3993	ZVAL_BOOL(&tmp, value);
3994	return zend_update_static_property(scope, name, name_length, &tmp);
3995}
3996/* }}} */
3997
3998ZEND_API int zend_update_static_property_long(zend_class_entry *scope, const char *name, size_t name_length, zend_long value) /* {{{ */
3999{
4000	zval tmp;
4001
4002	ZVAL_LONG(&tmp, value);
4003	return zend_update_static_property(scope, name, name_length, &tmp);
4004}
4005/* }}} */
4006
4007ZEND_API int zend_update_static_property_double(zend_class_entry *scope, const char *name, size_t name_length, double value) /* {{{ */
4008{
4009	zval tmp;
4010
4011	ZVAL_DOUBLE(&tmp, value);
4012	return zend_update_static_property(scope, name, name_length, &tmp);
4013}
4014/* }}} */
4015
4016ZEND_API int zend_update_static_property_string(zend_class_entry *scope, const char *name, size_t name_length, const char *value) /* {{{ */
4017{
4018	zval tmp;
4019
4020	ZVAL_STRING(&tmp, value);
4021	Z_SET_REFCOUNT(tmp, 0);
4022	return zend_update_static_property(scope, name, name_length, &tmp);
4023}
4024/* }}} */
4025
4026ZEND_API int zend_update_static_property_stringl(zend_class_entry *scope, const char *name, size_t name_length, const char *value, size_t value_len) /* {{{ */
4027{
4028	zval tmp;
4029
4030	ZVAL_STRINGL(&tmp, value, value_len);
4031	Z_SET_REFCOUNT(tmp, 0);
4032	return zend_update_static_property(scope, name, name_length, &tmp);
4033}
4034/* }}} */
4035
4036ZEND_API zval *zend_read_property(zend_class_entry *scope, zval *object, const char *name, size_t name_length, zend_bool silent, zval *rv) /* {{{ */
4037{
4038	zval property, *value;
4039	zend_class_entry *old_scope = EG(scope);
4040
4041	EG(scope) = scope;
4042
4043	if (!Z_OBJ_HT_P(object)->read_property) {
4044		zend_error_noreturn(E_CORE_ERROR, "Property %s of class %s cannot be read", name, ZSTR_VAL(Z_OBJCE_P(object)->name));
4045	}
4046
4047	ZVAL_STRINGL(&property, name, name_length);
4048	value = Z_OBJ_HT_P(object)->read_property(object, &property, silent?BP_VAR_IS:BP_VAR_R, NULL, rv);
4049	zval_ptr_dtor(&property);
4050
4051	EG(scope) = old_scope;
4052	return value;
4053}
4054/* }}} */
4055
4056ZEND_API zval *zend_read_static_property(zend_class_entry *scope, const char *name, size_t name_length, zend_bool silent) /* {{{ */
4057{
4058	zval *property;
4059	zend_class_entry *old_scope = EG(scope);
4060	zend_string *key = zend_string_init(name, name_length, 0);
4061
4062	EG(scope) = scope;
4063	property = zend_std_get_static_property(scope, key, silent);
4064	EG(scope) = old_scope;
4065	zend_string_free(key);
4066
4067	return property;
4068}
4069/* }}} */
4070
4071ZEND_API void zend_save_error_handling(zend_error_handling *current) /* {{{ */
4072{
4073	current->handling = EG(error_handling);
4074	current->exception = EG(exception_class);
4075	ZVAL_COPY(&current->user_handler, &EG(user_error_handler));
4076}
4077/* }}} */
4078
4079ZEND_API void zend_replace_error_handling(zend_error_handling_t error_handling, zend_class_entry *exception_class, zend_error_handling *current) /* {{{ */
4080{
4081	if (current) {
4082		zend_save_error_handling(current);
4083		if (error_handling != EH_NORMAL && Z_TYPE(EG(user_error_handler)) != IS_UNDEF) {
4084			zval_ptr_dtor(&EG(user_error_handler));
4085			ZVAL_UNDEF(&EG(user_error_handler));
4086		}
4087	}
4088	EG(error_handling) = error_handling;
4089	EG(exception_class) = error_handling == EH_THROW ? exception_class : NULL;
4090}
4091/* }}} */
4092
4093static int same_zval(zval *zv1, zval *zv2)  /* {{{ */
4094{
4095	if (Z_TYPE_P(zv1) != Z_TYPE_P(zv2)) {
4096		return 0;
4097	}
4098	switch (Z_TYPE_P(zv1)) {
4099		case IS_UNDEF:
4100		case IS_NULL:
4101		case IS_FALSE:
4102		case IS_TRUE:
4103			return 1;
4104		case IS_LONG:
4105			return Z_LVAL_P(zv1) == Z_LVAL_P(zv2);
4106		case IS_DOUBLE:
4107			return Z_LVAL_P(zv1) == Z_LVAL_P(zv2);
4108		case IS_STRING:
4109		case IS_ARRAY:
4110		case IS_OBJECT:
4111		case IS_RESOURCE:
4112			return Z_COUNTED_P(zv1) == Z_COUNTED_P(zv2);
4113		default:
4114			return 0;
4115	}
4116}
4117/* }}} */
4118
4119ZEND_API void zend_restore_error_handling(zend_error_handling *saved) /* {{{ */
4120{
4121	EG(error_handling) = saved->handling;
4122	EG(exception_class) = saved->handling == EH_THROW ? saved->exception : NULL;
4123	if (Z_TYPE(saved->user_handler) != IS_UNDEF
4124		&& !same_zval(&saved->user_handler, &EG(user_error_handler))) {
4125		zval_ptr_dtor(&EG(user_error_handler));
4126		ZVAL_COPY_VALUE(&EG(user_error_handler), &saved->user_handler);
4127	} else if (Z_TYPE(saved->user_handler)) {
4128		zval_ptr_dtor(&saved->user_handler);
4129	}
4130	ZVAL_UNDEF(&saved->user_handler);
4131}
4132/* }}} */
4133
4134ZEND_API zend_string* zend_find_alias_name(zend_class_entry *ce, zend_string *name) /* {{{ */
4135{
4136	zend_trait_alias *alias, **alias_ptr;
4137
4138	if ((alias_ptr = ce->trait_aliases)) {
4139		alias = *alias_ptr;
4140		while (alias) {
4141			if (alias->alias && zend_string_equals_ci(alias->alias, name)) {
4142				return alias->alias;
4143			}
4144			alias_ptr++;
4145			alias = *alias_ptr;
4146		}
4147	}
4148
4149	return name;
4150}
4151/* }}} */
4152
4153ZEND_API zend_string *zend_resolve_method_name(zend_class_entry *ce, zend_function *f) /* {{{ */
4154{
4155	zend_function *func;
4156	HashTable *function_table;
4157	zend_string *name;
4158
4159	if (f->common.type != ZEND_USER_FUNCTION ||
4160	    (f->op_array.refcount && *(f->op_array.refcount) < 2) ||
4161	    !f->common.scope ||
4162	    !f->common.scope->trait_aliases) {
4163		return f->common.function_name;
4164	}
4165
4166	function_table = &ce->function_table;
4167	ZEND_HASH_FOREACH_STR_KEY_PTR(function_table, name, func) {
4168		if (func == f) {
4169			if (!name) {
4170				return f->common.function_name;
4171			}
4172			if (ZSTR_LEN(name) == ZSTR_LEN(f->common.function_name) &&
4173			    !strncasecmp(ZSTR_VAL(name), ZSTR_VAL(f->common.function_name), ZSTR_LEN(f->common.function_name))) {
4174				return f->common.function_name;
4175			}
4176			return zend_find_alias_name(f->common.scope, name);
4177		}
4178	} ZEND_HASH_FOREACH_END();
4179	return f->common.function_name;
4180}
4181/* }}} */
4182
4183ZEND_API const char *zend_get_object_type(const zend_class_entry *ce) /* {{{ */
4184{
4185	if(ce->ce_flags & ZEND_ACC_TRAIT) {
4186		return "trait";
4187	} else if (ce->ce_flags & ZEND_ACC_INTERFACE) {
4188		return "interface";
4189	} else {
4190		return "class";
4191	}
4192}
4193/* }}} */
4194
4195/*
4196 * Local variables:
4197 * tab-width: 4
4198 * c-basic-offset: 4
4199 * indent-tabs-mode: t
4200 * End:
4201 */
4202