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