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 +----------------------------------------------------------------------+
15 */
16
17 #ifdef HAVE_CONFIG_H
18 # include "config.h"
19 #endif
20
21 #include "php.h"
22 #include "ext/standard/php_var.h"
23 #include "zend_smart_str.h"
24 #include "zend_interfaces.h"
25 #include "zend_exceptions.h"
26
27 #include "spl_iterators.h"
28 #include "spl_array.h"
29 #include "spl_array_arginfo.h"
30 #include "spl_exceptions.h"
31 #include "spl_functions.h" /* For spl_set_private_debug_info_property() */
32
33 /* Defined later in the file */
34 PHPAPI zend_class_entry *spl_ce_ArrayIterator;
35 PHPAPI zend_class_entry *spl_ce_RecursiveArrayIterator;
36
37 /* ArrayObject class */
38 static zend_object_handlers spl_handler_ArrayObject;
39 PHPAPI zend_class_entry *spl_ce_ArrayObject;
40
41 typedef struct _spl_array_object {
42 zval array;
43 uint32_t ht_iter;
44 int ar_flags;
45 unsigned char nApplyCount;
46 bool is_child;
47 Bucket *bucket;
48 zend_function *fptr_offset_get;
49 zend_function *fptr_offset_set;
50 zend_function *fptr_offset_has;
51 zend_function *fptr_offset_del;
52 zend_function *fptr_count;
53 zend_class_entry* ce_get_iterator;
54 zend_object std;
55 } spl_array_object;
56
spl_array_from_obj(zend_object * obj)57 static inline spl_array_object *spl_array_from_obj(zend_object *obj) /* {{{ */ {
58 return (spl_array_object*)((char*)(obj) - XtOffsetOf(spl_array_object, std));
59 }
60 /* }}} */
61
62 #define Z_SPLARRAY_P(zv) spl_array_from_obj(Z_OBJ_P((zv)))
63
spl_array_get_hash_table_ptr(spl_array_object * intern)64 static inline HashTable **spl_array_get_hash_table_ptr(spl_array_object* intern) { /* {{{ */
65 //??? TODO: Delay duplication for arrays; only duplicate for write operations
66 if (intern->ar_flags & SPL_ARRAY_IS_SELF) {
67 /* rebuild properties */
68 zend_std_get_properties_ex(&intern->std);
69 return &intern->std.properties;
70 } else if (intern->ar_flags & SPL_ARRAY_USE_OTHER) {
71 spl_array_object *other = Z_SPLARRAY_P(&intern->array);
72 return spl_array_get_hash_table_ptr(other);
73 } else if (Z_TYPE(intern->array) == IS_ARRAY) {
74 return &Z_ARRVAL(intern->array);
75 } else {
76 zend_object *obj = Z_OBJ(intern->array);
77 /* rebuild properties */
78 zend_std_get_properties_ex(obj);
79 if (GC_REFCOUNT(obj->properties) > 1) {
80 if (EXPECTED(!(GC_FLAGS(obj->properties) & IS_ARRAY_IMMUTABLE))) {
81 GC_DELREF(obj->properties);
82 }
83 obj->properties = zend_array_dup(obj->properties);
84 }
85 return &obj->properties;
86 }
87 }
88 /* }}} */
89
spl_array_get_hash_table(spl_array_object * intern)90 static inline HashTable *spl_array_get_hash_table(spl_array_object* intern) { /* {{{ */
91 return *spl_array_get_hash_table_ptr(intern);
92 }
93 /* }}} */
94
spl_array_is_object(spl_array_object * intern)95 static inline bool spl_array_is_object(spl_array_object *intern) /* {{{ */
96 {
97 while (intern->ar_flags & SPL_ARRAY_USE_OTHER) {
98 intern = Z_SPLARRAY_P(&intern->array);
99 }
100 return (intern->ar_flags & SPL_ARRAY_IS_SELF) || Z_TYPE(intern->array) == IS_OBJECT;
101 }
102 /* }}} */
103
104 static zend_result spl_array_skip_protected(spl_array_object *intern, HashTable *aht);
105
spl_array_create_ht_iter(HashTable * ht,spl_array_object * intern)106 static zend_never_inline void spl_array_create_ht_iter(HashTable *ht, spl_array_object* intern) /* {{{ */
107 {
108 intern->ht_iter = zend_hash_iterator_add(ht, zend_hash_get_current_pos(ht));
109 zend_hash_internal_pointer_reset_ex(ht, &EG(ht_iterators)[intern->ht_iter].pos);
110 spl_array_skip_protected(intern, ht);
111 }
112 /* }}} */
113
spl_array_get_pos_ptr(HashTable * ht,spl_array_object * intern)114 static zend_always_inline uint32_t *spl_array_get_pos_ptr(HashTable *ht, spl_array_object* intern) /* {{{ */
115 {
116 if (UNEXPECTED(intern->ht_iter == (uint32_t)-1)) {
117 spl_array_create_ht_iter(ht, intern);
118 }
119 return &EG(ht_iterators)[intern->ht_iter].pos;
120 }
121 /* }}} */
122
123 /* {{{ spl_array_object_free_storage */
spl_array_object_free_storage(zend_object * object)124 static void spl_array_object_free_storage(zend_object *object)
125 {
126 spl_array_object *intern = spl_array_from_obj(object);
127
128 if (intern->ht_iter != (uint32_t) -1) {
129 zend_hash_iterator_del(intern->ht_iter);
130 }
131
132 zend_object_std_dtor(&intern->std);
133
134 zval_ptr_dtor(&intern->array);
135 }
136 /* }}} */
137
138 /* {{{ spl_array_object_new_ex */
spl_array_object_new_ex(zend_class_entry * class_type,zend_object * orig,int clone_orig)139 static zend_object *spl_array_object_new_ex(zend_class_entry *class_type, zend_object *orig, int clone_orig)
140 {
141 spl_array_object *intern;
142 zend_class_entry *parent = class_type;
143 int inherited = 0;
144
145 intern = zend_object_alloc(sizeof(spl_array_object), parent);
146
147 zend_object_std_init(&intern->std, class_type);
148 object_properties_init(&intern->std, class_type);
149
150 intern->ar_flags = 0;
151 intern->is_child = false;
152 intern->bucket = NULL;
153 intern->ce_get_iterator = spl_ce_ArrayIterator;
154 if (orig) {
155 spl_array_object *other = spl_array_from_obj(orig);
156
157 intern->ar_flags &= ~ SPL_ARRAY_CLONE_MASK;
158 intern->ar_flags |= (other->ar_flags & SPL_ARRAY_CLONE_MASK);
159 intern->ce_get_iterator = other->ce_get_iterator;
160 if (clone_orig) {
161 if (other->ar_flags & SPL_ARRAY_IS_SELF) {
162 ZVAL_UNDEF(&intern->array);
163 } else if (instanceof_function(class_type, spl_ce_ArrayObject)) {
164 ZVAL_ARR(&intern->array,
165 zend_array_dup(spl_array_get_hash_table(other)));
166 } else {
167 #if ZEND_DEBUG
168 /* This is because the call to instanceof_function will remain because
169 * the compiler can't prove in this compile unit that this function is
170 * side-effect-free.
171 * See https://github.com/php/php-src/pull/14518#discussion_r1638740932 */
172 ZEND_ASSERT(instanceof_function(class_type, spl_ce_ArrayIterator));
173 #endif
174
175 ZVAL_OBJ_COPY(&intern->array, orig);
176 intern->ar_flags |= SPL_ARRAY_USE_OTHER;
177 }
178 } else {
179 ZVAL_OBJ_COPY(&intern->array, orig);
180 intern->ar_flags |= SPL_ARRAY_USE_OTHER;
181 }
182 } else {
183 array_init(&intern->array);
184 }
185
186 while (parent) {
187 if (parent == spl_ce_ArrayIterator || parent == spl_ce_RecursiveArrayIterator || parent == spl_ce_ArrayObject) {
188 break;
189 }
190 parent = parent->parent;
191 inherited = 1;
192 }
193
194 ZEND_ASSERT(parent);
195
196 if (inherited) {
197 intern->fptr_offset_get = zend_hash_str_find_ptr(&class_type->function_table, "offsetget", sizeof("offsetget") - 1);
198 if (intern->fptr_offset_get->common.scope == parent) {
199 intern->fptr_offset_get = NULL;
200 }
201 intern->fptr_offset_set = zend_hash_str_find_ptr(&class_type->function_table, "offsetset", sizeof("offsetset") - 1);
202 if (intern->fptr_offset_set->common.scope == parent) {
203 intern->fptr_offset_set = NULL;
204 }
205 intern->fptr_offset_has = zend_hash_str_find_ptr(&class_type->function_table, "offsetexists", sizeof("offsetexists") - 1);
206 if (intern->fptr_offset_has->common.scope == parent) {
207 intern->fptr_offset_has = NULL;
208 }
209 intern->fptr_offset_del = zend_hash_str_find_ptr(&class_type->function_table, "offsetunset", sizeof("offsetunset") - 1);
210 if (intern->fptr_offset_del->common.scope == parent) {
211 intern->fptr_offset_del = NULL;
212 }
213 /* Find count() method */
214 intern->fptr_count = zend_hash_find_ptr(&class_type->function_table, ZSTR_KNOWN(ZEND_STR_COUNT));
215 if (intern->fptr_count->common.scope == parent) {
216 intern->fptr_count = NULL;
217 }
218 }
219
220 intern->ht_iter = (uint32_t)-1;
221 return &intern->std;
222 }
223 /* }}} */
224
225 /* {{{ spl_array_object_new */
spl_array_object_new(zend_class_entry * class_type)226 static zend_object *spl_array_object_new(zend_class_entry *class_type)
227 {
228 return spl_array_object_new_ex(class_type, NULL, 0);
229 }
230 /* }}} */
231
232 /* {{{ spl_array_object_clone */
spl_array_object_clone(zend_object * old_object)233 static zend_object *spl_array_object_clone(zend_object *old_object)
234 {
235 zend_object *new_object;
236
237 new_object = spl_array_object_new_ex(old_object->ce, old_object, 1);
238
239 zend_objects_clone_members(new_object, old_object);
240
241 return new_object;
242 }
243 /* }}} */
244
245 typedef struct {
246 zend_string *key;
247 zend_ulong h;
248 bool release_key;
249 } spl_hash_key;
250
spl_hash_key_release(spl_hash_key * key)251 static void spl_hash_key_release(spl_hash_key *key) {
252 if (key->release_key) {
253 zend_string_release_ex(key->key, 0);
254 }
255 }
256
257 /* This function does not throw any exceptions for illegal offsets, calls to
258 * zend_illegal_container_offset(); need to be made if the return value is FAILURE */
get_hash_key(spl_hash_key * key,spl_array_object * intern,zval * offset)259 static zend_result get_hash_key(spl_hash_key *key, spl_array_object *intern, zval *offset)
260 {
261 key->release_key = false;
262 try_again:
263 switch (Z_TYPE_P(offset)) {
264 case IS_NULL:
265 key->key = ZSTR_EMPTY_ALLOC();
266 return SUCCESS;
267 case IS_STRING:
268 key->key = Z_STR_P(offset);
269 if (ZEND_HANDLE_NUMERIC(key->key, key->h)) {
270 key->key = NULL;
271 break;
272 }
273 return SUCCESS;
274 case IS_RESOURCE:
275 zend_use_resource_as_offset(offset);
276 key->key = NULL;
277 key->h = Z_RES_P(offset)->handle;
278 break;
279 case IS_DOUBLE:
280 key->key = NULL;
281 key->h = zend_dval_to_lval_safe(Z_DVAL_P(offset));
282 break;
283 case IS_FALSE:
284 key->key = NULL;
285 key->h = 0;
286 break;
287 case IS_TRUE:
288 key->key = NULL;
289 key->h = 1;
290 break;
291 case IS_LONG:
292 key->key = NULL;
293 key->h = Z_LVAL_P(offset);
294 break;
295 case IS_REFERENCE:
296 ZVAL_DEREF(offset);
297 goto try_again;
298 default:
299 return FAILURE;
300 }
301
302 if (spl_array_is_object(intern)) {
303 key->key = zend_long_to_str(key->h);
304 key->release_key = true;
305 }
306 return SUCCESS;
307 }
308
spl_array_get_dimension_ptr(bool check_inherited,spl_array_object * intern,const zend_string * ce_name,zval * offset,int type)309 static zval *spl_array_get_dimension_ptr(bool check_inherited, spl_array_object *intern, const zend_string *ce_name,
310 zval *offset, int type) /* {{{ */
311 {
312 zval *retval;
313 spl_hash_key key;
314 HashTable *ht = spl_array_get_hash_table(intern);
315
316 if (!offset || Z_ISUNDEF_P(offset) || !ht) {
317 return &EG(uninitialized_zval);
318 }
319
320 if ((type == BP_VAR_W || type == BP_VAR_RW) && intern->nApplyCount > 0) {
321 zend_throw_error(NULL, "Modification of ArrayObject during sorting is prohibited");
322 return &EG(error_zval);
323 }
324
325 if (get_hash_key(&key, intern, offset) == FAILURE) {
326 zend_illegal_container_offset(ce_name, offset, type);
327 return (type == BP_VAR_W || type == BP_VAR_RW) ?
328 &EG(error_zval) : &EG(uninitialized_zval);
329 }
330
331 if (key.key) {
332 retval = zend_hash_find(ht, key.key);
333 if (retval) {
334 if (Z_TYPE_P(retval) == IS_INDIRECT) {
335 retval = Z_INDIRECT_P(retval);
336 if (Z_TYPE_P(retval) == IS_UNDEF) {
337 switch (type) {
338 case BP_VAR_R:
339 zend_error(E_WARNING, "Undefined array key \"%s\"", ZSTR_VAL(key.key));
340 ZEND_FALLTHROUGH;
341 case BP_VAR_UNSET:
342 case BP_VAR_IS:
343 retval = &EG(uninitialized_zval);
344 break;
345 case BP_VAR_RW:
346 zend_error(E_WARNING,"Undefined array key \"%s\"", ZSTR_VAL(key.key));
347 ZEND_FALLTHROUGH;
348 case BP_VAR_W: {
349 ZVAL_NULL(retval);
350 }
351 }
352 }
353 }
354 } else {
355 switch (type) {
356 case BP_VAR_R:
357 zend_error(E_WARNING, "Undefined array key \"%s\"", ZSTR_VAL(key.key));
358 ZEND_FALLTHROUGH;
359 case BP_VAR_UNSET:
360 case BP_VAR_IS:
361 retval = &EG(uninitialized_zval);
362 break;
363 case BP_VAR_RW:
364 zend_error(E_WARNING,"Undefined array key \"%s\"", ZSTR_VAL(key.key));
365 ZEND_FALLTHROUGH;
366 case BP_VAR_W: {
367 zval value;
368 ZVAL_NULL(&value);
369 retval = zend_hash_update(ht, key.key, &value);
370 }
371 }
372 }
373 spl_hash_key_release(&key);
374 } else {
375 if ((retval = zend_hash_index_find(ht, key.h)) == NULL) {
376 switch (type) {
377 case BP_VAR_R:
378 zend_error(E_WARNING, "Undefined array key " ZEND_LONG_FMT, key.h);
379 ZEND_FALLTHROUGH;
380 case BP_VAR_UNSET:
381 case BP_VAR_IS:
382 retval = &EG(uninitialized_zval);
383 break;
384 case BP_VAR_RW:
385 zend_error(E_WARNING, "Undefined array key " ZEND_LONG_FMT, key.h);
386 ZEND_FALLTHROUGH;
387 case BP_VAR_W: {
388 zval value;
389 ZVAL_NULL(&value);
390 retval = zend_hash_index_update(ht, key.h, &value);
391 }
392 }
393 }
394 }
395 return retval;
396 } /* }}} */
397
398 static int spl_array_has_dimension(zend_object *object, zval *offset, int check_empty);
399
spl_array_read_dimension_ex(int check_inherited,zend_object * object,zval * offset,int type,zval * rv)400 static zval *spl_array_read_dimension_ex(int check_inherited, zend_object *object, zval *offset, int type, zval *rv) /* {{{ */
401 {
402 spl_array_object *intern = spl_array_from_obj(object);
403 zval *ret;
404
405 if (check_inherited &&
406 (intern->fptr_offset_get || (type == BP_VAR_IS && intern->fptr_offset_has))) {
407 if (type == BP_VAR_IS) {
408 if (!spl_array_has_dimension(object, offset, 0)) {
409 return &EG(uninitialized_zval);
410 }
411 }
412
413 if (intern->fptr_offset_get) {
414 zval tmp;
415 if (!offset) {
416 ZVAL_UNDEF(&tmp);
417 offset = &tmp;
418 }
419 zend_call_method_with_1_params(object, object->ce, &intern->fptr_offset_get, "offsetGet", rv, offset);
420
421 if (!Z_ISUNDEF_P(rv)) {
422 return rv;
423 }
424 return &EG(uninitialized_zval);
425 }
426 }
427
428 ret = spl_array_get_dimension_ptr(check_inherited, intern, object->ce->name, offset, type);
429
430 /* When in a write context,
431 * ZE has to be fooled into thinking this is in a reference set
432 * by separating (if necessary) and returning as IS_REFERENCE (with refcount == 1)
433 */
434
435 if ((type == BP_VAR_W || type == BP_VAR_RW || type == BP_VAR_UNSET) &&
436 !Z_ISREF_P(ret) &&
437 EXPECTED(ret != &EG(uninitialized_zval))) {
438 ZVAL_NEW_REF(ret, ret);
439 }
440
441 return ret;
442 } /* }}} */
443
spl_array_read_dimension(zend_object * object,zval * offset,int type,zval * rv)444 static zval *spl_array_read_dimension(zend_object *object, zval *offset, int type, zval *rv) /* {{{ */
445 {
446 return spl_array_read_dimension_ex(1, object, offset, type, rv);
447 } /* }}} */
448
449 /*
450 * The assertion(HT_ASSERT_RC1(ht)) failed because the refcount was increased manually when intern->is_child is true.
451 * We have to set the refcount to 1 to make assertion success and restore the refcount to the original value after
452 * modifying the array when intern->is_child is true.
453 */
spl_array_set_refcount(bool is_child,HashTable * ht,uint32_t refcount)454 static uint32_t spl_array_set_refcount(bool is_child, HashTable *ht, uint32_t refcount) /* {{{ */
455 {
456 uint32_t old_refcount = 0;
457 if (is_child) {
458 old_refcount = GC_REFCOUNT(ht);
459 GC_SET_REFCOUNT(ht, refcount);
460 }
461
462 return old_refcount;
463 } /* }}} */
464
spl_array_write_dimension_ex(int check_inherited,zend_object * object,zval * offset,zval * value)465 static void spl_array_write_dimension_ex(int check_inherited, zend_object *object, zval *offset, zval *value) /* {{{ */
466 {
467 spl_array_object *intern = spl_array_from_obj(object);
468 HashTable *ht;
469 spl_hash_key key;
470
471 if (check_inherited && intern->fptr_offset_set) {
472 zval tmp;
473
474 if (!offset) {
475 ZVAL_NULL(&tmp);
476 offset = &tmp;
477 }
478 zend_call_method_with_2_params(object, object->ce, &intern->fptr_offset_set, "offsetSet", NULL, offset, value);
479 return;
480 }
481
482 if (intern->nApplyCount > 0) {
483 zend_throw_error(NULL, "Modification of ArrayObject during sorting is prohibited");
484 return;
485 }
486
487 Z_TRY_ADDREF_P(value);
488
489 uint32_t refcount = 0;
490 if (!offset || Z_TYPE_P(offset) == IS_NULL) {
491 ht = spl_array_get_hash_table(intern);
492 refcount = spl_array_set_refcount(intern->is_child, ht, 1);
493 zend_hash_next_index_insert(ht, value);
494
495 if (refcount) {
496 spl_array_set_refcount(intern->is_child, ht, refcount);
497 }
498 return;
499 }
500
501 if (get_hash_key(&key, intern, offset) == FAILURE) {
502 zend_illegal_container_offset(object->ce->name, offset, BP_VAR_W);
503 zval_ptr_dtor(value);
504 return;
505 }
506
507 ht = spl_array_get_hash_table(intern);
508 refcount = spl_array_set_refcount(intern->is_child, ht, 1);
509 if (key.key) {
510 zend_hash_update_ind(ht, key.key, value);
511 spl_hash_key_release(&key);
512 } else {
513 zend_hash_index_update(ht, key.h, value);
514 }
515
516 if (refcount) {
517 spl_array_set_refcount(intern->is_child, ht, refcount);
518 }
519 } /* }}} */
520
spl_array_write_dimension(zend_object * object,zval * offset,zval * value)521 static void spl_array_write_dimension(zend_object *object, zval *offset, zval *value) /* {{{ */
522 {
523 spl_array_write_dimension_ex(1, object, offset, value);
524 } /* }}} */
525
spl_array_unset_dimension_ex(int check_inherited,zend_object * object,zval * offset)526 static void spl_array_unset_dimension_ex(int check_inherited, zend_object *object, zval *offset) /* {{{ */
527 {
528 HashTable *ht;
529 spl_array_object *intern = spl_array_from_obj(object);
530 spl_hash_key key;
531
532 if (check_inherited && intern->fptr_offset_del) {
533 zend_call_method_with_1_params(object, object->ce, &intern->fptr_offset_del, "offsetUnset", NULL, offset);
534 return;
535 }
536
537 if (intern->nApplyCount > 0) {
538 zend_throw_error(NULL, "Modification of ArrayObject during sorting is prohibited");
539 return;
540 }
541
542 if (get_hash_key(&key, intern, offset) == FAILURE) {
543 zend_illegal_container_offset(object->ce->name, offset, BP_VAR_UNSET);
544 return;
545 }
546
547 ht = spl_array_get_hash_table(intern);
548 uint32_t refcount = spl_array_set_refcount(intern->is_child, ht, 1);
549
550 if (key.key) {
551 zval *data = zend_hash_find(ht, key.key);
552 if (data) {
553 if (Z_TYPE_P(data) == IS_INDIRECT) {
554 data = Z_INDIRECT_P(data);
555 if (Z_TYPE_P(data) != IS_UNDEF) {
556 zval garbage;
557 ZVAL_COPY_VALUE(&garbage, data);
558 ZVAL_UNDEF(data);
559 HT_FLAGS(ht) |= HASH_FLAG_HAS_EMPTY_IND;
560 zend_hash_move_forward_ex(ht, spl_array_get_pos_ptr(ht, intern));
561 if (spl_array_is_object(intern)) {
562 spl_array_skip_protected(intern, ht);
563 }
564 zval_ptr_dtor(&garbage);
565 }
566 } else {
567 zend_hash_del(ht, key.key);
568 }
569 }
570 spl_hash_key_release(&key);
571 } else {
572 zend_hash_index_del(ht, key.h);
573 }
574
575 if (refcount) {
576 spl_array_set_refcount(intern->is_child, ht, refcount);
577 }
578 } /* }}} */
579
spl_array_unset_dimension(zend_object * object,zval * offset)580 static void spl_array_unset_dimension(zend_object *object, zval *offset) /* {{{ */
581 {
582 spl_array_unset_dimension_ex(1, object, offset);
583 } /* }}} */
584
585 /* check_empty can take value 0, 1, or 2
586 * 0/1 are used as normal boolean, but 2 is used for the case when this function is called from
587 * the offsetExists() method, in which case it needs to report the offset exist even if the value is null */
spl_array_has_dimension_ex(bool check_inherited,zend_object * object,zval * offset,int check_empty)588 static bool spl_array_has_dimension_ex(bool check_inherited, zend_object *object, zval *offset, int check_empty) /* {{{ */
589 {
590 spl_array_object *intern = spl_array_from_obj(object);
591 zval rv, *value = NULL, *tmp;
592
593 if (check_inherited && intern->fptr_offset_has) {
594 zend_call_method_with_1_params(object, object->ce, &intern->fptr_offset_has, "offsetExists", &rv, offset);
595
596 if (!zend_is_true(&rv)) {
597 zval_ptr_dtor(&rv);
598 return 0;
599 }
600 zval_ptr_dtor(&rv);
601
602 /* For isset calls we don't need to check the value, so return early */
603 if (!check_empty) {
604 return 1;
605 } else if (intern->fptr_offset_get) {
606 value = spl_array_read_dimension_ex(1, object, offset, BP_VAR_R, &rv);
607 }
608 }
609
610 if (!value) {
611 HashTable *ht = spl_array_get_hash_table(intern);
612 spl_hash_key key;
613
614 if (get_hash_key(&key, intern, offset) == FAILURE) {
615 zend_illegal_container_offset(object->ce->name, offset, BP_VAR_IS);
616 return 0;
617 }
618
619 if (key.key) {
620 tmp = zend_hash_find(ht, key.key);
621 spl_hash_key_release(&key);
622 } else {
623 tmp = zend_hash_index_find(ht, key.h);
624 }
625
626 if (!tmp) {
627 return 0;
628 }
629
630 /* check_empty is only equal to 2 if it is called from offsetExists on this class,
631 * where it needs to report an offset exists even if the value is null */
632 if (check_empty == 2) {
633 return 1;
634 }
635
636 if (check_empty && check_inherited && intern->fptr_offset_get) {
637 value = spl_array_read_dimension_ex(1, object, offset, BP_VAR_R, &rv);
638 } else {
639 value = tmp;
640 }
641 }
642
643 if (value == &rv) {
644 zval_ptr_dtor(&rv);
645 }
646
647 /* empty() check the value is not falsy, isset() only check it is not null */
648 return check_empty ? zend_is_true(value) : Z_TYPE_P(value) != IS_NULL;
649 } /* }}} */
650
spl_array_has_dimension(zend_object * object,zval * offset,int check_empty)651 static int spl_array_has_dimension(zend_object *object, zval *offset, int check_empty) /* {{{ */
652 {
653 return spl_array_has_dimension_ex(/* check_inherited */ true, object, offset, check_empty);
654 } /* }}} */
655
656 /* {{{ Returns whether the requested $index exists. */
PHP_METHOD(ArrayObject,offsetExists)657 PHP_METHOD(ArrayObject, offsetExists)
658 {
659 zval *index;
660 if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &index) == FAILURE) {
661 RETURN_THROWS();
662 }
663 RETURN_BOOL(spl_array_has_dimension_ex(/* check_inherited */ false, Z_OBJ_P(ZEND_THIS), index, 2));
664 } /* }}} */
665
666 /* {{{ Returns the value at the specified $index. */
PHP_METHOD(ArrayObject,offsetGet)667 PHP_METHOD(ArrayObject, offsetGet)
668 {
669 zval *value, *index;
670 if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &index) == FAILURE) {
671 RETURN_THROWS();
672 }
673 value = spl_array_read_dimension_ex(0, Z_OBJ_P(ZEND_THIS), index, BP_VAR_R, return_value);
674 if (value != return_value) {
675 RETURN_COPY_DEREF(value);
676 }
677 } /* }}} */
678
679 /* {{{ Sets the value at the specified $index to $newval. */
PHP_METHOD(ArrayObject,offsetSet)680 PHP_METHOD(ArrayObject, offsetSet)
681 {
682 zval *index, *value;
683 if (zend_parse_parameters(ZEND_NUM_ARGS(), "zz", &index, &value) == FAILURE) {
684 RETURN_THROWS();
685 }
686 spl_array_write_dimension_ex(0, Z_OBJ_P(ZEND_THIS), index, value);
687 } /* }}} */
688
spl_array_iterator_append(zval * object,zval * append_value)689 void spl_array_iterator_append(zval *object, zval *append_value) /* {{{ */
690 {
691 spl_array_object *intern = Z_SPLARRAY_P(object);
692
693 if (spl_array_is_object(intern)) {
694 zend_throw_error(NULL, "Cannot append properties to objects, use %s::offsetSet() instead", ZSTR_VAL(Z_OBJCE_P(object)->name));
695 return;
696 }
697
698 spl_array_write_dimension(Z_OBJ_P(object), NULL, append_value);
699 } /* }}} */
700
701 /* {{{ Appends the value (cannot be called for objects). */
PHP_METHOD(ArrayObject,append)702 PHP_METHOD(ArrayObject, append)
703 {
704 zval *value;
705
706 if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &value) == FAILURE) {
707 RETURN_THROWS();
708 }
709 spl_array_iterator_append(ZEND_THIS, value);
710 } /* }}} */
711
712 /* {{{ Unsets the value at the specified $index. */
PHP_METHOD(ArrayObject,offsetUnset)713 PHP_METHOD(ArrayObject, offsetUnset)
714 {
715 zval *index;
716 if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &index) == FAILURE) {
717 RETURN_THROWS();
718 }
719 spl_array_unset_dimension_ex(0, Z_OBJ_P(ZEND_THIS), index);
720 } /* }}} */
721
722 /* {{{ Return a copy of the contained array */
PHP_METHOD(ArrayObject,getArrayCopy)723 PHP_METHOD(ArrayObject, getArrayCopy)
724 {
725 zval *object = ZEND_THIS;
726 spl_array_object *intern = Z_SPLARRAY_P(object);
727
728 if (zend_parse_parameters_none() == FAILURE) {
729 RETURN_THROWS();
730 }
731
732 RETURN_ARR(zend_array_dup(spl_array_get_hash_table(intern)));
733 } /* }}} */
734
spl_array_get_properties_for(zend_object * object,zend_prop_purpose purpose)735 static HashTable *spl_array_get_properties_for(zend_object *object, zend_prop_purpose purpose) /* {{{ */
736 {
737 spl_array_object *intern = spl_array_from_obj(object);
738 HashTable *ht;
739 bool dup;
740
741 if (intern->ar_flags & SPL_ARRAY_STD_PROP_LIST) {
742 return zend_std_get_properties_for(object, purpose);
743 }
744
745 /* We are supposed to be the only owner of the internal hashtable.
746 * The "dup" flag decides whether this is a "long-term" use where
747 * we need to duplicate, or a "temporary" one, where we can expect
748 * that no operations on the ArrayObject will be performed in the
749 * meantime. */
750 switch (purpose) {
751 case ZEND_PROP_PURPOSE_ARRAY_CAST:
752 dup = 1;
753 break;
754 case ZEND_PROP_PURPOSE_VAR_EXPORT:
755 case ZEND_PROP_PURPOSE_JSON:
756 dup = 0;
757 break;
758 default:
759 return zend_std_get_properties_for(object, purpose);
760 }
761
762 ht = spl_array_get_hash_table(intern);
763 if (dup) {
764 ht = zend_array_dup(ht);
765 } else {
766 GC_ADDREF(ht);
767 }
768 return ht;
769 } /* }}} */
770
spl_array_get_debug_info(zend_object * obj)771 static inline HashTable* spl_array_get_debug_info(zend_object *obj) /* {{{ */
772 {
773 spl_array_object *intern = spl_array_from_obj(obj);
774 HashTable *properties = zend_std_get_properties_ex(&intern->std);
775
776 if (intern->ar_flags & SPL_ARRAY_IS_SELF) {
777 return zend_array_dup(properties);
778 } else {
779 HashTable *debug_info;
780
781 debug_info = zend_new_array(zend_hash_num_elements(properties) + 1);
782 zend_hash_copy(debug_info, properties, (copy_ctor_func_t) zval_add_ref);
783
784 zval *storage = &intern->array;
785 Z_TRY_ADDREF_P(storage);
786
787 const zend_class_entry *base_class_ce = instanceof_function(obj->ce, spl_ce_ArrayIterator)
788 ? spl_ce_ArrayIterator : spl_ce_ArrayObject;
789
790 spl_set_private_debug_info_property(base_class_ce, "storage", strlen("storage"), debug_info, storage);
791
792 return debug_info;
793 }
794 }
795 /* }}} */
796
spl_array_get_gc(zend_object * obj,zval ** gc_data,int * gc_data_count)797 static HashTable *spl_array_get_gc(zend_object *obj, zval **gc_data, int *gc_data_count) /* {{{ */
798 {
799 spl_array_object *intern = spl_array_from_obj(obj);
800 *gc_data = &intern->array;
801 *gc_data_count = 1;
802 return zend_std_get_properties(obj);
803 }
804 /* }}} */
805
spl_array_read_property(zend_object * object,zend_string * name,int type,void ** cache_slot,zval * rv)806 static zval *spl_array_read_property(zend_object *object, zend_string *name, int type, void **cache_slot, zval *rv) /* {{{ */
807 {
808 spl_array_object *intern = spl_array_from_obj(object);
809
810 if ((intern->ar_flags & SPL_ARRAY_ARRAY_AS_PROPS) != 0
811 && !zend_std_has_property(object, name, ZEND_PROPERTY_EXISTS, NULL)) {
812 zval member;
813 ZVAL_STR(&member, name);
814 return spl_array_read_dimension(object, &member, type, rv);
815 }
816 return zend_std_read_property(object, name, type, cache_slot, rv);
817 } /* }}} */
818
spl_array_write_property(zend_object * object,zend_string * name,zval * value,void ** cache_slot)819 static zval *spl_array_write_property(zend_object *object, zend_string *name, zval *value, void **cache_slot) /* {{{ */
820 {
821 spl_array_object *intern = spl_array_from_obj(object);
822
823 if ((intern->ar_flags & SPL_ARRAY_ARRAY_AS_PROPS) != 0
824 && !zend_std_has_property(object, name, ZEND_PROPERTY_EXISTS, NULL)) {
825 zval member;
826 ZVAL_STR(&member, name);
827 spl_array_write_dimension(object, &member, value);
828 return value;
829 }
830 return zend_std_write_property(object, name, value, cache_slot);
831 } /* }}} */
832
spl_array_get_property_ptr_ptr(zend_object * object,zend_string * name,int type,void ** cache_slot)833 static zval *spl_array_get_property_ptr_ptr(zend_object *object, zend_string *name, int type, void **cache_slot) /* {{{ */
834 {
835 spl_array_object *intern = spl_array_from_obj(object);
836
837 if ((intern->ar_flags & SPL_ARRAY_ARRAY_AS_PROPS) != 0
838 && !zend_std_has_property(object, name, ZEND_PROPERTY_EXISTS, NULL)) {
839 /* If object has offsetGet() overridden, then fallback to read_property,
840 * which will call offsetGet(). */
841 zval member;
842 if (intern->fptr_offset_get) {
843 return NULL;
844 }
845 ZVAL_STR(&member, name);
846 return spl_array_get_dimension_ptr(1, intern, object->ce->name, &member, type);
847 }
848 return zend_std_get_property_ptr_ptr(object, name, type, cache_slot);
849 } /* }}} */
850
spl_array_has_property(zend_object * object,zend_string * name,int has_set_exists,void ** cache_slot)851 static int spl_array_has_property(zend_object *object, zend_string *name, int has_set_exists, void **cache_slot) /* {{{ */
852 {
853 spl_array_object *intern = spl_array_from_obj(object);
854
855 if ((intern->ar_flags & SPL_ARRAY_ARRAY_AS_PROPS) != 0
856 && !zend_std_has_property(object, name, ZEND_PROPERTY_EXISTS, NULL)) {
857 zval member;
858 ZVAL_STR(&member, name);
859 return spl_array_has_dimension(object, &member, has_set_exists);
860 }
861 return zend_std_has_property(object, name, has_set_exists, cache_slot);
862 } /* }}} */
863
spl_array_unset_property(zend_object * object,zend_string * name,void ** cache_slot)864 static void spl_array_unset_property(zend_object *object, zend_string *name, void **cache_slot) /* {{{ */
865 {
866 spl_array_object *intern = spl_array_from_obj(object);
867
868 if ((intern->ar_flags & SPL_ARRAY_ARRAY_AS_PROPS) != 0
869 && !zend_std_has_property(object, name, ZEND_PROPERTY_EXISTS, NULL)) {
870 zval member;
871 ZVAL_STR(&member, name);
872 spl_array_unset_dimension(object, &member);
873 return;
874 }
875 zend_std_unset_property(object, name, cache_slot);
876 } /* }}} */
877
spl_array_compare_objects(zval * o1,zval * o2)878 static int spl_array_compare_objects(zval *o1, zval *o2) /* {{{ */
879 {
880 HashTable *ht1,
881 *ht2;
882 spl_array_object *intern1,
883 *intern2;
884 int result = 0;
885
886 ZEND_COMPARE_OBJECTS_FALLBACK(o1, o2);
887
888 intern1 = Z_SPLARRAY_P(o1);
889 intern2 = Z_SPLARRAY_P(o2);
890 ht1 = spl_array_get_hash_table(intern1);
891 ht2 = spl_array_get_hash_table(intern2);
892
893 result = zend_compare_symbol_tables(ht1, ht2);
894 /* if we just compared std.properties, don't do it again */
895 if (result == 0 &&
896 !(ht1 == intern1->std.properties && ht2 == intern2->std.properties)) {
897 result = zend_std_compare_objects(o1, o2);
898 }
899 return result;
900 } /* }}} */
901
spl_array_skip_protected(spl_array_object * intern,HashTable * aht)902 static zend_result spl_array_skip_protected(spl_array_object *intern, HashTable *aht) /* {{{ */
903 {
904 zend_string *string_key;
905 zend_ulong num_key;
906 zval *data;
907
908 if (spl_array_is_object(intern)) {
909 uint32_t *pos_ptr = spl_array_get_pos_ptr(aht, intern);
910
911 do {
912 if (zend_hash_get_current_key_ex(aht, &string_key, &num_key, pos_ptr) == HASH_KEY_IS_STRING) {
913 data = zend_hash_get_current_data_ex(aht, pos_ptr);
914 if (data && Z_TYPE_P(data) == IS_INDIRECT &&
915 Z_TYPE_P(data = Z_INDIRECT_P(data)) == IS_UNDEF) {
916 /* skip */
917 } else if (!ZSTR_LEN(string_key) || ZSTR_VAL(string_key)[0]) {
918 return SUCCESS;
919 }
920 } else {
921 return SUCCESS;
922 }
923 if (zend_hash_has_more_elements_ex(aht, pos_ptr) != SUCCESS) {
924 return FAILURE;
925 }
926 zend_hash_move_forward_ex(aht, pos_ptr);
927 } while (1);
928 }
929 return FAILURE;
930 } /* }}} */
931
932 /* {{{ spl_array_set_array */
spl_array_set_array(zval * object,spl_array_object * intern,zval * array,zend_long ar_flags,bool just_array)933 static void spl_array_set_array(zval *object, spl_array_object *intern, zval *array, zend_long ar_flags, bool just_array) {
934 /* Handled by ZPP prior to this, or for __unserialize() before passing to here */
935 ZEND_ASSERT(Z_TYPE_P(array) == IS_ARRAY || Z_TYPE_P(array) == IS_OBJECT);
936 zval garbage;
937 ZVAL_UNDEF(&garbage);
938 if (Z_TYPE_P(array) == IS_ARRAY) {
939 ZVAL_COPY_VALUE(&garbage, &intern->array);
940 if (Z_REFCOUNT_P(array) == 1) {
941 ZVAL_COPY(&intern->array, array);
942 } else {
943 //??? TODO: try to avoid array duplication
944 ZVAL_ARR(&intern->array, zend_array_dup(Z_ARR_P(array)));
945
946 if (intern->is_child) {
947 Z_TRY_DELREF(intern->bucket->val);
948 /*
949 * replace bucket->val with copied array, so the changes between
950 * parent and child object can affect each other.
951 */
952 ZVAL_COPY(&intern->bucket->val, &intern->array);
953 }
954 }
955 } else {
956 if (Z_OBJ_HT_P(array) == &spl_handler_ArrayObject) {
957 ZVAL_COPY_VALUE(&garbage, &intern->array);
958 if (just_array) {
959 spl_array_object *other = Z_SPLARRAY_P(array);
960 ar_flags = other->ar_flags & ~SPL_ARRAY_INT_MASK;
961 }
962 if (Z_OBJ_P(object) == Z_OBJ_P(array)) {
963 ar_flags |= SPL_ARRAY_IS_SELF;
964 ZVAL_UNDEF(&intern->array);
965 } else {
966 ar_flags |= SPL_ARRAY_USE_OTHER;
967 ZVAL_COPY(&intern->array, array);
968 }
969 } else {
970 zend_object_get_properties_t handler = Z_OBJ_HANDLER_P(array, get_properties);
971 if (handler != zend_std_get_properties || Z_OBJ_HANDLER_P(array, get_properties_for)) {
972 zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0,
973 "Overloaded object of type %s is not compatible with %s",
974 ZSTR_VAL(Z_OBJCE_P(array)->name), ZSTR_VAL(intern->std.ce->name));
975 ZEND_ASSERT(Z_TYPE(garbage) == IS_UNDEF);
976 return;
977 }
978 ZVAL_COPY_VALUE(&garbage, &intern->array);
979 ZVAL_COPY(&intern->array, array);
980 }
981 }
982
983 intern->ar_flags &= ~SPL_ARRAY_IS_SELF & ~SPL_ARRAY_USE_OTHER;
984 intern->ar_flags |= ar_flags;
985 if (intern->ht_iter != (uint32_t)-1) {
986 zend_hash_iterator_del(intern->ht_iter);
987 intern->ht_iter = (uint32_t)-1;
988 }
989
990 zval_ptr_dtor(&garbage);
991 }
992 /* }}} */
993
994 /* {{{ Constructs a new array object from an array or object. */
PHP_METHOD(ArrayObject,__construct)995 PHP_METHOD(ArrayObject, __construct)
996 {
997 zval *object = ZEND_THIS;
998 spl_array_object *intern;
999 zval *array;
1000 zend_long ar_flags = 0;
1001 zend_class_entry *ce_get_iterator = spl_ce_ArrayIterator;
1002
1003 if (ZEND_NUM_ARGS() == 0) {
1004 return; /* nothing to do */
1005 }
1006
1007 if (zend_parse_parameters(ZEND_NUM_ARGS(), "|AlC", &array, &ar_flags, &ce_get_iterator) == FAILURE) {
1008 RETURN_THROWS();
1009 }
1010
1011 intern = Z_SPLARRAY_P(object);
1012
1013 if (ZEND_NUM_ARGS() > 2) {
1014 intern->ce_get_iterator = ce_get_iterator;
1015 }
1016
1017 ar_flags &= ~SPL_ARRAY_INT_MASK;
1018
1019 spl_array_set_array(object, intern, array, ar_flags, ZEND_NUM_ARGS() == 1);
1020 }
1021 /* }}} */
1022
1023 /* {{{ Set the class used in getIterator. */
PHP_METHOD(ArrayObject,setIteratorClass)1024 PHP_METHOD(ArrayObject, setIteratorClass)
1025 {
1026 zval *object = ZEND_THIS;
1027 spl_array_object *intern = Z_SPLARRAY_P(object);
1028 zend_class_entry *ce_get_iterator = spl_ce_ArrayIterator;
1029
1030 ZEND_PARSE_PARAMETERS_START(1, 1)
1031 Z_PARAM_CLASS(ce_get_iterator)
1032 ZEND_PARSE_PARAMETERS_END();
1033
1034 intern->ce_get_iterator = ce_get_iterator;
1035 }
1036 /* }}} */
1037
1038 /* {{{ Get the class used in getIterator. */
PHP_METHOD(ArrayObject,getIteratorClass)1039 PHP_METHOD(ArrayObject, getIteratorClass)
1040 {
1041 zval *object = ZEND_THIS;
1042 spl_array_object *intern = Z_SPLARRAY_P(object);
1043
1044 if (zend_parse_parameters_none() == FAILURE) {
1045 RETURN_THROWS();
1046 }
1047
1048 zend_string_addref(intern->ce_get_iterator->name);
1049 RETURN_STR(intern->ce_get_iterator->name);
1050 }
1051 /* }}} */
1052
1053 /* {{{ Get flags */
PHP_METHOD(ArrayObject,getFlags)1054 PHP_METHOD(ArrayObject, getFlags)
1055 {
1056 zval *object = ZEND_THIS;
1057 spl_array_object *intern = Z_SPLARRAY_P(object);
1058
1059 if (zend_parse_parameters_none() == FAILURE) {
1060 RETURN_THROWS();
1061 }
1062
1063 RETURN_LONG(intern->ar_flags & ~SPL_ARRAY_INT_MASK);
1064 }
1065 /* }}} */
1066
1067 /* {{{ Set flags */
PHP_METHOD(ArrayObject,setFlags)1068 PHP_METHOD(ArrayObject, setFlags)
1069 {
1070 zval *object = ZEND_THIS;
1071 spl_array_object *intern = Z_SPLARRAY_P(object);
1072 zend_long ar_flags = 0;
1073
1074 if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &ar_flags) == FAILURE) {
1075 RETURN_THROWS();
1076 }
1077
1078 intern->ar_flags = (intern->ar_flags & SPL_ARRAY_INT_MASK) | (ar_flags & ~SPL_ARRAY_INT_MASK);
1079 }
1080 /* }}} */
1081
1082 /* {{{ Replace the referenced array or object with a new one and return the old one (right now copy - to be changed) */
PHP_METHOD(ArrayObject,exchangeArray)1083 PHP_METHOD(ArrayObject, exchangeArray)
1084 {
1085 zval *object = ZEND_THIS, *array;
1086 spl_array_object *intern = Z_SPLARRAY_P(object);
1087
1088 if (zend_parse_parameters(ZEND_NUM_ARGS(), "A", &array) == FAILURE) {
1089 RETURN_THROWS();
1090 }
1091
1092 if (intern->nApplyCount > 0) {
1093 zend_throw_error(NULL, "Modification of ArrayObject during sorting is prohibited");
1094 RETURN_THROWS();
1095 }
1096
1097 RETVAL_ARR(zend_array_dup(spl_array_get_hash_table(intern)));
1098 spl_array_set_array(object, intern, array, 0L, 1);
1099 }
1100 /* }}} */
1101
1102 /* {{{ Create a new iterator from a ArrayObject instance */
PHP_METHOD(ArrayObject,getIterator)1103 PHP_METHOD(ArrayObject, getIterator)
1104 {
1105 zval *object = ZEND_THIS;
1106 spl_array_object *intern = Z_SPLARRAY_P(object);
1107
1108 if (zend_parse_parameters_none() == FAILURE) {
1109 RETURN_THROWS();
1110 }
1111
1112 RETURN_OBJ(spl_array_object_new_ex(intern->ce_get_iterator, Z_OBJ_P(object), 0));
1113 }
1114 /* }}} */
1115
spl_array_object_count_elements_helper(spl_array_object * intern)1116 static zend_long spl_array_object_count_elements_helper(spl_array_object *intern) /* {{{ */
1117 {
1118 HashTable *aht = spl_array_get_hash_table(intern);
1119 if (spl_array_is_object(intern)) {
1120 zend_long count = 0;
1121 zend_string *key;
1122 zval *val;
1123 /* Count public/dynamic properties */
1124 ZEND_HASH_FOREACH_STR_KEY_VAL(aht, key, val) {
1125 if (Z_TYPE_P(val) == IS_INDIRECT) {
1126 if (Z_TYPE_P(Z_INDIRECT_P(val)) == IS_UNDEF) continue;
1127 if (key && ZSTR_VAL(key)[0] == '\0') continue;
1128 }
1129 count++;
1130 } ZEND_HASH_FOREACH_END();
1131 return count;
1132 } else {
1133 return zend_hash_num_elements(aht);
1134 }
1135 } /* }}} */
1136
spl_array_object_count_elements(zend_object * object,zend_long * count)1137 static zend_result spl_array_object_count_elements(zend_object *object, zend_long *count) /* {{{ */
1138 {
1139 spl_array_object *intern = spl_array_from_obj(object);
1140
1141 if (intern->fptr_count) {
1142 zval rv;
1143 zend_call_method_with_0_params(object, intern->std.ce, &intern->fptr_count, "count", &rv);
1144 if (Z_TYPE(rv) != IS_UNDEF) {
1145 *count = zval_get_long(&rv);
1146 zval_ptr_dtor(&rv);
1147 return SUCCESS;
1148 }
1149 *count = 0;
1150 return FAILURE;
1151 }
1152 *count = spl_array_object_count_elements_helper(intern);
1153 return SUCCESS;
1154 } /* }}} */
1155
1156 /* {{{ Return the number of elements in the Iterator. */
PHP_METHOD(ArrayObject,count)1157 PHP_METHOD(ArrayObject, count)
1158 {
1159 spl_array_object *intern = Z_SPLARRAY_P(ZEND_THIS);
1160
1161 if (zend_parse_parameters_none() == FAILURE) {
1162 RETURN_THROWS();
1163 }
1164
1165 RETURN_LONG(spl_array_object_count_elements_helper(intern));
1166 } /* }}} */
1167
spl_array_method(INTERNAL_FUNCTION_PARAMETERS,char * fname,size_t fname_len,int use_arg)1168 static void spl_array_method(INTERNAL_FUNCTION_PARAMETERS, char *fname, size_t fname_len, int use_arg) /* {{{ */
1169 {
1170 spl_array_object *intern = Z_SPLARRAY_P(ZEND_THIS);
1171 HashTable **ht_ptr = spl_array_get_hash_table_ptr(intern);
1172 HashTable *aht = *ht_ptr;
1173 zval function_name, params[2], *arg = NULL;
1174
1175 ZVAL_STRINGL(&function_name, fname, fname_len);
1176
1177 ZVAL_NEW_EMPTY_REF(¶ms[0]);
1178 ZVAL_ARR(Z_REFVAL(params[0]), aht);
1179 GC_ADDREF(aht);
1180
1181 if (!use_arg) {
1182 if (zend_parse_parameters_none() == FAILURE) {
1183 goto exit;
1184 }
1185
1186 intern->nApplyCount++;
1187 call_user_function(EG(function_table), NULL, &function_name, return_value, 1, params);
1188 intern->nApplyCount--;
1189 } else if (use_arg == SPL_ARRAY_METHOD_SORT_FLAGS_ARG) {
1190 zend_long sort_flags = 0;
1191 if (zend_parse_parameters(ZEND_NUM_ARGS(), "|l", &sort_flags) == FAILURE) {
1192 goto exit;
1193 }
1194 ZVAL_LONG(¶ms[1], sort_flags);
1195 intern->nApplyCount++;
1196 call_user_function(EG(function_table), NULL, &function_name, return_value, 2, params);
1197 intern->nApplyCount--;
1198 } else {
1199 if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &arg) == FAILURE) {
1200 goto exit;
1201 }
1202 ZVAL_COPY_VALUE(¶ms[1], arg);
1203 intern->nApplyCount++;
1204 call_user_function(EG(function_table), NULL, &function_name, return_value, 2, params);
1205 intern->nApplyCount--;
1206 }
1207
1208 exit:
1209 {
1210 zval *ht_zv = Z_REFVAL(params[0]);
1211 zend_array_release(*ht_ptr);
1212 SEPARATE_ARRAY(ht_zv);
1213 *ht_ptr = Z_ARRVAL_P(ht_zv);
1214 ZVAL_NULL(ht_zv);
1215 zval_ptr_dtor(¶ms[0]);
1216 zend_string_free(Z_STR(function_name));
1217 }
1218 } /* }}} */
1219
1220 #define SPL_ARRAY_METHOD(cname, fname, use_arg) \
1221 PHP_METHOD(cname, fname) \
1222 { \
1223 spl_array_method(INTERNAL_FUNCTION_PARAM_PASSTHRU, #fname, sizeof(#fname)-1, use_arg); \
1224 }
1225
1226 /* {{{ Sort the entries by values. */
SPL_ARRAY_METHOD(ArrayObject,asort,SPL_ARRAY_METHOD_SORT_FLAGS_ARG)1227 SPL_ARRAY_METHOD(ArrayObject, asort, SPL_ARRAY_METHOD_SORT_FLAGS_ARG) /* }}} */
1228
1229 /* {{{ Sort the entries by key. */
1230 SPL_ARRAY_METHOD(ArrayObject, ksort, SPL_ARRAY_METHOD_SORT_FLAGS_ARG) /* }}} */
1231
1232 /* {{{ Sort the entries by values user defined function. */
1233 SPL_ARRAY_METHOD(ArrayObject, uasort, SPL_ARRAY_METHOD_CALLBACK_ARG) /* }}} */
1234
1235 /* {{{ Sort the entries by key using user defined function. */
1236 SPL_ARRAY_METHOD(ArrayObject, uksort, SPL_ARRAY_METHOD_CALLBACK_ARG) /* }}} */
1237
1238 /* {{{ Sort the entries by values using "natural order" algorithm. */
1239 SPL_ARRAY_METHOD(ArrayObject, natsort, SPL_ARRAY_METHOD_NO_ARG) /* }}} */
1240
1241 /* {{{ Sort the entries by key using case insensitive "natural order" algorithm. */
1242 SPL_ARRAY_METHOD(ArrayObject, natcasesort, SPL_ARRAY_METHOD_NO_ARG) /* }}} */
1243
1244 /* {{{ Serialize the object */
1245 PHP_METHOD(ArrayObject, serialize)
1246 {
1247 zval *object = ZEND_THIS;
1248 spl_array_object *intern = Z_SPLARRAY_P(object);
1249 zval members, flags;
1250 php_serialize_data_t var_hash;
1251 smart_str buf = {0};
1252
1253 if (zend_parse_parameters_none() == FAILURE) {
1254 RETURN_THROWS();
1255 }
1256
1257 PHP_VAR_SERIALIZE_INIT(var_hash);
1258
1259 ZVAL_LONG(&flags, (intern->ar_flags & SPL_ARRAY_CLONE_MASK));
1260
1261 /* storage */
1262 smart_str_appendl(&buf, "x:", 2);
1263 php_var_serialize(&buf, &flags, &var_hash);
1264
1265 if (!(intern->ar_flags & SPL_ARRAY_IS_SELF)) {
1266 php_var_serialize(&buf, &intern->array, &var_hash);
1267 smart_str_appendc(&buf, ';');
1268 }
1269
1270 /* members */
1271 smart_str_appendl(&buf, "m:", 2);
1272
1273 ZVAL_ARR(&members, zend_std_get_properties_ex(&intern->std));
1274
1275 php_var_serialize(&buf, &members, &var_hash); /* finishes the string */
1276
1277 /* done */
1278 PHP_VAR_SERIALIZE_DESTROY(var_hash);
1279
1280 RETURN_STR(smart_str_extract(&buf));
1281 } /* }}} */
1282
1283 /* {{{ unserialize the object */
PHP_METHOD(ArrayObject,unserialize)1284 PHP_METHOD(ArrayObject, unserialize)
1285 {
1286 zval *object = ZEND_THIS;
1287 spl_array_object *intern = Z_SPLARRAY_P(object);
1288
1289 char *buf;
1290 size_t buf_len;
1291 const unsigned char *p, *s;
1292 php_unserialize_data_t var_hash;
1293 zval *members, *zflags, *array;
1294 zend_long flags;
1295
1296 if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &buf, &buf_len) == FAILURE) {
1297 RETURN_THROWS();
1298 }
1299
1300 if (buf_len == 0) {
1301 return;
1302 }
1303
1304 if (intern->nApplyCount > 0) {
1305 zend_throw_error(NULL, "Modification of ArrayObject during sorting is prohibited");
1306 RETURN_THROWS();
1307 }
1308
1309 /* storage */
1310 s = p = (const unsigned char*)buf;
1311 PHP_VAR_UNSERIALIZE_INIT(var_hash);
1312
1313 if (*p!= 'x' || *++p != ':') {
1314 goto outexcept;
1315 }
1316 ++p;
1317
1318 zflags = var_tmp_var(&var_hash);
1319 if (!php_var_unserialize(zflags, &p, s + buf_len, &var_hash) || Z_TYPE_P(zflags) != IS_LONG) {
1320 goto outexcept;
1321 }
1322
1323 --p; /* for ';' */
1324 flags = Z_LVAL_P(zflags);
1325 /* flags needs to be verified and we also need to verify whether the next
1326 * thing we get is ';'. After that we require an 'm' or something else
1327 * where 'm' stands for members and anything else should be an array. If
1328 * neither 'a' or 'm' follows we have an error. */
1329
1330 if (*p != ';') {
1331 goto outexcept;
1332 }
1333 ++p;
1334
1335 if (flags & SPL_ARRAY_IS_SELF) {
1336 /* If IS_SELF is used, the flags are not followed by an array/object */
1337 intern->ar_flags &= ~SPL_ARRAY_CLONE_MASK;
1338 intern->ar_flags |= flags & SPL_ARRAY_CLONE_MASK;
1339 zval_ptr_dtor(&intern->array);
1340 ZVAL_UNDEF(&intern->array);
1341 } else {
1342 if (*p!='a' && *p!='O' && *p!='C' && *p!='r') {
1343 goto outexcept;
1344 }
1345
1346 array = var_tmp_var(&var_hash);
1347 if (!php_var_unserialize(array, &p, s + buf_len, &var_hash)
1348 || (Z_TYPE_P(array) != IS_ARRAY && Z_TYPE_P(array) != IS_OBJECT)) {
1349 goto outexcept;
1350 }
1351
1352 intern->ar_flags &= ~SPL_ARRAY_CLONE_MASK;
1353 intern->ar_flags |= flags & SPL_ARRAY_CLONE_MASK;
1354
1355 if (Z_TYPE_P(array) == IS_ARRAY) {
1356 zval_ptr_dtor(&intern->array);
1357 ZVAL_COPY_VALUE(&intern->array, array);
1358 ZVAL_NULL(array);
1359 SEPARATE_ARRAY(&intern->array);
1360 } else {
1361 spl_array_set_array(object, intern, array, 0L, 1);
1362 }
1363
1364 if (*p != ';') {
1365 goto outexcept;
1366 }
1367 ++p;
1368 }
1369
1370 /* members */
1371 if (*p!= 'm' || *++p != ':') {
1372 goto outexcept;
1373 }
1374 ++p;
1375
1376 members = var_tmp_var(&var_hash);
1377 if (!php_var_unserialize(members, &p, s + buf_len, &var_hash) || Z_TYPE_P(members) != IS_ARRAY) {
1378 goto outexcept;
1379 }
1380
1381 /* copy members */
1382 object_properties_load(&intern->std, Z_ARRVAL_P(members));
1383
1384 /* done reading $serialized */
1385 PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
1386 return;
1387
1388 outexcept:
1389 PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
1390 zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0, "Error at offset " ZEND_LONG_FMT " of %zd bytes", (zend_long)((char*)p - buf), buf_len);
1391 RETURN_THROWS();
1392
1393 } /* }}} */
1394
1395 /* {{{ */
PHP_METHOD(ArrayObject,__serialize)1396 PHP_METHOD(ArrayObject, __serialize)
1397 {
1398 spl_array_object *intern = Z_SPLARRAY_P(ZEND_THIS);
1399 zval tmp;
1400
1401 if (zend_parse_parameters_none() == FAILURE) {
1402 RETURN_THROWS();
1403 }
1404
1405 array_init(return_value);
1406
1407 /* flags */
1408 ZVAL_LONG(&tmp, (intern->ar_flags & SPL_ARRAY_CLONE_MASK));
1409 zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &tmp);
1410
1411 /* storage */
1412 if (intern->ar_flags & SPL_ARRAY_IS_SELF) {
1413 ZVAL_NULL(&tmp);
1414 } else {
1415 ZVAL_COPY(&tmp, &intern->array);
1416 }
1417 zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &tmp);
1418
1419 /* members */
1420 ZVAL_ARR(&tmp, zend_proptable_to_symtable(
1421 zend_std_get_properties(&intern->std), /* always_duplicate */ 1));
1422 zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &tmp);
1423
1424 /* iterator class */
1425 if (intern->ce_get_iterator == spl_ce_ArrayIterator) {
1426 ZVAL_NULL(&tmp);
1427 } else {
1428 ZVAL_STR_COPY(&tmp, intern->ce_get_iterator->name);
1429 }
1430 zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &tmp);
1431 }
1432 /* }}} */
1433
1434
1435 /* {{{ */
PHP_METHOD(ArrayObject,__unserialize)1436 PHP_METHOD(ArrayObject, __unserialize)
1437 {
1438 spl_array_object *intern = Z_SPLARRAY_P(ZEND_THIS);
1439 HashTable *data;
1440 zval *flags_zv, *storage_zv, *members_zv, *iterator_class_zv;
1441 zend_long flags;
1442
1443 if (zend_parse_parameters(ZEND_NUM_ARGS(), "h", &data) == FAILURE) {
1444 RETURN_THROWS();
1445 }
1446
1447 flags_zv = zend_hash_index_find(data, 0);
1448 storage_zv = zend_hash_index_find(data, 1);
1449 members_zv = zend_hash_index_find(data, 2);
1450 iterator_class_zv = zend_hash_index_find(data, 3);
1451
1452 if (!flags_zv || !storage_zv || !members_zv ||
1453 Z_TYPE_P(flags_zv) != IS_LONG || Z_TYPE_P(members_zv) != IS_ARRAY ||
1454 (iterator_class_zv && (Z_TYPE_P(iterator_class_zv) != IS_NULL &&
1455 Z_TYPE_P(iterator_class_zv) != IS_STRING))) {
1456 zend_throw_exception(spl_ce_UnexpectedValueException,
1457 "Incomplete or ill-typed serialization data", 0);
1458 RETURN_THROWS();
1459 }
1460
1461 flags = Z_LVAL_P(flags_zv);
1462 intern->ar_flags &= ~SPL_ARRAY_CLONE_MASK;
1463 intern->ar_flags |= flags & SPL_ARRAY_CLONE_MASK;
1464
1465 if (flags & SPL_ARRAY_IS_SELF) {
1466 zval_ptr_dtor(&intern->array);
1467 ZVAL_UNDEF(&intern->array);
1468 } else {
1469 if (Z_TYPE_P(storage_zv) != IS_OBJECT && Z_TYPE_P(storage_zv) != IS_ARRAY) {
1470 /* TODO Use UnexpectedValueException instead? And better error message? */
1471 zend_throw_exception(spl_ce_InvalidArgumentException, "Passed variable is not an array or object", 0);
1472 RETURN_THROWS();
1473 }
1474 spl_array_set_array(ZEND_THIS, intern, storage_zv, 0L, 1);
1475 }
1476
1477 object_properties_load(&intern->std, Z_ARRVAL_P(members_zv));
1478 if (EG(exception)) {
1479 RETURN_THROWS();
1480 }
1481
1482 if (iterator_class_zv && Z_TYPE_P(iterator_class_zv) == IS_STRING) {
1483 zend_class_entry *ce = zend_lookup_class(Z_STR_P(iterator_class_zv));
1484
1485 if (!ce) {
1486 zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
1487 "Cannot deserialize ArrayObject with iterator class '%s'; no such class exists",
1488 ZSTR_VAL(Z_STR_P(iterator_class_zv)));
1489 RETURN_THROWS();
1490 }
1491
1492 if (!instanceof_function(ce, zend_ce_iterator)) {
1493 zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
1494 "Cannot deserialize ArrayObject with iterator class '%s'; this class does not implement the Iterator interface",
1495 ZSTR_VAL(Z_STR_P(iterator_class_zv)));
1496 RETURN_THROWS();
1497 }
1498
1499 intern->ce_get_iterator = ce;
1500 }
1501 }
1502 /* }}} */
1503
1504 /* {{{ */
PHP_METHOD(ArrayObject,__debugInfo)1505 PHP_METHOD(ArrayObject, __debugInfo)
1506 {
1507 if (zend_parse_parameters_none() == FAILURE) {
1508 RETURN_THROWS();
1509 }
1510
1511 RETURN_ARR(spl_array_get_debug_info(Z_OBJ_P(ZEND_THIS)));
1512 } /* }}} */
1513
1514 /*** ArrayIterator class ***/
1515 typedef struct _spl_array_iterator {
1516 zend_object_iterator it;
1517 bool by_ref;
1518 } spl_array_iterator;
1519
spl_array_next_ex(spl_array_object * intern,HashTable * aht)1520 static zend_result spl_array_next_ex(spl_array_object *intern, HashTable *aht) /* {{{ */
1521 {
1522 uint32_t *pos_ptr = spl_array_get_pos_ptr(aht, intern);
1523
1524 zend_hash_move_forward_ex(aht, pos_ptr);
1525 if (spl_array_is_object(intern)) {
1526 return spl_array_skip_protected(intern, aht);
1527 } else {
1528 return zend_hash_has_more_elements_ex(aht, pos_ptr);
1529 }
1530 } /* }}} */
1531
spl_array_next(spl_array_object * intern)1532 static zend_result spl_array_next(spl_array_object *intern) /* {{{ */
1533 {
1534 HashTable *aht = spl_array_get_hash_table(intern);
1535
1536 return spl_array_next_ex(intern, aht);
1537
1538 } /* }}} */
1539
spl_array_it_dtor(zend_object_iterator * iter)1540 static void spl_array_it_dtor(zend_object_iterator *iter) /* {{{ */
1541 {
1542 zval_ptr_dtor(&iter->data);
1543 }
1544 /* }}} */
1545
spl_array_it_valid(zend_object_iterator * iter)1546 static zend_result spl_array_it_valid(zend_object_iterator *iter) /* {{{ */
1547 {
1548 spl_array_object *object = Z_SPLARRAY_P(&iter->data);
1549 HashTable *aht = spl_array_get_hash_table(object);
1550 return zend_hash_has_more_elements_ex(aht, spl_array_get_pos_ptr(aht, object));
1551 }
1552 /* }}} */
1553
spl_array_it_get_current_data(zend_object_iterator * iter)1554 static zval *spl_array_it_get_current_data(zend_object_iterator *iter) /* {{{ */
1555 {
1556 spl_array_iterator *array_iter = (spl_array_iterator*)iter;
1557 spl_array_object *object = Z_SPLARRAY_P(&iter->data);
1558 HashTable *aht = spl_array_get_hash_table(object);
1559 zval *data = zend_hash_get_current_data_ex(aht, spl_array_get_pos_ptr(aht, object));
1560 if (data && Z_TYPE_P(data) == IS_INDIRECT) {
1561 data = Z_INDIRECT_P(data);
1562 }
1563 // ZEND_FE_FETCH_RW converts the value to a reference but doesn't know the source is a property.
1564 // Typed properties must add a type source to the reference, and readonly properties must fail.
1565 if (array_iter->by_ref
1566 && Z_TYPE_P(data) != IS_REFERENCE
1567 && Z_TYPE(object->array) == IS_OBJECT
1568 && !(object->ar_flags & (SPL_ARRAY_IS_SELF|SPL_ARRAY_USE_OTHER))) {
1569 zend_string *key;
1570 zend_hash_get_current_key_ex(aht, &key, NULL, spl_array_get_pos_ptr(aht, object));
1571 zend_class_entry *ce = Z_OBJCE(object->array);
1572 zend_property_info *prop_info = zend_get_property_info(ce, key, true);
1573 ZEND_ASSERT(prop_info != ZEND_WRONG_PROPERTY_INFO);
1574 if (EXPECTED(prop_info != NULL) && ZEND_TYPE_IS_SET(prop_info->type)) {
1575 if (prop_info->flags & ZEND_ACC_READONLY) {
1576 zend_throw_error(NULL,
1577 "Cannot acquire reference to readonly property %s::$%s",
1578 ZSTR_VAL(prop_info->ce->name), ZSTR_VAL(key));
1579 return NULL;
1580 }
1581 ZVAL_NEW_REF(data, data);
1582 ZEND_REF_ADD_TYPE_SOURCE(Z_REF_P(data), prop_info);
1583 }
1584 }
1585 return data;
1586 }
1587 /* }}} */
1588
spl_array_it_get_current_key(zend_object_iterator * iter,zval * key)1589 static void spl_array_it_get_current_key(zend_object_iterator *iter, zval *key) /* {{{ */
1590 {
1591 spl_array_object *object = Z_SPLARRAY_P(&iter->data);
1592 HashTable *aht = spl_array_get_hash_table(object);
1593 zend_hash_get_current_key_zval_ex(aht, key, spl_array_get_pos_ptr(aht, object));
1594 }
1595 /* }}} */
1596
spl_array_it_move_forward(zend_object_iterator * iter)1597 static void spl_array_it_move_forward(zend_object_iterator *iter) /* {{{ */
1598 {
1599 spl_array_object *object = Z_SPLARRAY_P(&iter->data);
1600 HashTable *aht = spl_array_get_hash_table(object);
1601 spl_array_next_ex(object, aht);
1602 }
1603 /* }}} */
1604
spl_array_rewind(spl_array_object * intern)1605 static void spl_array_rewind(spl_array_object *intern) /* {{{ */
1606 {
1607 HashTable *aht = spl_array_get_hash_table(intern);
1608
1609 if (intern->ht_iter == (uint32_t)-1) {
1610 spl_array_get_pos_ptr(aht, intern);
1611 } else {
1612 zend_hash_internal_pointer_reset_ex(aht, spl_array_get_pos_ptr(aht, intern));
1613 spl_array_skip_protected(intern, aht);
1614 }
1615 }
1616 /* }}} */
1617
spl_array_it_rewind(zend_object_iterator * iter)1618 static void spl_array_it_rewind(zend_object_iterator *iter) /* {{{ */
1619 {
1620 spl_array_object *object = Z_SPLARRAY_P(&iter->data);
1621 spl_array_rewind(object);
1622 }
1623 /* }}} */
1624
spl_array_it_get_gc(zend_object_iterator * iter,zval ** table,int * n)1625 static HashTable *spl_array_it_get_gc(zend_object_iterator *iter, zval **table, int *n)
1626 {
1627 *n = 1;
1628 *table = &iter->data;
1629 return NULL;
1630 }
1631
1632 /* iterator handler table */
1633 static const zend_object_iterator_funcs spl_array_it_funcs = {
1634 spl_array_it_dtor,
1635 spl_array_it_valid,
1636 spl_array_it_get_current_data,
1637 spl_array_it_get_current_key,
1638 spl_array_it_move_forward,
1639 spl_array_it_rewind,
1640 NULL,
1641 spl_array_it_get_gc,
1642 };
1643
spl_array_get_iterator(zend_class_entry * ce,zval * object,int by_ref)1644 static zend_object_iterator *spl_array_get_iterator(zend_class_entry *ce, zval *object, int by_ref) /* {{{ */
1645 {
1646 spl_array_iterator *iterator = emalloc(sizeof(spl_array_iterator));
1647 zend_iterator_init(&iterator->it);
1648
1649 ZVAL_OBJ_COPY(&iterator->it.data, Z_OBJ_P(object));
1650 iterator->it.funcs = &spl_array_it_funcs;
1651 iterator->by_ref = by_ref;
1652
1653 return &iterator->it;
1654 }
1655 /* }}} */
1656
1657 /* {{{ Constructs a new array iterator from an array or object. */
PHP_METHOD(ArrayIterator,__construct)1658 PHP_METHOD(ArrayIterator, __construct)
1659 {
1660 zval *object = ZEND_THIS;
1661 spl_array_object *intern;
1662 zval *array;
1663 zend_long ar_flags = 0;
1664
1665 if (ZEND_NUM_ARGS() == 0) {
1666 return; /* nothing to do */
1667 }
1668
1669 if (zend_parse_parameters(ZEND_NUM_ARGS(), "|Al", &array, &ar_flags) == FAILURE) {
1670 RETURN_THROWS();
1671 }
1672
1673 intern = Z_SPLARRAY_P(object);
1674
1675 ar_flags &= ~SPL_ARRAY_INT_MASK;
1676
1677 spl_array_set_array(object, intern, array, ar_flags, ZEND_NUM_ARGS() == 1);
1678 }
1679 /* }}} */
1680
1681 /* {{{ Rewind array back to the start */
PHP_METHOD(ArrayIterator,rewind)1682 PHP_METHOD(ArrayIterator, rewind)
1683 {
1684 zval *object = ZEND_THIS;
1685 spl_array_object *intern = Z_SPLARRAY_P(object);
1686
1687 if (zend_parse_parameters_none() == FAILURE) {
1688 RETURN_THROWS();
1689 }
1690
1691 spl_array_rewind(intern);
1692 }
1693 /* }}} */
1694
1695 /* {{{ Seek to position. */
PHP_METHOD(ArrayIterator,seek)1696 PHP_METHOD(ArrayIterator, seek)
1697 {
1698 zend_long opos, position;
1699 zval *object = ZEND_THIS;
1700 spl_array_object *intern = Z_SPLARRAY_P(object);
1701 HashTable *aht = spl_array_get_hash_table(intern);
1702 int result;
1703
1704 if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &position) == FAILURE) {
1705 RETURN_THROWS();
1706 }
1707
1708 opos = position;
1709
1710 if (position >= 0) { /* negative values are not supported */
1711 spl_array_rewind(intern);
1712 result = SUCCESS;
1713
1714 while (position-- > 0 && (result = spl_array_next(intern)) == SUCCESS);
1715
1716 if (result == SUCCESS && zend_hash_has_more_elements_ex(aht, spl_array_get_pos_ptr(aht, intern)) == SUCCESS) {
1717 return; /* ok */
1718 }
1719 }
1720 zend_throw_exception_ex(spl_ce_OutOfBoundsException, 0, "Seek position " ZEND_LONG_FMT " is out of range", opos);
1721 } /* }}} */
1722
1723 /* {{{ Return current array entry */
PHP_METHOD(ArrayIterator,current)1724 PHP_METHOD(ArrayIterator, current)
1725 {
1726 zval *object = ZEND_THIS;
1727 spl_array_object *intern = Z_SPLARRAY_P(object);
1728 zval *entry;
1729 HashTable *aht = spl_array_get_hash_table(intern);
1730
1731 if (zend_parse_parameters_none() == FAILURE) {
1732 RETURN_THROWS();
1733 }
1734
1735 if ((entry = zend_hash_get_current_data_ex(aht, spl_array_get_pos_ptr(aht, intern))) == NULL) {
1736 RETURN_NULL();
1737 }
1738 if (Z_TYPE_P(entry) == IS_INDIRECT) {
1739 entry = Z_INDIRECT_P(entry);
1740 if (Z_TYPE_P(entry) == IS_UNDEF) {
1741 RETURN_NULL();
1742 }
1743 }
1744 RETURN_COPY_DEREF(entry);
1745 }
1746 /* }}} */
1747
spl_array_iterator_key(zval * object,zval * return_value)1748 void spl_array_iterator_key(zval *object, zval *return_value) /* {{{ */
1749 {
1750 spl_array_object *intern = Z_SPLARRAY_P(object);
1751 HashTable *aht = spl_array_get_hash_table(intern);
1752
1753 zend_hash_get_current_key_zval_ex(aht, return_value, spl_array_get_pos_ptr(aht, intern));
1754 }
1755 /* }}} */
1756
1757 /* {{{ Return current array key */
PHP_METHOD(ArrayIterator,key)1758 PHP_METHOD(ArrayIterator, key)
1759 {
1760 if (zend_parse_parameters_none() == FAILURE) {
1761 RETURN_THROWS();
1762 }
1763
1764 spl_array_iterator_key(ZEND_THIS, return_value);
1765 } /* }}} */
1766
1767 /* {{{ Move to next entry */
PHP_METHOD(ArrayIterator,next)1768 PHP_METHOD(ArrayIterator, next)
1769 {
1770 zval *object = ZEND_THIS;
1771 spl_array_object *intern = Z_SPLARRAY_P(object);
1772 HashTable *aht = spl_array_get_hash_table(intern);
1773
1774 if (zend_parse_parameters_none() == FAILURE) {
1775 RETURN_THROWS();
1776 }
1777
1778 spl_array_next_ex(intern, aht);
1779 }
1780 /* }}} */
1781
1782 /* {{{ Check whether array contains more entries */
PHP_METHOD(ArrayIterator,valid)1783 PHP_METHOD(ArrayIterator, valid)
1784 {
1785 zval *object = ZEND_THIS;
1786 spl_array_object *intern = Z_SPLARRAY_P(object);
1787 HashTable *aht = spl_array_get_hash_table(intern);
1788
1789 if (zend_parse_parameters_none() == FAILURE) {
1790 RETURN_THROWS();
1791 }
1792
1793 RETURN_BOOL(zend_hash_has_more_elements_ex(aht, spl_array_get_pos_ptr(aht, intern)) == SUCCESS);
1794 }
1795 /* }}} */
1796
1797 /*** RecursiveArrayIterator methods ***/
1798
1799 /* {{{ Check whether current element has children (e.g. is an array) */
PHP_METHOD(RecursiveArrayIterator,hasChildren)1800 PHP_METHOD(RecursiveArrayIterator, hasChildren)
1801 {
1802 zval *object = ZEND_THIS, *entry;
1803 spl_array_object *intern = Z_SPLARRAY_P(object);
1804 HashTable *aht = spl_array_get_hash_table(intern);
1805
1806 if (zend_parse_parameters_none() == FAILURE) {
1807 RETURN_THROWS();
1808 }
1809
1810 if ((entry = zend_hash_get_current_data_ex(aht, spl_array_get_pos_ptr(aht, intern))) == NULL) {
1811 RETURN_FALSE;
1812 }
1813
1814 if (Z_TYPE_P(entry) == IS_INDIRECT) {
1815 entry = Z_INDIRECT_P(entry);
1816 }
1817
1818 ZVAL_DEREF(entry);
1819 RETURN_BOOL(Z_TYPE_P(entry) == IS_ARRAY || (Z_TYPE_P(entry) == IS_OBJECT && (intern->ar_flags & SPL_ARRAY_CHILD_ARRAYS_ONLY) == 0));
1820 }
1821 /* }}} */
1822
spl_instantiate_child_arg(zend_class_entry * pce,zval * retval,zval * arg1,zval * arg2)1823 static void spl_instantiate_child_arg(zend_class_entry *pce, zval *retval, zval *arg1, zval *arg2) /* {{{ */
1824 {
1825 object_init_ex(retval, pce);
1826 spl_array_object *new_intern = Z_SPLARRAY_P(retval);
1827 /*
1828 * set new_intern->is_child is true to indicate that the object was created by
1829 * RecursiveArrayIterator::getChildren() method.
1830 */
1831 new_intern->is_child = true;
1832
1833 /* find the bucket of parent object. */
1834 new_intern->bucket = (Bucket *)((char *)(arg1) - XtOffsetOf(Bucket, val));;
1835 zend_call_known_instance_method_with_2_params(pce->constructor, Z_OBJ_P(retval), NULL, arg1, arg2);
1836 }
1837 /* }}} */
1838
1839 /* {{{ Create a sub iterator for the current element (same class as $this) */
PHP_METHOD(RecursiveArrayIterator,getChildren)1840 PHP_METHOD(RecursiveArrayIterator, getChildren)
1841 {
1842 zval *object = ZEND_THIS, *entry, flags;
1843 spl_array_object *intern = Z_SPLARRAY_P(object);
1844 HashTable *aht = spl_array_get_hash_table(intern);
1845
1846 if (zend_parse_parameters_none() == FAILURE) {
1847 RETURN_THROWS();
1848 }
1849
1850 if ((entry = zend_hash_get_current_data_ex(aht, spl_array_get_pos_ptr(aht, intern))) == NULL) {
1851 RETURN_NULL();
1852 }
1853
1854 if (Z_TYPE_P(entry) == IS_INDIRECT) {
1855 entry = Z_INDIRECT_P(entry);
1856 }
1857
1858 ZVAL_DEREF(entry);
1859 if (Z_TYPE_P(entry) == IS_OBJECT) {
1860 if ((intern->ar_flags & SPL_ARRAY_CHILD_ARRAYS_ONLY) != 0) {
1861 RETURN_NULL();
1862 }
1863 if (instanceof_function(Z_OBJCE_P(entry), Z_OBJCE_P(ZEND_THIS))) {
1864 RETURN_OBJ_COPY(Z_OBJ_P(entry));
1865 }
1866 }
1867
1868 ZVAL_LONG(&flags, intern->ar_flags);
1869 spl_instantiate_child_arg(Z_OBJCE_P(ZEND_THIS), return_value, entry, &flags);
1870 }
1871 /* }}} */
1872
1873 /* {{{ PHP_MINIT_FUNCTION(spl_array) */
PHP_MINIT_FUNCTION(spl_array)1874 PHP_MINIT_FUNCTION(spl_array)
1875 {
1876 spl_ce_ArrayObject = register_class_ArrayObject(zend_ce_aggregate, zend_ce_arrayaccess, zend_ce_serializable, zend_ce_countable);
1877 spl_ce_ArrayObject->create_object = spl_array_object_new;
1878 spl_ce_ArrayObject->default_object_handlers = &spl_handler_ArrayObject;
1879
1880 memcpy(&spl_handler_ArrayObject, &std_object_handlers, sizeof(zend_object_handlers));
1881
1882 spl_handler_ArrayObject.offset = XtOffsetOf(spl_array_object, std);
1883
1884 spl_handler_ArrayObject.clone_obj = spl_array_object_clone;
1885 spl_handler_ArrayObject.read_dimension = spl_array_read_dimension;
1886 spl_handler_ArrayObject.write_dimension = spl_array_write_dimension;
1887 spl_handler_ArrayObject.unset_dimension = spl_array_unset_dimension;
1888 spl_handler_ArrayObject.has_dimension = spl_array_has_dimension;
1889 spl_handler_ArrayObject.count_elements = spl_array_object_count_elements;
1890
1891 spl_handler_ArrayObject.get_properties_for = spl_array_get_properties_for;
1892 spl_handler_ArrayObject.get_gc = spl_array_get_gc;
1893 spl_handler_ArrayObject.read_property = spl_array_read_property;
1894 spl_handler_ArrayObject.write_property = spl_array_write_property;
1895 spl_handler_ArrayObject.get_property_ptr_ptr = spl_array_get_property_ptr_ptr;
1896 spl_handler_ArrayObject.has_property = spl_array_has_property;
1897 spl_handler_ArrayObject.unset_property = spl_array_unset_property;
1898
1899 spl_handler_ArrayObject.compare = spl_array_compare_objects;
1900 spl_handler_ArrayObject.free_obj = spl_array_object_free_storage;
1901
1902 spl_ce_ArrayIterator = register_class_ArrayIterator(spl_ce_SeekableIterator, zend_ce_arrayaccess, zend_ce_serializable, zend_ce_countable);
1903 spl_ce_ArrayIterator->create_object = spl_array_object_new;
1904 spl_ce_ArrayIterator->default_object_handlers = &spl_handler_ArrayObject;
1905 spl_ce_ArrayIterator->get_iterator = spl_array_get_iterator;
1906
1907 spl_ce_RecursiveArrayIterator = register_class_RecursiveArrayIterator(spl_ce_ArrayIterator, spl_ce_RecursiveIterator);
1908 spl_ce_RecursiveArrayIterator->create_object = spl_array_object_new;
1909 spl_ce_RecursiveArrayIterator->get_iterator = spl_array_get_iterator;
1910
1911 return SUCCESS;
1912 }
1913 /* }}} */
1914