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