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 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
|
/* comparator.c -- comparator functions
* Larry Greenfield
* $Id: comparator.c,v 1.17 2005/05/08 14:35:58 ken3 Exp $
*/
/***********************************************************
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 "comparator.h"
#include "tree.h"
#include "sieve.h"
#include "bytecode.h"
/*!!! uses B_CONTAINS not CONTAINS, etc, only works with bytecode*/
extern int strcasecmp(const char *, const char *);
typedef int (*compare_t)(const void *, const void *);
/* --- relational comparators --- */
/* these are generic wrappers in which 'rock' is the compare function */
static int rel_eq(const char *text, const char *pat, void *rock)
{
compare_t compar = (compare_t) rock;
return (compar(text, pat) == 0);
}
static int rel_ne(const char *text, const char *pat, void *rock)
{
compare_t compar = (compare_t) rock;
return (compar(text, pat) != 0);
}
static int rel_gt(const char *text, const char *pat, void *rock)
{
compare_t compar = (compare_t) rock;
return (compar(text, pat) > 0);
}
static int rel_ge(const char *text, const char *pat, void *rock)
{
compare_t compar = (compare_t) rock;
return (compar(text, pat) >= 0);
}
static int rel_lt(const char *text, const char *pat, void *rock)
{
compare_t compar = (compare_t) rock;
return (compar(text, pat) < 0);
}
static int rel_le(const char *text, const char *pat, void *rock)
{
compare_t compar = (compare_t) rock;
return (compar(text, pat) <= 0);
}
/* --- i;octet comparators --- */
/* just compare the two; these should be NULL terminated */
static int octet_cmp(const char *text, const char *pat)
{
size_t sl;
int r;
sl = strlen(text) < strlen(pat) ? strlen(text) : strlen(pat);
r = memcmp(text, pat, sl);
if (r == 0)
return (strlen(text) - strlen(pat));
else
return r;
}
/* we implement boyer-moore for hell of it, since this is probably
not very useful for sieve */
#if 0
int boyer_moore(char *text, char *pat)
{
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 *text, const char *pat,
void *rock __attribute__((unused)))
{
return (strstr(text, pat) != NULL);
}
static int octet_matches_(const char *text, const char *pat, int casemap)
{
const char *p;
const char *t;
char c;
t = text;
p = pat;
for (;;) {
if (*p == '\0') {
/* ran out of pattern */
return (*t == '\0');
}
c = *p++;
switch (c) {
case '?':
if (*t == '\0') {
return 0;
}
t++;
break;
case '*':
while (*p == '*' || *p == '?') {
if (*p == '?') {
/* eat the character now */
if (*t == '\0') {
return 0;
}
t++;
}
/* coalesce into a single wildcard */
p++;
}
if (*p == '\0') {
/* wildcard at end of string, any remaining text is ok */
return 1;
}
while (*t != '\0') {
/* recurse */
if (octet_matches_(t, p, casemap)) return 1;
t++;
}
case '\\':
p++;
/* falls through */
default:
if (casemap && (toupper(c) == toupper(*t))) {
t++;
} else if (!casemap && (c == *t)) {
t++;
} else {
/* literal char doesn't match */
return 0;
}
}
}
/* never reaches */
abort();
}
static int octet_matches(const char *text, const char *pat,
void *rock __attribute__((unused)))
{
return octet_matches_(text, pat, 0);
}
#ifdef ENABLE_REGEX
static int octet_regex(const char *text, const char *pat,
void *rock __attribute__((unused)))
{
return (!regexec((regex_t *) pat, text, 0, NULL, 0));
}
#endif
/* --- i;ascii-casemap comparators --- */
/* use strcasecmp() as the compare function */
/* sheer brute force */
static int ascii_casemap_contains(const char *text, const char *pat,
void *rock __attribute__((unused)))
{
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 *text, const char *pat,
void *rock __attribute__((unused)))
{
return octet_matches_(text, pat, 1);
}
/* i;ascii-numeric; only supports relational tests
*
* A \ B number not-num
* number A ? B B > A
* not-num A > B A == B
*/
/* From RFC 2244:
*
* The i;ascii-numeric comparator interprets strings as decimal
* positive integers represented as US-ASCII digits. All values
* which do not begin with a US-ASCII digit are considered equal
* with an ordinal value higher than all non-NIL single-valued
* attributes. Otherwise, all US-ASCII digits (octet values
* 0x30 to 0x39) are interpreted starting from the beginning of
* the string to the first non-digit or the end of the string.
*/
static int ascii_numeric_cmp(const char *text, const char *pat)
{
unsigned text_digit_len;
unsigned pat_digit_len;
if (isdigit((int) *pat)) {
if (isdigit((int) *text)) {
/* Count how many digits each string has */
for (text_digit_len = 0;
isdigit((int) text[text_digit_len]);
text_digit_len++);
for (pat_digit_len = 0;
isdigit((int) pat[pat_digit_len]);
pat_digit_len++);
if (text_digit_len < pat_digit_len) {
/* Pad "text" with leading 0s */
while (pat_digit_len > text_digit_len) {
/* "text" can only be less or equal to "pat" */
if ('0' < *pat) {
return (-1);
}
pat++;
pat_digit_len--;
}
} else if (text_digit_len > pat_digit_len) {
/* Pad "pad" with leading 0s */
while (text_digit_len > pat_digit_len) {
/* "pad" can only be greater or equal to "text" */
if (*text > '0') {
return 1;
}
text++;
text_digit_len--;
}
}
/* CLAIM: If we here, we have two non-empty digital suffixes
of equal length */
while (text_digit_len > 0) {
if (*text < *pat) {
return -1;
} else if (*text > *pat) {
return 1;
}
/* Characters are equal, carry on */
text++;
pat++;
text_digit_len--;
}
return (0);
} else {
return 1;
}
} else if (isdigit((int) *text)) {
return -1;
} else {
return 0; /* both not digits */
}
}
static comparator_t *lookup_rel(int relation)
{
comparator_t *ret;
ret = NULL;
switch (relation)
{
case B_EQ:
ret = &rel_eq;
break;
case B_NE:
ret = &rel_ne;
break;
case B_GT:
ret = &rel_gt;
break;
case B_GE:
ret = &rel_ge;
break;
case B_LT:
ret = &rel_lt;
break;
case B_LE:
ret = &rel_le;
}
return ret;
}
comparator_t *lookup_comp(int comp, int mode, int relation,
void **comprock)
{
comparator_t *ret;
ret = NULL;
*comprock = NULL;
#if VERBOSE
printf("comp%d mode%d relat%d \n", comp, mode, relation);
#endif
switch (comp)
{
case B_OCTET:
switch (mode) {
case B_IS:
ret = &rel_eq;
*comprock = (void **) &octet_cmp;
break;
case B_CONTAINS:
ret = &octet_contains;
break;
case B_MATCHES:
ret = &octet_matches;
break;
#ifdef ENABLE_REGEX
case B_REGEX:
ret = &octet_regex;
break;
#endif
case B_VALUE:
ret = lookup_rel(relation);
*comprock = (void **) &octet_cmp;
break;
}
break; /*end of octet */
case B_ASCIICASEMAP:
switch (mode) {
case B_IS:
ret = &rel_eq;
*comprock = (void **) &strcasecmp;
break;
case B_CONTAINS:
ret = &ascii_casemap_contains;
break;
case B_MATCHES:
ret = &ascii_casemap_matches;
break;
#ifdef ENABLE_REGEX
case B_REGEX:
/* the ascii-casemap destinction is made during
the compilation of the regex in verify_regex() */
ret = &octet_regex;
break;
#endif
case B_VALUE:
ret = lookup_rel(relation);
*comprock = &strcasecmp;
break;
}
break;/*end of ascii casemap */
case B_ASCIINUMERIC:
switch (mode) {
case B_IS:
ret = &rel_eq;
*comprock = (void **) &ascii_numeric_cmp;
break;
case B_COUNT:
case B_VALUE:
ret = lookup_rel(relation);
*comprock = (void **) &ascii_numeric_cmp;
break;
}
break;
}
return ret;
}
|