981044ecae048de738eaeb561d315d463249400b
[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 /* size_t */
19 #include <stddef.h>
20
21
22 /* OSRegex_Compile flags */
23 #define OS_RETURN_SUBSTRING     0000200
24 #define OS_CASE_SENSITIVE       0000400
25
26
27 /* Pattern maximum size */
28 #define OS_PATTERN_MAXSIZE      2048
29
30
31 /* Error codes */
32 #define OS_REGEX_REG_NULL       1
33 #define OS_REGEX_PATTERN_NULL   2
34 #define OS_REGEX_MAXSIZE        3
35 #define OS_REGEX_OUTOFMEMORY    4
36 #define OS_REGEX_STR_NULL       5
37 #define OS_REGEX_BADREGEX       6
38 #define OS_REGEX_BADPARENTHESIS 7
39 #define OS_REGEX_NO_MATCH       8
40
41
42 /* OSRegex structure */
43 typedef struct _OSRegex
44 {
45     int error;
46     int *flags;
47     char **patterns;
48     char **sub_strings;
49     const char ***prts_closure;
50     const char ***prts_str;
51 }OSRegex;
52
53
54 /* OSmatch structure */
55 typedef struct _OSMatch
56 {
57     int error;
58     size_t *size;
59     char **patterns;
60     int (**match_fp)(const char *str, const char *str2, size_t str_len, size_t size);
61 }OSMatch;
62
63
64 /*** Prototypes ***/
65
66
67 /** int OSRegex_Compile(char *pattern, OSRegex *reg, int flags) v0.1
68  * Compile a regular expression to be used later.
69  * Allowed flags are:
70  *      - OS_CASE_SENSITIVE
71  *      - OS_RETURN_SUBSTRING
72  * Returns 1 on success or 0 on error.
73  * The error code is set on reg->error.
74  */
75 int OSRegex_Compile(const char *pattern, OSRegex *reg, int flags);
76
77
78 /** char *OSRegex_Execute(char *str, OSRegex *reg) v0.1
79  * Compare an already compiled regular expression with
80  * a not NULL string.
81  * Returns end of str on success or NULL on error.
82  * The error code is set on reg->error.
83  */
84 const char *OSRegex_Execute(const char *str, OSRegex *reg) __attribute__((nonnull(2)));
85
86
87 /** int OSRegex_FreePattern(SRegex *reg) v0.1
88  * Release all the memory created by the compilation/executation
89  * phases.
90  * Returns void.
91  */
92 void OSRegex_FreePattern(OSRegex *reg) __attribute__((nonnull));
93
94
95 /** int OSRegex_FreeSubStrings(OSRegex *reg) v0.1
96  * Release all the memory created to store the sub strings.
97  * Returns void.
98  */
99 void OSRegex_FreeSubStrings(OSRegex *reg) __attribute__((nonnull));
100
101
102 /** int OS_Regex(char *pattern, char *str) v0.4
103  * This function is a wrapper around the compile/execute
104  * functions. It should only be used when the pattern is
105  * only going to be used once.
106  * Returns 1 on success or 0 on failure.
107  */
108 int OS_Regex(const char *pattern, const char *str);
109
110
111
112 /** int OSMatch_Compile(char *pattern, OSMatch *reg, int flags) v0.1
113  * Compile a pattern to be used later.
114  * Allowed flags are:
115  *      - OS_CASE_SENSITIVE
116  * Returns 1 on success or 0 on error.
117  * The error code is set on reg->error.
118  */
119 int OSMatch_Compile(const char *pattern, OSMatch *reg, int flags);
120
121
122 /** int OSMatch_Execute(char *str, int str_len, OSMatch *reg) v0.1
123  * Compare an already compiled pattern with
124  * a not NULL string.
125  * Returns 1 on success or 0 on error.
126  * The error code is set on reg->error.
127  */
128 int OSMatch_Execute(const char *str, size_t str_len, OSMatch *reg)  __attribute__((nonnull(3)));
129
130
131 /** int OSMatch_FreePattern(OSMatch *reg) v0.1
132  * Release all the memory created by the compilation/executation
133  * phases.
134  * Returns void.
135  */
136 void OSMatch_FreePattern(OSMatch *reg) __attribute__((nonnull));
137
138
139 int OS_Match2(const char *pattern, const char *str)  __attribute__((nonnull(2)));
140
141 int OS_Match3(char *pattern, char *str, char* delimiter);
142
143
144 /* OS_WordMatch v0.3:
145  * Searches for  pattern in the string
146  */
147 int OS_WordMatch(const char *pattern, const char *str) __attribute__((nonnull));
148 #define OS_Match OS_WordMatch
149
150
151 /** char **OS_StrBreak(char match, char *str, int size) v0.2
152  * Split a string into multiples pieces, divided by a char "match".
153  * Returns a NULL terminated array on success or NULL on error.
154  */
155 char **OS_StrBreak(char match, const char *str, size_t size);
156
157
158 /** int OS_StrHowClosedMatch(char *str1, char *str2) v0.1
159  * Returns the number of characters that both strings
160  * have in similar (start at the beginning of them).
161  */
162 size_t OS_StrHowClosedMatch(const char *str1, const char *str2);
163
164
165 /** Inline prototypes **/
166
167
168 /** int OS_StrStartsWith(char *str, char *pattern) v0.1
169  * Verifies if a string starts with the provided pattern.
170  * Returns 1 on success or 0 on failure.
171  */
172 int OS_StrStartsWith(const char *str, const char *pattern) __attribute__((nonnull));
173
174
175 /** int OS_StrIsNum(char *str) v0.1
176  * Checks if a specific string is numeric (like "129544")
177  */
178 int OS_StrIsNum(const char *str) __attribute__((nonnull));
179
180
181 /** int isValidChar(char c)
182  * Checks if a specified char is in the following range:
183  * a-z, A-Z, 0-9, _-.
184  */
185 #include "os_regex_maps.h"
186 #define isValidChar(x) (hostname_map[(unsigned char)x])
187
188
189 #endif
190
191
192 /* EOF */