new upstream release (3.3.0); modify package compatibility for Stretch
[ossec-hids.git] / src / os_regex / os_regex.h
1 /* Copyright (C) 2009 Trend Micro Inc.
2  * All right reserved.
3  *
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
7  * Foundation
8  */
9
10 /* See README for details */
11
12 #ifndef __OS_REGEX_H
13 #define __OS_REGEX_H
14
15 /* size_t */
16 #include <stddef.h>
17
18 #define PCRE2_CODE_UNIT_WIDTH 8
19 #include <pcre2.h>
20
21 /* OSRegex_Compile flags */
22 #define OS_RETURN_SUBSTRING     0000200
23 #define OS_CASE_SENSITIVE       0000400
24
25 /* Pattern maximum size */
26 #define OS_PATTERN_MAXSIZE      2048
27
28 /* Error codes */
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
38
39 #define OS_CONVERT_REGEX        1
40 #define OS_CONVERT_MATCH        2
41
42 /* OSRegex structure */
43 typedef struct _OSRegex {
44     int error;
45     char **sub_strings;
46     pcre2_code *regex;
47     pcre2_match_data *match_data;
48     size_t pattern_len;
49     char *pattern;
50     const char *(*exec_function)(const char *, struct _OSRegex *);
51 } OSRegex;
52
53 /* OSmatch structure */
54 typedef struct _OSMatch {
55     int error;
56     pcre2_code *regex;
57     pcre2_match_data *match_data;
58     size_t pattern_len;
59     char *pattern;
60     int (*exec_function)(const char *, size_t, struct _OSMatch *);
61 } OSMatch;
62
63 /* OSPcre2 structure */
64 typedef struct _OSPcre2 {
65     int error;
66     char **sub_strings;
67     pcre2_code *regex;
68     pcre2_match_data *match_data;
69     size_t pattern_len;
70     char *pattern;
71     const char *(*exec_function)(const char *, struct _OSPcre2 *);
72 } OSPcre2;
73
74 /*** Prototypes ***/
75
76 /* Convert an Ossec pattern, match or regex,
77  * to a PCRE2 pattern
78  * Allowed map are:
79  *      - OS_CONVERT_REGEX
80  *      - OS_CONVERT_MATCH
81  * Returns 1 on success or 0 on error.
82  */
83 int OSRegex_Convert(const char *pattern, char **converted_pattern, uint32_t map);
84
85 /* Compile a regular expression to be used later
86  * Allowed flags are:
87  *      - OS_CASE_SENSITIVE
88  *      - OS_RETURN_SUBSTRING
89  * Returns 1 on success or 0 on error.
90  * The error code is set on reg->error.
91  */
92 int OSRegex_Compile(const char *pattern, OSRegex *reg, int flags);
93
94 /* Compare an already compiled regular expression with
95  * a not NULL string.
96  * Returns end of str on success or NULL on error.
97  * The error code is set on reg->error.
98  */
99 const char *OSRegex_Execute(const char *str, OSRegex *reg) __attribute__((nonnull(2)));
100
101 /* Release all the memory created by the compilation/execution phases */
102 void OSRegex_FreePattern(OSRegex *reg) __attribute__((nonnull));
103
104
105 /* Release all the memory created to store the sub strings */
106 void OSRegex_FreeSubStrings(OSRegex *reg) __attribute__((nonnull));
107
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.
112  */
113 int OS_Regex(const char *pattern, const char *str);
114
115 /* Compile a pattern to be used later.
116  * Allowed flags are:
117  *      - OS_CASE_SENSITIVE
118  * Returns 1 on success or 0 on error.
119  * The error code is set on reg->error.
120  */
121 int OSMatch_Compile(const char *pattern, OSMatch *reg, int flags);
122
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.
126  */
127 int OSMatch_Execute(const char *str, size_t str_len, OSMatch *reg)  __attribute__((nonnull(3)));
128
129 /* Release all the memory created by the compilation/execution phases */
130 void OSMatch_FreePattern(OSMatch *reg) __attribute__((nonnull));
131
132 int OS_Match2(const char *pattern, const char *str)  __attribute__((nonnull(2)));
133
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
137
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.
142  */
143 int OSPcre2_Compile(const char *pattern, OSPcre2 *reg, int flags);
144
145 /* Compare an already compiled PCRE2 expression with
146  * a not NULL string.
147  * Returns end of str on success or NULL on error.
148  * The error code is set on reg->error.
149  */
150 const char *OSPcre2_Execute(const char *str, OSPcre2 *reg);
151
152 /* Release all the memory created by the compilation/execution phases */
153 void OSPcre2_FreePattern(OSPcre2 *reg);
154
155 /* Release all the memory created to store the sub strings */
156 void OSPcre2_FreeSubStrings(OSPcre2 *reg);
157
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.
162  */
163 int OS_Pcre2(const char *pattern, const char *str);
164
165 /* Split a string into multiples pieces, divided by a char "match".
166  * Returns a NULL terminated array on success or NULL on error.
167  */
168 char **OS_StrBreak(char match, const char *str, size_t size);
169
170 /* Returns the number of characters that both strings
171  * have in similar (start at the beginning of them).
172  */
173 size_t OS_StrHowClosedMatch(const char *str1, const char *str2);
174
175 /** Inline prototypes **/
176
177 /* Verifies if a string starts with the provided pattern.
178  * Returns 1 on success or 0 on failure.
179  */
180 int OS_StrStartsWith(const char *str, const char *pattern) __attribute__((nonnull));
181
182 /* Checks if a specific string is numeric (like "129544") */
183 int OS_StrIsNum(const char *str);
184
185 /* Checks if a specified char is in the following range:
186  * a-z, A-Z, 0-9, _-.
187  */
188 extern const unsigned char hostname_map[256];
189 #define isValidChar(x) (hostname_map[(unsigned char)x])
190
191 #endif /* __OS_REGEX_H */