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