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