PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LCOV - code coverage report
Current view: top level - Zend - zend_constants.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 232 283 82.0 %
Date: 2016-05-03 Functions: 18 25 72.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    +----------------------------------------------------------------------+
       3             :    | Zend Engine                                                          |
       4             :    +----------------------------------------------------------------------+
       5             :    | Copyright (c) 1998-2016 Zend Technologies Ltd. (http://www.zend.com) |
       6             :    +----------------------------------------------------------------------+
       7             :    | This source file is subject to version 2.00 of the Zend license,     |
       8             :    | that is bundled with this package in the file LICENSE, and is        |
       9             :    | available through the world-wide-web at the following url:           |
      10             :    | http://www.zend.com/license/2_00.txt.                                |
      11             :    | If you did not receive a copy of the Zend license and are unable to  |
      12             :    | obtain it through the world-wide-web, please send a note to          |
      13             :    | license@zend.com so we can mail you a copy immediately.              |
      14             :    +----------------------------------------------------------------------+
      15             :    | Authors: Andi Gutmans <andi@zend.com>                                |
      16             :    |          Zeev Suraski <zeev@zend.com>                                |
      17             :    +----------------------------------------------------------------------+
      18             : */
      19             : 
      20             : /* $Id$ */
      21             : 
      22             : #include "zend.h"
      23             : #include "zend_constants.h"
      24             : #include "zend_exceptions.h"
      25             : #include "zend_execute.h"
      26             : #include "zend_variables.h"
      27             : #include "zend_operators.h"
      28             : #include "zend_globals.h"
      29             : #include "zend_API.h"
      30             : 
      31    57465484 : void free_zend_constant(zval *zv)
      32             : {
      33    57465484 :         zend_constant *c = Z_PTR_P(zv);
      34             : 
      35    57465484 :         if (!(c->flags & CONST_PERSISTENT)) {
      36       70509 :                 zval_dtor(&c->value);
      37             :         } else {
      38    57394975 :                 zval_internal_dtor(&c->value);
      39             :         }
      40    57465484 :         if (c->name) {
      41    57465484 :                 zend_string_release(c->name);
      42             :         }
      43    57465484 :         pefree(c, c->flags & CONST_PERSISTENT);
      44    57465484 : }
      45             : 
      46             : 
      47           0 : static void copy_zend_constant(zval *zv)
      48             : {
      49           0 :         zend_constant *c = Z_PTR_P(zv);
      50             : 
      51           0 :         Z_PTR_P(zv) = pemalloc(sizeof(zend_constant), c->flags & CONST_PERSISTENT);
      52           0 :         memcpy(Z_PTR_P(zv), c, sizeof(zend_constant));
      53             : 
      54           0 :         c = Z_PTR_P(zv);
      55           0 :         c->name = zend_string_copy(c->name);
      56           0 :         if (!(c->flags & CONST_PERSISTENT)) {
      57           0 :                 zval_copy_ctor(&c->value);
      58             :         } else {
      59           0 :                 if (Z_TYPE(c->value) == IS_STRING) {
      60           0 :                         Z_STR(c->value) = zend_string_dup(Z_STR(c->value), 1);
      61             :                 }
      62             :         }
      63           0 : }
      64             : 
      65             : 
      66           0 : void zend_copy_constants(HashTable *target, HashTable *source)
      67             : {
      68           0 :         zend_hash_copy(target, source, copy_zend_constant);
      69           0 : }
      70             : 
      71             : 
      72       93088 : static int clean_non_persistent_constant(zval *zv)
      73             : {
      74       93088 :         zend_constant *c = Z_PTR_P(zv);
      75       93088 :         return (c->flags & CONST_PERSISTENT) ? ZEND_HASH_APPLY_STOP : ZEND_HASH_APPLY_REMOVE;
      76             : }
      77             : 
      78             : 
      79           0 : static int clean_non_persistent_constant_full(zval *zv)
      80             : {
      81           0 :         zend_constant *c = Z_PTR_P(zv);
      82           0 :         return (c->flags & CONST_PERSISTENT) ? 0 : 1;
      83             : }
      84             : 
      85             : 
      86           0 : static int clean_module_constant(zval *el, void *arg)
      87             : {
      88           0 :         zend_constant *c = (zend_constant *)Z_PTR_P(el);
      89           0 :         int module_number = *(int *)arg;
      90             : 
      91           0 :         if (c->module_number == module_number) {
      92           0 :                 return 1;
      93             :         } else {
      94           0 :                 return 0;
      95             :         }
      96             : }
      97             : 
      98             : 
      99           0 : void clean_module_constants(int module_number)
     100             : {
     101           0 :         zend_hash_apply_with_argument(EG(zend_constants), clean_module_constant, (void *) &module_number);
     102           0 : }
     103             : 
     104             : 
     105           0 : int zend_startup_constants(void)
     106             : {
     107           0 :         EG(zend_constants) = (HashTable *) malloc(sizeof(HashTable));
     108             : 
     109           0 :         zend_hash_init(EG(zend_constants), 128, NULL, ZEND_CONSTANT_DTOR, 1);
     110           0 :         return SUCCESS;
     111             : }
     112             : 
     113             : 
     114             : 
     115       22587 : void zend_register_standard_constants(void)
     116             : {
     117       22587 :         REGISTER_MAIN_LONG_CONSTANT("E_ERROR", E_ERROR, CONST_PERSISTENT | CONST_CS);
     118       22587 :         REGISTER_MAIN_LONG_CONSTANT("E_RECOVERABLE_ERROR", E_RECOVERABLE_ERROR, CONST_PERSISTENT | CONST_CS);
     119       22587 :         REGISTER_MAIN_LONG_CONSTANT("E_WARNING", E_WARNING, CONST_PERSISTENT | CONST_CS);
     120       22587 :         REGISTER_MAIN_LONG_CONSTANT("E_PARSE", E_PARSE, CONST_PERSISTENT | CONST_CS);
     121       22587 :         REGISTER_MAIN_LONG_CONSTANT("E_NOTICE", E_NOTICE, CONST_PERSISTENT | CONST_CS);
     122       22587 :         REGISTER_MAIN_LONG_CONSTANT("E_STRICT", E_STRICT, CONST_PERSISTENT | CONST_CS);
     123       22587 :         REGISTER_MAIN_LONG_CONSTANT("E_DEPRECATED", E_DEPRECATED, CONST_PERSISTENT | CONST_CS);
     124       22587 :         REGISTER_MAIN_LONG_CONSTANT("E_CORE_ERROR", E_CORE_ERROR, CONST_PERSISTENT | CONST_CS);
     125       22587 :         REGISTER_MAIN_LONG_CONSTANT("E_CORE_WARNING", E_CORE_WARNING, CONST_PERSISTENT | CONST_CS);
     126       22587 :         REGISTER_MAIN_LONG_CONSTANT("E_COMPILE_ERROR", E_COMPILE_ERROR, CONST_PERSISTENT | CONST_CS);
     127       22587 :         REGISTER_MAIN_LONG_CONSTANT("E_COMPILE_WARNING", E_COMPILE_WARNING, CONST_PERSISTENT | CONST_CS);
     128       22587 :         REGISTER_MAIN_LONG_CONSTANT("E_USER_ERROR", E_USER_ERROR, CONST_PERSISTENT | CONST_CS);
     129       22587 :         REGISTER_MAIN_LONG_CONSTANT("E_USER_WARNING", E_USER_WARNING, CONST_PERSISTENT | CONST_CS);
     130       22587 :         REGISTER_MAIN_LONG_CONSTANT("E_USER_NOTICE", E_USER_NOTICE, CONST_PERSISTENT | CONST_CS);
     131       22587 :         REGISTER_MAIN_LONG_CONSTANT("E_USER_DEPRECATED", E_USER_DEPRECATED, CONST_PERSISTENT | CONST_CS);
     132             : 
     133       22587 :         REGISTER_MAIN_LONG_CONSTANT("E_ALL", E_ALL, CONST_PERSISTENT | CONST_CS);
     134             : 
     135       22587 :         REGISTER_MAIN_LONG_CONSTANT("DEBUG_BACKTRACE_PROVIDE_OBJECT", DEBUG_BACKTRACE_PROVIDE_OBJECT, CONST_PERSISTENT | CONST_CS);
     136       22587 :         REGISTER_MAIN_LONG_CONSTANT("DEBUG_BACKTRACE_IGNORE_ARGS", DEBUG_BACKTRACE_IGNORE_ARGS, CONST_PERSISTENT | CONST_CS);
     137             :         /* true/false constants */
     138             :         {
     139       22587 :                 REGISTER_MAIN_BOOL_CONSTANT("TRUE", 1, CONST_PERSISTENT | CONST_CT_SUBST);
     140       22587 :                 REGISTER_MAIN_BOOL_CONSTANT("FALSE", 0, CONST_PERSISTENT | CONST_CT_SUBST);
     141       22587 :                 REGISTER_MAIN_BOOL_CONSTANT("ZEND_THREAD_SAFE", ZTS_V, CONST_PERSISTENT | CONST_CS);
     142       22587 :                 REGISTER_MAIN_BOOL_CONSTANT("ZEND_DEBUG_BUILD", ZEND_DEBUG, CONST_PERSISTENT | CONST_CS);
     143             :         }
     144       22587 :         REGISTER_MAIN_NULL_CONSTANT("NULL", CONST_PERSISTENT | CONST_CT_SUBST);
     145       22587 : }
     146             : 
     147             : 
     148           0 : int zend_shutdown_constants(void)
     149             : {
     150           0 :         zend_hash_destroy(EG(zend_constants));
     151           0 :         free(EG(zend_constants));
     152           0 :         return SUCCESS;
     153             : }
     154             : 
     155             : 
     156       22579 : void clean_non_persistent_constants(void)
     157             : {
     158       22579 :         if (EG(full_tables_cleanup)) {
     159           0 :                 zend_hash_apply(EG(zend_constants), clean_non_persistent_constant_full);
     160             :         } else {
     161       22579 :                 zend_hash_reverse_apply(EG(zend_constants), clean_non_persistent_constant);
     162             :         }
     163       22579 : }
     164             : 
     165       22587 : ZEND_API void zend_register_null_constant(const char *name, size_t name_len, int flags, int module_number)
     166             : {
     167             :         zend_constant c;
     168             : 
     169       22587 :         ZVAL_NULL(&c.value);
     170       22587 :         c.flags = flags;
     171       45174 :         c.name = zend_string_init(name, name_len, flags & CONST_PERSISTENT);
     172       22587 :         c.module_number = module_number;
     173       22587 :         zend_register_constant(&c);
     174       22587 : }
     175             : 
     176       90348 : ZEND_API void zend_register_bool_constant(const char *name, size_t name_len, zend_bool bval, int flags, int module_number)
     177             : {
     178             :         zend_constant c;
     179             : 
     180       90348 :         ZVAL_BOOL(&c.value, bval);
     181       90348 :         c.flags = flags;
     182      180696 :         c.name = zend_string_init(name, name_len, flags & CONST_PERSISTENT);
     183       90348 :         c.module_number = module_number;
     184       90348 :         zend_register_constant(&c);
     185       90348 : }
     186             : 
     187    54616013 : ZEND_API void zend_register_long_constant(const char *name, size_t name_len, zend_long lval, int flags, int module_number)
     188             : {
     189             :         zend_constant c;
     190             : 
     191    54616013 :         ZVAL_LONG(&c.value, lval);
     192    54616013 :         c.flags = flags;
     193   109232026 :         c.name = zend_string_init(name, name_len, flags & CONST_PERSISTENT);
     194    54616013 :         c.module_number = module_number;
     195    54616013 :         zend_register_constant(&c);
     196    54616013 : }
     197             : 
     198             : 
     199      429153 : ZEND_API void zend_register_double_constant(const char *name, size_t name_len, double dval, int flags, int module_number)
     200             : {
     201             :         zend_constant c;
     202             : 
     203      429153 :         ZVAL_DOUBLE(&c.value, dval);
     204      429153 :         c.flags = flags;
     205      858306 :         c.name = zend_string_init(name, name_len, flags & CONST_PERSISTENT);
     206      429153 :         c.module_number = module_number;
     207      429153 :         zend_register_constant(&c);
     208      429153 : }
     209             : 
     210             : 
     211     2146014 : ZEND_API void zend_register_stringl_constant(const char *name, size_t name_len, char *strval, size_t strlen, int flags, int module_number)
     212             : {
     213             :         zend_constant c;
     214             : 
     215     4292028 :         ZVAL_NEW_STR(&c.value, zend_string_init(strval, strlen, flags & CONST_PERSISTENT));
     216     2146014 :         c.flags = flags;
     217     4292028 :         c.name = zend_string_init(name, name_len, flags & CONST_PERSISTENT);
     218     2146014 :         c.module_number = module_number;
     219     2146014 :         zend_register_constant(&c);
     220     2146014 : }
     221             : 
     222             : 
     223     1694025 : ZEND_API void zend_register_string_constant(const char *name, size_t name_len, char *strval, int flags, int module_number)
     224             : {
     225     1694025 :         zend_register_stringl_constant(name, name_len, strval, strlen(strval), flags, module_number);
     226     1694025 : }
     227             : 
     228        1792 : static zend_constant *zend_get_special_constant(const char *name, size_t name_len)
     229             : {
     230             :         zend_constant *c;
     231             :         static char haltoff[] = "__COMPILER_HALT_OFFSET__";
     232             : 
     233        1792 :         if (!EG(current_execute_data)) {
     234         984 :                 return NULL;
     235        1311 :         } else if (name_len == sizeof("__COMPILER_HALT_OFFSET__")-1 &&
     236         503 :                   !memcmp(name, "__COMPILER_HALT_OFFSET__", sizeof("__COMPILER_HALT_OFFSET__")-1)) {
     237             :                 const char *cfilename;
     238             :                 zend_string *haltname;
     239             :                 size_t clen;
     240             : 
     241         503 :                 cfilename = zend_get_executed_filename();
     242         503 :                 clen = strlen(cfilename);
     243             :                 /* check for __COMPILER_HALT_OFFSET__ */
     244         503 :                 haltname = zend_mangle_property_name(haltoff,
     245             :                         sizeof("__COMPILER_HALT_OFFSET__") - 1, cfilename, clen, 0);
     246        1006 :                 c = zend_hash_find_ptr(EG(zend_constants), haltname);
     247             :                 zend_string_free(haltname);
     248         503 :                 return c;
     249             :         } else {
     250         305 :                 return NULL;
     251             :         }
     252             : }
     253             : 
     254       10388 : ZEND_API int zend_verify_const_access(zend_class_constant *c, zend_class_entry *scope) /* {{{ */
     255             : {
     256       10388 :         if (Z_ACCESS_FLAGS(c->value) & ZEND_ACC_PUBLIC) {
     257       10377 :                 return 1;
     258          11 :         } else if (Z_ACCESS_FLAGS(c->value) & ZEND_ACC_PRIVATE) {
     259           5 :                 return (c->ce == scope);
     260             :         } else {
     261             :                 ZEND_ASSERT(Z_ACCESS_FLAGS(c->value) & ZEND_ACC_PROTECTED);
     262           6 :                 return zend_check_protected(c->ce, scope);
     263             :         }
     264             : }
     265             : /* }}} */
     266             : 
     267        1232 : ZEND_API zval *zend_get_constant_str(const char *name, size_t name_len)
     268             : {
     269             :         zend_constant *c;
     270             :         ALLOCA_FLAG(use_heap)
     271             : 
     272        2464 :         if ((c = zend_hash_str_find_ptr(EG(zend_constants), name, name_len)) == NULL) {
     273         919 :                 char *lcname = do_alloca(name_len + 1, use_heap);
     274         919 :                 zend_str_tolower_copy(lcname, name, name_len);
     275        1838 :                 if ((c = zend_hash_str_find_ptr(EG(zend_constants), lcname, name_len)) != NULL) {
     276         217 :                         if (c->flags & CONST_CS) {
     277           0 :                                 c = NULL;
     278             :                         }
     279             :                 } else {
     280         702 :                         c = zend_get_special_constant(name, name_len);
     281             :                 }
     282         919 :                 free_alloca(lcname, use_heap);
     283             :         }
     284             : 
     285        1232 :         return c ? &c->value : NULL;
     286             : }
     287             : 
     288        1605 : ZEND_API zval *zend_get_constant(zend_string *name)
     289             : {
     290             :         zend_constant *c;
     291             :         ALLOCA_FLAG(use_heap)
     292             : 
     293        3210 :         if ((c = zend_hash_find_ptr(EG(zend_constants), name)) == NULL) {
     294        1090 :                 char *lcname = do_alloca(ZSTR_LEN(name) + 1, use_heap);
     295        1090 :                 zend_str_tolower_copy(lcname, ZSTR_VAL(name), ZSTR_LEN(name));
     296        2180 :                 if ((c = zend_hash_str_find_ptr(EG(zend_constants), lcname, ZSTR_LEN(name))) != NULL) {
     297           0 :                         if (c->flags & CONST_CS) {
     298           0 :                                 c = NULL;
     299             :                         }
     300             :                 } else {
     301        1090 :                         c = zend_get_special_constant(ZSTR_VAL(name), ZSTR_LEN(name));
     302             :                 }
     303        1090 :                 free_alloca(lcname, use_heap);
     304             :         }
     305             : 
     306        1605 :         return c ? &c->value : NULL;
     307             : }
     308             : 
     309         411 : ZEND_API zval *zend_get_constant_ex(zend_string *cname, zend_class_entry *scope, uint32_t flags)
     310             : {
     311             :         zend_constant *c;
     312             :         const char *colon;
     313         411 :         zend_class_entry *ce = NULL;
     314             :         zend_string *class_name;
     315         411 :         const char *name = ZSTR_VAL(cname);
     316         411 :         size_t name_len = ZSTR_LEN(cname);
     317             : 
     318             :         /* Skip leading \\ */
     319         411 :         if (name[0] == '\\') {
     320           8 :                 name += 1;
     321           8 :                 name_len -= 1;
     322           8 :                 cname = NULL;
     323             :         }
     324             : 
     325         553 :         if ((colon = zend_memrchr(name, ':', name_len)) &&
     326         142 :             colon > name && (*(colon - 1) == ':')) {
     327         142 :                 int class_name_len = colon - name - 1;
     328         142 :                 size_t const_name_len = name_len - class_name_len - 2;
     329         284 :                 zend_string *constant_name = zend_string_init(colon + 1, const_name_len, 0);
     330             :                 char *lcname;
     331         142 :                 zval *ret_constant = NULL;
     332             :                 ALLOCA_FLAG(use_heap)
     333             : 
     334         284 :                 class_name = zend_string_init(name, class_name_len, 0);
     335         142 :                 lcname = do_alloca(class_name_len + 1, use_heap);
     336         142 :                 zend_str_tolower_copy(lcname, name, class_name_len);
     337         142 :                 if (!scope) {
     338         133 :                         if (EG(current_execute_data)) {
     339         133 :                                 scope = EG(scope);
     340             :                         } else {
     341           0 :                                 scope = CG(active_class_entry);
     342             :                         }
     343             :                 }
     344             : 
     345         211 :                 if (class_name_len == sizeof("self")-1 &&
     346          36 :                     !memcmp(lcname, "self", sizeof("self")-1)) {
     347          33 :                         if (UNEXPECTED(!scope)) {
     348           0 :                                 zend_throw_error(NULL, "Cannot access self:: when no class scope is active");
     349           0 :                                 return NULL;
     350             :                         }
     351          33 :                         ce = scope;
     352         142 :                 } else if (class_name_len == sizeof("parent")-1 &&
     353          23 :                            !memcmp(lcname, "parent", sizeof("parent")-1)) {
     354          10 :                         if (UNEXPECTED(!scope)) {
     355           0 :                                 zend_throw_error(NULL, "Cannot access parent:: when no class scope is active");
     356           0 :                                 return NULL;
     357          10 :                         } else if (UNEXPECTED(!scope->parent)) {
     358           0 :                                 zend_throw_error(NULL, "Cannot access parent:: when current class scope has no parent");
     359           0 :                                 return NULL;
     360             :                         } else {
     361          10 :                                 ce = scope->parent;
     362             :                         }
     363         117 :                 } else if (class_name_len == sizeof("static")-1 &&
     364          13 :                            !memcmp(lcname, "static", sizeof("static")-1)) {
     365           5 :                         ce = zend_get_called_scope(EG(current_execute_data));
     366           5 :                         if (UNEXPECTED(!ce)) {
     367           0 :                                 zend_throw_error(NULL, "Cannot access static:: when no class scope is active");
     368           0 :                                 return NULL;
     369             :                         }
     370             :                 } else {
     371          94 :                         ce = zend_fetch_class(class_name, flags);
     372             :                 }
     373         136 :                 free_alloca(lcname, use_heap);
     374         136 :                 if (ce) {
     375         266 :                         zend_class_constant *c = zend_hash_find_ptr(&ce->constants_table, constant_name);
     376         133 :                         if (c == NULL) {
     377           6 :                                 if ((flags & ZEND_FETCH_CLASS_SILENT) == 0) {
     378           1 :                                         zend_throw_error(NULL, "Undefined class constant '%s::%s'", ZSTR_VAL(class_name), ZSTR_VAL(constant_name));
     379             :                                         zend_string_release(class_name);
     380             :                                         zend_string_free(constant_name);
     381           1 :                                         return NULL;
     382             :                                 }
     383           5 :                                 ret_constant = NULL;
     384             :                         } else {
     385         127 :                                 if (!zend_verify_const_access(c, scope)) {
     386           2 :                                         zend_throw_error(NULL, "Cannot access %s const %s::%s", zend_visibility_string(Z_ACCESS_FLAGS(c->value)), ZSTR_VAL(class_name), ZSTR_VAL(constant_name));
     387             :                                         zend_string_release(class_name);
     388             :                                         zend_string_free(constant_name);
     389           2 :                                         return NULL;
     390             :                                 }
     391         125 :                                 ret_constant = &c->value;
     392             :                         }
     393             :                 }
     394             :                 zend_string_release(class_name);
     395             :                 zend_string_free(constant_name);
     396         133 :                 if (ret_constant && Z_CONSTANT_P(ret_constant)) {
     397           8 :                         if (UNEXPECTED(zval_update_constant_ex(ret_constant, 1, ce) != SUCCESS)) {
     398           5 :                                 return NULL;
     399             :                         }
     400             :                 }
     401         128 :                 return ret_constant;
     402             :         }
     403             : 
     404             :         /* non-class constant */
     405         269 :         if ((colon = zend_memrchr(name, '\\', name_len)) != NULL) {
     406             :                 /* compound constant name */
     407          77 :                 int prefix_len = colon - name;
     408          77 :                 size_t const_name_len = name_len - prefix_len - 1;
     409          77 :                 const char *constant_name = colon + 1;
     410             :                 char *lcname;
     411             :                 size_t lcname_len;
     412             :                 ALLOCA_FLAG(use_heap)
     413             : 
     414          77 :                 lcname_len = prefix_len + 1 + const_name_len;
     415          77 :                 lcname = do_alloca(lcname_len + 1, use_heap);
     416          77 :                 zend_str_tolower_copy(lcname, name, prefix_len);
     417             :                 /* Check for namespace constant */
     418             : 
     419          77 :                 lcname[prefix_len] = '\\';
     420          77 :                 memcpy(lcname + prefix_len + 1, constant_name, const_name_len + 1);
     421             : 
     422         154 :                 if ((c = zend_hash_str_find_ptr(EG(zend_constants), lcname, lcname_len)) == NULL) {
     423             :                         /* try lowercase */
     424          25 :                         zend_str_tolower(lcname + prefix_len + 1, const_name_len);
     425          50 :                         if ((c = zend_hash_str_find_ptr(EG(zend_constants), lcname, lcname_len)) != NULL) {
     426           0 :                                 if ((c->flags & CONST_CS) != 0) {
     427           0 :                                         c = NULL;
     428             :                                 }
     429             :                         }
     430             :                 }
     431          77 :                 free_alloca(lcname, use_heap);
     432          77 :                 if (c) {
     433          52 :                         return &c->value;
     434             :                 }
     435             :                 /* name requires runtime resolution, need to check non-namespaced name */
     436          25 :                 if ((flags & IS_CONSTANT_UNQUALIFIED) != 0) {
     437          17 :                         return zend_get_constant_str(constant_name, const_name_len);
     438             :                 }
     439           8 :                 return NULL;
     440             :         }
     441             : 
     442         192 :         if (cname) {
     443         191 :                 return zend_get_constant(cname);
     444             :         } else {
     445           1 :                 return zend_get_constant_str(name, name_len);
     446             :         }
     447             : }
     448             : 
     449        3568 : zend_constant *zend_quick_get_constant(const zval *key, uint32_t flags)
     450             : {
     451             :         zend_constant *c;
     452             : 
     453        7136 :         if ((c = zend_hash_find_ptr(EG(zend_constants), Z_STR_P(key))) == NULL) {
     454         338 :                 key++;
     455         902 :                 if ((c = zend_hash_find_ptr(EG(zend_constants), Z_STR_P(key))) == NULL ||
     456         226 :                     (c->flags & CONST_CS) != 0) {
     457         113 :                         if ((flags & (IS_CONSTANT_IN_NAMESPACE|IS_CONSTANT_UNQUALIFIED)) == (IS_CONSTANT_IN_NAMESPACE|IS_CONSTANT_UNQUALIFIED)) {
     458          15 :                                 key++;
     459          30 :                                 if ((c = zend_hash_find_ptr(EG(zend_constants), Z_STR_P(key))) == NULL) {
     460           5 :                                     key++;
     461          10 :                                         if ((c = zend_hash_find_ptr(EG(zend_constants), Z_STR_P(key))) == NULL ||
     462           0 :                                             (c->flags & CONST_CS) != 0) {
     463             : 
     464           5 :                                                 key--;
     465           5 :                                                 c = NULL;
     466             :                                         }
     467             :                                 }
     468             :                         } else {
     469          98 :                                 key--;
     470          98 :                                 c = NULL;
     471             :                         }
     472             :                 }
     473             :         }
     474        3568 :         return c;
     475             : }
     476             : 
     477    57374034 : static void* zend_hash_add_constant(HashTable *ht, zend_string *key, zend_constant *c)
     478             : {
     479             :         void *ret;
     480    57374034 :         zend_constant *copy = pemalloc(sizeof(zend_constant), c->flags & CONST_PERSISTENT);
     481             : 
     482    57374034 :         memcpy(copy, c, sizeof(zend_constant));
     483    57374034 :         ret = zend_hash_add_ptr(ht, key, copy);
     484    57374034 :         if (!ret) {
     485           5 :                 pefree(copy, c->flags & CONST_PERSISTENT);
     486             :         }
     487    57374034 :         return ret;
     488             : }
     489             : 
     490    57374037 : ZEND_API int zend_register_constant(zend_constant *c)
     491             : {
     492    57374037 :         zend_string *lowercase_name = NULL;
     493             :         zend_string *name;
     494    57374037 :         int ret = SUCCESS;
     495             : 
     496             : #if 0
     497             :         printf("Registering constant for module %d\n", c->module_number);
     498             : #endif
     499             : 
     500    57374037 :         if (!(c->flags & CONST_CS)) {
     501     1988302 :                 lowercase_name = zend_string_alloc(ZSTR_LEN(c->name), c->flags & CONST_PERSISTENT);
     502      994151 :                 zend_str_tolower_copy(ZSTR_VAL(lowercase_name), ZSTR_VAL(c->name), ZSTR_LEN(c->name));
     503      994151 :                 lowercase_name = zend_new_interned_string(lowercase_name);
     504      994151 :                 name = lowercase_name;
     505             :         } else {
     506    56379886 :                 char *slash = strrchr(ZSTR_VAL(c->name), '\\');
     507    56379886 :                 if (slash) {
     508          98 :                         lowercase_name = zend_string_init(ZSTR_VAL(c->name), ZSTR_LEN(c->name), c->flags & CONST_PERSISTENT);
     509          49 :                         zend_str_tolower(ZSTR_VAL(lowercase_name), slash - ZSTR_VAL(c->name));
     510          49 :                         lowercase_name = zend_new_interned_string(lowercase_name);
     511          49 :                         name = lowercase_name;
     512             :                 } else {
     513    56379837 :                         name = c->name;
     514             :                 }
     515             :         }
     516             : 
     517             :         /* Check if the user is trying to define the internal pseudo constant name __COMPILER_HALT_OFFSET__ */
     518   173680653 :         if ((ZSTR_LEN(c->name) == sizeof("__COMPILER_HALT_OFFSET__")-1
     519    58932582 :                 && !memcmp(ZSTR_VAL(name), "__COMPILER_HALT_OFFSET__", sizeof("__COMPILER_HALT_OFFSET__")-1))
     520    57374034 :                 || zend_hash_add_constant(EG(zend_constants), name, c) == NULL) {
     521             : 
     522             :                 /* The internal __COMPILER_HALT_OFFSET__ is prefixed by NULL byte */
     523           8 :                 if (ZSTR_VAL(c->name)[0] == '\0' && ZSTR_LEN(c->name) > sizeof("\0__COMPILER_HALT_OFFSET__")-1
     524           0 :                         && memcmp(ZSTR_VAL(name), "\0__COMPILER_HALT_OFFSET__", sizeof("\0__COMPILER_HALT_OFFSET__")) == 0) {
     525             :                 }
     526           8 :                 zend_error(E_NOTICE,"Constant %s already defined", ZSTR_VAL(name));
     527           8 :                 zend_string_release(c->name);
     528           8 :                 if (!(c->flags & CONST_PERSISTENT)) {
     529           8 :                         zval_dtor(&c->value);
     530             :                 }
     531           8 :                 ret = FAILURE;
     532             :         }
     533    57374037 :         if (lowercase_name) {
     534             :                 zend_string_release(lowercase_name);
     535             :         }
     536    57374037 :         return ret;
     537             : }
     538             : 
     539             : 
     540             : /*
     541             :  * Local variables:
     542             :  * tab-width: 4
     543             :  * c-basic-offset: 4
     544             :  * indent-tabs-mode: t
     545             :  * End:
     546             :  */

Generated by: LCOV version 1.10

Generated at Wed, 04 May 2016 01:00:46 +0000 (39 hours ago)

Copyright © 2005-2016 The PHP Group
All rights reserved.