Coverage Report

Created: 2025-12-31 07:28

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
0
#define SOS_OVERRIDDEN_READ_DIMENSION  1
47
0
#define SOS_OVERRIDDEN_WRITE_DIMENSION 2
48
0
#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
120k
static inline spl_SplObjectStorage *spl_object_storage_from_obj(zend_object *obj) /* {{{ */ {
68
120k
  return (spl_SplObjectStorage*)((char*)(obj) - XtOffsetOf(spl_SplObjectStorage, std));
69
120k
}
70
/* }}} */
71
72
20.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
31.8k
{
76
31.8k
  spl_SplObjectStorage *intern = spl_object_storage_from_obj(object);
77
78
31.8k
  zend_object_std_dtor(&intern->std);
79
80
31.8k
  zend_hash_destroy(&intern->storage);
81
31.8k
} /* }}} */
82
83
8.46k
static zend_result spl_object_storage_get_hash(zend_hash_key *key, spl_SplObjectStorage *intern, zend_object *obj) {
84
8.46k
  if (UNEXPECTED(intern->fptr_get_hash)) {
85
0
    zval param;
86
0
    zval rv;
87
0
    ZVAL_OBJ(&param, obj);
88
0
    zend_call_method_with_1_params(&intern->std, intern->std.ce, &intern->fptr_get_hash, "getHash", &rv, &param);
89
0
    if (UNEXPECTED(Z_ISUNDEF(rv))) {
90
      /* An exception has occurred */
91
0
      return FAILURE;
92
0
    } else {
93
      /* TODO PHP 9: Remove this as this will be enforced from the return type */
94
0
      if (UNEXPECTED(Z_TYPE(rv) != IS_STRING)) {
95
0
        zend_type_error("%s::getHash(): Return value must be of type string, %s returned",
96
0
          ZSTR_VAL(intern->std.ce->name), zend_zval_value_name(&rv));
97
0
        zval_ptr_dtor(&rv);
98
0
        return FAILURE;
99
0
      } else {
100
0
        key->key = Z_STR(rv);
101
0
        return SUCCESS;
102
0
      }
103
0
    }
104
8.46k
  } else {
105
8.46k
    key->key = NULL;
106
8.46k
    key->h = obj->handle;
107
8.46k
    return SUCCESS;
108
8.46k
  }
109
8.46k
}
110
111
8.46k
static void spl_object_storage_free_hash(spl_SplObjectStorage *intern, zend_hash_key *key) {
112
8.46k
  if (key->key) {
113
0
    zend_string_release_ex(key->key, 0);
114
0
  }
115
8.46k
}
116
117
static void spl_object_storage_dtor(zval *element) /* {{{ */
118
10.3k
{
119
10.3k
  spl_SplObjectStorageElement *el = Z_PTR_P(element);
120
10.3k
  if (el) {
121
10.3k
    zend_object_release(el->obj);
122
10.3k
    zval_ptr_dtor(&el->inf);
123
10.3k
    efree(el);
124
10.3k
  }
125
10.3k
} /* }}} */
126
127
static spl_SplObjectStorageElement* spl_object_storage_get(spl_SplObjectStorage *intern, zend_hash_key *key) /* {{{ */
128
8.46k
{
129
8.46k
  if (key->key) {
130
0
    return zend_hash_find_ptr(&intern->storage, key->key);
131
8.46k
  } else {
132
8.46k
    return zend_hash_index_find_ptr(&intern->storage, key->h);
133
8.46k
  }
134
8.46k
} /* }}} */
135
136
static spl_SplObjectStorageElement *spl_object_storage_create_element(zend_object *obj, zval *inf) /* {{{ */
137
10.3k
{
138
10.3k
  spl_SplObjectStorageElement *pelement = emalloc(sizeof(spl_SplObjectStorageElement));
139
10.3k
  pelement->obj = obj;
140
10.3k
  GC_ADDREF(obj);
141
10.3k
  if (inf) {
142
1.90k
    ZVAL_COPY(&pelement->inf, inf);
143
8.43k
  } else {
144
8.43k
    ZVAL_NULL(&pelement->inf);
145
8.43k
  }
146
10.3k
  return pelement;
147
10.3k
} /* }}} */
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
10.3k
{
152
10.3k
  uint32_t handle = obj->handle;
153
10.3k
  zval *entry_zv = zend_hash_index_lookup(&intern->storage, handle);
154
10.3k
  spl_SplObjectStorageElement *pelement;
155
10.3k
  ZEND_ASSERT(!(intern->flags & SOS_OVERRIDDEN_WRITE_DIMENSION));
156
157
10.3k
  if (Z_TYPE_P(entry_zv) != IS_NULL) {
158
21
    zval zv_inf;
159
21
    ZEND_ASSERT(Z_TYPE_P(entry_zv) == IS_PTR);
160
21
    pelement = Z_PTR_P(entry_zv);
161
21
    ZVAL_COPY_VALUE(&zv_inf, &pelement->inf);
162
21
    if (inf) {
163
2
      ZVAL_COPY(&pelement->inf, inf);
164
19
    } else {
165
19
      ZVAL_NULL(&pelement->inf);
166
19
    }
167
    /* Call the old value's destructor last, in case it moves the entry */
168
21
    zval_ptr_dtor(&zv_inf);
169
21
    return pelement;
170
21
  }
171
172
  /* NULL initialization necessary because `spl_object_storage_create_element` could bail out due to OOM. */
173
10.3k
  ZVAL_PTR(entry_zv, NULL);
174
10.3k
  pelement = spl_object_storage_create_element(obj, inf);
175
10.3k
  Z_PTR_P(entry_zv) = pelement;
176
10.3k
  return pelement;
177
10.3k
} /* }}} */
178
179
static spl_SplObjectStorageElement *spl_object_storage_attach(spl_SplObjectStorage *intern, zend_object *obj, zval *inf) /* {{{ */
180
10.3k
{
181
10.3k
  if (EXPECTED(!(intern->flags & SOS_OVERRIDDEN_WRITE_DIMENSION))) {
182
10.3k
    return spl_object_storage_attach_handle(intern, obj, inf);
183
10.3k
  }
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
0
  (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
31.8k
{
259
31.8k
  spl_SplObjectStorage *intern;
260
31.8k
  zend_class_entry *parent = class_type;
261
262
31.8k
  intern = zend_object_alloc(sizeof(spl_SplObjectStorage), parent);
263
31.8k
  intern->pos = 0;
264
265
31.8k
  zend_object_std_init(&intern->std, class_type);
266
31.8k
  object_properties_init(&intern->std, class_type);
267
268
31.8k
  zend_hash_init(&intern->storage, 0, NULL, spl_object_storage_dtor, 0);
269
270
31.8k
  while (parent) {
271
31.8k
    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
31.7k
      if (class_type != spl_ce_SplObjectStorage) {
275
0
        zend_function *get_hash = zend_hash_str_find_ptr(&class_type->function_table, "gethash", sizeof("gethash") - 1);
276
0
        if (get_hash->common.scope != spl_ce_SplObjectStorage) {
277
0
          intern->fptr_get_hash = get_hash;
278
0
        }
279
0
        if (intern->fptr_get_hash != NULL ||
280
0
          SPL_OBJECT_STORAGE_CLASS_HAS_OVERRIDE(class_type, zf_offsetget) ||
281
0
          SPL_OBJECT_STORAGE_CLASS_HAS_OVERRIDE(class_type, zf_offsetexists)) {
282
0
          intern->flags |= SOS_OVERRIDDEN_READ_DIMENSION;
283
0
        }
284
285
0
        if (intern->fptr_get_hash != NULL ||
286
0
          SPL_OBJECT_STORAGE_CLASS_HAS_OVERRIDE(class_type, zf_offsetset)) {
287
0
          intern->flags |= SOS_OVERRIDDEN_WRITE_DIMENSION;
288
0
        }
289
290
0
        if (intern->fptr_get_hash != NULL ||
291
0
          SPL_OBJECT_STORAGE_CLASS_HAS_OVERRIDE(class_type, zf_offsetunset)) {
292
0
          intern->flags |= SOS_OVERRIDDEN_UNSET_DIMENSION;
293
0
        }
294
0
      }
295
31.7k
      break;
296
31.7k
    }
297
298
30
    parent = parent->parent;
299
30
  }
300
301
31.8k
  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
31.8k
  return &intern->std;
307
31.8k
}
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
68.7k
{
357
68.7k
  spl_SplObjectStorage *intern = spl_object_storage_from_obj(obj);
358
68.7k
  spl_SplObjectStorageElement *element;
359
68.7k
  zend_get_gc_buffer *gc_buffer = zend_get_gc_buffer_create();
360
361
115k
  ZEND_HASH_FOREACH_PTR(&intern->storage, element) {
362
115k
    zend_get_gc_buffer_add_obj(gc_buffer, element->obj);
363
115k
    zend_get_gc_buffer_add_zval(gc_buffer, &element->inf);
364
115k
  } ZEND_HASH_FOREACH_END();
365
366
68.7k
  zend_get_gc_buffer_use(gc_buffer, table, n);
367
68.7k
  return zend_std_get_properties(obj);
368
68.7k
}
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
31.8k
{
401
31.8k
  return spl_object_storage_new_ex(class_type, NULL);
402
31.8k
}
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
0
{
408
0
  if (EXPECTED(!intern->fptr_get_hash)) {
409
0
    return zend_hash_index_find(&intern->storage, obj->handle) != NULL;
410
0
  }
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
0
{
426
0
  zend_object *obj;
427
0
  zval *inf = NULL;
428
429
0
  spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS);
430
431
0
  ZEND_PARSE_PARAMETERS_START(1, 2)
432
0
    Z_PARAM_OBJ(obj)
433
0
    Z_PARAM_OPTIONAL
434
0
    Z_PARAM_ZVAL(inf)
435
0
  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
0
{
461
0
  spl_SplObjectStorage *intern = spl_object_storage_from_obj(object);
462
0
  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
0
    return zend_std_read_dimension(object, offset, type, rv);
465
0
  }
466
0
  spl_SplObjectStorageElement *element = zend_hash_index_find_ptr(&intern->storage, Z_OBJ_HANDLE_P(offset));
467
468
0
  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
0
  } 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
0
    ZVAL_COPY_DEREF(rv, &element->inf);
478
0
    return rv;
479
0
  }
480
0
}
481
482
static void spl_object_storage_write_dimension(zend_object *object, zval *offset, zval *inf)
483
0
{
484
0
  spl_SplObjectStorage *intern = spl_object_storage_from_obj(object);
485
0
  if (UNEXPECTED(offset == NULL || Z_TYPE_P(offset) != IS_OBJECT || (intern->flags & SOS_OVERRIDDEN_WRITE_DIMENSION))) {
486
0
    zend_std_write_dimension(object, offset, inf);
487
0
    return;
488
0
  }
489
0
  spl_object_storage_attach_handle(intern, Z_OBJ_P(offset), inf);
490
0
}
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
0
{
547
0
  zend_object *obj;
548
0
  spl_SplObjectStorageElement *element;
549
0
  spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS);
550
0
  zend_hash_key key;
551
552
0
  ZEND_PARSE_PARAMETERS_START(1, 1)
553
0
    Z_PARAM_OBJ(obj)
554
0
  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
0
{
645
0
  zend_object *obj;
646
0
  spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS);
647
648
0
  ZEND_PARSE_PARAMETERS_START(1, 1)
649
0
    Z_PARAM_OBJ(obj)
650
0
  ZEND_PARSE_PARAMETERS_END();
651
0
  RETURN_BOOL(spl_object_storage_contains(intern, obj));
652
0
} /* }}} */
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
4.33k
{
865
4.33k
  spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS);
866
867
4.33k
  char *buf;
868
4.33k
  size_t buf_len;
869
4.33k
  const unsigned char *p, *s;
870
4.33k
  php_unserialize_data_t var_hash;
871
4.33k
  zval *pcount, *pmembers;
872
4.33k
  spl_SplObjectStorageElement *element;
873
4.33k
  zend_long count;
874
875
4.33k
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &buf, &buf_len) == FAILURE) {
876
0
    RETURN_THROWS();
877
0
  }
878
879
4.33k
  if (buf_len == 0) {
880
1
    return;
881
1
  }
882
883
  /* storage */
884
4.33k
  s = p = (const unsigned char*)buf;
885
4.33k
  PHP_VAR_UNSERIALIZE_INIT(var_hash);
886
887
4.33k
  if (*p!= 'x' || *++p != ':') {
888
2
    goto outexcept;
889
2
  }
890
4.33k
  ++p;
891
892
4.33k
  pcount = var_tmp_var(&var_hash);
893
4.33k
  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
4.32k
  --p; /* for ';' */
898
4.32k
  count = Z_LVAL_P(pcount);
899
4.32k
  if (count < 0) {
900
52
    goto outexcept;
901
52
  }
902
903
12.7k
  while (count-- > 0) {
904
8.53k
    spl_SplObjectStorageElement *pelement;
905
8.53k
    zend_hash_key key;
906
8.53k
    zval *entry = var_tmp_var(&var_hash);
907
8.53k
    zval inf;
908
8.53k
    ZVAL_UNDEF(&inf);
909
910
8.53k
    if (*p != ';') {
911
8
      goto outexcept;
912
8
    }
913
8.53k
    ++p;
914
8.53k
    if(*p != 'O' && *p != 'C' && *p != 'r') {
915
35
      goto outexcept;
916
35
    }
917
    /* store reference to allow cross-references between different elements */
918
8.49k
    if (!php_var_unserialize(entry, &p, s + buf_len, &var_hash)) {
919
24
      goto outexcept;
920
24
    }
921
8.47k
    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
8.47k
    if (Z_TYPE_P(entry) != IS_OBJECT) {
929
1
      zval_ptr_dtor(&inf);
930
1
      goto outexcept;
931
1
    }
932
933
8.46k
    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
8.46k
    pelement = spl_object_storage_get(intern, &key);
938
8.46k
    spl_object_storage_free_hash(intern, &key);
939
8.46k
    if (pelement) {
940
21
      zval obj;
941
21
      if (!Z_ISUNDEF(pelement->inf)) {
942
21
        var_push_dtor(&var_hash, &pelement->inf);
943
21
      }
944
21
      ZVAL_OBJ(&obj, pelement->obj);
945
21
      var_push_dtor(&var_hash, &obj);
946
21
    }
947
8.46k
    element = spl_object_storage_attach(intern, Z_OBJ_P(entry), Z_ISUNDEF(inf)?NULL:&inf);
948
8.46k
    var_replace(&var_hash, &inf, &element->inf);
949
8.46k
    zval_ptr_dtor(&inf);
950
8.46k
  }
951
952
4.20k
  if (*p != ';') {
953
4
    goto outexcept;
954
4
  }
955
4.20k
  ++p;
956
957
  /* members */
958
4.20k
  if (*p!= 'm' || *++p != ':') {
959
4
    goto outexcept;
960
4
  }
961
4.19k
  ++p;
962
963
4.19k
  pmembers = var_tmp_var(&var_hash);
964
4.19k
  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
4.19k
  object_properties_load(&intern->std, Z_ARRVAL_P(pmembers));
970
971
4.19k
  PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
972
4.19k
  return;
973
974
137
outexcept:
975
137
  PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
976
137
  zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0, "Error at offset %zd of %zd bytes", ((char*)p - buf), buf_len);
977
137
  RETURN_THROWS();
978
979
137
} /* }}} */
980
981
/* {{{ */
982
PHP_METHOD(SplObjectStorage, __serialize)
983
0
{
984
0
  spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS);
985
0
  spl_SplObjectStorageElement *elem;
986
0
  zval tmp;
987
988
0
  ZEND_PARSE_PARAMETERS_NONE();
989
990
0
  array_init(return_value);
991
992
  /* storage */
993
0
  array_init_size(&tmp, 2 * zend_hash_num_elements(&intern->storage));
994
0
  ZEND_HASH_FOREACH_PTR(&intern->storage, elem) {
995
0
    zval obj;
996
0
    ZVAL_OBJ_COPY(&obj, elem->obj);
997
0
    zend_hash_next_index_insert(Z_ARRVAL(tmp), &obj);
998
0
    Z_TRY_ADDREF(elem->inf);
999
0
    zend_hash_next_index_insert(Z_ARRVAL(tmp), &elem->inf);
1000
0
  } ZEND_HASH_FOREACH_END();
1001
0
  zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &tmp);
1002
1003
  /* members */
1004
0
  ZVAL_ARR(&tmp, zend_proptable_to_symtable(
1005
0
    zend_std_get_properties(&intern->std), /* always_duplicate */ 1));
1006
0
  zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &tmp);
1007
0
} /* }}} */
1008
1009
/* {{{ */
1010
PHP_METHOD(SplObjectStorage, __unserialize)
1011
15.7k
{
1012
15.7k
  spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS);
1013
15.7k
  HashTable *data;
1014
15.7k
  zval *storage_zv, *members_zv, *key, *val;
1015
1016
15.7k
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "h", &data) == FAILURE) {
1017
0
    RETURN_THROWS();
1018
0
  }
1019
1020
15.7k
  storage_zv = zend_hash_index_find(data, 0);
1021
15.7k
  members_zv = zend_hash_index_find(data, 1);
1022
15.7k
  if (!storage_zv || !members_zv ||
1023
15.7k
      Z_TYPE_P(storage_zv) != IS_ARRAY || Z_TYPE_P(members_zv) != IS_ARRAY) {
1024
57
    zend_throw_exception(spl_ce_UnexpectedValueException,
1025
57
      "Incomplete or ill-typed serialization data", 0);
1026
57
    RETURN_THROWS();
1027
57
  }
1028
1029
15.7k
  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
15.7k
  key = NULL;
1035
23.3k
  ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(storage_zv), val) {
1036
23.3k
    if (key) {
1037
1.90k
      if (Z_TYPE_P(key) != IS_OBJECT) {
1038
3
        zend_throw_exception(spl_ce_UnexpectedValueException, "Non-object key", 0);
1039
3
        RETURN_THROWS();
1040
3
      }
1041
1042
1.90k
      ZVAL_DEREF(val);
1043
1.90k
      spl_object_storage_attach(intern, Z_OBJ_P(key), val);
1044
1.90k
      key = NULL;
1045
1.90k
    } else {
1046
1.90k
      key = val;
1047
1.90k
    }
1048
23.3k
  } ZEND_HASH_FOREACH_END();
1049
1050
15.7k
  object_properties_load(&intern->std, Z_ARRVAL_P(members_zv));
1051
15.7k
}
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
0
#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
0
{
1068
0
  spl_SplObjectStorage   *intern;
1069
0
  zend_long               flags = MIT_NEED_ALL|MIT_KEYS_NUMERIC;
1070
1071
0
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "|l", &flags) == FAILURE) {
1072
0
    RETURN_THROWS();
1073
0
  }
1074
1075
0
  intern = Z_SPLOBJSTORAGE_P(ZEND_THIS);
1076
0
  intern->flags = flags;
1077
0
}
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
0
{
1105
0
  spl_SplObjectStorage *intern;
1106
0
  zend_object *iterator = NULL;
1107
0
  zval zinfo;
1108
0
  zend_string *info_str;
1109
0
  zend_long info_long;
1110
0
  bool info_is_null = 1;
1111
1112
0
  ZEND_PARSE_PARAMETERS_START(1, 2)
1113
0
    Z_PARAM_OBJ_OF_CLASS(iterator, zend_ce_iterator)
1114
0
    Z_PARAM_OPTIONAL
1115
0
    Z_PARAM_STR_OR_LONG_OR_NULL(info_str, info_long, info_is_null)
1116
0
  ZEND_PARSE_PARAMETERS_END();
1117
1118
0
  intern = Z_SPLOBJSTORAGE_P(ZEND_THIS);
1119
1120
0
  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
0
  } else {
1140
0
    spl_object_storage_attach(intern, iterator, NULL);
1141
0
  }
1142
0
}
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
0
{
1184
0
  spl_SplObjectStorage        *intern;
1185
0
  spl_SplObjectStorageElement *element;
1186
1187
0
  intern = Z_SPLOBJSTORAGE_P(ZEND_THIS);
1188
1189
0
  ZEND_PARSE_PARAMETERS_NONE();
1190
1191
0
  zend_hash_internal_pointer_reset_ex(&intern->storage, &intern->pos);
1192
0
  while ((element = zend_hash_get_current_data_ptr_ex(&intern->storage, &intern->pos)) != NULL && !EG(exception)) {
1193
0
    zend_object *it = element->obj;
1194
0
    zend_call_known_instance_method_with_0_params(it->ce->iterator_funcs_ptr->zf_rewind, it, NULL);
1195
0
    zend_hash_move_forward_ex(&intern->storage, &intern->pos);
1196
0
  }
1197
0
}
1198
/* }}} */
1199
1200
/* {{{ Move all attached iterator instances forward */
1201
PHP_METHOD(MultipleIterator, next)
1202
0
{
1203
0
  spl_SplObjectStorage        *intern;
1204
0
  spl_SplObjectStorageElement *element;
1205
1206
0
  intern = Z_SPLOBJSTORAGE_P(ZEND_THIS);
1207
1208
0
  ZEND_PARSE_PARAMETERS_NONE();
1209
1210
0
  zend_hash_internal_pointer_reset_ex(&intern->storage, &intern->pos);
1211
0
  while ((element = zend_hash_get_current_data_ptr_ex(&intern->storage, &intern->pos)) != NULL && !EG(exception)) {
1212
0
    zend_object *it = element->obj;
1213
0
    zend_call_known_instance_method_with_0_params(it->ce->iterator_funcs_ptr->zf_next, it, NULL);
1214
0
    zend_hash_move_forward_ex(&intern->storage, &intern->pos);
1215
0
  }
1216
0
}
1217
/* }}} */
1218
1219
/* {{{ Return whether all or one sub iterator is valid depending on flags */
1220
PHP_METHOD(MultipleIterator, valid)
1221
0
{
1222
0
  spl_SplObjectStorage        *intern;
1223
0
  spl_SplObjectStorageElement *element;
1224
0
  zval                         retval;
1225
0
  zend_long                         expect, valid;
1226
1227
0
  intern = Z_SPLOBJSTORAGE_P(ZEND_THIS);
1228
1229
0
  ZEND_PARSE_PARAMETERS_NONE();
1230
1231
0
  if (!zend_hash_num_elements(&intern->storage)) {
1232
0
    RETURN_FALSE;
1233
0
  }
1234
1235
0
  expect = (intern->flags & MIT_NEED_ALL) ? 1 : 0;
1236
1237
0
  zend_hash_internal_pointer_reset_ex(&intern->storage, &intern->pos);
1238
0
  while ((element = zend_hash_get_current_data_ptr_ex(&intern->storage, &intern->pos)) != NULL && !EG(exception)) {
1239
0
    zend_object *it = element->obj;
1240
0
    zend_call_known_instance_method_with_0_params(it->ce->iterator_funcs_ptr->zf_valid, it, &retval);
1241
1242
0
    if (!Z_ISUNDEF(retval)) {
1243
0
      valid = (Z_TYPE(retval) == IS_TRUE);
1244
0
      zval_ptr_dtor(&retval);
1245
0
    } else {
1246
0
      valid = 0;
1247
0
    }
1248
1249
0
    if (expect != valid) {
1250
0
      RETURN_BOOL(!expect);
1251
0
    }
1252
1253
0
    zend_hash_move_forward_ex(&intern->storage, &intern->pos);
1254
0
  }
1255
1256
0
  RETURN_BOOL(expect);
1257
0
}
1258
/* }}} */
1259
1260
static void spl_multiple_iterator_get_all(spl_SplObjectStorage *intern, int get_type, zval *return_value) /* {{{ */
1261
0
{
1262
0
  spl_SplObjectStorageElement *element;
1263
0
  zval                         retval;
1264
0
  int                          valid = 1, num_elements;
1265
1266
0
  num_elements = zend_hash_num_elements(&intern->storage);
1267
0
  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
0
  array_init_size(return_value, num_elements);
1274
1275
0
  zend_hash_internal_pointer_reset_ex(&intern->storage, &intern->pos);
1276
0
  while ((element = zend_hash_get_current_data_ptr_ex(&intern->storage, &intern->pos)) != NULL && !EG(exception)) {
1277
0
    zend_object *it = element->obj;
1278
0
    zend_call_known_instance_method_with_0_params(it->ce->iterator_funcs_ptr->zf_valid, it, &retval);
1279
1280
0
    if (!Z_ISUNDEF(retval)) {
1281
0
      valid = Z_TYPE(retval) == IS_TRUE;
1282
0
      zval_ptr_dtor(&retval);
1283
0
    } else {
1284
0
      valid = 0;
1285
0
    }
1286
1287
0
    if (valid) {
1288
0
      if (SPL_MULTIPLE_ITERATOR_GET_ALL_CURRENT == get_type) {
1289
0
        zend_call_known_instance_method_with_0_params(it->ce->iterator_funcs_ptr->zf_current, it, &retval);
1290
0
      } else {
1291
0
        zend_call_known_instance_method_with_0_params(it->ce->iterator_funcs_ptr->zf_key, it, &retval);
1292
0
      }
1293
0
      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
0
    } 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
0
    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
0
    } else {
1322
0
      add_next_index_zval(return_value, &retval);
1323
0
    }
1324
1325
0
    zend_hash_move_forward_ex(&intern->storage, &intern->pos);
1326
0
  }
1327
0
}
1328
/* }}} */
1329
1330
/* {{{ Return an array of all registered Iterator instances current() result */
1331
PHP_METHOD(MultipleIterator, current)
1332
0
{
1333
0
  spl_SplObjectStorage        *intern;
1334
0
  intern = Z_SPLOBJSTORAGE_P(ZEND_THIS);
1335
1336
0
  ZEND_PARSE_PARAMETERS_NONE();
1337
1338
0
  spl_multiple_iterator_get_all(intern, SPL_MULTIPLE_ITERATOR_GET_ALL_CURRENT, return_value);
1339
0
}
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
14
{
1357
14
  spl_ce_SplObserver = register_class_SplObserver();
1358
14
  spl_ce_SplSubject = register_class_SplSubject();
1359
1360
14
  spl_ce_SplObjectStorage = register_class_SplObjectStorage(zend_ce_countable, spl_ce_SeekableIterator, zend_ce_serializable, zend_ce_arrayaccess);
1361
14
  spl_ce_SplObjectStorage->create_object = spl_SplObjectStorage_new;
1362
14
  spl_ce_SplObjectStorage->default_object_handlers = &spl_handler_SplObjectStorage;
1363
1364
14
  memcpy(&spl_handler_SplObjectStorage, &std_object_handlers, sizeof(zend_object_handlers));
1365
1366
14
  spl_handler_SplObjectStorage.offset          = XtOffsetOf(spl_SplObjectStorage, std);
1367
14
  spl_handler_SplObjectStorage.compare         = spl_object_storage_compare_objects;
1368
14
  spl_handler_SplObjectStorage.clone_obj       = spl_object_storage_clone;
1369
14
  spl_handler_SplObjectStorage.get_gc          = spl_object_storage_get_gc;
1370
14
  spl_handler_SplObjectStorage.free_obj        = spl_SplObjectStorage_free_storage;
1371
14
  spl_handler_SplObjectStorage.read_dimension  = spl_object_storage_read_dimension;
1372
14
  spl_handler_SplObjectStorage.write_dimension = spl_object_storage_write_dimension;
1373
14
  spl_handler_SplObjectStorage.has_dimension   = spl_object_storage_has_dimension;
1374
14
  spl_handler_SplObjectStorage.unset_dimension = spl_object_storage_unset_dimension;
1375
1376
14
  memcpy(&spl_handler_MultipleIterator, &spl_handler_SplObjectStorage, sizeof(zend_object_handlers));
1377
1378
14
  spl_handler_MultipleIterator.write_dimension = spl_multiple_iterator_write_dimension;
1379
1380
14
  spl_ce_MultipleIterator = register_class_MultipleIterator(zend_ce_iterator);
1381
14
  spl_ce_MultipleIterator->create_object = spl_SplObjectStorage_new;
1382
14
  spl_ce_MultipleIterator->default_object_handlers = &spl_handler_MultipleIterator;
1383
1384
14
  return SUCCESS;
1385
14
}
1386
/* }}} */