| 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 bithenge_scope {
|
|---|
| 53 | /** @privatesection */
|
|---|
| 54 | unsigned int refs;
|
|---|
| 55 | struct bithenge_scope *outer;
|
|---|
| 56 | char *error;
|
|---|
| 57 | bool barrier;
|
|---|
| 58 | int num_params;
|
|---|
| 59 | bithenge_node_t **params;
|
|---|
| 60 | bithenge_node_t *current_node;
|
|---|
| 61 | bithenge_node_t *in_node;
|
|---|
| 62 | } bithenge_scope_t;
|
|---|
| 63 |
|
|---|
| 64 | static inline void bithenge_scope_inc_ref(bithenge_scope_t *self) {
|
|---|
| 65 | self->refs++;
|
|---|
| 66 | }
|
|---|
| 67 |
|
|---|
| 68 | /** Operations that may be provided by a transform. All transforms must provide
|
|---|
| 69 | * apply and/or prefix_apply. To be used in struct transforms and repeat
|
|---|
| 70 | * transforms, transforms must provide prefix_length and/or prefix_apply. */
|
|---|
| 71 | typedef struct bithenge_transform_ops {
|
|---|
| 72 | /** @copydoc bithenge_transform_t::bithenge_transform_apply */
|
|---|
| 73 | int (*apply)(bithenge_transform_t *self, bithenge_scope_t *scope,
|
|---|
| 74 | bithenge_node_t *in, bithenge_node_t **out);
|
|---|
| 75 | /** @copydoc bithenge_transform_t::bithenge_transform_prefix_length */
|
|---|
| 76 | int (*prefix_length)(bithenge_transform_t *self,
|
|---|
| 77 | bithenge_scope_t *scope, bithenge_blob_t *blob, aoff64_t *out);
|
|---|
| 78 | /** @copydoc bithenge_transform_t::bithenge_transform_prefix_apply */
|
|---|
| 79 | int (*prefix_apply)(bithenge_transform_t *self,
|
|---|
| 80 | bithenge_scope_t *scope, bithenge_blob_t *blob,
|
|---|
| 81 | bithenge_node_t **out_node, aoff64_t *out_size);
|
|---|
| 82 | /** Destroy the transform.
|
|---|
| 83 | * @param self The transform. */
|
|---|
| 84 | void (*destroy)(bithenge_transform_t *self);
|
|---|
| 85 | } bithenge_transform_ops_t;
|
|---|
| 86 |
|
|---|
| 87 | /** Get the number of parameters required by a transform. This number is used
|
|---|
| 88 | * by the parser and param-wrapper. Takes ownership of nothing.
|
|---|
| 89 | * @param self The transform.
|
|---|
| 90 | * @return The number of parameters required. */
|
|---|
| 91 | static inline int bithenge_transform_num_params(bithenge_transform_t *self)
|
|---|
| 92 | {
|
|---|
| 93 | assert(self);
|
|---|
| 94 | return self->num_params;
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | /** Increment a transform's reference count.
|
|---|
| 98 | * @param self The transform to reference. */
|
|---|
| 99 | static inline void bithenge_transform_inc_ref(bithenge_transform_t *self)
|
|---|
| 100 | {
|
|---|
| 101 | assert(self);
|
|---|
| 102 | self->refs++;
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | /** Decrement a transform's reference count and free it if appropriate.
|
|---|
| 106 | * @param self The transform to dereference, or NULL. */
|
|---|
| 107 | static inline void bithenge_transform_dec_ref(bithenge_transform_t *self)
|
|---|
| 108 | {
|
|---|
| 109 | if (!self)
|
|---|
| 110 | return;
|
|---|
| 111 | assert(self->ops);
|
|---|
| 112 | if (--self->refs == 0)
|
|---|
| 113 | self->ops->destroy(self);
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | /** A transform with a name. */
|
|---|
| 117 | typedef struct {
|
|---|
| 118 | const char *name;
|
|---|
| 119 | bithenge_transform_t *transform;
|
|---|
| 120 | } bithenge_named_transform_t;
|
|---|
| 121 |
|
|---|
| 122 | extern bithenge_transform_t bithenge_ascii_transform;
|
|---|
| 123 | extern bithenge_transform_t bithenge_bit_transform;
|
|---|
| 124 | extern bithenge_transform_t bithenge_bits_be_transform;
|
|---|
| 125 | extern bithenge_transform_t bithenge_bits_le_transform;
|
|---|
| 126 | extern bithenge_transform_t bithenge_invalid_transform;
|
|---|
| 127 | extern bithenge_transform_t bithenge_known_length_transform;
|
|---|
| 128 | extern bithenge_transform_t bithenge_nonzero_boolean_transform;
|
|---|
| 129 | extern bithenge_transform_t bithenge_uint8_transform;
|
|---|
| 130 | extern bithenge_transform_t bithenge_uint16le_transform;
|
|---|
| 131 | extern bithenge_transform_t bithenge_uint16be_transform;
|
|---|
| 132 | extern bithenge_transform_t bithenge_uint32le_transform;
|
|---|
| 133 | extern bithenge_transform_t bithenge_uint32be_transform;
|
|---|
| 134 | extern bithenge_transform_t bithenge_uint64le_transform;
|
|---|
| 135 | extern bithenge_transform_t bithenge_uint64be_transform;
|
|---|
| 136 | extern bithenge_transform_t bithenge_uint_le_transform;
|
|---|
| 137 | extern bithenge_transform_t bithenge_uint_be_transform;
|
|---|
| 138 | extern bithenge_transform_t bithenge_zero_terminated_transform;
|
|---|
| 139 | extern bithenge_named_transform_t *bithenge_primitive_transforms;
|
|---|
| 140 |
|
|---|
| 141 | int bithenge_init_transform(bithenge_transform_t *,
|
|---|
| 142 | const bithenge_transform_ops_t *, int);
|
|---|
| 143 | int bithenge_transform_apply(bithenge_transform_t *, bithenge_scope_t *,
|
|---|
| 144 | bithenge_node_t *, bithenge_node_t **);
|
|---|
| 145 | int bithenge_transform_prefix_length(bithenge_transform_t *,
|
|---|
| 146 | bithenge_scope_t *, bithenge_blob_t *, aoff64_t *);
|
|---|
| 147 | int bithenge_transform_prefix_apply(bithenge_transform_t *, bithenge_scope_t *,
|
|---|
| 148 | bithenge_blob_t *, bithenge_node_t **, aoff64_t *);
|
|---|
| 149 | int bithenge_new_barrier_transform(bithenge_transform_t **,
|
|---|
| 150 | bithenge_transform_t *, int);
|
|---|
| 151 |
|
|---|
| 152 | int bithenge_scope_new(bithenge_scope_t **, bithenge_scope_t *);
|
|---|
| 153 | void bithenge_scope_dec_ref(bithenge_scope_t *);
|
|---|
| 154 | bithenge_scope_t *bithenge_scope_outer(bithenge_scope_t *);
|
|---|
| 155 | const char *bithenge_scope_get_error(bithenge_scope_t *);
|
|---|
| 156 | int bithenge_scope_error(bithenge_scope_t *, const char *, ...);
|
|---|
| 157 | bithenge_node_t *bithenge_scope_get_current_node(bithenge_scope_t *);
|
|---|
| 158 | void bithenge_scope_set_current_node(bithenge_scope_t *, bithenge_node_t *);
|
|---|
| 159 | bithenge_node_t *bithenge_scope_in_node(bithenge_scope_t *);
|
|---|
| 160 | void bithenge_scope_set_in_node(bithenge_scope_t *, bithenge_node_t *);
|
|---|
| 161 | void bithenge_scope_set_barrier(bithenge_scope_t *);
|
|---|
| 162 | bool bithenge_scope_is_barrier(bithenge_scope_t *);
|
|---|
| 163 | int bithenge_scope_alloc_params(bithenge_scope_t *, int);
|
|---|
| 164 | int bithenge_scope_set_param(bithenge_scope_t *, int, bithenge_node_t *);
|
|---|
| 165 | int bithenge_scope_get_param(bithenge_scope_t *, int, bithenge_node_t **);
|
|---|
| 166 |
|
|---|
| 167 | #endif
|
|---|
| 168 |
|
|---|
| 169 | /** @}
|
|---|
| 170 | */
|
|---|