<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.
 *****************************************************************************/

#ifndef _IB_RADIX_H_
#define _IB_RADIX_H_

/**
 * @file
 * @brief IronBee &mdash; Radix Utility Functions
 *
 * @author <NAME> <<EMAIL>>
 */

#include <ironbee/build.h>
#include <ironbee/types.h>
#include <ironbee/list.h>
#include <ironbee/field.h>     /* For ib_num_t */

#include <string.h>

#ifdef __cplusplus
extern "C" {
#endif

/**
 * @defgroup IronBeeUtilRadix Radix
 * @ingroup IronBeeUtil
 *
 * Datastructure to that maps bit string prefixes to arbitrary data.
 *
 * @{
 */

typedef struct ib_radix_t ib_radix_t;
typedef struct ib_radix_prefix_t ib_radix_prefix_t;
typedef struct ib_radix_node_t ib_radix_node_t;

typedef void (*ib_radix_update_fn_t)(ib_radix_node_t*, void*);
typedef void (*ib_radix_print_fn_t)(void *);
typedef void (*ib_radix_free_fn_t)(void *);

/**
 * Creates a new prefix instance
 *
 * @param prefix reference to a pointer that will link to the allocated prefix
 * @param pool memory pool that the allocation should use
 *
 * @returns Status code
 */
ib_status_t ib_radix_prefix_new(ib_radix_prefix_t **prefix,
                                ib_mpool_t *pool);

/**
 * Creates a new prefix instance
 *
 * @param prefix reference to a pointer that will link to the allocated prefix
 * @param rawbits sequence of bytes representing the key prefix
 * @param prefixlen size in bits with the len of the prefix
 * @param pool memory pool that the allocation should use
 *
 * @returns Status code
 */
ib_status_t ib_radix_prefix_create(ib_radix_prefix_t **prefix,
                                   uint8_t *rawbits,
                                   uint8_t prefixlen,
                                   ib_mpool_t *pool);

/**
 * creates a clone of the prefix instance
 *
 * @param orig pointer to the original prefix
 * @param new_prefix reference to a pointer to the allocated prefix
 * @param mp memory pool that the allocation should use
 *
 * @returns Status code
 */
ib_status_t ib_radix_clone_prefix(ib_radix_prefix_t *orig,
                                  ib_radix_prefix_t **new_prefix,
                                  ib_mpool_t *mp);

/**
 * destroy a prefix
 *
 * @param prefix to destroy
 * @param pool memory of the prefix
 *
 * @returns Status code
 */
ib_status_t ib_radix_prefix_destroy(ib_radix_prefix_t **prefix,
                                    ib_mpool_t *pool);


/**
 * Creates a new node instance
 *
 * @param node reference to a pointer that will link to the allocated node
 * @param pool memory pool that the allocation should use
 *
 * @returns Status code
 */
ib_status_t ib_radix_node_new(ib_radix_node_t **node,
                              ib_mpool_t *pool);

/**
 * creates a clone of the node instance
 *
 * @param orig pointer to the original node
 * @param new_node reference to a pointer that will link to the allocated node
 * @param mp memory pool that the allocation should use
 *
 * @returns Status code
 */
ib_status_t ib_radix_clone_node(ib_radix_node_t *orig,
                                ib_radix_node_t **new_node,
                                ib_mpool_t *mp);

/**
 * Destroy a node and its children (this includes prefix and userdata)
 *
 * @param radix the radix of the node
 * @param node the node to destroy
 * @param pool memory pool of the node
 *
 * @returns Status code
 */
ib_status_t ib_radix_node_destroy(ib_radix_t *radix,
                                  ib_radix_node_t **node,
                                  ib_mpool_t *pool);

/**
 * Creates a new radix tree registering functions to update, free and print
 * associated to each prefix it also register the memory pool it should use
 * for new allocations
 *
 * @param radix pointer to link the instance of the new radix
 * @param free_data pointer to the function that will be used to
 * free the userdata entries
 * @param update_data pointer to the function that knows how to update a node
 * with new user data
 * @param print_data pointer to a helper function that print_datas a userdata
 * @param pool memory pool that the allocation should use
 *
 * @returns Status code
 */
ib_status_t ib_radix_new(ib_radix_t **radix,
                         ib_radix_free_fn_t free_data,
                         ib_radix_print_fn_t print_data,
                         ib_radix_update_fn_t update_data,
                         ib_mpool_t *pool);

/*
 * Inserts a new user data associated to the prefix passed. The prefix is not
 * used, so developers are responsible to free that prefix
 * Keys can be of "any size" but this will be probably used for
 * CIDR data prefixes only (from 0 to 32 ~ 128 depending on IPv4
 * or IPv6 respectively)
 *
 * @param radix the radix of the node
 * @param prefix the prefix to use as index
 * @param prefix_data, the data to store under that prefix
 *
 * @returns Status code
 */
ib_status_t ib_radix_insert_data(ib_radix_t *radix,<commit_msg>Documented the behavior of NULL prefix_data<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.
 *****************************************************************************/

#ifndef _IB_RADIX_H_
#define _IB_RADIX_H_

/**
 * @file
 * @brief IronBee &mdash; Radix Utility Functions
 *
 * @author <NAME> <<EMAIL>>
 */

#include <ironbee/build.h>
#include <ironbee/types.h>
#include <ironbee/list.h>
#include <ironbee/field.h>     /* For ib_num_t */

#include <string.h>

#ifdef __cplusplus
extern "C" {
#endif

/**
 * @defgroup IronBeeUtilRadix Radix
 * @ingroup IronBeeUtil
 *
 * Datastructure to that maps bit string prefixes to arbitrary data.
 *
 * @{
 */

typedef struct ib_radix_t ib_radix_t;
typedef struct ib_radix_prefix_t ib_radix_prefix_t;
typedef struct ib_radix_node_t ib_radix_node_t;

typedef void (*ib_radix_update_fn_t)(ib_radix_node_t*, void*);
typedef void (*ib_radix_print_fn_t)(void *);
typedef void (*ib_radix_free_fn_t)(void *);

/**
 * Creates a new prefix instance
 *
 * @param prefix reference to a pointer that will link to the allocated prefix
 * @param pool memory pool that the allocation should use
 *
 * @returns Status code
 */
ib_status_t ib_radix_prefix_new(ib_radix_prefix_t **prefix,
                                ib_mpool_t *pool);

/**
 * Creates a new prefix instance
 *
 * @param prefix reference to a pointer that will link to the allocated prefix
 * @param rawbits sequence of bytes representing the key prefix
 * @param prefixlen size in bits with the len of the prefix
 * @param pool memory pool that the allocation should use
 *
 * @returns Status code
 */
ib_status_t ib_radix_prefix_create(ib_radix_prefix_t **prefix,
                                   uint8_t *rawbits,
                                   uint8_t prefixlen,
                                   ib_mpool_t *pool);

/**
 * creates a clone of the prefix instance
 *
 * @param orig pointer to the original prefix
 * @param new_prefix reference to a pointer to the allocated prefix
 * @param mp memory pool that the allocation should use
 *
 * @returns Status code
 */
ib_status_t ib_radix_clone_prefix(ib_radix_prefix_t *orig,
                                  ib_radix_prefix_t **new_prefix,
                                  ib_mpool_t *mp);

/**
 * destroy a prefix
 *
 * @param prefix to destroy
 * @param pool memory of the prefix
 *
 * @returns Status code
 */
ib_status_t ib_radix_prefix_destroy(ib_radix_prefix_t **prefix,
                                    ib_mpool_t *pool);


/**
 * Creates a new node instance
 *
 * @param node reference to a pointer that will link to the allocated node
 * @param pool memory pool that the allocation should use
 *
 * @returns Status code
 */
ib_status_t ib_radix_node_new(ib_radix_node_t **node,
                              ib_mpool_t *pool);

/**
 * creates a clone of the node instance
 *
 * @param orig pointer to the original node
 * @param new_node reference to a pointer that will link to the allocated node
 * @param mp memory pool that the allocation should use
 *
 * @returns Status code
 */
ib_status_t ib_radix_clone_node(ib_radix_node_t *orig,
                                ib_radix_node_t **new_node,
                                ib_mpool_t *mp);

/**
 * Destroy a node and its children (this includes prefix and userdata)
 *
 * @param radix the radix of the node
 * @param node the node to destroy
 * @param pool memory pool of the node
 *
 * @returns Status code
 */
ib_status_t ib_radix_node_destroy(ib_radix_t *radix,
                                  ib_radix_node_t **node,
                                  ib_mpool_t *pool);

/**
 * Creates a new radix tree registering functions to update, free and print
 * associated to each prefix it also register the memory pool it should use
 * for new allocations
 *
 * @param radix pointer to link the instance of the new radix
 * @param free_data pointer to the function that will be used to
 * free the userdata entries
 * @param update_data pointer to the function that knows how to update a node
 * with new user data
 * @param print_data pointer to a helper function that print_datas a userdata
 * @param pool memory pool that the allocation should use
 *
 * @returns Status code
 */
ib_status_t ib_radix_new(ib_radix_t **radix,
                         ib_radix_free_fn_t free_data,
                         ib_radix_print_fn_t print_data,
                         ib_radix_update_fn_t update_data,
                         ib_mpool_t *pool);

/*
 * Inserts a new user data associated to the prefix passed.
 *
 * The prefix is not used, so developers are responsible to free that prefix
 * Keys can be of "any size" but this will be probably used for CIDR data
 * prefixes only (from 0 to 32 ~ 128 depending on IPv4 or IPv6 respectively)
 *
 * @note: It's valid to insert NULL @a prefix_data, but if you do so,
 * it will always fail closest matches.
 *
 * @param radix the radix of the node
 * @param prefix the prefix to use as index
 * @param prefix_data, the data to store under that prefix
 *
 * @returns Status code
 */
ib_status_t ib_radix_insert_data(ib_radix_t *radix,
{'fraction_non_alphanumeric': 0.09180104086682024, 'fraction_numerical': 0.0023888746693968093, 'mean_word_length': 2.9762550881953866, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6851590', 'n_tokens_mistral': 3392, 'n_tokens_neox': 3274, 'n_words': 1311}