1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2022, 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 * SPDX-License-Identifier: curl
23 ***************************************************************************/
25 #include "curl_setup.h"
26 #ifndef CURL_DISABLE_FTP
27 #include <curl/curl.h>
29 #include "curl_fnmatch.h"
30 #include "curl_memory.h"
32 /* The last #include file should be: */
37 #define CURLFNM_CHARSET_LEN (sizeof(char) * 256)
38 #define CURLFNM_CHSET_SIZE (CURLFNM_CHARSET_LEN + 15)
40 #define CURLFNM_NEGATE CURLFNM_CHARSET_LEN
42 #define CURLFNM_ALNUM (CURLFNM_CHARSET_LEN + 1)
43 #define CURLFNM_DIGIT (CURLFNM_CHARSET_LEN + 2)
44 #define CURLFNM_XDIGIT (CURLFNM_CHARSET_LEN + 3)
45 #define CURLFNM_ALPHA (CURLFNM_CHARSET_LEN + 4)
46 #define CURLFNM_PRINT (CURLFNM_CHARSET_LEN + 5)
47 #define CURLFNM_BLANK (CURLFNM_CHARSET_LEN + 6)
48 #define CURLFNM_LOWER (CURLFNM_CHARSET_LEN + 7)
49 #define CURLFNM_GRAPH (CURLFNM_CHARSET_LEN + 8)
50 #define CURLFNM_SPACE (CURLFNM_CHARSET_LEN + 9)
51 #define CURLFNM_UPPER (CURLFNM_CHARSET_LEN + 10)
54 CURLFNM_SCHS_DEFAULT = 0,
56 CURLFNM_SCHS_RIGHTBRLEFTBR
71 #define SETCHARSET_OK 1
72 #define SETCHARSET_FAIL 0
74 static int parsekeyword(unsigned char **pattern, unsigned char *charset)
76 parsekey_state state = CURLFNM_PKW_INIT;
78 char keyword[KEYLEN] = { 0 };
81 unsigned char *p = *pattern;
82 for(i = 0; !found; i++) {
85 return SETCHARSET_FAIL;
87 case CURLFNM_PKW_INIT:
91 state = CURLFNM_PKW_DDOT;
93 return SETCHARSET_FAIL;
95 case CURLFNM_PKW_DDOT:
99 return SETCHARSET_FAIL;
104 *pattern = p; /* move caller's pattern pointer */
105 if(strcmp(keyword, "digit") == 0)
106 charset[CURLFNM_DIGIT] = 1;
107 else if(strcmp(keyword, "alnum") == 0)
108 charset[CURLFNM_ALNUM] = 1;
109 else if(strcmp(keyword, "alpha") == 0)
110 charset[CURLFNM_ALPHA] = 1;
111 else if(strcmp(keyword, "xdigit") == 0)
112 charset[CURLFNM_XDIGIT] = 1;
113 else if(strcmp(keyword, "print") == 0)
114 charset[CURLFNM_PRINT] = 1;
115 else if(strcmp(keyword, "graph") == 0)
116 charset[CURLFNM_GRAPH] = 1;
117 else if(strcmp(keyword, "space") == 0)
118 charset[CURLFNM_SPACE] = 1;
119 else if(strcmp(keyword, "blank") == 0)
120 charset[CURLFNM_BLANK] = 1;
121 else if(strcmp(keyword, "upper") == 0)
122 charset[CURLFNM_UPPER] = 1;
123 else if(strcmp(keyword, "lower") == 0)
124 charset[CURLFNM_LOWER] = 1;
126 return SETCHARSET_FAIL;
127 return SETCHARSET_OK;
130 /* Return the character class. */
131 static char_class charclass(unsigned char c)
142 /* Include a character or a range in set. */
143 static void setcharorrange(unsigned char **pp, unsigned char *charset)
145 unsigned char *p = (*pp)++;
146 unsigned char c = *p++;
149 if(ISALNUM(c) && *p++ == '-') {
150 char_class cc = charclass(c);
151 unsigned char endrange = *p++;
155 if(endrange >= c && charclass(endrange) == cc) {
156 while(c++ != endrange)
157 if(charclass(c) == cc) /* Chars in class may be not consecutive. */
164 /* returns 1 (true) if pattern is OK, 0 if is bad ("p" is pattern pointer) */
165 static int setcharset(unsigned char **p, unsigned char *charset)
167 setcharset_state state = CURLFNM_SCHS_DEFAULT;
168 bool something_found = FALSE;
171 memset(charset, 0, CURLFNM_CHSET_SIZE);
175 return SETCHARSET_FAIL;
178 case CURLFNM_SCHS_DEFAULT:
181 return SETCHARSET_OK;
182 something_found = TRUE;
183 state = CURLFNM_SCHS_RIGHTBR;
188 unsigned char *pp = *p + 1;
190 if(*pp++ == ':' && parsekeyword(&pp, charset))
196 something_found = TRUE;
198 else if(c == '^' || c == '!') {
199 if(!something_found) {
200 if(charset[CURLFNM_NEGATE]) {
202 something_found = TRUE;
205 charset[CURLFNM_NEGATE] = 1; /* negate charset */
214 setcharorrange(p, charset);
217 something_found = TRUE;
220 setcharorrange(p, charset);
221 something_found = TRUE;
224 case CURLFNM_SCHS_RIGHTBR:
226 state = CURLFNM_SCHS_RIGHTBRLEFTBR;
231 return SETCHARSET_OK;
233 else if(ISPRINT(c)) {
236 state = CURLFNM_SCHS_DEFAULT;
239 /* used 'goto fail' instead of 'return SETCHARSET_FAIL' to avoid a
240 * nonsense warning 'statement not reached' at end of the fnc when
241 * compiling on Solaris */
244 case CURLFNM_SCHS_RIGHTBRLEFTBR:
246 return SETCHARSET_OK;
247 state = CURLFNM_SCHS_DEFAULT;
254 return SETCHARSET_FAIL;
257 static int loop(const unsigned char *pattern, const unsigned char *string,
260 unsigned char *p = (unsigned char *)pattern;
261 unsigned char *s = (unsigned char *)string;
262 unsigned char charset[CURLFNM_CHSET_SIZE] = { 0 };
270 return CURL_FNMATCH_NOMATCH;
271 /* Regroup consecutive stars and question marks. This can be done because
272 '*?*?*' can be expressed as '??*'. */
275 return CURL_FNMATCH_MATCH;
278 return CURL_FNMATCH_NOMATCH;
283 /* Skip string characters until we find a match with pattern suffix. */
284 for(maxstars--; *s; s++) {
285 if(loop(p, s, maxstars) == CURL_FNMATCH_MATCH)
286 return CURL_FNMATCH_MATCH;
288 return CURL_FNMATCH_NOMATCH;
291 return CURL_FNMATCH_NOMATCH;
296 return *s? CURL_FNMATCH_NOMATCH: CURL_FNMATCH_MATCH;
301 return CURL_FNMATCH_NOMATCH;
304 pp = p + 1; /* Copy in case of syntax error in set. */
305 if(setcharset(&pp, charset)) {
308 return CURL_FNMATCH_NOMATCH;
309 if(charset[(unsigned int)*s])
311 else if(charset[CURLFNM_ALNUM])
313 else if(charset[CURLFNM_ALPHA])
315 else if(charset[CURLFNM_DIGIT])
317 else if(charset[CURLFNM_XDIGIT])
318 found = ISXDIGIT(*s);
319 else if(charset[CURLFNM_PRINT])
321 else if(charset[CURLFNM_SPACE])
323 else if(charset[CURLFNM_UPPER])
325 else if(charset[CURLFNM_LOWER])
327 else if(charset[CURLFNM_BLANK])
329 else if(charset[CURLFNM_GRAPH])
332 if(charset[CURLFNM_NEGATE])
336 return CURL_FNMATCH_NOMATCH;
341 /* Syntax error in set; mismatch! */
342 return CURL_FNMATCH_NOMATCH;
346 return CURL_FNMATCH_NOMATCH;
355 int Curl_fnmatch(void *ptr, const char *pattern, const char *string)
357 (void)ptr; /* the argument is specified by the curl_fnmatch_callback
358 prototype, but not used by Curl_fnmatch() */
359 if(!pattern || !string) {
360 return CURL_FNMATCH_FAIL;
362 return loop((unsigned char *)pattern, (unsigned char *)string, 2);
369 int Curl_fnmatch(void *ptr, const char *pattern, const char *string)
372 (void)ptr; /* the argument is specified by the curl_fnmatch_callback
373 prototype, but not used by Curl_fnmatch() */
374 if(!pattern || !string) {
375 return CURL_FNMATCH_FAIL;
377 rc = fnmatch(pattern, string, 0);
380 return CURL_FNMATCH_MATCH;
382 return CURL_FNMATCH_NOMATCH;
384 return CURL_FNMATCH_FAIL;
391 #endif /* if FTP is disabled */