comparator.c
5.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/* comparator.c -- comparator functions
* Larry Greenfield
* $Id$
*/
/***********************************************************
Copyright 1999 by Carnegie Mellon University
All Rights Reserved
Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee is hereby granted,
provided that the above copyright notice appear in all copies and that
both that copyright notice and this permission notice appear in
supporting documentation, and that the name of Carnegie Mellon
University not be used in advertising or publicity pertaining to
distribution of the software without specific, written prior
permission.
CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE FOR
ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
******************************************************************/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <fnmatch.h>
#ifdef HAVE_STRINGS_H
# include <strings.h>
#endif
#include "comparator.h"
#include "tree.h"
#include "sieve-gram.h"
/* --- i;octet comparators --- */
/* just compare the two; these should be NULL terminated */
static int octet_is(const char *pat, const char *text)
{
int sl;
sl = strlen(pat);
return (sl == strlen(text)) && !memcmp(pat, text, sl);
}
/* we implement boyer-moore for hell of it, since this is probably
not very useful for sieve */
#if 0
int boyer_moore(char *pat, char *text)
{
int i, j; /* indexes */
int M = strlen(pat); /* length of pattern */
int N = strlen(text); /* length of text */
int skip[256]; /* table of how much to skip, based on each character */
/* initialize skip table */
for (i = 0; i < 256; i++)
skip[i] = M;
for (i = 0; i < M; i++)
skip[(int) pat[i]] = M-i-1;
/* look for pat in text */
i = j = M-1;
do {
if (pat[j] == text[i]) {
i--;
j--;
} else {
if (M-j > skip[(int) text[i]]) {
i = i + M - j;
} else {
i = i + skip[(int) text[i]];
}
j = M-1;
}
} while (!((j < 0) || (i >= N)));
/* i+1 is the position of the match if i < N */
return (i < N) ? 1 : 0;
}
#endif
/* we do a brute force attack */
static int octet_contains(const char *pat, const char *text)
{
return (strstr(text, pat) != NULL);
}
static int octet_matches(const char *pat, const char *text)
{
return !fnmatch(pat, text, 0);
}
#ifdef ENABLE_REGEX
static int octet_regex(const char *pat, const char *text)
{
return (!regexec((regex_t *) pat, text, 0, NULL, 0));
}
#endif
/* --- i;ascii-casemap comparators --- */
static int ascii_casemap_is(const char *pat, const char *text)
{
int sl;
sl = strlen(pat);
return (sl == strlen(text)) && !strncasecmp(pat, text, sl);
}
/* sheer brute force */
static int ascii_casemap_contains(const char *pat, const char *text)
{
int N = strlen(text);
int M = strlen(pat);
int i, j;
i = 0, j = 0;
while ((j < M) && (i < N)) {
if (toupper(text[i]) == toupper(pat[j])) {
i++; j++;
} else {
i = i - j + 1;
j = 0;
}
}
return (j == M); /* we found a match! */
}
static int ascii_casemap_matches(const char *pat, const char *text)
{
int ret;
char *p, *t;
int i;
/* sigh, i'll just make local copies of these guys */
p = strdup(pat); t = strdup(text);
for (i = 0; p[i] != '\0'; i++)
p[i] = toupper(p[i]);
for (i = 0; t[i] != '\0'; i++)
t[i] = toupper(t[i]);
ret = !fnmatch(p, t, 0);
free(p); free(t);
return ret;
}
/* i;ascii-numeric; only supports "is"
equality: numerically equal, or both not numbers */
static int ascii_numeric_is(const char *pat, const char *text)
{
if (isdigit((int) *pat)) {
if (isdigit((int) *text)) {
return (atoi(pat) == atoi(text));
} else {
return 0;
}
} else if (isdigit((int) *text)) return 0;
else return 1; /* both not digits */
}
comparator_t *lookup_comp(const char *comp, int mode)
{
comparator_t *ret;
ret = NULL;
if (!strcmp(comp, "i;octet")) {
switch (mode) {
case IS:
ret = &octet_is;
break;
case CONTAINS:
ret = &octet_contains;
break;
case MATCHES:
ret = &octet_matches;
break;
#ifdef ENABLE_REGEX
case REGEX:
ret = &octet_regex;
break;
#endif
}
} else if (!strcmp(comp, "i;ascii-casemap")) {
switch (mode) {
case IS:
ret = &ascii_casemap_is;
break;
case CONTAINS:
ret = &ascii_casemap_contains;
break;
case MATCHES:
ret = &ascii_casemap_matches;
break;
#ifdef ENABLE_REGEX
case REGEX:
/* the ascii-casemap destinction is made during
the compilation of the regex in verify_regex() */
ret = &octet_regex;
break;
#endif
}
} else if (!strcmp(comp, "i;ascii-numeric")) {
switch (mode) {
case IS:
ret = &ascii_numeric_is;
break;
}
}
return ret;
}