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 | #include <byteorder.h>
|
---|
38 | #include <errno.h>
|
---|
39 | #include "blob.h"
|
---|
40 | #include "transform.h"
|
---|
41 |
|
---|
42 | static int transform_indestructible(bithenge_transform_t *xform)
|
---|
43 | {
|
---|
44 | return EINVAL;
|
---|
45 | }
|
---|
46 |
|
---|
47 | static int uint32le_apply(bithenge_transform_t *xform, bithenge_node_t *in,
|
---|
48 | bithenge_node_t **out)
|
---|
49 | {
|
---|
50 | int rc;
|
---|
51 | if (bithenge_node_type(in) != BITHENGE_NODE_BLOB)
|
---|
52 | return EINVAL;
|
---|
53 | bithenge_blob_t *blob = bithenge_node_as_blob(in);
|
---|
54 |
|
---|
55 | // Try to read 5 bytes and fail if the blob is too long.
|
---|
56 | uint32_t val[2];
|
---|
57 | aoff64_t size = sizeof(val[0]) + 1;
|
---|
58 | rc = bithenge_blob_read(blob, 0, (char *)val, &size);
|
---|
59 | if (rc != EOK)
|
---|
60 | return rc;
|
---|
61 | if (size != 4)
|
---|
62 | return EINVAL;
|
---|
63 |
|
---|
64 | return bithenge_new_integer_node(out, uint32_t_le2host(val[0]));
|
---|
65 | }
|
---|
66 |
|
---|
67 | static int uint32le_prefix_length(bithenge_transform_t *xform,
|
---|
68 | bithenge_blob_t *blob, aoff64_t *out)
|
---|
69 | {
|
---|
70 | *out = 4;
|
---|
71 | return EOK;
|
---|
72 | }
|
---|
73 |
|
---|
74 | static const bithenge_transform_ops_t uint32le_ops = {
|
---|
75 | .apply = uint32le_apply,
|
---|
76 | .prefix_length = uint32le_prefix_length,
|
---|
77 | .destroy = transform_indestructible,
|
---|
78 | };
|
---|
79 |
|
---|
80 | static bithenge_transform_t uint32le_transform = {
|
---|
81 | &uint32le_ops, 1
|
---|
82 | };
|
---|
83 |
|
---|
84 | /** Create a little-endian 32-bit unsigned integer transform.
|
---|
85 | * @param out Holds the transform.
|
---|
86 | * @return EOK on success or an error code from errno.h. */
|
---|
87 | int bithenge_uint32le_transform(bithenge_transform_t **out)
|
---|
88 | {
|
---|
89 | *out = &uint32le_transform;
|
---|
90 | return EOK;
|
---|
91 | }
|
---|
92 |
|
---|
93 | /** @}
|
---|
94 | */
|
---|