Fork us on GitHub Follow us on Facebook Follow us on Twitter

Changeset 0d1a8fd in mainline


Ignore:
Timestamp:
2012-06-24T18:44:34Z (8 years ago)
Author:
Sean Bartell <wingedtachikoma@…>
Branches:
master
Children:
f2da0bb
Parents:
03b2b2c
Message:

Bithenge: allow primitive transforms in scripts

Location:
uspace/app/bithenge
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/bithenge/linux/os.h

    r03b2b2c r0d1a8fd  
    8585}
    8686
     87static inline char *str_dup(const char *s)
     88{
     89        return strdup(s);
     90}
     91
    8792static inline char *str_ndup(const char *s, size_t max_len)
    8893{
     
    100105}
    101106
     107static inline uint32_t uint32_t_be2host(uint32_t val)
     108{
     109        return be32toh(val);
     110}
     111
    102112#endif
  • uspace/app/bithenge/script.c

    r03b2b2c r0d1a8fd  
    232232}
    233233
    234 /** Find a transform by name.
     234/** Find a transform by name. A reference will be added to the transform.
    235235 * @return The found transform, or NULL if none was found. */
    236236static bithenge_transform_t *get_named_transform(state_t *state,
    237237    const char *name)
    238238{
    239         for (transform_list_t *e = state->transform_list; e; e = e->next)
    240                 if (!str_cmp(e->name, name))
     239        for (transform_list_t *e = state->transform_list; e; e = e->next) {
     240                if (!str_cmp(e->name, name)) {
     241                        bithenge_transform_inc_ref(e->transform);
    241242                        return e->transform;
     243                }
     244        }
     245        for (int i = 0; bithenge_primitive_transforms[i].name; i++) {
     246                if (!str_cmp(bithenge_primitive_transforms[i].name, name)) {
     247                        bithenge_transform_t *xform =
     248                            bithenge_primitive_transforms[i].transform;
     249                        bithenge_transform_inc_ref(xform);
     250                        return xform;
     251                }
     252        }
    242253        return NULL;
    243254}
  • uspace/app/bithenge/transform.c

    r03b2b2c r0d1a8fd  
    3535 */
    3636
     37#include <assert.h>
    3738#include <errno.h>
     39#include <stdlib.h>
    3840#include "blob.h"
    3941#include "transform.h"
     
    4143static int transform_indestructible(bithenge_transform_t *xform)
    4244{
    43         return EINVAL;
     45        assert(false);
     46        return EOK;
    4447}
    4548
     
    6467}
    6568
    66 static int uint32le_prefix_length(bithenge_transform_t *xform,
    67     bithenge_blob_t *blob, aoff64_t *out)
     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)
    6891{
    6992        *out = 4;
     
    7396static const bithenge_transform_ops_t uint32le_ops = {
    7497        .apply = uint32le_apply,
    75         .prefix_length = uint32le_prefix_length,
     98        .prefix_length = prefix_length_4,
    7699        .destroy = transform_indestructible,
    77100};
    78101
    79 static bithenge_transform_t uint32le_transform = {
     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 = {
    80110        &uint32le_ops, 1
    81111};
    82112
    83 /** Create a little-endian 32-bit unsigned integer transform.
    84  * @param out Holds the transform.
    85  * @return EOK on success or an error code from errno.h. */
    86 int bithenge_uint32le_transform(bithenge_transform_t **out)
    87 {
    88         *out = &uint32le_transform;
    89         return EOK;
    90 }
     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;
    91126
    92127/** @}
  • uspace/app/bithenge/transform.h

    r03b2b2c r0d1a8fd  
    117117}
    118118
    119 int bithenge_uint32le_transform(bithenge_transform_t **out);
     119/** A transform with a name. */
     120typedef struct {
     121        const char *name;
     122        bithenge_transform_t *transform;
     123} bithenge_named_transform_t;
     124
     125extern bithenge_transform_t bithenge_uint32le_transform;
     126extern bithenge_transform_t bithenge_uint32be_transform;
     127extern bithenge_named_transform_t *bithenge_primitive_transforms;
    120128
    121129#endif
Note: See TracChangeset for help on using the changeset viewer.