curl_fnmatch.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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. #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_LOOP_DEFAULT = 0,
  43. CURLFNM_LOOP_BACKSLASH
  44. } loop_state;
  45. typedef enum {
  46. CURLFNM_SCHS_DEFAULT = 0,
  47. CURLFNM_SCHS_MAYRANGE,
  48. CURLFNM_SCHS_MAYRANGE2,
  49. CURLFNM_SCHS_RIGHTBR,
  50. CURLFNM_SCHS_RIGHTBRLEFTBR
  51. } setcharset_state;
  52. typedef enum {
  53. CURLFNM_PKW_INIT = 0,
  54. CURLFNM_PKW_DDOT
  55. } parsekey_state;
  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(ISALPHA(c) && ISLOWER(c))
  73. keyword[i] = c;
  74. else if(c == ':')
  75. state = CURLFNM_PKW_DDOT;
  76. else
  77. return 0;
  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. /* returns 1 (true) if pattern is OK, 0 if is bad ("p" is pattern pointer) */
  113. static int setcharset(unsigned char **p, unsigned char *charset)
  114. {
  115. setcharset_state state = CURLFNM_SCHS_DEFAULT;
  116. unsigned char rangestart = 0;
  117. unsigned char lastchar = 0;
  118. bool something_found = FALSE;
  119. unsigned char c;
  120. for(;;) {
  121. c = **p;
  122. if(!c)
  123. return SETCHARSET_FAIL;
  124. switch(state) {
  125. case CURLFNM_SCHS_DEFAULT:
  126. if(ISALNUM(c)) { /* ASCII value */
  127. rangestart = c;
  128. charset[c] = 1;
  129. (*p)++;
  130. state = CURLFNM_SCHS_MAYRANGE;
  131. something_found = TRUE;
  132. }
  133. else if(c == ']') {
  134. if(something_found)
  135. return SETCHARSET_OK;
  136. something_found = TRUE;
  137. state = CURLFNM_SCHS_RIGHTBR;
  138. charset[c] = 1;
  139. (*p)++;
  140. }
  141. else if(c == '[') {
  142. char c2 = *((*p) + 1);
  143. if(c2 == ':') { /* there has to be a keyword */
  144. (*p) += 2;
  145. if(parsekeyword(p, charset)) {
  146. state = CURLFNM_SCHS_DEFAULT;
  147. }
  148. else
  149. return SETCHARSET_FAIL;
  150. }
  151. else {
  152. charset[c] = 1;
  153. (*p)++;
  154. }
  155. something_found = TRUE;
  156. }
  157. else if(c == '?' || c == '*') {
  158. something_found = TRUE;
  159. charset[c] = 1;
  160. (*p)++;
  161. }
  162. else if(c == '^' || c == '!') {
  163. if(!something_found) {
  164. if(charset[CURLFNM_NEGATE]) {
  165. charset[c] = 1;
  166. something_found = TRUE;
  167. }
  168. else
  169. charset[CURLFNM_NEGATE] = 1; /* negate charset */
  170. }
  171. else
  172. charset[c] = 1;
  173. (*p)++;
  174. }
  175. else if(c == '\\') {
  176. c = *(++(*p));
  177. if(ISPRINT((c))) {
  178. something_found = TRUE;
  179. state = CURLFNM_SCHS_MAYRANGE;
  180. charset[c] = 1;
  181. rangestart = c;
  182. (*p)++;
  183. }
  184. else
  185. return SETCHARSET_FAIL;
  186. }
  187. else {
  188. charset[c] = 1;
  189. (*p)++;
  190. something_found = TRUE;
  191. }
  192. break;
  193. case CURLFNM_SCHS_MAYRANGE:
  194. if(c == '-') {
  195. charset[c] = 1;
  196. (*p)++;
  197. lastchar = '-';
  198. state = CURLFNM_SCHS_MAYRANGE2;
  199. }
  200. else if(c == '[') {
  201. state = CURLFNM_SCHS_DEFAULT;
  202. }
  203. else if(ISALNUM(c)) {
  204. charset[c] = 1;
  205. (*p)++;
  206. }
  207. else if(c == '\\') {
  208. c = *(++(*p));
  209. if(ISPRINT(c)) {
  210. charset[c] = 1;
  211. (*p)++;
  212. }
  213. else
  214. return SETCHARSET_FAIL;
  215. }
  216. else if(c == ']') {
  217. return SETCHARSET_OK;
  218. }
  219. else
  220. return SETCHARSET_FAIL;
  221. break;
  222. case CURLFNM_SCHS_MAYRANGE2:
  223. if(c == ']') {
  224. return SETCHARSET_OK;
  225. }
  226. else if(c == '\\') {
  227. c = *(++(*p));
  228. if(ISPRINT(c)) {
  229. charset[c] = 1;
  230. state = CURLFNM_SCHS_DEFAULT;
  231. (*p)++;
  232. }
  233. else
  234. return SETCHARSET_FAIL;
  235. }
  236. else if(c >= rangestart) {
  237. if((ISLOWER(c) && ISLOWER(rangestart)) ||
  238. (ISDIGIT(c) && ISDIGIT(rangestart)) ||
  239. (ISUPPER(c) && ISUPPER(rangestart))) {
  240. charset[lastchar] = 0;
  241. rangestart++;
  242. while(rangestart++ <= c)
  243. charset[rangestart-1] = 1;
  244. (*p)++;
  245. state = CURLFNM_SCHS_DEFAULT;
  246. }
  247. else
  248. return SETCHARSET_FAIL;
  249. }
  250. else
  251. return SETCHARSET_FAIL;
  252. break;
  253. case CURLFNM_SCHS_RIGHTBR:
  254. if(c == '[') {
  255. state = CURLFNM_SCHS_RIGHTBRLEFTBR;
  256. charset[c] = 1;
  257. (*p)++;
  258. }
  259. else if(c == ']') {
  260. return SETCHARSET_OK;
  261. }
  262. else if(ISPRINT(c)) {
  263. charset[c] = 1;
  264. (*p)++;
  265. state = CURLFNM_SCHS_DEFAULT;
  266. }
  267. else
  268. /* used 'goto fail' instead of 'return SETCHARSET_FAIL' to avoid a
  269. * nonsense warning 'statement not reached' at end of the fnc when
  270. * compiling on Solaris */
  271. goto fail;
  272. break;
  273. case CURLFNM_SCHS_RIGHTBRLEFTBR:
  274. if(c == ']') {
  275. return SETCHARSET_OK;
  276. }
  277. else {
  278. state = CURLFNM_SCHS_DEFAULT;
  279. charset[c] = 1;
  280. (*p)++;
  281. }
  282. break;
  283. }
  284. }
  285. fail:
  286. return SETCHARSET_FAIL;
  287. }
  288. static int loop(const unsigned char *pattern, const unsigned char *string,
  289. int maxstars)
  290. {
  291. loop_state state = CURLFNM_LOOP_DEFAULT;
  292. unsigned char *p = (unsigned char *)pattern;
  293. unsigned char *s = (unsigned char *)string;
  294. unsigned char charset[CURLFNM_CHSET_SIZE] = { 0 };
  295. int rc = 0;
  296. for(;;) {
  297. switch(state) {
  298. case CURLFNM_LOOP_DEFAULT:
  299. if(*p == '*') {
  300. if(!maxstars)
  301. return CURL_FNMATCH_NOMATCH;
  302. while(*(p + 1) == '*') /* eliminate multiple stars */
  303. p++;
  304. if(*s == '\0' && *(p + 1) == '\0')
  305. return CURL_FNMATCH_MATCH;
  306. rc = loop(p + 1, s, maxstars - 1); /* *.txt matches .txt <=>
  307. .txt matches .txt */
  308. if(rc == CURL_FNMATCH_MATCH)
  309. return CURL_FNMATCH_MATCH;
  310. if(*s) /* let the star eat up one character */
  311. s++;
  312. else
  313. return CURL_FNMATCH_NOMATCH;
  314. }
  315. else if(*p == '?') {
  316. if(ISPRINT(*s)) {
  317. s++;
  318. p++;
  319. }
  320. else if(*s == '\0')
  321. return CURL_FNMATCH_NOMATCH;
  322. else
  323. return CURL_FNMATCH_FAIL; /* cannot deal with other character */
  324. }
  325. else if(*p == '\0') {
  326. if(*s == '\0')
  327. return CURL_FNMATCH_MATCH;
  328. return CURL_FNMATCH_NOMATCH;
  329. }
  330. else if(*p == '\\') {
  331. state = CURLFNM_LOOP_BACKSLASH;
  332. p++;
  333. }
  334. else if(*p == '[') {
  335. unsigned char *pp = p + 1; /* cannot handle with pointer to register */
  336. if(setcharset(&pp, charset)) {
  337. int found = FALSE;
  338. if(charset[(unsigned int)*s])
  339. found = TRUE;
  340. else if(charset[CURLFNM_ALNUM])
  341. found = ISALNUM(*s);
  342. else if(charset[CURLFNM_ALPHA])
  343. found = ISALPHA(*s);
  344. else if(charset[CURLFNM_DIGIT])
  345. found = ISDIGIT(*s);
  346. else if(charset[CURLFNM_XDIGIT])
  347. found = ISXDIGIT(*s);
  348. else if(charset[CURLFNM_PRINT])
  349. found = ISPRINT(*s);
  350. else if(charset[CURLFNM_SPACE])
  351. found = ISSPACE(*s);
  352. else if(charset[CURLFNM_UPPER])
  353. found = ISUPPER(*s);
  354. else if(charset[CURLFNM_LOWER])
  355. found = ISLOWER(*s);
  356. else if(charset[CURLFNM_BLANK])
  357. found = ISBLANK(*s);
  358. else if(charset[CURLFNM_GRAPH])
  359. found = ISGRAPH(*s);
  360. if(charset[CURLFNM_NEGATE])
  361. found = !found;
  362. if(found) {
  363. p = pp + 1;
  364. if(*s)
  365. /* don't advance if we're matching on an empty string */
  366. s++;
  367. memset(charset, 0, CURLFNM_CHSET_SIZE);
  368. }
  369. else
  370. return CURL_FNMATCH_NOMATCH;
  371. }
  372. else
  373. return CURL_FNMATCH_FAIL;
  374. }
  375. else {
  376. if(*p++ != *s++)
  377. return CURL_FNMATCH_NOMATCH;
  378. }
  379. break;
  380. case CURLFNM_LOOP_BACKSLASH:
  381. if(ISPRINT(*p)) {
  382. if(*p++ == *s++)
  383. state = CURLFNM_LOOP_DEFAULT;
  384. else
  385. return CURL_FNMATCH_NOMATCH;
  386. }
  387. else
  388. return CURL_FNMATCH_FAIL;
  389. break;
  390. }
  391. }
  392. }
  393. /*
  394. * @unittest: 1307
  395. */
  396. int Curl_fnmatch(void *ptr, const char *pattern, const char *string)
  397. {
  398. (void)ptr; /* the argument is specified by the curl_fnmatch_callback
  399. prototype, but not used by Curl_fnmatch() */
  400. if(!pattern || !string) {
  401. return CURL_FNMATCH_FAIL;
  402. }
  403. return loop((unsigned char *)pattern, (unsigned char *)string, 5);
  404. }