2010-05-12 21:33:22 +08:00
|
|
|
/***************************************************************************
|
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2023-01-02 20:51:48 +08:00
|
|
|
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
|
2010-05-12 21:33:22 +08:00
|
|
|
*
|
|
|
|
* This software is licensed as described in the file COPYING, which
|
|
|
|
* you should have received as part of this distribution. The terms
|
2020-11-04 21:02:01 +08:00
|
|
|
* are also available at https://curl.se/docs/copyright.html.
|
2010-05-12 21:33:22 +08:00
|
|
|
*
|
|
|
|
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
|
|
|
|
* copies of the Software, and permit persons to whom the Software is
|
|
|
|
* furnished to do so, under the terms of the COPYING file.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
2022-05-17 17:16:50 +08:00
|
|
|
* SPDX-License-Identifier: curl
|
|
|
|
*
|
2010-05-12 21:33:22 +08:00
|
|
|
***************************************************************************/
|
|
|
|
|
2013-01-07 02:06:49 +08:00
|
|
|
#include "curl_setup.h"
|
2019-02-07 18:35:26 +08:00
|
|
|
#ifndef CURL_DISABLE_FTP
|
2016-04-01 20:21:12 +08:00
|
|
|
#include <curl/curl.h>
|
|
|
|
|
2010-06-02 19:19:04 +08:00
|
|
|
#include "curl_fnmatch.h"
|
2010-06-02 20:13:02 +08:00
|
|
|
#include "curl_memory.h"
|
2015-03-03 19:36:18 +08:00
|
|
|
|
2010-06-02 20:13:02 +08:00
|
|
|
/* The last #include file should be: */
|
2013-01-04 09:50:28 +08:00
|
|
|
#include "memdebug.h"
|
2010-06-02 20:13:02 +08:00
|
|
|
|
2018-05-31 21:57:54 +08:00
|
|
|
#ifndef HAVE_FNMATCH
|
|
|
|
|
2010-05-12 21:33:22 +08:00
|
|
|
#define CURLFNM_CHARSET_LEN (sizeof(char) * 256)
|
|
|
|
#define CURLFNM_CHSET_SIZE (CURLFNM_CHARSET_LEN + 15)
|
|
|
|
|
|
|
|
#define CURLFNM_NEGATE CURLFNM_CHARSET_LEN
|
|
|
|
|
|
|
|
#define CURLFNM_ALNUM (CURLFNM_CHARSET_LEN + 1)
|
|
|
|
#define CURLFNM_DIGIT (CURLFNM_CHARSET_LEN + 2)
|
|
|
|
#define CURLFNM_XDIGIT (CURLFNM_CHARSET_LEN + 3)
|
|
|
|
#define CURLFNM_ALPHA (CURLFNM_CHARSET_LEN + 4)
|
|
|
|
#define CURLFNM_PRINT (CURLFNM_CHARSET_LEN + 5)
|
|
|
|
#define CURLFNM_BLANK (CURLFNM_CHARSET_LEN + 6)
|
|
|
|
#define CURLFNM_LOWER (CURLFNM_CHARSET_LEN + 7)
|
|
|
|
#define CURLFNM_GRAPH (CURLFNM_CHARSET_LEN + 8)
|
|
|
|
#define CURLFNM_SPACE (CURLFNM_CHARSET_LEN + 9)
|
|
|
|
#define CURLFNM_UPPER (CURLFNM_CHARSET_LEN + 10)
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
CURLFNM_SCHS_DEFAULT = 0,
|
|
|
|
CURLFNM_SCHS_RIGHTBR,
|
|
|
|
CURLFNM_SCHS_RIGHTBRLEFTBR
|
|
|
|
} setcharset_state;
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
CURLFNM_PKW_INIT = 0,
|
|
|
|
CURLFNM_PKW_DDOT
|
|
|
|
} parsekey_state;
|
|
|
|
|
2018-01-29 23:21:50 +08:00
|
|
|
typedef enum {
|
|
|
|
CCLASS_OTHER = 0,
|
|
|
|
CCLASS_DIGIT,
|
|
|
|
CCLASS_UPPER,
|
|
|
|
CCLASS_LOWER
|
|
|
|
} char_class;
|
|
|
|
|
2010-05-12 21:33:22 +08:00
|
|
|
#define SETCHARSET_OK 1
|
|
|
|
#define SETCHARSET_FAIL 0
|
|
|
|
|
|
|
|
static int parsekeyword(unsigned char **pattern, unsigned char *charset)
|
|
|
|
{
|
|
|
|
parsekey_state state = CURLFNM_PKW_INIT;
|
|
|
|
#define KEYLEN 10
|
|
|
|
char keyword[KEYLEN] = { 0 };
|
|
|
|
int i;
|
2010-05-27 22:34:57 +08:00
|
|
|
unsigned char *p = *pattern;
|
2022-10-29 00:32:09 +08:00
|
|
|
bool found = FALSE;
|
2010-05-12 21:33:22 +08:00
|
|
|
for(i = 0; !found; i++) {
|
|
|
|
char c = *p++;
|
|
|
|
if(i >= KEYLEN)
|
|
|
|
return SETCHARSET_FAIL;
|
|
|
|
switch(state) {
|
|
|
|
case CURLFNM_PKW_INIT:
|
2018-01-31 03:22:17 +08:00
|
|
|
if(ISLOWER(c))
|
2010-05-12 21:33:22 +08:00
|
|
|
keyword[i] = c;
|
|
|
|
else if(c == ':')
|
|
|
|
state = CURLFNM_PKW_DDOT;
|
|
|
|
else
|
2018-01-31 03:22:17 +08:00
|
|
|
return SETCHARSET_FAIL;
|
2010-05-12 21:33:22 +08:00
|
|
|
break;
|
|
|
|
case CURLFNM_PKW_DDOT:
|
|
|
|
if(c == ']')
|
|
|
|
found = TRUE;
|
|
|
|
else
|
|
|
|
return SETCHARSET_FAIL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#undef KEYLEN
|
|
|
|
|
|
|
|
*pattern = p; /* move caller's pattern pointer */
|
|
|
|
if(strcmp(keyword, "digit") == 0)
|
|
|
|
charset[CURLFNM_DIGIT] = 1;
|
|
|
|
else if(strcmp(keyword, "alnum") == 0)
|
|
|
|
charset[CURLFNM_ALNUM] = 1;
|
|
|
|
else if(strcmp(keyword, "alpha") == 0)
|
|
|
|
charset[CURLFNM_ALPHA] = 1;
|
|
|
|
else if(strcmp(keyword, "xdigit") == 0)
|
|
|
|
charset[CURLFNM_XDIGIT] = 1;
|
|
|
|
else if(strcmp(keyword, "print") == 0)
|
|
|
|
charset[CURLFNM_PRINT] = 1;
|
|
|
|
else if(strcmp(keyword, "graph") == 0)
|
|
|
|
charset[CURLFNM_GRAPH] = 1;
|
|
|
|
else if(strcmp(keyword, "space") == 0)
|
|
|
|
charset[CURLFNM_SPACE] = 1;
|
|
|
|
else if(strcmp(keyword, "blank") == 0)
|
|
|
|
charset[CURLFNM_BLANK] = 1;
|
|
|
|
else if(strcmp(keyword, "upper") == 0)
|
|
|
|
charset[CURLFNM_UPPER] = 1;
|
|
|
|
else if(strcmp(keyword, "lower") == 0)
|
|
|
|
charset[CURLFNM_LOWER] = 1;
|
|
|
|
else
|
|
|
|
return SETCHARSET_FAIL;
|
|
|
|
return SETCHARSET_OK;
|
|
|
|
}
|
|
|
|
|
2018-01-29 23:21:50 +08:00
|
|
|
/* Return the character class. */
|
|
|
|
static char_class charclass(unsigned char c)
|
|
|
|
{
|
|
|
|
if(ISUPPER(c))
|
|
|
|
return CCLASS_UPPER;
|
|
|
|
if(ISLOWER(c))
|
|
|
|
return CCLASS_LOWER;
|
|
|
|
if(ISDIGIT(c))
|
|
|
|
return CCLASS_DIGIT;
|
|
|
|
return CCLASS_OTHER;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Include a character or a range in set. */
|
|
|
|
static void setcharorrange(unsigned char **pp, unsigned char *charset)
|
|
|
|
{
|
|
|
|
unsigned char *p = (*pp)++;
|
|
|
|
unsigned char c = *p++;
|
|
|
|
|
|
|
|
charset[c] = 1;
|
|
|
|
if(ISALNUM(c) && *p++ == '-') {
|
|
|
|
char_class cc = charclass(c);
|
|
|
|
unsigned char endrange = *p++;
|
|
|
|
|
|
|
|
if(endrange == '\\')
|
|
|
|
endrange = *p++;
|
|
|
|
if(endrange >= c && charclass(endrange) == cc) {
|
|
|
|
while(c++ != endrange)
|
|
|
|
if(charclass(c) == cc) /* Chars in class may be not consecutive. */
|
|
|
|
charset[c] = 1;
|
|
|
|
*pp = p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-12 21:33:22 +08:00
|
|
|
/* returns 1 (true) if pattern is OK, 0 if is bad ("p" is pattern pointer) */
|
|
|
|
static int setcharset(unsigned char **p, unsigned char *charset)
|
|
|
|
{
|
|
|
|
setcharset_state state = CURLFNM_SCHS_DEFAULT;
|
|
|
|
bool something_found = FALSE;
|
2010-05-27 22:34:57 +08:00
|
|
|
unsigned char c;
|
2018-01-29 23:21:50 +08:00
|
|
|
|
|
|
|
memset(charset, 0, CURLFNM_CHSET_SIZE);
|
2010-05-12 21:33:22 +08:00
|
|
|
for(;;) {
|
|
|
|
c = **p;
|
2017-11-10 15:52:45 +08:00
|
|
|
if(!c)
|
|
|
|
return SETCHARSET_FAIL;
|
|
|
|
|
2010-05-13 05:32:42 +08:00
|
|
|
switch(state) {
|
2010-05-12 21:33:22 +08:00
|
|
|
case CURLFNM_SCHS_DEFAULT:
|
2018-01-29 23:21:50 +08:00
|
|
|
if(c == ']') {
|
2010-05-12 21:33:22 +08:00
|
|
|
if(something_found)
|
|
|
|
return SETCHARSET_OK;
|
2017-03-10 21:28:37 +08:00
|
|
|
something_found = TRUE;
|
2010-05-12 21:33:22 +08:00
|
|
|
state = CURLFNM_SCHS_RIGHTBR;
|
|
|
|
charset[c] = 1;
|
|
|
|
(*p)++;
|
|
|
|
}
|
|
|
|
else if(c == '[') {
|
2018-01-31 03:22:17 +08:00
|
|
|
unsigned char *pp = *p + 1;
|
|
|
|
|
|
|
|
if(*pp++ == ':' && parsekeyword(&pp, charset))
|
|
|
|
*p = pp;
|
2010-05-12 21:33:22 +08:00
|
|
|
else {
|
|
|
|
charset[c] = 1;
|
|
|
|
(*p)++;
|
|
|
|
}
|
|
|
|
something_found = TRUE;
|
|
|
|
}
|
|
|
|
else if(c == '^' || c == '!') {
|
|
|
|
if(!something_found) {
|
|
|
|
if(charset[CURLFNM_NEGATE]) {
|
|
|
|
charset[c] = 1;
|
2010-05-31 23:22:43 +08:00
|
|
|
something_found = TRUE;
|
2010-05-12 21:33:22 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
charset[CURLFNM_NEGATE] = 1; /* negate charset */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
charset[c] = 1;
|
|
|
|
(*p)++;
|
|
|
|
}
|
|
|
|
else if(c == '\\') {
|
|
|
|
c = *(++(*p));
|
2018-01-29 23:21:50 +08:00
|
|
|
if(c)
|
|
|
|
setcharorrange(p, charset);
|
2010-05-12 21:33:22 +08:00
|
|
|
else
|
2018-01-29 23:21:50 +08:00
|
|
|
charset['\\'] = 1;
|
|
|
|
something_found = TRUE;
|
2010-05-12 21:33:22 +08:00
|
|
|
}
|
|
|
|
else {
|
2018-01-29 23:21:50 +08:00
|
|
|
setcharorrange(p, charset);
|
2010-05-12 21:33:22 +08:00
|
|
|
something_found = TRUE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case CURLFNM_SCHS_RIGHTBR:
|
|
|
|
if(c == '[') {
|
|
|
|
state = CURLFNM_SCHS_RIGHTBRLEFTBR;
|
|
|
|
charset[c] = 1;
|
|
|
|
(*p)++;
|
|
|
|
}
|
|
|
|
else if(c == ']') {
|
|
|
|
return SETCHARSET_OK;
|
|
|
|
}
|
2010-05-13 05:32:42 +08:00
|
|
|
else if(ISPRINT(c)) {
|
2010-05-12 21:33:22 +08:00
|
|
|
charset[c] = 1;
|
|
|
|
(*p)++;
|
|
|
|
state = CURLFNM_SCHS_DEFAULT;
|
|
|
|
}
|
|
|
|
else
|
2010-05-28 15:45:17 +08:00
|
|
|
/* used 'goto fail' instead of 'return SETCHARSET_FAIL' to avoid a
|
|
|
|
* nonsense warning 'statement not reached' at end of the fnc when
|
|
|
|
* compiling on Solaris */
|
|
|
|
goto fail;
|
2010-05-12 21:33:22 +08:00
|
|
|
break;
|
|
|
|
case CURLFNM_SCHS_RIGHTBRLEFTBR:
|
2018-01-31 03:22:17 +08:00
|
|
|
if(c == ']')
|
2010-05-12 21:33:22 +08:00
|
|
|
return SETCHARSET_OK;
|
2018-01-31 03:22:17 +08:00
|
|
|
state = CURLFNM_SCHS_DEFAULT;
|
|
|
|
charset[c] = 1;
|
|
|
|
(*p)++;
|
2010-05-12 21:33:22 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2010-05-28 15:45:17 +08:00
|
|
|
fail:
|
2010-05-12 21:33:22 +08:00
|
|
|
return SETCHARSET_FAIL;
|
|
|
|
}
|
|
|
|
|
2018-01-16 22:55:44 +08:00
|
|
|
static int loop(const unsigned char *pattern, const unsigned char *string,
|
|
|
|
int maxstars)
|
2010-05-12 21:33:22 +08:00
|
|
|
{
|
2010-05-27 22:34:57 +08:00
|
|
|
unsigned char *p = (unsigned char *)pattern;
|
|
|
|
unsigned char *s = (unsigned char *)string;
|
2010-05-12 21:33:22 +08:00
|
|
|
unsigned char charset[CURLFNM_CHSET_SIZE] = { 0 };
|
|
|
|
|
2011-04-20 21:17:42 +08:00
|
|
|
for(;;) {
|
2018-01-31 03:22:17 +08:00
|
|
|
unsigned char *pp;
|
|
|
|
|
|
|
|
switch(*p) {
|
|
|
|
case '*':
|
|
|
|
if(!maxstars)
|
2017-03-10 21:28:37 +08:00
|
|
|
return CURL_FNMATCH_NOMATCH;
|
2018-02-07 22:01:51 +08:00
|
|
|
/* Regroup consecutive stars and question marks. This can be done because
|
|
|
|
'*?*?*' can be expressed as '??*'. */
|
|
|
|
for(;;) {
|
|
|
|
if(*++p == '\0')
|
|
|
|
return CURL_FNMATCH_MATCH;
|
|
|
|
if(*p == '?') {
|
|
|
|
if(!*s++)
|
|
|
|
return CURL_FNMATCH_NOMATCH;
|
|
|
|
}
|
|
|
|
else if(*p != '*')
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* Skip string characters until we find a match with pattern suffix. */
|
|
|
|
for(maxstars--; *s; s++) {
|
|
|
|
if(loop(p, s, maxstars) == CURL_FNMATCH_MATCH)
|
|
|
|
return CURL_FNMATCH_MATCH;
|
|
|
|
}
|
|
|
|
return CURL_FNMATCH_NOMATCH;
|
2018-01-31 03:22:17 +08:00
|
|
|
case '?':
|
|
|
|
if(!*s)
|
|
|
|
return CURL_FNMATCH_NOMATCH;
|
|
|
|
s++;
|
|
|
|
p++;
|
|
|
|
break;
|
|
|
|
case '\0':
|
|
|
|
return *s? CURL_FNMATCH_NOMATCH: CURL_FNMATCH_MATCH;
|
|
|
|
case '\\':
|
|
|
|
if(p[1])
|
|
|
|
p++;
|
|
|
|
if(*s++ != *p++)
|
|
|
|
return CURL_FNMATCH_NOMATCH;
|
|
|
|
break;
|
|
|
|
case '[':
|
|
|
|
pp = p + 1; /* Copy in case of syntax error in set. */
|
|
|
|
if(setcharset(&pp, charset)) {
|
|
|
|
int found = FALSE;
|
|
|
|
if(!*s)
|
|
|
|
return CURL_FNMATCH_NOMATCH;
|
|
|
|
if(charset[(unsigned int)*s])
|
|
|
|
found = TRUE;
|
|
|
|
else if(charset[CURLFNM_ALNUM])
|
|
|
|
found = ISALNUM(*s);
|
|
|
|
else if(charset[CURLFNM_ALPHA])
|
|
|
|
found = ISALPHA(*s);
|
|
|
|
else if(charset[CURLFNM_DIGIT])
|
|
|
|
found = ISDIGIT(*s);
|
|
|
|
else if(charset[CURLFNM_XDIGIT])
|
|
|
|
found = ISXDIGIT(*s);
|
|
|
|
else if(charset[CURLFNM_PRINT])
|
|
|
|
found = ISPRINT(*s);
|
|
|
|
else if(charset[CURLFNM_SPACE])
|
|
|
|
found = ISSPACE(*s);
|
|
|
|
else if(charset[CURLFNM_UPPER])
|
|
|
|
found = ISUPPER(*s);
|
|
|
|
else if(charset[CURLFNM_LOWER])
|
|
|
|
found = ISLOWER(*s);
|
|
|
|
else if(charset[CURLFNM_BLANK])
|
|
|
|
found = ISBLANK(*s);
|
|
|
|
else if(charset[CURLFNM_GRAPH])
|
|
|
|
found = ISGRAPH(*s);
|
2010-05-12 21:33:22 +08:00
|
|
|
|
2018-01-31 03:22:17 +08:00
|
|
|
if(charset[CURLFNM_NEGATE])
|
|
|
|
found = !found;
|
2010-05-12 21:33:22 +08:00
|
|
|
|
2018-01-31 03:22:17 +08:00
|
|
|
if(!found)
|
2010-05-12 21:33:22 +08:00
|
|
|
return CURL_FNMATCH_NOMATCH;
|
2018-01-31 03:22:17 +08:00
|
|
|
p = pp + 1;
|
|
|
|
s++;
|
|
|
|
break;
|
2010-05-12 21:33:22 +08:00
|
|
|
}
|
2018-05-28 05:24:27 +08:00
|
|
|
/* Syntax error in set; mismatch! */
|
|
|
|
return CURL_FNMATCH_NOMATCH;
|
2018-01-31 03:22:17 +08:00
|
|
|
|
|
|
|
default:
|
|
|
|
if(*p++ != *s++)
|
|
|
|
return CURL_FNMATCH_NOMATCH;
|
2010-05-12 21:33:22 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-10 20:40:46 +08:00
|
|
|
/*
|
|
|
|
* @unittest: 1307
|
|
|
|
*/
|
2010-05-16 08:49:08 +08:00
|
|
|
int Curl_fnmatch(void *ptr, const char *pattern, const char *string)
|
2010-05-12 21:33:22 +08:00
|
|
|
{
|
2010-05-16 08:49:08 +08:00
|
|
|
(void)ptr; /* the argument is specified by the curl_fnmatch_callback
|
|
|
|
prototype, but not used by Curl_fnmatch() */
|
2010-05-12 21:33:22 +08:00
|
|
|
if(!pattern || !string) {
|
|
|
|
return CURL_FNMATCH_FAIL;
|
|
|
|
}
|
2018-05-18 22:48:13 +08:00
|
|
|
return loop((unsigned char *)pattern, (unsigned char *)string, 2);
|
2010-05-12 21:33:22 +08:00
|
|
|
}
|
2018-05-31 21:57:54 +08:00
|
|
|
#else
|
|
|
|
#include <fnmatch.h>
|
|
|
|
/*
|
|
|
|
* @unittest: 1307
|
|
|
|
*/
|
|
|
|
int Curl_fnmatch(void *ptr, const char *pattern, const char *string)
|
|
|
|
{
|
|
|
|
(void)ptr; /* the argument is specified by the curl_fnmatch_callback
|
|
|
|
prototype, but not used by Curl_fnmatch() */
|
|
|
|
if(!pattern || !string) {
|
|
|
|
return CURL_FNMATCH_FAIL;
|
|
|
|
}
|
2022-10-29 00:32:09 +08:00
|
|
|
|
|
|
|
switch(fnmatch(pattern, string, 0)) {
|
2018-05-31 21:57:54 +08:00
|
|
|
case 0:
|
|
|
|
return CURL_FNMATCH_MATCH;
|
|
|
|
case FNM_NOMATCH:
|
|
|
|
return CURL_FNMATCH_NOMATCH;
|
|
|
|
default:
|
|
|
|
return CURL_FNMATCH_FAIL;
|
|
|
|
}
|
|
|
|
/* not reached */
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
2019-02-07 18:35:26 +08:00
|
|
|
|
|
|
|
#endif /* if FTP is disabled */
|