X-Git-Url: http://www.privoxy.org/gitweb/?p=privoxy.git;a=blobdiff_plain;f=miscutil.c;h=4d39b431081a126ded0059f97c67b060368b08b4;hp=c3c270c5296641fef25fb3181cafd2eb98b3eb21;hb=e30955dc472815457c4e10ed744fa7aa29ac4800;hpb=2935a01f7fffce53f81e235557f3ae5459e200e6 diff --git a/miscutil.c b/miscutil.c index c3c270c5..4d39b431 100644 --- a/miscutil.c +++ b/miscutil.c @@ -1,4 +1,4 @@ -const char miscutil_rcs[] = "$Id: miscutil.c,v 1.3 2001/05/29 23:10:09 oes Exp $"; +const char miscutil_rcs[] = "$Id: miscutil.c,v 1.5 2001/06/01 10:31:51 oes Exp $"; /********************************************************************* * * File : $Source: /cvsroot/ijbswa/current/miscutil.c,v $ @@ -36,6 +36,14 @@ const char miscutil_rcs[] = "$Id: miscutil.c,v 1.3 2001/05/29 23:10:09 oes Exp $ * * Revisions : * $Log: miscutil.c,v $ + * Revision 1.5 2001/06/01 10:31:51 oes + * Added character class matching to trivimatch; renamed to simplematch + * + * Revision 1.4 2001/05/31 17:32:31 oes + * + * - Enhanced domain part globbing with infix and prefix asterisk + * matching and optional unanchored operation + * * Revision 1.3 2001/05/29 23:10:09 oes * * @@ -203,9 +211,9 @@ char *safe_strerror(int err) char buf[BUFSIZ]; -#ifndef NOSTRERROR +#ifdef HAVE_STRERROR s = strerror(err); -#endif /* NOSTRERROR */ +#endif /* HAVE_STRERROR */ if (s == NULL) { @@ -394,14 +402,12 @@ char *strsav(char *old, const char *text_to_append) /********************************************************************* * - * Function : trivimatch + * Function : simplematch * - * Description : Trivial string matching, with only one metacharacter, - * namely '*', which stands for zero or more arbitrary - * characters. - * - * Note: The * is greedy, i.e. it will try to match as - * much text es possible. + * Description : String matching, with a (greedy) '*' wildcard that + * stands for zero or more arbitrary characters and + * character classes in [], which take both enumerations + * and ranges. * * Parameters : * 1 : pattern = pattern for matching @@ -410,15 +416,21 @@ char *strsav(char *old, const char *text_to_append) * Returns : 0 if match, else nonzero * *********************************************************************/ -int trivimatch(char *pattern, char *text) +int simplematch(char *pattern, char *text) { - char *fallback; - char *pat = pattern; - char *txt = text; - int wildcard = 0; + char *fallback; + char *pat = pattern; + char *txt = text; + int wildcard = 0; + + char lastchar = 'a'; + unsigned i; + unsigned char charmap[32]; + while (*txt) { + /* EOF pattern but !EOF text? */ if (*pat == '\0') { @@ -428,46 +440,78 @@ int trivimatch(char *pattern, char *text) /* '*' in the pattern? */ if (*pat == '*') { - + /* The pattern ends afterwards? Speed up the return. */ if (*++pat == '\0') { return 0; } - + /* Else, set wildcard mode and remember position after '*' */ wildcard = 1; fallback = pat; } - /* Compare: */ - if (*pat != *txt) + /* Character range specification? */ + if (*pat == '[') + { + memset(charmap, '\0', sizeof(charmap)); + + while (*++pat != ']') + { + if (!*pat) + { + return 1; + } + else if (*pat == '-') + { + if ((*++pat == ']') || *pat == '\0') + { + return(1); + } + for(i = lastchar; i <= *pat; i++) + { + charmap[i / 8] |= (1 << (i % 8)); + } + } + else + { + charmap[*pat / 8] |= (1 << (*pat % 8)); + lastchar = *pat; + } + } + } /* -END- if Character range specification */ + + + /* Compare: Char match, or char range match*/ + if ((*pat == *txt) + || ((*pat == ']') && (charmap[*txt / 8] & (1 << (*txt % 8)))) ) + { + /* Sucess, go ahead */ + pat++; + } + else { - /* In wildcard mode, just try again */ + /* In wildcard mode, just try again after failiure */ if(wildcard) { - /* Without wildcard mode, this is fatal! */ pat = fallback; } - /* Bad luck otherwise */ + /* Else, bad luck */ else { return 1; } } - /* We had a match, advance */ - else - { - pat++; - } txt++; - } + } - if(*pat == '*') pat++; + /* Cut off extra '*'s */ + if(*pat == '*') pat++; - /* Hey, we've made it all the way through! */ - return(*pat); + /* If this is the pattern's end, fine! */ + return(*pat); }