Imported Upstream version 2.5.1
[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     
139 /* OS_WordMatch v0.3:
140  * Searches for  pattern in the string
141  */
142 int OS_WordMatch(char *pattern, char *str);
143 #define OS_Match OS_WordMatch
144
145
146 /** char **OS_StrBreak(char match, char *str, int size) v0.2
147  * Split a string into multiples pieces, divided by a char "match".
148  * Returns a NULL terminated array on success or NULL on error.
149  */
150 char **OS_StrBreak(char match, char *str, int size);
151   
152
153 /** int OS_StrHowClosedMatch(char *str1, char *str2) v0.1
154  * Returns the number of characters that both strings
155  * have in similar (start at the beginning of them).
156  */
157 int OS_StrHowClosedMatch(char *str1, char *str2);
158
159   
160 /** Inline prototypes **/
161
162
163 /** int OS_StrStartsWith(char *str, char *pattern) v0.1
164  * Verifies if a string starts with the provided pattern.
165  * Returns 1 on success or 0 on failure.
166  */
167 #include <string.h>
168 #define startswith(x,y) (strncmp(x,y,strlen(y)) == 0?1:0)
169 #define OS_StrStartsWith startswith
170
171
172 /** int OS_StrIsNum(char *str) v0.1
173  * Checks if a specific string is numeric (like "129544")
174  */
175 int OS_StrIsNum(char *str);
176
177
178 /** int isValidChar(char c)
179  * Checks if a specified char is in the following range:
180  * a-z, A-Z, 0-9, _-.
181  */
182 #include "os_regex_maps.h" 
183 #define isValidChar(x) (hostname_map[(unsigned char)x])
184
185
186 #endif
187
188
189 /* EOF */