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