curl_fnmatch.c 9.5 KB

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