diff options
Diffstat (limited to 'Zend/zend_constants.c')
-rw-r--r-- | Zend/zend_constants.c | 409 |
1 files changed, 214 insertions, 195 deletions
diff --git a/Zend/zend_constants.c b/Zend/zend_constants.c index 3feb02115a..4ca7ef4571 100644 --- a/Zend/zend_constants.c +++ b/Zend/zend_constants.c @@ -5,7 +5,7 @@ | Copyright (c) 1998-2015 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 | + | 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 | @@ -27,47 +27,67 @@ #include "zend_globals.h" #include "zend_API.h" -void free_zend_constant(zend_constant *c) +void free_zend_constant(zval *zv) { + zend_constant *c = Z_PTR_P(zv); + if (!(c->flags & CONST_PERSISTENT)) { zval_dtor(&c->value); + } else { + zval_internal_dtor(&c->value); + } + if (c->name) { + zend_string_release(c->name); } - str_free(c->name); + pefree(c, c->flags & CONST_PERSISTENT); } -void copy_zend_constant(zend_constant *c) +static void copy_zend_constant(zval *zv) { - c->name = str_strndup(c->name, c->name_len - 1); + zend_constant *c = Z_PTR_P(zv); + + Z_PTR_P(zv) = pemalloc(sizeof(zend_constant), c->flags & CONST_PERSISTENT); + memcpy(Z_PTR_P(zv), c, sizeof(zend_constant)); + + c = Z_PTR_P(zv); + c->name = zend_string_copy(c->name); if (!(c->flags & CONST_PERSISTENT)) { zval_copy_ctor(&c->value); + } else { + if (Z_TYPE(c->value) == IS_STRING) { + Z_STR(c->value) = zend_string_dup(Z_STR(c->value), 1); + } } } void zend_copy_constants(HashTable *target, HashTable *source) { - zend_constant tmp_constant; - - zend_hash_copy(target, source, (copy_ctor_func_t) copy_zend_constant, &tmp_constant, sizeof(zend_constant)); + zend_hash_copy(target, source, copy_zend_constant); } -static int clean_non_persistent_constant(const zend_constant *c TSRMLS_DC) +static int clean_non_persistent_constant(zval *zv) { + zend_constant *c = Z_PTR_P(zv); return (c->flags & CONST_PERSISTENT) ? ZEND_HASH_APPLY_STOP : ZEND_HASH_APPLY_REMOVE; } -static int clean_non_persistent_constant_full(const zend_constant *c TSRMLS_DC) +static int clean_non_persistent_constant_full(zval *zv) { + zend_constant *c = Z_PTR_P(zv); return (c->flags & CONST_PERSISTENT) ? 0 : 1; } -static int clean_module_constant(const zend_constant *c, int *module_number TSRMLS_DC) +static int clean_module_constant(zval *el, void *arg) { - if (c->module_number == *module_number) { + zend_constant *c = (zend_constant *)Z_PTR_P(el); + int module_number = *(int *)arg; + + if (c->module_number == module_number) { return 1; } else { return 0; @@ -75,25 +95,23 @@ static int clean_module_constant(const zend_constant *c, int *module_number TSRM } -void clean_module_constants(int module_number TSRMLS_DC) +void clean_module_constants(int module_number) { - zend_hash_apply_with_argument(EG(zend_constants), (apply_func_arg_t) clean_module_constant, (void *) &module_number TSRMLS_CC); + zend_hash_apply_with_argument(EG(zend_constants), clean_module_constant, (void *) &module_number); } -int zend_startup_constants(TSRMLS_D) +int zend_startup_constants(void) { EG(zend_constants) = (HashTable *) malloc(sizeof(HashTable)); - if (zend_hash_init(EG(zend_constants), 20, NULL, ZEND_CONSTANT_DTOR, 1)==FAILURE) { - return FAILURE; - } + zend_hash_init(EG(zend_constants), 128, NULL, ZEND_CONSTANT_DTOR, 1); return SUCCESS; } -void zend_register_standard_constants(TSRMLS_D) +void zend_register_standard_constants(void) { REGISTER_MAIN_LONG_CONSTANT("E_ERROR", E_ERROR, CONST_PERSISTENT | CONST_CS); REGISTER_MAIN_LONG_CONSTANT("E_RECOVERABLE_ERROR", E_RECOVERABLE_ERROR, CONST_PERSISTENT | CONST_CS); @@ -126,7 +144,7 @@ void zend_register_standard_constants(TSRMLS_D) } -int zend_shutdown_constants(TSRMLS_D) +int zend_shutdown_constants(void) { zend_hash_destroy(EG(zend_constants)); free(EG(zend_constants)); @@ -134,198 +152,207 @@ int zend_shutdown_constants(TSRMLS_D) } -void clean_non_persistent_constants(TSRMLS_D) +void clean_non_persistent_constants(void) { if (EG(full_tables_cleanup)) { - zend_hash_apply(EG(zend_constants), (apply_func_t) clean_non_persistent_constant_full TSRMLS_CC); + zend_hash_apply(EG(zend_constants), clean_non_persistent_constant_full); } else { - zend_hash_reverse_apply(EG(zend_constants), (apply_func_t) clean_non_persistent_constant TSRMLS_CC); + zend_hash_reverse_apply(EG(zend_constants), clean_non_persistent_constant); } } -ZEND_API void zend_register_null_constant(const char *name, uint name_len, int flags, int module_number TSRMLS_DC) +ZEND_API void zend_register_null_constant(const char *name, size_t name_len, int flags, int module_number) { zend_constant c; - + ZVAL_NULL(&c.value); c.flags = flags; - c.name = zend_strndup(name, name_len-1); - c.name_len = name_len; + c.name = zend_string_init(name, name_len, flags & CONST_PERSISTENT); c.module_number = module_number; - zend_register_constant(&c TSRMLS_CC); + zend_register_constant(&c); } -ZEND_API void zend_register_bool_constant(const char *name, uint name_len, zend_bool bval, int flags, int module_number TSRMLS_DC) +ZEND_API void zend_register_bool_constant(const char *name, size_t name_len, zend_bool bval, int flags, int module_number) { zend_constant c; - + ZVAL_BOOL(&c.value, bval); c.flags = flags; - c.name = zend_strndup(name, name_len-1); - c.name_len = name_len; + c.name = zend_string_init(name, name_len, flags & CONST_PERSISTENT); c.module_number = module_number; - zend_register_constant(&c TSRMLS_CC); + zend_register_constant(&c); } -ZEND_API void zend_register_long_constant(const char *name, uint name_len, long lval, int flags, int module_number TSRMLS_DC) +ZEND_API void zend_register_long_constant(const char *name, size_t name_len, zend_long lval, int flags, int module_number) { zend_constant c; - + ZVAL_LONG(&c.value, lval); c.flags = flags; - c.name = zend_strndup(name, name_len-1); - c.name_len = name_len; + c.name = zend_string_init(name, name_len, flags & CONST_PERSISTENT); c.module_number = module_number; - zend_register_constant(&c TSRMLS_CC); + zend_register_constant(&c); } -ZEND_API void zend_register_double_constant(const char *name, uint name_len, double dval, int flags, int module_number TSRMLS_DC) +ZEND_API void zend_register_double_constant(const char *name, size_t name_len, double dval, int flags, int module_number) { zend_constant c; - + ZVAL_DOUBLE(&c.value, dval); c.flags = flags; - c.name = zend_strndup(name, name_len-1); - c.name_len = name_len; + c.name = zend_string_init(name, name_len, flags & CONST_PERSISTENT); c.module_number = module_number; - zend_register_constant(&c TSRMLS_CC); + zend_register_constant(&c); } -ZEND_API void zend_register_stringl_constant(const char *name, uint name_len, char *strval, uint strlen, int flags, int module_number TSRMLS_DC) +ZEND_API void zend_register_stringl_constant(const char *name, size_t name_len, char *strval, size_t strlen, int flags, int module_number) { zend_constant c; - - ZVAL_STRINGL(&c.value, strval, strlen, 0); + + ZVAL_NEW_STR(&c.value, zend_string_init(strval, strlen, flags & CONST_PERSISTENT)); c.flags = flags; - c.name = zend_strndup(name, name_len-1); - c.name_len = name_len; + c.name = zend_string_init(name, name_len, flags & CONST_PERSISTENT); c.module_number = module_number; - zend_register_constant(&c TSRMLS_CC); + zend_register_constant(&c); } -ZEND_API void zend_register_string_constant(const char *name, uint name_len, char *strval, int flags, int module_number TSRMLS_DC) +ZEND_API void zend_register_string_constant(const char *name, size_t name_len, char *strval, int flags, int module_number) { - zend_register_stringl_constant(name, name_len, strval, strlen(strval), flags, module_number TSRMLS_CC); + zend_register_stringl_constant(name, name_len, strval, strlen(strval), flags, module_number); } -static int zend_get_special_constant(const char *name, uint name_len, zend_constant **c TSRMLS_DC) +static zend_constant *zend_get_special_constant(const char *name, size_t name_len) { - int ret; + zend_constant *c; static char haltoff[] = "__COMPILER_HALT_OFFSET__"; - if (!EG(in_execution)) { - return 0; + if (!EG(current_execute_data)) { + return NULL; } else if (name_len == sizeof("__CLASS__")-1 && !memcmp(name, "__CLASS__", sizeof("__CLASS__")-1)) { - zend_constant tmp; /* Returned constants may be cached, so they have to be stored */ if (EG(scope) && EG(scope)->name) { - int const_name_len; - char *const_name; - ALLOCA_FLAG(use_heap) - - const_name_len = sizeof("\0__CLASS__") + EG(scope)->name_length; - const_name = do_alloca(const_name_len, use_heap); - memcpy(const_name, "\0__CLASS__", sizeof("\0__CLASS__")-1); - zend_str_tolower_copy(const_name + sizeof("\0__CLASS__")-1, EG(scope)->name, EG(scope)->name_length); - if (zend_hash_find(EG(zend_constants), const_name, const_name_len, (void**)c) == FAILURE) { - zend_hash_add(EG(zend_constants), const_name, const_name_len, (void*)&tmp, sizeof(zend_constant), (void**)c); - memset(*c, 0, sizeof(zend_constant)); - Z_STRVAL((**c).value) = estrndup(EG(scope)->name, EG(scope)->name_length); - Z_STRLEN((**c).value) = EG(scope)->name_length; - Z_TYPE((**c).value) = IS_STRING; + size_t const_name_len; + zend_string *const_name; + + const_name_len = sizeof("\0__CLASS__") + EG(scope)->name->len; + const_name = zend_string_alloc(const_name_len, 0); + memcpy(const_name->val, "\0__CLASS__", sizeof("\0__CLASS__")-1); + zend_str_tolower_copy(const_name->val + sizeof("\0__CLASS__")-1, EG(scope)->name->val, EG(scope)->name->len); + if ((c = zend_hash_find_ptr(EG(zend_constants), const_name)) == NULL) { + c = emalloc(sizeof(zend_constant)); + memset(c, 0, sizeof(zend_constant)); + ZVAL_STR_COPY(&c->value, EG(scope)->name); + zend_hash_add_ptr(EG(zend_constants), const_name, c); } - free_alloca(const_name, use_heap); + zend_string_release(const_name); } else { - if (zend_hash_find(EG(zend_constants), "\0__CLASS__", sizeof("\0__CLASS__"), (void**)c) == FAILURE) { - zend_hash_add(EG(zend_constants), "\0__CLASS__", sizeof("\0__CLASS__"), (void*)&tmp, sizeof(zend_constant), (void**)c); - memset(*c, 0, sizeof(zend_constant)); - Z_STRVAL((**c).value) = estrndup("", 0); - Z_STRLEN((**c).value) = 0; - Z_TYPE((**c).value) = IS_STRING; + zend_string *const_name = zend_string_init("\0__CLASS__", sizeof("\0__CLASS__")-1, 0); + if ((c = zend_hash_find_ptr(EG(zend_constants), const_name)) == NULL) { + c = emalloc(sizeof(zend_constant)); + memset(c, 0, sizeof(zend_constant)); + ZVAL_EMPTY_STRING(&c->value); + zend_hash_add_ptr(EG(zend_constants), const_name, c); } + zend_string_release(const_name); } - return 1; + return c; } else if (name_len == sizeof("__COMPILER_HALT_OFFSET__")-1 && !memcmp(name, "__COMPILER_HALT_OFFSET__", sizeof("__COMPILER_HALT_OFFSET__")-1)) { const char *cfilename; - char *haltname; - int len, clen; + zend_string *haltname; + size_t clen; - cfilename = zend_get_executed_filename(TSRMLS_C); + cfilename = zend_get_executed_filename(); clen = strlen(cfilename); /* check for __COMPILER_HALT_OFFSET__ */ - zend_mangle_property_name(&haltname, &len, haltoff, + haltname = zend_mangle_property_name(haltoff, sizeof("__COMPILER_HALT_OFFSET__") - 1, cfilename, clen, 0); - ret = zend_hash_find(EG(zend_constants), haltname, len+1, (void **) c); - efree(haltname); - return (ret == SUCCESS); + c = zend_hash_find_ptr(EG(zend_constants), haltname); + zend_string_free(haltname); + return c; } else { - return 0; + return NULL; } } -ZEND_API int zend_get_constant(const char *name, uint name_len, zval *result TSRMLS_DC) +ZEND_API zval *zend_get_constant_str(const char *name, size_t name_len) { zend_constant *c; - int retval = 1; - char *lookup_name; - - if (zend_hash_find(EG(zend_constants), name, name_len+1, (void **) &c) == FAILURE) { - lookup_name = zend_str_tolower_dup(name, name_len); + ALLOCA_FLAG(use_heap) - if (zend_hash_find(EG(zend_constants), lookup_name, name_len+1, (void **) &c)==SUCCESS) { + if ((c = zend_hash_str_find_ptr(EG(zend_constants), name, name_len)) == NULL) { + char *lcname = do_alloca(name_len + 1, use_heap); + zend_str_tolower_copy(lcname, name, name_len); + if ((c = zend_hash_str_find_ptr(EG(zend_constants), lcname, name_len)) != NULL) { if (c->flags & CONST_CS) { - retval=0; + c = NULL; } } else { - retval = zend_get_special_constant(name, name_len, &c TSRMLS_CC); + c = zend_get_special_constant(name, name_len); } - efree(lookup_name); + free_alloca(lcname, use_heap); } - if (retval) { - *result = c->value; - zval_copy_ctor(result); - Z_SET_REFCOUNT_P(result, 1); - Z_UNSET_ISREF_P(result); + return c ? &c->value : NULL; +} + +ZEND_API zval *zend_get_constant(zend_string *name) +{ + zend_constant *c; + ALLOCA_FLAG(use_heap) + + if ((c = zend_hash_find_ptr(EG(zend_constants), name)) == NULL) { + char *lcname = do_alloca(name->len + 1, use_heap); + zend_str_tolower_copy(lcname, name->val, name->len); + if ((c = zend_hash_str_find_ptr(EG(zend_constants), lcname, name->len)) != NULL) { + if (c->flags & CONST_CS) { + c = NULL; + } + } else { + c = zend_get_special_constant(name->val, name->len); + } + free_alloca(lcname, use_heap); } - return retval; + return c ? &c->value : NULL; } -ZEND_API int zend_get_constant_ex(const char *name, uint name_len, zval *result, zend_class_entry *scope, ulong flags TSRMLS_DC) +ZEND_API zval *zend_get_constant_ex(zend_string *cname, zend_class_entry *scope, zend_ulong flags) { zend_constant *c; - int retval = 1; const char *colon; zend_class_entry *ce = NULL; - char *class_name; - zval **ret_constant; + zend_string *class_name; + const char *name = cname->val; + size_t name_len = cname->len; /* Skip leading \\ */ if (name[0] == '\\') { name += 1; name_len -= 1; + cname = NULL; } - if ((colon = zend_memrchr(name, ':', name_len)) && colon > name && (*(colon - 1) == ':')) { int class_name_len = colon - name - 1; - int const_name_len = name_len - class_name_len - 2; - const char *constant_name = colon + 1; + size_t const_name_len = name_len - class_name_len - 2; + zend_string *constant_name = zend_string_init(colon + 1, const_name_len, 0); char *lcname; + zval *ret_constant = NULL; + ALLOCA_FLAG(use_heap) - class_name = estrndup(name, class_name_len); - lcname = zend_str_tolower_dup(class_name, class_name_len); + class_name = zend_string_init(name, class_name_len, 0); + lcname = do_alloca(class_name_len + 1, use_heap); + zend_str_tolower_copy(lcname, name, class_name_len); if (!scope) { - if (EG(in_execution)) { + if (EG(current_execute_data)) { scope = EG(scope); } else { scope = CG(active_class_entry); @@ -338,9 +365,7 @@ ZEND_API int zend_get_constant_ex(const char *name, uint name_len, zval *result, ce = scope; } else { zend_error(E_ERROR, "Cannot access self:: when no class scope is active"); - retval = 0; } - efree(lcname); } else if (class_name_len == sizeof("parent")-1 && !memcmp(lcname, "parent", sizeof("parent")-1)) { if (!scope) { @@ -350,172 +375,166 @@ ZEND_API int zend_get_constant_ex(const char *name, uint name_len, zval *result, } else { ce = scope->parent; } - efree(lcname); } else if (class_name_len == sizeof("static")-1 && !memcmp(lcname, "static", sizeof("static")-1)) { - if (EG(called_scope)) { - ce = EG(called_scope); + if (EG(current_execute_data) && EG(current_execute_data)->called_scope) { + ce = EG(current_execute_data)->called_scope; } else { zend_error(E_ERROR, "Cannot access static:: when no class scope is active"); } - efree(lcname); } else { - efree(lcname); - ce = zend_fetch_class(class_name, class_name_len, flags TSRMLS_CC); + ce = zend_fetch_class(class_name, flags); } - if (retval && ce) { - if (zend_hash_find(&ce->constants_table, constant_name, const_name_len+1, (void **) &ret_constant) != SUCCESS) { - retval = 0; + free_alloca(lcname, use_heap); + if (ce) { + ret_constant = zend_hash_find(&ce->constants_table, constant_name); + if (ret_constant == NULL) { if ((flags & ZEND_FETCH_CLASS_SILENT) == 0) { - zend_error(E_ERROR, "Undefined class constant '%s::%s'", class_name, constant_name); + zend_error(E_ERROR, "Undefined class constant '%s::%s'", class_name->val, constant_name->val); } + } else if (Z_ISREF_P(ret_constant)) { + ret_constant = Z_REFVAL_P(ret_constant); } - } else if (!ce) { - retval = 0; } - efree(class_name); - goto finish; + zend_string_release(class_name); + zend_string_free(constant_name); + if (ret_constant && Z_CONSTANT_P(ret_constant)) { + zval_update_constant_ex(ret_constant, 1, ce); + } + return ret_constant; } /* non-class constant */ if ((colon = zend_memrchr(name, '\\', name_len)) != NULL) { /* compound constant name */ int prefix_len = colon - name; - int const_name_len = name_len - prefix_len - 1; + size_t const_name_len = name_len - prefix_len - 1; const char *constant_name = colon + 1; char *lcname; - int found_const = 0; + size_t lcname_len; + ALLOCA_FLAG(use_heap) - lcname = zend_str_tolower_dup(name, prefix_len); + lcname_len = prefix_len + 1 + const_name_len; + lcname = do_alloca(lcname_len + 1, use_heap); + zend_str_tolower_copy(lcname, name, prefix_len); /* Check for namespace constant */ - /* Concatenate lowercase namespace name and constant name */ - lcname = erealloc(lcname, prefix_len + 1 + const_name_len + 1); lcname[prefix_len] = '\\'; memcpy(lcname + prefix_len + 1, constant_name, const_name_len + 1); - if (zend_hash_find(EG(zend_constants), lcname, prefix_len + 1 + const_name_len + 1, (void **) &c) == SUCCESS) { - found_const = 1; - } else { + if ((c = zend_hash_str_find_ptr(EG(zend_constants), lcname, lcname_len)) == NULL) { /* try lowercase */ zend_str_tolower(lcname + prefix_len + 1, const_name_len); - if (zend_hash_find(EG(zend_constants), lcname, prefix_len + 1 + const_name_len + 1, (void **) &c) == SUCCESS) { - if ((c->flags & CONST_CS) == 0) { - found_const = 1; + if ((c = zend_hash_str_find_ptr(EG(zend_constants), lcname, lcname_len)) != NULL) { + if ((c->flags & CONST_CS) != 0) { + c = NULL; } } } - efree(lcname); - if(found_const) { - *result = c->value; - zval_update_constant_ex(&result, 1, NULL TSRMLS_CC); - zval_copy_ctor(result); - Z_SET_REFCOUNT_P(result, 1); - Z_UNSET_ISREF_P(result); - return 1; + free_alloca(lcname, use_heap); + if (c) { + return &c->value; } /* name requires runtime resolution, need to check non-namespaced name */ if ((flags & IS_CONSTANT_UNQUALIFIED) != 0) { - name = constant_name; - name_len = const_name_len; - return zend_get_constant(name, name_len, result TSRMLS_CC); - } - retval = 0; -finish: - if (retval) { - zval_update_constant_ex(ret_constant, 1, ce TSRMLS_CC); - *result = **ret_constant; - zval_copy_ctor(result); - INIT_PZVAL(result); + return zend_get_constant_str(constant_name, const_name_len); } - - return retval; + return NULL; } - return zend_get_constant(name, name_len, result TSRMLS_CC); + if (cname) { + return zend_get_constant(cname); + } else { + return zend_get_constant_str(name, name_len); + } } -zend_constant *zend_quick_get_constant(const zend_literal *key, ulong flags TSRMLS_DC) +zend_constant *zend_quick_get_constant(const zval *key, zend_ulong flags) { zend_constant *c; - if (zend_hash_quick_find(EG(zend_constants), Z_STRVAL(key->constant), Z_STRLEN(key->constant) + 1, key->hash_value, (void **) &c) == FAILURE) { + if ((c = zend_hash_find_ptr(EG(zend_constants), Z_STR_P(key))) == NULL) { key++; - if (zend_hash_quick_find(EG(zend_constants), Z_STRVAL(key->constant), Z_STRLEN(key->constant) + 1, key->hash_value, (void **) &c) == FAILURE || + if ((c = zend_hash_find_ptr(EG(zend_constants), Z_STR_P(key))) == NULL || (c->flags & CONST_CS) != 0) { if ((flags & (IS_CONSTANT_IN_NAMESPACE|IS_CONSTANT_UNQUALIFIED)) == (IS_CONSTANT_IN_NAMESPACE|IS_CONSTANT_UNQUALIFIED)) { key++; - if (zend_hash_quick_find(EG(zend_constants), Z_STRVAL(key->constant), Z_STRLEN(key->constant) + 1, key->hash_value, (void **) &c) == FAILURE) { + if ((c = zend_hash_find_ptr(EG(zend_constants), Z_STR_P(key))) == NULL) { key++; - if (zend_hash_quick_find(EG(zend_constants), Z_STRVAL(key->constant), Z_STRLEN(key->constant) + 1, key->hash_value, (void **) &c) == FAILURE || + if ((c = zend_hash_find_ptr(EG(zend_constants), Z_STR_P(key))) == NULL || (c->flags & CONST_CS) != 0) { key--; - if (!zend_get_special_constant(Z_STRVAL(key->constant), Z_STRLEN(key->constant), &c TSRMLS_CC)) { - return NULL; - } + c = zend_get_special_constant(Z_STRVAL_P(key), Z_STRLEN_P(key)); } } } else { key--; - if (!zend_get_special_constant(Z_STRVAL(key->constant), Z_STRLEN(key->constant), &c TSRMLS_CC)) { - return NULL; - } + c = zend_get_special_constant(Z_STRVAL_P(key), Z_STRLEN_P(key)); } } } return c; } -ZEND_API int zend_register_constant(zend_constant *c TSRMLS_DC) +static void* zend_hash_add_constant(HashTable *ht, zend_string *key, zend_constant *c) { - char *lowercase_name = NULL; - char *name; + void *ret; + zend_constant *copy = pemalloc(sizeof(zend_constant), c->flags & CONST_PERSISTENT); + + memcpy(copy, c, sizeof(zend_constant)); + ret = zend_hash_add_ptr(ht, key, copy); + if (!ret) { + pefree(copy, c->flags & CONST_PERSISTENT); + } + return ret; +} + +ZEND_API int zend_register_constant(zend_constant *c) +{ + zend_string *lowercase_name = NULL; + zend_string *name; int ret = SUCCESS; - ulong chash; #if 0 printf("Registering constant for module %d\n", c->module_number); #endif if (!(c->flags & CONST_CS)) { - /* keep in mind that c->name_len already contains the '\0' */ - lowercase_name = estrndup(c->name, c->name_len-1); - zend_str_tolower(lowercase_name, c->name_len-1); - lowercase_name = (char*)zend_new_interned_string(lowercase_name, c->name_len, 1 TSRMLS_CC); + lowercase_name = zend_string_alloc(c->name->len, c->flags & CONST_PERSISTENT); + zend_str_tolower_copy(lowercase_name->val, c->name->val, c->name->len); + lowercase_name = zend_new_interned_string(lowercase_name); name = lowercase_name; } else { - char *slash = strrchr(c->name, '\\'); + char *slash = strrchr(c->name->val, '\\'); if (slash) { - lowercase_name = estrndup(c->name, c->name_len-1); - zend_str_tolower(lowercase_name, slash-c->name); - lowercase_name = (char*)zend_new_interned_string(lowercase_name, c->name_len, 1 TSRMLS_CC); + lowercase_name = zend_string_init(c->name->val, c->name->len, c->flags & CONST_PERSISTENT); + zend_str_tolower(lowercase_name->val, slash - c->name->val); + lowercase_name = zend_new_interned_string(lowercase_name); name = lowercase_name; } else { name = c->name; } } - chash = str_hash(name, c->name_len-1); /* Check if the user is trying to define the internal pseudo constant name __COMPILER_HALT_OFFSET__ */ - if ((c->name_len == sizeof("__COMPILER_HALT_OFFSET__") - && !memcmp(name, "__COMPILER_HALT_OFFSET__", sizeof("__COMPILER_HALT_OFFSET__")-1)) - || zend_hash_quick_add(EG(zend_constants), name, c->name_len, chash, (void *) c, sizeof(zend_constant), NULL)==FAILURE) { - + if ((c->name->len == sizeof("__COMPILER_HALT_OFFSET__")-1 + && !memcmp(name->val, "__COMPILER_HALT_OFFSET__", sizeof("__COMPILER_HALT_OFFSET__")-1)) + || zend_hash_add_constant(EG(zend_constants), name, c) == NULL) { + /* The internal __COMPILER_HALT_OFFSET__ is prefixed by NULL byte */ - if (c->name[0] == '\0' && c->name_len > sizeof("\0__COMPILER_HALT_OFFSET__") - && memcmp(name, "\0__COMPILER_HALT_OFFSET__", sizeof("\0__COMPILER_HALT_OFFSET__")) == 0) { - name++; + if (c->name->val[0] == '\0' && c->name->len > sizeof("\0__COMPILER_HALT_OFFSET__")-1 + && memcmp(name->val, "\0__COMPILER_HALT_OFFSET__", sizeof("\0__COMPILER_HALT_OFFSET__")) == 0) { } - zend_error(E_NOTICE,"Constant %s already defined", name); - str_free(c->name); + zend_error(E_NOTICE,"Constant %s already defined", name->val); + zend_string_release(c->name); if (!(c->flags & CONST_PERSISTENT)) { zval_dtor(&c->value); } ret = FAILURE; } if (lowercase_name) { - str_efree(lowercase_name); + zend_string_release(lowercase_name); } return ret; } |