fuzzing: fix fuzz_stable_sort_r_unstable comparison
[Samba.git] / lib / util / ms_fnmatch.c
blob48454a8eccee4fa1bfaa64276b6598a37b2daf9e
1 /*
2 Unix SMB/CIFS implementation.
3 filename matching routine
4 Copyright (C) Andrew Tridgell 1992-2004
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 This module was originally based on fnmatch.c copyright by the Free
22 Software Foundation. It bears little (if any) resemblance to that
23 code now
26 /**
27 * @file
28 * @brief MS-style Filename matching
31 #include "replace.h"
32 #include "lib/util/samba_util.h"
33 #include "libcli/smb/smb_constants.h"
35 static int null_match(const char *p)
37 for (;*p;p++) {
38 if (*p != '*' &&
39 *p != '<' &&
40 *p != '"' &&
41 *p != '>') return -1;
43 return 0;
47 the max_n structure is purely for efficiency, it doesn't contribute
48 to the matching algorithm except by ensuring that the algorithm does
49 not grow exponentially
51 struct max_n {
52 const char *predot;
53 const char *postdot;
58 p and n are the pattern and string being matched. The max_n array is
59 an optimisation only. The ldot pointer is NULL if the string does
60 not contain a '.', otherwise it points at the last dot in 'n'.
62 static int ms_fnmatch_core(const char *p, const char *n,
63 struct max_n *max_n, const char *ldot,
64 bool is_case_sensitive)
66 codepoint_t c, c2;
67 int i;
68 size_t size, size_n;
70 while ((c = next_codepoint(p, &size))) {
71 p += size;
73 switch (c) {
74 case '*':
75 /* a '*' matches zero or more characters of any type */
76 if (max_n != NULL && max_n->predot &&
77 max_n->predot <= n) {
78 return null_match(p);
80 for (i=0; n[i]; i += size_n) {
81 next_codepoint(n+i, &size_n);
82 if (ms_fnmatch_core(p, n+i, max_n+1, ldot, is_case_sensitive) == 0) {
83 return 0;
86 if (max_n != NULL && (!max_n->predot ||
87 max_n->predot > n)) {
88 max_n->predot = n;
90 return null_match(p);
92 case '<':
93 /* a '<' matches zero or more characters of
94 any type, but stops matching at the last
95 '.' in the string. */
96 if (max_n != NULL && max_n->predot &&
97 max_n->predot <= n) {
98 return null_match(p);
100 if (max_n != NULL && max_n->postdot &&
101 max_n->postdot <= n && n <= ldot) {
102 return -1;
104 for (i=0; n[i]; i += size_n) {
105 next_codepoint(n+i, &size_n);
106 if (ms_fnmatch_core(p, n+i, max_n+1, ldot, is_case_sensitive) == 0) return 0;
107 if (n+i == ldot) {
108 if (ms_fnmatch_core(p, n+i+size_n, max_n+1, ldot, is_case_sensitive) == 0) return 0;
109 if (max_n != NULL) {
110 if (!max_n->postdot ||
111 max_n->postdot > n) {
112 max_n->postdot = n;
115 return -1;
118 if (max_n != NULL && (!max_n->predot ||
119 max_n->predot > n)) {
120 max_n->predot = n;
122 return null_match(p);
124 case '?':
125 /* a '?' matches any single character */
126 if (! *n) {
127 return -1;
129 next_codepoint(n, &size_n);
130 n += size_n;
131 break;
133 case '>':
134 /* a '?' matches any single character, but
135 treats '.' specially */
136 if (n[0] == '.') {
137 if (! n[1] && null_match(p) == 0) {
138 return 0;
140 break;
142 if (! *n) return null_match(p);
143 next_codepoint(n, &size_n);
144 n += size_n;
145 break;
147 case '"':
148 /* a bit like a soft '.' */
149 if (*n == 0 && null_match(p) == 0) {
150 return 0;
152 if (*n != '.') return -1;
153 next_codepoint(n, &size_n);
154 n += size_n;
155 break;
157 default:
158 c2 = next_codepoint(n, &size_n);
159 if (c != c2) {
160 if (is_case_sensitive) {
161 return -1;
163 if (codepoint_cmpi(c, c2) != 0) {
164 return -1;
167 n += size_n;
168 break;
172 if (! *n) {
173 return 0;
176 return -1;
179 int ms_fnmatch_protocol(const char *pattern, const char *string, int protocol,
180 bool is_case_sensitive)
182 int ret = -1;
183 size_t count, i;
185 if (strcmp(string, "..") == 0) {
186 string = ".";
189 if (strpbrk(pattern, "<>*?\"") == NULL) {
190 /* this is not just an optimisation - it is essential
191 for LANMAN1 correctness */
192 return strcasecmp_m(pattern, string);
195 if (protocol <= PROTOCOL_LANMAN2) {
196 char *p = talloc_strdup(NULL, pattern);
197 if (p == NULL) {
198 return -1;
201 for older negotiated protocols it is possible to
202 translate the pattern to produce a "new style"
203 pattern that exactly matches w2k behaviour
205 for (i=0;p[i];i++) {
206 if (p[i] == '?') {
207 p[i] = '>';
208 } else if (p[i] == '.' &&
209 (p[i+1] == '?' ||
210 p[i+1] == '*' ||
211 p[i+1] == 0)) {
212 p[i] = '"';
213 } else if (p[i] == '*' &&
214 p[i+1] == '.') {
215 p[i] = '<';
218 ret = ms_fnmatch_protocol(p, string, PROTOCOL_NT1,
219 is_case_sensitive);
220 talloc_free(p);
221 return ret;
224 for (count=i=0;pattern[i];i++) {
225 if (pattern[i] == '*' || pattern[i] == '<') count++;
228 /* If the pattern includes '*' or '<' */
229 if (count >= 1) {
230 struct max_n max_n[count];
232 memset(max_n, 0, sizeof(struct max_n) * count);
234 ret = ms_fnmatch_core(pattern, string, max_n, strrchr(string, '.'),
235 is_case_sensitive);
236 } else {
237 ret = ms_fnmatch_core(pattern, string, NULL, strrchr(string, '.'),
238 is_case_sensitive);
241 return ret;
245 /** a generic fnmatch function - uses for non-CIFS pattern matching */
246 int gen_fnmatch(const char *pattern, const char *string)
248 return ms_fnmatch_protocol(pattern, string, PROTOCOL_NT1, false);