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