Coverage Report

Created: 2026-09-14 07:06

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/curl/lib/curl_fnmatch.c
Line
Count
Source
1
/***************************************************************************
2
 *                                  _   _ ____  _
3
 *  Project                     ___| | | |  _ \| |
4
 *                             / __| | | | |_) | |
5
 *                            | (__| |_| |  _ <| |___
6
 *                             \___|\___/|_| \_\_____|
7
 *
8
 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
9
 *
10
 * This software is licensed as described in the file COPYING, which
11
 * you should have received as part of this distribution. The terms
12
 * are also available at https://curl.se/docs/copyright.html.
13
 *
14
 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15
 * copies of the Software, and permit persons to whom the Software is
16
 * furnished to do so, under the terms of the COPYING file.
17
 *
18
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19
 * KIND, either express or implied.
20
 *
21
 * SPDX-License-Identifier: curl
22
 *
23
 ***************************************************************************/
24
#include "curl_setup.h"
25
26
#ifndef CURL_DISABLE_FTP
27
28
#include "curl_fnmatch.h"
29
30
#ifndef HAVE_FNMATCH
31
32
#define CURLFNM_CHARSET_LEN (sizeof(char) * 256)
33
#define CURLFNM_CHSET_SIZE (CURLFNM_CHARSET_LEN + 15)
34
35
#define CURLFNM_NEGATE  CURLFNM_CHARSET_LEN
36
37
#define CURLFNM_ALNUM   (CURLFNM_CHARSET_LEN + 1)
38
#define CURLFNM_DIGIT   (CURLFNM_CHARSET_LEN + 2)
39
#define CURLFNM_XDIGIT  (CURLFNM_CHARSET_LEN + 3)
40
#define CURLFNM_ALPHA   (CURLFNM_CHARSET_LEN + 4)
41
#define CURLFNM_PRINT   (CURLFNM_CHARSET_LEN + 5)
42
#define CURLFNM_BLANK   (CURLFNM_CHARSET_LEN + 6)
43
#define CURLFNM_LOWER   (CURLFNM_CHARSET_LEN + 7)
44
#define CURLFNM_GRAPH   (CURLFNM_CHARSET_LEN + 8)
45
#define CURLFNM_SPACE   (CURLFNM_CHARSET_LEN + 9)
46
#define CURLFNM_UPPER   (CURLFNM_CHARSET_LEN + 10)
47
48
typedef enum {
49
  CURLFNM_SCHS_DEFAULT = 0,
50
  CURLFNM_SCHS_RIGHTBR,
51
  CURLFNM_SCHS_RIGHTBRLEFTBR
52
} setcharset_state;
53
54
typedef enum {
55
  CURLFNM_PKW_INIT = 0,
56
  CURLFNM_PKW_DDOT
57
} parsekey_state;
58
59
typedef enum {
60
  CCLASS_OTHER = 0,
61
  CCLASS_DIGIT,
62
  CCLASS_UPPER,
63
  CCLASS_LOWER
64
} char_class;
65
66
#define SETCHARSET_OK     1
67
#define SETCHARSET_FAIL   0
68
69
static int parsekeyword(const unsigned char **pattern, unsigned char *charset)
70
{
71
  parsekey_state state = CURLFNM_PKW_INIT;
72
  char keyword[10] = { 0 };
73
  size_t i;
74
  const unsigned char *p = *pattern;
75
  bool found = FALSE;
76
  for(i = 0; !found; i++) {
77
    char c = (char)*p++;
78
    if(i >= sizeof(keyword))
79
      return SETCHARSET_FAIL;
80
    switch(state) {
81
    case CURLFNM_PKW_INIT:
82
      if(ISLOWER(c))
83
        keyword[i] = c;
84
      else if(c == ':')
85
        state = CURLFNM_PKW_DDOT;
86
      else
87
        return SETCHARSET_FAIL;
88
      break;
89
    case CURLFNM_PKW_DDOT:
90
      if(c == ']')
91
        found = TRUE;
92
      else
93
        return SETCHARSET_FAIL;
94
    }
95
  }
96
#undef KEYLEN
97
98
  *pattern = p; /* move caller's pattern pointer */
99
  if(!strcmp(keyword, "digit"))
100
    charset[CURLFNM_DIGIT] = 1;
101
  else if(!strcmp(keyword, "alnum"))
102
    charset[CURLFNM_ALNUM] = 1;
103
  else if(!strcmp(keyword, "alpha"))
104
    charset[CURLFNM_ALPHA] = 1;
105
  else if(!strcmp(keyword, "xdigit"))
106
    charset[CURLFNM_XDIGIT] = 1;
107
  else if(!strcmp(keyword, "print"))
108
    charset[CURLFNM_PRINT] = 1;
109
  else if(!strcmp(keyword, "graph"))
110
    charset[CURLFNM_GRAPH] = 1;
111
  else if(!strcmp(keyword, "space"))
112
    charset[CURLFNM_SPACE] = 1;
113
  else if(!strcmp(keyword, "blank"))
114
    charset[CURLFNM_BLANK] = 1;
115
  else if(!strcmp(keyword, "upper"))
116
    charset[CURLFNM_UPPER] = 1;
117
  else if(!strcmp(keyword, "lower"))
118
    charset[CURLFNM_LOWER] = 1;
119
  else
120
    return SETCHARSET_FAIL;
121
  return SETCHARSET_OK;
122
}
123
124
/* Return the character class. */
125
static char_class charclass(unsigned char c)
126
{
127
  if(ISUPPER(c))
128
    return CCLASS_UPPER;
129
  if(ISLOWER(c))
130
    return CCLASS_LOWER;
131
  if(ISDIGIT(c))
132
    return CCLASS_DIGIT;
133
  return CCLASS_OTHER;
134
}
135
136
/* Include a character or a range in set. */
137
static void setcharorrange(const unsigned char **pp, unsigned char *charset)
138
{
139
  const unsigned char *p = (*pp)++;
140
  unsigned char c = *p++;
141
142
  charset[c] = 1;
143
  if(ISALNUM(c) && *p++ == '-') {
144
    char_class cc = charclass(c);
145
    unsigned char endrange = *p++;
146
147
    if(endrange == '\\')
148
      endrange = *p++;
149
    if(endrange >= c && charclass(endrange) == cc) {
150
      while(c++ != endrange)
151
        if(charclass(c) == cc)  /* Chars in class may be not consecutive. */
152
          charset[c] = 1;
153
      *pp = p;
154
    }
155
  }
156
}
157
158
/* returns 1 (TRUE) if pattern is OK, 0 if is bad ("p" is pattern pointer) */
159
static int setcharset(const unsigned char **p, unsigned char *charset)
160
{
161
  setcharset_state state = CURLFNM_SCHS_DEFAULT;
162
  bool something_found = FALSE;
163
  unsigned char c;
164
165
  memset(charset, 0, CURLFNM_CHSET_SIZE);
166
  for(;;) {
167
    c = **p;
168
    if(!c)
169
      return SETCHARSET_FAIL;
170
171
    switch(state) {
172
    case CURLFNM_SCHS_DEFAULT:
173
      if(c == ']') {
174
        if(something_found)
175
          return SETCHARSET_OK;
176
        something_found = TRUE;
177
        state = CURLFNM_SCHS_RIGHTBR;
178
        charset[c] = 1;
179
        (*p)++;
180
      }
181
      else if(c == '[') {
182
        const unsigned char *pp = *p + 1;
183
184
        if(*pp++ == ':' && parsekeyword(&pp, charset))
185
          *p = pp;
186
        else {
187
          charset[c] = 1;
188
          (*p)++;
189
        }
190
        something_found = TRUE;
191
      }
192
      else if(c == '^' || c == '!') {
193
        if(!something_found) {
194
          if(charset[CURLFNM_NEGATE]) {
195
            charset[c] = 1;
196
            something_found = TRUE;
197
          }
198
          else
199
            charset[CURLFNM_NEGATE] = 1; /* negate charset */
200
        }
201
        else
202
          charset[c] = 1;
203
        (*p)++;
204
      }
205
      else if(c == '\\') {
206
        c = *(++(*p));
207
        if(c)
208
          setcharorrange(p, charset);
209
        else
210
          charset['\\'] = 1;
211
        something_found = TRUE;
212
      }
213
      else {
214
        setcharorrange(p, charset);
215
        something_found = TRUE;
216
      }
217
      break;
218
    case CURLFNM_SCHS_RIGHTBR:
219
      if(c == '[') {
220
        state = CURLFNM_SCHS_RIGHTBRLEFTBR;
221
        charset[c] = 1;
222
        (*p)++;
223
      }
224
      else if(c == ']') {
225
        return SETCHARSET_OK;
226
      }
227
      else if(ISPRINT(c)) {
228
        charset[c] = 1;
229
        (*p)++;
230
        state = CURLFNM_SCHS_DEFAULT;
231
      }
232
      else
233
        /* used 'goto fail' instead of 'return SETCHARSET_FAIL' to avoid a
234
         * nonsense warning 'statement not reached' at end of the fnc when
235
         * compiling on Solaris */
236
        goto fail;
237
      break;
238
    case CURLFNM_SCHS_RIGHTBRLEFTBR:
239
      if(c == ']')
240
        return SETCHARSET_OK;
241
      state = CURLFNM_SCHS_DEFAULT;
242
      charset[c] = 1;
243
      (*p)++;
244
      break;
245
    }
246
  }
247
fail:
248
  return SETCHARSET_FAIL;
249
}
250
251
/* match one pattern token against one string character, advancing both */
252
static int matchtoken(const unsigned char **pp, const unsigned char **sp)
253
{
254
  const unsigned char *p = *pp;
255
  const unsigned char *s = *sp;
256
257
  switch(*p) {
258
  case '?':
259
    break;
260
  case '\\':
261
    if(p[1])
262
      p++;
263
    if(*s != *p)
264
      return 0;
265
    break;
266
  case '[': {
267
    unsigned char charset[CURLFNM_CHSET_SIZE];
268
    const unsigned char *pp2 = p + 1; /* Copy in case of syntax error. */
269
    bool found = FALSE;
270
    if(!setcharset(&pp2, charset))
271
      return -1; /* Syntax error in set; mismatch! */
272
    if(charset[(unsigned int)*s])
273
      found = TRUE;
274
    else if(charset[CURLFNM_ALNUM])
275
      found = ISALNUM(*s);
276
    else if(charset[CURLFNM_ALPHA])
277
      found = ISALPHA(*s);
278
    else if(charset[CURLFNM_DIGIT])
279
      found = ISDIGIT(*s);
280
    else if(charset[CURLFNM_XDIGIT])
281
      found = ISXDIGIT(*s);
282
    else if(charset[CURLFNM_PRINT])
283
      found = ISPRINT(*s);
284
    else if(charset[CURLFNM_SPACE])
285
      found = ISBLANK(*s);
286
    else if(charset[CURLFNM_UPPER])
287
      found = ISUPPER(*s);
288
    else if(charset[CURLFNM_LOWER])
289
      found = ISLOWER(*s);
290
    else if(charset[CURLFNM_BLANK])
291
      found = ISBLANK(*s);
292
    else if(charset[CURLFNM_GRAPH])
293
      found = ISGRAPH(*s);
294
295
    if(charset[CURLFNM_NEGATE])
296
      found = !found;
297
298
    if(!found)
299
      return 0;
300
    *pp = pp2 + 1;
301
    *sp = s + 1;
302
    return 1;
303
  }
304
  default:
305
    if(*p != *s)
306
      return 0;
307
    break;
308
  }
309
  *pp = p + 1;
310
  *sp = s + 1;
311
  return 1;
312
}
313
314
/* greedy match with backtracking to the most recent '*' */
315
static int loop(const unsigned char *pattern, const unsigned char *string)
316
{
317
  const unsigned char *p = pattern;
318
  const unsigned char *s = string;
319
  const unsigned char *star_p = NULL;
320
  const unsigned char *star_s = NULL;
321
  int maxstars = 2;
322
323
  while(*s) {
324
    if(*p == '*') {
325
      if(!maxstars)
326
        return CURL_FNMATCH_NOMATCH;
327
      maxstars--;
328
      for(;;) {
329
        p++;
330
        if(!*p)
331
          return CURL_FNMATCH_MATCH;
332
        if(*p == '?') {
333
          if(!*s)
334
            return CURL_FNMATCH_NOMATCH;
335
          s++;
336
        }
337
        else if(*p != '*')
338
          break;
339
      }
340
      star_p = p;
341
      star_s = s;
342
    }
343
    else if(*p) {
344
      const unsigned char *np = p;
345
      const unsigned char *ns = s;
346
      int r = matchtoken(&np, &ns);
347
      if(r < 0)
348
        return CURL_FNMATCH_NOMATCH;
349
      if(r) {
350
        p = np;
351
        s = ns;
352
        continue;
353
      }
354
      if(!star_p)
355
        return CURL_FNMATCH_NOMATCH;
356
      p = star_p;
357
      s = ++star_s;
358
    }
359
    else if(star_p) {
360
      p = star_p;
361
      s = ++star_s;
362
    }
363
    else
364
      return CURL_FNMATCH_NOMATCH;
365
  }
366
  while(*p == '*') {
367
    if(!maxstars)
368
      return CURL_FNMATCH_NOMATCH;
369
    maxstars--;
370
    do {
371
      p++;
372
      if(*p == '?')
373
        return CURL_FNMATCH_NOMATCH;
374
    } while(*p == '*');
375
  }
376
  return *p ? CURL_FNMATCH_NOMATCH : CURL_FNMATCH_MATCH;
377
}
378
379
/*
380
 * @unittest: 1307
381
 */
382
int Curl_fnmatch(void *ptr, const char *pattern, const char *string)
383
{
384
  (void)ptr; /* the argument is specified by the curl_fnmatch_callback
385
                prototype, but not used by Curl_fnmatch() */
386
  if(!pattern || !string) {
387
    return CURL_FNMATCH_FAIL;
388
  }
389
  return loop((const unsigned char *)pattern,
390
              (const unsigned char *)string);
391
}
392
#else /* HAVE_FNMATCH */
393
394
#include <fnmatch.h>
395
/*
396
 * @unittest: 1307
397
 */
398
int Curl_fnmatch(void *ptr, const char *pattern, const char *string)
399
0
{
400
0
  (void)ptr; /* the argument is specified by the curl_fnmatch_callback
401
                prototype, but not used by Curl_fnmatch() */
402
0
  if(!pattern || !string) {
403
0
    return CURL_FNMATCH_FAIL;
404
0
  }
405
406
0
  switch(fnmatch(pattern, string, 0)) {
407
0
  case 0:
408
0
    return CURL_FNMATCH_MATCH;
409
0
  case FNM_NOMATCH:
410
0
    return CURL_FNMATCH_NOMATCH;
411
0
  default:
412
0
    return CURL_FNMATCH_FAIL;
413
0
  }
414
  /* not reached */
415
0
}
416
#endif /* !HAVE_FNMATCH */
417
418
#endif /* !CURL_DISABLE_FTP */