xref: /illumos-gate/usr/src/lib/libc/port/regex/regexec.c (revision 14247eb6)
1 /*
2  * Copyright 2023 Bill Sommerfeld <sommerfeld@hamachi.org>
3  * Copyright 2010 Nexenta Systems, Inc.  All rights reserved.
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 "lint.h"
44 #include "file64.h"
45 #include <sys/types.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <limits.h>
50 #include <ctype.h>
51 #include <regex.h>
52 #include <wchar.h>
53 #include <wctype.h>
54 #include <assert.h>
55 
56 #include "utils.h"
57 #include "regex2.h"
58 
59 static size_t
xmbrtowc(wint_t * wi,const char * s,size_t n,mbstate_t * mbs,wint_t dummy)60 xmbrtowc(wint_t *wi, const char *s, size_t n, mbstate_t *mbs, wint_t dummy)
61 {
62 	size_t nr;
63 	wchar_t wc;
64 
65 	nr = mbrtowc(&wc, s, n, mbs);
66 	if (wi != NULL)
67 		*wi = wc;
68 	if (nr == 0)
69 		return (1);
70 	else if (nr == (size_t)-1 || nr == (size_t)-2) {
71 		(void) memset(mbs, 0, sizeof (*mbs));
72 		if (wi != NULL)
73 			*wi = dummy;
74 		return (1);
75 	} else
76 		return (nr);
77 }
78 
79 static size_t
xmbrtowc_dummy(wint_t * wi,const char * s,size_t n __unused,mbstate_t * mbs __unused,wint_t dummy __unused)80 xmbrtowc_dummy(wint_t *wi, const char *s, size_t n __unused,
81     mbstate_t *mbs __unused, wint_t dummy __unused)
82 {
83 	if (wi != NULL)
84 		*wi = (unsigned char)*s;
85 	return (1);
86 }
87 
88 /* macros for manipulating states, small version */
89 #define	states	long
90 #define	states1	states		/* for later use in regexec() decision */
91 #define	CLEAR(v)	((v) = 0)
92 #define	SET0(v, n)	((v) &= ~((unsigned long)1 << (n)))
93 #define	SET1(v, n)	((v) |= (unsigned long)1 << (n))
94 #define	ISSET(v, n)	(((v) & ((unsigned long)1 << (n))) != 0)
95 #define	ASSIGN(d, s)	((d) = (s))
96 #define	EQ(a, b)	((a) == (b))
97 #define	STATEVARS	long dummy	/* dummy version */
98 #define	STATESETUP(m, n)	/* nothing */
99 #define	STATETEARDOWN(m)	/* nothing */
100 #define	SETUP(v)	((v) = 0)
101 #define	onestate	long
102 #define	INIT(o, n)	((o) = (unsigned long)1 << (n))
103 #define	INC(o)	((o) <<= 1)
104 #define	ISSTATEIN(v, o)	(((v) & (o)) != 0)
105 /* some abbreviations; note that some of these know variable names! */
106 /* do "if I'm here, I can also be there" etc without branches */
107 #define	FWD(dst, src, n)	((dst) |= ((unsigned long)(src)&(here)) << (n))
108 #define	BACK(dst, src, n)	((dst) |= ((unsigned long)(src)&(here)) >> (n))
109 #define	ISSETBACK(v, n)	(((v) & ((unsigned long)here >> (n))) != 0)
110 /* no multibyte support */
111 #define	XMBRTOWC	xmbrtowc_dummy
112 #define	ZAPSTATE(mbs)	((void)(mbs))
113 #define	NC_ENGINE NC_MAX
114 /* function names */
115 #define	SNAMES			/* engine.c looks after details */
116 
117 #include "engine.c"
118 
119 /* now undo things */
120 #undef	states
121 #undef	CLEAR
122 #undef	SET0
123 #undef	SET1
124 #undef	ISSET
125 #undef	ASSIGN
126 #undef	EQ
127 #undef	STATEVARS
128 #undef	STATESETUP
129 #undef	STATETEARDOWN
130 #undef	SETUP
131 #undef	onestate
132 #undef	INIT
133 #undef	INC
134 #undef	ISSTATEIN
135 #undef	FWD
136 #undef	BACK
137 #undef	ISSETBACK
138 #undef	SNAMES
139 #undef	XMBRTOWC
140 #undef	ZAPSTATE
141 #undef	NC_ENGINE
142 
143 /* macros for manipulating states, large version */
144 #define	states	char *
145 #define	CLEAR(v)	(void) memset(v, 0, m->g->nstates)
146 #define	SET0(v, n)	((v)[n] = 0)
147 #define	SET1(v, n)	((v)[n] = 1)
148 #define	ISSET(v, n)	((v)[n])
149 #define	ASSIGN(d, s)	(void) memcpy(d, s, m->g->nstates)
150 #define	EQ(a, b)	(memcmp(a, b, m->g->nstates) == 0)
151 #define	STATEVARS	long vn; char *space
152 #define	STATESETUP(m, nv) { (m)->space = malloc((nv)*(m)->g->nstates); \
153 	if ((m)->space == NULL) \
154 		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 #define	NC_ENGINE NC_MAX
171 /* function names */
172 #define	LNAMES			/* flag */
173 
174 #include "engine.c"
175 
176 /* multibyte character & large states version */
177 #undef	LNAMES
178 #undef	XMBRTOWC
179 #undef	ZAPSTATE
180 #undef	NC_ENGINE
181 #define	XMBRTOWC	xmbrtowc
182 #define	NC_ENGINE NC_WIDE
183 #define	ZAPSTATE(mbs)	(void) memset((mbs), 0, sizeof (*(mbs)))
184 #define	MNAMES
185 
186 #include "engine.c"
187 
188 /*
189  * regexec - interface for matching
190  *
191  * We put this here so we can exploit knowledge of the state representation
192  * when choosing which matcher to call.  Also, by this point the matchers
193  * have been prototyped.
194  */
195 int				/* 0 success, REG_NOMATCH failure */
regexec(const regex_t * _RESTRICT_KYWD preg,const char * _RESTRICT_KYWD string,size_t nmatch,regmatch_t pmatch[_RESTRICT_KYWD],int eflags)196 regexec(const regex_t *_RESTRICT_KYWD preg, const char *_RESTRICT_KYWD string,
197     size_t nmatch, regmatch_t pmatch[_RESTRICT_KYWD], int eflags)
198 {
199 	struct re_guts *g = preg->re_g;
200 #ifdef REDEBUG
201 #define	GOODFLAGS(f)	(f)
202 #else
203 #ifdef	REG_STARTEND
204 #define	GOODFLAGS(f)	((f)&(REG_NOTBOL|REG_NOTEOL|REG_STARTEND))
205 #else
206 #define	GOODFLAGS(f)	((f)&(REG_NOTBOL|REG_NOTEOL))
207 #endif
208 #endif
209 
210 	if (preg->re_magic != MAGIC1 || g->magic != MAGIC2)
211 		return (REG_BADPAT);
212 	assert(!(g->iflags&BAD));
213 	if (g->iflags&BAD)		/* backstop for no-debug case */
214 		return (REG_BADPAT);
215 	eflags = GOODFLAGS(eflags);
216 
217 	if (g->mb_cur_max > 1)
218 		return (mmatcher(g, string, nmatch, pmatch, eflags));
219 	else if (g->nstates <= CHAR_BIT*sizeof (states1) && !(eflags&REG_LARGE))
220 		return (smatcher(g, string, nmatch, pmatch, eflags));
221 	else
222 		return (lmatcher(g, string, nmatch, pmatch, eflags));
223 }
224