curl_fnmatch.c 10 KB

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