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 "zend_interfaces.h"
24 #include "zend_exceptions.h"
25 #include "zend_attributes.h"
26
27 #include "spl_fixedarray_arginfo.h"
28 #include "spl_fixedarray.h"
29 #include "spl_exceptions.h"
30 #include "ext/json/php_json.h" /* For php_json_serializable_ce */
31
32 static zend_object_handlers spl_handler_SplFixedArray;
33 PHPAPI zend_class_entry *spl_ce_SplFixedArray;
34
35 /* Check if the object is an instance of a subclass of SplFixedArray that overrides method's implementation.
36 * Expect subclassing SplFixedArray to be rare and check that first. */
37 #define HAS_FIXEDARRAY_ARRAYACCESS_OVERRIDE(object, method) UNEXPECTED((object)->ce != spl_ce_SplFixedArray && (object)->ce->arrayaccess_funcs_ptr->method->common.scope != spl_ce_SplFixedArray)
38
39 typedef struct _spl_fixedarray {
40 zend_long size;
41 /* It is possible to resize this, so this can't be combined with the object */
42 zval *elements;
43 /* If positive, it's a resize within a resize and the value gives the desired size. If -1, it's not. */
44 zend_long cached_resize;
45 } spl_fixedarray;
46
47 typedef struct _spl_fixedarray_object {
48 spl_fixedarray array;
49 zend_function *fptr_count;
50 zend_object std;
51 } spl_fixedarray_object;
52
53 typedef struct _spl_fixedarray_it {
54 zend_object_iterator intern;
55 zend_long current;
56 } spl_fixedarray_it;
57
spl_fixed_array_from_obj(zend_object * obj)58 static spl_fixedarray_object *spl_fixed_array_from_obj(zend_object *obj)
59 {
60 return (spl_fixedarray_object*)((char*)(obj) - XtOffsetOf(spl_fixedarray_object, std));
61 }
62
63 #define Z_SPLFIXEDARRAY_P(zv) spl_fixed_array_from_obj(Z_OBJ_P((zv)))
64
65 /* Helps enforce the invariants in debug mode:
66 * - if size == 0, then elements == NULL
67 * - if size > 0, then elements != NULL
68 * - size is not less than 0
69 */
spl_fixedarray_empty(spl_fixedarray * array)70 static bool spl_fixedarray_empty(spl_fixedarray *array)
71 {
72 if (array->elements) {
73 ZEND_ASSERT(array->size > 0);
74 return false;
75 }
76 ZEND_ASSERT(array->size == 0);
77 return true;
78 }
79
spl_fixedarray_default_ctor(spl_fixedarray * array)80 static void spl_fixedarray_default_ctor(spl_fixedarray *array)
81 {
82 array->size = 0;
83 array->elements = NULL;
84 array->cached_resize = -1;
85 }
86
87 /* 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)88 static void spl_fixedarray_init_elems(spl_fixedarray *array, zend_long from, zend_long to)
89 {
90 ZEND_ASSERT(from <= to);
91 zval *begin = array->elements + from, *end = array->elements + to;
92
93 while (begin != end) {
94 ZVAL_NULL(begin++);
95 }
96 }
97
spl_fixedarray_init_non_empty_struct(spl_fixedarray * array,zend_long size)98 static void spl_fixedarray_init_non_empty_struct(spl_fixedarray *array, zend_long size)
99 {
100 array->size = 0; /* reset size in case ecalloc() fails */
101 array->elements = size ? safe_emalloc(size, sizeof(zval), 0) : NULL;
102 array->size = size;
103 array->cached_resize = -1;
104 }
105
spl_fixedarray_init(spl_fixedarray * array,zend_long size)106 static void spl_fixedarray_init(spl_fixedarray *array, zend_long size)
107 {
108 if (size > 0) {
109 spl_fixedarray_init_non_empty_struct(array, size);
110 spl_fixedarray_init_elems(array, 0, size);
111 } else {
112 spl_fixedarray_default_ctor(array);
113 }
114 }
115
116 /* Copies the range [begin, end) into the fixedarray, beginning at `offset`.
117 * Does not dtor the existing elements.
118 */
spl_fixedarray_copy_range(spl_fixedarray * array,zend_long offset,zval * begin,zval * end)119 static void spl_fixedarray_copy_range(spl_fixedarray *array, zend_long offset, zval *begin, zval *end)
120 {
121 ZEND_ASSERT(offset >= 0);
122 ZEND_ASSERT(array->size - offset >= end - begin);
123
124 zval *to = &array->elements[offset];
125 while (begin != end) {
126 ZVAL_COPY(to++, begin++);
127 }
128 }
129
spl_fixedarray_copy_ctor(spl_fixedarray * to,spl_fixedarray * from)130 static void spl_fixedarray_copy_ctor(spl_fixedarray *to, spl_fixedarray *from)
131 {
132 zend_long size = from->size;
133 spl_fixedarray_init(to, size);
134 if (size != 0) {
135 zval *begin = from->elements, *end = from->elements + size;
136 spl_fixedarray_copy_range(to, 0, begin, end);
137 }
138 }
139
140 /* Destructs the elements in the range [from, to).
141 * Caller is expected to bounds check.
142 */
spl_fixedarray_dtor_range(spl_fixedarray * array,zend_long from,zend_long to)143 static void spl_fixedarray_dtor_range(spl_fixedarray *array, zend_long from, zend_long to)
144 {
145 array->size = from;
146 zval *begin = array->elements + from, *end = array->elements + to;
147 while (begin != end) {
148 zval_ptr_dtor(begin++);
149 }
150 }
151
152 /* Destructs and frees contents but not the array itself.
153 * If you want to re-use the array then you need to re-initialize it.
154 */
spl_fixedarray_dtor(spl_fixedarray * array)155 static void spl_fixedarray_dtor(spl_fixedarray *array)
156 {
157 if (!spl_fixedarray_empty(array)) {
158 zval *begin = array->elements, *end = array->elements + array->size;
159 array->elements = NULL;
160 array->size = 0;
161 while (begin != end) {
162 zval_ptr_dtor(--end);
163 }
164 efree(begin);
165 }
166 }
167
spl_fixedarray_resize(spl_fixedarray * array,zend_long size)168 static void spl_fixedarray_resize(spl_fixedarray *array, zend_long size)
169 {
170 if (size == array->size) {
171 /* nothing to do */
172 return;
173 }
174
175 /* first initialization */
176 if (array->size == 0) {
177 spl_fixedarray_init(array, size);
178 return;
179 }
180
181 if (UNEXPECTED(array->cached_resize >= 0)) {
182 /* We're already resizing, so just remember the desired size.
183 * The resize will happen later. */
184 array->cached_resize = size;
185 return;
186 }
187 array->cached_resize = size;
188
189 /* clearing the array */
190 if (size == 0) {
191 spl_fixedarray_dtor(array);
192 array->elements = NULL;
193 array->size = 0;
194 } else if (size > array->size) {
195 array->elements = safe_erealloc(array->elements, size, sizeof(zval), 0);
196 spl_fixedarray_init_elems(array, array->size, size);
197 array->size = size;
198 } else { /* size < array->size */
199 /* Size set in spl_fixedarray_dtor_range() */
200 spl_fixedarray_dtor_range(array, size, array->size);
201 array->elements = erealloc(array->elements, sizeof(zval) * size);
202 }
203
204 /* If resized within the destructor, take the last resize command and perform it */
205 zend_long cached_resize = array->cached_resize;
206 array->cached_resize = -1;
207 if (cached_resize != size) {
208 spl_fixedarray_resize(array, cached_resize);
209 }
210 }
211
spl_fixedarray_object_get_gc(zend_object * obj,zval ** table,int * n)212 static HashTable* spl_fixedarray_object_get_gc(zend_object *obj, zval **table, int *n)
213 {
214 spl_fixedarray_object *intern = spl_fixed_array_from_obj(obj);
215 HashTable *ht = zend_std_get_properties(obj);
216
217 *table = intern->array.elements;
218 *n = (int)intern->array.size;
219
220 return ht;
221 }
222
spl_fixedarray_object_get_properties_for(zend_object * obj,zend_prop_purpose purpose)223 static HashTable* spl_fixedarray_object_get_properties_for(zend_object *obj, zend_prop_purpose purpose)
224 {
225 /* This has __serialize, so the purpose is not ZEND_PROP_PURPOSE_SERIALIZE, which would expect a non-null return value */
226 ZEND_ASSERT(purpose != ZEND_PROP_PURPOSE_SERIALIZE);
227
228 const spl_fixedarray_object *intern = spl_fixed_array_from_obj(obj);
229 /*
230 * SplFixedArray can be subclassed or have dynamic properties (With or without AllowDynamicProperties in subclasses).
231 * Instances of subclasses with declared properties may have properties but not yet have a property table.
232 */
233 HashTable *source_properties = obj->properties ? obj->properties : (obj->ce->default_properties_count ? zend_std_get_properties(obj) : NULL);
234
235 const zend_long size = intern->array.size;
236 if (size == 0 && (!source_properties || !zend_hash_num_elements(source_properties))) {
237 return NULL;
238 }
239 zval *const elements = intern->array.elements;
240 HashTable *ht = zend_new_array(size);
241
242 for (zend_long i = 0; i < size; i++) {
243 Z_TRY_ADDREF_P(&elements[i]);
244 zend_hash_next_index_insert(ht, &elements[i]);
245 }
246 if (source_properties && zend_hash_num_elements(source_properties) > 0) {
247 zend_long nkey;
248 zend_string *skey;
249 zval *value;
250 ZEND_HASH_MAP_FOREACH_KEY_VAL_IND(source_properties, nkey, skey, value) {
251 Z_TRY_ADDREF_P(value);
252 if (skey) {
253 zend_hash_add_new(ht, skey, value);
254 } else {
255 zend_hash_index_update(ht, nkey, value);
256 }
257 } ZEND_HASH_FOREACH_END();
258 }
259
260 return ht;
261 }
262
spl_fixedarray_object_free_storage(zend_object * object)263 static void spl_fixedarray_object_free_storage(zend_object *object)
264 {
265 spl_fixedarray_object *intern = spl_fixed_array_from_obj(object);
266 spl_fixedarray_dtor(&intern->array);
267 zend_object_std_dtor(&intern->std);
268 }
269
spl_fixedarray_object_new_ex(zend_class_entry * class_type,zend_object * orig,bool clone_orig)270 static zend_object *spl_fixedarray_object_new_ex(zend_class_entry *class_type, zend_object *orig, bool clone_orig)
271 {
272 spl_fixedarray_object *intern;
273 zend_class_entry *parent = class_type;
274 bool inherited = false;
275
276 intern = zend_object_alloc(sizeof(spl_fixedarray_object), parent);
277
278 zend_object_std_init(&intern->std, class_type);
279 object_properties_init(&intern->std, class_type);
280
281 if (orig && clone_orig) {
282 spl_fixedarray_object *other = spl_fixed_array_from_obj(orig);
283 spl_fixedarray_copy_ctor(&intern->array, &other->array);
284 }
285
286 while (parent) {
287 if (parent == spl_ce_SplFixedArray) {
288 break;
289 }
290
291 parent = parent->parent;
292 inherited = true;
293 }
294
295 ZEND_ASSERT(parent);
296
297 if (UNEXPECTED(inherited)) {
298 /* Find count() method */
299 zend_function *fptr_count = zend_hash_find_ptr(&class_type->function_table, ZSTR_KNOWN(ZEND_STR_COUNT));
300 if (fptr_count->common.scope == parent) {
301 fptr_count = NULL;
302 }
303 intern->fptr_count = fptr_count;
304 }
305
306 return &intern->std;
307 }
308
spl_fixedarray_new(zend_class_entry * class_type)309 static zend_object *spl_fixedarray_new(zend_class_entry *class_type)
310 {
311 return spl_fixedarray_object_new_ex(class_type, NULL, 0);
312 }
313
spl_fixedarray_object_clone(zend_object * old_object)314 static zend_object *spl_fixedarray_object_clone(zend_object *old_object)
315 {
316 zend_object *new_object = spl_fixedarray_object_new_ex(old_object->ce, old_object, 1);
317
318 zend_objects_clone_members(new_object, old_object);
319
320 return new_object;
321 }
322
spl_offset_convert_to_long(zval * offset)323 static zend_long spl_offset_convert_to_long(zval *offset) /* {{{ */
324 {
325 try_again:
326 switch (Z_TYPE_P(offset)) {
327 case IS_STRING: {
328 zend_ulong index;
329 if (ZEND_HANDLE_NUMERIC(Z_STR_P(offset), index)) {
330 return (zend_long) index;
331 }
332 break;
333 }
334 case IS_DOUBLE:
335 return zend_dval_to_lval_safe(Z_DVAL_P(offset));
336 case IS_LONG:
337 return Z_LVAL_P(offset);
338 case IS_FALSE:
339 return 0;
340 case IS_TRUE:
341 return 1;
342 case IS_REFERENCE:
343 offset = Z_REFVAL_P(offset);
344 goto try_again;
345 case IS_RESOURCE:
346 zend_use_resource_as_offset(offset);
347 return Z_RES_HANDLE_P(offset);
348 }
349
350 /* Use SplFixedArray name from the CE */
351 zend_illegal_container_offset(spl_ce_SplFixedArray->name, offset, BP_VAR_R);
352 return 0;
353 }
354
spl_fixedarray_object_read_dimension_helper(spl_fixedarray_object * intern,zval * offset)355 static zval *spl_fixedarray_object_read_dimension_helper(spl_fixedarray_object *intern, zval *offset)
356 {
357 zend_long index;
358
359 /* we have to return NULL on error here to avoid memleak because of
360 * ZE duplicating uninitialized_zval_ptr */
361 if (!offset) {
362 zend_throw_error(NULL, "[] operator not supported for SplFixedArray");
363 return NULL;
364 }
365
366 index = spl_offset_convert_to_long(offset);
367 if (EG(exception)) {
368 return NULL;
369 }
370
371 if (index < 0 || index >= intern->array.size) {
372 zend_throw_exception(spl_ce_OutOfBoundsException, "Index invalid or out of range", 0);
373 return NULL;
374 } else {
375 return &intern->array.elements[index];
376 }
377 }
378
379 static int spl_fixedarray_object_has_dimension(zend_object *object, zval *offset, int check_empty);
380
spl_fixedarray_object_read_dimension(zend_object * object,zval * offset,int type,zval * rv)381 static zval *spl_fixedarray_object_read_dimension(zend_object *object, zval *offset, int type, zval *rv)
382 {
383 if (type == BP_VAR_IS && !spl_fixedarray_object_has_dimension(object, offset, 0)) {
384 return &EG(uninitialized_zval);
385 }
386
387 if (HAS_FIXEDARRAY_ARRAYACCESS_OVERRIDE(object, zf_offsetget)) {
388 zval tmp;
389 if (!offset) {
390 ZVAL_NULL(&tmp);
391 offset = &tmp;
392 }
393 zend_call_known_instance_method_with_1_params(object->ce->arrayaccess_funcs_ptr->zf_offsetget, object, rv, offset);
394 if (!Z_ISUNDEF_P(rv)) {
395 return rv;
396 }
397 return &EG(uninitialized_zval);
398 }
399
400 spl_fixedarray_object *intern = spl_fixed_array_from_obj(object);
401 return spl_fixedarray_object_read_dimension_helper(intern, offset);
402 }
403
spl_fixedarray_object_write_dimension_helper(spl_fixedarray_object * intern,zval * offset,zval * value)404 static void spl_fixedarray_object_write_dimension_helper(spl_fixedarray_object *intern, zval *offset, zval *value)
405 {
406 zend_long index;
407
408 if (!offset) {
409 /* '$array[] = value' syntax is not supported */
410 zend_throw_error(NULL, "[] operator not supported for SplFixedArray");
411 return;
412 }
413
414 index = spl_offset_convert_to_long(offset);
415 if (EG(exception)) {
416 return;
417 }
418
419 if (index < 0 || index >= intern->array.size) {
420 zend_throw_exception(spl_ce_OutOfBoundsException, "Index invalid or out of range", 0);
421 return;
422 } else {
423 /* Fix #81429 */
424 zval *ptr = &(intern->array.elements[index]);
425 zval tmp;
426 ZVAL_COPY_VALUE(&tmp, ptr);
427 ZVAL_COPY_DEREF(ptr, value);
428 zval_ptr_dtor(&tmp);
429 }
430 }
431
spl_fixedarray_object_write_dimension(zend_object * object,zval * offset,zval * value)432 static void spl_fixedarray_object_write_dimension(zend_object *object, zval *offset, zval *value)
433 {
434 if (HAS_FIXEDARRAY_ARRAYACCESS_OVERRIDE(object, zf_offsetset)) {
435 zval tmp;
436
437 if (!offset) {
438 ZVAL_NULL(&tmp);
439 offset = &tmp;
440 }
441 zend_call_known_instance_method_with_2_params(object->ce->arrayaccess_funcs_ptr->zf_offsetset, object, NULL, offset, value);
442 return;
443 }
444
445 spl_fixedarray_object *intern = spl_fixed_array_from_obj(object);
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 zend_throw_exception(spl_ce_OutOfBoundsException, "Index invalid or out of range", 0);
460 return;
461 } else {
462 zval garbage;
463 ZVAL_COPY_VALUE(&garbage, &intern->array.elements[index]);
464 ZVAL_NULL(&intern->array.elements[index]);
465 zval_ptr_dtor(&garbage);
466 }
467 }
468
spl_fixedarray_object_unset_dimension(zend_object * object,zval * offset)469 static void spl_fixedarray_object_unset_dimension(zend_object *object, zval *offset)
470 {
471 if (UNEXPECTED(HAS_FIXEDARRAY_ARRAYACCESS_OVERRIDE(object, zf_offsetunset))) {
472 zend_call_known_instance_method_with_1_params(object->ce->arrayaccess_funcs_ptr->zf_offsetunset, object, NULL, offset);
473 return;
474 }
475
476 spl_fixedarray_object *intern = spl_fixed_array_from_obj(object);
477 spl_fixedarray_object_unset_dimension_helper(intern, offset);
478 }
479
spl_fixedarray_object_has_dimension_helper(spl_fixedarray_object * intern,zval * offset,bool check_empty)480 static bool spl_fixedarray_object_has_dimension_helper(spl_fixedarray_object *intern, zval *offset, bool check_empty)
481 {
482 zend_long index;
483
484 index = spl_offset_convert_to_long(offset);
485 if (EG(exception)) {
486 return false;
487 }
488
489 if (index < 0 || index >= intern->array.size) {
490 return false;
491 }
492
493 if (check_empty) {
494 return zend_is_true(&intern->array.elements[index]);
495 }
496
497 return Z_TYPE(intern->array.elements[index]) != IS_NULL;
498 }
499
spl_fixedarray_object_has_dimension(zend_object * object,zval * offset,int check_empty)500 static int spl_fixedarray_object_has_dimension(zend_object *object, zval *offset, int check_empty)
501 {
502 if (HAS_FIXEDARRAY_ARRAYACCESS_OVERRIDE(object, zf_offsetexists)) {
503 zval rv;
504
505 zend_call_known_instance_method_with_1_params(object->ce->arrayaccess_funcs_ptr->zf_offsetexists, object, &rv, offset);
506 bool result = zend_is_true(&rv);
507 zval_ptr_dtor(&rv);
508 return result;
509 }
510
511 spl_fixedarray_object *intern = spl_fixed_array_from_obj(object);
512
513 return spl_fixedarray_object_has_dimension_helper(intern, offset, check_empty);
514 }
515
spl_fixedarray_object_count_elements(zend_object * object,zend_long * count)516 static zend_result spl_fixedarray_object_count_elements(zend_object *object, zend_long *count)
517 {
518 spl_fixedarray_object *intern;
519
520 intern = spl_fixed_array_from_obj(object);
521 if (UNEXPECTED(intern->fptr_count)) {
522 zval rv;
523 zend_call_known_instance_method_with_0_params(intern->fptr_count, object, &rv);
524 if (!Z_ISUNDEF(rv)) {
525 *count = zval_get_long(&rv);
526 zval_ptr_dtor(&rv);
527 } else {
528 *count = 0;
529 }
530 } else {
531 *count = intern->array.size;
532 }
533 return SUCCESS;
534 }
535
PHP_METHOD(SplFixedArray,__construct)536 PHP_METHOD(SplFixedArray, __construct)
537 {
538 zval *object = ZEND_THIS;
539 spl_fixedarray_object *intern;
540 zend_long size = 0;
541
542 if (zend_parse_parameters(ZEND_NUM_ARGS(), "|l", &size) == FAILURE) {
543 RETURN_THROWS();
544 }
545
546 if (size < 0) {
547 zend_argument_value_error(1, "must be greater than or equal to 0");
548 RETURN_THROWS();
549 }
550
551 intern = Z_SPLFIXEDARRAY_P(object);
552
553 if (!spl_fixedarray_empty(&intern->array)) {
554 /* called __construct() twice, bail out */
555 return;
556 }
557
558 spl_fixedarray_init(&intern->array, size);
559 }
560
PHP_METHOD(SplFixedArray,__wakeup)561 PHP_METHOD(SplFixedArray, __wakeup)
562 {
563 spl_fixedarray_object *intern = Z_SPLFIXEDARRAY_P(ZEND_THIS);
564 HashTable *intern_ht = zend_std_get_properties(Z_OBJ_P(ZEND_THIS));
565 zval *data;
566
567 if (zend_parse_parameters_none() == FAILURE) {
568 RETURN_THROWS();
569 }
570
571 if (intern->array.size == 0) {
572 int index = 0;
573 int size = zend_hash_num_elements(intern_ht);
574
575 spl_fixedarray_init(&intern->array, size);
576
577 ZEND_HASH_FOREACH_VAL(intern_ht, data) {
578 ZVAL_COPY(&intern->array.elements[index], data);
579 index++;
580 } ZEND_HASH_FOREACH_END();
581
582 /* Remove the unserialised properties, since we now have the elements
583 * within the spl_fixedarray_object structure. */
584 zend_hash_clean(intern_ht);
585 }
586 }
587
PHP_METHOD(SplFixedArray,__serialize)588 PHP_METHOD(SplFixedArray, __serialize)
589 {
590 spl_fixedarray_object *intern = Z_SPLFIXEDARRAY_P(ZEND_THIS);
591 zval *current;
592 zend_string *key;
593
594 if (zend_parse_parameters_none() == FAILURE) {
595 RETURN_THROWS();
596 }
597
598 HashTable *ht = zend_std_get_properties(&intern->std);
599 uint32_t num_properties = zend_hash_num_elements(ht);
600 array_init_size(return_value, intern->array.size + num_properties);
601
602 /* elements */
603 for (zend_long i = 0; i < intern->array.size; i++) {
604 current = &intern->array.elements[i];
605 zend_hash_next_index_insert(Z_ARRVAL_P(return_value), current);
606 Z_TRY_ADDREF_P(current);
607 }
608
609 /* members */
610 ZEND_HASH_FOREACH_STR_KEY_VAL_IND(ht, key, current) {
611 /* If the properties table was already rebuild, it will also contain the
612 * array elements. The array elements are already added in the above loop.
613 * We can detect array elements by the fact that their key == NULL. */
614 if (key != NULL) {
615 zend_hash_add_new(Z_ARRVAL_P(return_value), key, current);
616 Z_TRY_ADDREF_P(current);
617 }
618 } ZEND_HASH_FOREACH_END();
619 }
620
PHP_METHOD(SplFixedArray,__unserialize)621 PHP_METHOD(SplFixedArray, __unserialize)
622 {
623 spl_fixedarray_object *intern = Z_SPLFIXEDARRAY_P(ZEND_THIS);
624 HashTable *data;
625 zval members_zv, *elem;
626 zend_string *key;
627 zend_long size;
628
629 if (zend_parse_parameters(ZEND_NUM_ARGS(), "h", &data) == FAILURE) {
630 RETURN_THROWS();
631 }
632
633 if (intern->array.size == 0) {
634 size = zend_hash_num_elements(data);
635 spl_fixedarray_init_non_empty_struct(&intern->array, size);
636 if (!size) {
637 return;
638 }
639 array_init(&members_zv);
640
641 intern->array.size = 0;
642 ZEND_HASH_FOREACH_STR_KEY_VAL(data, key, elem) {
643 if (key == NULL) {
644 ZVAL_COPY(&intern->array.elements[intern->array.size], elem);
645 intern->array.size++;
646 } else {
647 Z_TRY_ADDREF_P(elem);
648 zend_hash_add(Z_ARRVAL(members_zv), key, elem);
649 }
650 } ZEND_HASH_FOREACH_END();
651
652 if (intern->array.size != size) {
653 if (intern->array.size) {
654 intern->array.elements = erealloc(intern->array.elements, sizeof(zval) * intern->array.size);
655 } else {
656 efree(intern->array.elements);
657 intern->array.elements = NULL;
658 }
659 }
660
661 object_properties_load(&intern->std, Z_ARRVAL(members_zv));
662 zval_ptr_dtor(&members_zv);
663 }
664 }
665
PHP_METHOD(SplFixedArray,count)666 PHP_METHOD(SplFixedArray, count)
667 {
668 zval *object = ZEND_THIS;
669 spl_fixedarray_object *intern;
670
671 if (zend_parse_parameters_none() == FAILURE) {
672 RETURN_THROWS();
673 }
674
675 intern = Z_SPLFIXEDARRAY_P(object);
676 RETURN_LONG(intern->array.size);
677 }
678
PHP_METHOD(SplFixedArray,toArray)679 PHP_METHOD(SplFixedArray, toArray)
680 {
681 spl_fixedarray_object *intern;
682
683 if (zend_parse_parameters_none() == FAILURE) {
684 RETURN_THROWS();
685 }
686
687 intern = Z_SPLFIXEDARRAY_P(ZEND_THIS);
688
689 if (!spl_fixedarray_empty(&intern->array)) {
690 array_init(return_value);
691 for (zend_long i = 0; i < intern->array.size; i++) {
692 zend_hash_index_update(Z_ARRVAL_P(return_value), i, &intern->array.elements[i]);
693 Z_TRY_ADDREF(intern->array.elements[i]);
694 }
695 } else {
696 RETURN_EMPTY_ARRAY();
697 }
698 }
699
PHP_METHOD(SplFixedArray,fromArray)700 PHP_METHOD(SplFixedArray, fromArray)
701 {
702 zval *data;
703 spl_fixedarray array;
704 spl_fixedarray_object *intern;
705 int num;
706 bool save_indexes = 1;
707
708 if (zend_parse_parameters(ZEND_NUM_ARGS(), "a|b", &data, &save_indexes) == FAILURE) {
709 RETURN_THROWS();
710 }
711
712 num = zend_hash_num_elements(Z_ARRVAL_P(data));
713
714 if (num > 0 && save_indexes) {
715 zval *element;
716 zend_string *str_index;
717 zend_ulong num_index, max_index = 0;
718 zend_long tmp;
719
720 ZEND_HASH_FOREACH_KEY(Z_ARRVAL_P(data), num_index, str_index) {
721 if (str_index != NULL || (zend_long)num_index < 0) {
722 zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0, "array must contain only positive integer keys");
723 RETURN_THROWS();
724 }
725
726 if (num_index > max_index) {
727 max_index = num_index;
728 }
729 } ZEND_HASH_FOREACH_END();
730
731 tmp = max_index + 1;
732 if (tmp <= 0) {
733 zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0, "integer overflow detected");
734 RETURN_THROWS();
735 }
736 spl_fixedarray_init(&array, tmp);
737
738 ZEND_HASH_FOREACH_NUM_KEY_VAL(Z_ARRVAL_P(data), num_index, element) {
739 ZVAL_COPY_DEREF(&array.elements[num_index], element);
740 } ZEND_HASH_FOREACH_END();
741
742 } else if (num > 0 && !save_indexes) {
743 zval *element;
744 zend_long i = 0;
745
746 spl_fixedarray_init(&array, num);
747
748 ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(data), element) {
749 ZVAL_COPY_DEREF(&array.elements[i], element);
750 i++;
751 } ZEND_HASH_FOREACH_END();
752 } else {
753 spl_fixedarray_init(&array, 0);
754 }
755
756 object_init_ex(return_value, spl_ce_SplFixedArray);
757
758 intern = Z_SPLFIXEDARRAY_P(return_value);
759 intern->array = array;
760 }
761
PHP_METHOD(SplFixedArray,getSize)762 PHP_METHOD(SplFixedArray, getSize)
763 {
764 zval *object = ZEND_THIS;
765 spl_fixedarray_object *intern;
766
767 if (zend_parse_parameters_none() == FAILURE) {
768 RETURN_THROWS();
769 }
770
771 intern = Z_SPLFIXEDARRAY_P(object);
772 RETURN_LONG(intern->array.size);
773 }
774
PHP_METHOD(SplFixedArray,setSize)775 PHP_METHOD(SplFixedArray, setSize)
776 {
777 zval *object = ZEND_THIS;
778 spl_fixedarray_object *intern;
779 zend_long size;
780
781 if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &size) == FAILURE) {
782 RETURN_THROWS();
783 }
784
785 if (size < 0) {
786 zend_argument_value_error(1, "must be greater than or equal to 0");
787 RETURN_THROWS();
788 }
789
790 intern = Z_SPLFIXEDARRAY_P(object);
791
792 spl_fixedarray_resize(&intern->array, size);
793 RETURN_TRUE;
794 }
795
796 /* Returns whether the requested $index exists. */
PHP_METHOD(SplFixedArray,offsetExists)797 PHP_METHOD(SplFixedArray, offsetExists)
798 {
799 zval *zindex;
800 spl_fixedarray_object *intern;
801
802 if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &zindex) == FAILURE) {
803 RETURN_THROWS();
804 }
805
806 intern = Z_SPLFIXEDARRAY_P(ZEND_THIS);
807
808 RETURN_BOOL(spl_fixedarray_object_has_dimension_helper(intern, zindex, 0));
809 }
810
811 /* Returns the value at the specified $index. */
PHP_METHOD(SplFixedArray,offsetGet)812 PHP_METHOD(SplFixedArray, offsetGet)
813 {
814 zval *zindex, *value;
815 spl_fixedarray_object *intern;
816
817 if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &zindex) == FAILURE) {
818 RETURN_THROWS();
819 }
820
821 intern = Z_SPLFIXEDARRAY_P(ZEND_THIS);
822 value = spl_fixedarray_object_read_dimension_helper(intern, zindex);
823
824 if (value) {
825 RETURN_COPY_DEREF(value);
826 } else {
827 RETURN_NULL();
828 }
829 }
830
831 /* Sets the value at the specified $index to $newval. */
PHP_METHOD(SplFixedArray,offsetSet)832 PHP_METHOD(SplFixedArray, offsetSet)
833 {
834 zval *zindex, *value;
835 spl_fixedarray_object *intern;
836
837 if (zend_parse_parameters(ZEND_NUM_ARGS(), "zz", &zindex, &value) == FAILURE) {
838 RETURN_THROWS();
839 }
840
841 intern = Z_SPLFIXEDARRAY_P(ZEND_THIS);
842 spl_fixedarray_object_write_dimension_helper(intern, zindex, value);
843
844 }
845
846 /* Unsets the value at the specified $index. */
PHP_METHOD(SplFixedArray,offsetUnset)847 PHP_METHOD(SplFixedArray, offsetUnset)
848 {
849 zval *zindex;
850 spl_fixedarray_object *intern;
851
852 if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &zindex) == FAILURE) {
853 RETURN_THROWS();
854 }
855
856 intern = Z_SPLFIXEDARRAY_P(ZEND_THIS);
857 spl_fixedarray_object_unset_dimension_helper(intern, zindex);
858
859 }
860
861 /* Create a new iterator from a SplFixedArray instance. */
PHP_METHOD(SplFixedArray,getIterator)862 PHP_METHOD(SplFixedArray, getIterator)
863 {
864 if (zend_parse_parameters_none() == FAILURE) {
865 RETURN_THROWS();
866 }
867
868 zend_create_internal_iterator_zval(return_value, ZEND_THIS);
869 }
870
PHP_METHOD(SplFixedArray,jsonSerialize)871 PHP_METHOD(SplFixedArray, jsonSerialize)
872 {
873 ZEND_PARSE_PARAMETERS_NONE();
874
875 spl_fixedarray_object *intern = Z_SPLFIXEDARRAY_P(ZEND_THIS);
876 array_init_size(return_value, intern->array.size);
877 for (zend_long i = 0; i < intern->array.size; i++) {
878 zend_hash_next_index_insert_new(Z_ARR_P(return_value), &intern->array.elements[i]);
879 Z_TRY_ADDREF(intern->array.elements[i]);
880 }
881 }
882
spl_fixedarray_it_dtor(zend_object_iterator * iter)883 static void spl_fixedarray_it_dtor(zend_object_iterator *iter)
884 {
885 zval_ptr_dtor(&iter->data);
886 }
887
spl_fixedarray_it_rewind(zend_object_iterator * iter)888 static void spl_fixedarray_it_rewind(zend_object_iterator *iter)
889 {
890 ((spl_fixedarray_it*)iter)->current = 0;
891 }
892
spl_fixedarray_it_valid(zend_object_iterator * iter)893 static zend_result spl_fixedarray_it_valid(zend_object_iterator *iter)
894 {
895 spl_fixedarray_it *iterator = (spl_fixedarray_it*)iter;
896 spl_fixedarray_object *object = Z_SPLFIXEDARRAY_P(&iter->data);
897
898 if (iterator->current >= 0 && iterator->current < object->array.size) {
899 return SUCCESS;
900 }
901
902 return FAILURE;
903 }
904
spl_fixedarray_it_get_current_data(zend_object_iterator * iter)905 static zval *spl_fixedarray_it_get_current_data(zend_object_iterator *iter)
906 {
907 zval zindex, *data;
908 spl_fixedarray_it *iterator = (spl_fixedarray_it*)iter;
909 spl_fixedarray_object *object = Z_SPLFIXEDARRAY_P(&iter->data);
910
911 ZVAL_LONG(&zindex, iterator->current);
912 data = spl_fixedarray_object_read_dimension_helper(object, &zindex);
913
914 if (data == NULL) {
915 data = &EG(uninitialized_zval);
916 }
917 return data;
918 }
919
spl_fixedarray_it_get_current_key(zend_object_iterator * iter,zval * key)920 static void spl_fixedarray_it_get_current_key(zend_object_iterator *iter, zval *key)
921 {
922 ZVAL_LONG(key, ((spl_fixedarray_it*)iter)->current);
923 }
924
spl_fixedarray_it_move_forward(zend_object_iterator * iter)925 static void spl_fixedarray_it_move_forward(zend_object_iterator *iter)
926 {
927 ((spl_fixedarray_it*)iter)->current++;
928 }
929
930 /* iterator handler table */
931 static const zend_object_iterator_funcs spl_fixedarray_it_funcs = {
932 spl_fixedarray_it_dtor,
933 spl_fixedarray_it_valid,
934 spl_fixedarray_it_get_current_data,
935 spl_fixedarray_it_get_current_key,
936 spl_fixedarray_it_move_forward,
937 spl_fixedarray_it_rewind,
938 NULL,
939 NULL, /* get_gc */
940 };
941
spl_fixedarray_get_iterator(zend_class_entry * ce,zval * object,int by_ref)942 static zend_object_iterator *spl_fixedarray_get_iterator(zend_class_entry *ce, zval *object, int by_ref)
943 {
944 spl_fixedarray_it *iterator;
945
946 if (by_ref) {
947 zend_throw_error(NULL, "An iterator cannot be used with foreach by reference");
948 return NULL;
949 }
950
951 iterator = emalloc(sizeof(spl_fixedarray_it));
952
953 zend_iterator_init((zend_object_iterator*)iterator);
954
955 ZVAL_OBJ_COPY(&iterator->intern.data, Z_OBJ_P(object));
956 iterator->intern.funcs = &spl_fixedarray_it_funcs;
957
958 return &iterator->intern;
959 }
960
PHP_MINIT_FUNCTION(spl_fixedarray)961 PHP_MINIT_FUNCTION(spl_fixedarray)
962 {
963 spl_ce_SplFixedArray = register_class_SplFixedArray(
964 zend_ce_aggregate, zend_ce_arrayaccess, zend_ce_countable, php_json_serializable_ce);
965 spl_ce_SplFixedArray->create_object = spl_fixedarray_new;
966 spl_ce_SplFixedArray->default_object_handlers = &spl_handler_SplFixedArray;
967 spl_ce_SplFixedArray->get_iterator = spl_fixedarray_get_iterator;
968
969 memcpy(&spl_handler_SplFixedArray, &std_object_handlers, sizeof(zend_object_handlers));
970
971 spl_handler_SplFixedArray.offset = XtOffsetOf(spl_fixedarray_object, std);
972 spl_handler_SplFixedArray.clone_obj = spl_fixedarray_object_clone;
973 spl_handler_SplFixedArray.read_dimension = spl_fixedarray_object_read_dimension;
974 spl_handler_SplFixedArray.write_dimension = spl_fixedarray_object_write_dimension;
975 spl_handler_SplFixedArray.unset_dimension = spl_fixedarray_object_unset_dimension;
976 spl_handler_SplFixedArray.has_dimension = spl_fixedarray_object_has_dimension;
977 spl_handler_SplFixedArray.count_elements = spl_fixedarray_object_count_elements;
978 spl_handler_SplFixedArray.get_properties_for = spl_fixedarray_object_get_properties_for;
979 spl_handler_SplFixedArray.get_gc = spl_fixedarray_object_get_gc;
980 spl_handler_SplFixedArray.free_obj = spl_fixedarray_object_free_storage;
981
982 return SUCCESS;
983 }
984