[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;
|
---|
| 48 | } bithenge_transform_t;
|
---|
| 49 |
|
---|
[43788b2] | 50 | /** Context and parameters used when applying transforms. */
|
---|
| 51 | typedef struct {
|
---|
| 52 | /** @privatesection */
|
---|
| 53 | } bithenge_transform_context_t;
|
---|
| 54 |
|
---|
[d5070ef] | 55 | /** Operations that may be provided by a transform. */
|
---|
[04a7435f] | 56 | typedef struct bithenge_transform_ops {
|
---|
[d5070ef] | 57 | /** @copydoc bithenge_transform_t::bithenge_transform_apply */
|
---|
[43788b2] | 58 | int (*apply)(bithenge_transform_t *self,
|
---|
| 59 | bithenge_transform_context_t *context, bithenge_node_t *in,
|
---|
| 60 | bithenge_node_t **out);
|
---|
[d5070ef] | 61 | /** @copydoc bithenge_transform_t::bithenge_transform_prefix_length */
|
---|
[43788b2] | 62 | int (*prefix_length)(bithenge_transform_t *self,
|
---|
| 63 | bithenge_transform_context_t *context, bithenge_blob_t *blob,
|
---|
| 64 | aoff64_t *out);
|
---|
[d5070ef] | 65 | /** Destroy the transform.
|
---|
[978ccaf1] | 66 | * @param self The transform. */
|
---|
| 67 | void (*destroy)(bithenge_transform_t *self);
|
---|
[d5070ef] | 68 | } bithenge_transform_ops_t;
|
---|
| 69 |
|
---|
[43788b2] | 70 | /** Initialize a transform context. It must be destroyed with @a
|
---|
| 71 | * bithenge_transform_context_destroy after it is used.
|
---|
| 72 | * @param[out] context The context to initialize. */
|
---|
| 73 | static inline void bithenge_transform_context_init(
|
---|
| 74 | bithenge_transform_context_t *context)
|
---|
| 75 | {
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | /** Destroy a transform context.
|
---|
| 79 | * @param context The context to destroy.
|
---|
| 80 | * @return EOK on success or an error code from errno.h. */
|
---|
| 81 | static inline void bithenge_transform_context_destroy(
|
---|
| 82 | bithenge_transform_context_t *context)
|
---|
| 83 | {
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | /** Apply a transform. Takes ownership of nothing.
|
---|
[d5070ef] | 87 | * @memberof bithenge_transform_t
|
---|
[978ccaf1] | 88 | * @param self The transform.
|
---|
[43788b2] | 89 | * @param context The context.
|
---|
[d5070ef] | 90 | * @param in The input tree.
|
---|
| 91 | * @param[out] out Where the output tree will be stored.
|
---|
| 92 | * @return EOK on success or an error code from errno.h. */
|
---|
[978ccaf1] | 93 | static inline int bithenge_transform_apply(bithenge_transform_t *self,
|
---|
[43788b2] | 94 | bithenge_transform_context_t *context, bithenge_node_t *in,
|
---|
| 95 | bithenge_node_t **out)
|
---|
[d5070ef] | 96 | {
|
---|
[978ccaf1] | 97 | assert(self);
|
---|
| 98 | assert(self->ops);
|
---|
[43788b2] | 99 | return self->ops->apply(self, context, in, out);
|
---|
[d5070ef] | 100 | }
|
---|
| 101 |
|
---|
| 102 | /** Find the length of the prefix of a blob this transform can use as input. In
|
---|
| 103 | * other words, figure out how many bytes this transform will use up. This
|
---|
[da0fef6] | 104 | * method is optional and can return an error, but it must succeed for struct
|
---|
[43788b2] | 105 | * subtransforms. Takes ownership of nothing.
|
---|
[d5070ef] | 106 | * @memberof bithenge_transform_t
|
---|
[978ccaf1] | 107 | * @param self The transform.
|
---|
[43788b2] | 108 | * @param context The context.
|
---|
[d5070ef] | 109 | * @param blob The blob.
|
---|
| 110 | * @param[out] out Where the prefix length will be stored.
|
---|
| 111 | * @return EOK on success, ENOTSUP if not supported, or another error code from
|
---|
| 112 | * errno.h. */
|
---|
[978ccaf1] | 113 | static inline int bithenge_transform_prefix_length(bithenge_transform_t *self,
|
---|
[43788b2] | 114 | bithenge_transform_context_t *context, bithenge_blob_t *blob,
|
---|
| 115 | aoff64_t *out)
|
---|
[d5070ef] | 116 | {
|
---|
[978ccaf1] | 117 | assert(self);
|
---|
| 118 | assert(self->ops);
|
---|
| 119 | if (!self->ops->prefix_length)
|
---|
[d5070ef] | 120 | return ENOTSUP;
|
---|
[43788b2] | 121 | return self->ops->prefix_length(self, context, blob, out);
|
---|
[d5070ef] | 122 | }
|
---|
| 123 |
|
---|
| 124 | /** Increment a transform's reference count.
|
---|
[978ccaf1] | 125 | * @param self The transform to reference. */
|
---|
| 126 | static inline void bithenge_transform_inc_ref(bithenge_transform_t *self)
|
---|
[d5070ef] | 127 | {
|
---|
[978ccaf1] | 128 | assert(self);
|
---|
| 129 | self->refs++;
|
---|
[d5070ef] | 130 | }
|
---|
| 131 |
|
---|
[f2da0bb] | 132 | /** Decrement a transform's reference count and free it if appropriate.
|
---|
[978ccaf1] | 133 | * @param self The transform to dereference, or NULL. */
|
---|
| 134 | static inline void bithenge_transform_dec_ref(bithenge_transform_t *self)
|
---|
[d5070ef] | 135 | {
|
---|
[978ccaf1] | 136 | if (!self)
|
---|
| 137 | return;
|
---|
| 138 | assert(self->ops);
|
---|
| 139 | if (--self->refs == 0)
|
---|
| 140 | self->ops->destroy(self);
|
---|
[d5070ef] | 141 | }
|
---|
| 142 |
|
---|
[0d1a8fd] | 143 | /** A transform with a name. */
|
---|
| 144 | typedef struct {
|
---|
| 145 | const char *name;
|
---|
| 146 | bithenge_transform_t *transform;
|
---|
| 147 | } bithenge_named_transform_t;
|
---|
| 148 |
|
---|
[600f5d1] | 149 | extern bithenge_transform_t bithenge_ascii_transform;
|
---|
[5f4cf872] | 150 | extern bithenge_transform_t bithenge_uint8_transform;
|
---|
| 151 | extern bithenge_transform_t bithenge_uint16le_transform;
|
---|
| 152 | extern bithenge_transform_t bithenge_uint16be_transform;
|
---|
[0d1a8fd] | 153 | extern bithenge_transform_t bithenge_uint32le_transform;
|
---|
| 154 | extern bithenge_transform_t bithenge_uint32be_transform;
|
---|
[5f4cf872] | 155 | extern bithenge_transform_t bithenge_uint64le_transform;
|
---|
| 156 | extern bithenge_transform_t bithenge_uint64be_transform;
|
---|
[600f5d1] | 157 | extern bithenge_transform_t bithenge_zero_terminated_transform;
|
---|
[0d1a8fd] | 158 | extern bithenge_named_transform_t *bithenge_primitive_transforms;
|
---|
[d5070ef] | 159 |
|
---|
[978ccaf1] | 160 | int bithenge_init_transform(bithenge_transform_t *self,
|
---|
[04a7435f] | 161 | const bithenge_transform_ops_t *ops);
|
---|
| 162 | int bithenge_new_struct(bithenge_transform_t **out,
|
---|
| 163 | bithenge_named_transform_t *subtransforms);
|
---|
[600f5d1] | 164 | int bithenge_new_composed_transform(bithenge_transform_t **,
|
---|
| 165 | bithenge_transform_t **, size_t);
|
---|
[04a7435f] | 166 |
|
---|
[d5070ef] | 167 | #endif
|
---|
| 168 |
|
---|
| 169 | /** @}
|
---|
| 170 | */
|
---|