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