glob.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. #include <linux/module.h>
  2. #include <linux/glob.h>
  3. /*
  4. * The only reason this code can be compiled as a module is because the
  5. * ATA code that depends on it can be as well. In practice, they're
  6. * both usually compiled in and the module overhead goes away.
  7. */
  8. MODULE_DESCRIPTION("glob(7) matching");
  9. MODULE_LICENSE("Dual MIT/GPL");
  10. /**
  11. * glob_match - Shell-style pattern matching, like !fnmatch(pat, str, 0)
  12. * @pat: Shell-style pattern to match, e.g. "*.[ch]".
  13. * @str: String to match. The pattern must match the entire string.
  14. *
  15. * Perform shell-style glob matching, returning true (1) if the match
  16. * succeeds, or false (0) if it fails. Equivalent to !fnmatch(@pat, @str, 0).
  17. *
  18. * Pattern metacharacters are ?, *, [ and \.
  19. * (And, inside character classes, !, - and ].)
  20. *
  21. * This is small and simple implementation intended for device blacklists
  22. * where a string is matched against a number of patterns. Thus, it
  23. * does not preprocess the patterns. It is non-recursive, and run-time
  24. * is at most quadratic: strlen(@str)*strlen(@pat).
  25. *
  26. * An example of the worst case is glob_match("*aaaaa", "aaaaaaaaaa");
  27. * it takes 6 passes over the pattern before matching the string.
  28. *
  29. * Like !fnmatch(@pat, @str, 0) and unlike the shell, this does NOT
  30. * treat / or leading . specially; it isn't actually used for pathnames.
  31. *
  32. * Note that according to glob(7) (and unlike bash), character classes
  33. * are complemented by a leading !; this does not support the regex-style
  34. * [^a-z] syntax.
  35. *
  36. * An opening bracket without a matching close is matched literally.
  37. */
  38. bool __pure glob_match(char const *pat, char const *str)
  39. {
  40. /*
  41. * Backtrack to previous * on mismatch and retry starting one
  42. * character later in the string. Because * matches all characters
  43. * (no exception for /), it can be easily proved that there's
  44. * never a need to backtrack multiple levels.
  45. */
  46. char const *back_pat = NULL, *back_str = back_str;
  47. /*
  48. * Loop over each token (character or class) in pat, matching
  49. * it against the remaining unmatched tail of str. Return false
  50. * on mismatch, or true after matching the trailing nul bytes.
  51. */
  52. for (;;) {
  53. unsigned char c = *str++;
  54. unsigned char d = *pat++;
  55. switch (d) {
  56. case '?': /* Wildcard: anything but nul */
  57. if (c == '\0')
  58. return false;
  59. break;
  60. case '*': /* Any-length wildcard */
  61. if (*pat == '\0') /* Optimize trailing * case */
  62. return true;
  63. back_pat = pat;
  64. back_str = --str; /* Allow zero-length match */
  65. break;
  66. case '[': { /* Character class */
  67. bool match = false, inverted = (*pat == '!');
  68. char const *class = pat + inverted;
  69. unsigned char a = *class++;
  70. /*
  71. * Iterate over each span in the character class.
  72. * A span is either a single character a, or a
  73. * range a-b. The first span may begin with ']'.
  74. */
  75. do {
  76. unsigned char b = a;
  77. if (a == '\0') /* Malformed */
  78. goto literal;
  79. if (class[0] == '-' && class[1] != ']') {
  80. b = class[1];
  81. if (b == '\0')
  82. goto literal;
  83. class += 2;
  84. /* Any special action if a > b? */
  85. }
  86. match |= (a <= c && c <= b);
  87. } while ((a = *class++) != ']');
  88. if (match == inverted)
  89. goto backtrack;
  90. pat = class;
  91. }
  92. break;
  93. case '\\':
  94. d = *pat++;
  95. /*FALLTHROUGH*/
  96. default: /* Literal character */
  97. literal:
  98. if (c == d) {
  99. if (d == '\0')
  100. return true;
  101. break;
  102. }
  103. backtrack:
  104. if (c == '\0' || !back_pat)
  105. return false; /* No point continuing */
  106. /* Try again from last *, one character later in str. */
  107. pat = back_pat;
  108. str = ++back_str;
  109. break;
  110. }
  111. }
  112. }
  113. EXPORT_SYMBOL(glob_match);
  114. #ifdef CONFIG_GLOB_SELFTEST
  115. #include <linux/printk.h>
  116. #include <linux/moduleparam.h>
  117. /* Boot with "glob.verbose=1" to show successful tests, too */
  118. static bool verbose = false;
  119. module_param(verbose, bool, 0);
  120. struct glob_test {
  121. char const *pat, *str;
  122. bool expected;
  123. };
  124. static bool __pure __init test(char const *pat, char const *str, bool expected)
  125. {
  126. bool match = glob_match(pat, str);
  127. bool success = match == expected;
  128. /* Can't get string literals into a particular section, so... */
  129. static char const msg_error[] __initconst =
  130. KERN_ERR "glob: \"%s\" vs. \"%s\": %s *** ERROR ***\n";
  131. static char const msg_ok[] __initconst =
  132. KERN_DEBUG "glob: \"%s\" vs. \"%s\": %s OK\n";
  133. static char const mismatch[] __initconst = "mismatch";
  134. char const *message;
  135. if (!success)
  136. message = msg_error;
  137. else if (verbose)
  138. message = msg_ok;
  139. else
  140. return success;
  141. printk(message, pat, str, mismatch + 3*match);
  142. return success;
  143. }
  144. /*
  145. * The tests are all jammed together in one array to make it simpler
  146. * to place that array in the .init.rodata section. The obvious
  147. * "array of structures containing char *" has no way to force the
  148. * pointed-to strings to be in a particular section.
  149. *
  150. * Anyway, a test consists of:
  151. * 1. Expected glob_match result: '1' or '0'.
  152. * 2. Pattern to match: null-terminated string
  153. * 3. String to match against: null-terminated string
  154. *
  155. * The list of tests is terminated with a final '\0' instead of
  156. * a glob_match result character.
  157. */
  158. static char const glob_tests[] __initconst =
  159. /* Some basic tests */
  160. "1" "a\0" "a\0"
  161. "0" "a\0" "b\0"
  162. "0" "a\0" "aa\0"
  163. "0" "a\0" "\0"
  164. "1" "\0" "\0"
  165. "0" "\0" "a\0"
  166. /* Simple character class tests */
  167. "1" "[a]\0" "a\0"
  168. "0" "[a]\0" "b\0"
  169. "0" "[!a]\0" "a\0"
  170. "1" "[!a]\0" "b\0"
  171. "1" "[ab]\0" "a\0"
  172. "1" "[ab]\0" "b\0"
  173. "0" "[ab]\0" "c\0"
  174. "1" "[!ab]\0" "c\0"
  175. "1" "[a-c]\0" "b\0"
  176. "0" "[a-c]\0" "d\0"
  177. /* Corner cases in character class parsing */
  178. "1" "[a-c-e-g]\0" "-\0"
  179. "0" "[a-c-e-g]\0" "d\0"
  180. "1" "[a-c-e-g]\0" "f\0"
  181. "1" "[]a-ceg-ik[]\0" "a\0"
  182. "1" "[]a-ceg-ik[]\0" "]\0"
  183. "1" "[]a-ceg-ik[]\0" "[\0"
  184. "1" "[]a-ceg-ik[]\0" "h\0"
  185. "0" "[]a-ceg-ik[]\0" "f\0"
  186. "0" "[!]a-ceg-ik[]\0" "h\0"
  187. "0" "[!]a-ceg-ik[]\0" "]\0"
  188. "1" "[!]a-ceg-ik[]\0" "f\0"
  189. /* Simple wild cards */
  190. "1" "?\0" "a\0"
  191. "0" "?\0" "aa\0"
  192. "0" "??\0" "a\0"
  193. "1" "?x?\0" "axb\0"
  194. "0" "?x?\0" "abx\0"
  195. "0" "?x?\0" "xab\0"
  196. /* Asterisk wild cards (backtracking) */
  197. "0" "*??\0" "a\0"
  198. "1" "*??\0" "ab\0"
  199. "1" "*??\0" "abc\0"
  200. "1" "*??\0" "abcd\0"
  201. "0" "??*\0" "a\0"
  202. "1" "??*\0" "ab\0"
  203. "1" "??*\0" "abc\0"
  204. "1" "??*\0" "abcd\0"
  205. "0" "?*?\0" "a\0"
  206. "1" "?*?\0" "ab\0"
  207. "1" "?*?\0" "abc\0"
  208. "1" "?*?\0" "abcd\0"
  209. "1" "*b\0" "b\0"
  210. "1" "*b\0" "ab\0"
  211. "0" "*b\0" "ba\0"
  212. "1" "*b\0" "bb\0"
  213. "1" "*b\0" "abb\0"
  214. "1" "*b\0" "bab\0"
  215. "1" "*bc\0" "abbc\0"
  216. "1" "*bc\0" "bc\0"
  217. "1" "*bc\0" "bbc\0"
  218. "1" "*bc\0" "bcbc\0"
  219. /* Multiple asterisks (complex backtracking) */
  220. "1" "*ac*\0" "abacadaeafag\0"
  221. "1" "*ac*ae*ag*\0" "abacadaeafag\0"
  222. "1" "*a*b*[bc]*[ef]*g*\0" "abacadaeafag\0"
  223. "0" "*a*b*[ef]*[cd]*g*\0" "abacadaeafag\0"
  224. "1" "*abcd*\0" "abcabcabcabcdefg\0"
  225. "1" "*ab*cd*\0" "abcabcabcabcdefg\0"
  226. "1" "*abcd*abcdef*\0" "abcabcdabcdeabcdefg\0"
  227. "0" "*abcd*\0" "abcabcabcabcefg\0"
  228. "0" "*ab*cd*\0" "abcabcabcabcefg\0";
  229. static int __init glob_init(void)
  230. {
  231. unsigned successes = 0;
  232. unsigned n = 0;
  233. char const *p = glob_tests;
  234. static char const message[] __initconst =
  235. KERN_INFO "glob: %u self-tests passed, %u failed\n";
  236. /*
  237. * Tests are jammed together in a string. The first byte is '1'
  238. * or '0' to indicate the expected outcome, or '\0' to indicate the
  239. * end of the tests. Then come two null-terminated strings: the
  240. * pattern and the string to match it against.
  241. */
  242. while (*p) {
  243. bool expected = *p++ & 1;
  244. char const *pat = p;
  245. p += strlen(p) + 1;
  246. successes += test(pat, p, expected);
  247. p += strlen(p) + 1;
  248. n++;
  249. }
  250. n -= successes;
  251. printk(message, successes, n);
  252. /* What's the errno for "kernel bug detected"? Guess... */
  253. return n ? -ECANCELED : 0;
  254. }
  255. /* We need a dummy exit function to allow unload */
  256. static void __exit glob_fini(void) { }
  257. module_init(glob_init);
  258. module_exit(glob_fini);
  259. #endif /* CONFIG_GLOB_SELFTEST */