<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_STRING_H_
#define _IB_STRING_H_

/**
 * @file
 * @brief IronBee --- String Utility Functions
 *
 * @author <NAME> <<EMAIL>>
 */

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

#include <stdlib.h>
#include <string.h>

#ifdef __cplusplus
extern "C" {
#endif

/**
 * @defgroup IronBeeUtilString String
 * @ingroup IronBeeUtil
 *
 * Functions to manipulate strings.
 *
 * @{
 */


/**
 * Operations for functions that modify strings
 */
typedef enum {
    IB_STROP_INPLACE,     /**< Perform the operation in-place */
    IB_STROP_COPY,        /**< Always copy the input */
    IB_STROP_COW,         /**< Use copy-on-write semantics */
    IB_STROP_BUF,         /**< Use pre-allocated buffer */
} ib_strop_t;

/**
 * @name String operator result flags
 *
 * @{
 */
#define IB_STRFLAG_NONE          (0x0)   /**< No flags */
#define IB_STRFLAG_MODIFIED   (1 << 0)   /**< Output is different from input */
#define IB_STRFLAG_NEWBUF     (1 << 1)   /**< Output is a new buffer */
#define IB_STRFLAG_ALIAS      (1 << 2)   /**< Output is an alias into input */
/** @} */

/**
 * Generic string modification function, ex version
 *
 * @param[in] op String modify operation
 * @param[in] mp Memory pool to use for allocations
 * @param[in] data_in Pointer to input data
 * @param[in] dlen_in Length of @a data_in
 * @param[out] data_out Pointer to output data
 * @param[out] dlen_out Length of @a data_out
 * @param[out] result Flags detailing the result (@c IB_STRFLAG_xx)
 *
 * @result Status code
 */
typedef ib_status_t (* ib_strmod_ex_fn_t) (ib_strop_t op,
                                           ib_mpool_t *mp,
                                           uint8_t *data_in,
                                           size_t dlen_in,
                                           uint8_t **data_out,
                                           size_t *dlen_out,
                                           ib_flags_t *result);

/**
 * Generic string modification function, string version
 *
 * @param[in] op String modify operation
 * @param[in] mp Memory pool for allocations
 * @param[in] str_in Data to operate on
 * @param[out] str_out Output data
 * @param[out] result Result flags (@c IB_STRFLAG_xxx)
 *
 * @returns Status code.
 */
typedef ib_status_t (* ib_strmod_fn_t) (ib_strop_t op,
                                        ib_mpool_t *mp,
                                        char *str_in,
                                        char **str_out,
                                        ib_flags_t *result);

/**
 * Convert string const to string and length parameters.
 *
 * Allows using a NUL terminated string in place of two parameters
 * (char*, len) by calling strlen().
 *
 * @param s String
 */
#define IB_S2SL(s)  (s), strlen(s)

/**
 * Convert string const to unsigned string and length parameters.
 *
 * Allows using a NUL terminated string in place of two parameters
 * (uint8_t*, len) by calling strlen().
 *
 * @param s String
 */
#define IB_S2USL(s)  ((uint8_t *)(s)), strlen(s)

/**
 * strchr() equivalent that operates on a string buffer with a length
 * which can have embedded NUL characters in it.
 *
 * @param[in] s String
 * @param[in] l Length
 * @param[in] c Character to search for
 */
char DLL_PUBLIC *ib_strchr(const char *s, size_t l, int c);

/**
 * Look for a character in a string that can have embedded NUL characters
 * in it.  This version will ignore NUL characters.
 *
 * @param[in] str String to search
 * @param[in] len length of the str
 * @param[in] c The character to search for
 * @param[out] offset Offset of the character; -1 if not found
 *
 * @return Status code
 */
ib_status_t DLL_PUBLIC ib_strchr_nul_ignore(const char *str,
                                            size_t len,
                                            int c,
                                            ssize_t *offset);

/**
 * Look for a character in a string that can have embedded NUL characters
 * in it.  This version returns an error if a NUL character is encountered
 * before len chars.
 *
 * @param[in] str String to search<commit_msg>string.h: Removed prototype for non-existent ib_strchr()<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_STRING_H_
#define _IB_STRING_H_

/**
 * @file
 * @brief IronBee --- String Utility Functions
 *
 * @author <NAME> <<EMAIL>>
 */

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

#include <stdlib.h>
#include <string.h>

#ifdef __cplusplus
extern "C" {
#endif

/**
 * @defgroup IronBeeUtilString String
 * @ingroup IronBeeUtil
 *
 * Functions to manipulate strings.
 *
 * @{
 */


/**
 * Operations for functions that modify strings
 */
typedef enum {
    IB_STROP_INPLACE,     /**< Perform the operation in-place */
    IB_STROP_COPY,        /**< Always copy the input */
    IB_STROP_COW,         /**< Use copy-on-write semantics */
    IB_STROP_BUF,         /**< Use pre-allocated buffer */
} ib_strop_t;

/**
 * @name String operator result flags
 *
 * @{
 */
#define IB_STRFLAG_NONE          (0x0)   /**< No flags */
#define IB_STRFLAG_MODIFIED   (1 << 0)   /**< Output is different from input */
#define IB_STRFLAG_NEWBUF     (1 << 1)   /**< Output is a new buffer */
#define IB_STRFLAG_ALIAS      (1 << 2)   /**< Output is an alias into input */
/** @} */

/**
 * Generic string modification function, ex version
 *
 * @param[in] op String modify operation
 * @param[in] mp Memory pool to use for allocations
 * @param[in] data_in Pointer to input data
 * @param[in] dlen_in Length of @a data_in
 * @param[out] data_out Pointer to output data
 * @param[out] dlen_out Length of @a data_out
 * @param[out] result Flags detailing the result (@c IB_STRFLAG_xx)
 *
 * @result Status code
 */
typedef ib_status_t (* ib_strmod_ex_fn_t) (ib_strop_t op,
                                           ib_mpool_t *mp,
                                           uint8_t *data_in,
                                           size_t dlen_in,
                                           uint8_t **data_out,
                                           size_t *dlen_out,
                                           ib_flags_t *result);

/**
 * Generic string modification function, string version
 *
 * @param[in] op String modify operation
 * @param[in] mp Memory pool for allocations
 * @param[in] str_in Data to operate on
 * @param[out] str_out Output data
 * @param[out] result Result flags (@c IB_STRFLAG_xxx)
 *
 * @returns Status code.
 */
typedef ib_status_t (* ib_strmod_fn_t) (ib_strop_t op,
                                        ib_mpool_t *mp,
                                        char *str_in,
                                        char **str_out,
                                        ib_flags_t *result);

/**
 * Convert string const to string and length parameters.
 *
 * Allows using a NUL terminated string in place of two parameters
 * (char*, len) by calling strlen().
 *
 * @param s String
 */
#define IB_S2SL(s)  (s), strlen(s)

/**
 * Convert string const to unsigned string and length parameters.
 *
 * Allows using a NUL terminated string in place of two parameters
 * (uint8_t*, len) by calling strlen().
 *
 * @param s String
 */
#define IB_S2USL(s)  ((uint8_t *)(s)), strlen(s)

/**
 * Look for a character in a string that can have embedded NUL characters
 * in it.  This version will ignore NUL characters.
 *
 * @param[in] str String to search
 * @param[in] len length of the str
 * @param[in] c The character to search for
 * @param[out] offset Offset of the character; -1 if not found
 *
 * @return Status code
 */
ib_status_t DLL_PUBLIC ib_strchr_nul_ignore(const char *str,
                                            size_t len,
                                            int c,
                                            ssize_t *offset);

/**
 * Look for a character in a string that can have embedded NUL characters
 * in it.  This version returns an error if a NUL character is encountered
 * before len chars.
 *
 * @param[in] str String to search
{'fraction_non_alphanumeric': 0.12361026884980797, 'fraction_numerical': 0.003840711542348898, 'mean_word_length': 2.3759808938928693, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 48, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2325723', 'n_tokens_mistral': 2820, 'n_tokens_neox': 2720, 'n_words': 1030}