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 3) 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);
29 /** int OSMatch_Compile(char *pattern, OSMatch *reg, int flags) v0.1
30 * Compile a pattern to be used later.
33 * Returns 1 on success or 0 on error.
34 * The error code is set on reg->error.
36 int OSMatch_Compile(char *pattern, OSMatch *reg, int flags)
40 int end_of_string = 0;
44 char *new_str_free = NULL;
47 /* Checking for references not initialized */
54 /* Initializing OSRegex structure */
60 /* The pattern can't be null */
63 reg->error = OS_REGEX_PATTERN_NULL;
68 /* Maximum size of the pattern */
69 if(strlen(pattern) > OS_PATTERN_MAXSIZE)
71 reg->error = OS_REGEX_MAXSIZE;
76 /* Duping the pattern for our internal work */
77 new_str = strdup(pattern);
80 reg->error = OS_REGEX_OUTOFMEMORY;
83 new_str_free = new_str;
87 /* Getting the number of sub patterns */
90 /* The pattern must be always lower case if
91 * case sensitive is set
93 if(!(flags & OS_CASE_SENSITIVE))
95 *pt = charmap[(uchar)*pt];
98 /* Number of sub patterns */
107 /* For the last pattern */
109 reg->patterns = calloc(count +1, sizeof(char *));
110 reg->size = calloc(count +1, sizeof(int));
111 reg->match_fp = calloc(count +1, sizeof(void *));
114 /* Memory allocation error check */
115 if(!reg->patterns || !reg->size || !reg->match_fp)
117 reg->error = OS_REGEX_OUTOFMEMORY;
122 /* Initializing each sub pattern */
123 for(i = 0; i<=count; i++)
125 reg->patterns[i] = NULL;
126 reg->match_fp[i] = NULL;
132 /* Reassigning pt to the beginning of the string */
136 /* Getting the sub patterns */
139 if((*pt == OR) || (*pt == '\0'))
148 /* Dupping the string */
149 if(*new_str == BEGINREGEX)
150 reg->patterns[i] = strdup(new_str +1);
152 reg->patterns[i] = strdup(new_str);
155 if(!reg->patterns[i])
157 reg->error = OS_REGEX_OUTOFMEMORY;
162 /* If the string has ^ and $ */
163 if((*new_str == BEGINREGEX) && (*(pt -1) == ENDREGEX))
165 reg->match_fp[i] = _os_strcmp;
166 reg->size[i] = strlen(reg->patterns[i]) -1;
167 reg->patterns[i][reg->size[i]] = '\0';
169 else if(strlen(new_str) == 0)
171 reg->match_fp[i] = _os_strmatch;
175 /* String only has $ */
176 else if(*(pt -1) == ENDREGEX)
178 reg->match_fp[i] = _os_strcmp_last;
179 reg->size[i] = strlen(reg->patterns[i]) -1;
180 reg->patterns[i][reg->size[i]] = '\0';
183 /* If string starts with ^, use strncmp */
184 else if(*new_str == BEGINREGEX)
186 reg->match_fp[i] = _os_strncmp;
187 reg->size[i] = strlen(reg->patterns[i]);
191 reg->match_fp[i] = _OS_Match;
192 reg->size[i] = strlen(reg->patterns[i]);
206 }while(!end_of_string);
222 OSMatch_FreePattern(reg);