]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/wildcard.cpp
Move InspIRCd::IsValidMask() to helperfuncs.cpp
[user/henk/code/inspircd.git] / src / wildcard.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2007 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 #include "inspircd.h"
15 #include "hashcomp.h"
16 #include "inspstring.h"
17
18 using irc::sockets::MatchCIDR;
19
20 // Wed 27 Apr 2005 - Brain
21 // I've taken our our old wildcard routine -
22 // although comprehensive, it was topheavy and very
23 // slow, and ate masses of cpu when doing lots of
24 // comparisons. This is the 'de-facto' routine used
25 // by many, nobody really knows who wrote it first
26 // or what license its under, i've seen examples of it
27 // (unattributed to any author) all over the 'net.
28 // For now, we'll just consider this public domain.
29
30 CoreExport bool csmatch(const char *str, const char *mask)
31 {
32         unsigned char *cp = NULL, *mp = NULL;
33         unsigned char* string = (unsigned char*)str;
34         unsigned char* wild = (unsigned char*)mask;
35
36         while ((*string) && (*wild != '*'))
37         {
38                 if ((*wild != *string) && (*wild != '?'))
39                 {
40                         return 0;
41                 }
42                 wild++;
43                 string++;
44         }
45
46         while (*string)
47         {
48                 if (*wild == '*')
49                 {
50                         if (!*++wild)
51                         {
52                                 return 1;
53                         }
54                         mp = wild;
55                         cp = string+1;
56                 }
57                 else
58                 if ((*wild == *string) || (*wild == '?'))
59                 {
60                         wild++;
61                         string++;
62                 }
63                 else
64                 {
65                         wild = mp;
66                         string = cp++;
67                 }
68
69         }
70
71         while (*wild == '*')
72         {
73                 wild++;
74         }
75
76         return !*wild;
77 }
78
79 CoreExport bool match(const char *str, const char *mask)
80 {
81         unsigned char *cp = NULL, *mp = NULL;
82         unsigned char* string = (unsigned char*)str;
83         unsigned char* wild = (unsigned char*)mask;
84
85         while ((*string) && (*wild != '*'))
86         {
87                 if ((lowermap[*wild] != lowermap[*string]) && (*wild != '?'))
88                 {
89                         return 0;
90                 }
91                 wild++;
92                 string++;
93         }
94
95         while (*string)
96         {
97                 if (*wild == '*')
98                 {
99                         if (!*++wild)
100                         {
101                                 return 1;
102                         }
103                         mp = wild;
104                         cp = string+1;
105                 }
106                 else
107                 if ((lowermap[*wild] == lowermap[*string]) || (*wild == '?'))
108                 {
109                         wild++;
110                         string++;
111                 }
112                 else
113                 {
114                         wild = mp;
115                         string = cp++;
116                 }
117
118         }
119
120         while (*wild == '*')
121         {
122                 wild++;
123         }
124
125         return !*wild;
126 }
127
128 /* Overloaded function that has the option of using cidr */
129 CoreExport bool match(const char *str, const char *mask, bool use_cidr_match)
130 {
131         if (use_cidr_match && MatchCIDR(str, mask, true))
132                 return true;
133         return match(str, mask);
134 }
135
136 CoreExport bool match(bool case_sensitive, const char *str, const char *mask, bool use_cidr_match)
137 {
138         if (use_cidr_match && MatchCIDR(str, mask, true))
139                 return true;
140         return csmatch(str, mask);
141 }
142
143 CoreExport bool match(bool case_sensitive, const char *str, const char *mask)
144 {
145         return case_sensitive ? csmatch(str, mask) : match(str, mask);
146 }
147