source: mainline/uspace/app/bithenge/transform.c@ f2da0bb

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since f2da0bb was 0d1a8fd, checked in by Sean Bartell <wingedtachikoma@…>, 13 years ago

Bithenge: allow primitive transforms in scripts

  • Property mode set to 100644
File size: 3.7 KB
Line 
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 <assert.h>
38#include <errno.h>
39#include <stdlib.h>
40#include "blob.h"
41#include "transform.h"
42
43static int transform_indestructible(bithenge_transform_t *xform)
44{
45 assert(false);
46 return EOK;
47}
48
49static int uint32le_apply(bithenge_transform_t *xform, bithenge_node_t *in,
50 bithenge_node_t **out)
51{
52 int rc;
53 if (bithenge_node_type(in) != BITHENGE_NODE_BLOB)
54 return EINVAL;
55 bithenge_blob_t *blob = bithenge_node_as_blob(in);
56
57 // Try to read 5 bytes and fail if the blob is too long.
58 uint32_t val[2];
59 aoff64_t size = sizeof(val[0]) + 1;
60 rc = bithenge_blob_read(blob, 0, (char *)val, &size);
61 if (rc != EOK)
62 return rc;
63 if (size != 4)
64 return EINVAL;
65
66 return bithenge_new_integer_node(out, uint32_t_le2host(val[0]));
67}
68
69static int uint32be_apply(bithenge_transform_t *xform, bithenge_node_t *in,
70 bithenge_node_t **out)
71{
72 int rc;
73 if (bithenge_node_type(in) != BITHENGE_NODE_BLOB)
74 return EINVAL;
75 bithenge_blob_t *blob = bithenge_node_as_blob(in);
76
77 // Try to read 5 bytes and fail if the blob is too long.
78 uint32_t val[2];
79 aoff64_t size = sizeof(val[0]) + 1;
80 rc = bithenge_blob_read(blob, 0, (char *)val, &size);
81 if (rc != EOK)
82 return rc;
83 if (size != 4)
84 return EINVAL;
85
86 return bithenge_new_integer_node(out, uint32_t_be2host(val[0]));
87}
88
89static int prefix_length_4(bithenge_transform_t *xform, bithenge_blob_t *blob,
90 aoff64_t *out)
91{
92 *out = 4;
93 return EOK;
94}
95
96static const bithenge_transform_ops_t uint32le_ops = {
97 .apply = uint32le_apply,
98 .prefix_length = prefix_length_4,
99 .destroy = transform_indestructible,
100};
101
102static const bithenge_transform_ops_t uint32be_ops = {
103 .apply = uint32be_apply,
104 .prefix_length = prefix_length_4,
105 .destroy = transform_indestructible,
106};
107
108/** The little-endian 32-bit unsigned integer transform. */
109bithenge_transform_t bithenge_uint32le_transform = {
110 &uint32le_ops, 1
111};
112
113/** The big-endian 32-bit unsigned integer transform. */
114bithenge_transform_t bithenge_uint32be_transform = {
115 &uint32be_ops, 1
116};
117
118static bithenge_named_transform_t primitive_transforms[] = {
119 {"uint32le", &bithenge_uint32le_transform},
120 {"uint32be", &bithenge_uint32be_transform},
121 {NULL, NULL}
122};
123
124/** An array of named built-in transforms. */
125bithenge_named_transform_t *bithenge_primitive_transforms = primitive_transforms;
126
127/** @}
128 */
Note: See TracBrowser for help on using the repository browser.