]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/wildcard.cpp
4b00e7261b947156a40d21477e2457efbec5b842
[user/henk/code/inspircd.git] / src / wildcard.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2008 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 /* $Core */
15
16 #include "inspircd.h"
17 #include "hashcomp.h"
18 #include "inspstring.h"
19
20 /*
21  * Wildcard matching, the third (and probably final) iteration!
22  *
23  */
24 static bool match_internal(const unsigned char *mask, const unsigned char *str, unsigned const char *map)
25 {
26         const unsigned char *wild = str;
27         const unsigned char *string = mask;
28         const unsigned char *cp = NULL;
29         const unsigned char *mp = NULL;
30
31         if (!map)
32                 map = lowermap; // default to case insensitive search
33
34         while ((*string) && (*wild != '*'))
35         {
36                 if ((map[*wild] != map[*string]) && (*wild != '?'))
37                 {
38                         return false;
39                 }
40
41                 ++wild;
42                 ++string;
43         }
44
45         while (*string)
46         {
47                 if (*wild == '*')
48                 {
49                         if (!*++wild)
50                         {
51                                 return true;
52                         }
53
54                         mp = wild;
55                         cp = string+1;
56                 }
57                 // if mapped char == mapped wild OR wild is ?
58                 else if ((map[*wild] == map[*string]) || (*wild == '?'))
59                 {
60                         ++wild;
61                         ++string;
62                 }
63                 else
64                 {
65                         wild = mp;
66                         string = cp++;
67                 }
68         }
69
70         while (*wild == '*')
71         {
72                 wild++;
73         }
74
75         return (*wild == 0);
76 }
77
78 CoreExport bool InspIRCd::Match(const std::string &str, const std::string &mask, unsigned const char *map)
79 {
80         return match_internal((const unsigned char *)str.c_str(), (const unsigned char *)mask.c_str(), map);
81 }
82
83 CoreExport bool InspIRCd::Match(const  char *str, const char *mask, unsigned const char *map)
84 {
85         return match_internal((const unsigned char *)str, (const unsigned char *)mask, map);
86 }
87
88
89 CoreExport bool InspIRCd::MatchCIDR(const std::string &str, const std::string &mask, unsigned const char *map)
90 {
91         if (irc::sockets::MatchCIDR(str, mask, true))
92                 return true;
93
94         // Fall back to regular match
95         return InspIRCd::Match(str, mask, NULL);
96 }
97
98 CoreExport bool InspIRCd::MatchCIDR(const  char *str, const char *mask, unsigned const char *map)
99 {
100         if (irc::sockets::MatchCIDR(str, mask, true))
101                 return true;
102
103         // Fall back to regular match
104         return InspIRCd::Match(str, mask, NULL);
105 }
106