Imported Upstream version 2.7
[ossec-hids.git] / src / os_regex / os_regex.h
1 /*   $OSSEC, os_regex.h, v0.3, 2005/04/05, Daniel B. Cid$   */
2
3 /* Copyright (C) 2009 Trend Micro Inc.
4  * All right reserved.
5  *
6  * This program is a free software; you can redistribute it
7  * and/or modify it under the terms of the GNU General Public
8  * License (version 2) as published by the FSF - Free Software
9  * Foundation
10  */
11
12 /* See README for details */
13
14
15 #ifndef __OS_REGEX_H
16 #define __OS_REGEX_H
17
18
19 /* OSRegex_Compile flags */
20 #define OS_RETURN_SUBSTRING     0000200
21 #define OS_CASE_SENSITIVE       0000400
22
23
24 /* Pattern maximum size */
25 #define OS_PATTERN_MAXSIZE      2048
26
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
38
39 /* OSRegex structure */
40 typedef struct _OSRegex
41 {
42     int error;
43     int *flags;
44     char **patterns;
45     char **sub_strings;
46     char ***prts_closure;
47     char ***prts_str;
48 }OSRegex;
49
50
51 /* OSmatch structure */
52 typedef struct _OSMatch
53 {
54     int error;
55     int *size;
56     char **patterns;
57     int (**match_fp)(char *str, char *str2, int str_len, int size);
58 }OSMatch;
59
60
61 /*** Prototypes ***/
62
63
64 /** int OSRegex_Compile(char *pattern, OSRegex *reg, int flags) v0.1
65  * Compile a regular expression to be used later.
66  * Allowed flags are:
67  *      - OS_CASE_SENSITIVE
68  *      - OS_RETURN_SUBSTRING
69  * Returns 1 on success or 0 on error.
70  * The error code is set on reg->error.
71  */
72 int OSRegex_Compile(char *pattern, OSRegex *reg, int flags);
73
74
75 /** char *OSRegex_Execute(char *str, OSRegex *reg) v0.1
76  * Compare an already compiled regular expression with
77  * a not NULL string.
78  * Returns end of str on success or NULL on error.
79  * The error code is set on reg->error.
80  */
81 char *OSRegex_Execute(char *str, OSRegex *reg);
82
83
84 /** int OSRegex_FreePattern(SRegex *reg) v0.1
85  * Release all the memory created by the compilation/executation
86  * phases.
87  * Returns void.
88  */
89 void OSRegex_FreePattern(OSRegex *reg);
90
91
92 /** int OSRegex_FreeSubStrings(OSRegex *reg) v0.1
93  * Release all the memory created to store the sub strings.
94  * Returns void.
95  */
96 void OSRegex_FreeSubStrings(OSRegex *reg);
97
98
99 /** int OS_Regex(char *pattern, char *str) v0.4
100  * This function is a wrapper around the compile/execute
101  * functions. It should only be used when the pattern is
102  * only going to be used once.
103  * Returns 1 on success or 0 on failure.
104  */
105 int OS_Regex(char *pattern, char *str);
106
107
108
109 /** int OSMatch_Compile(char *pattern, OSMatch *reg, int flags) v0.1
110  * Compile a pattern to be used later.
111  * Allowed flags are:
112  *      - OS_CASE_SENSITIVE
113  * Returns 1 on success or 0 on error.
114  * The error code is set on reg->error.
115  */
116 int OSMatch_Compile(char *pattern, OSMatch *reg, int flags);
117
118
119 /** int OSMatch_Execute(char *str, int str_len, OSMatch *reg) v0.1
120  * Compare an already compiled pattern with
121  * a not NULL string.
122  * Returns 1 on success or 0 on error.
123  * The error code is set on reg->error.
124  */
125 int OSMatch_Execute(char *str, int str_len, OSMatch *reg);
126
127
128 /** int OSMatch_FreePattern(OSMatch *reg) v0.1
129  * Release all the memory created by the compilation/executation
130  * phases.
131  * Returns void.
132  */
133 void OSMatch_FreePattern(OSMatch *reg);
134
135
136 int OS_Match2(char *pattern, char *str);
137
138 int OS_Match3(char *pattern, char *str, char* delimiter);
139
140
141 /* OS_WordMatch v0.3:
142  * Searches for  pattern in the string
143  */
144 int OS_WordMatch(char *pattern, char *str);
145 #define OS_Match OS_WordMatch
146
147
148 /** char **OS_StrBreak(char match, char *str, int size) v0.2
149  * Split a string into multiples pieces, divided by a char "match".
150  * Returns a NULL terminated array on success or NULL on error.
151  */
152 char **OS_StrBreak(char match, char *str, int size);
153
154
155 /** int OS_StrHowClosedMatch(char *str1, char *str2) v0.1
156  * Returns the number of characters that both strings
157  * have in similar (start at the beginning of them).
158  */
159 int OS_StrHowClosedMatch(char *str1, char *str2);
160
161
162 /** Inline prototypes **/
163
164
165 /** int OS_StrStartsWith(char *str, char *pattern) v0.1
166  * Verifies if a string starts with the provided pattern.
167  * Returns 1 on success or 0 on failure.
168  */
169 #include <string.h>
170 #define startswith(x,y) (strncmp(x,y,strlen(y)) == 0?1:0)
171 #define OS_StrStartsWith startswith
172
173
174 /** int OS_StrIsNum(char *str) v0.1
175  * Checks if a specific string is numeric (like "129544")
176  */
177 int OS_StrIsNum(char *str);
178
179
180 /** int isValidChar(char c)
181  * Checks if a specified char is in the following range:
182  * a-z, A-Z, 0-9, _-.
183  */
184 #include "os_regex_maps.h"
185 #define isValidChar(x) (hostname_map[(unsigned char)x])
186
187
188 #endif
189
190
191 /* EOF */