Changeset 0d1a8fd in mainline for uspace/app/bithenge/transform.c


Ignore:
Timestamp:
2012-06-24T18:44:34Z (12 years ago)
Author:
Sean Bartell <wingedtachikoma@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
f2da0bb
Parents:
03b2b2c
Message:

Bithenge: allow primitive transforms in scripts

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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/** @}
Note: See TracChangeset for help on using the changeset viewer.