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. */
|
---|
44 | typedef 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. */
|
---|
52 | typedef 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. All transforms must provide
|
---|
60 | * apply and/or prefix_apply. To be used in struct transforms and repeat
|
---|
61 | * transforms, transforms must provide prefix_length and/or prefix_apply. */
|
---|
62 | typedef struct bithenge_transform_ops {
|
---|
63 | /** @copydoc bithenge_transform_t::bithenge_transform_apply */
|
---|
64 | int (*apply)(bithenge_transform_t *self, bithenge_scope_t *scope,
|
---|
65 | bithenge_node_t *in, bithenge_node_t **out);
|
---|
66 | /** @copydoc bithenge_transform_t::bithenge_transform_prefix_length */
|
---|
67 | int (*prefix_length)(bithenge_transform_t *self,
|
---|
68 | bithenge_scope_t *scope, bithenge_blob_t *blob, aoff64_t *out);
|
---|
69 | /** @copydoc bithenge_transform_t::bithenge_transform_prefix_apply */
|
---|
70 | int (*prefix_apply)(bithenge_transform_t *self,
|
---|
71 | bithenge_scope_t *scope, bithenge_blob_t *blob,
|
---|
72 | bithenge_node_t **out_node, aoff64_t *out_size);
|
---|
73 | /** Destroy the transform.
|
---|
74 | * @param self The transform. */
|
---|
75 | void (*destroy)(bithenge_transform_t *self);
|
---|
76 | } bithenge_transform_ops_t;
|
---|
77 |
|
---|
78 | /** Get the number of parameters required by a transform. This number is used
|
---|
79 | * by the parser and param-wrapper. Takes ownership of nothing.
|
---|
80 | * @param self The transform.
|
---|
81 | * @return The number of parameters required. */
|
---|
82 | static inline int bithenge_transform_num_params(bithenge_transform_t *self)
|
---|
83 | {
|
---|
84 | assert(self);
|
---|
85 | return self->num_params;
|
---|
86 | }
|
---|
87 |
|
---|
88 | /** Increment a transform's reference count.
|
---|
89 | * @param self The transform to reference. */
|
---|
90 | static inline void bithenge_transform_inc_ref(bithenge_transform_t *self)
|
---|
91 | {
|
---|
92 | assert(self);
|
---|
93 | self->refs++;
|
---|
94 | }
|
---|
95 |
|
---|
96 | /** Decrement a transform's reference count and free it if appropriate.
|
---|
97 | * @param self The transform to dereference, or NULL. */
|
---|
98 | static inline void bithenge_transform_dec_ref(bithenge_transform_t *self)
|
---|
99 | {
|
---|
100 | if (!self)
|
---|
101 | return;
|
---|
102 | assert(self->ops);
|
---|
103 | if (--self->refs == 0)
|
---|
104 | self->ops->destroy(self);
|
---|
105 | }
|
---|
106 |
|
---|
107 | /** A transform with a name. */
|
---|
108 | typedef struct {
|
---|
109 | const char *name;
|
---|
110 | bithenge_transform_t *transform;
|
---|
111 | } bithenge_named_transform_t;
|
---|
112 |
|
---|
113 | extern bithenge_transform_t bithenge_ascii_transform;
|
---|
114 | extern bithenge_transform_t bithenge_invalid_transform;
|
---|
115 | extern bithenge_transform_t bithenge_known_length_transform;
|
---|
116 | extern bithenge_transform_t bithenge_nonzero_boolean_transform;
|
---|
117 | extern bithenge_transform_t bithenge_uint8_transform;
|
---|
118 | extern bithenge_transform_t bithenge_uint16le_transform;
|
---|
119 | extern bithenge_transform_t bithenge_uint16be_transform;
|
---|
120 | extern bithenge_transform_t bithenge_uint32le_transform;
|
---|
121 | extern bithenge_transform_t bithenge_uint32be_transform;
|
---|
122 | extern bithenge_transform_t bithenge_uint64le_transform;
|
---|
123 | extern bithenge_transform_t bithenge_uint64be_transform;
|
---|
124 | extern bithenge_transform_t bithenge_zero_terminated_transform;
|
---|
125 | extern bithenge_named_transform_t *bithenge_primitive_transforms;
|
---|
126 |
|
---|
127 | int bithenge_init_transform(bithenge_transform_t *,
|
---|
128 | const bithenge_transform_ops_t *, int);
|
---|
129 | int bithenge_transform_apply(bithenge_transform_t *, bithenge_scope_t *,
|
---|
130 | bithenge_node_t *, bithenge_node_t **);
|
---|
131 | int bithenge_transform_prefix_length(bithenge_transform_t *,
|
---|
132 | bithenge_scope_t *, bithenge_blob_t *, aoff64_t *);
|
---|
133 | int bithenge_transform_prefix_apply(bithenge_transform_t *, bithenge_scope_t *,
|
---|
134 | bithenge_blob_t *, bithenge_node_t **, aoff64_t *);
|
---|
135 | int bithenge_new_scope_transform(bithenge_transform_t **,
|
---|
136 | bithenge_transform_t *, int);
|
---|
137 | int bithenge_new_composed_transform(bithenge_transform_t **,
|
---|
138 | bithenge_transform_t **, size_t);
|
---|
139 |
|
---|
140 | void bithenge_scope_init(bithenge_scope_t *);
|
---|
141 | void bithenge_scope_destroy(bithenge_scope_t *);
|
---|
142 | int bithenge_scope_copy(bithenge_scope_t *, bithenge_scope_t *);
|
---|
143 | void bithenge_scope_set_current_node(bithenge_scope_t *, bithenge_node_t *);
|
---|
144 | bithenge_node_t *bithenge_scope_get_current_node(bithenge_scope_t *);
|
---|
145 | int bithenge_scope_alloc_params(bithenge_scope_t *, int);
|
---|
146 | int bithenge_scope_set_param(bithenge_scope_t *, int, bithenge_node_t *);
|
---|
147 | int bithenge_scope_get_param(bithenge_scope_t *, int, bithenge_node_t **);
|
---|
148 |
|
---|
149 | #endif
|
---|
150 |
|
---|
151 | /** @}
|
---|
152 | */
|
---|