X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=src%2Fwildcard.cpp;h=2df7a5bec10e769b3e3445e8ebc18132fbfaee0c;hb=82bf46fa061a4f18071e73756e4e0d618eb57320;hp=123abc500bf85fa81991f9db42458ef67508833b;hpb=5c328da92fbf7a7e8b26ded2df085d3cf49f67f5;p=user%2Fhenk%2Fcode%2Finspircd.git diff --git a/src/wildcard.cpp b/src/wildcard.cpp index 123abc500..2df7a5bec 100644 --- a/src/wildcard.cpp +++ b/src/wildcard.cpp @@ -2,115 +2,160 @@ * | 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" -void Delete(char* str,int pos) -{ - char moo[MAXBUF]; - strlcpy(moo,str,MAXBUF); - moo[pos] = '\0'; - strlcpy(str,moo,MAXBUF); - strlcat(str,moo+pos+1,MAXBUF); -} +using irc::sockets::MatchCIDR; -void Insert(char* substr,char* str,int pos) +/* Rewritten to operate on more effective C++ std::string types + * rather than char* to avoid data copies. + * - Brain + */ + +CoreExport bool csmatch(const std::string &str, const std::string &mask) { - std::string a = str; - a.insert(pos,substr); - strlcpy(str,a.c_str(),MAXBUF); -} + std::string::const_iterator cp, mp; + //unsigned char *cp = NULL, *mp = NULL; + //unsigned char* string = (unsigned char*)str; + //unsigned char* wild = (unsigned char*)mask; -int MWC = 0; + std::string::const_iterator wild = mask.begin(); + std::string::const_iterator string = str.begin(); -bool match2(char* literal,char* mask) -{ + if (mask.empty()) + return false; + + while ((string != str.end()) && (wild != mask.end()) && (*wild != '*')) + { + if ((*wild != *string) && (*wild != '?')) + return 0; -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; + wild++; + string++; + } + while (string != str.end()) + { + if (wild != mask.end() && *wild == '*') + { + if (++wild == mask.end()) + return 1; + + mp = wild; + cp = string; + + if (cp != str.end()) + cp++; + } + else + if ((string != str.end() && wild != mask.end()) && ((*wild == *string) || (*wild == '?'))) + { + wild++; + string++; + } + else + { + wild = mp; + if (cp == str.end()) + cp = str.end(); + else + string = cp++; + } + + } + + while ((wild != mask.end()) && (*wild == '*')) + wild++; + + return wild == mask.end(); } -bool match(const char* literal, const char* mask) +CoreExport bool match(const std::string &str, const std::string &mask) { - char L[10240]; - char M[10240]; - MWC = 0; - strlcpy(L,literal,10240); - strlcpy(M,mask,10240); - strlower(L); - strlower(M); - // short circuit literals - log(DEBUG,"Match '%s' to '%s'",L,M); - if ((!strchr(M,'*')) && (!strchr(M,'?'))) + std::string::const_iterator cp, mp; + std::string::const_iterator wild = mask.begin(); + std::string::const_iterator string = str.begin(); + + if (mask.empty()) + return false; + + while ((string != str.end()) && (wild != mask.end()) && (*wild != '*')) { - if (!strcasecmp(L,M)) + if ((lowermap[(unsigned char)*wild] != lowermap[(unsigned char)*string]) && (*wild != '?')) + return 0; + + wild++; + string++; + } + + while (string != str.end()) + { + if (wild != mask.end() && *wild == '*') { - return true; + if (++wild == mask.end()) + return 1; + + mp = wild; + cp = string; + + if (cp != str.end()) + cp++; + + } + else + if ((string != str.end() && wild != mask.end()) && ((lowermap[(unsigned char)*wild] == lowermap[(unsigned char)*string]) || (*wild == '?'))) + { + wild++; + string++; } + else + { + wild = mp; + if (cp == str.end()) + string = str.end(); + else + string = cp++; + } + } - match2(L,M); - return (MWC == 2); + + while ((wild != mask.end()) && (*wild == '*')) + wild++; + + return wild == mask.end(); +} + +/* Overloaded function that has the option of using cidr */ +CoreExport bool match(const std::string &str, const std::string &mask, bool use_cidr_match) +{ + if (use_cidr_match && MatchCIDR(str, mask, true)) + return true; + return match(str, mask); +} + +CoreExport bool match(bool case_sensitive, const std::string &str, const std::string &mask, bool use_cidr_match) +{ + if (use_cidr_match && MatchCIDR(str, mask, true)) + return true; + + return case_sensitive ? csmatch(str, mask) : match(str, mask); +} + +CoreExport bool match(bool case_sensitive, const std::string &str, const std::string &mask) +{ + return case_sensitive ? csmatch(str, mask) : match(str, mask); }