X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=src%2Fwildcard.cpp;h=0e6e8a874eaf4e3d6e309b1fe9ba0a51c3636137;hb=525eeb51a52ca9dbd001f0897222f6e5a3dccfb1;hp=ce4d7d3147f549332dba73a512c837f5d2e6ab5d;hpb=e7f0a0fb7edf96abbddf72eadb490b5eb22447ec;p=user%2Fhenk%2Fcode%2Finspircd.git diff --git a/src/wildcard.cpp b/src/wildcard.cpp index ce4d7d314..0e6e8a874 100644 --- a/src/wildcard.cpp +++ b/src/wildcard.cpp @@ -2,105 +2,149 @@ * | Inspire Internet Relay Chat Daemon | * +------------------------------------+ * - * Inspire is copyright (C) 2002-2004 ChatSpike-Dev. - * E-mail: - * - * - * - * Written by Craig Edwards, Craig McLure, and others. + * InspIRCd: (C) 2002-2008 InspIRCd Development Team + * See: http://www.inspircd.org/wiki/index.php/Credits + * * This program is free but copyrighted software; see * the file COPYING for details. * * --------------------------------------------------- */ -#include -#include "inspircd_config.h" +/* $Core: libIRCDwildcard */ + #include "inspircd.h" +#include "hashcomp.h" +#include "inspstring.h" + +using irc::sockets::MatchCIDR; + +// 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. -void Delete(char* str,int pos) +CoreExport bool csmatch(const char *str, const char *mask) { - char moo[MAXBUF]; - strlcpy(moo,str,MAXBUF); - moo[pos] = '\0'; - strlcpy(str,moo,MAXBUF); - strlcat(str,moo+pos+1,MAXBUF); + unsigned char *cp = NULL, *mp = NULL; + unsigned char* string = (unsigned char*)str; + unsigned char* wild = (unsigned char*)mask; + + while ((*string) && (*wild != '*')) + { + if ((*wild != *string) && (*wild != '?')) + { + return 0; + } + wild++; + string++; + } + + while (*string) + { + if (*wild == '*') + { + if (!*++wild) + { + return 1; + } + mp = wild; + cp = string+1; + } + else + if ((*wild == *string) || (*wild == '?')) + { + wild++; + string++; + } + else + { + wild = mp; + string = cp++; + } + + } + + while (*wild == '*') + { + wild++; + } + + return !*wild; } -void Insert(char* substr,char* str,int pos) +CoreExport bool match(const char *str, const char *mask) { - std::string a = str; - a.insert(pos,substr); - strlcpy(str,a.c_str(),MAXBUF); -} + unsigned char *cp = NULL, *mp = NULL; + unsigned char* string = (unsigned char*)str; + unsigned char* wild = (unsigned char*)mask; + + while ((*string) && (*wild != '*')) + { + if ((lowermap[*wild] != lowermap[*string]) && (*wild != '?')) + { + return 0; + } + wild++; + string++; + } + + while (*string) + { + if (*wild == '*') + { + if (!*++wild) + { + return 1; + } + mp = wild; + cp = string+1; + } + else + if ((lowermap[*wild] == lowermap[*string]) || (*wild == '?')) + { + wild++; + string++; + } + else + { + wild = mp; + string = cp++; + } + } -int MWC = 0; + while (*wild == '*') + { + wild++; + } -bool match2(char* literal,char* mask) + return !*wild; +} + +/* Overloaded function that has the option of using cidr */ +CoreExport bool match(const char *str, const char *mask, bool use_cidr_match) { + if (use_cidr_match && MatchCIDR(str, mask, true)) + return true; + return match(str, mask); +} -char OldM[MAXBUF]; -int I,I2; - -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)) - { - strlcpy(OldM,mask,MAXBUF); - - Delete(mask,I); - - while (strlen(mask)<255) - { - match2(literal,mask); - if (MWC==2) - return 1; - - Insert("?",mask,I); - } - strlcpy(mask,OldM,MAXBUF); - 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; +CoreExport bool match(bool case_sensitive, const char *str, const char *mask, bool use_cidr_match) +{ + if (use_cidr_match && MatchCIDR(str, mask, true)) + return true; + return case_sensitive ? csmatch(str, mask) : match(str, mask); } -bool match(const char* literal, const char* mask) +CoreExport bool match(bool case_sensitive, const char *str, const char *mask) { - char L[10240]; - char M[10240]; - MWC = 0; - strlcpy(L,literal,10240); - strlcpy(M,mask,10240); - strlower(L); - strlower(M); - match2(L,M); - return (MWC == 2); + return case_sensitive ? csmatch(str, mask) : match(str, mask); }