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