diff options
author | Dmitry Stogov <dmitry@zend.com> | 2014-09-01 20:57:33 +0400 |
---|---|---|
committer | Dmitry Stogov <dmitry@zend.com> | 2014-09-01 20:57:33 +0400 |
commit | 88d7ca44f645c6e1bbdb17affd7a34113911093d (patch) | |
tree | 15c356bf3749c703ca52e89081ec8219d237b615 /Zend/zend_ini.c | |
parent | b9f3247267299cd38da851057c1bb90090db3e20 (diff) | |
download | php-git-88d7ca44f645c6e1bbdb17affd7a34113911093d.tar.gz |
Refactored INI subsystem to use zend_string* instead of char*
Diffstat (limited to 'Zend/zend_ini.c')
-rw-r--r-- | Zend/zend_ini.c | 150 |
1 files changed, 92 insertions, 58 deletions
diff --git a/Zend/zend_ini.c b/Zend/zend_ini.c index 59c2776a85..21c2c2204e 100644 --- a/Zend/zend_ini.c +++ b/Zend/zend_ini.c @@ -56,7 +56,7 @@ static int zend_restore_ini_entry_cb(zend_ini_entry *ini_entry, int stage TSRMLS /* even if on_modify bails out, we have to continue on with restoring, since there can be allocated variables that would be freed on MM shutdown and would lead to memory corruption later ini entry is modified again */ - result = ini_entry->on_modify(ini_entry, ini_entry->orig_value, ini_entry->orig_value_length, ini_entry->mh_arg1, ini_entry->mh_arg2, ini_entry->mh_arg3, stage TSRMLS_CC); + result = ini_entry->on_modify(ini_entry, ini_entry->orig_value, ini_entry->mh_arg1, ini_entry->mh_arg2, ini_entry->mh_arg3, stage TSRMLS_CC); } zend_end_try(); } if (stage == ZEND_INI_STAGE_RUNTIME && result == FAILURE) { @@ -64,14 +64,12 @@ static int zend_restore_ini_entry_cb(zend_ini_entry *ini_entry, int stage TSRMLS return 1; } if (ini_entry->value != ini_entry->orig_value) { - efree(ini_entry->value); + zend_string_release(ini_entry->value); } ini_entry->value = ini_entry->orig_value; - ini_entry->value_length = ini_entry->orig_value_length; ini_entry->modifiable = ini_entry->orig_modifiable; ini_entry->modified = 0; ini_entry->orig_value = NULL; - ini_entry->orig_value_length = 0; ini_entry->orig_modifiable = 0; } return 0; @@ -177,11 +175,10 @@ ZEND_API void zend_ini_sort_entries(TSRMLS_D) /* {{{ */ /* * Registration / unregistration */ -ZEND_API int zend_register_ini_entries(const zend_ini_entry *ini_entry, int module_number TSRMLS_DC) /* {{{ */ +ZEND_API int zend_register_ini_entries(const zend_ini_entry_def *ini_entry, int module_number TSRMLS_DC) /* {{{ */ { - const zend_ini_entry *p = ini_entry; - zend_ini_entry *hashed_ini_entry; - zval default_value; + zend_ini_entry *p; + zval *default_value; HashTable *directives = registered_zend_ini_directives; zend_bool config_directive_success = 0; @@ -199,26 +196,43 @@ ZEND_API int zend_register_ini_entries(const zend_ini_entry *ini_entry, int modu } #endif - while (p->name) { + while (ini_entry->name) { + p = pemalloc(sizeof(zend_ini_entry), 1); + p->name = zend_string_init(ini_entry->name, ini_entry->name_length, 1); + p->on_modify = ini_entry->on_modify; + p->mh_arg1 = ini_entry->mh_arg1; + p->mh_arg2 = ini_entry->mh_arg2; + p->mh_arg3 = ini_entry->mh_arg3; + p->value = ini_entry->value ? + zend_string_init(ini_entry->value, ini_entry->value_length, 1) : NULL; + p->orig_value = NULL; + p->displayer = ini_entry->displayer; + p->modifiable = ini_entry->modifiable; + + p->orig_modifiable = 0; + p->modified = 0; + p->module_number = module_number; + config_directive_success = 0; - if ((hashed_ini_entry = zend_hash_str_add_mem(directives, p->name, p->name_length, (void*)p, sizeof(zend_ini_entry))) == NULL) { + if (zend_hash_add_ptr(directives, p->name, (void*)p) == NULL) { + if (p->orig_value) { + zend_string_release(p->orig_value); + } zend_unregister_ini_entries(module_number TSRMLS_CC); return FAILURE; } - hashed_ini_entry->module_number = module_number; - if ((zend_get_configuration_directive(p->name, p->name_length, &default_value)) == SUCCESS) { - if (!hashed_ini_entry->on_modify - || hashed_ini_entry->on_modify(hashed_ini_entry, Z_STRVAL(default_value), Z_STRLEN(default_value), hashed_ini_entry->mh_arg1, hashed_ini_entry->mh_arg2, hashed_ini_entry->mh_arg3, ZEND_INI_STAGE_STARTUP TSRMLS_CC) == SUCCESS) { - hashed_ini_entry->value = Z_STRVAL(default_value); - hashed_ini_entry->value_length = Z_STRLEN(default_value); + if ((default_value = zend_get_configuration_directive(p->name)) != NULL) { + if (!p->on_modify + || p->on_modify(p, Z_STR_P(default_value), p->mh_arg1, p->mh_arg2, p->mh_arg3, ZEND_INI_STAGE_STARTUP TSRMLS_CC) == SUCCESS) { + p->value = Z_STR_P(default_value); config_directive_success = 1; } } - if (!config_directive_success && hashed_ini_entry->on_modify) { - hashed_ini_entry->on_modify(hashed_ini_entry, hashed_ini_entry->value, hashed_ini_entry->value_length, hashed_ini_entry->mh_arg1, hashed_ini_entry->mh_arg2, hashed_ini_entry->mh_arg3, ZEND_INI_STAGE_STARTUP TSRMLS_CC); + if (!config_directive_success && p->on_modify) { + p->on_modify(p, p->value, p->mh_arg1, p->mh_arg2, p->mh_arg3, ZEND_INI_STAGE_STARTUP TSRMLS_CC); } - p++; + ini_entry++; } return SUCCESS; } @@ -250,18 +264,43 @@ ZEND_API void zend_ini_refresh_caches(int stage TSRMLS_DC) /* {{{ */ /* }}} */ #endif -ZEND_API int zend_alter_ini_entry(zend_string *name, char *new_value, uint new_value_length, int modify_type, int stage) /* {{{ */ +ZEND_API int zend_alter_ini_entry(zend_string *name, zend_string *new_value, int modify_type, int stage) /* {{{ */ { TSRMLS_FETCH(); - return zend_alter_ini_entry_ex(name, new_value, new_value_length, modify_type, stage, 0 TSRMLS_CC); + return zend_alter_ini_entry_ex(name, new_value, modify_type, stage, 0 TSRMLS_CC); +} +/* }}} */ + +ZEND_API int zend_alter_ini_entry_chars(zend_string *name, const char *value, size_t value_length, int modify_type, int stage) /* {{{ */ +{ + int ret; + zend_string *new_value; + TSRMLS_FETCH(); + + new_value = zend_string_init(value, value_length, stage != ZEND_INI_STAGE_RUNTIME); + ret = zend_alter_ini_entry_ex(name, new_value, modify_type, stage, 0 TSRMLS_CC); + zend_string_release(new_value); + return ret; +} +/* }}} */ + +ZEND_API int zend_alter_ini_entry_chars_ex(zend_string *name, const char *value, size_t value_length, int modify_type, int stage, int force_change TSRMLS_DC) /* {{{ */ +{ + int ret; + zend_string *new_value; + + new_value = zend_string_init(value, value_length, stage != ZEND_INI_STAGE_RUNTIME); + ret = zend_alter_ini_entry_ex(name, new_value, modify_type, stage, force_change TSRMLS_CC); + zend_string_release(new_value); + return ret; } /* }}} */ -ZEND_API int zend_alter_ini_entry_ex(zend_string *name, char *new_value, uint new_value_length, int modify_type, int stage, int force_change TSRMLS_DC) /* {{{ */ +ZEND_API int zend_alter_ini_entry_ex(zend_string *name, zend_string *new_value, int modify_type, int stage, int force_change TSRMLS_DC) /* {{{ */ { zend_ini_entry *ini_entry; - char *duplicate; + zend_string *duplicate; zend_bool modifiable; zend_bool modified; @@ -288,23 +327,21 @@ ZEND_API int zend_alter_ini_entry_ex(zend_string *name, char *new_value, uint ne } if (!modified) { ini_entry->orig_value = ini_entry->value; - ini_entry->orig_value_length = ini_entry->value_length; ini_entry->orig_modifiable = modifiable; ini_entry->modified = 1; zend_hash_add_ptr(EG(modified_ini_directives), name, ini_entry); } - duplicate = estrndup(new_value, new_value_length); + duplicate = zend_string_copy(new_value); if (!ini_entry->on_modify - || ini_entry->on_modify(ini_entry, duplicate, new_value_length, ini_entry->mh_arg1, ini_entry->mh_arg2, ini_entry->mh_arg3, stage TSRMLS_CC) == SUCCESS) { + || ini_entry->on_modify(ini_entry, duplicate, ini_entry->mh_arg1, ini_entry->mh_arg2, ini_entry->mh_arg3, stage TSRMLS_CC) == SUCCESS) { if (modified && ini_entry->orig_value != ini_entry->value) { /* we already changed the value, free the changed value */ - efree(ini_entry->value); + zend_string_release(ini_entry->value); } ini_entry->value = duplicate; - ini_entry->value_length = new_value_length; } else { - efree(duplicate); + zend_string_release(duplicate); return FAILURE; } @@ -360,9 +397,9 @@ ZEND_API zend_long zend_ini_long(char *name, uint name_length, int orig) /* {{{ ini_entry = zend_hash_str_find_ptr(EG(ini_directives), name, name_length); if (ini_entry) { if (orig && ini_entry->modified) { - return (ini_entry->orig_value ? ZEND_STRTOL(ini_entry->orig_value, NULL, 0) : 0); + return (ini_entry->orig_value ? ZEND_STRTOL(ini_entry->orig_value->val, NULL, 0) : 0); } else { - return (ini_entry->value ? ZEND_STRTOL(ini_entry->value, NULL, 0) : 0); + return (ini_entry->value ? ZEND_STRTOL(ini_entry->value->val, NULL, 0) : 0); } } @@ -378,9 +415,9 @@ ZEND_API double zend_ini_double(char *name, uint name_length, int orig) /* {{{ * ini_entry = zend_hash_str_find_ptr(EG(ini_directives), name, name_length); if (ini_entry) { if (orig && ini_entry->modified) { - return (double) (ini_entry->orig_value ? zend_strtod(ini_entry->orig_value, NULL) : 0.0); + return (double) (ini_entry->orig_value ? zend_strtod(ini_entry->orig_value->val, NULL) : 0.0); } else { - return (double) (ini_entry->value ? zend_strtod(ini_entry->value, NULL) : 0.0); + return (double) (ini_entry->value ? zend_strtod(ini_entry->value->val, NULL) : 0.0); } } @@ -400,9 +437,9 @@ ZEND_API char *zend_ini_string_ex(char *name, uint name_length, int orig, zend_b } if (orig && ini_entry->modified) { - return ini_entry->orig_value; + return ini_entry->orig_value ? ini_entry->orig_value->val : NULL; } else { - return ini_entry->value; + return ini_entry->value ? ini_entry->value->val : NULL; } } else { if (exists) { @@ -470,29 +507,26 @@ static void zend_ini_displayer_cb(zend_ini_entry *ini_entry, int type) /* {{{ */ ZEND_INI_DISP(zend_ini_boolean_displayer_cb) /* {{{ */ { - int value, tmp_value_len; - char *tmp_value; + int value; + zend_string *tmp_value; if (type == ZEND_INI_DISPLAY_ORIG && ini_entry->modified) { tmp_value = (ini_entry->orig_value ? ini_entry->orig_value : NULL ); - tmp_value_len = ini_entry->orig_value_length; } else if (ini_entry->value) { tmp_value = ini_entry->value; - tmp_value_len = ini_entry->value_length; } else { tmp_value = NULL; - tmp_value_len = 0; } if (tmp_value) { - if (tmp_value_len == 4 && strcasecmp(tmp_value, "true") == 0) { + if (tmp_value->len == 4 && strcasecmp(tmp_value->val, "true") == 0) { value = 1; - } else if (tmp_value_len == 3 && strcasecmp(tmp_value, "yes") == 0) { + } else if (tmp_value->len == 3 && strcasecmp(tmp_value->val, "yes") == 0) { value = 1; - } else if (tmp_value_len == 2 && strcasecmp(tmp_value, "on") == 0) { + } else if (tmp_value->len == 2 && strcasecmp(tmp_value->val, "on") == 0) { value = 1; } else { - value = atoi(tmp_value); + value = atoi(tmp_value->val); } } else { value = 0; @@ -511,9 +545,9 @@ ZEND_INI_DISP(zend_ini_color_displayer_cb) /* {{{ */ char *value; if (type == ZEND_INI_DISPLAY_ORIG && ini_entry->modified) { - value = ini_entry->orig_value; + value = ini_entry->orig_value->val; } else if (ini_entry->value) { - value = ini_entry->value; + value = ini_entry->value->val; } else { value = NULL; } @@ -538,9 +572,9 @@ ZEND_INI_DISP(display_link_numbers) /* {{{ */ char *value; if (type == ZEND_INI_DISPLAY_ORIG && ini_entry->modified) { - value = ini_entry->orig_value; + value = ini_entry->orig_value->val; } else if (ini_entry->value) { - value = ini_entry->value; + value = ini_entry->value->val; } else { value = NULL; } @@ -569,17 +603,17 @@ ZEND_API ZEND_INI_MH(OnUpdateBool) /* {{{ */ p = (zend_bool *) (base+(size_t) mh_arg1); - if (new_value_length == 2 && strcasecmp("on", new_value) == 0) { + if (new_value->len == 2 && strcasecmp("on", new_value->val) == 0) { *p = (zend_bool) 1; } - else if (new_value_length == 3 && strcasecmp("yes", new_value) == 0) { + else if (new_value->len == 3 && strcasecmp("yes", new_value->val) == 0) { *p = (zend_bool) 1; } - else if (new_value_length == 4 && strcasecmp("true", new_value) == 0) { + else if (new_value->len == 4 && strcasecmp("true", new_value->val) == 0) { *p = (zend_bool) 1; } else { - *p = (zend_bool) atoi(new_value); + *p = (zend_bool) atoi(new_value->val); } return SUCCESS; } @@ -598,7 +632,7 @@ ZEND_API ZEND_INI_MH(OnUpdateLong) /* {{{ */ p = (zend_long *) (base+(size_t) mh_arg1); - *p = zend_atol(new_value, new_value_length); + *p = zend_atol(new_value->val, new_value->len); return SUCCESS; } /* }}} */ @@ -614,7 +648,7 @@ ZEND_API ZEND_INI_MH(OnUpdateLongGEZero) /* {{{ */ base = (char *) ts_resource(*((int *) mh_arg2)); #endif - tmp = zend_atol(new_value, new_value_length); + tmp = zend_atol(new_value->val, new_value->len); if (tmp < 0) { return FAILURE; } @@ -639,7 +673,7 @@ ZEND_API ZEND_INI_MH(OnUpdateReal) /* {{{ */ p = (double *) (base+(size_t) mh_arg1); - *p = zend_strtod(new_value, NULL); + *p = zend_strtod(new_value->val, NULL); return SUCCESS; } /* }}} */ @@ -657,7 +691,7 @@ ZEND_API ZEND_INI_MH(OnUpdateString) /* {{{ */ p = (char **) (base+(size_t) mh_arg1); - *p = new_value; + *p = new_value ? new_value->val : NULL; return SUCCESS; } /* }}} */ @@ -673,13 +707,13 @@ ZEND_API ZEND_INI_MH(OnUpdateStringUnempty) /* {{{ */ base = (char *) ts_resource(*((int *) mh_arg2)); #endif - if (new_value && !new_value[0]) { + if (new_value && !new_value->val[0]) { return FAILURE; } p = (char **) (base+(size_t) mh_arg1); - *p = new_value; + *p = new_value ? new_value->val : NULL; return SUCCESS; } /* }}} */ |