[3c70376] | 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 | * Compound transforms.
|
---|
| 35 | */
|
---|
| 36 |
|
---|
| 37 | #include <stdlib.h>
|
---|
[8fc0f47c] | 38 | #include <bithenge/compound.h>
|
---|
| 39 | #include <bithenge/expression.h>
|
---|
| 40 | #include <bithenge/transform.h>
|
---|
| 41 | #include <bithenge/tree.h>
|
---|
[6cd10ac] | 42 | #include "common.h"
|
---|
[3c70376] | 43 |
|
---|
| 44 | /***************** compose_transform *****************/
|
---|
| 45 |
|
---|
| 46 | typedef struct {
|
---|
| 47 | bithenge_transform_t base;
|
---|
| 48 | bithenge_transform_t **xforms;
|
---|
| 49 | size_t num;
|
---|
| 50 | } compose_transform_t;
|
---|
| 51 |
|
---|
| 52 | static bithenge_transform_t *compose_as_transform(compose_transform_t *xform)
|
---|
| 53 | {
|
---|
| 54 | return &xform->base;
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | static compose_transform_t *transform_as_compose(bithenge_transform_t *xform)
|
---|
| 58 | {
|
---|
| 59 | return (compose_transform_t *)xform;
|
---|
| 60 | }
|
---|
| 61 |
|
---|
[b7fd2a0] | 62 | static errno_t compose_apply(bithenge_transform_t *base, bithenge_scope_t *scope,
|
---|
[3c70376] | 63 | bithenge_node_t *in, bithenge_node_t **out)
|
---|
| 64 | {
|
---|
[b7fd2a0] | 65 | errno_t rc;
|
---|
[3c70376] | 66 | compose_transform_t *self = transform_as_compose(base);
|
---|
| 67 | bithenge_node_inc_ref(in);
|
---|
| 68 |
|
---|
| 69 | /* i ranges from (self->num - 1) to 0 inside the loop. */
|
---|
[84239b1] | 70 | size_t i = self->num;
|
---|
| 71 | while (i-- != 0) {
|
---|
[3c70376] | 72 | bithenge_node_t *tmp;
|
---|
| 73 | rc = bithenge_transform_apply(self->xforms[i], scope, in,
|
---|
| 74 | &tmp);
|
---|
| 75 | bithenge_node_dec_ref(in);
|
---|
| 76 | if (rc != EOK)
|
---|
| 77 | return rc;
|
---|
| 78 | in = tmp;
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | *out = in;
|
---|
[0464967] | 82 | return EOK;
|
---|
[3c70376] | 83 | }
|
---|
| 84 |
|
---|
[b7fd2a0] | 85 | static errno_t compose_prefix_length(bithenge_transform_t *base,
|
---|
[3c70376] | 86 | bithenge_scope_t *scope, bithenge_blob_t *blob, aoff64_t *out)
|
---|
| 87 | {
|
---|
| 88 | compose_transform_t *self = transform_as_compose(base);
|
---|
| 89 | return bithenge_transform_prefix_length(self->xforms[self->num - 1],
|
---|
| 90 | scope, blob, out);
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | static void compose_destroy(bithenge_transform_t *base)
|
---|
| 94 | {
|
---|
| 95 | compose_transform_t *self = transform_as_compose(base);
|
---|
| 96 | for (size_t i = 0; i < self->num; i++)
|
---|
| 97 | bithenge_transform_dec_ref(self->xforms[i]);
|
---|
| 98 | free(self->xforms);
|
---|
| 99 | free(self);
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | static const bithenge_transform_ops_t compose_transform_ops = {
|
---|
| 103 | .apply = compose_apply,
|
---|
| 104 | .prefix_length = compose_prefix_length,
|
---|
| 105 | .destroy = compose_destroy,
|
---|
| 106 | };
|
---|
| 107 |
|
---|
| 108 | /** Create a composition of multiple transforms. When the result is applied to a
|
---|
| 109 | * node, each transform is applied in turn, with the last transform applied
|
---|
| 110 | * first. @a xforms may contain any number of transforms or no transforms at
|
---|
| 111 | * all. This function takes ownership of @a xforms and the references therein.
|
---|
| 112 | * @param[out] out Holds the result.
|
---|
| 113 | * @param[in] xforms The transforms to apply.
|
---|
| 114 | * @param num The number of transforms.
|
---|
[7c3fb9b] | 115 | * @return EOK on success or an error code from errno.h.
|
---|
| 116 | */
|
---|
[b7fd2a0] | 117 | errno_t bithenge_new_composed_transform(bithenge_transform_t **out,
|
---|
[3c70376] | 118 | bithenge_transform_t **xforms, size_t num)
|
---|
| 119 | {
|
---|
| 120 | if (num == 0) {
|
---|
| 121 | /* TODO: optimize */
|
---|
| 122 | } else if (num == 1) {
|
---|
| 123 | *out = xforms[0];
|
---|
| 124 | free(xforms);
|
---|
| 125 | return EOK;
|
---|
| 126 | }
|
---|
| 127 |
|
---|
[b7fd2a0] | 128 | errno_t rc;
|
---|
[3c70376] | 129 | compose_transform_t *self = malloc(sizeof(*self));
|
---|
| 130 | if (!self) {
|
---|
| 131 | rc = ENOMEM;
|
---|
| 132 | goto error;
|
---|
| 133 | }
|
---|
| 134 | rc = bithenge_init_transform(compose_as_transform(self),
|
---|
| 135 | &compose_transform_ops, 0);
|
---|
| 136 | if (rc != EOK)
|
---|
| 137 | goto error;
|
---|
| 138 | self->xforms = xforms;
|
---|
| 139 | self->num = num;
|
---|
| 140 | *out = compose_as_transform(self);
|
---|
| 141 | return EOK;
|
---|
| 142 | error:
|
---|
| 143 | for (size_t i = 0; i < num; i++)
|
---|
| 144 | bithenge_transform_dec_ref(xforms[i]);
|
---|
| 145 | free(xforms);
|
---|
| 146 | free(self);
|
---|
| 147 | return rc;
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 | /***************** if_transform *****************/
|
---|
| 151 |
|
---|
| 152 | typedef struct {
|
---|
| 153 | bithenge_transform_t base;
|
---|
| 154 | bithenge_expression_t *expr;
|
---|
| 155 | bithenge_transform_t *true_xform, *false_xform;
|
---|
| 156 | } if_transform_t;
|
---|
| 157 |
|
---|
| 158 | static inline bithenge_transform_t *if_as_transform(if_transform_t *self)
|
---|
| 159 | {
|
---|
| 160 | return &self->base;
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | static inline if_transform_t *transform_as_if(bithenge_transform_t *base)
|
---|
| 164 | {
|
---|
| 165 | return (if_transform_t *)base;
|
---|
| 166 | }
|
---|
| 167 |
|
---|
[b7fd2a0] | 168 | static errno_t if_transform_choose(if_transform_t *self, bithenge_scope_t *scope,
|
---|
[3c70376] | 169 | bool *out)
|
---|
| 170 | {
|
---|
| 171 | bithenge_node_t *cond_node;
|
---|
[b7fd2a0] | 172 | errno_t rc = bithenge_expression_evaluate(self->expr, scope, &cond_node);
|
---|
[3c70376] | 173 | if (rc != EOK)
|
---|
| 174 | return rc;
|
---|
| 175 | if (bithenge_node_type(cond_node) != BITHENGE_NODE_BOOLEAN) {
|
---|
| 176 | bithenge_node_dec_ref(cond_node);
|
---|
| 177 | return EINVAL;
|
---|
| 178 | }
|
---|
| 179 | *out = bithenge_boolean_node_value(cond_node);
|
---|
| 180 | bithenge_node_dec_ref(cond_node);
|
---|
| 181 | return EOK;
|
---|
| 182 | }
|
---|
| 183 |
|
---|
[b7fd2a0] | 184 | static errno_t if_transform_apply(bithenge_transform_t *base,
|
---|
[3c70376] | 185 | bithenge_scope_t *scope, bithenge_node_t *in, bithenge_node_t **out)
|
---|
| 186 | {
|
---|
| 187 | if_transform_t *self = transform_as_if(base);
|
---|
| 188 | bool cond;
|
---|
[b7fd2a0] | 189 | errno_t rc = if_transform_choose(self, scope, &cond);
|
---|
[3c70376] | 190 | if (rc != EOK)
|
---|
| 191 | return rc;
|
---|
| 192 | return bithenge_transform_apply(
|
---|
| 193 | cond ? self->true_xform : self->false_xform, scope, in, out);
|
---|
| 194 | }
|
---|
| 195 |
|
---|
[b7fd2a0] | 196 | static errno_t if_transform_prefix_length(bithenge_transform_t *base,
|
---|
[3c70376] | 197 | bithenge_scope_t *scope, bithenge_blob_t *in, aoff64_t *out)
|
---|
| 198 | {
|
---|
| 199 | if_transform_t *self = transform_as_if(base);
|
---|
| 200 | bool cond;
|
---|
[b7fd2a0] | 201 | errno_t rc = if_transform_choose(self, scope, &cond);
|
---|
[3c70376] | 202 | if (rc != EOK)
|
---|
| 203 | return rc;
|
---|
| 204 | return bithenge_transform_prefix_length(
|
---|
| 205 | cond ? self->true_xform : self->false_xform, scope, in, out);
|
---|
| 206 | }
|
---|
| 207 |
|
---|
| 208 | static void if_transform_destroy(bithenge_transform_t *base)
|
---|
| 209 | {
|
---|
| 210 | if_transform_t *self = transform_as_if(base);
|
---|
| 211 | bithenge_expression_dec_ref(self->expr);
|
---|
| 212 | bithenge_transform_dec_ref(self->true_xform);
|
---|
| 213 | bithenge_transform_dec_ref(self->false_xform);
|
---|
| 214 | free(self);
|
---|
| 215 | }
|
---|
| 216 |
|
---|
| 217 | static const bithenge_transform_ops_t if_transform_ops = {
|
---|
| 218 | .apply = if_transform_apply,
|
---|
| 219 | .prefix_length = if_transform_prefix_length,
|
---|
| 220 | .destroy = if_transform_destroy,
|
---|
| 221 | };
|
---|
| 222 |
|
---|
| 223 | /** Create a transform that applies either of two transforms depending on a
|
---|
| 224 | * boolean expression. Takes references to @a expr, @a true_xform, and
|
---|
| 225 | * @a false_xform.
|
---|
| 226 | * @param[out] out Holds the new transform.
|
---|
| 227 | * @param expr The boolean expression to evaluate.
|
---|
| 228 | * @param true_xform The transform to apply if the expression is true.
|
---|
| 229 | * @param false_xform The transform to apply if the expression is false.
|
---|
[7c3fb9b] | 230 | * @return EOK on success or an error code from errno.h.
|
---|
| 231 | */
|
---|
[b7fd2a0] | 232 | errno_t bithenge_if_transform(bithenge_transform_t **out,
|
---|
[3c70376] | 233 | bithenge_expression_t *expr, bithenge_transform_t *true_xform,
|
---|
| 234 | bithenge_transform_t *false_xform)
|
---|
| 235 | {
|
---|
[b7fd2a0] | 236 | errno_t rc;
|
---|
[3c70376] | 237 | if_transform_t *self = malloc(sizeof(*self));
|
---|
| 238 | if (!self) {
|
---|
| 239 | rc = ENOMEM;
|
---|
| 240 | goto error;
|
---|
| 241 | }
|
---|
| 242 |
|
---|
| 243 | rc = bithenge_init_transform(if_as_transform(self), &if_transform_ops,
|
---|
| 244 | 0);
|
---|
| 245 | if (rc != EOK)
|
---|
| 246 | goto error;
|
---|
| 247 |
|
---|
| 248 | self->expr = expr;
|
---|
| 249 | self->true_xform = true_xform;
|
---|
| 250 | self->false_xform = false_xform;
|
---|
| 251 | *out = if_as_transform(self);
|
---|
| 252 | return EOK;
|
---|
| 253 |
|
---|
| 254 | error:
|
---|
| 255 | free(self);
|
---|
| 256 | bithenge_expression_dec_ref(expr);
|
---|
| 257 | bithenge_transform_dec_ref(true_xform);
|
---|
| 258 | bithenge_transform_dec_ref(false_xform);
|
---|
| 259 | return rc;
|
---|
| 260 | }
|
---|
| 261 |
|
---|
[c54f5d0] | 262 | /***************** partial_transform *****************/
|
---|
| 263 |
|
---|
| 264 | typedef struct {
|
---|
| 265 | bithenge_transform_t base;
|
---|
| 266 | bithenge_transform_t *xform;
|
---|
| 267 | } partial_transform_t;
|
---|
| 268 |
|
---|
| 269 | static inline bithenge_transform_t *partial_as_transform(
|
---|
| 270 | partial_transform_t *self)
|
---|
| 271 | {
|
---|
| 272 | return &self->base;
|
---|
| 273 | }
|
---|
| 274 |
|
---|
| 275 | static inline partial_transform_t *transform_as_partial(
|
---|
| 276 | bithenge_transform_t *base)
|
---|
| 277 | {
|
---|
| 278 | return (partial_transform_t *)base;
|
---|
| 279 | }
|
---|
| 280 |
|
---|
[b7fd2a0] | 281 | static errno_t partial_transform_apply(bithenge_transform_t *base,
|
---|
[c54f5d0] | 282 | bithenge_scope_t *scope, bithenge_node_t *in, bithenge_node_t **out)
|
---|
| 283 | {
|
---|
| 284 | partial_transform_t *self = transform_as_partial(base);
|
---|
| 285 | if (bithenge_node_type(in) != BITHENGE_NODE_BLOB)
|
---|
| 286 | return EINVAL;
|
---|
| 287 | return bithenge_transform_prefix_apply(self->xform, scope,
|
---|
[1b6b76d] | 288 | bithenge_node_as_blob(in), out, NULL);
|
---|
[c54f5d0] | 289 | }
|
---|
| 290 |
|
---|
| 291 | static void partial_transform_destroy(bithenge_transform_t *base)
|
---|
| 292 | {
|
---|
| 293 | partial_transform_t *self = transform_as_partial(base);
|
---|
| 294 | bithenge_transform_dec_ref(self->xform);
|
---|
| 295 | free(self);
|
---|
| 296 | }
|
---|
| 297 |
|
---|
| 298 | static const bithenge_transform_ops_t partial_transform_ops = {
|
---|
| 299 | .apply = partial_transform_apply,
|
---|
| 300 | .destroy = partial_transform_destroy,
|
---|
| 301 | };
|
---|
| 302 |
|
---|
| 303 | /** Create a transform that doesn't require its subtransform to use the whole
|
---|
| 304 | * input. Takes a reference to @a xform.
|
---|
| 305 | * @param[out] out Holds the new transform.
|
---|
| 306 | * @param xform The subtransform to apply.
|
---|
[7c3fb9b] | 307 | * @return EOK on success or an error code from errno.h.
|
---|
| 308 | */
|
---|
[b7fd2a0] | 309 | errno_t bithenge_partial_transform(bithenge_transform_t **out,
|
---|
[c54f5d0] | 310 | bithenge_transform_t *xform)
|
---|
| 311 | {
|
---|
[b7fd2a0] | 312 | errno_t rc;
|
---|
[c54f5d0] | 313 | partial_transform_t *self = malloc(sizeof(*self));
|
---|
| 314 | if (!self) {
|
---|
| 315 | rc = ENOMEM;
|
---|
| 316 | goto error;
|
---|
| 317 | }
|
---|
| 318 |
|
---|
| 319 | rc = bithenge_init_transform(partial_as_transform(self),
|
---|
| 320 | &partial_transform_ops, 0);
|
---|
| 321 | if (rc != EOK)
|
---|
| 322 | goto error;
|
---|
| 323 |
|
---|
| 324 | self->xform = xform;
|
---|
| 325 | *out = partial_as_transform(self);
|
---|
| 326 | return EOK;
|
---|
| 327 |
|
---|
| 328 | error:
|
---|
| 329 | free(self);
|
---|
| 330 | bithenge_transform_dec_ref(xform);
|
---|
| 331 | return rc;
|
---|
| 332 | }
|
---|
| 333 |
|
---|
[3c70376] | 334 | /** @}
|
---|
| 335 | */
|
---|