Imported Upstream version 2.3
[ossec-hids.git] / src / os_regex / os_match_execute.c
1 /*   $OSSEC, os_match_execute.c, v0.1, 2006/04/18, 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 3) as published by the FSF - Free Software
9  * Foundation
10  */
11
12
13 #include <stdio.h>
14 #include <string.h>
15 #include <stdlib.h>
16
17 #include "os_regex.h"
18 #include "os_regex_internal.h"
19
20
21 /** Internal matching **/
22 int _OS_Match(char *pattern, char *str, int str_len, int size)
23 {
24     int i = 0,j;
25     char *pt = pattern;
26
27     size = str_len - size;
28
29     /* Look to match the first pattern */
30     do
31     {
32         /* Match */
33         if(charmap[(uchar)str[i]] == *pt)
34         {
35             pt++;
36             j = i+1;
37
38             while(*pt != '\0')
39             {
40                 if(str[j] == '\0')
41                     return(FALSE);
42                     
43                 else if(*pt != charmap[(uchar)str[j]])
44                 {
45                     pt = pattern;           
46                     goto nnext;
47                 }
48                 j++;pt++;
49             }
50             return(TRUE);
51             nnext:
52             continue;
53         }
54     }while(++i <= size);
55
56     return(FALSE);
57 }
58
59
60 /** Internal matching **/
61 int _os_strncmp(char *pattern, char *str, int str_len, int size)
62 {
63     if(strncasecmp(pattern, str, size) == 0)
64         return(TRUE);
65         
66     return(FALSE);    
67 }
68
69 /** Internal matching **/
70 int _os_strcmp(char *pattern, char *str, int str_len, int size)
71 {
72     if(strcasecmp(pattern, str) == 0)
73         return(TRUE);
74     
75     return(FALSE);    
76 }
77
78 int _os_strmatch(char *pattern, char *str, int str_len, int size)
79 {
80     return(TRUE);
81 }
82
83
84 /** Internal matching **/
85 int _os_strcmp_last(char *pattern, char *str, int str_len, int size)
86 {
87     /* Size of the string must be bigger */
88     if((str_len - size) < 0)
89         return(FALSE);
90     
91     if(strcasecmp(pattern, str + (str_len - size)) == 0)
92         return(TRUE);
93     
94     return(FALSE);            
95 }
96
97
98 /** int OSMatch_Execute(char *str, int str_len, OSMatch *reg) v0.1
99  * Compare an already compiled pattern with
100  * a not NULL string.
101  * Returns 1 on success or 0 on error.
102  * The error code is set on reg->error.
103  */
104 int OSMatch_Execute(char *str, int str_len, OSMatch *reg)
105 {
106     short int i = 0;
107     
108     /* The string can't be NULL */
109     if(str == NULL)
110     {
111         reg->error = OS_REGEX_STR_NULL;
112         return(0);
113     }
114
115
116     /* Looping on all sub patterns */
117     while(reg->patterns[i])
118     {
119         if(reg->match_fp[i](reg->patterns[i], 
120                             str, 
121                             str_len, 
122                             reg->size[i]) == TRUE)
123         {
124             return(1);
125         }
126         i++;
127     }
128
129     return(0);
130 }    
131
132
133 /* EOF */