1 /* Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
2 
3    This program is free software; you can redistribute it and/or modify
4    it under the terms of the GNU General Public License, version 2.0,
5    as published by the Free Software Foundation.
6 
7    This program is also distributed with certain software (including
8    but not limited to OpenSSL) that is licensed under separate terms,
9    as designated in a particular file or component or in included license
10    documentation.  The authors of MySQL hereby grant you an additional
11    permission to link the program and your derivative works with the
12    separately licensed software that they have included with MySQL.
13 
14    Without limiting anything contained in the foregoing, this file,
15    which is part of C Driver for MySQL (Connector/C), is also subject to the
16    Universal FOSS Exception, version 1.0, a copy of which can be found at
17    http://oss.oracle.com/licenses/universal-foss-exception.
18 
19    This program is distributed in the hope that it will be useful,
20    but WITHOUT ANY WARRANTY; without even the implied warranty of
21    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22    GNU General Public License, version 2.0, for more details.
23 
24    You should have received a copy of the GNU General Public License
25    along with this program; if not, write to the Free Software
26    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */
27 
28 /**
29   @file mysys/mf_wcomp.cc
30   Functions for comparing with wild-cards.
31 */
32 #include "mf_wcomp.h"
33 
34 #include "my_dbug.h"
35 
36 /*
37   This function is different from the wildcmp function of collations.
38   1. This function doesn't guard it from stack overrun.
39   2. This function is not aware of charset.
40   3. This function's expression string can be considered as pattern. Please
41   refer to the declaration.
42 */
wild_compare_full(const char * str,int strlen,const char * wildstr,int wildlen,bool str_is_pattern,char w_prefix,char w_one,char w_many)43 int wild_compare_full(const char *str, int strlen, const char *wildstr,
44                       int wildlen, bool str_is_pattern, char w_prefix,
45                       char w_one, char w_many) {
46   const char *strend = str + strlen;
47   const char *wildend = wildstr + wildlen;
48   char cmp;
49   DBUG_TRACE;
50 
51   while (wildstr < wildend) {
52     /*
53       Loop through expression string (str) and pattern string (wildstr) byte by
54       byte until they are different, or we find a wildcard char (w_many or
55       w_one) in pattern string.
56     */
57     while (wildstr < wildend && *wildstr != w_many && *wildstr != w_one) {
58       if (*wildstr == w_prefix && wildstr + 1 < wildend) {
59         wildstr++;
60         /*
61           If there is a escape char in pattern string, and expression string can
62           be considered as pattern, there should be a escape char in input
63           string too.
64         */
65         if (str_is_pattern && str < strend && *str++ != w_prefix) return 1;
66       }
67       if (str == strend || *wildstr++ != *str++) return 1;
68     }
69     if (wildstr == wildend) return str < strend;
70     /*
71       Skip one char if wildcard is w_one. If expression string can be
72       considered as pattern, any char in expression string except of w_many can
73       be skipped.
74     */
75     if (*wildstr++ == w_one) {
76       if (str == strend || (str_is_pattern && *str == w_many))
77         return 1; /* One char; skip */
78       if (*str++ == w_prefix && str_is_pattern) str++;
79     } else { /* Found '*' */
80       /*
81         If wildcard char is w_many, then we skip any wildcard char following
82         it.
83       */
84       while (str_is_pattern && str < strend && *str == w_many) str++;
85       for (; wildstr < wildend && (*wildstr == w_many || *wildstr == w_one);
86            wildstr++) {
87         if (*wildstr == w_many) {
88           while (str_is_pattern && str < strend && *str == w_many) str++;
89         } else {
90           if (str_is_pattern && str + 1 < strend && *str == w_prefix)
91             str += 2;
92           else if (str == strend)
93             return 1;
94         }
95       }
96       if (wildstr == wildend) return 0; /* '*' as last char: OK */
97       if ((cmp = *wildstr) == w_prefix && wildstr + 1 < wildend &&
98           !str_is_pattern)
99         cmp = wildstr[1];
100       // cmp is the character following w_many.
101       for (;; str++) {
102         /*
103           Skip until we find a character in the expression string that is
104           equal to cmp. For the character not equal to cmp, we consider they are
105           all matched by w_many.
106         */
107         while (str < strend && *str != cmp) str++;
108         if (str == strend) return 1;
109         // Recursively call ourselves until we find a match.
110         if (wild_compare_full(str, strend - str, wildstr, wildend - wildstr,
111                               str_is_pattern, w_prefix, w_one, w_many) == 0)
112           return 0;
113       }
114       /* We will never come here */
115     }
116   }
117   return str < strend;
118 } /* wild_compare */
119 
wild_compare(const char * str,int strlen,const char * wildstr,int wildlen,bool str_is_pattern)120 int wild_compare(const char *str, int strlen, const char *wildstr, int wildlen,
121                  bool str_is_pattern) {
122   return wild_compare_full(str, strlen, wildstr, wildlen, str_is_pattern,
123                            wild_prefix, wild_one, wild_many);
124 }
125