1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2018, Daniel Stenberg, <daniel@haxx.se>, et al.
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at https://curl.haxx.se/docs/copyright.html.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
23 #include "curl_setup.h"
25 #include <curl/curl.h>
27 #include "curl_fnmatch.h"
28 #include "curl_memory.h"
30 /* The last #include file should be: */
33 #define CURLFNM_CHARSET_LEN (sizeof(char) * 256)
34 #define CURLFNM_CHSET_SIZE (CURLFNM_CHARSET_LEN + 15)
36 #define CURLFNM_NEGATE CURLFNM_CHARSET_LEN
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)
50 CURLFNM_SCHS_DEFAULT = 0,
52 CURLFNM_SCHS_RIGHTBRLEFTBR
67 #define SETCHARSET_OK 1
68 #define SETCHARSET_FAIL 0
70 static int parsekeyword(unsigned char **pattern, unsigned char *charset)
72 parsekey_state state = CURLFNM_PKW_INIT;
74 char keyword[KEYLEN] = { 0 };
77 unsigned char *p = *pattern;
78 for(i = 0; !found; i++) {
81 return SETCHARSET_FAIL;
83 case CURLFNM_PKW_INIT:
87 state = CURLFNM_PKW_DDOT;
89 return SETCHARSET_FAIL;
91 case CURLFNM_PKW_DDOT:
95 return SETCHARSET_FAIL;
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;
122 return SETCHARSET_FAIL;
123 return SETCHARSET_OK;
126 /* Return the character class. */
127 static char_class charclass(unsigned char c)
138 /* Include a character or a range in set. */
139 static void setcharorrange(unsigned char **pp, unsigned char *charset)
141 unsigned char *p = (*pp)++;
142 unsigned char c = *p++;
145 if(ISALNUM(c) && *p++ == '-') {
146 char_class cc = charclass(c);
147 unsigned char 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. */
160 /* returns 1 (true) if pattern is OK, 0 if is bad ("p" is pattern pointer) */
161 static int setcharset(unsigned char **p, unsigned char *charset)
163 setcharset_state state = CURLFNM_SCHS_DEFAULT;
164 bool something_found = FALSE;
167 memset(charset, 0, CURLFNM_CHSET_SIZE);
171 return SETCHARSET_FAIL;
174 case CURLFNM_SCHS_DEFAULT:
177 return SETCHARSET_OK;
178 something_found = TRUE;
179 state = CURLFNM_SCHS_RIGHTBR;
184 unsigned char *pp = *p + 1;
186 if(*pp++ == ':' && parsekeyword(&pp, charset))
192 something_found = TRUE;
194 else if(c == '^' || c == '!') {
195 if(!something_found) {
196 if(charset[CURLFNM_NEGATE]) {
198 something_found = TRUE;
201 charset[CURLFNM_NEGATE] = 1; /* negate charset */
210 setcharorrange(p, charset);
213 something_found = TRUE;
216 setcharorrange(p, charset);
217 something_found = TRUE;
220 case CURLFNM_SCHS_RIGHTBR:
222 state = CURLFNM_SCHS_RIGHTBRLEFTBR;
227 return SETCHARSET_OK;
229 else if(ISPRINT(c)) {
232 state = CURLFNM_SCHS_DEFAULT;
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 */
240 case CURLFNM_SCHS_RIGHTBRLEFTBR:
242 return SETCHARSET_OK;
243 state = CURLFNM_SCHS_DEFAULT;
250 return SETCHARSET_FAIL;
253 static int loop(const unsigned char *pattern, const unsigned char *string,
256 unsigned char *p = (unsigned char *)pattern;
257 unsigned char *s = (unsigned char *)string;
258 unsigned char charset[CURLFNM_CHSET_SIZE] = { 0 };
266 return CURL_FNMATCH_NOMATCH;
267 /* Regroup consecutive stars and question marks. This can be done because
268 '*?*?*' can be expressed as '??*'. */
271 return CURL_FNMATCH_MATCH;
274 return CURL_FNMATCH_NOMATCH;
279 /* 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;
284 return CURL_FNMATCH_NOMATCH;
287 return CURL_FNMATCH_NOMATCH;
292 return *s? CURL_FNMATCH_NOMATCH: CURL_FNMATCH_MATCH;
297 return CURL_FNMATCH_NOMATCH;
300 pp = p + 1; /* Copy in case of syntax error in set. */
301 if(setcharset(&pp, charset)) {
304 return CURL_FNMATCH_NOMATCH;
305 if(charset[(unsigned int)*s])
307 else if(charset[CURLFNM_ALNUM])
309 else if(charset[CURLFNM_ALPHA])
311 else if(charset[CURLFNM_DIGIT])
313 else if(charset[CURLFNM_XDIGIT])
314 found = ISXDIGIT(*s);
315 else if(charset[CURLFNM_PRINT])
317 else if(charset[CURLFNM_SPACE])
319 else if(charset[CURLFNM_UPPER])
321 else if(charset[CURLFNM_LOWER])
323 else if(charset[CURLFNM_BLANK])
325 else if(charset[CURLFNM_GRAPH])
328 if(charset[CURLFNM_NEGATE])
332 return CURL_FNMATCH_NOMATCH;
338 /* Syntax error in set: this must be taken as a regular character. */
342 return CURL_FNMATCH_NOMATCH;
351 int Curl_fnmatch(void *ptr, const char *pattern, const char *string)
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;
358 return loop((unsigned char *)pattern, (unsigned char *)string, 5);