[d5070ef] | 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 |
|
---|
[03b2b2c] | 40 | #include "blob.h"
|
---|
[d5070ef] | 41 | #include "tree.h"
|
---|
| 42 |
|
---|
| 43 | /** A transform that creates a new tree from an old tree. */
|
---|
| 44 | typedef struct {
|
---|
| 45 | /** @privatesection */
|
---|
[04a7435f] | 46 | const struct bithenge_transform_ops *ops;
|
---|
[d5070ef] | 47 | unsigned int refs;
|
---|
[03cad47] | 48 | int num_params;
|
---|
[d5070ef] | 49 | } bithenge_transform_t;
|
---|
| 50 |
|
---|
[43788b2] | 51 | /** Context and parameters used when applying transforms. */
|
---|
| 52 | typedef struct {
|
---|
| 53 | /** @privatesection */
|
---|
[5a7c0e6] | 54 | int num_params;
|
---|
[f85ca3f] | 55 | bithenge_node_t **params;
|
---|
| 56 | bithenge_node_t *current_node;
|
---|
[5a7c0e6] | 57 | } bithenge_scope_t;
|
---|
[43788b2] | 58 |
|
---|
[cb4a66d2] | 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. */
|
---|
[04a7435f] | 62 | typedef struct bithenge_transform_ops {
|
---|
[d5070ef] | 63 | /** @copydoc bithenge_transform_t::bithenge_transform_apply */
|
---|
[5a7c0e6] | 64 | int (*apply)(bithenge_transform_t *self, bithenge_scope_t *scope,
|
---|
| 65 | bithenge_node_t *in, bithenge_node_t **out);
|
---|
[d5070ef] | 66 | /** @copydoc bithenge_transform_t::bithenge_transform_prefix_length */
|
---|
[43788b2] | 67 | int (*prefix_length)(bithenge_transform_t *self,
|
---|
[5a7c0e6] | 68 | bithenge_scope_t *scope, bithenge_blob_t *blob, aoff64_t *out);
|
---|
[cb4a66d2] | 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);
|
---|
[d5070ef] | 73 | /** Destroy the transform.
|
---|
[978ccaf1] | 74 | * @param self The transform. */
|
---|
| 75 | void (*destroy)(bithenge_transform_t *self);
|
---|
[d5070ef] | 76 | } bithenge_transform_ops_t;
|
---|
| 77 |
|
---|
[03cad47] | 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.
|
---|
[5a7c0e6] | 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);
|
---|
[03cad47] | 85 | return self->num_params;
|
---|
[43788b2] | 86 | }
|
---|
| 87 |
|
---|
[d5070ef] | 88 | /** Increment a transform's reference count.
|
---|
[978ccaf1] | 89 | * @param self The transform to reference. */
|
---|
| 90 | static inline void bithenge_transform_inc_ref(bithenge_transform_t *self)
|
---|
[d5070ef] | 91 | {
|
---|
[978ccaf1] | 92 | assert(self);
|
---|
| 93 | self->refs++;
|
---|
[d5070ef] | 94 | }
|
---|
| 95 |
|
---|
[f2da0bb] | 96 | /** Decrement a transform's reference count and free it if appropriate.
|
---|
[978ccaf1] | 97 | * @param self The transform to dereference, or NULL. */
|
---|
| 98 | static inline void bithenge_transform_dec_ref(bithenge_transform_t *self)
|
---|
[d5070ef] | 99 | {
|
---|
[978ccaf1] | 100 | if (!self)
|
---|
| 101 | return;
|
---|
| 102 | assert(self->ops);
|
---|
| 103 | if (--self->refs == 0)
|
---|
| 104 | self->ops->destroy(self);
|
---|
[d5070ef] | 105 | }
|
---|
| 106 |
|
---|
[0d1a8fd] | 107 | /** A transform with a name. */
|
---|
| 108 | typedef struct {
|
---|
| 109 | const char *name;
|
---|
| 110 | bithenge_transform_t *transform;
|
---|
| 111 | } bithenge_named_transform_t;
|
---|
| 112 |
|
---|
[600f5d1] | 113 | extern bithenge_transform_t bithenge_ascii_transform;
|
---|
[4056ad0] | 114 | extern bithenge_transform_t bithenge_known_length_transform;
|
---|
[78d3a00] | 115 | extern bithenge_transform_t bithenge_invalid_transform;
|
---|
[5f4cf872] | 116 | extern bithenge_transform_t bithenge_uint8_transform;
|
---|
| 117 | extern bithenge_transform_t bithenge_uint16le_transform;
|
---|
| 118 | extern bithenge_transform_t bithenge_uint16be_transform;
|
---|
[0d1a8fd] | 119 | extern bithenge_transform_t bithenge_uint32le_transform;
|
---|
| 120 | extern bithenge_transform_t bithenge_uint32be_transform;
|
---|
[5f4cf872] | 121 | extern bithenge_transform_t bithenge_uint64le_transform;
|
---|
| 122 | extern bithenge_transform_t bithenge_uint64be_transform;
|
---|
[600f5d1] | 123 | extern bithenge_transform_t bithenge_zero_terminated_transform;
|
---|
[0d1a8fd] | 124 | extern bithenge_named_transform_t *bithenge_primitive_transforms;
|
---|
[d5070ef] | 125 |
|
---|
[4056ad0] | 126 | int bithenge_init_transform(bithenge_transform_t *,
|
---|
| 127 | const bithenge_transform_ops_t *, int);
|
---|
[cb4a66d2] | 128 | int bithenge_transform_apply(bithenge_transform_t *, bithenge_scope_t *,
|
---|
| 129 | bithenge_node_t *, bithenge_node_t **);
|
---|
| 130 | int bithenge_transform_prefix_length(bithenge_transform_t *,
|
---|
| 131 | bithenge_scope_t *, bithenge_blob_t *, aoff64_t *);
|
---|
| 132 | int bithenge_transform_prefix_apply(bithenge_transform_t *, bithenge_scope_t *,
|
---|
| 133 | bithenge_blob_t *, bithenge_node_t **, aoff64_t *);
|
---|
[b8d45e9e] | 134 | int bithenge_new_scope_transform(bithenge_transform_t **,
|
---|
[4056ad0] | 135 | bithenge_transform_t *, int);
|
---|
| 136 | int bithenge_new_struct(bithenge_transform_t **,
|
---|
| 137 | bithenge_named_transform_t *);
|
---|
[600f5d1] | 138 | int bithenge_new_composed_transform(bithenge_transform_t **,
|
---|
| 139 | bithenge_transform_t **, size_t);
|
---|
[04a7435f] | 140 |
|
---|
[b8d45e9e] | 141 | void bithenge_scope_init(bithenge_scope_t *);
|
---|
| 142 | void bithenge_scope_destroy(bithenge_scope_t *);
|
---|
| 143 | int bithenge_scope_copy(bithenge_scope_t *, bithenge_scope_t *);
|
---|
| 144 | void bithenge_scope_set_current_node(bithenge_scope_t *, bithenge_node_t *);
|
---|
| 145 | bithenge_node_t *bithenge_scope_get_current_node(bithenge_scope_t *);
|
---|
| 146 | int bithenge_scope_alloc_params(bithenge_scope_t *, int);
|
---|
| 147 | int bithenge_scope_set_param(bithenge_scope_t *, int, bithenge_node_t *);
|
---|
| 148 | int bithenge_scope_get_param(bithenge_scope_t *, int, bithenge_node_t **);
|
---|
| 149 |
|
---|
[d5070ef] | 150 | #endif
|
---|
| 151 |
|
---|
| 152 | /** @}
|
---|
| 153 | */
|
---|