Debian Codegraph @

subprojects/libcdvar-1/src/c-dvar-type.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 <errno.h>
11 #include <inttypes.h>
12 #include <stdarg.h>
13 #include <stdbool.h>
14 #include <stdlib.h>
15 #include "c-dvar.h"
16 #include "c-dvar-private.h"
17 #include "c-dvar-type.h"
18
19 static_assert(sizeof(CDVarType) == 4, "Unexpected padding in CDVarType");
20
21 _c_public_ const CDVarType c_dvar_type_y[] = { C_DVAR_T_INIT(C_DVAR_T_y) };
22 _c_public_ const CDVarType c_dvar_type_b[] = { C_DVAR_T_INIT(C_DVAR_T_b) };
23 _c_public_ const CDVarType c_dvar_type_n[] = { C_DVAR_T_INIT(C_DVAR_T_n) };
24 _c_public_ const CDVarType c_dvar_type_q[] = { C_DVAR_T_INIT(C_DVAR_T_q) };
25 _c_public_ const CDVarType c_dvar_type_i[] = { C_DVAR_T_INIT(C_DVAR_T_i) };
26 _c_public_ const CDVarType c_dvar_type_u[] = { C_DVAR_T_INIT(C_DVAR_T_u) };
27 _c_public_ const CDVarType c_dvar_type_x[] = { C_DVAR_T_INIT(C_DVAR_T_x) };
28 _c_public_ const CDVarType c_dvar_type_t[] = { C_DVAR_T_INIT(C_DVAR_T_t) };
29 _c_public_ const CDVarType c_dvar_type_h[] = { C_DVAR_T_INIT(C_DVAR_T_h) };
30 _c_public_ const CDVarType c_dvar_type_d[] = { C_DVAR_T_INIT(C_DVAR_T_d) };
31 _c_public_ const CDVarType c_dvar_type_s[] = { C_DVAR_T_INIT(C_DVAR_T_s) };
32 _c_public_ const CDVarType c_dvar_type_o[] = { C_DVAR_T_INIT(C_DVAR_T_o) };
33 _c_public_ const CDVarType c_dvar_type_g[] = { C_DVAR_T_INIT(C_DVAR_T_g) };
34 _c_public_ const CDVarType c_dvar_type_v[] = { C_DVAR_T_INIT(C_DVAR_T_v) };
35 _c_public_ const CDVarType c_dvar_type_unit[] = { C_DVAR_T_INIT(C_DVAR_T_TUPLE0) };
36
37 static const CDVarType *c_dvar_type_builtins[256] = {
38 ['y'] = c_dvar_type_y,
39 ['b'] = c_dvar_type_b,
40 ['n'] = c_dvar_type_n,
41 ['q'] = c_dvar_type_q,
42 ['i'] = c_dvar_type_i,
43 ['u'] = c_dvar_type_u,
44 ['x'] = c_dvar_type_x,
45 ['t'] = c_dvar_type_t,
46 ['h'] = c_dvar_type_h,
47 ['d'] = c_dvar_type_d,
48 ['s'] = c_dvar_type_s,
49 ['o'] = c_dvar_type_o,
50 ['g'] = c_dvar_type_g,
51 ['v'] = c_dvar_type_v,
52 };
53
54 /**
55 * c_dvar_type_new_from_signature() - parse D-Bus type from type signature
56 * @typep: output argument to store allocated type array
57 * @signature: type signature
58 * @n_signature: length of type signature
59 *
60 * This parses the D-Bus type signature @signature of length @n_signature (it
61 * does not require 0-termination) and stores the first fully parsed type in
62 * @typep.
63 *
64 * On success, this fills in a type array in @typep. If @typep is non-NULL, it
65 * must point to a pre-allocated array of the correct length (at most
66 * @n_signature elements). If @typep is NULL, it is dynamically allocated and
67 * the caller is responsible to free it via c_dvar_type_free() once done.
68 *
69 * In case @typep is non-NULL and pre-allocated by the caller, this function
70 * might modify it even if it ends up returning an error code. Hence, the
71 * contents of the type array are undefined on failure.
72 *
73 * This function parses the type signature from the beginning until it fully
74 * parsed a single complete type. The remaining bytes of the signature are not
75 * looked at. The length of the parsed signature can be retrieved at
76 * typep->length on success.
77 *
78 * Return: <0 on fatal failure,
79 * 0 on success,
80 * C_DVAR_E_OVERLONG_TYPE if type exceeds length limits,
81 * C_DVAR_E_DEPTH_OVERFLOW if type exceeds depth limits,
82 * C_DVAR_E_INVALID_TYPE if element constellation is not valid.
83 */
84 _c_public_ int c_dvar_type_new_from_signature(CDVarType **typep, const char *signature, size_t n_signature) {
85 _c_cleanup_(c_dvar_type_freep) CDVarType *allocated_type = NULL;
86 CDVarType *type, *stack[C_DVAR_TYPE_DEPTH_MAX], *container, *this;
87 size_t i, i_container, n_type, depth, depth_tuple;
88 const CDVarType *builtin;
89 char c;
90
91 /*
92 * As a first step, figure out how long the next type in @signature is.
93 * This requires iterating the entire type, counting opening/closing
94 * brackets. We do this up-front, with only limited type validation.
95 * Knowing the final type-size allows pre-allocating @type.
96 *
97 * Note that empty signatures will be rejected by this. The caller is
98 * responsible to check for empty signatures, otherwise you might end
99 * up in infinite loops.
100 */
101
102 n_type = 0;
103 depth = 0;
104
105 do {
106 if (_c_unlikely_(n_type >= n_signature || n_type >= C_DVAR_TYPE_LENGTH_MAX))
107 return C_DVAR_E_OVERLONG_TYPE;
108
109 c = signature[n_type++];
110 switch (c) {
111 case '(':
112 case '{':
113 ++depth;
114 break;
115 case ')':
116 case '}':
117 if (!depth--)
118 return C_DVAR_E_DEPTH_OVERFLOW;
119 break;
120 }
121 } while (c == 'a' || depth);
122
123 /*
124 * Now that we know the type length, we pre-allocate @type. Iterate the
125 * type again and parse all required information into the type array.
126 *
127 * While parsing, @this points at the current position in @type and
128 * needs to be filled in. @container points at the parent container of
129 * @this (or NULL if root level). @i_container is the offset of
130 * @container compared to @type (0 if NULL).
131 */
132
133 if (*typep) {
134 type = *typep;
135 } else {
136 allocated_type = malloc(n_type * sizeof(*allocated_type));
137 if (!allocated_type)
138 return -ENOMEM;
139
140 type = allocated_type;
141 }
142
143 depth = 0;
144 depth_tuple = 0;
145 container = NULL;
146
147 for (i = 0; i < n_type; ++i) {
148 c = signature[i];
149 this = type + i;
150 builtin = c_dvar_type_builtins[(uint8_t)c];
151 i_container = container ? (container - type) : 0;
152
153 /*
154 * In case our surrounding container is a DICT, we need to make
155 * sure that the _first_ following type is basic, and there are
156 * exactly 2 types following.
157 */
158 if (container && container->element == '{') {
159 if (i < i_container + 2) {
160 /* first type must be basic */
161 if (_c_unlikely_(!builtin || !builtin->basic))
162 return C_DVAR_E_INVALID_TYPE;
163 } else if (i == i_container + 2) {
164 /* there must be a second type */
165 if (_c_unlikely_(c == '}'))
166 return C_DVAR_E_INVALID_TYPE;
167 } else if (i > i_container + 2) {
168 /* DICT is closed after second type */
169 if (_c_unlikely_(c != '}'))
170 return C_DVAR_E_INVALID_TYPE;
171 }
172 }
173
174 switch (c) {
175 case '(':
176 case '{':
177 ++depth_tuple;
178 /* fallthrough */
179 case 'a':
180 ++depth;
181
182 /*
183 * D-Bus has very clear depth restrictions. Total depth
184 * must not exceed 64 (DEPTH_MAX), combined tuple depth
185 * must not exceed 32, nor can array depth exceed 32.
186 * We verify all three here, even though our
187 * implementation would work with a unified limit.
188 */
189 if (_c_unlikely_(depth > C_DVAR_TYPE_DEPTH_MAX))
190 return C_DVAR_E_DEPTH_OVERFLOW;
191 if (_c_unlikely_(depth_tuple > C_DVAR_TYPE_DEPTH_MAX / 2))
192 return C_DVAR_E_DEPTH_OVERFLOW;
193 if (_c_unlikely_(depth - depth_tuple > C_DVAR_TYPE_DEPTH_MAX / 2))
194 return C_DVAR_E_DEPTH_OVERFLOW;
195
196 this->size = 0;
197 this->alignment = 2 + (c != 'a');
198 this->element = c;
199 this->length = 1 + (c != 'a');
200 this->basic = 0;
201
202 /*
203 * We opened a new container type, so continue with the
204 * next character. Skip handling terminal types below.
205 */
206 stack[depth - 1] = this;
207 container = this;
208 continue;
209
210 case '}':
211 case ')':
212 if (_c_unlikely_(!container || container->element != ((c == '}') ? '{' : '(')))
213 return C_DVAR_E_INVALID_TYPE;
214
215 this->size = 0;
216 this->alignment = 0;
217 this->element = c;
218 this->length = 1;
219 this->basic = 0;
220
221 /*
222 * We closed a container. This is a terminal type,
223 * which will be handled below. Make sure to update
224 * our state accordingly.
225 */
226 this = container;
227 container = --depth ? stack[depth - 1] : NULL;
228 --depth_tuple;
229 break;
230
231 default:
232 if (_c_unlikely_(!builtin))
233 return C_DVAR_E_INVALID_TYPE;
234
235 /*
236 * This a a builtin type. It is always a terminal type,
237 * and will be handled below. Simply copy the builtin
238 * information into the type array;
239 */
240 *this = *builtin;
241 break;
242 }
243
244 /*
245 * At this point we handle terminal types. That means, we know
246 * that a full type was finished here. If the parent container
247 * is an array, it has to be completed here. If the parent
248 * container is a tuple, we need to account for the type.
249 */
250
251 while (container && container->element == 'a') {
252 container->length += this->length;
253
254 this = container;
255 container = --depth ? stack[depth - 1] : NULL;
256 }
257
258 if (container) {
259 if (this->size && (this == container + 1 || container->size)) {
260 container->size = c_align_to((size_t)container->size, 1 << this->alignment);
261 container->size += this->size;
262 } else {
263 container->size = 0;
264 }
265
266 container->length += this->length;
267 }
268
269 /*
270 * If, after handling a terminal type, we are back at the root
271 * level, the type must be complete. Hence, return the
272 * information to the caller.
273 */
274
275 if (!container) {
276 *typep = type;
277 allocated_type = NULL;
278 return 0;
279 }
280 }
281
282 return C_DVAR_E_INVALID_TYPE;
283 }
284
285 /**
286 * c_dvar_type_free() - free type object
287 * @type: type to free, or NULL
288 *
289 * This deallocates the memory for @type, previously allocated by one of the
290 * constructors.
291 *
292 * If @type is NULL, this is a no-op.
293 *
294 * Return: NULL is returned.
295 */
296 _c_public_ CDVarType *c_dvar_type_free(CDVarType *type) {
297 free(type);
298 return NULL;
299 }
300
301 /**
302 * c_dvar_type_compare_string() - compare type to string representation
303 * @subject: type to compare, or NULL
304 * @object: string representation to compare against
305 * @n_object: length of @object
306 *
307 * This compares @subject to the string representation of a valid type, given
308 * as @object (with length @n_object). This returns -1, 0, or 1, if @subject
309 * orders before, equals, or orders after @object, respectively.
310 *
311 * Note that no validation of either input is done. This means, if the
312 * arguments do not equal, then the string representation might be an invalid
313 * type.
314 *
315 * Note that @subject can be NULL, even though this would not represent a valid
316 * type.
317 *
318 * Return: -1, 0, or 1, if @subject orders before, equals, or orders after
319 * @object, respectively.
320 */
321 _c_public_ int c_dvar_type_compare_string(const CDVarType *subject, const char *object, size_t n_object) {
322 static const CDVarType null = {};
323 int diff;
324
325 subject = subject ?: &null;
326
327 if (subject->length != n_object)
328 return (subject->length > n_object) ? 1 : -1;
329
330 while (n_object-- > 0) {
331 diff = subject++->element - *object++;
332 if (diff)
333 return (diff > 0) ? 1 : -1 ;
334 }
335
336 return 0;
337 }