]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/wildcard.cpp
4032809965ea91007bb1b1e312563b0be3b9ad2f
[user/henk/code/inspircd.git] / src / wildcard.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
6  *                       E-mail:
7  *                <brain@chatspike.net>
8  *                <Craig@chatspike.net>
9  *     
10  * Written by Craig Edwards, Craig McLure, and others.
11  * This program is free but copyrighted software; see
12  *            the file COPYING for details.
13  *
14  * ---------------------------------------------------
15  */
16
17 using namespace std;
18
19 #include <string>
20 #include "inspircd_config.h"
21 #include "inspircd.h"
22 #include "helperfuncs.h"
23 #include "inspstring.h"
24
25 using irc::sockets::MatchCIDR;
26
27 // Wed 27 Apr 2005 - Brain
28 // I've taken our our old wildcard routine -
29 // although comprehensive, it was topheavy and very
30 // slow, and ate masses of cpu when doing lots of
31 // comparisons. This is the 'de-facto' routine used
32 // by many, nobody really knows who wrote it first
33 // or what license its under, i've seen examples of it
34 // (unattributed to any author) all over the 'net.
35 // For now, we'll just consider this public domain.
36
37 bool match(const char *str, const char *mask)
38 {
39         unsigned char *cp, *mp;
40         unsigned char* string = (unsigned char*)str;
41         unsigned char* wild = (unsigned char*)mask;
42         extern char lowermap[255];
43
44         while ((*string) && (*wild != '*'))
45         {
46                 if ((lowermap[*wild] != lowermap[*string]) && (*wild != '?'))
47                 {
48                         return 0;
49                 }
50                 wild++;
51                 string++;
52         }
53
54         while (*string)
55         {
56                 if (*wild == '*')
57                 {
58                         if (!*++wild)
59                         {
60                                 return 1;
61                         }
62                         mp = wild;
63                         cp = string+1;
64                 }
65                 else
66                 if ((lowermap[*wild] == lowermap[*string]) || (*wild == '?'))
67                 {
68                         wild++;
69                         string++;
70                 }
71                 else
72                 {
73                         wild = mp;
74                         string = cp++;
75                 }
76
77         }
78
79         while (*wild == '*')
80         {
81                 wild++;
82         }
83
84         return !*wild;
85 }
86
87 /* Overloaded function that has the option of using cidr */
88 bool match(const char *str, const char *mask, bool use_cidr_match)
89 {
90         if (use_cidr_match && MatchCIDR(str, mask, true))
91                 return true;
92         return match(str, mask);
93 }