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