]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/wildcard.cpp
9846b7d4f4213ee603cc6766cbacaad4b6afdf96
[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         while ((string != str.end()) && (wild != mask.end()) && (*wild != '*'))
39         {
40                 if ((*wild != *string) && (*wild != '?'))
41                         return 0;
42
43                 wild++;
44                 string++;
45         }
46
47         while (string != str.end())
48         {
49                 if (*wild == '*')
50                 {
51                         if (++wild == mask.end())
52                                 return 1;
53
54                         mp = wild;
55                         cp = string;
56                         cp++;
57                 }
58                 else
59                 if ((*wild == *string) || (*wild == '?'))
60                 {
61                         wild++;
62                         string++;
63                 }
64                 else
65                 {
66                         wild = mp;
67                         string = cp++;
68                 }
69
70         }
71
72         while ((wild != mask.end()) && (*wild == '*'))
73                 wild++;
74
75         return wild == mask.end();
76 }
77
78 CoreExport bool match(const std::string &str, const std::string &mask)
79 {
80         std::string::const_iterator cp, mp;
81         std::string::const_iterator wild = mask.begin();
82         std::string::const_iterator string = str.begin();
83
84         while ((string != str.end()) && (wild != mask.end()) && (*wild != '*'))
85         {
86                 if ((lowermap[(unsigned char)*wild] != lowermap[(unsigned char)*string]) && (*wild != '?'))
87                         return 0;
88
89                 wild++;
90                 string++;
91         }
92
93         while (string != str.end())
94         {
95                 if (*wild == '*')
96                 {
97                         if (++wild == mask.end())
98                                 return 1;
99
100                         mp = wild;
101                         cp = string;
102                         cp++;
103                 }
104                 else
105                 if ((lowermap[(unsigned char)*wild] == lowermap[(unsigned char)*string]) || (*wild == '?'))
106                 {
107                         wild++;
108                         string++;
109                 }
110                 else
111                 {
112                         wild = mp;
113                         string = cp++;
114                 }
115
116         }
117
118         while ((wild != mask.end()) && (*wild == '*'))
119                 wild++;
120
121         return wild == mask.end();
122 }
123
124 /* Overloaded function that has the option of using cidr */
125 CoreExport bool match(const std::string &str, const std::string &mask, bool use_cidr_match)
126 {
127         if (use_cidr_match && MatchCIDR(str, mask, true))
128                 return true;
129         return match(str, mask);
130 }
131
132 CoreExport bool match(bool case_sensitive, const std::string &str, const std::string &mask, bool use_cidr_match)
133 {
134         if (use_cidr_match && MatchCIDR(str, mask, true))
135                 return true;
136
137         return case_sensitive ? csmatch(str, mask) : match(str, mask);
138 }
139
140 CoreExport bool match(bool case_sensitive, const std::string &str, const std::string &mask)
141 {
142         return case_sensitive ? csmatch(str, mask) : match(str, mask);
143 }
144