Coverage Report

Created: 2025-11-16 06:23

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
105k
static inline spl_SplObjectStorage *spl_object_storage_from_obj(zend_object *obj) /* {{{ */ {
68
105k
  return (spl_SplObjectStorage*)((char*)(obj) - XtOffsetOf(spl_SplObjectStorage, std));
69
105k
}
70
/* }}} */
71
72
24.0k
#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
24.5k
{
76
24.5k
  spl_SplObjectStorage *intern = spl_object_storage_from_obj(object);
77
78
24.5k
  zend_object_std_dtor(&intern->std);
79
80
24.5k
  zend_hash_destroy(&intern->storage);
81
24.5k
} /* }}} */
82
83
6.84k
static zend_result spl_object_storage_get_hash(zend_hash_key *key, spl_SplObjectStorage *intern, zend_object *obj) {
84
6.84k
  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
6.84k
  } else {
105
6.84k
    key->key = NULL;
106
6.84k
    key->h = obj->handle;
107
6.84k
    return SUCCESS;
108
6.84k
  }
109
6.84k
}
110
111
6.84k
static void spl_object_storage_free_hash(spl_SplObjectStorage *intern, zend_hash_key *key) {
112
6.84k
  if (key->key) {
113
0
    zend_string_release_ex(key->key, 0);
114
0
  }
115
6.84k
}
116
117
static void spl_object_storage_dtor(zval *element) /* {{{ */
118
7.94k
{
119
7.94k
  spl_SplObjectStorageElement *el = Z_PTR_P(element);
120
7.94k
  if (el) {
121
7.94k
    zend_object_release(el->obj);
122
7.94k
    zval_ptr_dtor(&el->inf);
123
7.94k
    efree(el);
124
7.94k
  }
125
7.94k
} /* }}} */
126
127
static spl_SplObjectStorageElement* spl_object_storage_get(spl_SplObjectStorage *intern, zend_hash_key *key) /* {{{ */
128
6.84k
{
129
6.84k
  if (key->key) {
130
0
    return zend_hash_find_ptr(&intern->storage, key->key);
131
6.84k
  } else {
132
6.84k
    return zend_hash_index_find_ptr(&intern->storage, key->h);
133
6.84k
  }
134
6.84k
} /* }}} */
135
136
static spl_SplObjectStorageElement *spl_object_storage_create_element(zend_object *obj, zval *inf) /* {{{ */
137
7.94k
{
138
7.94k
  spl_SplObjectStorageElement *pelement = emalloc(sizeof(spl_SplObjectStorageElement));
139
7.94k
  pelement->obj = obj;
140
7.94k
  GC_ADDREF(obj);
141
7.94k
  if (inf) {
142
1.12k
    ZVAL_COPY(&pelement->inf, inf);
143
6.82k
  } else {
144
6.82k
    ZVAL_NULL(&pelement->inf);
145
6.82k
  }
146
7.94k
  return pelement;
147
7.94k
} /* }}} */
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
7.96k
{
152
7.96k
  uint32_t handle = obj->handle;
153
7.96k
  zval *entry_zv = zend_hash_index_lookup(&intern->storage, handle);
154
7.96k
  spl_SplObjectStorageElement *pelement;
155
7.96k
  ZEND_ASSERT(!(intern->flags & SOS_OVERRIDDEN_WRITE_DIMENSION));
156
157
7.96k
  if (Z_TYPE_P(entry_zv) != IS_NULL) {
158
20
    zval zv_inf;
159
20
    ZEND_ASSERT(Z_TYPE_P(entry_zv) == IS_PTR);
160
20
    pelement = Z_PTR_P(entry_zv);
161
20
    ZVAL_COPY_VALUE(&zv_inf, &pelement->inf);
162
20
    if (inf) {
163
2
      ZVAL_COPY(&pelement->inf, inf);
164
18
    } else {
165
18
      ZVAL_NULL(&pelement->inf);
166
18
    }
167
    /* Call the old value's destructor last, in case it moves the entry */
168
20
    zval_ptr_dtor(&zv_inf);
169
20
    return pelement;
170
20
  }
171
172
  /* NULL initialization necessary because `spl_object_storage_create_element` could bail out due to OOM. */
173
7.94k
  ZVAL_PTR(entry_zv, NULL);
174
7.94k
  pelement = spl_object_storage_create_element(obj, inf);
175
7.94k
  Z_PTR_P(entry_zv) = pelement;
176
7.94k
  return pelement;
177
7.96k
} /* }}} */
178
179
static spl_SplObjectStorageElement *spl_object_storage_attach(spl_SplObjectStorage *intern, zend_object *obj, zval *inf) /* {{{ */
180
7.90k
{
181
7.90k
  if (EXPECTED(!(intern->flags & SOS_OVERRIDDEN_WRITE_DIMENSION))) {
182
7.90k
    return spl_object_storage_attach_handle(intern, obj, inf);
183
7.90k
  }
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
24.5k
{
259
24.5k
  spl_SplObjectStorage *intern;
260
24.5k
  zend_class_entry *parent = class_type;
261
262
24.5k
  intern = zend_object_alloc(sizeof(spl_SplObjectStorage), parent);
263
24.5k
  intern->pos = 0;
264
265
24.5k
  zend_object_std_init(&intern->std, class_type);
266
24.5k
  object_properties_init(&intern->std, class_type);
267
268
24.5k
  zend_hash_init(&intern->storage, 0, NULL, spl_object_storage_dtor, 0);
269
270
24.6k
  while (parent) {
271
24.6k
    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
24.5k
      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
24.5k
      break;
296
24.5k
    }
297
298
78
    parent = parent->parent;
299
78
  }
300
301
24.5k
  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
24.5k
  return &intern->std;
307
24.5k
}
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
57.2k
{
357
57.2k
  spl_SplObjectStorage *intern = spl_object_storage_from_obj(obj);
358
57.2k
  spl_SplObjectStorageElement *element;
359
57.2k
  zend_get_gc_buffer *gc_buffer = zend_get_gc_buffer_create();
360
361
93.2k
  ZEND_HASH_FOREACH_PTR(&intern->storage, element) {
362
93.2k
    zend_get_gc_buffer_add_obj(gc_buffer, element->obj);
363
93.2k
    zend_get_gc_buffer_add_zval(gc_buffer, &element->inf);
364
93.2k
  } ZEND_HASH_FOREACH_END();
365
366
57.2k
  zend_get_gc_buffer_use(gc_buffer, table, n);
367
57.2k
  return zend_std_get_properties(obj);
368
57.2k
}
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
24.5k
{
401
24.5k
  return spl_object_storage_new_ex(class_type, NULL);
402
24.5k
}
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
  ZEND_PARSE_PARAMETERS_NONE();
677
678
0
  zend_hash_internal_pointer_reset_ex(&intern->storage, &intern->pos);
679
0
  intern->index = 0;
680
0
} /* }}} */
681
682
/* {{{ Returns whether current position is valid */
683
PHP_METHOD(SplObjectStorage, valid)
684
0
{
685
0
  spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS);
686
687
0
  ZEND_PARSE_PARAMETERS_NONE();
688
689
0
  RETURN_BOOL(zend_hash_has_more_elements_ex(&intern->storage, &intern->pos) == SUCCESS);
690
0
} /* }}} */
691
692
/* {{{ Returns current key */
693
PHP_METHOD(SplObjectStorage, key)
694
0
{
695
0
  spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS);
696
697
0
  ZEND_PARSE_PARAMETERS_NONE();
698
699
0
  RETURN_LONG(intern->index);
700
0
} /* }}} */
701
702
/* {{{ Returns current element */
703
PHP_METHOD(SplObjectStorage, current)
704
0
{
705
0
  spl_SplObjectStorageElement *element;
706
0
  spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS);
707
708
0
  ZEND_PARSE_PARAMETERS_NONE();
709
710
0
  if ((element = zend_hash_get_current_data_ptr_ex(&intern->storage, &intern->pos)) == NULL) {
711
0
    zend_throw_exception(spl_ce_RuntimeException, "Called current() on invalid iterator", 0);
712
0
    RETURN_THROWS();
713
0
  }
714
0
  ZVAL_OBJ_COPY(return_value, element->obj);
715
0
} /* }}} */
716
717
/* {{{ Returns associated information to current element */
718
PHP_METHOD(SplObjectStorage, getInfo)
719
0
{
720
0
  spl_SplObjectStorageElement *element;
721
0
  spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS);
722
723
0
  ZEND_PARSE_PARAMETERS_NONE();
724
725
0
  if ((element = zend_hash_get_current_data_ptr_ex(&intern->storage, &intern->pos)) == NULL) {
726
0
    RETURN_NULL();
727
0
  }
728
0
  ZVAL_COPY(return_value, &element->inf);
729
0
} /* }}} */
730
731
/* {{{ Sets associated information of current element to $inf */
732
PHP_METHOD(SplObjectStorage, setInfo)
733
0
{
734
0
  spl_SplObjectStorageElement *element;
735
0
  spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS);
736
0
  zval *inf;
737
738
0
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &inf) == FAILURE) {
739
0
    RETURN_THROWS();
740
0
  }
741
742
0
  if ((element = zend_hash_get_current_data_ptr_ex(&intern->storage, &intern->pos)) == NULL) {
743
0
    RETURN_NULL();
744
0
  }
745
0
  zval garbage;
746
0
  ZVAL_COPY_VALUE(&garbage, &element->inf);
747
0
  ZVAL_COPY(&element->inf, inf);
748
0
  zval_ptr_dtor(&garbage);
749
0
} /* }}} */
750
751
/* {{{ Moves position forward */
752
PHP_METHOD(SplObjectStorage, next)
753
0
{
754
0
  spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS);
755
756
0
  ZEND_PARSE_PARAMETERS_NONE();
757
758
0
  zend_hash_move_forward_ex(&intern->storage, &intern->pos);
759
0
  intern->index++;
760
0
} /* }}} */
761
762
/* {{{ Seek to position. */
763
PHP_METHOD(SplObjectStorage, seek)
764
0
{
765
0
  zend_long position;
766
0
  spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS);
767
768
0
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &position) == FAILURE) {
769
0
    RETURN_THROWS();
770
0
  }
771
772
0
  if (position < 0 || position >= zend_hash_num_elements(&intern->storage)) {
773
0
    zend_throw_exception_ex(spl_ce_OutOfBoundsException, 0, "Seek position " ZEND_LONG_FMT " is out of range", position);
774
0
    RETURN_THROWS();
775
0
  }
776
777
0
  if (position == 0) {
778
    /* fast path */
779
0
    zend_hash_internal_pointer_reset_ex(&intern->storage, &intern->pos);
780
0
    intern->index = 0;
781
0
  } else if (position > intern->index) {
782
    /* unlike the optimization below, it's not cheap to go to the end */
783
0
    do {
784
0
      zend_hash_move_forward_ex(&intern->storage, &intern->pos);
785
0
      intern->index++;
786
0
    } while (position > intern->index);
787
0
  } else if (position < intern->index) {
788
    /* optimization: check if it's more profitable to reset and do a forwards seek instead, it's cheap to reset */
789
0
    if (intern->index - position > position) {
790
0
      zend_hash_internal_pointer_reset_ex(&intern->storage, &intern->pos);
791
0
      intern->index = 0;
792
0
      do {
793
0
        zend_hash_move_forward_ex(&intern->storage, &intern->pos);
794
0
        intern->index++;
795
0
      } while (position > intern->index);
796
0
    } else {
797
0
      do {
798
0
        zend_hash_move_backwards_ex(&intern->storage, &intern->pos);
799
0
        intern->index--;
800
0
      } while (position < intern->index);
801
0
    }
802
0
  }
803
0
} /* }}} */
804
805
/* {{{ Serializes storage */
806
PHP_METHOD(SplObjectStorage, serialize)
807
0
{
808
0
  spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS);
809
810
0
  spl_SplObjectStorageElement *element;
811
0
  zval members, flags;
812
0
  HashPosition      pos;
813
0
  php_serialize_data_t var_hash;
814
0
  smart_str buf = {0};
815
816
0
  ZEND_PARSE_PARAMETERS_NONE();
817
818
0
  PHP_VAR_SERIALIZE_INIT(var_hash);
819
820
  /* storage */
821
0
  smart_str_appendl(&buf, "x:", 2);
822
0
  ZVAL_LONG(&flags, zend_hash_num_elements(&intern->storage));
823
0
  php_var_serialize(&buf, &flags, &var_hash);
824
825
0
  zend_hash_internal_pointer_reset_ex(&intern->storage, &pos);
826
827
0
  while (zend_hash_has_more_elements_ex(&intern->storage, &pos) == SUCCESS) {
828
0
    zval obj;
829
0
    if ((element = zend_hash_get_current_data_ptr_ex(&intern->storage, &pos)) == NULL) {
830
0
      smart_str_free(&buf);
831
0
      PHP_VAR_SERIALIZE_DESTROY(var_hash);
832
0
      RETURN_NULL();
833
0
    }
834
0
    ZVAL_OBJ(&obj, element->obj);
835
836
    /* Protect against modification; we need a full copy because the data may be refcounted. */
837
0
    zval inf_copy;
838
0
    ZVAL_COPY(&inf_copy, &element->inf);
839
840
0
    php_var_serialize(&buf, &obj, &var_hash);
841
0
    smart_str_appendc(&buf, ',');
842
0
    php_var_serialize(&buf, &inf_copy, &var_hash);
843
0
    smart_str_appendc(&buf, ';');
844
0
    zend_hash_move_forward_ex(&intern->storage, &pos);
845
846
0
    zval_ptr_dtor(&inf_copy);
847
0
  }
848
849
  /* members */
850
0
  smart_str_appendl(&buf, "m:", 2);
851
852
0
  ZVAL_ARR(&members, zend_array_dup(zend_std_get_properties(Z_OBJ_P(ZEND_THIS))));
853
0
  php_var_serialize(&buf, &members, &var_hash); /* finishes the string */
854
0
  zval_ptr_dtor(&members);
855
856
  /* done */
857
0
  PHP_VAR_SERIALIZE_DESTROY(var_hash);
858
859
0
  RETURN_STR(smart_str_extract(&buf));
860
0
} /* }}} */
861
862
/* {{{ Unserializes storage */
863
PHP_METHOD(SplObjectStorage, unserialize)
864
3.51k
{
865
3.51k
  spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS);
866
867
3.51k
  char *buf;
868
3.51k
  size_t buf_len;
869
3.51k
  const unsigned char *p, *s;
870
3.51k
  php_unserialize_data_t var_hash;
871
3.51k
  zval *pcount, *pmembers;
872
3.51k
  spl_SplObjectStorageElement *element;
873
3.51k
  zend_long count;
874
875
3.51k
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &buf, &buf_len) == FAILURE) {
876
0
    RETURN_THROWS();
877
0
  }
878
879
3.51k
  if (buf_len == 0) {
880
1
    return;
881
1
  }
882
883
  /* storage */
884
3.50k
  s = p = (const unsigned char*)buf;
885
3.50k
  PHP_VAR_UNSERIALIZE_INIT(var_hash);
886
887
3.50k
  if (*p!= 'x' || *++p != ':') {
888
4
    goto outexcept;
889
4
  }
890
3.50k
  ++p;
891
892
3.50k
  pcount = var_tmp_var(&var_hash);
893
3.50k
  if (!php_var_unserialize(pcount, &p, s + buf_len, &var_hash) || Z_TYPE_P(pcount) != IS_LONG) {
894
3
    goto outexcept;
895
3
  }
896
897
3.50k
  --p; /* for ';' */
898
3.50k
  count = Z_LVAL_P(pcount);
899
3.50k
  if (count < 0) {
900
30
    goto outexcept;
901
30
  }
902
903
10.3k
  while (count-- > 0) {
904
6.92k
    spl_SplObjectStorageElement *pelement;
905
6.92k
    zend_hash_key key;
906
6.92k
    zval *entry = var_tmp_var(&var_hash);
907
6.92k
    zval inf;
908
6.92k
    ZVAL_UNDEF(&inf);
909
910
6.92k
    if (*p != ';') {
911
5
      goto outexcept;
912
5
    }
913
6.91k
    ++p;
914
6.91k
    if(*p != 'O' && *p != 'C' && *p != 'r') {
915
41
      goto outexcept;
916
41
    }
917
    /* store reference to allow cross-references between different elements */
918
6.87k
    if (!php_var_unserialize(entry, &p, s + buf_len, &var_hash)) {
919
26
      goto outexcept;
920
26
    }
921
6.85k
    if (*p == ',') { /* new version has inf */
922
13
      ++p;
923
13
      if (!php_var_unserialize(&inf, &p, s + buf_len, &var_hash)) {
924
1
        zval_ptr_dtor(&inf);
925
1
        goto outexcept;
926
1
      }
927
13
    }
928
6.84k
    if (Z_TYPE_P(entry) != IS_OBJECT) {
929
1
      zval_ptr_dtor(&inf);
930
1
      goto outexcept;
931
1
    }
932
933
6.84k
    if (spl_object_storage_get_hash(&key, intern, Z_OBJ_P(entry)) == FAILURE) {
934
0
      zval_ptr_dtor(&inf);
935
0
      goto outexcept;
936
0
    }
937
6.84k
    pelement = spl_object_storage_get(intern, &key);
938
6.84k
    spl_object_storage_free_hash(intern, &key);
939
6.84k
    if (pelement) {
940
20
      zval obj;
941
20
      if (!Z_ISUNDEF(pelement->inf)) {
942
20
        var_push_dtor(&var_hash, &pelement->inf);
943
20
      }
944
20
      ZVAL_OBJ(&obj, pelement->obj);
945
20
      var_push_dtor(&var_hash, &obj);
946
20
    }
947
6.84k
    element = spl_object_storage_attach(intern, Z_OBJ_P(entry), Z_ISUNDEF(inf)?NULL:&inf);
948
6.84k
    var_replace(&var_hash, &inf, &element->inf);
949
6.84k
    zval_ptr_dtor(&inf);
950
6.84k
  }
951
952
3.39k
  if (*p != ';') {
953
3
    goto outexcept;
954
3
  }
955
3.39k
  ++p;
956
957
  /* members */
958
3.39k
  if (*p!= 'm' || *++p != ':') {
959
4
    goto outexcept;
960
4
  }
961
3.39k
  ++p;
962
963
3.39k
  pmembers = var_tmp_var(&var_hash);
964
3.39k
  if (!php_var_unserialize(pmembers, &p, s + buf_len, &var_hash) || Z_TYPE_P(pmembers) != IS_ARRAY) {
965
3
    goto outexcept;
966
3
  }
967
968
  /* copy members */
969
3.38k
  object_properties_load(&intern->std, Z_ARRVAL_P(pmembers));
970
971
3.38k
  PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
972
3.38k
  return;
973
974
121
outexcept:
975
121
  PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
976
121
  zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0, "Error at offset %zd of %zd bytes", ((char*)p - buf), buf_len);
977
121
  RETURN_THROWS();
978
979
121
} /* }}} */
980
981
/* {{{ */
982
PHP_METHOD(SplObjectStorage, __serialize)
983
10
{
984
10
  spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS);
985
10
  spl_SplObjectStorageElement *elem;
986
10
  zval tmp;
987
988
10
  ZEND_PARSE_PARAMETERS_NONE();
989
990
10
  array_init(return_value);
991
992
  /* storage */
993
10
  array_init_size(&tmp, 2 * zend_hash_num_elements(&intern->storage));
994
70
  ZEND_HASH_FOREACH_PTR(&intern->storage, elem) {
995
70
    zval obj;
996
70
    ZVAL_OBJ_COPY(&obj, elem->obj);
997
70
    zend_hash_next_index_insert(Z_ARRVAL(tmp), &obj);
998
70
    Z_TRY_ADDREF(elem->inf);
999
70
    zend_hash_next_index_insert(Z_ARRVAL(tmp), &elem->inf);
1000
70
  } ZEND_HASH_FOREACH_END();
1001
10
  zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &tmp);
1002
1003
  /* members */
1004
10
  ZVAL_ARR(&tmp, zend_proptable_to_symtable(
1005
10
    zend_std_get_properties(&intern->std), /* always_duplicate */ 1));
1006
10
  zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &tmp);
1007
10
} /* }}} */
1008
1009
/* {{{ */
1010
PHP_METHOD(SplObjectStorage, __unserialize)
1011
20.4k
{
1012
20.4k
  spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS);
1013
20.4k
  HashTable *data;
1014
20.4k
  zval *storage_zv, *members_zv, *key, *val;
1015
1016
20.4k
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "h", &data) == FAILURE) {
1017
0
    RETURN_THROWS();
1018
0
  }
1019
1020
20.4k
  storage_zv = zend_hash_index_find(data, 0);
1021
20.4k
  members_zv = zend_hash_index_find(data, 1);
1022
20.4k
  if (!storage_zv || !members_zv ||
1023
20.3k
      Z_TYPE_P(storage_zv) != IS_ARRAY || Z_TYPE_P(members_zv) != IS_ARRAY) {
1024
39
    zend_throw_exception(spl_ce_UnexpectedValueException,
1025
39
      "Incomplete or ill-typed serialization data", 0);
1026
39
    RETURN_THROWS();
1027
39
  }
1028
1029
20.3k
  if (zend_hash_num_elements(Z_ARRVAL_P(storage_zv)) % 2 != 0) {
1030
1
    zend_throw_exception(spl_ce_UnexpectedValueException, "Odd number of elements", 0);
1031
1
    RETURN_THROWS();
1032
1
  }
1033
1034
20.3k
  key = NULL;
1035
24.5k
  ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(storage_zv), val) {
1036
24.5k
    if (key) {
1037
1.05k
      if (Z_TYPE_P(key) != IS_OBJECT) {
1038
1
        zend_throw_exception(spl_ce_UnexpectedValueException, "Non-object key", 0);
1039
1
        RETURN_THROWS();
1040
1
      }
1041
1042
1.04k
      ZVAL_DEREF(val);
1043
1.04k
      spl_object_storage_attach(intern, Z_OBJ_P(key), val);
1044
1.04k
      key = NULL;
1045
1.05k
    } else {
1046
1.05k
      key = val;
1047
1.05k
    }
1048
24.5k
  } ZEND_HASH_FOREACH_END();
1049
1050
20.3k
  object_properties_load(&intern->std, Z_ARRVAL_P(members_zv));
1051
20.3k
}
1052
1053
/* {{{ */
1054
PHP_METHOD(SplObjectStorage, __debugInfo)
1055
0
{
1056
0
  ZEND_PARSE_PARAMETERS_NONE();
1057
1058
0
  RETURN_ARR(spl_object_storage_debug_info(Z_OBJ_P(ZEND_THIS)));
1059
0
}
1060
/* }}} */
1061
1062
30
#define SPL_MULTIPLE_ITERATOR_GET_ALL_CURRENT   1
1063
0
#define SPL_MULTIPLE_ITERATOR_GET_ALL_KEY       2
1064
1065
/* {{{ Iterator that iterates over several iterators one after the other */
1066
PHP_METHOD(MultipleIterator, __construct)
1067
5
{
1068
5
  spl_SplObjectStorage   *intern;
1069
5
  zend_long               flags = MIT_NEED_ALL|MIT_KEYS_NUMERIC;
1070
1071
5
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "|l", &flags) == FAILURE) {
1072
0
    RETURN_THROWS();
1073
0
  }
1074
1075
5
  intern = Z_SPLOBJSTORAGE_P(ZEND_THIS);
1076
5
  intern->flags = flags;
1077
5
}
1078
/* }}} */
1079
1080
/* {{{ Return current flags */
1081
PHP_METHOD(MultipleIterator, getFlags)
1082
0
{
1083
0
  spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS);
1084
1085
0
  ZEND_PARSE_PARAMETERS_NONE();
1086
0
  RETURN_LONG(intern->flags);
1087
0
}
1088
/* }}} */
1089
1090
/* {{{ Set flags */
1091
PHP_METHOD(MultipleIterator, setFlags)
1092
0
{
1093
0
  spl_SplObjectStorage *intern;
1094
0
  intern = Z_SPLOBJSTORAGE_P(ZEND_THIS);
1095
1096
0
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &intern->flags) == FAILURE) {
1097
0
    RETURN_THROWS();
1098
0
  }
1099
0
}
1100
/* }}} */
1101
1102
/* {{{ Attach a new iterator */
1103
PHP_METHOD(MultipleIterator, attachIterator)
1104
10
{
1105
10
  spl_SplObjectStorage *intern;
1106
10
  zend_object *iterator = NULL;
1107
10
  zval zinfo;
1108
10
  zend_string *info_str;
1109
10
  zend_long info_long;
1110
10
  bool info_is_null = 1;
1111
1112
30
  ZEND_PARSE_PARAMETERS_START(1, 2)
1113
40
    Z_PARAM_OBJ_OF_CLASS(iterator, zend_ce_iterator)
1114
10
    Z_PARAM_OPTIONAL
1115
20
    Z_PARAM_STR_OR_LONG_OR_NULL(info_str, info_long, info_is_null)
1116
20
  ZEND_PARSE_PARAMETERS_END();
1117
1118
10
  intern = Z_SPLOBJSTORAGE_P(ZEND_THIS);
1119
1120
10
  if (!info_is_null) {
1121
0
    spl_SplObjectStorageElement *element;
1122
1123
0
    if (info_str) {
1124
0
      ZVAL_STR(&zinfo, info_str);
1125
0
    } else {
1126
0
      ZVAL_LONG(&zinfo, info_long);
1127
0
    }
1128
1129
0
    zend_hash_internal_pointer_reset_ex(&intern->storage, &intern->pos);
1130
0
    while ((element = zend_hash_get_current_data_ptr_ex(&intern->storage, &intern->pos)) != NULL) {
1131
0
      if (fast_is_identical_function(&zinfo, &element->inf)) {
1132
0
        zend_throw_exception(spl_ce_InvalidArgumentException, "Key duplication error", 0);
1133
0
        RETURN_THROWS();
1134
0
      }
1135
0
      zend_hash_move_forward_ex(&intern->storage, &intern->pos);
1136
0
    }
1137
1138
0
    spl_object_storage_attach(intern, iterator, &zinfo);
1139
10
  } else {
1140
10
    spl_object_storage_attach(intern, iterator, NULL);
1141
10
  }
1142
10
}
1143
/* }}} */
1144
1145
/* {{{ Detaches an iterator */
1146
PHP_METHOD(MultipleIterator, detachIterator)
1147
0
{
1148
0
  zval *iterator;
1149
0
  spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS);
1150
1151
0
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &iterator, zend_ce_iterator) == FAILURE) {
1152
0
    RETURN_THROWS();
1153
0
  }
1154
0
  spl_object_storage_detach(intern, Z_OBJ_P(iterator));
1155
1156
0
  zend_hash_internal_pointer_reset_ex(&intern->storage, &intern->pos);
1157
0
  intern->index = 0;
1158
0
} /* }}} */
1159
1160
/* {{{ Determine whether the iterator exists */
1161
PHP_METHOD(MultipleIterator, containsIterator)
1162
0
{
1163
0
  zval *iterator;
1164
0
  spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS);
1165
1166
0
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &iterator, zend_ce_iterator) == FAILURE) {
1167
0
    RETURN_THROWS();
1168
0
  }
1169
0
  RETURN_BOOL(spl_object_storage_contains(intern, Z_OBJ_P(iterator)));
1170
0
} /* }}} */
1171
1172
PHP_METHOD(MultipleIterator, countIterators)
1173
0
{
1174
0
  spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS);
1175
1176
0
  ZEND_PARSE_PARAMETERS_NONE();
1177
1178
0
  RETURN_LONG(zend_hash_num_elements(&intern->storage));
1179
0
}
1180
1181
/* {{{ Rewind all attached iterator instances */
1182
PHP_METHOD(MultipleIterator, rewind)
1183
5
{
1184
5
  spl_SplObjectStorage        *intern;
1185
5
  spl_SplObjectStorageElement *element;
1186
1187
5
  intern = Z_SPLOBJSTORAGE_P(ZEND_THIS);
1188
1189
5
  ZEND_PARSE_PARAMETERS_NONE();
1190
1191
5
  zend_hash_internal_pointer_reset_ex(&intern->storage, &intern->pos);
1192
15
  while ((element = zend_hash_get_current_data_ptr_ex(&intern->storage, &intern->pos)) != NULL && !EG(exception)) {
1193
10
    zend_object *it = element->obj;
1194
10
    zend_call_known_instance_method_with_0_params(it->ce->iterator_funcs_ptr->zf_rewind, it, NULL);
1195
10
    zend_hash_move_forward_ex(&intern->storage, &intern->pos);
1196
10
  }
1197
5
}
1198
/* }}} */
1199
1200
/* {{{ Move all attached iterator instances forward */
1201
PHP_METHOD(MultipleIterator, next)
1202
10
{
1203
10
  spl_SplObjectStorage        *intern;
1204
10
  spl_SplObjectStorageElement *element;
1205
1206
10
  intern = Z_SPLOBJSTORAGE_P(ZEND_THIS);
1207
1208
10
  ZEND_PARSE_PARAMETERS_NONE();
1209
1210
10
  zend_hash_internal_pointer_reset_ex(&intern->storage, &intern->pos);
1211
30
  while ((element = zend_hash_get_current_data_ptr_ex(&intern->storage, &intern->pos)) != NULL && !EG(exception)) {
1212
20
    zend_object *it = element->obj;
1213
20
    zend_call_known_instance_method_with_0_params(it->ce->iterator_funcs_ptr->zf_next, it, NULL);
1214
20
    zend_hash_move_forward_ex(&intern->storage, &intern->pos);
1215
20
  }
1216
10
}
1217
/* }}} */
1218
1219
/* {{{ Return whether all or one sub iterator is valid depending on flags */
1220
PHP_METHOD(MultipleIterator, valid)
1221
15
{
1222
15
  spl_SplObjectStorage        *intern;
1223
15
  spl_SplObjectStorageElement *element;
1224
15
  zval                         retval;
1225
15
  zend_long                         expect, valid;
1226
1227
15
  intern = Z_SPLOBJSTORAGE_P(ZEND_THIS);
1228
1229
15
  ZEND_PARSE_PARAMETERS_NONE();
1230
1231
15
  if (!zend_hash_num_elements(&intern->storage)) {
1232
0
    RETURN_FALSE;
1233
0
  }
1234
1235
15
  expect = (intern->flags & MIT_NEED_ALL) ? 1 : 0;
1236
1237
15
  zend_hash_internal_pointer_reset_ex(&intern->storage, &intern->pos);
1238
35
  while ((element = zend_hash_get_current_data_ptr_ex(&intern->storage, &intern->pos)) != NULL && !EG(exception)) {
1239
25
    zend_object *it = element->obj;
1240
25
    zend_call_known_instance_method_with_0_params(it->ce->iterator_funcs_ptr->zf_valid, it, &retval);
1241
1242
25
    if (!Z_ISUNDEF(retval)) {
1243
25
      valid = (Z_TYPE(retval) == IS_TRUE);
1244
25
      zval_ptr_dtor(&retval);
1245
25
    } else {
1246
0
      valid = 0;
1247
0
    }
1248
1249
25
    if (expect != valid) {
1250
5
      RETURN_BOOL(!expect);
1251
5
    }
1252
1253
20
    zend_hash_move_forward_ex(&intern->storage, &intern->pos);
1254
20
  }
1255
1256
10
  RETURN_BOOL(expect);
1257
10
}
1258
/* }}} */
1259
1260
static void spl_multiple_iterator_get_all(spl_SplObjectStorage *intern, int get_type, zval *return_value) /* {{{ */
1261
10
{
1262
10
  spl_SplObjectStorageElement *element;
1263
10
  zval                         retval;
1264
10
  int                          valid = 1, num_elements;
1265
1266
10
  num_elements = zend_hash_num_elements(&intern->storage);
1267
10
  if (num_elements < 1) {
1268
0
    zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Called %s() on an invalid iterator",
1269
0
      get_type == SPL_MULTIPLE_ITERATOR_GET_ALL_CURRENT ? "current" : "key");
1270
0
    RETURN_THROWS();
1271
0
  }
1272
1273
10
  array_init_size(return_value, num_elements);
1274
1275
10
  zend_hash_internal_pointer_reset_ex(&intern->storage, &intern->pos);
1276
30
  while ((element = zend_hash_get_current_data_ptr_ex(&intern->storage, &intern->pos)) != NULL && !EG(exception)) {
1277
20
    zend_object *it = element->obj;
1278
20
    zend_call_known_instance_method_with_0_params(it->ce->iterator_funcs_ptr->zf_valid, it, &retval);
1279
1280
20
    if (!Z_ISUNDEF(retval)) {
1281
20
      valid = Z_TYPE(retval) == IS_TRUE;
1282
20
      zval_ptr_dtor(&retval);
1283
20
    } else {
1284
0
      valid = 0;
1285
0
    }
1286
1287
20
    if (valid) {
1288
20
      if (SPL_MULTIPLE_ITERATOR_GET_ALL_CURRENT == get_type) {
1289
20
        zend_call_known_instance_method_with_0_params(it->ce->iterator_funcs_ptr->zf_current, it, &retval);
1290
20
      } else {
1291
0
        zend_call_known_instance_method_with_0_params(it->ce->iterator_funcs_ptr->zf_key, it, &retval);
1292
0
      }
1293
20
      if (Z_ISUNDEF(retval)) {
1294
0
        zend_throw_exception(spl_ce_RuntimeException, "Failed to call sub iterator method", 0);
1295
0
        return;
1296
0
      }
1297
20
    } else if (intern->flags & MIT_NEED_ALL) {
1298
0
      if (SPL_MULTIPLE_ITERATOR_GET_ALL_CURRENT == get_type) {
1299
0
        zend_throw_exception(spl_ce_RuntimeException, "Called current() with non valid sub iterator", 0);
1300
0
      } else {
1301
0
        zend_throw_exception(spl_ce_RuntimeException, "Called key() with non valid sub iterator", 0);
1302
0
      }
1303
0
      return;
1304
0
    } else {
1305
0
      ZVAL_NULL(&retval);
1306
0
    }
1307
1308
20
    if (intern->flags & MIT_KEYS_ASSOC) {
1309
0
      switch (Z_TYPE(element->inf)) {
1310
0
        case IS_LONG:
1311
0
          add_index_zval(return_value, Z_LVAL(element->inf), &retval);
1312
0
          break;
1313
0
        case IS_STRING:
1314
0
          zend_symtable_update(Z_ARRVAL_P(return_value), Z_STR(element->inf), &retval);
1315
0
          break;
1316
0
        default:
1317
0
          zval_ptr_dtor(&retval);
1318
0
          zend_throw_exception(spl_ce_InvalidArgumentException, "Sub-Iterator is associated with NULL", 0);
1319
0
          return;
1320
0
      }
1321
20
    } else {
1322
20
      add_next_index_zval(return_value, &retval);
1323
20
    }
1324
1325
20
    zend_hash_move_forward_ex(&intern->storage, &intern->pos);
1326
20
  }
1327
10
}
1328
/* }}} */
1329
1330
/* {{{ Return an array of all registered Iterator instances current() result */
1331
PHP_METHOD(MultipleIterator, current)
1332
10
{
1333
10
  spl_SplObjectStorage        *intern;
1334
10
  intern = Z_SPLOBJSTORAGE_P(ZEND_THIS);
1335
1336
10
  ZEND_PARSE_PARAMETERS_NONE();
1337
1338
10
  spl_multiple_iterator_get_all(intern, SPL_MULTIPLE_ITERATOR_GET_ALL_CURRENT, return_value);
1339
10
}
1340
/* }}} */
1341
1342
/* {{{ Return an array of all registered Iterator instances key() result */
1343
PHP_METHOD(MultipleIterator, key)
1344
0
{
1345
0
  spl_SplObjectStorage *intern;
1346
0
  intern = Z_SPLOBJSTORAGE_P(ZEND_THIS);
1347
1348
0
  ZEND_PARSE_PARAMETERS_NONE();
1349
1350
0
  spl_multiple_iterator_get_all(intern, SPL_MULTIPLE_ITERATOR_GET_ALL_KEY, return_value);
1351
0
}
1352
/* }}} */
1353
1354
/* {{{ PHP_MINIT_FUNCTION(spl_observer) */
1355
PHP_MINIT_FUNCTION(spl_observer)
1356
16
{
1357
16
  spl_ce_SplObserver = register_class_SplObserver();
1358
16
  spl_ce_SplSubject = register_class_SplSubject();
1359
1360
16
  spl_ce_SplObjectStorage = register_class_SplObjectStorage(zend_ce_countable, spl_ce_SeekableIterator, zend_ce_serializable, zend_ce_arrayaccess);
1361
16
  spl_ce_SplObjectStorage->create_object = spl_SplObjectStorage_new;
1362
16
  spl_ce_SplObjectStorage->default_object_handlers = &spl_handler_SplObjectStorage;
1363
1364
16
  memcpy(&spl_handler_SplObjectStorage, &std_object_handlers, sizeof(zend_object_handlers));
1365
1366
16
  spl_handler_SplObjectStorage.offset          = XtOffsetOf(spl_SplObjectStorage, std);
1367
16
  spl_handler_SplObjectStorage.compare         = spl_object_storage_compare_objects;
1368
16
  spl_handler_SplObjectStorage.clone_obj       = spl_object_storage_clone;
1369
16
  spl_handler_SplObjectStorage.get_gc          = spl_object_storage_get_gc;
1370
16
  spl_handler_SplObjectStorage.free_obj        = spl_SplObjectStorage_free_storage;
1371
16
  spl_handler_SplObjectStorage.read_dimension  = spl_object_storage_read_dimension;
1372
16
  spl_handler_SplObjectStorage.write_dimension = spl_object_storage_write_dimension;
1373
16
  spl_handler_SplObjectStorage.has_dimension   = spl_object_storage_has_dimension;
1374
16
  spl_handler_SplObjectStorage.unset_dimension = spl_object_storage_unset_dimension;
1375
1376
16
  memcpy(&spl_handler_MultipleIterator, &spl_handler_SplObjectStorage, sizeof(zend_object_handlers));
1377
1378
16
  spl_handler_MultipleIterator.write_dimension = spl_multiple_iterator_write_dimension;
1379
1380
16
  spl_ce_MultipleIterator = register_class_MultipleIterator(zend_ce_iterator);
1381
16
  spl_ce_MultipleIterator->create_object = spl_SplObjectStorage_new;
1382
16
  spl_ce_MultipleIterator->default_object_handlers = &spl_handler_MultipleIterator;
1383
1384
16
  return SUCCESS;
1385
16
}
1386
/* }}} */