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