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