1 // #include "../iterators/php_vector_iterator.h"
2 #include "../handlers/php_vector_handlers.h"
3 #include "../classes/php_vector_ce.h"
4
5 #include "php_vector.h"
6
php_ds_vector_create_object_ex(ds_vector_t * vector)7 zend_object *php_ds_vector_create_object_ex(ds_vector_t *vector)
8 {
9 #if PHP_VERSION_ID < 70300
10 php_ds_vector_t *obj = ecalloc(1, sizeof(php_ds_vector_t) + zend_object_properties_size(php_ds_vector_ce));
11 #else
12 php_ds_vector_t *obj = zend_object_alloc(sizeof(php_ds_vector_t), php_ds_vector_ce);
13 #endif
14 zend_object_std_init(&obj->std, php_ds_vector_ce);
15 obj->std.handlers = &php_vector_handlers;
16 obj->vector = vector;
17
18 return &obj->std;
19 }
20
php_ds_vector_create_object(zend_class_entry * ce)21 zend_object *php_ds_vector_create_object(zend_class_entry *ce)
22 {
23 return php_ds_vector_create_object_ex(ds_vector());
24 }
25
php_ds_vector_create_clone(ds_vector_t * vector)26 zend_object *php_ds_vector_create_clone(ds_vector_t *vector)
27 {
28 return php_ds_vector_create_object_ex(ds_vector_clone(vector));
29 }
30
php_ds_vector_serialize(zval * object,unsigned char ** buffer,size_t * length,zend_serialize_data * data)31 int php_ds_vector_serialize(zval *object, unsigned char **buffer, size_t *length, zend_serialize_data *data)
32 {
33 ds_vector_t *vector = Z_DS_VECTOR_P(object);
34
35 php_serialize_data_t serialize_data = (php_serialize_data_t) data;
36 PHP_VAR_SERIALIZE_INIT(serialize_data);
37
38 if (DS_VECTOR_IS_EMPTY(vector)) {
39 SERIALIZE_SET_ZSTR(ZSTR_EMPTY_ALLOC());
40
41 } else {
42 zval *value;
43 smart_str buf = {0};
44
45 DS_VECTOR_FOREACH(vector, value) {
46 php_var_serialize(&buf, value, &serialize_data);
47 }
48 DS_VECTOR_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_vector_unserialize(zval * obj,zend_class_entry * ce,const unsigned char * buffer,size_t length,zend_unserialize_data * data)59 int php_ds_vector_unserialize(zval *obj, zend_class_entry *ce, const unsigned char *buffer, size_t length, zend_unserialize_data *data)
60 {
61 ds_vector_t *vector = ds_vector();
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
70 while (pos != end) {
71 zval *value = var_tmp_var(&unserialize_data);
72
73 if ( ! php_var_unserialize(value, &pos, end, &unserialize_data)) {
74 goto error;
75 }
76
77 ds_vector_push(vector, value);
78 }
79
80 ZVAL_DS_VECTOR(obj, vector);
81 PHP_VAR_UNSERIALIZE_DESTROY(unserialize_data);
82 return SUCCESS;
83
84 error:
85 ds_vector_free(vector);
86 PHP_VAR_UNSERIALIZE_DESTROY(unserialize_data);
87 UNSERIALIZE_ERROR();
88 return FAILURE;
89 }
90