1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 7                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) The PHP Group                                          |
6    +----------------------------------------------------------------------+
7    | This source file is subject to version 3.01 of the PHP license,      |
8    | that is bundled with this package in the file LICENSE, and is        |
9    | available through the world-wide-web at the following url:           |
10    | http://www.php.net/license/3_01.txt                                  |
11    | If you did not receive a copy of the PHP license and are unable to   |
12    | obtain it through the world-wide-web, please send a note to          |
13    | license@php.net so we can mail you a copy immediately.               |
14    +----------------------------------------------------------------------+
15    | Authors: Marcus Boerger <helly@php.net>                              |
16    +----------------------------------------------------------------------+
17  */
18 
19 #ifdef HAVE_CONFIG_H
20 # include "config.h"
21 #endif
22 
23 #include "php.h"
24 #include "php_ini.h"
25 #include "ext/standard/info.h"
26 #include "ext/standard/php_var.h"
27 #include "zend_smart_str.h"
28 #include "zend_interfaces.h"
29 #include "zend_exceptions.h"
30 
31 #include "php_spl.h"
32 #include "spl_functions.h"
33 #include "spl_engine.h"
34 #include "spl_iterators.h"
35 #include "spl_array.h"
36 #include "spl_exceptions.h"
37 
38 zend_object_handlers spl_handler_ArrayObject;
39 PHPAPI zend_class_entry  *spl_ce_ArrayObject;
40 
41 zend_object_handlers spl_handler_ArrayIterator;
42 PHPAPI zend_class_entry  *spl_ce_ArrayIterator;
43 PHPAPI zend_class_entry  *spl_ce_RecursiveArrayIterator;
44 
45 #define SPL_ARRAY_STD_PROP_LIST      0x00000001
46 #define SPL_ARRAY_ARRAY_AS_PROPS     0x00000002
47 #define SPL_ARRAY_CHILD_ARRAYS_ONLY  0x00000004
48 #define SPL_ARRAY_OVERLOADED_REWIND  0x00010000
49 #define SPL_ARRAY_OVERLOADED_VALID   0x00020000
50 #define SPL_ARRAY_OVERLOADED_KEY     0x00040000
51 #define SPL_ARRAY_OVERLOADED_CURRENT 0x00080000
52 #define SPL_ARRAY_OVERLOADED_NEXT    0x00100000
53 #define SPL_ARRAY_IS_SELF            0x01000000
54 #define SPL_ARRAY_USE_OTHER          0x02000000
55 #define SPL_ARRAY_INT_MASK           0xFFFF0000
56 #define SPL_ARRAY_CLONE_MASK         0x0100FFFF
57 
58 #define SPL_ARRAY_METHOD_NO_ARG				0
59 #define SPL_ARRAY_METHOD_USE_ARG    		1
60 #define SPL_ARRAY_METHOD_MAY_USER_ARG 		2
61 
62 typedef struct _spl_array_object {
63 	zval              array;
64 	uint32_t          ht_iter;
65 	int               ar_flags;
66 	unsigned char	  nApplyCount;
67 	zend_function     *fptr_offset_get;
68 	zend_function     *fptr_offset_set;
69 	zend_function     *fptr_offset_has;
70 	zend_function     *fptr_offset_del;
71 	zend_function     *fptr_count;
72 	zend_class_entry* ce_get_iterator;
73 	zend_object       std;
74 } spl_array_object;
75 
spl_array_from_obj(zend_object *obj)76 static inline spl_array_object *spl_array_from_obj(zend_object *obj) /* {{{ */ {
77 	return (spl_array_object*)((char*)(obj) - XtOffsetOf(spl_array_object, std));
78 }
79 /* }}} */
80 
81 #define Z_SPLARRAY_P(zv)  spl_array_from_obj(Z_OBJ_P((zv)))
82 
spl_array_get_hash_table_ptr(spl_array_object* intern)83 static inline HashTable **spl_array_get_hash_table_ptr(spl_array_object* intern) { /* {{{ */
84 	//??? TODO: Delay duplication for arrays; only duplicate for write operations
85 	if (intern->ar_flags & SPL_ARRAY_IS_SELF) {
86 		if (!intern->std.properties) {
87 			rebuild_object_properties(&intern->std);
88 		}
89 		return &intern->std.properties;
90 	} else if (intern->ar_flags & SPL_ARRAY_USE_OTHER) {
91 		spl_array_object *other = Z_SPLARRAY_P(&intern->array);
92 		return spl_array_get_hash_table_ptr(other);
93 	} else if (Z_TYPE(intern->array) == IS_ARRAY) {
94 		return &Z_ARRVAL(intern->array);
95 	} else {
96 		zend_object *obj = Z_OBJ(intern->array);
97 		if (!obj->properties) {
98 			rebuild_object_properties(obj);
99 		} else if (GC_REFCOUNT(obj->properties) > 1) {
100 			if (EXPECTED(!(GC_FLAGS(obj->properties) & IS_ARRAY_IMMUTABLE))) {
101 				GC_DELREF(obj->properties);
102 			}
103 			obj->properties = zend_array_dup(obj->properties);
104 		}
105 		return &obj->properties;
106 	}
107 }
108 /* }}} */
109 
spl_array_get_hash_table(spl_array_object* intern)110 static inline HashTable *spl_array_get_hash_table(spl_array_object* intern) { /* {{{ */
111 	return *spl_array_get_hash_table_ptr(intern);
112 }
113 /* }}} */
114 
spl_array_replace_hash_table(spl_array_object* intern, HashTable *ht)115 static inline void spl_array_replace_hash_table(spl_array_object* intern, HashTable *ht) { /* {{{ */
116 	HashTable **ht_ptr = spl_array_get_hash_table_ptr(intern);
117 	zend_array_destroy(*ht_ptr);
118 	*ht_ptr = ht;
119 }
120 /* }}} */
121 
spl_array_is_object(spl_array_object *intern)122 static inline zend_bool spl_array_is_object(spl_array_object *intern) /* {{{ */
123 {
124 	while (intern->ar_flags & SPL_ARRAY_USE_OTHER) {
125 		intern = Z_SPLARRAY_P(&intern->array);
126 	}
127 	return (intern->ar_flags & SPL_ARRAY_IS_SELF) || Z_TYPE(intern->array) == IS_OBJECT;
128 }
129 /* }}} */
130 
131 static int spl_array_skip_protected(spl_array_object *intern, HashTable *aht);
132 
spl_array_create_ht_iter(HashTable *ht, spl_array_object* intern)133 static zend_never_inline void spl_array_create_ht_iter(HashTable *ht, spl_array_object* intern) /* {{{ */
134 {
135 	intern->ht_iter = zend_hash_iterator_add(ht, zend_hash_get_current_pos(ht));
136 	zend_hash_internal_pointer_reset_ex(ht, &EG(ht_iterators)[intern->ht_iter].pos);
137 	spl_array_skip_protected(intern, ht);
138 }
139 /* }}} */
140 
spl_array_get_pos_ptr(HashTable *ht, spl_array_object* intern)141 static zend_always_inline uint32_t *spl_array_get_pos_ptr(HashTable *ht, spl_array_object* intern) /* {{{ */
142 {
143 	if (UNEXPECTED(intern->ht_iter == (uint32_t)-1)) {
144 		spl_array_create_ht_iter(ht, intern);
145 	}
146 	return &EG(ht_iterators)[intern->ht_iter].pos;
147 }
148 /* }}} */
149 
150 /* {{{ spl_array_object_free_storage */
spl_array_object_free_storage(zend_object *object)151 static void spl_array_object_free_storage(zend_object *object)
152 {
153 	spl_array_object *intern = spl_array_from_obj(object);
154 
155 	if (intern->ht_iter != (uint32_t) -1) {
156 		zend_hash_iterator_del(intern->ht_iter);
157 	}
158 
159 	zend_object_std_dtor(&intern->std);
160 
161 	zval_ptr_dtor(&intern->array);
162 }
163 /* }}} */
164 
165 zend_object_iterator *spl_array_get_iterator(zend_class_entry *ce, zval *object, int by_ref);
166 
167 /* {{{ spl_array_object_new_ex */
spl_array_object_new_ex(zend_class_entry *class_type, zval *orig, int clone_orig)168 static zend_object *spl_array_object_new_ex(zend_class_entry *class_type, zval *orig, int clone_orig)
169 {
170 	spl_array_object *intern;
171 	zend_class_entry *parent = class_type;
172 	int inherited = 0;
173 
174 	intern = zend_object_alloc(sizeof(spl_array_object), parent);
175 
176 	zend_object_std_init(&intern->std, class_type);
177 	object_properties_init(&intern->std, class_type);
178 
179 	intern->ar_flags = 0;
180 	intern->ce_get_iterator = spl_ce_ArrayIterator;
181 	if (orig) {
182 		spl_array_object *other = Z_SPLARRAY_P(orig);
183 
184 		intern->ar_flags &= ~ SPL_ARRAY_CLONE_MASK;
185 		intern->ar_flags |= (other->ar_flags & SPL_ARRAY_CLONE_MASK);
186 		intern->ce_get_iterator = other->ce_get_iterator;
187 		if (clone_orig) {
188 			if (other->ar_flags & SPL_ARRAY_IS_SELF) {
189 				ZVAL_UNDEF(&intern->array);
190 			} else if (Z_OBJ_HT_P(orig) == &spl_handler_ArrayObject) {
191 				ZVAL_ARR(&intern->array,
192 					zend_array_dup(spl_array_get_hash_table(other)));
193 			} else {
194 				ZEND_ASSERT(Z_OBJ_HT_P(orig) == &spl_handler_ArrayIterator);
195 				ZVAL_COPY(&intern->array, orig);
196 				intern->ar_flags |= SPL_ARRAY_USE_OTHER;
197 			}
198 		} else {
199 			ZVAL_COPY(&intern->array, orig);
200 			intern->ar_flags |= SPL_ARRAY_USE_OTHER;
201 		}
202 	} else {
203 		array_init(&intern->array);
204 	}
205 
206 	while (parent) {
207 		if (parent == spl_ce_ArrayIterator || parent == spl_ce_RecursiveArrayIterator) {
208 			intern->std.handlers = &spl_handler_ArrayIterator;
209 			break;
210 		} else if (parent == spl_ce_ArrayObject) {
211 			intern->std.handlers = &spl_handler_ArrayObject;
212 			break;
213 		}
214 		parent = parent->parent;
215 		inherited = 1;
216 	}
217 	if (!parent) { /* this must never happen */
218 		php_error_docref(NULL, E_COMPILE_ERROR, "Internal compiler error, Class is not child of ArrayObject or ArrayIterator");
219 	}
220 	if (inherited) {
221 		intern->fptr_offset_get = zend_hash_str_find_ptr(&class_type->function_table, "offsetget", sizeof("offsetget") - 1);
222 		if (intern->fptr_offset_get->common.scope == parent) {
223 			intern->fptr_offset_get = NULL;
224 		}
225 		intern->fptr_offset_set = zend_hash_str_find_ptr(&class_type->function_table, "offsetset", sizeof("offsetset") - 1);
226 		if (intern->fptr_offset_set->common.scope == parent) {
227 			intern->fptr_offset_set = NULL;
228 		}
229 		intern->fptr_offset_has = zend_hash_str_find_ptr(&class_type->function_table, "offsetexists", sizeof("offsetexists") - 1);
230 		if (intern->fptr_offset_has->common.scope == parent) {
231 			intern->fptr_offset_has = NULL;
232 		}
233 		intern->fptr_offset_del = zend_hash_str_find_ptr(&class_type->function_table, "offsetunset",  sizeof("offsetunset") - 1);
234 		if (intern->fptr_offset_del->common.scope == parent) {
235 			intern->fptr_offset_del = NULL;
236 		}
237 		intern->fptr_count = zend_hash_str_find_ptr(&class_type->function_table, "count", sizeof("count") - 1);
238 		if (intern->fptr_count->common.scope == parent) {
239 			intern->fptr_count = NULL;
240 		}
241 	}
242 	/* Cache iterator functions if ArrayIterator or derived. Check current's */
243 	/* cache since only current is always required */
244 	if (intern->std.handlers == &spl_handler_ArrayIterator) {
245 		zend_class_iterator_funcs *funcs_ptr = class_type->iterator_funcs_ptr;
246 
247 		if (!funcs_ptr->zf_current) {
248 			funcs_ptr->zf_rewind = zend_hash_str_find_ptr(&class_type->function_table, "rewind", sizeof("rewind") - 1);
249 			funcs_ptr->zf_valid = zend_hash_str_find_ptr(&class_type->function_table, "valid", sizeof("valid") - 1);
250 			funcs_ptr->zf_key = zend_hash_str_find_ptr(&class_type->function_table, "key", sizeof("key") - 1);
251 			funcs_ptr->zf_current = zend_hash_str_find_ptr(&class_type->function_table, "current", sizeof("current") - 1);
252 			funcs_ptr->zf_next = zend_hash_str_find_ptr(&class_type->function_table, "next", sizeof("next") - 1);
253 		}
254 		if (inherited) {
255 			if (funcs_ptr->zf_rewind->common.scope  != parent) intern->ar_flags |= SPL_ARRAY_OVERLOADED_REWIND;
256 			if (funcs_ptr->zf_valid->common.scope   != parent) intern->ar_flags |= SPL_ARRAY_OVERLOADED_VALID;
257 			if (funcs_ptr->zf_key->common.scope     != parent) intern->ar_flags |= SPL_ARRAY_OVERLOADED_KEY;
258 			if (funcs_ptr->zf_current->common.scope != parent) intern->ar_flags |= SPL_ARRAY_OVERLOADED_CURRENT;
259 			if (funcs_ptr->zf_next->common.scope    != parent) intern->ar_flags |= SPL_ARRAY_OVERLOADED_NEXT;
260 		}
261 	}
262 
263 	intern->ht_iter = (uint32_t)-1;
264 	return &intern->std;
265 }
266 /* }}} */
267 
268 /* {{{ spl_array_object_new */
spl_array_object_new(zend_class_entry *class_type)269 static zend_object *spl_array_object_new(zend_class_entry *class_type)
270 {
271 	return spl_array_object_new_ex(class_type, NULL, 0);
272 }
273 /* }}} */
274 
275 /* {{{ spl_array_object_clone */
spl_array_object_clone(zval *zobject)276 static zend_object *spl_array_object_clone(zval *zobject)
277 {
278 	zend_object *old_object;
279 	zend_object *new_object;
280 
281 	old_object = Z_OBJ_P(zobject);
282 	new_object = spl_array_object_new_ex(old_object->ce, zobject, 1);
283 
284 	zend_objects_clone_members(new_object, old_object);
285 
286 	return new_object;
287 }
288 /* }}} */
289 
spl_array_get_dimension_ptr(int check_inherited, spl_array_object *intern, zval *offset, int type)290 static zval *spl_array_get_dimension_ptr(int check_inherited, spl_array_object *intern, zval *offset, int type) /* {{{ */
291 {
292 	zval *retval;
293 	zend_long index;
294 	zend_string *offset_key;
295 	HashTable *ht = spl_array_get_hash_table(intern);
296 
297 	if (!offset || Z_ISUNDEF_P(offset) || !ht) {
298 		return &EG(uninitialized_zval);
299 	}
300 
301 	if ((type == BP_VAR_W || type == BP_VAR_RW) && intern->nApplyCount > 0) {
302 		zend_error(E_WARNING, "Modification of ArrayObject during sorting is prohibited");
303 		return &EG(error_zval);
304 	}
305 
306 try_again:
307 	switch (Z_TYPE_P(offset)) {
308 	case IS_NULL:
309 	   offset_key = ZSTR_EMPTY_ALLOC();
310 	   goto fetch_dim_string;
311 	case IS_STRING:
312 	   offset_key = Z_STR_P(offset);
313 fetch_dim_string:
314 		retval = zend_symtable_find(ht, offset_key);
315 		if (retval) {
316 			if (Z_TYPE_P(retval) == IS_INDIRECT) {
317 				retval = Z_INDIRECT_P(retval);
318 				if (Z_TYPE_P(retval) == IS_UNDEF) {
319 					switch (type) {
320 						case BP_VAR_R:
321 							zend_error(E_NOTICE, "Undefined index: %s", ZSTR_VAL(offset_key));
322 						case BP_VAR_UNSET:
323 						case BP_VAR_IS:
324 							retval = &EG(uninitialized_zval);
325 							break;
326 						case BP_VAR_RW:
327 							zend_error(E_NOTICE,"Undefined index: %s", ZSTR_VAL(offset_key));
328 						case BP_VAR_W: {
329 							ZVAL_NULL(retval);
330 						}
331 					}
332 				}
333 			}
334 		} else {
335 			switch (type) {
336 				case BP_VAR_R:
337 					zend_error(E_NOTICE, "Undefined index: %s", ZSTR_VAL(offset_key));
338 				case BP_VAR_UNSET:
339 				case BP_VAR_IS:
340 					retval = &EG(uninitialized_zval);
341 					break;
342 				case BP_VAR_RW:
343 					zend_error(E_NOTICE,"Undefined index: %s", ZSTR_VAL(offset_key));
344 				case BP_VAR_W: {
345 				    zval value;
346 					ZVAL_NULL(&value);
347 				    retval = zend_symtable_update(ht, offset_key, &value);
348 				}
349 			}
350 		}
351 		return retval;
352 	case IS_RESOURCE:
353 		zend_error(E_NOTICE, "Resource ID#%d used as offset, casting to integer (%d)", Z_RES_P(offset)->handle, Z_RES_P(offset)->handle);
354 		index = Z_RES_P(offset)->handle;
355 		goto num_index;
356 	case IS_DOUBLE:
357 		index = (zend_long)Z_DVAL_P(offset);
358 		goto num_index;
359 	case IS_FALSE:
360 		index = 0;
361 		goto num_index;
362 	case IS_TRUE:
363 		index = 1;
364 		goto num_index;
365 	case IS_LONG:
366 		index = Z_LVAL_P(offset);
367 num_index:
368 		if ((retval = zend_hash_index_find(ht, index)) == NULL) {
369 			switch (type) {
370 				case BP_VAR_R:
371 					zend_error(E_NOTICE, "Undefined offset: " ZEND_LONG_FMT, index);
372 				case BP_VAR_UNSET:
373 				case BP_VAR_IS:
374 					retval = &EG(uninitialized_zval);
375 					break;
376 				case BP_VAR_RW:
377 					zend_error(E_NOTICE, "Undefined offset: " ZEND_LONG_FMT, index);
378 				case BP_VAR_W: {
379 				    zval value;
380 					ZVAL_UNDEF(&value);
381 					retval = zend_hash_index_update(ht, index, &value);
382 			   }
383 			}
384 		}
385 		return retval;
386 	case IS_REFERENCE:
387 		ZVAL_DEREF(offset);
388 		goto try_again;
389 	default:
390 		zend_error(E_WARNING, "Illegal offset type");
391 		return (type == BP_VAR_W || type == BP_VAR_RW) ?
392 			&EG(error_zval) : &EG(uninitialized_zval);
393 	}
394 } /* }}} */
395 
396 static int spl_array_has_dimension(zval *object, zval *offset, int check_empty);
397 
spl_array_read_dimension_ex(int check_inherited, zval *object, zval *offset, int type, zval *rv)398 static zval *spl_array_read_dimension_ex(int check_inherited, zval *object, zval *offset, int type, zval *rv) /* {{{ */
399 {
400 	spl_array_object *intern = Z_SPLARRAY_P(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 			} else {
417 				SEPARATE_ARG_IF_REF(offset);
418 			}
419 			zend_call_method_with_1_params(object, Z_OBJCE_P(object), &intern->fptr_offset_get, "offsetGet", rv, offset);
420 			zval_ptr_dtor(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, 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(zval *object, zval *offset, int type, zval *rv)445 static zval *spl_array_read_dimension(zval *object, zval *offset, int type, zval *rv) /* {{{ */
446 {
447 	return spl_array_read_dimension_ex(1, object, offset, type, rv);
448 } /* }}} */
449 
spl_array_write_dimension_ex(int check_inherited, zval *object, zval *offset, zval *value)450 static void spl_array_write_dimension_ex(int check_inherited, zval *object, zval *offset, zval *value) /* {{{ */
451 {
452 	spl_array_object *intern = Z_SPLARRAY_P(object);
453 	zend_long index;
454 	HashTable *ht;
455 
456 	if (check_inherited && intern->fptr_offset_set) {
457 		zval tmp;
458 
459 		if (!offset) {
460 			ZVAL_NULL(&tmp);
461 			offset = &tmp;
462 		} else {
463 			SEPARATE_ARG_IF_REF(offset);
464 		}
465 		zend_call_method_with_2_params(object, Z_OBJCE_P(object), &intern->fptr_offset_set, "offsetSet", NULL, offset, value);
466 		zval_ptr_dtor(offset);
467 		return;
468 	}
469 
470 	if (intern->nApplyCount > 0) {
471 		zend_error(E_WARNING, "Modification of ArrayObject during sorting is prohibited");
472 		return;
473 	}
474 
475 	Z_TRY_ADDREF_P(value);
476 	if (!offset) {
477 		ht = spl_array_get_hash_table(intern);
478 		zend_hash_next_index_insert(ht, value);
479 		return;
480 	}
481 
482 try_again:
483 	switch (Z_TYPE_P(offset)) {
484 		case IS_STRING:
485 			ht = spl_array_get_hash_table(intern);
486 			zend_symtable_update_ind(ht, Z_STR_P(offset), value);
487 			return;
488 		case IS_DOUBLE:
489 			index = (zend_long)Z_DVAL_P(offset);
490 			goto num_index;
491 		case IS_RESOURCE:
492 			index = Z_RES_HANDLE_P(offset);
493 			goto num_index;
494 		case IS_FALSE:
495 			index = 0;
496 			goto num_index;
497 		case IS_TRUE:
498 			index = 1;
499 			goto num_index;
500 		case IS_LONG:
501 			index = Z_LVAL_P(offset);
502 num_index:
503 			ht = spl_array_get_hash_table(intern);
504 			zend_hash_index_update(ht, index, value);
505 			return;
506 		case IS_NULL:
507 			ht = spl_array_get_hash_table(intern);
508 			zend_hash_next_index_insert(ht, value);
509 			return;
510 		case IS_REFERENCE:
511 			ZVAL_DEREF(offset);
512 			goto try_again;
513 		default:
514 			zend_error(E_WARNING, "Illegal offset type");
515 			zval_ptr_dtor(value);
516 			return;
517 	}
518 } /* }}} */
519 
spl_array_write_dimension(zval *object, zval *offset, zval *value)520 static void spl_array_write_dimension(zval *object, zval *offset, zval *value) /* {{{ */
521 {
522 	spl_array_write_dimension_ex(1, object, offset, value);
523 } /* }}} */
524 
spl_array_unset_dimension_ex(int check_inherited, zval *object, zval *offset)525 static void spl_array_unset_dimension_ex(int check_inherited, zval *object, zval *offset) /* {{{ */
526 {
527 	zend_long index;
528 	HashTable *ht;
529 	spl_array_object *intern = Z_SPLARRAY_P(object);
530 
531 	if (check_inherited && intern->fptr_offset_del) {
532 		SEPARATE_ARG_IF_REF(offset);
533 		zend_call_method_with_1_params(object, Z_OBJCE_P(object), &intern->fptr_offset_del, "offsetUnset", NULL, offset);
534 		zval_ptr_dtor(offset);
535 		return;
536 	}
537 
538 	if (intern->nApplyCount > 0) {
539 		zend_error(E_WARNING, "Modification of ArrayObject during sorting is prohibited");
540 		return;
541 	}
542 
543 try_again:
544 	switch (Z_TYPE_P(offset)) {
545 	case IS_STRING:
546 		ht = spl_array_get_hash_table(intern);
547 		if (ht == &EG(symbol_table)) {
548 			if (zend_delete_global_variable(Z_STR_P(offset))) {
549 				zend_error(E_NOTICE,"Undefined index: %s", Z_STRVAL_P(offset));
550 			}
551 		} else {
552 			zval *data = zend_symtable_find(ht, Z_STR_P(offset));
553 
554 			if (data) {
555 				if (Z_TYPE_P(data) == IS_INDIRECT) {
556 					data = Z_INDIRECT_P(data);
557 					if (Z_TYPE_P(data) == IS_UNDEF) {
558 						zend_error(E_NOTICE,"Undefined index: %s", Z_STRVAL_P(offset));
559 					} else {
560 						zval_ptr_dtor(data);
561 						ZVAL_UNDEF(data);
562 						HT_FLAGS(ht) |= HASH_FLAG_HAS_EMPTY_IND;
563 						zend_hash_move_forward_ex(ht, spl_array_get_pos_ptr(ht, intern));
564 						if (spl_array_is_object(intern)) {
565 							spl_array_skip_protected(intern, ht);
566 						}
567 					}
568 				} else if (zend_symtable_del(ht, Z_STR_P(offset)) == FAILURE) {
569 					zend_error(E_NOTICE,"Undefined index: %s", Z_STRVAL_P(offset));
570 				}
571 			} else {
572 				zend_error(E_NOTICE,"Undefined index: %s", Z_STRVAL_P(offset));
573 			}
574 		}
575 		break;
576 	case IS_DOUBLE:
577 		index = (zend_long)Z_DVAL_P(offset);
578 		goto num_index;
579 	case IS_RESOURCE:
580 		index = Z_RES_HANDLE_P(offset);
581 		goto num_index;
582 	case IS_FALSE:
583 		index = 0;
584 		goto num_index;
585 	case IS_TRUE:
586 		index = 1;
587 		goto num_index;
588 	case IS_LONG:
589 		index = Z_LVAL_P(offset);
590 num_index:
591 		ht = spl_array_get_hash_table(intern);
592 		if (zend_hash_index_del(ht, index) == FAILURE) {
593 			zend_error(E_NOTICE,"Undefined offset: " ZEND_LONG_FMT, index);
594 		}
595 		break;
596 	case IS_REFERENCE:
597 		ZVAL_DEREF(offset);
598 		goto try_again;
599 	default:
600 		zend_error(E_WARNING, "Illegal offset type");
601 		return;
602 	}
603 } /* }}} */
604 
spl_array_unset_dimension(zval *object, zval *offset)605 static void spl_array_unset_dimension(zval *object, zval *offset) /* {{{ */
606 {
607 	spl_array_unset_dimension_ex(1, object, offset);
608 } /* }}} */
609 
spl_array_has_dimension_ex(int check_inherited, zval *object, zval *offset, int check_empty)610 static int spl_array_has_dimension_ex(int check_inherited, zval *object, zval *offset, int check_empty) /* {{{ */
611 {
612 	spl_array_object *intern = Z_SPLARRAY_P(object);
613 	zend_long index;
614 	zval rv, *value = NULL, *tmp;
615 
616 	if (check_inherited && intern->fptr_offset_has) {
617 		SEPARATE_ARG_IF_REF(offset);
618 		zend_call_method_with_1_params(object, Z_OBJCE_P(object), &intern->fptr_offset_has, "offsetExists", &rv, offset);
619 		zval_ptr_dtor(offset);
620 
621 		if (zend_is_true(&rv)) {
622 			zval_ptr_dtor(&rv);
623 			if (check_empty != 1) {
624 				return 1;
625 			} else if (intern->fptr_offset_get) {
626 				value = spl_array_read_dimension_ex(1, object, offset, BP_VAR_R, &rv);
627 			}
628 		} else {
629 			zval_ptr_dtor(&rv);
630 			return 0;
631 		}
632 	}
633 
634 	if (!value) {
635 		HashTable *ht = spl_array_get_hash_table(intern);
636 
637 try_again:
638 		switch (Z_TYPE_P(offset)) {
639 			case IS_STRING:
640 				if ((tmp = zend_symtable_find(ht, Z_STR_P(offset))) != NULL) {
641 					if (check_empty == 2) {
642 						return 1;
643 					}
644 				} else {
645 					return 0;
646 				}
647 				break;
648 
649 			case IS_DOUBLE:
650 				index = (zend_long)Z_DVAL_P(offset);
651 				goto num_index;
652 			case IS_RESOURCE:
653 				index = Z_RES_HANDLE_P(offset);
654 				goto num_index;
655 			case IS_FALSE:
656 				index = 0;
657 				goto num_index;
658 			case IS_TRUE:
659 				index = 1;
660 				goto num_index;
661 			case IS_LONG:
662 				index = Z_LVAL_P(offset);
663 num_index:
664 				if ((tmp = zend_hash_index_find(ht, index)) != NULL) {
665 					if (check_empty == 2) {
666 						return 1;
667 					}
668 				} else {
669 					return 0;
670 				}
671 				break;
672 			case IS_REFERENCE:
673 				ZVAL_DEREF(offset);
674 				goto try_again;
675 			default:
676 				zend_error(E_WARNING, "Illegal offset type");
677 				return 0;
678 		}
679 
680 		if (check_empty && check_inherited && intern->fptr_offset_get) {
681 			value = spl_array_read_dimension_ex(1, object, offset, BP_VAR_R, &rv);
682 		} else {
683 			value = tmp;
684 		}
685 	}
686 
687 	{
688 		zend_bool result = check_empty ? zend_is_true(value) : Z_TYPE_P(value) != IS_NULL;
689 		if (value == &rv) {
690 			zval_ptr_dtor(&rv);
691 		}
692 		return result;
693 	}
694 } /* }}} */
695 
spl_array_has_dimension(zval *object, zval *offset, int check_empty)696 static int spl_array_has_dimension(zval *object, zval *offset, int check_empty) /* {{{ */
697 {
698 	return spl_array_has_dimension_ex(1, object, offset, check_empty);
699 } /* }}} */
700 
701 /* {{{ proto bool ArrayObject::offsetExists(mixed $index)
702        proto bool ArrayIterator::offsetExists(mixed $index)
703    Returns whether the requested $index exists. */
SPL_METHODnull704 SPL_METHOD(Array, offsetExists)
705 {
706 	zval *index;
707 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &index) == FAILURE) {
708 		return;
709 	}
710 	RETURN_BOOL(spl_array_has_dimension_ex(0, ZEND_THIS, index, 2));
711 } /* }}} */
712 
713 /* {{{ proto mixed ArrayObject::offsetGet(mixed $index)
714        proto mixed ArrayIterator::offsetGet(mixed $index)
715    Returns the value at the specified $index. */
SPL_METHODnull716 SPL_METHOD(Array, offsetGet)
717 {
718 	zval *value, *index;
719 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &index) == FAILURE) {
720 		return;
721 	}
722 	value = spl_array_read_dimension_ex(0, ZEND_THIS, index, BP_VAR_R, return_value);
723 	if (value != return_value) {
724 		ZVAL_COPY_DEREF(return_value, value);
725 	}
726 } /* }}} */
727 
728 /* {{{ proto void ArrayObject::offsetSet(mixed $index, mixed $newval)
729        proto void ArrayIterator::offsetSet(mixed $index, mixed $newval)
730    Sets the value at the specified $index to $newval. */
SPL_METHODnull731 SPL_METHOD(Array, offsetSet)
732 {
733 	zval *index, *value;
734 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "zz", &index, &value) == FAILURE) {
735 		return;
736 	}
737 	spl_array_write_dimension_ex(0, ZEND_THIS, index, value);
738 } /* }}} */
739 
spl_array_iterator_append(zval *object, zval *append_value)740 void spl_array_iterator_append(zval *object, zval *append_value) /* {{{ */
741 {
742 	spl_array_object *intern = Z_SPLARRAY_P(object);
743 
744 	if (spl_array_is_object(intern)) {
745 		zend_throw_error(NULL, "Cannot append properties to objects, use %s::offsetSet() instead", ZSTR_VAL(Z_OBJCE_P(object)->name));
746 		return;
747 	}
748 
749 	spl_array_write_dimension(object, NULL, append_value);
750 } /* }}} */
751 
752 /* {{{ proto void ArrayObject::append(mixed $newval)
753        proto void ArrayIterator::append(mixed $newval)
754    Appends the value (cannot be called for objects). */
SPL_METHODnull755 SPL_METHOD(Array, append)
756 {
757 	zval *value;
758 
759 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &value) == FAILURE) {
760 		return;
761 	}
762 	spl_array_iterator_append(ZEND_THIS, value);
763 } /* }}} */
764 
765 /* {{{ proto void ArrayObject::offsetUnset(mixed $index)
766        proto void ArrayIterator::offsetUnset(mixed $index)
767    Unsets the value at the specified $index. */
SPL_METHODnull768 SPL_METHOD(Array, offsetUnset)
769 {
770 	zval *index;
771 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &index) == FAILURE) {
772 		return;
773 	}
774 	spl_array_unset_dimension_ex(0, ZEND_THIS, index);
775 } /* }}} */
776 
777 /* {{{ proto array ArrayObject::getArrayCopy()
778       proto array ArrayIterator::getArrayCopy()
779    Return a copy of the contained array */
SPL_METHODnull780 SPL_METHOD(Array, getArrayCopy)
781 {
782 	zval *object = ZEND_THIS;
783 	spl_array_object *intern = Z_SPLARRAY_P(object);
784 
785 	RETURN_ARR(zend_array_dup(spl_array_get_hash_table(intern)));
786 } /* }}} */
787 
spl_array_get_properties_for(zval *object, zend_prop_purpose purpose)788 static HashTable *spl_array_get_properties_for(zval *object, zend_prop_purpose purpose) /* {{{ */
789 {
790 	spl_array_object *intern = Z_SPLARRAY_P(object);
791 	HashTable *ht;
792 	zend_bool dup;
793 
794 	if (intern->ar_flags & SPL_ARRAY_STD_PROP_LIST) {
795 		return zend_std_get_properties_for(object, purpose);
796 	}
797 
798 	/* We are supposed to be the only owner of the internal hashtable.
799 	 * The "dup" flag decides whether this is a "long-term" use where
800 	 * we need to duplicate, or a "temporary" one, where we can expect
801 	 * that no operations on the ArrayObject will be performed in the
802 	 * meantime. */
803 	switch (purpose) {
804 		case ZEND_PROP_PURPOSE_ARRAY_CAST:
805 			dup = 1;
806 			break;
807 		case ZEND_PROP_PURPOSE_VAR_EXPORT:
808 		case ZEND_PROP_PURPOSE_JSON:
809 		case _ZEND_PROP_PURPOSE_ARRAY_KEY_EXISTS:
810 			dup = 0;
811 			break;
812 		default:
813 			return zend_std_get_properties_for(object, purpose);
814 	}
815 
816 	ht = spl_array_get_hash_table(intern);
817 	if (dup) {
818 		ht = zend_array_dup(ht);
819 	} else {
820 		GC_ADDREF(ht);
821 	}
822 	return ht;
823 } /* }}} */
824 
spl_array_get_debug_info(zval *obj)825 static inline HashTable* spl_array_get_debug_info(zval *obj) /* {{{ */
826 {
827 	zval *storage;
828 	zend_string *zname;
829 	zend_class_entry *base;
830 	spl_array_object *intern = Z_SPLARRAY_P(obj);
831 
832 	if (!intern->std.properties) {
833 		rebuild_object_properties(&intern->std);
834 	}
835 
836 	if (intern->ar_flags & SPL_ARRAY_IS_SELF) {
837 		return zend_array_dup(intern->std.properties);
838 	} else {
839 		HashTable *debug_info;
840 
841 		debug_info = zend_new_array(zend_hash_num_elements(intern->std.properties) + 1);
842 		zend_hash_copy(debug_info, intern->std.properties, (copy_ctor_func_t) zval_add_ref);
843 
844 		storage = &intern->array;
845 		Z_TRY_ADDREF_P(storage);
846 
847 		base = Z_OBJ_HT_P(obj) == &spl_handler_ArrayIterator
848 			? spl_ce_ArrayIterator : spl_ce_ArrayObject;
849 		zname = spl_gen_private_prop_name(base, "storage", sizeof("storage")-1);
850 		zend_symtable_update(debug_info, zname, storage);
851 		zend_string_release_ex(zname, 0);
852 
853 		return debug_info;
854 	}
855 }
856 /* }}} */
857 
spl_array_get_gc(zval *obj, zval **gc_data, int *gc_data_count)858 static HashTable *spl_array_get_gc(zval *obj, zval **gc_data, int *gc_data_count) /* {{{ */
859 {
860 	spl_array_object *intern = Z_SPLARRAY_P(obj);
861 	*gc_data = &intern->array;
862 	*gc_data_count = 1;
863 	return zend_std_get_properties(obj);
864 }
865 /* }}} */
866 
spl_array_read_property(zval *object, zval *member, int type, void **cache_slot, zval *rv)867 static zval *spl_array_read_property(zval *object, zval *member, int type, void **cache_slot, zval *rv) /* {{{ */
868 {
869 	spl_array_object *intern = Z_SPLARRAY_P(object);
870 
871 	if ((intern->ar_flags & SPL_ARRAY_ARRAY_AS_PROPS) != 0
872 		&& !zend_std_has_property(object, member, ZEND_PROPERTY_EXISTS, NULL)) {
873 		return spl_array_read_dimension(object, member, type, rv);
874 	}
875 	return zend_std_read_property(object, member, type, cache_slot, rv);
876 } /* }}} */
877 
spl_array_write_property(zval *object, zval *member, zval *value, void **cache_slot)878 static zval *spl_array_write_property(zval *object, zval *member, zval *value, void **cache_slot) /* {{{ */
879 {
880 	spl_array_object *intern = Z_SPLARRAY_P(object);
881 
882 	if ((intern->ar_flags & SPL_ARRAY_ARRAY_AS_PROPS) != 0
883 	&& !zend_std_has_property(object, member, ZEND_PROPERTY_EXISTS, NULL)) {
884 		spl_array_write_dimension(object, member, value);
885 		return value;
886 	}
887 	return zend_std_write_property(object, member, value, cache_slot);
888 } /* }}} */
889 
spl_array_get_property_ptr_ptr(zval *object, zval *member, int type, void **cache_slot)890 static zval *spl_array_get_property_ptr_ptr(zval *object, zval *member, int type, void **cache_slot) /* {{{ */
891 {
892 	spl_array_object *intern = Z_SPLARRAY_P(object);
893 
894 	if ((intern->ar_flags & SPL_ARRAY_ARRAY_AS_PROPS) != 0
895 		&& !zend_std_has_property(object, member, ZEND_PROPERTY_EXISTS, NULL)) {
896 		/* If object has offsetGet() overridden, then fallback to read_property,
897 		 * which will call offsetGet(). */
898 		if (intern->fptr_offset_get) {
899 			return NULL;
900 		}
901 		return spl_array_get_dimension_ptr(1, intern, member, type);
902 	}
903 	return zend_std_get_property_ptr_ptr(object, member, type, cache_slot);
904 } /* }}} */
905 
spl_array_has_property(zval *object, zval *member, int has_set_exists, void **cache_slot)906 static int spl_array_has_property(zval *object, zval *member, int has_set_exists, void **cache_slot) /* {{{ */
907 {
908 	spl_array_object *intern = Z_SPLARRAY_P(object);
909 
910 	if ((intern->ar_flags & SPL_ARRAY_ARRAY_AS_PROPS) != 0
911 		&& !zend_std_has_property(object, member, ZEND_PROPERTY_EXISTS, NULL)) {
912 		return spl_array_has_dimension(object, member, has_set_exists);
913 	}
914 	return zend_std_has_property(object, member, has_set_exists, cache_slot);
915 } /* }}} */
916 
spl_array_unset_property(zval *object, zval *member, void **cache_slot)917 static void spl_array_unset_property(zval *object, zval *member, void **cache_slot) /* {{{ */
918 {
919 	spl_array_object *intern = Z_SPLARRAY_P(object);
920 
921 	if ((intern->ar_flags & SPL_ARRAY_ARRAY_AS_PROPS) != 0
922 		&& !zend_std_has_property(object, member, ZEND_PROPERTY_EXISTS, NULL)) {
923 		spl_array_unset_dimension(object, member);
924 		return;
925 	}
926 	zend_std_unset_property(object, member, cache_slot);
927 } /* }}} */
928 
spl_array_compare_objects(zval *o1, zval *o2)929 static int spl_array_compare_objects(zval *o1, zval *o2) /* {{{ */
930 {
931 	HashTable			*ht1,
932 						*ht2;
933 	spl_array_object	*intern1,
934 						*intern2;
935 	int					result	= 0;
936 
937 	intern1	= Z_SPLARRAY_P(o1);
938 	intern2	= Z_SPLARRAY_P(o2);
939 	ht1		= spl_array_get_hash_table(intern1);
940 	ht2		= spl_array_get_hash_table(intern2);
941 
942 	result = zend_compare_symbol_tables(ht1, ht2);
943 	/* if we just compared std.properties, don't do it again */
944 	if (result == 0 &&
945 			!(ht1 == intern1->std.properties && ht2 == intern2->std.properties)) {
946 		result = zend_std_compare_objects(o1, o2);
947 	}
948 	return result;
949 } /* }}} */
950 
spl_array_skip_protected(spl_array_object *intern, HashTable *aht)951 static int spl_array_skip_protected(spl_array_object *intern, HashTable *aht) /* {{{ */
952 {
953 	zend_string *string_key;
954 	zend_ulong num_key;
955 	zval *data;
956 
957 	if (spl_array_is_object(intern)) {
958 		uint32_t *pos_ptr = spl_array_get_pos_ptr(aht, intern);
959 
960 		do {
961 			if (zend_hash_get_current_key_ex(aht, &string_key, &num_key, pos_ptr) == HASH_KEY_IS_STRING) {
962 				data = zend_hash_get_current_data_ex(aht, pos_ptr);
963 				if (data && Z_TYPE_P(data) == IS_INDIRECT &&
964 				    Z_TYPE_P(data = Z_INDIRECT_P(data)) == IS_UNDEF) {
965 					/* skip */
966 				} else if (!ZSTR_LEN(string_key) || ZSTR_VAL(string_key)[0]) {
967 					return SUCCESS;
968 				}
969 			} else {
970 				return SUCCESS;
971 			}
972 			if (zend_hash_has_more_elements_ex(aht, pos_ptr) != SUCCESS) {
973 				return FAILURE;
974 			}
975 			zend_hash_move_forward_ex(aht, pos_ptr);
976 		} while (1);
977 	}
978 	return FAILURE;
979 } /* }}} */
980 
spl_array_next_ex(spl_array_object *intern, HashTable *aht)981 static int spl_array_next_ex(spl_array_object *intern, HashTable *aht) /* {{{ */
982 {
983 	uint32_t *pos_ptr = spl_array_get_pos_ptr(aht, intern);
984 
985 	zend_hash_move_forward_ex(aht, pos_ptr);
986 	if (spl_array_is_object(intern)) {
987 		return spl_array_skip_protected(intern, aht);
988 	} else {
989 		return zend_hash_has_more_elements_ex(aht, pos_ptr);
990 	}
991 } /* }}} */
992 
spl_array_next(spl_array_object *intern)993 static int spl_array_next(spl_array_object *intern) /* {{{ */
994 {
995 	HashTable *aht = spl_array_get_hash_table(intern);
996 
997 	return spl_array_next_ex(intern, aht);
998 
999 } /* }}} */
1000 
spl_array_it_dtor(zend_object_iterator *iter)1001 static void spl_array_it_dtor(zend_object_iterator *iter) /* {{{ */
1002 {
1003 	zend_user_it_invalidate_current(iter);
1004 	zval_ptr_dtor(&iter->data);
1005 }
1006 /* }}} */
1007 
spl_array_it_valid(zend_object_iterator *iter)1008 static int spl_array_it_valid(zend_object_iterator *iter) /* {{{ */
1009 {
1010 	spl_array_object *object = Z_SPLARRAY_P(&iter->data);
1011 	HashTable *aht = spl_array_get_hash_table(object);
1012 
1013 	if (object->ar_flags & SPL_ARRAY_OVERLOADED_VALID) {
1014 		return zend_user_it_valid(iter);
1015 	} else {
1016 		return zend_hash_has_more_elements_ex(aht, spl_array_get_pos_ptr(aht, object));
1017 	}
1018 }
1019 /* }}} */
1020 
spl_array_it_get_current_data(zend_object_iterator *iter)1021 static zval *spl_array_it_get_current_data(zend_object_iterator *iter) /* {{{ */
1022 {
1023 	spl_array_object *object = Z_SPLARRAY_P(&iter->data);
1024 	HashTable *aht = spl_array_get_hash_table(object);
1025 
1026 	if (object->ar_flags & SPL_ARRAY_OVERLOADED_CURRENT) {
1027 		return zend_user_it_get_current_data(iter);
1028 	} else {
1029 		zval *data = zend_hash_get_current_data_ex(aht, spl_array_get_pos_ptr(aht, object));
1030 		if (data && Z_TYPE_P(data) == IS_INDIRECT) {
1031 			data = Z_INDIRECT_P(data);
1032 		}
1033 		return data;
1034 	}
1035 }
1036 /* }}} */
1037 
spl_array_it_get_current_key(zend_object_iterator *iter, zval *key)1038 static void spl_array_it_get_current_key(zend_object_iterator *iter, zval *key) /* {{{ */
1039 {
1040 	spl_array_object *object = Z_SPLARRAY_P(&iter->data);
1041 	HashTable *aht = spl_array_get_hash_table(object);
1042 
1043 	if (object->ar_flags & SPL_ARRAY_OVERLOADED_KEY) {
1044 		zend_user_it_get_current_key(iter, key);
1045 	} else {
1046 		zend_hash_get_current_key_zval_ex(aht, key, spl_array_get_pos_ptr(aht, object));
1047 	}
1048 }
1049 /* }}} */
1050 
spl_array_it_move_forward(zend_object_iterator *iter)1051 static void spl_array_it_move_forward(zend_object_iterator *iter) /* {{{ */
1052 {
1053 	spl_array_object *object = Z_SPLARRAY_P(&iter->data);
1054 	HashTable *aht = spl_array_get_hash_table(object);
1055 
1056 	if (object->ar_flags & SPL_ARRAY_OVERLOADED_NEXT) {
1057 		zend_user_it_move_forward(iter);
1058 	} else {
1059 		zend_user_it_invalidate_current(iter);
1060 		spl_array_next_ex(object, aht);
1061 	}
1062 }
1063 /* }}} */
1064 
spl_array_rewind(spl_array_object *intern)1065 static void spl_array_rewind(spl_array_object *intern) /* {{{ */
1066 {
1067 	HashTable *aht = spl_array_get_hash_table(intern);
1068 
1069 	if (intern->ht_iter == (uint32_t)-1) {
1070 		spl_array_get_pos_ptr(aht, intern);
1071 	} else {
1072 		zend_hash_internal_pointer_reset_ex(aht, spl_array_get_pos_ptr(aht, intern));
1073 		spl_array_skip_protected(intern, aht);
1074 	}
1075 }
1076 /* }}} */
1077 
spl_array_it_rewind(zend_object_iterator *iter)1078 static void spl_array_it_rewind(zend_object_iterator *iter) /* {{{ */
1079 {
1080 	spl_array_object *object = Z_SPLARRAY_P(&iter->data);
1081 
1082 	if (object->ar_flags & SPL_ARRAY_OVERLOADED_REWIND) {
1083 		zend_user_it_rewind(iter);
1084 	} else {
1085 		zend_user_it_invalidate_current(iter);
1086 		spl_array_rewind(object);
1087 	}
1088 }
1089 /* }}} */
1090 
1091 /* {{{ spl_array_set_array */
spl_array_set_array(zval *object, spl_array_object *intern, zval *array, zend_long ar_flags, int just_array)1092 static void spl_array_set_array(zval *object, spl_array_object *intern, zval *array, zend_long ar_flags, int just_array) {
1093 	if (Z_TYPE_P(array) != IS_OBJECT && Z_TYPE_P(array) != IS_ARRAY) {
1094 		zend_throw_exception(spl_ce_InvalidArgumentException, "Passed variable is not an array or object", 0);
1095 		return;
1096 	}
1097 
1098 	if (Z_TYPE_P(array) == IS_ARRAY) {
1099 		zval_ptr_dtor(&intern->array);
1100 		if (Z_REFCOUNT_P(array) == 1) {
1101 			ZVAL_COPY(&intern->array, array);
1102 		} else {
1103 			//??? TODO: try to avoid array duplication
1104 			ZVAL_ARR(&intern->array, zend_array_dup(Z_ARR_P(array)));
1105 		}
1106 	} else {
1107 		if (Z_OBJ_HT_P(array) == &spl_handler_ArrayObject || Z_OBJ_HT_P(array) == &spl_handler_ArrayIterator) {
1108 			zval_ptr_dtor(&intern->array);
1109 			if (just_array)	{
1110 				spl_array_object *other = Z_SPLARRAY_P(array);
1111 				ar_flags = other->ar_flags & ~SPL_ARRAY_INT_MASK;
1112 			}
1113 			if (Z_OBJ_P(object) == Z_OBJ_P(array)) {
1114 				ar_flags |= SPL_ARRAY_IS_SELF;
1115 				ZVAL_UNDEF(&intern->array);
1116 			} else {
1117 				ar_flags |= SPL_ARRAY_USE_OTHER;
1118 				ZVAL_COPY(&intern->array, array);
1119 			}
1120 		} else {
1121 			zend_object_get_properties_t handler = Z_OBJ_HANDLER_P(array, get_properties);
1122 			if (handler != zend_std_get_properties) {
1123 				zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0,
1124 					"Overloaded object of type %s is not compatible with %s",
1125 					ZSTR_VAL(Z_OBJCE_P(array)->name), ZSTR_VAL(intern->std.ce->name));
1126 				return;
1127 			}
1128 			zval_ptr_dtor(&intern->array);
1129 			ZVAL_COPY(&intern->array, array);
1130 		}
1131 	}
1132 
1133 	intern->ar_flags &= ~SPL_ARRAY_IS_SELF & ~SPL_ARRAY_USE_OTHER;
1134 	intern->ar_flags |= ar_flags;
1135 	intern->ht_iter = (uint32_t)-1;
1136 }
1137 /* }}} */
1138 
1139 /* iterator handler table */
1140 static const zend_object_iterator_funcs spl_array_it_funcs = {
1141 	spl_array_it_dtor,
1142 	spl_array_it_valid,
1143 	spl_array_it_get_current_data,
1144 	spl_array_it_get_current_key,
1145 	spl_array_it_move_forward,
1146 	spl_array_it_rewind,
1147 	NULL
1148 };
1149 
spl_array_get_iterator(zend_class_entry *ce, zval *object, int by_ref)1150 zend_object_iterator *spl_array_get_iterator(zend_class_entry *ce, zval *object, int by_ref) /* {{{ */
1151 {
1152 	zend_user_iterator *iterator;
1153 	spl_array_object *array_object = Z_SPLARRAY_P(object);
1154 
1155 	if (by_ref && (array_object->ar_flags & SPL_ARRAY_OVERLOADED_CURRENT)) {
1156 		zend_throw_exception(spl_ce_RuntimeException, "An iterator cannot be used with foreach by reference", 0);
1157 		return NULL;
1158 	}
1159 
1160 	iterator = emalloc(sizeof(zend_user_iterator));
1161 
1162 	zend_iterator_init(&iterator->it);
1163 
1164 	Z_ADDREF_P(object);
1165 	ZVAL_OBJ(&iterator->it.data, Z_OBJ_P(object));
1166 	iterator->it.funcs = &spl_array_it_funcs;
1167 	iterator->ce = ce;
1168 	ZVAL_UNDEF(&iterator->value);
1169 
1170 	return &iterator->it;
1171 }
1172 /* }}} */
1173 
1174 /* {{{ proto ArrayObject::__construct([array|object ar = array() [, int flags = 0 [, string iterator_class = "ArrayIterator"]]])
1175    Constructs a new array object from an array or object. */
SPL_METHODnull1176 SPL_METHOD(Array, __construct)
1177 {
1178 	zval *object = ZEND_THIS;
1179 	spl_array_object *intern;
1180 	zval *array;
1181 	zend_long ar_flags = 0;
1182 	zend_class_entry *ce_get_iterator = spl_ce_Iterator;
1183 
1184 	if (ZEND_NUM_ARGS() == 0) {
1185 		return; /* nothing to do */
1186 	}
1187 
1188 	if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "z|lC", &array, &ar_flags, &ce_get_iterator) == FAILURE) {
1189 		return;
1190 	}
1191 
1192 	intern = Z_SPLARRAY_P(object);
1193 
1194 	if (ZEND_NUM_ARGS() > 2) {
1195 		intern->ce_get_iterator = ce_get_iterator;
1196 	}
1197 
1198 	ar_flags &= ~SPL_ARRAY_INT_MASK;
1199 
1200 	spl_array_set_array(object, intern, array, ar_flags, ZEND_NUM_ARGS() == 1);
1201 }
1202  /* }}} */
1203 
1204 /* {{{ proto ArrayIterator::__construct([array|object ar = array() [, int flags = 0]])
1205    Constructs a new array iterator from an array or object. */
SPL_METHODnull1206 SPL_METHOD(ArrayIterator, __construct)
1207 {
1208 	zval *object = ZEND_THIS;
1209 	spl_array_object *intern;
1210 	zval *array;
1211 	zend_long ar_flags = 0;
1212 
1213 	if (ZEND_NUM_ARGS() == 0) {
1214 		return; /* nothing to do */
1215 	}
1216 
1217 	if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "z|l", &array, &ar_flags) == FAILURE) {
1218 		return;
1219 	}
1220 
1221 	intern = Z_SPLARRAY_P(object);
1222 
1223 	ar_flags &= ~SPL_ARRAY_INT_MASK;
1224 
1225 	spl_array_set_array(object, intern, array, ar_flags, ZEND_NUM_ARGS() == 1);
1226 }
1227  /* }}} */
1228 
1229 /* {{{ proto void ArrayObject::setIteratorClass(string iterator_class)
1230    Set the class used in getIterator. */
SPL_METHODnull1231 SPL_METHOD(Array, setIteratorClass)
1232 {
1233 	zval *object = ZEND_THIS;
1234 	spl_array_object *intern = Z_SPLARRAY_P(object);
1235 	zend_class_entry * ce_get_iterator = spl_ce_Iterator;
1236 
1237 	ZEND_PARSE_PARAMETERS_START(1, 1)
1238 		Z_PARAM_CLASS(ce_get_iterator)
1239 	ZEND_PARSE_PARAMETERS_END();
1240 
1241 	intern->ce_get_iterator = ce_get_iterator;
1242 }
1243 /* }}} */
1244 
1245 /* {{{ proto string ArrayObject::getIteratorClass()
1246    Get the class used in getIterator. */
SPL_METHODnull1247 SPL_METHOD(Array, getIteratorClass)
1248 {
1249 	zval *object = ZEND_THIS;
1250 	spl_array_object *intern = Z_SPLARRAY_P(object);
1251 
1252 	if (zend_parse_parameters_none() == FAILURE) {
1253 		return;
1254 	}
1255 
1256 	zend_string_addref(intern->ce_get_iterator->name);
1257 	RETURN_STR(intern->ce_get_iterator->name);
1258 }
1259 /* }}} */
1260 
1261 /* {{{ proto int ArrayObject::getFlags()
1262    Get flags */
SPL_METHODnull1263 SPL_METHOD(Array, getFlags)
1264 {
1265 	zval *object = ZEND_THIS;
1266 	spl_array_object *intern = Z_SPLARRAY_P(object);
1267 
1268 	if (zend_parse_parameters_none() == FAILURE) {
1269 		return;
1270 	}
1271 
1272 	RETURN_LONG(intern->ar_flags & ~SPL_ARRAY_INT_MASK);
1273 }
1274 /* }}} */
1275 
1276 /* {{{ proto void ArrayObject::setFlags(int flags)
1277    Set flags */
SPL_METHODnull1278 SPL_METHOD(Array, setFlags)
1279 {
1280 	zval *object = ZEND_THIS;
1281 	spl_array_object *intern = Z_SPLARRAY_P(object);
1282 	zend_long ar_flags = 0;
1283 
1284 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &ar_flags) == FAILURE) {
1285 		return;
1286 	}
1287 
1288 	intern->ar_flags = (intern->ar_flags & SPL_ARRAY_INT_MASK) | (ar_flags & ~SPL_ARRAY_INT_MASK);
1289 }
1290 /* }}} */
1291 
1292 /* {{{ proto Array|Object ArrayObject::exchangeArray(Array|Object input = array())
1293    Replace the referenced array or object with a new one and return the old one (right now copy - to be changed) */
SPL_METHODnull1294 SPL_METHOD(Array, exchangeArray)
1295 {
1296 	zval *object = ZEND_THIS, *array;
1297 	spl_array_object *intern = Z_SPLARRAY_P(object);
1298 
1299 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &array) == FAILURE) {
1300 		return;
1301 	}
1302 
1303 	if (intern->nApplyCount > 0) {
1304 		zend_error(E_WARNING, "Modification of ArrayObject during sorting is prohibited");
1305 		return;
1306 	}
1307 
1308 	RETVAL_ARR(zend_array_dup(spl_array_get_hash_table(intern)));
1309 	spl_array_set_array(object, intern, array, 0L, 1);
1310 }
1311 /* }}} */
1312 
1313 /* {{{ proto ArrayIterator ArrayObject::getIterator()
1314    Create a new iterator from a ArrayObject instance */
SPL_METHODnull1315 SPL_METHOD(Array, getIterator)
1316 {
1317 	zval *object = ZEND_THIS;
1318 	spl_array_object *intern = Z_SPLARRAY_P(object);
1319 
1320 	if (zend_parse_parameters_none() == FAILURE) {
1321 		return;
1322 	}
1323 
1324 	ZVAL_OBJ(return_value, spl_array_object_new_ex(intern->ce_get_iterator, object, 0));
1325 }
1326 /* }}} */
1327 
1328 /* {{{ proto void ArrayIterator::rewind()
1329    Rewind array back to the start */
SPL_METHODnull1330 SPL_METHOD(Array, rewind)
1331 {
1332 	zval *object = ZEND_THIS;
1333 	spl_array_object *intern = Z_SPLARRAY_P(object);
1334 
1335 	if (zend_parse_parameters_none() == FAILURE) {
1336 		return;
1337 	}
1338 
1339 	spl_array_rewind(intern);
1340 }
1341 /* }}} */
1342 
1343 /* {{{ proto void ArrayIterator::seek(int $position)
1344    Seek to position. */
SPL_METHODnull1345 SPL_METHOD(Array, seek)
1346 {
1347 	zend_long opos, position;
1348 	zval *object = ZEND_THIS;
1349 	spl_array_object *intern = Z_SPLARRAY_P(object);
1350 	HashTable *aht = spl_array_get_hash_table(intern);
1351 	int result;
1352 
1353 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &position) == FAILURE) {
1354 		return;
1355 	}
1356 
1357 	opos = position;
1358 
1359 	if (position >= 0) { /* negative values are not supported */
1360 		spl_array_rewind(intern);
1361 		result = SUCCESS;
1362 
1363 		while (position-- > 0 && (result = spl_array_next(intern)) == SUCCESS);
1364 
1365 		if (result == SUCCESS && zend_hash_has_more_elements_ex(aht, spl_array_get_pos_ptr(aht, intern)) == SUCCESS) {
1366 			return; /* ok */
1367 		}
1368 	}
1369 	zend_throw_exception_ex(spl_ce_OutOfBoundsException, 0, "Seek position " ZEND_LONG_FMT " is out of range", opos);
1370 } /* }}} */
1371 
spl_array_object_count_elements_helper(spl_array_object *intern)1372 static zend_long spl_array_object_count_elements_helper(spl_array_object *intern) /* {{{ */
1373 {
1374 	HashTable *aht = spl_array_get_hash_table(intern);
1375 	if (spl_array_is_object(intern)) {
1376 		zend_long count = 0;
1377 		zend_string *key;
1378 		zval *val;
1379 		/* Count public/dynamic properties */
1380 		ZEND_HASH_FOREACH_STR_KEY_VAL(aht, key, val) {
1381 			if (Z_TYPE_P(val) == IS_INDIRECT) {
1382 				if (Z_TYPE_P(Z_INDIRECT_P(val)) == IS_UNDEF) continue;
1383 				if (key && ZSTR_VAL(key)[0] == '\0') continue;
1384 			}
1385 			count++;
1386 		} ZEND_HASH_FOREACH_END();
1387 		return count;
1388 	} else {
1389 		return zend_hash_num_elements(aht);
1390 	}
1391 } /* }}} */
1392 
spl_array_object_count_elements(zval *object, zend_long *count)1393 int spl_array_object_count_elements(zval *object, zend_long *count) /* {{{ */
1394 {
1395 	spl_array_object *intern = Z_SPLARRAY_P(object);
1396 
1397 	if (intern->fptr_count) {
1398 		zval rv;
1399 		zend_call_method_with_0_params(object, intern->std.ce, &intern->fptr_count, "count", &rv);
1400 		if (Z_TYPE(rv) != IS_UNDEF) {
1401 			*count = zval_get_long(&rv);
1402 			zval_ptr_dtor(&rv);
1403 			return SUCCESS;
1404 		}
1405 		*count = 0;
1406 		return FAILURE;
1407 	}
1408 	*count = spl_array_object_count_elements_helper(intern);
1409 	return SUCCESS;
1410 } /* }}} */
1411 
1412 /* {{{ proto int ArrayObject::count()
1413        proto int ArrayIterator::count()
1414    Return the number of elements in the Iterator. */
SPL_METHODnull1415 SPL_METHOD(Array, count)
1416 {
1417 	spl_array_object *intern = Z_SPLARRAY_P(ZEND_THIS);
1418 
1419 	if (zend_parse_parameters_none() == FAILURE) {
1420 		return;
1421 	}
1422 
1423 	RETURN_LONG(spl_array_object_count_elements_helper(intern));
1424 } /* }}} */
1425 
spl_array_method(INTERNAL_FUNCTION_PARAMETERS, char *fname, int fname_len, int use_arg)1426 static void spl_array_method(INTERNAL_FUNCTION_PARAMETERS, char *fname, int fname_len, int use_arg) /* {{{ */
1427 {
1428 	spl_array_object *intern = Z_SPLARRAY_P(ZEND_THIS);
1429 	HashTable *aht = spl_array_get_hash_table(intern);
1430 	zval function_name, params[2], *arg = NULL;
1431 
1432 	ZVAL_STRINGL(&function_name, fname, fname_len);
1433 
1434 	ZVAL_NEW_EMPTY_REF(&params[0]);
1435 	ZVAL_ARR(Z_REFVAL(params[0]), aht);
1436 	GC_ADDREF(aht);
1437 
1438 	if (!use_arg) {
1439 		intern->nApplyCount++;
1440 		call_user_function(EG(function_table), NULL, &function_name, return_value, 1, params);
1441 		intern->nApplyCount--;
1442 	} else if (use_arg == SPL_ARRAY_METHOD_MAY_USER_ARG) {
1443 		if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "|z", &arg) == FAILURE) {
1444 			zend_throw_exception(spl_ce_BadMethodCallException, "Function expects one argument at most", 0);
1445 			goto exit;
1446 		}
1447 		if (arg) {
1448 			ZVAL_COPY_VALUE(&params[1], arg);
1449 		}
1450 		intern->nApplyCount++;
1451 		call_user_function(EG(function_table), NULL, &function_name, return_value, arg ? 2 : 1, params);
1452 		intern->nApplyCount--;
1453 	} else {
1454 		if (ZEND_NUM_ARGS() != 1 || zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "z", &arg) == FAILURE) {
1455 			zend_throw_exception(spl_ce_BadMethodCallException, "Function expects exactly one argument", 0);
1456 			goto exit;
1457 		}
1458 		ZVAL_COPY_VALUE(&params[1], arg);
1459 		intern->nApplyCount++;
1460 		call_user_function(EG(function_table), NULL, &function_name, return_value, 2, params);
1461 		intern->nApplyCount--;
1462 	}
1463 
1464 exit:
1465 	{
1466 		HashTable *new_ht = Z_ARRVAL_P(Z_REFVAL(params[0]));
1467 		if (aht != new_ht) {
1468 			spl_array_replace_hash_table(intern, new_ht);
1469 		} else {
1470 			GC_DELREF(aht);
1471 		}
1472 		ZVAL_NULL(Z_REFVAL(params[0]));
1473 		zval_ptr_dtor(&params[0]);
1474 		zend_string_free(Z_STR(function_name));
1475 	}
1476 } /* }}} */
1477 
1478 #define SPL_ARRAY_METHOD(cname, fname, use_arg) \
1479 SPL_METHOD(cname, fname) \
1480 { \
1481 	spl_array_method(INTERNAL_FUNCTION_PARAM_PASSTHRU, #fname, sizeof(#fname)-1, use_arg); \
1482 }
1483 
1484 /* {{{ proto int ArrayObject::asort([int $sort_flags = SORT_REGULAR ])
1485        proto int ArrayIterator::asort([int $sort_flags = SORT_REGULAR ])
1486    Sort the entries by values. */
SPL_ARRAY_METHODnull1487 SPL_ARRAY_METHOD(Array, asort, SPL_ARRAY_METHOD_MAY_USER_ARG) /* }}} */
1488 
1489 /* {{{ proto int ArrayObject::ksort([int $sort_flags = SORT_REGULAR ])
1490        proto int ArrayIterator::ksort([int $sort_flags = SORT_REGULAR ])
1491    Sort the entries by key. */
1492 SPL_ARRAY_METHOD(Array, ksort, SPL_ARRAY_METHOD_MAY_USER_ARG) /* }}} */
1493 
1494 /* {{{ proto int ArrayObject::uasort(callback cmp_function)
1495        proto int ArrayIterator::uasort(callback cmp_function)
1496    Sort the entries by values user defined function. */
1497 SPL_ARRAY_METHOD(Array, uasort, SPL_ARRAY_METHOD_USE_ARG) /* }}} */
1498 
1499 /* {{{ proto int ArrayObject::uksort(callback cmp_function)
1500        proto int ArrayIterator::uksort(callback cmp_function)
1501    Sort the entries by key using user defined function. */
1502 SPL_ARRAY_METHOD(Array, uksort, SPL_ARRAY_METHOD_USE_ARG) /* }}} */
1503 
1504 /* {{{ proto int ArrayObject::natsort()
1505        proto int ArrayIterator::natsort()
1506    Sort the entries by values using "natural order" algorithm. */
1507 SPL_ARRAY_METHOD(Array, natsort, SPL_ARRAY_METHOD_NO_ARG) /* }}} */
1508 
1509 /* {{{ proto int ArrayObject::natcasesort()
1510        proto int ArrayIterator::natcasesort()
1511    Sort the entries by key using case insensitive "natural order" algorithm. */
1512 SPL_ARRAY_METHOD(Array, natcasesort, SPL_ARRAY_METHOD_NO_ARG) /* }}} */
1513 
1514 /* {{{ proto mixed|NULL ArrayIterator::current()
1515    Return current array entry */
1516 SPL_METHOD(Array, current)
1517 {
1518 	zval *object = ZEND_THIS;
1519 	spl_array_object *intern = Z_SPLARRAY_P(object);
1520 	zval *entry;
1521 	HashTable *aht = spl_array_get_hash_table(intern);
1522 
1523 	if (zend_parse_parameters_none() == FAILURE) {
1524 		return;
1525 	}
1526 
1527 	if ((entry = zend_hash_get_current_data_ex(aht, spl_array_get_pos_ptr(aht, intern))) == NULL) {
1528 		return;
1529 	}
1530 	if (Z_TYPE_P(entry) == IS_INDIRECT) {
1531 		entry = Z_INDIRECT_P(entry);
1532 		if (Z_TYPE_P(entry) == IS_UNDEF) {
1533 			return;
1534 		}
1535 	}
1536 	ZVAL_COPY_DEREF(return_value, entry);
1537 }
1538 /* }}} */
1539 
1540 /* {{{ proto mixed|NULL ArrayIterator::key()
1541    Return current array key */
SPL_METHODnull1542 SPL_METHOD(Array, key)
1543 {
1544 	if (zend_parse_parameters_none() == FAILURE) {
1545 		return;
1546 	}
1547 
1548 	spl_array_iterator_key(ZEND_THIS, return_value);
1549 } /* }}} */
1550 
spl_array_iterator_key(zval *object, zval *return_value)1551 void spl_array_iterator_key(zval *object, zval *return_value) /* {{{ */
1552 {
1553 	spl_array_object *intern = Z_SPLARRAY_P(object);
1554 	HashTable *aht = spl_array_get_hash_table(intern);
1555 
1556 	zend_hash_get_current_key_zval_ex(aht, return_value, spl_array_get_pos_ptr(aht, intern));
1557 }
1558 /* }}} */
1559 
1560 /* {{{ proto void ArrayIterator::next()
1561    Move to next entry */
SPL_METHODnull1562 SPL_METHOD(Array, next)
1563 {
1564 	zval *object = ZEND_THIS;
1565 	spl_array_object *intern = Z_SPLARRAY_P(object);
1566 	HashTable *aht = spl_array_get_hash_table(intern);
1567 
1568 	if (zend_parse_parameters_none() == FAILURE) {
1569 		return;
1570 	}
1571 
1572 	spl_array_next_ex(intern, aht);
1573 }
1574 /* }}} */
1575 
1576 /* {{{ proto bool ArrayIterator::valid()
1577    Check whether array contains more entries */
SPL_METHODnull1578 SPL_METHOD(Array, valid)
1579 {
1580 	zval *object = ZEND_THIS;
1581 	spl_array_object *intern = Z_SPLARRAY_P(object);
1582 	HashTable *aht = spl_array_get_hash_table(intern);
1583 
1584 	if (zend_parse_parameters_none() == FAILURE) {
1585 		return;
1586 	}
1587 
1588 	RETURN_BOOL(zend_hash_has_more_elements_ex(aht, spl_array_get_pos_ptr(aht, intern)) == SUCCESS);
1589 }
1590 /* }}} */
1591 
1592 /* {{{ proto bool RecursiveArrayIterator::hasChildren()
1593    Check whether current element has children (e.g. is an array) */
SPL_METHODnull1594 SPL_METHOD(Array, hasChildren)
1595 {
1596 	zval *object = ZEND_THIS, *entry;
1597 	spl_array_object *intern = Z_SPLARRAY_P(object);
1598 	HashTable *aht = spl_array_get_hash_table(intern);
1599 
1600 	if (zend_parse_parameters_none() == FAILURE) {
1601 		return;
1602 	}
1603 
1604 	if ((entry = zend_hash_get_current_data_ex(aht, spl_array_get_pos_ptr(aht, intern))) == NULL) {
1605 		RETURN_FALSE;
1606 	}
1607 
1608 	if (Z_TYPE_P(entry) == IS_INDIRECT) {
1609 		entry = Z_INDIRECT_P(entry);
1610 	}
1611 
1612 	ZVAL_DEREF(entry);
1613 	RETURN_BOOL(Z_TYPE_P(entry) == IS_ARRAY || (Z_TYPE_P(entry) == IS_OBJECT && (intern->ar_flags & SPL_ARRAY_CHILD_ARRAYS_ONLY) == 0));
1614 }
1615 /* }}} */
1616 
1617 /* {{{ proto object RecursiveArrayIterator::getChildren()
1618    Create a sub iterator for the current element (same class as $this) */
SPL_METHODnull1619 SPL_METHOD(Array, getChildren)
1620 {
1621 	zval *object = ZEND_THIS, *entry, flags;
1622 	spl_array_object *intern = Z_SPLARRAY_P(object);
1623 	HashTable *aht = spl_array_get_hash_table(intern);
1624 
1625 	if (zend_parse_parameters_none() == FAILURE) {
1626 		return;
1627 	}
1628 
1629 	if ((entry = zend_hash_get_current_data_ex(aht, spl_array_get_pos_ptr(aht, intern))) == NULL) {
1630 		return;
1631 	}
1632 
1633 	if (Z_TYPE_P(entry) == IS_INDIRECT) {
1634 		entry = Z_INDIRECT_P(entry);
1635 	}
1636 
1637 	ZVAL_DEREF(entry);
1638 	if (Z_TYPE_P(entry) == IS_OBJECT) {
1639 		if ((intern->ar_flags & SPL_ARRAY_CHILD_ARRAYS_ONLY) != 0) {
1640 			return;
1641 		}
1642 		if (instanceof_function(Z_OBJCE_P(entry), Z_OBJCE_P(ZEND_THIS))) {
1643 			ZVAL_OBJ(return_value, Z_OBJ_P(entry));
1644 			Z_ADDREF_P(return_value);
1645 			return;
1646 		}
1647 	}
1648 
1649 	ZVAL_LONG(&flags, intern->ar_flags);
1650 	spl_instantiate_arg_ex2(Z_OBJCE_P(ZEND_THIS), return_value, entry, &flags);
1651 }
1652 /* }}} */
1653 
1654 /* {{{ proto string ArrayObject::serialize()
1655    Serialize the object */
SPL_METHODnull1656 SPL_METHOD(Array, serialize)
1657 {
1658 	zval *object = ZEND_THIS;
1659 	spl_array_object *intern = Z_SPLARRAY_P(object);
1660 	zval members, flags;
1661 	php_serialize_data_t var_hash;
1662 	smart_str buf = {0};
1663 
1664 	if (zend_parse_parameters_none() == FAILURE) {
1665 		return;
1666 	}
1667 
1668 	PHP_VAR_SERIALIZE_INIT(var_hash);
1669 
1670 	ZVAL_LONG(&flags, (intern->ar_flags & SPL_ARRAY_CLONE_MASK));
1671 
1672 	/* storage */
1673 	smart_str_appendl(&buf, "x:", 2);
1674 	php_var_serialize(&buf, &flags, &var_hash);
1675 
1676 	if (!(intern->ar_flags & SPL_ARRAY_IS_SELF)) {
1677 		php_var_serialize(&buf, &intern->array, &var_hash);
1678 		smart_str_appendc(&buf, ';');
1679 	}
1680 
1681 	/* members */
1682 	smart_str_appendl(&buf, "m:", 2);
1683 	if (!intern->std.properties) {
1684 		rebuild_object_properties(&intern->std);
1685 	}
1686 
1687 	ZVAL_ARR(&members, intern->std.properties);
1688 
1689 	php_var_serialize(&buf, &members, &var_hash); /* finishes the string */
1690 
1691 	/* done */
1692 	PHP_VAR_SERIALIZE_DESTROY(var_hash);
1693 
1694 	if (buf.s) {
1695 		RETURN_NEW_STR(buf.s);
1696 	}
1697 
1698 	RETURN_NULL();
1699 } /* }}} */
1700 
1701 /* {{{ proto void ArrayObject::unserialize(string serialized)
1702  * unserialize the object
1703  */
SPL_METHODnull1704 SPL_METHOD(Array, unserialize)
1705 {
1706 	zval *object = ZEND_THIS;
1707 	spl_array_object *intern = Z_SPLARRAY_P(object);
1708 
1709 	char *buf;
1710 	size_t buf_len;
1711 	const unsigned char *p, *s;
1712 	php_unserialize_data_t var_hash;
1713 	zval *members, *zflags, *array;
1714 	zend_long flags;
1715 
1716 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &buf, &buf_len) == FAILURE) {
1717 		return;
1718 	}
1719 
1720 	if (buf_len == 0) {
1721 		return;
1722 	}
1723 
1724 	if (intern->nApplyCount > 0) {
1725 		zend_error(E_WARNING, "Modification of ArrayObject during sorting is prohibited");
1726 		return;
1727 	}
1728 
1729 	/* storage */
1730 	s = p = (const unsigned char*)buf;
1731 	PHP_VAR_UNSERIALIZE_INIT(var_hash);
1732 
1733 	if (*p!= 'x' || *++p != ':') {
1734 		goto outexcept;
1735 	}
1736 	++p;
1737 
1738 	zflags = var_tmp_var(&var_hash);
1739 	if (!php_var_unserialize(zflags, &p, s + buf_len, &var_hash) || Z_TYPE_P(zflags) != IS_LONG) {
1740 		goto outexcept;
1741 	}
1742 
1743 	--p; /* for ';' */
1744 	flags = Z_LVAL_P(zflags);
1745 	/* flags needs to be verified and we also need to verify whether the next
1746 	 * thing we get is ';'. After that we require an 'm' or something else
1747 	 * where 'm' stands for members and anything else should be an array. If
1748 	 * neither 'a' or 'm' follows we have an error. */
1749 
1750 	if (*p != ';') {
1751 		goto outexcept;
1752 	}
1753 	++p;
1754 
1755 	if (flags & SPL_ARRAY_IS_SELF) {
1756 		/* If IS_SELF is used, the flags are not followed by an array/object */
1757 		intern->ar_flags &= ~SPL_ARRAY_CLONE_MASK;
1758 		intern->ar_flags |= flags & SPL_ARRAY_CLONE_MASK;
1759 		zval_ptr_dtor(&intern->array);
1760 		ZVAL_UNDEF(&intern->array);
1761 	} else {
1762 		if (*p!='a' && *p!='O' && *p!='C' && *p!='r') {
1763 			goto outexcept;
1764 		}
1765 
1766 		array = var_tmp_var(&var_hash);
1767 		if (!php_var_unserialize(array, &p, s + buf_len, &var_hash)
1768 				|| (Z_TYPE_P(array) != IS_ARRAY && Z_TYPE_P(array) != IS_OBJECT)) {
1769 			goto outexcept;
1770 		}
1771 
1772 		intern->ar_flags &= ~SPL_ARRAY_CLONE_MASK;
1773 		intern->ar_flags |= flags & SPL_ARRAY_CLONE_MASK;
1774 
1775 		if (Z_TYPE_P(array) == IS_ARRAY) {
1776 			zval_ptr_dtor(&intern->array);
1777 			ZVAL_COPY_VALUE(&intern->array, array);
1778 			ZVAL_NULL(array);
1779 			SEPARATE_ARRAY(&intern->array);
1780 		} else {
1781 			spl_array_set_array(object, intern, array, 0L, 1);
1782 		}
1783 
1784 		if (*p != ';') {
1785 			goto outexcept;
1786 		}
1787         ++p;
1788 	}
1789 
1790 	/* members */
1791 	if (*p!= 'm' || *++p != ':') {
1792 		goto outexcept;
1793 	}
1794 	++p;
1795 
1796 	members = var_tmp_var(&var_hash);
1797 	if (!php_var_unserialize(members, &p, s + buf_len, &var_hash) || Z_TYPE_P(members) != IS_ARRAY) {
1798 		goto outexcept;
1799 	}
1800 
1801 	/* copy members */
1802 	object_properties_load(&intern->std, Z_ARRVAL_P(members));
1803 
1804 	/* done reading $serialized */
1805 	PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
1806 	return;
1807 
1808 outexcept:
1809 	PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
1810 	zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0, "Error at offset " ZEND_LONG_FMT " of %zd bytes", (zend_long)((char*)p - buf), buf_len);
1811 	return;
1812 
1813 } /* }}} */
1814 
1815 /* {{{ proto array ArrayObject::__serialize() */
SPL_METHODnull1816 SPL_METHOD(Array, __serialize)
1817 {
1818 	spl_array_object *intern = Z_SPLARRAY_P(ZEND_THIS);
1819 	zval tmp;
1820 
1821 	if (zend_parse_parameters_none_throw() == FAILURE) {
1822 		return;
1823 	}
1824 
1825 	array_init(return_value);
1826 
1827 	/* flags */
1828 	ZVAL_LONG(&tmp, (intern->ar_flags & SPL_ARRAY_CLONE_MASK));
1829 	zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &tmp);
1830 
1831 	/* storage */
1832 	if (intern->ar_flags & SPL_ARRAY_IS_SELF) {
1833 		ZVAL_NULL(&tmp);
1834 	} else {
1835 		ZVAL_COPY(&tmp, &intern->array);
1836 	}
1837 	zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &tmp);
1838 
1839 	/* members */
1840 	ZVAL_ARR(&tmp, zend_std_get_properties(ZEND_THIS));
1841 	Z_TRY_ADDREF(tmp);
1842 	zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &tmp);
1843 
1844 	/* iterator class */
1845 	if (intern->ce_get_iterator == spl_ce_ArrayIterator) {
1846 		ZVAL_NULL(&tmp);
1847 	} else {
1848 		ZVAL_STR_COPY(&tmp, intern->ce_get_iterator->name);
1849 	}
1850 	zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &tmp);
1851 }
1852 /* }}} */
1853 
1854 
1855 /* {{{ proto void ArrayObject::__unserialize(array data) */
SPL_METHODnull1856 SPL_METHOD(Array, __unserialize)
1857 {
1858 	spl_array_object *intern = Z_SPLARRAY_P(ZEND_THIS);
1859 	HashTable *data;
1860 	zval *flags_zv, *storage_zv, *members_zv, *iterator_class_zv;
1861 	zend_long flags;
1862 
1863 	if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "h", &data) == FAILURE) {
1864 		return;
1865 	}
1866 
1867 	flags_zv          = zend_hash_index_find(data, 0);
1868 	storage_zv        = zend_hash_index_find(data, 1);
1869 	members_zv        = zend_hash_index_find(data, 2);
1870 	iterator_class_zv = zend_hash_index_find(data, 3);
1871 
1872 	if (!flags_zv || !storage_zv || !members_zv ||
1873 			Z_TYPE_P(flags_zv) != IS_LONG || Z_TYPE_P(members_zv) != IS_ARRAY ||
1874 			(iterator_class_zv && (Z_TYPE_P(iterator_class_zv) != IS_NULL &&
1875 				Z_TYPE_P(iterator_class_zv) != IS_STRING))) {
1876 		zend_throw_exception(spl_ce_UnexpectedValueException,
1877 			"Incomplete or ill-typed serialization data", 0);
1878 		return;
1879 	}
1880 
1881 	flags = Z_LVAL_P(flags_zv);
1882 	intern->ar_flags &= ~SPL_ARRAY_CLONE_MASK;
1883 	intern->ar_flags |= flags & SPL_ARRAY_CLONE_MASK;
1884 
1885 	if (flags & SPL_ARRAY_IS_SELF) {
1886 		zval_ptr_dtor(&intern->array);
1887 		ZVAL_UNDEF(&intern->array);
1888 	} else {
1889 		spl_array_set_array(ZEND_THIS, intern, storage_zv, 0L, 1);
1890 	}
1891 
1892 	object_properties_load(&intern->std, Z_ARRVAL_P(members_zv));
1893 
1894 	if (iterator_class_zv && Z_TYPE_P(iterator_class_zv) == IS_STRING) {
1895 		zend_class_entry *ce = zend_lookup_class(Z_STR_P(iterator_class_zv));
1896 
1897 		if (!ce) {
1898 			zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
1899 				"Cannot deserialize ArrayObject with iterator class '%s'; no such class exists",
1900 				ZSTR_VAL(Z_STR_P(iterator_class_zv)));
1901 			return;
1902 		} else if (!instanceof_function(ce, spl_ce_Iterator)) {
1903 			zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
1904 				"Cannot deserialize ArrayObject with iterator class '%s'; this class does not implement the Iterator interface",
1905 				ZSTR_VAL(Z_STR_P(iterator_class_zv)));
1906 			return;
1907 		} else {
1908 			intern->ce_get_iterator = ce;
1909 		}
1910 	}
1911 }
1912 /* }}} */
1913 
1914 /* {{{ proto void Array::__debugInfo() */
SPL_METHODnull1915 SPL_METHOD(Array, __debugInfo)
1916 {
1917 	if (zend_parse_parameters_none() == FAILURE) {
1918 		return;
1919 	}
1920 
1921 	RETURN_ARR(spl_array_get_debug_info(getThis()));
1922 } /* }}} */
1923 
1924 /* {{{ arginfo and function table */
1925 ZEND_BEGIN_ARG_INFO_EX(arginfo_array___construct, 0, 0, 0)
1926 	ZEND_ARG_INFO(0, input)
1927 	ZEND_ARG_INFO(0, flags)
1928 	ZEND_ARG_INFO(0, iterator_class)
1929 ZEND_END_ARG_INFO()
1930 
1931 /* ArrayIterator::__construct and ArrayObject::__construct have different signatures */
1932 ZEND_BEGIN_ARG_INFO_EX(arginfo_array_iterator___construct, 0, 0, 0)
1933 	ZEND_ARG_INFO(0, array)
1934 	ZEND_ARG_INFO(0, flags)
1935 ZEND_END_ARG_INFO()
1936 
1937 ZEND_BEGIN_ARG_INFO_EX(arginfo_array_offsetGet, 0, 0, 1)
1938 	ZEND_ARG_INFO(0, index)
1939 ZEND_END_ARG_INFO()
1940 
1941 ZEND_BEGIN_ARG_INFO_EX(arginfo_array_offsetSet, 0, 0, 2)
1942 	ZEND_ARG_INFO(0, index)
1943 	ZEND_ARG_INFO(0, newval)
1944 ZEND_END_ARG_INFO()
1945 
1946 ZEND_BEGIN_ARG_INFO(arginfo_array_append, 0)
1947 	ZEND_ARG_INFO(0, value)
1948 ZEND_END_ARG_INFO()
1949 
1950 ZEND_BEGIN_ARG_INFO(arginfo_array_seek, 0)
1951 	ZEND_ARG_INFO(0, position)
1952 ZEND_END_ARG_INFO()
1953 
1954 ZEND_BEGIN_ARG_INFO(arginfo_array_exchangeArray, 0)
1955 	ZEND_ARG_INFO(0, input)
1956 ZEND_END_ARG_INFO()
1957 
1958 ZEND_BEGIN_ARG_INFO(arginfo_array_setFlags, 0)
1959 	ZEND_ARG_INFO(0, flags)
1960 ZEND_END_ARG_INFO()
1961 
1962 ZEND_BEGIN_ARG_INFO(arginfo_array_setIteratorClass, 0)
1963 	ZEND_ARG_INFO(0, iteratorClass)
1964 ZEND_END_ARG_INFO()
1965 
1966 ZEND_BEGIN_ARG_INFO(arginfo_array_uXsort, 0)
1967 	ZEND_ARG_INFO(0, cmp_function)
1968 ZEND_END_ARG_INFO();
1969 
1970 ZEND_BEGIN_ARG_INFO(arginfo_array_unserialize, 0)
1971 	ZEND_ARG_INFO(0, serialized)
1972 ZEND_END_ARG_INFO();
1973 
1974 ZEND_BEGIN_ARG_INFO(arginfo_array_void, 0)
1975 ZEND_END_ARG_INFO()
1976 
1977 static const zend_function_entry spl_funcs_ArrayObject[] = {
1978 	SPL_ME(Array, __construct,      arginfo_array___construct,      ZEND_ACC_PUBLIC)
1979 	SPL_ME(Array, offsetExists,     arginfo_array_offsetGet,        ZEND_ACC_PUBLIC)
1980 	SPL_ME(Array, offsetGet,        arginfo_array_offsetGet,        ZEND_ACC_PUBLIC)
1981 	SPL_ME(Array, offsetSet,        arginfo_array_offsetSet,        ZEND_ACC_PUBLIC)
1982 	SPL_ME(Array, offsetUnset,      arginfo_array_offsetGet,        ZEND_ACC_PUBLIC)
1983 	SPL_ME(Array, append,           arginfo_array_append,           ZEND_ACC_PUBLIC)
1984 	SPL_ME(Array, getArrayCopy,     arginfo_array_void,             ZEND_ACC_PUBLIC)
1985 	SPL_ME(Array, count,            arginfo_array_void,             ZEND_ACC_PUBLIC)
1986 	SPL_ME(Array, getFlags,         arginfo_array_void,             ZEND_ACC_PUBLIC)
1987 	SPL_ME(Array, setFlags,         arginfo_array_setFlags,         ZEND_ACC_PUBLIC)
1988 	SPL_ME(Array, asort,            arginfo_array_void,             ZEND_ACC_PUBLIC)
1989 	SPL_ME(Array, ksort,            arginfo_array_void,             ZEND_ACC_PUBLIC)
1990 	SPL_ME(Array, uasort,           arginfo_array_uXsort,           ZEND_ACC_PUBLIC)
1991 	SPL_ME(Array, uksort,           arginfo_array_uXsort,           ZEND_ACC_PUBLIC)
1992 	SPL_ME(Array, natsort,          arginfo_array_void,             ZEND_ACC_PUBLIC)
1993 	SPL_ME(Array, natcasesort,      arginfo_array_void,             ZEND_ACC_PUBLIC)
1994 	SPL_ME(Array, unserialize,      arginfo_array_unserialize,      ZEND_ACC_PUBLIC)
1995 	SPL_ME(Array, serialize,        arginfo_array_void,             ZEND_ACC_PUBLIC)
1996 	SPL_ME(Array, __unserialize,    arginfo_array_unserialize,      ZEND_ACC_PUBLIC)
1997 	SPL_ME(Array, __serialize,      arginfo_array_void,             ZEND_ACC_PUBLIC)
1998 	SPL_ME(Array, __debugInfo,      arginfo_array_void,             ZEND_ACC_PUBLIC)
1999 	/* ArrayObject specific */
2000 	SPL_ME(Array, getIterator,      arginfo_array_void,             ZEND_ACC_PUBLIC)
2001 	SPL_ME(Array, exchangeArray,    arginfo_array_exchangeArray,    ZEND_ACC_PUBLIC)
2002 	SPL_ME(Array, setIteratorClass, arginfo_array_setIteratorClass, ZEND_ACC_PUBLIC)
2003 	SPL_ME(Array, getIteratorClass, arginfo_array_void,             ZEND_ACC_PUBLIC)
2004 	PHP_FE_END
2005 };
2006 
2007 static const zend_function_entry spl_funcs_ArrayIterator[] = {
2008 	SPL_ME(ArrayIterator, __construct, arginfo_array_iterator___construct,      ZEND_ACC_PUBLIC)
2009 	SPL_ME(Array, offsetExists,     arginfo_array_offsetGet,        ZEND_ACC_PUBLIC)
2010 	SPL_ME(Array, offsetGet,        arginfo_array_offsetGet,        ZEND_ACC_PUBLIC)
2011 	SPL_ME(Array, offsetSet,        arginfo_array_offsetSet,        ZEND_ACC_PUBLIC)
2012 	SPL_ME(Array, offsetUnset,      arginfo_array_offsetGet,        ZEND_ACC_PUBLIC)
2013 	SPL_ME(Array, append,           arginfo_array_append,           ZEND_ACC_PUBLIC)
2014 	SPL_ME(Array, getArrayCopy,     arginfo_array_void,             ZEND_ACC_PUBLIC)
2015 	SPL_ME(Array, count,            arginfo_array_void,             ZEND_ACC_PUBLIC)
2016 	SPL_ME(Array, getFlags,         arginfo_array_void,             ZEND_ACC_PUBLIC)
2017 	SPL_ME(Array, setFlags,         arginfo_array_setFlags,         ZEND_ACC_PUBLIC)
2018 	SPL_ME(Array, asort,            arginfo_array_void,             ZEND_ACC_PUBLIC)
2019 	SPL_ME(Array, ksort,            arginfo_array_void,             ZEND_ACC_PUBLIC)
2020 	SPL_ME(Array, uasort,           arginfo_array_uXsort,           ZEND_ACC_PUBLIC)
2021 	SPL_ME(Array, uksort,           arginfo_array_uXsort,           ZEND_ACC_PUBLIC)
2022 	SPL_ME(Array, natsort,          arginfo_array_void,             ZEND_ACC_PUBLIC)
2023 	SPL_ME(Array, natcasesort,      arginfo_array_void,             ZEND_ACC_PUBLIC)
2024 	SPL_ME(Array, unserialize,      arginfo_array_unserialize,      ZEND_ACC_PUBLIC)
2025 	SPL_ME(Array, serialize,        arginfo_array_void,             ZEND_ACC_PUBLIC)
2026 	SPL_ME(Array, __unserialize,    arginfo_array_unserialize,      ZEND_ACC_PUBLIC)
2027 	SPL_ME(Array, __serialize,      arginfo_array_void,             ZEND_ACC_PUBLIC)
2028 	SPL_ME(Array, __debugInfo,      arginfo_array_void,             ZEND_ACC_PUBLIC)
2029 	/* ArrayIterator specific */
2030 	SPL_ME(Array, rewind,           arginfo_array_void,             ZEND_ACC_PUBLIC)
2031 	SPL_ME(Array, current,          arginfo_array_void,             ZEND_ACC_PUBLIC)
2032 	SPL_ME(Array, key,              arginfo_array_void,             ZEND_ACC_PUBLIC)
2033 	SPL_ME(Array, next,             arginfo_array_void,             ZEND_ACC_PUBLIC)
2034 	SPL_ME(Array, valid,            arginfo_array_void,             ZEND_ACC_PUBLIC)
2035 	SPL_ME(Array, seek,             arginfo_array_seek,             ZEND_ACC_PUBLIC)
2036 	PHP_FE_END
2037 };
2038 
2039 static const zend_function_entry spl_funcs_RecursiveArrayIterator[] = {
2040 	SPL_ME(Array, hasChildren,   arginfo_array_void, ZEND_ACC_PUBLIC)
2041 	SPL_ME(Array, getChildren,   arginfo_array_void, ZEND_ACC_PUBLIC)
2042 	PHP_FE_END
2043 };
2044 /* }}} */
2045 
2046 /* {{{ PHP_MINIT_FUNCTION(spl_array) */
PHP_MINIT_FUNCTIONnull2047 PHP_MINIT_FUNCTION(spl_array)
2048 {
2049 	REGISTER_SPL_STD_CLASS_EX(ArrayObject, spl_array_object_new, spl_funcs_ArrayObject);
2050 	REGISTER_SPL_IMPLEMENTS(ArrayObject, Aggregate);
2051 	REGISTER_SPL_IMPLEMENTS(ArrayObject, ArrayAccess);
2052 	REGISTER_SPL_IMPLEMENTS(ArrayObject, Serializable);
2053 	REGISTER_SPL_IMPLEMENTS(ArrayObject, Countable);
2054 	memcpy(&spl_handler_ArrayObject, &std_object_handlers, sizeof(zend_object_handlers));
2055 
2056 	spl_handler_ArrayObject.offset = XtOffsetOf(spl_array_object, std);
2057 
2058 	spl_handler_ArrayObject.clone_obj = spl_array_object_clone;
2059 	spl_handler_ArrayObject.read_dimension = spl_array_read_dimension;
2060 	spl_handler_ArrayObject.write_dimension = spl_array_write_dimension;
2061 	spl_handler_ArrayObject.unset_dimension = spl_array_unset_dimension;
2062 	spl_handler_ArrayObject.has_dimension = spl_array_has_dimension;
2063 	spl_handler_ArrayObject.count_elements = spl_array_object_count_elements;
2064 
2065 	spl_handler_ArrayObject.get_properties_for = spl_array_get_properties_for;
2066 	spl_handler_ArrayObject.get_gc = spl_array_get_gc;
2067 	spl_handler_ArrayObject.read_property = spl_array_read_property;
2068 	spl_handler_ArrayObject.write_property = spl_array_write_property;
2069 	spl_handler_ArrayObject.get_property_ptr_ptr = spl_array_get_property_ptr_ptr;
2070 	spl_handler_ArrayObject.has_property = spl_array_has_property;
2071 	spl_handler_ArrayObject.unset_property = spl_array_unset_property;
2072 
2073 	spl_handler_ArrayObject.compare_objects = spl_array_compare_objects;
2074 	spl_handler_ArrayObject.dtor_obj = zend_objects_destroy_object;
2075 	spl_handler_ArrayObject.free_obj = spl_array_object_free_storage;
2076 
2077 	REGISTER_SPL_STD_CLASS_EX(ArrayIterator, spl_array_object_new, spl_funcs_ArrayIterator);
2078 	REGISTER_SPL_IMPLEMENTS(ArrayIterator, Iterator);
2079 	REGISTER_SPL_IMPLEMENTS(ArrayIterator, ArrayAccess);
2080 	REGISTER_SPL_IMPLEMENTS(ArrayIterator, SeekableIterator);
2081 	REGISTER_SPL_IMPLEMENTS(ArrayIterator, Serializable);
2082 	REGISTER_SPL_IMPLEMENTS(ArrayIterator, Countable);
2083 	memcpy(&spl_handler_ArrayIterator, &spl_handler_ArrayObject, sizeof(zend_object_handlers));
2084 	spl_ce_ArrayIterator->get_iterator = spl_array_get_iterator;
2085 	spl_ce_ArrayIterator->ce_flags |= ZEND_ACC_REUSE_GET_ITERATOR;
2086 
2087 
2088 	REGISTER_SPL_CLASS_CONST_LONG(ArrayObject,   "STD_PROP_LIST",    SPL_ARRAY_STD_PROP_LIST);
2089 	REGISTER_SPL_CLASS_CONST_LONG(ArrayObject,   "ARRAY_AS_PROPS",   SPL_ARRAY_ARRAY_AS_PROPS);
2090 
2091 	REGISTER_SPL_CLASS_CONST_LONG(ArrayIterator, "STD_PROP_LIST",    SPL_ARRAY_STD_PROP_LIST);
2092 	REGISTER_SPL_CLASS_CONST_LONG(ArrayIterator, "ARRAY_AS_PROPS",   SPL_ARRAY_ARRAY_AS_PROPS);
2093 
2094 	REGISTER_SPL_SUB_CLASS_EX(RecursiveArrayIterator, ArrayIterator, spl_array_object_new, spl_funcs_RecursiveArrayIterator);
2095 	REGISTER_SPL_IMPLEMENTS(RecursiveArrayIterator, RecursiveIterator);
2096 	spl_ce_RecursiveArrayIterator->get_iterator = spl_array_get_iterator;
2097 	spl_ce_RecursiveArrayIterator->ce_flags |= ZEND_ACC_REUSE_GET_ITERATOR;
2098 
2099 	REGISTER_SPL_CLASS_CONST_LONG(RecursiveArrayIterator, "CHILD_ARRAYS_ONLY", SPL_ARRAY_CHILD_ARRAYS_ONLY);
2100 
2101 	return SUCCESS;
2102 }
2103 /* }}} */
2104