xref: /PHP-7.3/ext/spl/spl_fixedarray.c (revision 47c74555)
1 /*
2   +----------------------------------------------------------------------+
3   | PHP Version 7                                                        |
4   +----------------------------------------------------------------------+
5   | Copyright (c) 1997-2018 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   | Author: Antony Dovgal <tony@daylessday.org>                          |
16   |         Etienne Kneuss <colder@php.net>                              |
17   +----------------------------------------------------------------------+
18 */
19 
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23 
24 #include "php.h"
25 #include "php_ini.h"
26 #include "ext/standard/info.h"
27 #include "zend_exceptions.h"
28 
29 #include "php_spl.h"
30 #include "spl_functions.h"
31 #include "spl_engine.h"
32 #include "spl_fixedarray.h"
33 #include "spl_exceptions.h"
34 #include "spl_iterators.h"
35 
36 zend_object_handlers spl_handler_SplFixedArray;
37 PHPAPI zend_class_entry *spl_ce_SplFixedArray;
38 
39 #ifdef COMPILE_DL_SPL_FIXEDARRAY
40 ZEND_GET_MODULE(spl_fixedarray)
41 #endif
42 
43 typedef struct _spl_fixedarray { /* {{{ */
44 	zend_long size;
45 	zval *elements;
46 } spl_fixedarray;
47 /* }}} */
48 
49 typedef struct _spl_fixedarray_object { /* {{{ */
50 	spl_fixedarray        array;
51 	zend_function         *fptr_offset_get;
52 	zend_function         *fptr_offset_set;
53 	zend_function         *fptr_offset_has;
54 	zend_function         *fptr_offset_del;
55 	zend_function         *fptr_count;
56 	int                    current;
57 	int                    flags;
58 	zend_class_entry      *ce_get_iterator;
59 	zend_object            std;
60 } spl_fixedarray_object;
61 /* }}} */
62 
63 typedef struct _spl_fixedarray_it { /* {{{ */
64 	zend_user_iterator     intern;
65 } spl_fixedarray_it;
66 /* }}} */
67 
68 #define SPL_FIXEDARRAY_OVERLOADED_REWIND  0x0001
69 #define SPL_FIXEDARRAY_OVERLOADED_VALID   0x0002
70 #define SPL_FIXEDARRAY_OVERLOADED_KEY     0x0004
71 #define SPL_FIXEDARRAY_OVERLOADED_CURRENT 0x0008
72 #define SPL_FIXEDARRAY_OVERLOADED_NEXT    0x0010
73 
spl_fixed_array_from_obj(zend_object * obj)74 static inline spl_fixedarray_object *spl_fixed_array_from_obj(zend_object *obj) /* {{{ */ {
75 	return (spl_fixedarray_object*)((char*)(obj) - XtOffsetOf(spl_fixedarray_object, std));
76 }
77 /* }}} */
78 
79 #define Z_SPLFIXEDARRAY_P(zv)  spl_fixed_array_from_obj(Z_OBJ_P((zv)))
80 
spl_fixedarray_init(spl_fixedarray * array,zend_long size)81 static void spl_fixedarray_init(spl_fixedarray *array, zend_long size) /* {{{ */
82 {
83 	if (size > 0) {
84 		array->size = 0; /* reset size in case ecalloc() fails */
85 		array->elements = ecalloc(size, sizeof(zval));
86 		array->size = size;
87 	} else {
88 		array->elements = NULL;
89 		array->size = 0;
90 	}
91 }
92 /* }}} */
93 
spl_fixedarray_resize(spl_fixedarray * array,zend_long size)94 static void spl_fixedarray_resize(spl_fixedarray *array, zend_long size) /* {{{ */
95 {
96 	if (size == array->size) {
97 		/* nothing to do */
98 		return;
99 	}
100 
101 	/* first initialization */
102 	if (array->size == 0) {
103 		spl_fixedarray_init(array, size);
104 		return;
105 	}
106 
107 	/* clearing the array */
108 	if (size == 0) {
109 		zend_long i;
110 
111 		for (i = 0; i < array->size; i++) {
112 			zval_ptr_dtor(&(array->elements[i]));
113 		}
114 
115 		if (array->elements) {
116 			efree(array->elements);
117 			array->elements = NULL;
118 		}
119 	} else if (size > array->size) {
120 		array->elements = safe_erealloc(array->elements, size, sizeof(zval), 0);
121 		memset(array->elements + array->size, '\0', sizeof(zval) * (size - array->size));
122 	} else { /* size < array->size */
123 		zend_long i;
124 
125 		for (i = size; i < array->size; i++) {
126 			zval_ptr_dtor(&(array->elements[i]));
127 		}
128 		array->elements = erealloc(array->elements, sizeof(zval) * size);
129 	}
130 
131 	array->size = size;
132 }
133 /* }}} */
134 
spl_fixedarray_copy(spl_fixedarray * to,spl_fixedarray * from)135 static void spl_fixedarray_copy(spl_fixedarray *to, spl_fixedarray *from) /* {{{ */
136 {
137 	int i;
138 	for (i = 0; i < from->size; i++) {
139 		ZVAL_COPY(&to->elements[i], &from->elements[i]);
140 	}
141 }
142 /* }}} */
143 
spl_fixedarray_object_get_gc(zval * obj,zval ** table,int * n)144 static HashTable* spl_fixedarray_object_get_gc(zval *obj, zval **table, int *n) /* {{{{ */
145 {
146 	spl_fixedarray_object *intern  = Z_SPLFIXEDARRAY_P(obj);
147 	HashTable *ht = zend_std_get_properties(obj);
148 
149 	*table = intern->array.elements;
150 	*n = (int)intern->array.size;
151 
152 	return ht;
153 }
154 /* }}}} */
155 
spl_fixedarray_object_get_properties(zval * obj)156 static HashTable* spl_fixedarray_object_get_properties(zval *obj) /* {{{{ */
157 {
158 	spl_fixedarray_object *intern  = Z_SPLFIXEDARRAY_P(obj);
159 	HashTable *ht = zend_std_get_properties(obj);
160 	zend_long  i = 0;
161 
162 	if (intern->array.size > 0) {
163 		zend_long j = zend_hash_num_elements(ht);
164 
165 		for (i = 0; i < intern->array.size; i++) {
166 			if (!Z_ISUNDEF(intern->array.elements[i])) {
167 				zend_hash_index_update(ht, i, &intern->array.elements[i]);
168 				Z_TRY_ADDREF(intern->array.elements[i]);
169 			} else {
170 				zend_hash_index_update(ht, i, &EG(uninitialized_zval));
171 			}
172 		}
173 		if (j > intern->array.size) {
174 			for (i = intern->array.size; i < j; ++i) {
175 				zend_hash_index_del(ht, i);
176 			}
177 		}
178 	}
179 
180 	return ht;
181 }
182 /* }}}} */
183 
spl_fixedarray_object_free_storage(zend_object * object)184 static void spl_fixedarray_object_free_storage(zend_object *object) /* {{{ */
185 {
186 	spl_fixedarray_object *intern = spl_fixed_array_from_obj(object);
187 	zend_long i;
188 
189 	if (intern->array.size > 0) {
190 		for (i = 0; i < intern->array.size; i++) {
191 			zval_ptr_dtor(&(intern->array.elements[i]));
192 		}
193 
194 		if (intern->array.size > 0 && intern->array.elements) {
195 			efree(intern->array.elements);
196 		}
197 	}
198 
199 	zend_object_std_dtor(&intern->std);
200 }
201 /* }}} */
202 
203 zend_object_iterator *spl_fixedarray_get_iterator(zend_class_entry *ce, zval *object, int by_ref);
204 
spl_fixedarray_object_new_ex(zend_class_entry * class_type,zval * orig,int clone_orig)205 static zend_object *spl_fixedarray_object_new_ex(zend_class_entry *class_type, zval *orig, int clone_orig) /* {{{ */
206 {
207 	spl_fixedarray_object *intern;
208 	zend_class_entry     *parent = class_type;
209 	int                   inherited = 0;
210 
211 	intern = zend_object_alloc(sizeof(spl_fixedarray_object), parent);
212 
213 	zend_object_std_init(&intern->std, class_type);
214 	object_properties_init(&intern->std, class_type);
215 
216 	intern->current = 0;
217 	intern->flags = 0;
218 
219 	if (orig && clone_orig) {
220 		spl_fixedarray_object *other = Z_SPLFIXEDARRAY_P(orig);
221 		intern->ce_get_iterator = other->ce_get_iterator;
222 		spl_fixedarray_init(&intern->array, other->array.size);
223 		spl_fixedarray_copy(&intern->array, &other->array);
224 	}
225 
226 	while (parent) {
227 		if (parent == spl_ce_SplFixedArray) {
228 			intern->std.handlers = &spl_handler_SplFixedArray;
229 			class_type->get_iterator = spl_fixedarray_get_iterator;
230 			break;
231 		}
232 
233 		parent = parent->parent;
234 		inherited = 1;
235 	}
236 
237 	if (!parent) { /* this must never happen */
238 		php_error_docref(NULL, E_COMPILE_ERROR, "Internal compiler error, Class is not child of SplFixedArray");
239 	}
240 
241 	if (!class_type->iterator_funcs_ptr->zf_current) {
242 		class_type->iterator_funcs_ptr->zf_rewind = zend_hash_str_find_ptr(&class_type->function_table, "rewind", sizeof("rewind") - 1);
243 		class_type->iterator_funcs_ptr->zf_valid = zend_hash_str_find_ptr(&class_type->function_table, "valid", sizeof("valid") - 1);
244 		class_type->iterator_funcs_ptr->zf_key = zend_hash_str_find_ptr(&class_type->function_table, "key", sizeof("key") - 1);
245 		class_type->iterator_funcs_ptr->zf_current = zend_hash_str_find_ptr(&class_type->function_table, "current", sizeof("current") - 1);
246 		class_type->iterator_funcs_ptr->zf_next = zend_hash_str_find_ptr(&class_type->function_table, "next", sizeof("next") - 1);
247 	}
248 	if (inherited) {
249 		if (class_type->iterator_funcs_ptr->zf_rewind->common.scope  != parent) {
250 			intern->flags |= SPL_FIXEDARRAY_OVERLOADED_REWIND;
251 		}
252 		if (class_type->iterator_funcs_ptr->zf_valid->common.scope   != parent) {
253 			intern->flags |= SPL_FIXEDARRAY_OVERLOADED_VALID;
254 		}
255 		if (class_type->iterator_funcs_ptr->zf_key->common.scope     != parent) {
256 			intern->flags |= SPL_FIXEDARRAY_OVERLOADED_KEY;
257 		}
258 		if (class_type->iterator_funcs_ptr->zf_current->common.scope != parent) {
259 			intern->flags |= SPL_FIXEDARRAY_OVERLOADED_CURRENT;
260 		}
261 		if (class_type->iterator_funcs_ptr->zf_next->common.scope    != parent) {
262 			intern->flags |= SPL_FIXEDARRAY_OVERLOADED_NEXT;
263 		}
264 
265 		intern->fptr_offset_get = zend_hash_str_find_ptr(&class_type->function_table, "offsetget", sizeof("offsetget") - 1);
266 		if (intern->fptr_offset_get->common.scope == parent) {
267 			intern->fptr_offset_get = NULL;
268 		}
269 		intern->fptr_offset_set = zend_hash_str_find_ptr(&class_type->function_table, "offsetset", sizeof("offsetset") - 1);
270 		if (intern->fptr_offset_set->common.scope == parent) {
271 			intern->fptr_offset_set = NULL;
272 		}
273 		intern->fptr_offset_has = zend_hash_str_find_ptr(&class_type->function_table, "offsetexists", sizeof("offsetexists") - 1);
274 		if (intern->fptr_offset_has->common.scope == parent) {
275 			intern->fptr_offset_has = NULL;
276 		}
277 		intern->fptr_offset_del = zend_hash_str_find_ptr(&class_type->function_table, "offsetunset", sizeof("offsetunset") - 1);
278 		if (intern->fptr_offset_del->common.scope == parent) {
279 			intern->fptr_offset_del = NULL;
280 		}
281 		intern->fptr_count = zend_hash_str_find_ptr(&class_type->function_table, "count", sizeof("count") - 1);
282 		if (intern->fptr_count->common.scope == parent) {
283 			intern->fptr_count = NULL;
284 		}
285 	}
286 
287 	return &intern->std;
288 }
289 /* }}} */
290 
spl_fixedarray_new(zend_class_entry * class_type)291 static zend_object *spl_fixedarray_new(zend_class_entry *class_type) /* {{{ */
292 {
293 	return spl_fixedarray_object_new_ex(class_type, NULL, 0);
294 }
295 /* }}} */
296 
spl_fixedarray_object_clone(zval * zobject)297 static zend_object *spl_fixedarray_object_clone(zval *zobject) /* {{{ */
298 {
299 	zend_object *old_object;
300 	zend_object *new_object;
301 
302 	old_object  = Z_OBJ_P(zobject);
303 	new_object = spl_fixedarray_object_new_ex(old_object->ce, zobject, 1);
304 
305 	zend_objects_clone_members(new_object, old_object);
306 
307 	return new_object;
308 }
309 /* }}} */
310 
spl_fixedarray_object_read_dimension_helper(spl_fixedarray_object * intern,zval * offset)311 static inline zval *spl_fixedarray_object_read_dimension_helper(spl_fixedarray_object *intern, zval *offset) /* {{{ */
312 {
313 	zend_long index;
314 
315 	/* we have to return NULL on error here to avoid memleak because of
316 	 * ZE duplicating uninitialized_zval_ptr */
317 	if (!offset) {
318 		zend_throw_exception(spl_ce_RuntimeException, "Index invalid or out of range", 0);
319 		return NULL;
320 	}
321 
322 	if (Z_TYPE_P(offset) != IS_LONG) {
323 		index = spl_offset_convert_to_long(offset);
324 	} else {
325 		index = Z_LVAL_P(offset);
326 	}
327 
328 	if (index < 0 || index >= intern->array.size) {
329 		zend_throw_exception(spl_ce_RuntimeException, "Index invalid or out of range", 0);
330 		return NULL;
331 	} else if (Z_ISUNDEF(intern->array.elements[index])) {
332 		return NULL;
333 	} else {
334 		return &intern->array.elements[index];
335 	}
336 }
337 /* }}} */
338 
339 static int spl_fixedarray_object_has_dimension(zval *object, zval *offset, int check_empty);
340 
spl_fixedarray_object_read_dimension(zval * object,zval * offset,int type,zval * rv)341 static zval *spl_fixedarray_object_read_dimension(zval *object, zval *offset, int type, zval *rv) /* {{{ */
342 {
343 	spl_fixedarray_object *intern;
344 
345 	intern = Z_SPLFIXEDARRAY_P(object);
346 
347 	if (type == BP_VAR_IS && !spl_fixedarray_object_has_dimension(object, offset, 0)) {
348 		return &EG(uninitialized_zval);
349 	}
350 
351 	if (intern->fptr_offset_get) {
352 		zval tmp;
353 		if (!offset) {
354 			ZVAL_NULL(&tmp);
355 			offset = &tmp;
356 		} else {
357 			SEPARATE_ARG_IF_REF(offset);
358 		}
359 		zend_call_method_with_1_params(object, intern->std.ce, &intern->fptr_offset_get, "offsetGet", rv, offset);
360 		zval_ptr_dtor(offset);
361 		if (!Z_ISUNDEF_P(rv)) {
362 			return rv;
363 		}
364 		return &EG(uninitialized_zval);
365 	}
366 
367 	return spl_fixedarray_object_read_dimension_helper(intern, offset);
368 }
369 /* }}} */
370 
spl_fixedarray_object_write_dimension_helper(spl_fixedarray_object * intern,zval * offset,zval * value)371 static inline void spl_fixedarray_object_write_dimension_helper(spl_fixedarray_object *intern, zval *offset, zval *value) /* {{{ */
372 {
373 	zend_long index;
374 
375 	if (!offset) {
376 		/* '$array[] = value' syntax is not supported */
377 		zend_throw_exception(spl_ce_RuntimeException, "Index invalid or out of range", 0);
378 		return;
379 	}
380 
381 	if (Z_TYPE_P(offset) != IS_LONG) {
382 		index = spl_offset_convert_to_long(offset);
383 	} else {
384 		index = Z_LVAL_P(offset);
385 	}
386 
387 	if (index < 0 || index >= intern->array.size) {
388 		zend_throw_exception(spl_ce_RuntimeException, "Index invalid or out of range", 0);
389 		return;
390 	} else {
391 		if (!Z_ISUNDEF(intern->array.elements[index])) {
392 			zval_ptr_dtor(&(intern->array.elements[index]));
393 		}
394 		ZVAL_COPY_DEREF(&intern->array.elements[index], value);
395 	}
396 }
397 /* }}} */
398 
spl_fixedarray_object_write_dimension(zval * object,zval * offset,zval * value)399 static void spl_fixedarray_object_write_dimension(zval *object, zval *offset, zval *value) /* {{{ */
400 {
401 	spl_fixedarray_object *intern;
402 	zval tmp;
403 
404 	intern = Z_SPLFIXEDARRAY_P(object);
405 
406 	if (intern->fptr_offset_set) {
407 		if (!offset) {
408 			ZVAL_NULL(&tmp);
409 			offset = &tmp;
410 		} else {
411 			SEPARATE_ARG_IF_REF(offset);
412 		}
413 		SEPARATE_ARG_IF_REF(value);
414 		zend_call_method_with_2_params(object, intern->std.ce, &intern->fptr_offset_set, "offsetSet", NULL, offset, value);
415 		zval_ptr_dtor(value);
416 		zval_ptr_dtor(offset);
417 		return;
418 	}
419 
420 	spl_fixedarray_object_write_dimension_helper(intern, offset, value);
421 }
422 /* }}} */
423 
spl_fixedarray_object_unset_dimension_helper(spl_fixedarray_object * intern,zval * offset)424 static inline void spl_fixedarray_object_unset_dimension_helper(spl_fixedarray_object *intern, zval *offset) /* {{{ */
425 {
426 	zend_long index;
427 
428 	if (Z_TYPE_P(offset) != IS_LONG) {
429 		index = spl_offset_convert_to_long(offset);
430 	} else {
431 		index = Z_LVAL_P(offset);
432 	}
433 
434 	if (index < 0 || index >= intern->array.size) {
435 		zend_throw_exception(spl_ce_RuntimeException, "Index invalid or out of range", 0);
436 		return;
437 	} else {
438 		zval_ptr_dtor(&(intern->array.elements[index]));
439 		ZVAL_UNDEF(&intern->array.elements[index]);
440 	}
441 }
442 /* }}} */
443 
spl_fixedarray_object_unset_dimension(zval * object,zval * offset)444 static void spl_fixedarray_object_unset_dimension(zval *object, zval *offset) /* {{{ */
445 {
446 	spl_fixedarray_object *intern;
447 
448 	intern = Z_SPLFIXEDARRAY_P(object);
449 
450 	if (intern->fptr_offset_del) {
451 		SEPARATE_ARG_IF_REF(offset);
452 		zend_call_method_with_1_params(object, intern->std.ce, &intern->fptr_offset_del, "offsetUnset", NULL, offset);
453 		zval_ptr_dtor(offset);
454 		return;
455 	}
456 
457 	spl_fixedarray_object_unset_dimension_helper(intern, offset);
458 
459 }
460 /* }}} */
461 
spl_fixedarray_object_has_dimension_helper(spl_fixedarray_object * intern,zval * offset,int check_empty)462 static inline int spl_fixedarray_object_has_dimension_helper(spl_fixedarray_object *intern, zval *offset, int check_empty) /* {{{ */
463 {
464 	zend_long index;
465 	int retval;
466 
467 	if (Z_TYPE_P(offset) != IS_LONG) {
468 		index = spl_offset_convert_to_long(offset);
469 	} else {
470 		index = Z_LVAL_P(offset);
471 	}
472 
473 	if (index < 0 || index >= intern->array.size) {
474 		retval = 0;
475 	} else {
476 		if (Z_ISUNDEF(intern->array.elements[index])) {
477 			retval = 0;
478 		} else if (check_empty) {
479 			if (zend_is_true(&intern->array.elements[index])) {
480 				retval = 1;
481 			} else {
482 				retval = 0;
483 			}
484 		} else { /* != NULL and !check_empty */
485 			retval = 1;
486 		}
487 	}
488 
489 	return retval;
490 }
491 /* }}} */
492 
spl_fixedarray_object_has_dimension(zval * object,zval * offset,int check_empty)493 static int spl_fixedarray_object_has_dimension(zval *object, zval *offset, int check_empty) /* {{{ */
494 {
495 	spl_fixedarray_object *intern;
496 
497 	intern = Z_SPLFIXEDARRAY_P(object);
498 
499 	if (intern->fptr_offset_has) {
500 		zval rv;
501 		zend_bool result;
502 
503 		SEPARATE_ARG_IF_REF(offset);
504 		zend_call_method_with_1_params(object, intern->std.ce, &intern->fptr_offset_has, "offsetExists", &rv, offset);
505 		zval_ptr_dtor(offset);
506 		result = zend_is_true(&rv);
507 		zval_ptr_dtor(&rv);
508 		return result;
509 	}
510 
511 	return spl_fixedarray_object_has_dimension_helper(intern, offset, check_empty);
512 }
513 /* }}} */
514 
spl_fixedarray_object_count_elements(zval * object,zend_long * count)515 static int spl_fixedarray_object_count_elements(zval *object, zend_long *count) /* {{{ */
516 {
517 	spl_fixedarray_object *intern;
518 
519 	intern = Z_SPLFIXEDARRAY_P(object);
520 	if (intern->fptr_count) {
521 		zval rv;
522 		zend_call_method_with_0_params(object, intern->std.ce, &intern->fptr_count, "count", &rv);
523 		if (!Z_ISUNDEF(rv)) {
524 			*count = zval_get_long(&rv);
525 			zval_ptr_dtor(&rv);
526 		} else {
527 			*count = 0;
528 		}
529 	} else {
530 		*count = intern->array.size;
531 	}
532 	return SUCCESS;
533 }
534 /* }}} */
535 
536 /* {{{ proto SplFixedArray::__construct([int size])
537 */
SPL_METHOD(SplFixedArray,__construct)538 SPL_METHOD(SplFixedArray, __construct)
539 {
540 	zval *object = getThis();
541 	spl_fixedarray_object *intern;
542 	zend_long size = 0;
543 
544 	if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "|l", &size) == FAILURE) {
545 		return;
546 	}
547 
548 	if (size < 0) {
549 		zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0, "array size cannot be less than zero");
550 		return;
551 	}
552 
553 	intern = Z_SPLFIXEDARRAY_P(object);
554 
555 	if (intern->array.size > 0) {
556 		/* called __construct() twice, bail out */
557 		return;
558 	}
559 
560 	spl_fixedarray_init(&intern->array, size);
561 }
562 /* }}} */
563 
564 /* {{{ proto SplFixedArray::__wakeup()
565 */
SPL_METHOD(SplFixedArray,__wakeup)566 SPL_METHOD(SplFixedArray, __wakeup)
567 {
568 	spl_fixedarray_object *intern = Z_SPLFIXEDARRAY_P(getThis());
569 	HashTable *intern_ht = zend_std_get_properties(getThis());
570 	zval *data;
571 
572 	if (zend_parse_parameters_none() == FAILURE) {
573 		return;
574 	}
575 
576 	if (intern->array.size == 0) {
577 		int index = 0;
578 		int size = zend_hash_num_elements(intern_ht);
579 
580 		spl_fixedarray_init(&intern->array, size);
581 
582 		ZEND_HASH_FOREACH_VAL(intern_ht, data) {
583 			ZVAL_COPY(&intern->array.elements[index], data);
584 			index++;
585 		} ZEND_HASH_FOREACH_END();
586 
587 		/* Remove the unserialised properties, since we now have the elements
588 		 * within the spl_fixedarray_object structure. */
589 		zend_hash_clean(intern_ht);
590 	}
591 }
592 /* }}} */
593 
594 /* {{{ proto int SplFixedArray::count(void)
595 */
SPL_METHOD(SplFixedArray,count)596 SPL_METHOD(SplFixedArray, count)
597 {
598 	zval *object = getThis();
599 	spl_fixedarray_object *intern;
600 
601 	if (zend_parse_parameters_none() == FAILURE) {
602 		return;
603 	}
604 
605 	intern = Z_SPLFIXEDARRAY_P(object);
606 	RETURN_LONG(intern->array.size);
607 }
608 /* }}} */
609 
610 /* {{{ proto object SplFixedArray::toArray()
611 */
SPL_METHOD(SplFixedArray,toArray)612 SPL_METHOD(SplFixedArray, toArray)
613 {
614 	spl_fixedarray_object *intern;
615 
616 	if (zend_parse_parameters_none() == FAILURE) {
617 		return;
618 	}
619 
620 	intern = Z_SPLFIXEDARRAY_P(getThis());
621 
622 	if (intern->array.size > 0) {
623 		int i = 0;
624 
625 		array_init(return_value);
626 		for (; i < intern->array.size; i++) {
627 			if (!Z_ISUNDEF(intern->array.elements[i])) {
628 				zend_hash_index_update(Z_ARRVAL_P(return_value), i, &intern->array.elements[i]);
629 				Z_TRY_ADDREF(intern->array.elements[i]);
630 			} else {
631 				zend_hash_index_update(Z_ARRVAL_P(return_value), i, &EG(uninitialized_zval));
632 			}
633 		}
634 	} else {
635 		ZVAL_EMPTY_ARRAY(return_value);
636 	}
637 }
638 /* }}} */
639 
640 /* {{{ proto object SplFixedArray::fromArray(array data[, bool save_indexes])
641 */
SPL_METHOD(SplFixedArray,fromArray)642 SPL_METHOD(SplFixedArray, fromArray)
643 {
644 	zval *data;
645 	spl_fixedarray array;
646 	spl_fixedarray_object *intern;
647 	int num;
648 	zend_bool save_indexes = 1;
649 
650 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "a|b", &data, &save_indexes) == FAILURE) {
651 		return;
652 	}
653 
654 	num = zend_hash_num_elements(Z_ARRVAL_P(data));
655 
656 	if (num > 0 && save_indexes) {
657 		zval *element;
658 		zend_string *str_index;
659 		zend_ulong num_index, max_index = 0;
660 		zend_long tmp;
661 
662 		ZEND_HASH_FOREACH_KEY(Z_ARRVAL_P(data), num_index, str_index) {
663 			if (str_index != NULL || (zend_long)num_index < 0) {
664 				zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0, "array must contain only positive integer keys");
665 				return;
666 			}
667 
668 			if (num_index > max_index) {
669 				max_index = num_index;
670 			}
671 		} ZEND_HASH_FOREACH_END();
672 
673 		tmp = max_index + 1;
674 		if (tmp <= 0) {
675 			zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0, "integer overflow detected");
676 			return;
677 		}
678 		spl_fixedarray_init(&array, tmp);
679 
680 		ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(data), num_index, str_index, element) {
681 			ZVAL_COPY_DEREF(&array.elements[num_index], element);
682 		} ZEND_HASH_FOREACH_END();
683 
684 	} else if (num > 0 && !save_indexes) {
685 		zval *element;
686 		zend_long i = 0;
687 
688 		spl_fixedarray_init(&array, num);
689 
690 		ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(data), element) {
691 			ZVAL_COPY_DEREF(&array.elements[i], element);
692 			i++;
693 		} ZEND_HASH_FOREACH_END();
694 	} else {
695 		spl_fixedarray_init(&array, 0);
696 	}
697 
698 	object_init_ex(return_value, spl_ce_SplFixedArray);
699 
700 	intern = Z_SPLFIXEDARRAY_P(return_value);
701 	intern->array = array;
702 }
703 /* }}} */
704 
705 /* {{{ proto int SplFixedArray::getSize(void)
706 */
SPL_METHOD(SplFixedArray,getSize)707 SPL_METHOD(SplFixedArray, getSize)
708 {
709 	zval *object = getThis();
710 	spl_fixedarray_object *intern;
711 
712 	if (zend_parse_parameters_none() == FAILURE) {
713 		return;
714 	}
715 
716 	intern = Z_SPLFIXEDARRAY_P(object);
717 	RETURN_LONG(intern->array.size);
718 }
719 /* }}} */
720 
721 /* {{{ proto bool SplFixedArray::setSize(int size)
722 */
SPL_METHOD(SplFixedArray,setSize)723 SPL_METHOD(SplFixedArray, setSize)
724 {
725 	zval *object = getThis();
726 	spl_fixedarray_object *intern;
727 	zend_long size;
728 
729 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &size) == FAILURE) {
730 		return;
731 	}
732 
733 	if (size < 0) {
734 		zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0, "array size cannot be less than zero");
735 		return;
736 	}
737 
738 	intern = Z_SPLFIXEDARRAY_P(object);
739 
740 	spl_fixedarray_resize(&intern->array, size);
741 	RETURN_TRUE;
742 }
743 /* }}} */
744 
745 /* {{{ proto bool SplFixedArray::offsetExists(mixed $index)
746  Returns whether the requested $index exists. */
SPL_METHOD(SplFixedArray,offsetExists)747 SPL_METHOD(SplFixedArray, offsetExists)
748 {
749 	zval                  *zindex;
750 	spl_fixedarray_object  *intern;
751 
752 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &zindex) == FAILURE) {
753 		return;
754 	}
755 
756 	intern = Z_SPLFIXEDARRAY_P(getThis());
757 
758 	RETURN_BOOL(spl_fixedarray_object_has_dimension_helper(intern, zindex, 0));
759 } /* }}} */
760 
761 /* {{{ proto mixed SplFixedArray::offsetGet(mixed $index)
762  Returns the value at the specified $index. */
SPL_METHOD(SplFixedArray,offsetGet)763 SPL_METHOD(SplFixedArray, offsetGet)
764 {
765 	zval *zindex, *value;
766 	spl_fixedarray_object  *intern;
767 
768 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &zindex) == FAILURE) {
769 		return;
770 	}
771 
772 	intern = Z_SPLFIXEDARRAY_P(getThis());
773 	value = spl_fixedarray_object_read_dimension_helper(intern, zindex);
774 
775 	if (value) {
776 		ZVAL_COPY_DEREF(return_value, value);
777 	} else {
778 		RETURN_NULL();
779 	}
780 } /* }}} */
781 
782 /* {{{ proto void SplFixedArray::offsetSet(mixed $index, mixed $newval)
783  Sets the value at the specified $index to $newval. */
SPL_METHOD(SplFixedArray,offsetSet)784 SPL_METHOD(SplFixedArray, offsetSet)
785 {
786 	zval                  *zindex, *value;
787 	spl_fixedarray_object  *intern;
788 
789 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "zz", &zindex, &value) == FAILURE) {
790 		return;
791 	}
792 
793 	intern = Z_SPLFIXEDARRAY_P(getThis());
794 	spl_fixedarray_object_write_dimension_helper(intern, zindex, value);
795 
796 } /* }}} */
797 
798 /* {{{ proto void SplFixedArray::offsetUnset(mixed $index)
799  Unsets the value at the specified $index. */
SPL_METHOD(SplFixedArray,offsetUnset)800 SPL_METHOD(SplFixedArray, offsetUnset)
801 {
802 	zval                  *zindex;
803 	spl_fixedarray_object  *intern;
804 
805 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &zindex) == FAILURE) {
806 		return;
807 	}
808 
809 	intern = Z_SPLFIXEDARRAY_P(getThis());
810 	spl_fixedarray_object_unset_dimension_helper(intern, zindex);
811 
812 } /* }}} */
813 
spl_fixedarray_it_dtor(zend_object_iterator * iter)814 static void spl_fixedarray_it_dtor(zend_object_iterator *iter) /* {{{ */
815 {
816 	spl_fixedarray_it  *iterator = (spl_fixedarray_it *)iter;
817 
818 	zend_user_it_invalidate_current(iter);
819 	zval_ptr_dtor(&iterator->intern.it.data);
820 }
821 /* }}} */
822 
spl_fixedarray_it_rewind(zend_object_iterator * iter)823 static void spl_fixedarray_it_rewind(zend_object_iterator *iter) /* {{{ */
824 {
825 	spl_fixedarray_object *object = Z_SPLFIXEDARRAY_P(&iter->data);
826 
827 	if (object->flags & SPL_FIXEDARRAY_OVERLOADED_REWIND) {
828 		zend_user_it_rewind(iter);
829 	} else {
830 		object->current = 0;
831 	}
832 }
833 /* }}} */
834 
spl_fixedarray_it_valid(zend_object_iterator * iter)835 static int spl_fixedarray_it_valid(zend_object_iterator *iter) /* {{{ */
836 {
837 	spl_fixedarray_object *object = Z_SPLFIXEDARRAY_P(&iter->data);
838 
839 	if (object->flags & SPL_FIXEDARRAY_OVERLOADED_VALID) {
840 		return zend_user_it_valid(iter);
841 	}
842 
843 	if (object->current >= 0 && object->current < object->array.size) {
844 		return SUCCESS;
845 	}
846 
847 	return FAILURE;
848 }
849 /* }}} */
850 
spl_fixedarray_it_get_current_data(zend_object_iterator * iter)851 static zval *spl_fixedarray_it_get_current_data(zend_object_iterator *iter) /* {{{ */
852 {
853 	zval zindex;
854 	spl_fixedarray_object *object = Z_SPLFIXEDARRAY_P(&iter->data);
855 
856 	if (object->flags & SPL_FIXEDARRAY_OVERLOADED_CURRENT) {
857 		return zend_user_it_get_current_data(iter);
858 	} else {
859 		zval *data;
860 
861 		ZVAL_LONG(&zindex, object->current);
862 
863 		data = spl_fixedarray_object_read_dimension_helper(object, &zindex);
864 
865 		if (data == NULL) {
866 			data = &EG(uninitialized_zval);
867 		}
868 		return data;
869 	}
870 }
871 /* }}} */
872 
spl_fixedarray_it_get_current_key(zend_object_iterator * iter,zval * key)873 static void spl_fixedarray_it_get_current_key(zend_object_iterator *iter, zval *key) /* {{{ */
874 {
875 	spl_fixedarray_object *object = Z_SPLFIXEDARRAY_P(&iter->data);
876 
877 	if (object->flags & SPL_FIXEDARRAY_OVERLOADED_KEY) {
878 		zend_user_it_get_current_key(iter, key);
879 	} else {
880 		ZVAL_LONG(key, object->current);
881 	}
882 }
883 /* }}} */
884 
spl_fixedarray_it_move_forward(zend_object_iterator * iter)885 static void spl_fixedarray_it_move_forward(zend_object_iterator *iter) /* {{{ */
886 {
887 	spl_fixedarray_object *object = Z_SPLFIXEDARRAY_P(&iter->data);
888 
889 	if (object->flags & SPL_FIXEDARRAY_OVERLOADED_NEXT) {
890 		zend_user_it_move_forward(iter);
891 	} else {
892 		zend_user_it_invalidate_current(iter);
893 		object->current++;
894 	}
895 }
896 /* }}} */
897 
898 /* {{{  proto int SplFixedArray::key()
899    Return current array key */
SPL_METHOD(SplFixedArray,key)900 SPL_METHOD(SplFixedArray, key)
901 {
902 	spl_fixedarray_object *intern = Z_SPLFIXEDARRAY_P(getThis());
903 
904 	if (zend_parse_parameters_none() == FAILURE) {
905 		return;
906 	}
907 
908 	RETURN_LONG(intern->current);
909 }
910 /* }}} */
911 
912 /* {{{ proto void SplFixedArray::next()
913    Move to next entry */
SPL_METHOD(SplFixedArray,next)914 SPL_METHOD(SplFixedArray, next)
915 {
916 	spl_fixedarray_object *intern = Z_SPLFIXEDARRAY_P(getThis());
917 
918 	if (zend_parse_parameters_none() == FAILURE) {
919 		return;
920 	}
921 
922 	intern->current++;
923 }
924 /* }}} */
925 
926 /* {{{ proto bool SplFixedArray::valid()
927    Check whether the datastructure contains more entries */
SPL_METHOD(SplFixedArray,valid)928 SPL_METHOD(SplFixedArray, valid)
929 {
930 	spl_fixedarray_object *intern = Z_SPLFIXEDARRAY_P(getThis());
931 
932 	if (zend_parse_parameters_none() == FAILURE) {
933 		return;
934 	}
935 
936 	RETURN_BOOL(intern->current >= 0 && intern->current < intern->array.size);
937 }
938 /* }}} */
939 
940 /* {{{ proto void SplFixedArray::rewind()
941    Rewind the datastructure back to the start */
SPL_METHOD(SplFixedArray,rewind)942 SPL_METHOD(SplFixedArray, rewind)
943 {
944 	spl_fixedarray_object *intern = Z_SPLFIXEDARRAY_P(getThis());
945 
946 	if (zend_parse_parameters_none() == FAILURE) {
947 		return;
948 	}
949 
950 	intern->current = 0;
951 }
952 /* }}} */
953 
954 /* {{{ proto mixed|NULL SplFixedArray::current()
955    Return current datastructure entry */
SPL_METHOD(SplFixedArray,current)956 SPL_METHOD(SplFixedArray, current)
957 {
958 	zval zindex, *value;
959 	spl_fixedarray_object *intern  = Z_SPLFIXEDARRAY_P(getThis());
960 
961 	if (zend_parse_parameters_none() == FAILURE) {
962 		return;
963 	}
964 
965 	ZVAL_LONG(&zindex, intern->current);
966 
967 	value = spl_fixedarray_object_read_dimension_helper(intern, &zindex);
968 
969 	if (value) {
970 		ZVAL_COPY_DEREF(return_value, value);
971 	} else {
972 		RETURN_NULL();
973 	}
974 }
975 /* }}} */
976 
977 /* iterator handler table */
978 static const zend_object_iterator_funcs spl_fixedarray_it_funcs = {
979 	spl_fixedarray_it_dtor,
980 	spl_fixedarray_it_valid,
981 	spl_fixedarray_it_get_current_data,
982 	spl_fixedarray_it_get_current_key,
983 	spl_fixedarray_it_move_forward,
984 	spl_fixedarray_it_rewind,
985 	NULL
986 };
987 
spl_fixedarray_get_iterator(zend_class_entry * ce,zval * object,int by_ref)988 zend_object_iterator *spl_fixedarray_get_iterator(zend_class_entry *ce, zval *object, int by_ref) /* {{{ */
989 {
990 	spl_fixedarray_it *iterator;
991 
992 	if (by_ref) {
993 		zend_throw_exception(spl_ce_RuntimeException, "An iterator cannot be used with foreach by reference", 0);
994 		return NULL;
995 	}
996 
997 	iterator = emalloc(sizeof(spl_fixedarray_it));
998 
999 	zend_iterator_init((zend_object_iterator*)iterator);
1000 
1001 	ZVAL_COPY(&iterator->intern.it.data, object);
1002 	iterator->intern.it.funcs = &spl_fixedarray_it_funcs;
1003 	iterator->intern.ce = ce;
1004 	ZVAL_UNDEF(&iterator->intern.value);
1005 
1006 	return &iterator->intern.it;
1007 }
1008 /* }}} */
1009 
1010 ZEND_BEGIN_ARG_INFO_EX(arginfo_splfixedarray_construct, 0, 0, 0)
1011 	ZEND_ARG_INFO(0, size)
1012 ZEND_END_ARG_INFO()
1013 
1014 ZEND_BEGIN_ARG_INFO_EX(arginfo_fixedarray_offsetGet, 0, 0, 1)
1015 	ZEND_ARG_INFO(0, index)
1016 ZEND_END_ARG_INFO()
1017 
1018 ZEND_BEGIN_ARG_INFO_EX(arginfo_fixedarray_offsetSet, 0, 0, 2)
1019 	ZEND_ARG_INFO(0, index)
1020 	ZEND_ARG_INFO(0, newval)
1021 ZEND_END_ARG_INFO()
1022 
1023 ZEND_BEGIN_ARG_INFO(arginfo_fixedarray_setSize, 0)
1024 	ZEND_ARG_INFO(0, value)
1025 ZEND_END_ARG_INFO()
1026 
1027 ZEND_BEGIN_ARG_INFO_EX(arginfo_fixedarray_fromArray, 0, 0, 1)
1028 	ZEND_ARG_INFO(0, data)
1029 	ZEND_ARG_INFO(0, save_indexes)
1030 ZEND_END_ARG_INFO()
1031 
1032 ZEND_BEGIN_ARG_INFO(arginfo_splfixedarray_void, 0)
1033 ZEND_END_ARG_INFO()
1034 
1035 static const zend_function_entry spl_funcs_SplFixedArray[] = { /* {{{ */
1036 	SPL_ME(SplFixedArray, __construct,     arginfo_splfixedarray_construct,ZEND_ACC_PUBLIC)
1037 	SPL_ME(SplFixedArray, __wakeup,        arginfo_splfixedarray_void,     ZEND_ACC_PUBLIC)
1038 	SPL_ME(SplFixedArray, count,           arginfo_splfixedarray_void,     ZEND_ACC_PUBLIC)
1039 	SPL_ME(SplFixedArray, toArray,         arginfo_splfixedarray_void,     ZEND_ACC_PUBLIC)
1040 	SPL_ME(SplFixedArray, fromArray,       arginfo_fixedarray_fromArray,   ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
1041 	SPL_ME(SplFixedArray, getSize,         arginfo_splfixedarray_void,     ZEND_ACC_PUBLIC)
1042 	SPL_ME(SplFixedArray, setSize,         arginfo_fixedarray_setSize,     ZEND_ACC_PUBLIC)
1043 	SPL_ME(SplFixedArray, offsetExists,    arginfo_fixedarray_offsetGet,   ZEND_ACC_PUBLIC)
1044 	SPL_ME(SplFixedArray, offsetGet,       arginfo_fixedarray_offsetGet,   ZEND_ACC_PUBLIC)
1045 	SPL_ME(SplFixedArray, offsetSet,       arginfo_fixedarray_offsetSet,   ZEND_ACC_PUBLIC)
1046 	SPL_ME(SplFixedArray, offsetUnset,     arginfo_fixedarray_offsetGet,   ZEND_ACC_PUBLIC)
1047 	SPL_ME(SplFixedArray, rewind,          arginfo_splfixedarray_void,     ZEND_ACC_PUBLIC)
1048 	SPL_ME(SplFixedArray, current,         arginfo_splfixedarray_void,     ZEND_ACC_PUBLIC)
1049 	SPL_ME(SplFixedArray, key,             arginfo_splfixedarray_void,     ZEND_ACC_PUBLIC)
1050 	SPL_ME(SplFixedArray, next,            arginfo_splfixedarray_void,     ZEND_ACC_PUBLIC)
1051 	SPL_ME(SplFixedArray, valid,           arginfo_splfixedarray_void,     ZEND_ACC_PUBLIC)
1052 	PHP_FE_END
1053 };
1054 /* }}} */
1055 
1056 /* {{{ PHP_MINIT_FUNCTION */
PHP_MINIT_FUNCTION(spl_fixedarray)1057 PHP_MINIT_FUNCTION(spl_fixedarray)
1058 {
1059 	REGISTER_SPL_STD_CLASS_EX(SplFixedArray, spl_fixedarray_new, spl_funcs_SplFixedArray);
1060 	memcpy(&spl_handler_SplFixedArray, &std_object_handlers, sizeof(zend_object_handlers));
1061 
1062 	spl_handler_SplFixedArray.offset          = XtOffsetOf(spl_fixedarray_object, std);
1063 	spl_handler_SplFixedArray.clone_obj       = spl_fixedarray_object_clone;
1064 	spl_handler_SplFixedArray.read_dimension  = spl_fixedarray_object_read_dimension;
1065 	spl_handler_SplFixedArray.write_dimension = spl_fixedarray_object_write_dimension;
1066 	spl_handler_SplFixedArray.unset_dimension = spl_fixedarray_object_unset_dimension;
1067 	spl_handler_SplFixedArray.has_dimension   = spl_fixedarray_object_has_dimension;
1068 	spl_handler_SplFixedArray.count_elements  = spl_fixedarray_object_count_elements;
1069 	spl_handler_SplFixedArray.get_properties  = spl_fixedarray_object_get_properties;
1070 	spl_handler_SplFixedArray.get_gc          = spl_fixedarray_object_get_gc;
1071 	spl_handler_SplFixedArray.dtor_obj        = zend_objects_destroy_object;
1072 	spl_handler_SplFixedArray.free_obj        = spl_fixedarray_object_free_storage;
1073 
1074 	REGISTER_SPL_IMPLEMENTS(SplFixedArray, Iterator);
1075 	REGISTER_SPL_IMPLEMENTS(SplFixedArray, ArrayAccess);
1076 	REGISTER_SPL_IMPLEMENTS(SplFixedArray, Countable);
1077 
1078 	spl_ce_SplFixedArray->get_iterator = spl_fixedarray_get_iterator;
1079 
1080 	return SUCCESS;
1081 }
1082 /* }}} */
1083 
1084 
1085 /*
1086  * Local variables:
1087  * tab-width: 4
1088  * c-basic-offset: 4
1089  * End:
1090  * vim600: noet sw=4 ts=4 fdm=marker
1091  * vim<600: noet sw=4 ts=4
1092  */
1093