1 /* $OSSEC, os_regex.h, v0.3, 2005/04/05, Daniel B. Cid$ */
3 /* Copyright (C) 2009 Trend Micro Inc.
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 3) as published by the FSF - Free Software
12 /* See README for details */
19 /* OSRegex_Compile flags */
20 #define OS_RETURN_SUBSTRING 0000200
21 #define OS_CASE_SENSITIVE 0000400
24 /* Pattern maximum size */
25 #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
39 /* OSRegex structure */
40 typedef struct _OSRegex
51 /* OSmatch structure */
52 typedef struct _OSMatch
57 int (**match_fp)(char *str, char *str2, int str_len, int size);
64 /** int OSRegex_Compile(char *pattern, OSRegex *reg, int flags) v0.1
65 * Compile a regular expression to be used later.
68 * - OS_RETURN_SUBSTRING
69 * Returns 1 on success or 0 on error.
70 * The error code is set on reg->error.
72 int OSRegex_Compile(char *pattern, OSRegex *reg, int flags);
75 /** char *OSRegex_Execute(char *str, OSRegex *reg) v0.1
76 * Compare an already compiled regular expression with
78 * Returns end of str on success or NULL on error.
79 * The error code is set on reg->error.
81 char *OSRegex_Execute(char *str, OSRegex *reg);
84 /** int OSRegex_FreePattern(SRegex *reg) v0.1
85 * Release all the memory created by the compilation/executation
89 void OSRegex_FreePattern(OSRegex *reg);
92 /** int OSRegex_FreeSubStrings(OSRegex *reg) v0.1
93 * Release all the memory created to store the sub strings.
96 void OSRegex_FreeSubStrings(OSRegex *reg);
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.
105 int OS_Regex(char *pattern, char *str);
109 /** int OSMatch_Compile(char *pattern, OSMatch *reg, int flags) v0.1
110 * Compile a pattern to be used later.
112 * - OS_CASE_SENSITIVE
113 * Returns 1 on success or 0 on error.
114 * The error code is set on reg->error.
116 int OSMatch_Compile(char *pattern, OSMatch *reg, int flags);
119 /** int OSMatch_Execute(char *str, int str_len, OSMatch *reg) v0.1
120 * Compare an already compiled pattern with
122 * Returns 1 on success or 0 on error.
123 * The error code is set on reg->error.
125 int OSMatch_Execute(char *str, int str_len, OSMatch *reg);
128 /** int OSMatch_FreePattern(OSMatch *reg) v0.1
129 * Release all the memory created by the compilation/executation
133 void OSMatch_FreePattern(OSMatch *reg);
136 int OS_Match2(char *pattern, char *str);
139 /* OS_WordMatch v0.3:
140 * Searches for pattern in the string
142 int OS_WordMatch(char *pattern, char *str);
143 #define OS_Match OS_WordMatch
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.
150 char **OS_StrBreak(char match, char *str, int size);
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).
157 int OS_StrHowClosedMatch(char *str1, char *str2);
160 /** Inline prototypes **/
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.
168 #define startswith(x,y) (strncmp(x,y,strlen(y)) == 0?1:0)
169 #define OS_StrStartsWith startswith
172 /** int OS_StrIsNum(char *str) v0.1
173 * Checks if a specific string is numeric (like "129544")
175 int OS_StrIsNum(char *str);
178 /** int isValidChar(char c)
179 * Checks if a specified char is in the following range:
182 #include "os_regex_maps.h"
183 #define isValidChar(x) (hostname_map[(unsigned char)x])