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