xref: /linux/lib/glob.c (revision c9ba82624b684679b37c5062b697c85f932089b9)
1 // SPDX-License-Identifier: (GPL-2.0 OR MIT)
2 #include <linux/module.h>
3 #include <linux/glob.h>
4 #include <linux/export.h>
5 
6 /*
7  * The only reason this code can be compiled as a module is because the
8  * ATA code that depends on it can be as well.  In practice, they're
9  * both usually compiled in and the module overhead goes away.
10  */
11 MODULE_DESCRIPTION("glob(7) matching");
12 MODULE_LICENSE("Dual MIT/GPL");
13 
14 /**
15  * glob_match - Shell-style pattern matching, like !fnmatch(pat, str, 0)
16  * @pat: Shell-style pattern to match, e.g. "*.[ch]".
17  * @str: String to match.  The pattern must match the entire string.
18  *
19  * Perform shell-style glob matching, returning true (1) if the match
20  * succeeds, or false (0) if it fails.  Equivalent to !fnmatch(@pat, @str, 0).
21  *
22  * Pattern metacharacters are ?, *, [ and \.
23  * (And, inside character classes, !, - and ].)
24  *
25  * This is a small and simple implementation intended for device denylists
26  * where a string is matched against a number of patterns.  Thus, it
27  * does not preprocess the patterns.  It is non-recursive, and run-time
28  * is at most quadratic: strlen(@str)*strlen(@pat).
29  *
30  * An example of the worst case is glob_match("*aaaaa", "aaaaaaaaaa");
31  * it takes 6 passes over the pattern before matching the string.
32  *
33  * Like !fnmatch(@pat, @str, 0) and unlike the shell, this does NOT
34  * treat / or leading . specially; it isn't actually used for pathnames.
35  *
36  * Note that according to glob(7) (and unlike bash), character classes
37  * are complemented by a leading !; this does not support the regex-style
38  * [^a-z] syntax.
39  *
40  * An opening bracket without a matching close is matched literally.
41  */
42 bool __pure glob_match(char const *pat, char const *str)
43 {
44 	/*
45 	 * Backtrack to previous * on mismatch and retry starting one
46 	 * character later in the string.  Because * matches all characters
47 	 * (no exception for /), it can be easily proved that there's
48 	 * never a need to backtrack multiple levels.
49 	 */
50 	char const *back_pat = NULL, *back_str = NULL;
51 
52 	/*
53 	 * Loop over each token (character or class) in pat, matching
54 	 * it against the remaining unmatched tail of str.  Return false
55 	 * on mismatch, or true after matching the trailing nul bytes.
56 	 */
57 	for (;;) {
58 		unsigned char c = *str++;
59 		unsigned char d = *pat++;
60 
61 		switch (d) {
62 		case '?':	/* Wildcard: anything but nul */
63 			if (c == '\0')
64 				return false;
65 			break;
66 		case '*':	/* Any-length wildcard */
67 			if (*pat == '\0')	/* Optimize trailing * case */
68 				return true;
69 			back_pat = pat;
70 			back_str = --str;	/* Allow zero-length match */
71 			break;
72 		case '[': {	/* Character class */
73 			if (c == '\0')	/* No possible match */
74 				return false;
75 			bool match = false, inverted = (*pat == '!');
76 			char const *class = inverted ? pat + 1 : pat;
77 			unsigned char a = *class++;
78 
79 			/*
80 			 * Iterate over each span in the character class.
81 			 * A span is either a single character a, or a
82 			 * range a-b.  The first span may begin with ']'.
83 			 */
84 			do {
85 				unsigned char b = a;
86 
87 				if (a == '\0')	/* Malformed */
88 					goto literal;
89 
90 				if (class[0] == '-' && class[1] != ']') {
91 					b = class[1];
92 
93 					if (b == '\0')
94 						goto literal;
95 
96 					class += 2;
97 					/* Any special action if a > b? */
98 				}
99 				if (a <= c && c <= b)
100 					match = true;
101 			} while ((a = *class++) != ']');
102 
103 			if (match == inverted)
104 				goto backtrack;
105 			pat = class;
106 			}
107 			break;
108 		case '\\':
109 			d = *pat++;
110 			fallthrough;
111 		default:	/* Literal character */
112 literal:
113 			if (c == d) {
114 				if (d == '\0')
115 					return true;
116 				break;
117 			}
118 backtrack:
119 			if (c == '\0' || !back_pat)
120 				return false;	/* No point continuing */
121 			/* Try again from last *, one character later in str. */
122 			pat = back_pat;
123 			str = ++back_str;
124 			break;
125 		}
126 	}
127 }
128 EXPORT_SYMBOL(glob_match);
129