]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/wildcard.cpp
Remove last vestige of libircdfoo, by changing tag into a single identifier marking...
[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 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 != mask.end() && *wild == '*')
53                 {
54                         if (++wild == mask.end())
55                                 return 1;
56
57                         mp = wild;
58                         cp = string;
59
60                         if (cp != str.end())
61                                 cp++;
62                 }
63                 else
64                 if ((string != str.end() && wild != mask.end()) && ((*wild == *string) || (*wild == '?')))
65                 {
66                         wild++;
67                         string++;
68                 }
69                 else
70                 {
71                         wild = mp;
72                         if (cp == str.end())
73                                 cp = str.end();
74                         else
75                                 string = cp++;
76                 }
77
78         }
79
80         while ((wild != mask.end()) && (*wild == '*'))
81                 wild++;
82
83         return wild == mask.end();
84 }
85
86 CoreExport bool match(const std::string &str, const std::string &mask)
87 {
88         std::string::const_iterator cp, mp;
89         std::string::const_iterator wild = mask.begin();
90         std::string::const_iterator string = str.begin();
91
92         if (mask.empty())
93                 return false;
94
95         while ((string != str.end()) && (wild != mask.end()) && (*wild != '*'))
96         {
97                 if ((lowermap[(unsigned char)*wild] != lowermap[(unsigned char)*string]) && (*wild != '?'))
98                         return 0;
99
100                 wild++;
101                 string++;
102         }
103
104         while (string != str.end())
105         {
106                 if (wild != mask.end() && *wild == '*')
107                 {
108                         if (++wild == mask.end())
109                                 return 1;
110
111                         mp = wild;
112                         cp = string;
113
114                         if (cp != str.end())
115                                 cp++;
116
117                 }
118                 else
119                 if ((string != str.end() && wild != mask.end()) && ((lowermap[(unsigned char)*wild] == lowermap[(unsigned char)*string]) || (*wild == '?')))
120                 {
121                         wild++;
122                         string++;
123                 }
124                 else
125                 {
126                         wild = mp;
127                         if (cp == str.end())
128                                 string = str.end();
129                         else
130                                 string = cp++;
131                 }
132
133         }
134
135         while ((wild != mask.end()) && (*wild == '*'))
136                 wild++;
137
138         return wild == mask.end();
139 }
140
141 /* Overloaded function that has the option of using cidr */
142 CoreExport bool match(const std::string &str, const std::string &mask, bool use_cidr_match)
143 {
144         if (use_cidr_match && MatchCIDR(str, mask, true))
145                 return true;
146         return match(str, mask);
147 }
148
149 CoreExport bool match(bool case_sensitive, const std::string &str, const std::string &mask, bool use_cidr_match)
150 {
151         if (use_cidr_match && MatchCIDR(str, mask, true))
152                 return true;
153
154         return case_sensitive ? csmatch(str, mask) : match(str, mask);
155 }
156
157 CoreExport bool match(bool case_sensitive, const std::string &str, const std::string &mask)
158 {
159         return case_sensitive ? csmatch(str, mask) : match(str, mask);
160 }
161