<commit_before>/*****************************************************************************
 * Licensed to Qualys, Inc. (QUALYS) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * QUALYS licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ****************************************************************************/

/**
 * @file
 * @brief Predicate --- Standard IronBee Implementation
 *
 * @author <NAME> <<EMAIL>>
 */

#include <predicate/standard_ironbee.hpp>

#include <predicate/call_factory.hpp>
#include <predicate/call_helpers.hpp>
#include <predicate/validate.hpp>

#include <ironbeepp/operator.hpp>
#include <ironbeepp/transformation.hpp>

#include <ironbee/rule_engine.h>

using namespace std;

namespace IronBee {
namespace Predicate {
namespace Standard {

struct Var::data_t
{
    ib_var_source_t *source;
};

Var::Var() :
    m_data(new data_t)
{
    // nop
}

string Var::name() const
{
    return "var";
}

bool Var::validate(NodeReporter reporter) const
{
    bool result = true;
    result = Validate::n_children(reporter, 1) && result;
    result = Validate::nth_child_is_string(reporter, 0) && result;

    return result;
}

void Var::pre_eval(Environment environment, NodeReporter reporter)
{
    const ib_var_config_t* config;

    config = ib_engine_var_config_get_const(environment.ib());
    assert(config != NULL);

    // Key must be static.
    Value key_field = literal_value(children().front());
    IronBee::ConstByteString key = key_field.value_as_byte_string();

    IronBee::throw_if_error(
        ib_var_source_acquire(
            &(m_data->source),
            environment.main_memory_pool().ib(),
            config,
            key.const_data(), key.length()
        )
    );
}

void Var::calculate(EvalContext context)
{
    ib_field_t* data_field;
    ib_status_t rc;

    if (is_aliased()) {
        return;
    }

    rc = ib_var_source_get(
        m_data->source,
        &data_field,
        context.ib()->var_store
    );
    if (rc == IB_ENOENT) {
        finish();
        return;
    }
    else {
        IronBee::throw_if_error(rc);
    }

    Value value(data_field);
    if (
        value.is_dynamic() ||
        value.type() != Value::LIST
    ) {
        add_value(value);
        finish();<commit_msg>predicate/standard_ironbee: Updated to use Ironbee++ var interface.<commit_after>/*****************************************************************************
 * Licensed to Qualys, Inc. (QUALYS) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * QUALYS licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ****************************************************************************/

/**
 * @file
 * @brief Predicate --- Standard IronBee Implementation
 *
 * @author <NAME> <<EMAIL>.com>
 */

#include <predicate/standard_ironbee.hpp>

#include <predicate/call_factory.hpp>
#include <predicate/call_helpers.hpp>
#include <predicate/validate.hpp>

#include <ironbeepp/operator.hpp>
#include <ironbeepp/transformation.hpp>
#include <ironbeepp/var.hpp>

#include <ironbee/rule_engine.h>

using namespace std;

namespace IronBee {
namespace Predicate {
namespace Standard {

struct Var::data_t
{
    VarSource source;
};

Var::Var() :
    m_data(new data_t)
{
    // nop
}

string Var::name() const
{
    return "var";
}

bool Var::validate(NodeReporter reporter) const
{
    bool result = true;
    result = Validate::n_children(reporter, 1) && result;
    result = Validate::nth_child_is_string(reporter, 0) && result;

    return result;
}

void Var::pre_eval(Environment environment, NodeReporter reporter)
{
    // Key must be static.
    Value key_field = literal_value(children().front());
    IronBee::ConstByteString key = key_field.value_as_byte_string();

    m_data->source = VarSource::acquire(
        environment.main_memory_pool(),
        environment.var_config(),
        key.const_data(), key.length()
    );
}

void Var::calculate(EvalContext context)
{
    Value value;

    if (is_aliased()) {
        return;
    }

    try {
        value = m_data->source.get(context.var_store());
    }
    catch (enoent) {
        finish();
        return;
    }

    if (
        value.is_dynamic() ||
        value.type() != Value::LIST
    ) {
        add_value(value);
        finish();
{'fraction_non_alphanumeric': 0.15973227206946455, 'fraction_numerical': 0.002170767004341534, 'mean_word_length': 3.5245499181669393, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3372693', 'n_tokens_mistral': 1617, 'n_tokens_neox': 1580, 'n_words': 452}