102fb4f7540d8c023d592baad33f91176e5e330e
[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 #ifdef ZEND_ENGINE_2
22
23 static inline zval *new_zval(zend_class_entry *ce)
24 {
25 zval *z;
26 if (ce->type & ZEND_INTERNAL_CLASS) {
27 z = malloc(sizeof(zval));
28 } else {
29 ALLOC_ZVAL(z);
30 }
31 INIT_PZVAL(z);
32 return z;
33 }
34
35 static inline zval *tmp_zval(void)
36 {
37 zval *z;
38 ALLOC_ZVAL(z);
39 z->is_ref = 0;
40 z->refcount = 0;
41 return z;
42 }
43
44
45 #if (PHP_MAJOR_VERSION == 5) && (PHP_MINOR_VERSION == 0)
46 int zend_declare_property_double(zend_class_entry *ce, char *name, int name_length, double value, int access_type TSRMLS_DC)
47 {
48 zval *property = new_zval(ce);
49 ZVAL_DOUBLE(property, value);
50 return zend_declare_property(ce, name, name_length, property, access_type TSRMLS_CC);
51 }
52
53 void zend_update_property_double(zend_class_entry *scope, zval *object, char *name, int name_length, double value TSRMLS_DC)
54 {
55 zval *tmp = tmp_zval();
56 ZVAL_DOUBLE(tmp, value);
57 zend_update_property(scope, object, name, name_length, tmp TSRMLS_CC);
58 }
59
60 int zend_declare_property_bool(zend_class_entry *ce, char *name, int name_length, long value, int access_type TSRMLS_DC)
61 {
62 zval *property = new_zval(ce);
63 ZVAL_BOOL(property, value);
64 return zend_declare_property(ce, name, name_length, property, access_type TSRMLS_CC);
65 }
66
67 void zend_update_property_bool(zend_class_entry *scope, zval *object, char *name, int name_length, long value TSRMLS_DC)
68 {
69 zval *tmp = tmp_zval();
70 ZVAL_BOOL(tmp, value);
71 zend_update_property(scope, object, name, name_length, tmp TSRMLS_CC);
72 }
73
74 #endif
75
76 #if (PHP_MAJOR_VERSION >= 5)
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);
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);
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);
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);
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
121 int zend_update_static_property(zend_class_entry *scope, char *name, size_t name_len, zval *value TSRMLS_DC)
122 {
123 int retval;
124 zval **property = NULL;
125 zend_class_entry *old_scope = EG(scope);
126
127 EG(scope) = scope;
128 if (!(property = zend_std_get_static_property(scope, name, name_len, 0 TSRMLS_CC))) {
129 EG(scope) = old_scope;
130 return FAILURE;
131 }
132 EG(scope) = old_scope;
133 if (*property == value) {
134 return SUCCESS;
135 }
136
137 if (scope->type & ZEND_INTERNAL_CLASS) {
138 int refcount;
139 zend_uchar is_ref;
140
141 refcount = (*property)->refcount;
142 is_ref = (*property)->is_ref;
143
144 /* clean */
145 switch (Z_TYPE_PP(property))
146 {
147 case IS_BOOL: case IS_LONG: case IS_NULL:
148 break;
149
150 case IS_RESOURCE:
151 zend_list_delete(Z_LVAL_PP(property));
152 break;
153
154 case IS_STRING: case IS_CONSTANT:
155 free(Z_STRVAL_PP(property));
156 break;
157
158 case IS_OBJECT:
159 if (Z_OBJ_HT_PP(property)->del_ref) {
160 Z_OBJ_HT_PP(property)->del_ref(*property TSRMLS_CC);
161 }
162 break;
163
164 case IS_ARRAY: case IS_CONSTANT_ARRAY:
165 if (Z_ARRVAL_PP(property) && Z_ARRVAL_PP(property) != &EG(symbol_table)) {
166 zend_hash_destroy(Z_ARRVAL_PP(property));
167 free(Z_ARRVAL_PP(property));
168 }
169 break;
170 }
171
172 /* copy */
173 **property = *value;
174
175 /* ctor */
176 switch (Z_TYPE_PP(property))
177 {
178 case IS_BOOL: case IS_LONG: case IS_NULL:
179 break;
180
181 case IS_RESOURCE:
182 zend_list_addref(Z_LVAL_PP(property));
183 break;
184
185 case IS_STRING: case IS_CONSTANT:
186 Z_STRVAL_PP(property) = (char *) zend_strndup(Z_STRVAL_PP(property), Z_STRLEN_PP(property));
187 break;
188
189 case IS_OBJECT:
190 if (Z_OBJ_HT_PP(property)->add_ref) {
191 Z_OBJ_HT_PP(property)->add_ref(*property TSRMLS_CC);
192 }
193 break;
194
195 case IS_ARRAY: case IS_CONSTANT_ARRAY:
196 {
197 if (Z_ARRVAL_PP(property) != &EG(symbol_table)) {
198 zval *tmp;
199 HashTable *old = Z_ARRVAL_PP(property);
200
201 Z_ARRVAL_PP(property) = (HashTable *) malloc(sizeof(HashTable));
202 zend_hash_init(Z_ARRVAL_PP(property), 0, NULL, ZVAL_PTR_DTOR, 0);
203 zend_hash_copy(Z_ARRVAL_PP(property), old, (copy_ctor_func_t) zval_add_ref, (void *) &tmp, sizeof(zval *));
204 }
205 }
206 break;
207 }
208
209 (*property)->refcount = refcount;
210 (*property)->is_ref = is_ref;
211
212 retval = SUCCESS;
213
214 } else {
215 if (PZVAL_IS_REF(*property)) {
216 zval_dtor(*property);
217 (*property)->type = value->type;
218 (*property)->value = value->value;
219
220 if (value->refcount) {
221 zval_copy_ctor(*property);
222 }
223
224 retval = SUCCESS;
225 } else {
226 value->refcount++;
227 if (PZVAL_IS_REF(value)) {
228 SEPARATE_ZVAL(&value);
229 }
230
231 retval = zend_hash_update(scope->static_members, name, name_len+1, &value, sizeof(zval *), NULL);
232 }
233 }
234
235 if (!value->refcount) {
236 zval_dtor(value);
237 FREE_ZVAL(value);
238 }
239
240 return retval;
241 }
242
243 int zend_update_static_property_bool(zend_class_entry *scope, char *name, size_t name_len, zend_bool value TSRMLS_DC)
244 {
245 zval *tmp = tmp_zval();
246 ZVAL_BOOL(tmp, value);
247 return zend_update_static_property(scope, name, name_len, tmp TSRMLS_CC);
248 }
249
250 int zend_update_static_property_long(zend_class_entry *scope, char *name, size_t name_len, long value TSRMLS_DC)
251 {
252 zval *tmp = tmp_zval();
253 ZVAL_LONG(tmp, value);
254 return zend_update_static_property(scope, name, name_len, tmp TSRMLS_CC);
255 }
256
257 int zend_update_static_property_double(zend_class_entry *scope, char *name, size_t name_len, double value TSRMLS_DC)
258 {
259 zval *tmp = tmp_zval();
260 ZVAL_DOUBLE(tmp, value);
261 return zend_update_static_property(scope, name, name_len, tmp TSRMLS_CC);
262 }
263
264 int zend_update_static_property_string(zend_class_entry *scope, char *name, size_t name_len, char *value TSRMLS_DC)
265 {
266 zval *tmp = tmp_zval();
267 ZVAL_STRING(tmp, value, 1);
268 return zend_update_static_property(scope, name, name_len, tmp TSRMLS_CC);
269 }
270
271 int zend_update_static_property_stringl(zend_class_entry *scope, char *name, size_t name_len, char *value, size_t value_len TSRMLS_DC)
272 {
273 zval *tmp = tmp_zval();
274 ZVAL_STRINGL(tmp, value, value_len, 1);
275 return zend_update_static_property(scope, name, name_len, tmp TSRMLS_CC);
276 }
277
278 #endif
279 #endif
280
281 /*
282 * Local variables:
283 * tab-width: 4
284 * c-basic-offset: 4
285 * End:
286 * vim600: noet sw=4 ts=4 fdm=marker
287 * vim<600: noet sw=4 ts=4
288 */
289