source: mainline/uspace/app/bithenge/transform.h@ 47a728e1

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 47a728e1 was 78d3a00, checked in by Sean Bartell <wingedtachikoma@…>, 13 years ago

Bithenge: add switch transforms and sugar

  • Property mode set to 100644
File size: 9.7 KB
Line 
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#ifndef BITHENGE_TRANSFORM_H_
38#define BITHENGE_TRANSFORM_H_
39
40#include "blob.h"
41#include "tree.h"
42
43/** A transform that creates a new tree from an old tree. */
44typedef struct {
45 /** @privatesection */
46 const struct bithenge_transform_ops *ops;
47 unsigned int refs;
48 int num_params;
49} bithenge_transform_t;
50
51/** Context and parameters used when applying transforms. */
52typedef struct {
53 /** @privatesection */
54 int num_params;
55 bithenge_node_t **params;
56 bithenge_node_t *current_node;
57} bithenge_scope_t;
58
59/** Operations that may be provided by a transform. */
60typedef struct bithenge_transform_ops {
61 /** @copydoc bithenge_transform_t::bithenge_transform_apply */
62 int (*apply)(bithenge_transform_t *self, bithenge_scope_t *scope,
63 bithenge_node_t *in, bithenge_node_t **out);
64 /** @copydoc bithenge_transform_t::bithenge_transform_prefix_length */
65 int (*prefix_length)(bithenge_transform_t *self,
66 bithenge_scope_t *scope, bithenge_blob_t *blob, aoff64_t *out);
67 /** Destroy the transform.
68 * @param self The transform. */
69 void (*destroy)(bithenge_transform_t *self);
70} bithenge_transform_ops_t;
71
72/** Initialize a transform scope. It must be destroyed with @a
73 * bithenge_scope_destroy after it is used.
74 * @param[out] scope The scope to initialize. */
75static inline void bithenge_scope_init(bithenge_scope_t *scope)
76{
77 scope->num_params = 0;
78 scope->params = NULL;
79 scope->current_node = NULL;
80}
81
82/** Destroy a transform scope.
83 * @param scope The scope to destroy.
84 * @return EOK on success or an error code from errno.h. */
85static inline void bithenge_scope_destroy(bithenge_scope_t *scope)
86{
87 bithenge_node_dec_ref(scope->current_node);
88 for (int i = 0; i < scope->num_params; i++)
89 bithenge_node_dec_ref(scope->params[i]);
90 free(scope->params);
91}
92
93/** Copy a scope.
94 * @param[out] out The scope to fill in; must have been initialized with @a
95 * bithenge_scope_init.
96 * @param scope The scope to copy.
97 * @return EOK on success or an error code from errno.h. */
98static inline int bithenge_scope_copy(bithenge_scope_t *out,
99 bithenge_scope_t *scope)
100{
101 out->params = malloc(sizeof(*out->params) * scope->num_params);
102 if (!out->params)
103 return ENOMEM;
104 memcpy(out->params, scope->params, sizeof(*out->params) *
105 scope->num_params);
106 out->num_params = scope->num_params;
107 for (int i = 0; i < out->num_params; i++)
108 bithenge_node_inc_ref(out->params[i]);
109 out->current_node = scope->current_node;
110 if (out->current_node)
111 bithenge_node_inc_ref(out->current_node);
112 return EOK;
113}
114
115/** Set the current node being created. Takes a reference to @a node.
116 * @param scope The scope to set the current node in.
117 * @param node The current node being created.
118 * @return EOK on success or an error code from errno.h. */
119static inline void bithenge_scope_set_current_node(bithenge_scope_t *scope,
120 bithenge_node_t *node)
121{
122 bithenge_node_dec_ref(scope->current_node);
123 scope->current_node = node;
124}
125
126/** Get the current node being created, which may be NULL.
127 * @param scope The scope to get the current node from.
128 * @return The node being created, or NULL. */
129static inline bithenge_node_t *bithenge_scope_get_current_node(
130 bithenge_scope_t *scope)
131{
132 if (scope->current_node)
133 bithenge_node_inc_ref(scope->current_node);
134 return scope->current_node;
135}
136
137/** Allocate parameters. The parameters must then be set with @a
138 * bithenge_scope_set_param. This must not be called on a scope that already
139 * has parameters.
140 * @param scope The scope in which to allocate parameters.
141 * @param num_params The number of parameters to allocate.
142 * @return EOK on success or an error code from errno.h. */
143static inline int bithenge_scope_alloc_params(bithenge_scope_t *scope,
144 int num_params)
145{
146 scope->params = malloc(sizeof(*scope->params) * num_params);
147 if (!scope->params)
148 return ENOMEM;
149 scope->num_params = num_params;
150 for (int i = 0; i < num_params; i++)
151 scope->params[i] = NULL;
152 return EOK;
153}
154
155/** Set a parameter. Takes a reference to @a value. Note that range checking is
156 * not done in release builds.
157 * @param scope The scope in which to allocate parameters.
158 * @param i The index of the parameter to set.
159 * @param value The value to store in the parameter.
160 * @return EOK on success or an error code from errno.h. */
161static inline int bithenge_scope_set_param( bithenge_scope_t *scope, int i,
162 bithenge_node_t *node)
163{
164 assert(scope);
165 assert(i >= 0 && i < scope->num_params);
166 scope->params[i] = node;
167 return EOK;
168}
169
170/** Get a parameter. Note that range checking is not done in release builds.
171 * @param scope The scope to get the parameter from.
172 * @param i The index of the parameter to set.
173 * @param[out] out Stores a new reference to the parameter.
174 * @return EOK on success or an error code from errno.h. */
175static inline int bithenge_scope_get_param(bithenge_scope_t *scope, int i,
176 bithenge_node_t **out)
177{
178 assert(scope);
179 assert(i >= 0 && i < scope->num_params);
180 *out = scope->params[i];
181 bithenge_node_inc_ref(*out);
182 return EOK;
183}
184
185/** Get the number of parameters required by a transform. This number is used
186 * by the parser and param-wrapper. Takes ownership of nothing.
187 * @param self The transform.
188 * @return The number of parameters required. */
189static inline int bithenge_transform_num_params(bithenge_transform_t *self)
190{
191 assert(self);
192 return self->num_params;
193}
194
195/** Apply a transform. Takes ownership of nothing.
196 * @memberof bithenge_transform_t
197 * @param self The transform.
198 * @param scope The scope.
199 * @param in The input tree.
200 * @param[out] out Where the output tree will be stored.
201 * @return EOK on success or an error code from errno.h. */
202static inline int bithenge_transform_apply(bithenge_transform_t *self,
203 bithenge_scope_t *scope, bithenge_node_t *in, bithenge_node_t **out)
204{
205 assert(self);
206 assert(self->ops);
207 return self->ops->apply(self, scope, in, out);
208}
209
210/** Find the length of the prefix of a blob this transform can use as input. In
211 * other words, figure out how many bytes this transform will use up. This
212 * method is optional and can return an error, but it must succeed for struct
213 * subtransforms. Takes ownership of nothing.
214 * @memberof bithenge_transform_t
215 * @param self The transform.
216 * @param scope The scope.
217 * @param blob The blob.
218 * @param[out] out Where the prefix length will be stored.
219 * @return EOK on success, ENOTSUP if not supported, or another error code from
220 * errno.h. */
221static inline int bithenge_transform_prefix_length(bithenge_transform_t *self,
222 bithenge_scope_t *scope, bithenge_blob_t *blob, aoff64_t *out)
223{
224 assert(self);
225 assert(self->ops);
226 if (!self->ops->prefix_length)
227 return ENOTSUP;
228 return self->ops->prefix_length(self, scope, blob, out);
229}
230
231/** Increment a transform's reference count.
232 * @param self The transform to reference. */
233static inline void bithenge_transform_inc_ref(bithenge_transform_t *self)
234{
235 assert(self);
236 self->refs++;
237}
238
239/** Decrement a transform's reference count and free it if appropriate.
240 * @param self The transform to dereference, or NULL. */
241static inline void bithenge_transform_dec_ref(bithenge_transform_t *self)
242{
243 if (!self)
244 return;
245 assert(self->ops);
246 if (--self->refs == 0)
247 self->ops->destroy(self);
248}
249
250/** A transform with a name. */
251typedef struct {
252 const char *name;
253 bithenge_transform_t *transform;
254} bithenge_named_transform_t;
255
256extern bithenge_transform_t bithenge_ascii_transform;
257extern bithenge_transform_t bithenge_known_length_transform;
258extern bithenge_transform_t bithenge_invalid_transform;
259extern bithenge_transform_t bithenge_uint8_transform;
260extern bithenge_transform_t bithenge_uint16le_transform;
261extern bithenge_transform_t bithenge_uint16be_transform;
262extern bithenge_transform_t bithenge_uint32le_transform;
263extern bithenge_transform_t bithenge_uint32be_transform;
264extern bithenge_transform_t bithenge_uint64le_transform;
265extern bithenge_transform_t bithenge_uint64be_transform;
266extern bithenge_transform_t bithenge_zero_terminated_transform;
267extern bithenge_named_transform_t *bithenge_primitive_transforms;
268
269int bithenge_init_transform(bithenge_transform_t *,
270 const bithenge_transform_ops_t *, int);
271int bithenge_new_param_transform(bithenge_transform_t **,
272 bithenge_transform_t *, int);
273int bithenge_new_struct(bithenge_transform_t **,
274 bithenge_named_transform_t *);
275int bithenge_new_composed_transform(bithenge_transform_t **,
276 bithenge_transform_t **, size_t);
277
278#endif
279
280/** @}
281 */
Note: See TracBrowser for help on using the repository browser.