Example #1
0
/// @test Test ironbee library - transformation registration
TEST(TestIronBee, test_tfn)
{
    ib_engine_t *ib;
    ib_status_t rc;
    ib_tfn_t *tfn = (ib_tfn_t *)-1;
    ib_flags_t flags;
    uint8_t data_in[128];
    ib_field_t *fin;
    const ib_field_t *fout;
    ib_bytestr_t *bs;

    ibtest_engine_create(&ib);

    ASSERT_EQ(IB_OK, ib_tfn_register(ib, "foo2bar", foo2bar,
                                     IB_TFN_FLAG_NONE, NULL));
    ASSERT_EQ(IB_OK, ib_tfn_lookup(ib, "foo2bar", &tfn));
    ASSERT_NE((ib_tfn_t *)-1, tfn);
    ASSERT_TRUE(tfn);

    ib_bytestr_dup_nulstr(&bs, ib->mp, "foo");
    fin = NULL;
    ib_field_create(
        &fin, ib->mp, IB_FIELD_NAME("ByteStr"),
        IB_FTYPE_BYTESTR, ib_ftype_bytestr_in(bs)
    );
    fout = NULL;
    flags = 0;
    rc = ib_tfn_transform(ib, ib->mp, tfn, fin, &fout, &flags);
    ASSERT_EQ(rc, IB_OK);
    ASSERT_NE((ib_tfn_t *)-1, tfn);
    ASSERT_TRUE(IB_TFN_CHECK_FMODIFIED(flags));
    ASSERT_NE(fin, fout);

    strcpy((char *)data_in, "foo");
    fin = NULL;
    ib_field_create(
        &fin, ib->mp, IB_FIELD_NAME("NulStr"),
        IB_FTYPE_NULSTR,
        ib_ftype_nulstr_in((char *)data_in)
    );
    fout = NULL;
    flags = 0;
    rc = ib_tfn_transform(ib, ib->mp, tfn, fin, &fout, &flags);
    ASSERT_EQ(rc, IB_OK);
    ASSERT_NE((ib_tfn_t *)-1, tfn);
    ASSERT_TRUE(IB_TFN_CHECK_FMODIFIED(flags));
    ASSERT_NE(fin, fout);

    ibtest_engine_destroy(ib);
}
Example #2
0
File: tfn.c Project: niq/ironbee
ib_status_t ib_tfn_transform_field(ib_tfn_t *tfn,
                                   ib_field_t *f,
                                   ib_flags_t *pflags)
{
    IB_FTRACE_INIT(ib_tfn_transform);
    ib_bytestr_t *bs;
    char *str;
    uint8_t *data_out;
    size_t dlen_out;
    ib_status_t rc;

    switch(f->type) {
        case IB_FTYPE_BYTESTR:
            bs = ib_field_value_bytestr(f);

            rc = tfn->transform(tfn->fndata,
                                f->mp,
                                ib_bytestr_ptr(bs),
                                ib_bytestr_length(bs),
                                &data_out,
                                &dlen_out,
                                pflags);

            /* If it is modified and not done inplace, then the
             * field value needs to be updated.
             */
            if (   IB_TFN_CHECK_FMODIFIED(*pflags)
                && !IB_TFN_CHECK_FINPLACE(*pflags))
            {
                ib_bytestr_t *bs_new;

                rc = ib_bytestr_alias_mem(&bs_new, f->mp, data_out, dlen_out);
                if (rc != IB_OK) {
                    IB_FTRACE_RET_STATUS(rc);
                }

                rc = ib_field_setv(f, bs_new);
            }

            IB_FTRACE_RET_STATUS(rc);

        case IB_FTYPE_NULSTR:
            str = ib_field_value_nulstr(f),

            rc = tfn->transform(tfn->fndata,
                                f->mp,
                                (uint8_t *)str,
                                strlen(str),
                                &data_out,
                                &dlen_out,
                                pflags);

            /* If it is modified and not done inplace, then the
             * field value needs to be updated.
             *
             * NOTE: Anytime a transformation modifies data it
             *       MUST NUL terminate the data and it is a bug
             *       if this is not done.
             */
            if (   IB_TFN_CHECK_FMODIFIED(*pflags)
                && !IB_TFN_CHECK_FINPLACE(*pflags))
            {
                rc = ib_field_setv(f, data_out);
            }

            IB_FTRACE_RET_STATUS(rc);
    }


    IB_FTRACE_RET_STATUS(IB_EINVAL);
}