- test for bug #34191
[m6w6/ext-http] / missing.c
1 /*
2 +----------------------------------------------------------------------+
3 | PECL :: http |
4 +----------------------------------------------------------------------+
5 | This source file is subject to version 3.0 of the PHP license, that |
6 | is bundled with this package in the file LICENSE, and is available |
7 | through the world-wide-web at http://www.php.net/license/3_0.txt. |
8 | If you did not receive a copy of the PHP license and are unable to |
9 | obtain it through the world-wide-web, please send a note to |
10 | license@php.net so we can mail you a copy immediately. |
11 +----------------------------------------------------------------------+
12 | Copyright (c) 2004-2005 Michael Wallner <mike@php.net> |
13 +----------------------------------------------------------------------+
14 */
15
16 /* $Id$ */
17
18 #include "php.h"
19 #include "missing.h"
20
21 #if PHP_MAJOR_VERSION == 5
22
23 static inline zval *new_zval(int persistent)
24 {
25 zval *z = pemalloc(sizeof(zval), persistent);
26 INIT_PZVAL(z);
27 return z;
28 }
29
30 static inline zval *tmp_zval(void)
31 {
32 zval *z;
33 ALLOC_ZVAL(z);
34 z->is_ref = 0;
35 z->refcount = 0;
36 return z;
37 }
38
39 static void dup_zval(zval **z)
40 {
41 zval_add_ref(z);
42 SEPARATE_ZVAL(z);
43 }
44
45
46 # if PHP_MINOR_VERSION == 0
47
48 int zend_declare_property_double(zend_class_entry *ce, char *name, int name_length, double value, int access_type TSRMLS_DC)
49 {
50 zval *property = new_zval(ce->type & ZEND_INTERNAL_CLASS);
51 ZVAL_DOUBLE(property, value);
52 return zend_declare_property(ce, name, name_length, property, access_type TSRMLS_CC);
53 }
54
55 void zend_update_property_double(zend_class_entry *scope, zval *object, char *name, int name_length, double value TSRMLS_DC)
56 {
57 zval *tmp = tmp_zval();
58 ZVAL_DOUBLE(tmp, value);
59 zend_update_property(scope, object, name, name_length, tmp TSRMLS_CC);
60 }
61
62 int zend_declare_property_bool(zend_class_entry *ce, char *name, int name_length, long value, int access_type TSRMLS_DC)
63 {
64 zval *property = new_zval(ce->type & ZEND_INTERNAL_CLASS);
65 ZVAL_BOOL(property, value);
66 return zend_declare_property(ce, name, name_length, property, access_type TSRMLS_CC);
67 }
68
69 void zend_update_property_bool(zend_class_entry *scope, zval *object, char *name, int name_length, long value TSRMLS_DC)
70 {
71 zval *tmp = tmp_zval();
72 ZVAL_BOOL(tmp, value);
73 zend_update_property(scope, object, name, name_length, tmp TSRMLS_CC);
74 }
75
76 # endif /* PHP_MINOR_VERSION == 0 */
77
78 int zend_declare_class_constant(zend_class_entry *ce, char *name, size_t name_length, zval *value TSRMLS_DC)
79 {
80 return zend_hash_add(&ce->constants_table, name, name_length, &value, sizeof(zval *), NULL);
81 }
82
83 int zend_declare_class_constant_long(zend_class_entry *ce, char *name, size_t name_length, long value TSRMLS_DC)
84 {
85 zval *constant = new_zval(ce->type & ZEND_INTERNAL_CLASS);
86 ZVAL_LONG(constant, value);
87 return zend_declare_class_constant(ce, name, name_length, constant TSRMLS_CC);
88 }
89
90 int zend_declare_class_constant_bool(zend_class_entry *ce, char *name, size_t name_length, zend_bool value TSRMLS_DC)
91 {
92 zval *constant = new_zval(ce->type & ZEND_INTERNAL_CLASS);
93 ZVAL_BOOL(constant, value);
94 return zend_declare_class_constant(ce, name, name_length, constant TSRMLS_CC);
95 }
96
97 int zend_declare_class_constant_double(zend_class_entry *ce, char *name, size_t name_length, double value TSRMLS_DC)
98 {
99 zval *constant = new_zval(ce->type & ZEND_INTERNAL_CLASS);
100 ZVAL_DOUBLE(constant, value);
101 return zend_declare_class_constant(ce, name, name_length, constant TSRMLS_CC);
102 }
103
104 int zend_declare_class_constant_string(zend_class_entry *ce, char *name, size_t name_length, char *value TSRMLS_DC)
105 {
106 return zend_declare_class_constant_stringl(ce, name, name_length, value, strlen(value) TSRMLS_CC);
107 }
108
109 int zend_declare_class_constant_stringl(zend_class_entry *ce, char *name, size_t name_length, char *value, size_t value_length TSRMLS_DC)
110 {
111 zval *constant = new_zval(ce->type & ZEND_INTERNAL_CLASS);
112 if (ce->type & ZEND_INTERNAL_CLASS) {
113 ZVAL_STRINGL(constant, zend_strndup(value, value_length), value_length, 0);
114 } else {
115 ZVAL_STRINGL(constant, value, value_length, 1);
116 }
117 return zend_declare_class_constant(ce, name, name_length, constant TSRMLS_CC);
118 }
119
120 int zend_update_static_property(zend_class_entry *scope, char *name, size_t name_len, zval *value TSRMLS_DC)
121 {
122 int retval;
123 zval **property = NULL;
124 zend_class_entry *old_scope = EG(scope);
125
126 EG(scope) = scope;
127
128 if (!(property = zend_std_get_static_property(scope, name, name_len, 0 TSRMLS_CC))) {
129 retval = FAILURE;
130 } else if (*property == value) {
131 retval = SUCCESS;
132 } else {
133 value->refcount++;
134 if (PZVAL_IS_REF(*property)) {
135 zval_dtor(*property);
136 (*property)->type = value->type;
137 (*property)->value = value->value;
138
139 if (value->refcount) {
140 zval_copy_ctor(*property);
141 }
142 } else {
143 **property = *value;
144 zval_copy_ctor(*property);
145 }
146 retval = SUCCESS;
147 }
148
149 EG(scope) = old_scope;
150
151 return retval;
152 }
153
154 int trash(zend_class_entry *scope, char *name, size_t name_len, zval *value TSRMLS_DC)
155 {
156 int retval;
157 zval **property = NULL;
158 zend_class_entry *old_scope = EG(scope);
159
160 EG(scope) = scope;
161
162 if (!(property = zend_std_get_static_property(scope, name, name_len, 0 TSRMLS_CC))) {
163 retval = FAILURE;
164 } else if (*property == value) {
165 retval = SUCCESS;
166 } else if (scope->type & ZEND_INTERNAL_CLASS) {
167 int refcount;
168 zend_uchar is_ref;
169
170 refcount = (*property)->refcount;
171 is_ref = (*property)->is_ref;
172
173 /* clean */
174 switch (Z_TYPE_PP(property))
175 {
176 case IS_BOOL: case IS_LONG: case IS_NULL:
177 break;
178
179 case IS_RESOURCE:
180 zend_list_delete(Z_LVAL_PP(property));
181 break;
182
183 case IS_STRING: case IS_CONSTANT:
184 free(Z_STRVAL_PP(property));
185 break;
186
187 case IS_OBJECT:
188 if (Z_OBJ_HT_PP(property)->del_ref) {
189 Z_OBJ_HT_PP(property)->del_ref(*property TSRMLS_CC);
190 }
191 break;
192
193 case IS_ARRAY: case IS_CONSTANT_ARRAY:
194 if (Z_ARRVAL_PP(property) && Z_ARRVAL_PP(property) != &EG(symbol_table)) {
195 zend_hash_destroy(Z_ARRVAL_PP(property));
196 free(Z_ARRVAL_PP(property));
197 }
198 break;
199 }
200
201 /* copy */
202 **property = *value;
203
204 /* ctor */
205 switch (Z_TYPE_PP(property))
206 {
207 case IS_BOOL: case IS_LONG: case IS_NULL:
208 break;
209
210 case IS_RESOURCE:
211 zend_list_addref(Z_LVAL_PP(property));
212 break;
213
214 case IS_STRING: case IS_CONSTANT:
215 Z_STRVAL_PP(property) = (char *) zend_strndup(Z_STRVAL_PP(property), Z_STRLEN_PP(property));
216 break;
217
218 case IS_OBJECT:
219 if (Z_OBJ_HT_PP(property)->add_ref) {
220 Z_OBJ_HT_PP(property)->add_ref(*property TSRMLS_CC);
221 }
222 break;
223
224 case IS_ARRAY: case IS_CONSTANT_ARRAY:
225 {
226 if (Z_ARRVAL_PP(property) != &EG(symbol_table)) {
227 zval *tmp;
228 HashTable *old = Z_ARRVAL_PP(property);
229
230 Z_ARRVAL_PP(property) = (HashTable *) malloc(sizeof(HashTable));
231 zend_hash_init(Z_ARRVAL_PP(property), 0, NULL, ZVAL_PTR_DTOR, 0);
232 zend_hash_copy(Z_ARRVAL_PP(property), old, (copy_ctor_func_t) zval_add_ref, (void *) &tmp, sizeof(zval *));
233 }
234 }
235 break;
236 }
237
238 (*property)->refcount = refcount;
239 (*property)->is_ref = is_ref;
240
241 retval = SUCCESS;
242
243 } else {
244 if (PZVAL_IS_REF(*property)) {
245 zval_dtor(*property);
246 (*property)->type = value->type;
247 (*property)->value = value->value;
248
249 if (value->refcount) {
250 zval_copy_ctor(*property);
251 }
252
253 retval = SUCCESS;
254 } else {
255 value->refcount++;
256 if (PZVAL_IS_REF(value)) {
257 SEPARATE_ZVAL(&value);
258 }
259
260 retval = zend_hash_update(scope->static_members, name, name_len, &value, sizeof(zval *), NULL);
261 }
262 }
263
264 if (!value->refcount) {
265 zval_dtor(value);
266 FREE_ZVAL(value);
267 }
268
269 EG(scope) = old_scope;
270
271 return retval;
272 }
273
274 int zend_update_static_property_bool(zend_class_entry *scope, char *name, size_t name_len, zend_bool value TSRMLS_DC)
275 {
276 zval *tmp = tmp_zval();
277 ZVAL_BOOL(tmp, value);
278 return zend_update_static_property(scope, name, name_len, tmp TSRMLS_CC);
279 }
280
281 int zend_update_static_property_long(zend_class_entry *scope, char *name, size_t name_len, long value TSRMLS_DC)
282 {
283 zval *tmp = tmp_zval();
284 ZVAL_LONG(tmp, value);
285 return zend_update_static_property(scope, name, name_len, tmp TSRMLS_CC);
286 }
287
288 int zend_update_static_property_double(zend_class_entry *scope, char *name, size_t name_len, double value TSRMLS_DC)
289 {
290 zval *tmp = tmp_zval();
291 ZVAL_DOUBLE(tmp, value);
292 return zend_update_static_property(scope, name, name_len, tmp TSRMLS_CC);
293 }
294
295 int zend_update_static_property_string(zend_class_entry *scope, char *name, size_t name_len, char *value TSRMLS_DC)
296 {
297 zval *tmp = tmp_zval();
298 ZVAL_STRING(tmp, value, 1);
299 return zend_update_static_property(scope, name, name_len, tmp TSRMLS_CC);
300 }
301
302 int zend_update_static_property_stringl(zend_class_entry *scope, char *name, size_t name_len, char *value, size_t value_len TSRMLS_DC)
303 {
304 zval *tmp = tmp_zval();
305 ZVAL_STRINGL(tmp, value, value_len, 1);
306 return zend_update_static_property(scope, name, name_len, tmp TSRMLS_CC);
307 }
308
309 void zend_fix_static_properties(zend_class_entry *ce, HashTable *static_members TSRMLS_DC)
310 {
311 zend_hash_copy(static_members, ce->static_members, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
312 zend_hash_destroy(ce->static_members);
313 zend_hash_init_ex(ce->static_members, 0, NULL, ZVAL_PTR_DTOR, 1, 0);
314 }
315
316 void zend_init_static_properties(zend_class_entry *ce, HashTable *static_members TSRMLS_DC)
317 {
318 zend_hash_copy(ce->static_members, static_members, (copy_ctor_func_t) dup_zval, NULL, sizeof(zval *));
319 }
320
321 void zend_clean_static_properties(zend_class_entry *ce TSRMLS_DC)
322 {
323 zend_hash_clean(ce->static_members);
324 }
325
326 #endif /* PHP_MAJOR_VERSION == 5 */
327
328 /*
329 * Local variables:
330 * tab-width: 4
331 * c-basic-offset: 4
332 * End:
333 * vim600: noet sw=4 ts=4 fdm=marker
334 * vim<600: noet sw=4 ts=4
335 */
336