コード例 #1
0
ファイル: test_util_field.cpp プロジェクト: B0SB05/ironbee
/// @test Test util field library - ib_field_create() ib_field_create()
TEST_F(TestIBUtilField, test_field_create)
{
    ib_field_t *f;
    ib_status_t rc;
    const char *nulstrval = "TestValue";
    ib_num_t numval = 5;
    ib_bytestr_t *bytestrval;
    const char *nulout;
    const char *nulcopy;

    nulcopy = ib_mm_strdup(MM(), nulstrval);
    ASSERT_STRNE(NULL, nulcopy);
    rc = ib_field_create(&f, MM(), IB_S2SL("test_nulstr"),
                         IB_FTYPE_NULSTR, ib_ftype_nulstr_in(nulcopy));
    ASSERT_EQ(IB_OK, rc);
    ASSERT_TRUE(f);
    ASSERT_EQ(11UL, f->nlen);
    ASSERT_EQ(0, memcmp("test_nulstr", f->name, 11));

    rc = ib_field_value(f, ib_ftype_nulstr_out(&nulout));
    ASSERT_EQ(IB_OK, rc);
    ASSERT_STREQ(nulstrval, nulout);

    rc = ib_field_create(&f, MM(), IB_S2SL("test_num"),
                         IB_FTYPE_NUM, ib_ftype_num_in(&numval));
    ASSERT_EQ(IB_OK, rc);
    ASSERT_TRUE(f);
    ASSERT_EQ(8UL, f->nlen);
    ASSERT_EQ(0, memcmp("test_num", f->name, 8));

    rc = ib_bytestr_dup_mem(&bytestrval, MM(),
                            (uint8_t *)nulstrval, strlen(nulstrval));
    ASSERT_EQ(IB_OK, rc);
    ASSERT_TRUE(f);

    rc = ib_field_create(&f, MM(), IB_S2SL("test_bytestr"),
                         IB_FTYPE_BYTESTR, ib_ftype_bytestr_in(bytestrval));
    ASSERT_EQ(IB_OK, rc);
    ASSERT_TRUE(f);
    ASSERT_EQ(12UL, f->nlen);
    ASSERT_EQ(0, memcmp("test_bytestr", f->name, 12));

    rc = ib_field_create(&f, MM(), IB_S2SL("test_nulstr_ex"),
                         IB_FTYPE_NULSTR, ib_ftype_nulstr_in(nulstrval));
    ASSERT_EQ(IB_OK, rc);
    ASSERT_TRUE(f);

    rc = ib_field_create(&f, MM(), IB_S2SL("test_num_ex"),
                         IB_FTYPE_NUM, ib_ftype_num_in(&numval));
    ASSERT_EQ(IB_OK, rc);
    ASSERT_TRUE(f);

    rc = ib_field_create(&f, MM(), IB_S2SL("test_bytestr_ex"),
                         IB_FTYPE_BYTESTR, ib_ftype_bytestr_in(bytestrval));
    ASSERT_EQ(IB_OK, rc);
    ASSERT_TRUE(f);
}
コード例 #2
0
ファイル: test_util_field.cpp プロジェクト: B0SB05/ironbee
TEST_F(TestIBUtilField, Alias)
{
    ib_num_t   num1;
    ib_num_t   num2;
    ib_float_t flt1;
    ib_float_t flt2;
    char *s = NULL;
    const char *v;
    ib_field_t *f;
    ib_status_t rc;

    rc = ib_field_create_alias(&f, MM(), "foo", 3,
                               IB_FTYPE_NULSTR,
                               ib_ftype_nulstr_mutable_out(&s));
    ASSERT_EQ(IB_OK, rc);
    v = "hello";
    rc = ib_field_setv(f, ib_ftype_nulstr_in(v));
    ASSERT_EQ(IB_OK, rc);
    ASSERT_STREQ(v, s);

    /*
     * Alias a numeric field
     */
    num1 = 1;
    rc = ib_field_create_alias(&f, MM(), "num", 3,
                               IB_FTYPE_NUM,
                               ib_ftype_num_in(&num1));
    ASSERT_EQ(IB_OK, rc);

    rc = ib_field_value(f, ib_ftype_num_out(&num2));
    ASSERT_EQ(IB_OK, rc);
    ASSERT_EQ(num1, num2);

    num1 = 3;
    rc = ib_field_value(f, ib_ftype_num_out(&num2));
    ASSERT_EQ(IB_OK, rc);
    ASSERT_EQ(num1, num2);


    /*
     * Alias a floating point field
     */
    flt1 = 1.1;
    rc = ib_field_create_alias(&f, MM(), "flt", 3,
                               IB_FTYPE_FLOAT,
                               ib_ftype_float_in(&flt1));
    ASSERT_EQ(IB_OK, rc);

    rc = ib_field_value(f, ib_ftype_float_out(&flt2));
    ASSERT_EQ(IB_OK, rc);
    ASSERT_EQ(flt1, flt2);

    flt1 = 1.5;
    rc = ib_field_value(f, ib_ftype_float_out(&flt2));
    ASSERT_EQ(IB_OK, rc);
    ASSERT_EQ(flt1, flt2);
}
コード例 #3
0
ファイル: test_operator.cpp プロジェクト: aburan28/ironbee
TEST_F(CoreOperatorsTest, ContainsTest)
{
    ib_status_t status;
    ib_num_t call_result;
    ib_operator_inst_t *op;
    ib_rule_t *rule = NULL; /* Not used by this operator. */

    status = ib_operator_inst_create(ib_engine,
                                     NULL,
                                     rule,
                                     IB_OP_FLAG_PHASE,
                                     "contains",
                                     "needle",
                                     IB_OPINST_FLAG_NONE,
                                     &op);
    ASSERT_EQ(IB_OK, status);

    // call contains
    ib_field_t *field;
    const char *matching = "data with needle in it";
    const char *nonmatching = "non matching string";
    ib_field_create(
        &field,
        ib_engine_pool_main_get(ib_engine),
        IB_FIELD_NAME("testfield"),
        IB_FTYPE_NULSTR,
        NULL
    );

    ib_rule_exec_t rule_exec;
    memset(&rule_exec, 0, sizeof(rule_exec));
    rule_exec.ib = ib_engine;
    rule_exec.rule = rule;

    ib_field_setv(field, ib_ftype_nulstr_in(matching));
    status = ib_operator_execute(&rule_exec, op, field, &call_result);
    ASSERT_EQ(IB_OK, status);
    EXPECT_EQ(1, call_result);

    ib_field_setv(field, ib_ftype_nulstr_in(nonmatching));
    status = ib_operator_execute(&rule_exec, op, field, &call_result);
    ASSERT_EQ(IB_OK, status);
    EXPECT_EQ(0, call_result);
}
コード例 #4
0
ファイル: action.c プロジェクト: niubl/ironbee
ib_status_t ib_action_inst_create_ex(
    ib_engine_t *ib,
    const char *name,
    const char *parameters,
    ib_action_inst_t **act_inst)
{
    assert(ib != NULL);
    assert(name != NULL);

    ib_hash_t *action_hash = ib->actions;
    ib_action_t *action;
    ib_status_t rc;
    ib_mpool_t *mpool = ib_engine_pool_main_get(ib);

    assert(mpool != NULL);

    rc = ib_hash_get(action_hash, &action, name);
    if (rc != IB_OK) {
        /* name is not registered */
        return rc;
    }

    *act_inst = (ib_action_inst_t *)ib_mpool_alloc(mpool,
                                                   sizeof(ib_action_inst_t));
    if (*act_inst == NULL) {
        return IB_EALLOC;
    }
    (*act_inst)->action = action;
    (*act_inst)->params = ib_mpool_strdup(mpool, parameters);
    (*act_inst)->fparam = NULL;

    if (action->fn_create != NULL) {
        rc = action->fn_create(
            ib,
            parameters,
            *act_inst,
            action->cbdata_create
        );
        if (rc != IB_OK) {
            return rc;
        }
    }
    else {
        rc = IB_OK;
    }

    if ((*act_inst)->fparam == NULL) {
        rc = ib_field_create(&((*act_inst)->fparam),
                             mpool,
                             IB_FIELD_NAME("param"),
                             IB_FTYPE_NULSTR,
                             ib_ftype_nulstr_in(parameters));
    }

    return rc;
}
コード例 #5
0
TEST_F(CoreOperatorsTest, ContainsTest)
{
    ib_status_t status;
    ib_num_t call_result;
    const ib_operator_t *op;
    void *instance_data;

    status = ib_operator_lookup(ib_engine, "contains", &op);

    ASSERT_EQ(IB_OK, status);


    status = ib_operator_inst_create(op,
                                     ib_context_main(ib_engine),
                                     IB_OP_CAPABILITY_NON_STREAM,
                                     "needle",
                                     &instance_data);
    ASSERT_EQ(IB_OK, status);

    // call contains
    ib_field_t *field;
    const char *matching = "data with needle in it";
    const char *nonmatching = "non matching string";
    ib_field_create(
        &field,
        ib_engine_pool_main_get(ib_engine),
        IB_FIELD_NAME("testfield"),
        IB_FTYPE_NULSTR,
        NULL
    );

    ib_field_setv(field, ib_ftype_nulstr_in(matching));
    status = ib_operator_inst_execute(op, instance_data, ib_tx, field, NULL, &call_result);
    ASSERT_EQ(IB_OK, status);
    EXPECT_EQ(1, call_result);

    ib_field_setv(field, ib_ftype_nulstr_in(nonmatching));
    status = ib_operator_inst_execute(op, instance_data, ib_tx, field, NULL, &call_result);
    ASSERT_EQ(IB_OK, status);
    EXPECT_EQ(0, call_result);
}
コード例 #6
0
ファイル: field.cpp プロジェクト: strategist922/ironbee
void Field::set_null_string(
    const char* value,
    const char* arg, size_t arg_length
) const
{
    Internal::check_type(NULL_STRING, type());
    Internal::set_value(
        ib(),
        ib_ftype_nulstr_in(value),
        arg, arg_length
    );
}
コード例 #7
0
TEST_F(TestIronBeeModuleRulesLua, operator_test)
{
    const ib_operator_t *op;
    void *instance_data;
    ib_num_t result;

    ib_field_t* field1;

    const char* op_name = "test_module_rules_lua.lua";
    const char* rule_name = "luarule001";

    char* str1 = (char *)ib_mpool_strdup(ib_engine->mp, "string 1");
    ASSERT_TRUE(str1);

    // Create field 1.
    ASSERT_EQ(IB_OK,
        ib_field_create(
            &field1,
            ib_engine->mp,
            IB_FIELD_NAME("field1"),
            IB_FTYPE_NULSTR,
            ib_ftype_nulstr_in(str1)
        )
    );

    /* Configure the operator. */
    configureIronBee();

    // Ensure that the operator exists.
    ASSERT_EQ(IB_OK,
        ib_operator_lookup(ib_engine, op_name, &op)
    );

    ASSERT_EQ(IB_OK, ib_operator_inst_create(op,
                                             ib_context_main(ib_engine),
                                             IB_OP_CAPABILITY_NON_STREAM,
                                             rule_name,
                                             &instance_data));
    performTx();

    // Attempt to match.
    ASSERT_EQ(IB_OK, ib_operator_inst_execute(op,
                                          instance_data,
                                          ib_tx,
                                          field1,
                                          NULL,
                                          &result));

    // This time we should succeed.
    ASSERT_TRUE(result);
}
コード例 #8
0
ファイル: test_engine.cpp プロジェクト: strategist922/ironbee
/// @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);
}
コード例 #9
0
ファイル: field.cpp プロジェクト: strategist922/ironbee
Field Field::create_null_string(
    MemoryPool  pool,
    const char* name,
    size_t      name_length,
    const char* value
)
{
    return Internal::create_field(
        pool,
        name, name_length,
        Field::NULL_STRING,
        ib_ftype_nulstr_in(value)
    );
}
コード例 #10
0
ファイル: test_util_field.cpp プロジェクト: aburan28/ironbee
TEST_F(TestIBUtilField, Alias)
{
    char *s = NULL;
    const char *v;
    ib_field_t *f;
    ib_status_t rc;

    rc = ib_field_create_alias(&f, MemPool(), "foo", 3, IB_FTYPE_NULSTR,
        ib_ftype_nulstr_mutable_out(&s));
    ASSERT_EQ(IB_OK, rc);
    v = "hello";
    rc = ib_field_setv(f, ib_ftype_nulstr_in(v));
    ASSERT_EQ(IB_OK, rc);
    ASSERT_EQ(std::string(v), std::string(s));
}
コード例 #11
0
ファイル: user_agent.c プロジェクト: dune73/ironbee
/**
 * Store a field in the agent list
 *
 * Creates a new field and adds it to the agent list field list.
 *
 * @param[in] ib IronBee object
 * @param[in,out] mp Memory pool to allocate from
 * @param[in] agent_list Field to add the field to
 * @param[in] name Field name
 * @param[in] value Field value
 *
 * @returns Status code
 */
static ib_status_t modua_store_field(ib_engine_t *ib,
                                     ib_mpool_t *mp,
                                     ib_field_t *agent_list,
                                     const char *name,
                                     const char *value)
{
    IB_FTRACE_INIT();
    ib_field_t *tmp_field = NULL;
    ib_status_t rc = IB_OK;

    /* No value?  Do nothing */
    if (value == NULL) {
        ib_log_debug3(ib, "No %s field in user agent", name);
        IB_FTRACE_RET_STATUS(IB_OK);
    }

    /* Create the field */
    rc = ib_field_create(
        &tmp_field,
        mp,
        IB_FIELD_NAME(name),
        IB_FTYPE_NULSTR,
        ib_ftype_nulstr_in(value)
    );
    if (rc != IB_OK) {
        ib_log_alert(ib,
                     "Error creating user agent %s field: %s", name, ib_status_to_string(rc));
        IB_FTRACE_RET_STATUS(rc);
    }

    /* Add the field to the list */
    rc = ib_field_list_add(agent_list, tmp_field);
    if (rc != IB_OK) {
        ib_log_alert(ib,
                     "Error adding user agent %s field: %s", name, ib_status_to_string(rc));
        IB_FTRACE_RET_STATUS(rc);
    }

    ib_log_debug3(ib, "Stored user agent %s '%s'", name, value);

    IB_FTRACE_RET_STATUS(IB_OK);
}
コード例 #12
0
ファイル: test_util_field.cpp プロジェクト: B0SB05/ironbee
// Cached version of the above dyn_get function.
static ib_status_t dyn_get_cached(
    const ib_field_t *f,
    void *out_value,
    const void *arg,
    size_t alen,
    void *data
)
{
    /* Call the get function */
    const char* cval;
    dyn_get(f, &cval, arg, alen, data);

    /* Cache the value */
    /* Caching does not semantically change value, so we can safely ignore
     * the constness of f. */
    ib_field_make_static((ib_field_t *)f);
    ib_field_setv((ib_field_t *)f, ib_ftype_nulstr_in(cval));

    *reinterpret_cast<const char**>(out_value) = cval;

    return IB_OK;
}
コード例 #13
0
ファイル: geoip.c プロジェクト: poona/ironbee
/**
 * Lookup the IP address in the GeoIP database
 *
 * @param[in] ib IronBee engine
 * @param[in] tx Transaction
 * @param[in] event Event
 * @param[in] data callback data (Module configuration)
 */
static ib_status_t geoip_lookup(
    ib_engine_t *ib,
    ib_tx_t *tx,
    ib_state_event_type_t event,
    void *data
)
{
    assert(ib != NULL);
    assert(event == handle_context_tx_event);
    assert(data != NULL);

    const char *ip = tx->er_ipstr;
    const module_data_t *mod_data = (const module_data_t *)data;

    if (ip == NULL) {
        ib_log_alert_tx(tx, "Trying to lookup NULL IP in GEOIP");
        return IB_EINVAL;
    }

#ifdef GEOIP_HAVE_VERSION
    /**
     * Some configurations exist as single characters and must be converted to
     * a string. This is simply a place to assemble that string before
     * it is passed into ip_data_add_nulstr.
     * This is only needed if we have support confidence items. WAM
     */
    char one_char_str[2] = { '\0', '\0' };
#endif /* GEOIP_HAVE_VERSION */

    ib_status_t rc;

    /* Declare and initialize the GeoIP property list.
     * Regardless of if we find a record or not, we want to create the list
     * artifact so that later modules know we ran and did [not] find a
     * record. */
    ib_field_t *geoip_lst = NULL;

    ib_field_t *tmp_field = NULL;

    /* Id of geo ip record to read. */
    int geoip_id;

    ib_log_debug_tx(tx, "GeoIP Lookup '%s'", ip);

    /* Build a new list. */
    rc = ib_var_source_initialize(
        mod_data->geoip_source,
        &geoip_lst,
        tx->var_store,
        IB_FTYPE_LIST
    );

    /* NOTICE: Called before GeoIP_record_by_addr allocates a
     * GeoIPRecord. */
    if (rc != IB_OK) {
        ib_log_alert_tx(tx, "Unable to add GEOIP var.");
        return IB_EINVAL;
    }

    if (mod_data->geoip_db == NULL) {
        ib_log_alert_tx(tx,
                        "GeoIP database was never opened. Perhaps the "
                        "configuration file needs a GeoIPDatabaseFile "
                        "\"/usr/share/geoip/GeoLite.dat\" line?");
        return IB_EINVAL;
    }

    geoip_id = GeoIP_id_by_addr(mod_data->geoip_db, ip);

    if (geoip_id > 0) {
        const char *tmp_str;

        ib_log_debug_tx(tx, "GeoIP record found.");

        /* Add integers. */
        tmp_field = NULL;

        tmp_str = GeoIP_code_by_id(geoip_id);
        if (tmp_str)
        {
            ib_field_create(&tmp_field,
                            tx->mp,
                            IB_FIELD_NAME("country_code"),
                            IB_FTYPE_NULSTR,
                            ib_ftype_nulstr_in(tmp_str));
            ib_field_list_add(geoip_lst, tmp_field);
        }

        tmp_str = GeoIP_code3_by_id(geoip_id);
        if (tmp_str)
        {
            ib_field_create(&tmp_field,
                            tx->mp,
                            IB_FIELD_NAME("country_code3"),
                            IB_FTYPE_NULSTR,
                            ib_ftype_nulstr_in(tmp_str));
            ib_field_list_add(geoip_lst, tmp_field);
        }

        tmp_str = GeoIP_country_name_by_id(mod_data->geoip_db, geoip_id);
        if (tmp_str)
        {
            ib_field_create(&tmp_field,
                            tx->mp,
                            IB_FIELD_NAME("country_name"),
                            IB_FTYPE_NULSTR,
                            ib_ftype_nulstr_in(tmp_str));
            ib_field_list_add(geoip_lst, tmp_field);
        }

        tmp_str = GeoIP_continent_by_id(geoip_id);
        if (tmp_str)
        {
            ib_field_create(&tmp_field,
                            tx->mp,
                            IB_FIELD_NAME("continent_code"),
                            IB_FTYPE_NULSTR,
                            ib_ftype_nulstr_in(tmp_str));
            ib_field_list_add(geoip_lst, tmp_field);
        }
    }
    else
    {
        ib_log_debug_tx(tx, "No GeoIP record found.");
        ib_field_create(&tmp_field,
                        tx->mp,
                        IB_FIELD_NAME("country_code"),
                        IB_FTYPE_NULSTR,
                        ib_ftype_nulstr_in("O1"));
        ib_field_list_add(geoip_lst, tmp_field);
        ib_field_create(&tmp_field,
                        tx->mp,
                        IB_FIELD_NAME("country_code3"),
                        IB_FTYPE_NULSTR,
                        ib_ftype_nulstr_in("O01"));
        ib_field_list_add(geoip_lst, tmp_field);
        ib_field_create(&tmp_field,
                        tx->mp,
                        IB_FIELD_NAME("country_name"),
                        IB_FTYPE_NULSTR,
                        ib_ftype_nulstr_in("Other Country"));
        ib_field_list_add(geoip_lst, tmp_field);
        ib_field_create(&tmp_field,
                        tx->mp,
                        IB_FIELD_NAME("continent_code"),
                        IB_FTYPE_NULSTR,
                        ib_ftype_nulstr_in("O1"));
        ib_field_list_add(geoip_lst, tmp_field);
    }

    return IB_OK;
}
コード例 #14
0
ファイル: field.c プロジェクト: aburan28/ironbee
/**
 * Attempt to convert a single field.
 *
 * If the desired type matches the in_field type, out_field is set to NULL
 * and IB_OK is returned.
 *
 * @param[in,out] mp Memory pool to use.
 * @param[in] desired_type The type to try to convert this to.
 * @param[in] in_field The input field.
 * @param[out] out_field The output field to write to.
 *
 * @returns
 *   - IB_OK On success.
 *   - IB_EINVAL If a string cannot be converted to a number type
 *               or some other invalid type conversion is requested.
 *   - IB_EALLOC Memory allocation error.
 */
ib_status_t ib_field_convert(
    ib_mpool_t *mp,
    const ib_ftype_t desired_type,
    const ib_field_t *in_field,
    ib_field_t **out_field)
{
    assert(mp);
    assert(in_field);

    ib_status_t rc;

    /* Holder values for in_field values before being set in out_field. */
    size_t sz;
    const char *str;
    const ib_bytestr_t *bstr;
    ib_num_t num;
    ib_float_t flt;
    void *new_field_value;


    if (in_field->type == desired_type) {
        *out_field = NULL;
        return IB_OK;
    }

    switch (in_field->type) {
        case IB_FTYPE_NULSTR:

            /* Extract string. */
            rc = ib_field_value(in_field, ib_ftype_nulstr_out(&str));
            if (rc!=IB_OK){
                return rc;
            }

            switch(desired_type) {
                case IB_FTYPE_BYTESTR:
                    rc = ib_bytestr_dup_nulstr((ib_bytestr_t **)&bstr, mp, str);
                    if (rc!=IB_OK){
                        return rc;
                    }
                    new_field_value = ib_ftype_bytestr_in(bstr);
                    break;
                case IB_FTYPE_NUM:
                    rc = ib_string_to_num(str, 0, &num);
                    new_field_value = ib_ftype_num_in(&num);
                    break;
                case IB_FTYPE_FLOAT:
                    rc = ib_string_to_float(str, &flt);
                    new_field_value = ib_ftype_float_in(&flt);
                    break;
                default:
                    return IB_EINVAL;
            }
            break;
        case IB_FTYPE_BYTESTR:

            /* Extract bytestr. */
            rc = ib_field_value(in_field, ib_ftype_bytestr_out(&bstr));
            if (rc!=IB_OK){
                return rc;
            }
            sz = ib_bytestr_length(bstr);

            /* Convert byte str. */
            switch(desired_type) {
                case IB_FTYPE_NULSTR:
                    str = ib_mpool_memdup_to_str(mp, bstr, sz);
                    if (!str) {
                        return rc;
                    }
                    new_field_value = ib_ftype_nulstr_in(str);
                    break;
                case IB_FTYPE_NUM:
                    rc = ib_string_to_num_ex((char *)bstr, sz, 0, &num);
                    new_field_value = ib_ftype_num_in(&num);
                    break;
                case IB_FTYPE_FLOAT:
                    rc = ib_string_to_float_ex((char *)bstr, sz, &flt);
                    new_field_value = ib_ftype_float_in(&flt);
                    break;
                default:
                    return IB_EINVAL;
            }
            break;
        case IB_FTYPE_NUM:

            /* Extract unum. */
            rc = ib_field_value(in_field, ib_ftype_num_out(&num));
            if (rc!=IB_OK){
                return rc;
            }

            switch(desired_type) {
                case IB_FTYPE_NULSTR:
                    str = ib_num_to_string(mp, num);
                    if (!str) {
                        return IB_EINVAL;
                    }
                    new_field_value = ib_ftype_nulstr_in(str);
                    break;
                case IB_FTYPE_BYTESTR:
                    str = ib_num_to_string(mp, num);
                    if (!str) {
                        return IB_EINVAL;
                    }
                    rc = ib_bytestr_dup_nulstr((ib_bytestr_t **)&bstr, mp, str);
                    if (rc!=IB_OK){
                        return rc;
                    }
                    new_field_value = ib_ftype_bytestr_in(bstr);
                    break;
                case IB_FTYPE_FLOAT:
                    flt = (ib_float_t)num;
                    new_field_value = ib_ftype_float_in(&flt);
                    break;
                default:
                    return IB_EINVAL;
            }
            break;
        case IB_FTYPE_FLOAT:

            /* Extract unum. */
            rc = ib_field_value(in_field, ib_ftype_float_out(&flt));
            if (rc!=IB_OK){
                return rc;
            }

            switch(desired_type) {
                case IB_FTYPE_NULSTR:
                    str = ib_float_to_string(mp, flt);
                    if (!str) {
                        return IB_EINVAL;
                    }
                    new_field_value = ib_ftype_nulstr_in(str);
                    break;
                case IB_FTYPE_BYTESTR:
                    str = ib_float_to_string(mp, flt);
                    if (!str) {
                        return IB_EINVAL;
                    }
                    rc = ib_bytestr_dup_nulstr((ib_bytestr_t **)&bstr, mp, str);
                    if (rc!=IB_OK){
                        return rc;
                    }
                    new_field_value = ib_ftype_bytestr_in(bstr);
                    break;
                case IB_FTYPE_NUM:
                    num = (ib_float_t)flt;
                    new_field_value = ib_ftype_num_in(&num);
                    break;
                default:
                    return IB_EINVAL;
            }
            break;
        default:
            return IB_EINVAL;
    }

    rc = ib_field_create(
        out_field,
        mp,
        in_field->name,
        in_field->nlen,
        desired_type,
        new_field_value);
    return rc;
}
コード例 #15
0
ファイル: field.c プロジェクト: niubl/ironbee
ib_status_t ib_field_convert(
    ib_mpool_t        *mp,
    const ib_ftype_t   desired_type,
    const ib_field_t  *in_field,
    ib_field_t       **out_field
)
{
    assert(mp);
    assert(in_field);

    ib_status_t rc;

    /* Holder values for in_field values before being set in out_field. */
    size_t sz;
    const char *str;
    const ib_bytestr_t *bstr;
    ib_num_t num;
    ib_time_t tme;
    ib_float_t flt;
    void *new_field_value;

    if (in_field->type == desired_type) {
        *out_field = NULL;
        return IB_OK;
    }

    switch (in_field->type) {
    case IB_FTYPE_NULSTR:

        /* Extract string.  Note that a zero-length nulstr field can
         * have a NULL value in the union. */
        if ( (in_field->val->u.nulstr == NULL) &&
             (in_field->val->pval == NULL) )
        {
            str = "";
        }
        else {
            rc = ib_field_value(in_field, ib_ftype_nulstr_out(&str));
            if (rc != IB_OK){
                return rc;
            }
        }

        switch (desired_type) {
        case IB_FTYPE_BYTESTR:
            rc = ib_bytestr_dup_nulstr((ib_bytestr_t **)&bstr, mp, str);
            if (rc != IB_OK) {
                return rc;
            }
            new_field_value = ib_ftype_bytestr_in(bstr);
            break;
        case IB_FTYPE_TIME:
            rc = ib_string_to_time(str, &tme);
            if (rc != IB_OK) {
                return rc;
            }
            new_field_value = ib_ftype_time_in(&tme);
            break;
        case IB_FTYPE_NUM:
            rc = ib_string_to_num(str, 0, &num);
            if (rc != IB_OK) {
                return rc;
            }
            new_field_value = ib_ftype_num_in(&num);
            break;
        case IB_FTYPE_FLOAT:
            rc = ib_string_to_float(str, &flt);
            if (rc != IB_OK) {
                return rc;
            }
            new_field_value = ib_ftype_float_in(&flt);
            break;
        default:
            return IB_EINVAL;
        }
        break;

    case IB_FTYPE_BYTESTR:

        /* Extract bytestr. */
        rc = ib_field_value(in_field, ib_ftype_bytestr_out(&bstr));
        if (rc != IB_OK){
            return rc;
        }
        str = (const char *)ib_bytestr_const_ptr(bstr);
        sz = ib_bytestr_length(bstr);

        /* Convert byte str. */
        switch(desired_type) {
        case IB_FTYPE_NULSTR:
            str = ib_mpool_memdup_to_str(mp, str, sz);
            if (!str) {
                return rc;
            }
            new_field_value = ib_ftype_nulstr_in(str);
            break;
        case IB_FTYPE_TIME:
            rc = ib_string_to_time_ex(str, sz, &tme);
            if (rc != IB_OK) {
                return rc;
            }
            new_field_value = ib_ftype_time_in(&tme);
            break;
        case IB_FTYPE_NUM:
            rc = ib_string_to_num_ex(str, sz, 0, &num);
            if (rc != IB_OK) {
                return rc;
            }
            new_field_value = ib_ftype_num_in(&num);
            break;
        case IB_FTYPE_FLOAT:
            rc = ib_string_to_float_ex(str, sz, &flt);
            if (rc != IB_OK) {
                return rc;
            }
            new_field_value = ib_ftype_float_in(&flt);
            break;
        default:
            return IB_EINVAL;
        }
        break;

    case IB_FTYPE_TIME:

        /* Extract time. */
        rc = ib_field_value(in_field, ib_ftype_time_out(&tme));
        if (rc != IB_OK){
            return rc;
        }

        switch (desired_type) {
        case IB_FTYPE_NULSTR:
            str = ib_time_to_string(mp, tme);
            if (! str) {
                return IB_EINVAL;
            }
            new_field_value = ib_ftype_nulstr_in(str);
            break;
        case IB_FTYPE_BYTESTR:
            str = ib_time_to_string(mp, tme);
            if (! str) {
                return IB_EINVAL;
            }
            rc = ib_bytestr_dup_nulstr((ib_bytestr_t **)&bstr, mp, str);
            if (rc != IB_OK){
                return rc;
            }
            new_field_value = ib_ftype_bytestr_in(bstr);
            break;
        case IB_FTYPE_FLOAT:
            flt = (ib_float_t)tme;
            /* Check that our assignment is within error=1, or fail. */
            if (llabs(tme - flt) > 1) {
                return IB_EINVAL;
            }
            new_field_value = ib_ftype_float_in(&flt);
            break;
        default:
            return IB_EINVAL;
        }
        break;

    case IB_FTYPE_NUM:

        /* Extract unum. */
        rc = ib_field_value(in_field, ib_ftype_num_out(&num));
        if (rc != IB_OK){
            return rc;
        }

        switch (desired_type) {
        case IB_FTYPE_NULSTR:
            str = ib_num_to_string(mp, num);
            if (! str) {
                return IB_EINVAL;
            }
            new_field_value = ib_ftype_nulstr_in(str);
            break;
        case IB_FTYPE_BYTESTR:
            str = ib_num_to_string(mp, num);
            if (! str) {
                return IB_EINVAL;
            }
            rc = ib_bytestr_dup_nulstr((ib_bytestr_t **)&bstr, mp, str);
            if (rc != IB_OK){
                return rc;
            }
            new_field_value = ib_ftype_bytestr_in(bstr);
            break;
        case IB_FTYPE_TIME:
            if (num < 0) {
                return IB_EINVAL;
            }
            tme = (ib_time_t)num;
            new_field_value = ib_ftype_time_in(&tme);
            break;
        case IB_FTYPE_FLOAT:
            flt = (ib_float_t)num;
            if (llabs(flt - num) > 1) {
                return IB_EINVAL;
            }
            new_field_value = ib_ftype_float_in(&flt);
            break;
        default:
            return IB_EINVAL;
        }
        break;

    case IB_FTYPE_FLOAT:

        /* Extract unum. */
        rc = ib_field_value(in_field, ib_ftype_float_out(&flt));
        if (rc != IB_OK){
            return rc;
        }

        switch (desired_type) {
        case IB_FTYPE_NULSTR:
            str = ib_float_to_string(mp, flt);
            if (!str) {
                return IB_EINVAL;
            }
            new_field_value = ib_ftype_nulstr_in(str);
            break;
        case IB_FTYPE_BYTESTR:
            str = ib_float_to_string(mp, flt);
            if (!str) {
                return IB_EINVAL;
            }
            rc = ib_bytestr_dup_nulstr((ib_bytestr_t **)&bstr, mp, str);
            if (rc != IB_OK){
                return rc;
            }
            new_field_value = ib_ftype_bytestr_in(bstr);
            break;
        case IB_FTYPE_TIME:
            if (flt < 0) {
                return IB_EINVAL;
            }
            tme = (ib_float_t)flt;
            new_field_value = ib_ftype_time_in(&tme);
            break;
        case IB_FTYPE_NUM:
            num = (ib_float_t)flt;
            new_field_value = ib_ftype_num_in(&num);
            break;
        default:
            return IB_EINVAL;
        }
        break;

    default:
        return IB_EINVAL;
    }

    rc = ib_field_create(
        out_field,
        mp,
        in_field->name,
        in_field->nlen,
        desired_type,
        new_field_value
    );
    return rc;
}
コード例 #16
0
ファイル: test_util_field.cpp プロジェクト: B0SB05/ironbee
TEST_F(TestIBUtilField, ConvertString)
{
    ib_field_t *f1;
    ib_field_t *f2;
    ib_status_t rc;
    ib_num_t    num;
    ib_float_t  flt;

    /*
     * Convert numeric string to number
     */

    /* Create the field */
    rc = ib_field_create(&f1, MM(), "one", 3,
                         IB_FTYPE_NULSTR, ib_ftype_nulstr_in("1"));
    ASSERT_EQ(IB_OK, rc);

    /* Attempt a numeric conversion. */
    rc = ib_field_convert(MM(), IB_FTYPE_NUM, f1, &f2);
    ASSERT_EQ(IB_OK, rc);

    /* Pull out param value for check. */
    rc = ib_field_value(f2, ib_ftype_num_out(&num));
    ASSERT_EQ(IB_OK, rc);
    ASSERT_EQ(1, num);

    /*
     * Convert floating-point string to float
     */

    /* Create the field */
    rc = ib_field_create(&f1, MM(), "one", 3,
                         IB_FTYPE_NULSTR, ib_ftype_nulstr_in("1.1"));
    ASSERT_EQ(IB_OK, rc);

    /* Attempt a numeric conversion. */
    rc = ib_field_convert(MM(), IB_FTYPE_FLOAT, f1, &f2);
    ASSERT_EQ(IB_OK, rc);

    /* Pull out param value for check. */
    rc = ib_field_value(f2, ib_ftype_float_out(&flt));
    ASSERT_EQ(IB_OK, rc);
    ASSERT_FLOAT_EQ(1.1, flt);

    /*
     * Convert non-numeric string to number
     */

    /* Create the field */
    rc = ib_field_create(&f1, MM(), "one", 3,
                         IB_FTYPE_NULSTR, ib_ftype_nulstr_in("x1"));
    ASSERT_EQ(IB_OK, rc);

    /* Attempt a numeric conversion. */
    rc = ib_field_convert(MM(), IB_FTYPE_NUM, f1, &f2);
    ASSERT_EQ(IB_EINVAL, rc);

    /*
     * Convert floating-point string to number
     */

    /* Create the field */
    rc = ib_field_create(&f1, MM(), "one", 3,
                         IB_FTYPE_NULSTR, ib_ftype_nulstr_in("1.1"));
    ASSERT_EQ(IB_OK, rc);

    /* Attempt a numeric conversion. */
    rc = ib_field_convert(MM(), IB_FTYPE_NUM, f1, &f2);
    ASSERT_EQ(IB_EINVAL, rc);


    /*
     * Convert non-numeric string to float
     */

    /* Create the field */
    rc = ib_field_create(&f1, MM(), "one", 3,
                         IB_FTYPE_NULSTR, ib_ftype_nulstr_in("x1.1"));
    ASSERT_EQ(IB_OK, rc);

    /* Attempt a numeric conversion. */
    rc = ib_field_convert(MM(), IB_FTYPE_FLOAT, f1, &f2);
    ASSERT_EQ(IB_EINVAL, rc);
}
コード例 #17
0
ファイル: field.c プロジェクト: niubl/ironbee
static ib_status_t field_from_string_internal(
    ib_mpool_t *mp,
    const char *name,
    size_t nlen,
    const char *vstr,
    size_t vlen,
    bool vstr_is_nulstr,
    ib_field_t **pfield)
{
    assert(mp != NULL);
    assert(name != NULL);
    assert(vstr != NULL);
    assert(pfield != NULL);

    ib_status_t conv;
    ib_status_t rc = IB_OK;
    ib_field_t *field;

    *pfield = NULL;

    /* Try to convert to an integer */
    if (*pfield == NULL) {
        ib_num_t num_val;
        if (vstr_is_nulstr) {
            conv = ib_string_to_num(vstr, 0, &num_val);
        }
        else {
            conv = ib_string_to_num_ex(vstr, vlen, 0, &num_val);
        }
        if (conv == IB_OK) {
            rc = ib_field_create(&field, mp,
                                 name, nlen,
                                 IB_FTYPE_NUM,
                                 ib_ftype_num_in(&num_val));
            *pfield = field;
        }
    }

    /* Try to convert to a float */
    if (*pfield == NULL) {
        ib_float_t float_val;
        if (vstr_is_nulstr) {
            conv = ib_string_to_float(vstr, &float_val);
        }
        else {
            conv = ib_string_to_float_ex(vstr, vlen, &float_val);
        }
        if (conv == IB_OK) {
            rc = ib_field_create(&field, mp,
                                 name, nlen,
                                 IB_FTYPE_FLOAT,
                                 ib_ftype_float_in(&float_val));
            *pfield = field;
        }
    }

    /* Finally, assume that it's a string */
    if (*pfield == NULL) {
        if (vstr_is_nulstr) {
            rc = ib_field_create(&field, mp,
                                 name, nlen,
                                 IB_FTYPE_NULSTR,
                                 ib_ftype_nulstr_in(vstr));
        }
        else {
            ib_bytestr_t *bs;
            rc = ib_bytestr_dup_mem(&bs, mp, (const uint8_t *)vstr, vlen);
            if (rc != IB_OK) {
                return rc;
            }
            rc = ib_field_create(&field, mp,
                                 name, nlen,
                                 IB_FTYPE_BYTESTR,
                                 ib_ftype_bytestr_in(bs));
        }
        *pfield = field;
    }

    return rc;
}
コード例 #18
0
TEST_F(OperatorTest, OperatorCallTest)
{
    ib_status_t status;
    ib_num_t call_result;
    void *instance_data;
    ib_operator_t *op;
    const ib_operator_t *cop;

    status = ib_operator_create_and_register(
        &op,
        ib_engine,
        "test_op",
        IB_OP_CAPABILITY_NON_STREAM,
        test_create_fn, NULL,
        NULL, NULL,
        test_execute_fn, NULL
    );
    ASSERT_EQ(IB_OK, status);

    status = ib_operator_lookup(ib_engine, "test_op", &cop);

    ASSERT_EQ(IB_OK, status);

    status = ib_operator_inst_create(op,
                                     ib_context_main(ib_engine),
                                     IB_OP_CAPABILITY_NON_STREAM,
                                     "INVALID",
                                     &instance_data);
    ASSERT_EQ(IB_EINVAL, status);

    status = ib_operator_inst_create(op,
                                     ib_context_main(ib_engine),
                                     IB_OP_CAPABILITY_NON_STREAM,
                                     "data",
                                     &instance_data);
    ASSERT_EQ(IB_OK, status);


    ib_field_t *field;
    const char *matching = "data matching string";
    const char *nonmatching = "non matching string";
    ib_field_create(
        &field,
        ib_engine_pool_main_get(ib_engine),
        IB_FIELD_NAME("testfield"),
        IB_FTYPE_NULSTR,
        NULL
    );

    ib_field_setv(field, ib_ftype_nulstr_in(matching));
    status = ib_operator_inst_execute(op, instance_data, ib_tx, field, NULL, &call_result);
    ASSERT_EQ(IB_OK, status);
    EXPECT_EQ(1, call_result);

    ib_field_setv(field, ib_ftype_nulstr_in(nonmatching));
    status = ib_operator_inst_execute(op, instance_data, ib_tx, field, NULL, &call_result);
    ASSERT_EQ(IB_OK, status);
    EXPECT_EQ(0, call_result);

    status = ib_operator_inst_destroy(op, instance_data);
    ASSERT_EQ(IB_OK, status);
}
コード例 #19
0
ファイル: field.cpp プロジェクト: strategist922/ironbee
void Field::set_null_string(const char* value) const
{
    Internal::check_type(NULL_STRING, type());
    Internal::set_value(ib(), ib_ftype_nulstr_in(value));
}
コード例 #20
0
ファイル: core_tfns.c プロジェクト: BillTheBest/ironbee
/**
 * String modification transformation core
 *
 * @param[in] ib IronBee engine
 * @param[in] mp Memory pool to use for allocations.
 * @param[in] fndata Function specific data.
 * @param[in] fin Input field.
 * @param[out] fout Output field.
 * @param[out] pflags Transformation flags.
 *
 * @returns IB_OK if successful.
 */
static ib_status_t tfn_strmod(ib_engine_t *ib,
                              ib_mpool_t *mp,
                              ib_strmod_fn_t str_fn,
                              ib_strmod_ex_fn_t ex_fn,
                              const ib_field_t *fin,
                              ib_field_t **fout,
                              ib_flags_t *pflags)
{
    IB_FTRACE_INIT();
    ib_status_t rc;
    ib_flags_t result;

    assert(ib != NULL);
    assert(mp != NULL);
    assert(str_fn != NULL);
    assert(ex_fn != NULL);
    assert(fin != NULL);
    assert(fout != NULL);
    assert(pflags != NULL);

    /* Initialize the output field pointer */
    *fout = NULL;

    switch(fin->type) {
    case IB_FTYPE_NULSTR :
    {
        const char *in;
        char *out;
        rc = ib_field_value(fin, ib_ftype_nulstr_out(&in));
        if (rc != IB_OK) {
            IB_FTRACE_RET_STATUS(rc);
        }
        if (in == NULL) {
            IB_FTRACE_RET_STATUS(IB_EINVAL);
        }
        rc = str_fn(IB_STROP_COW, mp, (char *)in, &out, &result);
        if (rc != IB_OK) {
            IB_FTRACE_RET_STATUS(rc);
        }
        rc = ib_field_create(fout, mp,
                             fin->name, fin->nlen,
                             IB_FTYPE_NULSTR,
                             ib_ftype_nulstr_in(out));
        if (rc != IB_OK) {
            IB_FTRACE_RET_STATUS(rc);
        }
        break;
    }

    case IB_FTYPE_BYTESTR:
    {
        const ib_bytestr_t *bs;
        const uint8_t *din;
        uint8_t *dout;
        size_t dlen;
        rc = ib_field_value(fin, ib_ftype_bytestr_out(&bs));
        if (rc != IB_OK) {
            IB_FTRACE_RET_STATUS(rc);
        }
        if (bs == NULL) {
            IB_FTRACE_RET_STATUS(IB_EINVAL);
        }
        din = ib_bytestr_const_ptr(bs);
        if (din == NULL) {
            IB_FTRACE_RET_STATUS(IB_EINVAL);
        }
        dlen = ib_bytestr_length(bs);
        rc = ex_fn(IB_STROP_COW, mp,
                   (uint8_t *)din, dlen,
                   &dout, &dlen,
                   &result);
        if (rc != IB_OK) {
            IB_FTRACE_RET_STATUS(rc);
        }
        rc = ib_field_create_bytestr_alias(fout, mp,
                                           fin->name, fin->nlen,
                                           dout, dlen);
        if (rc != IB_OK) {
            IB_FTRACE_RET_STATUS(rc);
        }
        break;
    }
    default:
        IB_FTRACE_RET_STATUS(IB_EINVAL);
    } /* switch(fin->type) */

    /* Check the flags */
    if (ib_flags_all(result, IB_STRFLAG_MODIFIED) == true) {
        *pflags = IB_TFN_FMODIFIED;
    }
    else {
        *pflags = IB_TFN_NONE;
    }

    IB_FTRACE_RET_STATUS(IB_OK);
}
コード例 #21
0
ファイル: test_module_pcre.cpp プロジェクト: B0SB05/ironbee
    virtual void SetUp()
    {
        ib_status_t rc;
        const char *s1 = "string 1";
        const char *s2 = "string 2";

        BaseTransactionFixture::SetUp();
        configureIronBee();
        performTx();

        ib_mm_t mm = ib_engine_mm_main_get(ib_engine);
        char* str1 = (char *)ib_mm_alloc(mm, (strlen(s1)+1));
        if (str1 == NULL) {
            throw std::runtime_error("Could not allocate string 1.");
        }
        strcpy(str1, s1);
        char* str2 = (char *)ib_mm_alloc(mm, (strlen(s2)+1));
        if (str1 == NULL) {
            throw std::runtime_error("Could not allocate string 2.");
        }
        strcpy(str2, s2);

        // Create field 1.
        rc = ib_field_create(&field1,
                             mm,
                             IB_S2SL("field1"),
                             IB_FTYPE_NULSTR,
                             ib_ftype_nulstr_in(str1));
        if (rc != IB_OK) {
            throw std::runtime_error("Could not initialize field1.");
        }

        // Create field 2.
        rc = ib_field_create(&field2,
                             mm,
                             IB_S2SL("field2"),
                             IB_FTYPE_NULSTR,
                             ib_ftype_nulstr_in(str2));
        if (rc != IB_OK) {
            throw std::runtime_error("Could not initialize field2.");
        }

        /* Create rule 1 */
        rc = ib_rule_create(ib_engine,
                            ib_context_engine(ib_engine),
                            __FILE__,
                            __LINE__,
                            true,
                            &rule1);
        if (rc != IB_OK) {
            throw std::runtime_error("Could not create rule1.");
        }
        rc = ib_rule_set_id(ib_engine, rule1, "rule1");
        if (rc != IB_OK) {
            throw std::runtime_error("Could not set ID for rule1.");
        }

        /* Create the rule execution object #1 */
        memset(&rule_exec1, 0, sizeof(rule_exec1));
        rule_exec1.ib = ib_engine;
        rule_exec1.tx = ib_tx;
        rule_exec1.rule = rule1;

        /* Create rule 2 */
        rc = (ib_rule_create(ib_engine,
                             ib_context_engine(ib_engine),
                             __FILE__,
                             __LINE__,
                             true,
                             &rule2));
        if (rc != IB_OK) {
            throw std::runtime_error("Could not create rule2.");
        }
        rc = ib_rule_set_id(ib_engine, rule2, "rule2");
        if (rc != IB_OK) {
            throw std::runtime_error("Could not set ID for rule2.");
        }
        rule2->flags |= IB_RULE_FLAG_CAPTURE;

        /* Create the rule execution object #1 */
        memset(&rule_exec2, 0, sizeof(rule_exec2));
        rule_exec2.ib = ib_engine;
        rule_exec2.tx = ib_tx;
        rule_exec2.rule = rule2;
    }
コード例 #22
0
ファイル: test_engine.cpp プロジェクト: strategist922/ironbee
static ib_status_t foo2bar(ib_engine_t *ib,
                           ib_mpool_t *mp,
                           void *fndata,
                           const ib_field_t *fin,
                           const ib_field_t **fout,
                           ib_flags_t *pflags)
{
    ib_status_t rc = IB_OK;
    ib_field_t *fnew;

    if (fin->type == IB_FTYPE_BYTESTR) {
        const ib_bytestr_t *ibs;
        rc = ib_field_value(fin, ib_ftype_bytestr_out(&ibs));
        if (rc != IB_OK) {
            return rc;
        }

        const uint8_t *data_in;
        size_t dlen_in;
        uint8_t *data_out;

        assert (ibs != NULL);

        data_in = ib_bytestr_const_ptr(ibs);
        dlen_in = ib_bytestr_length(ibs);

        if ( (data_in != NULL) &&
             (dlen_in == 3) &&
             (strncmp("foo", (char *)data_in, 3) == 0) )
        {
            data_out = (uint8_t *)ib_mpool_alloc(mp, dlen_in);
            if (data_out == NULL) {
                return IB_EINVAL;
            }
            *pflags = (IB_TFN_FMODIFIED);
            *(data_out+0) = 'b';
            *(data_out+1) = 'a';
            *(data_out+2) = 'r';
        }
        else {
            data_out = (uint8_t *)data_in;
        }
        rc = ib_field_create_bytestr_alias(&fnew, mp,
                                           fin->name, fin->nlen,
                                           data_out, dlen_in);
        if (rc == IB_OK) {
            *fout = fnew;
        }
    }
    else if (fin->type == IB_FTYPE_NULSTR) {
        const char *in;
        char *out;

        rc = ib_field_value(fin, ib_ftype_nulstr_out(&in));
        if (rc != IB_OK) {
            return rc;
        }
        if ( (in != NULL) && (strncmp(in, "foo", 3) == 0) ) {
            out = (char *)ib_mpool_alloc(mp, strlen(in) + 1);
            if (out == NULL) {
                return IB_EINVAL;
            }

            *pflags = (IB_TFN_FMODIFIED);
            *(out+0) = 'b';
            *(out+1) = 'a';
            *(out+2) = 'r';
            *(out+3) = '\0';
        }
        else {
            out = (char *)in;
        }
        rc = ib_field_create(&fnew, mp, fin->name, fin->nlen,
                             IB_FTYPE_NULSTR, ib_ftype_nulstr_in(out));
        if (rc == IB_OK) {
            *fout = fnew;
        }
    }
    else {
        return IB_EINVAL;
    }

    return rc;
}
コード例 #23
0
ファイル: test_util_field.cpp プロジェクト: aburan28/ironbee
/// @test Test util field library - ib_field_format() with nulstr
TEST_F(TestIBUtilField, test_field_format_nulstr)
{
    ib_field_t *f;
    ib_status_t rc;
    char fmtbuf1[8];
    char fmtbuf2[32];
    const char *nul1 = "\"a\n\"";
    const char *nul2 = "\fabcdefghijk\t";
    const char *tname;
    const char *buf;
    char *nulcopy;

    nulcopy = MemPoolStrDup(nul1);
    ASSERT_STRNE(NULL, nulcopy);
    rc = ib_field_create(&f, MemPool(), IB_FIELD_NAME("test_nulstr"),
                         IB_FTYPE_NULSTR, ib_ftype_nulstr_in(nulcopy));
    ASSERT_EQ(IB_OK, rc);
    ASSERT_TRUE(f);

    buf = ib_field_format(f, false, false, &tname, fmtbuf1, sizeof(fmtbuf1));
    ASSERT_STREQ(nul1, fmtbuf1);
    ASSERT_STREQ("NULSTR", tname);
    ASSERT_EQ(buf, fmtbuf1);

    buf = ib_field_format(f, true, false, &tname, fmtbuf1, sizeof(fmtbuf1));
    ASSERT_STREQ("\"\"a\n\"\"", fmtbuf1);
    ASSERT_STREQ("NULSTR", tname);
    ASSERT_EQ(buf, fmtbuf1);

    buf = ib_field_format(f, true, true, &tname, fmtbuf1, sizeof(fmtbuf1));
    ASSERT_STREQ("\"\\\"a\\n\"", fmtbuf1);
    ASSERT_STREQ("NULSTR", tname);
    ASSERT_EQ(buf, fmtbuf1);


    nulcopy = MemPoolStrDup(nul2);
    ASSERT_STRNE(NULL, nulcopy);
    rc = ib_field_create(&f, MemPool(), IB_FIELD_NAME("test_nulstr"),
                         IB_FTYPE_NULSTR, ib_ftype_nulstr_in(nulcopy));
    ASSERT_EQ(IB_OK, rc);
    ASSERT_TRUE(f);

    buf = ib_field_format(f, false, false, NULL, fmtbuf1, sizeof(fmtbuf1));
    ASSERT_STREQ("\fabcdef", fmtbuf1);
    ASSERT_EQ(buf, fmtbuf1);

    buf = ib_field_format(f, false, false, NULL, fmtbuf2, sizeof(fmtbuf2));
    ASSERT_STREQ(nul2, fmtbuf2);
    ASSERT_EQ(buf, fmtbuf2);

    buf = ib_field_format(f, true, false, NULL, fmtbuf2, sizeof(fmtbuf2));
    ASSERT_STREQ("\"\fabcdefghijk\t\"", fmtbuf2);
    ASSERT_EQ(buf, fmtbuf2);

    buf = ib_field_format(f, false, true, NULL, fmtbuf2, sizeof(fmtbuf2));
    ASSERT_STREQ("\\fabcdefghijk\\t", fmtbuf2);
    ASSERT_EQ(buf, fmtbuf2);

    buf = ib_field_format(f, true, true, NULL, fmtbuf2, sizeof(fmtbuf2));
    ASSERT_STREQ("\"\\fabcdefghijk\\t\"", fmtbuf2);
    ASSERT_EQ(buf, fmtbuf2);

}
コード例 #24
0
ファイル: core_tfns.c プロジェクト: niubl/ironbee
/**
 * String modification transformation core
 *
 * @param[in] mp Memory pool to use for allocations.
 * @param[in] str_fn NUL-terminated string transformation function
 * @param[in] ex_fn EX (string/length) transformation function
 * @param[in] fin Input field.
 * @param[out] fout Output field. This is NULL on error.
 *
 * @returns IB_OK if successful.
 */
static ib_status_t tfn_strmod(ib_mpool_t *mp,
                              ib_strmod_fn_t str_fn,
                              ib_strmod_ex_fn_t ex_fn,
                              const ib_field_t *fin,
                              const ib_field_t **fout)
{
    ib_status_t rc;
    ib_flags_t result;
    ib_field_t *fnew;

    assert(mp != NULL);
    assert(str_fn != NULL);
    assert(ex_fn != NULL);
    assert(fin != NULL);
    assert(fout != NULL);

    /* Initialize the output field pointer */
    *fout = NULL;

    switch(fin->type) {
    case IB_FTYPE_NULSTR :
    {
        const char *in;
        char *out;
        rc = ib_field_value(fin, ib_ftype_nulstr_out(&in));
        if (rc != IB_OK) {
            return rc;
        }
        if (in == NULL) {
            return IB_EINVAL;
        }
        rc = str_fn(IB_STROP_COW, mp, (char *)in, &out, &result);
        if (rc != IB_OK) {
            return rc;
        }
        rc = ib_field_create(&fnew, mp,
                             fin->name, fin->nlen,
                             IB_FTYPE_NULSTR,
                             ib_ftype_nulstr_in(out));
        if (rc != IB_OK) {
            return rc;
        }
        *fout = fnew;
        break;
    }

    case IB_FTYPE_BYTESTR:
    {
        const ib_bytestr_t *bs;
        const uint8_t *din;
        uint8_t *dout;
        size_t dlen;
        rc = ib_field_value(fin, ib_ftype_bytestr_out(&bs));
        if (rc != IB_OK) {
            return rc;
        }
        if (bs == NULL) {
            return IB_EINVAL;
        }
        din = ib_bytestr_const_ptr(bs);
        if (din == NULL) {
            return IB_EINVAL;
        }
        dlen = ib_bytestr_length(bs);
        rc = ex_fn(IB_STROP_COW, mp,
                   (uint8_t *)din, dlen,
                   &dout, &dlen,
                   &result);
        if (rc != IB_OK) {
            return rc;
        }
        rc = ib_field_create_bytestr_alias(&fnew, mp,
                                           fin->name, fin->nlen,
                                           dout, dlen);
        if (rc != IB_OK) {
            return rc;
        }
        *fout = fnew;
        break;
    }

    default:
        return IB_EINVAL;
    } /* switch(fin->type) */

    return IB_OK;
}
コード例 #25
0
ファイル: test_operator.cpp プロジェクト: aburan28/ironbee
TEST_F(OperatorTest, OperatorCallTest)
{
    ib_status_t status;
    ib_num_t call_result;
    ib_rule_t *rule = NULL; /* Unused by this operator. */
    ib_operator_inst_t *op;

    status = ib_operator_register(ib_engine,
                                  "test_op",
                                  IB_OP_FLAG_PHASE,
                                  test_create_fn,
                                  NULL,
                                  test_destroy_fn,
                                  NULL,
                                  test_execute_fn,
                                  NULL);
    ASSERT_EQ(IB_OK, status);

    status = ib_operator_inst_create(ib_engine,
                                     NULL,
                                     rule,
                                     IB_OP_FLAG_PHASE,
                                     "test_op",
                                     "INVALID",
                                     IB_OPINST_FLAG_NONE,
                                     &op);
    ASSERT_EQ(IB_EINVAL, status);

    status = ib_operator_inst_create(ib_engine,
                                     NULL,
                                     rule,
                                     IB_OP_FLAG_PHASE,
                                     "test_op",
                                     "data",
                                     IB_OPINST_FLAG_NONE,
                                     &op);
    ASSERT_EQ(IB_OK, status);

    ib_rule_exec_t rule_exec;
    memset(&rule_exec, 0, sizeof(rule_exec));
    rule_exec.ib = ib_engine;
    rule_exec.rule = rule;

    ib_field_t *field;
    const char *matching = "data matching string";
    const char *nonmatching = "non matching string";
    ib_field_create(
        &field,
        ib_engine_pool_main_get(ib_engine),
        IB_FIELD_NAME("testfield"),
        IB_FTYPE_NULSTR,
        NULL
    );

    ib_field_setv(field, ib_ftype_nulstr_in(matching));
    status = ib_operator_execute(&rule_exec, op, field, &call_result);
    ASSERT_EQ(IB_OK, status);
    EXPECT_EQ(1, call_result);

    ib_field_setv(field, ib_ftype_nulstr_in(nonmatching));
    status = ib_operator_execute(&rule_exec, op, field, &call_result);
    ASSERT_EQ(IB_OK, status);
    EXPECT_EQ(0, call_result);

    status = ib_operator_inst_destroy(op);
    ASSERT_EQ(IB_OK, status);
}