1 |
/* |
2 |
* ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd) |
3 |
* |
4 |
* Copyright (c) 1997-2021 ircd-hybrid development team |
5 |
* |
6 |
* This program is free software; you can redistribute it and/or modify |
7 |
* it under the terms of the GNU General Public License as published by |
8 |
* the Free Software Foundation; either version 2 of the License, or |
9 |
* (at your option) any later version. |
10 |
* |
11 |
* This program is distributed in the hope that it will be useful, |
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
* GNU General Public License for more details. |
15 |
* |
16 |
* You should have received a copy of the GNU General Public License |
17 |
* along with this program; if not, write to the Free Software |
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 |
19 |
* USA |
20 |
*/ |
21 |
|
22 |
/*! \file match.c |
23 |
* \brief Functions to match/compare strings. |
24 |
* \version $Id$ |
25 |
*/ |
26 |
|
27 |
#include "match.h" |
28 |
|
29 |
|
30 |
#define ToLower(c) (ToLowerTab[(unsigned char)(c)]) |
31 |
|
32 |
static const unsigned char ToLowerTab[] = |
33 |
{ |
34 |
0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, |
35 |
0xb, 0xc, 0xd, 0xe, 0xf, 0x10, 0x11, 0x12, 0x13, 0x14, |
36 |
0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, |
37 |
0x1e, 0x1f, |
38 |
' ', '!', '"', '#', '$', '%', '&', 0x27, '(', ')', |
39 |
'*', '+', ',', '-', '.', '/', |
40 |
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', |
41 |
':', ';', '<', '=', '>', '?', |
42 |
'@', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', |
43 |
'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', |
44 |
't', 'u', 'v', 'w', 'x', 'y', 'z', '{', '|', '}', '~', |
45 |
'_', |
46 |
'`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', |
47 |
'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', |
48 |
't', 'u', 'v', 'w', 'x', 'y', 'z', '{', '|', '}', '~', |
49 |
0x7f, |
50 |
0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, |
51 |
0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, |
52 |
0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, |
53 |
0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, |
54 |
0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, |
55 |
0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, |
56 |
0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, |
57 |
0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, |
58 |
0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, |
59 |
0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, |
60 |
0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, |
61 |
0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf, |
62 |
0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, |
63 |
0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, |
64 |
0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, |
65 |
0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff |
66 |
}; |
67 |
|
68 |
/*! \brief Check a string against a mask. |
69 |
* This test checks using traditional IRC wildcards only: '*' means |
70 |
* match zero or more characters of any type; '?' means match exactly |
71 |
* one character of any type. A backslash escapes the next character |
72 |
* so that a wildcard may be matched exactly. |
73 |
* param mask Wildcard-containing mask. |
74 |
* param name String to check against \a mask. |
75 |
* return Zero if \a mask matches \a name, non-zero if no match. |
76 |
*/ |
77 |
int |
78 |
match(const char *mask, const char *name) |
79 |
{ |
80 |
const char *m = mask, *n = name; |
81 |
const char *m_tmp = mask, *n_tmp = name; |
82 |
unsigned int star = 0; |
83 |
|
84 |
while (1) |
85 |
{ |
86 |
switch (*m) |
87 |
{ |
88 |
case '\0': |
89 |
if (*n == '\0') |
90 |
return 0; |
91 |
backtrack: |
92 |
if (m_tmp == mask) |
93 |
return 1; |
94 |
|
95 |
m = m_tmp; |
96 |
n = ++n_tmp; |
97 |
|
98 |
if (*n == '\0') |
99 |
return 1; |
100 |
break; |
101 |
case '\\': |
102 |
++m; |
103 |
|
104 |
/* allow escaping to force capitalization */ |
105 |
if (*m++ != *n++) |
106 |
goto backtrack; |
107 |
break; |
108 |
case '*': |
109 |
case '?': |
110 |
for (star = 0; ; ++m) |
111 |
{ |
112 |
if (*m == '*') |
113 |
star = 1; |
114 |
else if (*m == '?') |
115 |
{ |
116 |
if (*n++ == '\0') |
117 |
goto backtrack; |
118 |
} |
119 |
else |
120 |
break; |
121 |
} |
122 |
|
123 |
if (star) |
124 |
{ |
125 |
if (*m == '\0') |
126 |
return 0; |
127 |
else if (*m == '\\') |
128 |
{ |
129 |
m_tmp = ++m; |
130 |
|
131 |
if (*m == '\0') |
132 |
return 1; |
133 |
for (n_tmp = n; *n && *n != *m; ++n) |
134 |
; |
135 |
} |
136 |
else |
137 |
{ |
138 |
m_tmp = m; |
139 |
for (n_tmp = n; *n && (ToLower(*n) != ToLower(*m)); ++n) |
140 |
; |
141 |
} |
142 |
} |
143 |
/* and fall through */ |
144 |
default: |
145 |
if (*n == '\0') |
146 |
return *m != '\0'; |
147 |
if (ToLower(*m) != ToLower(*n)) |
148 |
goto backtrack; |
149 |
++m; |
150 |
++n; |
151 |
break; |
152 |
} |
153 |
} |
154 |
|
155 |
return 1; |
156 |
} |