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