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