Imported Upstream version 2.7
[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 2) 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 int _os_strstr(char *pattern, char *str, int str_len, int size)
84 {
85     if(strstr(str, pattern) != NULL)
86     {
87         return(TRUE);
88     }
89     return(FALSE);
90 }
91
92
93 /** Internal matching **/
94 int _os_strcmp_last(char *pattern, char *str, int str_len, int size)
95 {
96     /* Size of the string must be bigger */
97     if((str_len - size) < 0)
98         return(FALSE);
99
100     if(strcasecmp(pattern, str + (str_len - size)) == 0)
101         return(TRUE);
102
103     return(FALSE);
104 }
105
106
107 /** int OSMatch_Execute(char *str, int str_len, OSMatch *reg) v0.1
108  * Compare an already compiled pattern with
109  * a not NULL string.
110  * Returns 1 on success or 0 on error.
111  * The error code is set on reg->error.
112  */
113 int OSMatch_Execute(char *str, int str_len, OSMatch *reg)
114 {
115     short int i = 0;
116
117     /* The string can't be NULL */
118     if(str == NULL)
119     {
120         reg->error = OS_REGEX_STR_NULL;
121         return(0);
122     }
123
124
125     /* Looping on all sub patterns */
126     while(reg->patterns[i])
127     {
128         if(reg->match_fp[i](reg->patterns[i],
129                             str,
130                             str_len,
131                             reg->size[i]) == TRUE)
132         {
133             return(1);
134         }
135         i++;
136     }
137
138     return(0);
139 }
140
141
142 /* EOF */