1/* Copyright (C) 1991-2021 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>. */
17
18#ifndef _LIBC
19# include <libc-config.h>
20#endif
21
22/* Enable GNU extensions in fnmatch.h. */
23#ifndef _GNU_SOURCE
24# define _GNU_SOURCE 1
25#endif
26
27#include <fnmatch.h>
28
29#include <assert.h>
30#include <errno.h>
31#include <ctype.h>
32#include <string.h>
33#include <stdlib.h>
34#if defined _LIBC || HAVE_ALLOCA
35# include <alloca.h>
36#endif
37#include <wchar.h>
38#include <wctype.h>
39#include <stddef.h>
40#include <stdbool.h>
41
42/* We need some of the locale data (the collation sequence information)
43 but there is no interface to get this information in general. Therefore
44 we support a correct implementation only in glibc. */
45#ifdef _LIBC
46# include "../locale/localeinfo.h"
47# include "../locale/coll-lookup.h"
48# include <shlib-compat.h>
49
50# define CONCAT(a,b) __CONCAT(a,b)
51# define btowc __btowc
52# define iswctype __iswctype
53# define mbsrtowcs __mbsrtowcs
54# define mempcpy __mempcpy
55# define strnlen __strnlen
56# define towlower __towlower
57# define wcscat __wcscat
58# define wcslen __wcslen
59# define wctype __wctype
60# define wmemchr __wmemchr
61# define wmempcpy __wmempcpy
62# define fnmatch __fnmatch
63extern int fnmatch (const char *pattern, const char *string, int flags);
64#endif
65
66#ifdef _LIBC
67# if __GNUC__ >= 7
68# define FALLTHROUGH __attribute__ ((__fallthrough__))
69# else
70# define FALLTHROUGH ((void) 0)
71# endif
72#else
73# include "attribute.h"
74#endif
75
76#include <intprops.h>
77#include <flexmember.h>
78#include <scratch_buffer.h>
79
80#ifdef _LIBC
81typedef ptrdiff_t idx_t;
82#else
83# include "idx.h"
84#endif
85
86/* We often have to test for FNM_FILE_NAME and FNM_PERIOD being both set. */
87#define NO_LEADING_PERIOD(flags) \
88 ((flags & (FNM_FILE_NAME | FNM_PERIOD)) == (FNM_FILE_NAME | FNM_PERIOD))
89
90#ifndef _LIBC
91# if HAVE_ALLOCA
92/* The OS usually guarantees only one guard page at the bottom of the stack,
93 and a page size can be as small as 4096 bytes. So we cannot safely
94 allocate anything larger than 4096 bytes. Also care for the possibility
95 of a few compiler-allocated temporary stack slots. */
96# define __libc_use_alloca(n) ((n) < 4032)
97# else
98/* Just use malloc. */
99# define __libc_use_alloca(n) false
100# undef alloca
101# define alloca(n) malloc (n)
102# endif
103# define alloca_account(size, avar) ((avar) += (size), alloca (size))
104#endif
105
106/* Provide support for user-defined character classes, based on the functions
107 from ISO C 90 amendment 1. */
108#ifdef CHARCLASS_NAME_MAX
109# define CHAR_CLASS_MAX_LENGTH CHARCLASS_NAME_MAX
110#else
111/* This shouldn't happen but some implementation might still have this
112 problem. Use a reasonable default value. */
113# define CHAR_CLASS_MAX_LENGTH 256
114#endif
115
116#define IS_CHAR_CLASS(string) wctype (string)
117
118/* Avoid depending on library functions or files
119 whose names are inconsistent. */
120
121/* Global variable. */
122static int posixly_correct;
123
124/* Note that this evaluates C many times. */
125#define FOLD(c) ((flags & FNM_CASEFOLD) ? tolower (c) : (c))
126#define CHAR char
127#define UCHAR unsigned char
128#define INT int
129#define FCT internal_fnmatch
130#define EXT ext_match
131#define END end_pattern
132#define STRUCT fnmatch_struct
133#define L_(CS) CS
134#define BTOWC(C) btowc (C)
135#define STRLEN(S) strlen (S)
136#define STRCAT(D, S) strcat (D, S)
137#define MEMPCPY(D, S, N) mempcpy (D, S, N)
138#define MEMCHR(S, C, N) memchr (S, C, N)
139#define WIDE_CHAR_VERSION 0
140#ifdef _LIBC
141# include <locale/weight.h>
142# define FINDIDX findidx
143#endif
144#include "fnmatch_loop.c"
145
146
147#define FOLD(c) ((flags & FNM_CASEFOLD) ? towlower (c) : (c))
148#define CHAR wchar_t
149#define UCHAR wint_t
150#define INT wint_t
151#define FCT internal_fnwmatch
152#define EXT ext_wmatch
153#define END end_wpattern
154#define L_(CS) L##CS
155#define BTOWC(C) (C)
156#define STRLEN(S) wcslen (S)
157#define STRCAT(D, S) wcscat (D, S)
158#define MEMPCPY(D, S, N) wmempcpy (D, S, N)
159#define MEMCHR(S, C, N) wmemchr (S, C, N)
160#define WIDE_CHAR_VERSION 1
161#ifdef _LIBC
162/* Change the name the header defines so it doesn't conflict with
163 the <locale/weight.h> version included above. */
164# define findidx findidxwc
165# include <locale/weightwc.h>
166# undef findidx
167# define FINDIDX findidxwc
168#endif
169
170#undef IS_CHAR_CLASS
171/* We have to convert the wide character string in a multibyte string. But
172 we know that the character class names consist of alphanumeric characters
173 from the portable character set, and since the wide character encoding
174 for a member of the portable character set is the same code point as
175 its single-byte encoding, we can use a simplified method to convert the
176 string to a multibyte character string. */
177static wctype_t
178is_char_class (const wchar_t *wcs)
179{
180 char s[CHAR_CLASS_MAX_LENGTH + 1];
181 char *cp = s;
182
183 do
184 {
185 /* Test for a printable character from the portable character set. */
186#ifdef _LIBC
187 if (*wcs < 0x20 || *wcs > 0x7e
188 || *wcs == 0x24 || *wcs == 0x40 || *wcs == 0x60)
189 return (wctype_t) 0;
190#else
191 switch (*wcs)
192 {
193 case L' ': case L'!': case L'"': case L'#': case L'%':
194 case L'&': case L'\'': case L'(': case L')': case L'*':
195 case L'+': case L',': case L'-': case L'.': case L'/':
196 case L'0': case L'1': case L'2': case L'3': case L'4':
197 case L'5': case L'6': case L'7': case L'8': case L'9':
198 case L':': case L';': case L'<': case L'=': case L'>':
199 case L'?':
200 case L'A': case L'B': case L'C': case L'D': case L'E':
201 case L'F': case L'G': case L'H': case L'I': case L'J':
202 case L'K': case L'L': case L'M': case L'N': case L'O':
203 case L'P': case L'Q': case L'R': case L'S': case L'T':
204 case L'U': case L'V': case L'W': case L'X': case L'Y':
205 case L'Z':
206 case L'[': case L'\\': case L']': case L'^': case L'_':
207 case L'a': case L'b': case L'c': case L'd': case L'e':
208 case L'f': case L'g': case L'h': case L'i': case L'j':
209 case L'k': case L'l': case L'm': case L'n': case L'o':
210 case L'p': case L'q': case L'r': case L's': case L't':
211 case L'u': case L'v': case L'w': case L'x': case L'y':
212 case L'z': case L'{': case L'|': case L'}': case L'~':
213 break;
214 default:
215 return (wctype_t) 0;
216 }
217#endif
218
219 /* Avoid overrunning the buffer. */
220 if (cp == s + CHAR_CLASS_MAX_LENGTH)
221 return (wctype_t) 0;
222
223 *cp++ = (char) *wcs++;
224 }
225 while (*wcs != L'\0');
226
227 *cp = '\0';
228
229 return wctype (s);
230}
231#define IS_CHAR_CLASS(string) is_char_class (string)
232
233#include "fnmatch_loop.c"
234
235static int
236fnmatch_convert_to_wide (const char *str, struct scratch_buffer *buf,
237 size_t *n)
238{
239 mbstate_t ps;
240 memset (&ps, '\0', sizeof (ps));
241
242 size_t nw = buf->length / sizeof (wchar_t);
243 *n = strnlen (str, nw - 1);
244 if (__glibc_likely (*n < nw))
245 {
246 const char *p = str;
247 *n = mbsrtowcs (buf->data, &p, *n + 1, &ps);
248 if (__glibc_unlikely (*n == (size_t) -1))
249 /* Something wrong.
250 XXX Do we have to set 'errno' to something which mbsrtows hasn't
251 already done? */
252 return -1;
253 if (p == NULL)
254 return 0;
255 memset (&ps, '\0', sizeof (ps));
256 }
257
258 *n = mbsrtowcs (NULL, &str, 0, &ps);
259 if (__glibc_unlikely (*n == (size_t) -1))
260 return -1;
261 if (!scratch_buffer_set_array_size (buf, *n + 1, sizeof (wchar_t)))
262 {
263 __set_errno (ENOMEM);
264 return -2;
265 }
266 assert (mbsinit (&ps));
267 mbsrtowcs (buf->data, &str, *n + 1, &ps);
268 return 0;
269}
270
271int
272fnmatch (const char *pattern, const char *string, int flags)
273{
274 if (__glibc_unlikely (MB_CUR_MAX != 1))
275 {
276 size_t n;
277 struct scratch_buffer wpattern;
278 scratch_buffer_init (&wpattern);
279 struct scratch_buffer wstring;
280 scratch_buffer_init (&wstring);
281 int r;
282
283 /* Convert the strings into wide characters. Any conversion issue
284 fallback to the ascii version. */
285 r = fnmatch_convert_to_wide (pattern, &wpattern, &n);
286 if (r == 0)
287 {
288 r = fnmatch_convert_to_wide (string, &wstring, &n);
289 if (r == 0)
290 r = internal_fnwmatch (wpattern.data, wstring.data,
291 (wchar_t *) wstring.data + n,
292 flags & FNM_PERIOD, flags, NULL,
293 false);
294 }
295
296 scratch_buffer_free (&wstring);
297 scratch_buffer_free (&wpattern);
298
299 if (r == -2 || r == 0)
300 return r;
301 }
302
303 return internal_fnmatch (pattern, string, string + strlen (string),
304 flags & FNM_PERIOD, flags, NULL, 0);
305}
306
307#undef fnmatch
308versioned_symbol (libc, __fnmatch, fnmatch, GLIBC_2_2_3);
309#if SHLIB_COMPAT(libc, GLIBC_2_0, GLIBC_2_2_3)
310strong_alias (__fnmatch, __fnmatch_old)
311compat_symbol (libc, __fnmatch_old, fnmatch, GLIBC_2_0);
312#endif
313libc_hidden_ver (__fnmatch, fnmatch)
314