/Zend/zend_API.c
C | 4372 lines | 3516 code | 582 blank | 274 comment | 900 complexity | 7b0abf5c34e929660435e49cc03972e1 MD5 | raw file
Possible License(s): BSD-2-Clause, BSD-3-Clause, MPL-2.0-no-copyleft-exception, LGPL-2.1
Large files files are truncated, but you can click here to view the full file
- /*
- +----------------------------------------------------------------------+
- | Zend Engine |
- +----------------------------------------------------------------------+
- | Copyright (c) Zend Technologies Ltd. (http://www.zend.com) |
- +----------------------------------------------------------------------+
- | This source file is subject to version 2.00 of the Zend license, |
- | that is bundled with this package in the file LICENSE, and is |
- | available through the world-wide-web at the following url: |
- | http://www.zend.com/license/2_00.txt. |
- | If you did not receive a copy of the Zend license and are unable to |
- | obtain it through the world-wide-web, please send a note to |
- | license@zend.com so we can mail you a copy immediately. |
- +----------------------------------------------------------------------+
- | Authors: Andi Gutmans <andi@php.net> |
- | Zeev Suraski <zeev@php.net> |
- | Andrei Zmievski <andrei@php.net> |
- | Dmitry Stogov <dmitry@php.net> |
- +----------------------------------------------------------------------+
- */
- #include "zend.h"
- #include "zend_execute.h"
- #include "zend_API.h"
- #include "zend_modules.h"
- #include "zend_extensions.h"
- #include "zend_constants.h"
- #include "zend_interfaces.h"
- #include "zend_exceptions.h"
- #include "zend_closures.h"
- #include "zend_inheritance.h"
- #include "zend_ini.h"
- #include <stdarg.h>
- /* these variables are true statics/globals, and have to be mutex'ed on every access */
- ZEND_API HashTable module_registry;
- static zend_module_entry **module_request_startup_handlers;
- static zend_module_entry **module_request_shutdown_handlers;
- static zend_module_entry **module_post_deactivate_handlers;
- static zend_class_entry **class_cleanup_handlers;
- ZEND_API int _zend_get_parameters_array_ex(int param_count, zval *argument_array) /* {{{ */
- {
- zval *param_ptr;
- int arg_count;
- param_ptr = ZEND_CALL_ARG(EG(current_execute_data), 1);
- arg_count = ZEND_CALL_NUM_ARGS(EG(current_execute_data));
- if (param_count>arg_count) {
- return FAILURE;
- }
- while (param_count-->0) {
- ZVAL_COPY_VALUE(argument_array, param_ptr);
- argument_array++;
- param_ptr++;
- }
- return SUCCESS;
- }
- /* }}} */
- ZEND_API int zend_copy_parameters_array(int param_count, zval *argument_array) /* {{{ */
- {
- zval *param_ptr;
- int arg_count;
- param_ptr = ZEND_CALL_ARG(EG(current_execute_data), 1);
- arg_count = ZEND_CALL_NUM_ARGS(EG(current_execute_data));
- if (param_count>arg_count) {
- return FAILURE;
- }
- while (param_count-->0) {
- Z_TRY_ADDREF_P(param_ptr);
- zend_hash_next_index_insert_new(Z_ARRVAL_P(argument_array), param_ptr);
- param_ptr++;
- }
- return SUCCESS;
- }
- /* }}} */
- ZEND_API ZEND_COLD void zend_wrong_param_count(void) /* {{{ */
- {
- const char *space;
- const char *class_name = get_active_class_name(&space);
- zend_argument_count_error("Wrong parameter count for %s%s%s()", class_name, space, get_active_function_name());
- }
- /* }}} */
- /* Argument parsing API -- andrei */
- ZEND_API const char *zend_get_type_by_const(int type) /* {{{ */
- {
- switch(type) {
- case IS_FALSE:
- case IS_TRUE:
- case _IS_BOOL:
- return "bool";
- case IS_LONG:
- return "int";
- case IS_DOUBLE:
- return "float";
- case IS_STRING:
- return "string";
- case IS_OBJECT:
- return "object";
- case IS_RESOURCE:
- return "resource";
- case IS_NULL:
- return "null";
- case IS_CALLABLE:
- return "callable";
- case IS_ITERABLE:
- return "iterable";
- case IS_ARRAY:
- return "array";
- case IS_VOID:
- return "void";
- case _IS_NUMBER:
- return "number";
- default:
- return "unknown";
- }
- }
- /* }}} */
- ZEND_API const char *zend_zval_type_name(const zval *arg) /* {{{ */
- {
- ZVAL_DEREF(arg);
- return zend_get_type_by_const(Z_TYPE_P(arg));
- }
- /* }}} */
- ZEND_API zend_string *zend_zval_get_type(const zval *arg) /* {{{ */
- {
- switch (Z_TYPE_P(arg)) {
- case IS_NULL:
- return ZSTR_KNOWN(ZEND_STR_NULL);
- case IS_FALSE:
- case IS_TRUE:
- return ZSTR_KNOWN(ZEND_STR_BOOLEAN);
- case IS_LONG:
- return ZSTR_KNOWN(ZEND_STR_INTEGER);
- case IS_DOUBLE:
- return ZSTR_KNOWN(ZEND_STR_DOUBLE);
- case IS_STRING:
- return ZSTR_KNOWN(ZEND_STR_STRING);
- case IS_ARRAY:
- return ZSTR_KNOWN(ZEND_STR_ARRAY);
- case IS_OBJECT:
- return ZSTR_KNOWN(ZEND_STR_OBJECT);
- case IS_RESOURCE:
- if (zend_rsrc_list_get_rsrc_type(Z_RES_P(arg))) {
- return ZSTR_KNOWN(ZEND_STR_RESOURCE);
- } else {
- return ZSTR_KNOWN(ZEND_STR_CLOSED_RESOURCE);
- }
- default:
- return NULL;
- }
- }
- /* }}} */
- ZEND_API ZEND_COLD int ZEND_FASTCALL zend_wrong_parameters_none_error(void) /* {{{ */
- {
- int num_args = ZEND_CALL_NUM_ARGS(EG(current_execute_data));
- zend_function *active_function = EG(current_execute_data)->func;
- const char *class_name = active_function->common.scope ? ZSTR_VAL(active_function->common.scope->name) : "";
- zend_argument_count_error(
- "%s%s%s() expects %s %d parameter%s, %d given",
- class_name, \
- class_name[0] ? "::" : "", \
- ZSTR_VAL(active_function->common.function_name),
- "exactly",
- 0,
- "s",
- num_args);
- return FAILURE;
- }
- /* }}} */
- ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_parameters_count_error(int min_num_args, int max_num_args) /* {{{ */
- {
- int num_args = ZEND_CALL_NUM_ARGS(EG(current_execute_data));
- zend_function *active_function = EG(current_execute_data)->func;
- const char *class_name = active_function->common.scope ? ZSTR_VAL(active_function->common.scope->name) : "";
- zend_argument_count_error(
- "%s%s%s() expects %s %d parameter%s, %d given",
- class_name, \
- class_name[0] ? "::" : "", \
- ZSTR_VAL(active_function->common.function_name),
- min_num_args == max_num_args ? "exactly" : num_args < min_num_args ? "at least" : "at most",
- num_args < min_num_args ? min_num_args : max_num_args,
- (num_args < min_num_args ? min_num_args : max_num_args) == 1 ? "" : "s",
- num_args);
- }
- /* }}} */
- ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_parameter_type_error(int num, zend_expected_type expected_type, zval *arg) /* {{{ */
- {
- static const char * const expected_error[] = {
- Z_EXPECTED_TYPES(Z_EXPECTED_TYPE_STR)
- NULL
- };
- if (EG(exception)) {
- return;
- }
- zend_argument_type_error(num, "must be %s, %s given", expected_error[expected_type], zend_zval_type_name(arg));
- }
- /* }}} */
- ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_parameter_class_error(int num, const char *name, zval *arg) /* {{{ */
- {
- if (EG(exception)) {
- return;
- }
- zend_argument_type_error(num, "must be of type %s, %s given", name, zend_zval_type_name(arg));
- }
- /* }}} */
- ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_parameter_class_or_null_error(int num, const char *name, zval *arg) /* {{{ */
- {
- if (EG(exception)) {
- return;
- }
- zend_argument_type_error(num, "must be of type ?%s, %s given", name, zend_zval_type_name(arg));
- }
- /* }}} */
- ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_callback_error(int num, char *error) /* {{{ */
- {
- if (EG(exception)) {
- return;
- }
- zend_argument_type_error(num, "must be a valid callback, %s", error);
- efree(error);
- }
- /* }}} */
- ZEND_API ZEND_COLD void ZEND_FASTCALL zend_argument_error_variadic(zend_class_entry *error_ce, uint32_t arg_num, const char *format, va_list va) /* {{{ */
- {
- const char *space;
- const char *class_name;
- const char *arg_name;
- char *message = NULL;
- if (EG(exception)) {
- return;
- }
- class_name = get_active_class_name(&space);
- arg_name = get_active_function_arg_name(arg_num);
- zend_vspprintf(&message, 0, format, va);
- zend_throw_error(error_ce, "%s%s%s(): Argument #%d%s%s%s %s",
- class_name, space, get_active_function_name(), arg_num,
- arg_name ? " ($" : "", arg_name ? arg_name : "", arg_name ? ")" : "", message
- );
- efree(message);
- }
- /* }}} */
- ZEND_API ZEND_COLD void ZEND_FASTCALL zend_argument_error(zend_class_entry *error_ce, uint32_t arg_num, const char *format, ...) /* {{{ */
- {
- va_list va;
- va_start(va, format);
- zend_argument_error_variadic(error_ce, arg_num, format, va);
- va_end(va);
- }
- /* }}} */
- ZEND_API ZEND_COLD void ZEND_FASTCALL zend_argument_type_error(uint32_t arg_num, const char *format, ...) /* {{{ */
- {
- va_list va;
- va_start(va, format);
- zend_argument_error_variadic(zend_ce_type_error, arg_num, format, va);
- va_end(va);
- }
- /* }}} */
- ZEND_API ZEND_COLD void ZEND_FASTCALL zend_argument_value_error(uint32_t arg_num, const char *format, ...) /* {{{ */
- {
- va_list va;
- va_start(va, format);
- zend_argument_error_variadic(zend_ce_value_error, arg_num, format, va);
- va_end(va);
- }
- /* }}} */
- ZEND_API int ZEND_FASTCALL zend_parse_arg_class(zval *arg, zend_class_entry **pce, int num, int check_null) /* {{{ */
- {
- zend_class_entry *ce_base = *pce;
- if (check_null && Z_TYPE_P(arg) == IS_NULL) {
- *pce = NULL;
- return 1;
- }
- if (!try_convert_to_string(arg)) {
- *pce = NULL;
- return 0;
- }
- *pce = zend_lookup_class(Z_STR_P(arg));
- if (ce_base) {
- if ((!*pce || !instanceof_function(*pce, ce_base))) {
- zend_argument_type_error(num, "must be a class name derived from %s, '%s' given", ZSTR_VAL(ce_base->name), Z_STRVAL_P(arg));
- *pce = NULL;
- return 0;
- }
- }
- if (!*pce) {
- zend_argument_type_error(num, "must be a valid class name, '%s' given", Z_STRVAL_P(arg));
- return 0;
- }
- return 1;
- }
- /* }}} */
- ZEND_API int ZEND_FASTCALL zend_parse_arg_bool_weak(zval *arg, zend_bool *dest) /* {{{ */
- {
- if (EXPECTED(Z_TYPE_P(arg) <= IS_STRING)) {
- *dest = zend_is_true(arg);
- } else {
- return 0;
- }
- return 1;
- }
- /* }}} */
- ZEND_API int ZEND_FASTCALL zend_parse_arg_bool_slow(zval *arg, zend_bool *dest) /* {{{ */
- {
- if (UNEXPECTED(ZEND_ARG_USES_STRICT_TYPES())) {
- return 0;
- }
- return zend_parse_arg_bool_weak(arg, dest);
- }
- /* }}} */
- ZEND_API int ZEND_FASTCALL zend_parse_arg_long_weak(zval *arg, zend_long *dest) /* {{{ */
- {
- if (EXPECTED(Z_TYPE_P(arg) == IS_DOUBLE)) {
- if (UNEXPECTED(zend_isnan(Z_DVAL_P(arg)))) {
- return 0;
- }
- if (UNEXPECTED(!ZEND_DOUBLE_FITS_LONG(Z_DVAL_P(arg)))) {
- return 0;
- } else {
- *dest = zend_dval_to_lval(Z_DVAL_P(arg));
- }
- } else if (EXPECTED(Z_TYPE_P(arg) == IS_STRING)) {
- double d;
- int type;
- if (UNEXPECTED((type = is_numeric_str_function(Z_STR_P(arg), dest, &d)) != IS_LONG)) {
- if (EXPECTED(type != 0)) {
- if (UNEXPECTED(zend_isnan(d))) {
- return 0;
- }
- if (UNEXPECTED(!ZEND_DOUBLE_FITS_LONG(d))) {
- return 0;
- } else {
- *dest = zend_dval_to_lval(d);
- }
- } else {
- return 0;
- }
- }
- if (UNEXPECTED(EG(exception))) {
- return 0;
- }
- } else if (EXPECTED(Z_TYPE_P(arg) < IS_TRUE)) {
- *dest = 0;
- } else if (EXPECTED(Z_TYPE_P(arg) == IS_TRUE)) {
- *dest = 1;
- } else {
- return 0;
- }
- return 1;
- }
- /* }}} */
- ZEND_API int ZEND_FASTCALL zend_parse_arg_long_slow(zval *arg, zend_long *dest) /* {{{ */
- {
- if (UNEXPECTED(ZEND_ARG_USES_STRICT_TYPES())) {
- return 0;
- }
- return zend_parse_arg_long_weak(arg, dest);
- }
- /* }}} */
- ZEND_API int ZEND_FASTCALL zend_parse_arg_double_weak(zval *arg, double *dest) /* {{{ */
- {
- if (EXPECTED(Z_TYPE_P(arg) == IS_LONG)) {
- *dest = (double)Z_LVAL_P(arg);
- } else if (EXPECTED(Z_TYPE_P(arg) == IS_STRING)) {
- zend_long l;
- int type;
- if (UNEXPECTED((type = is_numeric_str_function(Z_STR_P(arg), &l, dest)) != IS_DOUBLE)) {
- if (EXPECTED(type != 0)) {
- *dest = (double)(l);
- } else {
- return 0;
- }
- }
- if (UNEXPECTED(EG(exception))) {
- return 0;
- }
- } else if (EXPECTED(Z_TYPE_P(arg) < IS_TRUE)) {
- *dest = 0.0;
- } else if (EXPECTED(Z_TYPE_P(arg) == IS_TRUE)) {
- *dest = 1.0;
- } else {
- return 0;
- }
- return 1;
- }
- /* }}} */
- ZEND_API int ZEND_FASTCALL zend_parse_arg_double_slow(zval *arg, double *dest) /* {{{ */
- {
- if (EXPECTED(Z_TYPE_P(arg) == IS_LONG)) {
- /* SSTH Exception: IS_LONG may be accepted instead as IS_DOUBLE */
- *dest = (double)Z_LVAL_P(arg);
- } else if (UNEXPECTED(ZEND_ARG_USES_STRICT_TYPES())) {
- return 0;
- }
- return zend_parse_arg_double_weak(arg, dest);
- }
- /* }}} */
- ZEND_API int ZEND_FASTCALL zend_parse_arg_number_slow(zval *arg, zval **dest) /* {{{ */
- {
- if (UNEXPECTED(ZEND_ARG_USES_STRICT_TYPES())) {
- return 0;
- }
- if (Z_TYPE_P(arg) == IS_STRING) {
- zend_string *str = Z_STR_P(arg);
- zend_long lval;
- double dval;
- zend_uchar type = is_numeric_string(ZSTR_VAL(str), ZSTR_LEN(str), &lval, &dval, -1);
- if (type == IS_LONG) {
- ZVAL_LONG(arg, lval);
- } else if (type == IS_DOUBLE) {
- ZVAL_DOUBLE(arg, dval);
- } else {
- return 0;
- }
- zend_string_release(str);
- } else if (Z_TYPE_P(arg) < IS_TRUE) {
- ZVAL_LONG(arg, 0);
- } else if (Z_TYPE_P(arg) == IS_TRUE) {
- ZVAL_LONG(arg, 1);
- } else {
- return 0;
- }
- *dest = arg;
- return 1;
- }
- /* }}} */
- ZEND_API int ZEND_FASTCALL zend_parse_arg_str_weak(zval *arg, zend_string **dest) /* {{{ */
- {
- if (EXPECTED(Z_TYPE_P(arg) < IS_STRING)) {
- convert_to_string(arg);
- *dest = Z_STR_P(arg);
- } else if (UNEXPECTED(Z_TYPE_P(arg) == IS_OBJECT)) {
- zend_object *zobj = Z_OBJ_P(arg);
- zval obj;
- if (zobj->handlers->cast_object(zobj, &obj, IS_STRING) == SUCCESS) {
- OBJ_RELEASE(zobj);
- ZVAL_COPY_VALUE(arg, &obj);
- *dest = Z_STR_P(arg);
- return 1;
- }
- return 0;
- } else {
- return 0;
- }
- return 1;
- }
- /* }}} */
- ZEND_API int ZEND_FASTCALL zend_parse_arg_str_slow(zval *arg, zend_string **dest) /* {{{ */
- {
- if (UNEXPECTED(ZEND_ARG_USES_STRICT_TYPES())) {
- return 0;
- }
- return zend_parse_arg_str_weak(arg, dest);
- }
- /* }}} */
- ZEND_API int ZEND_FASTCALL zend_parse_arg_str_or_long_slow(zval *arg, zend_string **dest_str, zend_long *dest_long) /* {{{ */
- {
- if (UNEXPECTED(ZEND_ARG_USES_STRICT_TYPES())) {
- return 0;
- }
- if (zend_parse_arg_long_weak(arg, dest_long)) {
- *dest_str = NULL;
- return 1;
- } else if (zend_parse_arg_str_weak(arg, dest_str)) {
- *dest_long = 0;
- return 1;
- } else {
- return 0;
- }
- }
- /* }}} */
- static const char *zend_parse_arg_impl(int arg_num, zval *arg, va_list *va, const char **spec, char **error) /* {{{ */
- {
- const char *spec_walk = *spec;
- char c = *spec_walk++;
- int check_null = 0;
- int separate = 0;
- zval *real_arg = arg;
- /* scan through modifiers */
- ZVAL_DEREF(arg);
- while (1) {
- if (*spec_walk == '/') {
- SEPARATE_ZVAL_NOREF(arg);
- real_arg = arg;
- separate = 1;
- } else if (*spec_walk == '!') {
- check_null = 1;
- } else {
- break;
- }
- spec_walk++;
- }
- switch (c) {
- case 'l':
- {
- zend_long *p = va_arg(*va, zend_long *);
- zend_bool *is_null = NULL;
- if (check_null) {
- is_null = va_arg(*va, zend_bool *);
- }
- if (!zend_parse_arg_long(arg, p, is_null, check_null)) {
- return check_null ? "?int" : "int";
- }
- }
- break;
- case 'd':
- {
- double *p = va_arg(*va, double *);
- zend_bool *is_null = NULL;
- if (check_null) {
- is_null = va_arg(*va, zend_bool *);
- }
- if (!zend_parse_arg_double(arg, p, is_null, check_null)) {
- return check_null ? "?float" : "float";
- }
- }
- break;
- case 'n':
- {
- zval **p = va_arg(*va, zval **);
- if (!zend_parse_arg_number(arg, p, check_null)) {
- return check_null ? "int|float|null" : "int|float";
- }
- }
- break;
- case 's':
- {
- char **p = va_arg(*va, char **);
- size_t *pl = va_arg(*va, size_t *);
- if (!zend_parse_arg_string(arg, p, pl, check_null)) {
- return check_null ? "?string" : "string";
- }
- }
- break;
- case 'p':
- {
- char **p = va_arg(*va, char **);
- size_t *pl = va_arg(*va, size_t *);
- if (!zend_parse_arg_path(arg, p, pl, check_null)) {
- zend_spprintf(error, 0, "a valid path%s, %s given",
- check_null ? " or null" : "", zend_zval_type_name(arg)
- );
- return "";
- }
- }
- break;
- case 'P':
- {
- zend_string **str = va_arg(*va, zend_string **);
- if (!zend_parse_arg_path_str(arg, str, check_null)) {
- zend_spprintf(error, 0, "a valid path%s, %s given",
- check_null ? " or null" : "", zend_zval_type_name(arg)
- );
- return "";
- }
- }
- break;
- case 'S':
- {
- zend_string **str = va_arg(*va, zend_string **);
- if (!zend_parse_arg_str(arg, str, check_null)) {
- return check_null ? "?string" : "string";
- }
- }
- break;
- case 'b':
- {
- zend_bool *p = va_arg(*va, zend_bool *);
- zend_bool *is_null = NULL;
- if (check_null) {
- is_null = va_arg(*va, zend_bool *);
- }
- if (!zend_parse_arg_bool(arg, p, is_null, check_null)) {
- return check_null ? "?bool" : "bool";
- }
- }
- break;
- case 'r':
- {
- zval **p = va_arg(*va, zval **);
- if (!zend_parse_arg_resource(arg, p, check_null)) {
- return check_null ? "resource or null" : "resource";
- }
- }
- break;
- case 'A':
- case 'a':
- {
- zval **p = va_arg(*va, zval **);
- if (!zend_parse_arg_array(arg, p, check_null, c == 'A')) {
- return check_null ? "?array" : "array";
- }
- }
- break;
- case 'H':
- case 'h':
- {
- HashTable **p = va_arg(*va, HashTable **);
- if (!zend_parse_arg_array_ht(arg, p, check_null, c == 'H', separate)) {
- return check_null ? "?array" : "array";
- }
- }
- break;
- case 'o':
- {
- zval **p = va_arg(*va, zval **);
- if (!zend_parse_arg_object(arg, p, NULL, check_null)) {
- return check_null ? "?object" : "object";
- }
- }
- break;
- case 'O':
- {
- zval **p = va_arg(*va, zval **);
- zend_class_entry *ce = va_arg(*va, zend_class_entry *);
- if (!zend_parse_arg_object(arg, p, ce, check_null)) {
- if (ce) {
- if (check_null) {
- zend_spprintf(error, 0, "of type ?%s, %s given", ZSTR_VAL(ce->name), zend_zval_type_name(arg));
- return "";
- } else {
- return ZSTR_VAL(ce->name);
- }
- } else {
- return check_null ? "?object" : "object";
- }
- }
- }
- break;
- case 'C':
- {
- zend_class_entry *lookup, **pce = va_arg(*va, zend_class_entry **);
- zend_class_entry *ce_base = *pce;
- if (check_null && Z_TYPE_P(arg) == IS_NULL) {
- *pce = NULL;
- break;
- }
- if (!try_convert_to_string(arg)) {
- *pce = NULL;
- return ""; /* try_convert_to_string() throws an exception */
- }
- if ((lookup = zend_lookup_class(Z_STR_P(arg))) == NULL) {
- *pce = NULL;
- } else {
- *pce = lookup;
- }
- if (ce_base) {
- if ((!*pce || !instanceof_function(*pce, ce_base))) {
- zend_spprintf(error, 0, "a class name derived from %s%s, '%s' given",
- ZSTR_VAL(ce_base->name), check_null ? " or null" : "", Z_STRVAL_P(arg));
- *pce = NULL;
- return "";
- }
- }
- if (!*pce) {
- zend_spprintf(error, 0, "a valid class name%s, '%s' given",
- check_null ? " or null" : "", Z_STRVAL_P(arg));
- return "";
- }
- break;
- }
- break;
- case 'f':
- {
- zend_fcall_info *fci = va_arg(*va, zend_fcall_info *);
- zend_fcall_info_cache *fcc = va_arg(*va, zend_fcall_info_cache *);
- char *is_callable_error = NULL;
- if (check_null && Z_TYPE_P(arg) == IS_NULL) {
- fci->size = 0;
- fcc->function_handler = 0;
- break;
- }
- if (zend_fcall_info_init(arg, 0, fci, fcc, NULL, &is_callable_error) == SUCCESS) {
- ZEND_ASSERT(!is_callable_error);
- break;
- }
- if (is_callable_error) {
- zend_spprintf(error, 0, "a valid callback%s, %s", check_null ? " or null" : "", is_callable_error);
- efree(is_callable_error);
- return "";
- } else {
- return check_null ? "a valid callback or null" : "a valid callback";
- }
- }
- case 'z':
- {
- zval **p = va_arg(*va, zval **);
- zend_parse_arg_zval_deref(real_arg, p, check_null);
- }
- break;
- case 'Z': /* replace with 'z' */
- case 'L': /* replace with 'l' */
- ZEND_ASSERT(0 && "ZPP modifier no longer supported");
- default:
- return "unknown";
- }
- *spec = spec_walk;
- return NULL;
- }
- /* }}} */
- static int zend_parse_arg(int arg_num, zval *arg, va_list *va, const char **spec, int flags) /* {{{ */
- {
- const char *expected_type = NULL;
- char *error = NULL;
- expected_type = zend_parse_arg_impl(arg_num, arg, va, spec, &error);
- if (expected_type) {
- if (EG(exception)) {
- return FAILURE;
- }
- if (!(flags & ZEND_PARSE_PARAMS_QUIET) && (*expected_type || error)) {
- if (error) {
- zend_argument_type_error(arg_num, "must be %s", error);
- efree(error);
- } else {
- zend_argument_type_error(arg_num, "must be of type %s, %s given", expected_type, zend_zval_type_name(arg));
- }
- } else if (error) {
- efree(error);
- }
- return FAILURE;
- }
- return SUCCESS;
- }
- /* }}} */
- ZEND_API int zend_parse_parameter(int flags, int arg_num, zval *arg, const char *spec, ...)
- {
- va_list va;
- int ret;
- va_start(va, spec);
- ret = zend_parse_arg(arg_num, arg, &va, &spec, flags);
- va_end(va);
- return ret;
- }
- static ZEND_COLD void zend_parse_parameters_debug_error(const char *msg) {
- zend_function *active_function = EG(current_execute_data)->func;
- const char *class_name = active_function->common.scope
- ? ZSTR_VAL(active_function->common.scope->name) : "";
- zend_error_noreturn(E_CORE_ERROR, "%s%s%s(): %s",
- class_name, class_name[0] ? "::" : "",
- ZSTR_VAL(active_function->common.function_name), msg);
- }
- static int zend_parse_va_args(int num_args, const char *type_spec, va_list *va, int flags) /* {{{ */
- {
- const char *spec_walk;
- int c, i;
- int min_num_args = -1;
- int max_num_args = 0;
- int post_varargs = 0;
- zval *arg;
- int arg_count;
- zend_bool have_varargs = 0;
- zval **varargs = NULL;
- int *n_varargs = NULL;
- for (spec_walk = type_spec; *spec_walk; spec_walk++) {
- c = *spec_walk;
- switch (c) {
- case 'l': case 'd':
- case 's': case 'b':
- case 'r': case 'a':
- case 'o': case 'O':
- case 'z': case 'Z':
- case 'C': case 'h':
- case 'f': case 'A':
- case 'H': case 'p':
- case 'S': case 'P':
- case 'L': case 'n':
- max_num_args++;
- break;
- case '|':
- min_num_args = max_num_args;
- break;
- case '/':
- case '!':
- /* Pass */
- break;
- case '*':
- case '+':
- if (have_varargs) {
- zend_parse_parameters_debug_error(
- "only one varargs specifier (* or +) is permitted");
- return FAILURE;
- }
- have_varargs = 1;
- /* we expect at least one parameter in varargs */
- if (c == '+') {
- max_num_args++;
- }
- /* mark the beginning of varargs */
- post_varargs = max_num_args;
- break;
- default:
- zend_parse_parameters_debug_error("bad type specifier while parsing parameters");
- return FAILURE;
- }
- }
- if (min_num_args < 0) {
- min_num_args = max_num_args;
- }
- if (have_varargs) {
- /* calculate how many required args are at the end of the specifier list */
- post_varargs = max_num_args - post_varargs;
- max_num_args = -1;
- }
- if (num_args < min_num_args || (num_args > max_num_args && max_num_args >= 0)) {
- if (!(flags & ZEND_PARSE_PARAMS_QUIET)) {
- zend_function *active_function = EG(current_execute_data)->func;
- const char *class_name = active_function->common.scope ? ZSTR_VAL(active_function->common.scope->name) : "";
- zend_argument_count_error("%s%s%s() expects %s %d parameter%s, %d given",
- class_name,
- class_name[0] ? "::" : "",
- ZSTR_VAL(active_function->common.function_name),
- min_num_args == max_num_args ? "exactly" : num_args < min_num_args ? "at least" : "at most",
- num_args < min_num_args ? min_num_args : max_num_args,
- (num_args < min_num_args ? min_num_args : max_num_args) == 1 ? "" : "s",
- num_args);
- }
- return FAILURE;
- }
- arg_count = ZEND_CALL_NUM_ARGS(EG(current_execute_data));
- if (num_args > arg_count) {
- zend_parse_parameters_debug_error("could not obtain parameters for parsing");
- return FAILURE;
- }
- i = 0;
- while (num_args-- > 0) {
- if (*type_spec == '|') {
- type_spec++;
- }
- if (*type_spec == '*' || *type_spec == '+') {
- int num_varargs = num_args + 1 - post_varargs;
- /* eat up the passed in storage even if it won't be filled in with varargs */
- varargs = va_arg(*va, zval **);
- n_varargs = va_arg(*va, int *);
- type_spec++;
- if (num_varargs > 0) {
- *n_varargs = num_varargs;
- *varargs = ZEND_CALL_ARG(EG(current_execute_data), i + 1);
- /* adjust how many args we have left and restart loop */
- num_args += 1 - num_varargs;
- i += num_varargs;
- continue;
- } else {
- *varargs = NULL;
- *n_varargs = 0;
- }
- }
- arg = ZEND_CALL_ARG(EG(current_execute_data), i + 1);
- if (zend_parse_arg(i+1, arg, va, &type_spec, flags) == FAILURE) {
- /* clean up varargs array if it was used */
- if (varargs && *varargs) {
- *varargs = NULL;
- }
- return FAILURE;
- }
- i++;
- }
- return SUCCESS;
- }
- /* }}} */
- ZEND_API int zend_parse_parameters_ex(int flags, int num_args, const char *type_spec, ...) /* {{{ */
- {
- va_list va;
- int retval;
- va_start(va, type_spec);
- retval = zend_parse_va_args(num_args, type_spec, &va, flags);
- va_end(va);
- return retval;
- }
- /* }}} */
- ZEND_API int zend_parse_parameters(int num_args, const char *type_spec, ...) /* {{{ */
- {
- va_list va;
- int retval;
- int flags = 0;
- va_start(va, type_spec);
- retval = zend_parse_va_args(num_args, type_spec, &va, flags);
- va_end(va);
- return retval;
- }
- /* }}} */
- ZEND_API int zend_parse_method_parameters(int num_args, zval *this_ptr, const char *type_spec, ...) /* {{{ */
- {
- va_list va;
- int retval;
- int flags = 0;
- const char *p = type_spec;
- zval **object;
- zend_class_entry *ce;
- /* Just checking this_ptr is not enough, because fcall_common_helper does not set
- * Z_OBJ(EG(This)) to NULL when calling an internal function with common.scope == NULL.
- * In that case EG(This) would still be the $this from the calling code and we'd take the
- * wrong branch here. */
- zend_bool is_method = EG(current_execute_data)->func->common.scope != NULL;
- if (!is_method || !this_ptr || Z_TYPE_P(this_ptr) != IS_OBJECT) {
- va_start(va, type_spec);
- retval = zend_parse_va_args(num_args, type_spec, &va, flags);
- va_end(va);
- } else {
- p++;
- va_start(va, type_spec);
- object = va_arg(va, zval **);
- ce = va_arg(va, zend_class_entry *);
- *object = this_ptr;
- if (ce && !instanceof_function(Z_OBJCE_P(this_ptr), ce)) {
- zend_error_noreturn(E_CORE_ERROR, "%s::%s() must be derived from %s::%s",
- ZSTR_VAL(Z_OBJCE_P(this_ptr)->name), get_active_function_name(), ZSTR_VAL(ce->name), get_active_function_name());
- }
- retval = zend_parse_va_args(num_args, p, &va, flags);
- va_end(va);
- }
- return retval;
- }
- /* }}} */
- ZEND_API int zend_parse_method_parameters_ex(int flags, int num_args, zval *this_ptr, const char *type_spec, ...) /* {{{ */
- {
- va_list va;
- int retval;
- const char *p = type_spec;
- zval **object;
- zend_class_entry *ce;
- if (!this_ptr) {
- va_start(va, type_spec);
- retval = zend_parse_va_args(num_args, type_spec, &va, flags);
- va_end(va);
- } else {
- p++;
- va_start(va, type_spec);
- object = va_arg(va, zval **);
- ce = va_arg(va, zend_class_entry *);
- *object = this_ptr;
- if (ce && !instanceof_function(Z_OBJCE_P(this_ptr), ce)) {
- if (!(flags & ZEND_PARSE_PARAMS_QUIET)) {
- zend_error_noreturn(E_CORE_ERROR, "%s::%s() must be derived from %s::%s",
- ZSTR_VAL(ce->name), get_active_function_name(), ZSTR_VAL(Z_OBJCE_P(this_ptr)->name), get_active_function_name());
- }
- va_end(va);
- return FAILURE;
- }
- retval = zend_parse_va_args(num_args, p, &va, flags);
- va_end(va);
- }
- return retval;
- }
- /* }}} */
- /* This function should be called after the constructor has been called
- * because it may call __set from the uninitialized object otherwise. */
- ZEND_API void zend_merge_properties(zval *obj, HashTable *properties) /* {{{ */
- {
- zend_object *zobj = Z_OBJ_P(obj);
- zend_object_write_property_t write_property = zobj->handlers->write_property;
- zend_class_entry *old_scope = EG(fake_scope);
- zend_string *key;
- zval *value;
- EG(fake_scope) = Z_OBJCE_P(obj);
- ZEND_HASH_FOREACH_STR_KEY_VAL(properties, key, value) {
- if (key) {
- write_property(zobj, key, value, NULL);
- }
- } ZEND_HASH_FOREACH_END();
- EG(fake_scope) = old_scope;
- }
- /* }}} */
- ZEND_API int zend_update_class_constants(zend_class_entry *class_type) /* {{{ */
- {
- if (!(class_type->ce_flags & ZEND_ACC_CONSTANTS_UPDATED)) {
- zend_class_constant *c;
- zval *val;
- zend_property_info *prop_info;
- if (class_type->parent) {
- if (UNEXPECTED(zend_update_class_constants(class_type->parent) != SUCCESS)) {
- return FAILURE;
- }
- }
- ZEND_HASH_FOREACH_PTR(&class_type->constants_table, c) {
- val = &c->value;
- if (Z_TYPE_P(val) == IS_CONSTANT_AST) {
- if (UNEXPECTED(zval_update_constant_ex(val, c->ce) != SUCCESS)) {
- return FAILURE;
- }
- }
- } ZEND_HASH_FOREACH_END();
- if (class_type->default_static_members_count && !CE_STATIC_MEMBERS(class_type)) {
- if (class_type->type == ZEND_INTERNAL_CLASS || (class_type->ce_flags & (ZEND_ACC_IMMUTABLE|ZEND_ACC_PRELOADED))) {
- zend_class_init_statics(class_type);
- }
- }
- ZEND_HASH_FOREACH_PTR(&class_type->properties_info, prop_info) {
- if (prop_info->flags & ZEND_ACC_STATIC) {
- val = CE_STATIC_MEMBERS(class_type) + prop_info->offset;
- } else {
- val = (zval*)((char*)class_type->default_properties_table + prop_info->offset - OBJ_PROP_TO_OFFSET(0));
- }
- if (Z_TYPE_P(val) == IS_CONSTANT_AST) {
- if (ZEND_TYPE_IS_SET(prop_info->type)) {
- zval tmp;
- ZVAL_COPY(&tmp, val);
- if (UNEXPECTED(zval_update_constant_ex(&tmp, prop_info->ce) != SUCCESS)) {
- zval_ptr_dtor(&tmp);
- return FAILURE;
- }
- /* property initializers must always be evaluated with strict types */;
- if (UNEXPECTED(!zend_verify_property_type(prop_info, &tmp, /* strict */ 1))) {
- zval_ptr_dtor(&tmp);
- return FAILURE;
- }
- zval_ptr_dtor(val);
- ZVAL_COPY_VALUE(val, &tmp);
- } else if (UNEXPECTED(zval_update_constant_ex(val, prop_info->ce) != SUCCESS)) {
- return FAILURE;
- }
- }
- } ZEND_HASH_FOREACH_END();
- class_type->ce_flags |= ZEND_ACC_CONSTANTS_UPDATED;
- }
- return SUCCESS;
- }
- /* }}} */
- static zend_always_inline void _object_properties_init(zend_object *object, zend_class_entry *class_type) /* {{{ */
- {
- if (class_type->default_properties_count) {
- zval *src = class_type->default_properties_table;
- zval *dst = object->properties_table;
- zval *end = src + class_type->default_properties_count;
- if (UNEXPECTED(class_type->type == ZEND_INTERNAL_CLASS)) {
- do {
- ZVAL_COPY_OR_DUP_PROP(dst, src);
- src++;
- dst++;
- } while (src != end);
- } else {
- do {
- ZVAL_COPY_PROP(dst, src);
- src++;
- dst++;
- } while (src != end);
- }
- }
- }
- /* }}} */
- ZEND_API void object_properties_init(zend_object *object, zend_class_entry *class_type) /* {{{ */
- {
- object->properties = NULL;
- _object_properties_init(object, class_type);
- }
- /* }}} */
- ZEND_API void object_properties_init_ex(zend_object *object, HashTable *properties) /* {{{ */
- {
- object->properties = properties;
- if (object->ce->default_properties_count) {
- zval *prop;
- zend_string *key;
- zend_property_info *property_info;
- ZEND_HASH_FOREACH_STR_KEY_VAL(properties, key, prop) {
- property_info = zend_get_property_info(object->ce, key, 1);
- if (property_info != ZEND_WRONG_PROPERTY_INFO &&
- property_info &&
- (property_info->flags & ZEND_ACC_STATIC) == 0) {
- zval *slot = OBJ_PROP(object, property_info->offset);
- if (UNEXPECTED(ZEND_TYPE_IS_SET(property_info->type))) {
- zval tmp;
- ZVAL_COPY_VALUE(&tmp, prop);
- if (UNEXPECTED(!zend_verify_property_type(property_info, &tmp, 0))) {
- continue;
- }
- ZVAL_COPY_VALUE(slot, &tmp);
- } else {
- ZVAL_COPY_VALUE(slot, prop);
- }
- ZVAL_INDIRECT(prop, slot);
- }
- } ZEND_HASH_FOREACH_END();
- }
- }
- /* }}} */
- ZEND_API void object_properties_load(zend_object *object, HashTable *properties) /* {{{ */
- {
- zval *prop, tmp;
- zend_string *key;
- zend_long h;
- zend_property_info *property_info;
- ZEND_HASH_FOREACH_KEY_VAL(properties, h, key, prop) {
- if (key) {
- if (ZSTR_VAL(key)[0] == '\0') {
- const char *class_name, *prop_name;
- size_t prop_name_len;
- if (zend_unmangle_property_name_ex(key, &class_name, &prop_name, &prop_name_len) == SUCCESS) {
- zend_string *pname = zend_string_init(prop_name, prop_name_len, 0);
- zend_class_entry *prev_scope = EG(fake_scope);
- if (class_name && class_name[0] != '*') {
- zend_string *cname = zend_string_init(class_name, strlen(class_name), 0);
- EG(fake_scope) = zend_lookup_class(cname);
- zend_string_release_ex(cname, 0);
- }
- property_info = zend_get_property_info(object->ce, pname, 1);
- zend_string_release_ex(pname, 0);
- EG(fake_scope) = prev_scope;
- } else {
- property_info = ZEND_WRONG_PROPERTY_INFO;
- }
- } else {
- property_info = zend_get_property_info(object->ce, key, 1);
- }
- if (property_info != ZEND_WRONG_PROPERTY_INFO &&
- property_info &&
- (property_info->flags & ZEND_ACC_STATIC) == 0) {
- zval *slot = OBJ_PROP(object, property_info->offset);
- zval_ptr_dtor(slot);
- ZVAL_COPY_VALUE(slot, prop);
- zval_add_ref(slot);
- if (object->properties) {
- ZVAL_INDIRECT(&tmp, slot);
- zend_hash_update(object->properties, key, &tmp);
- }
- } else {
- if (!object->properties) {
- rebuild_object_properties(object);
- }
- prop = zend_hash_update(object->properties, key, prop);
- zval_add_ref(prop);
- }
- } else {
- if (!object->properties) {
- rebuild_object_properties(object);
- }
- prop = zend_hash_index_update(object->properties, h, prop);
- zval_add_ref(prop);
- }
- } ZEND_HASH_FOREACH_END();
- }
- /* }}} */
- /* This function requires 'properties' to contain all props declared in the
- * class and all props being public. If only a subset is given or the class
- * has protected members then you need to merge the properties separately by
- * calling zend_merge_properties(). */
- static zend_always_inline int _object_and_properties_init(zval *arg, zend_class_entry *class_type, HashTable *properties) /* {{{ */
- {
- if (UNEXPECTED(class_type->ce_flags & (ZEND_ACC_INTERFACE|ZEND_ACC_TRAIT|ZEND_ACC_IMPLICIT_ABSTRACT_CLASS|ZEND_ACC_EXPLICIT_ABSTRACT_CLASS))) {
- if (class_type->ce_flags & ZEND_ACC_INTERFACE) {
- zend_throw_error(NULL, "Cannot instantiate interface %s", ZSTR_VAL(class_type->name));
- } else if (class_type->ce_flags & ZEND_ACC_TRAIT) {
- zend_throw_error(NULL, "Cannot instantiate trait %s", ZSTR_VAL(class_type->name));
- } else {
- zend_throw_error(NULL, "Cannot instantiate abstract class %s", ZSTR_VAL(class_type->name));
- }
- ZVAL_NULL(arg);
- Z_OBJ_P(arg) = NULL;
- return FAILURE;
- }
- if (UNEXPECTED(!(class_type->ce_flags & ZEND_ACC_CONSTANTS_UPDATED))) {
- if (UNEXPECTED(zend_update_class_constants(class_type) != SUCCESS)) {
- ZVAL_NULL(arg);
- Z_OBJ_P(arg) = NULL;
- return FAILURE;
- }
- }
- if (class_type->create_object == NULL) {
- zend_object *obj = zend_objects_new(class_type);
- ZVAL_OBJ(arg, obj);
- if (properties) {
- object_properties_init_ex(obj, properties);
- } else {
- _object_properties_init(obj, class_type);
- }
- } else {
- ZVAL_OBJ(arg, class_type->create_object(class_type));
- }
- return SUCCESS;
- }
- /* }}} */
- ZEND_API int object_and_properties_init(zval *arg, zend_class_entry *class_type, HashTable *properties) /* {{{ */
- {
- return _object_and_properties_init(arg, class_type, properties);
- }
- /* }}} */
- ZEND_API int object_init_ex(zval *arg, zend_class_entry *class_type) /* {{{ */
- {
- return _object_and_properties_init(arg, class_type, NULL);
- }
- /* }}} */
- ZEND_API int object_init(zval *arg) /* {{{ */
- {
- ZVAL_OBJ(arg, zend_objects_new(zend_standard_class_def));
- return SUCCESS;
- }
- /* }}} */
- ZEND_API int add_assoc_long_ex(zval *arg, const char *key, size_t key_len, zend_long n) /* {{{ */
- {
- zval tmp;
- ZVAL_LONG(&tmp, n);
- zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
- return SUCCESS;
- }
- /* }}} */
- ZEND_API int add_assoc_null_ex(zval *arg, const char *key, size_t key_len) /* {{{ */
- {
- zval tmp;
- ZVAL_NULL(&tmp);
- zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
- return SUCCESS;
- }
- /* }}} */
- ZEND_API int add_assoc_bool_ex(zval *arg, const char *key, size_t key_len, int b) /* {{{ */
- {
- zval tmp;
- ZVAL_BOOL(&tmp, b);
- zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
- return SUCCESS;
- }
- /* }}} */
- ZEND_API int add_assoc_resource_ex(zval *arg, const char *key, size_t key_len, zend_resource *r) /* {{{ */
- {
- zval tmp;
- ZVAL_RES(&tmp, r);
- zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
- return SUCCESS;
- }
- /* }}} */
- ZEND_API int add_assoc_double_ex(zval *arg, const char *key, size_t key_len, double d) /* {{{ */
- {
- zval tmp;
- ZVAL_DOUBLE(&tmp, d);
- zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
- return SUCCESS;
- }
- /* }}} */
- ZEND_API int add_assoc_str_ex(zval *arg, const char *key, size_t key_len, zend_string *str) /* {{{ */
- {
- zval tmp;
- ZVAL_STR(&tmp, str);
- zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
- return SUCCESS;
- }
- /* }}} */
- ZEND_API int add_assoc_string_ex(zval *arg, const char *key, size_t key_len, const char *str) /* {{{ */
- {
- zval tmp;
- ZVAL_STRING(&tmp, str);
- zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
- return SUCCESS;
- }
- /* }}} */
- ZEND_API int add_assoc_stringl_ex(zval *arg, const char *key, size_t key_len, const char *str, size_t length) /* {{{ */
- {
- zval tmp;
- ZVAL_STRINGL(&tmp, str, length);
- zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
- return SUCCESS;
- }
- /* }}} */
- ZEND_API int add_assoc_zval_ex(zval *arg, const char *key, size_t key_len, zval *value) /* {{{ */
- {
- zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, value);
- return SUCCESS;
- }
- /* }}} */
- ZEND_API int add_index_long(zval *arg, zend_ulong index, zend_long n) /* {{{ */
- {
- zval tmp;
- ZVAL_LONG(&tmp, n);
- zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp);
- return SUCCESS;
- }
- /* }}} */
- ZEND_API int add_index_null(zval *arg, zend_ulong index) /* {{{ */
- {
- zval tmp;
- ZVAL_NULL(&tmp);
- zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp);
- return SUCCESS;
- }
- /* }}} */
- ZEND_API int add_index_bool(zval *arg, zend_ulong index, int b) /* {{{ */
- {
- zval tmp;
- ZVAL_BOOL(&tmp, b);
- zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp);
- return SUCCESS;
- }
- /* }}} */
- ZEND_API int add_index_resource(zval *arg, zend_ulong index, zend_resource *r) /* {{{ */
- {
- zval tmp;
- ZVAL_RES(&tmp, r);
- zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp);
- return SUCCESS;
- }
- /* }}} */
- ZEND_API int add_index_double(zval *arg, zend_ulong index, double d) /* {{{ */
- {
- zval tmp;
- ZVAL_DOUBLE(&tmp, d);
- zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp);
- return SUCCESS;
- }
- /* }}} */
- ZEND_API int add_index_str(zval *arg, zend_ulong index, zend_string *str) /* {{{ */
- {
- zval tmp;
- ZVAL_STR(&tmp, str);
- zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp);
- return SUCCESS;
- }
- /* }}} */
- ZEND_API int add_index_string(zval *arg, zend_ulong index, const char *str) /* {{{ */
- {
- zval tmp;
- ZVAL_STRING(&tmp, str);
- zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp);
- return SUCCESS;
- }
- /* }}} */
- ZEND_API int add_index_stringl(zval *arg, zend_ulong index, const char *str, size_t length) /* {{{ */
- {
- zval tmp;
- ZVAL_STRINGL(&tmp, str, length);
- zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp);
- return SUCCESS;
- }
- /* }}} */
- ZEND_API int add_next_index_long(zval *arg, zend_long n) /* {{{ */
- {
- zval tmp;
- ZVAL_LONG(&tmp, n);
- return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE;
- }
- /* }}} */
- ZEND_API int add_next_index_null(zval *arg) /* {{{ */
- {
- zval tmp;
- ZVAL_NULL(&tmp);
- return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE;
- }
- /* }}} */
- ZEND_API int add_next_index_bool(zval *arg, int b) /* {{{ */
- {
- zval tmp;
- ZVAL_BOOL(&tmp, b);
- return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE;
- }
- /* }}} */
- ZEND_API int add_next_index_resource(zval *arg, zend_resource *r) /* {{{ */
- {
- zval tmp;
- ZVAL_RES(&tmp, r);
- return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE;
- }
- /* }}} */
- ZEND_API int add_next_index_double(zval *arg, double d) /* {{{ */
- {
- zval tmp;
- ZVAL_DOUBLE(&tmp, d);
- return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE;
- }
- /* }}} */
- ZEND_API int add_next_index_str(zval *arg, zend_string *str) /* {{{ */
- {
- zval tmp;
- ZVAL_STR(&tmp, str);
- return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE;
- }
- /* }}} */
- ZEND_API int add_next_index_string(zval *arg, const char *str) /* {{{ */
- {
- zval tmp;
- ZVAL_STRING(&tmp, str);
- return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE;
- }
- /* }}} */
- ZEND_API int add_next_index_stringl(zval *arg, const char *str, size_t length) /* {{{ */
- {
- zval tmp;
- ZVAL_STRINGL(&tmp, str, length);
- return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE;
- }
- /* }}} */
- ZEND_API int array_set_zval_key(HashTable *ht, zval *key, zval *value) /* {{{ */
- {
- zval *result;
- switch (Z_TYPE_P(key)) {
- case IS_STRING:
- result = zend_symtable_update(ht, Z_STR_P(key), value);
- break;
- case IS_NULL:
- result = zend_hash_update(ht, ZSTR_EMPTY_ALLOC(), value);
- break;
- case IS_RESOURCE:
- zend_error(E_WARNING, "Resource ID#%d used as offset, casting to integer (%d)", Z_RES_HANDLE_P(key), Z_RES_HANDLE_P(key));
- result = zend_hash_index_update(ht, Z_RES_HANDLE_P(key), value);
- break;
- case IS_FALSE:
- result = zend_hash_index_update(ht, 0, value);
- break;
- case IS_TRUE:
- result = zend_hash_index_update(ht, 1, value);
- break;
- case IS_LONG:
- result = zend_hash_index_update(ht, Z_LVAL_P(key), value);
- break;
- case IS_DOUBLE:
- result = zend_hash_index_update(ht, zend_dval_to_lval(Z_DVAL_P(key)), value);
- break;
- default:
- zend_type_error("Illegal offset type");
- result = NULL;
- }
- if (result) {
- Z_TRY_ADDREF_P(result);
- return SUCCESS;
- } else {
- return FAILURE;
- }
- }
- /* }}} */
- ZEND_API int add_property_long_ex(zval *arg, const char *key, size_t key_len, zend_long n) /* {{{ */
- {
- zval tmp;
- ZVAL_LONG(&tmp, n);
- return add_property_zval_ex(arg, key, key_len, &tmp);
- }
- /* }}} */
- ZEND_API int add_property_bool_ex(zval *arg, const char *key, size_t key_len, zend_long b) /* {{{ */
- {
- zval tmp;
- ZVAL_BOOL(&tmp, b);
- return add_property_zval_ex(arg, key, key_len, &tmp);
- }
- /* }}} */
- ZEND_API int add_property_null_ex(zval *arg, const char *key, size_t key_len) /* {{{ */
- {
- zval tmp;
- ZVAL_NULL(&tmp);
- return add_property_zval_ex(arg, key, key_len, &tmp);
- }
- /* }}} */
- ZEND_API int add_property_resource_ex(zval *arg, const char *key, size_t key_len, zend_resource *r) /* {{{ */
- {
- zval tmp;
- ZVAL_RES(&tmp, r);
- add_property_zval_ex(arg, key, key_len, &tmp);
- zval_ptr_dtor(&tmp); /* write_property will add 1 to refcount */
- return SUCCESS;
- }
- /* }}} */
- ZEND_API int add_property_double_ex(zval *arg, const char *key, size_t key_len, double d) /* {{{ */
- {
- zval tmp;
- ZVAL_DOUBLE(&tmp, d);
- return add_property_zval_ex(arg, key, key_len, &tmp);
- }
- /* }}} */
- ZEND_API int add_property_str_ex(zval *arg, const char *key, size_t key_len, zend_string *str) /* {{{ */
- {
- zval tmp;
- ZVAL_STR(&tmp, str);
- add_property_zval_ex(arg, key, key_len, &tmp);
- zval_ptr_dtor(&tmp); /* write_property will add 1 to refcount */
- return SUCCESS;
- }
- /* }}} */
- ZEND_API int add_property_string_ex(zval *arg, const char *key, size_t key_len, const char *str) /* {{{ */
- {
- zval tmp;
- ZVAL_STRING(&tmp, str);
- add_property_zval_ex(arg, key, key_len, &tmp);
- zval_ptr_dtor(&tmp); /* write_property will add 1 to refcount */
- return SUCCESS;
- }
- /* }}} */
- ZEND_API int add_property_stringl_ex(zval *arg, const char *key, size_t key_len, const char *str, size_t length) /* {{{ */
- {
- zval tmp;
- ZVAL_STRINGL(&tmp, str, length);
- add_property_zval_ex(arg, key, key_len, &tmp);
- zval_ptr_dtor(&tmp); /* write_property will add 1 to refcount */
- return SUCCESS;
- }
- /* }}} */
- ZEND_API int add_property_zval_ex(zval *arg, const char *key, size_t key_len, zval *value) /* {{{ */
- {
- zend_string *str;
- str = zend_string_init(key, key_len, 0);
- Z_OBJ_HANDLER_P(arg, write_property)(Z_OBJ_P(arg), str, value, NULL);
- zend_string_release_ex(str, 0);
- return SUCCESS;
- }
- /* }}} */
- ZEND_API int zend_startup_module_ex(zend_module_entry *module) /* {{{ */
- {
- size_t name_len;
- zend_string *lcname;
- if (module->module_started) {
- return SUCCESS;
- }
- module->module_started = 1;
- /* Check module dependencies */
- if (module->deps) {
- const zend_module_dep *dep = module->deps;
- while (dep->name) {
- if (dep->type == MODULE_DEP_REQUIRED) {
- zend_module_entry *req_mod;
- name_len = strlen(dep->name);
- lcname = zend_string_alloc(name_len, 0);
- zend_str_tolower_copy(ZSTR_VAL(lcname), dep->name, name_len);
- if ((req_mod = zend_hash_find_ptr(&module_registry, lcname)) == NULL || !req_mod->module_started) {
- zend_string_efree(lcname);
- /* TODO: Check version relationship */
- zend_error(E_CORE_WARNING, "Cannot load module '%s' because required module '%s' is not loaded", module->name, dep->name);
- module->module_started = 0;
- return FAILURE;
- }
- zend_string_efree(lcname);
- }
- ++dep;
- }
- }
- /* Initialize module globals */
- if (module->globals_size) {
- #ifdef ZTS
- ts_allocate_id(module->globals_id_ptr, module->globals_size, (ts_allocate_ctor) module->globals_ctor, (ts_allocate_dtor) module->globals_dtor);
- #else
- if (module->globals_ctor) {
- module->globals_ctor(module->globals_ptr);
- }
- #endif
- }
- if (module->module_startup_func) {
- EG(current_module) = module;
- if (module->module_startup_func(module->type, module->module_number)==FAILURE) {
- zend_error_noreturn(E_CORE_ERROR,"Unable to start %s module", module->name);
- EG(current_module) = NULL;
- return FAILURE;
- }
- EG(current_module) = NULL;
- }
- return SUCCESS;
- }
- /* }}} */
- static int zend_startup_module_zval(zval *zv) /* {{{ */
- {
- zend_module_entry *module = Z_PTR_P(zv);
- return (zend_startup_module_ex(module) == SUCCESS) ? ZEND_HASH_APPLY_KEEP : ZEND_HASH_APPLY_REMOVE;
- }
- /* }}} */
- static void zend_sort_modules(void *base, size_t count, size_t siz, compare_func_t compare, swap_func_t swp) /* {{{ */
- {
- Bucket *b1 = base;
- Bucket *b2;
- Bucket *end = b1 + count;
- Bucket tmp;
- zend_module_entry *m, *r;
- while (b1 < end) {
- try_again:
- m = (zend_module_entry*)Z_PTR(b1->val);
- if (!m->module_started && m->deps) {
- const zend_module_dep *dep = m->deps;
- while (dep->name) {
- if (dep->type == MODULE_DEP_REQUIRED || dep->type == MODULE_DEP_OPTIONAL) {
- b2 = b1 + 1;
- while (b2 < end) {
- r = (zend_module_entry*)Z_PTR(b2->val);
- if (strcasecmp(dep->name, r->name) == 0) {
- tmp = *b1;
- *b1 = *b2;
- *b2 = tmp;
- goto try_again;
- }
- b2++;
- }
- }
- dep++;
- }
- }
- b1++;
- }
- }
- /* }}} */
- ZEND_API void zend_collect_module_handlers(void) /* {{{ */
- {
- zend_module_entry *module;
- int startup_count = 0;
- int shutdown_count = 0;
- int post_deactivate_count = 0;
- zend_class_entry *ce;
- int class_count = 0;
- /* Collect extensions with request startup/shutdown handlers */
- ZEND_HASH_FOREACH_PTR(&module_registry, module) {
- if (module->request_startup_func) {
- startup_count++;
- }
- if (module->request_shutdown_func) {
- shutdown_count++;
- }
- if (module->post_deactivate_func) {
- post_deactivate_count++;
- }
- } ZEND_HASH_FOREACH_END();
- module_request_startup_handlers = (zend_module_entry**)malloc(
- sizeof(zend_module_entry*) *
- (startup_count + 1 +
- shutdown_count + 1 +
- post_deactivate_count + 1));
- module_request_startup_handlers[startup_count] = NULL;
- module_request_shutdown_handlers = module_request_startup_handlers + startup_count + 1;
- module_request_shutdown_handlers[shutdown_count] = NULL;
- module_post_deactivate_handlers = module_request_shutdown_handlers + shutdown_count + 1;
- module_post_deactivate_handlers[post_deactivate_count] = NULL;
- startup_count = 0;
- ZEND_HASH_FOREACH_PTR(&module_registry, module) {
- if (module->request_startup_func) {
- module_request_startup_handlers[startup_count++] = module;
- }
- if (module->request_shutdown_func) {
- module_request_shutdown_handlers[--shutdown_count] = module;
- }
- if (module->post_deactivate_func) {
- module_post_deactivate_handlers[--post_deactivate_count] = module;
- }
- } ZEND_HASH_FOREACH_END();
- /* Collect internal classes with static members */
- ZEND_HASH_FOREACH_PTR(CG(class_table), ce) {
- if (ce->type == ZEND_INTERNAL_CLASS &&
- ce->default_static_members_count > 0) {
- class_count++;
- }
- } ZEND_HASH_FOREACH_END();
- class_cleanup_handlers = (zend_class_entry**)malloc(
- sizeof(zend_class_entry*) *
- (class_count + 1));
- class_cleanup_handlers[class_count] = NULL;
- if (class_count) {
- ZEND_HASH_FOREACH_PTR(CG(class_table), ce) {
- if (ce->type == ZEND_INTERNAL_CLASS &&
- ce->default_static_members_count > 0) {
- class_cleanup_handlers[--class_count] = ce;
- }
- } ZEND_HASH_FOREACH_END();
- }
- }
- /* }}} */
- ZEND_API int zend_startup_modules(void) /* {{{ */
- {
- zend_hash_sort_ex(&module_registry, zend_sort_modules, NULL, 0);
- zend_hash_apply(&module_registry, zend_startup_module_zval);
- return SUCCESS;
- }
- /* }}} */
- ZEND_API void zend_destroy_modules(void) /* {{{ */
- {
- free(class_cleanup_handlers);
- free(module_request_startup_handlers);
- zend_hash_graceful_reverse_destroy(&module_registry);
- }
- /* }}} */
- ZEND_API zend_module_entry* zend_register_module_ex(zend_module_entry *module) /* {{{ */
- {
- size_t name_len;
- zend_string *lcname;
- zend_module_entry *module_ptr;
- if (!module) {
- return NULL;
- }
- #if 0
- zend_printf("%s: Registering module %d\n", module->name, module->module_number);
- #endif
- /* Check module dependencies */
- if (module->deps) {
- const zend_module_dep *dep = module->deps;
- while (dep->name) {
- if (dep->type == MODULE_DEP_CONFLICTS) {
- name_len = strlen(dep->name);
- lcname = zend_string_alloc(name_len, 0);
- zend_str_tolower_copy(ZSTR_VAL(lcname), dep->name, name_len);
- if (zend_hash_exists(&module_registry, lcname) || zend_get_extension(dep->name)) {
- zend_string_efree(lcname);
- /* TODO: Check version relatiā¦
Large files files are truncated, but you can click here to view the full file