xref: /PHP-5.6/Zend/zend_API.c (revision 0e6fe3a4)
1 /*
2    +----------------------------------------------------------------------+
3    | Zend Engine                                                          |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1998-2016 Zend Technologies Ltd. (http://www.zend.com) |
6    +----------------------------------------------------------------------+
7    | This source file is subject to version 2.00 of the Zend license,     |
8    | that is bundled with this package in the file LICENSE, and is        |
9    | available through the world-wide-web at the following url:           |
10    | http://www.zend.com/license/2_00.txt.                                |
11    | If you did not receive a copy of the Zend license and are unable to  |
12    | obtain it through the world-wide-web, please send a note to          |
13    | license@zend.com so we can mail you a copy immediately.              |
14    +----------------------------------------------------------------------+
15    | Authors: Andi Gutmans <andi@zend.com>                                |
16    |          Zeev Suraski <zeev@zend.com>                                |
17    |          Andrei Zmievski <andrei@php.net>                            |
18    +----------------------------------------------------------------------+
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, 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 (IS_CONSTANT_TYPE(Z_TYPE_PP(pp))) {
1057 		zend_class_entry **scope = EG(in_execution)?&EG(scope):&CG(active_class_entry);
1058 
1059 		if ((*scope)->parent) {
1060 			zend_class_entry *ce = *scope;
1061 			HashPosition pos;
1062 			zend_property_info *prop_info;
1063 
1064 			do {
1065 				for (zend_hash_internal_pointer_reset_ex(&ce->properties_info, &pos);
1066 				     zend_hash_get_current_data_ex(&ce->properties_info, (void **) &prop_info, &pos) == SUCCESS;
1067 				     zend_hash_move_forward_ex(&ce->properties_info, &pos)) {
1068 					if (is_static == ((prop_info->flags & ZEND_ACC_STATIC) != 0) &&
1069 					    offset == prop_info->offset) {
1070 						int ret;
1071 						zend_class_entry *old_scope = *scope;
1072 						*scope = prop_info->ce;
1073 						ret = zval_update_constant(pp, 1 TSRMLS_CC);
1074 						*scope = old_scope;
1075 						return ret;
1076 					}
1077 				}
1078 				ce = ce->parent;
1079 			} while (ce);
1080 
1081 		}
1082 		return zval_update_constant(pp, 1 TSRMLS_CC);
1083 	}
1084 	return 0;
1085 }
1086 /* }}} */
1087 
zend_update_class_constants(zend_class_entry * class_type TSRMLS_DC)1088 ZEND_API void zend_update_class_constants(zend_class_entry *class_type TSRMLS_DC) /* {{{ */
1089 {
1090 	if ((class_type->ce_flags & ZEND_ACC_CONSTANTS_UPDATED) == 0 || (!CE_STATIC_MEMBERS(class_type) && class_type->default_static_members_count)) {
1091 		zend_class_entry **scope = EG(in_execution)?&EG(scope):&CG(active_class_entry);
1092 		zend_class_entry *old_scope = *scope;
1093 		int i;
1094 
1095 		*scope = class_type;
1096 		zend_hash_apply_with_argument(&class_type->constants_table, (apply_func_arg_t) zval_update_constant, (void *)1 TSRMLS_CC);
1097 
1098 		for (i = 0; i < class_type->default_properties_count; i++) {
1099 			if (class_type->default_properties_table[i]) {
1100 				zval_update_class_constant(&class_type->default_properties_table[i], 0, i TSRMLS_CC);
1101 			}
1102 		}
1103 
1104 		if (!CE_STATIC_MEMBERS(class_type) && class_type->default_static_members_count) {
1105 			zval **p;
1106 
1107 			if (class_type->parent) {
1108 				zend_update_class_constants(class_type->parent TSRMLS_CC);
1109 			}
1110 #if ZTS
1111 			CG(static_members_table)[(zend_intptr_t)(class_type->static_members_table)] = emalloc(sizeof(zval*) * class_type->default_static_members_count);
1112 #else
1113 			class_type->static_members_table = emalloc(sizeof(zval*) * class_type->default_static_members_count);
1114 #endif
1115 			for (i = 0; i < class_type->default_static_members_count; i++) {
1116 				p = &class_type->default_static_members_table[i];
1117 				if (Z_ISREF_PP(p) &&
1118 					class_type->parent &&
1119 					i < class_type->parent->default_static_members_count &&
1120 					*p == class_type->parent->default_static_members_table[i] &&
1121 					CE_STATIC_MEMBERS(class_type->parent)[i]
1122 				) {
1123 					zval *q = CE_STATIC_MEMBERS(class_type->parent)[i];
1124 
1125 					Z_ADDREF_P(q);
1126 					Z_SET_ISREF_P(q);
1127 					CE_STATIC_MEMBERS(class_type)[i] = q;
1128 				} else {
1129 					zval *r;
1130 
1131 					ALLOC_ZVAL(r);
1132 					*r = **p;
1133 					INIT_PZVAL(r);
1134 					zval_copy_ctor(r);
1135 					CE_STATIC_MEMBERS(class_type)[i] = r;
1136 				}
1137 			}
1138 		}
1139 
1140 		for (i = 0; i < class_type->default_static_members_count; i++) {
1141 			zval_update_class_constant(&CE_STATIC_MEMBERS(class_type)[i], 1, i TSRMLS_CC);
1142 		}
1143 
1144 		*scope = old_scope;
1145 		class_type->ce_flags |= ZEND_ACC_CONSTANTS_UPDATED;
1146 	}
1147 }
1148 /* }}} */
1149 
object_properties_init(zend_object * object,zend_class_entry * class_type)1150 ZEND_API void object_properties_init(zend_object *object, zend_class_entry *class_type) /* {{{ */
1151 {
1152 	int i;
1153 
1154 	if (class_type->default_properties_count) {
1155 		object->properties_table = emalloc(sizeof(zval*) * class_type->default_properties_count);
1156 		for (i = 0; i < class_type->default_properties_count; i++) {
1157 			object->properties_table[i] = class_type->default_properties_table[i];
1158 			if (class_type->default_properties_table[i]) {
1159 #if ZTS
1160 				ALLOC_ZVAL( object->properties_table[i]);
1161 				MAKE_COPY_ZVAL(&class_type->default_properties_table[i], object->properties_table[i]);
1162 #else
1163 				Z_ADDREF_P(object->properties_table[i]);
1164 #endif
1165 			}
1166 		}
1167 		object->properties = NULL;
1168 	}
1169 }
1170 /* }}} */
1171 
1172 /* This function requires 'properties' to contain all props declared in the
1173  * class and all props being public. If only a subset is given or the class
1174  * has protected members then you need to merge the properties separately by
1175  * calling zend_merge_properties(). */
_object_and_properties_init(zval * arg,zend_class_entry * class_type,HashTable * properties ZEND_FILE_LINE_DC TSRMLS_DC)1176 ZEND_API int _object_and_properties_init(zval *arg, zend_class_entry *class_type, HashTable *properties ZEND_FILE_LINE_DC TSRMLS_DC) /* {{{ */
1177 {
1178 	zend_object *object;
1179 
1180 	if (class_type->ce_flags & (ZEND_ACC_INTERFACE|ZEND_ACC_IMPLICIT_ABSTRACT_CLASS|ZEND_ACC_EXPLICIT_ABSTRACT_CLASS)) {
1181 		char *what =   (class_type->ce_flags & ZEND_ACC_INTERFACE)                ? "interface"
1182 					 :((class_type->ce_flags & ZEND_ACC_TRAIT) == ZEND_ACC_TRAIT) ? "trait"
1183 					 :                                                              "abstract class";
1184 		zend_error(E_ERROR, "Cannot instantiate %s %s", what, class_type->name);
1185 	}
1186 
1187 	zend_update_class_constants(class_type TSRMLS_CC);
1188 
1189 	Z_TYPE_P(arg) = IS_OBJECT;
1190 	if (class_type->create_object == NULL) {
1191 		Z_OBJVAL_P(arg) = zend_objects_new(&object, class_type TSRMLS_CC);
1192 		if (properties) {
1193 			object->properties = properties;
1194 			object->properties_table = NULL;
1195 		} else {
1196 			object_properties_init(object, class_type);
1197 		}
1198 	} else {
1199 		Z_OBJVAL_P(arg) = class_type->create_object(class_type TSRMLS_CC);
1200 	}
1201 	return SUCCESS;
1202 }
1203 /* }}} */
1204 
_object_init_ex(zval * arg,zend_class_entry * class_type ZEND_FILE_LINE_DC TSRMLS_DC)1205 ZEND_API int _object_init_ex(zval *arg, zend_class_entry *class_type ZEND_FILE_LINE_DC TSRMLS_DC) /* {{{ */
1206 {
1207 	return _object_and_properties_init(arg, class_type, 0 ZEND_FILE_LINE_RELAY_CC TSRMLS_CC);
1208 }
1209 /* }}} */
1210 
_object_init(zval * arg ZEND_FILE_LINE_DC TSRMLS_DC)1211 ZEND_API int _object_init(zval *arg ZEND_FILE_LINE_DC TSRMLS_DC) /* {{{ */
1212 {
1213 	return _object_init_ex(arg, zend_standard_class_def ZEND_FILE_LINE_RELAY_CC TSRMLS_CC);
1214 }
1215 /* }}} */
1216 
add_assoc_function(zval * arg,const char * key,void (* function_ptr)(INTERNAL_FUNCTION_PARAMETERS))1217 ZEND_API int add_assoc_function(zval *arg, const char *key, void (*function_ptr)(INTERNAL_FUNCTION_PARAMETERS)) /* {{{ */
1218 {
1219 	zend_error(E_WARNING, "add_assoc_function() is no longer supported");
1220 	return FAILURE;
1221 }
1222 /* }}} */
1223 
add_assoc_long_ex(zval * arg,const char * key,uint key_len,long n)1224 ZEND_API int add_assoc_long_ex(zval *arg, const char *key, uint key_len, long n) /* {{{ */
1225 {
1226 	zval *tmp;
1227 
1228 	MAKE_STD_ZVAL(tmp);
1229 	ZVAL_LONG(tmp, n);
1230 
1231 	return zend_symtable_update(Z_ARRVAL_P(arg), key, key_len, (void *) &tmp, sizeof(zval *), NULL);
1232 }
1233 /* }}} */
1234 
add_assoc_null_ex(zval * arg,const char * key,uint key_len)1235 ZEND_API int add_assoc_null_ex(zval *arg, const char *key, uint key_len) /* {{{ */
1236 {
1237 	zval *tmp;
1238 
1239 	MAKE_STD_ZVAL(tmp);
1240 	ZVAL_NULL(tmp);
1241 
1242 	return zend_symtable_update(Z_ARRVAL_P(arg), key, key_len, (void *) &tmp, sizeof(zval *), NULL);
1243 }
1244 /* }}} */
1245 
add_assoc_bool_ex(zval * arg,const char * key,uint key_len,int b)1246 ZEND_API int add_assoc_bool_ex(zval *arg, const char *key, uint key_len, int b) /* {{{ */
1247 {
1248 	zval *tmp;
1249 
1250 	MAKE_STD_ZVAL(tmp);
1251 	ZVAL_BOOL(tmp, b);
1252 
1253 	return zend_symtable_update(Z_ARRVAL_P(arg), key, key_len, (void *) &tmp, sizeof(zval *), NULL);
1254 }
1255 /* }}} */
1256 
add_assoc_resource_ex(zval * arg,const char * key,uint key_len,int r)1257 ZEND_API int add_assoc_resource_ex(zval *arg, const char *key, uint key_len, int r) /* {{{ */
1258 {
1259 	zval *tmp;
1260 
1261 	MAKE_STD_ZVAL(tmp);
1262 	ZVAL_RESOURCE(tmp, r);
1263 
1264 	return zend_symtable_update(Z_ARRVAL_P(arg), key, key_len, (void *) &tmp, sizeof(zval *), NULL);
1265 }
1266 /* }}} */
1267 
add_assoc_double_ex(zval * arg,const char * key,uint key_len,double d)1268 ZEND_API int add_assoc_double_ex(zval *arg, const char *key, uint key_len, double d) /* {{{ */
1269 {
1270 	zval *tmp;
1271 
1272 	MAKE_STD_ZVAL(tmp);
1273 	ZVAL_DOUBLE(tmp, d);
1274 
1275 	return zend_symtable_update(Z_ARRVAL_P(arg), key, key_len, (void *) &tmp, sizeof(zval *), NULL);
1276 }
1277 /* }}} */
1278 
add_assoc_string_ex(zval * arg,const char * key,uint key_len,char * str,int duplicate)1279 ZEND_API int add_assoc_string_ex(zval *arg, const char *key, uint key_len, char *str, int duplicate) /* {{{ */
1280 {
1281 	zval *tmp;
1282 	size_t _len = strlen(str);
1283 
1284 	if (UNEXPECTED(_len > INT_MAX)) {
1285 		zend_error_noreturn(E_ERROR, "String overflow, max size is %d", INT_MAX);
1286 	}
1287 
1288 	MAKE_STD_ZVAL(tmp);
1289 	ZVAL_STRINGL(tmp, str, _len, duplicate);
1290 
1291 	return zend_symtable_update(Z_ARRVAL_P(arg), key, key_len, (void *) &tmp, sizeof(zval *), NULL);
1292 }
1293 /* }}} */
1294 
add_assoc_stringl_ex(zval * arg,const char * key,uint key_len,char * str,uint length,int duplicate)1295 ZEND_API int add_assoc_stringl_ex(zval *arg, const char *key, uint key_len, char *str, uint length, int duplicate) /* {{{ */
1296 {
1297 	zval *tmp;
1298 
1299 	if (UNEXPECTED(length > INT_MAX)) {
1300 		zend_error_noreturn(E_ERROR, "String overflow, max size is %d", INT_MAX);
1301 	}
1302 
1303 	MAKE_STD_ZVAL(tmp);
1304 	ZVAL_STRINGL(tmp, str, length, duplicate);
1305 
1306 	return zend_symtable_update(Z_ARRVAL_P(arg), key, key_len, (void *) &tmp, sizeof(zval *), NULL);
1307 }
1308 /* }}} */
1309 
add_assoc_zval_ex(zval * arg,const char * key,uint key_len,zval * value)1310 ZEND_API int add_assoc_zval_ex(zval *arg, const char *key, uint key_len, zval *value) /* {{{ */
1311 {
1312 	return zend_symtable_update(Z_ARRVAL_P(arg), key, key_len, (void *) &value, sizeof(zval *), NULL);
1313 }
1314 /* }}} */
1315 
add_index_long(zval * arg,ulong index,long n)1316 ZEND_API int add_index_long(zval *arg, ulong index, long n) /* {{{ */
1317 {
1318 	zval *tmp;
1319 
1320 	MAKE_STD_ZVAL(tmp);
1321 	ZVAL_LONG(tmp, n);
1322 
1323 	return zend_hash_index_update(Z_ARRVAL_P(arg), index, (void *) &tmp, sizeof(zval *), NULL);
1324 }
1325 /* }}} */
1326 
add_index_null(zval * arg,ulong index)1327 ZEND_API int add_index_null(zval *arg, ulong index) /* {{{ */
1328 {
1329 	zval *tmp;
1330 
1331 	MAKE_STD_ZVAL(tmp);
1332 	ZVAL_NULL(tmp);
1333 
1334 	return zend_hash_index_update(Z_ARRVAL_P(arg), index, (void *) &tmp, sizeof(zval *), NULL);
1335 }
1336 /* }}} */
1337 
add_index_bool(zval * arg,ulong index,int b)1338 ZEND_API int add_index_bool(zval *arg, ulong index, int b) /* {{{ */
1339 {
1340 	zval *tmp;
1341 
1342 	MAKE_STD_ZVAL(tmp);
1343 	ZVAL_BOOL(tmp, b);
1344 
1345 	return zend_hash_index_update(Z_ARRVAL_P(arg), index, (void *) &tmp, sizeof(zval *), NULL);
1346 }
1347 /* }}} */
1348 
add_index_resource(zval * arg,ulong index,int r)1349 ZEND_API int add_index_resource(zval *arg, ulong index, int r) /* {{{ */
1350 {
1351 	zval *tmp;
1352 
1353 	MAKE_STD_ZVAL(tmp);
1354 	ZVAL_RESOURCE(tmp, r);
1355 
1356 	return zend_hash_index_update(Z_ARRVAL_P(arg), index, (void *) &tmp, sizeof(zval *), NULL);
1357 }
1358 /* }}} */
1359 
add_index_double(zval * arg,ulong index,double d)1360 ZEND_API int add_index_double(zval *arg, ulong index, double d) /* {{{ */
1361 {
1362 	zval *tmp;
1363 
1364 	MAKE_STD_ZVAL(tmp);
1365 	ZVAL_DOUBLE(tmp, d);
1366 
1367 	return zend_hash_index_update(Z_ARRVAL_P(arg), index, (void *) &tmp, sizeof(zval *), NULL);
1368 }
1369 /* }}} */
1370 
add_index_string(zval * arg,ulong index,const char * str,int duplicate)1371 ZEND_API int add_index_string(zval *arg, ulong index, const char *str, int duplicate) /* {{{ */
1372 {
1373 	zval *tmp;
1374 	size_t _len = strlen(str);
1375 
1376 	if (UNEXPECTED(_len > INT_MAX)) {
1377 		zend_error_noreturn(E_ERROR, "String overflow, max size is %d", INT_MAX);
1378 	}
1379 
1380 	MAKE_STD_ZVAL(tmp);
1381 	ZVAL_STRING(tmp, str, duplicate);
1382 
1383 	return zend_hash_index_update(Z_ARRVAL_P(arg), index, (void *) &tmp, sizeof(zval *), NULL);
1384 }
1385 /* }}} */
1386 
add_index_stringl(zval * arg,ulong index,const char * str,uint length,int duplicate)1387 ZEND_API int add_index_stringl(zval *arg, ulong index, const char *str, uint length, int duplicate) /* {{{ */
1388 {
1389 	zval *tmp;
1390 
1391 	if (UNEXPECTED(length > INT_MAX)) {
1392 		zend_error_noreturn(E_ERROR, "String overflow, max size is %d", INT_MAX);
1393 	}
1394 
1395 	MAKE_STD_ZVAL(tmp);
1396 	ZVAL_STRINGL(tmp, str, length, duplicate);
1397 
1398 	return zend_hash_index_update(Z_ARRVAL_P(arg), index, (void *) &tmp, sizeof(zval *), NULL);
1399 }
1400 /* }}} */
1401 
add_index_zval(zval * arg,ulong index,zval * value)1402 ZEND_API int add_index_zval(zval *arg, ulong index, zval *value) /* {{{ */
1403 {
1404 	return zend_hash_index_update(Z_ARRVAL_P(arg), index, (void *) &value, sizeof(zval *), NULL);
1405 }
1406 /* }}} */
1407 
add_next_index_long(zval * arg,long n)1408 ZEND_API int add_next_index_long(zval *arg, long n) /* {{{ */
1409 {
1410 	zval *tmp;
1411 
1412 	MAKE_STD_ZVAL(tmp);
1413 	ZVAL_LONG(tmp, n);
1414 
1415 	return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp, sizeof(zval *), NULL);
1416 }
1417 /* }}} */
1418 
add_next_index_null(zval * arg)1419 ZEND_API int add_next_index_null(zval *arg) /* {{{ */
1420 {
1421 	zval *tmp;
1422 
1423 	MAKE_STD_ZVAL(tmp);
1424 	ZVAL_NULL(tmp);
1425 
1426 	return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp, sizeof(zval *), NULL);
1427 }
1428 /* }}} */
1429 
add_next_index_bool(zval * arg,int b)1430 ZEND_API int add_next_index_bool(zval *arg, int b) /* {{{ */
1431 {
1432 	zval *tmp;
1433 
1434 	MAKE_STD_ZVAL(tmp);
1435 	ZVAL_BOOL(tmp, b);
1436 
1437 	return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp, sizeof(zval *), NULL);
1438 }
1439 /* }}} */
1440 
add_next_index_resource(zval * arg,int r)1441 ZEND_API int add_next_index_resource(zval *arg, int r) /* {{{ */
1442 {
1443 	zval *tmp;
1444 
1445 	MAKE_STD_ZVAL(tmp);
1446 	ZVAL_RESOURCE(tmp, r);
1447 
1448 	return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp, sizeof(zval *), NULL);
1449 }
1450 /* }}} */
1451 
add_next_index_double(zval * arg,double d)1452 ZEND_API int add_next_index_double(zval *arg, double d) /* {{{ */
1453 {
1454 	zval *tmp;
1455 
1456 	MAKE_STD_ZVAL(tmp);
1457 	ZVAL_DOUBLE(tmp, d);
1458 
1459 	return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp, sizeof(zval *), NULL);
1460 }
1461 /* }}} */
1462 
add_next_index_string(zval * arg,const char * str,int duplicate)1463 ZEND_API int add_next_index_string(zval *arg, const char *str, int duplicate) /* {{{ */
1464 {
1465 	zval *tmp;
1466 
1467 	MAKE_STD_ZVAL(tmp);
1468 	ZVAL_STRING(tmp, str, duplicate);
1469 
1470 	return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp, sizeof(zval *), NULL);
1471 }
1472 /* }}} */
1473 
add_next_index_stringl(zval * arg,const char * str,uint length,int duplicate)1474 ZEND_API int add_next_index_stringl(zval *arg, const char *str, uint length, int duplicate) /* {{{ */
1475 {
1476 	zval *tmp;
1477 
1478 	if (UNEXPECTED(length > INT_MAX)) {
1479 		zend_error_noreturn(E_ERROR, "String overflow, max size is %d", INT_MAX);
1480 	}
1481 	MAKE_STD_ZVAL(tmp);
1482 	ZVAL_STRINGL(tmp, str, length, duplicate);
1483 
1484 	return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp, sizeof(zval *), NULL);
1485 }
1486 /* }}} */
1487 
add_next_index_zval(zval * arg,zval * value)1488 ZEND_API int add_next_index_zval(zval *arg, zval *value) /* {{{ */
1489 {
1490 	return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &value, sizeof(zval *), NULL);
1491 }
1492 /* }}} */
1493 
add_get_assoc_string_ex(zval * arg,const char * key,uint key_len,const char * str,void ** dest,int duplicate)1494 ZEND_API int add_get_assoc_string_ex(zval *arg, const char *key, uint key_len, const char *str, void **dest, int duplicate) /* {{{ */
1495 {
1496 	zval *tmp;
1497 	size_t _len = strlen(str);
1498 
1499 	if (UNEXPECTED(_len > INT_MAX)) {
1500 		zend_error_noreturn(E_ERROR, "String overflow, max size is %d", INT_MAX);
1501 	}
1502 
1503 	MAKE_STD_ZVAL(tmp);
1504 	ZVAL_STRINGL(tmp, str, _len, duplicate);
1505 
1506 	return zend_symtable_update(Z_ARRVAL_P(arg), key, key_len, (void *) &tmp, sizeof(zval *), dest);
1507 }
1508 /* }}} */
1509 
add_get_assoc_stringl_ex(zval * arg,const char * key,uint key_len,const char * str,uint length,void ** dest,int duplicate)1510 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) /* {{{ */
1511 {
1512 	zval *tmp;
1513 
1514 	if (UNEXPECTED(length > INT_MAX)) {
1515 		zend_error_noreturn(E_ERROR, "String overflow, max size is %d", INT_MAX);
1516 	}
1517 
1518 	MAKE_STD_ZVAL(tmp);
1519 	ZVAL_STRINGL(tmp, str, length, duplicate);
1520 
1521 	return zend_symtable_update(Z_ARRVAL_P(arg), key, key_len, (void *) &tmp, sizeof(zval *), dest);
1522 }
1523 /* }}} */
1524 
add_get_index_long(zval * arg,ulong index,long l,void ** dest)1525 ZEND_API int add_get_index_long(zval *arg, ulong index, long l, void **dest) /* {{{ */
1526 {
1527 	zval *tmp;
1528 
1529 	MAKE_STD_ZVAL(tmp);
1530 	ZVAL_LONG(tmp, l);
1531 
1532 	return zend_hash_index_update(Z_ARRVAL_P(arg), index, (void *) &tmp, sizeof(zval *), dest);
1533 }
1534 /* }}} */
1535 
add_get_index_double(zval * arg,ulong index,double d,void ** dest)1536 ZEND_API int add_get_index_double(zval *arg, ulong index, double d, void **dest) /* {{{ */
1537 {
1538 	zval *tmp;
1539 
1540 	MAKE_STD_ZVAL(tmp);
1541 	ZVAL_DOUBLE(tmp, d);
1542 
1543 	return zend_hash_index_update(Z_ARRVAL_P(arg), index, (void *) &tmp, sizeof(zval *), dest);
1544 }
1545 /* }}} */
1546 
add_get_index_string(zval * arg,ulong index,const char * str,void ** dest,int duplicate)1547 ZEND_API int add_get_index_string(zval *arg, ulong index, const char *str, void **dest, int duplicate) /* {{{ */
1548 {
1549 	zval *tmp;
1550 
1551 	MAKE_STD_ZVAL(tmp);
1552 	ZVAL_STRING(tmp, str, duplicate);
1553 
1554 	return zend_hash_index_update(Z_ARRVAL_P(arg), index, (void *) &tmp, sizeof(zval *), dest);
1555 }
1556 /* }}} */
1557 
add_get_index_stringl(zval * arg,ulong index,const char * str,uint length,void ** dest,int duplicate)1558 ZEND_API int add_get_index_stringl(zval *arg, ulong index, const char *str, uint length, void **dest, int duplicate) /* {{{ */
1559 {
1560 	zval *tmp;
1561 
1562 	MAKE_STD_ZVAL(tmp);
1563 	ZVAL_STRINGL(tmp, str, length, duplicate);
1564 
1565 	return zend_hash_index_update(Z_ARRVAL_P(arg), index, (void *) &tmp, sizeof(zval *), dest);
1566 }
1567 /* }}} */
1568 
array_set_zval_key(HashTable * ht,zval * key,zval * value)1569 ZEND_API int array_set_zval_key(HashTable *ht, zval *key, zval *value) /* {{{ */
1570 {
1571 	int result;
1572 
1573 	switch (Z_TYPE_P(key)) {
1574 		case IS_STRING:
1575 			result = zend_symtable_update(ht, Z_STRVAL_P(key), Z_STRLEN_P(key) + 1, &value, sizeof(zval *), NULL);
1576 			break;
1577 		case IS_NULL:
1578 			result = zend_symtable_update(ht, "", 1, &value, sizeof(zval *), NULL);
1579 			break;
1580 		case IS_RESOURCE:
1581 			zend_error(E_STRICT, "Resource ID#%ld used as offset, casting to integer (%ld)", Z_LVAL_P(key), Z_LVAL_P(key));
1582 			/* break missing intentionally */
1583 		case IS_BOOL:
1584 		case IS_LONG:
1585 			result = zend_hash_index_update(ht, Z_LVAL_P(key), &value, sizeof(zval *), NULL);
1586 			break;
1587 		case IS_DOUBLE:
1588 			result = zend_hash_index_update(ht, zend_dval_to_lval(Z_DVAL_P(key)), &value, sizeof(zval *), NULL);
1589 			break;
1590 		default:
1591 			zend_error(E_WARNING, "Illegal offset type");
1592 			result = FAILURE;
1593 	}
1594 
1595 	if (result == SUCCESS) {
1596 		Z_ADDREF_P(value);
1597 	}
1598 
1599 	return result;
1600 }
1601 /* }}} */
1602 
add_property_long_ex(zval * arg,const char * key,uint key_len,long n TSRMLS_DC)1603 ZEND_API int add_property_long_ex(zval *arg, const char *key, uint key_len, long n TSRMLS_DC) /* {{{ */
1604 {
1605 	zval *tmp;
1606 	zval *z_key;
1607 
1608 	MAKE_STD_ZVAL(tmp);
1609 	ZVAL_LONG(tmp, n);
1610 
1611 	MAKE_STD_ZVAL(z_key);
1612 	ZVAL_STRINGL(z_key, key, key_len-1, 1);
1613 
1614 	Z_OBJ_HANDLER_P(arg, write_property)(arg, z_key, tmp, 0 TSRMLS_CC);
1615 	zval_ptr_dtor(&tmp); /* write_property will add 1 to refcount */
1616 	zval_ptr_dtor(&z_key);
1617 	return SUCCESS;
1618 }
1619 /* }}} */
1620 
add_property_bool_ex(zval * arg,const char * key,uint key_len,int b TSRMLS_DC)1621 ZEND_API int add_property_bool_ex(zval *arg, const char *key, uint key_len, int b TSRMLS_DC) /* {{{ */
1622 {
1623 	zval *tmp;
1624 	zval *z_key;
1625 
1626 	MAKE_STD_ZVAL(tmp);
1627 	ZVAL_BOOL(tmp, b);
1628 
1629 	MAKE_STD_ZVAL(z_key);
1630 	ZVAL_STRINGL(z_key, key, key_len-1, 1);
1631 
1632 	Z_OBJ_HANDLER_P(arg, write_property)(arg, z_key, tmp, 0 TSRMLS_CC);
1633 	zval_ptr_dtor(&tmp); /* write_property will add 1 to refcount */
1634 	zval_ptr_dtor(&z_key);
1635 	return SUCCESS;
1636 }
1637 /* }}} */
1638 
add_property_null_ex(zval * arg,const char * key,uint key_len TSRMLS_DC)1639 ZEND_API int add_property_null_ex(zval *arg, const char *key, uint key_len TSRMLS_DC) /* {{{ */
1640 {
1641 	zval *tmp;
1642 	zval *z_key;
1643 
1644 	MAKE_STD_ZVAL(tmp);
1645 	ZVAL_NULL(tmp);
1646 
1647 	MAKE_STD_ZVAL(z_key);
1648 	ZVAL_STRINGL(z_key, key, key_len-1, 1);
1649 
1650 	Z_OBJ_HANDLER_P(arg, write_property)(arg, z_key, tmp, 0 TSRMLS_CC);
1651 	zval_ptr_dtor(&tmp); /* write_property will add 1 to refcount */
1652 	zval_ptr_dtor(&z_key);
1653 	return SUCCESS;
1654 }
1655 /* }}} */
1656 
add_property_resource_ex(zval * arg,const char * key,uint key_len,long n TSRMLS_DC)1657 ZEND_API int add_property_resource_ex(zval *arg, const char *key, uint key_len, long n TSRMLS_DC) /* {{{ */
1658 {
1659 	zval *tmp;
1660 	zval *z_key;
1661 
1662 	MAKE_STD_ZVAL(tmp);
1663 	ZVAL_RESOURCE(tmp, n);
1664 
1665 	MAKE_STD_ZVAL(z_key);
1666 	ZVAL_STRINGL(z_key, key, key_len-1, 1);
1667 
1668 	Z_OBJ_HANDLER_P(arg, write_property)(arg, z_key, tmp, 0 TSRMLS_CC);
1669 	zval_ptr_dtor(&tmp); /* write_property will add 1 to refcount */
1670 	zval_ptr_dtor(&z_key);
1671 	return SUCCESS;
1672 }
1673 /* }}} */
1674 
add_property_double_ex(zval * arg,const char * key,uint key_len,double d TSRMLS_DC)1675 ZEND_API int add_property_double_ex(zval *arg, const char *key, uint key_len, double d TSRMLS_DC) /* {{{ */
1676 {
1677 	zval *tmp;
1678 	zval *z_key;
1679 
1680 	MAKE_STD_ZVAL(tmp);
1681 	ZVAL_DOUBLE(tmp, d);
1682 
1683 	MAKE_STD_ZVAL(z_key);
1684 	ZVAL_STRINGL(z_key, key, key_len-1, 1);
1685 
1686 	Z_OBJ_HANDLER_P(arg, write_property)(arg, z_key, tmp, 0 TSRMLS_CC);
1687 	zval_ptr_dtor(&tmp); /* write_property will add 1 to refcount */
1688 	zval_ptr_dtor(&z_key);
1689 	return SUCCESS;
1690 }
1691 /* }}} */
1692 
add_property_string_ex(zval * arg,const char * key,uint key_len,const char * str,int duplicate TSRMLS_DC)1693 ZEND_API int add_property_string_ex(zval *arg, const char *key, uint key_len, const char *str, int duplicate TSRMLS_DC) /* {{{ */
1694 {
1695 	zval *tmp;
1696 	zval *z_key;
1697 	size_t _len = strlen(str);
1698 
1699 	if (UNEXPECTED(_len > INT_MAX)) {
1700 		zend_error_noreturn(E_ERROR, "String overflow, max size is %d", INT_MAX);
1701 	}
1702 
1703 	MAKE_STD_ZVAL(tmp);
1704 	ZVAL_STRINGL(tmp, str, _len, duplicate);
1705 
1706 	MAKE_STD_ZVAL(z_key);
1707 	ZVAL_STRINGL(z_key, key, key_len-1, 1);
1708 
1709 	Z_OBJ_HANDLER_P(arg, write_property)(arg, z_key, tmp, 0 TSRMLS_CC);
1710 	zval_ptr_dtor(&tmp); /* write_property will add 1 to refcount */
1711 	zval_ptr_dtor(&z_key);
1712 	return SUCCESS;
1713 }
1714 /* }}} */
1715 
add_property_stringl_ex(zval * arg,const char * key,uint key_len,const char * str,uint length,int duplicate TSRMLS_DC)1716 ZEND_API int add_property_stringl_ex(zval *arg, const char *key, uint key_len, const char *str, uint length, int duplicate TSRMLS_DC) /* {{{ */
1717 {
1718 	zval *tmp;
1719 	zval *z_key;
1720 
1721 	if (UNEXPECTED(length > INT_MAX)) {
1722 		zend_error_noreturn(E_ERROR, "String overflow, max size is %d", INT_MAX);
1723 	}
1724 
1725 	MAKE_STD_ZVAL(tmp);
1726 	ZVAL_STRINGL(tmp, str, length, duplicate);
1727 
1728 	MAKE_STD_ZVAL(z_key);
1729 	ZVAL_STRINGL(z_key, key, key_len-1, 1);
1730 
1731 	Z_OBJ_HANDLER_P(arg, write_property)(arg, z_key, tmp, 0 TSRMLS_CC);
1732 	zval_ptr_dtor(&tmp); /* write_property will add 1 to refcount */
1733 	zval_ptr_dtor(&z_key);
1734 	return SUCCESS;
1735 }
1736 /* }}} */
1737 
add_property_zval_ex(zval * arg,const char * key,uint key_len,zval * value TSRMLS_DC)1738 ZEND_API int add_property_zval_ex(zval *arg, const char *key, uint key_len, zval *value TSRMLS_DC) /* {{{ */
1739 {
1740 	zval *z_key;
1741 
1742 	MAKE_STD_ZVAL(z_key);
1743 	ZVAL_STRINGL(z_key, key, key_len-1, 1);
1744 
1745 	Z_OBJ_HANDLER_P(arg, write_property)(arg, z_key, value, 0 TSRMLS_CC);
1746 	zval_ptr_dtor(&z_key);
1747 	return SUCCESS;
1748 }
1749 /* }}} */
1750 
zend_startup_module_ex(zend_module_entry * module TSRMLS_DC)1751 ZEND_API int zend_startup_module_ex(zend_module_entry *module TSRMLS_DC) /* {{{ */
1752 {
1753 	int name_len;
1754 	char *lcname;
1755 
1756 	if (module->module_started) {
1757 		return SUCCESS;
1758 	}
1759 	module->module_started = 1;
1760 
1761 	/* Check module dependencies */
1762 	if (module->deps) {
1763 		const zend_module_dep *dep = module->deps;
1764 
1765 		while (dep->name) {
1766 			if (dep->type == MODULE_DEP_REQUIRED) {
1767 				zend_module_entry *req_mod;
1768 
1769 				name_len = strlen(dep->name);
1770 				lcname = zend_str_tolower_dup(dep->name, name_len);
1771 
1772 				if (zend_hash_find(&module_registry, lcname, name_len+1, (void**)&req_mod) == FAILURE || !req_mod->module_started) {
1773 					efree(lcname);
1774 					/* TODO: Check version relationship */
1775 					zend_error(E_CORE_WARNING, "Cannot load module '%s' because required module '%s' is not loaded", module->name, dep->name);
1776 					module->module_started = 0;
1777 					return FAILURE;
1778 				}
1779 				efree(lcname);
1780 			}
1781 			++dep;
1782 		}
1783 	}
1784 
1785 	/* Initialize module globals */
1786 	if (module->globals_size) {
1787 #ifdef ZTS
1788 		ts_allocate_id(module->globals_id_ptr, module->globals_size, (ts_allocate_ctor) module->globals_ctor, (ts_allocate_dtor) module->globals_dtor);
1789 #else
1790 		if (module->globals_ctor) {
1791 			module->globals_ctor(module->globals_ptr TSRMLS_CC);
1792 		}
1793 #endif
1794 	}
1795 	if (module->module_startup_func) {
1796 		EG(current_module) = module;
1797 		if (module->module_startup_func(module->type, module->module_number TSRMLS_CC)==FAILURE) {
1798 			zend_error(E_CORE_ERROR,"Unable to start %s module", module->name);
1799 			EG(current_module) = NULL;
1800 			return FAILURE;
1801 		}
1802 		EG(current_module) = NULL;
1803 	}
1804 	return SUCCESS;
1805 }
1806 /* }}} */
1807 
zend_startup_module_int(zend_module_entry * module TSRMLS_DC)1808 static int zend_startup_module_int(zend_module_entry *module TSRMLS_DC) /* {{{ */
1809 {
1810 	return (zend_startup_module_ex(module TSRMLS_CC) == SUCCESS) ? ZEND_HASH_APPLY_KEEP : ZEND_HASH_APPLY_REMOVE;
1811 }
1812 /* }}} */
1813 
zend_sort_modules(void * base,size_t count,size_t siz,compare_func_t compare TSRMLS_DC)1814 static void zend_sort_modules(void *base, size_t count, size_t siz, compare_func_t compare TSRMLS_DC) /* {{{ */
1815 {
1816 	Bucket **b1 = base;
1817 	Bucket **b2;
1818 	Bucket **end = b1 + count;
1819 	Bucket *tmp;
1820 	zend_module_entry *m, *r;
1821 
1822 	while (b1 < end) {
1823 try_again:
1824 		m = (zend_module_entry*)(*b1)->pData;
1825 		if (!m->module_started && m->deps) {
1826 			const zend_module_dep *dep = m->deps;
1827 			while (dep->name) {
1828 				if (dep->type == MODULE_DEP_REQUIRED || dep->type == MODULE_DEP_OPTIONAL) {
1829 					b2 = b1 + 1;
1830 					while (b2 < end) {
1831 						r = (zend_module_entry*)(*b2)->pData;
1832 						if (strcasecmp(dep->name, r->name) == 0) {
1833 							tmp = *b1;
1834 							*b1 = *b2;
1835 							*b2 = tmp;
1836 							goto try_again;
1837 						}
1838 						b2++;
1839 					}
1840 				}
1841 				dep++;
1842 			}
1843 		}
1844 		b1++;
1845 	}
1846 }
1847 /* }}} */
1848 
zend_collect_module_handlers(TSRMLS_D)1849 ZEND_API void zend_collect_module_handlers(TSRMLS_D) /* {{{ */
1850 {
1851 	HashPosition pos;
1852 	zend_module_entry *module;
1853 	int startup_count = 0;
1854 	int shutdown_count = 0;
1855 	int post_deactivate_count = 0;
1856 	zend_class_entry **pce;
1857 	int class_count = 0;
1858 
1859 	/* Collect extensions with request startup/shutdown handlers */
1860 	for (zend_hash_internal_pointer_reset_ex(&module_registry, &pos);
1861 	     zend_hash_get_current_data_ex(&module_registry, (void *) &module, &pos) == SUCCESS;
1862 	     zend_hash_move_forward_ex(&module_registry, &pos)) {
1863 		if (module->request_startup_func) {
1864 			startup_count++;
1865 		}
1866 		if (module->request_shutdown_func) {
1867 			shutdown_count++;
1868 		}
1869 		if (module->post_deactivate_func) {
1870 			post_deactivate_count++;
1871 		}
1872 	}
1873 	module_request_startup_handlers = (zend_module_entry**)malloc(
1874 	    sizeof(zend_module_entry*) *
1875 		(startup_count + 1 +
1876 		 shutdown_count + 1 +
1877 		 post_deactivate_count + 1));
1878 	module_request_startup_handlers[startup_count] = NULL;
1879 	module_request_shutdown_handlers = module_request_startup_handlers + startup_count + 1;
1880 	module_request_shutdown_handlers[shutdown_count] = NULL;
1881 	module_post_deactivate_handlers = module_request_shutdown_handlers + shutdown_count + 1;
1882 	module_post_deactivate_handlers[post_deactivate_count] = NULL;
1883 	startup_count = 0;
1884 
1885 	for (zend_hash_internal_pointer_reset_ex(&module_registry, &pos);
1886 	     zend_hash_get_current_data_ex(&module_registry, (void *) &module, &pos) == SUCCESS;
1887 	     zend_hash_move_forward_ex(&module_registry, &pos)) {
1888 		if (module->request_startup_func) {
1889 			module_request_startup_handlers[startup_count++] = module;
1890 		}
1891 		if (module->request_shutdown_func) {
1892 			module_request_shutdown_handlers[--shutdown_count] = module;
1893 		}
1894 		if (module->post_deactivate_func) {
1895 			module_post_deactivate_handlers[--post_deactivate_count] = module;
1896 		}
1897 	}
1898 
1899 	/* Collect internal classes with static members */
1900 	for (zend_hash_internal_pointer_reset_ex(CG(class_table), &pos);
1901 	     zend_hash_get_current_data_ex(CG(class_table), (void *) &pce, &pos) == SUCCESS;
1902 	     zend_hash_move_forward_ex(CG(class_table), &pos)) {
1903 		if ((*pce)->type == ZEND_INTERNAL_CLASS &&
1904 		    (*pce)->default_static_members_count > 0) {
1905 		    class_count++;
1906 		}
1907 	}
1908 
1909 	class_cleanup_handlers = (zend_class_entry**)malloc(
1910 		sizeof(zend_class_entry*) *
1911 		(class_count + 1));
1912 	class_cleanup_handlers[class_count] = NULL;
1913 
1914 	if (class_count) {
1915 		for (zend_hash_internal_pointer_reset_ex(CG(class_table), &pos);
1916 		     zend_hash_get_current_data_ex(CG(class_table), (void *) &pce, &pos) == SUCCESS;
1917 	    	 zend_hash_move_forward_ex(CG(class_table), &pos)) {
1918 			if ((*pce)->type == ZEND_INTERNAL_CLASS &&
1919 			    (*pce)->default_static_members_count > 0) {
1920 			    class_cleanup_handlers[--class_count] = *pce;
1921 			}
1922 		}
1923 	}
1924 }
1925 /* }}} */
1926 
zend_startup_modules(TSRMLS_D)1927 ZEND_API int zend_startup_modules(TSRMLS_D) /* {{{ */
1928 {
1929 	zend_hash_sort(&module_registry, zend_sort_modules, NULL, 0 TSRMLS_CC);
1930 	zend_hash_apply(&module_registry, (apply_func_t)zend_startup_module_int TSRMLS_CC);
1931 	return SUCCESS;
1932 }
1933 /* }}} */
1934 
zend_destroy_modules(void)1935 ZEND_API void zend_destroy_modules(void) /* {{{ */
1936 {
1937 	free(class_cleanup_handlers);
1938 	free(module_request_startup_handlers);
1939 	zend_hash_graceful_reverse_destroy(&module_registry);
1940 }
1941 /* }}} */
1942 
zend_register_module_ex(zend_module_entry * module TSRMLS_DC)1943 ZEND_API zend_module_entry* zend_register_module_ex(zend_module_entry *module TSRMLS_DC) /* {{{ */
1944 {
1945 	int name_len;
1946 	char *lcname;
1947 	zend_module_entry *module_ptr;
1948 
1949 	if (!module) {
1950 		return NULL;
1951 	}
1952 
1953 #if 0
1954 	zend_printf("%s: Registering module %d\n", module->name, module->module_number);
1955 #endif
1956 
1957 	/* Check module dependencies */
1958 	if (module->deps) {
1959 		const zend_module_dep *dep = module->deps;
1960 
1961 		while (dep->name) {
1962 			if (dep->type == MODULE_DEP_CONFLICTS) {
1963 				name_len = strlen(dep->name);
1964 				lcname = zend_str_tolower_dup(dep->name, name_len);
1965 
1966 				if (zend_hash_exists(&module_registry, lcname, name_len+1)) {
1967 					efree(lcname);
1968 					/* TODO: Check version relationship */
1969 					zend_error(E_CORE_WARNING, "Cannot load module '%s' because conflicting module '%s' is already loaded", module->name, dep->name);
1970 					return NULL;
1971 				}
1972 				efree(lcname);
1973 			}
1974 			++dep;
1975 		}
1976 	}
1977 
1978 	name_len = strlen(module->name);
1979 	lcname = zend_str_tolower_dup(module->name, name_len);
1980 
1981 	if (zend_hash_add(&module_registry, lcname, name_len+1, (void *)module, sizeof(zend_module_entry), (void**)&module_ptr)==FAILURE) {
1982 		zend_error(E_CORE_WARNING, "Module '%s' already loaded", module->name);
1983 		efree(lcname);
1984 		return NULL;
1985 	}
1986 	efree(lcname);
1987 	module = module_ptr;
1988 	EG(current_module) = module;
1989 
1990 	if (module->functions && zend_register_functions(NULL, module->functions, NULL, module->type TSRMLS_CC)==FAILURE) {
1991 		EG(current_module) = NULL;
1992 		zend_error(E_CORE_WARNING,"%s: Unable to register functions, unable to load", module->name);
1993 		return NULL;
1994 	}
1995 
1996 	EG(current_module) = NULL;
1997 	return module;
1998 }
1999 /* }}} */
2000 
zend_register_internal_module(zend_module_entry * module TSRMLS_DC)2001 ZEND_API zend_module_entry* zend_register_internal_module(zend_module_entry *module TSRMLS_DC) /* {{{ */
2002 {
2003 	module->module_number = zend_next_free_module();
2004 	module->type = MODULE_PERSISTENT;
2005 	return zend_register_module_ex(module TSRMLS_CC);
2006 }
2007 /* }}} */
2008 
zend_check_magic_method_implementation(const zend_class_entry * ce,const zend_function * fptr,int error_type TSRMLS_DC)2009 ZEND_API void zend_check_magic_method_implementation(const zend_class_entry *ce, const zend_function *fptr, int error_type TSRMLS_DC) /* {{{ */
2010 {
2011 	char lcname[16];
2012 	int name_len;
2013 
2014 	/* we don't care if the function name is longer, in fact lowercasing only
2015 	 * the beginning of the name speeds up the check process */
2016 	name_len = strlen(fptr->common.function_name);
2017 	zend_str_tolower_copy(lcname, fptr->common.function_name, MIN(name_len, sizeof(lcname)-1));
2018 	lcname[sizeof(lcname)-1] = '\0'; /* zend_str_tolower_copy won't necessarily set the zero byte */
2019 
2020 	if (name_len == sizeof(ZEND_DESTRUCTOR_FUNC_NAME) - 1 && !memcmp(lcname, ZEND_DESTRUCTOR_FUNC_NAME, sizeof(ZEND_DESTRUCTOR_FUNC_NAME) - 1) && fptr->common.num_args != 0) {
2021 		zend_error(error_type, "Destructor %s::%s() cannot take arguments", ce->name, ZEND_DESTRUCTOR_FUNC_NAME);
2022 	} else if (name_len == sizeof(ZEND_CLONE_FUNC_NAME) - 1 && !memcmp(lcname, ZEND_CLONE_FUNC_NAME, sizeof(ZEND_CLONE_FUNC_NAME) - 1) && fptr->common.num_args != 0) {
2023 		zend_error(error_type, "Method %s::%s() cannot accept any arguments", ce->name, ZEND_CLONE_FUNC_NAME);
2024 	} else if (name_len == sizeof(ZEND_GET_FUNC_NAME) - 1 && !memcmp(lcname, ZEND_GET_FUNC_NAME, sizeof(ZEND_GET_FUNC_NAME) - 1)) {
2025 		if (fptr->common.num_args != 1) {
2026 			zend_error(error_type, "Method %s::%s() must take exactly 1 argument", ce->name, ZEND_GET_FUNC_NAME);
2027 		} else if (ARG_SHOULD_BE_SENT_BY_REF(fptr, 1)) {
2028 			zend_error(error_type, "Method %s::%s() cannot take arguments by reference", ce->name, ZEND_GET_FUNC_NAME);
2029 		}
2030 	} else if (name_len == sizeof(ZEND_SET_FUNC_NAME) - 1 && !memcmp(lcname, ZEND_SET_FUNC_NAME, sizeof(ZEND_SET_FUNC_NAME) - 1)) {
2031 		if (fptr->common.num_args != 2) {
2032 			zend_error(error_type, "Method %s::%s() must take exactly 2 arguments", ce->name, ZEND_SET_FUNC_NAME);
2033 		} else if (ARG_SHOULD_BE_SENT_BY_REF(fptr, 1) || ARG_SHOULD_BE_SENT_BY_REF(fptr, 2)) {
2034 			zend_error(error_type, "Method %s::%s() cannot take arguments by reference", ce->name, ZEND_SET_FUNC_NAME);
2035 		}
2036 	} else if (name_len == sizeof(ZEND_UNSET_FUNC_NAME) - 1 && !memcmp(lcname, ZEND_UNSET_FUNC_NAME, sizeof(ZEND_UNSET_FUNC_NAME) - 1)) {
2037 		if (fptr->common.num_args != 1) {
2038 			zend_error(error_type, "Method %s::%s() must take exactly 1 argument", ce->name, ZEND_UNSET_FUNC_NAME);
2039 		} else if (ARG_SHOULD_BE_SENT_BY_REF(fptr, 1)) {
2040 			zend_error(error_type, "Method %s::%s() cannot take arguments by reference", ce->name, ZEND_UNSET_FUNC_NAME);
2041 		}
2042 	} else if (name_len == sizeof(ZEND_ISSET_FUNC_NAME) - 1 && !memcmp(lcname, ZEND_ISSET_FUNC_NAME, sizeof(ZEND_ISSET_FUNC_NAME) - 1)) {
2043 		if (fptr->common.num_args != 1) {
2044 			zend_error(error_type, "Method %s::%s() must take exactly 1 argument", ce->name, ZEND_ISSET_FUNC_NAME);
2045 		} else if (ARG_SHOULD_BE_SENT_BY_REF(fptr, 1)) {
2046 			zend_error(error_type, "Method %s::%s() cannot take arguments by reference", ce->name, ZEND_ISSET_FUNC_NAME);
2047 		}
2048 	} else if (name_len == sizeof(ZEND_CALL_FUNC_NAME) - 1 && !memcmp(lcname, ZEND_CALL_FUNC_NAME, sizeof(ZEND_CALL_FUNC_NAME) - 1)) {
2049 		if (fptr->common.num_args != 2) {
2050 			zend_error(error_type, "Method %s::%s() must take exactly 2 arguments", ce->name, ZEND_CALL_FUNC_NAME);
2051 		} else if (ARG_SHOULD_BE_SENT_BY_REF(fptr, 1) || ARG_SHOULD_BE_SENT_BY_REF(fptr, 2)) {
2052 			zend_error(error_type, "Method %s::%s() cannot take arguments by reference", ce->name, ZEND_CALL_FUNC_NAME);
2053 		}
2054 	} else if (name_len == sizeof(ZEND_CALLSTATIC_FUNC_NAME) - 1 &&
2055 		!memcmp(lcname, ZEND_CALLSTATIC_FUNC_NAME, sizeof(ZEND_CALLSTATIC_FUNC_NAME)-1)
2056 	) {
2057 		if (fptr->common.num_args != 2) {
2058 			zend_error(error_type, "Method %s::%s() must take exactly 2 arguments", ce->name, ZEND_CALLSTATIC_FUNC_NAME);
2059 		} else if (ARG_SHOULD_BE_SENT_BY_REF(fptr, 1) || ARG_SHOULD_BE_SENT_BY_REF(fptr, 2)) {
2060 			zend_error(error_type, "Method %s::%s() cannot take arguments by reference", ce->name, ZEND_CALLSTATIC_FUNC_NAME);
2061 		}
2062  	} else if (name_len == sizeof(ZEND_TOSTRING_FUNC_NAME) - 1 &&
2063  		!memcmp(lcname, ZEND_TOSTRING_FUNC_NAME, sizeof(ZEND_TOSTRING_FUNC_NAME)-1) && fptr->common.num_args != 0
2064 	) {
2065 		zend_error(error_type, "Method %s::%s() cannot take arguments", ce->name, ZEND_TOSTRING_FUNC_NAME);
2066 	} else if (name_len == sizeof(ZEND_DEBUGINFO_FUNC_NAME) - 1 &&
2067 		!memcmp(lcname, ZEND_DEBUGINFO_FUNC_NAME, sizeof(ZEND_DEBUGINFO_FUNC_NAME)-1) && fptr->common.num_args != 0) {
2068 		zend_error(error_type, "Method %s::%s() cannot take arguments", ce->name, ZEND_DEBUGINFO_FUNC_NAME);
2069 	}
2070 }
2071 /* }}} */
2072 
2073 /* 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)2074 ZEND_API int zend_register_functions(zend_class_entry *scope, const zend_function_entry *functions, HashTable *function_table, int type TSRMLS_DC) /* {{{ */
2075 {
2076 	const zend_function_entry *ptr = functions;
2077 	zend_function function, *reg_function;
2078 	zend_internal_function *internal_function = (zend_internal_function *)&function;
2079 	int count=0, unload=0;
2080 	HashTable *target_function_table = function_table;
2081 	int error_type;
2082 	zend_function *ctor = NULL, *dtor = NULL, *clone = NULL, *__get = NULL, *__set = NULL, *__unset = NULL, *__isset = NULL, *__call = NULL, *__callstatic = NULL, *__tostring = NULL, *__debugInfo = NULL;
2083 	const char *lowercase_name;
2084 	int fname_len;
2085 	const char *lc_class_name = NULL;
2086 	int class_name_len = 0;
2087 	zend_ulong hash;
2088 
2089 	if (type==MODULE_PERSISTENT) {
2090 		error_type = E_CORE_WARNING;
2091 	} else {
2092 		error_type = E_WARNING;
2093 	}
2094 
2095 	if (!target_function_table) {
2096 		target_function_table = CG(function_table);
2097 	}
2098 	internal_function->type = ZEND_INTERNAL_FUNCTION;
2099 	internal_function->module = EG(current_module);
2100 
2101 	if (scope) {
2102 		class_name_len = strlen(scope->name);
2103 		if ((lc_class_name = zend_memrchr(scope->name, '\\', class_name_len))) {
2104 			++lc_class_name;
2105 			class_name_len -= (lc_class_name - scope->name);
2106 			lc_class_name = zend_str_tolower_dup(lc_class_name, class_name_len);
2107 		} else {
2108 			lc_class_name = zend_str_tolower_dup(scope->name, class_name_len);
2109 		}
2110 	}
2111 
2112 	while (ptr->fname) {
2113 		internal_function->handler = ptr->handler;
2114 		internal_function->function_name = (char*)ptr->fname;
2115 		internal_function->scope = scope;
2116 		internal_function->prototype = NULL;
2117 		if (ptr->flags) {
2118 			if (!(ptr->flags & ZEND_ACC_PPP_MASK)) {
2119 				if (ptr->flags != ZEND_ACC_DEPRECATED || scope) {
2120 					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);
2121 				}
2122 				internal_function->fn_flags = ZEND_ACC_PUBLIC | ptr->flags;
2123 			} else {
2124 				internal_function->fn_flags = ptr->flags;
2125 			}
2126 		} else {
2127 			internal_function->fn_flags = ZEND_ACC_PUBLIC;
2128 		}
2129 		if (ptr->arg_info) {
2130 			zend_internal_function_info *info = (zend_internal_function_info*)ptr->arg_info;
2131 
2132 			internal_function->arg_info = (zend_arg_info*)ptr->arg_info+1;
2133 			internal_function->num_args = ptr->num_args;
2134 			/* Currently you cannot denote that the function can accept less arguments than num_args */
2135 			if (info->required_num_args == -1) {
2136 				internal_function->required_num_args = ptr->num_args;
2137 			} else {
2138 				internal_function->required_num_args = info->required_num_args;
2139 			}
2140 			if (info->return_reference) {
2141 				internal_function->fn_flags |= ZEND_ACC_RETURN_REFERENCE;
2142 			}
2143 			if (ptr->arg_info[ptr->num_args].is_variadic) {
2144 				internal_function->fn_flags |= ZEND_ACC_VARIADIC;
2145 			}
2146 		} else {
2147 			internal_function->arg_info = NULL;
2148 			internal_function->num_args = 0;
2149 			internal_function->required_num_args = 0;
2150 		}
2151 		if (ptr->flags & ZEND_ACC_ABSTRACT) {
2152 			if (scope) {
2153 				/* This is a class that must be abstract itself. Here we set the check info. */
2154 				scope->ce_flags |= ZEND_ACC_IMPLICIT_ABSTRACT_CLASS;
2155 				if (!(scope->ce_flags & ZEND_ACC_INTERFACE)) {
2156 					/* Since the class is not an interface it needs to be declared as a abstract class. */
2157 					/* Since here we are handling internal functions only we can add the keyword flag. */
2158 					/* This time we set the flag for the keyword 'abstract'. */
2159 					scope->ce_flags |= ZEND_ACC_EXPLICIT_ABSTRACT_CLASS;
2160 				}
2161 			}
2162 			if (ptr->flags & ZEND_ACC_STATIC && (!scope || !(scope->ce_flags & ZEND_ACC_INTERFACE))) {
2163 				zend_error(error_type, "Static function %s%s%s() cannot be abstract", scope ? scope->name : "", scope ? "::" : "", ptr->fname);
2164 			}
2165 		} else {
2166 			if (scope && (scope->ce_flags & ZEND_ACC_INTERFACE)) {
2167 				efree((char*)lc_class_name);
2168 				zend_error(error_type, "Interface %s cannot contain non abstract method %s()", scope->name, ptr->fname);
2169 				return FAILURE;
2170 			}
2171 			if (!internal_function->handler) {
2172 				if (scope) {
2173 					efree((char*)lc_class_name);
2174 				}
2175 				zend_error(error_type, "Method %s%s%s() cannot be a NULL function", scope ? scope->name : "", scope ? "::" : "", ptr->fname);
2176 				zend_unregister_functions(functions, count, target_function_table TSRMLS_CC);
2177 				return FAILURE;
2178 			}
2179 		}
2180 		fname_len = strlen(ptr->fname);
2181 		lowercase_name = zend_new_interned_string(zend_str_tolower_dup(ptr->fname, fname_len), fname_len + 1, 1 TSRMLS_CC);
2182 		hash = str_hash(lowercase_name, fname_len);
2183 		if (zend_hash_quick_add(target_function_table, lowercase_name, fname_len+1, hash, &function, sizeof(zend_function), (void**)&reg_function) == FAILURE) {
2184 			unload=1;
2185 			str_efree(lowercase_name);
2186 			break;
2187 		}
2188 
2189 		/* If types of arguments have to be checked */
2190 		if (reg_function->common.arg_info && reg_function->common.num_args) {
2191 			int i;
2192 			for (i = 0; i < reg_function->common.num_args; i++) {
2193 				if (reg_function->common.arg_info[i].class_name ||
2194 				    reg_function->common.arg_info[i].type_hint) {
2195 				    reg_function->common.fn_flags |= ZEND_ACC_HAS_TYPE_HINTS;
2196 					break;
2197 				}
2198 			}
2199 		}
2200 
2201 		if (scope) {
2202 			/* Look for ctor, dtor, clone
2203 			 * If it's an old-style constructor, store it only if we don't have
2204 			 * a constructor already.
2205 			 */
2206 			if ((fname_len == class_name_len) && !ctor && !memcmp(lowercase_name, lc_class_name, class_name_len+1)) {
2207 				ctor = reg_function;
2208 			} else if ((fname_len == sizeof(ZEND_CONSTRUCTOR_FUNC_NAME)-1) && !memcmp(lowercase_name, ZEND_CONSTRUCTOR_FUNC_NAME, sizeof(ZEND_CONSTRUCTOR_FUNC_NAME) - 1)) {
2209 				ctor = reg_function;
2210 			} else if ((fname_len == sizeof(ZEND_DESTRUCTOR_FUNC_NAME)-1) && !memcmp(lowercase_name, ZEND_DESTRUCTOR_FUNC_NAME, sizeof(ZEND_DESTRUCTOR_FUNC_NAME) - 1)) {
2211 				dtor = reg_function;
2212 				if (internal_function->num_args) {
2213 					zend_error(error_type, "Destructor %s::%s() cannot take arguments", scope->name, ptr->fname);
2214 				}
2215 			} else if ((fname_len == sizeof(ZEND_CLONE_FUNC_NAME)-1) && !memcmp(lowercase_name, ZEND_CLONE_FUNC_NAME, sizeof(ZEND_CLONE_FUNC_NAME) - 1)) {
2216 				clone = reg_function;
2217 			} else if ((fname_len == sizeof(ZEND_CALL_FUNC_NAME)-1) && !memcmp(lowercase_name, ZEND_CALL_FUNC_NAME, sizeof(ZEND_CALL_FUNC_NAME) - 1)) {
2218 				__call = reg_function;
2219 			} else if ((fname_len == sizeof(ZEND_CALLSTATIC_FUNC_NAME)-1) && !memcmp(lowercase_name, ZEND_CALLSTATIC_FUNC_NAME, sizeof(ZEND_CALLSTATIC_FUNC_NAME) - 1)) {
2220 				__callstatic = reg_function;
2221 			} else if ((fname_len == sizeof(ZEND_TOSTRING_FUNC_NAME)-1) && !memcmp(lowercase_name, ZEND_TOSTRING_FUNC_NAME, sizeof(ZEND_TOSTRING_FUNC_NAME) - 1)) {
2222 				__tostring = reg_function;
2223 			} else if ((fname_len == sizeof(ZEND_GET_FUNC_NAME)-1) && !memcmp(lowercase_name, ZEND_GET_FUNC_NAME, sizeof(ZEND_GET_FUNC_NAME) - 1)) {
2224 				__get = reg_function;
2225 			} else if ((fname_len == sizeof(ZEND_SET_FUNC_NAME)-1) && !memcmp(lowercase_name, ZEND_SET_FUNC_NAME, sizeof(ZEND_SET_FUNC_NAME) - 1)) {
2226 				__set = reg_function;
2227 			} else if ((fname_len == sizeof(ZEND_UNSET_FUNC_NAME)-1) && !memcmp(lowercase_name, ZEND_UNSET_FUNC_NAME, sizeof(ZEND_UNSET_FUNC_NAME) - 1)) {
2228 				__unset = reg_function;
2229 			} else if ((fname_len == sizeof(ZEND_ISSET_FUNC_NAME)-1) && !memcmp(lowercase_name, ZEND_ISSET_FUNC_NAME, sizeof(ZEND_ISSET_FUNC_NAME) - 1)) {
2230 				__isset = reg_function;
2231 			} else if ((fname_len == sizeof(ZEND_DEBUGINFO_FUNC_NAME)-1) && !memcmp(lowercase_name, ZEND_DEBUGINFO_FUNC_NAME, sizeof(ZEND_DEBUGINFO_FUNC_NAME) - 1)) {
2232 				__debugInfo = reg_function;
2233 			} else {
2234 				reg_function = NULL;
2235 			}
2236 			if (reg_function) {
2237 				zend_check_magic_method_implementation(scope, reg_function, error_type TSRMLS_CC);
2238 			}
2239 		}
2240 		ptr++;
2241 		count++;
2242 		str_efree(lowercase_name);
2243 	}
2244 	if (unload) { /* before unloading, display all remaining bad function in the module */
2245 		if (scope) {
2246 			efree((char*)lc_class_name);
2247 		}
2248 		while (ptr->fname) {
2249 			fname_len = strlen(ptr->fname);
2250 			lowercase_name = zend_str_tolower_dup(ptr->fname, fname_len);
2251 			if (zend_hash_exists(target_function_table, lowercase_name, fname_len+1)) {
2252 				zend_error(error_type, "Function registration failed - duplicate name - %s%s%s", scope ? scope->name : "", scope ? "::" : "", ptr->fname);
2253 			}
2254 			efree((char*)lowercase_name);
2255 			ptr++;
2256 		}
2257 		zend_unregister_functions(functions, count, target_function_table TSRMLS_CC);
2258 		return FAILURE;
2259 	}
2260 	if (scope) {
2261 		scope->constructor = ctor;
2262 		scope->destructor = dtor;
2263 		scope->clone = clone;
2264 		scope->__call = __call;
2265 		scope->__callstatic = __callstatic;
2266 		scope->__tostring = __tostring;
2267 		scope->__get = __get;
2268 		scope->__set = __set;
2269 		scope->__unset = __unset;
2270 		scope->__isset = __isset;
2271 		scope->__debugInfo = __debugInfo;
2272 		if (ctor) {
2273 			ctor->common.fn_flags |= ZEND_ACC_CTOR;
2274 			if (ctor->common.fn_flags & ZEND_ACC_STATIC) {
2275 				zend_error(error_type, "Constructor %s::%s() cannot be static", scope->name, ctor->common.function_name);
2276 			}
2277 			ctor->common.fn_flags &= ~ZEND_ACC_ALLOW_STATIC;
2278 		}
2279 		if (dtor) {
2280 			dtor->common.fn_flags |= ZEND_ACC_DTOR;
2281 			if (dtor->common.fn_flags & ZEND_ACC_STATIC) {
2282 				zend_error(error_type, "Destructor %s::%s() cannot be static", scope->name, dtor->common.function_name);
2283 			}
2284 			dtor->common.fn_flags &= ~ZEND_ACC_ALLOW_STATIC;
2285 		}
2286 		if (clone) {
2287 			clone->common.fn_flags |= ZEND_ACC_CLONE;
2288 			if (clone->common.fn_flags & ZEND_ACC_STATIC) {
2289 				zend_error(error_type, "Constructor %s::%s() cannot be static", scope->name, clone->common.function_name);
2290 			}
2291 			clone->common.fn_flags &= ~ZEND_ACC_ALLOW_STATIC;
2292 		}
2293 		if (__call) {
2294 			if (__call->common.fn_flags & ZEND_ACC_STATIC) {
2295 				zend_error(error_type, "Method %s::%s() cannot be static", scope->name, __call->common.function_name);
2296 			}
2297 			__call->common.fn_flags &= ~ZEND_ACC_ALLOW_STATIC;
2298 		}
2299 		if (__callstatic) {
2300 			if (!(__callstatic->common.fn_flags & ZEND_ACC_STATIC)) {
2301 				zend_error(error_type, "Method %s::%s() must be static", scope->name, __callstatic->common.function_name);
2302 			}
2303 			__callstatic->common.fn_flags |= ZEND_ACC_STATIC;
2304 		}
2305 		if (__tostring) {
2306 			if (__tostring->common.fn_flags & ZEND_ACC_STATIC) {
2307 				zend_error(error_type, "Method %s::%s() cannot be static", scope->name, __tostring->common.function_name);
2308 			}
2309 			__tostring->common.fn_flags &= ~ZEND_ACC_ALLOW_STATIC;
2310 		}
2311 		if (__get) {
2312 			if (__get->common.fn_flags & ZEND_ACC_STATIC) {
2313 				zend_error(error_type, "Method %s::%s() cannot be static", scope->name, __get->common.function_name);
2314 			}
2315 			__get->common.fn_flags &= ~ZEND_ACC_ALLOW_STATIC;
2316 		}
2317 		if (__set) {
2318 			if (__set->common.fn_flags & ZEND_ACC_STATIC) {
2319 				zend_error(error_type, "Method %s::%s() cannot be static", scope->name, __set->common.function_name);
2320 			}
2321 			__set->common.fn_flags &= ~ZEND_ACC_ALLOW_STATIC;
2322 		}
2323 		if (__unset) {
2324 			if (__unset->common.fn_flags & ZEND_ACC_STATIC) {
2325 				zend_error(error_type, "Method %s::%s() cannot be static", scope->name, __unset->common.function_name);
2326 			}
2327 			__unset->common.fn_flags &= ~ZEND_ACC_ALLOW_STATIC;
2328 		}
2329 		if (__isset) {
2330 			if (__isset->common.fn_flags & ZEND_ACC_STATIC) {
2331 				zend_error(error_type, "Method %s::%s() cannot be static", scope->name, __isset->common.function_name);
2332 			}
2333 			__isset->common.fn_flags &= ~ZEND_ACC_ALLOW_STATIC;
2334 		}
2335 		if (__debugInfo) {
2336 			if (__debugInfo->common.fn_flags & ZEND_ACC_STATIC) {
2337 				zend_error(error_type, "Method %s::%s() cannot be static", scope->name, __debugInfo->common.function_name);
2338 			}
2339 		}
2340 		efree((char*)lc_class_name);
2341 	}
2342 	return SUCCESS;
2343 }
2344 /* }}} */
2345 
2346 /* count=-1 means erase all functions, otherwise,
2347  * erase the first count functions
2348  */
zend_unregister_functions(const zend_function_entry * functions,int count,HashTable * function_table TSRMLS_DC)2349 ZEND_API void zend_unregister_functions(const zend_function_entry *functions, int count, HashTable *function_table TSRMLS_DC) /* {{{ */
2350 {
2351 	const zend_function_entry *ptr = functions;
2352 	int i=0;
2353 	HashTable *target_function_table = function_table;
2354 
2355 	if (!target_function_table) {
2356 		target_function_table = CG(function_table);
2357 	}
2358 	while (ptr->fname) {
2359 		if (count!=-1 && i>=count) {
2360 			break;
2361 		}
2362 #if 0
2363 		zend_printf("Unregistering %s()\n", ptr->fname);
2364 #endif
2365 		zend_hash_del(target_function_table, ptr->fname, strlen(ptr->fname)+1);
2366 		ptr++;
2367 		i++;
2368 	}
2369 }
2370 /* }}} */
2371 
zend_startup_module(zend_module_entry * module)2372 ZEND_API int zend_startup_module(zend_module_entry *module) /* {{{ */
2373 {
2374 	TSRMLS_FETCH();
2375 
2376 	if ((module = zend_register_internal_module(module TSRMLS_CC)) != NULL && zend_startup_module_ex(module TSRMLS_CC) == SUCCESS) {
2377 		return SUCCESS;
2378 	}
2379 	return FAILURE;
2380 }
2381 /* }}} */
2382 
zend_get_module_started(const char * module_name)2383 ZEND_API int zend_get_module_started(const char *module_name) /* {{{ */
2384 {
2385 	zend_module_entry *module;
2386 
2387 	return (zend_hash_find(&module_registry, module_name, strlen(module_name)+1, (void**)&module) == SUCCESS && module->module_started) ? SUCCESS : FAILURE;
2388 }
2389 /* }}} */
2390 
clean_module_class(const zend_class_entry ** ce,int * module_number TSRMLS_DC)2391 static int clean_module_class(const zend_class_entry **ce, int *module_number TSRMLS_DC) /* {{{ */
2392 {
2393 	if ((*ce)->type == ZEND_INTERNAL_CLASS && (*ce)->info.internal.module->module_number == *module_number) {
2394 		return ZEND_HASH_APPLY_REMOVE;
2395 	} else {
2396 		return ZEND_HASH_APPLY_KEEP;
2397 	}
2398 }
2399 /* }}} */
2400 
clean_module_classes(int module_number TSRMLS_DC)2401 static void clean_module_classes(int module_number TSRMLS_DC) /* {{{ */
2402 {
2403 	zend_hash_apply_with_argument(EG(class_table), (apply_func_arg_t) clean_module_class, (void *) &module_number TSRMLS_CC);
2404 }
2405 /* }}} */
2406 
module_destructor(zend_module_entry * module)2407 void module_destructor(zend_module_entry *module) /* {{{ */
2408 {
2409 	TSRMLS_FETCH();
2410 
2411 	if (module->type == MODULE_TEMPORARY) {
2412 		zend_clean_module_rsrc_dtors(module->module_number TSRMLS_CC);
2413 		clean_module_constants(module->module_number TSRMLS_CC);
2414 		clean_module_classes(module->module_number TSRMLS_CC);
2415 	}
2416 
2417 	if (module->module_started && module->module_shutdown_func) {
2418 #if 0
2419 		zend_printf("%s: Module shutdown\n", module->name);
2420 #endif
2421 		module->module_shutdown_func(module->type, module->module_number TSRMLS_CC);
2422 	}
2423 
2424 	/* Deinitilaise module globals */
2425 	if (module->globals_size) {
2426 #ifdef ZTS
2427 		if (*module->globals_id_ptr) {
2428 			ts_free_id(*module->globals_id_ptr);
2429 		}
2430 #else
2431 		if (module->globals_dtor) {
2432 			module->globals_dtor(module->globals_ptr TSRMLS_CC);
2433 		}
2434 #endif
2435 	}
2436 
2437 	module->module_started=0;
2438 	if (module->functions) {
2439 		zend_unregister_functions(module->functions, -1, NULL TSRMLS_CC);
2440 	}
2441 
2442 #if HAVE_LIBDL
2443 #if !(defined(NETWARE) && defined(APACHE_1_BUILD))
2444 	if (module->handle && !getenv("ZEND_DONT_UNLOAD_MODULES")) {
2445 		DL_UNLOAD(module->handle);
2446 	}
2447 #endif
2448 #endif
2449 }
2450 /* }}} */
2451 
zend_activate_modules(TSRMLS_D)2452 ZEND_API void zend_activate_modules(TSRMLS_D) /* {{{ */
2453 {
2454 	zend_module_entry **p = module_request_startup_handlers;
2455 
2456 	while (*p) {
2457 		zend_module_entry *module = *p;
2458 
2459 		if (module->request_startup_func(module->type, module->module_number TSRMLS_CC)==FAILURE) {
2460 			zend_error(E_WARNING, "request_startup() for %s module failed", module->name);
2461 			exit(1);
2462 		}
2463 		p++;
2464 	}
2465 }
2466 /* }}} */
2467 
2468 /* call request shutdown for all modules */
module_registry_cleanup(zend_module_entry * module TSRMLS_DC)2469 int module_registry_cleanup(zend_module_entry *module TSRMLS_DC) /* {{{ */
2470 {
2471 	if (module->request_shutdown_func) {
2472 #if 0
2473 		zend_printf("%s: Request shutdown\n", module->name);
2474 #endif
2475 		module->request_shutdown_func(module->type, module->module_number TSRMLS_CC);
2476 	}
2477 	return 0;
2478 }
2479 /* }}} */
2480 
zend_deactivate_modules(TSRMLS_D)2481 ZEND_API void zend_deactivate_modules(TSRMLS_D) /* {{{ */
2482 {
2483 	EG(opline_ptr) = NULL; /* we're no longer executing anything */
2484 
2485 	zend_try {
2486 		if (EG(full_tables_cleanup)) {
2487 			zend_hash_reverse_apply(&module_registry, (apply_func_t) module_registry_cleanup TSRMLS_CC);
2488 		} else {
2489 			zend_module_entry **p = module_request_shutdown_handlers;
2490 
2491 			while (*p) {
2492 				zend_module_entry *module = *p;
2493 
2494 				module->request_shutdown_func(module->type, module->module_number TSRMLS_CC);
2495 				p++;
2496 			}
2497 		}
2498 	} zend_end_try();
2499 }
2500 /* }}} */
2501 
zend_cleanup_internal_classes(TSRMLS_D)2502 ZEND_API void zend_cleanup_internal_classes(TSRMLS_D) /* {{{ */
2503 {
2504 	zend_class_entry **p = class_cleanup_handlers;
2505 
2506 	while (*p) {
2507 		zend_cleanup_internal_class_data(*p TSRMLS_CC);
2508 		p++;
2509 	}
2510 }
2511 /* }}} */
2512 
module_registry_unload_temp(const zend_module_entry * module TSRMLS_DC)2513 int module_registry_unload_temp(const zend_module_entry *module TSRMLS_DC) /* {{{ */
2514 {
2515 	return (module->type == MODULE_TEMPORARY) ? ZEND_HASH_APPLY_REMOVE : ZEND_HASH_APPLY_STOP;
2516 }
2517 /* }}} */
2518 
exec_done_cb(zend_module_entry * module TSRMLS_DC)2519 static int exec_done_cb(zend_module_entry *module TSRMLS_DC) /* {{{ */
2520 {
2521 	if (module->post_deactivate_func) {
2522 		module->post_deactivate_func();
2523 	}
2524 	return 0;
2525 }
2526 /* }}} */
2527 
zend_post_deactivate_modules(TSRMLS_D)2528 ZEND_API void zend_post_deactivate_modules(TSRMLS_D) /* {{{ */
2529 {
2530 	if (EG(full_tables_cleanup)) {
2531 		zend_hash_apply(&module_registry, (apply_func_t) exec_done_cb TSRMLS_CC);
2532 		zend_hash_reverse_apply(&module_registry, (apply_func_t) module_registry_unload_temp TSRMLS_CC);
2533 	} else {
2534 		zend_module_entry **p = module_post_deactivate_handlers;
2535 
2536 		while (*p) {
2537 			zend_module_entry *module = *p;
2538 
2539 			module->post_deactivate_func();
2540 			p++;
2541 		}
2542 	}
2543 }
2544 /* }}} */
2545 
2546 /* return the next free module number */
zend_next_free_module(void)2547 ZEND_API int zend_next_free_module(void) /* {{{ */
2548 {
2549 	return zend_hash_num_elements(&module_registry) + 1;
2550 }
2551 /* }}} */
2552 
do_register_internal_class(zend_class_entry * orig_class_entry,zend_uint ce_flags TSRMLS_DC)2553 static zend_class_entry *do_register_internal_class(zend_class_entry *orig_class_entry, zend_uint ce_flags TSRMLS_DC) /* {{{ */
2554 {
2555 	zend_class_entry *class_entry = malloc(sizeof(zend_class_entry));
2556 	char *lowercase_name = emalloc(orig_class_entry->name_length + 1);
2557 	zend_ulong hash;
2558 	*class_entry = *orig_class_entry;
2559 
2560 	class_entry->type = ZEND_INTERNAL_CLASS;
2561 	zend_initialize_class_data(class_entry, 0 TSRMLS_CC);
2562 	class_entry->ce_flags = ce_flags;
2563 	class_entry->info.internal.module = EG(current_module);
2564 
2565 	if (class_entry->info.internal.builtin_functions) {
2566 		zend_register_functions(class_entry, class_entry->info.internal.builtin_functions, &class_entry->function_table, MODULE_PERSISTENT TSRMLS_CC);
2567 	}
2568 
2569 	zend_str_tolower_copy(lowercase_name, orig_class_entry->name, class_entry->name_length);
2570 	lowercase_name = (char*)zend_new_interned_string(lowercase_name, class_entry->name_length + 1, 1 TSRMLS_CC);
2571 	hash = str_hash(lowercase_name, class_entry->name_length);
2572 	zend_hash_quick_update(CG(class_table), lowercase_name, class_entry->name_length+1, hash, &class_entry, sizeof(zend_class_entry *), NULL);
2573 	str_efree(lowercase_name);
2574 	return class_entry;
2575 }
2576 /* }}} */
2577 
2578 /* If parent_ce is not NULL then it inherits from parent_ce
2579  * If parent_ce is NULL and parent_name isn't then it looks for the parent and inherits from it
2580  * If both parent_ce and parent_name are NULL it does a regular class registration
2581  * If parent_name is specified but not found NULL is returned
2582  */
zend_register_internal_class_ex(zend_class_entry * class_entry,zend_class_entry * parent_ce,char * parent_name TSRMLS_DC)2583 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) /* {{{ */
2584 {
2585 	zend_class_entry *register_class;
2586 
2587 	if (!parent_ce && parent_name) {
2588 		zend_class_entry **pce;
2589 		if (zend_hash_find(CG(class_table), parent_name, strlen(parent_name)+1, (void **) &pce)==FAILURE) {
2590 			return NULL;
2591 		} else {
2592 			parent_ce = *pce;
2593 		}
2594 	}
2595 
2596 	register_class = zend_register_internal_class(class_entry TSRMLS_CC);
2597 
2598 	if (parent_ce) {
2599 		zend_do_inheritance(register_class, parent_ce TSRMLS_CC);
2600 	}
2601 	return register_class;
2602 }
2603 /* }}} */
2604 
zend_class_implements(zend_class_entry * class_entry TSRMLS_DC,int num_interfaces,...)2605 ZEND_API void zend_class_implements(zend_class_entry *class_entry TSRMLS_DC, int num_interfaces, ...) /* {{{ */
2606 {
2607 	zend_class_entry *interface_entry;
2608 	va_list interface_list;
2609 	va_start(interface_list, num_interfaces);
2610 
2611 	while (num_interfaces--) {
2612 		interface_entry = va_arg(interface_list, zend_class_entry *);
2613 		zend_do_implement_interface(class_entry, interface_entry TSRMLS_CC);
2614 	}
2615 
2616 	va_end(interface_list);
2617 }
2618 /* }}} */
2619 
2620 /* A class that contains at least one abstract method automatically becomes an abstract class.
2621  */
zend_register_internal_class(zend_class_entry * orig_class_entry TSRMLS_DC)2622 ZEND_API zend_class_entry *zend_register_internal_class(zend_class_entry *orig_class_entry TSRMLS_DC) /* {{{ */
2623 {
2624 	return do_register_internal_class(orig_class_entry, 0 TSRMLS_CC);
2625 }
2626 /* }}} */
2627 
zend_register_internal_interface(zend_class_entry * orig_class_entry TSRMLS_DC)2628 ZEND_API zend_class_entry *zend_register_internal_interface(zend_class_entry *orig_class_entry TSRMLS_DC) /* {{{ */
2629 {
2630 	return do_register_internal_class(orig_class_entry, ZEND_ACC_INTERFACE TSRMLS_CC);
2631 }
2632 /* }}} */
2633 
zend_register_class_alias_ex(const char * name,int name_len,zend_class_entry * ce TSRMLS_DC)2634 ZEND_API int zend_register_class_alias_ex(const char *name, int name_len, zend_class_entry *ce TSRMLS_DC) /* {{{ */
2635 {
2636 	char *lcname = zend_str_tolower_dup(name, name_len);
2637 	int ret;
2638 
2639 	if (lcname[0] == '\\') {
2640 		ret = zend_hash_add(CG(class_table), lcname+1, name_len, &ce, sizeof(zend_class_entry *), NULL);
2641 	} else {
2642 		ret = zend_hash_add(CG(class_table), lcname, name_len+1, &ce, sizeof(zend_class_entry *), NULL);
2643 	}
2644 
2645 	efree(lcname);
2646 	if (ret == SUCCESS) {
2647 		ce->refcount++;
2648 	}
2649 	return ret;
2650 }
2651 /* }}} */
2652 
zend_set_hash_symbol(zval * symbol,const char * name,int name_length,zend_bool is_ref,int num_symbol_tables,...)2653 ZEND_API int zend_set_hash_symbol(zval *symbol, const char *name, int name_length, zend_bool is_ref, int num_symbol_tables, ...) /* {{{ */
2654 {
2655 	HashTable *symbol_table;
2656 	va_list symbol_table_list;
2657 
2658 	if (num_symbol_tables <= 0) return FAILURE;
2659 
2660 	Z_SET_ISREF_TO_P(symbol, is_ref);
2661 
2662 	va_start(symbol_table_list, num_symbol_tables);
2663 	while (num_symbol_tables-- > 0) {
2664 		symbol_table = va_arg(symbol_table_list, HashTable *);
2665 		zend_hash_update(symbol_table, name, name_length + 1, &symbol, sizeof(zval *), NULL);
2666 		zval_add_ref(&symbol);
2667 	}
2668 	va_end(symbol_table_list);
2669 	return SUCCESS;
2670 }
2671 /* }}} */
2672 
2673 /* Disabled functions support */
2674 
2675 /* {{{ proto void display_disabled_function(void)
2676 Dummy function which displays an error when a disabled function is called. */
ZEND_FUNCTION(display_disabled_function)2677 ZEND_API ZEND_FUNCTION(display_disabled_function)
2678 {
2679 	zend_error(E_WARNING, "%s() has been disabled for security reasons", get_active_function_name(TSRMLS_C));
2680 }
2681 /* }}} */
2682 
zend_disable_function(char * function_name,uint function_name_length TSRMLS_DC)2683 ZEND_API int zend_disable_function(char *function_name, uint function_name_length TSRMLS_DC) /* {{{ */
2684 {
2685 	zend_internal_function *func;
2686 	if (zend_hash_find(CG(function_table), function_name, function_name_length+1, (void **)&func)==SUCCESS) {
2687 		func->arg_info = NULL;
2688 		func->handler = ZEND_FN(display_disabled_function);
2689 		return SUCCESS;
2690 	}
2691 	return FAILURE;
2692 }
2693 /* }}} */
2694 
2695 #ifdef ZEND_WIN32
2696 #pragma optimize("", off)
2697 #endif
display_disabled_class(zend_class_entry * class_type TSRMLS_DC)2698 static zend_object_value display_disabled_class(zend_class_entry *class_type TSRMLS_DC) /* {{{ */
2699 {
2700 	zend_object_value retval;
2701 	zend_object *intern;
2702 	retval = zend_objects_new(&intern, class_type TSRMLS_CC);
2703 	zend_error(E_WARNING, "%s() has been disabled for security reasons", class_type->name);
2704 	return retval;
2705 }
2706 #ifdef ZEND_WIN32
2707 #pragma optimize("", on)
2708 #endif
2709 /* }}} */
2710 
2711 static const zend_function_entry disabled_class_new[] = {
2712 	ZEND_FE_END
2713 };
2714 
zend_disable_class(char * class_name,uint class_name_length TSRMLS_DC)2715 ZEND_API int zend_disable_class(char *class_name, uint class_name_length TSRMLS_DC) /* {{{ */
2716 {
2717 	zend_class_entry **disabled_class;
2718 
2719 	zend_str_tolower(class_name, class_name_length);
2720 	if (zend_hash_find(CG(class_table), class_name, class_name_length+1, (void **)&disabled_class)==FAILURE) {
2721 		return FAILURE;
2722 	}
2723 	INIT_CLASS_ENTRY_INIT_METHODS((**disabled_class), disabled_class_new, NULL, NULL, NULL, NULL, NULL);
2724 	(*disabled_class)->create_object = display_disabled_class;
2725 	zend_hash_clean(&((*disabled_class)->function_table));
2726 	return SUCCESS;
2727 }
2728 /* }}} */
2729 
zend_is_callable_check_class(const char * name,int name_len,zend_fcall_info_cache * fcc,int * strict_class,char ** error TSRMLS_DC)2730 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) /* {{{ */
2731 {
2732 	int ret = 0;
2733 	zend_class_entry **pce;
2734 	char *lcname = zend_str_tolower_dup(name, name_len);
2735 
2736 	*strict_class = 0;
2737 	if (name_len == sizeof("self") - 1 &&
2738 	    !memcmp(lcname, "self", sizeof("self") - 1)) {
2739 		if (!EG(scope)) {
2740 			if (error) *error = estrdup("cannot access self:: when no class scope is active");
2741 		} else {
2742 			fcc->called_scope = EG(called_scope);
2743 			fcc->calling_scope = EG(scope);
2744 			if (!fcc->object_ptr) {
2745 				fcc->object_ptr = EG(This);
2746 			}
2747 			ret = 1;
2748 		}
2749 	} else if (name_len == sizeof("parent") - 1 &&
2750 		       !memcmp(lcname, "parent", sizeof("parent") - 1)) {
2751 		if (!EG(scope)) {
2752 			if (error) *error = estrdup("cannot access parent:: when no class scope is active");
2753 		} else if (!EG(scope)->parent) {
2754 			if (error) *error = estrdup("cannot access parent:: when current class scope has no parent");
2755 		} else {
2756 			fcc->called_scope = EG(called_scope);
2757 			fcc->calling_scope = EG(scope)->parent;
2758 			if (!fcc->object_ptr) {
2759 				fcc->object_ptr = EG(This);
2760 			}
2761 			*strict_class = 1;
2762 			ret = 1;
2763 		}
2764 	} else if (name_len == sizeof("static") - 1 &&
2765 	           !memcmp(lcname, "static", sizeof("static") - 1)) {
2766 		if (!EG(called_scope)) {
2767 			if (error) *error = estrdup("cannot access static:: when no class scope is active");
2768 		} else {
2769 			fcc->called_scope = EG(called_scope);
2770 			fcc->calling_scope = EG(called_scope);
2771 			if (!fcc->object_ptr) {
2772 				fcc->object_ptr = EG(This);
2773 			}
2774 			*strict_class = 1;
2775 			ret = 1;
2776 		}
2777 	} else if (zend_lookup_class_ex(name, name_len, NULL, 1, &pce TSRMLS_CC) == SUCCESS) {
2778 		zend_class_entry *scope = EG(active_op_array) ? EG(active_op_array)->scope : NULL;
2779 
2780 		fcc->calling_scope = *pce;
2781 		if (scope && !fcc->object_ptr && EG(This) &&
2782 		    instanceof_function(Z_OBJCE_P(EG(This)), scope TSRMLS_CC) &&
2783 		    instanceof_function(scope, fcc->calling_scope TSRMLS_CC)) {
2784 			fcc->object_ptr = EG(This);
2785 			fcc->called_scope = Z_OBJCE_P(fcc->object_ptr);
2786 		} else {
2787 			fcc->called_scope = fcc->object_ptr ? Z_OBJCE_P(fcc->object_ptr) : fcc->calling_scope;
2788 		}
2789 		*strict_class = 1;
2790 		ret = 1;
2791 	} else {
2792 		if (error) zend_spprintf(error, 0, "class '%.*s' not found", name_len, name);
2793 	}
2794 	efree(lcname);
2795 	return ret;
2796 }
2797 /* }}} */
2798 
zend_is_callable_check_func(int check_flags,zval * callable,zend_fcall_info_cache * fcc,int strict_class,char ** error TSRMLS_DC)2799 static int zend_is_callable_check_func(int check_flags, zval *callable, zend_fcall_info_cache *fcc, int strict_class, char **error TSRMLS_DC) /* {{{ */
2800 {
2801 	zend_class_entry *ce_org = fcc->calling_scope;
2802 	int retval = 0;
2803 	char *mname, *lmname;
2804 	const char *colon;
2805 	int clen, mlen;
2806 	zend_class_entry *last_scope;
2807 	HashTable *ftable;
2808 	int call_via_handler = 0;
2809 
2810 	if (error) {
2811 		*error = NULL;
2812 	}
2813 
2814 	fcc->calling_scope = NULL;
2815 	fcc->function_handler = NULL;
2816 
2817 	if (!ce_org) {
2818 		/* Skip leading \ */
2819 		if (Z_STRVAL_P(callable)[0] == '\\') {
2820 			mlen = Z_STRLEN_P(callable) - 1;
2821 			lmname = zend_str_tolower_dup(Z_STRVAL_P(callable) + 1, mlen);
2822 		} else {
2823 			mlen = Z_STRLEN_P(callable);
2824 			lmname = zend_str_tolower_dup(Z_STRVAL_P(callable), mlen);
2825 		}
2826 		/* Check if function with given name exists.
2827 		 * This may be a compound name that includes namespace name */
2828 		if (zend_hash_find(EG(function_table), lmname, mlen+1, (void**)&fcc->function_handler) == SUCCESS) {
2829 			efree(lmname);
2830 			return 1;
2831 		}
2832 		efree(lmname);
2833 	}
2834 
2835 	/* Split name into class/namespace and method/function names */
2836 	if ((colon = zend_memrchr(Z_STRVAL_P(callable), ':', Z_STRLEN_P(callable))) != NULL &&
2837 		colon > Z_STRVAL_P(callable) &&
2838 		*(colon-1) == ':'
2839 	) {
2840 		colon--;
2841 		clen = colon - Z_STRVAL_P(callable);
2842 		mlen = Z_STRLEN_P(callable) - clen - 2;
2843 
2844 		if (colon == Z_STRVAL_P(callable)) {
2845 			if (error) zend_spprintf(error, 0, "invalid function name");
2846 			return 0;
2847 		}
2848 
2849 		/* This is a compound name.
2850 		 * Try to fetch class and then find static method. */
2851 		last_scope = EG(scope);
2852 		if (ce_org) {
2853 			EG(scope) = ce_org;
2854 		}
2855 
2856 		if (!zend_is_callable_check_class(Z_STRVAL_P(callable), clen, fcc, &strict_class, error TSRMLS_CC)) {
2857 			EG(scope) = last_scope;
2858 			return 0;
2859 		}
2860 		EG(scope) = last_scope;
2861 
2862 		ftable = &fcc->calling_scope->function_table;
2863 		if (ce_org && !instanceof_function(ce_org, fcc->calling_scope TSRMLS_CC)) {
2864 			if (error) zend_spprintf(error, 0, "class '%s' is not a subclass of '%s'", ce_org->name, fcc->calling_scope->name);
2865 			return 0;
2866 		}
2867 		mname = Z_STRVAL_P(callable) + clen + 2;
2868 	} else if (ce_org) {
2869 		/* Try to fetch find static method of given class. */
2870 		mlen = Z_STRLEN_P(callable);
2871 		mname = Z_STRVAL_P(callable);
2872 		ftable = &ce_org->function_table;
2873 		fcc->calling_scope = ce_org;
2874 	} else {
2875 		/* We already checked for plain function before. */
2876 		if (error && !(check_flags & IS_CALLABLE_CHECK_SILENT)) {
2877 			zend_spprintf(error, 0, "function '%s' not found or invalid function name", Z_STRVAL_P(callable));
2878 		}
2879 		return 0;
2880 	}
2881 
2882 	lmname = zend_str_tolower_dup(mname, mlen);
2883 	if (strict_class &&
2884 	    fcc->calling_scope &&
2885 	    mlen == sizeof(ZEND_CONSTRUCTOR_FUNC_NAME)-1 &&
2886 	    !memcmp(lmname, ZEND_CONSTRUCTOR_FUNC_NAME, sizeof(ZEND_CONSTRUCTOR_FUNC_NAME) - 1)) {
2887 		fcc->function_handler = fcc->calling_scope->constructor;
2888 		if (fcc->function_handler) {
2889 			retval = 1;
2890 		}
2891 	} else if (zend_hash_find(ftable, lmname, mlen+1, (void**)&fcc->function_handler) == SUCCESS) {
2892 		retval = 1;
2893 		if ((fcc->function_handler->op_array.fn_flags & ZEND_ACC_CHANGED) &&
2894 		    !strict_class && EG(scope) &&
2895 		    instanceof_function(fcc->function_handler->common.scope, EG(scope) TSRMLS_CC)) {
2896 			zend_function *priv_fbc;
2897 
2898 			if (zend_hash_find(&EG(scope)->function_table, lmname, mlen+1, (void **) &priv_fbc)==SUCCESS
2899 				&& priv_fbc->common.fn_flags & ZEND_ACC_PRIVATE
2900 				&& priv_fbc->common.scope == EG(scope)) {
2901 				fcc->function_handler = priv_fbc;
2902 			}
2903 		}
2904 		if ((check_flags & IS_CALLABLE_CHECK_NO_ACCESS) == 0 &&
2905 		    (fcc->calling_scope &&
2906 		     ((fcc->object_ptr && fcc->calling_scope->__call) ||
2907 		      (!fcc->object_ptr && fcc->calling_scope->__callstatic)))) {
2908 			if (fcc->function_handler->op_array.fn_flags & ZEND_ACC_PRIVATE) {
2909 				if (!zend_check_private(fcc->function_handler, fcc->object_ptr ? Z_OBJCE_P(fcc->object_ptr) : EG(scope), lmname, mlen TSRMLS_CC)) {
2910 					retval = 0;
2911 					fcc->function_handler = NULL;
2912 					goto get_function_via_handler;
2913 				}
2914 			} else if (fcc->function_handler->common.fn_flags & ZEND_ACC_PROTECTED) {
2915 				if (!zend_check_protected(fcc->function_handler->common.scope, EG(scope))) {
2916 					retval = 0;
2917 					fcc->function_handler = NULL;
2918 					goto get_function_via_handler;
2919 				}
2920 			}
2921 		}
2922 	} else {
2923 get_function_via_handler:
2924 		if (fcc->object_ptr && fcc->calling_scope == ce_org) {
2925 			if (strict_class && ce_org->__call) {
2926 				fcc->function_handler = emalloc(sizeof(zend_internal_function));
2927 				fcc->function_handler->internal_function.type = ZEND_INTERNAL_FUNCTION;
2928 				fcc->function_handler->internal_function.module = (ce_org->type == ZEND_INTERNAL_CLASS) ? ce_org->info.internal.module : NULL;
2929 				fcc->function_handler->internal_function.handler = zend_std_call_user_call;
2930 				fcc->function_handler->internal_function.arg_info = NULL;
2931 				fcc->function_handler->internal_function.num_args = 0;
2932 				fcc->function_handler->internal_function.scope = ce_org;
2933 				fcc->function_handler->internal_function.fn_flags = ZEND_ACC_CALL_VIA_HANDLER;
2934 				fcc->function_handler->internal_function.function_name = estrndup(mname, mlen);
2935 				call_via_handler = 1;
2936 				retval = 1;
2937 			} else if (Z_OBJ_HT_P(fcc->object_ptr)->get_method) {
2938 				fcc->function_handler = Z_OBJ_HT_P(fcc->object_ptr)->get_method(&fcc->object_ptr, mname, mlen, NULL TSRMLS_CC);
2939 				if (fcc->function_handler) {
2940 					if (strict_class &&
2941 					    (!fcc->function_handler->common.scope ||
2942 					     !instanceof_function(ce_org, fcc->function_handler->common.scope TSRMLS_CC))) {
2943 						if ((fcc->function_handler->common.fn_flags & ZEND_ACC_CALL_VIA_HANDLER) != 0) {
2944 							if (fcc->function_handler->type != ZEND_OVERLOADED_FUNCTION) {
2945 								efree((char*)fcc->function_handler->common.function_name);
2946 							}
2947 							efree(fcc->function_handler);
2948 						}
2949 					} else {
2950 						retval = 1;
2951 						call_via_handler = (fcc->function_handler->common.fn_flags & ZEND_ACC_CALL_VIA_HANDLER) != 0;
2952 					}
2953 				}
2954 			}
2955 		} else if (fcc->calling_scope) {
2956 			if (fcc->calling_scope->get_static_method) {
2957 				fcc->function_handler = fcc->calling_scope->get_static_method(fcc->calling_scope, mname, mlen TSRMLS_CC);
2958 			} else {
2959 				fcc->function_handler = zend_std_get_static_method(fcc->calling_scope, mname, mlen, NULL TSRMLS_CC);
2960 			}
2961 			if (fcc->function_handler) {
2962 				retval = 1;
2963 				call_via_handler = (fcc->function_handler->common.fn_flags & ZEND_ACC_CALL_VIA_HANDLER) != 0;
2964 				if (call_via_handler && !fcc->object_ptr && EG(This) &&
2965 				    Z_OBJ_HT_P(EG(This))->get_class_entry &&
2966 				    instanceof_function(Z_OBJCE_P(EG(This)), fcc->calling_scope TSRMLS_CC)) {
2967 					fcc->object_ptr = EG(This);
2968 				}
2969 			}
2970 		}
2971 	}
2972 
2973 	if (retval) {
2974 		if (fcc->calling_scope && !call_via_handler) {
2975 			if (!fcc->object_ptr && (fcc->function_handler->common.fn_flags & ZEND_ACC_ABSTRACT)) {
2976 				if (error) {
2977 					zend_spprintf(error, 0, "cannot call abstract method %s::%s()", fcc->calling_scope->name, fcc->function_handler->common.function_name);
2978 					retval = 0;
2979 				} else {
2980 					zend_error(E_ERROR, "Cannot call abstract method %s::%s()", fcc->calling_scope->name, fcc->function_handler->common.function_name);
2981 				}
2982 			} else if (!fcc->object_ptr && !(fcc->function_handler->common.fn_flags & ZEND_ACC_STATIC)) {
2983 				int severity;
2984 				char *verb;
2985 				if (fcc->function_handler->common.fn_flags & ZEND_ACC_ALLOW_STATIC) {
2986 					severity = E_STRICT;
2987 					verb = "should not";
2988 				} else {
2989 					/* An internal function assumes $this is present and won't check that. So PHP would crash by allowing the call. */
2990 					severity = E_ERROR;
2991 					verb = "cannot";
2992 				}
2993 				if ((check_flags & IS_CALLABLE_CHECK_IS_STATIC) != 0) {
2994 					retval = 0;
2995 				}
2996 				if (EG(This) && instanceof_function(Z_OBJCE_P(EG(This)), fcc->calling_scope TSRMLS_CC)) {
2997 					fcc->object_ptr = EG(This);
2998 					if (error) {
2999 						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);
3000 						if (severity == E_ERROR) {
3001 							retval = 0;
3002 						}
3003 					} else if (retval) {
3004 						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);
3005 					}
3006 				} else {
3007 					if (error) {
3008 						zend_spprintf(error, 0, "non-static method %s::%s() %s be called statically", fcc->calling_scope->name, fcc->function_handler->common.function_name, verb);
3009 						if (severity == E_ERROR) {
3010 							retval = 0;
3011 						}
3012 					} else if (retval) {
3013 						zend_error(severity, "Non-static method %s::%s() %s be called statically", fcc->calling_scope->name, fcc->function_handler->common.function_name, verb);
3014 					}
3015 				}
3016 			}
3017 			if (retval && (check_flags & IS_CALLABLE_CHECK_NO_ACCESS) == 0) {
3018 				if (fcc->function_handler->op_array.fn_flags & ZEND_ACC_PRIVATE) {
3019 					if (!zend_check_private(fcc->function_handler, fcc->object_ptr ? Z_OBJCE_P(fcc->object_ptr) : EG(scope), lmname, mlen TSRMLS_CC)) {
3020 						if (error) {
3021 							if (*error) {
3022 								efree(*error);
3023 							}
3024 							zend_spprintf(error, 0, "cannot access private method %s::%s()", fcc->calling_scope->name, fcc->function_handler->common.function_name);
3025 						}
3026 						retval = 0;
3027 					}
3028 				} else if ((fcc->function_handler->common.fn_flags & ZEND_ACC_PROTECTED)) {
3029 					if (!zend_check_protected(fcc->function_handler->common.scope, EG(scope))) {
3030 						if (error) {
3031 							if (*error) {
3032 								efree(*error);
3033 							}
3034 							zend_spprintf(error, 0, "cannot access protected method %s::%s()", fcc->calling_scope->name, fcc->function_handler->common.function_name);
3035 						}
3036 						retval = 0;
3037 					}
3038 				}
3039 			}
3040 		}
3041 	} else if (error && !(check_flags & IS_CALLABLE_CHECK_SILENT)) {
3042 		if (fcc->calling_scope) {
3043 			if (error) zend_spprintf(error, 0, "class '%s' does not have a method '%s'", fcc->calling_scope->name, mname);
3044 		} else {
3045 			if (error) zend_spprintf(error, 0, "function '%s' does not exist", mname);
3046 		}
3047 	}
3048 	efree(lmname);
3049 
3050 	if (fcc->object_ptr) {
3051 		fcc->called_scope = Z_OBJCE_P(fcc->object_ptr);
3052 	}
3053 	if (retval) {
3054 		fcc->initialized = 1;
3055 	}
3056 	return retval;
3057 }
3058 /* }}} */
3059 
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)3060 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) /* {{{ */
3061 {
3062 	zend_bool ret;
3063 	int callable_name_len_local;
3064 	zend_fcall_info_cache fcc_local;
3065 
3066 	if (callable_name) {
3067 		*callable_name = NULL;
3068 	}
3069 	if (callable_name_len == NULL) {
3070 		callable_name_len = &callable_name_len_local;
3071 	}
3072 	if (fcc == NULL) {
3073 		fcc = &fcc_local;
3074 	}
3075 	if (error) {
3076 		*error = NULL;
3077 	}
3078 
3079 	fcc->initialized = 0;
3080 	fcc->calling_scope = NULL;
3081 	fcc->called_scope = NULL;
3082 	fcc->function_handler = NULL;
3083 	fcc->calling_scope = NULL;
3084 	fcc->object_ptr = NULL;
3085 
3086 	if (object_ptr && Z_TYPE_P(object_ptr) != IS_OBJECT) {
3087 		object_ptr = NULL;
3088 	}
3089 	if (object_ptr &&
3090 	    (!EG(objects_store).object_buckets ||
3091 	     !EG(objects_store).object_buckets[Z_OBJ_HANDLE_P(object_ptr)].valid)) {
3092 		return 0;
3093 	}
3094 
3095 	switch (Z_TYPE_P(callable)) {
3096 		case IS_STRING:
3097 			if (object_ptr) {
3098 				fcc->object_ptr = object_ptr;
3099 				fcc->calling_scope = Z_OBJCE_P(object_ptr);
3100 				if (callable_name) {
3101 					char *ptr;
3102 
3103 					*callable_name_len = fcc->calling_scope->name_length + Z_STRLEN_P(callable) + sizeof("::") - 1;
3104 					ptr = *callable_name = emalloc(*callable_name_len + 1);
3105 					memcpy(ptr, fcc->calling_scope->name, fcc->calling_scope->name_length);
3106 					ptr += fcc->calling_scope->name_length;
3107 					memcpy(ptr, "::", sizeof("::") - 1);
3108 					ptr += sizeof("::") - 1;
3109 					memcpy(ptr, Z_STRVAL_P(callable), Z_STRLEN_P(callable) + 1);
3110 				}
3111 			} else if (callable_name) {
3112 				*callable_name = estrndup(Z_STRVAL_P(callable), Z_STRLEN_P(callable));
3113 				*callable_name_len = Z_STRLEN_P(callable);
3114 			}
3115 			if (check_flags & IS_CALLABLE_CHECK_SYNTAX_ONLY) {
3116 				fcc->called_scope = fcc->calling_scope;
3117 				return 1;
3118 			}
3119 
3120 			ret = zend_is_callable_check_func(check_flags, callable, fcc, 0, error TSRMLS_CC);
3121 			if (fcc == &fcc_local &&
3122 			    fcc->function_handler &&
3123 				((fcc->function_handler->type == ZEND_INTERNAL_FUNCTION &&
3124 			      (fcc->function_handler->common.fn_flags & ZEND_ACC_CALL_VIA_HANDLER)) ||
3125 			     fcc->function_handler->type == ZEND_OVERLOADED_FUNCTION_TEMPORARY ||
3126 			     fcc->function_handler->type == ZEND_OVERLOADED_FUNCTION)) {
3127 				if (fcc->function_handler->type != ZEND_OVERLOADED_FUNCTION) {
3128 					efree((char*)fcc->function_handler->common.function_name);
3129 				}
3130 				efree(fcc->function_handler);
3131 			}
3132 			return ret;
3133 
3134 		case IS_ARRAY:
3135 			{
3136 				zval **method = NULL;
3137 				zval **obj = NULL;
3138 				int strict_class = 0;
3139 
3140 				if (zend_hash_num_elements(Z_ARRVAL_P(callable)) == 2) {
3141 					zend_hash_index_find(Z_ARRVAL_P(callable), 0, (void **) &obj);
3142 					zend_hash_index_find(Z_ARRVAL_P(callable), 1, (void **) &method);
3143 				}
3144 				if (obj && method &&
3145 					(Z_TYPE_PP(obj) == IS_OBJECT ||
3146 					Z_TYPE_PP(obj) == IS_STRING) &&
3147 					Z_TYPE_PP(method) == IS_STRING) {
3148 
3149 					if (Z_TYPE_PP(obj) == IS_STRING) {
3150 						if (callable_name) {
3151 							char *ptr;
3152 
3153 							*callable_name_len = Z_STRLEN_PP(obj) + Z_STRLEN_PP(method) + sizeof("::") - 1;
3154 							ptr = *callable_name = emalloc(*callable_name_len + 1);
3155 							memcpy(ptr, Z_STRVAL_PP(obj), Z_STRLEN_PP(obj));
3156 							ptr += Z_STRLEN_PP(obj);
3157 							memcpy(ptr, "::", sizeof("::") - 1);
3158 							ptr += sizeof("::") - 1;
3159 							memcpy(ptr, Z_STRVAL_PP(method), Z_STRLEN_PP(method) + 1);
3160 						}
3161 
3162 						if (check_flags & IS_CALLABLE_CHECK_SYNTAX_ONLY) {
3163 							return 1;
3164 						}
3165 
3166 						if (!zend_is_callable_check_class(Z_STRVAL_PP(obj), Z_STRLEN_PP(obj), fcc, &strict_class, error TSRMLS_CC)) {
3167 							return 0;
3168 						}
3169 
3170 					} else {
3171 						if (!EG(objects_store).object_buckets ||
3172 						    !EG(objects_store).object_buckets[Z_OBJ_HANDLE_PP(obj)].valid) {
3173 							return 0;
3174 						}
3175 
3176 						fcc->calling_scope = Z_OBJCE_PP(obj); /* TBFixed: what if it's overloaded? */
3177 
3178 						fcc->object_ptr = *obj;
3179 
3180 						if (callable_name) {
3181 							char *ptr;
3182 
3183 							*callable_name_len = fcc->calling_scope->name_length + Z_STRLEN_PP(method) + sizeof("::") - 1;
3184 							ptr = *callable_name = emalloc(*callable_name_len + 1);
3185 							memcpy(ptr, fcc->calling_scope->name, fcc->calling_scope->name_length);
3186 							ptr += fcc->calling_scope->name_length;
3187 							memcpy(ptr, "::", sizeof("::") - 1);
3188 							ptr += sizeof("::") - 1;
3189 							memcpy(ptr, Z_STRVAL_PP(method), Z_STRLEN_PP(method) + 1);
3190 						}
3191 
3192 						if (check_flags & IS_CALLABLE_CHECK_SYNTAX_ONLY) {
3193 							fcc->called_scope = fcc->calling_scope;
3194 							return 1;
3195 						}
3196 					}
3197 
3198 					ret = zend_is_callable_check_func(check_flags, *method, fcc, strict_class, error TSRMLS_CC);
3199 					if (fcc == &fcc_local &&
3200 					    fcc->function_handler &&
3201 						((fcc->function_handler->type == ZEND_INTERNAL_FUNCTION &&
3202 					      (fcc->function_handler->common.fn_flags & ZEND_ACC_CALL_VIA_HANDLER)) ||
3203 					     fcc->function_handler->type == ZEND_OVERLOADED_FUNCTION_TEMPORARY ||
3204 					     fcc->function_handler->type == ZEND_OVERLOADED_FUNCTION)) {
3205 						if (fcc->function_handler->type != ZEND_OVERLOADED_FUNCTION) {
3206 							efree((char*)fcc->function_handler->common.function_name);
3207 						}
3208 						efree(fcc->function_handler);
3209 					}
3210 					return ret;
3211 
3212 				} else {
3213 					if (zend_hash_num_elements(Z_ARRVAL_P(callable)) == 2) {
3214 						if (!obj || (Z_TYPE_PP(obj) != IS_STRING && Z_TYPE_PP(obj) != IS_OBJECT)) {
3215 							if (error) zend_spprintf(error, 0, "first array member is not a valid class name or object");
3216 						} else {
3217 							if (error) zend_spprintf(error, 0, "second array member is not a valid method");
3218 						}
3219 					} else {
3220 						if (error) zend_spprintf(error, 0, "array must have exactly two members");
3221 					}
3222 					if (callable_name) {
3223 						*callable_name = estrndup("Array", sizeof("Array")-1);
3224 						*callable_name_len = sizeof("Array") - 1;
3225 					}
3226 				}
3227 			}
3228 			return 0;
3229 
3230 		case IS_OBJECT:
3231 			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) {
3232 				fcc->called_scope = fcc->calling_scope;
3233 				if (callable_name) {
3234 					zend_class_entry *ce = Z_OBJCE_P(callable); /* TBFixed: what if it's overloaded? */
3235 
3236 					*callable_name_len = ce->name_length + sizeof("::__invoke") - 1;
3237 					*callable_name = emalloc(*callable_name_len + 1);
3238 					memcpy(*callable_name, ce->name, ce->name_length);
3239 					memcpy((*callable_name) + ce->name_length, "::__invoke", sizeof("::__invoke"));
3240 				}
3241 				return 1;
3242 			}
3243 			/* break missing intentionally */
3244 
3245 		default:
3246 			if (callable_name) {
3247 				zval expr_copy;
3248 				int use_copy;
3249 
3250 				zend_make_printable_zval(callable, &expr_copy, &use_copy);
3251 				*callable_name = estrndup(Z_STRVAL(expr_copy), Z_STRLEN(expr_copy));
3252 				*callable_name_len = Z_STRLEN(expr_copy);
3253 				zval_dtor(&expr_copy);
3254 			}
3255 			if (error) zend_spprintf(error, 0, "no array or string given");
3256 			return 0;
3257 	}
3258 }
3259 /* }}} */
3260 
zend_is_callable(zval * callable,uint check_flags,char ** callable_name TSRMLS_DC)3261 ZEND_API zend_bool zend_is_callable(zval *callable, uint check_flags, char **callable_name TSRMLS_DC) /* {{{ */
3262 {
3263 	return zend_is_callable_ex(callable, NULL, check_flags, callable_name, NULL, NULL, NULL TSRMLS_CC);
3264 }
3265 /* }}} */
3266 
zend_make_callable(zval * callable,char ** callable_name TSRMLS_DC)3267 ZEND_API zend_bool zend_make_callable(zval *callable, char **callable_name TSRMLS_DC) /* {{{ */
3268 {
3269 	zend_fcall_info_cache fcc;
3270 
3271 	if (zend_is_callable_ex(callable, NULL, IS_CALLABLE_STRICT, callable_name, NULL, &fcc, NULL TSRMLS_CC)) {
3272 		if (Z_TYPE_P(callable) == IS_STRING && fcc.calling_scope) {
3273 			zval_dtor(callable);
3274 			array_init(callable);
3275 			add_next_index_string(callable, fcc.calling_scope->name, 1);
3276 			add_next_index_string(callable, fcc.function_handler->common.function_name, 1);
3277 		}
3278 		if (fcc.function_handler &&
3279 			((fcc.function_handler->type == ZEND_INTERNAL_FUNCTION &&
3280 		      (fcc.function_handler->common.fn_flags & ZEND_ACC_CALL_VIA_HANDLER)) ||
3281 		     fcc.function_handler->type == ZEND_OVERLOADED_FUNCTION_TEMPORARY ||
3282 		     fcc.function_handler->type == ZEND_OVERLOADED_FUNCTION)) {
3283 			if (fcc.function_handler->type != ZEND_OVERLOADED_FUNCTION) {
3284 				efree((char*)fcc.function_handler->common.function_name);
3285 			}
3286 			efree(fcc.function_handler);
3287 		}
3288 		return 1;
3289 	}
3290 	return 0;
3291 }
3292 /* }}} */
3293 
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)3294 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) /* {{{ */
3295 {
3296 	if (!zend_is_callable_ex(callable, NULL, check_flags, callable_name, NULL, fcc, error TSRMLS_CC)) {
3297 		return FAILURE;
3298 	}
3299 
3300 	fci->size = sizeof(*fci);
3301 	fci->function_table = fcc->calling_scope ? &fcc->calling_scope->function_table : EG(function_table);
3302 	fci->object_ptr = fcc->object_ptr;
3303 	fci->function_name = callable;
3304 	fci->retval_ptr_ptr = NULL;
3305 	fci->param_count = 0;
3306 	fci->params = NULL;
3307 	fci->no_separation = 1;
3308 	fci->symbol_table = NULL;
3309 
3310 	return SUCCESS;
3311 }
3312 /* }}} */
3313 
zend_fcall_info_args_clear(zend_fcall_info * fci,int free_mem)3314 ZEND_API void zend_fcall_info_args_clear(zend_fcall_info *fci, int free_mem) /* {{{ */
3315 {
3316 	if (fci->params) {
3317 		if (free_mem) {
3318 			efree(fci->params);
3319 			fci->params = NULL;
3320 		}
3321 	}
3322 	fci->param_count = 0;
3323 }
3324 /* }}} */
3325 
zend_fcall_info_args_save(zend_fcall_info * fci,int * param_count,zval **** params)3326 ZEND_API void zend_fcall_info_args_save(zend_fcall_info *fci, int *param_count, zval ****params) /* {{{ */
3327 {
3328 	*param_count = fci->param_count;
3329 	*params = fci->params;
3330 	fci->param_count = 0;
3331 	fci->params = NULL;
3332 }
3333 /* }}} */
3334 
zend_fcall_info_args_restore(zend_fcall_info * fci,int param_count,zval *** params)3335 ZEND_API void zend_fcall_info_args_restore(zend_fcall_info *fci, int param_count, zval ***params) /* {{{ */
3336 {
3337 	zend_fcall_info_args_clear(fci, 1);
3338 	fci->param_count = param_count;
3339 	fci->params = params;
3340 }
3341 /* }}} */
3342 
zend_fcall_info_args(zend_fcall_info * fci,zval * args TSRMLS_DC)3343 ZEND_API int zend_fcall_info_args(zend_fcall_info *fci, zval *args TSRMLS_DC) /* {{{ */
3344 {
3345 	HashPosition pos;
3346 	zval **arg, ***params;
3347 
3348 	zend_fcall_info_args_clear(fci, !args);
3349 
3350 	if (!args) {
3351 		return SUCCESS;
3352 	}
3353 
3354 	if (Z_TYPE_P(args) != IS_ARRAY) {
3355 		return FAILURE;
3356 	}
3357 
3358 	fci->param_count = zend_hash_num_elements(Z_ARRVAL_P(args));
3359 	fci->params = params = (zval ***) erealloc(fci->params, fci->param_count * sizeof(zval **));
3360 
3361 	zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(args), &pos);
3362 	while (zend_hash_get_current_data_ex(Z_ARRVAL_P(args), (void *) &arg, &pos) == SUCCESS) {
3363 		*params++ = arg;
3364 		zend_hash_move_forward_ex(Z_ARRVAL_P(args), &pos);
3365 	}
3366 
3367 	return SUCCESS;
3368 }
3369 /* }}} */
3370 
zend_fcall_info_argp(zend_fcall_info * fci TSRMLS_DC,int argc,zval *** argv)3371 ZEND_API int zend_fcall_info_argp(zend_fcall_info *fci TSRMLS_DC, int argc, zval ***argv) /* {{{ */
3372 {
3373 	int i;
3374 
3375 	if (argc < 0) {
3376 		return FAILURE;
3377 	}
3378 
3379 	zend_fcall_info_args_clear(fci, !argc);
3380 
3381 	if (argc) {
3382 		fci->param_count = argc;
3383 		fci->params = (zval ***) erealloc(fci->params, fci->param_count * sizeof(zval **));
3384 
3385 		for (i = 0; i < argc; ++i) {
3386 			fci->params[i] = argv[i];
3387 		}
3388 	}
3389 
3390 	return SUCCESS;
3391 }
3392 /* }}} */
3393 
zend_fcall_info_argv(zend_fcall_info * fci TSRMLS_DC,int argc,va_list * argv)3394 ZEND_API int zend_fcall_info_argv(zend_fcall_info *fci TSRMLS_DC, int argc, va_list *argv) /* {{{ */
3395 {
3396 	int i;
3397 	zval **arg;
3398 
3399 	if (argc < 0) {
3400 		return FAILURE;
3401 	}
3402 
3403 	zend_fcall_info_args_clear(fci, !argc);
3404 
3405 	if (argc) {
3406 		fci->param_count = argc;
3407 		fci->params = (zval ***) erealloc(fci->params, fci->param_count * sizeof(zval **));
3408 
3409 		for (i = 0; i < argc; ++i) {
3410 			arg = va_arg(*argv, zval **);
3411 			fci->params[i] = arg;
3412 		}
3413 	}
3414 
3415 	return SUCCESS;
3416 }
3417 /* }}} */
3418 
zend_fcall_info_argn(zend_fcall_info * fci TSRMLS_DC,int argc,...)3419 ZEND_API int zend_fcall_info_argn(zend_fcall_info *fci TSRMLS_DC, int argc, ...) /* {{{ */
3420 {
3421 	int ret;
3422 	va_list argv;
3423 
3424 	va_start(argv, argc);
3425 	ret = zend_fcall_info_argv(fci TSRMLS_CC, argc, &argv);
3426 	va_end(argv);
3427 
3428 	return ret;
3429 }
3430 /* }}} */
3431 
zend_fcall_info_call(zend_fcall_info * fci,zend_fcall_info_cache * fcc,zval ** retval_ptr_ptr,zval * args TSRMLS_DC)3432 ZEND_API int zend_fcall_info_call(zend_fcall_info *fci, zend_fcall_info_cache *fcc, zval **retval_ptr_ptr, zval *args TSRMLS_DC) /* {{{ */
3433 {
3434 	zval *retval, ***org_params = NULL;
3435 	int result, org_count = 0;
3436 
3437 	fci->retval_ptr_ptr = retval_ptr_ptr ? retval_ptr_ptr : &retval;
3438 	if (args) {
3439 		zend_fcall_info_args_save(fci, &org_count, &org_params);
3440 		zend_fcall_info_args(fci, args TSRMLS_CC);
3441 	}
3442 	result = zend_call_function(fci, fcc TSRMLS_CC);
3443 
3444 	if (!retval_ptr_ptr && retval) {
3445 		zval_ptr_dtor(&retval);
3446 	}
3447 	if (args) {
3448 		zend_fcall_info_args_restore(fci, org_count, org_params);
3449 	}
3450 	return result;
3451 }
3452 /* }}} */
3453 
zend_get_module_version(const char * module_name)3454 ZEND_API const char *zend_get_module_version(const char *module_name) /* {{{ */
3455 {
3456 	char *lname;
3457 	int name_len = strlen(module_name);
3458 	zend_module_entry *module;
3459 
3460 	lname = zend_str_tolower_dup(module_name, name_len);
3461 	if (zend_hash_find(&module_registry, lname, name_len + 1, (void**)&module) == FAILURE) {
3462 		efree(lname);
3463 		return NULL;
3464 	}
3465 	efree(lname);
3466 	return module->version;
3467 }
3468 /* }}} */
3469 
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)3470 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) /* {{{ */
3471 {
3472 	zend_property_info property_info, *property_info_ptr;
3473 	const char *interned_name;
3474 	ulong h = zend_get_hash_value(name, name_length+1);
3475 
3476 	if (!(access_type & ZEND_ACC_PPP_MASK)) {
3477 		access_type |= ZEND_ACC_PUBLIC;
3478 	}
3479 	if (access_type & ZEND_ACC_STATIC) {
3480 		if (zend_hash_quick_find(&ce->properties_info, name, name_length + 1, h, (void**)&property_info_ptr) == SUCCESS &&
3481 		    (property_info_ptr->flags & ZEND_ACC_STATIC) != 0) {
3482 			property_info.offset = property_info_ptr->offset;
3483 			zval_ptr_dtor(&ce->default_static_members_table[property_info.offset]);
3484 			zend_hash_quick_del(&ce->properties_info, name, name_length + 1, h);
3485 		} else {
3486 			property_info.offset = ce->default_static_members_count++;
3487 			ce->default_static_members_table = perealloc(ce->default_static_members_table, sizeof(zval*) * ce->default_static_members_count, ce->type == ZEND_INTERNAL_CLASS);
3488 		}
3489 		ce->default_static_members_table[property_info.offset] = property;
3490 		if (ce->type == ZEND_USER_CLASS) {
3491 			ce->static_members_table = ce->default_static_members_table;
3492 		}
3493 	} else {
3494 		if (zend_hash_quick_find(&ce->properties_info, name, name_length + 1, h, (void**)&property_info_ptr) == SUCCESS &&
3495 		    (property_info_ptr->flags & ZEND_ACC_STATIC) == 0) {
3496 			property_info.offset = property_info_ptr->offset;
3497 			zval_ptr_dtor(&ce->default_properties_table[property_info.offset]);
3498 			zend_hash_quick_del(&ce->properties_info, name, name_length + 1, h);
3499 		} else {
3500 			property_info.offset = ce->default_properties_count++;
3501 			ce->default_properties_table = perealloc(ce->default_properties_table, sizeof(zval*) * ce->default_properties_count, ce->type == ZEND_INTERNAL_CLASS);
3502 		}
3503 		ce->default_properties_table[property_info.offset] = property;
3504 	}
3505 	if (ce->type & ZEND_INTERNAL_CLASS) {
3506 		switch(Z_TYPE_P(property)) {
3507 			case IS_ARRAY:
3508 			case IS_OBJECT:
3509 			case IS_RESOURCE:
3510 				zend_error(E_CORE_ERROR, "Internal zval's can't be arrays, objects or resources");
3511 				break;
3512 			default:
3513 				break;
3514 		}
3515 	}
3516 	switch (access_type & ZEND_ACC_PPP_MASK) {
3517 		case ZEND_ACC_PRIVATE: {
3518 				char *priv_name;
3519 				int priv_name_length;
3520 
3521 				zend_mangle_property_name(&priv_name, &priv_name_length, ce->name, ce->name_length, name, name_length, ce->type & ZEND_INTERNAL_CLASS);
3522 				property_info.name = priv_name;
3523 				property_info.name_length = priv_name_length;
3524 			}
3525 			break;
3526 		case ZEND_ACC_PROTECTED: {
3527 				char *prot_name;
3528 				int prot_name_length;
3529 
3530 				zend_mangle_property_name(&prot_name, &prot_name_length, "*", 1, name, name_length, ce->type & ZEND_INTERNAL_CLASS);
3531 				property_info.name = prot_name;
3532 				property_info.name_length = prot_name_length;
3533 			}
3534 			break;
3535 		case ZEND_ACC_PUBLIC:
3536 			if (IS_INTERNED(name)) {
3537 				property_info.name = (char*)name;
3538 			} else {
3539 				property_info.name = ce->type & ZEND_INTERNAL_CLASS ? zend_strndup(name, name_length) : estrndup(name, name_length);
3540 			}
3541 			property_info.name_length = name_length;
3542 			break;
3543 	}
3544 
3545 	interned_name = zend_new_interned_string(property_info.name, property_info.name_length+1, 0 TSRMLS_CC);
3546 	if (interned_name != property_info.name) {
3547 		if (ce->type == ZEND_USER_CLASS) {
3548 			efree((char*)property_info.name);
3549 		} else {
3550 			free((char*)property_info.name);
3551 		}
3552 		property_info.name = interned_name;
3553 	}
3554 
3555 	property_info.flags = access_type;
3556 	property_info.h = (access_type & ZEND_ACC_PUBLIC) ? h : zend_get_hash_value(property_info.name, property_info.name_length+1);
3557 
3558 	property_info.doc_comment = doc_comment;
3559 	property_info.doc_comment_len = doc_comment_len;
3560 
3561 	property_info.ce = ce;
3562 
3563 	zend_hash_quick_update(&ce->properties_info, name, name_length+1, h, &property_info, sizeof(zend_property_info), NULL);
3564 
3565 	return SUCCESS;
3566 }
3567 /* }}} */
3568 
zend_declare_property(zend_class_entry * ce,const char * name,int name_length,zval * property,int access_type TSRMLS_DC)3569 ZEND_API int zend_declare_property(zend_class_entry *ce, const char *name, int name_length, zval *property, int access_type TSRMLS_DC) /* {{{ */
3570 {
3571 	return zend_declare_property_ex(ce, name, name_length, property, access_type, NULL, 0 TSRMLS_CC);
3572 }
3573 /* }}} */
3574 
zend_declare_property_null(zend_class_entry * ce,const char * name,int name_length,int access_type TSRMLS_DC)3575 ZEND_API int zend_declare_property_null(zend_class_entry *ce, const char *name, int name_length, int access_type TSRMLS_DC) /* {{{ */
3576 {
3577 	zval *property;
3578 
3579 	if (ce->type & ZEND_INTERNAL_CLASS) {
3580 		ALLOC_PERMANENT_ZVAL(property);
3581 	} else {
3582 		ALLOC_ZVAL(property);
3583 	}
3584 	INIT_ZVAL(*property);
3585 	return zend_declare_property(ce, name, name_length, property, access_type TSRMLS_CC);
3586 }
3587 /* }}} */
3588 
zend_declare_property_bool(zend_class_entry * ce,const char * name,int name_length,long value,int access_type TSRMLS_DC)3589 ZEND_API int zend_declare_property_bool(zend_class_entry *ce, const char *name, int name_length, long value, int access_type TSRMLS_DC) /* {{{ */
3590 {
3591 	zval *property;
3592 
3593 	if (ce->type & ZEND_INTERNAL_CLASS) {
3594 		ALLOC_PERMANENT_ZVAL(property);
3595 	} else {
3596 		ALLOC_ZVAL(property);
3597 	}
3598 	INIT_PZVAL(property);
3599 	ZVAL_BOOL(property, value);
3600 	return zend_declare_property(ce, name, name_length, property, access_type TSRMLS_CC);
3601 }
3602 /* }}} */
3603 
zend_declare_property_long(zend_class_entry * ce,const char * name,int name_length,long value,int access_type TSRMLS_DC)3604 ZEND_API int zend_declare_property_long(zend_class_entry *ce, const char *name, int name_length, long value, int access_type TSRMLS_DC) /* {{{ */
3605 {
3606 	zval *property;
3607 
3608 	if (ce->type & ZEND_INTERNAL_CLASS) {
3609 		ALLOC_PERMANENT_ZVAL(property);
3610 	} else {
3611 		ALLOC_ZVAL(property);
3612 	}
3613 	INIT_PZVAL(property);
3614 	ZVAL_LONG(property, value);
3615 	return zend_declare_property(ce, name, name_length, property, access_type TSRMLS_CC);
3616 }
3617 /* }}} */
3618 
zend_declare_property_double(zend_class_entry * ce,const char * name,int name_length,double value,int access_type TSRMLS_DC)3619 ZEND_API int zend_declare_property_double(zend_class_entry *ce, const char *name, int name_length, double value, int access_type TSRMLS_DC) /* {{{ */
3620 {
3621 	zval *property;
3622 
3623 	if (ce->type & ZEND_INTERNAL_CLASS) {
3624 		ALLOC_PERMANENT_ZVAL(property);
3625 	} else {
3626 		ALLOC_ZVAL(property);
3627 	}
3628 	INIT_PZVAL(property);
3629 	ZVAL_DOUBLE(property, value);
3630 	return zend_declare_property(ce, name, name_length, property, access_type TSRMLS_CC);
3631 }
3632 /* }}} */
3633 
zend_declare_property_string(zend_class_entry * ce,const char * name,int name_length,const char * value,int access_type TSRMLS_DC)3634 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) /* {{{ */
3635 {
3636 	zval *property;
3637 	int len = strlen(value);
3638 
3639 	if (ce->type & ZEND_INTERNAL_CLASS) {
3640 		ALLOC_PERMANENT_ZVAL(property);
3641 		ZVAL_STRINGL(property, zend_strndup(value, len), len, 0);
3642 	} else {
3643 		ALLOC_ZVAL(property);
3644 		ZVAL_STRINGL(property, value, len, 1);
3645 	}
3646 	INIT_PZVAL(property);
3647 	return zend_declare_property(ce, name, name_length, property, access_type TSRMLS_CC);
3648 }
3649 /* }}} */
3650 
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)3651 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) /* {{{ */
3652 {
3653 	zval *property;
3654 
3655 	if (ce->type & ZEND_INTERNAL_CLASS) {
3656 		ALLOC_PERMANENT_ZVAL(property);
3657 		ZVAL_STRINGL(property, zend_strndup(value, value_len), value_len, 0);
3658 	} else {
3659 		ALLOC_ZVAL(property);
3660 		ZVAL_STRINGL(property, value, value_len, 1);
3661 	}
3662 	INIT_PZVAL(property);
3663 	return zend_declare_property(ce, name, name_length, property, access_type TSRMLS_CC);
3664 }
3665 /* }}} */
3666 
zend_declare_class_constant(zend_class_entry * ce,const char * name,size_t name_length,zval * value TSRMLS_DC)3667 ZEND_API int zend_declare_class_constant(zend_class_entry *ce, const char *name, size_t name_length, zval *value TSRMLS_DC) /* {{{ */
3668 {
3669 	return zend_hash_update(&ce->constants_table, name, name_length+1, &value, sizeof(zval *), NULL);
3670 }
3671 /* }}} */
3672 
zend_declare_class_constant_null(zend_class_entry * ce,const char * name,size_t name_length TSRMLS_DC)3673 ZEND_API int zend_declare_class_constant_null(zend_class_entry *ce, const char *name, size_t name_length TSRMLS_DC) /* {{{ */
3674 {
3675 	zval *constant;
3676 
3677 	if (ce->type & ZEND_INTERNAL_CLASS) {
3678 		ALLOC_PERMANENT_ZVAL(constant);
3679 	} else {
3680 		ALLOC_ZVAL(constant);
3681 	}
3682 	ZVAL_NULL(constant);
3683 	INIT_PZVAL(constant);
3684 	return zend_declare_class_constant(ce, name, name_length, constant TSRMLS_CC);
3685 }
3686 /* }}} */
3687 
zend_declare_class_constant_long(zend_class_entry * ce,const char * name,size_t name_length,long value TSRMLS_DC)3688 ZEND_API int zend_declare_class_constant_long(zend_class_entry *ce, const char *name, size_t name_length, long value TSRMLS_DC) /* {{{ */
3689 {
3690 	zval *constant;
3691 
3692 	if (ce->type & ZEND_INTERNAL_CLASS) {
3693 		ALLOC_PERMANENT_ZVAL(constant);
3694 	} else {
3695 		ALLOC_ZVAL(constant);
3696 	}
3697 	ZVAL_LONG(constant, value);
3698 	INIT_PZVAL(constant);
3699 	return zend_declare_class_constant(ce, name, name_length, constant TSRMLS_CC);
3700 }
3701 /* }}} */
3702 
zend_declare_class_constant_bool(zend_class_entry * ce,const char * name,size_t name_length,zend_bool value TSRMLS_DC)3703 ZEND_API int zend_declare_class_constant_bool(zend_class_entry *ce, const char *name, size_t name_length, zend_bool value TSRMLS_DC) /* {{{ */
3704 {
3705 	zval *constant;
3706 
3707 	if (ce->type & ZEND_INTERNAL_CLASS) {
3708 		ALLOC_PERMANENT_ZVAL(constant);
3709 	} else {
3710 		ALLOC_ZVAL(constant);
3711 	}
3712 	ZVAL_BOOL(constant, value);
3713 	INIT_PZVAL(constant);
3714 	return zend_declare_class_constant(ce, name, name_length, constant TSRMLS_CC);
3715 }
3716 /* }}} */
3717 
zend_declare_class_constant_double(zend_class_entry * ce,const char * name,size_t name_length,double value TSRMLS_DC)3718 ZEND_API int zend_declare_class_constant_double(zend_class_entry *ce, const char *name, size_t name_length, double value TSRMLS_DC) /* {{{ */
3719 {
3720 	zval *constant;
3721 
3722 	if (ce->type & ZEND_INTERNAL_CLASS) {
3723 		ALLOC_PERMANENT_ZVAL(constant);
3724 	} else {
3725 		ALLOC_ZVAL(constant);
3726 	}
3727 	ZVAL_DOUBLE(constant, value);
3728 	INIT_PZVAL(constant);
3729 	return zend_declare_class_constant(ce, name, name_length, constant TSRMLS_CC);
3730 }
3731 /* }}} */
3732 
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)3733 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) /* {{{ */
3734 {
3735 	zval *constant;
3736 
3737 	if (ce->type & ZEND_INTERNAL_CLASS) {
3738 		ALLOC_PERMANENT_ZVAL(constant);
3739 		ZVAL_STRINGL(constant, zend_strndup(value, value_length), value_length, 0);
3740 	} else {
3741 		ALLOC_ZVAL(constant);
3742 		ZVAL_STRINGL(constant, value, value_length, 1);
3743 	}
3744 	INIT_PZVAL(constant);
3745 	return zend_declare_class_constant(ce, name, name_length, constant TSRMLS_CC);
3746 }
3747 /* }}} */
3748 
zend_declare_class_constant_string(zend_class_entry * ce,const char * name,size_t name_length,const char * value TSRMLS_DC)3749 ZEND_API int zend_declare_class_constant_string(zend_class_entry *ce, const char *name, size_t name_length, const char *value TSRMLS_DC) /* {{{ */
3750 {
3751 	return zend_declare_class_constant_stringl(ce, name, name_length, value, strlen(value) TSRMLS_CC);
3752 }
3753 /* }}} */
3754 
zend_update_property(zend_class_entry * scope,zval * object,const char * name,int name_length,zval * value TSRMLS_DC)3755 ZEND_API void zend_update_property(zend_class_entry *scope, zval *object, const char *name, int name_length, zval *value TSRMLS_DC) /* {{{ */
3756 {
3757 	zval *property;
3758 	zend_class_entry *old_scope = EG(scope);
3759 
3760 	EG(scope) = scope;
3761 
3762 	if (!Z_OBJ_HT_P(object)->write_property) {
3763 		const char *class_name;
3764 		zend_uint class_name_len;
3765 
3766 		zend_get_object_classname(object, &class_name, &class_name_len TSRMLS_CC);
3767 
3768 		zend_error(E_CORE_ERROR, "Property %s of class %s cannot be updated", name, class_name);
3769 	}
3770 	MAKE_STD_ZVAL(property);
3771 	ZVAL_STRINGL(property, name, name_length, 1);
3772 	Z_OBJ_HT_P(object)->write_property(object, property, value, 0 TSRMLS_CC);
3773 	zval_ptr_dtor(&property);
3774 
3775 	EG(scope) = old_scope;
3776 }
3777 /* }}} */
3778 
zend_unset_property(zend_class_entry * scope,zval * object,const char * name,int name_length TSRMLS_DC)3779 ZEND_API void zend_unset_property(zend_class_entry *scope, zval *object, const char *name, int name_length TSRMLS_DC) /* {{{ */
3780 {
3781 	zval *property;
3782 	zend_class_entry *old_scope = EG(scope);
3783 
3784 	EG(scope) = scope;
3785 
3786 	if (!Z_OBJ_HT_P(object)->unset_property) {
3787 		const char *class_name;
3788 		zend_uint class_name_len;
3789 
3790 		zend_get_object_classname(object, &class_name, &class_name_len TSRMLS_CC);
3791 
3792 		zend_error(E_CORE_ERROR, "Property %s of class %s cannot be unset", name, class_name);
3793 	}
3794 	MAKE_STD_ZVAL(property);
3795 	ZVAL_STRINGL(property, name, name_length, 1);
3796 	Z_OBJ_HT_P(object)->unset_property(object, property, 0 TSRMLS_CC);
3797 	zval_ptr_dtor(&property);
3798 
3799 	EG(scope) = old_scope;
3800 }
3801 /* }}} */
3802 
zend_update_property_null(zend_class_entry * scope,zval * object,const char * name,int name_length TSRMLS_DC)3803 ZEND_API void zend_update_property_null(zend_class_entry *scope, zval *object, const char *name, int name_length TSRMLS_DC) /* {{{ */
3804 {
3805 	zval *tmp;
3806 
3807 	ALLOC_ZVAL(tmp);
3808 	Z_UNSET_ISREF_P(tmp);
3809 	Z_SET_REFCOUNT_P(tmp, 0);
3810 	ZVAL_NULL(tmp);
3811 	zend_update_property(scope, object, name, name_length, tmp TSRMLS_CC);
3812 }
3813 /* }}} */
3814 
zend_update_property_bool(zend_class_entry * scope,zval * object,const char * name,int name_length,long value TSRMLS_DC)3815 ZEND_API void zend_update_property_bool(zend_class_entry *scope, zval *object, const char *name, int name_length, long value TSRMLS_DC) /* {{{ */
3816 {
3817 	zval *tmp;
3818 
3819 	ALLOC_ZVAL(tmp);
3820 	Z_UNSET_ISREF_P(tmp);
3821 	Z_SET_REFCOUNT_P(tmp, 0);
3822 	ZVAL_BOOL(tmp, value);
3823 	zend_update_property(scope, object, name, name_length, tmp TSRMLS_CC);
3824 }
3825 /* }}} */
3826 
zend_update_property_long(zend_class_entry * scope,zval * object,const char * name,int name_length,long value TSRMLS_DC)3827 ZEND_API void zend_update_property_long(zend_class_entry *scope, zval *object, const char *name, int name_length, long value TSRMLS_DC) /* {{{ */
3828 {
3829 	zval *tmp;
3830 
3831 	ALLOC_ZVAL(tmp);
3832 	Z_UNSET_ISREF_P(tmp);
3833 	Z_SET_REFCOUNT_P(tmp, 0);
3834 	ZVAL_LONG(tmp, value);
3835 	zend_update_property(scope, object, name, name_length, tmp TSRMLS_CC);
3836 }
3837 /* }}} */
3838 
zend_update_property_double(zend_class_entry * scope,zval * object,const char * name,int name_length,double value TSRMLS_DC)3839 ZEND_API void zend_update_property_double(zend_class_entry *scope, zval *object, const char *name, int name_length, double value TSRMLS_DC) /* {{{ */
3840 {
3841 	zval *tmp;
3842 
3843 	ALLOC_ZVAL(tmp);
3844 	Z_UNSET_ISREF_P(tmp);
3845 	Z_SET_REFCOUNT_P(tmp, 0);
3846 	ZVAL_DOUBLE(tmp, value);
3847 	zend_update_property(scope, object, name, name_length, tmp TSRMLS_CC);
3848 }
3849 /* }}} */
3850 
zend_update_property_string(zend_class_entry * scope,zval * object,const char * name,int name_length,const char * value TSRMLS_DC)3851 ZEND_API void zend_update_property_string(zend_class_entry *scope, zval *object, const char *name, int name_length, const char *value TSRMLS_DC) /* {{{ */
3852 {
3853 	zval *tmp;
3854 
3855 	ALLOC_ZVAL(tmp);
3856 	Z_UNSET_ISREF_P(tmp);
3857 	Z_SET_REFCOUNT_P(tmp, 0);
3858 	ZVAL_STRING(tmp, value, 1);
3859 	zend_update_property(scope, object, name, name_length, tmp TSRMLS_CC);
3860 }
3861 /* }}} */
3862 
zend_update_property_stringl(zend_class_entry * scope,zval * object,const char * name,int name_length,const char * value,int value_len TSRMLS_DC)3863 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) /* {{{ */
3864 {
3865 	zval *tmp;
3866 
3867 	ALLOC_ZVAL(tmp);
3868 	Z_UNSET_ISREF_P(tmp);
3869 	Z_SET_REFCOUNT_P(tmp, 0);
3870 	ZVAL_STRINGL(tmp, value, value_len, 1);
3871 	zend_update_property(scope, object, name, name_length, tmp TSRMLS_CC);
3872 }
3873 /* }}} */
3874 
zend_update_static_property(zend_class_entry * scope,const char * name,int name_length,zval * value TSRMLS_DC)3875 ZEND_API int zend_update_static_property(zend_class_entry *scope, const char *name, int name_length, zval *value TSRMLS_DC) /* {{{ */
3876 {
3877 	zval **property;
3878 	zend_class_entry *old_scope = EG(scope);
3879 
3880 	EG(scope) = scope;
3881 	property = zend_std_get_static_property(scope, name, name_length, 0, NULL TSRMLS_CC);
3882 	EG(scope) = old_scope;
3883 	if (!property) {
3884 		return FAILURE;
3885 	} else {
3886 		if (*property != value) {
3887 			if (PZVAL_IS_REF(*property)) {
3888 				zval_dtor(*property);
3889 				Z_TYPE_PP(property) = Z_TYPE_P(value);
3890 				(*property)->value = value->value;
3891 				if (Z_REFCOUNT_P(value) > 0) {
3892 					zval_copy_ctor(*property);
3893 				} else {
3894 					efree(value);
3895 				}
3896 			} else {
3897 				zval *garbage = *property;
3898 
3899 				Z_ADDREF_P(value);
3900 				if (PZVAL_IS_REF(value)) {
3901 					SEPARATE_ZVAL(&value);
3902 				}
3903 				*property = value;
3904 				zval_ptr_dtor(&garbage);
3905 			}
3906 		}
3907 		return SUCCESS;
3908 	}
3909 }
3910 /* }}} */
3911 
zend_update_static_property_null(zend_class_entry * scope,const char * name,int name_length TSRMLS_DC)3912 ZEND_API int zend_update_static_property_null(zend_class_entry *scope, const char *name, int name_length TSRMLS_DC) /* {{{ */
3913 {
3914 	zval *tmp;
3915 
3916 	ALLOC_ZVAL(tmp);
3917 	Z_UNSET_ISREF_P(tmp);
3918 	Z_SET_REFCOUNT_P(tmp, 0);
3919 	ZVAL_NULL(tmp);
3920 	return zend_update_static_property(scope, name, name_length, tmp TSRMLS_CC);
3921 }
3922 /* }}} */
3923 
zend_update_static_property_bool(zend_class_entry * scope,const char * name,int name_length,long value TSRMLS_DC)3924 ZEND_API int zend_update_static_property_bool(zend_class_entry *scope, const char *name, int name_length, long value TSRMLS_DC) /* {{{ */
3925 {
3926 	zval *tmp;
3927 
3928 	ALLOC_ZVAL(tmp);
3929 	Z_UNSET_ISREF_P(tmp);
3930 	Z_SET_REFCOUNT_P(tmp, 0);
3931 	ZVAL_BOOL(tmp, value);
3932 	return zend_update_static_property(scope, name, name_length, tmp TSRMLS_CC);
3933 }
3934 /* }}} */
3935 
zend_update_static_property_long(zend_class_entry * scope,const char * name,int name_length,long value TSRMLS_DC)3936 ZEND_API int zend_update_static_property_long(zend_class_entry *scope, const char *name, int name_length, long value TSRMLS_DC) /* {{{ */
3937 {
3938 	zval *tmp;
3939 
3940 	ALLOC_ZVAL(tmp);
3941 	Z_UNSET_ISREF_P(tmp);
3942 	Z_SET_REFCOUNT_P(tmp, 0);
3943 	ZVAL_LONG(tmp, value);
3944 	return zend_update_static_property(scope, name, name_length, tmp TSRMLS_CC);
3945 }
3946 /* }}} */
3947 
zend_update_static_property_double(zend_class_entry * scope,const char * name,int name_length,double value TSRMLS_DC)3948 ZEND_API int zend_update_static_property_double(zend_class_entry *scope, const char *name, int name_length, double value TSRMLS_DC) /* {{{ */
3949 {
3950 	zval *tmp;
3951 
3952 	ALLOC_ZVAL(tmp);
3953 	Z_UNSET_ISREF_P(tmp);
3954 	Z_SET_REFCOUNT_P(tmp, 0);
3955 	ZVAL_DOUBLE(tmp, value);
3956 	return zend_update_static_property(scope, name, name_length, tmp TSRMLS_CC);
3957 }
3958 /* }}} */
3959 
zend_update_static_property_string(zend_class_entry * scope,const char * name,int name_length,const char * value TSRMLS_DC)3960 ZEND_API int zend_update_static_property_string(zend_class_entry *scope, const char *name, int name_length, const char *value TSRMLS_DC) /* {{{ */
3961 {
3962 	zval *tmp;
3963 
3964 	ALLOC_ZVAL(tmp);
3965 	Z_UNSET_ISREF_P(tmp);
3966 	Z_SET_REFCOUNT_P(tmp, 0);
3967 	ZVAL_STRING(tmp, value, 1);
3968 	return zend_update_static_property(scope, name, name_length, tmp TSRMLS_CC);
3969 }
3970 /* }}} */
3971 
zend_update_static_property_stringl(zend_class_entry * scope,const char * name,int name_length,const char * value,int value_len TSRMLS_DC)3972 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) /* {{{ */
3973 {
3974 	zval *tmp;
3975 
3976 	ALLOC_ZVAL(tmp);
3977 	Z_UNSET_ISREF_P(tmp);
3978 	Z_SET_REFCOUNT_P(tmp, 0);
3979 	ZVAL_STRINGL(tmp, value, value_len, 1);
3980 	return zend_update_static_property(scope, name, name_length, tmp TSRMLS_CC);
3981 }
3982 /* }}} */
3983 
zend_read_property(zend_class_entry * scope,zval * object,const char * name,int name_length,zend_bool silent TSRMLS_DC)3984 ZEND_API zval *zend_read_property(zend_class_entry *scope, zval *object, const char *name, int name_length, zend_bool silent TSRMLS_DC) /* {{{ */
3985 {
3986 	zval *property, *value;
3987 	zend_class_entry *old_scope = EG(scope);
3988 
3989 	EG(scope) = scope;
3990 
3991 	if (!Z_OBJ_HT_P(object)->read_property) {
3992 		const char *class_name;
3993 		zend_uint class_name_len;
3994 
3995 		zend_get_object_classname(object, &class_name, &class_name_len TSRMLS_CC);
3996 		zend_error(E_CORE_ERROR, "Property %s of class %s cannot be read", name, class_name);
3997 	}
3998 
3999 	MAKE_STD_ZVAL(property);
4000 	ZVAL_STRINGL(property, name, name_length, 1);
4001 	value = Z_OBJ_HT_P(object)->read_property(object, property, silent?BP_VAR_IS:BP_VAR_R, 0 TSRMLS_CC);
4002 	zval_ptr_dtor(&property);
4003 
4004 	EG(scope) = old_scope;
4005 	return value;
4006 }
4007 /* }}} */
4008 
zend_read_static_property(zend_class_entry * scope,const char * name,int name_length,zend_bool silent TSRMLS_DC)4009 ZEND_API zval *zend_read_static_property(zend_class_entry *scope, const char *name, int name_length, zend_bool silent TSRMLS_DC) /* {{{ */
4010 {
4011 	zval **property;
4012 	zend_class_entry *old_scope = EG(scope);
4013 
4014 	EG(scope) = scope;
4015 	property = zend_std_get_static_property(scope, name, name_length, silent, NULL TSRMLS_CC);
4016 	EG(scope) = old_scope;
4017 
4018 	return property?*property:NULL;
4019 }
4020 /* }}} */
4021 
zend_save_error_handling(zend_error_handling * current TSRMLS_DC)4022 ZEND_API void zend_save_error_handling(zend_error_handling *current TSRMLS_DC) /* {{{ */
4023 {
4024 	current->handling = EG(error_handling);
4025 	current->exception = EG(exception_class);
4026 	current->user_handler = EG(user_error_handler);
4027 	if (current->user_handler) {
4028 		Z_ADDREF_P(current->user_handler);
4029 	}
4030 }
4031 /* }}} */
4032 
zend_replace_error_handling(zend_error_handling_t error_handling,zend_class_entry * exception_class,zend_error_handling * current TSRMLS_DC)4033 ZEND_API void zend_replace_error_handling(zend_error_handling_t error_handling, zend_class_entry *exception_class, zend_error_handling *current TSRMLS_DC) /* {{{ */
4034 {
4035 	if (current) {
4036 		zend_save_error_handling(current TSRMLS_CC);
4037 		if (error_handling != EH_NORMAL && EG(user_error_handler)) {
4038 			zval_ptr_dtor(&EG(user_error_handler));
4039 			EG(user_error_handler) = NULL;
4040 		}
4041 	}
4042 	EG(error_handling) = error_handling;
4043 	EG(exception_class) = error_handling == EH_THROW ? exception_class : NULL;
4044 }
4045 /* }}} */
4046 
zend_restore_error_handling(zend_error_handling * saved TSRMLS_DC)4047 ZEND_API void zend_restore_error_handling(zend_error_handling *saved TSRMLS_DC) /* {{{ */
4048 {
4049 	EG(error_handling) = saved->handling;
4050 	EG(exception_class) = saved->handling == EH_THROW ? saved->exception : NULL;
4051 	if (saved->user_handler	&& saved->user_handler != EG(user_error_handler)) {
4052 		if (EG(user_error_handler)) {
4053 			zval_ptr_dtor(&EG(user_error_handler));
4054 		}
4055 		EG(user_error_handler) = saved->user_handler;
4056 	} else if (saved->user_handler) {
4057 		zval_ptr_dtor(&saved->user_handler);
4058 	}
4059 	saved->user_handler = NULL;
4060 }
4061 /* }}} */
4062 
zend_find_alias_name(zend_class_entry * ce,const char * name,zend_uint len)4063 ZEND_API const char* zend_find_alias_name(zend_class_entry *ce, const char *name, zend_uint len) /* {{{ */
4064 {
4065 	zend_trait_alias *alias, **alias_ptr;
4066 
4067 	if ((alias_ptr = ce->trait_aliases)) {
4068 		alias = *alias_ptr;
4069 		while (alias) {
4070 			if (alias->alias_len == len &&
4071 				!strncasecmp(name, alias->alias, alias->alias_len)) {
4072 				return alias->alias;
4073 			}
4074 			alias_ptr++;
4075 			alias = *alias_ptr;
4076 		}
4077 	}
4078 
4079 	return name;
4080 }
4081 /* }}} */
4082 
zend_resolve_method_name(zend_class_entry * ce,zend_function * f)4083 ZEND_API const char* zend_resolve_method_name(zend_class_entry *ce, zend_function *f) /* {{{ */
4084 {
4085 	zend_function *func;
4086 	HashPosition iterator;
4087 	HashTable *function_table;
4088 
4089 	if (f->common.type != ZEND_USER_FUNCTION ||
4090 	    *(f->op_array.refcount) < 2 ||
4091 	    !f->common.scope ||
4092 	    !f->common.scope->trait_aliases) {
4093 		return f->common.function_name;
4094 	}
4095 
4096 	function_table = &ce->function_table;
4097 	zend_hash_internal_pointer_reset_ex(function_table, &iterator);
4098 	while (zend_hash_get_current_data_ex(function_table, (void **)&func, &iterator) == SUCCESS) {
4099 		if (func == f) {
4100 			char *name;
4101 			uint len;
4102 			ulong idx;
4103 
4104 			if (zend_hash_get_current_key_ex(function_table, &name, &len, &idx, 0, &iterator) != HASH_KEY_IS_STRING) {
4105 				return f->common.function_name;
4106 			}
4107 			--len;
4108 			if (len == strlen(f->common.function_name) &&
4109 			    !strncasecmp(name, f->common.function_name, len)) {
4110 				return f->common.function_name;
4111 			}
4112 			return zend_find_alias_name(f->common.scope, name, len);
4113 		}
4114 		zend_hash_move_forward_ex(function_table, &iterator);
4115 	}
4116 	return f->common.function_name;
4117 }
4118 /* }}} */
4119 
4120 /*
4121  * Local variables:
4122  * tab-width: 4
4123  * c-basic-offset: 4
4124  * indent-tabs-mode: t
4125  * End:
4126  */
4127