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