]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/wildcard.cpp
3a91e8350ca5c51634216b6841d8622f399bd61a
[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: libIRCDwildcard */
15
16 #include "inspircd.h"
17 #include "hashcomp.h"
18 #include "inspstring.h"
19
20 using irc::sockets::MatchCIDR;
21
22 /* Rewritten to operate on more effective C++ std::string types
23  * rather than char* to avoid data copies.
24  * - Brain
25  */
26
27 CoreExport bool csmatch(const std::string &str, const std::string &mask)
28 {
29         std::string::const_iterator cp, mp;
30
31         //unsigned char *cp = NULL, *mp = NULL;
32         //unsigned char* string = (unsigned char*)str;
33         //unsigned char* wild = (unsigned char*)mask;
34
35         std::string::const_iterator wild = mask.begin();
36         std::string::const_iterator string = str.begin();
37
38         if (mask.empty())
39                 return false;
40
41         while ((string != str.end()) && (wild != mask.end()) && (*wild != '*'))
42         {
43                 if ((*wild != *string) && (*wild != '?'))
44                         return 0;
45
46                 wild++;
47                 string++;
48         }
49
50         while (string != str.end())
51         {
52                 if (*wild == '*')
53                 {
54                         if (++wild == mask.end())
55                                 return 1;
56
57                         mp = wild;
58                         cp = string;
59                         cp++;
60                 }
61                 else
62                 if ((*wild == *string) || (*wild == '?'))
63                 {
64                         wild++;
65                         string++;
66                 }
67                 else
68                 {
69                         wild = mp;
70                         string = cp++;
71                 }
72
73         }
74
75         while ((wild != mask.end()) && (*wild == '*'))
76                 wild++;
77
78         return wild == mask.end();
79 }
80
81 CoreExport bool match(const std::string &str, const std::string &mask)
82 {
83         std::string::const_iterator cp, mp;
84         std::string::const_iterator wild = mask.begin();
85         std::string::const_iterator string = str.begin();
86
87         if (mask.empty())
88                 return false;
89
90         while ((string != str.end()) && (wild != mask.end()) && (*wild != '*'))
91         {
92                 if ((lowermap[(unsigned char)*wild] != lowermap[(unsigned char)*string]) && (*wild != '?'))
93                         return 0;
94
95                 wild++;
96                 string++;
97         }
98
99         while (string != str.end())
100         {
101                 if (*wild == '*')
102                 {
103                         if (++wild == mask.end())
104                                 return 1;
105
106                         mp = wild;
107                         cp = string;
108                         cp++;
109                 }
110                 else
111                 if ((lowermap[(unsigned char)*wild] == lowermap[(unsigned char)*string]) || (*wild == '?'))
112                 {
113                         wild++;
114                         string++;
115                 }
116                 else
117                 {
118                         wild = mp;
119                         string = cp++;
120                 }
121
122         }
123
124         while ((wild != mask.end()) && (*wild == '*'))
125                 wild++;
126
127         return wild == mask.end();
128 }
129
130 /* Overloaded function that has the option of using cidr */
131 CoreExport bool match(const std::string &str, const std::string &mask, bool use_cidr_match)
132 {
133         if (use_cidr_match && MatchCIDR(str, mask, true))
134                 return true;
135         return match(str, mask);
136 }
137
138 CoreExport bool match(bool case_sensitive, const std::string &str, const std::string &mask, bool use_cidr_match)
139 {
140         if (use_cidr_match && MatchCIDR(str, mask, true))
141                 return true;
142
143         return case_sensitive ? csmatch(str, mask) : match(str, mask);
144 }
145
146 CoreExport bool match(bool case_sensitive, const std::string &str, const std::string &mask)
147 {
148         return case_sensitive ? csmatch(str, mask) : match(str, mask);
149 }
150