blob: 0179a4f71771be7edaa2c96583838a05ec70e2f7 [file] [log] [blame]
Lucas Eckels9bd90e62012-08-06 15:07:02 -07001/***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
Elliott Hughes0128fe42018-02-27 14:57:55 -08008 * Copyright (C) 1998 - 2018, Daniel Stenberg, <daniel@haxx.se>, et al.
Lucas Eckels9bd90e62012-08-06 15:07:02 -07009 *
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
Alex Deymo8f1a2142016-06-28 14:49:26 -070012 * are also available at https://curl.haxx.se/docs/copyright.html.
Lucas Eckels9bd90e62012-08-06 15:07:02 -070013 *
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
17 *
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
20 *
21 ***************************************************************************/
22
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070023#include "curl_setup.h"
Lucas Eckels9bd90e62012-08-06 15:07:02 -070024
Alex Deymo8f1a2142016-06-28 14:49:26 -070025#include <curl/curl.h>
26
Lucas Eckels9bd90e62012-08-06 15:07:02 -070027#include "curl_fnmatch.h"
Lucas Eckels9bd90e62012-08-06 15:07:02 -070028#include "curl_memory.h"
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070029
Lucas Eckels9bd90e62012-08-06 15:07:02 -070030/* The last #include file should be: */
31#include "memdebug.h"
32
33#define CURLFNM_CHARSET_LEN (sizeof(char) * 256)
34#define CURLFNM_CHSET_SIZE (CURLFNM_CHARSET_LEN + 15)
35
36#define CURLFNM_NEGATE CURLFNM_CHARSET_LEN
37
38#define CURLFNM_ALNUM (CURLFNM_CHARSET_LEN + 1)
39#define CURLFNM_DIGIT (CURLFNM_CHARSET_LEN + 2)
40#define CURLFNM_XDIGIT (CURLFNM_CHARSET_LEN + 3)
41#define CURLFNM_ALPHA (CURLFNM_CHARSET_LEN + 4)
42#define CURLFNM_PRINT (CURLFNM_CHARSET_LEN + 5)
43#define CURLFNM_BLANK (CURLFNM_CHARSET_LEN + 6)
44#define CURLFNM_LOWER (CURLFNM_CHARSET_LEN + 7)
45#define CURLFNM_GRAPH (CURLFNM_CHARSET_LEN + 8)
46#define CURLFNM_SPACE (CURLFNM_CHARSET_LEN + 9)
47#define CURLFNM_UPPER (CURLFNM_CHARSET_LEN + 10)
48
49typedef enum {
Lucas Eckels9bd90e62012-08-06 15:07:02 -070050 CURLFNM_SCHS_DEFAULT = 0,
Lucas Eckels9bd90e62012-08-06 15:07:02 -070051 CURLFNM_SCHS_RIGHTBR,
52 CURLFNM_SCHS_RIGHTBRLEFTBR
53} setcharset_state;
54
55typedef enum {
56 CURLFNM_PKW_INIT = 0,
57 CURLFNM_PKW_DDOT
58} parsekey_state;
59
Elliott Hughescac39802018-04-27 16:19:43 -070060typedef enum {
61 CCLASS_OTHER = 0,
62 CCLASS_DIGIT,
63 CCLASS_UPPER,
64 CCLASS_LOWER
65} char_class;
66
Lucas Eckels9bd90e62012-08-06 15:07:02 -070067#define SETCHARSET_OK 1
68#define SETCHARSET_FAIL 0
69
70static int parsekeyword(unsigned char **pattern, unsigned char *charset)
71{
72 parsekey_state state = CURLFNM_PKW_INIT;
73#define KEYLEN 10
74 char keyword[KEYLEN] = { 0 };
75 int found = FALSE;
76 int i;
77 unsigned char *p = *pattern;
78 for(i = 0; !found; i++) {
79 char c = *p++;
80 if(i >= KEYLEN)
81 return SETCHARSET_FAIL;
82 switch(state) {
83 case CURLFNM_PKW_INIT:
Elliott Hughescac39802018-04-27 16:19:43 -070084 if(ISLOWER(c))
Lucas Eckels9bd90e62012-08-06 15:07:02 -070085 keyword[i] = c;
86 else if(c == ':')
87 state = CURLFNM_PKW_DDOT;
88 else
Elliott Hughescac39802018-04-27 16:19:43 -070089 return SETCHARSET_FAIL;
Lucas Eckels9bd90e62012-08-06 15:07:02 -070090 break;
91 case CURLFNM_PKW_DDOT:
92 if(c == ']')
93 found = TRUE;
94 else
95 return SETCHARSET_FAIL;
96 }
97 }
98#undef KEYLEN
99
100 *pattern = p; /* move caller's pattern pointer */
101 if(strcmp(keyword, "digit") == 0)
102 charset[CURLFNM_DIGIT] = 1;
103 else if(strcmp(keyword, "alnum") == 0)
104 charset[CURLFNM_ALNUM] = 1;
105 else if(strcmp(keyword, "alpha") == 0)
106 charset[CURLFNM_ALPHA] = 1;
107 else if(strcmp(keyword, "xdigit") == 0)
108 charset[CURLFNM_XDIGIT] = 1;
109 else if(strcmp(keyword, "print") == 0)
110 charset[CURLFNM_PRINT] = 1;
111 else if(strcmp(keyword, "graph") == 0)
112 charset[CURLFNM_GRAPH] = 1;
113 else if(strcmp(keyword, "space") == 0)
114 charset[CURLFNM_SPACE] = 1;
115 else if(strcmp(keyword, "blank") == 0)
116 charset[CURLFNM_BLANK] = 1;
117 else if(strcmp(keyword, "upper") == 0)
118 charset[CURLFNM_UPPER] = 1;
119 else if(strcmp(keyword, "lower") == 0)
120 charset[CURLFNM_LOWER] = 1;
121 else
122 return SETCHARSET_FAIL;
123 return SETCHARSET_OK;
124}
125
Elliott Hughescac39802018-04-27 16:19:43 -0700126/* Return the character class. */
127static char_class charclass(unsigned char c)
128{
129 if(ISUPPER(c))
130 return CCLASS_UPPER;
131 if(ISLOWER(c))
132 return CCLASS_LOWER;
133 if(ISDIGIT(c))
134 return CCLASS_DIGIT;
135 return CCLASS_OTHER;
136}
137
138/* Include a character or a range in set. */
139static void setcharorrange(unsigned char **pp, unsigned char *charset)
140{
141 unsigned char *p = (*pp)++;
142 unsigned char c = *p++;
143
144 charset[c] = 1;
145 if(ISALNUM(c) && *p++ == '-') {
146 char_class cc = charclass(c);
147 unsigned char endrange = *p++;
148
149 if(endrange == '\\')
150 endrange = *p++;
151 if(endrange >= c && charclass(endrange) == cc) {
152 while(c++ != endrange)
153 if(charclass(c) == cc) /* Chars in class may be not consecutive. */
154 charset[c] = 1;
155 *pp = p;
156 }
157 }
158}
159
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700160/* returns 1 (true) if pattern is OK, 0 if is bad ("p" is pattern pointer) */
161static int setcharset(unsigned char **p, unsigned char *charset)
162{
163 setcharset_state state = CURLFNM_SCHS_DEFAULT;
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700164 bool something_found = FALSE;
165 unsigned char c;
Elliott Hughescac39802018-04-27 16:19:43 -0700166
167 memset(charset, 0, CURLFNM_CHSET_SIZE);
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700168 for(;;) {
169 c = **p;
Alex Deymo486467e2017-12-19 19:04:07 +0100170 if(!c)
171 return SETCHARSET_FAIL;
172
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700173 switch(state) {
174 case CURLFNM_SCHS_DEFAULT:
Elliott Hughescac39802018-04-27 16:19:43 -0700175 if(c == ']') {
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700176 if(something_found)
177 return SETCHARSET_OK;
Elliott Hughes82be86d2017-09-20 17:00:17 -0700178 something_found = TRUE;
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700179 state = CURLFNM_SCHS_RIGHTBR;
180 charset[c] = 1;
181 (*p)++;
182 }
183 else if(c == '[') {
Elliott Hughescac39802018-04-27 16:19:43 -0700184 unsigned char *pp = *p + 1;
185
186 if(*pp++ == ':' && parsekeyword(&pp, charset))
187 *p = pp;
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700188 else {
189 charset[c] = 1;
190 (*p)++;
191 }
192 something_found = TRUE;
193 }
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700194 else if(c == '^' || c == '!') {
195 if(!something_found) {
196 if(charset[CURLFNM_NEGATE]) {
197 charset[c] = 1;
198 something_found = TRUE;
199 }
200 else
201 charset[CURLFNM_NEGATE] = 1; /* negate charset */
202 }
203 else
204 charset[c] = 1;
205 (*p)++;
206 }
207 else if(c == '\\') {
208 c = *(++(*p));
Elliott Hughescac39802018-04-27 16:19:43 -0700209 if(c)
210 setcharorrange(p, charset);
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700211 else
Elliott Hughescac39802018-04-27 16:19:43 -0700212 charset['\\'] = 1;
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700213 something_found = TRUE;
214 }
Elliott Hughescac39802018-04-27 16:19:43 -0700215 else {
216 setcharorrange(p, charset);
217 something_found = TRUE;
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700218 }
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700219 break;
220 case CURLFNM_SCHS_RIGHTBR:
221 if(c == '[') {
222 state = CURLFNM_SCHS_RIGHTBRLEFTBR;
223 charset[c] = 1;
224 (*p)++;
225 }
226 else if(c == ']') {
227 return SETCHARSET_OK;
228 }
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700229 else if(ISPRINT(c)) {
230 charset[c] = 1;
231 (*p)++;
232 state = CURLFNM_SCHS_DEFAULT;
233 }
234 else
235 /* used 'goto fail' instead of 'return SETCHARSET_FAIL' to avoid a
236 * nonsense warning 'statement not reached' at end of the fnc when
237 * compiling on Solaris */
238 goto fail;
239 break;
240 case CURLFNM_SCHS_RIGHTBRLEFTBR:
Elliott Hughescac39802018-04-27 16:19:43 -0700241 if(c == ']')
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700242 return SETCHARSET_OK;
Elliott Hughescac39802018-04-27 16:19:43 -0700243 state = CURLFNM_SCHS_DEFAULT;
244 charset[c] = 1;
245 (*p)++;
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700246 break;
247 }
248 }
249fail:
250 return SETCHARSET_FAIL;
251}
252
Elliott Hughes0128fe42018-02-27 14:57:55 -0800253static int loop(const unsigned char *pattern, const unsigned char *string,
254 int maxstars)
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700255{
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700256 unsigned char *p = (unsigned char *)pattern;
257 unsigned char *s = (unsigned char *)string;
258 unsigned char charset[CURLFNM_CHSET_SIZE] = { 0 };
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700259
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700260 for(;;) {
Elliott Hughescac39802018-04-27 16:19:43 -0700261 unsigned char *pp;
262
263 switch(*p) {
264 case '*':
265 if(!maxstars)
Elliott Hughes82be86d2017-09-20 17:00:17 -0700266 return CURL_FNMATCH_NOMATCH;
Elliott Hughescac39802018-04-27 16:19:43 -0700267 /* Regroup consecutive stars and question marks. This can be done because
268 '*?*?*' can be expressed as '??*'. */
269 for(;;) {
270 if(*++p == '\0')
271 return CURL_FNMATCH_MATCH;
272 if(*p == '?') {
273 if(!*s++)
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700274 return CURL_FNMATCH_NOMATCH;
275 }
Elliott Hughescac39802018-04-27 16:19:43 -0700276 else if(*p != '*')
277 break;
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700278 }
Elliott Hughescac39802018-04-27 16:19:43 -0700279 /* Skip string characters until we find a match with pattern suffix. */
280 for(maxstars--; *s; s++) {
281 if(loop(p, s, maxstars) == CURL_FNMATCH_MATCH)
282 return CURL_FNMATCH_MATCH;
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700283 }
Elliott Hughescac39802018-04-27 16:19:43 -0700284 return CURL_FNMATCH_NOMATCH;
285 case '?':
286 if(!*s)
287 return CURL_FNMATCH_NOMATCH;
288 s++;
289 p++;
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700290 break;
Elliott Hughescac39802018-04-27 16:19:43 -0700291 case '\0':
292 return *s? CURL_FNMATCH_NOMATCH: CURL_FNMATCH_MATCH;
293 case '\\':
294 if(p[1])
295 p++;
296 if(*s++ != *p++)
297 return CURL_FNMATCH_NOMATCH;
298 break;
299 case '[':
300 pp = p + 1; /* Copy in case of syntax error in set. */
301 if(setcharset(&pp, charset)) {
302 int found = FALSE;
303 if(!*s)
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700304 return CURL_FNMATCH_NOMATCH;
Elliott Hughescac39802018-04-27 16:19:43 -0700305 if(charset[(unsigned int)*s])
306 found = TRUE;
307 else if(charset[CURLFNM_ALNUM])
308 found = ISALNUM(*s);
309 else if(charset[CURLFNM_ALPHA])
310 found = ISALPHA(*s);
311 else if(charset[CURLFNM_DIGIT])
312 found = ISDIGIT(*s);
313 else if(charset[CURLFNM_XDIGIT])
314 found = ISXDIGIT(*s);
315 else if(charset[CURLFNM_PRINT])
316 found = ISPRINT(*s);
317 else if(charset[CURLFNM_SPACE])
318 found = ISSPACE(*s);
319 else if(charset[CURLFNM_UPPER])
320 found = ISUPPER(*s);
321 else if(charset[CURLFNM_LOWER])
322 found = ISLOWER(*s);
323 else if(charset[CURLFNM_BLANK])
324 found = ISBLANK(*s);
325 else if(charset[CURLFNM_GRAPH])
326 found = ISGRAPH(*s);
327
328 if(charset[CURLFNM_NEGATE])
329 found = !found;
330
331 if(!found)
332 return CURL_FNMATCH_NOMATCH;
333 p = pp + 1;
334 s++;
335 break;
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700336 }
Elliott Hughescac39802018-04-27 16:19:43 -0700337
338 /* Syntax error in set: this must be taken as a regular character. */
339 /* FALLTHROUGH */
340 default:
341 if(*p++ != *s++)
342 return CURL_FNMATCH_NOMATCH;
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700343 break;
344 }
345 }
346}
347
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700348/*
349 * @unittest: 1307
350 */
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700351int Curl_fnmatch(void *ptr, const char *pattern, const char *string)
352{
353 (void)ptr; /* the argument is specified by the curl_fnmatch_callback
354 prototype, but not used by Curl_fnmatch() */
355 if(!pattern || !string) {
356 return CURL_FNMATCH_FAIL;
357 }
Elliott Hughes0128fe42018-02-27 14:57:55 -0800358 return loop((unsigned char *)pattern, (unsigned char *)string, 5);
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700359}