]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/wildcard.cpp
Braunvieh!
[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 static bool match_internal(const unsigned char *str, const unsigned char *mask, unsigned const char *map)
21 {
22         unsigned char *cp = NULL, *mp = NULL;
23         unsigned char* string = (unsigned char*)str;
24         unsigned char* wild = (unsigned char*)mask;
25
26         if (!map)
27                 map = rfc_case_insensitive_map;
28
29         while ((*string) && (*wild != '*'))
30         {
31                 if ((map[*wild] != map[*string]) && (*wild != '?'))
32                 {
33                         return 0;
34                 }
35                 wild++;
36                 string++;
37         }
38
39         while (*string)
40         {
41                 if (*wild == '*')
42                 {
43                         if (!*++wild)
44                         {
45                                 return 1;
46                         }
47                         mp = wild;
48                         cp = string+1;
49                 }
50                 else
51                 if ((map[*wild] == map[*string]) || (*wild == '?'))
52                 {
53                         wild++;
54                         string++;
55                 }
56                 else
57                 {
58                         wild = mp;
59                         string = cp++;
60                 }
61
62         }
63
64         while (*wild == '*')
65         {
66                 wild++;
67         }
68
69         return !*wild;
70 }
71
72 /********************************************************************
73  * Below here is all wrappers around match_internal
74  ********************************************************************/
75
76 CoreExport bool InspIRCd::Match(const std::string &str, const std::string &mask, unsigned const char *map)
77 {
78         return match_internal((const unsigned char *)str.c_str(), (const unsigned char *)mask.c_str(), map);
79 }
80
81 CoreExport bool InspIRCd::Match(const  char *str, const char *mask, unsigned const char *map)
82 {
83         return match_internal((const unsigned char *)str, (const unsigned char *)mask, map);
84 }
85
86 CoreExport bool InspIRCd::MatchCIDR(const std::string &str, const std::string &mask, unsigned const char *map)
87 {
88         if (irc::sockets::MatchCIDR(str, mask, true))
89                 return true;
90
91         // Fall back to regular match
92         return InspIRCd::Match(str, mask, NULL);
93 }
94
95 CoreExport bool InspIRCd::MatchCIDR(const  char *str, const char *mask, unsigned const char *map)
96 {
97         if (irc::sockets::MatchCIDR(str, mask, true))
98                 return true;
99
100         // Fall back to regular match
101         return InspIRCd::Match(str, mask, NULL);
102 }
103