1 /* $OSSEC, os_regex_match.c, v0.3, 2005/06/09, 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"
22 * Go as faster as you can :)
25 * '|' to separate multiple OR patterns
26 * '^' to match the begining of a string
31 static int _InternalMatch(const char *pattern, const char *str,size_t count) __attribute__((nonnull));
35 * Searches for pattern in the string
37 int OS_WordMatch(const char *pattern, const char *str)
46 if(pattern[count] == '|')
48 /* If we match '|' , search with
51 if(_InternalMatch(pattern, str, count))
65 }while(pattern[count] != '\0');
67 /* Last check until end of string */
68 return(_InternalMatch(pattern, str,count));
71 /* Internal match function */
72 static int _InternalMatch(const char *pattern, const char *str, size_t pattern_size)
74 const uchar *pt = (const uchar *)pattern;
75 const uchar *st = (const uchar *)str;
77 const uchar last_char = (const uchar) pattern[pattern_size];
80 /* Return true for some odd expressions */
85 /* If '^' specified, just do a strncasecmp */
86 else if(*pattern == '^')
91 /* Compare two string */
92 if(strncasecmp(pattern,str,pattern_size) == 0)
103 /* Look to match the first pattern */
107 if(charmap[*st] == charmap[*pt])
109 str = (const char *)st++;
112 while(*pt != last_char)
117 else if(charmap[*pt] != charmap[*st])
123 /* Return here if pt == last_char */
127 st = (const uchar *)str;
128 pt = (const uchar *)pattern;