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