18c2ecf20Sopenharmony_ci#include <linux/module.h> 28c2ecf20Sopenharmony_ci#include <linux/glob.h> 38c2ecf20Sopenharmony_ci 48c2ecf20Sopenharmony_ci/* 58c2ecf20Sopenharmony_ci * The only reason this code can be compiled as a module is because the 68c2ecf20Sopenharmony_ci * ATA code that depends on it can be as well. In practice, they're 78c2ecf20Sopenharmony_ci * both usually compiled in and the module overhead goes away. 88c2ecf20Sopenharmony_ci */ 98c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("glob(7) matching"); 108c2ecf20Sopenharmony_ciMODULE_LICENSE("Dual MIT/GPL"); 118c2ecf20Sopenharmony_ci 128c2ecf20Sopenharmony_ci/** 138c2ecf20Sopenharmony_ci * glob_match - Shell-style pattern matching, like !fnmatch(pat, str, 0) 148c2ecf20Sopenharmony_ci * @pat: Shell-style pattern to match, e.g. "*.[ch]". 158c2ecf20Sopenharmony_ci * @str: String to match. The pattern must match the entire string. 168c2ecf20Sopenharmony_ci * 178c2ecf20Sopenharmony_ci * Perform shell-style glob matching, returning true (1) if the match 188c2ecf20Sopenharmony_ci * succeeds, or false (0) if it fails. Equivalent to !fnmatch(@pat, @str, 0). 198c2ecf20Sopenharmony_ci * 208c2ecf20Sopenharmony_ci * Pattern metacharacters are ?, *, [ and \. 218c2ecf20Sopenharmony_ci * (And, inside character classes, !, - and ].) 228c2ecf20Sopenharmony_ci * 238c2ecf20Sopenharmony_ci * This is small and simple implementation intended for device blacklists 248c2ecf20Sopenharmony_ci * where a string is matched against a number of patterns. Thus, it 258c2ecf20Sopenharmony_ci * does not preprocess the patterns. It is non-recursive, and run-time 268c2ecf20Sopenharmony_ci * is at most quadratic: strlen(@str)*strlen(@pat). 278c2ecf20Sopenharmony_ci * 288c2ecf20Sopenharmony_ci * An example of the worst case is glob_match("*aaaaa", "aaaaaaaaaa"); 298c2ecf20Sopenharmony_ci * it takes 6 passes over the pattern before matching the string. 308c2ecf20Sopenharmony_ci * 318c2ecf20Sopenharmony_ci * Like !fnmatch(@pat, @str, 0) and unlike the shell, this does NOT 328c2ecf20Sopenharmony_ci * treat / or leading . specially; it isn't actually used for pathnames. 338c2ecf20Sopenharmony_ci * 348c2ecf20Sopenharmony_ci * Note that according to glob(7) (and unlike bash), character classes 358c2ecf20Sopenharmony_ci * are complemented by a leading !; this does not support the regex-style 368c2ecf20Sopenharmony_ci * [^a-z] syntax. 378c2ecf20Sopenharmony_ci * 388c2ecf20Sopenharmony_ci * An opening bracket without a matching close is matched literally. 398c2ecf20Sopenharmony_ci */ 408c2ecf20Sopenharmony_cibool __pure glob_match(char const *pat, char const *str) 418c2ecf20Sopenharmony_ci{ 428c2ecf20Sopenharmony_ci /* 438c2ecf20Sopenharmony_ci * Backtrack to previous * on mismatch and retry starting one 448c2ecf20Sopenharmony_ci * character later in the string. Because * matches all characters 458c2ecf20Sopenharmony_ci * (no exception for /), it can be easily proved that there's 468c2ecf20Sopenharmony_ci * never a need to backtrack multiple levels. 478c2ecf20Sopenharmony_ci */ 488c2ecf20Sopenharmony_ci char const *back_pat = NULL, *back_str = back_str; 498c2ecf20Sopenharmony_ci 508c2ecf20Sopenharmony_ci /* 518c2ecf20Sopenharmony_ci * Loop over each token (character or class) in pat, matching 528c2ecf20Sopenharmony_ci * it against the remaining unmatched tail of str. Return false 538c2ecf20Sopenharmony_ci * on mismatch, or true after matching the trailing nul bytes. 548c2ecf20Sopenharmony_ci */ 558c2ecf20Sopenharmony_ci for (;;) { 568c2ecf20Sopenharmony_ci unsigned char c = *str++; 578c2ecf20Sopenharmony_ci unsigned char d = *pat++; 588c2ecf20Sopenharmony_ci 598c2ecf20Sopenharmony_ci switch (d) { 608c2ecf20Sopenharmony_ci case '?': /* Wildcard: anything but nul */ 618c2ecf20Sopenharmony_ci if (c == '\0') 628c2ecf20Sopenharmony_ci return false; 638c2ecf20Sopenharmony_ci break; 648c2ecf20Sopenharmony_ci case '*': /* Any-length wildcard */ 658c2ecf20Sopenharmony_ci if (*pat == '\0') /* Optimize trailing * case */ 668c2ecf20Sopenharmony_ci return true; 678c2ecf20Sopenharmony_ci back_pat = pat; 688c2ecf20Sopenharmony_ci back_str = --str; /* Allow zero-length match */ 698c2ecf20Sopenharmony_ci break; 708c2ecf20Sopenharmony_ci case '[': { /* Character class */ 718c2ecf20Sopenharmony_ci bool match = false, inverted = (*pat == '!'); 728c2ecf20Sopenharmony_ci char const *class = pat + inverted; 738c2ecf20Sopenharmony_ci unsigned char a = *class++; 748c2ecf20Sopenharmony_ci 758c2ecf20Sopenharmony_ci /* 768c2ecf20Sopenharmony_ci * Iterate over each span in the character class. 778c2ecf20Sopenharmony_ci * A span is either a single character a, or a 788c2ecf20Sopenharmony_ci * range a-b. The first span may begin with ']'. 798c2ecf20Sopenharmony_ci */ 808c2ecf20Sopenharmony_ci do { 818c2ecf20Sopenharmony_ci unsigned char b = a; 828c2ecf20Sopenharmony_ci 838c2ecf20Sopenharmony_ci if (a == '\0') /* Malformed */ 848c2ecf20Sopenharmony_ci goto literal; 858c2ecf20Sopenharmony_ci 868c2ecf20Sopenharmony_ci if (class[0] == '-' && class[1] != ']') { 878c2ecf20Sopenharmony_ci b = class[1]; 888c2ecf20Sopenharmony_ci 898c2ecf20Sopenharmony_ci if (b == '\0') 908c2ecf20Sopenharmony_ci goto literal; 918c2ecf20Sopenharmony_ci 928c2ecf20Sopenharmony_ci class += 2; 938c2ecf20Sopenharmony_ci /* Any special action if a > b? */ 948c2ecf20Sopenharmony_ci } 958c2ecf20Sopenharmony_ci match |= (a <= c && c <= b); 968c2ecf20Sopenharmony_ci } while ((a = *class++) != ']'); 978c2ecf20Sopenharmony_ci 988c2ecf20Sopenharmony_ci if (match == inverted) 998c2ecf20Sopenharmony_ci goto backtrack; 1008c2ecf20Sopenharmony_ci pat = class; 1018c2ecf20Sopenharmony_ci } 1028c2ecf20Sopenharmony_ci break; 1038c2ecf20Sopenharmony_ci case '\\': 1048c2ecf20Sopenharmony_ci d = *pat++; 1058c2ecf20Sopenharmony_ci /* fall through */ 1068c2ecf20Sopenharmony_ci default: /* Literal character */ 1078c2ecf20Sopenharmony_ciliteral: 1088c2ecf20Sopenharmony_ci if (c == d) { 1098c2ecf20Sopenharmony_ci if (d == '\0') 1108c2ecf20Sopenharmony_ci return true; 1118c2ecf20Sopenharmony_ci break; 1128c2ecf20Sopenharmony_ci } 1138c2ecf20Sopenharmony_cibacktrack: 1148c2ecf20Sopenharmony_ci if (c == '\0' || !back_pat) 1158c2ecf20Sopenharmony_ci return false; /* No point continuing */ 1168c2ecf20Sopenharmony_ci /* Try again from last *, one character later in str. */ 1178c2ecf20Sopenharmony_ci pat = back_pat; 1188c2ecf20Sopenharmony_ci str = ++back_str; 1198c2ecf20Sopenharmony_ci break; 1208c2ecf20Sopenharmony_ci } 1218c2ecf20Sopenharmony_ci } 1228c2ecf20Sopenharmony_ci} 1238c2ecf20Sopenharmony_ciEXPORT_SYMBOL(glob_match); 124