xref: /ext-ds/src/php/objects/php_set.c (revision 8595b29f)
1 #include "../iterators/php_set_iterator.h"
2 #include "../handlers/php_set_handlers.h"
3 #include "../classes/php_set_ce.h"
4 
5 #include "php_set.h"
6 
php_ds_set_create_object_ex(ds_set_t * set)7 zend_object *php_ds_set_create_object_ex(ds_set_t *set)
8 {
9 #if PHP_VERSION_ID < 70300
10     php_ds_set_t *obj = ecalloc(1, sizeof(php_ds_set_t) + zend_object_properties_size(php_ds_set_ce));
11 #else
12     php_ds_set_t *obj = zend_object_alloc(sizeof(php_ds_set_t), php_ds_set_ce);
13 #endif
14     zend_object_std_init(&obj->std, php_ds_set_ce);
15     obj->std.handlers = &php_ds_set_handlers;
16     obj->set = set;
17     return &obj->std;
18 }
19 
php_ds_set_create_object(zend_class_entry * ce)20 zend_object *php_ds_set_create_object(zend_class_entry *ce)
21 {
22     return php_ds_set_create_object_ex(ds_set());
23 }
24 
php_ds_set_create_clone(ds_set_t * set)25 zend_object *php_ds_set_create_clone(ds_set_t *set)
26 {
27     return php_ds_set_create_object_ex(ds_set_clone(set));
28 }
29 
php_ds_set_serialize(zval * object,unsigned char ** buffer,size_t * length,zend_serialize_data * data)30 int php_ds_set_serialize(zval *object, unsigned char **buffer, size_t *length, zend_serialize_data *data)
31 {
32     ds_set_t *set = Z_DS_SET_P(object);
33 
34     php_serialize_data_t serialize_data = (php_serialize_data_t) data;
35     PHP_VAR_SERIALIZE_INIT(serialize_data);
36 
37     if (set->table->size == 0) {
38         SERIALIZE_SET_ZSTR(ZSTR_EMPTY_ALLOC());
39 
40     } else {
41 
42         zval *key;
43         smart_str buf = {0};
44 
45         DS_HTABLE_FOREACH_KEY(set->table, key) {
46             php_var_serialize(&buf, key, &serialize_data);
47         }
48         DS_HTABLE_FOREACH_END();
49 
50         smart_str_0(&buf);
51         SERIALIZE_SET_ZSTR(buf.s);
52         zend_string_release(buf.s);
53     }
54 
55     PHP_VAR_SERIALIZE_DESTROY(serialize_data);
56     return SUCCESS;
57 }
58 
php_ds_set_unserialize(zval * object,zend_class_entry * ce,const unsigned char * buffer,size_t length,zend_unserialize_data * data)59 int php_ds_set_unserialize(zval *object, zend_class_entry *ce, const unsigned char *buffer, size_t length, zend_unserialize_data *data)
60 {
61     ds_set_t *set = ds_set();
62 
63     php_unserialize_data_t unserialize_data = (php_unserialize_data_t) data;
64 
65     const unsigned char *pos = buffer;
66     const unsigned char *end = buffer + length;
67 
68     PHP_VAR_UNSERIALIZE_INIT(unserialize_data);
69     ZVAL_DS_SET(object, set);
70 
71     while (pos != end) {
72         zval *value = var_tmp_var(&unserialize_data);
73 
74         if ( ! php_var_unserialize(value, &pos, end, &unserialize_data)) {
75             goto error;
76         }
77 
78         ds_set_add(set, value);
79     }
80 
81     PHP_VAR_UNSERIALIZE_DESTROY(unserialize_data);
82     return SUCCESS;
83 
84 error:
85     ds_set_free(set);
86     PHP_VAR_UNSERIALIZE_DESTROY(unserialize_data);
87     UNSERIALIZE_ERROR();
88     return FAILURE;
89 }
90