xref: /freebsd/lib/libc/regex/regexec.c (revision 5e3190f700637fcfc1a52daeaa4a031fdd2557c7)
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 #include <sys/cdefs.h>
42 /*
43  * the outer shell of regexec()
44  *
45  * This file includes engine.c three times, after muchos fiddling with the
46  * macros that code uses.  This lets the same code operate on two different
47  * representations for state sets and characters.
48  */
49 #include <sys/types.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <limits.h>
54 #include <ctype.h>
55 #include <regex.h>
56 #include <wchar.h>
57 #include <wctype.h>
58 
59 #include "utils.h"
60 #include "regex2.h"
61 
62 static int nope __unused = 0;	/* for use in asserts; shuts lint up */
63 
64 static __inline size_t
65 xmbrtowc(wint_t *wi, const char *s, size_t n, mbstate_t *mbs, wint_t dummy)
66 {
67 	size_t nr;
68 	wchar_t wc;
69 
70 	nr = mbrtowc(&wc, s, n, mbs);
71 	if (wi != NULL)
72 		*wi = wc;
73 	if (nr == 0)
74 		return (1);
75 	else if (nr == (size_t)-1 || nr == (size_t)-2) {
76 		memset(mbs, 0, sizeof(*mbs));
77 		if (wi != NULL)
78 			*wi = dummy;
79 		return (1);
80 	} else
81                 return (nr);
82 }
83 
84 static __inline size_t
85 xmbrtowc_dummy(wint_t *wi,
86 		const char *s,
87 		size_t n __unused,
88 		mbstate_t *mbs __unused,
89 		wint_t dummy __unused)
90 {
91 
92 	if (wi != NULL)
93 		*wi = (unsigned char)*s;
94 	return (1);
95 }
96 
97 /* macros for manipulating states, small version */
98 #define	states1	long		/* for later use in regexec() decision */
99 #define	states	states1
100 #define	CLEAR(v)	((v) = 0)
101 #define	SET0(v, n)	((v) &= ~((unsigned long)1 << (n)))
102 #define	SET1(v, n)	((v) |= (unsigned long)1 << (n))
103 #define	ISSET(v, n)	(((v) & ((unsigned long)1 << (n))) != 0)
104 #define	ASSIGN(d, s)	((d) = (s))
105 #define	EQ(a, b)	((a) == (b))
106 #define	STATEVARS	long dummy	/* dummy version */
107 #define	STATESETUP(m, n)	/* nothing */
108 #define	STATETEARDOWN(m)	/* nothing */
109 #define	SETUP(v)	((v) = 0)
110 #define	onestate	long
111 #define	INIT(o, n)	((o) = (unsigned long)1 << (n))
112 #define	INC(o)	((o) <<= 1)
113 #define	ISSTATEIN(v, o)	(((v) & (o)) != 0)
114 /* some abbreviations; note that some of these know variable names! */
115 /* do "if I'm here, I can also be there" etc without branches */
116 #define	FWD(dst, src, n)	((dst) |= ((unsigned long)(src)&(here)) << (n))
117 #define	BACK(dst, src, n)	((dst) |= ((unsigned long)(src)&(here)) >> (n))
118 #define	ISSETBACK(v, n)	(((v) & ((unsigned long)here >> (n))) != 0)
119 /* no multibyte support */
120 #define	XMBRTOWC	xmbrtowc_dummy
121 #define	ZAPSTATE(mbs)	((void)(mbs))
122 /* function names */
123 #define SNAMES			/* engine.c looks after details */
124 
125 #include "engine.c"
126 
127 /* now undo things */
128 #undef	states
129 #undef	CLEAR
130 #undef	SET0
131 #undef	SET1
132 #undef	ISSET
133 #undef	ASSIGN
134 #undef	EQ
135 #undef	STATEVARS
136 #undef	STATESETUP
137 #undef	STATETEARDOWN
138 #undef	SETUP
139 #undef	onestate
140 #undef	INIT
141 #undef	INC
142 #undef	ISSTATEIN
143 #undef	FWD
144 #undef	BACK
145 #undef	ISSETBACK
146 #undef	SNAMES
147 #undef	XMBRTOWC
148 #undef	ZAPSTATE
149 
150 /* macros for manipulating states, large version */
151 #define	states	char *
152 #define	CLEAR(v)	memset(v, 0, m->g->nstates)
153 #define	SET0(v, n)	((v)[n] = 0)
154 #define	SET1(v, n)	((v)[n] = 1)
155 #define	ISSET(v, n)	((v)[n])
156 #define	ASSIGN(d, s)	memcpy(d, s, m->g->nstates)
157 #define	EQ(a, b)	(memcmp(a, b, m->g->nstates) == 0)
158 #define	STATEVARS	long vn; char *space
159 #define	STATESETUP(m, nv)	{ (m)->space = malloc((nv)*(m)->g->nstates); \
160 				if ((m)->space == NULL) return(REG_ESPACE); \
161 				(m)->vn = 0; }
162 #define	STATETEARDOWN(m)	{ free((m)->space); }
163 #define	SETUP(v)	((v) = &m->space[m->vn++ * m->g->nstates])
164 #define	onestate	long
165 #define	INIT(o, n)	((o) = (n))
166 #define	INC(o)	((o)++)
167 #define	ISSTATEIN(v, o)	((v)[o])
168 /* some abbreviations; note that some of these know variable names! */
169 /* do "if I'm here, I can also be there" etc without branches */
170 #define	FWD(dst, src, n)	((dst)[here+(n)] |= (src)[here])
171 #define	BACK(dst, src, n)	((dst)[here-(n)] |= (src)[here])
172 #define	ISSETBACK(v, n)	((v)[here - (n)])
173 /* no multibyte support */
174 #define	XMBRTOWC	xmbrtowc_dummy
175 #define	ZAPSTATE(mbs)	((void)(mbs))
176 /* function names */
177 #define	LNAMES			/* flag */
178 
179 #include "engine.c"
180 
181 /* multibyte character & large states version */
182 #undef	LNAMES
183 #undef	XMBRTOWC
184 #undef	ZAPSTATE
185 #define	XMBRTOWC	xmbrtowc
186 #define	ZAPSTATE(mbs)	memset((mbs), 0, sizeof(*(mbs)))
187 #define	MNAMES
188 
189 #include "engine.c"
190 
191 /*
192  - regexec - interface for matching
193  = extern int regexec(const regex_t *, const char *, size_t, \
194  =					regmatch_t [], int);
195  = #define	REG_NOTBOL	00001
196  = #define	REG_NOTEOL	00002
197  = #define	REG_STARTEND	00004
198  = #define	REG_TRACE	00400	// tracing of execution
199  = #define	REG_LARGE	01000	// force large representation
200  = #define	REG_BACKR	02000	// force use of backref code
201  *
202  * We put this here so we can exploit knowledge of the state representation
203  * when choosing which matcher to call.  Also, by this point the matchers
204  * have been prototyped.
205  */
206 int				/* 0 success, REG_NOMATCH failure */
207 regexec(const regex_t * __restrict preg,
208 	const char * __restrict string,
209 	size_t nmatch,
210 	regmatch_t pmatch[__restrict],
211 	int eflags)
212 {
213 	struct re_guts *g = preg->re_g;
214 #ifdef REDEBUG
215 #	define	GOODFLAGS(f)	(f)
216 #else
217 #	define	GOODFLAGS(f)	((f)&(REG_NOTBOL|REG_NOTEOL|REG_STARTEND))
218 #endif
219 
220 	if (preg->re_magic != MAGIC1 || g->magic != MAGIC2)
221 		return(REG_BADPAT);
222 	assert(!(g->iflags&BAD));
223 	if (g->iflags&BAD)		/* backstop for no-debug case */
224 		return(REG_BADPAT);
225 	eflags = GOODFLAGS(eflags);
226 
227 	if (MB_CUR_MAX > 1)
228 		return(mmatcher(g, string, nmatch, pmatch, eflags));
229 	else if (g->nstates <= CHAR_BIT*sizeof(states1) && !(eflags&REG_LARGE))
230 		return(smatcher(g, string, nmatch, pmatch, eflags));
231 	else
232 		return(lmatcher(g, string, nmatch, pmatch, eflags));
233 }
234