Back to home page

LXR

 
 

    


0001 #include <linux/module.h>
0002 #include <linux/glob.h>
0003 
0004 /*
0005  * The only reason this code can be compiled as a module is because the
0006  * ATA code that depends on it can be as well.  In practice, they're
0007  * both usually compiled in and the module overhead goes away.
0008  */
0009 MODULE_DESCRIPTION("glob(7) matching");
0010 MODULE_LICENSE("Dual MIT/GPL");
0011 
0012 /**
0013  * glob_match - Shell-style pattern matching, like !fnmatch(pat, str, 0)
0014  * @pat: Shell-style pattern to match, e.g. "*.[ch]".
0015  * @str: String to match.  The pattern must match the entire string.
0016  *
0017  * Perform shell-style glob matching, returning true (1) if the match
0018  * succeeds, or false (0) if it fails.  Equivalent to !fnmatch(@pat, @str, 0).
0019  *
0020  * Pattern metacharacters are ?, *, [ and \.
0021  * (And, inside character classes, !, - and ].)
0022  *
0023  * This is small and simple implementation intended for device blacklists
0024  * where a string is matched against a number of patterns.  Thus, it
0025  * does not preprocess the patterns.  It is non-recursive, and run-time
0026  * is at most quadratic: strlen(@str)*strlen(@pat).
0027  *
0028  * An example of the worst case is glob_match("*aaaaa", "aaaaaaaaaa");
0029  * it takes 6 passes over the pattern before matching the string.
0030  *
0031  * Like !fnmatch(@pat, @str, 0) and unlike the shell, this does NOT
0032  * treat / or leading . specially; it isn't actually used for pathnames.
0033  *
0034  * Note that according to glob(7) (and unlike bash), character classes
0035  * are complemented by a leading !; this does not support the regex-style
0036  * [^a-z] syntax.
0037  *
0038  * An opening bracket without a matching close is matched literally.
0039  */
0040 bool __pure glob_match(char const *pat, char const *str)
0041 {
0042     /*
0043      * Backtrack to previous * on mismatch and retry starting one
0044      * character later in the string.  Because * matches all characters
0045      * (no exception for /), it can be easily proved that there's
0046      * never a need to backtrack multiple levels.
0047      */
0048     char const *back_pat = NULL, *back_str = back_str;
0049 
0050     /*
0051      * Loop over each token (character or class) in pat, matching
0052      * it against the remaining unmatched tail of str.  Return false
0053      * on mismatch, or true after matching the trailing nul bytes.
0054      */
0055     for (;;) {
0056         unsigned char c = *str++;
0057         unsigned char d = *pat++;
0058 
0059         switch (d) {
0060         case '?':   /* Wildcard: anything but nul */
0061             if (c == '\0')
0062                 return false;
0063             break;
0064         case '*':   /* Any-length wildcard */
0065             if (*pat == '\0')   /* Optimize trailing * case */
0066                 return true;
0067             back_pat = pat;
0068             back_str = --str;   /* Allow zero-length match */
0069             break;
0070         case '[': { /* Character class */
0071             bool match = false, inverted = (*pat == '!');
0072             char const *class = pat + inverted;
0073             unsigned char a = *class++;
0074 
0075             /*
0076              * Iterate over each span in the character class.
0077              * A span is either a single character a, or a
0078              * range a-b.  The first span may begin with ']'.
0079              */
0080             do {
0081                 unsigned char b = a;
0082 
0083                 if (a == '\0')  /* Malformed */
0084                     goto literal;
0085 
0086                 if (class[0] == '-' && class[1] != ']') {
0087                     b = class[1];
0088 
0089                     if (b == '\0')
0090                         goto literal;
0091 
0092                     class += 2;
0093                     /* Any special action if a > b? */
0094                 }
0095                 match |= (a <= c && c <= b);
0096             } while ((a = *class++) != ']');
0097 
0098             if (match == inverted)
0099                 goto backtrack;
0100             pat = class;
0101             }
0102             break;
0103         case '\\':
0104             d = *pat++;
0105             /*FALLTHROUGH*/
0106         default:    /* Literal character */
0107 literal:
0108             if (c == d) {
0109                 if (d == '\0')
0110                     return true;
0111                 break;
0112             }
0113 backtrack:
0114             if (c == '\0' || !back_pat)
0115                 return false;   /* No point continuing */
0116             /* Try again from last *, one character later in str. */
0117             pat = back_pat;
0118             str = ++back_str;
0119             break;
0120         }
0121     }
0122 }
0123 EXPORT_SYMBOL(glob_match);
0124 
0125 
0126 #ifdef CONFIG_GLOB_SELFTEST
0127 
0128 #include <linux/printk.h>
0129 #include <linux/moduleparam.h>
0130 
0131 /* Boot with "glob.verbose=1" to show successful tests, too */
0132 static bool verbose = false;
0133 module_param(verbose, bool, 0);
0134 
0135 struct glob_test {
0136     char const *pat, *str;
0137     bool expected;
0138 };
0139 
0140 static bool __pure __init test(char const *pat, char const *str, bool expected)
0141 {
0142     bool match = glob_match(pat, str);
0143     bool success = match == expected;
0144 
0145     /* Can't get string literals into a particular section, so... */
0146     static char const msg_error[] __initconst =
0147         KERN_ERR "glob: \"%s\" vs. \"%s\": %s *** ERROR ***\n";
0148     static char const msg_ok[] __initconst =
0149         KERN_DEBUG "glob: \"%s\" vs. \"%s\": %s OK\n";
0150     static char const mismatch[] __initconst = "mismatch";
0151     char const *message;
0152 
0153     if (!success)
0154         message = msg_error;
0155     else if (verbose)
0156         message = msg_ok;
0157     else
0158         return success;
0159 
0160     printk(message, pat, str, mismatch + 3*match);
0161     return success;
0162 }
0163 
0164 /*
0165  * The tests are all jammed together in one array to make it simpler
0166  * to place that array in the .init.rodata section.  The obvious
0167  * "array of structures containing char *" has no way to force the
0168  * pointed-to strings to be in a particular section.
0169  *
0170  * Anyway, a test consists of:
0171  * 1. Expected glob_match result: '1' or '0'.
0172  * 2. Pattern to match: null-terminated string
0173  * 3. String to match against: null-terminated string
0174  *
0175  * The list of tests is terminated with a final '\0' instead of
0176  * a glob_match result character.
0177  */
0178 static char const glob_tests[] __initconst =
0179     /* Some basic tests */
0180     "1" "a\0" "a\0"
0181     "0" "a\0" "b\0"
0182     "0" "a\0" "aa\0"
0183     "0" "a\0" "\0"
0184     "1" "\0" "\0"
0185     "0" "\0" "a\0"
0186     /* Simple character class tests */
0187     "1" "[a]\0" "a\0"
0188     "0" "[a]\0" "b\0"
0189     "0" "[!a]\0" "a\0"
0190     "1" "[!a]\0" "b\0"
0191     "1" "[ab]\0" "a\0"
0192     "1" "[ab]\0" "b\0"
0193     "0" "[ab]\0" "c\0"
0194     "1" "[!ab]\0" "c\0"
0195     "1" "[a-c]\0" "b\0"
0196     "0" "[a-c]\0" "d\0"
0197     /* Corner cases in character class parsing */
0198     "1" "[a-c-e-g]\0" "-\0"
0199     "0" "[a-c-e-g]\0" "d\0"
0200     "1" "[a-c-e-g]\0" "f\0"
0201     "1" "[]a-ceg-ik[]\0" "a\0"
0202     "1" "[]a-ceg-ik[]\0" "]\0"
0203     "1" "[]a-ceg-ik[]\0" "[\0"
0204     "1" "[]a-ceg-ik[]\0" "h\0"
0205     "0" "[]a-ceg-ik[]\0" "f\0"
0206     "0" "[!]a-ceg-ik[]\0" "h\0"
0207     "0" "[!]a-ceg-ik[]\0" "]\0"
0208     "1" "[!]a-ceg-ik[]\0" "f\0"
0209     /* Simple wild cards */
0210     "1" "?\0" "a\0"
0211     "0" "?\0" "aa\0"
0212     "0" "??\0" "a\0"
0213     "1" "?x?\0" "axb\0"
0214     "0" "?x?\0" "abx\0"
0215     "0" "?x?\0" "xab\0"
0216     /* Asterisk wild cards (backtracking) */
0217     "0" "*??\0" "a\0"
0218     "1" "*??\0" "ab\0"
0219     "1" "*??\0" "abc\0"
0220     "1" "*??\0" "abcd\0"
0221     "0" "??*\0" "a\0"
0222     "1" "??*\0" "ab\0"
0223     "1" "??*\0" "abc\0"
0224     "1" "??*\0" "abcd\0"
0225     "0" "?*?\0" "a\0"
0226     "1" "?*?\0" "ab\0"
0227     "1" "?*?\0" "abc\0"
0228     "1" "?*?\0" "abcd\0"
0229     "1" "*b\0" "b\0"
0230     "1" "*b\0" "ab\0"
0231     "0" "*b\0" "ba\0"
0232     "1" "*b\0" "bb\0"
0233     "1" "*b\0" "abb\0"
0234     "1" "*b\0" "bab\0"
0235     "1" "*bc\0" "abbc\0"
0236     "1" "*bc\0" "bc\0"
0237     "1" "*bc\0" "bbc\0"
0238     "1" "*bc\0" "bcbc\0"
0239     /* Multiple asterisks (complex backtracking) */
0240     "1" "*ac*\0" "abacadaeafag\0"
0241     "1" "*ac*ae*ag*\0" "abacadaeafag\0"
0242     "1" "*a*b*[bc]*[ef]*g*\0" "abacadaeafag\0"
0243     "0" "*a*b*[ef]*[cd]*g*\0" "abacadaeafag\0"
0244     "1" "*abcd*\0" "abcabcabcabcdefg\0"
0245     "1" "*ab*cd*\0" "abcabcabcabcdefg\0"
0246     "1" "*abcd*abcdef*\0" "abcabcdabcdeabcdefg\0"
0247     "0" "*abcd*\0" "abcabcabcabcefg\0"
0248     "0" "*ab*cd*\0" "abcabcabcabcefg\0";
0249 
0250 static int __init glob_init(void)
0251 {
0252     unsigned successes = 0;
0253     unsigned n = 0;
0254     char const *p = glob_tests;
0255     static char const message[] __initconst =
0256         KERN_INFO "glob: %u self-tests passed, %u failed\n";
0257 
0258     /*
0259      * Tests are jammed together in a string.  The first byte is '1'
0260      * or '0' to indicate the expected outcome, or '\0' to indicate the
0261      * end of the tests.  Then come two null-terminated strings: the
0262      * pattern and the string to match it against.
0263      */
0264     while (*p) {
0265         bool expected = *p++ & 1;
0266         char const *pat = p;
0267 
0268         p += strlen(p) + 1;
0269         successes += test(pat, p, expected);
0270         p += strlen(p) + 1;
0271         n++;
0272     }
0273 
0274     n -= successes;
0275     printk(message, successes, n);
0276 
0277     /* What's the errno for "kernel bug detected"?  Guess... */
0278     return n ? -ECANCELED : 0;
0279 }
0280 
0281 /* We need a dummy exit function to allow unload */
0282 static void __exit glob_fini(void) { }
0283 
0284 module_init(glob_init);
0285 module_exit(glob_fini);
0286 
0287 #endif /* CONFIG_GLOB_SELFTEST */