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