xref: /ext-ds/src/php/objects/php_deque.c (revision 8595b29f)
1 // #include "../iterators/php_deque_iterator.h"
2 #include "../handlers/php_deque_handlers.h"
3 #include "../classes/php_deque_ce.h"
4 
5 #include "php_deque.h"
6 
php_ds_deque_create_object_ex(ds_deque_t * deque)7 zend_object *php_ds_deque_create_object_ex(ds_deque_t *deque)
8 {
9 #if PHP_VERSION_ID < 70300
10 	php_ds_deque_t *obj = ecalloc(1, sizeof(php_ds_deque_t) + zend_object_properties_size(php_ds_deque_ce));
11 #else
12     php_ds_deque_t *obj = zend_object_alloc(sizeof(php_ds_deque_t), php_ds_deque_ce);
13 #endif
14     zend_object_std_init(&obj->std, php_ds_deque_ce);
15     obj->std.handlers = &php_deque_handlers;
16     obj->deque = deque;
17     return &obj->std;
18 }
19 
php_ds_deque_create_object(zend_class_entry * ce)20 zend_object *php_ds_deque_create_object(zend_class_entry *ce)
21 {
22     return php_ds_deque_create_object_ex(ds_deque());
23 }
24 
php_ds_deque_create_clone(ds_deque_t * deque)25 zend_object *php_ds_deque_create_clone(ds_deque_t *deque)
26 {
27     return php_ds_deque_create_object_ex(ds_deque_clone(deque));
28 }
29 
php_ds_deque_serialize(zval * object,unsigned char ** buffer,size_t * length,zend_serialize_data * data)30 int php_ds_deque_serialize(zval *object, unsigned char **buffer, size_t *length, zend_serialize_data *data)
31 {
32     ds_deque_t *deque = Z_DS_DEQUE_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 (deque->size == 0) {
38         SERIALIZE_SET_ZSTR(ZSTR_EMPTY_ALLOC());
39 
40     } else {
41 
42         zval *value;
43         smart_str buf = {0};
44 
45         DS_DEQUE_FOREACH(deque, value) {
46             php_var_serialize(&buf, value, &serialize_data);
47         }
48         DS_DEQUE_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_deque_unserialize(zval * object,zend_class_entry * ce,const unsigned char * buffer,size_t length,zend_unserialize_data * data)59 int php_ds_deque_unserialize(zval *object, zend_class_entry *ce, const unsigned char *buffer, size_t length, zend_unserialize_data *data)
60 {
61     ds_deque_t *deque = ds_deque();
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_deque_push(deque, value);
78     }
79 
80     ZVAL_DS_DEQUE(object, deque);
81     PHP_VAR_UNSERIALIZE_DESTROY(unserialize_data);
82     return SUCCESS;
83 
84 error:
85     ds_deque_free(deque);
86     PHP_VAR_UNSERIALIZE_DESTROY(unserialize_data);
87     UNSERIALIZE_ERROR();
88     return FAILURE;
89 }
90