curl_fnmatch.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2022, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  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.
  13. *
  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.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "curl_setup.h"
  25. #ifndef CURL_DISABLE_FTP
  26. #include <curl/curl.h>
  27. #include "curl_fnmatch.h"
  28. #include "curl_memory.h"
  29. /* The last #include file should be: */
  30. #include "memdebug.h"
  31. #ifndef HAVE_FNMATCH
  32. #define CURLFNM_CHARSET_LEN (sizeof(char) * 256)
  33. #define CURLFNM_CHSET_SIZE (CURLFNM_CHARSET_LEN + 15)
  34. #define CURLFNM_NEGATE CURLFNM_CHARSET_LEN
  35. #define CURLFNM_ALNUM (CURLFNM_CHARSET_LEN + 1)
  36. #define CURLFNM_DIGIT (CURLFNM_CHARSET_LEN + 2)
  37. #define CURLFNM_XDIGIT (CURLFNM_CHARSET_LEN + 3)
  38. #define CURLFNM_ALPHA (CURLFNM_CHARSET_LEN + 4)
  39. #define CURLFNM_PRINT (CURLFNM_CHARSET_LEN + 5)
  40. #define CURLFNM_BLANK (CURLFNM_CHARSET_LEN + 6)
  41. #define CURLFNM_LOWER (CURLFNM_CHARSET_LEN + 7)
  42. #define CURLFNM_GRAPH (CURLFNM_CHARSET_LEN + 8)
  43. #define CURLFNM_SPACE (CURLFNM_CHARSET_LEN + 9)
  44. #define CURLFNM_UPPER (CURLFNM_CHARSET_LEN + 10)
  45. typedef enum {
  46. CURLFNM_SCHS_DEFAULT = 0,
  47. CURLFNM_SCHS_RIGHTBR,
  48. CURLFNM_SCHS_RIGHTBRLEFTBR
  49. } setcharset_state;
  50. typedef enum {
  51. CURLFNM_PKW_INIT = 0,
  52. CURLFNM_PKW_DDOT
  53. } parsekey_state;
  54. typedef enum {
  55. CCLASS_OTHER = 0,
  56. CCLASS_DIGIT,
  57. CCLASS_UPPER,
  58. CCLASS_LOWER
  59. } char_class;
  60. #define SETCHARSET_OK 1
  61. #define SETCHARSET_FAIL 0
  62. static int parsekeyword(unsigned char **pattern, unsigned char *charset)
  63. {
  64. parsekey_state state = CURLFNM_PKW_INIT;
  65. #define KEYLEN 10
  66. char keyword[KEYLEN] = { 0 };
  67. int found = FALSE;
  68. int i;
  69. unsigned char *p = *pattern;
  70. for(i = 0; !found; i++) {
  71. char c = *p++;
  72. if(i >= KEYLEN)
  73. return SETCHARSET_FAIL;
  74. switch(state) {
  75. case CURLFNM_PKW_INIT:
  76. if(ISLOWER(c))
  77. keyword[i] = c;
  78. else if(c == ':')
  79. state = CURLFNM_PKW_DDOT;
  80. else
  81. return SETCHARSET_FAIL;
  82. break;
  83. case CURLFNM_PKW_DDOT:
  84. if(c == ']')
  85. found = TRUE;
  86. else
  87. return SETCHARSET_FAIL;
  88. }
  89. }
  90. #undef KEYLEN
  91. *pattern = p; /* move caller's pattern pointer */
  92. if(strcmp(keyword, "digit") == 0)
  93. charset[CURLFNM_DIGIT] = 1;
  94. else if(strcmp(keyword, "alnum") == 0)
  95. charset[CURLFNM_ALNUM] = 1;
  96. else if(strcmp(keyword, "alpha") == 0)
  97. charset[CURLFNM_ALPHA] = 1;
  98. else if(strcmp(keyword, "xdigit") == 0)
  99. charset[CURLFNM_XDIGIT] = 1;
  100. else if(strcmp(keyword, "print") == 0)
  101. charset[CURLFNM_PRINT] = 1;
  102. else if(strcmp(keyword, "graph") == 0)
  103. charset[CURLFNM_GRAPH] = 1;
  104. else if(strcmp(keyword, "space") == 0)
  105. charset[CURLFNM_SPACE] = 1;
  106. else if(strcmp(keyword, "blank") == 0)
  107. charset[CURLFNM_BLANK] = 1;
  108. else if(strcmp(keyword, "upper") == 0)
  109. charset[CURLFNM_UPPER] = 1;
  110. else if(strcmp(keyword, "lower") == 0)
  111. charset[CURLFNM_LOWER] = 1;
  112. else
  113. return SETCHARSET_FAIL;
  114. return SETCHARSET_OK;
  115. }
  116. /* Return the character class. */
  117. static char_class charclass(unsigned char c)
  118. {
  119. if(ISUPPER(c))
  120. return CCLASS_UPPER;
  121. if(ISLOWER(c))
  122. return CCLASS_LOWER;
  123. if(ISDIGIT(c))
  124. return CCLASS_DIGIT;
  125. return CCLASS_OTHER;
  126. }
  127. /* Include a character or a range in set. */
  128. static void setcharorrange(unsigned char **pp, unsigned char *charset)
  129. {
  130. unsigned char *p = (*pp)++;
  131. unsigned char c = *p++;
  132. charset[c] = 1;
  133. if(ISALNUM(c) && *p++ == '-') {
  134. char_class cc = charclass(c);
  135. unsigned char endrange = *p++;
  136. if(endrange == '\\')
  137. endrange = *p++;
  138. if(endrange >= c && charclass(endrange) == cc) {
  139. while(c++ != endrange)
  140. if(charclass(c) == cc) /* Chars in class may be not consecutive. */
  141. charset[c] = 1;
  142. *pp = p;
  143. }
  144. }
  145. }
  146. /* returns 1 (true) if pattern is OK, 0 if is bad ("p" is pattern pointer) */
  147. static int setcharset(unsigned char **p, unsigned char *charset)
  148. {
  149. setcharset_state state = CURLFNM_SCHS_DEFAULT;
  150. bool something_found = FALSE;
  151. unsigned char c;
  152. memset(charset, 0, CURLFNM_CHSET_SIZE);
  153. for(;;) {
  154. c = **p;
  155. if(!c)
  156. return SETCHARSET_FAIL;
  157. switch(state) {
  158. case CURLFNM_SCHS_DEFAULT:
  159. if(c == ']') {
  160. if(something_found)
  161. return SETCHARSET_OK;
  162. something_found = TRUE;
  163. state = CURLFNM_SCHS_RIGHTBR;
  164. charset[c] = 1;
  165. (*p)++;
  166. }
  167. else if(c == '[') {
  168. unsigned char *pp = *p + 1;
  169. if(*pp++ == ':' && parsekeyword(&pp, charset))
  170. *p = pp;
  171. else {
  172. charset[c] = 1;
  173. (*p)++;
  174. }
  175. something_found = TRUE;
  176. }
  177. else if(c == '^' || c == '!') {
  178. if(!something_found) {
  179. if(charset[CURLFNM_NEGATE]) {
  180. charset[c] = 1;
  181. something_found = TRUE;
  182. }
  183. else
  184. charset[CURLFNM_NEGATE] = 1; /* negate charset */
  185. }
  186. else
  187. charset[c] = 1;
  188. (*p)++;
  189. }
  190. else if(c == '\\') {
  191. c = *(++(*p));
  192. if(c)
  193. setcharorrange(p, charset);
  194. else
  195. charset['\\'] = 1;
  196. something_found = TRUE;
  197. }
  198. else {
  199. setcharorrange(p, charset);
  200. something_found = TRUE;
  201. }
  202. break;
  203. case CURLFNM_SCHS_RIGHTBR:
  204. if(c == '[') {
  205. state = CURLFNM_SCHS_RIGHTBRLEFTBR;
  206. charset[c] = 1;
  207. (*p)++;
  208. }
  209. else if(c == ']') {
  210. return SETCHARSET_OK;
  211. }
  212. else if(ISPRINT(c)) {
  213. charset[c] = 1;
  214. (*p)++;
  215. state = CURLFNM_SCHS_DEFAULT;
  216. }
  217. else
  218. /* used 'goto fail' instead of 'return SETCHARSET_FAIL' to avoid a
  219. * nonsense warning 'statement not reached' at end of the fnc when
  220. * compiling on Solaris */
  221. goto fail;
  222. break;
  223. case CURLFNM_SCHS_RIGHTBRLEFTBR:
  224. if(c == ']')
  225. return SETCHARSET_OK;
  226. state = CURLFNM_SCHS_DEFAULT;
  227. charset[c] = 1;
  228. (*p)++;
  229. break;
  230. }
  231. }
  232. fail:
  233. return SETCHARSET_FAIL;
  234. }
  235. static int loop(const unsigned char *pattern, const unsigned char *string,
  236. int maxstars)
  237. {
  238. unsigned char *p = (unsigned char *)pattern;
  239. unsigned char *s = (unsigned char *)string;
  240. unsigned char charset[CURLFNM_CHSET_SIZE] = { 0 };
  241. for(;;) {
  242. unsigned char *pp;
  243. switch(*p) {
  244. case '*':
  245. if(!maxstars)
  246. return CURL_FNMATCH_NOMATCH;
  247. /* Regroup consecutive stars and question marks. This can be done because
  248. '*?*?*' can be expressed as '??*'. */
  249. for(;;) {
  250. if(*++p == '\0')
  251. return CURL_FNMATCH_MATCH;
  252. if(*p == '?') {
  253. if(!*s++)
  254. return CURL_FNMATCH_NOMATCH;
  255. }
  256. else if(*p != '*')
  257. break;
  258. }
  259. /* Skip string characters until we find a match with pattern suffix. */
  260. for(maxstars--; *s; s++) {
  261. if(loop(p, s, maxstars) == CURL_FNMATCH_MATCH)
  262. return CURL_FNMATCH_MATCH;
  263. }
  264. return CURL_FNMATCH_NOMATCH;
  265. case '?':
  266. if(!*s)
  267. return CURL_FNMATCH_NOMATCH;
  268. s++;
  269. p++;
  270. break;
  271. case '\0':
  272. return *s? CURL_FNMATCH_NOMATCH: CURL_FNMATCH_MATCH;
  273. case '\\':
  274. if(p[1])
  275. p++;
  276. if(*s++ != *p++)
  277. return CURL_FNMATCH_NOMATCH;
  278. break;
  279. case '[':
  280. pp = p + 1; /* Copy in case of syntax error in set. */
  281. if(setcharset(&pp, charset)) {
  282. int found = FALSE;
  283. if(!*s)
  284. return CURL_FNMATCH_NOMATCH;
  285. if(charset[(unsigned int)*s])
  286. found = TRUE;
  287. else if(charset[CURLFNM_ALNUM])
  288. found = ISALNUM(*s);
  289. else if(charset[CURLFNM_ALPHA])
  290. found = ISALPHA(*s);
  291. else if(charset[CURLFNM_DIGIT])
  292. found = ISDIGIT(*s);
  293. else if(charset[CURLFNM_XDIGIT])
  294. found = ISXDIGIT(*s);
  295. else if(charset[CURLFNM_PRINT])
  296. found = ISPRINT(*s);
  297. else if(charset[CURLFNM_SPACE])
  298. found = ISSPACE(*s);
  299. else if(charset[CURLFNM_UPPER])
  300. found = ISUPPER(*s);
  301. else if(charset[CURLFNM_LOWER])
  302. found = ISLOWER(*s);
  303. else if(charset[CURLFNM_BLANK])
  304. found = ISBLANK(*s);
  305. else if(charset[CURLFNM_GRAPH])
  306. found = ISGRAPH(*s);
  307. if(charset[CURLFNM_NEGATE])
  308. found = !found;
  309. if(!found)
  310. return CURL_FNMATCH_NOMATCH;
  311. p = pp + 1;
  312. s++;
  313. break;
  314. }
  315. /* Syntax error in set; mismatch! */
  316. return CURL_FNMATCH_NOMATCH;
  317. default:
  318. if(*p++ != *s++)
  319. return CURL_FNMATCH_NOMATCH;
  320. break;
  321. }
  322. }
  323. }
  324. /*
  325. * @unittest: 1307
  326. */
  327. int Curl_fnmatch(void *ptr, const char *pattern, const char *string)
  328. {
  329. (void)ptr; /* the argument is specified by the curl_fnmatch_callback
  330. prototype, but not used by Curl_fnmatch() */
  331. if(!pattern || !string) {
  332. return CURL_FNMATCH_FAIL;
  333. }
  334. return loop((unsigned char *)pattern, (unsigned char *)string, 2);
  335. }
  336. #else
  337. #include <fnmatch.h>
  338. /*
  339. * @unittest: 1307
  340. */
  341. int Curl_fnmatch(void *ptr, const char *pattern, const char *string)
  342. {
  343. int rc;
  344. (void)ptr; /* the argument is specified by the curl_fnmatch_callback
  345. prototype, but not used by Curl_fnmatch() */
  346. if(!pattern || !string) {
  347. return CURL_FNMATCH_FAIL;
  348. }
  349. rc = fnmatch(pattern, string, 0);
  350. switch(rc) {
  351. case 0:
  352. return CURL_FNMATCH_MATCH;
  353. case FNM_NOMATCH:
  354. return CURL_FNMATCH_NOMATCH;
  355. default:
  356. return CURL_FNMATCH_FAIL;
  357. }
  358. /* not reached */
  359. }
  360. #endif
  361. #endif /* if FTP is disabled */