curl_fnmatch.c 10 KB

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