/[svn]/hopm/trunk/src/match.c
ViewVC logotype

Contents of /hopm/trunk/src/match.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 5135 - (show annotations)
Thu Dec 25 18:51:51 2014 UTC (5 years, 5 months ago) by michael
File MIME type: text/x-chdr
File size: 6024 byte(s)
- propset svn:eol-style native

1 /*
2 * ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd)
3 *
4 * Copyright (c) 1997-2014 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 <stdarg.h>
28 #include <string.h>
29
30 #include "match.h"
31
32 #define ToLower(c) (ToLowerTab[(unsigned char)(c)])
33
34 static const unsigned char ToLowerTab[] =
35 {
36 0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa,
37 0xb, 0xc, 0xd, 0xe, 0xf, 0x10, 0x11, 0x12, 0x13, 0x14,
38 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d,
39 0x1e, 0x1f,
40 ' ', '!', '"', '#', '$', '%', '&', 0x27, '(', ')',
41 '*', '+', ',', '-', '.', '/',
42 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
43 ':', ';', '<', '=', '>', '?',
44 '@', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
45 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',
46 't', 'u', 'v', 'w', 'x', 'y', 'z', '{', '|', '}', '~',
47 '_',
48 '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
49 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',
50 't', 'u', 'v', 'w', 'x', 'y', 'z', '{', '|', '}', '~',
51 0x7f,
52 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
53 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
54 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99,
55 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
56 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9,
57 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf,
58 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9,
59 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf,
60 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9,
61 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf,
62 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9,
63 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf,
64 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9,
65 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
66 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9,
67 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff
68 };
69
70 /*! \brief Check a string against a mask.
71 * This test checks using traditional IRC wildcards only: '*' means
72 * match zero or more characters of any type; '?' means match exactly
73 * one character of any type. A backslash escapes the next character
74 * so that a wildcard may be matched exactly.
75 * param mask Wildcard-containing mask.
76 * param name String to check against \a mask.
77 * return Zero if \a mask matches \a name, non-zero if no match.
78 */
79 int
80 match(const char *mask, const char *name)
81 {
82 const char *m = mask, *n = name;
83 const char *m_tmp = mask, *n_tmp = name;
84 unsigned int star = 0;
85
86 while (1)
87 {
88 switch (*m)
89 {
90 case '\0':
91 if (*n == '\0')
92 return 0;
93 backtrack:
94 if (m_tmp == mask)
95 return 1;
96
97 m = m_tmp;
98 n = ++n_tmp;
99
100 if (*n == '\0')
101 return 1;
102 break;
103 case '\\':
104 ++m;
105
106 /* allow escaping to force capitalization */
107 if (*m++ != *n++)
108 goto backtrack;
109 break;
110 case '*':
111 case '?':
112 for (star = 0; ; ++m)
113 {
114 if (*m == '*')
115 star = 1;
116 else if (*m == '?')
117 {
118 if (*n++ == '\0')
119 goto backtrack;
120 }
121 else
122 break;
123 }
124
125 if (star)
126 {
127 if (*m == '\0')
128 return 0;
129 else if (*m == '\\')
130 {
131 m_tmp = ++m;
132
133 if (*m == '\0')
134 return 1;
135 for (n_tmp = n; *n && *n != *m; ++n)
136 ;
137 }
138 else
139 {
140 m_tmp = m;
141 for (n_tmp = n; *n && (ToLower(*n) != ToLower(*m)); ++n)
142 ;
143 }
144 }
145 /* and fall through */
146 default:
147 if (*n == '\0')
148 return *m != '\0';
149 if (ToLower(*m) != ToLower(*n))
150 goto backtrack;
151 ++m;
152 ++n;
153 break;
154 }
155 }
156
157 return 1;
158 }
159
160 /*
161 * collapse()
162 * Collapse a pattern string into minimal components.
163 * This particular version is "in place", so that it changes the pattern
164 * which is to be reduced to a "minimal" size.
165 *
166 * (C) Carlo Wood - 6 Oct 1998
167 * Speedup rewrite by Andrea Cocito, December 1998.
168 * Note that this new optimized algorithm can *only* work in place.
169 */
170
171 /*! \brief Collapse a mask string to remove redundancies.
172 * Specifically, it replaces a sequence of '*' followed by additional
173 * '*' or '?' with the same number of '?'s as the input, followed by
174 * one '*'. This minimizes useless backtracking when matching later.
175 * \param mask Mask string to collapse.
176 * \return Pointer to the start of the string.
177 */
178 char *
179 collapse(char *mask)
180 {
181 unsigned int star = 0;
182 char *m = mask;
183 char *b = NULL;
184
185 if (m)
186 {
187 do
188 {
189 if ((*m == '*') && (*(m + 1) == '*' || *(m + 1) == '?'))
190 {
191 b = m;
192
193 do
194 {
195 if (*m == '*')
196 star = 1;
197 else
198 {
199 if (star && (*m != '?'))
200 {
201 *b++ = '*';
202 star = 0;
203 }
204
205 *b++ = *m;
206
207 if ((*m == '\\') && (*(m + 1) == '*' || *(m + 1) == '?'))
208 *b++ = *++m;
209 }
210 } while (*m++);
211
212 break;
213 }
214 else
215 {
216 if ((*m == '\\') && (*(m + 1) == '*' || *(m + 1) == '?'))
217 ++m;
218 }
219 } while (*m++);
220 }
221
222 return mask;
223 }

Properties

Name Value
svn:eol-style native
svn:keywords Id

svnadmin@ircd-hybrid.org
ViewVC Help
Powered by ViewVC 1.1.28