wildcard.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. /*
  2. * Wildcard matching engine for use with SFTP-based file transfer
  3. * programs (PSFTP, new-look PSCP): since SFTP has no notion of
  4. * getting the remote side to do globbing (and rightly so) we have
  5. * to do it locally, by retrieving all the filenames in a directory
  6. * and checking each against the wildcard pattern.
  7. */
  8. #include <assert.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. /* MPEXT: This makes compilation from command-line hang */
  12. #ifndef MPEXT
  13. #include "putty.h"
  14. #endif
  15. /*
  16. * Definition of wildcard syntax:
  17. *
  18. * - * matches any sequence of characters, including zero.
  19. * - ? matches exactly one character which can be anything.
  20. * - [abc] matches exactly one character which is a, b or c.
  21. * - [a-f] matches anything from a through f.
  22. * - [^a-f] matches anything _except_ a through f.
  23. * - [-_] matches - or _; [^-_] matches anything else. (The - is
  24. * non-special if it occurs immediately after the opening
  25. * bracket or ^.)
  26. * - [a^] matches an a or a ^. (The ^ is non-special if it does
  27. * _not_ occur immediately after the opening bracket.)
  28. * - \*, \?, \[, \], \\ match the single characters *, ?, [, ], \.
  29. * - All other characters are non-special and match themselves.
  30. */
  31. /*
  32. * Some notes on differences from POSIX globs (IEEE Std 1003.1, 2003 ed.):
  33. * - backslashes act as escapes even within [] bracket expressions
  34. * - does not support [!...] for non-matching list (POSIX are weird);
  35. * NB POSIX allows [^...] as well via "A bracket expression starting
  36. * with an unquoted circumflex character produces unspecified
  37. * results". If we wanted to allow [!...] we might want to define
  38. * [^!] as having its literal meaning (match '^' or '!').
  39. * - none of the scary [[:class:]] stuff, etc
  40. */
  41. /*
  42. * The wildcard matching technique we use is very simple and
  43. * potentially O(N^2) in running time, but I don't anticipate it
  44. * being that bad in reality (particularly since N will be the size
  45. * of a filename, which isn't all that much). Perhaps one day, once
  46. * PuTTY has grown a regexp matcher for some other reason, I might
  47. * come back and reimplement wildcards by translating them into
  48. * regexps or directly into NFAs; but for the moment, in the
  49. * absence of any other need for the NFA->DFA translation engine,
  50. * anything more than the simplest possible wildcard matcher is
  51. * vast code-size overkill.
  52. *
  53. * Essentially, these wildcards are much simpler than regexps in
  54. * that they consist of a sequence of rigid fragments (? and [...]
  55. * can never match more or less than one character) separated by
  56. * asterisks. It is therefore extremely simple to look at a rigid
  57. * fragment and determine whether or not it begins at a particular
  58. * point in the test string; so we can search along the string
  59. * until we find each fragment, then search for the next. As long
  60. * as we find each fragment in the _first_ place it occurs, there
  61. * will never be a danger of having to backpedal and try to find it
  62. * again somewhere else.
  63. */
  64. enum {
  65. WC_TRAILINGBACKSLASH = 1,
  66. WC_UNCLOSEDCLASS,
  67. WC_INVALIDRANGE
  68. };
  69. /*
  70. * Error reporting is done by returning various negative values
  71. * from the wildcard routines. Passing any such value to wc_error
  72. * will give a human-readable message.
  73. */
  74. const char *wc_error(int value)
  75. {
  76. value = abs(value);
  77. switch (value) {
  78. case WC_TRAILINGBACKSLASH:
  79. return "'\' occurred at end of string (expected another character)";
  80. case WC_UNCLOSEDCLASS:
  81. return "expected ']' to close character class";
  82. case WC_INVALIDRANGE:
  83. return "character range was not terminated (']' just after '-')";
  84. }
  85. return "INTERNAL ERROR: unrecognised wildcard error number";
  86. }
  87. /*
  88. * This is the routine that tests a target string to see if an
  89. * initial substring of it matches a fragment. If successful, it
  90. * returns 1, and advances both `fragment' and `target' past the
  91. * fragment and matching substring respectively. If unsuccessful it
  92. * returns zero. If the wildcard fragment suffers a syntax error,
  93. * it returns <0 and the precise value indexes into wc_error.
  94. */
  95. static int wc_match_fragment(const char **fragment, const char **target)
  96. {
  97. const char *f, *t;
  98. f = *fragment;
  99. t = *target;
  100. /*
  101. * The fragment terminates at either the end of the string, or
  102. * the first (unescaped) *.
  103. */
  104. while (*f && *f != '*' && *t) {
  105. /*
  106. * Extract one character from t, and one character's worth
  107. * of pattern from f, and step along both. Return 0 if they
  108. * fail to match.
  109. */
  110. if (*f == '\\') {
  111. /*
  112. * Backslash, which means f[1] is to be treated as a
  113. * literal character no matter what it is. It may not
  114. * be the end of the string.
  115. */
  116. if (!f[1])
  117. return -WC_TRAILINGBACKSLASH; /* error */
  118. if (f[1] != *t)
  119. return 0; /* failed to match */
  120. f += 2;
  121. } else if (*f == '?') {
  122. /*
  123. * Question mark matches anything.
  124. */
  125. f++;
  126. } else if (*f == '[') {
  127. int invert = 0;
  128. int matched = 0;
  129. /*
  130. * Open bracket introduces a character class.
  131. */
  132. f++;
  133. if (*f == '^') {
  134. invert = 1;
  135. f++;
  136. }
  137. while (*f != ']') {
  138. if (*f == '\\')
  139. f++; /* backslashes still work */
  140. if (!*f)
  141. return -WC_UNCLOSEDCLASS; /* error again */
  142. if (f[1] == '-') {
  143. int lower, upper, ourchr;
  144. lower = (unsigned char) *f++;
  145. f++; /* eat the minus */
  146. if (*f == ']')
  147. return -WC_INVALIDRANGE; /* different error! */
  148. if (*f == '\\')
  149. f++; /* backslashes _still_ work */
  150. if (!*f)
  151. return -WC_UNCLOSEDCLASS; /* error again */
  152. upper = (unsigned char) *f++;
  153. ourchr = (unsigned char) *t;
  154. if (lower > upper) {
  155. int t = lower; lower = upper; upper = t;
  156. }
  157. if (ourchr >= lower && ourchr <= upper)
  158. matched = 1;
  159. } else {
  160. matched |= (*t == *f++);
  161. }
  162. }
  163. if (invert == matched)
  164. return 0; /* failed to match character class */
  165. f++; /* eat the ] */
  166. } else {
  167. /*
  168. * Non-special character matches itself.
  169. */
  170. if (*f != *t)
  171. return 0;
  172. f++;
  173. }
  174. /*
  175. * Now we've done that, increment t past the character we
  176. * matched.
  177. */
  178. t++;
  179. }
  180. if (!*f || *f == '*') {
  181. /*
  182. * We have reached the end of f without finding a mismatch;
  183. * so we're done. Update the caller pointers and return 1.
  184. */
  185. *fragment = f;
  186. *target = t;
  187. return 1;
  188. }
  189. /*
  190. * Otherwise, we must have reached the end of t before we
  191. * reached the end of f; so we've failed. Return 0.
  192. */
  193. return 0;
  194. }
  195. /*
  196. * This is the real wildcard matching routine. It returns 1 for a
  197. * successful match, 0 for an unsuccessful match, and <0 for a
  198. * syntax error in the wildcard.
  199. */
  200. int wc_match(const char *wildcard, const char *target)
  201. {
  202. int ret;
  203. /*
  204. * Every time we see a '*' _followed_ by a fragment, we just
  205. * search along the string for a location at which the fragment
  206. * matches. The only special case is when we see a fragment
  207. * right at the start, in which case we just call the matching
  208. * routine once and give up if it fails.
  209. */
  210. if (*wildcard != '*') {
  211. ret = wc_match_fragment(&wildcard, &target);
  212. if (ret <= 0)
  213. return ret; /* pass back failure or error alike */
  214. }
  215. while (*wildcard) {
  216. assert(*wildcard == '*');
  217. while (*wildcard == '*')
  218. wildcard++;
  219. /*
  220. * It's possible we've just hit the end of the wildcard
  221. * after seeing a *, in which case there's no need to
  222. * bother searching any more because we've won.
  223. */
  224. if (!*wildcard)
  225. return 1;
  226. /*
  227. * Now `wildcard' points at the next fragment. So we
  228. * attempt to match it against `target', and if that fails
  229. * we increment `target' and try again, and so on. When we
  230. * find we're about to try matching against the empty
  231. * string, we give up and return 0.
  232. */
  233. ret = 0;
  234. while (*target) {
  235. const char *save_w = wildcard, *save_t = target;
  236. ret = wc_match_fragment(&wildcard, &target);
  237. if (ret < 0)
  238. return ret; /* syntax error */
  239. if (ret > 0 && !*wildcard && *target) {
  240. /*
  241. * Final special case - literally.
  242. *
  243. * This situation arises when we are matching a
  244. * _terminal_ fragment of the wildcard (that is,
  245. * there is nothing after it, e.g. "*a"), and it
  246. * has matched _too early_. For example, matching
  247. * "*a" against "parka" will match the "a" fragment
  248. * against the _first_ a, and then (if it weren't
  249. * for this special case) matching would fail
  250. * because we're at the end of the wildcard but not
  251. * at the end of the target string.
  252. *
  253. * In this case what we must do is measure the
  254. * length of the fragment in the target (which is
  255. * why we saved `target'), jump straight to that
  256. * distance from the end of the string using
  257. * strlen, and match the same fragment again there
  258. * (which is why we saved `wildcard'). Then we
  259. * return whatever that operation returns.
  260. */
  261. target = save_t + strlen(save_t) - (target - save_t);
  262. wildcard = save_w;
  263. return wc_match_fragment(&wildcard, &target);
  264. }
  265. if (ret > 0)
  266. break;
  267. target++;
  268. }
  269. if (ret > 0)
  270. continue;
  271. return 0;
  272. }
  273. /*
  274. * If we reach here, it must be because we successfully matched
  275. * a fragment and then found ourselves right at the end of the
  276. * wildcard. Hence, we return 1 if and only if we are also
  277. * right at the end of the target.
  278. */
  279. return (*target ? 0 : 1);
  280. }
  281. /*
  282. * Another utility routine that translates a non-wildcard string
  283. * into its raw equivalent by removing any escaping backslashes.
  284. * Expects a target string buffer of anything up to the length of
  285. * the original wildcard. You can also pass NULL as the output
  286. * buffer if you're only interested in the return value.
  287. *
  288. * Returns 1 on success, or 0 if a wildcard character was
  289. * encountered. In the latter case the output string MAY not be
  290. * zero-terminated and you should not use it for anything!
  291. */
  292. int wc_unescape(char *output, const char *wildcard)
  293. {
  294. while (*wildcard) {
  295. if (*wildcard == '\\') {
  296. wildcard++;
  297. /* We are lenient about trailing backslashes in non-wildcards. */
  298. if (*wildcard) {
  299. if (output)
  300. *output++ = *wildcard;
  301. wildcard++;
  302. }
  303. } else if (*wildcard == '*' || *wildcard == '?' ||
  304. *wildcard == '[' || *wildcard == ']') {
  305. return 0; /* it's a wildcard! */
  306. } else {
  307. if (output)
  308. *output++ = *wildcard;
  309. wildcard++;
  310. }
  311. }
  312. if (output)
  313. *output = '\0';
  314. return 1; /* it's clean */
  315. }
  316. #ifdef TESTMODE
  317. struct test {
  318. const char *wildcard;
  319. const char *target;
  320. int expected_result;
  321. };
  322. const struct test fragment_tests[] = {
  323. /*
  324. * We exhaustively unit-test the fragment matching routine
  325. * itself, which should save us the need to test all its
  326. * intricacies during the full wildcard tests.
  327. */
  328. {"abc", "abc", 1},
  329. {"abc", "abd", 0},
  330. {"abc", "abcd", 1},
  331. {"abcd", "abc", 0},
  332. {"ab[cd]", "abc", 1},
  333. {"ab[cd]", "abd", 1},
  334. {"ab[cd]", "abe", 0},
  335. {"ab[^cd]", "abc", 0},
  336. {"ab[^cd]", "abd", 0},
  337. {"ab[^cd]", "abe", 1},
  338. {"ab\\", "abc", -WC_TRAILINGBACKSLASH},
  339. {"ab\\*", "ab*", 1},
  340. {"ab\\?", "ab*", 0},
  341. {"ab?", "abc", 1},
  342. {"ab?", "ab", 0},
  343. {"ab[", "abc", -WC_UNCLOSEDCLASS},
  344. {"ab[c-", "abb", -WC_UNCLOSEDCLASS},
  345. {"ab[c-]", "abb", -WC_INVALIDRANGE},
  346. {"ab[c-e]", "abb", 0},
  347. {"ab[c-e]", "abc", 1},
  348. {"ab[c-e]", "abd", 1},
  349. {"ab[c-e]", "abe", 1},
  350. {"ab[c-e]", "abf", 0},
  351. {"ab[e-c]", "abb", 0},
  352. {"ab[e-c]", "abc", 1},
  353. {"ab[e-c]", "abd", 1},
  354. {"ab[e-c]", "abe", 1},
  355. {"ab[e-c]", "abf", 0},
  356. {"ab[^c-e]", "abb", 1},
  357. {"ab[^c-e]", "abc", 0},
  358. {"ab[^c-e]", "abd", 0},
  359. {"ab[^c-e]", "abe", 0},
  360. {"ab[^c-e]", "abf", 1},
  361. {"ab[^e-c]", "abb", 1},
  362. {"ab[^e-c]", "abc", 0},
  363. {"ab[^e-c]", "abd", 0},
  364. {"ab[^e-c]", "abe", 0},
  365. {"ab[^e-c]", "abf", 1},
  366. {"ab[a^]", "aba", 1},
  367. {"ab[a^]", "ab^", 1},
  368. {"ab[a^]", "abb", 0},
  369. {"ab[^a^]", "aba", 0},
  370. {"ab[^a^]", "ab^", 0},
  371. {"ab[^a^]", "abb", 1},
  372. {"ab[-c]", "ab-", 1},
  373. {"ab[-c]", "abc", 1},
  374. {"ab[-c]", "abd", 0},
  375. {"ab[^-c]", "ab-", 0},
  376. {"ab[^-c]", "abc", 0},
  377. {"ab[^-c]", "abd", 1},
  378. {"ab[\\[-\\]]", "abZ", 0},
  379. {"ab[\\[-\\]]", "ab[", 1},
  380. {"ab[\\[-\\]]", "ab\\", 1},
  381. {"ab[\\[-\\]]", "ab]", 1},
  382. {"ab[\\[-\\]]", "ab^", 0},
  383. {"ab[^\\[-\\]]", "abZ", 1},
  384. {"ab[^\\[-\\]]", "ab[", 0},
  385. {"ab[^\\[-\\]]", "ab\\", 0},
  386. {"ab[^\\[-\\]]", "ab]", 0},
  387. {"ab[^\\[-\\]]", "ab^", 1},
  388. {"ab[a-fA-F]", "aba", 1},
  389. {"ab[a-fA-F]", "abF", 1},
  390. {"ab[a-fA-F]", "abZ", 0},
  391. };
  392. const struct test full_tests[] = {
  393. {"a", "argh", 0},
  394. {"a", "ba", 0},
  395. {"a", "a", 1},
  396. {"a*", "aardvark", 1},
  397. {"a*", "badger", 0},
  398. {"*a", "park", 0},
  399. {"*a", "pArka", 1},
  400. {"*a", "parka", 1},
  401. {"*a*", "park", 1},
  402. {"*a*", "perk", 0},
  403. {"?b*r?", "abracadabra", 1},
  404. {"?b*r?", "abracadabr", 0},
  405. {"?b*r?", "abracadabzr", 0},
  406. };
  407. int main(void)
  408. {
  409. int i;
  410. int fails, passes;
  411. fails = passes = 0;
  412. for (i = 0; i < sizeof(fragment_tests)/sizeof(*fragment_tests); i++) {
  413. const char *f, *t;
  414. int eret, aret;
  415. f = fragment_tests[i].wildcard;
  416. t = fragment_tests[i].target;
  417. eret = fragment_tests[i].expected_result;
  418. aret = wc_match_fragment(&f, &t);
  419. if (aret != eret) {
  420. printf("failed test: /%s/ against /%s/ returned %d not %d\n",
  421. fragment_tests[i].wildcard, fragment_tests[i].target,
  422. aret, eret);
  423. fails++;
  424. } else
  425. passes++;
  426. }
  427. for (i = 0; i < sizeof(full_tests)/sizeof(*full_tests); i++) {
  428. const char *f, *t;
  429. int eret, aret;
  430. f = full_tests[i].wildcard;
  431. t = full_tests[i].target;
  432. eret = full_tests[i].expected_result;
  433. aret = wc_match(f, t);
  434. if (aret != eret) {
  435. printf("failed test: /%s/ against /%s/ returned %d not %d\n",
  436. full_tests[i].wildcard, full_tests[i].target,
  437. aret, eret);
  438. fails++;
  439. } else
  440. passes++;
  441. }
  442. printf("passed %d, failed %d\n", passes, fails);
  443. return 0;
  444. }
  445. #endif