Imported Upstream version 2.7
[ossec-hids.git] / src / os_regex / os_regex_match.c
1 /*   $OSSEC, os_regex_match.c, v0.3, 2005/06/09, Daniel B. Cid$   */
2
3 /* Copyright (C) 2009 Trend Micro Inc.
4  * All right reserved.
5  *
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
9  * Foundation
10  */
11
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <ctype.h>
17 #include "os_regex_internal.h"
18
19 /* Algorithm:
20  *       Go as faster as you can :)
21  *
22  * Supports:
23  *      '|' to separate multiple OR patterns
24  *      '^' to match the begining of a string
25  */
26
27
28 /** Prototypes **/
29 int _InternalMatch(char *pattern, char *str,int count);
30
31
32 /* OS_WordMatch v0.3:
33  * Searches for  pattern in the string
34  */
35 int OS_WordMatch(char *pattern, char *str)
36 {
37     int count = 0;
38
39     if(*pattern == '\0')
40         return(FALSE);
41
42     do
43     {
44         if(pattern[count] == '|')
45         {
46             /* If we match '|' , search with
47              * we have so far.
48              */
49             if(_InternalMatch(pattern, str, count))
50             {
51                 return(TRUE);
52             }
53             else
54             {
55                 pattern += count+1;
56                 count = 0;
57                 continue;
58             }
59         }
60
61         count++;
62
63     }while(pattern[count] != '\0');
64
65     /* Last check until end of string */
66     return(_InternalMatch(pattern, str,count));
67 }
68
69 /* Internal match function */
70 int _InternalMatch(char *pattern, char *str, int pattern_size)
71 {
72     uchar *pt = (uchar *)pattern;
73     uchar *st = (uchar *)str;
74
75     uchar last_char = pattern[pattern_size];
76
77
78     /* Return true for some odd expressions */
79     if(*pattern == '\0')
80         return(TRUE);
81
82
83     /* If '^' specified, just do a strncasecmp */
84     else if(*pattern == '^')
85     {
86         pattern++;
87         pattern_size --;
88
89         /* Compare two string */
90         if(strncasecmp(pattern,str,pattern_size) == 0)
91             return(TRUE);
92         return(FALSE);
93     }
94
95
96     /* Null line */
97     else if(*st == '\0')
98         return(FALSE);
99
100
101     /* Look to match the first pattern */
102     do
103     {
104         /* Match */
105         if(charmap[*st] == charmap[*pt])
106         {
107             str = (char *)st++;
108             pt++;
109
110             while(*pt != last_char)
111             {
112                 if(*st == '\0')
113                     return(FALSE);
114
115                 else if(charmap[*pt] != charmap[*st])
116                     goto error;
117
118                 st++;pt++;
119             }
120
121             /* Return here if pt == last_char */
122             return(TRUE);
123
124             error:
125                 st = (uchar *)str;
126                 pt = (uchar *)pattern;
127
128         }
129
130         st++;
131     }while(*st != '\0');
132
133     return(FALSE);
134 }
135 /* EOF */