]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/wildcard.cpp
Fix missing bounds checks in wildcard.cpp causing crash in bug #590 and related
[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 false;
45
46                 wild++;
47                 string++;
48         }
49
50         if (wild == mask.end() && string != str.end())
51                 return false;
52
53         while (string != str.end())
54         {
55                 if (wild != mask.end() && *wild == '*')
56                 {
57                         if (++wild == mask.end())
58                                 return true;
59
60                         mp = wild;
61                         cp = string;
62
63                         if (cp != str.end())
64                                 cp++;
65                 }
66                 else
67                 if ((string != str.end() && wild != mask.end()) && ((*wild == *string) || (*wild == '?')))
68                 {
69                         wild++;
70                         string++;
71                 }
72                 else
73                 {
74                         wild = mp;
75                         if (cp == str.end())
76                                 cp = str.end();
77                         else
78                                 string = cp++;
79                 }
80
81         }
82
83         while ((wild != mask.end()) && (*wild == '*'))
84                 wild++;
85
86         return wild == mask.end();
87 }
88
89 CoreExport bool match(const std::string &str, const std::string &mask)
90 {
91         std::string::const_iterator cp, mp;
92         std::string::const_iterator wild = mask.begin();
93         std::string::const_iterator string = str.begin();
94
95         if (mask.empty())
96                 return false;
97
98         while ((string != str.end()) && (wild != mask.end()) && (*wild != '*'))
99         {
100                 if ((lowermap[(unsigned char)*wild] != lowermap[(unsigned char)*string]) && (*wild != '?'))
101                         return false;
102
103                 wild++;
104                 string++;
105                 //printf("Iterate first loop\n");
106         }
107
108         if (wild == mask.end() && string != str.end())
109                 return false;
110
111         while (string != str.end())
112         {
113                 //printf("outer\n %c", *string);
114                 if (wild != mask.end() && *wild == '*')
115                 {
116
117                         //printf("inner %c\n", *wild);
118                         if (++wild == mask.end())
119                                 return true;
120
121                         mp = wild;
122                         cp = string;
123
124                         if (cp != str.end())
125                                 cp++;
126
127                 }
128                 else
129                 if ((string != str.end() && wild != mask.end()) && ((lowermap[(unsigned char)*wild] == lowermap[(unsigned char)*string]) || (*wild == '?')))
130                 {
131                         if (wild != mask.end())
132                                 wild++;
133
134                         if (string != str.end())
135                                 string++;
136                 }
137                 else
138                 {
139                         wild = mp;
140                         if (cp == str.end())
141                                 string = str.end();
142                         else
143                                 string = cp++;
144                 }
145
146         }
147
148         while ((wild != mask.end()) && (*wild == '*'))
149                 wild++;
150
151         return wild == mask.end();
152 }
153
154 /* Overloaded function that has the option of using cidr */
155 CoreExport bool match(const std::string &str, const std::string &mask, bool use_cidr_match)
156 {
157         if (use_cidr_match && MatchCIDR(str, mask, true))
158                 return true;
159         return match(str, mask);
160 }
161
162 CoreExport bool match(bool case_sensitive, const std::string &str, const std::string &mask, bool use_cidr_match)
163 {
164         if (use_cidr_match && MatchCIDR(str, mask, true))
165                 return true;
166
167         return case_sensitive ? csmatch(str, mask) : match(str, mask);
168 }
169
170 CoreExport bool match(bool case_sensitive, const std::string &str, const std::string &mask)
171 {
172         return case_sensitive ? csmatch(str, mask) : match(str, mask);
173 }
174