1 | /*
|
---|
2 | * Copyright (c) 2012 Sean Bartell
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup bithenge
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /**
|
---|
33 | * @file
|
---|
34 | * Transforms.
|
---|
35 | */
|
---|
36 |
|
---|
37 | #include <assert.h>
|
---|
38 | #include <errno.h>
|
---|
39 | #include <stdarg.h>
|
---|
40 | #include <stdlib.h>
|
---|
41 | #include <bithenge/blob.h>
|
---|
42 | #include <bithenge/print.h>
|
---|
43 | #include <bithenge/transform.h>
|
---|
44 | #include "common.h"
|
---|
45 |
|
---|
46 |
|
---|
47 |
|
---|
48 | /***************** transform *****************/
|
---|
49 |
|
---|
50 | /** Initialize a new transform.
|
---|
51 | * @param[out] self Transform to initialize.
|
---|
52 | * @param[in] ops Operations provided by the transform.
|
---|
53 | * @param num_params The number of parameters required. If this is nonzero, the
|
---|
54 | * transform will get its own context with parameters, probably provided by a
|
---|
55 | * param_wrapper. If this is zero, the existing outer context will be used with
|
---|
56 | * whatever parameters it has, so they can be passed to any param_wrappers
|
---|
57 | * within.
|
---|
58 | * @return EOK or an error code from errno.h.
|
---|
59 | */
|
---|
60 | errno_t bithenge_init_transform(bithenge_transform_t *self,
|
---|
61 | const bithenge_transform_ops_t *ops, int num_params)
|
---|
62 | {
|
---|
63 | assert(ops);
|
---|
64 | assert(ops->apply || ops->prefix_apply);
|
---|
65 | assert(ops->destroy);
|
---|
66 | if (bithenge_should_fail())
|
---|
67 | return ENOMEM;
|
---|
68 | self->ops = ops;
|
---|
69 | self->refs = 1;
|
---|
70 | self->num_params = num_params;
|
---|
71 | return EOK;
|
---|
72 | }
|
---|
73 |
|
---|
74 | static void transform_indestructible(bithenge_transform_t *self)
|
---|
75 | {
|
---|
76 | assert(false);
|
---|
77 | }
|
---|
78 |
|
---|
79 | /** Apply a transform. Takes ownership of nothing.
|
---|
80 | * @memberof bithenge_transform_t
|
---|
81 | * @param self The transform.
|
---|
82 | * @param scope The scope.
|
---|
83 | * @param in The input tree.
|
---|
84 | * @param[out] out Where the output tree will be stored.
|
---|
85 | * @return EOK on success or an error code from errno.h.
|
---|
86 | */
|
---|
87 | errno_t bithenge_transform_apply(bithenge_transform_t *self,
|
---|
88 | bithenge_scope_t *scope, bithenge_node_t *in, bithenge_node_t **out)
|
---|
89 | {
|
---|
90 | assert(self);
|
---|
91 | assert(self->ops);
|
---|
92 | if (self->ops->apply)
|
---|
93 | return self->ops->apply(self, scope, in, out);
|
---|
94 |
|
---|
95 | if (bithenge_node_type(in) != BITHENGE_NODE_BLOB)
|
---|
96 | return EINVAL;
|
---|
97 | aoff64_t self_size, whole_size;
|
---|
98 | errno_t rc = bithenge_transform_prefix_apply(self, scope,
|
---|
99 | bithenge_node_as_blob(in), out, &self_size);
|
---|
100 | if (rc != EOK)
|
---|
101 | return rc;
|
---|
102 | rc = bithenge_blob_size(bithenge_node_as_blob(in), &whole_size);
|
---|
103 | if (rc == EOK && whole_size != self_size)
|
---|
104 | rc = EINVAL;
|
---|
105 | if (rc != EOK) {
|
---|
106 | bithenge_node_dec_ref(*out);
|
---|
107 | return rc;
|
---|
108 | }
|
---|
109 | return EOK;
|
---|
110 | }
|
---|
111 |
|
---|
112 | /** Find the length of the prefix of a blob this transform can use as input. In
|
---|
113 | * other words, figure out how many bytes this transform will use up. This
|
---|
114 | * method is optional and can return an error, but it must succeed for struct
|
---|
115 | * subtransforms. Takes ownership of nothing.
|
---|
116 | * @memberof bithenge_transform_t
|
---|
117 | * @param self The transform.
|
---|
118 | * @param scope The scope.
|
---|
119 | * @param blob The blob.
|
---|
120 | * @param[out] out Where the prefix length will be stored.
|
---|
121 | * @return EOK on success, ENOTSUP if not supported, or another error code from
|
---|
122 | * errno.h.
|
---|
123 | */
|
---|
124 | errno_t bithenge_transform_prefix_length(bithenge_transform_t *self,
|
---|
125 | bithenge_scope_t *scope, bithenge_blob_t *blob, aoff64_t *out)
|
---|
126 | {
|
---|
127 | assert(self);
|
---|
128 | assert(self->ops);
|
---|
129 | if (self->ops->prefix_length)
|
---|
130 | return self->ops->prefix_length(self, scope, blob, out);
|
---|
131 | if (!self->ops->prefix_apply)
|
---|
132 | return ENOTSUP;
|
---|
133 |
|
---|
134 | bithenge_node_t *node;
|
---|
135 | errno_t rc = bithenge_transform_prefix_apply(self, scope, blob, &node,
|
---|
136 | out);
|
---|
137 | if (rc != EOK)
|
---|
138 | return rc;
|
---|
139 | bithenge_node_dec_ref(node);
|
---|
140 | return EOK;
|
---|
141 | }
|
---|
142 |
|
---|
143 | /** Apply this transform to a prefix of a blob. In other words, feed as much of
|
---|
144 | * the blob into this transform as possible. Takes ownership of nothing.
|
---|
145 | * @memberof bithenge_transform_t
|
---|
146 | * @param self The transform.
|
---|
147 | * @param scope The scope.
|
---|
148 | * @param blob The blob.
|
---|
149 | * @param[out] out_node Holds the result of applying this transform to the
|
---|
150 | * prefix.
|
---|
151 | * @param[out] out_size Holds the size of the prefix. Can be null, in which
|
---|
152 | * case the size is not determined.
|
---|
153 | * @return EOK on success, ENOTSUP if not supported, or another error code from
|
---|
154 | * errno.h.
|
---|
155 | */
|
---|
156 | errno_t bithenge_transform_prefix_apply(bithenge_transform_t *self,
|
---|
157 | bithenge_scope_t *scope, bithenge_blob_t *blob, bithenge_node_t **out_node,
|
---|
158 | aoff64_t *out_size)
|
---|
159 | {
|
---|
160 | assert(self);
|
---|
161 | assert(self->ops);
|
---|
162 | if (self->ops->prefix_apply)
|
---|
163 | return self->ops->prefix_apply(self, scope, blob, out_node,
|
---|
164 | out_size);
|
---|
165 | if (!self->ops->prefix_length)
|
---|
166 | return ENOTSUP;
|
---|
167 |
|
---|
168 | aoff64_t size;
|
---|
169 | errno_t rc = bithenge_transform_prefix_length(self, scope, blob, &size);
|
---|
170 | if (rc != EOK)
|
---|
171 | return rc;
|
---|
172 | bithenge_node_t *prefix_blob;
|
---|
173 | bithenge_blob_inc_ref(blob);
|
---|
174 | rc = bithenge_new_subblob(&prefix_blob, blob, 0, size);
|
---|
175 | if (rc != EOK)
|
---|
176 | return rc;
|
---|
177 | rc = bithenge_transform_apply(self, scope, prefix_blob, out_node);
|
---|
178 | bithenge_node_dec_ref(prefix_blob);
|
---|
179 | if (out_size)
|
---|
180 | *out_size = size;
|
---|
181 | return rc;
|
---|
182 | }
|
---|
183 |
|
---|
184 |
|
---|
185 |
|
---|
186 | /***************** scope *****************/
|
---|
187 |
|
---|
188 | /** Create a transform scope. It must be dereferenced with @a
|
---|
189 | * bithenge_scope_dec_ref after it is used. Takes ownership of nothing.
|
---|
190 | * @memberof bithenge_scope_t
|
---|
191 | * @param[out] out Holds the new scope.
|
---|
192 | * @param outer The outer scope, or NULL.
|
---|
193 | * @return EOK on success or an error code from errno.h.
|
---|
194 | */
|
---|
195 | errno_t bithenge_scope_new(bithenge_scope_t **out, bithenge_scope_t *outer)
|
---|
196 | {
|
---|
197 | bithenge_scope_t *self = malloc(sizeof(*self));
|
---|
198 | if (!self)
|
---|
199 | return ENOMEM;
|
---|
200 | self->refs = 1;
|
---|
201 | if (outer)
|
---|
202 | bithenge_scope_inc_ref(outer);
|
---|
203 | self->outer = outer;
|
---|
204 | self->error = NULL;
|
---|
205 | self->barrier = false;
|
---|
206 | self->num_params = 0;
|
---|
207 | self->params = NULL;
|
---|
208 | self->current_node = NULL;
|
---|
209 | self->in_node = NULL;
|
---|
210 | *out = self;
|
---|
211 | return EOK;
|
---|
212 | }
|
---|
213 |
|
---|
214 | /** Dereference a transform scope.
|
---|
215 | * @memberof bithenge_scope_t
|
---|
216 | * @param self The scope to dereference, or NULL.
|
---|
217 | */
|
---|
218 | void bithenge_scope_dec_ref(bithenge_scope_t *self)
|
---|
219 | {
|
---|
220 | if (!self)
|
---|
221 | return;
|
---|
222 | if (--self->refs)
|
---|
223 | return;
|
---|
224 | bithenge_node_dec_ref(self->current_node);
|
---|
225 | for (int i = 0; i < self->num_params; i++)
|
---|
226 | bithenge_node_dec_ref(self->params[i]);
|
---|
227 | bithenge_scope_dec_ref(self->outer);
|
---|
228 | free(self->params);
|
---|
229 | free(self->error);
|
---|
230 | free(self);
|
---|
231 | }
|
---|
232 |
|
---|
233 | /** Get the outer scope of a scope, which may be NULL.
|
---|
234 | * @memberof bithenge_scope_t
|
---|
235 | * @param self The scope to examine.
|
---|
236 | * @return The outer scope, which may be NULL.
|
---|
237 | */
|
---|
238 | bithenge_scope_t *bithenge_scope_outer(bithenge_scope_t *self)
|
---|
239 | {
|
---|
240 | return self->outer;
|
---|
241 | }
|
---|
242 |
|
---|
243 | /** Get the error message stored in the scope, which may be NULL. The error
|
---|
244 | * message only exists as long as the scope does.
|
---|
245 | * @memberof bithenge_scope_t
|
---|
246 | * @param scope The scope to get the error message from.
|
---|
247 | * @return The error message, or NULL.
|
---|
248 | */
|
---|
249 | const char *bithenge_scope_get_error(bithenge_scope_t *scope)
|
---|
250 | {
|
---|
251 | return scope->error;
|
---|
252 | }
|
---|
253 |
|
---|
254 | /** Set the error message for the scope. The error message is stored in the
|
---|
255 | * outermost scope, but if any scope already has an error message this error
|
---|
256 | * message is ignored.
|
---|
257 | * @memberof bithenge_scope_t
|
---|
258 | * @param scope The scope.
|
---|
259 | * @param format The format string.
|
---|
260 | * @return EINVAL normally, or another error code from errno.h.
|
---|
261 | */
|
---|
262 | errno_t bithenge_scope_error(bithenge_scope_t *scope, const char *format, ...)
|
---|
263 | {
|
---|
264 | if (scope->error)
|
---|
265 | return EINVAL;
|
---|
266 | while (scope->outer) {
|
---|
267 | scope = scope->outer;
|
---|
268 | if (scope->error)
|
---|
269 | return EINVAL;
|
---|
270 | }
|
---|
271 | size_t space_left = 256;
|
---|
272 | scope->error = malloc(space_left);
|
---|
273 | if (!scope->error)
|
---|
274 | return ENOMEM;
|
---|
275 | char *out = scope->error;
|
---|
276 | va_list ap;
|
---|
277 | va_start(ap, format);
|
---|
278 |
|
---|
279 | while (*format) {
|
---|
280 | if (format[0] == '%' && format[1] == 't') {
|
---|
281 | format += 2;
|
---|
282 | errno_t rc = bithenge_print_node_to_string(&out,
|
---|
283 | &space_left, BITHENGE_PRINT_PYTHON,
|
---|
284 | va_arg(ap, bithenge_node_t *));
|
---|
285 | if (rc != EOK) {
|
---|
286 | va_end(ap);
|
---|
287 | return rc;
|
---|
288 | }
|
---|
289 | } else {
|
---|
290 | const char *end = str_chr(format, '%');
|
---|
291 | if (!end)
|
---|
292 | end = format + str_length(format);
|
---|
293 | size_t size = min((size_t)(end - format),
|
---|
294 | space_left - 1);
|
---|
295 | memcpy(out, format, size);
|
---|
296 | format = end;
|
---|
297 | out += size;
|
---|
298 | space_left -= size;
|
---|
299 | }
|
---|
300 | }
|
---|
301 | *out = '\0';
|
---|
302 |
|
---|
303 | va_end(ap);
|
---|
304 | return EINVAL;
|
---|
305 | }
|
---|
306 |
|
---|
307 | /** Get the current node being created, which may be NULL.
|
---|
308 | * @memberof bithenge_scope_t
|
---|
309 | * @param scope The scope to get the current node from.
|
---|
310 | * @return The node being created, or NULL.
|
---|
311 | */
|
---|
312 | bithenge_node_t *bithenge_scope_get_current_node(bithenge_scope_t *scope)
|
---|
313 | {
|
---|
314 | if (scope->current_node)
|
---|
315 | bithenge_node_inc_ref(scope->current_node);
|
---|
316 | return scope->current_node;
|
---|
317 | }
|
---|
318 |
|
---|
319 | /** Set the current node being created. Takes a reference to @a node.
|
---|
320 | * @memberof bithenge_scope_t
|
---|
321 | * @param scope The scope to set the current node in.
|
---|
322 | * @param node The current node being created, or NULL.
|
---|
323 | */
|
---|
324 | void bithenge_scope_set_current_node(bithenge_scope_t *scope,
|
---|
325 | bithenge_node_t *node)
|
---|
326 | {
|
---|
327 | bithenge_node_dec_ref(scope->current_node);
|
---|
328 | scope->current_node = node;
|
---|
329 | }
|
---|
330 |
|
---|
331 | /** Get the current input node, which may be NULL.
|
---|
332 | * @memberof bithenge_scope_t
|
---|
333 | * @param scope The scope to get the current input node from.
|
---|
334 | * @return The input node, or NULL.
|
---|
335 | */
|
---|
336 | bithenge_node_t *bithenge_scope_in_node(bithenge_scope_t *scope)
|
---|
337 | {
|
---|
338 | if (scope->in_node)
|
---|
339 | bithenge_node_inc_ref(scope->in_node);
|
---|
340 | return scope->in_node;
|
---|
341 | }
|
---|
342 |
|
---|
343 | /** Set the current input node. Takes a reference to @a node.
|
---|
344 | * @memberof bithenge_scope_t
|
---|
345 | * @param scope The scope to set the input node in.
|
---|
346 | * @param node The input node, or NULL.
|
---|
347 | */
|
---|
348 | void bithenge_scope_set_in_node(bithenge_scope_t *scope, bithenge_node_t *node)
|
---|
349 | {
|
---|
350 | bithenge_node_dec_ref(scope->in_node);
|
---|
351 | scope->in_node = node;
|
---|
352 | }
|
---|
353 |
|
---|
354 | /** Set a scope as a barrier.
|
---|
355 | * @memberof bithenge_scope_t
|
---|
356 | * @param self The scope to change.
|
---|
357 | */
|
---|
358 | void bithenge_scope_set_barrier(bithenge_scope_t *self)
|
---|
359 | {
|
---|
360 | self->barrier = true;
|
---|
361 | }
|
---|
362 |
|
---|
363 | /** Check whether a scope is a barrier, meaning that variable lookup stops at
|
---|
364 | * it.
|
---|
365 | * @memberof bithenge_scope_t
|
---|
366 | * @param self The scope to check.
|
---|
367 | * @return Whether the scope is a barrier.
|
---|
368 | */
|
---|
369 | bool bithenge_scope_is_barrier(bithenge_scope_t *self)
|
---|
370 | {
|
---|
371 | return self->barrier;
|
---|
372 | }
|
---|
373 |
|
---|
374 | /** Allocate parameters. The parameters must then be set with @a
|
---|
375 | * bithenge_scope_set_param. This must not be called on a scope that already
|
---|
376 | * has parameters.
|
---|
377 | * @memberof bithenge_scope_t
|
---|
378 | * @param scope The scope in which to allocate parameters.
|
---|
379 | * @param num_params The number of parameters to allocate.
|
---|
380 | * @return EOK on success or an error code from errno.h.
|
---|
381 | */
|
---|
382 | errno_t bithenge_scope_alloc_params(bithenge_scope_t *scope, int num_params)
|
---|
383 | {
|
---|
384 | scope->params = malloc(sizeof(*scope->params) * num_params);
|
---|
385 | if (!scope->params)
|
---|
386 | return ENOMEM;
|
---|
387 | scope->num_params = num_params;
|
---|
388 | for (int i = 0; i < num_params; i++)
|
---|
389 | scope->params[i] = NULL;
|
---|
390 | return EOK;
|
---|
391 | }
|
---|
392 |
|
---|
393 | /** Set a parameter. Takes a reference to @a node. Note that range checking is
|
---|
394 | * not done in release builds.
|
---|
395 | * @memberof bithenge_scope_t
|
---|
396 | * @param scope The scope in which to allocate parameters.
|
---|
397 | * @param i The index of the parameter to set.
|
---|
398 | * @param node The value to store in the parameter.
|
---|
399 | * @return EOK on success or an error code from errno.h.
|
---|
400 | */
|
---|
401 | errno_t bithenge_scope_set_param(bithenge_scope_t *scope, int i,
|
---|
402 | bithenge_node_t *node)
|
---|
403 | {
|
---|
404 | assert(scope);
|
---|
405 | assert(i >= 0 && i < scope->num_params);
|
---|
406 | if (bithenge_should_fail()) {
|
---|
407 | bithenge_node_dec_ref(node);
|
---|
408 | return ENOMEM;
|
---|
409 | }
|
---|
410 | scope->params[i] = node;
|
---|
411 | return EOK;
|
---|
412 | }
|
---|
413 |
|
---|
414 | /** Get a parameter. Note that range checking is not done in release builds.
|
---|
415 | * @memberof bithenge_scope_t
|
---|
416 | * @param scope The scope to get the parameter from.
|
---|
417 | * @param i The index of the parameter to set.
|
---|
418 | * @param[out] out Stores a new reference to the parameter.
|
---|
419 | * @return EOK on success or an error code from errno.h.
|
---|
420 | */
|
---|
421 | errno_t bithenge_scope_get_param(bithenge_scope_t *scope, int i,
|
---|
422 | bithenge_node_t **out)
|
---|
423 | {
|
---|
424 | assert(scope);
|
---|
425 | if (scope->num_params) {
|
---|
426 | assert(i >= 0 && i < scope->num_params);
|
---|
427 | *out = scope->params[i];
|
---|
428 | bithenge_node_inc_ref(*out);
|
---|
429 | return EOK;
|
---|
430 | } else {
|
---|
431 | return bithenge_scope_get_param(scope->outer, i, out);
|
---|
432 | }
|
---|
433 | }
|
---|
434 |
|
---|
435 |
|
---|
436 |
|
---|
437 | /***************** barrier_transform *****************/
|
---|
438 |
|
---|
439 | typedef struct {
|
---|
440 | bithenge_transform_t base;
|
---|
441 | bithenge_transform_t *transform;
|
---|
442 | } barrier_transform_t;
|
---|
443 |
|
---|
444 | static inline barrier_transform_t *transform_as_barrier(
|
---|
445 | bithenge_transform_t *base)
|
---|
446 | {
|
---|
447 | return (barrier_transform_t *)base;
|
---|
448 | }
|
---|
449 |
|
---|
450 | static inline bithenge_transform_t *barrier_as_transform(
|
---|
451 | barrier_transform_t *self)
|
---|
452 | {
|
---|
453 | return &self->base;
|
---|
454 | }
|
---|
455 |
|
---|
456 | static errno_t barrier_transform_apply(bithenge_transform_t *base,
|
---|
457 | bithenge_scope_t *scope, bithenge_node_t *in, bithenge_node_t **out)
|
---|
458 | {
|
---|
459 | barrier_transform_t *self = transform_as_barrier(base);
|
---|
460 | bithenge_scope_t *inner_scope;
|
---|
461 | errno_t rc = bithenge_scope_new(&inner_scope, scope);
|
---|
462 | if (rc != EOK)
|
---|
463 | return rc;
|
---|
464 | bithenge_scope_set_barrier(inner_scope);
|
---|
465 | bithenge_scope_set_in_node(inner_scope, in);
|
---|
466 | rc = bithenge_transform_apply(self->transform, inner_scope, in, out);
|
---|
467 | bithenge_scope_dec_ref(inner_scope);
|
---|
468 | return rc;
|
---|
469 | }
|
---|
470 |
|
---|
471 | static errno_t barrier_transform_prefix_length(bithenge_transform_t *base,
|
---|
472 | bithenge_scope_t *scope, bithenge_blob_t *in, aoff64_t *out)
|
---|
473 | {
|
---|
474 | barrier_transform_t *self = transform_as_barrier(base);
|
---|
475 | bithenge_scope_t *inner_scope;
|
---|
476 | errno_t rc = bithenge_scope_new(&inner_scope, scope);
|
---|
477 | if (rc != EOK)
|
---|
478 | return rc;
|
---|
479 | bithenge_scope_set_barrier(inner_scope);
|
---|
480 | bithenge_scope_set_in_node(inner_scope, bithenge_blob_as_node(in));
|
---|
481 | rc = bithenge_transform_prefix_length(self->transform, inner_scope, in,
|
---|
482 | out);
|
---|
483 | bithenge_scope_dec_ref(inner_scope);
|
---|
484 | return rc;
|
---|
485 | }
|
---|
486 |
|
---|
487 | static errno_t barrier_transform_prefix_apply(bithenge_transform_t *base,
|
---|
488 | bithenge_scope_t *scope, bithenge_blob_t *in, bithenge_node_t **out_node,
|
---|
489 | aoff64_t *out_length)
|
---|
490 | {
|
---|
491 | barrier_transform_t *self = transform_as_barrier(base);
|
---|
492 | bithenge_scope_t *inner_scope;
|
---|
493 | errno_t rc = bithenge_scope_new(&inner_scope, scope);
|
---|
494 | if (rc != EOK)
|
---|
495 | return rc;
|
---|
496 | bithenge_scope_set_barrier(inner_scope);
|
---|
497 | bithenge_scope_set_in_node(inner_scope, bithenge_blob_as_node(in));
|
---|
498 | rc = bithenge_transform_prefix_apply(self->transform, inner_scope, in,
|
---|
499 | out_node, out_length);
|
---|
500 | bithenge_scope_dec_ref(inner_scope);
|
---|
501 | return rc;
|
---|
502 | }
|
---|
503 |
|
---|
504 | static void barrier_transform_destroy(bithenge_transform_t *base)
|
---|
505 | {
|
---|
506 | barrier_transform_t *self = transform_as_barrier(base);
|
---|
507 | bithenge_transform_dec_ref(self->transform);
|
---|
508 | free(self);
|
---|
509 | }
|
---|
510 |
|
---|
511 | static const bithenge_transform_ops_t barrier_transform_ops = {
|
---|
512 | .apply = barrier_transform_apply,
|
---|
513 | .prefix_length = barrier_transform_prefix_length,
|
---|
514 | .prefix_apply = barrier_transform_prefix_apply,
|
---|
515 | .destroy = barrier_transform_destroy,
|
---|
516 | };
|
---|
517 |
|
---|
518 | /** Set the subtransform of a barrier transform. This must be done before the
|
---|
519 | * barrier transform is used. Takes a reference to @a transform.
|
---|
520 | * @param base The barrier transform.
|
---|
521 | * @param transform The subtransform to use for all operations.
|
---|
522 | * @return EOK on success or an error code from errno.h.
|
---|
523 | */
|
---|
524 | errno_t bithenge_barrier_transform_set_subtransform(bithenge_transform_t *base,
|
---|
525 | bithenge_transform_t *transform)
|
---|
526 | {
|
---|
527 | assert(transform);
|
---|
528 | assert(bithenge_transform_num_params(transform) == 0);
|
---|
529 |
|
---|
530 | if (bithenge_should_fail()) {
|
---|
531 | bithenge_transform_dec_ref(transform);
|
---|
532 | return ENOMEM;
|
---|
533 | }
|
---|
534 |
|
---|
535 | barrier_transform_t *self = transform_as_barrier(base);
|
---|
536 | assert(!self->transform);
|
---|
537 | self->transform = transform;
|
---|
538 | return EOK;
|
---|
539 | }
|
---|
540 |
|
---|
541 | /** Create a wrapper transform that creates a new scope. This ensures nothing
|
---|
542 | * from the outer scope is passed in, other than parameters. The wrapper may
|
---|
543 | * have a different value for num_params. The subtransform must be set with @a
|
---|
544 | * bithenge_barrier_transform_set_subtransform before the result is used.
|
---|
545 | * @param[out] out Holds the created transform.
|
---|
546 | * @param num_params The number of parameters to require, which may be 0.
|
---|
547 | * @return EOK on success or an error code from errno.h.
|
---|
548 | */
|
---|
549 | errno_t bithenge_new_barrier_transform(bithenge_transform_t **out, int num_params)
|
---|
550 | {
|
---|
551 | errno_t rc;
|
---|
552 | barrier_transform_t *self = malloc(sizeof(*self));
|
---|
553 | if (!self) {
|
---|
554 | rc = ENOMEM;
|
---|
555 | goto error;
|
---|
556 | }
|
---|
557 | rc = bithenge_init_transform(barrier_as_transform(self),
|
---|
558 | &barrier_transform_ops, num_params);
|
---|
559 | if (rc != EOK)
|
---|
560 | goto error;
|
---|
561 | self->transform = NULL;
|
---|
562 | *out = barrier_as_transform(self);
|
---|
563 | return EOK;
|
---|
564 | error:
|
---|
565 | free(self);
|
---|
566 | return rc;
|
---|
567 | }
|
---|
568 |
|
---|
569 |
|
---|
570 |
|
---|
571 | /***************** ascii *****************/
|
---|
572 |
|
---|
573 | static errno_t ascii_apply(bithenge_transform_t *self, bithenge_scope_t *scope,
|
---|
574 | bithenge_node_t *in, bithenge_node_t **out)
|
---|
575 | {
|
---|
576 | errno_t rc;
|
---|
577 | if (bithenge_node_type(in) != BITHENGE_NODE_BLOB)
|
---|
578 | return EINVAL;
|
---|
579 | bithenge_blob_t *blob = bithenge_node_as_blob(in);
|
---|
580 | aoff64_t size;
|
---|
581 | rc = bithenge_blob_size(blob, &size);
|
---|
582 | if (rc != EOK)
|
---|
583 | return rc;
|
---|
584 |
|
---|
585 | char *buffer = malloc(size + 1);
|
---|
586 | if (!buffer)
|
---|
587 | return ENOMEM;
|
---|
588 | aoff64_t size_read = size;
|
---|
589 | rc = bithenge_blob_read(blob, 0, buffer, &size_read);
|
---|
590 | if (rc != EOK) {
|
---|
591 | free(buffer);
|
---|
592 | return rc;
|
---|
593 | }
|
---|
594 | if (size_read != size) {
|
---|
595 | free(buffer);
|
---|
596 | return EINVAL;
|
---|
597 | }
|
---|
598 | buffer[size] = '\0';
|
---|
599 |
|
---|
600 | /* TODO: what if the OS encoding is incompatible with ASCII? */
|
---|
601 | return bithenge_new_string_node(out, buffer, true);
|
---|
602 | }
|
---|
603 |
|
---|
604 | static const bithenge_transform_ops_t ascii_ops = {
|
---|
605 | .apply = ascii_apply,
|
---|
606 | .destroy = transform_indestructible,
|
---|
607 | };
|
---|
608 |
|
---|
609 | /** The ASCII text transform. */
|
---|
610 | bithenge_transform_t bithenge_ascii_transform = {
|
---|
611 | &ascii_ops, 1, 0
|
---|
612 | };
|
---|
613 |
|
---|
614 |
|
---|
615 |
|
---|
616 | /***************** bit *****************/
|
---|
617 |
|
---|
618 | static errno_t bit_prefix_apply(bithenge_transform_t *self,
|
---|
619 | bithenge_scope_t *scope, bithenge_blob_t *blob, bithenge_node_t **out_node,
|
---|
620 | aoff64_t *out_size)
|
---|
621 | {
|
---|
622 | char buffer;
|
---|
623 | aoff64_t size = 1;
|
---|
624 | errno_t rc = bithenge_blob_read_bits(blob, 0, &buffer, &size, true);
|
---|
625 | if (rc != EOK)
|
---|
626 | return rc;
|
---|
627 | if (size != 1)
|
---|
628 | return EINVAL;
|
---|
629 | if (out_size)
|
---|
630 | *out_size = size;
|
---|
631 | return bithenge_new_boolean_node(out_node, (buffer & 1) != 0);
|
---|
632 | }
|
---|
633 |
|
---|
634 | static const bithenge_transform_ops_t bit_ops = {
|
---|
635 | .prefix_apply = bit_prefix_apply,
|
---|
636 | .destroy = transform_indestructible,
|
---|
637 | };
|
---|
638 |
|
---|
639 | /** A transform that decodes a bit as a boolean. */
|
---|
640 | bithenge_transform_t bithenge_bit_transform = {
|
---|
641 | &bit_ops, 1, 0
|
---|
642 | };
|
---|
643 |
|
---|
644 |
|
---|
645 |
|
---|
646 | /***************** bits_be, bits_le *****************/
|
---|
647 |
|
---|
648 | typedef struct {
|
---|
649 | bithenge_blob_t base;
|
---|
650 | bithenge_blob_t *bytes;
|
---|
651 | bool little_endian;
|
---|
652 | } bits_xe_blob_t;
|
---|
653 |
|
---|
654 | static bits_xe_blob_t *blob_as_bits_xe(bithenge_blob_t *base)
|
---|
655 | {
|
---|
656 | return (bits_xe_blob_t *)base;
|
---|
657 | }
|
---|
658 |
|
---|
659 | static bithenge_blob_t *bits_xe_as_blob(bits_xe_blob_t *self)
|
---|
660 | {
|
---|
661 | return &self->base;
|
---|
662 | }
|
---|
663 |
|
---|
664 | static errno_t bits_xe_size(bithenge_blob_t *base, aoff64_t *out)
|
---|
665 | {
|
---|
666 | bits_xe_blob_t *self = blob_as_bits_xe(base);
|
---|
667 | errno_t rc = bithenge_blob_size(self->bytes, out);
|
---|
668 | *out *= 8;
|
---|
669 | return rc;
|
---|
670 | }
|
---|
671 |
|
---|
672 | static uint8_t reverse_byte(uint8_t val)
|
---|
673 | {
|
---|
674 | val = ((val & 0x0f) << 4) ^ ((val & 0xf0) >> 4);
|
---|
675 | val = ((val & 0x33) << 2) ^ ((val & 0xcc) >> 2);
|
---|
676 | val = ((val & 0x55) << 1) ^ ((val & 0xaa) >> 1);
|
---|
677 | return val;
|
---|
678 | }
|
---|
679 |
|
---|
680 | static errno_t bits_xe_read_bits(bithenge_blob_t *base, aoff64_t offset,
|
---|
681 | char *buffer, aoff64_t *size, bool little_endian)
|
---|
682 | {
|
---|
683 | bits_xe_blob_t *self = blob_as_bits_xe(base);
|
---|
684 | aoff64_t bytes_offset = offset / 8;
|
---|
685 | aoff64_t bit_offset = offset % 8;
|
---|
686 | aoff64_t output_num_bytes = (*size + 7) / 8;
|
---|
687 | aoff64_t bytes_size = (*size + bit_offset + 7) / 8;
|
---|
688 | bool separate_buffer = bit_offset != 0;
|
---|
689 | uint8_t *bytes_buffer;
|
---|
690 | if (separate_buffer) {
|
---|
691 | /* Allocate an extra byte, to make sure byte1 can be read. */
|
---|
692 | bytes_buffer = malloc(bytes_size + 1);
|
---|
693 | if (!bytes_buffer)
|
---|
694 | return ENOMEM;
|
---|
695 | } else
|
---|
696 | bytes_buffer = (uint8_t *)buffer;
|
---|
697 |
|
---|
698 | errno_t rc = bithenge_blob_read(self->bytes, bytes_offset,
|
---|
699 | (char *)bytes_buffer, &bytes_size);
|
---|
700 | if (rc != EOK)
|
---|
701 | goto end;
|
---|
702 | *size = min(*size, bytes_size * 8 - bit_offset);
|
---|
703 |
|
---|
704 | if (little_endian != self->little_endian)
|
---|
705 | for (aoff64_t i = 0; i < bytes_size; i++)
|
---|
706 | bytes_buffer[i] = reverse_byte(bytes_buffer[i]);
|
---|
707 |
|
---|
708 | if (bit_offset || separate_buffer) {
|
---|
709 | for (aoff64_t i = 0; i < output_num_bytes; i++) {
|
---|
710 | uint8_t byte0 = bytes_buffer[i];
|
---|
711 | uint8_t byte1 = bytes_buffer[i + 1];
|
---|
712 | buffer[i] = little_endian ?
|
---|
713 | (byte0 >> bit_offset) ^ (byte1 << (8 - bit_offset)) :
|
---|
714 | (byte0 << bit_offset) ^ (byte1 >> (8 - bit_offset));
|
---|
715 | }
|
---|
716 | }
|
---|
717 |
|
---|
718 | end:
|
---|
719 | if (separate_buffer)
|
---|
720 | free(bytes_buffer);
|
---|
721 | return rc;
|
---|
722 | }
|
---|
723 |
|
---|
724 | static void bits_xe_destroy(bithenge_blob_t *base)
|
---|
725 | {
|
---|
726 | bits_xe_blob_t *self = blob_as_bits_xe(base);
|
---|
727 | bithenge_blob_dec_ref(self->bytes);
|
---|
728 | free(self);
|
---|
729 | }
|
---|
730 |
|
---|
731 | static const bithenge_random_access_blob_ops_t bits_xe_blob_ops = {
|
---|
732 | .size = bits_xe_size,
|
---|
733 | .read_bits = bits_xe_read_bits,
|
---|
734 | .destroy = bits_xe_destroy,
|
---|
735 | };
|
---|
736 |
|
---|
737 | static errno_t bits_xe_apply(bithenge_transform_t *self, bithenge_scope_t *scope,
|
---|
738 | bithenge_node_t *in, bithenge_node_t **out)
|
---|
739 | {
|
---|
740 | if (bithenge_node_type(in) != BITHENGE_NODE_BLOB)
|
---|
741 | return EINVAL;
|
---|
742 | bits_xe_blob_t *blob = malloc(sizeof(*blob));
|
---|
743 | if (!blob)
|
---|
744 | return ENOMEM;
|
---|
745 | errno_t rc = bithenge_init_random_access_blob(bits_xe_as_blob(blob),
|
---|
746 | &bits_xe_blob_ops);
|
---|
747 | if (rc != EOK) {
|
---|
748 | free(blob);
|
---|
749 | return rc;
|
---|
750 | }
|
---|
751 | bithenge_node_inc_ref(in);
|
---|
752 | blob->bytes = bithenge_node_as_blob(in);
|
---|
753 | blob->little_endian = (self == &bithenge_bits_le_transform);
|
---|
754 | *out = bithenge_blob_as_node(bits_xe_as_blob(blob));
|
---|
755 | return EOK;
|
---|
756 | }
|
---|
757 |
|
---|
758 | static const bithenge_transform_ops_t bits_xe_ops = {
|
---|
759 | .apply = bits_xe_apply,
|
---|
760 | .destroy = transform_indestructible,
|
---|
761 | };
|
---|
762 |
|
---|
763 | /** A transform that converts a byte blob to a bit blob, most-significant bit
|
---|
764 | * first.
|
---|
765 | */
|
---|
766 | bithenge_transform_t bithenge_bits_be_transform = {
|
---|
767 | &bits_xe_ops, 1, 0
|
---|
768 | };
|
---|
769 |
|
---|
770 | /** A transform that converts a byte blob to a bit blob, least-significant bit
|
---|
771 | * first.
|
---|
772 | */
|
---|
773 | bithenge_transform_t bithenge_bits_le_transform = {
|
---|
774 | &bits_xe_ops, 1, 0
|
---|
775 | };
|
---|
776 |
|
---|
777 |
|
---|
778 |
|
---|
779 | /***************** invalid *****************/
|
---|
780 |
|
---|
781 | static errno_t invalid_apply(bithenge_transform_t *self, bithenge_scope_t *scope,
|
---|
782 | bithenge_node_t *in, bithenge_node_t **out)
|
---|
783 | {
|
---|
784 | return EINVAL;
|
---|
785 | }
|
---|
786 |
|
---|
787 | static const bithenge_transform_ops_t invalid_ops = {
|
---|
788 | .apply = invalid_apply,
|
---|
789 | .destroy = transform_indestructible,
|
---|
790 | };
|
---|
791 |
|
---|
792 | /** A transform that always raises an error. */
|
---|
793 | bithenge_transform_t bithenge_invalid_transform = {
|
---|
794 | &invalid_ops, 1, 0
|
---|
795 | };
|
---|
796 |
|
---|
797 |
|
---|
798 |
|
---|
799 | /***************** known_length *****************/
|
---|
800 |
|
---|
801 | static errno_t known_length_apply(bithenge_transform_t *self,
|
---|
802 | bithenge_scope_t *scope, bithenge_node_t *in, bithenge_node_t **out)
|
---|
803 | {
|
---|
804 | bithenge_node_t *length_node;
|
---|
805 | errno_t rc = bithenge_scope_get_param(scope, 0, &length_node);
|
---|
806 | if (rc != EOK)
|
---|
807 | return rc;
|
---|
808 | if (bithenge_node_type(length_node) != BITHENGE_NODE_INTEGER) {
|
---|
809 | bithenge_node_dec_ref(length_node);
|
---|
810 | return EINVAL;
|
---|
811 | }
|
---|
812 | bithenge_int_t length = bithenge_integer_node_value(length_node);
|
---|
813 | bithenge_node_dec_ref(length_node);
|
---|
814 |
|
---|
815 | if (bithenge_node_type(in) != BITHENGE_NODE_BLOB)
|
---|
816 | return EINVAL;
|
---|
817 | aoff64_t size;
|
---|
818 | rc = bithenge_blob_size(bithenge_node_as_blob(in), &size);
|
---|
819 | if (rc != EOK)
|
---|
820 | return rc;
|
---|
821 | if (length != (bithenge_int_t)size)
|
---|
822 | return EINVAL;
|
---|
823 |
|
---|
824 | bithenge_node_inc_ref(in);
|
---|
825 | *out = in;
|
---|
826 | return EOK;
|
---|
827 | }
|
---|
828 |
|
---|
829 | static errno_t known_length_prefix_length(bithenge_transform_t *self,
|
---|
830 | bithenge_scope_t *scope, bithenge_blob_t *in, aoff64_t *out)
|
---|
831 | {
|
---|
832 | bithenge_node_t *length_node;
|
---|
833 | errno_t rc = bithenge_scope_get_param(scope, 0, &length_node);
|
---|
834 | if (rc != EOK)
|
---|
835 | return rc;
|
---|
836 | if (bithenge_node_type(length_node) != BITHENGE_NODE_INTEGER) {
|
---|
837 | bithenge_node_dec_ref(length_node);
|
---|
838 | return EINVAL;
|
---|
839 | }
|
---|
840 | bithenge_int_t length = bithenge_integer_node_value(length_node);
|
---|
841 | bithenge_node_dec_ref(length_node);
|
---|
842 |
|
---|
843 | *out = (aoff64_t)length;
|
---|
844 | return EOK;
|
---|
845 | }
|
---|
846 |
|
---|
847 | static const bithenge_transform_ops_t known_length_ops = {
|
---|
848 | .apply = known_length_apply,
|
---|
849 | .prefix_length = known_length_prefix_length,
|
---|
850 | .destroy = transform_indestructible,
|
---|
851 | };
|
---|
852 |
|
---|
853 | /** Pass through a blob, but require its length to equal the first argument. */
|
---|
854 | bithenge_transform_t bithenge_known_length_transform = {
|
---|
855 | &known_length_ops, 1, 1
|
---|
856 | };
|
---|
857 |
|
---|
858 | static errno_t nonzero_boolean_apply(bithenge_transform_t *self,
|
---|
859 | bithenge_scope_t *scope, bithenge_node_t *in, bithenge_node_t **out)
|
---|
860 | {
|
---|
861 | if (bithenge_node_type(in) != BITHENGE_NODE_INTEGER)
|
---|
862 | return EINVAL;
|
---|
863 | bool value = bithenge_integer_node_value(in) != 0;
|
---|
864 | return bithenge_new_boolean_node(out, value);
|
---|
865 | }
|
---|
866 |
|
---|
867 | static const bithenge_transform_ops_t nonzero_boolean_ops = {
|
---|
868 | .apply = nonzero_boolean_apply,
|
---|
869 | .destroy = transform_indestructible,
|
---|
870 | };
|
---|
871 |
|
---|
872 | /** A transform that converts integers to booleans, true if nonzero. */
|
---|
873 | bithenge_transform_t bithenge_nonzero_boolean_transform = {
|
---|
874 | &nonzero_boolean_ops, 1, 0
|
---|
875 | };
|
---|
876 |
|
---|
877 | static errno_t prefix_length_1(bithenge_transform_t *self, bithenge_scope_t *scope,
|
---|
878 | bithenge_blob_t *blob, aoff64_t *out)
|
---|
879 | {
|
---|
880 | *out = 1;
|
---|
881 | return EOK;
|
---|
882 | }
|
---|
883 |
|
---|
884 | static errno_t prefix_length_2(bithenge_transform_t *self, bithenge_scope_t *scope,
|
---|
885 | bithenge_blob_t *blob, aoff64_t *out)
|
---|
886 | {
|
---|
887 | *out = 2;
|
---|
888 | return EOK;
|
---|
889 | }
|
---|
890 |
|
---|
891 | static errno_t prefix_length_4(bithenge_transform_t *self, bithenge_scope_t *scope,
|
---|
892 | bithenge_blob_t *blob, aoff64_t *out)
|
---|
893 | {
|
---|
894 | *out = 4;
|
---|
895 | return EOK;
|
---|
896 | }
|
---|
897 |
|
---|
898 | static errno_t prefix_length_8(bithenge_transform_t *self, bithenge_scope_t *scope,
|
---|
899 | bithenge_blob_t *blob, aoff64_t *out)
|
---|
900 | {
|
---|
901 | *out = 8;
|
---|
902 | return EOK;
|
---|
903 | }
|
---|
904 |
|
---|
905 | static uint8_t uint8_t_identity(uint8_t arg)
|
---|
906 | {
|
---|
907 | return arg;
|
---|
908 | }
|
---|
909 |
|
---|
910 | /** @cond internal */
|
---|
911 | #define MAKE_UINT_TRANSFORM(NAME, TYPE, ENDIAN, PREFIX_LENGTH_FUNC) \
|
---|
912 | static errno_t NAME##_apply(bithenge_transform_t *self, \
|
---|
913 | bithenge_scope_t *scope, bithenge_node_t *in, \
|
---|
914 | bithenge_node_t **out) \
|
---|
915 | { \
|
---|
916 | errno_t rc; \
|
---|
917 | if (bithenge_node_type(in) != BITHENGE_NODE_BLOB) \
|
---|
918 | return EINVAL; \
|
---|
919 | bithenge_blob_t *blob = bithenge_node_as_blob(in); \
|
---|
920 | \
|
---|
921 | /* Read too many bytes; success means the blob is too long. */ \
|
---|
922 | TYPE val[2]; \
|
---|
923 | aoff64_t size = sizeof(val[0]) + 1; \
|
---|
924 | rc = bithenge_blob_read(blob, 0, (char *)val, &size); \
|
---|
925 | if (rc != EOK) \
|
---|
926 | return rc; \
|
---|
927 | if (size != sizeof(val[0])) \
|
---|
928 | return EINVAL; \
|
---|
929 | \
|
---|
930 | return bithenge_new_integer_node(out, ENDIAN(val[0])); \
|
---|
931 | } \
|
---|
932 | \
|
---|
933 | static const bithenge_transform_ops_t NAME##_ops = { \
|
---|
934 | .apply = NAME##_apply, \
|
---|
935 | .prefix_length = PREFIX_LENGTH_FUNC, \
|
---|
936 | .destroy = transform_indestructible, \
|
---|
937 | }; \
|
---|
938 | \
|
---|
939 | bithenge_transform_t bithenge_##NAME##_transform = { \
|
---|
940 | &NAME##_ops, 1, 0 \
|
---|
941 | }
|
---|
942 |
|
---|
943 | MAKE_UINT_TRANSFORM(uint8, uint8_t, uint8_t_identity, prefix_length_1);
|
---|
944 | MAKE_UINT_TRANSFORM(uint16le, uint16_t, uint16_t_le2host, prefix_length_2);
|
---|
945 | MAKE_UINT_TRANSFORM(uint16be, uint16_t, uint16_t_be2host, prefix_length_2);
|
---|
946 | MAKE_UINT_TRANSFORM(uint32le, uint32_t, uint32_t_le2host, prefix_length_4);
|
---|
947 | MAKE_UINT_TRANSFORM(uint32be, uint32_t, uint32_t_be2host, prefix_length_4);
|
---|
948 | MAKE_UINT_TRANSFORM(uint64le, uint64_t, uint64_t_le2host, prefix_length_8);
|
---|
949 | MAKE_UINT_TRANSFORM(uint64be, uint64_t, uint64_t_be2host, prefix_length_8);
|
---|
950 | /** @endcond */
|
---|
951 |
|
---|
952 |
|
---|
953 |
|
---|
954 | /***************** uint_be, uint_le *****************/
|
---|
955 |
|
---|
956 | static errno_t uint_xe_prefix_apply(bithenge_transform_t *self,
|
---|
957 | bithenge_scope_t *scope, bithenge_blob_t *blob, bithenge_node_t **out_node,
|
---|
958 | aoff64_t *out_size)
|
---|
959 | {
|
---|
960 | bool little_endian = (self == &bithenge_uint_le_transform);
|
---|
961 | bithenge_node_t *num_bits_node;
|
---|
962 | errno_t rc = bithenge_scope_get_param(scope, 0, &num_bits_node);
|
---|
963 | if (rc != EOK)
|
---|
964 | return rc;
|
---|
965 | if (bithenge_node_type(num_bits_node) != BITHENGE_NODE_INTEGER) {
|
---|
966 | bithenge_node_dec_ref(num_bits_node);
|
---|
967 | return EINVAL;
|
---|
968 | }
|
---|
969 | bithenge_int_t num_bits = bithenge_integer_node_value(num_bits_node);
|
---|
970 | bithenge_node_dec_ref(num_bits_node);
|
---|
971 | if (num_bits < 0)
|
---|
972 | return EINVAL;
|
---|
973 | if ((size_t)num_bits > sizeof(bithenge_int_t) * 8 - 1)
|
---|
974 | return EINVAL;
|
---|
975 |
|
---|
976 | aoff64_t size = num_bits;
|
---|
977 | uint8_t buffer[sizeof(bithenge_int_t)];
|
---|
978 | rc = bithenge_blob_read_bits(blob, 0, (char *)buffer, &size,
|
---|
979 | little_endian);
|
---|
980 | if (rc != EOK)
|
---|
981 | return rc;
|
---|
982 | if (size != (aoff64_t)num_bits)
|
---|
983 | return EINVAL;
|
---|
984 | if (out_size)
|
---|
985 | *out_size = size;
|
---|
986 |
|
---|
987 | bithenge_int_t result = 0;
|
---|
988 | bithenge_int_t num_easy_bytes = num_bits / 8;
|
---|
989 | if (little_endian) {
|
---|
990 | for (bithenge_int_t i = 0; i < num_easy_bytes; i++)
|
---|
991 | result += buffer[i] << 8 * i;
|
---|
992 | if (num_bits % 8)
|
---|
993 | result += (buffer[num_easy_bytes] &
|
---|
994 | ((1 << num_bits % 8) - 1)) << 8 * num_easy_bytes;
|
---|
995 | } else {
|
---|
996 | for (bithenge_int_t i = 0; i < num_easy_bytes; i++)
|
---|
997 | result += buffer[i] << (num_bits - 8 * (i + 1));
|
---|
998 | if (num_bits % 8)
|
---|
999 | result += buffer[num_easy_bytes] >> (8 - num_bits % 8);
|
---|
1000 | }
|
---|
1001 |
|
---|
1002 | return bithenge_new_integer_node(out_node, result);
|
---|
1003 | }
|
---|
1004 |
|
---|
1005 | static const bithenge_transform_ops_t uint_xe_ops = {
|
---|
1006 | .prefix_apply = uint_xe_prefix_apply,
|
---|
1007 | .destroy = transform_indestructible,
|
---|
1008 | };
|
---|
1009 |
|
---|
1010 | /** A transform that reads an unsigned integer from an arbitrary number of
|
---|
1011 | * bits, most-significant bit first.
|
---|
1012 | */
|
---|
1013 | bithenge_transform_t bithenge_uint_be_transform = {
|
---|
1014 | &uint_xe_ops, 1, 1
|
---|
1015 | };
|
---|
1016 |
|
---|
1017 | /** A transform that reads an unsigned integer from an arbitrary number of
|
---|
1018 | * bits, least-significant bit first.
|
---|
1019 | */
|
---|
1020 | bithenge_transform_t bithenge_uint_le_transform = {
|
---|
1021 | &uint_xe_ops, 1, 1
|
---|
1022 | };
|
---|
1023 |
|
---|
1024 |
|
---|
1025 |
|
---|
1026 | /***************** zero_terminated *****************/
|
---|
1027 |
|
---|
1028 | static errno_t zero_terminated_apply(bithenge_transform_t *self,
|
---|
1029 | bithenge_scope_t *scope, bithenge_node_t *in, bithenge_node_t **out)
|
---|
1030 | {
|
---|
1031 | errno_t rc;
|
---|
1032 | if (bithenge_node_type(in) != BITHENGE_NODE_BLOB)
|
---|
1033 | return EINVAL;
|
---|
1034 | bithenge_blob_t *blob = bithenge_node_as_blob(in);
|
---|
1035 | aoff64_t size;
|
---|
1036 | rc = bithenge_blob_size(blob, &size);
|
---|
1037 | if (rc != EOK)
|
---|
1038 | return rc;
|
---|
1039 | if (size < 1)
|
---|
1040 | return EINVAL;
|
---|
1041 | char ch;
|
---|
1042 | aoff64_t size_read = 1;
|
---|
1043 | rc = bithenge_blob_read(blob, size - 1, &ch, &size_read);
|
---|
1044 | if (rc != EOK)
|
---|
1045 | return rc;
|
---|
1046 | if (size_read != 1 || ch != '\0')
|
---|
1047 | return EINVAL;
|
---|
1048 | bithenge_blob_inc_ref(blob);
|
---|
1049 | return bithenge_new_subblob(out, blob, 0, size - 1);
|
---|
1050 | }
|
---|
1051 |
|
---|
1052 | static errno_t zero_terminated_prefix_length(bithenge_transform_t *self,
|
---|
1053 | bithenge_scope_t *scope, bithenge_blob_t *blob, aoff64_t *out)
|
---|
1054 | {
|
---|
1055 | errno_t rc;
|
---|
1056 | char buffer[4096];
|
---|
1057 | aoff64_t offset = 0, size_read = sizeof(buffer);
|
---|
1058 | do {
|
---|
1059 | rc = bithenge_blob_read(blob, offset, buffer, &size_read);
|
---|
1060 | if (rc != EOK)
|
---|
1061 | return rc;
|
---|
1062 | char *found = memchr(buffer, '\0', size_read);
|
---|
1063 | if (found) {
|
---|
1064 | *out = found - buffer + offset + 1;
|
---|
1065 | return EOK;
|
---|
1066 | }
|
---|
1067 | offset += size_read;
|
---|
1068 | } while (size_read == sizeof(buffer));
|
---|
1069 | return EINVAL;
|
---|
1070 | }
|
---|
1071 |
|
---|
1072 | static const bithenge_transform_ops_t zero_terminated_ops = {
|
---|
1073 | .apply = zero_terminated_apply,
|
---|
1074 | .prefix_length = zero_terminated_prefix_length,
|
---|
1075 | .destroy = transform_indestructible,
|
---|
1076 | };
|
---|
1077 |
|
---|
1078 | /** The zero-terminated data transform. */
|
---|
1079 | bithenge_transform_t bithenge_zero_terminated_transform = {
|
---|
1080 | &zero_terminated_ops, 1, 0
|
---|
1081 | };
|
---|
1082 |
|
---|
1083 | static bithenge_named_transform_t primitive_transforms[] = {
|
---|
1084 | { "ascii", &bithenge_ascii_transform },
|
---|
1085 | { "bit", &bithenge_bit_transform },
|
---|
1086 | { "bits_be", &bithenge_bits_be_transform },
|
---|
1087 | { "bits_le", &bithenge_bits_le_transform },
|
---|
1088 | { "known_length", &bithenge_known_length_transform },
|
---|
1089 | { "nonzero_boolean", &bithenge_nonzero_boolean_transform },
|
---|
1090 | { "uint8", &bithenge_uint8_transform },
|
---|
1091 | { "uint16be", &bithenge_uint16be_transform },
|
---|
1092 | { "uint16le", &bithenge_uint16le_transform },
|
---|
1093 | { "uint32be", &bithenge_uint32be_transform },
|
---|
1094 | { "uint32le", &bithenge_uint32le_transform },
|
---|
1095 | { "uint64be", &bithenge_uint64be_transform },
|
---|
1096 | { "uint64le", &bithenge_uint64le_transform },
|
---|
1097 | { "uint_be", &bithenge_uint_be_transform },
|
---|
1098 | { "uint_le", &bithenge_uint_le_transform },
|
---|
1099 | { "zero_terminated", &bithenge_zero_terminated_transform },
|
---|
1100 | { NULL, NULL }
|
---|
1101 | };
|
---|
1102 |
|
---|
1103 | /** An array of named built-in transforms. */
|
---|
1104 | bithenge_named_transform_t *bithenge_primitive_transforms = primitive_transforms;
|
---|
1105 |
|
---|
1106 | /** @}
|
---|
1107 | */
|
---|