1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2020, 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.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"
24 #ifndef CURL_DISABLE_FTP
25 #include <curl/curl.h>
27 #include "curl_fnmatch.h"
28 #include "curl_memory.h"
30 /* The last #include file should be: */
35 #define CURLFNM_CHARSET_LEN (sizeof(char) * 256)
36 #define CURLFNM_CHSET_SIZE (CURLFNM_CHARSET_LEN + 15)
38 #define CURLFNM_NEGATE CURLFNM_CHARSET_LEN
40 #define CURLFNM_ALNUM (CURLFNM_CHARSET_LEN + 1)
41 #define CURLFNM_DIGIT (CURLFNM_CHARSET_LEN + 2)
42 #define CURLFNM_XDIGIT (CURLFNM_CHARSET_LEN + 3)
43 #define CURLFNM_ALPHA (CURLFNM_CHARSET_LEN + 4)
44 #define CURLFNM_PRINT (CURLFNM_CHARSET_LEN + 5)
45 #define CURLFNM_BLANK (CURLFNM_CHARSET_LEN + 6)
46 #define CURLFNM_LOWER (CURLFNM_CHARSET_LEN + 7)
47 #define CURLFNM_GRAPH (CURLFNM_CHARSET_LEN + 8)
48 #define CURLFNM_SPACE (CURLFNM_CHARSET_LEN + 9)
49 #define CURLFNM_UPPER (CURLFNM_CHARSET_LEN + 10)
52 CURLFNM_SCHS_DEFAULT = 0,
54 CURLFNM_SCHS_RIGHTBRLEFTBR
69 #define SETCHARSET_OK 1
70 #define SETCHARSET_FAIL 0
72 static int parsekeyword(unsigned char **pattern, unsigned char *charset)
74 parsekey_state state = CURLFNM_PKW_INIT;
76 char keyword[KEYLEN] = { 0 };
79 unsigned char *p = *pattern;
80 for(i = 0; !found; i++) {
83 return SETCHARSET_FAIL;
85 case CURLFNM_PKW_INIT:
89 state = CURLFNM_PKW_DDOT;
91 return SETCHARSET_FAIL;
93 case CURLFNM_PKW_DDOT:
97 return SETCHARSET_FAIL;
102 *pattern = p; /* move caller's pattern pointer */
103 if(strcmp(keyword, "digit") == 0)
104 charset[CURLFNM_DIGIT] = 1;
105 else if(strcmp(keyword, "alnum") == 0)
106 charset[CURLFNM_ALNUM] = 1;
107 else if(strcmp(keyword, "alpha") == 0)
108 charset[CURLFNM_ALPHA] = 1;
109 else if(strcmp(keyword, "xdigit") == 0)
110 charset[CURLFNM_XDIGIT] = 1;
111 else if(strcmp(keyword, "print") == 0)
112 charset[CURLFNM_PRINT] = 1;
113 else if(strcmp(keyword, "graph") == 0)
114 charset[CURLFNM_GRAPH] = 1;
115 else if(strcmp(keyword, "space") == 0)
116 charset[CURLFNM_SPACE] = 1;
117 else if(strcmp(keyword, "blank") == 0)
118 charset[CURLFNM_BLANK] = 1;
119 else if(strcmp(keyword, "upper") == 0)
120 charset[CURLFNM_UPPER] = 1;
121 else if(strcmp(keyword, "lower") == 0)
122 charset[CURLFNM_LOWER] = 1;
124 return SETCHARSET_FAIL;
125 return SETCHARSET_OK;
128 /* Return the character class. */
129 static char_class charclass(unsigned char c)
140 /* Include a character or a range in set. */
141 static void setcharorrange(unsigned char **pp, unsigned char *charset)
143 unsigned char *p = (*pp)++;
144 unsigned char c = *p++;
147 if(ISALNUM(c) && *p++ == '-') {
148 char_class cc = charclass(c);
149 unsigned char endrange = *p++;
153 if(endrange >= c && charclass(endrange) == cc) {
154 while(c++ != endrange)
155 if(charclass(c) == cc) /* Chars in class may be not consecutive. */
162 /* returns 1 (true) if pattern is OK, 0 if is bad ("p" is pattern pointer) */
163 static int setcharset(unsigned char **p, unsigned char *charset)
165 setcharset_state state = CURLFNM_SCHS_DEFAULT;
166 bool something_found = FALSE;
169 memset(charset, 0, CURLFNM_CHSET_SIZE);
173 return SETCHARSET_FAIL;
176 case CURLFNM_SCHS_DEFAULT:
179 return SETCHARSET_OK;
180 something_found = TRUE;
181 state = CURLFNM_SCHS_RIGHTBR;
186 unsigned char *pp = *p + 1;
188 if(*pp++ == ':' && parsekeyword(&pp, charset))
194 something_found = TRUE;
196 else if(c == '^' || c == '!') {
197 if(!something_found) {
198 if(charset[CURLFNM_NEGATE]) {
200 something_found = TRUE;
203 charset[CURLFNM_NEGATE] = 1; /* negate charset */
212 setcharorrange(p, charset);
215 something_found = TRUE;
218 setcharorrange(p, charset);
219 something_found = TRUE;
222 case CURLFNM_SCHS_RIGHTBR:
224 state = CURLFNM_SCHS_RIGHTBRLEFTBR;
229 return SETCHARSET_OK;
231 else if(ISPRINT(c)) {
234 state = CURLFNM_SCHS_DEFAULT;
237 /* used 'goto fail' instead of 'return SETCHARSET_FAIL' to avoid a
238 * nonsense warning 'statement not reached' at end of the fnc when
239 * compiling on Solaris */
242 case CURLFNM_SCHS_RIGHTBRLEFTBR:
244 return SETCHARSET_OK;
245 state = CURLFNM_SCHS_DEFAULT;
252 return SETCHARSET_FAIL;
255 static int loop(const unsigned char *pattern, const unsigned char *string,
258 unsigned char *p = (unsigned char *)pattern;
259 unsigned char *s = (unsigned char *)string;
260 unsigned char charset[CURLFNM_CHSET_SIZE] = { 0 };
268 return CURL_FNMATCH_NOMATCH;
269 /* Regroup consecutive stars and question marks. This can be done because
270 '*?*?*' can be expressed as '??*'. */
273 return CURL_FNMATCH_MATCH;
276 return CURL_FNMATCH_NOMATCH;
281 /* Skip string characters until we find a match with pattern suffix. */
282 for(maxstars--; *s; s++) {
283 if(loop(p, s, maxstars) == CURL_FNMATCH_MATCH)
284 return CURL_FNMATCH_MATCH;
286 return CURL_FNMATCH_NOMATCH;
289 return CURL_FNMATCH_NOMATCH;
294 return *s? CURL_FNMATCH_NOMATCH: CURL_FNMATCH_MATCH;
299 return CURL_FNMATCH_NOMATCH;
302 pp = p + 1; /* Copy in case of syntax error in set. */
303 if(setcharset(&pp, charset)) {
306 return CURL_FNMATCH_NOMATCH;
307 if(charset[(unsigned int)*s])
309 else if(charset[CURLFNM_ALNUM])
311 else if(charset[CURLFNM_ALPHA])
313 else if(charset[CURLFNM_DIGIT])
315 else if(charset[CURLFNM_XDIGIT])
316 found = ISXDIGIT(*s);
317 else if(charset[CURLFNM_PRINT])
319 else if(charset[CURLFNM_SPACE])
321 else if(charset[CURLFNM_UPPER])
323 else if(charset[CURLFNM_LOWER])
325 else if(charset[CURLFNM_BLANK])
327 else if(charset[CURLFNM_GRAPH])
330 if(charset[CURLFNM_NEGATE])
334 return CURL_FNMATCH_NOMATCH;
339 /* Syntax error in set; mismatch! */
340 return CURL_FNMATCH_NOMATCH;
344 return CURL_FNMATCH_NOMATCH;
353 int Curl_fnmatch(void *ptr, const char *pattern, const char *string)
355 (void)ptr; /* the argument is specified by the curl_fnmatch_callback
356 prototype, but not used by Curl_fnmatch() */
357 if(!pattern || !string) {
358 return CURL_FNMATCH_FAIL;
360 return loop((unsigned char *)pattern, (unsigned char *)string, 2);
367 int Curl_fnmatch(void *ptr, const char *pattern, const char *string)
370 (void)ptr; /* the argument is specified by the curl_fnmatch_callback
371 prototype, but not used by Curl_fnmatch() */
372 if(!pattern || !string) {
373 return CURL_FNMATCH_FAIL;
375 rc = fnmatch(pattern, string, 0);
378 return CURL_FNMATCH_MATCH;
380 return CURL_FNMATCH_NOMATCH;
382 return CURL_FNMATCH_FAIL;
389 #endif /* if FTP is disabled */