]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/wildcard.cpp
match() is no longer a function+no header, now a static method of InspIRCd class...
[user/henk/code/inspircd.git] / src / wildcard.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2008 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 */
15
16 #include "inspircd.h"
17 #include "hashcomp.h"
18 #include "inspstring.h"
19
20 /*
21  * Wildcard matching, the third (and probably final) iteration!
22  *
23  */
24 static bool match_internal(const unsigned char *str, const unsigned char *mask, unsigned const char *map)
25 {
26         const unsigned char *wild = str;
27         const unsigned char *string = mask;
28         const unsigned char *cp = NULL;
29         const unsigned char *mp = NULL;
30
31         while ((*string) && (*wild != '*'))
32         {
33                 if (!map)
34                 {
35                         if ((*wild != *string) && (*wild != '?'))
36                         {
37                                 return false;
38                         }
39                 }
40                 else
41                 {
42                         if (map[*wild] != map[*string] && (*wild != '?'))
43                         {
44                                 return false;
45                         }
46                 }
47
48                 ++wild;
49                 ++string;
50         }
51
52         while (*string)
53         {
54                 if (*wild == '*')
55                 {
56                         if (!*++wild)
57                         {
58                                 return true;
59                         }
60
61                         mp = wild;
62                         cp = string+1;
63                 }
64                 // if there is no charmap and str == wild OR
65                 // there is a map and mapped char == mapped wild AND
66                 // wild is NOT ?
67                 else if (((!map && *wild == *string) || (map && map[*wild] == map[*string])) && (*wild == '?'))
68                 {
69                         ++wild;
70                         ++string;
71                 }
72                 else
73                 {
74                         wild = mp;
75                         string = cp++;
76                 }
77         }
78
79         while (*wild == '*')
80         {
81                 wild++;
82         }
83
84         return (*wild == 0);
85 }
86
87 CoreExport bool InspIRCd::Match(const std::string &str, const std::string &mask, unsigned const char *map)
88 {
89         return match_internal((const unsigned char *)str.c_str(), (const unsigned char *)mask.c_str(), map);
90 }
91
92 CoreExport bool InspIRCd::Match(const  char *str, const char *mask, unsigned const char *map)
93 {
94         return match_internal((const unsigned char *)str, (const unsigned char *)mask, map);
95 }
96
97
98 CoreExport bool InspIRCd::MatchCIDR(const std::string &str, const std::string &mask, unsigned const char *map)
99 {
100         if (irc::sockets::MatchCIDR(str, mask, true))
101                 return true;
102
103         // Fall back to regular match
104         return InspIRCd::Match(str, mask, NULL);
105 }
106
107 CoreExport bool InspIRCd::MatchCIDR(const  char *str, const char *mask, unsigned const char *map)
108 {
109         if (irc::sockets::MatchCIDR(str, mask, true))
110                 return true;
111
112         // Fall back to regular match
113         return InspIRCd::Match(str, mask, NULL);
114 }
115