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_ptr_dtor(data);
557 ZVAL_UNDEF(data);
558 HT_FLAGS(ht) |= HASH_FLAG_HAS_EMPTY_IND;
559 zend_hash_move_forward_ex(ht, spl_array_get_pos_ptr(ht, intern));
560 if (spl_array_is_object(intern)) {
561 spl_array_skip_protected(intern, ht);
562 }
563 }
564 } else {
565 zend_hash_del(ht, key.key);
566 }
567 }
568 spl_hash_key_release(&key);
569 } else {
570 zend_hash_index_del(ht, key.h);
571 }
572
573 if (refcount) {
574 spl_array_set_refcount(intern->is_child, ht, refcount);
575 }
576 } /* }}} */
577
spl_array_unset_dimension(zend_object * object,zval * offset)578 static void spl_array_unset_dimension(zend_object *object, zval *offset) /* {{{ */
579 {
580 spl_array_unset_dimension_ex(1, object, offset);
581 } /* }}} */
582
583 /* check_empty can take value 0, 1, or 2
584 * 0/1 are used as normal boolean, but 2 is used for the case when this function is called from
585 * 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)586 static bool spl_array_has_dimension_ex(bool check_inherited, zend_object *object, zval *offset, int check_empty) /* {{{ */
587 {
588 spl_array_object *intern = spl_array_from_obj(object);
589 zval rv, *value = NULL, *tmp;
590
591 if (check_inherited && intern->fptr_offset_has) {
592 zend_call_method_with_1_params(object, object->ce, &intern->fptr_offset_has, "offsetExists", &rv, offset);
593
594 if (!zend_is_true(&rv)) {
595 zval_ptr_dtor(&rv);
596 return 0;
597 }
598 zval_ptr_dtor(&rv);
599
600 /* For isset calls we don't need to check the value, so return early */
601 if (!check_empty) {
602 return 1;
603 } else if (intern->fptr_offset_get) {
604 value = spl_array_read_dimension_ex(1, object, offset, BP_VAR_R, &rv);
605 }
606 }
607
608 if (!value) {
609 HashTable *ht = spl_array_get_hash_table(intern);
610 spl_hash_key key;
611
612 if (get_hash_key(&key, intern, offset) == FAILURE) {
613 zend_illegal_container_offset(object->ce->name, offset, BP_VAR_IS);
614 return 0;
615 }
616
617 if (key.key) {
618 tmp = zend_hash_find(ht, key.key);
619 spl_hash_key_release(&key);
620 } else {
621 tmp = zend_hash_index_find(ht, key.h);
622 }
623
624 if (!tmp) {
625 return 0;
626 }
627
628 /* check_empty is only equal to 2 if it is called from offsetExists on this class,
629 * where it needs to report an offset exists even if the value is null */
630 if (check_empty == 2) {
631 return 1;
632 }
633
634 if (check_empty && check_inherited && intern->fptr_offset_get) {
635 value = spl_array_read_dimension_ex(1, object, offset, BP_VAR_R, &rv);
636 } else {
637 value = tmp;
638 }
639 }
640
641 if (value == &rv) {
642 zval_ptr_dtor(&rv);
643 }
644
645 /* empty() check the value is not falsy, isset() only check it is not null */
646 return check_empty ? zend_is_true(value) : Z_TYPE_P(value) != IS_NULL;
647 } /* }}} */
648
spl_array_has_dimension(zend_object * object,zval * offset,int check_empty)649 static int spl_array_has_dimension(zend_object *object, zval *offset, int check_empty) /* {{{ */
650 {
651 return spl_array_has_dimension_ex(/* check_inherited */ true, object, offset, check_empty);
652 } /* }}} */
653
654 /* {{{ Returns whether the requested $index exists. */
PHP_METHOD(ArrayObject,offsetExists)655 PHP_METHOD(ArrayObject, offsetExists)
656 {
657 zval *index;
658 if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &index) == FAILURE) {
659 RETURN_THROWS();
660 }
661 RETURN_BOOL(spl_array_has_dimension_ex(/* check_inherited */ false, Z_OBJ_P(ZEND_THIS), index, 2));
662 } /* }}} */
663
664 /* {{{ Returns the value at the specified $index. */
PHP_METHOD(ArrayObject,offsetGet)665 PHP_METHOD(ArrayObject, offsetGet)
666 {
667 zval *value, *index;
668 if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &index) == FAILURE) {
669 RETURN_THROWS();
670 }
671 value = spl_array_read_dimension_ex(0, Z_OBJ_P(ZEND_THIS), index, BP_VAR_R, return_value);
672 if (value != return_value) {
673 RETURN_COPY_DEREF(value);
674 }
675 } /* }}} */
676
677 /* {{{ Sets the value at the specified $index to $newval. */
PHP_METHOD(ArrayObject,offsetSet)678 PHP_METHOD(ArrayObject, offsetSet)
679 {
680 zval *index, *value;
681 if (zend_parse_parameters(ZEND_NUM_ARGS(), "zz", &index, &value) == FAILURE) {
682 RETURN_THROWS();
683 }
684 spl_array_write_dimension_ex(0, Z_OBJ_P(ZEND_THIS), index, value);
685 } /* }}} */
686
spl_array_iterator_append(zval * object,zval * append_value)687 void spl_array_iterator_append(zval *object, zval *append_value) /* {{{ */
688 {
689 spl_array_object *intern = Z_SPLARRAY_P(object);
690
691 if (spl_array_is_object(intern)) {
692 zend_throw_error(NULL, "Cannot append properties to objects, use %s::offsetSet() instead", ZSTR_VAL(Z_OBJCE_P(object)->name));
693 return;
694 }
695
696 spl_array_write_dimension(Z_OBJ_P(object), NULL, append_value);
697 } /* }}} */
698
699 /* {{{ Appends the value (cannot be called for objects). */
PHP_METHOD(ArrayObject,append)700 PHP_METHOD(ArrayObject, append)
701 {
702 zval *value;
703
704 if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &value) == FAILURE) {
705 RETURN_THROWS();
706 }
707 spl_array_iterator_append(ZEND_THIS, value);
708 } /* }}} */
709
710 /* {{{ Unsets the value at the specified $index. */
PHP_METHOD(ArrayObject,offsetUnset)711 PHP_METHOD(ArrayObject, offsetUnset)
712 {
713 zval *index;
714 if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &index) == FAILURE) {
715 RETURN_THROWS();
716 }
717 spl_array_unset_dimension_ex(0, Z_OBJ_P(ZEND_THIS), index);
718 } /* }}} */
719
720 /* {{{ Return a copy of the contained array */
PHP_METHOD(ArrayObject,getArrayCopy)721 PHP_METHOD(ArrayObject, getArrayCopy)
722 {
723 zval *object = ZEND_THIS;
724 spl_array_object *intern = Z_SPLARRAY_P(object);
725
726 if (zend_parse_parameters_none() == FAILURE) {
727 RETURN_THROWS();
728 }
729
730 RETURN_ARR(zend_array_dup(spl_array_get_hash_table(intern)));
731 } /* }}} */
732
spl_array_get_properties_for(zend_object * object,zend_prop_purpose purpose)733 static HashTable *spl_array_get_properties_for(zend_object *object, zend_prop_purpose purpose) /* {{{ */
734 {
735 spl_array_object *intern = spl_array_from_obj(object);
736 HashTable *ht;
737 bool dup;
738
739 if (intern->ar_flags & SPL_ARRAY_STD_PROP_LIST) {
740 return zend_std_get_properties_for(object, purpose);
741 }
742
743 /* We are supposed to be the only owner of the internal hashtable.
744 * The "dup" flag decides whether this is a "long-term" use where
745 * we need to duplicate, or a "temporary" one, where we can expect
746 * that no operations on the ArrayObject will be performed in the
747 * meantime. */
748 switch (purpose) {
749 case ZEND_PROP_PURPOSE_ARRAY_CAST:
750 dup = 1;
751 break;
752 case ZEND_PROP_PURPOSE_VAR_EXPORT:
753 case ZEND_PROP_PURPOSE_JSON:
754 dup = 0;
755 break;
756 default:
757 return zend_std_get_properties_for(object, purpose);
758 }
759
760 ht = spl_array_get_hash_table(intern);
761 if (dup) {
762 ht = zend_array_dup(ht);
763 } else {
764 GC_ADDREF(ht);
765 }
766 return ht;
767 } /* }}} */
768
spl_array_get_debug_info(zend_object * obj)769 static inline HashTable* spl_array_get_debug_info(zend_object *obj) /* {{{ */
770 {
771 spl_array_object *intern = spl_array_from_obj(obj);
772 HashTable *properties = zend_std_get_properties_ex(&intern->std);
773
774 if (intern->ar_flags & SPL_ARRAY_IS_SELF) {
775 return zend_array_dup(properties);
776 } else {
777 HashTable *debug_info;
778
779 debug_info = zend_new_array(zend_hash_num_elements(properties) + 1);
780 zend_hash_copy(debug_info, properties, (copy_ctor_func_t) zval_add_ref);
781
782 zval *storage = &intern->array;
783 Z_TRY_ADDREF_P(storage);
784
785 const zend_class_entry *base_class_ce = instanceof_function(obj->ce, spl_ce_ArrayIterator)
786 ? spl_ce_ArrayIterator : spl_ce_ArrayObject;
787
788 spl_set_private_debug_info_property(base_class_ce, "storage", strlen("storage"), debug_info, storage);
789
790 return debug_info;
791 }
792 }
793 /* }}} */
794
spl_array_get_gc(zend_object * obj,zval ** gc_data,int * gc_data_count)795 static HashTable *spl_array_get_gc(zend_object *obj, zval **gc_data, int *gc_data_count) /* {{{ */
796 {
797 spl_array_object *intern = spl_array_from_obj(obj);
798 *gc_data = &intern->array;
799 *gc_data_count = 1;
800 return zend_std_get_properties(obj);
801 }
802 /* }}} */
803
spl_array_read_property(zend_object * object,zend_string * name,int type,void ** cache_slot,zval * rv)804 static zval *spl_array_read_property(zend_object *object, zend_string *name, int type, void **cache_slot, zval *rv) /* {{{ */
805 {
806 spl_array_object *intern = spl_array_from_obj(object);
807
808 if ((intern->ar_flags & SPL_ARRAY_ARRAY_AS_PROPS) != 0
809 && !zend_std_has_property(object, name, ZEND_PROPERTY_EXISTS, NULL)) {
810 zval member;
811 ZVAL_STR(&member, name);
812 return spl_array_read_dimension(object, &member, type, rv);
813 }
814 return zend_std_read_property(object, name, type, cache_slot, rv);
815 } /* }}} */
816
spl_array_write_property(zend_object * object,zend_string * name,zval * value,void ** cache_slot)817 static zval *spl_array_write_property(zend_object *object, zend_string *name, zval *value, void **cache_slot) /* {{{ */
818 {
819 spl_array_object *intern = spl_array_from_obj(object);
820
821 if ((intern->ar_flags & SPL_ARRAY_ARRAY_AS_PROPS) != 0
822 && !zend_std_has_property(object, name, ZEND_PROPERTY_EXISTS, NULL)) {
823 zval member;
824 ZVAL_STR(&member, name);
825 spl_array_write_dimension(object, &member, value);
826 return value;
827 }
828 return zend_std_write_property(object, name, value, cache_slot);
829 } /* }}} */
830
spl_array_get_property_ptr_ptr(zend_object * object,zend_string * name,int type,void ** cache_slot)831 static zval *spl_array_get_property_ptr_ptr(zend_object *object, zend_string *name, int type, void **cache_slot) /* {{{ */
832 {
833 spl_array_object *intern = spl_array_from_obj(object);
834
835 if ((intern->ar_flags & SPL_ARRAY_ARRAY_AS_PROPS) != 0
836 && !zend_std_has_property(object, name, ZEND_PROPERTY_EXISTS, NULL)) {
837 /* If object has offsetGet() overridden, then fallback to read_property,
838 * which will call offsetGet(). */
839 zval member;
840 if (intern->fptr_offset_get) {
841 return NULL;
842 }
843 ZVAL_STR(&member, name);
844 return spl_array_get_dimension_ptr(1, intern, object->ce->name, &member, type);
845 }
846 return zend_std_get_property_ptr_ptr(object, name, type, cache_slot);
847 } /* }}} */
848
spl_array_has_property(zend_object * object,zend_string * name,int has_set_exists,void ** cache_slot)849 static int spl_array_has_property(zend_object *object, zend_string *name, int has_set_exists, void **cache_slot) /* {{{ */
850 {
851 spl_array_object *intern = spl_array_from_obj(object);
852
853 if ((intern->ar_flags & SPL_ARRAY_ARRAY_AS_PROPS) != 0
854 && !zend_std_has_property(object, name, ZEND_PROPERTY_EXISTS, NULL)) {
855 zval member;
856 ZVAL_STR(&member, name);
857 return spl_array_has_dimension(object, &member, has_set_exists);
858 }
859 return zend_std_has_property(object, name, has_set_exists, cache_slot);
860 } /* }}} */
861
spl_array_unset_property(zend_object * object,zend_string * name,void ** cache_slot)862 static void spl_array_unset_property(zend_object *object, zend_string *name, void **cache_slot) /* {{{ */
863 {
864 spl_array_object *intern = spl_array_from_obj(object);
865
866 if ((intern->ar_flags & SPL_ARRAY_ARRAY_AS_PROPS) != 0
867 && !zend_std_has_property(object, name, ZEND_PROPERTY_EXISTS, NULL)) {
868 zval member;
869 ZVAL_STR(&member, name);
870 spl_array_unset_dimension(object, &member);
871 return;
872 }
873 zend_std_unset_property(object, name, cache_slot);
874 } /* }}} */
875
spl_array_compare_objects(zval * o1,zval * o2)876 static int spl_array_compare_objects(zval *o1, zval *o2) /* {{{ */
877 {
878 HashTable *ht1,
879 *ht2;
880 spl_array_object *intern1,
881 *intern2;
882 int result = 0;
883
884 ZEND_COMPARE_OBJECTS_FALLBACK(o1, o2);
885
886 intern1 = Z_SPLARRAY_P(o1);
887 intern2 = Z_SPLARRAY_P(o2);
888 ht1 = spl_array_get_hash_table(intern1);
889 ht2 = spl_array_get_hash_table(intern2);
890
891 result = zend_compare_symbol_tables(ht1, ht2);
892 /* if we just compared std.properties, don't do it again */
893 if (result == 0 &&
894 !(ht1 == intern1->std.properties && ht2 == intern2->std.properties)) {
895 result = zend_std_compare_objects(o1, o2);
896 }
897 return result;
898 } /* }}} */
899
spl_array_skip_protected(spl_array_object * intern,HashTable * aht)900 static zend_result spl_array_skip_protected(spl_array_object *intern, HashTable *aht) /* {{{ */
901 {
902 zend_string *string_key;
903 zend_ulong num_key;
904 zval *data;
905
906 if (spl_array_is_object(intern)) {
907 uint32_t *pos_ptr = spl_array_get_pos_ptr(aht, intern);
908
909 do {
910 if (zend_hash_get_current_key_ex(aht, &string_key, &num_key, pos_ptr) == HASH_KEY_IS_STRING) {
911 data = zend_hash_get_current_data_ex(aht, pos_ptr);
912 if (data && Z_TYPE_P(data) == IS_INDIRECT &&
913 Z_TYPE_P(data = Z_INDIRECT_P(data)) == IS_UNDEF) {
914 /* skip */
915 } else if (!ZSTR_LEN(string_key) || ZSTR_VAL(string_key)[0]) {
916 return SUCCESS;
917 }
918 } else {
919 return SUCCESS;
920 }
921 if (zend_hash_has_more_elements_ex(aht, pos_ptr) != SUCCESS) {
922 return FAILURE;
923 }
924 zend_hash_move_forward_ex(aht, pos_ptr);
925 } while (1);
926 }
927 return FAILURE;
928 } /* }}} */
929
930 /* {{{ spl_array_set_array */
spl_array_set_array(zval * object,spl_array_object * intern,zval * array,zend_long ar_flags,bool just_array)931 static void spl_array_set_array(zval *object, spl_array_object *intern, zval *array, zend_long ar_flags, bool just_array) {
932 /* Handled by ZPP prior to this, or for __unserialize() before passing to here */
933 ZEND_ASSERT(Z_TYPE_P(array) == IS_ARRAY || Z_TYPE_P(array) == IS_OBJECT);
934 if (Z_TYPE_P(array) == IS_ARRAY) {
935 zval_ptr_dtor(&intern->array);
936 if (Z_REFCOUNT_P(array) == 1) {
937 ZVAL_COPY(&intern->array, array);
938 } else {
939 //??? TODO: try to avoid array duplication
940 ZVAL_ARR(&intern->array, zend_array_dup(Z_ARR_P(array)));
941
942 if (intern->is_child) {
943 Z_TRY_DELREF(intern->bucket->val);
944 /*
945 * replace bucket->val with copied array, so the changes between
946 * parent and child object can affect each other.
947 */
948 ZVAL_COPY(&intern->bucket->val, &intern->array);
949 }
950 }
951 } else {
952 if (Z_OBJ_HT_P(array) == &spl_handler_ArrayObject) {
953 zval_ptr_dtor(&intern->array);
954 if (just_array) {
955 spl_array_object *other = Z_SPLARRAY_P(array);
956 ar_flags = other->ar_flags & ~SPL_ARRAY_INT_MASK;
957 }
958 if (Z_OBJ_P(object) == Z_OBJ_P(array)) {
959 ar_flags |= SPL_ARRAY_IS_SELF;
960 ZVAL_UNDEF(&intern->array);
961 } else {
962 ar_flags |= SPL_ARRAY_USE_OTHER;
963 ZVAL_COPY(&intern->array, array);
964 }
965 } else {
966 zend_object_get_properties_t handler = Z_OBJ_HANDLER_P(array, get_properties);
967 if (handler != zend_std_get_properties || Z_OBJ_HANDLER_P(array, get_properties_for)) {
968 zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0,
969 "Overloaded object of type %s is not compatible with %s",
970 ZSTR_VAL(Z_OBJCE_P(array)->name), ZSTR_VAL(intern->std.ce->name));
971 return;
972 }
973 zval_ptr_dtor(&intern->array);
974 ZVAL_COPY(&intern->array, array);
975 }
976 }
977
978 intern->ar_flags &= ~SPL_ARRAY_IS_SELF & ~SPL_ARRAY_USE_OTHER;
979 intern->ar_flags |= ar_flags;
980 if (intern->ht_iter != (uint32_t)-1) {
981 zend_hash_iterator_del(intern->ht_iter);
982 intern->ht_iter = (uint32_t)-1;
983 }
984 }
985 /* }}} */
986
987 /* {{{ Constructs a new array object from an array or object. */
PHP_METHOD(ArrayObject,__construct)988 PHP_METHOD(ArrayObject, __construct)
989 {
990 zval *object = ZEND_THIS;
991 spl_array_object *intern;
992 zval *array;
993 zend_long ar_flags = 0;
994 zend_class_entry *ce_get_iterator = spl_ce_ArrayIterator;
995
996 if (ZEND_NUM_ARGS() == 0) {
997 return; /* nothing to do */
998 }
999
1000 if (zend_parse_parameters(ZEND_NUM_ARGS(), "|AlC", &array, &ar_flags, &ce_get_iterator) == FAILURE) {
1001 RETURN_THROWS();
1002 }
1003
1004 intern = Z_SPLARRAY_P(object);
1005
1006 if (ZEND_NUM_ARGS() > 2) {
1007 intern->ce_get_iterator = ce_get_iterator;
1008 }
1009
1010 ar_flags &= ~SPL_ARRAY_INT_MASK;
1011
1012 spl_array_set_array(object, intern, array, ar_flags, ZEND_NUM_ARGS() == 1);
1013 }
1014 /* }}} */
1015
1016 /* {{{ Set the class used in getIterator. */
PHP_METHOD(ArrayObject,setIteratorClass)1017 PHP_METHOD(ArrayObject, setIteratorClass)
1018 {
1019 zval *object = ZEND_THIS;
1020 spl_array_object *intern = Z_SPLARRAY_P(object);
1021 zend_class_entry *ce_get_iterator = spl_ce_ArrayIterator;
1022
1023 ZEND_PARSE_PARAMETERS_START(1, 1)
1024 Z_PARAM_CLASS(ce_get_iterator)
1025 ZEND_PARSE_PARAMETERS_END();
1026
1027 intern->ce_get_iterator = ce_get_iterator;
1028 }
1029 /* }}} */
1030
1031 /* {{{ Get the class used in getIterator. */
PHP_METHOD(ArrayObject,getIteratorClass)1032 PHP_METHOD(ArrayObject, getIteratorClass)
1033 {
1034 zval *object = ZEND_THIS;
1035 spl_array_object *intern = Z_SPLARRAY_P(object);
1036
1037 if (zend_parse_parameters_none() == FAILURE) {
1038 RETURN_THROWS();
1039 }
1040
1041 zend_string_addref(intern->ce_get_iterator->name);
1042 RETURN_STR(intern->ce_get_iterator->name);
1043 }
1044 /* }}} */
1045
1046 /* {{{ Get flags */
PHP_METHOD(ArrayObject,getFlags)1047 PHP_METHOD(ArrayObject, getFlags)
1048 {
1049 zval *object = ZEND_THIS;
1050 spl_array_object *intern = Z_SPLARRAY_P(object);
1051
1052 if (zend_parse_parameters_none() == FAILURE) {
1053 RETURN_THROWS();
1054 }
1055
1056 RETURN_LONG(intern->ar_flags & ~SPL_ARRAY_INT_MASK);
1057 }
1058 /* }}} */
1059
1060 /* {{{ Set flags */
PHP_METHOD(ArrayObject,setFlags)1061 PHP_METHOD(ArrayObject, setFlags)
1062 {
1063 zval *object = ZEND_THIS;
1064 spl_array_object *intern = Z_SPLARRAY_P(object);
1065 zend_long ar_flags = 0;
1066
1067 if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &ar_flags) == FAILURE) {
1068 RETURN_THROWS();
1069 }
1070
1071 intern->ar_flags = (intern->ar_flags & SPL_ARRAY_INT_MASK) | (ar_flags & ~SPL_ARRAY_INT_MASK);
1072 }
1073 /* }}} */
1074
1075 /* {{{ 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)1076 PHP_METHOD(ArrayObject, exchangeArray)
1077 {
1078 zval *object = ZEND_THIS, *array;
1079 spl_array_object *intern = Z_SPLARRAY_P(object);
1080
1081 if (zend_parse_parameters(ZEND_NUM_ARGS(), "A", &array) == FAILURE) {
1082 RETURN_THROWS();
1083 }
1084
1085 if (intern->nApplyCount > 0) {
1086 zend_throw_error(NULL, "Modification of ArrayObject during sorting is prohibited");
1087 RETURN_THROWS();
1088 }
1089
1090 RETVAL_ARR(zend_array_dup(spl_array_get_hash_table(intern)));
1091 spl_array_set_array(object, intern, array, 0L, 1);
1092 }
1093 /* }}} */
1094
1095 /* {{{ Create a new iterator from a ArrayObject instance */
PHP_METHOD(ArrayObject,getIterator)1096 PHP_METHOD(ArrayObject, getIterator)
1097 {
1098 zval *object = ZEND_THIS;
1099 spl_array_object *intern = Z_SPLARRAY_P(object);
1100
1101 if (zend_parse_parameters_none() == FAILURE) {
1102 RETURN_THROWS();
1103 }
1104
1105 RETURN_OBJ(spl_array_object_new_ex(intern->ce_get_iterator, Z_OBJ_P(object), 0));
1106 }
1107 /* }}} */
1108
spl_array_object_count_elements_helper(spl_array_object * intern)1109 static zend_long spl_array_object_count_elements_helper(spl_array_object *intern) /* {{{ */
1110 {
1111 HashTable *aht = spl_array_get_hash_table(intern);
1112 if (spl_array_is_object(intern)) {
1113 zend_long count = 0;
1114 zend_string *key;
1115 zval *val;
1116 /* Count public/dynamic properties */
1117 ZEND_HASH_FOREACH_STR_KEY_VAL(aht, key, val) {
1118 if (Z_TYPE_P(val) == IS_INDIRECT) {
1119 if (Z_TYPE_P(Z_INDIRECT_P(val)) == IS_UNDEF) continue;
1120 if (key && ZSTR_VAL(key)[0] == '\0') continue;
1121 }
1122 count++;
1123 } ZEND_HASH_FOREACH_END();
1124 return count;
1125 } else {
1126 return zend_hash_num_elements(aht);
1127 }
1128 } /* }}} */
1129
spl_array_object_count_elements(zend_object * object,zend_long * count)1130 static zend_result spl_array_object_count_elements(zend_object *object, zend_long *count) /* {{{ */
1131 {
1132 spl_array_object *intern = spl_array_from_obj(object);
1133
1134 if (intern->fptr_count) {
1135 zval rv;
1136 zend_call_method_with_0_params(object, intern->std.ce, &intern->fptr_count, "count", &rv);
1137 if (Z_TYPE(rv) != IS_UNDEF) {
1138 *count = zval_get_long(&rv);
1139 zval_ptr_dtor(&rv);
1140 return SUCCESS;
1141 }
1142 *count = 0;
1143 return FAILURE;
1144 }
1145 *count = spl_array_object_count_elements_helper(intern);
1146 return SUCCESS;
1147 } /* }}} */
1148
1149 /* {{{ Return the number of elements in the Iterator. */
PHP_METHOD(ArrayObject,count)1150 PHP_METHOD(ArrayObject, count)
1151 {
1152 spl_array_object *intern = Z_SPLARRAY_P(ZEND_THIS);
1153
1154 if (zend_parse_parameters_none() == FAILURE) {
1155 RETURN_THROWS();
1156 }
1157
1158 RETURN_LONG(spl_array_object_count_elements_helper(intern));
1159 } /* }}} */
1160
spl_array_method(INTERNAL_FUNCTION_PARAMETERS,char * fname,size_t fname_len,int use_arg)1161 static void spl_array_method(INTERNAL_FUNCTION_PARAMETERS, char *fname, size_t fname_len, int use_arg) /* {{{ */
1162 {
1163 spl_array_object *intern = Z_SPLARRAY_P(ZEND_THIS);
1164 HashTable **ht_ptr = spl_array_get_hash_table_ptr(intern);
1165 HashTable *aht = *ht_ptr;
1166 zval function_name, params[2], *arg = NULL;
1167
1168 ZVAL_STRINGL(&function_name, fname, fname_len);
1169
1170 ZVAL_NEW_EMPTY_REF(¶ms[0]);
1171 ZVAL_ARR(Z_REFVAL(params[0]), aht);
1172 GC_ADDREF(aht);
1173
1174 if (!use_arg) {
1175 if (zend_parse_parameters_none() == FAILURE) {
1176 goto exit;
1177 }
1178
1179 intern->nApplyCount++;
1180 call_user_function(EG(function_table), NULL, &function_name, return_value, 1, params);
1181 intern->nApplyCount--;
1182 } else if (use_arg == SPL_ARRAY_METHOD_SORT_FLAGS_ARG) {
1183 zend_long sort_flags = 0;
1184 if (zend_parse_parameters(ZEND_NUM_ARGS(), "|l", &sort_flags) == FAILURE) {
1185 goto exit;
1186 }
1187 ZVAL_LONG(¶ms[1], sort_flags);
1188 intern->nApplyCount++;
1189 call_user_function(EG(function_table), NULL, &function_name, return_value, 2, params);
1190 intern->nApplyCount--;
1191 } else {
1192 if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &arg) == FAILURE) {
1193 goto exit;
1194 }
1195 ZVAL_COPY_VALUE(¶ms[1], arg);
1196 intern->nApplyCount++;
1197 call_user_function(EG(function_table), NULL, &function_name, return_value, 2, params);
1198 intern->nApplyCount--;
1199 }
1200
1201 exit:
1202 {
1203 zval *ht_zv = Z_REFVAL(params[0]);
1204 zend_array_release(*ht_ptr);
1205 SEPARATE_ARRAY(ht_zv);
1206 *ht_ptr = Z_ARRVAL_P(ht_zv);
1207 ZVAL_NULL(ht_zv);
1208 zval_ptr_dtor(¶ms[0]);
1209 zend_string_free(Z_STR(function_name));
1210 }
1211 } /* }}} */
1212
1213 #define SPL_ARRAY_METHOD(cname, fname, use_arg) \
1214 PHP_METHOD(cname, fname) \
1215 { \
1216 spl_array_method(INTERNAL_FUNCTION_PARAM_PASSTHRU, #fname, sizeof(#fname)-1, use_arg); \
1217 }
1218
1219 /* {{{ Sort the entries by values. */
SPL_ARRAY_METHOD(ArrayObject,asort,SPL_ARRAY_METHOD_SORT_FLAGS_ARG)1220 SPL_ARRAY_METHOD(ArrayObject, asort, SPL_ARRAY_METHOD_SORT_FLAGS_ARG) /* }}} */
1221
1222 /* {{{ Sort the entries by key. */
1223 SPL_ARRAY_METHOD(ArrayObject, ksort, SPL_ARRAY_METHOD_SORT_FLAGS_ARG) /* }}} */
1224
1225 /* {{{ Sort the entries by values user defined function. */
1226 SPL_ARRAY_METHOD(ArrayObject, uasort, SPL_ARRAY_METHOD_CALLBACK_ARG) /* }}} */
1227
1228 /* {{{ Sort the entries by key using user defined function. */
1229 SPL_ARRAY_METHOD(ArrayObject, uksort, SPL_ARRAY_METHOD_CALLBACK_ARG) /* }}} */
1230
1231 /* {{{ Sort the entries by values using "natural order" algorithm. */
1232 SPL_ARRAY_METHOD(ArrayObject, natsort, SPL_ARRAY_METHOD_NO_ARG) /* }}} */
1233
1234 /* {{{ Sort the entries by key using case insensitive "natural order" algorithm. */
1235 SPL_ARRAY_METHOD(ArrayObject, natcasesort, SPL_ARRAY_METHOD_NO_ARG) /* }}} */
1236
1237 /* {{{ Serialize the object */
1238 PHP_METHOD(ArrayObject, serialize)
1239 {
1240 zval *object = ZEND_THIS;
1241 spl_array_object *intern = Z_SPLARRAY_P(object);
1242 zval members, flags;
1243 php_serialize_data_t var_hash;
1244 smart_str buf = {0};
1245
1246 if (zend_parse_parameters_none() == FAILURE) {
1247 RETURN_THROWS();
1248 }
1249
1250 PHP_VAR_SERIALIZE_INIT(var_hash);
1251
1252 ZVAL_LONG(&flags, (intern->ar_flags & SPL_ARRAY_CLONE_MASK));
1253
1254 /* storage */
1255 smart_str_appendl(&buf, "x:", 2);
1256 php_var_serialize(&buf, &flags, &var_hash);
1257
1258 if (!(intern->ar_flags & SPL_ARRAY_IS_SELF)) {
1259 php_var_serialize(&buf, &intern->array, &var_hash);
1260 smart_str_appendc(&buf, ';');
1261 }
1262
1263 /* members */
1264 smart_str_appendl(&buf, "m:", 2);
1265
1266 ZVAL_ARR(&members, zend_std_get_properties_ex(&intern->std));
1267
1268 php_var_serialize(&buf, &members, &var_hash); /* finishes the string */
1269
1270 /* done */
1271 PHP_VAR_SERIALIZE_DESTROY(var_hash);
1272
1273 RETURN_STR(smart_str_extract(&buf));
1274 } /* }}} */
1275
1276 /* {{{ unserialize the object */
PHP_METHOD(ArrayObject,unserialize)1277 PHP_METHOD(ArrayObject, unserialize)
1278 {
1279 zval *object = ZEND_THIS;
1280 spl_array_object *intern = Z_SPLARRAY_P(object);
1281
1282 char *buf;
1283 size_t buf_len;
1284 const unsigned char *p, *s;
1285 php_unserialize_data_t var_hash;
1286 zval *members, *zflags, *array;
1287 zend_long flags;
1288
1289 if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &buf, &buf_len) == FAILURE) {
1290 RETURN_THROWS();
1291 }
1292
1293 if (buf_len == 0) {
1294 return;
1295 }
1296
1297 if (intern->nApplyCount > 0) {
1298 zend_throw_error(NULL, "Modification of ArrayObject during sorting is prohibited");
1299 RETURN_THROWS();
1300 }
1301
1302 /* storage */
1303 s = p = (const unsigned char*)buf;
1304 PHP_VAR_UNSERIALIZE_INIT(var_hash);
1305
1306 if (*p!= 'x' || *++p != ':') {
1307 goto outexcept;
1308 }
1309 ++p;
1310
1311 zflags = var_tmp_var(&var_hash);
1312 if (!php_var_unserialize(zflags, &p, s + buf_len, &var_hash) || Z_TYPE_P(zflags) != IS_LONG) {
1313 goto outexcept;
1314 }
1315
1316 --p; /* for ';' */
1317 flags = Z_LVAL_P(zflags);
1318 /* flags needs to be verified and we also need to verify whether the next
1319 * thing we get is ';'. After that we require an 'm' or something else
1320 * where 'm' stands for members and anything else should be an array. If
1321 * neither 'a' or 'm' follows we have an error. */
1322
1323 if (*p != ';') {
1324 goto outexcept;
1325 }
1326 ++p;
1327
1328 if (flags & SPL_ARRAY_IS_SELF) {
1329 /* If IS_SELF is used, the flags are not followed by an array/object */
1330 intern->ar_flags &= ~SPL_ARRAY_CLONE_MASK;
1331 intern->ar_flags |= flags & SPL_ARRAY_CLONE_MASK;
1332 zval_ptr_dtor(&intern->array);
1333 ZVAL_UNDEF(&intern->array);
1334 } else {
1335 if (*p!='a' && *p!='O' && *p!='C' && *p!='r') {
1336 goto outexcept;
1337 }
1338
1339 array = var_tmp_var(&var_hash);
1340 if (!php_var_unserialize(array, &p, s + buf_len, &var_hash)
1341 || (Z_TYPE_P(array) != IS_ARRAY && Z_TYPE_P(array) != IS_OBJECT)) {
1342 goto outexcept;
1343 }
1344
1345 intern->ar_flags &= ~SPL_ARRAY_CLONE_MASK;
1346 intern->ar_flags |= flags & SPL_ARRAY_CLONE_MASK;
1347
1348 if (Z_TYPE_P(array) == IS_ARRAY) {
1349 zval_ptr_dtor(&intern->array);
1350 ZVAL_COPY_VALUE(&intern->array, array);
1351 ZVAL_NULL(array);
1352 SEPARATE_ARRAY(&intern->array);
1353 } else {
1354 spl_array_set_array(object, intern, array, 0L, 1);
1355 }
1356
1357 if (*p != ';') {
1358 goto outexcept;
1359 }
1360 ++p;
1361 }
1362
1363 /* members */
1364 if (*p!= 'm' || *++p != ':') {
1365 goto outexcept;
1366 }
1367 ++p;
1368
1369 members = var_tmp_var(&var_hash);
1370 if (!php_var_unserialize(members, &p, s + buf_len, &var_hash) || Z_TYPE_P(members) != IS_ARRAY) {
1371 goto outexcept;
1372 }
1373
1374 /* copy members */
1375 object_properties_load(&intern->std, Z_ARRVAL_P(members));
1376
1377 /* done reading $serialized */
1378 PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
1379 return;
1380
1381 outexcept:
1382 PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
1383 zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0, "Error at offset " ZEND_LONG_FMT " of %zd bytes", (zend_long)((char*)p - buf), buf_len);
1384 RETURN_THROWS();
1385
1386 } /* }}} */
1387
1388 /* {{{ */
PHP_METHOD(ArrayObject,__serialize)1389 PHP_METHOD(ArrayObject, __serialize)
1390 {
1391 spl_array_object *intern = Z_SPLARRAY_P(ZEND_THIS);
1392 zval tmp;
1393
1394 if (zend_parse_parameters_none() == FAILURE) {
1395 RETURN_THROWS();
1396 }
1397
1398 array_init(return_value);
1399
1400 /* flags */
1401 ZVAL_LONG(&tmp, (intern->ar_flags & SPL_ARRAY_CLONE_MASK));
1402 zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &tmp);
1403
1404 /* storage */
1405 if (intern->ar_flags & SPL_ARRAY_IS_SELF) {
1406 ZVAL_NULL(&tmp);
1407 } else {
1408 ZVAL_COPY(&tmp, &intern->array);
1409 }
1410 zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &tmp);
1411
1412 /* members */
1413 ZVAL_ARR(&tmp, zend_proptable_to_symtable(
1414 zend_std_get_properties(&intern->std), /* always_duplicate */ 1));
1415 zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &tmp);
1416
1417 /* iterator class */
1418 if (intern->ce_get_iterator == spl_ce_ArrayIterator) {
1419 ZVAL_NULL(&tmp);
1420 } else {
1421 ZVAL_STR_COPY(&tmp, intern->ce_get_iterator->name);
1422 }
1423 zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &tmp);
1424 }
1425 /* }}} */
1426
1427
1428 /* {{{ */
PHP_METHOD(ArrayObject,__unserialize)1429 PHP_METHOD(ArrayObject, __unserialize)
1430 {
1431 spl_array_object *intern = Z_SPLARRAY_P(ZEND_THIS);
1432 HashTable *data;
1433 zval *flags_zv, *storage_zv, *members_zv, *iterator_class_zv;
1434 zend_long flags;
1435
1436 if (zend_parse_parameters(ZEND_NUM_ARGS(), "h", &data) == FAILURE) {
1437 RETURN_THROWS();
1438 }
1439
1440 flags_zv = zend_hash_index_find(data, 0);
1441 storage_zv = zend_hash_index_find(data, 1);
1442 members_zv = zend_hash_index_find(data, 2);
1443 iterator_class_zv = zend_hash_index_find(data, 3);
1444
1445 if (!flags_zv || !storage_zv || !members_zv ||
1446 Z_TYPE_P(flags_zv) != IS_LONG || Z_TYPE_P(members_zv) != IS_ARRAY ||
1447 (iterator_class_zv && (Z_TYPE_P(iterator_class_zv) != IS_NULL &&
1448 Z_TYPE_P(iterator_class_zv) != IS_STRING))) {
1449 zend_throw_exception(spl_ce_UnexpectedValueException,
1450 "Incomplete or ill-typed serialization data", 0);
1451 RETURN_THROWS();
1452 }
1453
1454 flags = Z_LVAL_P(flags_zv);
1455 intern->ar_flags &= ~SPL_ARRAY_CLONE_MASK;
1456 intern->ar_flags |= flags & SPL_ARRAY_CLONE_MASK;
1457
1458 if (flags & SPL_ARRAY_IS_SELF) {
1459 zval_ptr_dtor(&intern->array);
1460 ZVAL_UNDEF(&intern->array);
1461 } else {
1462 if (Z_TYPE_P(storage_zv) != IS_OBJECT && Z_TYPE_P(storage_zv) != IS_ARRAY) {
1463 /* TODO Use UnexpectedValueException instead? And better error message? */
1464 zend_throw_exception(spl_ce_InvalidArgumentException, "Passed variable is not an array or object", 0);
1465 RETURN_THROWS();
1466 }
1467 spl_array_set_array(ZEND_THIS, intern, storage_zv, 0L, 1);
1468 }
1469
1470 object_properties_load(&intern->std, Z_ARRVAL_P(members_zv));
1471 if (EG(exception)) {
1472 RETURN_THROWS();
1473 }
1474
1475 if (iterator_class_zv && Z_TYPE_P(iterator_class_zv) == IS_STRING) {
1476 zend_class_entry *ce = zend_lookup_class(Z_STR_P(iterator_class_zv));
1477
1478 if (!ce) {
1479 zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
1480 "Cannot deserialize ArrayObject with iterator class '%s'; no such class exists",
1481 ZSTR_VAL(Z_STR_P(iterator_class_zv)));
1482 RETURN_THROWS();
1483 }
1484
1485 if (!instanceof_function(ce, zend_ce_iterator)) {
1486 zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
1487 "Cannot deserialize ArrayObject with iterator class '%s'; this class does not implement the Iterator interface",
1488 ZSTR_VAL(Z_STR_P(iterator_class_zv)));
1489 RETURN_THROWS();
1490 }
1491
1492 intern->ce_get_iterator = ce;
1493 }
1494 }
1495 /* }}} */
1496
1497 /* {{{ */
PHP_METHOD(ArrayObject,__debugInfo)1498 PHP_METHOD(ArrayObject, __debugInfo)
1499 {
1500 if (zend_parse_parameters_none() == FAILURE) {
1501 RETURN_THROWS();
1502 }
1503
1504 RETURN_ARR(spl_array_get_debug_info(Z_OBJ_P(ZEND_THIS)));
1505 } /* }}} */
1506
1507 /*** ArrayIterator class ***/
1508 typedef struct _spl_array_iterator {
1509 zend_object_iterator it;
1510 bool by_ref;
1511 } spl_array_iterator;
1512
spl_array_next_ex(spl_array_object * intern,HashTable * aht)1513 static zend_result spl_array_next_ex(spl_array_object *intern, HashTable *aht) /* {{{ */
1514 {
1515 uint32_t *pos_ptr = spl_array_get_pos_ptr(aht, intern);
1516
1517 zend_hash_move_forward_ex(aht, pos_ptr);
1518 if (spl_array_is_object(intern)) {
1519 return spl_array_skip_protected(intern, aht);
1520 } else {
1521 return zend_hash_has_more_elements_ex(aht, pos_ptr);
1522 }
1523 } /* }}} */
1524
spl_array_next(spl_array_object * intern)1525 static zend_result spl_array_next(spl_array_object *intern) /* {{{ */
1526 {
1527 HashTable *aht = spl_array_get_hash_table(intern);
1528
1529 return spl_array_next_ex(intern, aht);
1530
1531 } /* }}} */
1532
spl_array_it_dtor(zend_object_iterator * iter)1533 static void spl_array_it_dtor(zend_object_iterator *iter) /* {{{ */
1534 {
1535 zval_ptr_dtor(&iter->data);
1536 }
1537 /* }}} */
1538
spl_array_it_valid(zend_object_iterator * iter)1539 static zend_result spl_array_it_valid(zend_object_iterator *iter) /* {{{ */
1540 {
1541 spl_array_object *object = Z_SPLARRAY_P(&iter->data);
1542 HashTable *aht = spl_array_get_hash_table(object);
1543 return zend_hash_has_more_elements_ex(aht, spl_array_get_pos_ptr(aht, object));
1544 }
1545 /* }}} */
1546
spl_array_it_get_current_data(zend_object_iterator * iter)1547 static zval *spl_array_it_get_current_data(zend_object_iterator *iter) /* {{{ */
1548 {
1549 spl_array_iterator *array_iter = (spl_array_iterator*)iter;
1550 spl_array_object *object = Z_SPLARRAY_P(&iter->data);
1551 HashTable *aht = spl_array_get_hash_table(object);
1552 zval *data = zend_hash_get_current_data_ex(aht, spl_array_get_pos_ptr(aht, object));
1553 if (data && Z_TYPE_P(data) == IS_INDIRECT) {
1554 data = Z_INDIRECT_P(data);
1555 }
1556 // ZEND_FE_FETCH_RW converts the value to a reference but doesn't know the source is a property.
1557 // Typed properties must add a type source to the reference, and readonly properties must fail.
1558 if (array_iter->by_ref
1559 && Z_TYPE_P(data) != IS_REFERENCE
1560 && Z_TYPE(object->array) == IS_OBJECT
1561 && !(object->ar_flags & (SPL_ARRAY_IS_SELF|SPL_ARRAY_USE_OTHER))) {
1562 zend_string *key;
1563 zend_hash_get_current_key_ex(aht, &key, NULL, spl_array_get_pos_ptr(aht, object));
1564 zend_class_entry *ce = Z_OBJCE(object->array);
1565 zend_property_info *prop_info = zend_get_property_info(ce, key, true);
1566 ZEND_ASSERT(prop_info != ZEND_WRONG_PROPERTY_INFO);
1567 if (EXPECTED(prop_info != NULL) && ZEND_TYPE_IS_SET(prop_info->type)) {
1568 if (prop_info->flags & ZEND_ACC_READONLY) {
1569 zend_throw_error(NULL,
1570 "Cannot acquire reference to readonly property %s::$%s",
1571 ZSTR_VAL(prop_info->ce->name), ZSTR_VAL(key));
1572 return NULL;
1573 }
1574 ZVAL_NEW_REF(data, data);
1575 ZEND_REF_ADD_TYPE_SOURCE(Z_REF_P(data), prop_info);
1576 }
1577 }
1578 return data;
1579 }
1580 /* }}} */
1581
spl_array_it_get_current_key(zend_object_iterator * iter,zval * key)1582 static void spl_array_it_get_current_key(zend_object_iterator *iter, zval *key) /* {{{ */
1583 {
1584 spl_array_object *object = Z_SPLARRAY_P(&iter->data);
1585 HashTable *aht = spl_array_get_hash_table(object);
1586 zend_hash_get_current_key_zval_ex(aht, key, spl_array_get_pos_ptr(aht, object));
1587 }
1588 /* }}} */
1589
spl_array_it_move_forward(zend_object_iterator * iter)1590 static void spl_array_it_move_forward(zend_object_iterator *iter) /* {{{ */
1591 {
1592 spl_array_object *object = Z_SPLARRAY_P(&iter->data);
1593 HashTable *aht = spl_array_get_hash_table(object);
1594 spl_array_next_ex(object, aht);
1595 }
1596 /* }}} */
1597
spl_array_rewind(spl_array_object * intern)1598 static void spl_array_rewind(spl_array_object *intern) /* {{{ */
1599 {
1600 HashTable *aht = spl_array_get_hash_table(intern);
1601
1602 if (intern->ht_iter == (uint32_t)-1) {
1603 spl_array_get_pos_ptr(aht, intern);
1604 } else {
1605 zend_hash_internal_pointer_reset_ex(aht, spl_array_get_pos_ptr(aht, intern));
1606 spl_array_skip_protected(intern, aht);
1607 }
1608 }
1609 /* }}} */
1610
spl_array_it_rewind(zend_object_iterator * iter)1611 static void spl_array_it_rewind(zend_object_iterator *iter) /* {{{ */
1612 {
1613 spl_array_object *object = Z_SPLARRAY_P(&iter->data);
1614 spl_array_rewind(object);
1615 }
1616 /* }}} */
1617
spl_array_it_get_gc(zend_object_iterator * iter,zval ** table,int * n)1618 static HashTable *spl_array_it_get_gc(zend_object_iterator *iter, zval **table, int *n)
1619 {
1620 *n = 1;
1621 *table = &iter->data;
1622 return NULL;
1623 }
1624
1625 /* iterator handler table */
1626 static const zend_object_iterator_funcs spl_array_it_funcs = {
1627 spl_array_it_dtor,
1628 spl_array_it_valid,
1629 spl_array_it_get_current_data,
1630 spl_array_it_get_current_key,
1631 spl_array_it_move_forward,
1632 spl_array_it_rewind,
1633 NULL,
1634 spl_array_it_get_gc,
1635 };
1636
spl_array_get_iterator(zend_class_entry * ce,zval * object,int by_ref)1637 static zend_object_iterator *spl_array_get_iterator(zend_class_entry *ce, zval *object, int by_ref) /* {{{ */
1638 {
1639 spl_array_iterator *iterator = emalloc(sizeof(spl_array_iterator));
1640 zend_iterator_init(&iterator->it);
1641
1642 ZVAL_OBJ_COPY(&iterator->it.data, Z_OBJ_P(object));
1643 iterator->it.funcs = &spl_array_it_funcs;
1644 iterator->by_ref = by_ref;
1645
1646 return &iterator->it;
1647 }
1648 /* }}} */
1649
1650 /* {{{ Constructs a new array iterator from an array or object. */
PHP_METHOD(ArrayIterator,__construct)1651 PHP_METHOD(ArrayIterator, __construct)
1652 {
1653 zval *object = ZEND_THIS;
1654 spl_array_object *intern;
1655 zval *array;
1656 zend_long ar_flags = 0;
1657
1658 if (ZEND_NUM_ARGS() == 0) {
1659 return; /* nothing to do */
1660 }
1661
1662 if (zend_parse_parameters(ZEND_NUM_ARGS(), "|Al", &array, &ar_flags) == FAILURE) {
1663 RETURN_THROWS();
1664 }
1665
1666 intern = Z_SPLARRAY_P(object);
1667
1668 ar_flags &= ~SPL_ARRAY_INT_MASK;
1669
1670 spl_array_set_array(object, intern, array, ar_flags, ZEND_NUM_ARGS() == 1);
1671 }
1672 /* }}} */
1673
1674 /* {{{ Rewind array back to the start */
PHP_METHOD(ArrayIterator,rewind)1675 PHP_METHOD(ArrayIterator, rewind)
1676 {
1677 zval *object = ZEND_THIS;
1678 spl_array_object *intern = Z_SPLARRAY_P(object);
1679
1680 if (zend_parse_parameters_none() == FAILURE) {
1681 RETURN_THROWS();
1682 }
1683
1684 spl_array_rewind(intern);
1685 }
1686 /* }}} */
1687
1688 /* {{{ Seek to position. */
PHP_METHOD(ArrayIterator,seek)1689 PHP_METHOD(ArrayIterator, seek)
1690 {
1691 zend_long opos, position;
1692 zval *object = ZEND_THIS;
1693 spl_array_object *intern = Z_SPLARRAY_P(object);
1694 HashTable *aht = spl_array_get_hash_table(intern);
1695 int result;
1696
1697 if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &position) == FAILURE) {
1698 RETURN_THROWS();
1699 }
1700
1701 opos = position;
1702
1703 if (position >= 0) { /* negative values are not supported */
1704 spl_array_rewind(intern);
1705 result = SUCCESS;
1706
1707 while (position-- > 0 && (result = spl_array_next(intern)) == SUCCESS);
1708
1709 if (result == SUCCESS && zend_hash_has_more_elements_ex(aht, spl_array_get_pos_ptr(aht, intern)) == SUCCESS) {
1710 return; /* ok */
1711 }
1712 }
1713 zend_throw_exception_ex(spl_ce_OutOfBoundsException, 0, "Seek position " ZEND_LONG_FMT " is out of range", opos);
1714 } /* }}} */
1715
1716 /* {{{ Return current array entry */
PHP_METHOD(ArrayIterator,current)1717 PHP_METHOD(ArrayIterator, current)
1718 {
1719 zval *object = ZEND_THIS;
1720 spl_array_object *intern = Z_SPLARRAY_P(object);
1721 zval *entry;
1722 HashTable *aht = spl_array_get_hash_table(intern);
1723
1724 if (zend_parse_parameters_none() == FAILURE) {
1725 RETURN_THROWS();
1726 }
1727
1728 if ((entry = zend_hash_get_current_data_ex(aht, spl_array_get_pos_ptr(aht, intern))) == NULL) {
1729 RETURN_NULL();
1730 }
1731 if (Z_TYPE_P(entry) == IS_INDIRECT) {
1732 entry = Z_INDIRECT_P(entry);
1733 if (Z_TYPE_P(entry) == IS_UNDEF) {
1734 RETURN_NULL();
1735 }
1736 }
1737 RETURN_COPY_DEREF(entry);
1738 }
1739 /* }}} */
1740
spl_array_iterator_key(zval * object,zval * return_value)1741 void spl_array_iterator_key(zval *object, zval *return_value) /* {{{ */
1742 {
1743 spl_array_object *intern = Z_SPLARRAY_P(object);
1744 HashTable *aht = spl_array_get_hash_table(intern);
1745
1746 zend_hash_get_current_key_zval_ex(aht, return_value, spl_array_get_pos_ptr(aht, intern));
1747 }
1748 /* }}} */
1749
1750 /* {{{ Return current array key */
PHP_METHOD(ArrayIterator,key)1751 PHP_METHOD(ArrayIterator, key)
1752 {
1753 if (zend_parse_parameters_none() == FAILURE) {
1754 RETURN_THROWS();
1755 }
1756
1757 spl_array_iterator_key(ZEND_THIS, return_value);
1758 } /* }}} */
1759
1760 /* {{{ Move to next entry */
PHP_METHOD(ArrayIterator,next)1761 PHP_METHOD(ArrayIterator, next)
1762 {
1763 zval *object = ZEND_THIS;
1764 spl_array_object *intern = Z_SPLARRAY_P(object);
1765 HashTable *aht = spl_array_get_hash_table(intern);
1766
1767 if (zend_parse_parameters_none() == FAILURE) {
1768 RETURN_THROWS();
1769 }
1770
1771 spl_array_next_ex(intern, aht);
1772 }
1773 /* }}} */
1774
1775 /* {{{ Check whether array contains more entries */
PHP_METHOD(ArrayIterator,valid)1776 PHP_METHOD(ArrayIterator, valid)
1777 {
1778 zval *object = ZEND_THIS;
1779 spl_array_object *intern = Z_SPLARRAY_P(object);
1780 HashTable *aht = spl_array_get_hash_table(intern);
1781
1782 if (zend_parse_parameters_none() == FAILURE) {
1783 RETURN_THROWS();
1784 }
1785
1786 RETURN_BOOL(zend_hash_has_more_elements_ex(aht, spl_array_get_pos_ptr(aht, intern)) == SUCCESS);
1787 }
1788 /* }}} */
1789
1790 /*** RecursiveArrayIterator methods ***/
1791
1792 /* {{{ Check whether current element has children (e.g. is an array) */
PHP_METHOD(RecursiveArrayIterator,hasChildren)1793 PHP_METHOD(RecursiveArrayIterator, hasChildren)
1794 {
1795 zval *object = ZEND_THIS, *entry;
1796 spl_array_object *intern = Z_SPLARRAY_P(object);
1797 HashTable *aht = spl_array_get_hash_table(intern);
1798
1799 if (zend_parse_parameters_none() == FAILURE) {
1800 RETURN_THROWS();
1801 }
1802
1803 if ((entry = zend_hash_get_current_data_ex(aht, spl_array_get_pos_ptr(aht, intern))) == NULL) {
1804 RETURN_FALSE;
1805 }
1806
1807 if (Z_TYPE_P(entry) == IS_INDIRECT) {
1808 entry = Z_INDIRECT_P(entry);
1809 }
1810
1811 ZVAL_DEREF(entry);
1812 RETURN_BOOL(Z_TYPE_P(entry) == IS_ARRAY || (Z_TYPE_P(entry) == IS_OBJECT && (intern->ar_flags & SPL_ARRAY_CHILD_ARRAYS_ONLY) == 0));
1813 }
1814 /* }}} */
1815
spl_instantiate_child_arg(zend_class_entry * pce,zval * retval,zval * arg1,zval * arg2)1816 static void spl_instantiate_child_arg(zend_class_entry *pce, zval *retval, zval *arg1, zval *arg2) /* {{{ */
1817 {
1818 object_init_ex(retval, pce);
1819 spl_array_object *new_intern = Z_SPLARRAY_P(retval);
1820 /*
1821 * set new_intern->is_child is true to indicate that the object was created by
1822 * RecursiveArrayIterator::getChildren() method.
1823 */
1824 new_intern->is_child = true;
1825
1826 /* find the bucket of parent object. */
1827 new_intern->bucket = (Bucket *)((char *)(arg1) - XtOffsetOf(Bucket, val));;
1828 zend_call_known_instance_method_with_2_params(pce->constructor, Z_OBJ_P(retval), NULL, arg1, arg2);
1829 }
1830 /* }}} */
1831
1832 /* {{{ Create a sub iterator for the current element (same class as $this) */
PHP_METHOD(RecursiveArrayIterator,getChildren)1833 PHP_METHOD(RecursiveArrayIterator, getChildren)
1834 {
1835 zval *object = ZEND_THIS, *entry, flags;
1836 spl_array_object *intern = Z_SPLARRAY_P(object);
1837 HashTable *aht = spl_array_get_hash_table(intern);
1838
1839 if (zend_parse_parameters_none() == FAILURE) {
1840 RETURN_THROWS();
1841 }
1842
1843 if ((entry = zend_hash_get_current_data_ex(aht, spl_array_get_pos_ptr(aht, intern))) == NULL) {
1844 RETURN_NULL();
1845 }
1846
1847 if (Z_TYPE_P(entry) == IS_INDIRECT) {
1848 entry = Z_INDIRECT_P(entry);
1849 }
1850
1851 ZVAL_DEREF(entry);
1852 if (Z_TYPE_P(entry) == IS_OBJECT) {
1853 if ((intern->ar_flags & SPL_ARRAY_CHILD_ARRAYS_ONLY) != 0) {
1854 RETURN_NULL();
1855 }
1856 if (instanceof_function(Z_OBJCE_P(entry), Z_OBJCE_P(ZEND_THIS))) {
1857 RETURN_OBJ_COPY(Z_OBJ_P(entry));
1858 }
1859 }
1860
1861 ZVAL_LONG(&flags, intern->ar_flags);
1862 spl_instantiate_child_arg(Z_OBJCE_P(ZEND_THIS), return_value, entry, &flags);
1863 }
1864 /* }}} */
1865
1866 /* {{{ PHP_MINIT_FUNCTION(spl_array) */
PHP_MINIT_FUNCTION(spl_array)1867 PHP_MINIT_FUNCTION(spl_array)
1868 {
1869 spl_ce_ArrayObject = register_class_ArrayObject(zend_ce_aggregate, zend_ce_arrayaccess, zend_ce_serializable, zend_ce_countable);
1870 spl_ce_ArrayObject->create_object = spl_array_object_new;
1871 spl_ce_ArrayObject->default_object_handlers = &spl_handler_ArrayObject;
1872
1873 memcpy(&spl_handler_ArrayObject, &std_object_handlers, sizeof(zend_object_handlers));
1874
1875 spl_handler_ArrayObject.offset = XtOffsetOf(spl_array_object, std);
1876
1877 spl_handler_ArrayObject.clone_obj = spl_array_object_clone;
1878 spl_handler_ArrayObject.read_dimension = spl_array_read_dimension;
1879 spl_handler_ArrayObject.write_dimension = spl_array_write_dimension;
1880 spl_handler_ArrayObject.unset_dimension = spl_array_unset_dimension;
1881 spl_handler_ArrayObject.has_dimension = spl_array_has_dimension;
1882 spl_handler_ArrayObject.count_elements = spl_array_object_count_elements;
1883
1884 spl_handler_ArrayObject.get_properties_for = spl_array_get_properties_for;
1885 spl_handler_ArrayObject.get_gc = spl_array_get_gc;
1886 spl_handler_ArrayObject.read_property = spl_array_read_property;
1887 spl_handler_ArrayObject.write_property = spl_array_write_property;
1888 spl_handler_ArrayObject.get_property_ptr_ptr = spl_array_get_property_ptr_ptr;
1889 spl_handler_ArrayObject.has_property = spl_array_has_property;
1890 spl_handler_ArrayObject.unset_property = spl_array_unset_property;
1891
1892 spl_handler_ArrayObject.compare = spl_array_compare_objects;
1893 spl_handler_ArrayObject.free_obj = spl_array_object_free_storage;
1894
1895 spl_ce_ArrayIterator = register_class_ArrayIterator(spl_ce_SeekableIterator, zend_ce_arrayaccess, zend_ce_serializable, zend_ce_countable);
1896 spl_ce_ArrayIterator->create_object = spl_array_object_new;
1897 spl_ce_ArrayIterator->default_object_handlers = &spl_handler_ArrayObject;
1898 spl_ce_ArrayIterator->get_iterator = spl_array_get_iterator;
1899
1900 spl_ce_RecursiveArrayIterator = register_class_RecursiveArrayIterator(spl_ce_ArrayIterator, spl_ce_RecursiveIterator);
1901 spl_ce_RecursiveArrayIterator->create_object = spl_array_object_new;
1902 spl_ce_RecursiveArrayIterator->get_iterator = spl_array_get_iterator;
1903
1904 return SUCCESS;
1905 }
1906 /* }}} */
1907