xref: /illumos-gate/usr/src/lib/libc/port/regex/regexec.c (revision 4297a3b0)
1 /*
2  * Copyright (c) 1992, 1993, 1994 Henry Spencer.
3  * Copyright (c) 1992, 1993, 1994
4  *	The Regents of the University of California.  All rights reserved.
5  *
6  * This code is derived from software contributed to Berkeley by
7  * Henry Spencer.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 /*
35  * Copyright 2010 Nexenta Systems, Inc.  All rights reserved.
36  * Use is subject to license terms.
37  */
38 
39 /*
40  * the outer shell of regexec()
41  *
42  * This file includes engine.c three times, after muchos fiddling with the
43  * macros that code uses.  This lets the same code operate on two different
44  * representations for state sets and characters.
45  */
46 #include "lint.h"
47 #include "file64.h"
48 #include <sys/types.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include <limits.h>
53 #include <ctype.h>
54 #include <regex.h>
55 #include <wchar.h>
56 #include <wctype.h>
57 #include <note.h>
58 #include <assert.h>
59 
60 #include "utils.h"
61 #include "regex2.h"
62 
63 /* we want _NOTE, but not NOTE (which collides with our own use) */
64 #undef	NOTE
65 
66 static 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 		(void) memset(mbs, 0, sizeof (*mbs));
79 		if (wi != NULL)
80 			*wi = dummy;
81 		return (1);
82 	} else
83 		return (nr);
84 }
85 
86 static size_t
87 xmbrtowc_dummy(wint_t *wi, const char *s, size_t n, mbstate_t *mbs,
88     wint_t dummy)
89 {
90 	_NOTE(ARGUNUSED(n));
91 	_NOTE(ARGUNUSED(mbs));
92 	_NOTE(ARGUNUSED(dummy));
93 
94 	if (wi != NULL)
95 		*wi = (unsigned char)*s;
96 	return (1);
97 }
98 
99 /* macros for manipulating states, small version */
100 #define	states	long
101 #define	states1	states		/* for later use in regexec() decision */
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)	(void) 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)	(void) 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) \
163 		return (REG_ESPACE); \
164 	(m)->vn = 0; }
165 #define	STATETEARDOWN(m)	{ free((m)->space); }
166 #define	SETUP(v)	((v) = &m->space[m->vn++ * m->g->nstates])
167 #define	onestate	long
168 #define	INIT(o, n)	((o) = (n))
169 #define	INC(o)	((o)++)
170 #define	ISSTATEIN(v, o)	((v)[o])
171 /* some abbreviations; note that some of these know variable names! */
172 /* do "if I'm here, I can also be there" etc without branches */
173 #define	FWD(dst, src, n)	((dst)[here+(n)] |= (src)[here])
174 #define	BACK(dst, src, n)	((dst)[here-(n)] |= (src)[here])
175 #define	ISSETBACK(v, n)	((v)[here - (n)])
176 /* no multibyte support */
177 #define	XMBRTOWC	xmbrtowc_dummy
178 #define	ZAPSTATE(mbs)	((void)(mbs))
179 /* function names */
180 #define	LNAMES			/* flag */
181 
182 #include "engine.c"
183 
184 /* multibyte character & large states version */
185 #undef	LNAMES
186 #undef	XMBRTOWC
187 #undef	ZAPSTATE
188 #define	XMBRTOWC	xmbrtowc
189 #define	ZAPSTATE(mbs)	(void) memset((mbs), 0, sizeof (*(mbs)))
190 #define	MNAMES
191 
192 #include "engine.c"
193 
194 /*
195  * regexec - interface for matching
196  *
197  * We put this here so we can exploit knowledge of the state representation
198  * when choosing which matcher to call.  Also, by this point the matchers
199  * have been prototyped.
200  */
201 int				/* 0 success, REG_NOMATCH failure */
202 regexec(const regex_t *_RESTRICT_KYWD preg,
203     const char *_RESTRICT_KYWD string, size_t nmatch,
204 	regmatch_t pmatch[_RESTRICT_KYWD], int eflags)
205 {
206 	struct re_guts *g = preg->re_g;
207 #ifdef REDEBUG
208 #define	GOODFLAGS(f)	(f)
209 #else
210 #ifdef	REG_STARTEND
211 #define	GOODFLAGS(f)	((f)&(REG_NOTBOL|REG_NOTEOL|REG_STARTEND))
212 #else
213 #define	GOODFLAGS(f)	((f)&(REG_NOTBOL|REG_NOTEOL))
214 #endif
215 #endif
216 
217 	if (preg->re_magic != MAGIC1 || g->magic != MAGIC2)
218 		return (REG_BADPAT);
219 	assert(!(g->iflags&BAD));
220 	if (g->iflags&BAD)		/* backstop for no-debug case */
221 		return (REG_BADPAT);
222 	eflags = GOODFLAGS(eflags);
223 
224 	if (MB_CUR_MAX > 1)
225 		return (mmatcher(g, (char *)string, nmatch, pmatch, eflags));
226 #ifdef	REG_LARGE
227 	else if (g->nstates <= CHAR_BIT*sizeof (states1) && !(eflags&REG_LARGE))
228 #else
229 	else if (g->nstates <= CHAR_BIT*sizeof (states1))
230 #endif
231 		return (smatcher(g, (char *)string, nmatch, pmatch, eflags));
232 	else
233 		return (lmatcher(g, (char *)string, nmatch, pmatch, eflags));
234 }
235