1 /* $OSSEC, os_match_compile.c, v0.1, 2006/04/17, 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
19 #include "os_regex_internal.h"
21 /* Prototype fo the _OsMatch */
22 int _OS_Match(char *pattern, char *str, int str_len, int size);
23 int _os_strncmp(char *pattern, char *str, int str_len, int size);
24 int _os_strcmp_last(char *pattern, char *str, int str_len, int size);
25 int _os_strcmp(char *pattern, char *str, int str_len, int size);
26 int _os_strmatch(char *pattern, char *str, int str_len, int size);
27 int _os_strstr(char *pattern, char *str, int str_len, int size);
30 /** int OSMatch_Compile(char *pattern, OSMatch *reg, int flags) v0.1
31 * Compile a pattern to be used later.
34 * Returns 1 on success or 0 on error.
35 * The error code is set on reg->error.
37 int OSMatch_Compile(char *pattern, OSMatch *reg, int flags)
42 int end_of_string = 0;
46 char *new_str_free = NULL;
49 /* Checking for references not initialized */
56 /* Initializing OSRegex structure */
62 /* The pattern can't be null */
65 reg->error = OS_REGEX_PATTERN_NULL;
70 /* Maximum size of the pattern */
71 if(strlen(pattern) > OS_PATTERN_MAXSIZE)
73 reg->error = OS_REGEX_MAXSIZE;
78 /* Duping the pattern for our internal work */
79 new_str = strdup(pattern);
82 reg->error = OS_REGEX_OUTOFMEMORY;
85 new_str_free = new_str;
90 /* Getting the number of sub patterns */
93 /* The pattern must be always lower case if
94 * case sensitive is set
96 if(!(flags & OS_CASE_SENSITIVE))
98 *pt = charmap[(uchar)*pt];
101 /* Number of sub patterns */
114 /* For the last pattern */
116 reg->patterns = calloc(count +1, sizeof(char *));
117 reg->size = calloc(count +1, sizeof(int));
118 reg->match_fp = calloc(count +1, sizeof(void *));
121 /* Memory allocation error check */
122 if(!reg->patterns || !reg->size || !reg->match_fp)
124 reg->error = OS_REGEX_OUTOFMEMORY;
129 /* Initializing each sub pattern */
130 for(i = 0; i<=count; i++)
132 reg->patterns[i] = NULL;
133 reg->match_fp[i] = NULL;
139 /* Reassigning pt to the beginning of the string */
143 /* Getting the sub patterns */
146 if((*pt == OR) || (*pt == '\0'))
155 /* Dupping the string */
156 if(*new_str == BEGINREGEX)
157 reg->patterns[i] = strdup(new_str +1);
159 reg->patterns[i] = strdup(new_str);
162 if(!reg->patterns[i])
164 reg->error = OS_REGEX_OUTOFMEMORY;
169 /* If the string has ^ and $ */
170 if((*new_str == BEGINREGEX) && (*(pt -1) == ENDREGEX))
172 reg->match_fp[i] = _os_strcmp;
173 reg->size[i] = strlen(reg->patterns[i]) -1;
174 reg->patterns[i][reg->size[i]] = '\0';
176 else if(strlen(new_str) == 0)
178 reg->match_fp[i] = _os_strmatch;
182 /* String only has $ */
183 else if(*(pt -1) == ENDREGEX)
185 reg->match_fp[i] = _os_strcmp_last;
186 reg->size[i] = strlen(reg->patterns[i]) -1;
187 reg->patterns[i][reg->size[i]] = '\0';
190 /* If string starts with ^, use strncmp */
191 else if(*new_str == BEGINREGEX)
193 reg->match_fp[i] = _os_strncmp;
194 reg->size[i] = strlen(reg->patterns[i]);
197 else if(usstrstr == 1)
199 reg->match_fp[i] = _os_strstr;
200 reg->size[i] = strlen(reg->patterns[i]);
205 reg->match_fp[i] = _OS_Match;
206 reg->size[i] = strlen(reg->patterns[i]);
220 }while(!end_of_string);
236 OSMatch_FreePattern(reg);