curl_fnmatch.c 10 KB

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