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