1 /* Copyright (C) 2009 Trend Micro Inc.
4 * This program is a free software; you can redistribute it
5 * and/or modify it under the terms of the GNU General Public
6 * License (version 2) as published by the FSF - Free Software
10 /* See README for details */
18 #define PCRE2_CODE_UNIT_WIDTH 8
21 /* OSRegex_Compile flags */
22 #define OS_RETURN_SUBSTRING 0000200
23 #define OS_CASE_SENSITIVE 0000400
25 /* Pattern maximum size */
26 #define OS_PATTERN_MAXSIZE 2048
29 #define OS_REGEX_REG_NULL 1
30 #define OS_REGEX_PATTERN_NULL 2
31 #define OS_REGEX_MAXSIZE 3
32 #define OS_REGEX_OUTOFMEMORY 4
33 #define OS_REGEX_STR_NULL 5
34 #define OS_REGEX_BADREGEX 6
35 #define OS_REGEX_BADPARENTHESIS 7
36 #define OS_REGEX_NO_MATCH 8
37 #define OS_REGEX_NO_JIT 9
39 #define OS_CONVERT_REGEX 1
40 #define OS_CONVERT_MATCH 2
42 /* OSRegex structure */
43 typedef struct _OSRegex {
47 pcre2_match_data *match_data;
50 const char *(*exec_function)(const char *, struct _OSRegex *);
53 /* OSmatch structure */
54 typedef struct _OSMatch {
57 pcre2_match_data *match_data;
60 int (*exec_function)(const char *, size_t, struct _OSMatch *);
63 /* OSPcre2 structure */
64 typedef struct _OSPcre2 {
68 pcre2_match_data *match_data;
71 const char *(*exec_function)(const char *, struct _OSPcre2 *);
76 /* Convert an Ossec pattern, match or regex,
81 * Returns 1 on success or 0 on error.
83 int OSRegex_Convert(const char *pattern, char **converted_pattern, uint32_t map);
85 /* Compile a regular expression to be used later
88 * - OS_RETURN_SUBSTRING
89 * Returns 1 on success or 0 on error.
90 * The error code is set on reg->error.
92 int OSRegex_Compile(const char *pattern, OSRegex *reg, int flags);
94 /* Compare an already compiled regular expression with
96 * Returns end of str on success or NULL on error.
97 * The error code is set on reg->error.
99 const char *OSRegex_Execute(const char *str, OSRegex *reg) __attribute__((nonnull(2)));
101 /* Release all the memory created by the compilation/execution phases */
102 void OSRegex_FreePattern(OSRegex *reg) __attribute__((nonnull));
105 /* Release all the memory created to store the sub strings */
106 void OSRegex_FreeSubStrings(OSRegex *reg) __attribute__((nonnull));
108 /* This function is a wrapper around the compile/execute
109 * functions. It should only be used when the pattern is
110 * only going to be used once.
111 * Returns 1 on success or 0 on failure.
113 int OS_Regex(const char *pattern, const char *str);
115 /* Compile a pattern to be used later.
117 * - OS_CASE_SENSITIVE
118 * Returns 1 on success or 0 on error.
119 * The error code is set on reg->error.
121 int OSMatch_Compile(const char *pattern, OSMatch *reg, int flags);
123 /* Compare an already compiled pattern with a not NULL string.
124 * Returns 1 on success or 0 on error.
125 * The error code is set on reg->error.
127 int OSMatch_Execute(const char *str, size_t str_len, OSMatch *reg) __attribute__((nonnull(3)));
129 /* Release all the memory created by the compilation/execution phases */
130 void OSMatch_FreePattern(OSMatch *reg) __attribute__((nonnull));
132 int OS_Match2(const char *pattern, const char *str) __attribute__((nonnull(2)));
134 /* Searches for pattern in the string */
135 int OS_WordMatch(const char *pattern, const char *str) __attribute__((nonnull));
136 #define OS_Match OS_WordMatch
138 /* Compile a PCRE2 expression to be used later
139 * Allowed flags are the same as option in pcre2_compile
140 * Returns 1 on success or 0 on error.
141 * The error code is set on reg->error.
143 int OSPcre2_Compile(const char *pattern, OSPcre2 *reg, int flags);
145 /* Compare an already compiled PCRE2 expression with
147 * Returns end of str on success or NULL on error.
148 * The error code is set on reg->error.
150 const char *OSPcre2_Execute(const char *str, OSPcre2 *reg);
152 /* Release all the memory created by the compilation/execution phases */
153 void OSPcre2_FreePattern(OSPcre2 *reg);
155 /* Release all the memory created to store the sub strings */
156 void OSPcre2_FreeSubStrings(OSPcre2 *reg);
158 /* This function is a wrapper around the compile/execute
159 * functions. It should only be used when the pattern is
160 * only going to be used once.
161 * Returns 1 on success or 0 on failure.
163 int OS_Pcre2(const char *pattern, const char *str);
165 /* Split a string into multiples pieces, divided by a char "match".
166 * Returns a NULL terminated array on success or NULL on error.
168 char **OS_StrBreak(char match, const char *str, size_t size);
170 /* Returns the number of characters that both strings
171 * have in similar (start at the beginning of them).
173 size_t OS_StrHowClosedMatch(const char *str1, const char *str2);
175 /** Inline prototypes **/
177 /* Verifies if a string starts with the provided pattern.
178 * Returns 1 on success or 0 on failure.
180 int OS_StrStartsWith(const char *str, const char *pattern) __attribute__((nonnull));
182 /* Checks if a specific string is numeric (like "129544") */
183 int OS_StrIsNum(const char *str);
185 /* Checks if a specified char is in the following range:
188 extern const unsigned char hostname_map[256];
189 #define isValidChar(x) (hostname_map[(unsigned char)x])
191 #endif /* __OS_REGEX_H */