xref: /ext-ds/src/php/objects/php_stack.c (revision 8595b29f)
1 #include "../iterators/php_stack_iterator.h"
2 #include "../handlers/php_stack_handlers.h"
3 #include "../classes/php_stack_ce.h"
4 
5 #include "php_stack.h"
6 
php_ds_stack_create_object_ex(ds_stack_t * stack)7 zend_object *php_ds_stack_create_object_ex(ds_stack_t *stack)
8 {
9 #if PHP_VERSION_ID < 70300
10     php_ds_stack_t *obj = ecalloc(1, sizeof(php_ds_stack_t) + zend_object_properties_size(php_ds_stack_ce));
11 #else
12     php_ds_stack_t *obj = zend_object_alloc(sizeof(php_ds_stack_t), php_ds_stack_ce);
13 #endif
14     zend_object_std_init(&obj->std, php_ds_stack_ce);
15     obj->std.handlers = &php_ds_stack_handlers;
16     obj->stack = stack;
17     return &obj->std;
18 }
19 
php_ds_stack_create_object(zend_class_entry * ce)20 zend_object *php_ds_stack_create_object(zend_class_entry *ce)
21 {
22     return php_ds_stack_create_object_ex(ds_stack());
23 }
24 
php_ds_stack_create_clone(ds_stack_t * stack)25 zend_object *php_ds_stack_create_clone(ds_stack_t *stack)
26 {
27     return php_ds_stack_create_object_ex(ds_stack_clone(stack));
28 }
29 
php_ds_stack_serialize(zval * object,unsigned char ** buffer,size_t * length,zend_serialize_data * data)30 int php_ds_stack_serialize(zval *object, unsigned char **buffer, size_t *length, zend_serialize_data *data)
31 {
32     ds_stack_t *stack = Z_DS_STACK_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 (DS_STACK_SIZE(stack) == 0) {
38         SERIALIZE_SET_ZSTR(ZSTR_EMPTY_ALLOC());
39 
40     } else {
41 
42         zval *value;
43         smart_str buf = {0};
44 
45         DS_VECTOR_FOREACH(stack->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_stack_unserialize(zval * object,zend_class_entry * ce,const unsigned char * buffer,size_t length,zend_unserialize_data * data)59 int php_ds_stack_unserialize(zval *object, zend_class_entry *ce, const unsigned char *buffer, size_t length, zend_unserialize_data *data)
60 {
61     ds_stack_t *stack = ds_stack();
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_stack_push(stack, value);
78     }
79 
80     ZVAL_DS_STACK(object, stack);
81     PHP_VAR_UNSERIALIZE_DESTROY(unserialize_data);
82     return SUCCESS;
83 
84 error:
85     ds_stack_free(stack);
86     PHP_VAR_UNSERIALIZE_DESTROY(unserialize_data);
87     UNSERIALIZE_ERROR();
88     return FAILURE;
89 }
90