X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=src%2Fwildcard.cpp;h=3b95b5f7509011d70b94558fa0e5482fc50a9215;hb=59b1a8955142935b02af6446005ab47fc7c3fc8c;hp=f74de804a17736affd48370ada540263c5d93c4b;hpb=800ebbf95d489010e3a99b4975c9b864d96cd0d2;p=user%2Fhenk%2Fcode%2Finspircd.git diff --git a/src/wildcard.cpp b/src/wildcard.cpp index f74de804a..3b95b5f75 100644 --- a/src/wildcard.cpp +++ b/src/wildcard.cpp @@ -1,90 +1,93 @@ +/* +------------------------------------+ + * | Inspire Internet Relay Chat Daemon | + * +------------------------------------+ + * + * InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev. + * E-mail: + * + * + * + * Written by Craig Edwards, Craig McLure, and others. + * This program is free but copyrighted software; see + * the file COPYING for details. + * + * --------------------------------------------------- + */ + +using namespace std; + #include #include "inspircd_config.h" #include "inspircd.h" +#include "helperfuncs.h" +#include "inspstring.h" -void Delete(char* str,int pos) -{ - char moo[MAXBUF]; - strcpy(moo,str); - moo[pos] = '\0'; - strcpy(str,moo); - strcat(str,moo+pos+1); -} - -void Insert(char* substr,char* str,int pos) -{ - std::string a = str; - a.insert(pos,substr); - strcpy(str,a.c_str()); -} - +extern char lowermap[255]; -int MWC = 0; +// Wed 27 Apr 2005 - Brain +// I've taken our our old wildcard routine - +// although comprehensive, it was topheavy and very +// slow, and ate masses of cpu when doing lots of +// comparisons. This is the 'de-facto' routine used +// by many, nobody really knows who wrote it first +// or what license its under, i've seen examples of it +// (unattributed to any author) all over the 'net. +// For now, we'll just consider this public domain. -bool match2(char* literal,char* mask) +int wildcmp(char *wild, char *string) { + char *cp, *mp; + while ((*string) && (*wild != '*')) + { + if ((lowermap[(unsigned)*wild] != lowermap[(unsigned)*string]) && (*wild != '?')) + { + return 0; + } + wild++; + string++; + } -char OldM[MAXBUF]; -int I,I2; + while (*string) + { + if (*wild == '*') + { + if (!*++wild) + { + return 1; + } + mp = wild; + cp = string+1; + } + else + if ((lowermap[(unsigned)*wild] == lowermap[(unsigned)*string]) || (*wild == '?')) + { + wild++; + string++; + } + else + { + wild = mp; + string = cp++; + } -if (MWC) - return true; + } -if ((strstr(mask,"*")==0) && (strlen(literal) != strlen(mask))) - return 0; - I=0; - I2=0; - while (I < strlen(mask)) - { - if (I2 >= strlen(literal)) - return 0; - - if ((mask[I]=='*') && (MWC==0)) - { - strcpy(OldM,mask); - - Delete(mask,I); - - while (strlen(mask)<255) - { - match2(literal,mask); - if (MWC==2) - return 1; - - Insert("?",mask,I); - } - strcpy(mask,OldM); - Delete(mask,I); - Insert("?",mask,I); - } - if (mask[I]=='?') - { - I++; - I2++; - continue; - } - if (mask[I] != literal[I2]) - return 0; - if (MWC) - return 1; - I++; - I2++; - } - if (strlen(literal)==strlen(mask)) - MWC=2; + while (*wild == '*') + { + wild++; + } + return !*wild; } +// This wrapper function is required to convert both +// strings to 'scandanavian lowercase' and make copies +// of them to a safe location. It also ensures we don't +// bite off more than we can chew with the length of +// the string. + bool match(const char* literal, const char* mask) { - char L[10240]; - char M[10240]; - MWC = 0; - strncpy(L,literal,10240); - strncpy(M,mask,10240); - strlower(L); - strlower(M); - match2(L,M); - return (MWC == 2); + return wildcmp((char*)mask, (char*)literal); }