1 /* match.c - simple shell-style filename matcher
2 **
3 ** Only does ? * and **, and multiple patterns separated by |.  Returns 1 or 0.
4 **
5 ** Copyright � 1995,2000 by Jef Poskanzer <jef@acme.com>.
6 ** All rights reserved.
7 **
8 ** Redistribution and use in source and binary forms, with or without
9 ** modification, are permitted provided that the following conditions
10 ** are met:
11 ** 1. Redistributions of source code must retain the above copyright
12 **    notice, this list of conditions and the following disclaimer.
13 ** 2. Redistributions in binary form must reproduce the above copyright
14 **    notice, this list of conditions and the following disclaimer in the
15 **    documentation and/or other materials provided with the distribution.
16 **
17 ** THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 ** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 ** ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 ** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 ** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 ** OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 ** HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 ** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 ** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 ** SUCH DAMAGE.
28 */
29 /* 20131121 slightly modified by Sampo Kellomaki (sampo@zxid.org) for zxid project.
30  * See also fnmatch(1). */
31 
32 #include <zx/errmac.h>
33 #include <string.h>
34 
35 /* Called by:  zx_match x2, zx_match_one */
zx_match_one(const char * pat,int patlen,const char * str)36 static int zx_match_one(const char* pat, int patlen, const char* str)
37 {
38   const char* p;
39   int i, pl;
40 
41   for ( p = pat; p - pat < patlen; ++p, ++str ) {
42     if ( *p == '?' && *str != '\0' )
43       continue;
44     if ( *p == '*' ) {
45       ++p;
46       if ( *p == '*' ) {
47 	/* Double-wildcard matches anything. */
48 	++p;
49 	i = strlen( str );
50       } else
51 	/* Single-wildcard matches anything but slash. */
52 	i = strcspn( str, "/" );
53       pl = patlen - ( p - pat );
54       for ( ; i >= 0; --i )  /* try the rest of the pat to tails of str */
55 	if ( zx_match_one( p, pl, &(str[i]) ) )
56 	  return 1;
57       return 0;
58     }
59     if ( *p != *str )
60       return 0;
61   }
62   if ( *str == '\0' )
63     return 1;
64   return 0;
65 }
66 
67 /*() Check if simple path glob wild card pattern matches.
68  * Returns 0 on failure and 1 on match.
69  * Only does ?, * and **, and multiple patterns separated by |.
70  * Exact match, suffix match (*.wsp) and prefix match
71  * (/foo/bar*) are supported. The double asterisk (**) matches
72  * also slash (/). */
73 
74 /* Called by:  chkuid, do_file, really_check_referer x3, send_error_and_exit, zxid_mini_httpd_filter x2 */
zx_match(const char * pat,const char * str)75 int zx_match(const char* pat, const char* str)
76 {
77   const char* or_clause;
78   DD("pat(%s) str(%s)", pat, str);
79   for (;;) {
80     or_clause = strchr( pat, '|' );
81     if (!or_clause)
82       return zx_match_one( pat, strlen( pat ), str );
83     if ( zx_match_one( pat, or_clause - pat, str ) )
84       return 1;
85     pat = or_clause + 1;
86   }
87 }
88 
89 /* EOF */
90