Coverage Report

Created: 2025-06-13 06:43

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