source: mainline/uspace/app/bithenge/transform.h@ 32eb01b

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

Bithenge: allow defining transforms with parameters

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