]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/wildcard.cpp
Jesus, look who's the commit whore today. More header updates, and removal of namespa...
[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 match(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 ((lowermap[*wild] != lowermap[*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 ((lowermap[*wild] == lowermap[*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 /* Overloaded function that has the option of using cidr */
81 bool match(const char *str, const char *mask, bool use_cidr_match)
82 {
83         if (use_cidr_match && MatchCIDR(str, mask, true))
84                 return true;
85         return match(str, mask);
86 }