-
Notifications
You must be signed in to change notification settings - Fork 136
/
runkit_constants.c
286 lines (237 loc) · 8.98 KB
/
runkit_constants.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2006 The PHP Group, (c) 2008-2015 Dmitry Zenovich |
+----------------------------------------------------------------------+
| This source file is subject to the new BSD 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.opensource.org/licenses/BSD-3-Clause |
| If you did not receive a copy of the license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Sara Golemon <[email protected]> |
| Modified by Dmitry Zenovich <[email protected]> |
+----------------------------------------------------------------------+
*/
/* $Id$ */
#include "php_runkit.h"
#ifdef PHP_RUNKIT_MANIPULATION
/* {{{ php_runkit_fetch_const
*/
static int php_runkit_fetch_const(char *cname, int cname_len, zend_constant **constant, char **found_cname TSRMLS_DC)
{
char *lcase = NULL;
char *old_cname = cname;
int constant_name_len = cname_len;
#if RUNKIT_ABOVE53
char *separator;
PHP_RUNKIT_NORMALIZE_NAMESPACE(cname);
separator = (char *) zend_memrchr(cname, '\\', cname_len);
if (separator) {
int nsname_len = separator - cname;
char *constant_name = separator + 1;
constant_name_len = cname_len - nsname_len - 1;
lcase = emalloc(nsname_len + 1 + constant_name_len + 1);
memcpy(lcase, cname, nsname_len + 1);
memcpy(lcase + nsname_len + 1, constant_name, constant_name_len + 1);
zend_str_tolower(lcase, nsname_len);
cname = lcase;
}
#endif
if (zend_hash_find(EG(zend_constants), cname, cname_len + 1, (void*)constant) == FAILURE) {
if (!lcase) {
lcase = estrndup(cname, cname_len);
zend_str_tolower(lcase, cname_len);
} else {
zend_str_tolower(lcase + cname_len - constant_name_len, constant_name_len);
}
cname = lcase;
if (zend_hash_find(EG(zend_constants), cname, cname_len + 1, (void*)constant) == FAILURE || ((*constant)->flags & CONST_CS)) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Constant %s not found", old_cname);
efree(lcase);
return FAILURE;
}
}
if (found_cname) {
*found_cname = lcase ? lcase : estrndup(cname, cname_len);
}
return SUCCESS;
}
/* }}} */
/* {{{ php_runkit_update_children_consts
Scan the class_table for children of the class just updated */
int php_runkit_update_children_consts(RUNKIT_53_TSRMLS_ARG(void *pDest), int num_args, va_list args, zend_hash_key *hash_key)
{
zend_class_entry *ce = (zend_class_entry *) pDest;
zend_class_entry *parent_class = va_arg(args, zend_class_entry*);
zval **c = va_arg(args, zval**);
char *cname = va_arg(args, char*);
int cname_len = va_arg(args, int);
RUNKIT_UNDER53_TSRMLS_FETCH();
ce = *((zend_class_entry**)ce);
if (ce->parent != parent_class) {
/* Not a child, ignore */
return ZEND_HASH_APPLY_KEEP;
}
/* Process children of this child */
zend_hash_apply_with_arguments(RUNKIT_53_TSRMLS_PARAM(EG(class_table)), php_runkit_update_children_consts, 4, ce, c, cname, cname_len);
Z_ADDREF_P(*c);
zend_hash_del(&ce->constants_table, cname, cname_len + 1);
if (zend_hash_add(&ce->constants_table, cname, cname_len + 1, (void *) c, sizeof(zval**), NULL) == FAILURE) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error updating child class");
return ZEND_HASH_APPLY_KEEP;
}
return ZEND_HASH_APPLY_KEEP;
}
/* }}} */
/* {{{ php_runkit_constant_remove
*/
static int php_runkit_constant_remove(char *classname, int classname_len, char *constname, int constname_len TSRMLS_DC)
{
zend_constant *constant;
char *found_constname;
if (classname && (classname_len > 0)) {
zend_class_entry *ce;
if (php_runkit_fetch_class(classname, classname_len, &ce TSRMLS_CC)==FAILURE) {
return FAILURE;
}
if (!zend_hash_exists(&ce->constants_table, constname, constname_len+1)) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Constant %s::%s does not exist", classname, constname);
return FAILURE;
}
if (zend_hash_del(&ce->constants_table, constname, constname_len+1)==FAILURE) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to remove constant %s::%s", classname, constname);
return FAILURE;
}
#if (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION >= 4) || (PHP_MAJOR_VERSION > 5)
php_runkit_clear_all_functions_runtime_cache(TSRMLS_C);
#endif
return SUCCESS;
}
if (php_runkit_fetch_const(constname, constname_len, &constant, &found_constname TSRMLS_CC) == FAILURE) {
return FAILURE;
}
if (constant->module_number != PHP_USER_CONSTANT) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Only user defined constants may be removed.");
return FAILURE;
}
if (zend_hash_del(EG(zend_constants), found_constname, constant->name_len) == FAILURE) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to remove constant");
efree(found_constname);
return FAILURE;
}
efree(found_constname);
#if (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION >= 4) || (PHP_MAJOR_VERSION > 5)
php_runkit_clear_all_functions_runtime_cache(TSRMLS_C);
#endif
return SUCCESS;
}
/* }}} */
/* {{{ php_runkit_constant_add
*/
static int php_runkit_constant_add(char *classname, int classname_len, char *constname, int constname_len, zval *value TSRMLS_DC)
{
zend_class_entry *ce;
zval *copyval;
switch (value->type) {
case IS_LONG:
case IS_DOUBLE:
case IS_STRING:
case IS_BOOL:
case IS_RESOURCE:
case IS_NULL:
break;
default:
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Constants may only evaluate to scalar values");
return FAILURE;
}
if ((classname == NULL) || (classname_len == 0)) {
zend_constant c;
#if RUNKIT_ABOVE53
PHP_RUNKIT_NORMALIZE_NAMESPACE(constname);
#endif
/* Traditional global constant */
c.value = *value;
zval_copy_ctor(&c.value);
c.flags = CONST_CS;
c.name = zend_strndup(constname, constname_len);
c.name_len = constname_len + 1;
c.module_number = PHP_USER_CONSTANT;
return zend_register_constant(&c TSRMLS_CC);
}
if (php_runkit_fetch_class(classname, classname_len, &ce TSRMLS_CC)==FAILURE) {
return FAILURE;
}
ALLOC_ZVAL(copyval);
*copyval = *value;
zval_copy_ctor(copyval);
if (zend_hash_add(&ce->constants_table, constname, constname_len + 1, ©val, sizeof(zval *), NULL) == FAILURE) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to add constant to class definition");
zval_ptr_dtor(©val);
return FAILURE;
}
#if (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION >= 4) || (PHP_MAJOR_VERSION > 5)
php_runkit_clear_all_functions_runtime_cache(TSRMLS_C);
#endif
zend_hash_apply_with_arguments(RUNKIT_53_TSRMLS_PARAM(EG(class_table)), (apply_func_args_t)php_runkit_update_children_consts, 4, ce, ©val, constname, constname_len);
return SUCCESS;
}
/* }}} */
/* *****************
* Constants API *
***************** */
/* {{{ proto bool runkit_constant_redefine(string constname, mixed newvalue)
*/
PHP_FUNCTION(runkit_constant_redefine)
{
char *classname = NULL, *constname;
int classname_len = 0, constname_len;
zval *value;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sz", &constname, &constname_len, &value) == FAILURE) {
RETURN_FALSE;
}
PHP_RUNKIT_SPLIT_PN(classname, classname_len, constname, constname_len);
php_runkit_constant_remove(classname, classname_len, constname, constname_len TSRMLS_CC);
RETURN_BOOL(php_runkit_constant_add(classname, classname_len, constname, constname_len, value TSRMLS_CC) == SUCCESS);
}
/* }}} */
/* {{{ proto bool runkit_constant_remove(string constname)
*/
PHP_FUNCTION(runkit_constant_remove)
{
char *classname = NULL, *constname;
int classname_len = 0, constname_len;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &constname, &constname_len) == FAILURE) {
RETURN_FALSE;
}
PHP_RUNKIT_SPLIT_PN(classname, classname_len, constname, constname_len);
RETURN_BOOL(php_runkit_constant_remove(classname, classname_len, constname, constname_len TSRMLS_CC)==SUCCESS);
}
/* }}} */
/* {{{ proto bool runkit_constant_add(string constname, mixed value)
Similar to define(), but allows defining in class definitions as well
*/
PHP_FUNCTION(runkit_constant_add)
{
char *classname = NULL, *constname;
int classname_len = 0, constname_len;
zval *value;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sz", &constname, &constname_len, &value) == FAILURE) {
RETURN_FALSE;
}
PHP_RUNKIT_SPLIT_PN(classname, classname_len, constname, constname_len);
RETURN_BOOL(php_runkit_constant_add(classname, classname_len, constname, constname_len, value TSRMLS_CC) == SUCCESS);
}
/* }}} */
#endif /* PHP_RUNKIT_MANIPULATION */
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: noet sw=4 ts=4 fdm=marker
* vim<600: noet sw=4 ts=4
*/