1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 1992, 1993, 1994 Henry Spencer.
5 * Copyright (c) 1992, 1993, 1994
6 * The Regents of the University of California. All rights reserved.
7 *
8 * This code is derived from software contributed to Berkeley by
9 * Henry Spencer.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 * @(#)regexec.c 8.3 (Berkeley) 3/20/94
36 */
37
38 #if defined(LIBC_SCCS) && !defined(lint)
39 static char sccsid[] = "@(#)regexec.c 8.3 (Berkeley) 3/20/94";
40 #endif /* LIBC_SCCS and not lint */
41 /*
42 * the outer shell of regexec()
43 *
44 * This file includes engine.c three times, after muchos fiddling with the
45 * macros that code uses. This lets the same code operate on two different
46 * representations for state sets and characters.
47 */
48 #include <sys/types.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include <limits.h>
53 #include <ctype.h>
54 #include <regex.h>
55 #include <wchar.h>
56 #include <wctype.h>
57
58 #include "utils.h"
59 #include "regex2.h"
60
61 static int nope __unused = 0; /* for use in asserts; shuts lint up */
62
63 static __inline size_t
xmbrtowc(wint_t * wi,const char * s,size_t n,mbstate_t * mbs,wint_t dummy)64 xmbrtowc(wint_t *wi, const char *s, size_t n, mbstate_t *mbs, wint_t dummy)
65 {
66 size_t nr;
67 wchar_t wc;
68
69 nr = mbrtowc(&wc, s, n, mbs);
70 if (wi != NULL)
71 *wi = wc;
72 if (nr == 0)
73 return (1);
74 else if (nr == (size_t)-1 || nr == (size_t)-2) {
75 memset(mbs, 0, sizeof(*mbs));
76 if (wi != NULL)
77 *wi = dummy;
78 return (1);
79 } else
80 return (nr);
81 }
82
83 static __inline size_t
xmbrtowc_dummy(wint_t * wi,const char * s,size_t n __unused,mbstate_t * mbs __unused,wint_t dummy __unused)84 xmbrtowc_dummy(wint_t *wi,
85 const char *s,
86 size_t n __unused,
87 mbstate_t *mbs __unused,
88 wint_t dummy __unused)
89 {
90
91 if (wi != NULL)
92 *wi = (unsigned char)*s;
93 return (1);
94 }
95
96 /* macros for manipulating states, small version */
97 #define states1 long /* for later use in regexec() decision */
98 #define states states1
99 #define CLEAR(v) ((v) = 0)
100 #define SET0(v, n) ((v) &= ~((unsigned long)1 << (n)))
101 #define SET1(v, n) ((v) |= (unsigned long)1 << (n))
102 #define ISSET(v, n) (((v) & ((unsigned long)1 << (n))) != 0)
103 #define ASSIGN(d, s) ((d) = (s))
104 #define EQ(a, b) ((a) == (b))
105 #define STATEVARS long dummy /* dummy version */
106 #define STATESETUP(m, n) /* nothing */
107 #define STATETEARDOWN(m) /* nothing */
108 #define SETUP(v) ((v) = 0)
109 #define onestate long
110 #define INIT(o, n) ((o) = (unsigned long)1 << (n))
111 #define INC(o) ((o) <<= 1)
112 #define ISSTATEIN(v, o) (((v) & (o)) != 0)
113 /* some abbreviations; note that some of these know variable names! */
114 /* do "if I'm here, I can also be there" etc without branches */
115 #define FWD(dst, src, n) ((dst) |= ((unsigned long)(src)&(here)) << (n))
116 #define BACK(dst, src, n) ((dst) |= ((unsigned long)(src)&(here)) >> (n))
117 #define ISSETBACK(v, n) (((v) & ((unsigned long)here >> (n))) != 0)
118 /* no multibyte support */
119 #define XMBRTOWC xmbrtowc_dummy
120 #define ZAPSTATE(mbs) ((void)(mbs))
121 /* function names */
122 #define SNAMES /* engine.c looks after details */
123
124 #include "engine.c"
125
126 /* now undo things */
127 #undef states
128 #undef CLEAR
129 #undef SET0
130 #undef SET1
131 #undef ISSET
132 #undef ASSIGN
133 #undef EQ
134 #undef STATEVARS
135 #undef STATESETUP
136 #undef STATETEARDOWN
137 #undef SETUP
138 #undef onestate
139 #undef INIT
140 #undef INC
141 #undef ISSTATEIN
142 #undef FWD
143 #undef BACK
144 #undef ISSETBACK
145 #undef SNAMES
146 #undef XMBRTOWC
147 #undef ZAPSTATE
148
149 /* macros for manipulating states, large version */
150 #define states char *
151 #define CLEAR(v) memset(v, 0, m->g->nstates)
152 #define SET0(v, n) ((v)[n] = 0)
153 #define SET1(v, n) ((v)[n] = 1)
154 #define ISSET(v, n) ((v)[n])
155 #define ASSIGN(d, s) memcpy(d, s, m->g->nstates)
156 #define EQ(a, b) (memcmp(a, b, m->g->nstates) == 0)
157 #define STATEVARS long vn; char *space
158 #define STATESETUP(m, nv) { (m)->space = malloc((nv)*(m)->g->nstates); \
159 if ((m)->space == NULL) return(REG_ESPACE); \
160 (m)->vn = 0; }
161 #define STATETEARDOWN(m) { free((m)->space); }
162 #define SETUP(v) ((v) = &m->space[m->vn++ * m->g->nstates])
163 #define onestate long
164 #define INIT(o, n) ((o) = (n))
165 #define INC(o) ((o)++)
166 #define ISSTATEIN(v, o) ((v)[o])
167 /* some abbreviations; note that some of these know variable names! */
168 /* do "if I'm here, I can also be there" etc without branches */
169 #define FWD(dst, src, n) ((dst)[here+(n)] |= (src)[here])
170 #define BACK(dst, src, n) ((dst)[here-(n)] |= (src)[here])
171 #define ISSETBACK(v, n) ((v)[here - (n)])
172 /* no multibyte support */
173 #define XMBRTOWC xmbrtowc_dummy
174 #define ZAPSTATE(mbs) ((void)(mbs))
175 /* function names */
176 #define LNAMES /* flag */
177
178 #include "engine.c"
179
180 /* multibyte character & large states version */
181 #undef LNAMES
182 #undef XMBRTOWC
183 #undef ZAPSTATE
184 #define XMBRTOWC xmbrtowc
185 #define ZAPSTATE(mbs) memset((mbs), 0, sizeof(*(mbs)))
186 #define MNAMES
187
188 #include "engine.c"
189
190 /*
191 - regexec - interface for matching
192 = extern int regexec(const regex_t *, const char *, size_t, \
193 = regmatch_t [], int);
194 = #define REG_NOTBOL 00001
195 = #define REG_NOTEOL 00002
196 = #define REG_STARTEND 00004
197 = #define REG_TRACE 00400 // tracing of execution
198 = #define REG_LARGE 01000 // force large representation
199 = #define REG_BACKR 02000 // force use of backref code
200 *
201 * We put this here so we can exploit knowledge of the state representation
202 * when choosing which matcher to call. Also, by this point the matchers
203 * have been prototyped.
204 */
205 int /* 0 success, REG_NOMATCH failure */
regexec(const regex_t * __restrict preg,const char * __restrict string,size_t nmatch,regmatch_t pmatch[__restrict],int eflags)206 regexec(const regex_t * __restrict preg,
207 const char * __restrict string,
208 size_t nmatch,
209 regmatch_t pmatch[__restrict],
210 int eflags)
211 {
212 struct re_guts *g = preg->re_g;
213 #ifdef REDEBUG
214 # define GOODFLAGS(f) (f)
215 #else
216 # define GOODFLAGS(f) ((f)&(REG_NOTBOL|REG_NOTEOL|REG_STARTEND))
217 #endif
218
219 if (preg->re_magic != MAGIC1 || g->magic != MAGIC2)
220 return(REG_BADPAT);
221 assert(!(g->iflags&BAD));
222 if (g->iflags&BAD) /* backstop for no-debug case */
223 return(REG_BADPAT);
224 eflags = GOODFLAGS(eflags);
225
226 if (MB_CUR_MAX > 1)
227 return(mmatcher(g, string, nmatch, pmatch, eflags));
228 else if (g->nstates <= CHAR_BIT*sizeof(states1) && !(eflags®_LARGE))
229 return(smatcher(g, string, nmatch, pmatch, eflags));
230 else
231 return(lmatcher(g, string, nmatch, pmatch, eflags));
232 }
233