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