1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2011, 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 http://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 ***************************************************************************/
25 #include "curl_fnmatch.h"
27 #define _MPRINTF_REPLACE /* use our functions only */
28 #include <curl/mprintf.h>
30 #include "curl_memory.h"
31 /* The last #include file should be: */
34 #define CURLFNM_CHARSET_LEN (sizeof(char) * 256)
35 #define CURLFNM_CHSET_SIZE (CURLFNM_CHARSET_LEN + 15)
37 #define CURLFNM_NEGATE CURLFNM_CHARSET_LEN
39 #define CURLFNM_ALNUM (CURLFNM_CHARSET_LEN + 1)
40 #define CURLFNM_DIGIT (CURLFNM_CHARSET_LEN + 2)
41 #define CURLFNM_XDIGIT (CURLFNM_CHARSET_LEN + 3)
42 #define CURLFNM_ALPHA (CURLFNM_CHARSET_LEN + 4)
43 #define CURLFNM_PRINT (CURLFNM_CHARSET_LEN + 5)
44 #define CURLFNM_BLANK (CURLFNM_CHARSET_LEN + 6)
45 #define CURLFNM_LOWER (CURLFNM_CHARSET_LEN + 7)
46 #define CURLFNM_GRAPH (CURLFNM_CHARSET_LEN + 8)
47 #define CURLFNM_SPACE (CURLFNM_CHARSET_LEN + 9)
48 #define CURLFNM_UPPER (CURLFNM_CHARSET_LEN + 10)
51 CURLFNM_LOOP_DEFAULT = 0,
52 CURLFNM_LOOP_BACKSLASH
56 CURLFNM_SCHS_DEFAULT = 0,
57 CURLFNM_SCHS_MAYRANGE,
58 CURLFNM_SCHS_MAYRANGE2,
60 CURLFNM_SCHS_RIGHTBRLEFTBR
68 #define SETCHARSET_OK 1
69 #define SETCHARSET_FAIL 0
71 static int parsekeyword(unsigned char **pattern, unsigned char *charset)
73 parsekey_state state = CURLFNM_PKW_INIT;
75 char keyword[KEYLEN] = { 0 };
78 unsigned char *p = *pattern;
79 for(i = 0; !found; i++) {
82 return SETCHARSET_FAIL;
84 case CURLFNM_PKW_INIT:
85 if(ISALPHA(c) && ISLOWER(c))
88 state = CURLFNM_PKW_DDOT;
92 case CURLFNM_PKW_DDOT:
96 return SETCHARSET_FAIL;
101 *pattern = p; /* move caller's pattern pointer */
102 if(strcmp(keyword, "digit") == 0)
103 charset[CURLFNM_DIGIT] = 1;
104 else if(strcmp(keyword, "alnum") == 0)
105 charset[CURLFNM_ALNUM] = 1;
106 else if(strcmp(keyword, "alpha") == 0)
107 charset[CURLFNM_ALPHA] = 1;
108 else if(strcmp(keyword, "xdigit") == 0)
109 charset[CURLFNM_XDIGIT] = 1;
110 else if(strcmp(keyword, "print") == 0)
111 charset[CURLFNM_PRINT] = 1;
112 else if(strcmp(keyword, "graph") == 0)
113 charset[CURLFNM_GRAPH] = 1;
114 else if(strcmp(keyword, "space") == 0)
115 charset[CURLFNM_SPACE] = 1;
116 else if(strcmp(keyword, "blank") == 0)
117 charset[CURLFNM_BLANK] = 1;
118 else if(strcmp(keyword, "upper") == 0)
119 charset[CURLFNM_UPPER] = 1;
120 else if(strcmp(keyword, "lower") == 0)
121 charset[CURLFNM_LOWER] = 1;
123 return SETCHARSET_FAIL;
124 return SETCHARSET_OK;
127 /* returns 1 (true) if pattern is OK, 0 if is bad ("p" is pattern pointer) */
128 static int setcharset(unsigned char **p, unsigned char *charset)
130 setcharset_state state = CURLFNM_SCHS_DEFAULT;
131 unsigned char rangestart = 0;
132 unsigned char lastchar = 0;
133 bool something_found = FALSE;
138 case CURLFNM_SCHS_DEFAULT:
139 if(ISALNUM(c)) { /* ASCII value */
143 state = CURLFNM_SCHS_MAYRANGE;
144 something_found = TRUE;
148 return SETCHARSET_OK;
150 something_found = TRUE;
151 state = CURLFNM_SCHS_RIGHTBR;
157 if(c2 == ':') { /* there has to be a keyword */
159 if(parsekeyword(p, charset)) {
160 state = CURLFNM_SCHS_DEFAULT;
163 return SETCHARSET_FAIL;
169 something_found = TRUE;
171 else if(c == '?' || c == '*') {
172 something_found = TRUE;
176 else if(c == '^' || c == '!') {
177 if(!something_found) {
178 if(charset[CURLFNM_NEGATE]) {
180 something_found = TRUE;
183 charset[CURLFNM_NEGATE] = 1; /* negate charset */
192 something_found = TRUE;
193 state = CURLFNM_SCHS_MAYRANGE;
199 return SETCHARSET_FAIL;
202 return SETCHARSET_FAIL;
207 something_found = TRUE;
210 case CURLFNM_SCHS_MAYRANGE:
215 state = CURLFNM_SCHS_MAYRANGE2;
218 state = CURLFNM_SCHS_DEFAULT;
220 else if(ISALNUM(c)) {
231 return SETCHARSET_FAIL;
234 return SETCHARSET_OK;
237 return SETCHARSET_FAIL;
239 case CURLFNM_SCHS_MAYRANGE2:
243 return SETCHARSET_FAIL;
246 return SETCHARSET_OK;
252 state = CURLFNM_SCHS_DEFAULT;
256 return SETCHARSET_FAIL;
258 if(c >= rangestart) {
259 if((ISLOWER(c) && ISLOWER(rangestart)) ||
260 (ISDIGIT(c) && ISDIGIT(rangestart)) ||
261 (ISUPPER(c) && ISUPPER(rangestart))) {
262 charset[lastchar] = 0;
264 while(rangestart++ <= c)
265 charset[rangestart-1] = 1;
267 state = CURLFNM_SCHS_DEFAULT;
270 return SETCHARSET_FAIL;
273 case CURLFNM_SCHS_RIGHTBR:
275 state = CURLFNM_SCHS_RIGHTBRLEFTBR;
280 return SETCHARSET_OK;
283 return SETCHARSET_FAIL;
285 else if(ISPRINT(c)) {
288 state = CURLFNM_SCHS_DEFAULT;
291 /* used 'goto fail' instead of 'return SETCHARSET_FAIL' to avoid a
292 * nonsense warning 'statement not reached' at end of the fnc when
293 * compiling on Solaris */
296 case CURLFNM_SCHS_RIGHTBRLEFTBR:
298 return SETCHARSET_OK;
301 state = CURLFNM_SCHS_DEFAULT;
309 return SETCHARSET_FAIL;
312 static int loop(const unsigned char *pattern, const unsigned char *string)
314 loop_state state = CURLFNM_LOOP_DEFAULT;
315 unsigned char *p = (unsigned char *)pattern;
316 unsigned char *s = (unsigned char *)string;
317 unsigned char charset[CURLFNM_CHSET_SIZE] = { 0 };
322 case CURLFNM_LOOP_DEFAULT:
324 while(*(p+1) == '*') /* eliminate multiple stars */
326 if(*s == '\0' && *(p+1) == '\0')
327 return CURL_FNMATCH_MATCH;
328 rc = loop(p + 1, s); /* *.txt matches .txt <=> .txt matches .txt */
329 if(rc == CURL_FNMATCH_MATCH)
330 return CURL_FNMATCH_MATCH;
331 if(*s) /* let the star eat up one character */
334 return CURL_FNMATCH_NOMATCH;
342 return CURL_FNMATCH_NOMATCH;
344 return CURL_FNMATCH_FAIL; /* cannot deal with other character */
346 else if(*p == '\0') {
348 return CURL_FNMATCH_MATCH;
350 return CURL_FNMATCH_NOMATCH;
352 else if(*p == '\\') {
353 state = CURLFNM_LOOP_BACKSLASH;
357 unsigned char *pp = p+1; /* cannot handle with pointer to register */
358 if(setcharset(&pp, charset)) {
360 if(charset[(unsigned int)*s])
362 else if(charset[CURLFNM_ALNUM])
364 else if(charset[CURLFNM_ALPHA])
366 else if(charset[CURLFNM_DIGIT])
368 else if(charset[CURLFNM_XDIGIT])
369 found = ISXDIGIT(*s);
370 else if(charset[CURLFNM_PRINT])
372 else if(charset[CURLFNM_SPACE])
374 else if(charset[CURLFNM_UPPER])
376 else if(charset[CURLFNM_LOWER])
378 else if(charset[CURLFNM_BLANK])
380 else if(charset[CURLFNM_GRAPH])
383 if(charset[CURLFNM_NEGATE])
389 memset(charset, 0, CURLFNM_CHSET_SIZE);
392 return CURL_FNMATCH_NOMATCH;
395 return CURL_FNMATCH_FAIL;
399 return CURL_FNMATCH_NOMATCH;
402 case CURLFNM_LOOP_BACKSLASH:
405 state = CURLFNM_LOOP_DEFAULT;
407 return CURL_FNMATCH_NOMATCH;
410 return CURL_FNMATCH_FAIL;
416 int Curl_fnmatch(void *ptr, const char *pattern, const char *string)
418 (void)ptr; /* the argument is specified by the curl_fnmatch_callback
419 prototype, but not used by Curl_fnmatch() */
420 if(!pattern || !string) {
421 return CURL_FNMATCH_FAIL;
423 return loop((unsigned char *)pattern, (unsigned char *)string);