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