novi upstream verzije 2.8.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 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(const char *pattern, const char *str, size_t str_len, size_t size)
23 {
24     size_t i = 0,j;
25     const char *pt = pattern;
26
27     if(str_len < size)
28         return(FALSE);
29
30     size = str_len - size;
31
32     /* Look to match the first pattern */
33     do
34     {
35         /* Match */
36         if(charmap[(uchar)str[i]] == *pt)
37         {
38             pt++;
39             j = i+1;
40
41             while(*pt != '\0')
42             {
43                 if(str[j] == '\0')
44                     return(FALSE);
45
46                 else if(*pt != charmap[(uchar)str[j]])
47                 {
48                     pt = pattern;
49                     goto nnext;
50                 }
51                 j++;pt++;
52             }
53             return(TRUE);
54             nnext:
55             continue;
56         }
57     }while(++i <= size);
58
59     return(FALSE);
60 }
61
62
63 /** Internal matching **/
64 int _os_strncmp(const char *pattern, const char *str, __attribute__((unused)) size_t str_len, size_t size)
65 {
66     if(strncasecmp(pattern, str, size) == 0)
67         return(TRUE);
68
69     return(FALSE);
70 }
71
72 /** Internal matching **/
73 int _os_strcmp(const char *pattern, const char *str, __attribute__((unused)) size_t str_len, __attribute__((unused)) size_t size)
74 {
75     if(strcasecmp(pattern, str) == 0)
76         return(TRUE);
77
78     return(FALSE);
79 }
80
81 int _os_strmatch(__attribute__((unused)) const char *pattern, __attribute__((unused)) const char *str,
82         __attribute__((unused)) size_t str_len, __attribute__((unused)) size_t size)
83 {
84     return(TRUE);
85 }
86
87 int _os_strstr(const char *pattern, const char *str, __attribute__((unused)) size_t str_len, __attribute__((unused)) size_t size)
88 {
89     if(strstr(str, pattern) != NULL)
90     {
91         return(TRUE);
92     }
93     return(FALSE);
94 }
95
96
97 /** Internal matching **/
98 int _os_strcmp_last(const char *pattern, const char *str, size_t str_len, size_t size)
99 {
100     /* Size of the string must be bigger */
101     if(str_len < size)
102         return(FALSE);
103
104     if(strcasecmp(pattern, str + (str_len - size)) == 0)
105         return(TRUE);
106
107     return(FALSE);
108 }
109
110
111 /** int OSMatch_Execute(char *str, int str_len, OSMatch *reg) v0.1
112  * Compare an already compiled pattern with
113  * a not NULL string.
114  * Returns 1 on success or 0 on error.
115  * The error code is set on reg->error.
116  */
117 int OSMatch_Execute(const char *str, size_t str_len, OSMatch *reg)
118 {
119     short int i = 0;
120
121     /* The string can't be NULL */
122     if(str == NULL)
123     {
124         reg->error = OS_REGEX_STR_NULL;
125         return(0);
126     }
127
128
129     /* Looping on all sub patterns */
130     while(reg->patterns[i])
131     {
132         if(reg->match_fp[i](reg->patterns[i],
133                             str,
134                             str_len,
135                             reg->size[i]) == TRUE)
136         {
137             return(1);
138         }
139         i++;
140     }
141
142     return(0);
143 }
144
145
146 /* EOF */