| 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 | } bithenge_transform_t;
|
|---|
| 49 |
|
|---|
| 50 | /** Operations that may be provided by a transform. */
|
|---|
| 51 | typedef struct bithenge_transform_ops {
|
|---|
| 52 | /** @copydoc bithenge_transform_t::bithenge_transform_apply */
|
|---|
| 53 | int (*apply)(bithenge_transform_t *xform, bithenge_node_t *in, bithenge_node_t **out);
|
|---|
| 54 | /** @copydoc bithenge_transform_t::bithenge_transform_prefix_length */
|
|---|
| 55 | int (*prefix_length)(bithenge_transform_t *xform, bithenge_blob_t *blob, aoff64_t *out);
|
|---|
| 56 | /** Destroy the transform.
|
|---|
| 57 | * @param xform The transform.
|
|---|
| 58 | * @return EOK on success or an error code from errno.h. */
|
|---|
| 59 | int (*destroy)(bithenge_transform_t *xform);
|
|---|
| 60 | } bithenge_transform_ops_t;
|
|---|
| 61 |
|
|---|
| 62 | /** Apply a transform.
|
|---|
| 63 | * @memberof bithenge_transform_t
|
|---|
| 64 | * @param xform The transform.
|
|---|
| 65 | * @param in The input tree.
|
|---|
| 66 | * @param[out] out Where the output tree will be stored.
|
|---|
| 67 | * @return EOK on success or an error code from errno.h. */
|
|---|
| 68 | static inline int bithenge_transform_apply(bithenge_transform_t *xform,
|
|---|
| 69 | bithenge_node_t *in, bithenge_node_t **out)
|
|---|
| 70 | {
|
|---|
| 71 | assert(xform);
|
|---|
| 72 | assert(xform->ops);
|
|---|
| 73 | return xform->ops->apply(xform, in, out);
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | /** Find the length of the prefix of a blob this transform can use as input. In
|
|---|
| 77 | * other words, figure out how many bytes this transform will use up. This
|
|---|
| 78 | * method is optional and can return an error, but it must succeed for struct
|
|---|
| 79 | * subtransforms.
|
|---|
| 80 | * @memberof bithenge_transform_t
|
|---|
| 81 | * @param xform The transform.
|
|---|
| 82 | * @param blob The blob.
|
|---|
| 83 | * @param[out] out Where the prefix length will be stored.
|
|---|
| 84 | * @return EOK on success, ENOTSUP if not supported, or another error code from
|
|---|
| 85 | * errno.h. */
|
|---|
| 86 | static inline int bithenge_transform_prefix_length(bithenge_transform_t *xform,
|
|---|
| 87 | bithenge_blob_t *blob, aoff64_t *out)
|
|---|
| 88 | {
|
|---|
| 89 | assert(xform);
|
|---|
| 90 | assert(xform->ops);
|
|---|
| 91 | if (!xform->ops->prefix_length)
|
|---|
| 92 | return ENOTSUP;
|
|---|
| 93 | return xform->ops->prefix_length(xform, blob, out);
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | /** Increment a transform's reference count.
|
|---|
| 97 | * @param xform The transform to reference.
|
|---|
| 98 | * @return EOK on success or an error code from errno.h. */
|
|---|
| 99 | static inline int bithenge_transform_inc_ref(bithenge_transform_t *xform)
|
|---|
| 100 | {
|
|---|
| 101 | assert(xform);
|
|---|
| 102 | xform->refs++;
|
|---|
| 103 | return EOK;
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | /** Decrement a transform's reference count and free it if appropriate.
|
|---|
| 107 | * @param xform The transform to dereference, or NULL.
|
|---|
| 108 | * @return EOK on success or an error code from errno.h. */
|
|---|
| 109 | static inline int bithenge_transform_dec_ref(bithenge_transform_t *xform)
|
|---|
| 110 | {
|
|---|
| 111 | if (!xform)
|
|---|
| 112 | return EOK;
|
|---|
| 113 | assert(xform->ops);
|
|---|
| 114 | if (--xform->refs == 0)
|
|---|
| 115 | return xform->ops->destroy(xform);
|
|---|
| 116 | return EOK;
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 | /** A transform with a name. */
|
|---|
| 120 | typedef struct {
|
|---|
| 121 | const char *name;
|
|---|
| 122 | bithenge_transform_t *transform;
|
|---|
| 123 | } bithenge_named_transform_t;
|
|---|
| 124 |
|
|---|
| 125 | extern bithenge_transform_t bithenge_uint32le_transform;
|
|---|
| 126 | extern bithenge_transform_t bithenge_uint32be_transform;
|
|---|
| 127 | extern bithenge_named_transform_t *bithenge_primitive_transforms;
|
|---|
| 128 |
|
|---|
| 129 | int bithenge_new_transform(bithenge_transform_t *xform,
|
|---|
| 130 | const bithenge_transform_ops_t *ops);
|
|---|
| 131 |
|
|---|
| 132 | int bithenge_new_struct(bithenge_transform_t **out,
|
|---|
| 133 | bithenge_named_transform_t *subtransforms);
|
|---|
| 134 |
|
|---|
| 135 | #endif
|
|---|
| 136 |
|
|---|
| 137 | /** @}
|
|---|
| 138 | */
|
|---|