/src/php-src/ext/spl/spl_observer.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | +----------------------------------------------------------------------+ |
3 | | | Copyright (c) The PHP Group | |
4 | | +----------------------------------------------------------------------+ |
5 | | | This source file is subject to version 3.01 of the PHP license, | |
6 | | | that is bundled with this package in the file LICENSE, and is | |
7 | | | available through the world-wide-web at the following url: | |
8 | | | https://www.php.net/license/3_01.txt | |
9 | | | If you did not receive a copy of the PHP license and are unable to | |
10 | | | obtain it through the world-wide-web, please send a note to | |
11 | | | license@php.net so we can mail you a copy immediately. | |
12 | | +----------------------------------------------------------------------+ |
13 | | | Authors: Marcus Boerger <helly@php.net> | |
14 | | | Etienne Kneuss <colder@php.net> | |
15 | | +----------------------------------------------------------------------+ |
16 | | */ |
17 | | |
18 | | #ifdef HAVE_CONFIG_H |
19 | | # include "config.h" |
20 | | #endif |
21 | | |
22 | | #include "php.h" |
23 | | #include "ext/standard/php_array.h" /* For PHP_COUNT_* constants */ |
24 | | #include "ext/standard/php_var.h" |
25 | | #include "zend_smart_str.h" |
26 | | #include "zend_interfaces.h" |
27 | | #include "zend_exceptions.h" |
28 | | |
29 | | #include "php_spl.h" /* For php_spl_object_hash() */ |
30 | | #include "spl_observer.h" |
31 | | #include "spl_observer_arginfo.h" |
32 | | #include "spl_iterators.h" |
33 | | #include "spl_exceptions.h" |
34 | | #include "spl_functions.h" /* For spl_set_private_debug_info_property() */ |
35 | | |
36 | | PHPAPI zend_class_entry *spl_ce_SplObserver; |
37 | | PHPAPI zend_class_entry *spl_ce_SplSubject; |
38 | | PHPAPI zend_class_entry *spl_ce_SplObjectStorage; |
39 | | PHPAPI zend_class_entry *spl_ce_MultipleIterator; |
40 | | |
41 | | static zend_object_handlers spl_handler_SplObjectStorage; |
42 | | static zend_object_handlers spl_handler_MultipleIterator; |
43 | | |
44 | | /* Bit flags for marking internal functionality overridden by SplObjectStorage subclasses. */ |
45 | 18 | #define SOS_OVERRIDDEN_READ_DIMENSION 1 |
46 | 18 | #define SOS_OVERRIDDEN_WRITE_DIMENSION 2 |
47 | 18 | #define SOS_OVERRIDDEN_UNSET_DIMENSION 4 |
48 | | |
49 | | typedef struct _spl_SplObjectStorage { /* {{{ */ |
50 | | HashTable storage; |
51 | | zend_long index; |
52 | | HashPosition pos; |
53 | | /* In SplObjectStorage, flags is a hidden implementation detail to optimize ArrayAccess handlers. |
54 | | * In MultipleIterator on a different class hierarchy, flags is a user settable value controlling iteration behavior. */ |
55 | | zend_long flags; |
56 | | zend_function *fptr_get_hash; |
57 | | zend_object std; |
58 | | } spl_SplObjectStorage; /* }}} */ |
59 | | |
60 | | /* {{{ storage is an assoc array of [zend_object*]=>[zval *obj, zval *inf] */ |
61 | | typedef struct _spl_SplObjectStorageElement { |
62 | | zend_object *obj; |
63 | | zval inf; |
64 | | } spl_SplObjectStorageElement; /* }}} */ |
65 | | |
66 | 153k | static inline spl_SplObjectStorage *spl_object_storage_from_obj(zend_object *obj) /* {{{ */ { |
67 | 153k | return (spl_SplObjectStorage*)((char*)(obj) - XtOffsetOf(spl_SplObjectStorage, std)); |
68 | 153k | } |
69 | | /* }}} */ |
70 | | |
71 | 34.3k | #define Z_SPLOBJSTORAGE_P(zv) spl_object_storage_from_obj(Z_OBJ_P((zv))) |
72 | | |
73 | | static void spl_SplObjectStorage_free_storage(zend_object *object) /* {{{ */ |
74 | 36.0k | { |
75 | 36.0k | spl_SplObjectStorage *intern = spl_object_storage_from_obj(object); |
76 | | |
77 | 36.0k | zend_object_std_dtor(&intern->std); |
78 | | |
79 | 36.0k | zend_hash_destroy(&intern->storage); |
80 | 36.0k | } /* }}} */ |
81 | | |
82 | 7.90k | static zend_result spl_object_storage_get_hash(zend_hash_key *key, spl_SplObjectStorage *intern, zend_object *obj) { |
83 | 7.90k | if (UNEXPECTED(intern->fptr_get_hash)) { |
84 | 0 | zval param; |
85 | 0 | zval rv; |
86 | 0 | ZVAL_OBJ(¶m, obj); |
87 | 0 | zend_call_method_with_1_params(&intern->std, intern->std.ce, &intern->fptr_get_hash, "getHash", &rv, ¶m); |
88 | 0 | if (UNEXPECTED(Z_ISUNDEF(rv))) { |
89 | | /* An exception has occurred */ |
90 | 0 | return FAILURE; |
91 | 0 | } else { |
92 | | /* TODO PHP 9: Remove this as this will be enforced from the return type */ |
93 | 0 | if (UNEXPECTED(Z_TYPE(rv) != IS_STRING)) { |
94 | 0 | zend_type_error("%s::getHash(): Return value must be of type string, %s returned", |
95 | 0 | ZSTR_VAL(intern->std.ce->name), zend_zval_value_name(&rv)); |
96 | 0 | zval_ptr_dtor(&rv); |
97 | 0 | return FAILURE; |
98 | 0 | } else { |
99 | 0 | key->key = Z_STR(rv); |
100 | 0 | return SUCCESS; |
101 | 0 | } |
102 | 0 | } |
103 | 7.90k | } else { |
104 | 7.90k | key->key = NULL; |
105 | 7.90k | key->h = obj->handle; |
106 | 7.90k | return SUCCESS; |
107 | 7.90k | } |
108 | 7.90k | } |
109 | | |
110 | 7.90k | static void spl_object_storage_free_hash(spl_SplObjectStorage *intern, zend_hash_key *key) { |
111 | 7.90k | if (key->key) { |
112 | 0 | zend_string_release_ex(key->key, 0); |
113 | 0 | } |
114 | 7.90k | } |
115 | | |
116 | | static void spl_object_storage_dtor(zval *element) /* {{{ */ |
117 | 8.86k | { |
118 | 8.86k | spl_SplObjectStorageElement *el = Z_PTR_P(element); |
119 | 8.86k | if (el) { |
120 | 8.86k | zend_object_release(el->obj); |
121 | 8.86k | zval_ptr_dtor(&el->inf); |
122 | 8.86k | efree(el); |
123 | 8.86k | } |
124 | 8.86k | } /* }}} */ |
125 | | |
126 | | static spl_SplObjectStorageElement* spl_object_storage_get(spl_SplObjectStorage *intern, zend_hash_key *key) /* {{{ */ |
127 | 7.90k | { |
128 | 7.90k | if (key->key) { |
129 | 0 | return zend_hash_find_ptr(&intern->storage, key->key); |
130 | 7.90k | } else { |
131 | 7.90k | return zend_hash_index_find_ptr(&intern->storage, key->h); |
132 | 7.90k | } |
133 | 7.90k | } /* }}} */ |
134 | | |
135 | | static spl_SplObjectStorageElement *spl_object_storage_create_element(zend_object *obj, zval *inf) /* {{{ */ |
136 | 8.86k | { |
137 | 8.86k | spl_SplObjectStorageElement *pelement = emalloc(sizeof(spl_SplObjectStorageElement)); |
138 | 8.86k | pelement->obj = obj; |
139 | 8.86k | GC_ADDREF(obj); |
140 | 8.86k | if (inf) { |
141 | 980 | ZVAL_COPY(&pelement->inf, inf); |
142 | 7.88k | } else { |
143 | 7.88k | ZVAL_NULL(&pelement->inf); |
144 | 7.88k | } |
145 | 8.86k | return pelement; |
146 | 8.86k | } /* }}} */ |
147 | | |
148 | | /* A faster version of spl_object_storage_attach used when neither SplObjectStorage->getHash nor SplObjectStorage->offsetSet is overridden. */ |
149 | | static spl_SplObjectStorageElement *spl_object_storage_attach_handle(spl_SplObjectStorage *intern, zend_object *obj, zval *inf) /* {{{ */ |
150 | 8.87k | { |
151 | 8.87k | uint32_t handle = obj->handle; |
152 | 8.87k | zval *entry_zv = zend_hash_index_lookup(&intern->storage, handle); |
153 | 8.87k | spl_SplObjectStorageElement *pelement; |
154 | 8.87k | ZEND_ASSERT(!(intern->flags & SOS_OVERRIDDEN_WRITE_DIMENSION)); |
155 | | |
156 | 8.87k | if (Z_TYPE_P(entry_zv) != IS_NULL) { |
157 | 8 | zval zv_inf; |
158 | 8 | ZEND_ASSERT(Z_TYPE_P(entry_zv) == IS_PTR); |
159 | 8 | pelement = Z_PTR_P(entry_zv); |
160 | 8 | ZVAL_COPY_VALUE(&zv_inf, &pelement->inf); |
161 | 8 | if (inf) { |
162 | 2 | ZVAL_COPY(&pelement->inf, inf); |
163 | 6 | } else { |
164 | 6 | ZVAL_NULL(&pelement->inf); |
165 | 6 | } |
166 | | /* Call the old value's destructor last, in case it moves the entry */ |
167 | 8 | zval_ptr_dtor(&zv_inf); |
168 | 8 | return pelement; |
169 | 8 | } |
170 | | |
171 | | /* NULL initialization necessary because `spl_object_storage_create_element` could bail out due to OOM. */ |
172 | 8.86k | ZVAL_PTR(entry_zv, NULL); |
173 | 8.86k | pelement = spl_object_storage_create_element(obj, inf); |
174 | 8.86k | Z_PTR_P(entry_zv) = pelement; |
175 | 8.86k | return pelement; |
176 | 8.87k | } /* }}} */ |
177 | | |
178 | | static spl_SplObjectStorageElement *spl_object_storage_attach(spl_SplObjectStorage *intern, zend_object *obj, zval *inf) /* {{{ */ |
179 | 8.81k | { |
180 | 8.81k | if (EXPECTED(!(intern->flags & SOS_OVERRIDDEN_WRITE_DIMENSION))) { |
181 | 8.81k | return spl_object_storage_attach_handle(intern, obj, inf); |
182 | 8.81k | } |
183 | | /* getHash or offsetSet is overridden. */ |
184 | | |
185 | 0 | spl_SplObjectStorageElement *pelement, element; |
186 | 0 | zend_hash_key key; |
187 | 0 | if (spl_object_storage_get_hash(&key, intern, obj) == FAILURE) { |
188 | 0 | return NULL; |
189 | 0 | } |
190 | | |
191 | 0 | pelement = spl_object_storage_get(intern, &key); |
192 | |
|
193 | 0 | if (pelement) { |
194 | 0 | zval zv_inf; |
195 | 0 | ZVAL_COPY_VALUE(&zv_inf, &pelement->inf); |
196 | 0 | if (inf) { |
197 | 0 | ZVAL_COPY(&pelement->inf, inf); |
198 | 0 | } else { |
199 | 0 | ZVAL_NULL(&pelement->inf); |
200 | 0 | } |
201 | 0 | spl_object_storage_free_hash(intern, &key); |
202 | | /* Call the old value's destructor last, in case it moves the entry */ |
203 | 0 | zval_ptr_dtor(&zv_inf); |
204 | 0 | return pelement; |
205 | 0 | } |
206 | | |
207 | 0 | element.obj = obj; |
208 | 0 | GC_ADDREF(obj); |
209 | 0 | if (inf) { |
210 | 0 | ZVAL_COPY(&element.inf, inf); |
211 | 0 | } else { |
212 | 0 | ZVAL_NULL(&element.inf); |
213 | 0 | } |
214 | 0 | if (key.key) { |
215 | 0 | pelement = zend_hash_update_mem(&intern->storage, key.key, &element, sizeof(spl_SplObjectStorageElement)); |
216 | 0 | } else { |
217 | 0 | pelement = zend_hash_index_update_mem(&intern->storage, key.h, &element, sizeof(spl_SplObjectStorageElement)); |
218 | 0 | } |
219 | 0 | spl_object_storage_free_hash(intern, &key); |
220 | 0 | return pelement; |
221 | 0 | } /* }}} */ |
222 | | |
223 | | static zend_result spl_object_storage_detach(spl_SplObjectStorage *intern, zend_object *obj) /* {{{ */ |
224 | 0 | { |
225 | 0 | if (EXPECTED(!(intern->flags & SOS_OVERRIDDEN_UNSET_DIMENSION))) { |
226 | 0 | return zend_hash_index_del(&intern->storage, obj->handle); |
227 | 0 | } |
228 | 0 | zend_result ret = FAILURE; |
229 | 0 | zend_hash_key key; |
230 | 0 | if (spl_object_storage_get_hash(&key, intern, obj) == FAILURE) { |
231 | 0 | return ret; |
232 | 0 | } |
233 | 0 | if (key.key) { |
234 | 0 | ret = zend_hash_del(&intern->storage, key.key); |
235 | 0 | } else { |
236 | 0 | ret = zend_hash_index_del(&intern->storage, key.h); |
237 | 0 | } |
238 | 0 | spl_object_storage_free_hash(intern, &key); |
239 | |
|
240 | 0 | return ret; |
241 | 0 | } /* }}}*/ |
242 | | |
243 | 0 | static void spl_object_storage_addall(spl_SplObjectStorage *intern, spl_SplObjectStorage *other) { /* {{{ */ |
244 | 0 | spl_SplObjectStorageElement *element; |
245 | |
|
246 | 0 | ZEND_HASH_FOREACH_PTR(&other->storage, element) { |
247 | 0 | spl_object_storage_attach(intern, element->obj, &element->inf); |
248 | 0 | } ZEND_HASH_FOREACH_END(); |
249 | |
|
250 | 0 | intern->index = 0; |
251 | 0 | } /* }}} */ |
252 | | |
253 | | #define SPL_OBJECT_STORAGE_CLASS_HAS_OVERRIDE(class_type, zstr_method) \ |
254 | 72 | (class_type->arrayaccess_funcs_ptr && class_type->arrayaccess_funcs_ptr->zstr_method) |
255 | | |
256 | | static zend_object *spl_object_storage_new_ex(zend_class_entry *class_type, zend_object *orig) /* {{{ */ |
257 | 36.0k | { |
258 | 36.0k | spl_SplObjectStorage *intern; |
259 | 36.0k | zend_class_entry *parent = class_type; |
260 | | |
261 | 36.0k | intern = zend_object_alloc(sizeof(spl_SplObjectStorage), parent); |
262 | 36.0k | intern->pos = 0; |
263 | | |
264 | 36.0k | zend_object_std_init(&intern->std, class_type); |
265 | 36.0k | object_properties_init(&intern->std, class_type); |
266 | | |
267 | 36.0k | zend_hash_init(&intern->storage, 0, NULL, spl_object_storage_dtor, 0); |
268 | | |
269 | 36.1k | while (parent) { |
270 | 36.0k | if (parent == spl_ce_SplObjectStorage) { |
271 | | /* Possible optimization: Cache these results with a map from class entry to IS_NULL/IS_PTR. |
272 | | * Or maybe just a single item with the result for the most recently loaded subclass. */ |
273 | 35.9k | if (class_type != spl_ce_SplObjectStorage) { |
274 | 18 | zend_function *get_hash = zend_hash_str_find_ptr(&class_type->function_table, "gethash", sizeof("gethash") - 1); |
275 | 18 | if (get_hash->common.scope != spl_ce_SplObjectStorage) { |
276 | 0 | intern->fptr_get_hash = get_hash; |
277 | 0 | } |
278 | 18 | if (intern->fptr_get_hash != NULL || |
279 | 18 | SPL_OBJECT_STORAGE_CLASS_HAS_OVERRIDE(class_type, zf_offsetget) || |
280 | 18 | SPL_OBJECT_STORAGE_CLASS_HAS_OVERRIDE(class_type, zf_offsetexists)) { |
281 | 18 | intern->flags |= SOS_OVERRIDDEN_READ_DIMENSION; |
282 | 18 | } |
283 | | |
284 | 18 | if (intern->fptr_get_hash != NULL || |
285 | 18 | SPL_OBJECT_STORAGE_CLASS_HAS_OVERRIDE(class_type, zf_offsetset)) { |
286 | 18 | intern->flags |= SOS_OVERRIDDEN_WRITE_DIMENSION; |
287 | 18 | } |
288 | | |
289 | 18 | if (intern->fptr_get_hash != NULL || |
290 | 18 | SPL_OBJECT_STORAGE_CLASS_HAS_OVERRIDE(class_type, zf_offsetunset)) { |
291 | 18 | intern->flags |= SOS_OVERRIDDEN_UNSET_DIMENSION; |
292 | 18 | } |
293 | 18 | } |
294 | 35.9k | break; |
295 | 35.9k | } |
296 | | |
297 | 132 | parent = parent->parent; |
298 | 132 | } |
299 | | |
300 | 36.0k | if (orig) { |
301 | 0 | spl_SplObjectStorage *other = spl_object_storage_from_obj(orig); |
302 | 0 | spl_object_storage_addall(intern, other); |
303 | 0 | } |
304 | | |
305 | 36.0k | return &intern->std; |
306 | 36.0k | } |
307 | | /* }}} */ |
308 | | |
309 | | /* {{{ spl_object_storage_clone */ |
310 | | static zend_object *spl_object_storage_clone(zend_object *old_object) |
311 | 0 | { |
312 | 0 | zend_object *new_object; |
313 | |
|
314 | 0 | new_object = spl_object_storage_new_ex(old_object->ce, old_object); |
315 | |
|
316 | 0 | zend_objects_clone_members(new_object, old_object); |
317 | |
|
318 | 0 | return new_object; |
319 | 0 | } |
320 | | /* }}} */ |
321 | | |
322 | | static inline HashTable* spl_object_storage_debug_info(zend_object *obj) /* {{{ */ |
323 | 0 | { |
324 | 0 | spl_SplObjectStorage *intern = spl_object_storage_from_obj(obj); |
325 | 0 | spl_SplObjectStorageElement *element; |
326 | 0 | HashTable *props; |
327 | 0 | zval tmp, storage; |
328 | 0 | HashTable *debug_info; |
329 | |
|
330 | 0 | props = obj->handlers->get_properties(obj); |
331 | |
|
332 | 0 | debug_info = zend_new_array(zend_hash_num_elements(props) + 1); |
333 | 0 | zend_hash_copy(debug_info, props, (copy_ctor_func_t)zval_add_ref); |
334 | |
|
335 | 0 | array_init(&storage); |
336 | |
|
337 | 0 | ZEND_HASH_FOREACH_PTR(&intern->storage, element) { |
338 | 0 | array_init(&tmp); |
339 | 0 | zval obj; |
340 | 0 | ZVAL_OBJ_COPY(&obj, element->obj); |
341 | 0 | add_assoc_zval_ex(&tmp, "obj", sizeof("obj") - 1, &obj); |
342 | 0 | Z_TRY_ADDREF(element->inf); |
343 | 0 | add_assoc_zval_ex(&tmp, "inf", sizeof("inf") - 1, &element->inf); |
344 | 0 | zend_hash_next_index_insert(Z_ARRVAL(storage), &tmp); |
345 | 0 | } ZEND_HASH_FOREACH_END(); |
346 | |
|
347 | 0 | spl_set_private_debug_info_property(spl_ce_SplObjectStorage, "storage", strlen("storage"), debug_info, &storage); |
348 | |
|
349 | 0 | return debug_info; |
350 | 0 | } |
351 | | /* }}} */ |
352 | | |
353 | | /* overridden for garbage collection */ |
354 | | static HashTable *spl_object_storage_get_gc(zend_object *obj, zval **table, int *n) /* {{{ */ |
355 | 82.5k | { |
356 | 82.5k | spl_SplObjectStorage *intern = spl_object_storage_from_obj(obj); |
357 | 82.5k | spl_SplObjectStorageElement *element; |
358 | 82.5k | zend_get_gc_buffer *gc_buffer = zend_get_gc_buffer_create(); |
359 | | |
360 | 125k | ZEND_HASH_FOREACH_PTR(&intern->storage, element) { |
361 | 125k | zend_get_gc_buffer_add_obj(gc_buffer, element->obj); |
362 | 125k | zend_get_gc_buffer_add_zval(gc_buffer, &element->inf); |
363 | 125k | } ZEND_HASH_FOREACH_END(); |
364 | | |
365 | 82.5k | zend_get_gc_buffer_use(gc_buffer, table, n); |
366 | 82.5k | return zend_std_get_properties(obj); |
367 | 82.5k | } |
368 | | /* }}} */ |
369 | | |
370 | | static int spl_object_storage_compare_info(zval *e1, zval *e2) /* {{{ */ |
371 | 0 | { |
372 | 0 | spl_SplObjectStorageElement *s1 = (spl_SplObjectStorageElement*)Z_PTR_P(e1); |
373 | 0 | spl_SplObjectStorageElement *s2 = (spl_SplObjectStorageElement*)Z_PTR_P(e2); |
374 | |
|
375 | 0 | return zend_compare(&s1->inf, &s2->inf); |
376 | 0 | } |
377 | | /* }}} */ |
378 | | |
379 | | static int spl_object_storage_compare_objects(zval *o1, zval *o2) /* {{{ */ |
380 | 0 | { |
381 | 0 | zend_object *zo1; |
382 | 0 | zend_object *zo2; |
383 | |
|
384 | 0 | ZEND_COMPARE_OBJECTS_FALLBACK(o1, o2); |
385 | |
|
386 | 0 | zo1 = (zend_object *)Z_OBJ_P(o1); |
387 | 0 | zo2 = (zend_object *)Z_OBJ_P(o2); |
388 | |
|
389 | 0 | if (zo1->ce != spl_ce_SplObjectStorage || zo2->ce != spl_ce_SplObjectStorage) { |
390 | 0 | return ZEND_UNCOMPARABLE; |
391 | 0 | } |
392 | | |
393 | 0 | return zend_hash_compare(&(Z_SPLOBJSTORAGE_P(o1))->storage, &(Z_SPLOBJSTORAGE_P(o2))->storage, (compare_func_t)spl_object_storage_compare_info, 0); |
394 | 0 | } |
395 | | /* }}} */ |
396 | | |
397 | | /* {{{ spl_array_object_new */ |
398 | | static zend_object *spl_SplObjectStorage_new(zend_class_entry *class_type) |
399 | 36.0k | { |
400 | 36.0k | return spl_object_storage_new_ex(class_type, NULL); |
401 | 36.0k | } |
402 | | /* }}} */ |
403 | | |
404 | | /* Returns true if the SplObjectStorage contains an entry for getHash(obj), even if the corresponding value is null. */ |
405 | | static bool spl_object_storage_contains(spl_SplObjectStorage *intern, zend_object *obj) /* {{{ */ |
406 | 25 | { |
407 | 25 | if (EXPECTED(!intern->fptr_get_hash)) { |
408 | 25 | return zend_hash_index_find(&intern->storage, obj->handle) != NULL; |
409 | 25 | } |
410 | 0 | zend_hash_key key; |
411 | 0 | if (spl_object_storage_get_hash(&key, intern, obj) == FAILURE) { |
412 | 0 | return true; |
413 | 0 | } |
414 | | |
415 | 0 | ZEND_ASSERT(key.key); |
416 | 0 | bool found = zend_hash_exists(&intern->storage, key.key); |
417 | 0 | zend_string_release_ex(key.key, 0); |
418 | |
|
419 | 0 | return found; |
420 | 0 | } /* }}} */ |
421 | | |
422 | | /* {{{ Attaches an object to the storage if not yet contained */ |
423 | | PHP_METHOD(SplObjectStorage, attach) |
424 | 3 | { |
425 | 3 | zend_object *obj; |
426 | 3 | zval *inf = NULL; |
427 | | |
428 | 3 | spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS); |
429 | | |
430 | 9 | ZEND_PARSE_PARAMETERS_START(1, 2) |
431 | 12 | Z_PARAM_OBJ(obj) |
432 | 0 | Z_PARAM_OPTIONAL |
433 | 0 | Z_PARAM_ZVAL(inf) |
434 | 3 | ZEND_PARSE_PARAMETERS_END(); |
435 | 0 | spl_object_storage_attach(intern, obj, inf); |
436 | 0 | } /* }}} */ |
437 | | |
438 | | // todo: make spl_object_storage_has_dimension return bool as well |
439 | | static int spl_object_storage_has_dimension(zend_object *object, zval *offset, int check_empty) |
440 | 0 | { |
441 | 0 | spl_SplObjectStorage *intern = spl_object_storage_from_obj(object); |
442 | 0 | if (UNEXPECTED(offset == NULL || Z_TYPE_P(offset) != IS_OBJECT || (intern->flags & SOS_OVERRIDDEN_READ_DIMENSION))) { |
443 | | /* Can't optimize empty()/isset() check if getHash, offsetExists, or offsetGet is overridden */ |
444 | 0 | return zend_std_has_dimension(object, offset, check_empty); |
445 | 0 | } |
446 | 0 | spl_SplObjectStorageElement *element = zend_hash_index_find_ptr(&intern->storage, Z_OBJ_HANDLE_P(offset)); |
447 | 0 | if (!element) { |
448 | 0 | return 0; |
449 | 0 | } |
450 | | |
451 | 0 | if (check_empty) { |
452 | 0 | return i_zend_is_true(&element->inf); |
453 | 0 | } |
454 | | /* NOTE: SplObjectStorage->offsetExists() is an alias of SplObjectStorage->contains(), so this returns true even if the value is null. */ |
455 | 0 | return 1; |
456 | 0 | } |
457 | | |
458 | | static zval *spl_object_storage_read_dimension(zend_object *object, zval *offset, int type, zval *rv) |
459 | 74 | { |
460 | 74 | spl_SplObjectStorage *intern = spl_object_storage_from_obj(object); |
461 | 74 | if (UNEXPECTED(offset == NULL || Z_TYPE_P(offset) != IS_OBJECT || (intern->flags & SOS_OVERRIDDEN_READ_DIMENSION))) { |
462 | | /* Can't optimize it if getHash, offsetExists, or offsetGet is overridden */ |
463 | 2 | return zend_std_read_dimension(object, offset, type, rv); |
464 | 2 | } |
465 | 72 | spl_SplObjectStorageElement *element = zend_hash_index_find_ptr(&intern->storage, Z_OBJ_HANDLE_P(offset)); |
466 | | |
467 | 72 | if (!element) { |
468 | 0 | if (type == BP_VAR_IS) { |
469 | 0 | return &EG(uninitialized_zval); |
470 | 0 | } |
471 | 0 | zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0, "Object not found"); |
472 | 0 | return NULL; |
473 | 72 | } else { |
474 | | /* This deliberately returns a non-reference, even for BP_VAR_W and BP_VAR_RW, to behave the same way as SplObjectStorage did when using the default zend_std_read_dimension behavior. |
475 | | * i.e. This prevents taking a reference to an entry of SplObjectStorage because offsetGet would return a non-reference. */ |
476 | 72 | ZVAL_COPY_DEREF(rv, &element->inf); |
477 | 72 | return rv; |
478 | 72 | } |
479 | 72 | } |
480 | | |
481 | | static void spl_object_storage_write_dimension(zend_object *object, zval *offset, zval *inf) |
482 | 65 | { |
483 | 65 | spl_SplObjectStorage *intern = spl_object_storage_from_obj(object); |
484 | 65 | if (UNEXPECTED(offset == NULL || Z_TYPE_P(offset) != IS_OBJECT || (intern->flags & SOS_OVERRIDDEN_WRITE_DIMENSION))) { |
485 | 3 | zend_std_write_dimension(object, offset, inf); |
486 | 3 | return; |
487 | 3 | } |
488 | 62 | spl_object_storage_attach_handle(intern, Z_OBJ_P(offset), inf); |
489 | 62 | } |
490 | | |
491 | | static void spl_multiple_iterator_write_dimension(zend_object *object, zval *offset, zval *inf) |
492 | 0 | { |
493 | 0 | spl_SplObjectStorage *intern = spl_object_storage_from_obj(object); |
494 | 0 | if (UNEXPECTED(offset == NULL || Z_TYPE_P(offset) != IS_OBJECT || (intern->flags & SOS_OVERRIDDEN_WRITE_DIMENSION))) { |
495 | 0 | zend_std_write_dimension(object, offset, inf); |
496 | 0 | return; |
497 | 0 | } |
498 | 0 | if (UNEXPECTED(!Z_OBJCE_P(offset)->iterator_funcs_ptr || !Z_OBJCE_P(offset)->iterator_funcs_ptr->zf_valid)) { |
499 | 0 | zend_type_error("Can only attach objects that implement the Iterator interface"); |
500 | 0 | return; |
501 | 0 | } |
502 | 0 | spl_object_storage_attach_handle(intern, Z_OBJ_P(offset), inf); |
503 | 0 | } |
504 | | |
505 | | static void spl_object_storage_unset_dimension(zend_object *object, zval *offset) |
506 | 0 | { |
507 | 0 | spl_SplObjectStorage *intern = spl_object_storage_from_obj(object); |
508 | 0 | if (UNEXPECTED(Z_TYPE_P(offset) != IS_OBJECT || (intern->flags & SOS_OVERRIDDEN_UNSET_DIMENSION))) { |
509 | 0 | zend_std_unset_dimension(object, offset); |
510 | 0 | return; |
511 | 0 | } |
512 | 0 | zend_hash_index_del(&intern->storage, Z_OBJ_HANDLE_P(offset)); |
513 | 0 | } |
514 | | |
515 | | /* {{{ Detaches an object from the storage */ |
516 | | PHP_METHOD(SplObjectStorage, detach) |
517 | 0 | { |
518 | 0 | zend_object *obj; |
519 | 0 | spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS); |
520 | |
|
521 | 0 | ZEND_PARSE_PARAMETERS_START(1, 1) |
522 | 0 | Z_PARAM_OBJ(obj) |
523 | 0 | ZEND_PARSE_PARAMETERS_END(); |
524 | 0 | spl_object_storage_detach(intern, obj); |
525 | |
|
526 | 0 | zend_hash_internal_pointer_reset_ex(&intern->storage, &intern->pos); |
527 | 0 | intern->index = 0; |
528 | 0 | } /* }}} */ |
529 | | |
530 | | /* {{{ Returns the hash of an object */ |
531 | | PHP_METHOD(SplObjectStorage, getHash) |
532 | 0 | { |
533 | 0 | zend_object *obj; |
534 | |
|
535 | 0 | ZEND_PARSE_PARAMETERS_START(1, 1) |
536 | 0 | Z_PARAM_OBJ(obj) |
537 | 0 | ZEND_PARSE_PARAMETERS_END(); |
538 | | |
539 | 0 | RETURN_NEW_STR(php_spl_object_hash(obj)); |
540 | |
|
541 | 0 | } /* }}} */ |
542 | | |
543 | | /* {{{ Returns associated information for a stored object */ |
544 | | PHP_METHOD(SplObjectStorage, offsetGet) |
545 | 2 | { |
546 | 2 | zend_object *obj; |
547 | 2 | spl_SplObjectStorageElement *element; |
548 | 2 | spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS); |
549 | 2 | zend_hash_key key; |
550 | | |
551 | 6 | ZEND_PARSE_PARAMETERS_START(1, 1) |
552 | 8 | Z_PARAM_OBJ(obj) |
553 | 2 | ZEND_PARSE_PARAMETERS_END(); |
554 | | |
555 | 0 | if (spl_object_storage_get_hash(&key, intern, obj) == FAILURE) { |
556 | 0 | RETURN_NULL(); |
557 | 0 | } |
558 | | |
559 | 0 | element = spl_object_storage_get(intern, &key); |
560 | 0 | spl_object_storage_free_hash(intern, &key); |
561 | |
|
562 | 0 | if (!element) { |
563 | 0 | zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0, "Object not found"); |
564 | 0 | } else { |
565 | 0 | RETURN_COPY_DEREF(&element->inf); |
566 | 0 | } |
567 | 0 | } /* }}} */ |
568 | | |
569 | | /* {{{ Add all elements contained in $os */ |
570 | | PHP_METHOD(SplObjectStorage, addAll) |
571 | 0 | { |
572 | 0 | zval *obj; |
573 | 0 | spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS); |
574 | 0 | spl_SplObjectStorage *other; |
575 | |
|
576 | 0 | if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &obj, spl_ce_SplObjectStorage) == FAILURE) { |
577 | 0 | RETURN_THROWS(); |
578 | 0 | } |
579 | | |
580 | 0 | other = Z_SPLOBJSTORAGE_P(obj); |
581 | |
|
582 | 0 | spl_object_storage_addall(intern, other); |
583 | |
|
584 | 0 | RETURN_LONG(zend_hash_num_elements(&intern->storage)); |
585 | 0 | } /* }}} */ |
586 | | |
587 | | /* {{{ Remove all elements contained in $os */ |
588 | | PHP_METHOD(SplObjectStorage, removeAll) |
589 | 0 | { |
590 | 0 | zval *obj; |
591 | 0 | spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS); |
592 | 0 | spl_SplObjectStorage *other; |
593 | 0 | spl_SplObjectStorageElement *element; |
594 | |
|
595 | 0 | if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &obj, spl_ce_SplObjectStorage) == FAILURE) { |
596 | 0 | RETURN_THROWS(); |
597 | 0 | } |
598 | | |
599 | 0 | other = Z_SPLOBJSTORAGE_P(obj); |
600 | |
|
601 | 0 | zend_hash_internal_pointer_reset(&other->storage); |
602 | 0 | while ((element = zend_hash_get_current_data_ptr(&other->storage)) != NULL) { |
603 | 0 | if (spl_object_storage_detach(intern, element->obj) == FAILURE) { |
604 | 0 | zend_hash_move_forward(&other->storage); |
605 | 0 | } |
606 | 0 | } |
607 | |
|
608 | 0 | zend_hash_internal_pointer_reset_ex(&intern->storage, &intern->pos); |
609 | 0 | intern->index = 0; |
610 | |
|
611 | 0 | RETURN_LONG(zend_hash_num_elements(&intern->storage)); |
612 | 0 | } /* }}} */ |
613 | | |
614 | | /* {{{ Remove elements not common to both this SplObjectStorage instance and $os */ |
615 | | PHP_METHOD(SplObjectStorage, removeAllExcept) |
616 | 0 | { |
617 | 0 | zval *obj; |
618 | 0 | spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS); |
619 | 0 | spl_SplObjectStorage *other; |
620 | 0 | spl_SplObjectStorageElement *element; |
621 | |
|
622 | 0 | if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &obj, spl_ce_SplObjectStorage) == FAILURE) { |
623 | 0 | RETURN_THROWS(); |
624 | 0 | } |
625 | | |
626 | 0 | other = Z_SPLOBJSTORAGE_P(obj); |
627 | |
|
628 | 0 | ZEND_HASH_FOREACH_PTR(&intern->storage, element) { |
629 | 0 | if (!spl_object_storage_contains(other, element->obj)) { |
630 | 0 | spl_object_storage_detach(intern, element->obj); |
631 | 0 | } |
632 | 0 | } ZEND_HASH_FOREACH_END(); |
633 | |
|
634 | 0 | zend_hash_internal_pointer_reset_ex(&intern->storage, &intern->pos); |
635 | 0 | intern->index = 0; |
636 | |
|
637 | 0 | RETURN_LONG(zend_hash_num_elements(&intern->storage)); |
638 | 0 | } |
639 | | /* }}} */ |
640 | | |
641 | | /* {{{ Determine whether an object is contained in the storage */ |
642 | | PHP_METHOD(SplObjectStorage, contains) |
643 | 25 | { |
644 | 25 | zend_object *obj; |
645 | 25 | spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS); |
646 | | |
647 | 75 | ZEND_PARSE_PARAMETERS_START(1, 1) |
648 | 100 | Z_PARAM_OBJ(obj) |
649 | 25 | ZEND_PARSE_PARAMETERS_END(); |
650 | 25 | RETURN_BOOL(spl_object_storage_contains(intern, obj)); |
651 | 25 | } /* }}} */ |
652 | | |
653 | | /* {{{ Determine number of objects in storage */ |
654 | | PHP_METHOD(SplObjectStorage, count) |
655 | 0 | { |
656 | 0 | spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS); |
657 | 0 | zend_long mode = PHP_COUNT_NORMAL; |
658 | |
|
659 | 0 | if (zend_parse_parameters(ZEND_NUM_ARGS(), "|l", &mode) == FAILURE) { |
660 | 0 | RETURN_THROWS(); |
661 | 0 | } |
662 | | |
663 | 0 | if (mode == PHP_COUNT_RECURSIVE) { |
664 | 0 | RETURN_LONG(php_count_recursive(&intern->storage)); |
665 | 0 | } |
666 | | |
667 | 0 | RETURN_LONG(zend_hash_num_elements(&intern->storage)); |
668 | 0 | } /* }}} */ |
669 | | |
670 | | /* {{{ Rewind to first position */ |
671 | | PHP_METHOD(SplObjectStorage, rewind) |
672 | 0 | { |
673 | 0 | spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS); |
674 | |
|
675 | 0 | if (zend_parse_parameters_none() == FAILURE) { |
676 | 0 | RETURN_THROWS(); |
677 | 0 | } |
678 | | |
679 | 0 | zend_hash_internal_pointer_reset_ex(&intern->storage, &intern->pos); |
680 | 0 | intern->index = 0; |
681 | 0 | } /* }}} */ |
682 | | |
683 | | /* {{{ Returns whether current position is valid */ |
684 | | PHP_METHOD(SplObjectStorage, valid) |
685 | 0 | { |
686 | 0 | spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS); |
687 | |
|
688 | 0 | if (zend_parse_parameters_none() == FAILURE) { |
689 | 0 | RETURN_THROWS(); |
690 | 0 | } |
691 | | |
692 | 0 | RETURN_BOOL(zend_hash_has_more_elements_ex(&intern->storage, &intern->pos) == SUCCESS); |
693 | 0 | } /* }}} */ |
694 | | |
695 | | /* {{{ Returns current key */ |
696 | | PHP_METHOD(SplObjectStorage, key) |
697 | 0 | { |
698 | 0 | spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS); |
699 | |
|
700 | 0 | if (zend_parse_parameters_none() == FAILURE) { |
701 | 0 | RETURN_THROWS(); |
702 | 0 | } |
703 | | |
704 | 0 | RETURN_LONG(intern->index); |
705 | 0 | } /* }}} */ |
706 | | |
707 | | /* {{{ Returns current element */ |
708 | | PHP_METHOD(SplObjectStorage, current) |
709 | 0 | { |
710 | 0 | spl_SplObjectStorageElement *element; |
711 | 0 | spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS); |
712 | |
|
713 | 0 | if (zend_parse_parameters_none() == FAILURE) { |
714 | 0 | RETURN_THROWS(); |
715 | 0 | } |
716 | | |
717 | 0 | if ((element = zend_hash_get_current_data_ptr_ex(&intern->storage, &intern->pos)) == NULL) { |
718 | 0 | zend_throw_exception(spl_ce_RuntimeException, "Called current() on invalid iterator", 0); |
719 | 0 | RETURN_THROWS(); |
720 | 0 | } |
721 | 0 | ZVAL_OBJ_COPY(return_value, element->obj); |
722 | 0 | } /* }}} */ |
723 | | |
724 | | /* {{{ Returns associated information to current element */ |
725 | | PHP_METHOD(SplObjectStorage, getInfo) |
726 | 0 | { |
727 | 0 | spl_SplObjectStorageElement *element; |
728 | 0 | spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS); |
729 | |
|
730 | 0 | if (zend_parse_parameters_none() == FAILURE) { |
731 | 0 | RETURN_THROWS(); |
732 | 0 | } |
733 | | |
734 | 0 | if ((element = zend_hash_get_current_data_ptr_ex(&intern->storage, &intern->pos)) == NULL) { |
735 | 0 | RETURN_NULL(); |
736 | 0 | } |
737 | 0 | ZVAL_COPY(return_value, &element->inf); |
738 | 0 | } /* }}} */ |
739 | | |
740 | | /* {{{ Sets associated information of current element to $inf */ |
741 | | PHP_METHOD(SplObjectStorage, setInfo) |
742 | 0 | { |
743 | 0 | spl_SplObjectStorageElement *element; |
744 | 0 | spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS); |
745 | 0 | zval *inf; |
746 | |
|
747 | 0 | if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &inf) == FAILURE) { |
748 | 0 | RETURN_THROWS(); |
749 | 0 | } |
750 | | |
751 | 0 | if ((element = zend_hash_get_current_data_ptr_ex(&intern->storage, &intern->pos)) == NULL) { |
752 | 0 | RETURN_NULL(); |
753 | 0 | } |
754 | 0 | zval garbage; |
755 | 0 | ZVAL_COPY_VALUE(&garbage, &element->inf); |
756 | 0 | ZVAL_COPY(&element->inf, inf); |
757 | 0 | zval_ptr_dtor(&garbage); |
758 | 0 | } /* }}} */ |
759 | | |
760 | | /* {{{ Moves position forward */ |
761 | | PHP_METHOD(SplObjectStorage, next) |
762 | 0 | { |
763 | 0 | spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS); |
764 | |
|
765 | 0 | if (zend_parse_parameters_none() == FAILURE) { |
766 | 0 | RETURN_THROWS(); |
767 | 0 | } |
768 | | |
769 | 0 | zend_hash_move_forward_ex(&intern->storage, &intern->pos); |
770 | 0 | intern->index++; |
771 | 0 | } /* }}} */ |
772 | | |
773 | | /* {{{ Seek to position. */ |
774 | | PHP_METHOD(SplObjectStorage, seek) |
775 | 0 | { |
776 | 0 | zend_long position; |
777 | 0 | spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS); |
778 | |
|
779 | 0 | if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &position) == FAILURE) { |
780 | 0 | RETURN_THROWS(); |
781 | 0 | } |
782 | | |
783 | 0 | if (position < 0 || position >= zend_hash_num_elements(&intern->storage)) { |
784 | 0 | zend_throw_exception_ex(spl_ce_OutOfBoundsException, 0, "Seek position " ZEND_LONG_FMT " is out of range", position); |
785 | 0 | RETURN_THROWS(); |
786 | 0 | } |
787 | | |
788 | 0 | if (position == 0) { |
789 | | /* fast path */ |
790 | 0 | zend_hash_internal_pointer_reset_ex(&intern->storage, &intern->pos); |
791 | 0 | intern->index = 0; |
792 | 0 | } else if (position > intern->index) { |
793 | | /* unlike the optimization below, it's not cheap to go to the end */ |
794 | 0 | do { |
795 | 0 | zend_hash_move_forward_ex(&intern->storage, &intern->pos); |
796 | 0 | intern->index++; |
797 | 0 | } while (position > intern->index); |
798 | 0 | } else if (position < intern->index) { |
799 | | /* optimization: check if it's more profitable to reset and do a forwards seek instead, it's cheap to reset */ |
800 | 0 | if (intern->index - position > position) { |
801 | 0 | zend_hash_internal_pointer_reset_ex(&intern->storage, &intern->pos); |
802 | 0 | intern->index = 0; |
803 | 0 | do { |
804 | 0 | zend_hash_move_forward_ex(&intern->storage, &intern->pos); |
805 | 0 | intern->index++; |
806 | 0 | } while (position > intern->index); |
807 | 0 | } else { |
808 | 0 | do { |
809 | 0 | zend_hash_move_backwards_ex(&intern->storage, &intern->pos); |
810 | 0 | intern->index--; |
811 | 0 | } while (position < intern->index); |
812 | 0 | } |
813 | 0 | } |
814 | 0 | } /* }}} */ |
815 | | |
816 | | /* {{{ Serializes storage */ |
817 | | PHP_METHOD(SplObjectStorage, serialize) |
818 | 0 | { |
819 | 0 | spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS); |
820 | |
|
821 | 0 | spl_SplObjectStorageElement *element; |
822 | 0 | zval members, flags; |
823 | 0 | HashPosition pos; |
824 | 0 | php_serialize_data_t var_hash; |
825 | 0 | smart_str buf = {0}; |
826 | |
|
827 | 0 | if (zend_parse_parameters_none() == FAILURE) { |
828 | 0 | RETURN_THROWS(); |
829 | 0 | } |
830 | | |
831 | 0 | PHP_VAR_SERIALIZE_INIT(var_hash); |
832 | | |
833 | | /* storage */ |
834 | 0 | smart_str_appendl(&buf, "x:", 2); |
835 | 0 | ZVAL_LONG(&flags, zend_hash_num_elements(&intern->storage)); |
836 | 0 | php_var_serialize(&buf, &flags, &var_hash); |
837 | |
|
838 | 0 | zend_hash_internal_pointer_reset_ex(&intern->storage, &pos); |
839 | |
|
840 | 0 | while (zend_hash_has_more_elements_ex(&intern->storage, &pos) == SUCCESS) { |
841 | 0 | zval obj; |
842 | 0 | if ((element = zend_hash_get_current_data_ptr_ex(&intern->storage, &pos)) == NULL) { |
843 | 0 | smart_str_free(&buf); |
844 | 0 | PHP_VAR_SERIALIZE_DESTROY(var_hash); |
845 | 0 | RETURN_NULL(); |
846 | 0 | } |
847 | 0 | ZVAL_OBJ(&obj, element->obj); |
848 | | |
849 | | /* Protect against modification; we need a full copy because the data may be refcounted. */ |
850 | 0 | zval inf_copy; |
851 | 0 | ZVAL_COPY(&inf_copy, &element->inf); |
852 | |
|
853 | 0 | php_var_serialize(&buf, &obj, &var_hash); |
854 | 0 | smart_str_appendc(&buf, ','); |
855 | 0 | php_var_serialize(&buf, &inf_copy, &var_hash); |
856 | 0 | smart_str_appendc(&buf, ';'); |
857 | 0 | zend_hash_move_forward_ex(&intern->storage, &pos); |
858 | |
|
859 | 0 | zval_ptr_dtor(&inf_copy); |
860 | 0 | } |
861 | | |
862 | | /* members */ |
863 | 0 | smart_str_appendl(&buf, "m:", 2); |
864 | |
|
865 | 0 | ZVAL_ARR(&members, zend_array_dup(zend_std_get_properties(Z_OBJ_P(ZEND_THIS)))); |
866 | 0 | php_var_serialize(&buf, &members, &var_hash); /* finishes the string */ |
867 | 0 | zval_ptr_dtor(&members); |
868 | | |
869 | | /* done */ |
870 | 0 | PHP_VAR_SERIALIZE_DESTROY(var_hash); |
871 | |
|
872 | 0 | RETURN_STR(smart_str_extract(&buf)); |
873 | 0 | } /* }}} */ |
874 | | |
875 | | /* {{{ Unserializes storage */ |
876 | | PHP_METHOD(SplObjectStorage, unserialize) |
877 | 4.10k | { |
878 | 4.10k | spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS); |
879 | | |
880 | 4.10k | char *buf; |
881 | 4.10k | size_t buf_len; |
882 | 4.10k | const unsigned char *p, *s; |
883 | 4.10k | php_unserialize_data_t var_hash; |
884 | 4.10k | zval *pcount, *pmembers; |
885 | 4.10k | spl_SplObjectStorageElement *element; |
886 | 4.10k | zend_long count; |
887 | | |
888 | 4.10k | if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &buf, &buf_len) == FAILURE) { |
889 | 0 | RETURN_THROWS(); |
890 | 0 | } |
891 | | |
892 | 4.10k | if (buf_len == 0) { |
893 | 1 | return; |
894 | 1 | } |
895 | | |
896 | | /* storage */ |
897 | 4.10k | s = p = (const unsigned char*)buf; |
898 | 4.10k | PHP_VAR_UNSERIALIZE_INIT(var_hash); |
899 | | |
900 | 4.10k | if (*p!= 'x' || *++p != ':') { |
901 | 2 | goto outexcept; |
902 | 2 | } |
903 | 4.09k | ++p; |
904 | | |
905 | 4.09k | pcount = var_tmp_var(&var_hash); |
906 | 4.09k | if (!php_var_unserialize(pcount, &p, s + buf_len, &var_hash) || Z_TYPE_P(pcount) != IS_LONG) { |
907 | 3 | goto outexcept; |
908 | 3 | } |
909 | | |
910 | 4.09k | --p; /* for ';' */ |
911 | 4.09k | count = Z_LVAL_P(pcount); |
912 | 4.09k | if (count < 0) { |
913 | 63 | goto outexcept; |
914 | 63 | } |
915 | | |
916 | 11.9k | while (count-- > 0) { |
917 | 8.00k | spl_SplObjectStorageElement *pelement; |
918 | 8.00k | zend_hash_key key; |
919 | 8.00k | zval *entry = var_tmp_var(&var_hash); |
920 | 8.00k | zval inf; |
921 | 8.00k | ZVAL_UNDEF(&inf); |
922 | | |
923 | 8.00k | if (*p != ';') { |
924 | 9 | goto outexcept; |
925 | 9 | } |
926 | 7.99k | ++p; |
927 | 7.99k | if(*p != 'O' && *p != 'C' && *p != 'r') { |
928 | 62 | goto outexcept; |
929 | 62 | } |
930 | | /* store reference to allow cross-references between different elements */ |
931 | 7.93k | if (!php_var_unserialize(entry, &p, s + buf_len, &var_hash)) { |
932 | 21 | goto outexcept; |
933 | 21 | } |
934 | 7.91k | if (*p == ',') { /* new version has inf */ |
935 | 28 | ++p; |
936 | 28 | if (!php_var_unserialize(&inf, &p, s + buf_len, &var_hash)) { |
937 | 4 | zval_ptr_dtor(&inf); |
938 | 4 | goto outexcept; |
939 | 4 | } |
940 | 28 | } |
941 | 7.91k | if (Z_TYPE_P(entry) != IS_OBJECT) { |
942 | 1 | zval_ptr_dtor(&inf); |
943 | 1 | goto outexcept; |
944 | 1 | } |
945 | | |
946 | 7.90k | if (spl_object_storage_get_hash(&key, intern, Z_OBJ_P(entry)) == FAILURE) { |
947 | 0 | zval_ptr_dtor(&inf); |
948 | 0 | goto outexcept; |
949 | 0 | } |
950 | 7.90k | pelement = spl_object_storage_get(intern, &key); |
951 | 7.90k | spl_object_storage_free_hash(intern, &key); |
952 | 7.90k | if (pelement) { |
953 | 8 | zval obj; |
954 | 8 | if (!Z_ISUNDEF(pelement->inf)) { |
955 | 8 | var_push_dtor(&var_hash, &pelement->inf); |
956 | 8 | } |
957 | 8 | ZVAL_OBJ(&obj, pelement->obj); |
958 | 8 | var_push_dtor(&var_hash, &obj); |
959 | 8 | } |
960 | 7.90k | element = spl_object_storage_attach(intern, Z_OBJ_P(entry), Z_ISUNDEF(inf)?NULL:&inf); |
961 | 7.90k | var_replace(&var_hash, &inf, &element->inf); |
962 | 7.90k | zval_ptr_dtor(&inf); |
963 | 7.90k | } |
964 | | |
965 | 3.93k | if (*p != ';') { |
966 | 5 | goto outexcept; |
967 | 5 | } |
968 | 3.93k | ++p; |
969 | | |
970 | | /* members */ |
971 | 3.93k | if (*p!= 'm' || *++p != ':') { |
972 | 5 | goto outexcept; |
973 | 5 | } |
974 | 3.92k | ++p; |
975 | | |
976 | 3.92k | pmembers = var_tmp_var(&var_hash); |
977 | 3.92k | if (!php_var_unserialize(pmembers, &p, s + buf_len, &var_hash) || Z_TYPE_P(pmembers) != IS_ARRAY) { |
978 | 3 | goto outexcept; |
979 | 3 | } |
980 | | |
981 | | /* copy members */ |
982 | 3.92k | object_properties_load(&intern->std, Z_ARRVAL_P(pmembers)); |
983 | | |
984 | 3.92k | PHP_VAR_UNSERIALIZE_DESTROY(var_hash); |
985 | 3.92k | return; |
986 | | |
987 | 178 | outexcept: |
988 | 178 | PHP_VAR_UNSERIALIZE_DESTROY(var_hash); |
989 | 178 | zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0, "Error at offset %zd of %zd bytes", ((char*)p - buf), buf_len); |
990 | 178 | RETURN_THROWS(); |
991 | | |
992 | 178 | } /* }}} */ |
993 | | |
994 | | /* {{{ */ |
995 | | PHP_METHOD(SplObjectStorage, __serialize) |
996 | 10 | { |
997 | 10 | spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS); |
998 | 10 | spl_SplObjectStorageElement *elem; |
999 | 10 | zval tmp; |
1000 | | |
1001 | 10 | if (zend_parse_parameters_none() == FAILURE) { |
1002 | 0 | RETURN_THROWS(); |
1003 | 0 | } |
1004 | | |
1005 | 10 | array_init(return_value); |
1006 | | |
1007 | | /* storage */ |
1008 | 10 | array_init_size(&tmp, 2 * zend_hash_num_elements(&intern->storage)); |
1009 | 70 | ZEND_HASH_FOREACH_PTR(&intern->storage, elem) { |
1010 | 70 | zval obj; |
1011 | 70 | ZVAL_OBJ_COPY(&obj, elem->obj); |
1012 | 70 | zend_hash_next_index_insert(Z_ARRVAL(tmp), &obj); |
1013 | 70 | Z_TRY_ADDREF(elem->inf); |
1014 | 70 | zend_hash_next_index_insert(Z_ARRVAL(tmp), &elem->inf); |
1015 | 70 | } ZEND_HASH_FOREACH_END(); |
1016 | 10 | zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &tmp); |
1017 | | |
1018 | | /* members */ |
1019 | 10 | ZVAL_ARR(&tmp, zend_proptable_to_symtable( |
1020 | 10 | zend_std_get_properties(&intern->std), /* always_duplicate */ 1)); |
1021 | 10 | zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &tmp); |
1022 | 10 | } /* }}} */ |
1023 | | |
1024 | | /* {{{ */ |
1025 | | PHP_METHOD(SplObjectStorage, __unserialize) |
1026 | 30.1k | { |
1027 | 30.1k | spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS); |
1028 | 30.1k | HashTable *data; |
1029 | 30.1k | zval *storage_zv, *members_zv, *key, *val; |
1030 | | |
1031 | 30.1k | if (zend_parse_parameters(ZEND_NUM_ARGS(), "h", &data) == FAILURE) { |
1032 | 0 | RETURN_THROWS(); |
1033 | 0 | } |
1034 | | |
1035 | 30.1k | storage_zv = zend_hash_index_find(data, 0); |
1036 | 30.1k | members_zv = zend_hash_index_find(data, 1); |
1037 | 30.1k | if (!storage_zv || !members_zv || |
1038 | 30.1k | Z_TYPE_P(storage_zv) != IS_ARRAY || Z_TYPE_P(members_zv) != IS_ARRAY) { |
1039 | 64 | zend_throw_exception(spl_ce_UnexpectedValueException, |
1040 | 64 | "Incomplete or ill-typed serialization data", 0); |
1041 | 64 | RETURN_THROWS(); |
1042 | 64 | } |
1043 | | |
1044 | 30.0k | if (zend_hash_num_elements(Z_ARRVAL_P(storage_zv)) % 2 != 0) { |
1045 | 4 | zend_throw_exception(spl_ce_UnexpectedValueException, "Odd number of elements", 0); |
1046 | 4 | RETURN_THROWS(); |
1047 | 4 | } |
1048 | | |
1049 | 30.0k | key = NULL; |
1050 | 33.6k | ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(storage_zv), val) { |
1051 | 33.6k | if (key) { |
1052 | 898 | if (Z_TYPE_P(key) != IS_OBJECT) { |
1053 | 2 | zend_throw_exception(spl_ce_UnexpectedValueException, "Non-object key", 0); |
1054 | 2 | RETURN_THROWS(); |
1055 | 2 | } |
1056 | | |
1057 | 896 | ZVAL_DEREF(val); |
1058 | 896 | spl_object_storage_attach(intern, Z_OBJ_P(key), val); |
1059 | 896 | key = NULL; |
1060 | 898 | } else { |
1061 | 898 | key = val; |
1062 | 898 | } |
1063 | 33.6k | } ZEND_HASH_FOREACH_END(); |
1064 | | |
1065 | 30.0k | object_properties_load(&intern->std, Z_ARRVAL_P(members_zv)); |
1066 | 30.0k | } |
1067 | | |
1068 | | /* {{{ */ |
1069 | | PHP_METHOD(SplObjectStorage, __debugInfo) |
1070 | 0 | { |
1071 | 0 | if (zend_parse_parameters_none() == FAILURE) { |
1072 | 0 | RETURN_THROWS(); |
1073 | 0 | } |
1074 | | |
1075 | 0 | RETURN_ARR(spl_object_storage_debug_info(Z_OBJ_P(ZEND_THIS))); |
1076 | 0 | } |
1077 | | /* }}} */ |
1078 | | |
1079 | 30 | #define SPL_MULTIPLE_ITERATOR_GET_ALL_CURRENT 1 |
1080 | 0 | #define SPL_MULTIPLE_ITERATOR_GET_ALL_KEY 2 |
1081 | | |
1082 | | /* {{{ Iterator that iterates over several iterators one after the other */ |
1083 | | PHP_METHOD(MultipleIterator, __construct) |
1084 | 10 | { |
1085 | 10 | spl_SplObjectStorage *intern; |
1086 | 10 | zend_long flags = MIT_NEED_ALL|MIT_KEYS_NUMERIC; |
1087 | | |
1088 | 10 | if (zend_parse_parameters(ZEND_NUM_ARGS(), "|l", &flags) == FAILURE) { |
1089 | 0 | RETURN_THROWS(); |
1090 | 0 | } |
1091 | | |
1092 | 10 | intern = Z_SPLOBJSTORAGE_P(ZEND_THIS); |
1093 | 10 | intern->flags = flags; |
1094 | 10 | } |
1095 | | /* }}} */ |
1096 | | |
1097 | | /* {{{ Return current flags */ |
1098 | | PHP_METHOD(MultipleIterator, getFlags) |
1099 | 0 | { |
1100 | 0 | spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS); |
1101 | |
|
1102 | 0 | if (zend_parse_parameters_none() == FAILURE) { |
1103 | 0 | RETURN_THROWS(); |
1104 | 0 | } |
1105 | 0 | RETURN_LONG(intern->flags); |
1106 | 0 | } |
1107 | | /* }}} */ |
1108 | | |
1109 | | /* {{{ Set flags */ |
1110 | | PHP_METHOD(MultipleIterator, setFlags) |
1111 | 0 | { |
1112 | 0 | spl_SplObjectStorage *intern; |
1113 | 0 | intern = Z_SPLOBJSTORAGE_P(ZEND_THIS); |
1114 | |
|
1115 | 0 | if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &intern->flags) == FAILURE) { |
1116 | 0 | RETURN_THROWS(); |
1117 | 0 | } |
1118 | 0 | } |
1119 | | /* }}} */ |
1120 | | |
1121 | | /* {{{ Attach a new iterator */ |
1122 | | PHP_METHOD(MultipleIterator, attachIterator) |
1123 | 10 | { |
1124 | 10 | spl_SplObjectStorage *intern; |
1125 | 10 | zend_object *iterator = NULL; |
1126 | 10 | zval zinfo; |
1127 | 10 | zend_string *info_str; |
1128 | 10 | zend_long info_long; |
1129 | 10 | bool info_is_null = 1; |
1130 | | |
1131 | 30 | ZEND_PARSE_PARAMETERS_START(1, 2) |
1132 | 40 | Z_PARAM_OBJ_OF_CLASS(iterator, zend_ce_iterator) |
1133 | 10 | Z_PARAM_OPTIONAL |
1134 | 20 | Z_PARAM_STR_OR_LONG_OR_NULL(info_str, info_long, info_is_null) |
1135 | 20 | ZEND_PARSE_PARAMETERS_END(); |
1136 | | |
1137 | 10 | intern = Z_SPLOBJSTORAGE_P(ZEND_THIS); |
1138 | | |
1139 | 10 | if (!info_is_null) { |
1140 | 0 | spl_SplObjectStorageElement *element; |
1141 | |
|
1142 | 0 | if (info_str) { |
1143 | 0 | ZVAL_STR(&zinfo, info_str); |
1144 | 0 | } else { |
1145 | 0 | ZVAL_LONG(&zinfo, info_long); |
1146 | 0 | } |
1147 | |
|
1148 | 0 | zend_hash_internal_pointer_reset_ex(&intern->storage, &intern->pos); |
1149 | 0 | while ((element = zend_hash_get_current_data_ptr_ex(&intern->storage, &intern->pos)) != NULL) { |
1150 | 0 | if (fast_is_identical_function(&zinfo, &element->inf)) { |
1151 | 0 | zend_throw_exception(spl_ce_InvalidArgumentException, "Key duplication error", 0); |
1152 | 0 | RETURN_THROWS(); |
1153 | 0 | } |
1154 | 0 | zend_hash_move_forward_ex(&intern->storage, &intern->pos); |
1155 | 0 | } |
1156 | | |
1157 | 0 | spl_object_storage_attach(intern, iterator, &zinfo); |
1158 | 10 | } else { |
1159 | 10 | spl_object_storage_attach(intern, iterator, NULL); |
1160 | 10 | } |
1161 | 10 | } |
1162 | | /* }}} */ |
1163 | | |
1164 | | /* {{{ Detaches an iterator */ |
1165 | | PHP_METHOD(MultipleIterator, detachIterator) |
1166 | 0 | { |
1167 | 0 | zval *iterator; |
1168 | 0 | spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS); |
1169 | |
|
1170 | 0 | if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &iterator, zend_ce_iterator) == FAILURE) { |
1171 | 0 | RETURN_THROWS(); |
1172 | 0 | } |
1173 | 0 | spl_object_storage_detach(intern, Z_OBJ_P(iterator)); |
1174 | |
|
1175 | 0 | zend_hash_internal_pointer_reset_ex(&intern->storage, &intern->pos); |
1176 | 0 | intern->index = 0; |
1177 | 0 | } /* }}} */ |
1178 | | |
1179 | | /* {{{ Determine whether the iterator exists */ |
1180 | | PHP_METHOD(MultipleIterator, containsIterator) |
1181 | 0 | { |
1182 | 0 | zval *iterator; |
1183 | 0 | spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS); |
1184 | |
|
1185 | 0 | if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &iterator, zend_ce_iterator) == FAILURE) { |
1186 | 0 | RETURN_THROWS(); |
1187 | 0 | } |
1188 | 0 | RETURN_BOOL(spl_object_storage_contains(intern, Z_OBJ_P(iterator))); |
1189 | 0 | } /* }}} */ |
1190 | | |
1191 | | PHP_METHOD(MultipleIterator, countIterators) |
1192 | 0 | { |
1193 | 0 | spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS); |
1194 | |
|
1195 | 0 | if (zend_parse_parameters_none() == FAILURE) { |
1196 | 0 | RETURN_THROWS(); |
1197 | 0 | } |
1198 | | |
1199 | 0 | RETURN_LONG(zend_hash_num_elements(&intern->storage)); |
1200 | 0 | } |
1201 | | |
1202 | | /* {{{ Rewind all attached iterator instances */ |
1203 | | PHP_METHOD(MultipleIterator, rewind) |
1204 | 5 | { |
1205 | 5 | spl_SplObjectStorage *intern; |
1206 | 5 | spl_SplObjectStorageElement *element; |
1207 | | |
1208 | 5 | intern = Z_SPLOBJSTORAGE_P(ZEND_THIS); |
1209 | | |
1210 | 5 | if (zend_parse_parameters_none() == FAILURE) { |
1211 | 0 | RETURN_THROWS(); |
1212 | 0 | } |
1213 | | |
1214 | 5 | zend_hash_internal_pointer_reset_ex(&intern->storage, &intern->pos); |
1215 | 15 | while ((element = zend_hash_get_current_data_ptr_ex(&intern->storage, &intern->pos)) != NULL && !EG(exception)) { |
1216 | 10 | zend_object *it = element->obj; |
1217 | 10 | zend_call_known_instance_method_with_0_params(it->ce->iterator_funcs_ptr->zf_rewind, it, NULL); |
1218 | 10 | zend_hash_move_forward_ex(&intern->storage, &intern->pos); |
1219 | 10 | } |
1220 | 5 | } |
1221 | | /* }}} */ |
1222 | | |
1223 | | /* {{{ Move all attached iterator instances forward */ |
1224 | | PHP_METHOD(MultipleIterator, next) |
1225 | 10 | { |
1226 | 10 | spl_SplObjectStorage *intern; |
1227 | 10 | spl_SplObjectStorageElement *element; |
1228 | | |
1229 | 10 | intern = Z_SPLOBJSTORAGE_P(ZEND_THIS); |
1230 | | |
1231 | 10 | if (zend_parse_parameters_none() == FAILURE) { |
1232 | 0 | RETURN_THROWS(); |
1233 | 0 | } |
1234 | | |
1235 | 10 | zend_hash_internal_pointer_reset_ex(&intern->storage, &intern->pos); |
1236 | 30 | while ((element = zend_hash_get_current_data_ptr_ex(&intern->storage, &intern->pos)) != NULL && !EG(exception)) { |
1237 | 20 | zend_object *it = element->obj; |
1238 | 20 | zend_call_known_instance_method_with_0_params(it->ce->iterator_funcs_ptr->zf_next, it, NULL); |
1239 | 20 | zend_hash_move_forward_ex(&intern->storage, &intern->pos); |
1240 | 20 | } |
1241 | 10 | } |
1242 | | /* }}} */ |
1243 | | |
1244 | | /* {{{ Return whether all or one sub iterator is valid depending on flags */ |
1245 | | PHP_METHOD(MultipleIterator, valid) |
1246 | 15 | { |
1247 | 15 | spl_SplObjectStorage *intern; |
1248 | 15 | spl_SplObjectStorageElement *element; |
1249 | 15 | zval retval; |
1250 | 15 | zend_long expect, valid; |
1251 | | |
1252 | 15 | intern = Z_SPLOBJSTORAGE_P(ZEND_THIS); |
1253 | | |
1254 | 15 | if (zend_parse_parameters_none() == FAILURE) { |
1255 | 0 | RETURN_THROWS(); |
1256 | 0 | } |
1257 | | |
1258 | 15 | if (!zend_hash_num_elements(&intern->storage)) { |
1259 | 0 | RETURN_FALSE; |
1260 | 0 | } |
1261 | | |
1262 | 15 | expect = (intern->flags & MIT_NEED_ALL) ? 1 : 0; |
1263 | | |
1264 | 15 | zend_hash_internal_pointer_reset_ex(&intern->storage, &intern->pos); |
1265 | 35 | while ((element = zend_hash_get_current_data_ptr_ex(&intern->storage, &intern->pos)) != NULL && !EG(exception)) { |
1266 | 25 | zend_object *it = element->obj; |
1267 | 25 | zend_call_known_instance_method_with_0_params(it->ce->iterator_funcs_ptr->zf_valid, it, &retval); |
1268 | | |
1269 | 25 | if (!Z_ISUNDEF(retval)) { |
1270 | 25 | valid = (Z_TYPE(retval) == IS_TRUE); |
1271 | 25 | zval_ptr_dtor(&retval); |
1272 | 25 | } else { |
1273 | 0 | valid = 0; |
1274 | 0 | } |
1275 | | |
1276 | 25 | if (expect != valid) { |
1277 | 5 | RETURN_BOOL(!expect); |
1278 | 5 | } |
1279 | | |
1280 | 20 | zend_hash_move_forward_ex(&intern->storage, &intern->pos); |
1281 | 20 | } |
1282 | | |
1283 | 10 | RETURN_BOOL(expect); |
1284 | 10 | } |
1285 | | /* }}} */ |
1286 | | |
1287 | | static void spl_multiple_iterator_get_all(spl_SplObjectStorage *intern, int get_type, zval *return_value) /* {{{ */ |
1288 | 10 | { |
1289 | 10 | spl_SplObjectStorageElement *element; |
1290 | 10 | zval retval; |
1291 | 10 | int valid = 1, num_elements; |
1292 | | |
1293 | 10 | num_elements = zend_hash_num_elements(&intern->storage); |
1294 | 10 | if (num_elements < 1) { |
1295 | 0 | zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Called %s() on an invalid iterator", |
1296 | 0 | get_type == SPL_MULTIPLE_ITERATOR_GET_ALL_CURRENT ? "current" : "key"); |
1297 | 0 | RETURN_THROWS(); |
1298 | 0 | } |
1299 | | |
1300 | 10 | array_init_size(return_value, num_elements); |
1301 | | |
1302 | 10 | zend_hash_internal_pointer_reset_ex(&intern->storage, &intern->pos); |
1303 | 30 | while ((element = zend_hash_get_current_data_ptr_ex(&intern->storage, &intern->pos)) != NULL && !EG(exception)) { |
1304 | 20 | zend_object *it = element->obj; |
1305 | 20 | zend_call_known_instance_method_with_0_params(it->ce->iterator_funcs_ptr->zf_valid, it, &retval); |
1306 | | |
1307 | 20 | if (!Z_ISUNDEF(retval)) { |
1308 | 20 | valid = Z_TYPE(retval) == IS_TRUE; |
1309 | 20 | zval_ptr_dtor(&retval); |
1310 | 20 | } else { |
1311 | 0 | valid = 0; |
1312 | 0 | } |
1313 | | |
1314 | 20 | if (valid) { |
1315 | 20 | if (SPL_MULTIPLE_ITERATOR_GET_ALL_CURRENT == get_type) { |
1316 | 20 | zend_call_known_instance_method_with_0_params(it->ce->iterator_funcs_ptr->zf_current, it, &retval); |
1317 | 20 | } else { |
1318 | 0 | zend_call_known_instance_method_with_0_params(it->ce->iterator_funcs_ptr->zf_key, it, &retval); |
1319 | 0 | } |
1320 | 20 | if (Z_ISUNDEF(retval)) { |
1321 | 0 | zend_throw_exception(spl_ce_RuntimeException, "Failed to call sub iterator method", 0); |
1322 | 0 | return; |
1323 | 0 | } |
1324 | 20 | } else if (intern->flags & MIT_NEED_ALL) { |
1325 | 0 | if (SPL_MULTIPLE_ITERATOR_GET_ALL_CURRENT == get_type) { |
1326 | 0 | zend_throw_exception(spl_ce_RuntimeException, "Called current() with non valid sub iterator", 0); |
1327 | 0 | } else { |
1328 | 0 | zend_throw_exception(spl_ce_RuntimeException, "Called key() with non valid sub iterator", 0); |
1329 | 0 | } |
1330 | 0 | return; |
1331 | 0 | } else { |
1332 | 0 | ZVAL_NULL(&retval); |
1333 | 0 | } |
1334 | | |
1335 | 20 | if (intern->flags & MIT_KEYS_ASSOC) { |
1336 | 0 | switch (Z_TYPE(element->inf)) { |
1337 | 0 | case IS_LONG: |
1338 | 0 | add_index_zval(return_value, Z_LVAL(element->inf), &retval); |
1339 | 0 | break; |
1340 | 0 | case IS_STRING: |
1341 | 0 | zend_symtable_update(Z_ARRVAL_P(return_value), Z_STR(element->inf), &retval); |
1342 | 0 | break; |
1343 | 0 | default: |
1344 | 0 | zval_ptr_dtor(&retval); |
1345 | 0 | zend_throw_exception(spl_ce_InvalidArgumentException, "Sub-Iterator is associated with NULL", 0); |
1346 | 0 | return; |
1347 | 0 | } |
1348 | 20 | } else { |
1349 | 20 | add_next_index_zval(return_value, &retval); |
1350 | 20 | } |
1351 | | |
1352 | 20 | zend_hash_move_forward_ex(&intern->storage, &intern->pos); |
1353 | 20 | } |
1354 | 10 | } |
1355 | | /* }}} */ |
1356 | | |
1357 | | /* {{{ Return an array of all registered Iterator instances current() result */ |
1358 | | PHP_METHOD(MultipleIterator, current) |
1359 | 10 | { |
1360 | 10 | spl_SplObjectStorage *intern; |
1361 | 10 | intern = Z_SPLOBJSTORAGE_P(ZEND_THIS); |
1362 | | |
1363 | 10 | if (zend_parse_parameters_none() == FAILURE) { |
1364 | 0 | RETURN_THROWS(); |
1365 | 0 | } |
1366 | | |
1367 | 10 | spl_multiple_iterator_get_all(intern, SPL_MULTIPLE_ITERATOR_GET_ALL_CURRENT, return_value); |
1368 | 10 | } |
1369 | | /* }}} */ |
1370 | | |
1371 | | /* {{{ Return an array of all registered Iterator instances key() result */ |
1372 | | PHP_METHOD(MultipleIterator, key) |
1373 | 0 | { |
1374 | 0 | spl_SplObjectStorage *intern; |
1375 | 0 | intern = Z_SPLOBJSTORAGE_P(ZEND_THIS); |
1376 | |
|
1377 | 0 | if (zend_parse_parameters_none() == FAILURE) { |
1378 | 0 | RETURN_THROWS(); |
1379 | 0 | } |
1380 | | |
1381 | 0 | spl_multiple_iterator_get_all(intern, SPL_MULTIPLE_ITERATOR_GET_ALL_KEY, return_value); |
1382 | 0 | } |
1383 | | /* }}} */ |
1384 | | |
1385 | | /* {{{ PHP_MINIT_FUNCTION(spl_observer) */ |
1386 | | PHP_MINIT_FUNCTION(spl_observer) |
1387 | 16 | { |
1388 | 16 | spl_ce_SplObserver = register_class_SplObserver(); |
1389 | 16 | spl_ce_SplSubject = register_class_SplSubject(); |
1390 | | |
1391 | 16 | spl_ce_SplObjectStorage = register_class_SplObjectStorage(zend_ce_countable, spl_ce_SeekableIterator, zend_ce_serializable, zend_ce_arrayaccess); |
1392 | 16 | spl_ce_SplObjectStorage->create_object = spl_SplObjectStorage_new; |
1393 | 16 | spl_ce_SplObjectStorage->default_object_handlers = &spl_handler_SplObjectStorage; |
1394 | | |
1395 | 16 | memcpy(&spl_handler_SplObjectStorage, &std_object_handlers, sizeof(zend_object_handlers)); |
1396 | | |
1397 | 16 | spl_handler_SplObjectStorage.offset = XtOffsetOf(spl_SplObjectStorage, std); |
1398 | 16 | spl_handler_SplObjectStorage.compare = spl_object_storage_compare_objects; |
1399 | 16 | spl_handler_SplObjectStorage.clone_obj = spl_object_storage_clone; |
1400 | 16 | spl_handler_SplObjectStorage.get_gc = spl_object_storage_get_gc; |
1401 | 16 | spl_handler_SplObjectStorage.free_obj = spl_SplObjectStorage_free_storage; |
1402 | 16 | spl_handler_SplObjectStorage.read_dimension = spl_object_storage_read_dimension; |
1403 | 16 | spl_handler_SplObjectStorage.write_dimension = spl_object_storage_write_dimension; |
1404 | 16 | spl_handler_SplObjectStorage.has_dimension = spl_object_storage_has_dimension; |
1405 | 16 | spl_handler_SplObjectStorage.unset_dimension = spl_object_storage_unset_dimension; |
1406 | | |
1407 | 16 | memcpy(&spl_handler_MultipleIterator, &spl_handler_SplObjectStorage, sizeof(zend_object_handlers)); |
1408 | | |
1409 | 16 | spl_handler_MultipleIterator.write_dimension = spl_multiple_iterator_write_dimension; |
1410 | | |
1411 | 16 | spl_ce_MultipleIterator = register_class_MultipleIterator(zend_ce_iterator); |
1412 | 16 | spl_ce_MultipleIterator->create_object = spl_SplObjectStorage_new; |
1413 | 16 | spl_ce_MultipleIterator->default_object_handlers = &spl_handler_MultipleIterator; |
1414 | | |
1415 | 16 | return SUCCESS; |
1416 | 16 | } |
1417 | | /* }}} */ |