Debian Codegraph @

subprojects/libcdvar-1/src/c-dvar.c

raw
1 /*
2 * Context Management
3 *
4 * This file implements the generic management of the variant objects and other
5 * library features.
6 */
7
8 #include <assert.h>
9 #include <c-stdaux.h>
10 #include <endian.h>
11 #include <errno.h>
12 #include <inttypes.h>
13 #include <stdarg.h>
14 #include <stdbool.h>
15 #include <stdlib.h>
16 #include "c-dvar.h"
17 #include "c-dvar-private.h"
18
19 /**
20 * c_dvar_init() - initialize variant
21 * @var: variant to operate on
22 *
23 * This initializes a variant object that the caller allocated. This is
24 * equivalent to assigning C_DVAR_INIT to the variant.
25 */
26 _c_public_ void c_dvar_init(CDVar *var) {
27 *var = (CDVar)C_DVAR_INIT;
28 }
29
30 /**
31 * c_dvar_deinit() - deinitialize variant
32 * @var: variant to operate on
33 *
34 * This resets the given variant to its initial state. Any allocated data is
35 * released. All pointers to external resources are cleared and any state is
36 * reset to initial values.
37 *
38 * The object is left in a state equivalent to calling c_dvar_init() on it.
39 */
40 _c_public_ void c_dvar_deinit(CDVar *var) {
41 if (var->current)
42 c_dvar_rewind(var);
43
44 if (!var->ro)
45 free(var->data);
46
47 c_dvar_init(var);
48 }
49
50 /**
51 * c_dvar_new() - XXX
52 */
53 _c_public_ int c_dvar_new(CDVar **varp) {
54 _c_cleanup_(c_dvar_freep) CDVar *var = NULL;
55
56 var = malloc(sizeof(*var));
57 if (!var)
58 return -ENOMEM;
59
60 c_dvar_init(var);
61
62 *varp = var;
63 var = NULL;
64 return 0;
65 }
66
67 /**
68 * c_dvar_free() - XXX
69 */
70 _c_public_ CDVar *c_dvar_free(CDVar *var) {
71 if (!var)
72 return NULL;
73
74 c_dvar_deinit(var);
75 free(var);
76
77 return NULL;
78 }
79
80 /**
81 * c_dvar_is_big_endian() - XXX
82 */
83 _c_public_ bool c_dvar_is_big_endian(CDVar *var) {
84 return var->big_endian;
85 }
86
87 /**
88 * c_dvar_get_poison() - XXX
89 */
90 _c_public_ int c_dvar_get_poison(CDVar *var) {
91 return var->poison;
92 }
93
94 /**
95 * c_dvar_get_data() - XXX
96 */
97 _c_public_ void c_dvar_get_data(CDVar *var, void **datap, size_t *n_datap) {
98 if (datap)
99 *datap = var->data;
100 if (n_datap)
101 *n_datap = var->n_data;
102 }
103
104 /**
105 * c_dvar_get_root_type() - XXX
106 */
107 _c_public_ void c_dvar_get_root_types(CDVar *var, const CDVarType **typesp, size_t *n_typesp) {
108 if (typesp)
109 *typesp = var->current ? var->levels[0].parent_types : NULL;
110 if (n_typesp)
111 *n_typesp = var->current ? var->levels[0].n_parent_types : 0;
112 }
113
114 /**
115 * c_dvar_get_parent_type() - XXX
116 */
117 _c_public_ void c_dvar_get_parent_types(CDVar *var, const CDVarType **typesp, size_t *n_typesp) {
118 if (typesp)
119 *typesp = var->current ? var->current->parent_types : NULL;
120 if (n_typesp)
121 *n_typesp = var->current ? var->current->n_parent_types : 0;
122 }
123
124 void c_dvar_rewind(CDVar *var) {
125 for ( ; var->current > var->levels; --var->current)
126 if (var->current->allocated_parent_types)
127 free(var->current->parent_types);
128
129 /* root-level type is always caller-owned */
130 c_assert(!var->current->allocated_parent_types);
131 }
132
133 /*
134 * Internal helper that verifies the next varg-type to read is @c. This is
135 * called as iterator on format-strings for c-dvar variant bulk readers and
136 * writers. That is, @c is the next character in the format string. This
137 * function then verifies it matches what is expected. Otherwise, an error is
138 * returned.
139 */
140 int c_dvar_next_varg(CDVar *var, char c) {
141 char real_c;
142
143 /*
144 * Our varg format string language extends the type strings to become a
145 * full streaming language. To verify they match the elements in the
146 * actual type, we need the real type character to match against. We
147 * store it in @real_c, based on what is given as @c.
148 */
149 switch (c) {
150 case '[':
151 case ']':
152 /* array stream */
153 real_c = 'a';
154 break;
155 case '<':
156 case '>':
157 /* variant stream */
158 real_c = 'v';
159 break;
160 case ')':
161 real_c = '(';
162 break;
163 case '}':
164 real_c = '{';
165 break;
166 default:
167 /* everything else matches exactly */
168 real_c = c;
169 break;
170 }
171
172 switch (c) {
173 case ']':
174 case '>':
175 case ')':
176 case '}':
177 /*
178 * A container can only be exited if fully parsed. This means,
179 * the type iterator must be at the end and the container type
180 * must match.
181 * Note that for arrays the type-iterator is never advanced, so
182 * it is the job of the caller to verify additional constraints
183 * if required.
184 */
185 if ((real_c != 'a' && var->current->n_type) ||
186 var->current->container != real_c)
187 return -ENOTRECOVERABLE;
188
189 break;
190
191 case 'a':
192 case 'v':
193 /*
194 * Arrays and variants must be read one-by-one, using the "[]"
195 * and "<>" operators. There is no atomic reader for an entire
196 * array or variant right now.
197 */
198 return -ENOTRECOVERABLE;
199
200 case '[':
201 case '<':
202 case '(':
203 case '{':
204 /*
205 * Entering a type must not exceed the maximum depth. The type
206 * parser verifies this already, but this can be circumvented
207 * by using nested variants or hand-crafted types.
208 */
209 if (_c_unlikely_(var->current >= var->levels + C_DVAR_TYPE_DEPTH_MAX - 1))
210 return C_DVAR_E_DEPTH_OVERFLOW;
211
212 /* fallthrough */
213 default:
214 if (_c_unlikely_(!var->current->n_type ||
215 var->current->i_type->element != real_c))
216 return -ENOTRECOVERABLE;
217
218 break;
219 }
220
221 return 0;
222 }
223
224 void c_dvar_push(CDVar *var) {
225 ++var->current;
226
227 var->current->parent_types = (var->current - 1)->parent_types;
228 var->current->n_parent_types = (var->current - 1)->n_parent_types;
229 var->current->i_type = (var->current - 1)->i_type + 1;
230 var->current->n_type = (var->current - 1)->i_type->length - 1;
231 var->current->container = (var->current - 1)->i_type->element;
232 var->current->allocated_parent_types = false;
233 var->current->i_buffer = (var->current - 1)->i_buffer;
234
235 if (var->ro)
236 var->current->n_buffer = (var->current - 1)->n_buffer;
237 else
238 var->current->index = 0;
239 }
240
241 void c_dvar_pop(CDVar *var) {
242 size_t n;
243
244 if (var->current->allocated_parent_types)
245 free(var->current->parent_types);
246
247 --var->current;
248
249 n = (var->current + 1)->i_buffer - var->current->i_buffer;
250 var->current->i_buffer += n;
251
252 if (var->ro)
253 var->current->n_buffer -= n;
254 }