1 /*
2   +----------------------------------------------------------------------+
3   | Copyright (c) The PHP Group                                          |
4   +----------------------------------------------------------------------+
5   | This source file is subject to version 3.01 of the PHP license,      |
6   | that is bundled with this package in the file LICENSE, and is        |
7   | available through the world-wide-web at the following url:           |
8   | https://www.php.net/license/3_01.txt                                 |
9   | If you did not receive a copy of the PHP license and are unable to   |
10   | obtain it through the world-wide-web, please send a note to          |
11   | license@php.net so we can mail you a copy immediately.               |
12   +----------------------------------------------------------------------+
13   | Author: Antony Dovgal <tony@daylessday.org>                          |
14   |         Etienne Kneuss <colder@php.net>                              |
15   +----------------------------------------------------------------------+
16 */
17 
18 #ifdef HAVE_CONFIG_H
19 #include "config.h"
20 #endif
21 
22 #include "php.h"
23 #include "php_ini.h"
24 #include "ext/standard/info.h"
25 #include "zend_exceptions.h"
26 
27 #include "php_spl.h"
28 #include "spl_fixedarray_arginfo.h"
29 #include "spl_functions.h"
30 #include "spl_engine.h"
31 #include "spl_fixedarray.h"
32 #include "spl_exceptions.h"
33 #include "spl_iterators.h"
34 #include "ext/json/php_json.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 	/* It is possible to resize this, so this can't be combined with the object */
46 	zval *elements;
47 } spl_fixedarray;
48 
49 typedef struct _spl_fixedarray_methods {
50 	zend_function       *fptr_offset_get;
51 	zend_function       *fptr_offset_set;
52 	zend_function       *fptr_offset_has;
53 	zend_function       *fptr_offset_del;
54 	zend_function       *fptr_count;
55 } spl_fixedarray_methods;
56 
57 typedef struct _spl_fixedarray_object {
58 	spl_fixedarray          array;
59 	spl_fixedarray_methods *methods;
60 	zend_object             std;
61 } spl_fixedarray_object;
62 
63 typedef struct _spl_fixedarray_it {
64 	zend_object_iterator intern;
65 	zend_long            current;
66 } spl_fixedarray_it;
67 
spl_fixed_array_from_obj(zend_object * obj)68 static spl_fixedarray_object *spl_fixed_array_from_obj(zend_object *obj)
69 {
70 	return (spl_fixedarray_object*)((char*)(obj) - XtOffsetOf(spl_fixedarray_object, std));
71 }
72 
73 #define Z_SPLFIXEDARRAY_P(zv)  spl_fixed_array_from_obj(Z_OBJ_P((zv)))
74 
75 /* Helps enforce the invariants in debug mode:
76  *   - if size == 0, then elements == NULL
77  *   - if size > 0, then elements != NULL
78  *   - size is not less than 0
79  */
spl_fixedarray_empty(spl_fixedarray * array)80 static bool spl_fixedarray_empty(spl_fixedarray *array)
81 {
82 	if (array->elements) {
83 		ZEND_ASSERT(array->size > 0);
84 		return false;
85 	}
86 	ZEND_ASSERT(array->size == 0);
87 	return true;
88 }
89 
spl_fixedarray_default_ctor(spl_fixedarray * array)90 static void spl_fixedarray_default_ctor(spl_fixedarray *array)
91 {
92 	array->size = 0;
93 	array->elements = NULL;
94 }
95 
96 /* Initializes the range [from, to) to null. Does not dtor existing elements. */
spl_fixedarray_init_elems(spl_fixedarray * array,zend_long from,zend_long to)97 static void spl_fixedarray_init_elems(spl_fixedarray *array, zend_long from, zend_long to)
98 {
99 	ZEND_ASSERT(from <= to);
100 	zval *begin = array->elements + from, *end = array->elements + to;
101 
102 	while (begin != end) {
103 		ZVAL_NULL(begin++);
104 	}
105 }
106 
spl_fixedarray_init(spl_fixedarray * array,zend_long size)107 static void spl_fixedarray_init(spl_fixedarray *array, zend_long size)
108 {
109 	if (size > 0) {
110 		array->size = 0; /* reset size in case ecalloc() fails */
111 		array->elements = safe_emalloc(size, sizeof(zval), 0);
112 		array->size = size;
113 		spl_fixedarray_init_elems(array, 0, size);
114 	} else {
115 		spl_fixedarray_default_ctor(array);
116 	}
117 }
118 
119 /* Copies the range [begin, end) into the fixedarray, beginning at `offset`.
120  * Does not dtor the existing elements.
121  */
spl_fixedarray_copy_range(spl_fixedarray * array,zend_long offset,zval * begin,zval * end)122 static void spl_fixedarray_copy_range(spl_fixedarray *array, zend_long offset, zval *begin, zval *end)
123 {
124 	ZEND_ASSERT(offset >= 0);
125 	ZEND_ASSERT(array->size - offset >= end - begin);
126 
127 	zval *to = &array->elements[offset];
128 	while (begin != end) {
129 		ZVAL_COPY(to++, begin++);
130 	}
131 }
132 
spl_fixedarray_copy_ctor(spl_fixedarray * to,spl_fixedarray * from)133 static void spl_fixedarray_copy_ctor(spl_fixedarray *to, spl_fixedarray *from)
134 {
135 	zend_long size = from->size;
136 	spl_fixedarray_init(to, size);
137 	if (size != 0) {
138 		zval *begin = from->elements, *end = from->elements + size;
139 		spl_fixedarray_copy_range(to, 0, begin, end);
140 	}
141 }
142 
143 /* Destructs the elements in the range [from, to).
144  * Caller is expected to bounds check.
145  */
spl_fixedarray_dtor_range(spl_fixedarray * array,zend_long from,zend_long to)146 static void spl_fixedarray_dtor_range(spl_fixedarray *array, zend_long from, zend_long to)
147 {
148 	zval *begin = array->elements + from, *end = array->elements + to;
149 	while (begin != end) {
150 		zval_ptr_dtor(begin++);
151 	}
152 }
153 
154 /* Destructs and frees contents but not the array itself.
155  * If you want to re-use the array then you need to re-initialize it.
156  */
spl_fixedarray_dtor(spl_fixedarray * array)157 static void spl_fixedarray_dtor(spl_fixedarray *array)
158 {
159 	if (!spl_fixedarray_empty(array)) {
160 		zval *begin = array->elements, *end = array->elements + array->size;
161 		array->elements = NULL;
162 		array->size = 0;
163 		while (begin != end) {
164 			zval_ptr_dtor(--end);
165 		}
166 		efree(begin);
167 	}
168 }
169 
spl_fixedarray_resize(spl_fixedarray * array,zend_long size)170 static void spl_fixedarray_resize(spl_fixedarray *array, zend_long size)
171 {
172 	if (size == array->size) {
173 		/* nothing to do */
174 		return;
175 	}
176 
177 	/* first initialization */
178 	if (array->size == 0) {
179 		spl_fixedarray_init(array, size);
180 		return;
181 	}
182 
183 	/* clearing the array */
184 	if (size == 0) {
185 		spl_fixedarray_dtor(array);
186 		array->elements = NULL;
187 	} else if (size > array->size) {
188 		array->elements = safe_erealloc(array->elements, size, sizeof(zval), 0);
189 		spl_fixedarray_init_elems(array, array->size, size);
190 	} else { /* size < array->size */
191 		spl_fixedarray_dtor_range(array, size, array->size);
192 		array->elements = erealloc(array->elements, sizeof(zval) * size);
193 	}
194 
195 	array->size = size;
196 }
197 
spl_fixedarray_object_get_gc(zend_object * obj,zval ** table,int * n)198 static HashTable* spl_fixedarray_object_get_gc(zend_object *obj, zval **table, int *n)
199 {
200 	spl_fixedarray_object *intern = spl_fixed_array_from_obj(obj);
201 	HashTable *ht = zend_std_get_properties(obj);
202 
203 	*table = intern->array.elements;
204 	*n = (int)intern->array.size;
205 
206 	return ht;
207 }
208 
spl_fixedarray_object_get_properties(zend_object * obj)209 static HashTable* spl_fixedarray_object_get_properties(zend_object *obj)
210 {
211 	spl_fixedarray_object *intern = spl_fixed_array_from_obj(obj);
212 	HashTable *ht = zend_std_get_properties(obj);
213 
214 	if (!spl_fixedarray_empty(&intern->array)) {
215 		zend_long j = zend_hash_num_elements(ht);
216 
217 		for (zend_long i = 0; i < intern->array.size; i++) {
218 			zend_hash_index_update(ht, i, &intern->array.elements[i]);
219 			Z_TRY_ADDREF(intern->array.elements[i]);
220 		}
221 		if (j > intern->array.size) {
222 			for (zend_long i = intern->array.size; i < j; ++i) {
223 				zend_hash_index_del(ht, i);
224 			}
225 		}
226 	}
227 
228 	return ht;
229 }
230 
spl_fixedarray_object_free_storage(zend_object * object)231 static void spl_fixedarray_object_free_storage(zend_object *object)
232 {
233 	spl_fixedarray_object *intern = spl_fixed_array_from_obj(object);
234 	spl_fixedarray_dtor(&intern->array);
235 	zend_object_std_dtor(&intern->std);
236 	if (intern->methods) {
237 		efree(intern->methods);
238 	}
239 }
240 
spl_fixedarray_object_new_ex(zend_class_entry * class_type,zend_object * orig,bool clone_orig)241 static zend_object *spl_fixedarray_object_new_ex(zend_class_entry *class_type, zend_object *orig, bool clone_orig)
242 {
243 	spl_fixedarray_object *intern;
244 	zend_class_entry      *parent = class_type;
245 	bool                   inherited = false;
246 
247 	intern = zend_object_alloc(sizeof(spl_fixedarray_object), parent);
248 
249 	zend_object_std_init(&intern->std, class_type);
250 	object_properties_init(&intern->std, class_type);
251 
252 	if (orig && clone_orig) {
253 		spl_fixedarray_object *other = spl_fixed_array_from_obj(orig);
254 		spl_fixedarray_copy_ctor(&intern->array, &other->array);
255 	}
256 
257 	while (parent) {
258 		if (parent == spl_ce_SplFixedArray) {
259 			intern->std.handlers = &spl_handler_SplFixedArray;
260 			break;
261 		}
262 
263 		parent = parent->parent;
264 		inherited = true;
265 	}
266 
267 	ZEND_ASSERT(parent);
268 
269 	if (UNEXPECTED(inherited)) {
270 		spl_fixedarray_methods methods;
271 		methods.fptr_offset_get = zend_hash_str_find_ptr(&class_type->function_table, "offsetget", sizeof("offsetget") - 1);
272 		if (methods.fptr_offset_get->common.scope == parent) {
273 			methods.fptr_offset_get = NULL;
274 		}
275 		methods.fptr_offset_set = zend_hash_str_find_ptr(&class_type->function_table, "offsetset", sizeof("offsetset") - 1);
276 		if (methods.fptr_offset_set->common.scope == parent) {
277 			methods.fptr_offset_set = NULL;
278 		}
279 		methods.fptr_offset_has = zend_hash_str_find_ptr(&class_type->function_table, "offsetexists", sizeof("offsetexists") - 1);
280 		if (methods.fptr_offset_has->common.scope == parent) {
281 			methods.fptr_offset_has = NULL;
282 		}
283 		methods.fptr_offset_del = zend_hash_str_find_ptr(&class_type->function_table, "offsetunset", sizeof("offsetunset") - 1);
284 		if (methods.fptr_offset_del->common.scope == parent) {
285 			methods.fptr_offset_del = NULL;
286 		}
287 		methods.fptr_count = zend_hash_str_find_ptr(&class_type->function_table, "count", sizeof("count") - 1);
288 		if (methods.fptr_count->common.scope == parent) {
289 			methods.fptr_count = NULL;
290 		}
291 		/* Assume that most of the time in performance-sensitive code, SplFixedArray won't be subclassed with overrides for these ArrayAccess methods. */
292 		/* Save 32 bytes per object on 64-bit systems by combining the 5 null pointers into 1 null pointer */
293 		/* (This is already looking up 5 functions when any subclass of SplFixedArray is instantiated, which is inefficient) */
294 		if (methods.fptr_offset_get || methods.fptr_offset_set || methods.fptr_offset_del || methods.fptr_offset_has || methods.fptr_count) {
295 			intern->methods = emalloc(sizeof(spl_fixedarray_methods));
296 			*intern->methods = methods;
297 		}
298 	}
299 
300 	return &intern->std;
301 }
302 
spl_fixedarray_new(zend_class_entry * class_type)303 static zend_object *spl_fixedarray_new(zend_class_entry *class_type)
304 {
305 	return spl_fixedarray_object_new_ex(class_type, NULL, 0);
306 }
307 
spl_fixedarray_object_clone(zend_object * old_object)308 static zend_object *spl_fixedarray_object_clone(zend_object *old_object)
309 {
310 	zend_object *new_object = spl_fixedarray_object_new_ex(old_object->ce, old_object, 1);
311 
312 	zend_objects_clone_members(new_object, old_object);
313 
314 	return new_object;
315 }
316 
spl_offset_convert_to_long(zval * offset)317 static zend_long spl_offset_convert_to_long(zval *offset) /* {{{ */
318 {
319 	try_again:
320 	switch (Z_TYPE_P(offset)) {
321 		case IS_STRING: {
322 			zend_ulong index;
323 			if (ZEND_HANDLE_NUMERIC(Z_STR_P(offset), index)) {
324 				return (zend_long) index;
325 			}
326 			break;
327 		}
328 		case IS_DOUBLE:
329 			return zend_dval_to_lval_safe(Z_DVAL_P(offset));
330 		case IS_LONG:
331 			return Z_LVAL_P(offset);
332 		case IS_FALSE:
333 			return 0;
334 		case IS_TRUE:
335 			return 1;
336 		case IS_REFERENCE:
337 			offset = Z_REFVAL_P(offset);
338 			goto try_again;
339 		case IS_RESOURCE:
340 			zend_use_resource_as_offset(offset);
341 			return Z_RES_HANDLE_P(offset);
342 	}
343 
344 	zend_type_error("Illegal offset type");
345 	return 0;
346 }
347 
spl_fixedarray_object_read_dimension_helper(spl_fixedarray_object * intern,zval * offset)348 static zval *spl_fixedarray_object_read_dimension_helper(spl_fixedarray_object *intern, zval *offset)
349 {
350 	zend_long index;
351 
352 	/* we have to return NULL on error here to avoid memleak because of
353 	 * ZE duplicating uninitialized_zval_ptr */
354 	if (!offset) {
355 		zend_throw_error(NULL, "[] operator not supported for SplFixedArray");
356 		return NULL;
357 	}
358 
359 	index = spl_offset_convert_to_long(offset);
360 	if (EG(exception)) {
361 		return NULL;
362 	}
363 
364 	if (index < 0 || index >= intern->array.size) {
365 		// TODO Change error message and use OutOfBound SPL Exception?
366 		zend_throw_exception(spl_ce_RuntimeException, "Index invalid or out of range", 0);
367 		return NULL;
368 	} else {
369 		return &intern->array.elements[index];
370 	}
371 }
372 
373 static int spl_fixedarray_object_has_dimension(zend_object *object, zval *offset, int check_empty);
374 
spl_fixedarray_object_read_dimension(zend_object * object,zval * offset,int type,zval * rv)375 static zval *spl_fixedarray_object_read_dimension(zend_object *object, zval *offset, int type, zval *rv)
376 {
377 	spl_fixedarray_object *intern;
378 
379 	intern = spl_fixed_array_from_obj(object);
380 
381 	if (type == BP_VAR_IS && !spl_fixedarray_object_has_dimension(object, offset, 0)) {
382 		return &EG(uninitialized_zval);
383 	}
384 
385 	if (UNEXPECTED(intern->methods && intern->methods->fptr_offset_get)) {
386 		zval tmp;
387 		if (!offset) {
388 			ZVAL_NULL(&tmp);
389 			offset = &tmp;
390 		}
391 		zend_call_method_with_1_params(object, intern->std.ce, &intern->methods->fptr_offset_get, "offsetGet", rv, offset);
392 		if (!Z_ISUNDEF_P(rv)) {
393 			return rv;
394 		}
395 		return &EG(uninitialized_zval);
396 	}
397 
398 	return spl_fixedarray_object_read_dimension_helper(intern, offset);
399 }
400 
spl_fixedarray_object_write_dimension_helper(spl_fixedarray_object * intern,zval * offset,zval * value)401 static void spl_fixedarray_object_write_dimension_helper(spl_fixedarray_object *intern, zval *offset, zval *value)
402 {
403 	zend_long index;
404 
405 	if (!offset) {
406 		/* '$array[] = value' syntax is not supported */
407 		zend_throw_error(NULL, "[] operator not supported for SplFixedArray");
408 		return;
409 	}
410 
411 	index = spl_offset_convert_to_long(offset);
412 	if (EG(exception)) {
413 		return;
414 	}
415 
416 	if (index < 0 || index >= intern->array.size) {
417 		// TODO Change error message and use OutOfBound SPL Exception?
418 		zend_throw_exception(spl_ce_RuntimeException, "Index invalid or out of range", 0);
419 		return;
420 	} else {
421 		/* Fix #81429 */
422 		zval *ptr = &(intern->array.elements[index]);
423 		zval tmp;
424 		ZVAL_COPY_VALUE(&tmp, ptr);
425 		ZVAL_COPY_DEREF(ptr, value);
426 		zval_ptr_dtor(&tmp);
427 	}
428 }
429 
spl_fixedarray_object_write_dimension(zend_object * object,zval * offset,zval * value)430 static void spl_fixedarray_object_write_dimension(zend_object *object, zval *offset, zval *value)
431 {
432 	spl_fixedarray_object *intern;
433 	zval tmp;
434 
435 	intern = spl_fixed_array_from_obj(object);
436 
437 	if (UNEXPECTED(intern->methods && intern->methods->fptr_offset_set)) {
438 		if (!offset) {
439 			ZVAL_NULL(&tmp);
440 			offset = &tmp;
441 		}
442 		zend_call_method_with_2_params(object, intern->std.ce, &intern->methods->fptr_offset_set, "offsetSet", NULL, offset, value);
443 		return;
444 	}
445 
446 	spl_fixedarray_object_write_dimension_helper(intern, offset, value);
447 }
448 
spl_fixedarray_object_unset_dimension_helper(spl_fixedarray_object * intern,zval * offset)449 static void spl_fixedarray_object_unset_dimension_helper(spl_fixedarray_object *intern, zval *offset)
450 {
451 	zend_long index;
452 
453 	index = spl_offset_convert_to_long(offset);
454 	if (EG(exception)) {
455 		return;
456 	}
457 
458 	if (index < 0 || index >= intern->array.size) {
459 		// TODO Change error message and use OutOfBound SPL Exception?
460 		zend_throw_exception(spl_ce_RuntimeException, "Index invalid or out of range", 0);
461 		return;
462 	} else {
463 		zval_ptr_dtor(&(intern->array.elements[index]));
464 		ZVAL_NULL(&intern->array.elements[index]);
465 	}
466 }
467 
spl_fixedarray_object_unset_dimension(zend_object * object,zval * offset)468 static void spl_fixedarray_object_unset_dimension(zend_object *object, zval *offset)
469 {
470 	spl_fixedarray_object *intern;
471 
472 	intern = spl_fixed_array_from_obj(object);
473 
474 	if (UNEXPECTED(intern->methods && intern->methods->fptr_offset_del)) {
475 		zend_call_method_with_1_params(object, intern->std.ce, &intern->methods->fptr_offset_del, "offsetUnset", NULL, offset);
476 		return;
477 	}
478 
479 	spl_fixedarray_object_unset_dimension_helper(intern, offset);
480 }
481 
spl_fixedarray_object_has_dimension_helper(spl_fixedarray_object * intern,zval * offset,bool check_empty)482 static bool spl_fixedarray_object_has_dimension_helper(spl_fixedarray_object *intern, zval *offset, bool check_empty)
483 {
484 	zend_long index;
485 
486 	index = spl_offset_convert_to_long(offset);
487 	if (EG(exception)) {
488 		return false;
489 	}
490 
491 	if (index < 0 || index >= intern->array.size) {
492 		return false;
493 	}
494 
495 	if (check_empty) {
496 		return zend_is_true(&intern->array.elements[index]);
497 	}
498 
499 	return Z_TYPE(intern->array.elements[index]) != IS_NULL;
500 }
501 
spl_fixedarray_object_has_dimension(zend_object * object,zval * offset,int check_empty)502 static int spl_fixedarray_object_has_dimension(zend_object *object, zval *offset, int check_empty)
503 {
504 	spl_fixedarray_object *intern;
505 
506 	intern = spl_fixed_array_from_obj(object);
507 
508 	if (UNEXPECTED(intern->methods && intern->methods->fptr_offset_has)) {
509 		zval rv;
510 		bool result;
511 
512 		zend_call_method_with_1_params(object, intern->std.ce, &intern->methods->fptr_offset_has, "offsetExists", &rv, offset);
513 		result = zend_is_true(&rv);
514 		zval_ptr_dtor(&rv);
515 		return result;
516 	}
517 
518 	return spl_fixedarray_object_has_dimension_helper(intern, offset, check_empty);
519 }
520 
spl_fixedarray_object_count_elements(zend_object * object,zend_long * count)521 static int spl_fixedarray_object_count_elements(zend_object *object, zend_long *count)
522 {
523 	spl_fixedarray_object *intern;
524 
525 	intern = spl_fixed_array_from_obj(object);
526 	if (UNEXPECTED(intern->methods && intern->methods->fptr_count)) {
527 		zval rv;
528 		zend_call_method_with_0_params(object, intern->std.ce, &intern->methods->fptr_count, "count", &rv);
529 		if (!Z_ISUNDEF(rv)) {
530 			*count = zval_get_long(&rv);
531 			zval_ptr_dtor(&rv);
532 		} else {
533 			*count = 0;
534 		}
535 	} else {
536 		*count = intern->array.size;
537 	}
538 	return SUCCESS;
539 }
540 
PHP_METHOD(SplFixedArray,__construct)541 PHP_METHOD(SplFixedArray, __construct)
542 {
543 	zval *object = ZEND_THIS;
544 	spl_fixedarray_object *intern;
545 	zend_long size = 0;
546 
547 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "|l", &size) == FAILURE) {
548 		RETURN_THROWS();
549 	}
550 
551 	if (size < 0) {
552 		zend_argument_value_error(1, "must be greater than or equal to 0");
553 		RETURN_THROWS();
554 	}
555 
556 	intern = Z_SPLFIXEDARRAY_P(object);
557 
558 	if (!spl_fixedarray_empty(&intern->array)) {
559 		/* called __construct() twice, bail out */
560 		return;
561 	}
562 
563 	spl_fixedarray_init(&intern->array, size);
564 }
565 
PHP_METHOD(SplFixedArray,__wakeup)566 PHP_METHOD(SplFixedArray, __wakeup)
567 {
568 	spl_fixedarray_object *intern = Z_SPLFIXEDARRAY_P(ZEND_THIS);
569 	HashTable *intern_ht = zend_std_get_properties(Z_OBJ_P(ZEND_THIS));
570 	zval *data;
571 
572 	if (zend_parse_parameters_none() == FAILURE) {
573 		RETURN_THROWS();
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 
PHP_METHOD(SplFixedArray,count)593 PHP_METHOD(SplFixedArray, count)
594 {
595 	zval *object = ZEND_THIS;
596 	spl_fixedarray_object *intern;
597 
598 	if (zend_parse_parameters_none() == FAILURE) {
599 		RETURN_THROWS();
600 	}
601 
602 	intern = Z_SPLFIXEDARRAY_P(object);
603 	RETURN_LONG(intern->array.size);
604 }
605 
PHP_METHOD(SplFixedArray,toArray)606 PHP_METHOD(SplFixedArray, toArray)
607 {
608 	spl_fixedarray_object *intern;
609 
610 	if (zend_parse_parameters_none() == FAILURE) {
611 		RETURN_THROWS();
612 	}
613 
614 	intern = Z_SPLFIXEDARRAY_P(ZEND_THIS);
615 
616 	if (!spl_fixedarray_empty(&intern->array)) {
617 		array_init(return_value);
618 		for (zend_long i = 0; i < intern->array.size; i++) {
619 			zend_hash_index_update(Z_ARRVAL_P(return_value), i, &intern->array.elements[i]);
620 			Z_TRY_ADDREF(intern->array.elements[i]);
621 		}
622 	} else {
623 		RETURN_EMPTY_ARRAY();
624 	}
625 }
626 
PHP_METHOD(SplFixedArray,fromArray)627 PHP_METHOD(SplFixedArray, fromArray)
628 {
629 	zval *data;
630 	spl_fixedarray array;
631 	spl_fixedarray_object *intern;
632 	int num;
633 	bool save_indexes = 1;
634 
635 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "a|b", &data, &save_indexes) == FAILURE) {
636 		RETURN_THROWS();
637 	}
638 
639 	num = zend_hash_num_elements(Z_ARRVAL_P(data));
640 
641 	if (num > 0 && save_indexes) {
642 		zval *element;
643 		zend_string *str_index;
644 		zend_ulong num_index, max_index = 0;
645 		zend_long tmp;
646 
647 		ZEND_HASH_FOREACH_KEY(Z_ARRVAL_P(data), num_index, str_index) {
648 			if (str_index != NULL || (zend_long)num_index < 0) {
649 				zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0, "array must contain only positive integer keys");
650 				RETURN_THROWS();
651 			}
652 
653 			if (num_index > max_index) {
654 				max_index = num_index;
655 			}
656 		} ZEND_HASH_FOREACH_END();
657 
658 		tmp = max_index + 1;
659 		if (tmp <= 0) {
660 			zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0, "integer overflow detected");
661 			RETURN_THROWS();
662 		}
663 		spl_fixedarray_init(&array, tmp);
664 
665 		ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(data), num_index, str_index, element) {
666 			ZVAL_COPY_DEREF(&array.elements[num_index], element);
667 		} ZEND_HASH_FOREACH_END();
668 
669 	} else if (num > 0 && !save_indexes) {
670 		zval *element;
671 		zend_long i = 0;
672 
673 		spl_fixedarray_init(&array, num);
674 
675 		ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(data), element) {
676 			ZVAL_COPY_DEREF(&array.elements[i], element);
677 			i++;
678 		} ZEND_HASH_FOREACH_END();
679 	} else {
680 		spl_fixedarray_init(&array, 0);
681 	}
682 
683 	object_init_ex(return_value, spl_ce_SplFixedArray);
684 
685 	intern = Z_SPLFIXEDARRAY_P(return_value);
686 	intern->array = array;
687 }
688 
PHP_METHOD(SplFixedArray,getSize)689 PHP_METHOD(SplFixedArray, getSize)
690 {
691 	zval *object = ZEND_THIS;
692 	spl_fixedarray_object *intern;
693 
694 	if (zend_parse_parameters_none() == FAILURE) {
695 		RETURN_THROWS();
696 	}
697 
698 	intern = Z_SPLFIXEDARRAY_P(object);
699 	RETURN_LONG(intern->array.size);
700 }
701 
PHP_METHOD(SplFixedArray,setSize)702 PHP_METHOD(SplFixedArray, setSize)
703 {
704 	zval *object = ZEND_THIS;
705 	spl_fixedarray_object *intern;
706 	zend_long size;
707 
708 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &size) == FAILURE) {
709 		RETURN_THROWS();
710 	}
711 
712 	if (size < 0) {
713 		zend_argument_value_error(1, "must be greater than or equal to 0");
714 		RETURN_THROWS();
715 	}
716 
717 	intern = Z_SPLFIXEDARRAY_P(object);
718 
719 	spl_fixedarray_resize(&intern->array, size);
720 	RETURN_TRUE;
721 }
722 
723 /* Returns whether the requested $index exists. */
PHP_METHOD(SplFixedArray,offsetExists)724 PHP_METHOD(SplFixedArray, offsetExists)
725 {
726 	zval                  *zindex;
727 	spl_fixedarray_object  *intern;
728 
729 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &zindex) == FAILURE) {
730 		RETURN_THROWS();
731 	}
732 
733 	intern = Z_SPLFIXEDARRAY_P(ZEND_THIS);
734 
735 	RETURN_BOOL(spl_fixedarray_object_has_dimension_helper(intern, zindex, 0));
736 }
737 
738 /* Returns the value at the specified $index. */
PHP_METHOD(SplFixedArray,offsetGet)739 PHP_METHOD(SplFixedArray, offsetGet)
740 {
741 	zval *zindex, *value;
742 	spl_fixedarray_object  *intern;
743 
744 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &zindex) == FAILURE) {
745 		RETURN_THROWS();
746 	}
747 
748 	intern = Z_SPLFIXEDARRAY_P(ZEND_THIS);
749 	value = spl_fixedarray_object_read_dimension_helper(intern, zindex);
750 
751 	if (value) {
752 		RETURN_COPY_DEREF(value);
753 	} else {
754 		RETURN_NULL();
755 	}
756 }
757 
758 /* Sets the value at the specified $index to $newval. */
PHP_METHOD(SplFixedArray,offsetSet)759 PHP_METHOD(SplFixedArray, offsetSet)
760 {
761 	zval                  *zindex, *value;
762 	spl_fixedarray_object  *intern;
763 
764 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "zz", &zindex, &value) == FAILURE) {
765 		RETURN_THROWS();
766 	}
767 
768 	intern = Z_SPLFIXEDARRAY_P(ZEND_THIS);
769 	spl_fixedarray_object_write_dimension_helper(intern, zindex, value);
770 
771 }
772 
773 /* Unsets the value at the specified $index. */
PHP_METHOD(SplFixedArray,offsetUnset)774 PHP_METHOD(SplFixedArray, offsetUnset)
775 {
776 	zval                  *zindex;
777 	spl_fixedarray_object  *intern;
778 
779 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &zindex) == FAILURE) {
780 		RETURN_THROWS();
781 	}
782 
783 	intern = Z_SPLFIXEDARRAY_P(ZEND_THIS);
784 	spl_fixedarray_object_unset_dimension_helper(intern, zindex);
785 
786 }
787 
788 /* Create a new iterator from a SplFixedArray instance. */
PHP_METHOD(SplFixedArray,getIterator)789 PHP_METHOD(SplFixedArray, getIterator)
790 {
791 	if (zend_parse_parameters_none() == FAILURE) {
792 		return;
793 	}
794 
795 	zend_create_internal_iterator_zval(return_value, ZEND_THIS);
796 }
797 
PHP_METHOD(SplFixedArray,jsonSerialize)798 PHP_METHOD(SplFixedArray, jsonSerialize)
799 {
800 	ZEND_PARSE_PARAMETERS_NONE();
801 
802 	spl_fixedarray_object *intern = Z_SPLFIXEDARRAY_P(ZEND_THIS);
803 	array_init_size(return_value, intern->array.size);
804 	for (zend_long i = 0; i < intern->array.size; i++) {
805 		zend_hash_next_index_insert_new(Z_ARR_P(return_value), &intern->array.elements[i]);
806 		Z_TRY_ADDREF(intern->array.elements[i]);
807 	}
808 }
809 
spl_fixedarray_it_dtor(zend_object_iterator * iter)810 static void spl_fixedarray_it_dtor(zend_object_iterator *iter)
811 {
812 	zval_ptr_dtor(&iter->data);
813 }
814 
spl_fixedarray_it_rewind(zend_object_iterator * iter)815 static void spl_fixedarray_it_rewind(zend_object_iterator *iter)
816 {
817 	((spl_fixedarray_it*)iter)->current = 0;
818 }
819 
spl_fixedarray_it_valid(zend_object_iterator * iter)820 static int spl_fixedarray_it_valid(zend_object_iterator *iter)
821 {
822 	spl_fixedarray_it     *iterator = (spl_fixedarray_it*)iter;
823 	spl_fixedarray_object *object   = Z_SPLFIXEDARRAY_P(&iter->data);
824 
825 	if (iterator->current >= 0 && iterator->current < object->array.size) {
826 		return SUCCESS;
827 	}
828 
829 	return FAILURE;
830 }
831 
spl_fixedarray_it_get_current_data(zend_object_iterator * iter)832 static zval *spl_fixedarray_it_get_current_data(zend_object_iterator *iter)
833 {
834 	zval zindex, *data;
835 	spl_fixedarray_it     *iterator = (spl_fixedarray_it*)iter;
836 	spl_fixedarray_object *object   = Z_SPLFIXEDARRAY_P(&iter->data);
837 
838 	ZVAL_LONG(&zindex, iterator->current);
839 	data = spl_fixedarray_object_read_dimension_helper(object, &zindex);
840 
841 	if (data == NULL) {
842 		data = &EG(uninitialized_zval);
843 	}
844 	return data;
845 }
846 
spl_fixedarray_it_get_current_key(zend_object_iterator * iter,zval * key)847 static void spl_fixedarray_it_get_current_key(zend_object_iterator *iter, zval *key)
848 {
849 	ZVAL_LONG(key, ((spl_fixedarray_it*)iter)->current);
850 }
851 
spl_fixedarray_it_move_forward(zend_object_iterator * iter)852 static void spl_fixedarray_it_move_forward(zend_object_iterator *iter)
853 {
854 	((spl_fixedarray_it*)iter)->current++;
855 }
856 
857 /* iterator handler table */
858 static const zend_object_iterator_funcs spl_fixedarray_it_funcs = {
859 	spl_fixedarray_it_dtor,
860 	spl_fixedarray_it_valid,
861 	spl_fixedarray_it_get_current_data,
862 	spl_fixedarray_it_get_current_key,
863 	spl_fixedarray_it_move_forward,
864 	spl_fixedarray_it_rewind,
865 	NULL,
866 	NULL, /* get_gc */
867 };
868 
spl_fixedarray_get_iterator(zend_class_entry * ce,zval * object,int by_ref)869 zend_object_iterator *spl_fixedarray_get_iterator(zend_class_entry *ce, zval *object, int by_ref)
870 {
871 	spl_fixedarray_it *iterator;
872 
873 	if (by_ref) {
874 		zend_throw_error(NULL, "An iterator cannot be used with foreach by reference");
875 		return NULL;
876 	}
877 
878 	iterator = emalloc(sizeof(spl_fixedarray_it));
879 
880 	zend_iterator_init((zend_object_iterator*)iterator);
881 
882 	ZVAL_OBJ_COPY(&iterator->intern.data, Z_OBJ_P(object));
883 	iterator->intern.funcs = &spl_fixedarray_it_funcs;
884 
885 	return &iterator->intern;
886 }
887 
PHP_MINIT_FUNCTION(spl_fixedarray)888 PHP_MINIT_FUNCTION(spl_fixedarray)
889 {
890 	spl_ce_SplFixedArray = register_class_SplFixedArray(
891 		zend_ce_aggregate, zend_ce_arrayaccess, zend_ce_countable, php_json_serializable_ce);
892 	spl_ce_SplFixedArray->create_object = spl_fixedarray_new;
893 	spl_ce_SplFixedArray->get_iterator = spl_fixedarray_get_iterator;
894 	spl_ce_SplFixedArray->ce_flags |= ZEND_ACC_REUSE_GET_ITERATOR;
895 
896 	memcpy(&spl_handler_SplFixedArray, &std_object_handlers, sizeof(zend_object_handlers));
897 
898 	spl_handler_SplFixedArray.offset          = XtOffsetOf(spl_fixedarray_object, std);
899 	spl_handler_SplFixedArray.clone_obj       = spl_fixedarray_object_clone;
900 	spl_handler_SplFixedArray.read_dimension  = spl_fixedarray_object_read_dimension;
901 	spl_handler_SplFixedArray.write_dimension = spl_fixedarray_object_write_dimension;
902 	spl_handler_SplFixedArray.unset_dimension = spl_fixedarray_object_unset_dimension;
903 	spl_handler_SplFixedArray.has_dimension   = spl_fixedarray_object_has_dimension;
904 	spl_handler_SplFixedArray.count_elements  = spl_fixedarray_object_count_elements;
905 	spl_handler_SplFixedArray.get_properties  = spl_fixedarray_object_get_properties;
906 	spl_handler_SplFixedArray.get_gc          = spl_fixedarray_object_get_gc;
907 	spl_handler_SplFixedArray.free_obj        = spl_fixedarray_object_free_storage;
908 
909 	return SUCCESS;
910 }
911