curl_fnmatch.c 11 KB

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