Coverage Report

Created: 2025-09-27 06:26

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/php-src/ext/spl/spl_observer.c
Line
Count
Source
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
   | Authors: Marcus Boerger <helly@php.net>                              |
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 "ext/standard/php_array.h" /* For PHP_COUNT_* constants */
24
#include "ext/standard/php_var.h"
25
#include "zend_smart_str.h"
26
#include "zend_interfaces.h"
27
#include "zend_exceptions.h"
28
#include "zend_attributes.h"
29
30
#include "php_spl.h" /* For php_spl_object_hash() */
31
#include "spl_observer.h"
32
#include "spl_observer_arginfo.h"
33
#include "spl_iterators.h"
34
#include "spl_exceptions.h"
35
#include "spl_functions.h" /* For spl_set_private_debug_info_property() */
36
37
PHPAPI zend_class_entry     *spl_ce_SplObserver;
38
PHPAPI zend_class_entry     *spl_ce_SplSubject;
39
PHPAPI zend_class_entry     *spl_ce_SplObjectStorage;
40
PHPAPI zend_class_entry     *spl_ce_MultipleIterator;
41
42
static zend_object_handlers spl_handler_SplObjectStorage;
43
static zend_object_handlers spl_handler_MultipleIterator;
44
45
/* Bit flags for marking internal functionality overridden by SplObjectStorage subclasses. */
46
18
#define SOS_OVERRIDDEN_READ_DIMENSION  1
47
18
#define SOS_OVERRIDDEN_WRITE_DIMENSION 2
48
18
#define SOS_OVERRIDDEN_UNSET_DIMENSION 4
49
50
typedef struct _spl_SplObjectStorage { /* {{{ */
51
  HashTable         storage;
52
  zend_long         index;
53
  HashPosition      pos;
54
  /* In SplObjectStorage, flags is a hidden implementation detail to optimize ArrayAccess handlers.
55
   * In MultipleIterator on a different class hierarchy, flags is a user settable value controlling iteration behavior. */
56
  zend_long         flags;
57
  zend_function    *fptr_get_hash;
58
  zend_object       std;
59
} spl_SplObjectStorage; /* }}} */
60
61
/* {{{ storage is an assoc array of [zend_object*]=>[zval *obj, zval *inf] */
62
typedef struct _spl_SplObjectStorageElement {
63
  zend_object *obj;
64
  zval inf;
65
} spl_SplObjectStorageElement; /* }}} */
66
67
100k
static inline spl_SplObjectStorage *spl_object_storage_from_obj(zend_object *obj) /* {{{ */ {
68
100k
  return (spl_SplObjectStorage*)((char*)(obj) - XtOffsetOf(spl_SplObjectStorage, std));
69
100k
}
70
/* }}} */
71
72
22.2k
#define Z_SPLOBJSTORAGE_P(zv)  spl_object_storage_from_obj(Z_OBJ_P((zv)))
73
74
static void spl_SplObjectStorage_free_storage(zend_object *object) /* {{{ */
75
23.3k
{
76
23.3k
  spl_SplObjectStorage *intern = spl_object_storage_from_obj(object);
77
78
23.3k
  zend_object_std_dtor(&intern->std);
79
80
23.3k
  zend_hash_destroy(&intern->storage);
81
23.3k
} /* }}} */
82
83
8.14k
static zend_result spl_object_storage_get_hash(zend_hash_key *key, spl_SplObjectStorage *intern, zend_object *obj) {
84
8.14k
  if (UNEXPECTED(intern->fptr_get_hash)) {
85
0
    zval param;
86
0
    zval rv;
87
0
    ZVAL_OBJ(&param, obj);
88
0
    zend_call_method_with_1_params(&intern->std, intern->std.ce, &intern->fptr_get_hash, "getHash", &rv, &param);
89
0
    if (UNEXPECTED(Z_ISUNDEF(rv))) {
90
      /* An exception has occurred */
91
0
      return FAILURE;
92
0
    } else {
93
      /* TODO PHP 9: Remove this as this will be enforced from the return type */
94
0
      if (UNEXPECTED(Z_TYPE(rv) != IS_STRING)) {
95
0
        zend_type_error("%s::getHash(): Return value must be of type string, %s returned",
96
0
          ZSTR_VAL(intern->std.ce->name), zend_zval_value_name(&rv));
97
0
        zval_ptr_dtor(&rv);
98
0
        return FAILURE;
99
0
      } else {
100
0
        key->key = Z_STR(rv);
101
0
        return SUCCESS;
102
0
      }
103
0
    }
104
8.14k
  } else {
105
8.14k
    key->key = NULL;
106
8.14k
    key->h = obj->handle;
107
8.14k
    return SUCCESS;
108
8.14k
  }
109
8.14k
}
110
111
8.14k
static void spl_object_storage_free_hash(spl_SplObjectStorage *intern, zend_hash_key *key) {
112
8.14k
  if (key->key) {
113
0
    zend_string_release_ex(key->key, 0);
114
0
  }
115
8.14k
}
116
117
static void spl_object_storage_dtor(zval *element) /* {{{ */
118
9.68k
{
119
9.68k
  spl_SplObjectStorageElement *el = Z_PTR_P(element);
120
9.68k
  if (el) {
121
9.68k
    zend_object_release(el->obj);
122
9.68k
    zval_ptr_dtor(&el->inf);
123
9.68k
    efree(el);
124
9.68k
  }
125
9.68k
} /* }}} */
126
127
static spl_SplObjectStorageElement* spl_object_storage_get(spl_SplObjectStorage *intern, zend_hash_key *key) /* {{{ */
128
8.14k
{
129
8.14k
  if (key->key) {
130
0
    return zend_hash_find_ptr(&intern->storage, key->key);
131
8.14k
  } else {
132
8.14k
    return zend_hash_index_find_ptr(&intern->storage, key->h);
133
8.14k
  }
134
8.14k
} /* }}} */
135
136
static spl_SplObjectStorageElement *spl_object_storage_create_element(zend_object *obj, zval *inf) /* {{{ */
137
9.68k
{
138
9.68k
  spl_SplObjectStorageElement *pelement = emalloc(sizeof(spl_SplObjectStorageElement));
139
9.68k
  pelement->obj = obj;
140
9.68k
  GC_ADDREF(obj);
141
9.68k
  if (inf) {
142
1.55k
    ZVAL_COPY(&pelement->inf, inf);
143
8.13k
  } else {
144
8.13k
    ZVAL_NULL(&pelement->inf);
145
8.13k
  }
146
9.68k
  return pelement;
147
9.68k
} /* }}} */
148
149
/* A faster version of spl_object_storage_attach used when neither SplObjectStorage->getHash nor SplObjectStorage->offsetSet is overridden. */
150
static spl_SplObjectStorageElement *spl_object_storage_attach_handle(spl_SplObjectStorage *intern, zend_object *obj, zval *inf) /* {{{ */
151
9.69k
{
152
9.69k
  uint32_t handle = obj->handle;
153
9.69k
  zval *entry_zv = zend_hash_index_lookup(&intern->storage, handle);
154
9.69k
  spl_SplObjectStorageElement *pelement;
155
9.69k
  ZEND_ASSERT(!(intern->flags & SOS_OVERRIDDEN_WRITE_DIMENSION));
156
157
9.69k
  if (Z_TYPE_P(entry_zv) != IS_NULL) {
158
15
    zval zv_inf;
159
15
    ZEND_ASSERT(Z_TYPE_P(entry_zv) == IS_PTR);
160
15
    pelement = Z_PTR_P(entry_zv);
161
15
    ZVAL_COPY_VALUE(&zv_inf, &pelement->inf);
162
15
    if (inf) {
163
2
      ZVAL_COPY(&pelement->inf, inf);
164
13
    } else {
165
13
      ZVAL_NULL(&pelement->inf);
166
13
    }
167
    /* Call the old value's destructor last, in case it moves the entry */
168
15
    zval_ptr_dtor(&zv_inf);
169
15
    return pelement;
170
15
  }
171
172
  /* NULL initialization necessary because `spl_object_storage_create_element` could bail out due to OOM. */
173
9.68k
  ZVAL_PTR(entry_zv, NULL);
174
9.68k
  pelement = spl_object_storage_create_element(obj, inf);
175
9.68k
  Z_PTR_P(entry_zv) = pelement;
176
9.68k
  return pelement;
177
9.69k
} /* }}} */
178
179
static spl_SplObjectStorageElement *spl_object_storage_attach(spl_SplObjectStorage *intern, zend_object *obj, zval *inf) /* {{{ */
180
9.63k
{
181
9.63k
  if (EXPECTED(!(intern->flags & SOS_OVERRIDDEN_WRITE_DIMENSION))) {
182
9.63k
    return spl_object_storage_attach_handle(intern, obj, inf);
183
9.63k
  }
184
  /* getHash or offsetSet is overridden. */
185
186
0
  spl_SplObjectStorageElement *pelement, element;
187
0
  zend_hash_key key;
188
0
  if (spl_object_storage_get_hash(&key, intern, obj) == FAILURE) {
189
0
    return NULL;
190
0
  }
191
192
0
  pelement = spl_object_storage_get(intern, &key);
193
194
0
  if (pelement) {
195
0
    zval zv_inf;
196
0
    ZVAL_COPY_VALUE(&zv_inf, &pelement->inf);
197
0
    if (inf) {
198
0
      ZVAL_COPY(&pelement->inf, inf);
199
0
    } else {
200
0
      ZVAL_NULL(&pelement->inf);
201
0
    }
202
0
    spl_object_storage_free_hash(intern, &key);
203
    /* Call the old value's destructor last, in case it moves the entry */
204
0
    zval_ptr_dtor(&zv_inf);
205
0
    return pelement;
206
0
  }
207
208
0
  element.obj = obj;
209
0
  GC_ADDREF(obj);
210
0
  if (inf) {
211
0
    ZVAL_COPY(&element.inf, inf);
212
0
  } else {
213
0
    ZVAL_NULL(&element.inf);
214
0
  }
215
0
  if (key.key) {
216
0
    pelement = zend_hash_update_mem(&intern->storage, key.key, &element, sizeof(spl_SplObjectStorageElement));
217
0
  } else {
218
0
    pelement = zend_hash_index_update_mem(&intern->storage, key.h, &element, sizeof(spl_SplObjectStorageElement));
219
0
  }
220
0
  spl_object_storage_free_hash(intern, &key);
221
0
  return pelement;
222
0
} /* }}} */
223
224
static zend_result spl_object_storage_detach(spl_SplObjectStorage *intern, zend_object *obj) /* {{{ */
225
0
{
226
0
  if (EXPECTED(!(intern->flags & SOS_OVERRIDDEN_UNSET_DIMENSION))) {
227
0
    return zend_hash_index_del(&intern->storage, obj->handle);
228
0
  }
229
0
  zend_result ret = FAILURE;
230
0
  zend_hash_key key;
231
0
  if (spl_object_storage_get_hash(&key, intern, obj) == FAILURE) {
232
0
    return ret;
233
0
  }
234
0
  if (key.key) {
235
0
    ret = zend_hash_del(&intern->storage, key.key);
236
0
  } else {
237
0
    ret = zend_hash_index_del(&intern->storage, key.h);
238
0
  }
239
0
  spl_object_storage_free_hash(intern, &key);
240
241
0
  return ret;
242
0
} /* }}}*/
243
244
0
static void spl_object_storage_addall(spl_SplObjectStorage *intern, spl_SplObjectStorage *other) { /* {{{ */
245
0
  spl_SplObjectStorageElement *element;
246
247
0
  ZEND_HASH_FOREACH_PTR(&other->storage, element) {
248
0
    spl_object_storage_attach(intern, element->obj, &element->inf);
249
0
  } ZEND_HASH_FOREACH_END();
250
251
0
  intern->index = 0;
252
0
} /* }}} */
253
254
#define SPL_OBJECT_STORAGE_CLASS_HAS_OVERRIDE(class_type, zstr_method) \
255
72
  (class_type->arrayaccess_funcs_ptr && class_type->arrayaccess_funcs_ptr->zstr_method)
256
257
static zend_object *spl_object_storage_new_ex(zend_class_entry *class_type, zend_object *orig) /* {{{ */
258
23.3k
{
259
23.3k
  spl_SplObjectStorage *intern;
260
23.3k
  zend_class_entry *parent = class_type;
261
262
23.3k
  intern = zend_object_alloc(sizeof(spl_SplObjectStorage), parent);
263
23.3k
  intern->pos = 0;
264
265
23.3k
  zend_object_std_init(&intern->std, class_type);
266
23.3k
  object_properties_init(&intern->std, class_type);
267
268
23.3k
  zend_hash_init(&intern->storage, 0, NULL, spl_object_storage_dtor, 0);
269
270
23.4k
  while (parent) {
271
23.4k
    if (parent == spl_ce_SplObjectStorage) {
272
      /* Possible optimization: Cache these results with a map from class entry to IS_NULL/IS_PTR.
273
       * Or maybe just a single item with the result for the most recently loaded subclass. */
274
23.3k
      if (class_type != spl_ce_SplObjectStorage) {
275
18
        zend_function *get_hash = zend_hash_str_find_ptr(&class_type->function_table, "gethash", sizeof("gethash") - 1);
276
18
        if (get_hash->common.scope != spl_ce_SplObjectStorage) {
277
0
          intern->fptr_get_hash = get_hash;
278
0
        }
279
18
        if (intern->fptr_get_hash != NULL ||
280
18
          SPL_OBJECT_STORAGE_CLASS_HAS_OVERRIDE(class_type, zf_offsetget) ||
281
18
          SPL_OBJECT_STORAGE_CLASS_HAS_OVERRIDE(class_type, zf_offsetexists)) {
282
18
          intern->flags |= SOS_OVERRIDDEN_READ_DIMENSION;
283
18
        }
284
285
18
        if (intern->fptr_get_hash != NULL ||
286
18
          SPL_OBJECT_STORAGE_CLASS_HAS_OVERRIDE(class_type, zf_offsetset)) {
287
18
          intern->flags |= SOS_OVERRIDDEN_WRITE_DIMENSION;
288
18
        }
289
290
18
        if (intern->fptr_get_hash != NULL ||
291
18
          SPL_OBJECT_STORAGE_CLASS_HAS_OVERRIDE(class_type, zf_offsetunset)) {
292
18
          intern->flags |= SOS_OVERRIDDEN_UNSET_DIMENSION;
293
18
        }
294
18
      }
295
23.3k
      break;
296
23.3k
    }
297
298
81
    parent = parent->parent;
299
81
  }
300
301
23.3k
  if (orig) {
302
0
    spl_SplObjectStorage *other = spl_object_storage_from_obj(orig);
303
0
    spl_object_storage_addall(intern, other);
304
0
  }
305
306
23.3k
  return &intern->std;
307
23.3k
}
308
/* }}} */
309
310
/* {{{ spl_object_storage_clone */
311
static zend_object *spl_object_storage_clone(zend_object *old_object)
312
0
{
313
0
  zend_object *new_object;
314
315
0
  new_object = spl_object_storage_new_ex(old_object->ce, old_object);
316
317
0
  zend_objects_clone_members(new_object, old_object);
318
319
0
  return new_object;
320
0
}
321
/* }}} */
322
323
static inline HashTable* spl_object_storage_debug_info(zend_object *obj) /* {{{ */
324
0
{
325
0
  spl_SplObjectStorage *intern = spl_object_storage_from_obj(obj);
326
0
  spl_SplObjectStorageElement *element;
327
0
  HashTable *props;
328
0
  zval tmp, storage;
329
0
  HashTable *debug_info;
330
331
0
  props = obj->handlers->get_properties(obj);
332
333
0
  debug_info = zend_new_array(zend_hash_num_elements(props) + 1);
334
0
  zend_hash_copy(debug_info, props, (copy_ctor_func_t)zval_add_ref);
335
336
0
  array_init(&storage);
337
338
0
  ZEND_HASH_FOREACH_PTR(&intern->storage, element) {
339
0
    array_init(&tmp);
340
0
    zval obj;
341
0
    ZVAL_OBJ_COPY(&obj, element->obj);
342
0
    add_assoc_zval_ex(&tmp, "obj", sizeof("obj") - 1, &obj);
343
0
    Z_TRY_ADDREF(element->inf);
344
0
    add_assoc_zval_ex(&tmp, "inf", sizeof("inf") - 1, &element->inf);
345
0
    zend_hash_next_index_insert(Z_ARRVAL(storage), &tmp);
346
0
  } ZEND_HASH_FOREACH_END();
347
348
0
  spl_set_private_debug_info_property(spl_ce_SplObjectStorage, "storage", strlen("storage"), debug_info, &storage);
349
350
0
  return debug_info;
351
0
}
352
/* }}} */
353
354
/* overridden for garbage collection */
355
static HashTable *spl_object_storage_get_gc(zend_object *obj, zval **table, int *n) /* {{{ */
356
54.3k
{
357
54.3k
  spl_SplObjectStorage *intern = spl_object_storage_from_obj(obj);
358
54.3k
  spl_SplObjectStorageElement *element;
359
54.3k
  zend_get_gc_buffer *gc_buffer = zend_get_gc_buffer_create();
360
361
104k
  ZEND_HASH_FOREACH_PTR(&intern->storage, element) {
362
104k
    zend_get_gc_buffer_add_obj(gc_buffer, element->obj);
363
104k
    zend_get_gc_buffer_add_zval(gc_buffer, &element->inf);
364
104k
  } ZEND_HASH_FOREACH_END();
365
366
54.3k
  zend_get_gc_buffer_use(gc_buffer, table, n);
367
54.3k
  return zend_std_get_properties(obj);
368
54.3k
}
369
/* }}} */
370
371
static int spl_object_storage_compare_info(zval *e1, zval *e2) /* {{{ */
372
0
{
373
0
  spl_SplObjectStorageElement *s1 = (spl_SplObjectStorageElement*)Z_PTR_P(e1);
374
0
  spl_SplObjectStorageElement *s2 = (spl_SplObjectStorageElement*)Z_PTR_P(e2);
375
376
0
  return zend_compare(&s1->inf, &s2->inf);
377
0
}
378
/* }}} */
379
380
static int spl_object_storage_compare_objects(zval *o1, zval *o2) /* {{{ */
381
0
{
382
0
  zend_object *zo1;
383
0
  zend_object *zo2;
384
385
0
  ZEND_COMPARE_OBJECTS_FALLBACK(o1, o2);
386
387
0
  zo1 = (zend_object *)Z_OBJ_P(o1);
388
0
  zo2 = (zend_object *)Z_OBJ_P(o2);
389
390
0
  if (zo1->ce != spl_ce_SplObjectStorage || zo2->ce != spl_ce_SplObjectStorage) {
391
0
    return ZEND_UNCOMPARABLE;
392
0
  }
393
394
0
  return zend_hash_compare(&(Z_SPLOBJSTORAGE_P(o1))->storage, &(Z_SPLOBJSTORAGE_P(o2))->storage, (compare_func_t)spl_object_storage_compare_info, 0);
395
0
}
396
/* }}} */
397
398
/* {{{ spl_array_object_new */
399
static zend_object *spl_SplObjectStorage_new(zend_class_entry *class_type)
400
23.3k
{
401
23.3k
  return spl_object_storage_new_ex(class_type, NULL);
402
23.3k
}
403
/* }}} */
404
405
/* Returns true if the SplObjectStorage contains an entry for getHash(obj), even if the corresponding value is null. */
406
static bool spl_object_storage_contains(spl_SplObjectStorage *intern, zend_object *obj) /* {{{ */
407
25
{
408
25
  if (EXPECTED(!intern->fptr_get_hash)) {
409
25
    return zend_hash_index_find(&intern->storage, obj->handle) != NULL;
410
25
  }
411
0
  zend_hash_key key;
412
0
  if (spl_object_storage_get_hash(&key, intern, obj) == FAILURE) {
413
0
    return true;
414
0
  }
415
416
0
  ZEND_ASSERT(key.key);
417
0
  bool found = zend_hash_exists(&intern->storage, key.key);
418
0
  zend_string_release_ex(key.key, 0);
419
420
0
  return found;
421
0
} /* }}} */
422
423
/* {{{ Attaches an object to the storage if not yet contained */
424
PHP_METHOD(SplObjectStorage, attach)
425
3
{
426
3
  zend_object *obj;
427
3
  zval *inf = NULL;
428
429
3
  spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS);
430
431
9
  ZEND_PARSE_PARAMETERS_START(1, 2)
432
12
    Z_PARAM_OBJ(obj)
433
0
    Z_PARAM_OPTIONAL
434
0
    Z_PARAM_ZVAL(inf)
435
3
  ZEND_PARSE_PARAMETERS_END();
436
0
  spl_object_storage_attach(intern, obj, inf);
437
0
} /* }}} */
438
439
// todo: make spl_object_storage_has_dimension return bool as well
440
static int spl_object_storage_has_dimension(zend_object *object, zval *offset, int check_empty)
441
0
{
442
0
  spl_SplObjectStorage *intern = spl_object_storage_from_obj(object);
443
0
  if (UNEXPECTED(offset == NULL || Z_TYPE_P(offset) != IS_OBJECT || (intern->flags & SOS_OVERRIDDEN_READ_DIMENSION))) {
444
    /* Can't optimize empty()/isset() check if getHash, offsetExists, or offsetGet is overridden */
445
0
    return zend_std_has_dimension(object, offset, check_empty);
446
0
  }
447
0
  spl_SplObjectStorageElement *element = zend_hash_index_find_ptr(&intern->storage, Z_OBJ_HANDLE_P(offset));
448
0
  if (!element) {
449
0
    return 0;
450
0
  }
451
452
0
  if (check_empty) {
453
0
    return i_zend_is_true(&element->inf);
454
0
  }
455
  /* NOTE: SplObjectStorage->offsetExists() is an alias of SplObjectStorage->contains(), so this returns true even if the value is null. */
456
0
  return 1;
457
0
}
458
459
static zval *spl_object_storage_read_dimension(zend_object *object, zval *offset, int type, zval *rv)
460
74
{
461
74
  spl_SplObjectStorage *intern = spl_object_storage_from_obj(object);
462
74
  if (UNEXPECTED(offset == NULL || Z_TYPE_P(offset) != IS_OBJECT || (intern->flags & SOS_OVERRIDDEN_READ_DIMENSION))) {
463
    /* Can't optimize it if getHash, offsetExists, or offsetGet is overridden */
464
2
    return zend_std_read_dimension(object, offset, type, rv);
465
2
  }
466
72
  spl_SplObjectStorageElement *element = zend_hash_index_find_ptr(&intern->storage, Z_OBJ_HANDLE_P(offset));
467
468
72
  if (!element) {
469
0
    if (type == BP_VAR_IS) {
470
0
      return &EG(uninitialized_zval);
471
0
    }
472
0
    zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0, "Object not found");
473
0
    return NULL;
474
72
  } else {
475
    /* This deliberately returns a non-reference, even for BP_VAR_W and BP_VAR_RW, to behave the same way as SplObjectStorage did when using the default zend_std_read_dimension behavior.
476
     * i.e. This prevents taking a reference to an entry of SplObjectStorage because offsetGet would return a non-reference. */
477
72
    ZVAL_COPY_DEREF(rv, &element->inf);
478
72
    return rv;
479
72
  }
480
72
}
481
482
static void spl_object_storage_write_dimension(zend_object *object, zval *offset, zval *inf)
483
65
{
484
65
  spl_SplObjectStorage *intern = spl_object_storage_from_obj(object);
485
65
  if (UNEXPECTED(offset == NULL || Z_TYPE_P(offset) != IS_OBJECT || (intern->flags & SOS_OVERRIDDEN_WRITE_DIMENSION))) {
486
3
    zend_std_write_dimension(object, offset, inf);
487
3
    return;
488
3
  }
489
62
  spl_object_storage_attach_handle(intern, Z_OBJ_P(offset), inf);
490
62
}
491
492
static void spl_multiple_iterator_write_dimension(zend_object *object, zval *offset, zval *inf)
493
0
{
494
0
  spl_SplObjectStorage *intern = spl_object_storage_from_obj(object);
495
0
  if (UNEXPECTED(offset == NULL || Z_TYPE_P(offset) != IS_OBJECT || (intern->flags & SOS_OVERRIDDEN_WRITE_DIMENSION))) {
496
0
    zend_std_write_dimension(object, offset, inf);
497
0
    return;
498
0
  }
499
0
  if (UNEXPECTED(!Z_OBJCE_P(offset)->iterator_funcs_ptr || !Z_OBJCE_P(offset)->iterator_funcs_ptr->zf_valid)) {
500
0
    zend_type_error("Can only attach objects that implement the Iterator interface");
501
0
    return;
502
0
  }
503
0
  spl_object_storage_attach_handle(intern, Z_OBJ_P(offset), inf);
504
0
}
505
506
static void spl_object_storage_unset_dimension(zend_object *object, zval *offset)
507
0
{
508
0
  spl_SplObjectStorage *intern = spl_object_storage_from_obj(object);
509
0
  if (UNEXPECTED(Z_TYPE_P(offset) != IS_OBJECT || (intern->flags & SOS_OVERRIDDEN_UNSET_DIMENSION))) {
510
0
    zend_std_unset_dimension(object, offset);
511
0
    return;
512
0
  }
513
0
  zend_hash_index_del(&intern->storage, Z_OBJ_HANDLE_P(offset));
514
0
}
515
516
/* {{{ Detaches an object from the storage */
517
PHP_METHOD(SplObjectStorage, detach)
518
0
{
519
0
  zend_object *obj;
520
0
  spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS);
521
522
0
  ZEND_PARSE_PARAMETERS_START(1, 1)
523
0
    Z_PARAM_OBJ(obj)
524
0
  ZEND_PARSE_PARAMETERS_END();
525
0
  spl_object_storage_detach(intern, obj);
526
527
0
  zend_hash_internal_pointer_reset_ex(&intern->storage, &intern->pos);
528
0
  intern->index = 0;
529
0
} /* }}} */
530
531
/* {{{ Returns the hash of an object */
532
PHP_METHOD(SplObjectStorage, getHash)
533
0
{
534
0
  zend_object *obj;
535
536
0
  ZEND_PARSE_PARAMETERS_START(1, 1)
537
0
    Z_PARAM_OBJ(obj)
538
0
  ZEND_PARSE_PARAMETERS_END();
539
540
0
  RETURN_NEW_STR(php_spl_object_hash(obj));
541
542
0
} /* }}} */
543
544
/* {{{ Returns associated information for a stored object */
545
PHP_METHOD(SplObjectStorage, offsetGet)
546
2
{
547
2
  zend_object *obj;
548
2
  spl_SplObjectStorageElement *element;
549
2
  spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS);
550
2
  zend_hash_key key;
551
552
6
  ZEND_PARSE_PARAMETERS_START(1, 1)
553
8
    Z_PARAM_OBJ(obj)
554
2
  ZEND_PARSE_PARAMETERS_END();
555
556
0
  if (spl_object_storage_get_hash(&key, intern, obj) == FAILURE) {
557
0
    RETURN_NULL();
558
0
  }
559
560
0
  element = spl_object_storage_get(intern, &key);
561
0
  spl_object_storage_free_hash(intern, &key);
562
563
0
  if (!element) {
564
0
    zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0, "Object not found");
565
0
  } else {
566
0
    RETURN_COPY_DEREF(&element->inf);
567
0
  }
568
0
} /* }}} */
569
570
/* {{{ Add all elements contained in $os */
571
PHP_METHOD(SplObjectStorage, addAll)
572
0
{
573
0
  zval *obj;
574
0
  spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS);
575
0
  spl_SplObjectStorage *other;
576
577
0
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &obj, spl_ce_SplObjectStorage) == FAILURE) {
578
0
    RETURN_THROWS();
579
0
  }
580
581
0
  other = Z_SPLOBJSTORAGE_P(obj);
582
583
0
  spl_object_storage_addall(intern, other);
584
585
0
  RETURN_LONG(zend_hash_num_elements(&intern->storage));
586
0
} /* }}} */
587
588
/* {{{ Remove all elements contained in $os */
589
PHP_METHOD(SplObjectStorage, removeAll)
590
0
{
591
0
  zval *obj;
592
0
  spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS);
593
0
  spl_SplObjectStorage *other;
594
0
  spl_SplObjectStorageElement *element;
595
596
0
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &obj, spl_ce_SplObjectStorage) == FAILURE) {
597
0
    RETURN_THROWS();
598
0
  }
599
600
0
  other = Z_SPLOBJSTORAGE_P(obj);
601
602
0
  zend_hash_internal_pointer_reset(&other->storage);
603
0
  while ((element = zend_hash_get_current_data_ptr(&other->storage)) != NULL) {
604
0
    if (spl_object_storage_detach(intern, element->obj) == FAILURE) {
605
0
      zend_hash_move_forward(&other->storage);
606
0
    }
607
0
  }
608
609
0
  zend_hash_internal_pointer_reset_ex(&intern->storage, &intern->pos);
610
0
  intern->index = 0;
611
612
0
  RETURN_LONG(zend_hash_num_elements(&intern->storage));
613
0
} /* }}} */
614
615
/* {{{ Remove elements not common to both this SplObjectStorage instance and $os */
616
PHP_METHOD(SplObjectStorage, removeAllExcept)
617
0
{
618
0
  zval *obj;
619
0
  spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS);
620
0
  spl_SplObjectStorage *other;
621
0
  spl_SplObjectStorageElement *element;
622
623
0
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &obj, spl_ce_SplObjectStorage) == FAILURE) {
624
0
    RETURN_THROWS();
625
0
  }
626
627
0
  other = Z_SPLOBJSTORAGE_P(obj);
628
629
0
  ZEND_HASH_FOREACH_PTR(&intern->storage, element) {
630
0
    if (!spl_object_storage_contains(other, element->obj)) {
631
0
      spl_object_storage_detach(intern, element->obj);
632
0
    }
633
0
  } ZEND_HASH_FOREACH_END();
634
635
0
  zend_hash_internal_pointer_reset_ex(&intern->storage, &intern->pos);
636
0
  intern->index = 0;
637
638
0
  RETURN_LONG(zend_hash_num_elements(&intern->storage));
639
0
}
640
/* }}} */
641
642
/* {{{ Determine whether an object is contained in the storage */
643
PHP_METHOD(SplObjectStorage, contains)
644
25
{
645
25
  zend_object *obj;
646
25
  spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS);
647
648
75
  ZEND_PARSE_PARAMETERS_START(1, 1)
649
100
    Z_PARAM_OBJ(obj)
650
25
  ZEND_PARSE_PARAMETERS_END();
651
25
  RETURN_BOOL(spl_object_storage_contains(intern, obj));
652
25
} /* }}} */
653
654
/* {{{ Determine number of objects in storage */
655
PHP_METHOD(SplObjectStorage, count)
656
0
{
657
0
  spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS);
658
0
  zend_long mode = PHP_COUNT_NORMAL;
659
660
0
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "|l", &mode) == FAILURE) {
661
0
    RETURN_THROWS();
662
0
  }
663
664
0
  if (mode == PHP_COUNT_RECURSIVE) {
665
0
    RETURN_LONG(php_count_recursive(&intern->storage));
666
0
  }
667
668
0
  RETURN_LONG(zend_hash_num_elements(&intern->storage));
669
0
} /* }}} */
670
671
/* {{{ Rewind to first position */
672
PHP_METHOD(SplObjectStorage, rewind)
673
0
{
674
0
  spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS);
675
676
0
  if (zend_parse_parameters_none() == FAILURE) {
677
0
    RETURN_THROWS();
678
0
  }
679
680
0
  zend_hash_internal_pointer_reset_ex(&intern->storage, &intern->pos);
681
0
  intern->index = 0;
682
0
} /* }}} */
683
684
/* {{{ Returns whether current position is valid */
685
PHP_METHOD(SplObjectStorage, valid)
686
0
{
687
0
  spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS);
688
689
0
  if (zend_parse_parameters_none() == FAILURE) {
690
0
    RETURN_THROWS();
691
0
  }
692
693
0
  RETURN_BOOL(zend_hash_has_more_elements_ex(&intern->storage, &intern->pos) == SUCCESS);
694
0
} /* }}} */
695
696
/* {{{ Returns current key */
697
PHP_METHOD(SplObjectStorage, key)
698
0
{
699
0
  spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS);
700
701
0
  if (zend_parse_parameters_none() == FAILURE) {
702
0
    RETURN_THROWS();
703
0
  }
704
705
0
  RETURN_LONG(intern->index);
706
0
} /* }}} */
707
708
/* {{{ Returns current element */
709
PHP_METHOD(SplObjectStorage, current)
710
0
{
711
0
  spl_SplObjectStorageElement *element;
712
0
  spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS);
713
714
0
  if (zend_parse_parameters_none() == FAILURE) {
715
0
    RETURN_THROWS();
716
0
  }
717
718
0
  if ((element = zend_hash_get_current_data_ptr_ex(&intern->storage, &intern->pos)) == NULL) {
719
0
    zend_throw_exception(spl_ce_RuntimeException, "Called current() on invalid iterator", 0);
720
0
    RETURN_THROWS();
721
0
  }
722
0
  ZVAL_OBJ_COPY(return_value, element->obj);
723
0
} /* }}} */
724
725
/* {{{ Returns associated information to current element */
726
PHP_METHOD(SplObjectStorage, getInfo)
727
0
{
728
0
  spl_SplObjectStorageElement *element;
729
0
  spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS);
730
731
0
  if (zend_parse_parameters_none() == FAILURE) {
732
0
    RETURN_THROWS();
733
0
  }
734
735
0
  if ((element = zend_hash_get_current_data_ptr_ex(&intern->storage, &intern->pos)) == NULL) {
736
0
    RETURN_NULL();
737
0
  }
738
0
  ZVAL_COPY(return_value, &element->inf);
739
0
} /* }}} */
740
741
/* {{{ Sets associated information of current element to $inf */
742
PHP_METHOD(SplObjectStorage, setInfo)
743
0
{
744
0
  spl_SplObjectStorageElement *element;
745
0
  spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS);
746
0
  zval *inf;
747
748
0
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &inf) == FAILURE) {
749
0
    RETURN_THROWS();
750
0
  }
751
752
0
  if ((element = zend_hash_get_current_data_ptr_ex(&intern->storage, &intern->pos)) == NULL) {
753
0
    RETURN_NULL();
754
0
  }
755
0
  zval garbage;
756
0
  ZVAL_COPY_VALUE(&garbage, &element->inf);
757
0
  ZVAL_COPY(&element->inf, inf);
758
0
  zval_ptr_dtor(&garbage);
759
0
} /* }}} */
760
761
/* {{{ Moves position forward */
762
PHP_METHOD(SplObjectStorage, next)
763
0
{
764
0
  spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS);
765
766
0
  if (zend_parse_parameters_none() == FAILURE) {
767
0
    RETURN_THROWS();
768
0
  }
769
770
0
  zend_hash_move_forward_ex(&intern->storage, &intern->pos);
771
0
  intern->index++;
772
0
} /* }}} */
773
774
/* {{{ Seek to position. */
775
PHP_METHOD(SplObjectStorage, seek)
776
0
{
777
0
  zend_long position;
778
0
  spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS);
779
780
0
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &position) == FAILURE) {
781
0
    RETURN_THROWS();
782
0
  }
783
784
0
  if (position < 0 || position >= zend_hash_num_elements(&intern->storage)) {
785
0
    zend_throw_exception_ex(spl_ce_OutOfBoundsException, 0, "Seek position " ZEND_LONG_FMT " is out of range", position);
786
0
    RETURN_THROWS();
787
0
  }
788
789
0
  if (position == 0) {
790
    /* fast path */
791
0
    zend_hash_internal_pointer_reset_ex(&intern->storage, &intern->pos);
792
0
    intern->index = 0;
793
0
  } else if (position > intern->index) {
794
    /* unlike the optimization below, it's not cheap to go to the end */
795
0
    do {
796
0
      zend_hash_move_forward_ex(&intern->storage, &intern->pos);
797
0
      intern->index++;
798
0
    } while (position > intern->index);
799
0
  } else if (position < intern->index) {
800
    /* optimization: check if it's more profitable to reset and do a forwards seek instead, it's cheap to reset */
801
0
    if (intern->index - position > position) {
802
0
      zend_hash_internal_pointer_reset_ex(&intern->storage, &intern->pos);
803
0
      intern->index = 0;
804
0
      do {
805
0
        zend_hash_move_forward_ex(&intern->storage, &intern->pos);
806
0
        intern->index++;
807
0
      } while (position > intern->index);
808
0
    } else {
809
0
      do {
810
0
        zend_hash_move_backwards_ex(&intern->storage, &intern->pos);
811
0
        intern->index--;
812
0
      } while (position < intern->index);
813
0
    }
814
0
  }
815
0
} /* }}} */
816
817
/* {{{ Serializes storage */
818
PHP_METHOD(SplObjectStorage, serialize)
819
0
{
820
0
  spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS);
821
822
0
  spl_SplObjectStorageElement *element;
823
0
  zval members, flags;
824
0
  HashPosition      pos;
825
0
  php_serialize_data_t var_hash;
826
0
  smart_str buf = {0};
827
828
0
  if (zend_parse_parameters_none() == FAILURE) {
829
0
    RETURN_THROWS();
830
0
  }
831
832
0
  PHP_VAR_SERIALIZE_INIT(var_hash);
833
834
  /* storage */
835
0
  smart_str_appendl(&buf, "x:", 2);
836
0
  ZVAL_LONG(&flags, zend_hash_num_elements(&intern->storage));
837
0
  php_var_serialize(&buf, &flags, &var_hash);
838
839
0
  zend_hash_internal_pointer_reset_ex(&intern->storage, &pos);
840
841
0
  while (zend_hash_has_more_elements_ex(&intern->storage, &pos) == SUCCESS) {
842
0
    zval obj;
843
0
    if ((element = zend_hash_get_current_data_ptr_ex(&intern->storage, &pos)) == NULL) {
844
0
      smart_str_free(&buf);
845
0
      PHP_VAR_SERIALIZE_DESTROY(var_hash);
846
0
      RETURN_NULL();
847
0
    }
848
0
    ZVAL_OBJ(&obj, element->obj);
849
850
    /* Protect against modification; we need a full copy because the data may be refcounted. */
851
0
    zval inf_copy;
852
0
    ZVAL_COPY(&inf_copy, &element->inf);
853
854
0
    php_var_serialize(&buf, &obj, &var_hash);
855
0
    smart_str_appendc(&buf, ',');
856
0
    php_var_serialize(&buf, &inf_copy, &var_hash);
857
0
    smart_str_appendc(&buf, ';');
858
0
    zend_hash_move_forward_ex(&intern->storage, &pos);
859
860
0
    zval_ptr_dtor(&inf_copy);
861
0
  }
862
863
  /* members */
864
0
  smart_str_appendl(&buf, "m:", 2);
865
866
0
  ZVAL_ARR(&members, zend_array_dup(zend_std_get_properties(Z_OBJ_P(ZEND_THIS))));
867
0
  php_var_serialize(&buf, &members, &var_hash); /* finishes the string */
868
0
  zval_ptr_dtor(&members);
869
870
  /* done */
871
0
  PHP_VAR_SERIALIZE_DESTROY(var_hash);
872
873
0
  RETURN_STR(smart_str_extract(&buf));
874
0
} /* }}} */
875
876
/* {{{ Unserializes storage */
877
PHP_METHOD(SplObjectStorage, unserialize)
878
4.20k
{
879
4.20k
  spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS);
880
881
4.20k
  char *buf;
882
4.20k
  size_t buf_len;
883
4.20k
  const unsigned char *p, *s;
884
4.20k
  php_unserialize_data_t var_hash;
885
4.20k
  zval *pcount, *pmembers;
886
4.20k
  spl_SplObjectStorageElement *element;
887
4.20k
  zend_long count;
888
889
4.20k
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &buf, &buf_len) == FAILURE) {
890
0
    RETURN_THROWS();
891
0
  }
892
893
4.20k
  if (buf_len == 0) {
894
1
    return;
895
1
  }
896
897
  /* storage */
898
4.20k
  s = p = (const unsigned char*)buf;
899
4.20k
  PHP_VAR_UNSERIALIZE_INIT(var_hash);
900
901
4.20k
  if (*p!= 'x' || *++p != ':') {
902
4
    goto outexcept;
903
4
  }
904
4.20k
  ++p;
905
906
4.20k
  pcount = var_tmp_var(&var_hash);
907
4.20k
  if (!php_var_unserialize(pcount, &p, s + buf_len, &var_hash) || Z_TYPE_P(pcount) != IS_LONG) {
908
3
    goto outexcept;
909
3
  }
910
911
4.19k
  --p; /* for ';' */
912
4.19k
  count = Z_LVAL_P(pcount);
913
4.19k
  if (count < 0) {
914
59
    goto outexcept;
915
59
  }
916
917
12.2k
  while (count-- > 0) {
918
8.23k
    spl_SplObjectStorageElement *pelement;
919
8.23k
    zend_hash_key key;
920
8.23k
    zval *entry = var_tmp_var(&var_hash);
921
8.23k
    zval inf;
922
8.23k
    ZVAL_UNDEF(&inf);
923
924
8.23k
    if (*p != ';') {
925
6
      goto outexcept;
926
6
    }
927
8.23k
    ++p;
928
8.23k
    if(*p != 'O' && *p != 'C' && *p != 'r') {
929
59
      goto outexcept;
930
59
    }
931
    /* store reference to allow cross-references between different elements */
932
8.17k
    if (!php_var_unserialize(entry, &p, s + buf_len, &var_hash)) {
933
26
      goto outexcept;
934
26
    }
935
8.14k
    if (*p == ',') { /* new version has inf */
936
15
      ++p;
937
15
      if (!php_var_unserialize(&inf, &p, s + buf_len, &var_hash)) {
938
1
        zval_ptr_dtor(&inf);
939
1
        goto outexcept;
940
1
      }
941
15
    }
942
8.14k
    if (Z_TYPE_P(entry) != IS_OBJECT) {
943
1
      zval_ptr_dtor(&inf);
944
1
      goto outexcept;
945
1
    }
946
947
8.14k
    if (spl_object_storage_get_hash(&key, intern, Z_OBJ_P(entry)) == FAILURE) {
948
0
      zval_ptr_dtor(&inf);
949
0
      goto outexcept;
950
0
    }
951
8.14k
    pelement = spl_object_storage_get(intern, &key);
952
8.14k
    spl_object_storage_free_hash(intern, &key);
953
8.14k
    if (pelement) {
954
15
      zval obj;
955
15
      if (!Z_ISUNDEF(pelement->inf)) {
956
15
        var_push_dtor(&var_hash, &pelement->inf);
957
15
      }
958
15
      ZVAL_OBJ(&obj, pelement->obj);
959
15
      var_push_dtor(&var_hash, &obj);
960
15
    }
961
8.14k
    element = spl_object_storage_attach(intern, Z_OBJ_P(entry), Z_ISUNDEF(inf)?NULL:&inf);
962
8.14k
    var_replace(&var_hash, &inf, &element->inf);
963
8.14k
    zval_ptr_dtor(&inf);
964
8.14k
  }
965
966
4.04k
  if (*p != ';') {
967
4
    goto outexcept;
968
4
  }
969
4.04k
  ++p;
970
971
  /* members */
972
4.04k
  if (*p!= 'm' || *++p != ':') {
973
2
    goto outexcept;
974
2
  }
975
4.04k
  ++p;
976
977
4.04k
  pmembers = var_tmp_var(&var_hash);
978
4.04k
  if (!php_var_unserialize(pmembers, &p, s + buf_len, &var_hash) || Z_TYPE_P(pmembers) != IS_ARRAY) {
979
4
    goto outexcept;
980
4
  }
981
982
  /* copy members */
983
4.03k
  object_properties_load(&intern->std, Z_ARRVAL_P(pmembers));
984
985
4.03k
  PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
986
4.03k
  return;
987
988
169
outexcept:
989
169
  PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
990
169
  zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0, "Error at offset %zd of %zd bytes", ((char*)p - buf), buf_len);
991
169
  RETURN_THROWS();
992
993
169
} /* }}} */
994
995
/* {{{ */
996
PHP_METHOD(SplObjectStorage, __serialize)
997
10
{
998
10
  spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS);
999
10
  spl_SplObjectStorageElement *elem;
1000
10
  zval tmp;
1001
1002
10
  if (zend_parse_parameters_none() == FAILURE) {
1003
0
    RETURN_THROWS();
1004
0
  }
1005
1006
10
  array_init(return_value);
1007
1008
  /* storage */
1009
10
  array_init_size(&tmp, 2 * zend_hash_num_elements(&intern->storage));
1010
70
  ZEND_HASH_FOREACH_PTR(&intern->storage, elem) {
1011
70
    zval obj;
1012
70
    ZVAL_OBJ_COPY(&obj, elem->obj);
1013
70
    zend_hash_next_index_insert(Z_ARRVAL(tmp), &obj);
1014
70
    Z_TRY_ADDREF(elem->inf);
1015
70
    zend_hash_next_index_insert(Z_ARRVAL(tmp), &elem->inf);
1016
70
  } ZEND_HASH_FOREACH_END();
1017
10
  zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &tmp);
1018
1019
  /* members */
1020
10
  ZVAL_ARR(&tmp, zend_proptable_to_symtable(
1021
10
    zend_std_get_properties(&intern->std), /* always_duplicate */ 1));
1022
10
  zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &tmp);
1023
10
} /* }}} */
1024
1025
/* {{{ */
1026
PHP_METHOD(SplObjectStorage, __unserialize)
1027
17.9k
{
1028
17.9k
  spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS);
1029
17.9k
  HashTable *data;
1030
17.9k
  zval *storage_zv, *members_zv, *key, *val;
1031
1032
17.9k
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "h", &data) == FAILURE) {
1033
0
    RETURN_THROWS();
1034
0
  }
1035
1036
17.9k
  storage_zv = zend_hash_index_find(data, 0);
1037
17.9k
  members_zv = zend_hash_index_find(data, 1);
1038
17.9k
  if (!storage_zv || !members_zv ||
1039
17.9k
      Z_TYPE_P(storage_zv) != IS_ARRAY || Z_TYPE_P(members_zv) != IS_ARRAY) {
1040
44
    zend_throw_exception(spl_ce_UnexpectedValueException,
1041
44
      "Incomplete or ill-typed serialization data", 0);
1042
44
    RETURN_THROWS();
1043
44
  }
1044
1045
17.9k
  if (zend_hash_num_elements(Z_ARRVAL_P(storage_zv)) % 2 != 0) {
1046
1
    zend_throw_exception(spl_ce_UnexpectedValueException, "Odd number of elements", 0);
1047
1
    RETURN_THROWS();
1048
1
  }
1049
1050
17.9k
  key = NULL;
1051
23.8k
  ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(storage_zv), val) {
1052
23.8k
    if (key) {
1053
1.47k
      if (Z_TYPE_P(key) != IS_OBJECT) {
1054
1
        zend_throw_exception(spl_ce_UnexpectedValueException, "Non-object key", 0);
1055
1
        RETURN_THROWS();
1056
1
      }
1057
1058
1.47k
      ZVAL_DEREF(val);
1059
1.47k
      spl_object_storage_attach(intern, Z_OBJ_P(key), val);
1060
1.47k
      key = NULL;
1061
1.47k
    } else {
1062
1.47k
      key = val;
1063
1.47k
    }
1064
23.8k
  } ZEND_HASH_FOREACH_END();
1065
1066
17.9k
  object_properties_load(&intern->std, Z_ARRVAL_P(members_zv));
1067
17.9k
}
1068
1069
/* {{{ */
1070
PHP_METHOD(SplObjectStorage, __debugInfo)
1071
0
{
1072
0
  if (zend_parse_parameters_none() == FAILURE) {
1073
0
    RETURN_THROWS();
1074
0
  }
1075
1076
0
  RETURN_ARR(spl_object_storage_debug_info(Z_OBJ_P(ZEND_THIS)));
1077
0
}
1078
/* }}} */
1079
1080
30
#define SPL_MULTIPLE_ITERATOR_GET_ALL_CURRENT   1
1081
0
#define SPL_MULTIPLE_ITERATOR_GET_ALL_KEY       2
1082
1083
/* {{{ Iterator that iterates over several iterators one after the other */
1084
PHP_METHOD(MultipleIterator, __construct)
1085
7
{
1086
7
  spl_SplObjectStorage   *intern;
1087
7
  zend_long               flags = MIT_NEED_ALL|MIT_KEYS_NUMERIC;
1088
1089
7
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "|l", &flags) == FAILURE) {
1090
0
    RETURN_THROWS();
1091
0
  }
1092
1093
7
  intern = Z_SPLOBJSTORAGE_P(ZEND_THIS);
1094
7
  intern->flags = flags;
1095
7
}
1096
/* }}} */
1097
1098
/* {{{ Return current flags */
1099
PHP_METHOD(MultipleIterator, getFlags)
1100
0
{
1101
0
  spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS);
1102
1103
0
  if (zend_parse_parameters_none() == FAILURE) {
1104
0
    RETURN_THROWS();
1105
0
  }
1106
0
  RETURN_LONG(intern->flags);
1107
0
}
1108
/* }}} */
1109
1110
/* {{{ Set flags */
1111
PHP_METHOD(MultipleIterator, setFlags)
1112
0
{
1113
0
  spl_SplObjectStorage *intern;
1114
0
  intern = Z_SPLOBJSTORAGE_P(ZEND_THIS);
1115
1116
0
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &intern->flags) == FAILURE) {
1117
0
    RETURN_THROWS();
1118
0
  }
1119
0
}
1120
/* }}} */
1121
1122
/* {{{ Attach a new iterator */
1123
PHP_METHOD(MultipleIterator, attachIterator)
1124
12
{
1125
12
  spl_SplObjectStorage *intern;
1126
12
  zend_object *iterator = NULL;
1127
12
  zval zinfo;
1128
12
  zend_string *info_str;
1129
12
  zend_long info_long;
1130
12
  bool info_is_null = 1;
1131
1132
36
  ZEND_PARSE_PARAMETERS_START(1, 2)
1133
48
    Z_PARAM_OBJ_OF_CLASS(iterator, zend_ce_iterator)
1134
12
    Z_PARAM_OPTIONAL
1135
24
    Z_PARAM_STR_OR_LONG_OR_NULL(info_str, info_long, info_is_null)
1136
24
  ZEND_PARSE_PARAMETERS_END();
1137
1138
12
  intern = Z_SPLOBJSTORAGE_P(ZEND_THIS);
1139
1140
12
  if (!info_is_null) {
1141
0
    spl_SplObjectStorageElement *element;
1142
1143
0
    if (info_str) {
1144
0
      ZVAL_STR(&zinfo, info_str);
1145
0
    } else {
1146
0
      ZVAL_LONG(&zinfo, info_long);
1147
0
    }
1148
1149
0
    zend_hash_internal_pointer_reset_ex(&intern->storage, &intern->pos);
1150
0
    while ((element = zend_hash_get_current_data_ptr_ex(&intern->storage, &intern->pos)) != NULL) {
1151
0
      if (fast_is_identical_function(&zinfo, &element->inf)) {
1152
0
        zend_throw_exception(spl_ce_InvalidArgumentException, "Key duplication error", 0);
1153
0
        RETURN_THROWS();
1154
0
      }
1155
0
      zend_hash_move_forward_ex(&intern->storage, &intern->pos);
1156
0
    }
1157
1158
0
    spl_object_storage_attach(intern, iterator, &zinfo);
1159
12
  } else {
1160
12
    spl_object_storage_attach(intern, iterator, NULL);
1161
12
  }
1162
12
}
1163
/* }}} */
1164
1165
/* {{{ Detaches an iterator */
1166
PHP_METHOD(MultipleIterator, detachIterator)
1167
0
{
1168
0
  zval *iterator;
1169
0
  spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS);
1170
1171
0
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &iterator, zend_ce_iterator) == FAILURE) {
1172
0
    RETURN_THROWS();
1173
0
  }
1174
0
  spl_object_storage_detach(intern, Z_OBJ_P(iterator));
1175
1176
0
  zend_hash_internal_pointer_reset_ex(&intern->storage, &intern->pos);
1177
0
  intern->index = 0;
1178
0
} /* }}} */
1179
1180
/* {{{ Determine whether the iterator exists */
1181
PHP_METHOD(MultipleIterator, containsIterator)
1182
0
{
1183
0
  zval *iterator;
1184
0
  spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS);
1185
1186
0
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &iterator, zend_ce_iterator) == FAILURE) {
1187
0
    RETURN_THROWS();
1188
0
  }
1189
0
  RETURN_BOOL(spl_object_storage_contains(intern, Z_OBJ_P(iterator)));
1190
0
} /* }}} */
1191
1192
PHP_METHOD(MultipleIterator, countIterators)
1193
0
{
1194
0
  spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS);
1195
1196
0
  if (zend_parse_parameters_none() == FAILURE) {
1197
0
    RETURN_THROWS();
1198
0
  }
1199
1200
0
  RETURN_LONG(zend_hash_num_elements(&intern->storage));
1201
0
}
1202
1203
/* {{{ Rewind all attached iterator instances */
1204
PHP_METHOD(MultipleIterator, rewind)
1205
5
{
1206
5
  spl_SplObjectStorage        *intern;
1207
5
  spl_SplObjectStorageElement *element;
1208
1209
5
  intern = Z_SPLOBJSTORAGE_P(ZEND_THIS);
1210
1211
5
  if (zend_parse_parameters_none() == FAILURE) {
1212
0
    RETURN_THROWS();
1213
0
  }
1214
1215
5
  zend_hash_internal_pointer_reset_ex(&intern->storage, &intern->pos);
1216
15
  while ((element = zend_hash_get_current_data_ptr_ex(&intern->storage, &intern->pos)) != NULL && !EG(exception)) {
1217
10
    zend_object *it = element->obj;
1218
10
    zend_call_known_instance_method_with_0_params(it->ce->iterator_funcs_ptr->zf_rewind, it, NULL);
1219
10
    zend_hash_move_forward_ex(&intern->storage, &intern->pos);
1220
10
  }
1221
5
}
1222
/* }}} */
1223
1224
/* {{{ Move all attached iterator instances forward */
1225
PHP_METHOD(MultipleIterator, next)
1226
10
{
1227
10
  spl_SplObjectStorage        *intern;
1228
10
  spl_SplObjectStorageElement *element;
1229
1230
10
  intern = Z_SPLOBJSTORAGE_P(ZEND_THIS);
1231
1232
10
  if (zend_parse_parameters_none() == FAILURE) {
1233
0
    RETURN_THROWS();
1234
0
  }
1235
1236
10
  zend_hash_internal_pointer_reset_ex(&intern->storage, &intern->pos);
1237
30
  while ((element = zend_hash_get_current_data_ptr_ex(&intern->storage, &intern->pos)) != NULL && !EG(exception)) {
1238
20
    zend_object *it = element->obj;
1239
20
    zend_call_known_instance_method_with_0_params(it->ce->iterator_funcs_ptr->zf_next, it, NULL);
1240
20
    zend_hash_move_forward_ex(&intern->storage, &intern->pos);
1241
20
  }
1242
10
}
1243
/* }}} */
1244
1245
/* {{{ Return whether all or one sub iterator is valid depending on flags */
1246
PHP_METHOD(MultipleIterator, valid)
1247
15
{
1248
15
  spl_SplObjectStorage        *intern;
1249
15
  spl_SplObjectStorageElement *element;
1250
15
  zval                         retval;
1251
15
  zend_long                         expect, valid;
1252
1253
15
  intern = Z_SPLOBJSTORAGE_P(ZEND_THIS);
1254
1255
15
  if (zend_parse_parameters_none() == FAILURE) {
1256
0
    RETURN_THROWS();
1257
0
  }
1258
1259
15
  if (!zend_hash_num_elements(&intern->storage)) {
1260
0
    RETURN_FALSE;
1261
0
  }
1262
1263
15
  expect = (intern->flags & MIT_NEED_ALL) ? 1 : 0;
1264
1265
15
  zend_hash_internal_pointer_reset_ex(&intern->storage, &intern->pos);
1266
35
  while ((element = zend_hash_get_current_data_ptr_ex(&intern->storage, &intern->pos)) != NULL && !EG(exception)) {
1267
25
    zend_object *it = element->obj;
1268
25
    zend_call_known_instance_method_with_0_params(it->ce->iterator_funcs_ptr->zf_valid, it, &retval);
1269
1270
25
    if (!Z_ISUNDEF(retval)) {
1271
25
      valid = (Z_TYPE(retval) == IS_TRUE);
1272
25
      zval_ptr_dtor(&retval);
1273
25
    } else {
1274
0
      valid = 0;
1275
0
    }
1276
1277
25
    if (expect != valid) {
1278
5
      RETURN_BOOL(!expect);
1279
5
    }
1280
1281
20
    zend_hash_move_forward_ex(&intern->storage, &intern->pos);
1282
20
  }
1283
1284
10
  RETURN_BOOL(expect);
1285
10
}
1286
/* }}} */
1287
1288
static void spl_multiple_iterator_get_all(spl_SplObjectStorage *intern, int get_type, zval *return_value) /* {{{ */
1289
10
{
1290
10
  spl_SplObjectStorageElement *element;
1291
10
  zval                         retval;
1292
10
  int                          valid = 1, num_elements;
1293
1294
10
  num_elements = zend_hash_num_elements(&intern->storage);
1295
10
  if (num_elements < 1) {
1296
0
    zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Called %s() on an invalid iterator",
1297
0
      get_type == SPL_MULTIPLE_ITERATOR_GET_ALL_CURRENT ? "current" : "key");
1298
0
    RETURN_THROWS();
1299
0
  }
1300
1301
10
  array_init_size(return_value, num_elements);
1302
1303
10
  zend_hash_internal_pointer_reset_ex(&intern->storage, &intern->pos);
1304
30
  while ((element = zend_hash_get_current_data_ptr_ex(&intern->storage, &intern->pos)) != NULL && !EG(exception)) {
1305
20
    zend_object *it = element->obj;
1306
20
    zend_call_known_instance_method_with_0_params(it->ce->iterator_funcs_ptr->zf_valid, it, &retval);
1307
1308
20
    if (!Z_ISUNDEF(retval)) {
1309
20
      valid = Z_TYPE(retval) == IS_TRUE;
1310
20
      zval_ptr_dtor(&retval);
1311
20
    } else {
1312
0
      valid = 0;
1313
0
    }
1314
1315
20
    if (valid) {
1316
20
      if (SPL_MULTIPLE_ITERATOR_GET_ALL_CURRENT == get_type) {
1317
20
        zend_call_known_instance_method_with_0_params(it->ce->iterator_funcs_ptr->zf_current, it, &retval);
1318
20
      } else {
1319
0
        zend_call_known_instance_method_with_0_params(it->ce->iterator_funcs_ptr->zf_key, it, &retval);
1320
0
      }
1321
20
      if (Z_ISUNDEF(retval)) {
1322
0
        zend_throw_exception(spl_ce_RuntimeException, "Failed to call sub iterator method", 0);
1323
0
        return;
1324
0
      }
1325
20
    } else if (intern->flags & MIT_NEED_ALL) {
1326
0
      if (SPL_MULTIPLE_ITERATOR_GET_ALL_CURRENT == get_type) {
1327
0
        zend_throw_exception(spl_ce_RuntimeException, "Called current() with non valid sub iterator", 0);
1328
0
      } else {
1329
0
        zend_throw_exception(spl_ce_RuntimeException, "Called key() with non valid sub iterator", 0);
1330
0
      }
1331
0
      return;
1332
0
    } else {
1333
0
      ZVAL_NULL(&retval);
1334
0
    }
1335
1336
20
    if (intern->flags & MIT_KEYS_ASSOC) {
1337
0
      switch (Z_TYPE(element->inf)) {
1338
0
        case IS_LONG:
1339
0
          add_index_zval(return_value, Z_LVAL(element->inf), &retval);
1340
0
          break;
1341
0
        case IS_STRING:
1342
0
          zend_symtable_update(Z_ARRVAL_P(return_value), Z_STR(element->inf), &retval);
1343
0
          break;
1344
0
        default:
1345
0
          zval_ptr_dtor(&retval);
1346
0
          zend_throw_exception(spl_ce_InvalidArgumentException, "Sub-Iterator is associated with NULL", 0);
1347
0
          return;
1348
0
      }
1349
20
    } else {
1350
20
      add_next_index_zval(return_value, &retval);
1351
20
    }
1352
1353
20
    zend_hash_move_forward_ex(&intern->storage, &intern->pos);
1354
20
  }
1355
10
}
1356
/* }}} */
1357
1358
/* {{{ Return an array of all registered Iterator instances current() result */
1359
PHP_METHOD(MultipleIterator, current)
1360
10
{
1361
10
  spl_SplObjectStorage        *intern;
1362
10
  intern = Z_SPLOBJSTORAGE_P(ZEND_THIS);
1363
1364
10
  if (zend_parse_parameters_none() == FAILURE) {
1365
0
    RETURN_THROWS();
1366
0
  }
1367
1368
10
  spl_multiple_iterator_get_all(intern, SPL_MULTIPLE_ITERATOR_GET_ALL_CURRENT, return_value);
1369
10
}
1370
/* }}} */
1371
1372
/* {{{ Return an array of all registered Iterator instances key() result */
1373
PHP_METHOD(MultipleIterator, key)
1374
0
{
1375
0
  spl_SplObjectStorage *intern;
1376
0
  intern = Z_SPLOBJSTORAGE_P(ZEND_THIS);
1377
1378
0
  if (zend_parse_parameters_none() == FAILURE) {
1379
0
    RETURN_THROWS();
1380
0
  }
1381
1382
0
  spl_multiple_iterator_get_all(intern, SPL_MULTIPLE_ITERATOR_GET_ALL_KEY, return_value);
1383
0
}
1384
/* }}} */
1385
1386
/* {{{ PHP_MINIT_FUNCTION(spl_observer) */
1387
PHP_MINIT_FUNCTION(spl_observer)
1388
16
{
1389
16
  spl_ce_SplObserver = register_class_SplObserver();
1390
16
  spl_ce_SplSubject = register_class_SplSubject();
1391
1392
16
  spl_ce_SplObjectStorage = register_class_SplObjectStorage(zend_ce_countable, spl_ce_SeekableIterator, zend_ce_serializable, zend_ce_arrayaccess);
1393
16
  spl_ce_SplObjectStorage->create_object = spl_SplObjectStorage_new;
1394
16
  spl_ce_SplObjectStorage->default_object_handlers = &spl_handler_SplObjectStorage;
1395
1396
16
  memcpy(&spl_handler_SplObjectStorage, &std_object_handlers, sizeof(zend_object_handlers));
1397
1398
16
  spl_handler_SplObjectStorage.offset          = XtOffsetOf(spl_SplObjectStorage, std);
1399
16
  spl_handler_SplObjectStorage.compare         = spl_object_storage_compare_objects;
1400
16
  spl_handler_SplObjectStorage.clone_obj       = spl_object_storage_clone;
1401
16
  spl_handler_SplObjectStorage.get_gc          = spl_object_storage_get_gc;
1402
16
  spl_handler_SplObjectStorage.free_obj        = spl_SplObjectStorage_free_storage;
1403
16
  spl_handler_SplObjectStorage.read_dimension  = spl_object_storage_read_dimension;
1404
16
  spl_handler_SplObjectStorage.write_dimension = spl_object_storage_write_dimension;
1405
16
  spl_handler_SplObjectStorage.has_dimension   = spl_object_storage_has_dimension;
1406
16
  spl_handler_SplObjectStorage.unset_dimension = spl_object_storage_unset_dimension;
1407
1408
16
  memcpy(&spl_handler_MultipleIterator, &spl_handler_SplObjectStorage, sizeof(zend_object_handlers));
1409
1410
16
  spl_handler_MultipleIterator.write_dimension = spl_multiple_iterator_write_dimension;
1411
1412
16
  spl_ce_MultipleIterator = register_class_MultipleIterator(zend_ce_iterator);
1413
16
  spl_ce_MultipleIterator->create_object = spl_SplObjectStorage_new;
1414
16
  spl_ce_MultipleIterator->default_object_handlers = &spl_handler_MultipleIterator;
1415
1416
16
  return SUCCESS;
1417
16
}
1418
/* }}} */