X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=src%2Fwildcard.cpp;h=b49ba943a6bc0d18f95fcd34c4417002fb6dd64f;hb=484b718ccf1505360d62401dd09e3eca6b2568d8;hp=ce4d7d3147f549332dba73a512c837f5d2e6ab5d;hpb=e7f0a0fb7edf96abbddf72eadb490b5eb22447ec;p=user%2Fhenk%2Fcode%2Finspircd.git diff --git a/src/wildcard.cpp b/src/wildcard.cpp index ce4d7d314..b49ba943a 100644 --- a/src/wildcard.cpp +++ b/src/wildcard.cpp @@ -2,105 +2,116 @@ * | 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 */ + #include "inspircd.h" +#include "hashcomp.h" +#include "inspstring.h" -void Delete(char* str,int pos) +/* + * Wildcard matching! + * + * Iteration 1) + * Slow, horrible, etc. + * Iteration 2) + * The vastly available 'public domain' one + * Iteration 3) + * ZNC's, thought to be faster than ours, but it turned out that we could do better ;-) + * Iteration 4) + * Largely from work by peavey and myself (w00t) :) + * Iteration 5) + * peavey: Fix glob scan similar to 1.1, but scan ahead on glob in inner loop to retain speedup + * this fixes another case which we forgot to test. Add early return for obvious fail condition. + */ +static bool match_internal(const unsigned char *string, const unsigned char *wild, unsigned const char *map) { - char moo[MAXBUF]; - strlcpy(moo,str,MAXBUF); - moo[pos] = '\0'; - strlcpy(str,moo,MAXBUF); - strlcat(str,moo+pos+1,MAXBUF); -} + const unsigned char *s, *m; m = wild; -void Insert(char* substr,char* str,int pos) -{ - std::string a = str; - a.insert(pos,substr); - strlcpy(str,a.c_str(),MAXBUF); + if (*string && !*wild) + return false; + + if (!map) + map = rfc_case_insensitive_map; + + while (*string) + { + if (*wild == '*') + { + while (*wild && *wild == '*') + wild++; + + m = wild; + + if (!*wild) + return true; + else if (*wild != '?') + { + s = string; + while (*s) + { + if ((map[*wild] == map[*s])) + { + string = s; + if (*(wild+1) || !*(s+1)) + wild++; + break; + } + s++; + } + } + } + else if ( (map[*wild] == map[*string]) || (*wild == '?') ) + wild++; + else + wild = m; + + string++; + } + + while (*wild && *wild == '*') + wild++; + + return !*wild; } +/******************************************************************** + * Below here is all wrappers around match_internal + ********************************************************************/ -int MWC = 0; +CoreExport bool InspIRCd::Match(const std::string &str, const std::string &mask, unsigned const char *map) +{ + return match_internal((const unsigned char *)str.c_str(), (const unsigned char *)mask.c_str(), map); +} -bool match2(char* literal,char* mask) +CoreExport bool InspIRCd::Match(const char *str, const char *mask, unsigned const char *map) { + return match_internal((const unsigned char *)str, (const unsigned char *)mask, map); +} -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 InspIRCd::MatchCIDR(const std::string &str, const std::string &mask, unsigned const char *map) +{ + if (irc::sockets::MatchCIDR(str, mask, true)) + return true; + // Fall back to regular match + return InspIRCd::Match(str, mask, NULL); } -bool match(const char* literal, const char* mask) +CoreExport bool InspIRCd::MatchCIDR(const char *str, const char *mask, unsigned const char *map) { - 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); + if (irc::sockets::MatchCIDR(str, mask, true)) + return true; + + // Fall back to regular match + return InspIRCd::Match(str, mask, NULL); }