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 /** int OSMatch_Compile(char *pattern, OSMatch *reg, int flags) v0.1
22 * Compile a pattern to be used later.
25 * Returns 1 on success or 0 on error.
26 * The error code is set on reg->error.
28 int OSMatch_Compile(const char *pattern, OSMatch *reg, int flags)
33 int end_of_string = 0;
37 char *new_str_free = NULL;
40 /* Checking for references not initialized */
47 /* Initializing OSRegex structure */
54 /* The pattern can't be null */
57 reg->error = OS_REGEX_PATTERN_NULL;
62 /* Maximum size of the pattern */
63 if(strlen(pattern) > OS_PATTERN_MAXSIZE)
65 reg->error = OS_REGEX_MAXSIZE;
70 /* Duping the pattern for our internal work */
71 new_str = strdup(pattern);
74 reg->error = OS_REGEX_OUTOFMEMORY;
77 new_str_free = new_str;
82 /* Getting the number of sub patterns */
85 /* The pattern must be always lower case if
86 * case sensitive is set
88 if(!(flags & OS_CASE_SENSITIVE))
90 *pt = (char) charmap[(uchar)*pt];
93 /* Number of sub patterns */
106 /* For the last pattern */
108 reg->patterns = (char **) calloc(count +1, sizeof(char *));
109 reg->size = (size_t *) calloc(count +1, sizeof(size_t));
110 reg->match_fp = (int (**)(const char *, const char *, size_t, size_t)) calloc(count +1, sizeof(void *));
113 /* Memory allocation error check */
114 if(!reg->patterns || !reg->size || !reg->match_fp)
116 reg->error = OS_REGEX_OUTOFMEMORY;
121 /* Initializing each sub pattern */
122 for(i = 0; i<=count; i++)
124 reg->patterns[i] = NULL;
125 reg->match_fp[i] = NULL;
131 /* Reassigning pt to the beginning of the string */
135 /* Getting the sub patterns */
138 if((*pt == OR) || (*pt == '\0'))
147 /* Dupping the string */
148 if(*new_str == BEGINREGEX)
149 reg->patterns[i] = strdup(new_str +1);
151 reg->patterns[i] = strdup(new_str);
154 if(!reg->patterns[i])
156 reg->error = OS_REGEX_OUTOFMEMORY;
161 /* If the string has ^ and $ */
162 if((*new_str == BEGINREGEX) && (*(pt -1) == ENDREGEX))
164 reg->match_fp[i] = _os_strcmp;
165 reg->size[i] = strlen(reg->patterns[i]) -1;
166 reg->patterns[i][reg->size[i]] = '\0';
168 else if(strlen(new_str) == 0)
170 reg->match_fp[i] = _os_strmatch;
174 /* String only has $ */
175 else if(*(pt -1) == ENDREGEX)
177 reg->match_fp[i] = _os_strcmp_last;
178 reg->size[i] = strlen(reg->patterns[i]) -1;
179 reg->patterns[i][reg->size[i]] = '\0';
182 /* If string starts with ^, use strncmp */
183 else if(*new_str == BEGINREGEX)
185 reg->match_fp[i] = _os_strncmp;
186 reg->size[i] = strlen(reg->patterns[i]);
189 else if(usstrstr == 1)
191 reg->match_fp[i] = _os_strstr;
192 reg->size[i] = strlen(reg->patterns[i]);
197 reg->match_fp[i] = _OS_Match;
198 reg->size[i] = strlen(reg->patterns[i]);
212 }while(!end_of_string);
228 OSMatch_FreePattern(reg);