]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/wildcard.cpp
Move the socket functions like insp_ntoa into their own namespace. They arent really...
[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 extern char lowermap[255];
28
29 // Wed 27 Apr 2005 - Brain
30 // I've taken our our old wildcard routine -
31 // although comprehensive, it was topheavy and very
32 // slow, and ate masses of cpu when doing lots of
33 // comparisons. This is the 'de-facto' routine used
34 // by many, nobody really knows who wrote it first
35 // or what license its under, i've seen examples of it
36 // (unattributed to any author) all over the 'net.
37 // For now, we'll just consider this public domain.
38
39 bool match(const char *str, const char *mask)
40 {
41         unsigned char *cp, *mp;
42         unsigned char* string = (unsigned char*)str;
43         unsigned char* wild = (unsigned char*)mask;
44
45         while ((*string) && (*wild != '*'))
46         {
47                 if ((lowermap[*wild] != lowermap[*string]) && (*wild != '?'))
48                 {
49                         return 0;
50                 }
51                 wild++;
52                 string++;
53         }
54
55         while (*string)
56         {
57                 if (*wild == '*')
58                 {
59                         if (!*++wild)
60                         {
61                                 return 1;
62                         }
63                         mp = wild;
64                         cp = string+1;
65                 }
66                 else
67                 if ((lowermap[*wild] == lowermap[*string]) || (*wild == '?'))
68                 {
69                         wild++;
70                         string++;
71                 }
72                 else
73                 {
74                         wild = mp;
75                         string = cp++;
76                 }
77
78         }
79
80         while (*wild == '*')
81         {
82                 wild++;
83         }
84
85         return !*wild;
86 }
87
88 /* Overloaded function that has the option of using cidr */
89 bool match(const char *str, const char *mask, bool use_cidr_match)
90 {
91         if (use_cidr_match && MatchCIDR(str, mask, true))
92                 return true;
93         return match(str, mask);
94 }