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 if (UNEXPECTED(Z_OBJCE_P(array)->ce_flags & ZEND_ACC_ENUM)) {
979 zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0,
980 "Enums are not compatible with %s",
981 ZSTR_VAL(intern->std.ce->name));
982 ZEND_ASSERT(Z_TYPE(garbage) == IS_UNDEF);
983 return;
984 }
985 ZVAL_COPY_VALUE(&garbage, &intern->array);
986 ZVAL_COPY(&intern->array, array);
987 }
988 }
989
990 intern->ar_flags &= ~SPL_ARRAY_IS_SELF & ~SPL_ARRAY_USE_OTHER;
991 intern->ar_flags |= ar_flags;
992 if (intern->ht_iter != (uint32_t)-1) {
993 zend_hash_iterator_del(intern->ht_iter);
994 intern->ht_iter = (uint32_t)-1;
995 }
996
997 zval_ptr_dtor(&garbage);
998 }
999 /* }}} */
1000
1001 /* {{{ Constructs a new array object from an array or object. */
PHP_METHOD(ArrayObject,__construct)1002 PHP_METHOD(ArrayObject, __construct)
1003 {
1004 zval *object = ZEND_THIS;
1005 spl_array_object *intern;
1006 zval *array;
1007 zend_long ar_flags = 0;
1008 zend_class_entry *ce_get_iterator = spl_ce_ArrayIterator;
1009
1010 if (ZEND_NUM_ARGS() == 0) {
1011 return; /* nothing to do */
1012 }
1013
1014 if (zend_parse_parameters(ZEND_NUM_ARGS(), "|AlC", &array, &ar_flags, &ce_get_iterator) == FAILURE) {
1015 RETURN_THROWS();
1016 }
1017
1018 intern = Z_SPLARRAY_P(object);
1019
1020 if (ZEND_NUM_ARGS() > 2) {
1021 intern->ce_get_iterator = ce_get_iterator;
1022 }
1023
1024 ar_flags &= ~SPL_ARRAY_INT_MASK;
1025
1026 spl_array_set_array(object, intern, array, ar_flags, ZEND_NUM_ARGS() == 1);
1027 }
1028 /* }}} */
1029
1030 /* {{{ Set the class used in getIterator. */
PHP_METHOD(ArrayObject,setIteratorClass)1031 PHP_METHOD(ArrayObject, setIteratorClass)
1032 {
1033 zval *object = ZEND_THIS;
1034 spl_array_object *intern = Z_SPLARRAY_P(object);
1035 zend_class_entry *ce_get_iterator = spl_ce_ArrayIterator;
1036
1037 ZEND_PARSE_PARAMETERS_START(1, 1)
1038 Z_PARAM_CLASS(ce_get_iterator)
1039 ZEND_PARSE_PARAMETERS_END();
1040
1041 intern->ce_get_iterator = ce_get_iterator;
1042 }
1043 /* }}} */
1044
1045 /* {{{ Get the class used in getIterator. */
PHP_METHOD(ArrayObject,getIteratorClass)1046 PHP_METHOD(ArrayObject, getIteratorClass)
1047 {
1048 zval *object = ZEND_THIS;
1049 spl_array_object *intern = Z_SPLARRAY_P(object);
1050
1051 if (zend_parse_parameters_none() == FAILURE) {
1052 RETURN_THROWS();
1053 }
1054
1055 zend_string_addref(intern->ce_get_iterator->name);
1056 RETURN_STR(intern->ce_get_iterator->name);
1057 }
1058 /* }}} */
1059
1060 /* {{{ Get flags */
PHP_METHOD(ArrayObject,getFlags)1061 PHP_METHOD(ArrayObject, getFlags)
1062 {
1063 zval *object = ZEND_THIS;
1064 spl_array_object *intern = Z_SPLARRAY_P(object);
1065
1066 if (zend_parse_parameters_none() == FAILURE) {
1067 RETURN_THROWS();
1068 }
1069
1070 RETURN_LONG(intern->ar_flags & ~SPL_ARRAY_INT_MASK);
1071 }
1072 /* }}} */
1073
1074 /* {{{ Set flags */
PHP_METHOD(ArrayObject,setFlags)1075 PHP_METHOD(ArrayObject, setFlags)
1076 {
1077 zval *object = ZEND_THIS;
1078 spl_array_object *intern = Z_SPLARRAY_P(object);
1079 zend_long ar_flags = 0;
1080
1081 if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &ar_flags) == FAILURE) {
1082 RETURN_THROWS();
1083 }
1084
1085 intern->ar_flags = (intern->ar_flags & SPL_ARRAY_INT_MASK) | (ar_flags & ~SPL_ARRAY_INT_MASK);
1086 }
1087 /* }}} */
1088
1089 /* {{{ 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)1090 PHP_METHOD(ArrayObject, exchangeArray)
1091 {
1092 zval *object = ZEND_THIS, *array;
1093 spl_array_object *intern = Z_SPLARRAY_P(object);
1094
1095 if (zend_parse_parameters(ZEND_NUM_ARGS(), "A", &array) == FAILURE) {
1096 RETURN_THROWS();
1097 }
1098
1099 if (intern->nApplyCount > 0) {
1100 zend_throw_error(NULL, "Modification of ArrayObject during sorting is prohibited");
1101 RETURN_THROWS();
1102 }
1103
1104 RETVAL_ARR(zend_array_dup(spl_array_get_hash_table(intern)));
1105 spl_array_set_array(object, intern, array, 0L, 1);
1106 }
1107 /* }}} */
1108
1109 /* {{{ Create a new iterator from a ArrayObject instance */
PHP_METHOD(ArrayObject,getIterator)1110 PHP_METHOD(ArrayObject, getIterator)
1111 {
1112 zval *object = ZEND_THIS;
1113 spl_array_object *intern = Z_SPLARRAY_P(object);
1114
1115 if (zend_parse_parameters_none() == FAILURE) {
1116 RETURN_THROWS();
1117 }
1118
1119 RETURN_OBJ(spl_array_object_new_ex(intern->ce_get_iterator, Z_OBJ_P(object), 0));
1120 }
1121 /* }}} */
1122
spl_array_object_count_elements_helper(spl_array_object * intern)1123 static zend_long spl_array_object_count_elements_helper(spl_array_object *intern) /* {{{ */
1124 {
1125 HashTable *aht = spl_array_get_hash_table(intern);
1126 if (spl_array_is_object(intern)) {
1127 zend_long count = 0;
1128 zend_string *key;
1129 zval *val;
1130 /* Count public/dynamic properties */
1131 ZEND_HASH_FOREACH_STR_KEY_VAL(aht, key, val) {
1132 if (Z_TYPE_P(val) == IS_INDIRECT) {
1133 if (Z_TYPE_P(Z_INDIRECT_P(val)) == IS_UNDEF) continue;
1134 if (key && ZSTR_VAL(key)[0] == '\0') continue;
1135 }
1136 count++;
1137 } ZEND_HASH_FOREACH_END();
1138 return count;
1139 } else {
1140 return zend_hash_num_elements(aht);
1141 }
1142 } /* }}} */
1143
spl_array_object_count_elements(zend_object * object,zend_long * count)1144 static zend_result spl_array_object_count_elements(zend_object *object, zend_long *count) /* {{{ */
1145 {
1146 spl_array_object *intern = spl_array_from_obj(object);
1147
1148 if (intern->fptr_count) {
1149 zval rv;
1150 zend_call_method_with_0_params(object, intern->std.ce, &intern->fptr_count, "count", &rv);
1151 if (Z_TYPE(rv) != IS_UNDEF) {
1152 *count = zval_get_long(&rv);
1153 zval_ptr_dtor(&rv);
1154 return SUCCESS;
1155 }
1156 *count = 0;
1157 return FAILURE;
1158 }
1159 *count = spl_array_object_count_elements_helper(intern);
1160 return SUCCESS;
1161 } /* }}} */
1162
1163 /* {{{ Return the number of elements in the Iterator. */
PHP_METHOD(ArrayObject,count)1164 PHP_METHOD(ArrayObject, count)
1165 {
1166 spl_array_object *intern = Z_SPLARRAY_P(ZEND_THIS);
1167
1168 if (zend_parse_parameters_none() == FAILURE) {
1169 RETURN_THROWS();
1170 }
1171
1172 RETURN_LONG(spl_array_object_count_elements_helper(intern));
1173 } /* }}} */
1174
spl_array_method(INTERNAL_FUNCTION_PARAMETERS,char * fname,size_t fname_len,int use_arg)1175 static void spl_array_method(INTERNAL_FUNCTION_PARAMETERS, char *fname, size_t fname_len, int use_arg) /* {{{ */
1176 {
1177 spl_array_object *intern = Z_SPLARRAY_P(ZEND_THIS);
1178 HashTable **ht_ptr = spl_array_get_hash_table_ptr(intern);
1179 HashTable *aht = *ht_ptr;
1180 zval function_name, params[2], *arg = NULL;
1181
1182 ZVAL_STRINGL(&function_name, fname, fname_len);
1183
1184 ZVAL_NEW_EMPTY_REF(¶ms[0]);
1185 ZVAL_ARR(Z_REFVAL(params[0]), aht);
1186 GC_ADDREF(aht);
1187
1188 if (!use_arg) {
1189 if (zend_parse_parameters_none() == FAILURE) {
1190 goto exit;
1191 }
1192
1193 intern->nApplyCount++;
1194 call_user_function(EG(function_table), NULL, &function_name, return_value, 1, params);
1195 intern->nApplyCount--;
1196 } else if (use_arg == SPL_ARRAY_METHOD_SORT_FLAGS_ARG) {
1197 zend_long sort_flags = 0;
1198 if (zend_parse_parameters(ZEND_NUM_ARGS(), "|l", &sort_flags) == FAILURE) {
1199 goto exit;
1200 }
1201 ZVAL_LONG(¶ms[1], sort_flags);
1202 intern->nApplyCount++;
1203 call_user_function(EG(function_table), NULL, &function_name, return_value, 2, params);
1204 intern->nApplyCount--;
1205 } else {
1206 if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &arg) == FAILURE) {
1207 goto exit;
1208 }
1209 ZVAL_COPY_VALUE(¶ms[1], arg);
1210 intern->nApplyCount++;
1211 call_user_function(EG(function_table), NULL, &function_name, return_value, 2, params);
1212 intern->nApplyCount--;
1213 }
1214
1215 exit:
1216 {
1217 zval *ht_zv = Z_REFVAL(params[0]);
1218 zend_array_release(*ht_ptr);
1219 SEPARATE_ARRAY(ht_zv);
1220 *ht_ptr = Z_ARRVAL_P(ht_zv);
1221 ZVAL_NULL(ht_zv);
1222 zval_ptr_dtor(¶ms[0]);
1223 zend_string_free(Z_STR(function_name));
1224 }
1225 } /* }}} */
1226
1227 #define SPL_ARRAY_METHOD(cname, fname, use_arg) \
1228 PHP_METHOD(cname, fname) \
1229 { \
1230 spl_array_method(INTERNAL_FUNCTION_PARAM_PASSTHRU, #fname, sizeof(#fname)-1, use_arg); \
1231 }
1232
1233 /* {{{ Sort the entries by values. */
SPL_ARRAY_METHOD(ArrayObject,asort,SPL_ARRAY_METHOD_SORT_FLAGS_ARG)1234 SPL_ARRAY_METHOD(ArrayObject, asort, SPL_ARRAY_METHOD_SORT_FLAGS_ARG) /* }}} */
1235
1236 /* {{{ Sort the entries by key. */
1237 SPL_ARRAY_METHOD(ArrayObject, ksort, SPL_ARRAY_METHOD_SORT_FLAGS_ARG) /* }}} */
1238
1239 /* {{{ Sort the entries by values user defined function. */
1240 SPL_ARRAY_METHOD(ArrayObject, uasort, SPL_ARRAY_METHOD_CALLBACK_ARG) /* }}} */
1241
1242 /* {{{ Sort the entries by key using user defined function. */
1243 SPL_ARRAY_METHOD(ArrayObject, uksort, SPL_ARRAY_METHOD_CALLBACK_ARG) /* }}} */
1244
1245 /* {{{ Sort the entries by values using "natural order" algorithm. */
1246 SPL_ARRAY_METHOD(ArrayObject, natsort, SPL_ARRAY_METHOD_NO_ARG) /* }}} */
1247
1248 /* {{{ Sort the entries by key using case insensitive "natural order" algorithm. */
1249 SPL_ARRAY_METHOD(ArrayObject, natcasesort, SPL_ARRAY_METHOD_NO_ARG) /* }}} */
1250
1251 /* {{{ Serialize the object */
1252 PHP_METHOD(ArrayObject, serialize)
1253 {
1254 zval *object = ZEND_THIS;
1255 spl_array_object *intern = Z_SPLARRAY_P(object);
1256 zval members, flags;
1257 php_serialize_data_t var_hash;
1258 smart_str buf = {0};
1259
1260 if (zend_parse_parameters_none() == FAILURE) {
1261 RETURN_THROWS();
1262 }
1263
1264 PHP_VAR_SERIALIZE_INIT(var_hash);
1265
1266 ZVAL_LONG(&flags, (intern->ar_flags & SPL_ARRAY_CLONE_MASK));
1267
1268 /* storage */
1269 smart_str_appendl(&buf, "x:", 2);
1270 php_var_serialize(&buf, &flags, &var_hash);
1271
1272 if (!(intern->ar_flags & SPL_ARRAY_IS_SELF)) {
1273 php_var_serialize(&buf, &intern->array, &var_hash);
1274 smart_str_appendc(&buf, ';');
1275 }
1276
1277 /* members */
1278 smart_str_appendl(&buf, "m:", 2);
1279
1280 ZVAL_ARR(&members, zend_std_get_properties_ex(&intern->std));
1281
1282 php_var_serialize(&buf, &members, &var_hash); /* finishes the string */
1283
1284 /* done */
1285 PHP_VAR_SERIALIZE_DESTROY(var_hash);
1286
1287 RETURN_STR(smart_str_extract(&buf));
1288 } /* }}} */
1289
1290 /* {{{ unserialize the object */
PHP_METHOD(ArrayObject,unserialize)1291 PHP_METHOD(ArrayObject, unserialize)
1292 {
1293 zval *object = ZEND_THIS;
1294 spl_array_object *intern = Z_SPLARRAY_P(object);
1295
1296 char *buf;
1297 size_t buf_len;
1298 const unsigned char *p, *s;
1299 php_unserialize_data_t var_hash;
1300 zval *members, *zflags, *array;
1301 zend_long flags;
1302
1303 if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &buf, &buf_len) == FAILURE) {
1304 RETURN_THROWS();
1305 }
1306
1307 if (buf_len == 0) {
1308 return;
1309 }
1310
1311 if (intern->nApplyCount > 0) {
1312 zend_throw_error(NULL, "Modification of ArrayObject during sorting is prohibited");
1313 RETURN_THROWS();
1314 }
1315
1316 /* storage */
1317 s = p = (const unsigned char*)buf;
1318 PHP_VAR_UNSERIALIZE_INIT(var_hash);
1319
1320 if (*p!= 'x' || *++p != ':') {
1321 goto outexcept;
1322 }
1323 ++p;
1324
1325 zflags = var_tmp_var(&var_hash);
1326 if (!php_var_unserialize(zflags, &p, s + buf_len, &var_hash) || Z_TYPE_P(zflags) != IS_LONG) {
1327 goto outexcept;
1328 }
1329
1330 --p; /* for ';' */
1331 flags = Z_LVAL_P(zflags);
1332 /* flags needs to be verified and we also need to verify whether the next
1333 * thing we get is ';'. After that we require an 'm' or something else
1334 * where 'm' stands for members and anything else should be an array. If
1335 * neither 'a' or 'm' follows we have an error. */
1336
1337 if (*p != ';') {
1338 goto outexcept;
1339 }
1340 ++p;
1341
1342 if (flags & SPL_ARRAY_IS_SELF) {
1343 /* If IS_SELF is used, the flags are not followed by an array/object */
1344 intern->ar_flags &= ~SPL_ARRAY_CLONE_MASK;
1345 intern->ar_flags |= flags & SPL_ARRAY_CLONE_MASK;
1346 zval_ptr_dtor(&intern->array);
1347 ZVAL_UNDEF(&intern->array);
1348 } else {
1349 if (*p!='a' && *p!='O' && *p!='C' && *p!='r') {
1350 goto outexcept;
1351 }
1352
1353 array = var_tmp_var(&var_hash);
1354 if (!php_var_unserialize(array, &p, s + buf_len, &var_hash)
1355 || (Z_TYPE_P(array) != IS_ARRAY && Z_TYPE_P(array) != IS_OBJECT)) {
1356 goto outexcept;
1357 }
1358
1359 intern->ar_flags &= ~SPL_ARRAY_CLONE_MASK;
1360 intern->ar_flags |= flags & SPL_ARRAY_CLONE_MASK;
1361
1362 if (Z_TYPE_P(array) == IS_ARRAY) {
1363 zval_ptr_dtor(&intern->array);
1364 ZVAL_COPY_VALUE(&intern->array, array);
1365 ZVAL_NULL(array);
1366 SEPARATE_ARRAY(&intern->array);
1367 } else {
1368 spl_array_set_array(object, intern, array, 0L, 1);
1369 }
1370
1371 if (*p != ';') {
1372 goto outexcept;
1373 }
1374 ++p;
1375 }
1376
1377 /* members */
1378 if (*p!= 'm' || *++p != ':') {
1379 goto outexcept;
1380 }
1381 ++p;
1382
1383 members = var_tmp_var(&var_hash);
1384 if (!php_var_unserialize(members, &p, s + buf_len, &var_hash) || Z_TYPE_P(members) != IS_ARRAY) {
1385 goto outexcept;
1386 }
1387
1388 /* copy members */
1389 object_properties_load(&intern->std, Z_ARRVAL_P(members));
1390
1391 /* done reading $serialized */
1392 PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
1393 return;
1394
1395 outexcept:
1396 PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
1397 zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0, "Error at offset " ZEND_LONG_FMT " of %zd bytes", (zend_long)((char*)p - buf), buf_len);
1398 RETURN_THROWS();
1399
1400 } /* }}} */
1401
1402 /* {{{ */
PHP_METHOD(ArrayObject,__serialize)1403 PHP_METHOD(ArrayObject, __serialize)
1404 {
1405 spl_array_object *intern = Z_SPLARRAY_P(ZEND_THIS);
1406 zval tmp;
1407
1408 if (zend_parse_parameters_none() == FAILURE) {
1409 RETURN_THROWS();
1410 }
1411
1412 array_init(return_value);
1413
1414 /* flags */
1415 ZVAL_LONG(&tmp, (intern->ar_flags & SPL_ARRAY_CLONE_MASK));
1416 zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &tmp);
1417
1418 /* storage */
1419 if (intern->ar_flags & SPL_ARRAY_IS_SELF) {
1420 ZVAL_NULL(&tmp);
1421 } else {
1422 ZVAL_COPY(&tmp, &intern->array);
1423 }
1424 zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &tmp);
1425
1426 /* members */
1427 ZVAL_ARR(&tmp, zend_proptable_to_symtable(
1428 zend_std_get_properties(&intern->std), /* always_duplicate */ 1));
1429 zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &tmp);
1430
1431 /* iterator class */
1432 if (intern->ce_get_iterator == spl_ce_ArrayIterator) {
1433 ZVAL_NULL(&tmp);
1434 } else {
1435 ZVAL_STR_COPY(&tmp, intern->ce_get_iterator->name);
1436 }
1437 zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &tmp);
1438 }
1439 /* }}} */
1440
1441
1442 /* {{{ */
PHP_METHOD(ArrayObject,__unserialize)1443 PHP_METHOD(ArrayObject, __unserialize)
1444 {
1445 spl_array_object *intern = Z_SPLARRAY_P(ZEND_THIS);
1446 HashTable *data;
1447 zval *flags_zv, *storage_zv, *members_zv, *iterator_class_zv;
1448 zend_long flags;
1449
1450 if (zend_parse_parameters(ZEND_NUM_ARGS(), "h", &data) == FAILURE) {
1451 RETURN_THROWS();
1452 }
1453
1454 flags_zv = zend_hash_index_find(data, 0);
1455 storage_zv = zend_hash_index_find(data, 1);
1456 members_zv = zend_hash_index_find(data, 2);
1457 iterator_class_zv = zend_hash_index_find(data, 3);
1458
1459 if (!flags_zv || !storage_zv || !members_zv ||
1460 Z_TYPE_P(flags_zv) != IS_LONG || Z_TYPE_P(members_zv) != IS_ARRAY ||
1461 (iterator_class_zv && (Z_TYPE_P(iterator_class_zv) != IS_NULL &&
1462 Z_TYPE_P(iterator_class_zv) != IS_STRING))) {
1463 zend_throw_exception(spl_ce_UnexpectedValueException,
1464 "Incomplete or ill-typed serialization data", 0);
1465 RETURN_THROWS();
1466 }
1467
1468 flags = Z_LVAL_P(flags_zv);
1469 intern->ar_flags &= ~SPL_ARRAY_CLONE_MASK;
1470 intern->ar_flags |= flags & SPL_ARRAY_CLONE_MASK;
1471
1472 if (flags & SPL_ARRAY_IS_SELF) {
1473 zval_ptr_dtor(&intern->array);
1474 ZVAL_UNDEF(&intern->array);
1475 } else {
1476 if (Z_TYPE_P(storage_zv) != IS_OBJECT && Z_TYPE_P(storage_zv) != IS_ARRAY) {
1477 /* TODO Use UnexpectedValueException instead? And better error message? */
1478 zend_throw_exception(spl_ce_InvalidArgumentException, "Passed variable is not an array or object", 0);
1479 RETURN_THROWS();
1480 }
1481 spl_array_set_array(ZEND_THIS, intern, storage_zv, 0L, 1);
1482 }
1483
1484 object_properties_load(&intern->std, Z_ARRVAL_P(members_zv));
1485 if (EG(exception)) {
1486 RETURN_THROWS();
1487 }
1488
1489 if (iterator_class_zv && Z_TYPE_P(iterator_class_zv) == IS_STRING) {
1490 zend_class_entry *ce = zend_lookup_class(Z_STR_P(iterator_class_zv));
1491
1492 if (!ce) {
1493 zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
1494 "Cannot deserialize ArrayObject with iterator class '%s'; no such class exists",
1495 ZSTR_VAL(Z_STR_P(iterator_class_zv)));
1496 RETURN_THROWS();
1497 }
1498
1499 if (!instanceof_function(ce, zend_ce_iterator)) {
1500 zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
1501 "Cannot deserialize ArrayObject with iterator class '%s'; this class does not implement the Iterator interface",
1502 ZSTR_VAL(Z_STR_P(iterator_class_zv)));
1503 RETURN_THROWS();
1504 }
1505
1506 intern->ce_get_iterator = ce;
1507 }
1508 }
1509 /* }}} */
1510
1511 /* {{{ */
PHP_METHOD(ArrayObject,__debugInfo)1512 PHP_METHOD(ArrayObject, __debugInfo)
1513 {
1514 if (zend_parse_parameters_none() == FAILURE) {
1515 RETURN_THROWS();
1516 }
1517
1518 RETURN_ARR(spl_array_get_debug_info(Z_OBJ_P(ZEND_THIS)));
1519 } /* }}} */
1520
1521 /*** ArrayIterator class ***/
1522 typedef struct _spl_array_iterator {
1523 zend_object_iterator it;
1524 bool by_ref;
1525 } spl_array_iterator;
1526
spl_array_next_ex(spl_array_object * intern,HashTable * aht)1527 static zend_result spl_array_next_ex(spl_array_object *intern, HashTable *aht) /* {{{ */
1528 {
1529 uint32_t *pos_ptr = spl_array_get_pos_ptr(aht, intern);
1530
1531 zend_hash_move_forward_ex(aht, pos_ptr);
1532 if (spl_array_is_object(intern)) {
1533 return spl_array_skip_protected(intern, aht);
1534 } else {
1535 return zend_hash_has_more_elements_ex(aht, pos_ptr);
1536 }
1537 } /* }}} */
1538
spl_array_next(spl_array_object * intern)1539 static zend_result spl_array_next(spl_array_object *intern) /* {{{ */
1540 {
1541 HashTable *aht = spl_array_get_hash_table(intern);
1542
1543 return spl_array_next_ex(intern, aht);
1544
1545 } /* }}} */
1546
spl_array_it_dtor(zend_object_iterator * iter)1547 static void spl_array_it_dtor(zend_object_iterator *iter) /* {{{ */
1548 {
1549 zval_ptr_dtor(&iter->data);
1550 }
1551 /* }}} */
1552
spl_array_it_valid(zend_object_iterator * iter)1553 static zend_result spl_array_it_valid(zend_object_iterator *iter) /* {{{ */
1554 {
1555 spl_array_object *object = Z_SPLARRAY_P(&iter->data);
1556 HashTable *aht = spl_array_get_hash_table(object);
1557 return zend_hash_has_more_elements_ex(aht, spl_array_get_pos_ptr(aht, object));
1558 }
1559 /* }}} */
1560
spl_array_it_get_current_data(zend_object_iterator * iter)1561 static zval *spl_array_it_get_current_data(zend_object_iterator *iter) /* {{{ */
1562 {
1563 spl_array_iterator *array_iter = (spl_array_iterator*)iter;
1564 spl_array_object *object = Z_SPLARRAY_P(&iter->data);
1565 HashTable *aht = spl_array_get_hash_table(object);
1566 zval *data = zend_hash_get_current_data_ex(aht, spl_array_get_pos_ptr(aht, object));
1567 if (data && Z_TYPE_P(data) == IS_INDIRECT) {
1568 data = Z_INDIRECT_P(data);
1569 }
1570 // ZEND_FE_FETCH_RW converts the value to a reference but doesn't know the source is a property.
1571 // Typed properties must add a type source to the reference, and readonly properties must fail.
1572 if (array_iter->by_ref
1573 && Z_TYPE_P(data) != IS_REFERENCE
1574 && Z_TYPE(object->array) == IS_OBJECT
1575 && !(object->ar_flags & (SPL_ARRAY_IS_SELF|SPL_ARRAY_USE_OTHER))) {
1576 zend_string *key;
1577 zend_hash_get_current_key_ex(aht, &key, NULL, spl_array_get_pos_ptr(aht, object));
1578 zend_class_entry *ce = Z_OBJCE(object->array);
1579 zend_property_info *prop_info = zend_get_property_info(ce, key, true);
1580 ZEND_ASSERT(prop_info != ZEND_WRONG_PROPERTY_INFO);
1581 if (EXPECTED(prop_info != NULL) && ZEND_TYPE_IS_SET(prop_info->type)) {
1582 if (prop_info->flags & ZEND_ACC_READONLY) {
1583 zend_throw_error(NULL,
1584 "Cannot acquire reference to readonly property %s::$%s",
1585 ZSTR_VAL(prop_info->ce->name), ZSTR_VAL(key));
1586 return NULL;
1587 }
1588 ZVAL_NEW_REF(data, data);
1589 ZEND_REF_ADD_TYPE_SOURCE(Z_REF_P(data), prop_info);
1590 }
1591 }
1592 return data;
1593 }
1594 /* }}} */
1595
spl_array_it_get_current_key(zend_object_iterator * iter,zval * key)1596 static void spl_array_it_get_current_key(zend_object_iterator *iter, zval *key) /* {{{ */
1597 {
1598 spl_array_object *object = Z_SPLARRAY_P(&iter->data);
1599 HashTable *aht = spl_array_get_hash_table(object);
1600 zend_hash_get_current_key_zval_ex(aht, key, spl_array_get_pos_ptr(aht, object));
1601 }
1602 /* }}} */
1603
spl_array_it_move_forward(zend_object_iterator * iter)1604 static void spl_array_it_move_forward(zend_object_iterator *iter) /* {{{ */
1605 {
1606 spl_array_object *object = Z_SPLARRAY_P(&iter->data);
1607 HashTable *aht = spl_array_get_hash_table(object);
1608 spl_array_next_ex(object, aht);
1609 }
1610 /* }}} */
1611
spl_array_rewind(spl_array_object * intern)1612 static void spl_array_rewind(spl_array_object *intern) /* {{{ */
1613 {
1614 HashTable *aht = spl_array_get_hash_table(intern);
1615
1616 if (intern->ht_iter == (uint32_t)-1) {
1617 spl_array_get_pos_ptr(aht, intern);
1618 } else {
1619 zend_hash_internal_pointer_reset_ex(aht, spl_array_get_pos_ptr(aht, intern));
1620 spl_array_skip_protected(intern, aht);
1621 }
1622 }
1623 /* }}} */
1624
spl_array_it_rewind(zend_object_iterator * iter)1625 static void spl_array_it_rewind(zend_object_iterator *iter) /* {{{ */
1626 {
1627 spl_array_object *object = Z_SPLARRAY_P(&iter->data);
1628 spl_array_rewind(object);
1629 }
1630 /* }}} */
1631
spl_array_it_get_gc(zend_object_iterator * iter,zval ** table,int * n)1632 static HashTable *spl_array_it_get_gc(zend_object_iterator *iter, zval **table, int *n)
1633 {
1634 *n = 1;
1635 *table = &iter->data;
1636 return NULL;
1637 }
1638
1639 /* iterator handler table */
1640 static const zend_object_iterator_funcs spl_array_it_funcs = {
1641 spl_array_it_dtor,
1642 spl_array_it_valid,
1643 spl_array_it_get_current_data,
1644 spl_array_it_get_current_key,
1645 spl_array_it_move_forward,
1646 spl_array_it_rewind,
1647 NULL,
1648 spl_array_it_get_gc,
1649 };
1650
spl_array_get_iterator(zend_class_entry * ce,zval * object,int by_ref)1651 static zend_object_iterator *spl_array_get_iterator(zend_class_entry *ce, zval *object, int by_ref) /* {{{ */
1652 {
1653 spl_array_iterator *iterator = emalloc(sizeof(spl_array_iterator));
1654 zend_iterator_init(&iterator->it);
1655
1656 ZVAL_OBJ_COPY(&iterator->it.data, Z_OBJ_P(object));
1657 iterator->it.funcs = &spl_array_it_funcs;
1658 iterator->by_ref = by_ref;
1659
1660 return &iterator->it;
1661 }
1662 /* }}} */
1663
1664 /* {{{ Constructs a new array iterator from an array or object. */
PHP_METHOD(ArrayIterator,__construct)1665 PHP_METHOD(ArrayIterator, __construct)
1666 {
1667 zval *object = ZEND_THIS;
1668 spl_array_object *intern;
1669 zval *array;
1670 zend_long ar_flags = 0;
1671
1672 if (ZEND_NUM_ARGS() == 0) {
1673 return; /* nothing to do */
1674 }
1675
1676 if (zend_parse_parameters(ZEND_NUM_ARGS(), "|Al", &array, &ar_flags) == FAILURE) {
1677 RETURN_THROWS();
1678 }
1679
1680 intern = Z_SPLARRAY_P(object);
1681
1682 ar_flags &= ~SPL_ARRAY_INT_MASK;
1683
1684 spl_array_set_array(object, intern, array, ar_flags, ZEND_NUM_ARGS() == 1);
1685 }
1686 /* }}} */
1687
1688 /* {{{ Rewind array back to the start */
PHP_METHOD(ArrayIterator,rewind)1689 PHP_METHOD(ArrayIterator, rewind)
1690 {
1691 zval *object = ZEND_THIS;
1692 spl_array_object *intern = Z_SPLARRAY_P(object);
1693
1694 if (zend_parse_parameters_none() == FAILURE) {
1695 RETURN_THROWS();
1696 }
1697
1698 spl_array_rewind(intern);
1699 }
1700 /* }}} */
1701
1702 /* {{{ Seek to position. */
PHP_METHOD(ArrayIterator,seek)1703 PHP_METHOD(ArrayIterator, seek)
1704 {
1705 zend_long opos, position;
1706 zval *object = ZEND_THIS;
1707 spl_array_object *intern = Z_SPLARRAY_P(object);
1708 HashTable *aht = spl_array_get_hash_table(intern);
1709 int result;
1710
1711 if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &position) == FAILURE) {
1712 RETURN_THROWS();
1713 }
1714
1715 opos = position;
1716
1717 if (position >= 0) { /* negative values are not supported */
1718 spl_array_rewind(intern);
1719 result = SUCCESS;
1720
1721 while (position-- > 0 && (result = spl_array_next(intern)) == SUCCESS);
1722
1723 if (result == SUCCESS && zend_hash_has_more_elements_ex(aht, spl_array_get_pos_ptr(aht, intern)) == SUCCESS) {
1724 return; /* ok */
1725 }
1726 }
1727 zend_throw_exception_ex(spl_ce_OutOfBoundsException, 0, "Seek position " ZEND_LONG_FMT " is out of range", opos);
1728 } /* }}} */
1729
1730 /* {{{ Return current array entry */
PHP_METHOD(ArrayIterator,current)1731 PHP_METHOD(ArrayIterator, current)
1732 {
1733 zval *object = ZEND_THIS;
1734 spl_array_object *intern = Z_SPLARRAY_P(object);
1735 zval *entry;
1736 HashTable *aht = spl_array_get_hash_table(intern);
1737
1738 if (zend_parse_parameters_none() == FAILURE) {
1739 RETURN_THROWS();
1740 }
1741
1742 if ((entry = zend_hash_get_current_data_ex(aht, spl_array_get_pos_ptr(aht, intern))) == NULL) {
1743 RETURN_NULL();
1744 }
1745 if (Z_TYPE_P(entry) == IS_INDIRECT) {
1746 entry = Z_INDIRECT_P(entry);
1747 if (Z_TYPE_P(entry) == IS_UNDEF) {
1748 RETURN_NULL();
1749 }
1750 }
1751 RETURN_COPY_DEREF(entry);
1752 }
1753 /* }}} */
1754
spl_array_iterator_key(zval * object,zval * return_value)1755 void spl_array_iterator_key(zval *object, zval *return_value) /* {{{ */
1756 {
1757 spl_array_object *intern = Z_SPLARRAY_P(object);
1758 HashTable *aht = spl_array_get_hash_table(intern);
1759
1760 zend_hash_get_current_key_zval_ex(aht, return_value, spl_array_get_pos_ptr(aht, intern));
1761 }
1762 /* }}} */
1763
1764 /* {{{ Return current array key */
PHP_METHOD(ArrayIterator,key)1765 PHP_METHOD(ArrayIterator, key)
1766 {
1767 if (zend_parse_parameters_none() == FAILURE) {
1768 RETURN_THROWS();
1769 }
1770
1771 spl_array_iterator_key(ZEND_THIS, return_value);
1772 } /* }}} */
1773
1774 /* {{{ Move to next entry */
PHP_METHOD(ArrayIterator,next)1775 PHP_METHOD(ArrayIterator, next)
1776 {
1777 zval *object = ZEND_THIS;
1778 spl_array_object *intern = Z_SPLARRAY_P(object);
1779 HashTable *aht = spl_array_get_hash_table(intern);
1780
1781 if (zend_parse_parameters_none() == FAILURE) {
1782 RETURN_THROWS();
1783 }
1784
1785 spl_array_next_ex(intern, aht);
1786 }
1787 /* }}} */
1788
1789 /* {{{ Check whether array contains more entries */
PHP_METHOD(ArrayIterator,valid)1790 PHP_METHOD(ArrayIterator, valid)
1791 {
1792 zval *object = ZEND_THIS;
1793 spl_array_object *intern = Z_SPLARRAY_P(object);
1794 HashTable *aht = spl_array_get_hash_table(intern);
1795
1796 if (zend_parse_parameters_none() == FAILURE) {
1797 RETURN_THROWS();
1798 }
1799
1800 RETURN_BOOL(zend_hash_has_more_elements_ex(aht, spl_array_get_pos_ptr(aht, intern)) == SUCCESS);
1801 }
1802 /* }}} */
1803
1804 /*** RecursiveArrayIterator methods ***/
1805
1806 /* {{{ Check whether current element has children (e.g. is an array) */
PHP_METHOD(RecursiveArrayIterator,hasChildren)1807 PHP_METHOD(RecursiveArrayIterator, hasChildren)
1808 {
1809 zval *object = ZEND_THIS, *entry;
1810 spl_array_object *intern = Z_SPLARRAY_P(object);
1811 HashTable *aht = spl_array_get_hash_table(intern);
1812
1813 if (zend_parse_parameters_none() == FAILURE) {
1814 RETURN_THROWS();
1815 }
1816
1817 if ((entry = zend_hash_get_current_data_ex(aht, spl_array_get_pos_ptr(aht, intern))) == NULL) {
1818 RETURN_FALSE;
1819 }
1820
1821 if (Z_TYPE_P(entry) == IS_INDIRECT) {
1822 entry = Z_INDIRECT_P(entry);
1823 }
1824
1825 ZVAL_DEREF(entry);
1826 RETURN_BOOL(Z_TYPE_P(entry) == IS_ARRAY || (Z_TYPE_P(entry) == IS_OBJECT && (intern->ar_flags & SPL_ARRAY_CHILD_ARRAYS_ONLY) == 0));
1827 }
1828 /* }}} */
1829
spl_instantiate_child_arg(zend_class_entry * pce,zval * retval,zval * arg1,zval * arg2)1830 static void spl_instantiate_child_arg(zend_class_entry *pce, zval *retval, zval *arg1, zval *arg2) /* {{{ */
1831 {
1832 object_init_ex(retval, pce);
1833 spl_array_object *new_intern = Z_SPLARRAY_P(retval);
1834 /*
1835 * set new_intern->is_child is true to indicate that the object was created by
1836 * RecursiveArrayIterator::getChildren() method.
1837 */
1838 new_intern->is_child = true;
1839
1840 /* find the bucket of parent object. */
1841 new_intern->bucket = (Bucket *)((char *)(arg1) - XtOffsetOf(Bucket, val));;
1842 zend_call_known_instance_method_with_2_params(pce->constructor, Z_OBJ_P(retval), NULL, arg1, arg2);
1843 }
1844 /* }}} */
1845
1846 /* {{{ Create a sub iterator for the current element (same class as $this) */
PHP_METHOD(RecursiveArrayIterator,getChildren)1847 PHP_METHOD(RecursiveArrayIterator, getChildren)
1848 {
1849 zval *object = ZEND_THIS, *entry, flags;
1850 spl_array_object *intern = Z_SPLARRAY_P(object);
1851 HashTable *aht = spl_array_get_hash_table(intern);
1852
1853 if (zend_parse_parameters_none() == FAILURE) {
1854 RETURN_THROWS();
1855 }
1856
1857 if ((entry = zend_hash_get_current_data_ex(aht, spl_array_get_pos_ptr(aht, intern))) == NULL) {
1858 RETURN_NULL();
1859 }
1860
1861 if (Z_TYPE_P(entry) == IS_INDIRECT) {
1862 entry = Z_INDIRECT_P(entry);
1863 }
1864
1865 ZVAL_DEREF(entry);
1866 if (Z_TYPE_P(entry) == IS_OBJECT) {
1867 if ((intern->ar_flags & SPL_ARRAY_CHILD_ARRAYS_ONLY) != 0) {
1868 RETURN_NULL();
1869 }
1870 if (instanceof_function(Z_OBJCE_P(entry), Z_OBJCE_P(ZEND_THIS))) {
1871 RETURN_OBJ_COPY(Z_OBJ_P(entry));
1872 }
1873 }
1874
1875 ZVAL_LONG(&flags, intern->ar_flags);
1876 spl_instantiate_child_arg(Z_OBJCE_P(ZEND_THIS), return_value, entry, &flags);
1877 }
1878 /* }}} */
1879
1880 /* {{{ PHP_MINIT_FUNCTION(spl_array) */
PHP_MINIT_FUNCTION(spl_array)1881 PHP_MINIT_FUNCTION(spl_array)
1882 {
1883 spl_ce_ArrayObject = register_class_ArrayObject(zend_ce_aggregate, zend_ce_arrayaccess, zend_ce_serializable, zend_ce_countable);
1884 spl_ce_ArrayObject->create_object = spl_array_object_new;
1885 spl_ce_ArrayObject->default_object_handlers = &spl_handler_ArrayObject;
1886
1887 memcpy(&spl_handler_ArrayObject, &std_object_handlers, sizeof(zend_object_handlers));
1888
1889 spl_handler_ArrayObject.offset = XtOffsetOf(spl_array_object, std);
1890
1891 spl_handler_ArrayObject.clone_obj = spl_array_object_clone;
1892 spl_handler_ArrayObject.read_dimension = spl_array_read_dimension;
1893 spl_handler_ArrayObject.write_dimension = spl_array_write_dimension;
1894 spl_handler_ArrayObject.unset_dimension = spl_array_unset_dimension;
1895 spl_handler_ArrayObject.has_dimension = spl_array_has_dimension;
1896 spl_handler_ArrayObject.count_elements = spl_array_object_count_elements;
1897
1898 spl_handler_ArrayObject.get_properties_for = spl_array_get_properties_for;
1899 spl_handler_ArrayObject.get_gc = spl_array_get_gc;
1900 spl_handler_ArrayObject.read_property = spl_array_read_property;
1901 spl_handler_ArrayObject.write_property = spl_array_write_property;
1902 spl_handler_ArrayObject.get_property_ptr_ptr = spl_array_get_property_ptr_ptr;
1903 spl_handler_ArrayObject.has_property = spl_array_has_property;
1904 spl_handler_ArrayObject.unset_property = spl_array_unset_property;
1905
1906 spl_handler_ArrayObject.compare = spl_array_compare_objects;
1907 spl_handler_ArrayObject.free_obj = spl_array_object_free_storage;
1908
1909 spl_ce_ArrayIterator = register_class_ArrayIterator(spl_ce_SeekableIterator, zend_ce_arrayaccess, zend_ce_serializable, zend_ce_countable);
1910 spl_ce_ArrayIterator->create_object = spl_array_object_new;
1911 spl_ce_ArrayIterator->default_object_handlers = &spl_handler_ArrayObject;
1912 spl_ce_ArrayIterator->get_iterator = spl_array_get_iterator;
1913
1914 spl_ce_RecursiveArrayIterator = register_class_RecursiveArrayIterator(spl_ce_ArrayIterator, spl_ce_RecursiveIterator);
1915 spl_ce_RecursiveArrayIterator->create_object = spl_array_object_new;
1916 spl_ce_RecursiveArrayIterator->get_iterator = spl_array_get_iterator;
1917
1918 return SUCCESS;
1919 }
1920 /* }}} */
1921