1/*
2   +----------------------------------------------------------------------+
3   | PHP Version 5                                                        |
4   +----------------------------------------------------------------------+
5   | Copyright (c) 1997-2014 The PHP Group                                |
6   +----------------------------------------------------------------------+
7   | This source file is subject to version 3.01 of the PHP license,      |
8   | that is bundled with this package in the file LICENSE, and is        |
9   | available through the world-wide-web at the following url:           |
10   | http://www.php.net/license/3_01.txt                                  |
11   | If you did not receive a copy of the PHP license and are unable to   |
12   | obtain it through the world-wide-web, please send a note to          |
13   | license@php.net so we can mail you a copy immediately.               |
14   +----------------------------------------------------------------------+
15   | Authors: Marcus Boerger <helly@php.net>                              |
16   +----------------------------------------------------------------------+
17 */
18
19/* $Id$ */
20
21#ifdef HAVE_CONFIG_H
22# include "config.h"
23#endif
24
25#include "php.h"
26#include "php_ini.h"
27#include "ext/standard/info.h"
28#include "ext/standard/php_var.h"
29#include "ext/standard/php_smart_str.h"
30#include "zend_interfaces.h"
31#include "zend_exceptions.h"
32
33#include "php_spl.h"
34#include "spl_functions.h"
35#include "spl_engine.h"
36#include "spl_iterators.h"
37#include "spl_array.h"
38#include "spl_exceptions.h"
39
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 TSRMLS_DC) { /* {{{ */
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 TSRMLS_CC);
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 TSRMLS_DC);
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 TSRMLS_DC) /* {{{ */
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 TSRMLS_CC);
147    return FAILURE;
148
149} /* }}} */
150
151SPL_API int spl_hash_verify_pos(spl_array_object * intern TSRMLS_DC) /* {{{ */
152{
153    HashTable *ht = spl_array_get_hash_table(intern, 0 TSRMLS_CC);
154    return spl_hash_verify_pos_ex(intern, ht TSRMLS_CC);
155}
156/* }}} */
157
158/* {{{ spl_array_object_free_storage */
159static void spl_array_object_free_storage(zend_object *object TSRMLS_DC)
160{
161    spl_array_object *intern = spl_array_from_obj(object);
162
163    zend_object_std_dtor(&intern->std TSRMLS_CC);
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 TSRMLS_DC);
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 TSRMLS_DC)
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 TSRMLS_CC);
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 TSRMLS_CC, 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 TSRMLS_CC);
274    return &intern->std;
275}
276/* }}} */
277
278/* {{{ spl_array_object_new */
279static zend_object *spl_array_object_new(zend_class_entry *class_type TSRMLS_DC)
280{
281    return spl_array_object_new_ex(class_type, NULL, 0 TSRMLS_CC);
282}
283/* }}} */
284
285/* {{{ spl_array_object_clone */
286static zend_object *spl_array_object_clone(zval *zobject TSRMLS_DC)
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 TSRMLS_CC);
293
294    zend_objects_clone_members(new_object, old_object TSRMLS_CC);
295
296    return new_object;
297}
298/* }}} */
299
300static zval *spl_array_get_dimension_ptr(int check_inherited, zval *object, zval *offset, int type TSRMLS_DC) /* {{{ */
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 TSRMLS_CC);
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 TSRMLS_DC) /* {{{ */
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 TSRMLS_CC);
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 TSRMLS_DC) /* {{{ */
446{
447    return spl_array_read_dimension_ex(1, object, offset, type, rv TSRMLS_CC);
448} /* }}} */
449
450static void spl_array_write_dimension_ex(int check_inherited, zval *object, zval *offset, zval *value TSRMLS_DC) /* {{{ */
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 TSRMLS_CC);
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 TSRMLS_CC);
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 TSRMLS_CC);
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 TSRMLS_CC);
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 TSRMLS_DC) /* {{{ */
532{
533    spl_array_write_dimension_ex(1, object, offset, value TSRMLS_CC);
534} /* }}} */
535
536static void spl_array_unset_dimension_ex(int check_inherited, zval *object, zval *offset TSRMLS_DC) /* {{{ */
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 TSRMLS_CC);
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) TSRMLS_CC)) {
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 TSRMLS_CC);
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 TSRMLS_CC);
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 TSRMLS_CC); /* call rewind on FAILURE */
616} /* }}} */
617
618static void spl_array_unset_dimension(zval *object, zval *offset TSRMLS_DC) /* {{{ */
619{
620    spl_array_unset_dimension_ex(1, object, offset TSRMLS_CC);
621} /* }}} */
622
623static int spl_array_has_dimension_ex(int check_inherited, zval *object, zval *offset, int check_empty TSRMLS_DC) /* {{{ */
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 TSRMLS_CC)) {
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 TSRMLS_CC);
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 TSRMLS_CC);
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 TSRMLS_CC);
697        } else {
698            value = tmp;
699        }
700    }
701
702    return check_empty ? zend_is_true(value TSRMLS_CC) : Z_TYPE_P(value) != IS_NULL;
703} /* }}} */
704
705static int spl_array_has_dimension(zval *object, zval *offset, int check_empty TSRMLS_DC) /* {{{ */
706{
707    return spl_array_has_dimension_ex(1, object, offset, check_empty TSRMLS_CC);
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 TSRMLS_DC)
712{
713    if (!ht) {
714        php_error_docref(NULL TSRMLS_CC, 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 TSRMLS_CC) == FAILURE) {
719        php_error_docref(NULL TSRMLS_CC, 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 TSRMLS_DC)
728{
729    return spl_array_object_verify_pos_ex(object, ht, "" TSRMLS_CC);
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() TSRMLS_CC, "z", &index) == FAILURE) {
739        return;
740    }
741    RETURN_BOOL(spl_array_has_dimension_ex(0, getThis(), index, 2 TSRMLS_CC));
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() TSRMLS_CC, "z", &index) == FAILURE) {
751        return;
752    }
753    value = spl_array_read_dimension_ex(0, getThis(), index, BP_VAR_R, return_value TSRMLS_CC);
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() TSRMLS_CC, "zz", &index, &value) == FAILURE) {
766        return;
767    }
768    spl_array_write_dimension_ex(0, getThis(), index, value TSRMLS_CC);
769} /* }}} */
770
771void spl_array_iterator_append(zval *object, zval *append_value TSRMLS_DC) /* {{{ */
772{
773    spl_array_object *intern = Z_SPLARRAY_P(object);
774    HashTable *aht = spl_array_get_hash_table(intern, 0 TSRMLS_CC);
775
776    if (!aht) {
777        php_error_docref(NULL TSRMLS_CC, 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 TSRMLS_CC, 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 TSRMLS_CC);
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() TSRMLS_CC, "z", &value) == FAILURE) {
802        return;
803    }
804    spl_array_iterator_append(getThis(), value TSRMLS_CC);
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() TSRMLS_CC, "z", &index) == FAILURE) {
814        return;
815    }
816    spl_array_unset_dimension_ex(0, getThis(), index TSRMLS_CC);
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 TSRMLS_CC));
829} /* }}} */
830
831static HashTable *spl_array_get_properties(zval *object TSRMLS_DC) /* {{{ */
832{
833    spl_array_object *intern = Z_SPLARRAY_P(object);
834    HashTable *result;
835
836    if (intern->nApplyCount > 1) {
837        php_error_docref(NULL TSRMLS_CC, E_ERROR, "Nesting level too deep - recursive dependency?");
838    }
839
840    intern->nApplyCount++;
841    result = spl_array_get_hash_table(intern, 1 TSRMLS_CC);
842    intern->nApplyCount--;
843    return result;
844} /* }}} */
845
846static HashTable* spl_array_get_debug_info(zval *obj, int *is_temp TSRMLS_DC) /* {{{ */
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            zval_add_ref(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 TSRMLS_CC);
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 TSRMLS_DC) /* {{{ */
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 TSRMLS_CC)) {
891        return spl_array_read_dimension(object, member, type, rv TSRMLS_CC);
892    }
893    return std_object_handlers.read_property(object, member, type, cache_slot, rv TSRMLS_CC);
894} /* }}} */
895
896static void spl_array_write_property(zval *object, zval *member, zval *value, void **cache_slot TSRMLS_DC) /* {{{ */
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 TSRMLS_CC)) {
902        spl_array_write_dimension(object, member, value TSRMLS_CC);
903        return;
904    }
905    std_object_handlers.write_property(object, member, value, cache_slot TSRMLS_CC);
906} /* }}} */
907
908static zval *spl_array_get_property_ptr_ptr(zval *object, zval *member, int type, void **cache_slot TSRMLS_DC) /* {{{ */
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 TSRMLS_CC)) {
914        return spl_array_get_dimension_ptr(1, object, member, type TSRMLS_CC);
915    }
916    //!!! FIXME
917    //return std_object_handlers.get_property_ptr_ptr(object, member, type, key TSRMLS_CC);
918    return NULL;
919} /* }}} */
920
921static int spl_array_has_property(zval *object, zval *member, int has_set_exists, void **cache_slot TSRMLS_DC) /* {{{ */
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 TSRMLS_CC)) {
927        return spl_array_has_dimension(object, member, has_set_exists TSRMLS_CC);
928    }
929    return std_object_handlers.has_property(object, member, has_set_exists, cache_slot TSRMLS_CC);
930} /* }}} */
931
932static void spl_array_unset_property(zval *object, zval *member, void **cache_slot TSRMLS_DC) /* {{{ */
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 TSRMLS_CC)) {
938        spl_array_unset_dimension(object, member TSRMLS_CC);
939        spl_array_rewind(intern TSRMLS_CC); /* because deletion might invalidate position */
940        return;
941    }
942    std_object_handlers.unset_property(object, member, cache_slot TSRMLS_CC);
943} /* }}} */
944
945static int spl_array_compare_objects(zval *o1, zval *o2 TSRMLS_DC) /* {{{ */
946{
947    HashTable           *ht1,
948                        *ht2;
949    spl_array_object    *intern1,
950                        *intern2;
951    int                 result  = 0;
952    zval                temp_zv;
953
954    intern1 = Z_SPLARRAY_P(o1);
955    intern2 = Z_SPLARRAY_P(o2);
956    ht1     = spl_array_get_hash_table(intern1, 0 TSRMLS_CC);
957    ht2     = spl_array_get_hash_table(intern2, 0 TSRMLS_CC);
958
959    zend_compare_symbol_tables(&temp_zv, ht1, ht2 TSRMLS_CC);
960    result = (int)Z_LVAL(temp_zv);
961    /* if we just compared std.properties, don't do it again */
962    if (result == 0 &&
963            !(ht1 == intern1->std.properties && ht2 == intern2->std.properties)) {
964        result = std_object_handlers.compare_objects(o1, o2 TSRMLS_CC);
965    }
966    return result;
967} /* }}} */
968
969static int spl_array_skip_protected(spl_array_object *intern, HashTable *aht TSRMLS_DC) /* {{{ */
970{
971    zend_string *string_key;
972    zend_ulong num_key;
973    zval *data;
974
975    if (Z_TYPE(intern->array) == IS_OBJECT) {
976        do {
977            if (zend_hash_get_current_key_ex(aht, &string_key, &num_key, 0, &intern->pos) == HASH_KEY_IS_STRING) {
978                data = zend_hash_get_current_data_ex(aht, &intern->pos);
979                if (data && Z_TYPE_P(data) == IS_INDIRECT &&
980                    Z_TYPE_P(data = Z_INDIRECT_P(data)) == IS_UNDEF) {
981                    /* skip */
982                } else if (!string_key->len || string_key->val[0]) {
983                    return SUCCESS;
984                }
985            } else {
986                return SUCCESS;
987            }
988            if (zend_hash_has_more_elements_ex(aht, &intern->pos) != SUCCESS) {
989                return FAILURE;
990            }
991            zend_hash_move_forward_ex(aht, &intern->pos);
992            spl_array_update_pos(aht, intern);
993        } while (1);
994    }
995    return FAILURE;
996} /* }}} */
997
998static int spl_array_next_no_verify(spl_array_object *intern, HashTable *aht TSRMLS_DC) /* {{{ */
999{
1000    zend_hash_move_forward_ex(aht, &intern->pos);
1001    spl_array_update_pos(aht, intern);
1002    if (Z_TYPE(intern->array) == IS_OBJECT) {
1003        return spl_array_skip_protected(intern, aht TSRMLS_CC);
1004    } else {
1005        return zend_hash_has_more_elements_ex(aht, &intern->pos);
1006    }
1007} /* }}} */
1008
1009static int spl_array_next_ex(spl_array_object *intern, HashTable *aht TSRMLS_DC) /* {{{ */
1010{
1011    if ((intern->ar_flags & SPL_ARRAY_IS_REF) && spl_hash_verify_pos_ex(intern, aht TSRMLS_CC) == FAILURE) {
1012        php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Array was modified outside object and internal position is no longer valid");
1013        return FAILURE;
1014    }
1015
1016    return spl_array_next_no_verify(intern, aht TSRMLS_CC);
1017} /* }}} */
1018
1019static int spl_array_next(spl_array_object *intern TSRMLS_DC) /* {{{ */
1020{
1021    HashTable *aht = spl_array_get_hash_table(intern, 0 TSRMLS_CC);
1022
1023    return spl_array_next_ex(intern, aht TSRMLS_CC);
1024
1025} /* }}} */
1026
1027static void spl_array_it_dtor(zend_object_iterator *iter TSRMLS_DC) /* {{{ */
1028{
1029    zend_user_it_invalidate_current(iter TSRMLS_CC);
1030    zval_ptr_dtor(&iter->data);
1031}
1032/* }}} */
1033
1034static int spl_array_it_valid(zend_object_iterator *iter TSRMLS_DC) /* {{{ */
1035{
1036    spl_array_object *object = Z_SPLARRAY_P(&iter->data);
1037    HashTable *aht = spl_array_get_hash_table(object, 0 TSRMLS_CC);
1038
1039    if (object->ar_flags & SPL_ARRAY_OVERLOADED_VALID) {
1040        return zend_user_it_valid(iter TSRMLS_CC);
1041    } else {
1042        if (spl_array_object_verify_pos_ex(object, aht, "ArrayIterator::valid(): " TSRMLS_CC) == FAILURE) {
1043            return FAILURE;
1044        }
1045
1046        return zend_hash_has_more_elements_ex(aht, &object->pos);
1047    }
1048}
1049/* }}} */
1050
1051static zval *spl_array_it_get_current_data(zend_object_iterator *iter TSRMLS_DC) /* {{{ */
1052{
1053    spl_array_object *object = Z_SPLARRAY_P(&iter->data);
1054    HashTable *aht = spl_array_get_hash_table(object, 0 TSRMLS_CC);
1055
1056    if (object->ar_flags & SPL_ARRAY_OVERLOADED_CURRENT) {
1057        return zend_user_it_get_current_data(iter TSRMLS_CC);
1058    } else {
1059        zval *data = zend_hash_get_current_data_ex(aht, &object->pos);
1060        if (Z_TYPE_P(data) == IS_INDIRECT) {
1061            data = Z_INDIRECT_P(data);
1062        }
1063        return data;
1064    }
1065}
1066/* }}} */
1067
1068static void spl_array_it_get_current_key(zend_object_iterator *iter, zval *key TSRMLS_DC) /* {{{ */
1069{
1070    spl_array_object *object = Z_SPLARRAY_P(&iter->data);
1071    HashTable *aht = spl_array_get_hash_table(object, 0 TSRMLS_CC);
1072
1073    if (object->ar_flags & SPL_ARRAY_OVERLOADED_KEY) {
1074        zend_user_it_get_current_key(iter, key TSRMLS_CC);
1075    } else {
1076        if (spl_array_object_verify_pos_ex(object, aht, "ArrayIterator::current(): " TSRMLS_CC) == FAILURE) {
1077            ZVAL_NULL(key);
1078        } else {
1079            zend_hash_get_current_key_zval_ex(aht, key, &object->pos);
1080        }
1081    }
1082}
1083/* }}} */
1084
1085static void spl_array_it_move_forward(zend_object_iterator *iter TSRMLS_DC) /* {{{ */
1086{
1087    spl_array_object *object = Z_SPLARRAY_P(&iter->data);
1088    HashTable *aht = spl_array_get_hash_table(object, 0 TSRMLS_CC);
1089
1090    if (object->ar_flags & SPL_ARRAY_OVERLOADED_NEXT) {
1091        zend_user_it_move_forward(iter TSRMLS_CC);
1092    } else {
1093        zend_user_it_invalidate_current(iter TSRMLS_CC);
1094        if (!aht) {
1095            php_error_docref(NULL TSRMLS_CC, E_NOTICE, "ArrayIterator::current(): Array was modified outside object and is no longer an array");
1096            return;
1097        }
1098
1099        if ((object->ar_flags & SPL_ARRAY_IS_REF) && spl_hash_verify_pos_ex(object, aht TSRMLS_CC) == FAILURE) {
1100            php_error_docref(NULL TSRMLS_CC, E_NOTICE, "ArrayIterator::next(): Array was modified outside object and internal position is no longer valid");
1101        } else {
1102            spl_array_next_no_verify(object, aht TSRMLS_CC);
1103        }
1104    }
1105}
1106/* }}} */
1107
1108static void spl_array_rewind_ex(spl_array_object *intern, HashTable *aht TSRMLS_DC) /* {{{ */
1109{
1110
1111    zend_hash_internal_pointer_reset_ex(aht, &intern->pos);
1112    spl_array_update_pos(aht, intern);
1113    spl_array_skip_protected(intern, aht TSRMLS_CC);
1114
1115} /* }}} */
1116
1117static void spl_array_rewind(spl_array_object *intern TSRMLS_DC) /* {{{ */
1118{
1119    HashTable *aht = spl_array_get_hash_table(intern, 0 TSRMLS_CC);
1120
1121    if (!aht) {
1122        php_error_docref(NULL TSRMLS_CC, E_NOTICE, "ArrayIterator::rewind(): Array was modified outside object and is no longer an array");
1123        return;
1124    }
1125
1126    spl_array_rewind_ex(intern, aht TSRMLS_CC);
1127}
1128/* }}} */
1129
1130static void spl_array_it_rewind(zend_object_iterator *iter TSRMLS_DC) /* {{{ */
1131{
1132    spl_array_object *object = Z_SPLARRAY_P(&iter->data);
1133
1134    if (object->ar_flags & SPL_ARRAY_OVERLOADED_REWIND) {
1135        zend_user_it_rewind(iter TSRMLS_CC);
1136    } else {
1137        zend_user_it_invalidate_current(iter TSRMLS_CC);
1138        spl_array_rewind(object TSRMLS_CC);
1139    }
1140}
1141/* }}} */
1142
1143/* {{{ spl_array_set_array */
1144static void spl_array_set_array(zval *object, spl_array_object *intern, zval *array, zend_long ar_flags, int just_array TSRMLS_DC) {
1145
1146    if (Z_TYPE_P(array) == IS_ARRAY) {
1147        SEPARATE_ZVAL_IF_NOT_REF(array);
1148    }
1149
1150    if (Z_TYPE_P(array) == IS_OBJECT && (Z_OBJ_HT_P(array) == &spl_handler_ArrayObject || Z_OBJ_HT_P(array) == &spl_handler_ArrayIterator)) {
1151        zval_ptr_dtor(&intern->array);
1152        if (just_array) {
1153            spl_array_object *other = Z_SPLARRAY_P(array);
1154            ar_flags = other->ar_flags & ~SPL_ARRAY_INT_MASK;
1155        }
1156        ar_flags |= SPL_ARRAY_USE_OTHER;
1157        ZVAL_COPY_VALUE(&intern->array, array);
1158    } else {
1159        if (Z_TYPE_P(array) != IS_OBJECT && Z_TYPE_P(array) != IS_ARRAY) {
1160            zend_throw_exception(spl_ce_InvalidArgumentException, "Passed variable is not an array or object, using empty array instead", 0 TSRMLS_CC);
1161            return;
1162        }
1163        zval_ptr_dtor(&intern->array);
1164        ZVAL_COPY_VALUE(&intern->array, array);
1165    }
1166    if (Z_TYPE_P(array) == IS_OBJECT && Z_OBJ_P(object) == Z_OBJ_P(array)) {
1167        intern->ar_flags |= SPL_ARRAY_IS_SELF;
1168        intern->ar_flags &= ~SPL_ARRAY_USE_OTHER;
1169    } else {
1170        intern->ar_flags &= ~SPL_ARRAY_IS_SELF;
1171    }
1172    intern->ar_flags |= ar_flags;
1173    Z_ADDREF_P(&intern->array);
1174    if (Z_TYPE_P(array) == IS_OBJECT) {
1175        zend_object_get_properties_t handler = Z_OBJ_HANDLER_P(array, get_properties);
1176        if ((handler != std_object_handlers.get_properties && handler != spl_array_get_properties)
1177        || !spl_array_get_hash_table(intern, 0 TSRMLS_CC)) {
1178            zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0 TSRMLS_CC, "Overloaded object of type %s is not compatible with %s", Z_OBJCE_P(array)->name, intern->std.ce->name);
1179        }
1180    }
1181
1182    spl_array_rewind(intern TSRMLS_CC);
1183}
1184/* }}} */
1185
1186/* iterator handler table */
1187zend_object_iterator_funcs spl_array_it_funcs = {
1188    spl_array_it_dtor,
1189    spl_array_it_valid,
1190    spl_array_it_get_current_data,
1191    spl_array_it_get_current_key,
1192    spl_array_it_move_forward,
1193    spl_array_it_rewind
1194};
1195
1196zend_object_iterator *spl_array_get_iterator(zend_class_entry *ce, zval *object, int by_ref TSRMLS_DC) /* {{{ */
1197{
1198    zend_user_iterator *iterator;
1199    spl_array_object *array_object = Z_SPLARRAY_P(object);
1200
1201    if (by_ref && (array_object->ar_flags & SPL_ARRAY_OVERLOADED_CURRENT)) {
1202        zend_error(E_ERROR, "An iterator cannot be used with foreach by reference");
1203    }
1204
1205    iterator = emalloc(sizeof(zend_user_iterator));
1206
1207    zend_iterator_init(&iterator->it TSRMLS_CC);
1208
1209    ZVAL_COPY(&iterator->it.data, object);
1210    iterator->it.funcs = &spl_array_it_funcs;
1211    iterator->ce = ce;
1212    ZVAL_UNDEF(&iterator->value);
1213
1214    return &iterator->it;
1215}
1216/* }}} */
1217
1218/* {{{ proto void ArrayObject::__construct(array|object ar = array() [, int flags = 0 [, string iterator_class = "ArrayIterator"]])
1219       proto void ArrayIterator::__construct(array|object ar = array() [, int flags = 0])
1220   Constructs a new array iterator from a path. */
1221SPL_METHOD(Array, __construct)
1222{
1223    zval *object = getThis();
1224    spl_array_object *intern;
1225    zval *array;
1226    zend_long ar_flags = 0;
1227    zend_class_entry *ce_get_iterator = spl_ce_Iterator;
1228    zend_error_handling error_handling;
1229
1230    if (ZEND_NUM_ARGS() == 0) {
1231        return; /* nothing to do */
1232    }
1233
1234    zend_replace_error_handling(EH_THROW, spl_ce_InvalidArgumentException, &error_handling TSRMLS_CC);
1235
1236    intern = Z_SPLARRAY_P(object);
1237
1238    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|lC", &array, &ar_flags, &ce_get_iterator) == FAILURE) {
1239        zend_restore_error_handling(&error_handling TSRMLS_CC);
1240        return;
1241    }
1242
1243    if (ZEND_NUM_ARGS() > 2) {
1244        intern->ce_get_iterator = ce_get_iterator;
1245    }
1246
1247    ar_flags &= ~SPL_ARRAY_INT_MASK;
1248
1249    spl_array_set_array(object, intern, array, ar_flags, ZEND_NUM_ARGS() == 1 TSRMLS_CC);
1250
1251    zend_restore_error_handling(&error_handling TSRMLS_CC);
1252
1253}
1254 /* }}} */
1255
1256/* {{{ proto void ArrayObject::setIteratorClass(string iterator_class)
1257   Set the class used in getIterator. */
1258SPL_METHOD(Array, setIteratorClass)
1259{
1260    zval *object = getThis();
1261    spl_array_object *intern = Z_SPLARRAY_P(object);
1262    zend_class_entry * ce_get_iterator = spl_ce_Iterator;
1263
1264#ifndef FAST_ZPP
1265    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "C", &ce_get_iterator) == FAILURE) {
1266        return;
1267    }
1268#else
1269    ZEND_PARSE_PARAMETERS_START(1, 1)
1270        Z_PARAM_CLASS(ce_get_iterator)
1271    ZEND_PARSE_PARAMETERS_END();
1272#endif
1273
1274    intern->ce_get_iterator = ce_get_iterator;
1275}
1276/* }}} */
1277
1278/* {{{ proto string ArrayObject::getIteratorClass()
1279   Get the class used in getIterator. */
1280SPL_METHOD(Array, getIteratorClass)
1281{
1282    zval *object = getThis();
1283    spl_array_object *intern = Z_SPLARRAY_P(object);
1284
1285    if (zend_parse_parameters_none() == FAILURE) {
1286        return;
1287    }
1288
1289    zend_string_addref(intern->ce_get_iterator->name);
1290    RETURN_STR(intern->ce_get_iterator->name);
1291}
1292/* }}} */
1293
1294/* {{{ proto int ArrayObject::getFlags()
1295   Get flags */
1296SPL_METHOD(Array, getFlags)
1297{
1298    zval *object = getThis();
1299    spl_array_object *intern = Z_SPLARRAY_P(object);
1300
1301    if (zend_parse_parameters_none() == FAILURE) {
1302        return;
1303    }
1304
1305    RETURN_LONG(intern->ar_flags & ~SPL_ARRAY_INT_MASK);
1306}
1307/* }}} */
1308
1309/* {{{ proto void ArrayObject::setFlags(int flags)
1310   Set flags */
1311SPL_METHOD(Array, setFlags)
1312{
1313    zval *object = getThis();
1314    spl_array_object *intern = Z_SPLARRAY_P(object);
1315    zend_long ar_flags = 0;
1316
1317    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &ar_flags) == FAILURE) {
1318        return;
1319    }
1320
1321    intern->ar_flags = (intern->ar_flags & SPL_ARRAY_INT_MASK) | (ar_flags & ~SPL_ARRAY_INT_MASK);
1322}
1323/* }}} */
1324
1325/* {{{ proto Array|Object ArrayObject::exchangeArray(Array|Object ar = array())
1326   Replace the referenced array or object with a new one and return the old one (right now copy - to be changed) */
1327SPL_METHOD(Array, exchangeArray)
1328{
1329    zval *object = getThis(), *array;
1330    spl_array_object *intern = Z_SPLARRAY_P(object);
1331
1332    ZVAL_NEW_ARR(return_value);
1333    zend_array_dup(Z_ARRVAL_P(return_value), spl_array_get_hash_table(intern, 0 TSRMLS_CC));
1334    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &array) == FAILURE) {
1335        return;
1336    }
1337
1338    spl_array_set_array(object, intern, array, 0L, 1 TSRMLS_CC);
1339}
1340/* }}} */
1341
1342/* {{{ proto ArrayIterator ArrayObject::getIterator()
1343   Create a new iterator from a ArrayObject instance */
1344SPL_METHOD(Array, getIterator)
1345{
1346    zval *object = getThis();
1347    spl_array_object *intern = Z_SPLARRAY_P(object);
1348    HashTable *aht = spl_array_get_hash_table(intern, 0 TSRMLS_CC);
1349
1350    if (zend_parse_parameters_none() == FAILURE) {
1351        return;
1352    }
1353
1354    if (!aht) {
1355        php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Array was modified outside object and is no longer an array");
1356        return;
1357    }
1358
1359    ZVAL_OBJ(return_value, spl_array_object_new_ex(intern->ce_get_iterator, object, 0 TSRMLS_CC));
1360    Z_SET_REFCOUNT_P(return_value, 1);
1361    //!!!PZ_SET_ISREF_P(return_value);
1362}
1363/* }}} */
1364
1365/* {{{ proto void ArrayIterator::rewind()
1366   Rewind array back to the start */
1367SPL_METHOD(Array, rewind)
1368{
1369    zval *object = getThis();
1370    spl_array_object *intern = Z_SPLARRAY_P(object);
1371
1372    if (zend_parse_parameters_none() == FAILURE) {
1373        return;
1374    }
1375
1376    spl_array_rewind(intern TSRMLS_CC);
1377}
1378/* }}} */
1379
1380/* {{{ proto void ArrayIterator::seek(int $position)
1381   Seek to position. */
1382SPL_METHOD(Array, seek)
1383{
1384    zend_long opos, position;
1385    zval *object = getThis();
1386    spl_array_object *intern = Z_SPLARRAY_P(object);
1387    HashTable *aht = spl_array_get_hash_table(intern, 0 TSRMLS_CC);
1388    int result;
1389
1390    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &position) == FAILURE) {
1391        return;
1392    }
1393
1394    if (!aht) {
1395        php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Array was modified outside object and is no longer an array");
1396        return;
1397    }
1398
1399    opos = position;
1400
1401    if (position >= 0) { /* negative values are not supported */
1402        spl_array_rewind(intern TSRMLS_CC);
1403        result = SUCCESS;
1404
1405        while (position-- > 0 && (result = spl_array_next(intern TSRMLS_CC)) == SUCCESS);
1406
1407        if (result == SUCCESS && zend_hash_has_more_elements_ex(aht, &intern->pos) == SUCCESS) {
1408            return; /* ok */
1409        }
1410    }
1411    zend_throw_exception_ex(spl_ce_OutOfBoundsException, 0 TSRMLS_CC, "Seek position %pd is out of range", opos);
1412} /* }}} */
1413
1414int static spl_array_object_count_elements_helper(spl_array_object *intern, zend_long *count TSRMLS_DC) /* {{{ */
1415{
1416    HashTable *aht = spl_array_get_hash_table(intern, 0 TSRMLS_CC);
1417    HashPosition pos;
1418
1419    if (!aht) {
1420        php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Array was modified outside object and is no longer an array");
1421        *count = 0;
1422        return FAILURE;
1423    }
1424
1425    if (Z_TYPE(intern->array) == IS_OBJECT) {
1426        /* We need to store the 'pos' since we'll modify it in the functions
1427         * we're going to call and which do not support 'pos' as parameter. */
1428        pos = intern->pos;
1429        *count = 0;
1430        spl_array_rewind(intern TSRMLS_CC);
1431        while(intern->pos != INVALID_IDX && spl_array_next(intern TSRMLS_CC) == SUCCESS) {
1432            (*count)++;
1433        }
1434        spl_array_set_pos(intern, aht, pos);
1435        return SUCCESS;
1436    } else {
1437        *count = zend_hash_num_elements(aht);
1438        return SUCCESS;
1439    }
1440} /* }}} */
1441
1442int spl_array_object_count_elements(zval *object, zend_long *count TSRMLS_DC) /* {{{ */
1443{
1444    spl_array_object *intern = Z_SPLARRAY_P(object);
1445
1446    if (intern->fptr_count) {
1447        zval rv;
1448        zend_call_method_with_0_params(object, intern->std.ce, &intern->fptr_count, "count", &rv);
1449        if (Z_TYPE(rv) != IS_UNDEF) {
1450            zval_ptr_dtor(&intern->retval);
1451            ZVAL_ZVAL(&intern->retval, &rv, 0, 0);
1452            convert_to_long(&intern->retval);
1453            *count = (zend_long)Z_LVAL(intern->retval);
1454            return SUCCESS;
1455        }
1456        *count = 0;
1457        return FAILURE;
1458    }
1459    return spl_array_object_count_elements_helper(intern, count TSRMLS_CC);
1460} /* }}} */
1461
1462/* {{{ proto int ArrayObject::count()
1463       proto int ArrayIterator::count()
1464   Return the number of elements in the Iterator. */
1465SPL_METHOD(Array, count)
1466{
1467    zend_long count;
1468    spl_array_object *intern = Z_SPLARRAY_P(getThis());
1469
1470    if (zend_parse_parameters_none() == FAILURE) {
1471        return;
1472    }
1473
1474    spl_array_object_count_elements_helper(intern, &count TSRMLS_CC);
1475
1476    RETURN_LONG(count);
1477} /* }}} */
1478
1479static void spl_array_method(INTERNAL_FUNCTION_PARAMETERS, char *fname, int fname_len, int use_arg) /* {{{ */
1480{
1481    spl_array_object *intern = Z_SPLARRAY_P(getThis());
1482    HashTable *aht = spl_array_get_hash_table(intern, 0 TSRMLS_CC);
1483    zval tmp, *arg = NULL;
1484    zval retval;
1485
1486    /* A tricky way to pass "aht" by reference, copy HashTable */
1487    //??? It may be not safe, if user comparison handler accesses "aht"
1488    ZVAL_NEW_ARR(&tmp);
1489    *Z_ARRVAL(tmp) = *aht;
1490
1491    if (!use_arg) {
1492        aht->u.v.nApplyCount++;
1493        zend_call_method(NULL, NULL, NULL, fname, fname_len, &retval, 1, &tmp, NULL TSRMLS_CC);
1494        aht->u.v.nApplyCount--;
1495    } else if (use_arg == SPL_ARRAY_METHOD_MAY_USER_ARG) {
1496        if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS() TSRMLS_CC, "|z", &arg) == FAILURE) {
1497            zval_ptr_dtor(&tmp);
1498            zend_throw_exception(spl_ce_BadMethodCallException, "Function expects one argument at most", 0 TSRMLS_CC);
1499            return;
1500        }
1501        aht->u.v.nApplyCount++;
1502        zend_call_method(NULL, NULL, NULL, fname, fname_len, &retval, arg? 2 : 1, &tmp, arg TSRMLS_CC);
1503        aht->u.v.nApplyCount--;
1504    } else {
1505        if (ZEND_NUM_ARGS() != 1 || zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS() TSRMLS_CC, "z", &arg) == FAILURE) {
1506            zval_ptr_dtor(&tmp);
1507            zend_throw_exception(spl_ce_BadMethodCallException, "Function expects exactly one argument", 0 TSRMLS_CC);
1508            return;
1509        }
1510        aht->u.v.nApplyCount++;
1511        zend_call_method(NULL, NULL, NULL, fname, fname_len, &retval, 2, &tmp, arg TSRMLS_CC);
1512        aht->u.v.nApplyCount--;
1513    }
1514    /* A tricky way to pass "aht" by reference, copy back and cleanup */
1515    if (Z_ISREF(tmp) && Z_TYPE_P(Z_REFVAL(tmp))) {
1516        *aht = *Z_ARRVAL_P(Z_REFVAL(tmp));
1517        GC_REMOVE_FROM_BUFFER(Z_ARR_P(Z_REFVAL(tmp)));
1518        efree(Z_ARR_P(Z_REFVAL(tmp)));
1519        efree(Z_REF(tmp));
1520    }
1521    if (!Z_ISUNDEF(retval)) {
1522        ZVAL_COPY_VALUE(return_value, &retval);
1523    }
1524} /* }}} */
1525
1526#define SPL_ARRAY_METHOD(cname, fname, use_arg) \
1527SPL_METHOD(cname, fname) \
1528{ \
1529    spl_array_method(INTERNAL_FUNCTION_PARAM_PASSTHRU, #fname, sizeof(#fname)-1, use_arg); \
1530}
1531
1532/* {{{ proto int ArrayObject::asort([int $sort_flags = SORT_REGULAR ])
1533       proto int ArrayIterator::asort([int $sort_flags = SORT_REGULAR ])
1534   Sort the entries by values. */
1535SPL_ARRAY_METHOD(Array, asort, SPL_ARRAY_METHOD_MAY_USER_ARG) /* }}} */
1536
1537/* {{{ proto int ArrayObject::ksort([int $sort_flags = SORT_REGULAR ])
1538       proto int ArrayIterator::ksort([int $sort_flags = SORT_REGULAR ])
1539   Sort the entries by key. */
1540SPL_ARRAY_METHOD(Array, ksort, SPL_ARRAY_METHOD_MAY_USER_ARG) /* }}} */
1541
1542/* {{{ proto int ArrayObject::uasort(callback cmp_function)
1543       proto int ArrayIterator::uasort(callback cmp_function)
1544   Sort the entries by values user defined function. */
1545SPL_ARRAY_METHOD(Array, uasort, SPL_ARRAY_METHOD_USE_ARG) /* }}} */
1546
1547/* {{{ proto int ArrayObject::uksort(callback cmp_function)
1548       proto int ArrayIterator::uksort(callback cmp_function)
1549   Sort the entries by key using user defined function. */
1550SPL_ARRAY_METHOD(Array, uksort, SPL_ARRAY_METHOD_USE_ARG) /* }}} */
1551
1552/* {{{ proto int ArrayObject::natsort()
1553       proto int ArrayIterator::natsort()
1554   Sort the entries by values using "natural order" algorithm. */
1555SPL_ARRAY_METHOD(Array, natsort, SPL_ARRAY_METHOD_NO_ARG) /* }}} */
1556
1557/* {{{ proto int ArrayObject::natcasesort()
1558       proto int ArrayIterator::natcasesort()
1559   Sort the entries by key using case insensitive "natural order" algorithm. */
1560SPL_ARRAY_METHOD(Array, natcasesort, SPL_ARRAY_METHOD_NO_ARG) /* }}} */
1561
1562/* {{{ proto mixed|NULL ArrayIterator::current()
1563   Return current array entry */
1564SPL_METHOD(Array, current)
1565{
1566    zval *object = getThis();
1567    spl_array_object *intern = Z_SPLARRAY_P(object);
1568    zval *entry;
1569    HashTable *aht = spl_array_get_hash_table(intern, 0 TSRMLS_CC);
1570
1571    if (zend_parse_parameters_none() == FAILURE) {
1572        return;
1573    }
1574
1575    if (spl_array_object_verify_pos(intern, aht TSRMLS_CC) == FAILURE) {
1576        return;
1577    }
1578
1579    if ((entry = zend_hash_get_current_data_ex(aht, &intern->pos)) == NULL) {
1580        return;
1581    }
1582    if (Z_TYPE_P(entry) == IS_INDIRECT) {
1583        entry = Z_INDIRECT_P(entry);
1584        if (Z_TYPE_P(entry) == IS_UNDEF) {
1585            return;
1586        }
1587    }
1588    RETVAL_ZVAL(entry, 1, 0);
1589}
1590/* }}} */
1591
1592/* {{{ proto mixed|NULL ArrayIterator::key()
1593   Return current array key */
1594SPL_METHOD(Array, key)
1595{
1596    if (zend_parse_parameters_none() == FAILURE) {
1597        return;
1598    }
1599
1600    spl_array_iterator_key(getThis(), return_value TSRMLS_CC);
1601} /* }}} */
1602
1603void spl_array_iterator_key(zval *object, zval *return_value TSRMLS_DC) /* {{{ */
1604{
1605    spl_array_object *intern = Z_SPLARRAY_P(object);
1606    HashTable *aht = spl_array_get_hash_table(intern, 0 TSRMLS_CC);
1607
1608    if (spl_array_object_verify_pos(intern, aht TSRMLS_CC) == FAILURE) {
1609        return;
1610    }
1611
1612    zend_hash_get_current_key_zval_ex(aht, return_value, &intern->pos);
1613}
1614/* }}} */
1615
1616/* {{{ proto void ArrayIterator::next()
1617   Move to next entry */
1618SPL_METHOD(Array, next)
1619{
1620    zval *object = getThis();
1621    spl_array_object *intern = Z_SPLARRAY_P(object);
1622    HashTable *aht = spl_array_get_hash_table(intern, 0 TSRMLS_CC);
1623
1624    if (zend_parse_parameters_none() == FAILURE) {
1625        return;
1626    }
1627
1628    if (spl_array_object_verify_pos(intern, aht TSRMLS_CC) == FAILURE) {
1629        return;
1630    }
1631
1632    spl_array_next_no_verify(intern, aht TSRMLS_CC);
1633}
1634/* }}} */
1635
1636/* {{{ proto bool ArrayIterator::valid()
1637   Check whether array contains more entries */
1638SPL_METHOD(Array, valid)
1639{
1640    zval *object = getThis();
1641    spl_array_object *intern = Z_SPLARRAY_P(object);
1642    HashTable *aht = spl_array_get_hash_table(intern, 0 TSRMLS_CC);
1643
1644    if (zend_parse_parameters_none() == FAILURE) {
1645        return;
1646    }
1647
1648    if (spl_array_object_verify_pos(intern, aht TSRMLS_CC) == FAILURE) {
1649        RETURN_FALSE;
1650    } else {
1651        RETURN_BOOL(zend_hash_has_more_elements_ex(aht, &intern->pos) == SUCCESS);
1652    }
1653}
1654/* }}} */
1655
1656/* {{{ proto bool RecursiveArrayIterator::hasChildren()
1657   Check whether current element has children (e.g. is an array) */
1658SPL_METHOD(Array, hasChildren)
1659{
1660    zval *object = getThis(), *entry;
1661    spl_array_object *intern = Z_SPLARRAY_P(object);
1662    HashTable *aht = spl_array_get_hash_table(intern, 0 TSRMLS_CC);
1663
1664    if (zend_parse_parameters_none() == FAILURE) {
1665        return;
1666    }
1667
1668    if (spl_array_object_verify_pos(intern, aht TSRMLS_CC) == FAILURE) {
1669        RETURN_FALSE;
1670    }
1671
1672    if ((entry = zend_hash_get_current_data_ex(aht, &intern->pos)) == NULL) {
1673        RETURN_FALSE;
1674    }
1675
1676    RETURN_BOOL(Z_TYPE_P(entry) == IS_ARRAY || (Z_TYPE_P(entry) == IS_OBJECT && (intern->ar_flags & SPL_ARRAY_CHILD_ARRAYS_ONLY) == 0));
1677}
1678/* }}} */
1679
1680/* {{{ proto object RecursiveArrayIterator::getChildren()
1681   Create a sub iterator for the current element (same class as $this) */
1682SPL_METHOD(Array, getChildren)
1683{
1684    zval *object = getThis(), *entry, flags;
1685    spl_array_object *intern = Z_SPLARRAY_P(object);
1686    HashTable *aht = spl_array_get_hash_table(intern, 0 TSRMLS_CC);
1687
1688    if (zend_parse_parameters_none() == FAILURE) {
1689        return;
1690    }
1691
1692    if (spl_array_object_verify_pos(intern, aht TSRMLS_CC) == FAILURE) {
1693        return;
1694    }
1695
1696    if ((entry = zend_hash_get_current_data_ex(aht, &intern->pos)) == NULL) {
1697        return;
1698    }
1699
1700    if (Z_TYPE_P(entry) == IS_OBJECT) {
1701        if ((intern->ar_flags & SPL_ARRAY_CHILD_ARRAYS_ONLY) != 0) {
1702            return;
1703        }
1704        if (instanceof_function(Z_OBJCE_P(entry), Z_OBJCE_P(getThis()) TSRMLS_CC)) {
1705            RETURN_ZVAL(entry, 1, 0);
1706        }
1707    }
1708
1709    ZVAL_LONG(&flags, SPL_ARRAY_USE_OTHER | intern->ar_flags);
1710    spl_instantiate_arg_ex2(Z_OBJCE_P(getThis()), return_value, entry, &flags TSRMLS_CC);
1711}
1712/* }}} */
1713
1714/* {{{ proto string ArrayObject::serialize()
1715   Serialize the object */
1716SPL_METHOD(Array, serialize)
1717{
1718    zval *object = getThis();
1719    spl_array_object *intern = Z_SPLARRAY_P(object);
1720    HashTable *aht = spl_array_get_hash_table(intern, 0 TSRMLS_CC);
1721    zval members, flags;
1722    php_serialize_data_t var_hash;
1723    smart_str buf = {0};
1724
1725    if (zend_parse_parameters_none() == FAILURE) {
1726        return;
1727    }
1728
1729    if (!aht) {
1730        php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Array was modified outside object and is no longer an array");
1731        return;
1732    }
1733
1734    PHP_VAR_SERIALIZE_INIT(var_hash);
1735
1736    ZVAL_LONG(&flags, (intern->ar_flags & SPL_ARRAY_CLONE_MASK));
1737
1738    /* storage */
1739    smart_str_appendl(&buf, "x:", 2);
1740    //!!! php_var_serialize need to be modified
1741    php_var_serialize(&buf, &flags, &var_hash TSRMLS_CC);
1742
1743    if (!(intern->ar_flags & SPL_ARRAY_IS_SELF)) {
1744        php_var_serialize(&buf, &intern->array, &var_hash TSRMLS_CC);
1745        smart_str_appendc(&buf, ';');
1746    }
1747
1748    /* members */
1749    smart_str_appendl(&buf, "m:", 2);
1750    if (!intern->std.properties) {
1751        rebuild_object_properties(&intern->std);
1752    }
1753
1754    ZVAL_NEW_ARR(&members);
1755    zend_array_dup(Z_ARRVAL(members), intern->std.properties);
1756
1757    php_var_serialize(&buf, &members, &var_hash TSRMLS_CC); /* finishes the string */
1758
1759    zval_ptr_dtor(&members);
1760
1761    /* done */
1762    PHP_VAR_SERIALIZE_DESTROY(var_hash);
1763
1764    if (buf.s) {
1765        RETURN_STR(buf.s);
1766    }
1767
1768    RETURN_NULL();
1769} /* }}} */
1770
1771/* {{{ proto void ArrayObject::unserialize(string serialized)
1772 * unserialize the object
1773 */
1774SPL_METHOD(Array, unserialize)
1775{
1776    spl_array_object *intern = Z_SPLARRAY_P(getThis());
1777
1778    char *buf;
1779    size_t buf_len;
1780    const unsigned char *p, *s;
1781    php_unserialize_data_t var_hash;
1782    zval members, zflags;
1783    HashTable *aht;
1784    zend_long flags;
1785
1786    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &buf, &buf_len) == FAILURE) {
1787        return;
1788    }
1789
1790    if (buf_len == 0) {
1791        return;
1792    }
1793
1794    aht = spl_array_get_hash_table(intern, 0 TSRMLS_CC);
1795    if (aht->u.v.nApplyCount > 0) {
1796        zend_error(E_WARNING, "Modification of ArrayObject during sorting is prohibited");
1797        return;
1798    }
1799
1800    /* storage */
1801    s = p = (const unsigned char*)buf;
1802    PHP_VAR_UNSERIALIZE_INIT(var_hash);
1803
1804    if (*p!= 'x' || *++p != ':') {
1805        goto outexcept;
1806    }
1807    ++p;
1808
1809    if (!php_var_unserialize(&zflags, &p, s + buf_len, &var_hash TSRMLS_CC) || Z_TYPE(zflags) != IS_LONG) {
1810        goto outexcept;
1811    }
1812
1813    --p; /* for ';' */
1814    flags = Z_LVAL(zflags);
1815    /* flags needs to be verified and we also need to verify whether the next
1816     * thing we get is ';'. After that we require an 'm' or somethign else
1817     * where 'm' stands for members and anything else should be an array. If
1818     * neither 'a' or 'm' follows we have an error. */
1819
1820    if (*p != ';') {
1821        goto outexcept;
1822    }
1823    ++p;
1824
1825    if (*p!='m') {
1826        if (*p!='a' && *p!='O' && *p!='C' && *p!='r') {
1827            goto outexcept;
1828        }
1829        intern->ar_flags &= ~SPL_ARRAY_CLONE_MASK;
1830        intern->ar_flags |= flags & SPL_ARRAY_CLONE_MASK;
1831        zval_ptr_dtor(&intern->array);
1832        if (!php_var_unserialize(&intern->array, &p, s + buf_len, &var_hash TSRMLS_CC)) {
1833            goto outexcept;
1834        }
1835    }
1836    if (*p != ';') {
1837        goto outexcept;
1838    }
1839    ++p;
1840
1841    /* members */
1842    if (*p!= 'm' || *++p != ':') {
1843        goto outexcept;
1844    }
1845    ++p;
1846
1847    ZVAL_UNDEF(&members);
1848    if (!php_var_unserialize(&members, &p, s + buf_len, &var_hash TSRMLS_CC) || Z_TYPE(members) != IS_ARRAY) {
1849        zval_ptr_dtor(&members);
1850        goto outexcept;
1851    }
1852
1853    /* copy members */
1854    object_properties_load(&intern->std, Z_ARRVAL(members) TSRMLS_CC);
1855    zval_ptr_dtor(&members);
1856
1857    /* done reading $serialized */
1858
1859    PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
1860    return;
1861
1862outexcept:
1863    PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
1864    zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0 TSRMLS_CC, "Error at offset %pd of %d bytes", (zend_long)((char*)p - buf), buf_len);
1865    return;
1866
1867} /* }}} */
1868
1869/* {{{ arginfo and function tbale */
1870ZEND_BEGIN_ARG_INFO(arginfo_array___construct, 0)
1871    ZEND_ARG_INFO(0, array)
1872ZEND_END_ARG_INFO()
1873
1874ZEND_BEGIN_ARG_INFO_EX(arginfo_array_offsetGet, 0, 0, 1)
1875    ZEND_ARG_INFO(0, index)
1876ZEND_END_ARG_INFO()
1877
1878ZEND_BEGIN_ARG_INFO_EX(arginfo_array_offsetSet, 0, 0, 2)
1879    ZEND_ARG_INFO(0, index)
1880    ZEND_ARG_INFO(0, newval)
1881ZEND_END_ARG_INFO()
1882
1883ZEND_BEGIN_ARG_INFO(arginfo_array_append, 0)
1884    ZEND_ARG_INFO(0, value)
1885ZEND_END_ARG_INFO()
1886
1887ZEND_BEGIN_ARG_INFO(arginfo_array_seek, 0)
1888    ZEND_ARG_INFO(0, position)
1889ZEND_END_ARG_INFO()
1890
1891ZEND_BEGIN_ARG_INFO(arginfo_array_exchangeArray, 0)
1892    ZEND_ARG_INFO(0, array)
1893ZEND_END_ARG_INFO()
1894
1895ZEND_BEGIN_ARG_INFO(arginfo_array_setFlags, 0)
1896    ZEND_ARG_INFO(0, flags)
1897ZEND_END_ARG_INFO()
1898
1899ZEND_BEGIN_ARG_INFO(arginfo_array_setIteratorClass, 0)
1900    ZEND_ARG_INFO(0, iteratorClass)
1901ZEND_END_ARG_INFO()
1902
1903ZEND_BEGIN_ARG_INFO(arginfo_array_uXsort, 0)
1904    ZEND_ARG_INFO(0, cmp_function)
1905ZEND_END_ARG_INFO();
1906
1907ZEND_BEGIN_ARG_INFO(arginfo_array_unserialize, 0)
1908    ZEND_ARG_INFO(0, serialized)
1909ZEND_END_ARG_INFO();
1910
1911ZEND_BEGIN_ARG_INFO(arginfo_array_void, 0)
1912ZEND_END_ARG_INFO()
1913
1914static const zend_function_entry spl_funcs_ArrayObject[] = {
1915    SPL_ME(Array, __construct,      arginfo_array___construct,      ZEND_ACC_PUBLIC)
1916    SPL_ME(Array, offsetExists,     arginfo_array_offsetGet,        ZEND_ACC_PUBLIC)
1917    SPL_ME(Array, offsetGet,        arginfo_array_offsetGet,        ZEND_ACC_PUBLIC)
1918    SPL_ME(Array, offsetSet,        arginfo_array_offsetSet,        ZEND_ACC_PUBLIC)
1919    SPL_ME(Array, offsetUnset,      arginfo_array_offsetGet,        ZEND_ACC_PUBLIC)
1920    SPL_ME(Array, append,           arginfo_array_append,           ZEND_ACC_PUBLIC)
1921    SPL_ME(Array, getArrayCopy,     arginfo_array_void,             ZEND_ACC_PUBLIC)
1922    SPL_ME(Array, count,            arginfo_array_void,             ZEND_ACC_PUBLIC)
1923    SPL_ME(Array, getFlags,         arginfo_array_void,             ZEND_ACC_PUBLIC)
1924    SPL_ME(Array, setFlags,         arginfo_array_setFlags,         ZEND_ACC_PUBLIC)
1925    SPL_ME(Array, asort,            arginfo_array_void,             ZEND_ACC_PUBLIC)
1926    SPL_ME(Array, ksort,            arginfo_array_void,             ZEND_ACC_PUBLIC)
1927    SPL_ME(Array, uasort,           arginfo_array_uXsort,           ZEND_ACC_PUBLIC)
1928    SPL_ME(Array, uksort,           arginfo_array_uXsort,           ZEND_ACC_PUBLIC)
1929    SPL_ME(Array, natsort,          arginfo_array_void,             ZEND_ACC_PUBLIC)
1930    SPL_ME(Array, natcasesort,      arginfo_array_void,             ZEND_ACC_PUBLIC)
1931    SPL_ME(Array, unserialize,      arginfo_array_unserialize,      ZEND_ACC_PUBLIC)
1932    SPL_ME(Array, serialize,        arginfo_array_void,             ZEND_ACC_PUBLIC)
1933    /* ArrayObject specific */
1934    SPL_ME(Array, getIterator,      arginfo_array_void,             ZEND_ACC_PUBLIC)
1935    SPL_ME(Array, exchangeArray,    arginfo_array_exchangeArray,    ZEND_ACC_PUBLIC)
1936    SPL_ME(Array, setIteratorClass, arginfo_array_setIteratorClass, ZEND_ACC_PUBLIC)
1937    SPL_ME(Array, getIteratorClass, arginfo_array_void,             ZEND_ACC_PUBLIC)
1938    PHP_FE_END
1939};
1940
1941static const zend_function_entry spl_funcs_ArrayIterator[] = {
1942    SPL_ME(Array, __construct,      arginfo_array___construct,      ZEND_ACC_PUBLIC)
1943    SPL_ME(Array, offsetExists,     arginfo_array_offsetGet,        ZEND_ACC_PUBLIC)
1944    SPL_ME(Array, offsetGet,        arginfo_array_offsetGet,        ZEND_ACC_PUBLIC)
1945    SPL_ME(Array, offsetSet,        arginfo_array_offsetSet,        ZEND_ACC_PUBLIC)
1946    SPL_ME(Array, offsetUnset,      arginfo_array_offsetGet,        ZEND_ACC_PUBLIC)
1947    SPL_ME(Array, append,           arginfo_array_append,           ZEND_ACC_PUBLIC)
1948    SPL_ME(Array, getArrayCopy,     arginfo_array_void,             ZEND_ACC_PUBLIC)
1949    SPL_ME(Array, count,            arginfo_array_void,             ZEND_ACC_PUBLIC)
1950    SPL_ME(Array, getFlags,         arginfo_array_void,             ZEND_ACC_PUBLIC)
1951    SPL_ME(Array, setFlags,         arginfo_array_setFlags,         ZEND_ACC_PUBLIC)
1952    SPL_ME(Array, asort,            arginfo_array_void,             ZEND_ACC_PUBLIC)
1953    SPL_ME(Array, ksort,            arginfo_array_void,             ZEND_ACC_PUBLIC)
1954    SPL_ME(Array, uasort,           arginfo_array_uXsort,           ZEND_ACC_PUBLIC)
1955    SPL_ME(Array, uksort,           arginfo_array_uXsort,           ZEND_ACC_PUBLIC)
1956    SPL_ME(Array, natsort,          arginfo_array_void,             ZEND_ACC_PUBLIC)
1957    SPL_ME(Array, natcasesort,      arginfo_array_void,             ZEND_ACC_PUBLIC)
1958    SPL_ME(Array, unserialize,      arginfo_array_unserialize,      ZEND_ACC_PUBLIC)
1959    SPL_ME(Array, serialize,        arginfo_array_void,             ZEND_ACC_PUBLIC)
1960    /* ArrayIterator specific */
1961    SPL_ME(Array, rewind,           arginfo_array_void,             ZEND_ACC_PUBLIC)
1962    SPL_ME(Array, current,          arginfo_array_void,             ZEND_ACC_PUBLIC)
1963    SPL_ME(Array, key,              arginfo_array_void,             ZEND_ACC_PUBLIC)
1964    SPL_ME(Array, next,             arginfo_array_void,             ZEND_ACC_PUBLIC)
1965    SPL_ME(Array, valid,            arginfo_array_void,             ZEND_ACC_PUBLIC)
1966    SPL_ME(Array, seek,             arginfo_array_seek,             ZEND_ACC_PUBLIC)
1967    PHP_FE_END
1968};
1969
1970static const zend_function_entry spl_funcs_RecursiveArrayIterator[] = {
1971    SPL_ME(Array, hasChildren,   arginfo_array_void, ZEND_ACC_PUBLIC)
1972    SPL_ME(Array, getChildren,   arginfo_array_void, ZEND_ACC_PUBLIC)
1973    PHP_FE_END
1974};
1975/* }}} */
1976
1977/* {{{ PHP_MINIT_FUNCTION(spl_array) */
1978PHP_MINIT_FUNCTION(spl_array)
1979{
1980    REGISTER_SPL_STD_CLASS_EX(ArrayObject, spl_array_object_new, spl_funcs_ArrayObject);
1981    REGISTER_SPL_IMPLEMENTS(ArrayObject, Aggregate);
1982    REGISTER_SPL_IMPLEMENTS(ArrayObject, ArrayAccess);
1983    REGISTER_SPL_IMPLEMENTS(ArrayObject, Serializable);
1984    REGISTER_SPL_IMPLEMENTS(ArrayObject, Countable);
1985    memcpy(&spl_handler_ArrayObject, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
1986
1987    spl_handler_ArrayObject.offset = XtOffsetOf(spl_array_object, std);
1988
1989    spl_handler_ArrayObject.clone_obj = spl_array_object_clone;
1990    spl_handler_ArrayObject.read_dimension = spl_array_read_dimension;
1991    spl_handler_ArrayObject.write_dimension = spl_array_write_dimension;
1992    spl_handler_ArrayObject.unset_dimension = spl_array_unset_dimension;
1993    spl_handler_ArrayObject.has_dimension = spl_array_has_dimension;
1994    spl_handler_ArrayObject.count_elements = spl_array_object_count_elements;
1995
1996    spl_handler_ArrayObject.get_properties = spl_array_get_properties;
1997    spl_handler_ArrayObject.get_debug_info = spl_array_get_debug_info;
1998    spl_handler_ArrayObject.read_property = spl_array_read_property;
1999    spl_handler_ArrayObject.write_property = spl_array_write_property;
2000    spl_handler_ArrayObject.get_property_ptr_ptr = spl_array_get_property_ptr_ptr;
2001    spl_handler_ArrayObject.has_property = spl_array_has_property;
2002    spl_handler_ArrayObject.unset_property = spl_array_unset_property;
2003
2004    spl_handler_ArrayObject.compare_objects = spl_array_compare_objects;
2005    spl_handler_ArrayObject.dtor_obj = zend_objects_destroy_object;
2006    spl_handler_ArrayObject.free_obj = spl_array_object_free_storage;
2007
2008    REGISTER_SPL_STD_CLASS_EX(ArrayIterator, spl_array_object_new, spl_funcs_ArrayIterator);
2009    REGISTER_SPL_IMPLEMENTS(ArrayIterator, Iterator);
2010    REGISTER_SPL_IMPLEMENTS(ArrayIterator, ArrayAccess);
2011    REGISTER_SPL_IMPLEMENTS(ArrayIterator, SeekableIterator);
2012    REGISTER_SPL_IMPLEMENTS(ArrayIterator, Serializable);
2013    REGISTER_SPL_IMPLEMENTS(ArrayIterator, Countable);
2014    memcpy(&spl_handler_ArrayIterator, &spl_handler_ArrayObject, sizeof(zend_object_handlers));
2015    spl_ce_ArrayIterator->get_iterator = spl_array_get_iterator;
2016
2017    REGISTER_SPL_SUB_CLASS_EX(RecursiveArrayIterator, ArrayIterator, spl_array_object_new, spl_funcs_RecursiveArrayIterator);
2018    REGISTER_SPL_IMPLEMENTS(RecursiveArrayIterator, RecursiveIterator);
2019    spl_ce_RecursiveArrayIterator->get_iterator = spl_array_get_iterator;
2020
2021    REGISTER_SPL_CLASS_CONST_LONG(ArrayObject,   "STD_PROP_LIST",    SPL_ARRAY_STD_PROP_LIST);
2022    REGISTER_SPL_CLASS_CONST_LONG(ArrayObject,   "ARRAY_AS_PROPS",   SPL_ARRAY_ARRAY_AS_PROPS);
2023
2024    REGISTER_SPL_CLASS_CONST_LONG(ArrayIterator, "STD_PROP_LIST",    SPL_ARRAY_STD_PROP_LIST);
2025    REGISTER_SPL_CLASS_CONST_LONG(ArrayIterator, "ARRAY_AS_PROPS",   SPL_ARRAY_ARRAY_AS_PROPS);
2026
2027    REGISTER_SPL_CLASS_CONST_LONG(RecursiveArrayIterator, "CHILD_ARRAYS_ONLY", SPL_ARRAY_CHILD_ARRAYS_ONLY);
2028
2029    return SUCCESS;
2030}
2031/* }}} */
2032
2033/*
2034 * Local variables:
2035 * tab-width: 4
2036 * c-basic-offset: 4
2037 * End:
2038 * vim600: fdm=marker
2039 * vim: noet sw=4 ts=4
2040 */
2041