File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / php / Zend / zend_API.c
Revision 1.1.1.2 (vendor branch): download - view: text, annotated - select for diffs - revision graph
Tue May 29 12:34:35 2012 UTC (12 years, 1 month ago) by misho
Branches: php, MAIN
CVS tags: v5_4_3elwix, v5_4_17p0, HEAD
php 5.4.3+patches

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

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>