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 2) as published by the FSF - Free Software
12 /* See README for details */
22 /* OSRegex_Compile flags */
23 #define OS_RETURN_SUBSTRING 0000200
24 #define OS_CASE_SENSITIVE 0000400
27 /* Pattern maximum size */
28 #define OS_PATTERN_MAXSIZE 2048
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
42 /* OSRegex structure */
43 typedef struct _OSRegex
49 const char ***prts_closure;
50 const char ***prts_str;
54 /* OSmatch structure */
55 typedef struct _OSMatch
60 int (**match_fp)(const char *str, const char *str2, size_t str_len, size_t size);
67 /** int OSRegex_Compile(char *pattern, OSRegex *reg, int flags) v0.1
68 * Compile a regular expression to be used later.
71 * - OS_RETURN_SUBSTRING
72 * Returns 1 on success or 0 on error.
73 * The error code is set on reg->error.
75 int OSRegex_Compile(const char *pattern, OSRegex *reg, int flags);
78 /** char *OSRegex_Execute(char *str, OSRegex *reg) v0.1
79 * Compare an already compiled regular expression with
81 * Returns end of str on success or NULL on error.
82 * The error code is set on reg->error.
84 const char *OSRegex_Execute(const char *str, OSRegex *reg) __attribute__((nonnull(2)));
87 /** int OSRegex_FreePattern(SRegex *reg) v0.1
88 * Release all the memory created by the compilation/executation
92 void OSRegex_FreePattern(OSRegex *reg) __attribute__((nonnull));
95 /** int OSRegex_FreeSubStrings(OSRegex *reg) v0.1
96 * Release all the memory created to store the sub strings.
99 void OSRegex_FreeSubStrings(OSRegex *reg) __attribute__((nonnull));
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.
108 int OS_Regex(const char *pattern, const char *str);
112 /** int OSMatch_Compile(char *pattern, OSMatch *reg, int flags) v0.1
113 * Compile a pattern to be used later.
115 * - OS_CASE_SENSITIVE
116 * Returns 1 on success or 0 on error.
117 * The error code is set on reg->error.
119 int OSMatch_Compile(const char *pattern, OSMatch *reg, int flags);
122 /** int OSMatch_Execute(char *str, int str_len, OSMatch *reg) v0.1
123 * Compare an already compiled pattern with
125 * Returns 1 on success or 0 on error.
126 * The error code is set on reg->error.
128 int OSMatch_Execute(const char *str, size_t str_len, OSMatch *reg) __attribute__((nonnull(3)));
131 /** int OSMatch_FreePattern(OSMatch *reg) v0.1
132 * Release all the memory created by the compilation/executation
136 void OSMatch_FreePattern(OSMatch *reg) __attribute__((nonnull));
139 int OS_Match2(const char *pattern, const char *str) __attribute__((nonnull(2)));
141 int OS_Match3(char *pattern, char *str, char* delimiter);
144 /* OS_WordMatch v0.3:
145 * Searches for pattern in the string
147 int OS_WordMatch(const char *pattern, const char *str) __attribute__((nonnull));
148 #define OS_Match OS_WordMatch
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.
155 char **OS_StrBreak(char match, const char *str, size_t size);
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).
162 size_t OS_StrHowClosedMatch(const char *str1, const char *str2);
165 /** Inline prototypes **/
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.
172 int OS_StrStartsWith(const char *str, const char *pattern) __attribute__((nonnull));
175 /** int OS_StrIsNum(char *str) v0.1
176 * Checks if a specific string is numeric (like "129544")
178 int OS_StrIsNum(const char *str) __attribute__((nonnull));
181 /** int isValidChar(char c)
182 * Checks if a specified char is in the following range:
185 #include "os_regex_maps.h"
186 #define isValidChar(x) (hostname_map[(unsigned char)x])