xref: /aosp_15_r20/external/llvm/lib/Support/regexec.c (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker /*-
2*9880d681SAndroid Build Coastguard Worker  * This code is derived from OpenBSD's libc/regex, original license follows:
3*9880d681SAndroid Build Coastguard Worker  *
4*9880d681SAndroid Build Coastguard Worker  * Copyright (c) 1992, 1993, 1994 Henry Spencer.
5*9880d681SAndroid Build Coastguard Worker  * Copyright (c) 1992, 1993, 1994
6*9880d681SAndroid Build Coastguard Worker  *	The Regents of the University of California.  All rights reserved.
7*9880d681SAndroid Build Coastguard Worker  *
8*9880d681SAndroid Build Coastguard Worker  * This code is derived from software contributed to Berkeley by
9*9880d681SAndroid Build Coastguard Worker  * Henry Spencer.
10*9880d681SAndroid Build Coastguard Worker  *
11*9880d681SAndroid Build Coastguard Worker  * Redistribution and use in source and binary forms, with or without
12*9880d681SAndroid Build Coastguard Worker  * modification, are permitted provided that the following conditions
13*9880d681SAndroid Build Coastguard Worker  * are met:
14*9880d681SAndroid Build Coastguard Worker  * 1. Redistributions of source code must retain the above copyright
15*9880d681SAndroid Build Coastguard Worker  *    notice, this list of conditions and the following disclaimer.
16*9880d681SAndroid Build Coastguard Worker  * 2. Redistributions in binary form must reproduce the above copyright
17*9880d681SAndroid Build Coastguard Worker  *    notice, this list of conditions and the following disclaimer in the
18*9880d681SAndroid Build Coastguard Worker  *    documentation and/or other materials provided with the distribution.
19*9880d681SAndroid Build Coastguard Worker  * 3. Neither the name of the University nor the names of its contributors
20*9880d681SAndroid Build Coastguard Worker  *    may be used to endorse or promote products derived from this software
21*9880d681SAndroid Build Coastguard Worker  *    without specific prior written permission.
22*9880d681SAndroid Build Coastguard Worker  *
23*9880d681SAndroid Build Coastguard Worker  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24*9880d681SAndroid Build Coastguard Worker  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25*9880d681SAndroid Build Coastguard Worker  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26*9880d681SAndroid Build Coastguard Worker  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27*9880d681SAndroid Build Coastguard Worker  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28*9880d681SAndroid Build Coastguard Worker  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29*9880d681SAndroid Build Coastguard Worker  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30*9880d681SAndroid Build Coastguard Worker  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31*9880d681SAndroid Build Coastguard Worker  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32*9880d681SAndroid Build Coastguard Worker  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33*9880d681SAndroid Build Coastguard Worker  * SUCH DAMAGE.
34*9880d681SAndroid Build Coastguard Worker  *
35*9880d681SAndroid Build Coastguard Worker  *	@(#)regexec.c	8.3 (Berkeley) 3/20/94
36*9880d681SAndroid Build Coastguard Worker  */
37*9880d681SAndroid Build Coastguard Worker 
38*9880d681SAndroid Build Coastguard Worker /*
39*9880d681SAndroid Build Coastguard Worker  * the outer shell of llvm_regexec()
40*9880d681SAndroid Build Coastguard Worker  *
41*9880d681SAndroid Build Coastguard Worker  * This file includes engine.inc *twice*, after muchos fiddling with the
42*9880d681SAndroid Build Coastguard Worker  * macros that code uses.  This lets the same code operate on two different
43*9880d681SAndroid Build Coastguard Worker  * representations for state sets.
44*9880d681SAndroid Build Coastguard Worker  */
45*9880d681SAndroid Build Coastguard Worker #include <sys/types.h>
46*9880d681SAndroid Build Coastguard Worker #include <stdio.h>
47*9880d681SAndroid Build Coastguard Worker #include <stdlib.h>
48*9880d681SAndroid Build Coastguard Worker #include <string.h>
49*9880d681SAndroid Build Coastguard Worker #include <limits.h>
50*9880d681SAndroid Build Coastguard Worker #include <ctype.h>
51*9880d681SAndroid Build Coastguard Worker #include "regex_impl.h"
52*9880d681SAndroid Build Coastguard Worker 
53*9880d681SAndroid Build Coastguard Worker #include "regutils.h"
54*9880d681SAndroid Build Coastguard Worker #include "regex2.h"
55*9880d681SAndroid Build Coastguard Worker 
56*9880d681SAndroid Build Coastguard Worker /* macros for manipulating states, small version */
57*9880d681SAndroid Build Coastguard Worker /* FIXME: 'states' is assumed as 'long' on small version. */
58*9880d681SAndroid Build Coastguard Worker #define	states1	long		/* for later use in llvm_regexec() decision */
59*9880d681SAndroid Build Coastguard Worker #define	states	states1
60*9880d681SAndroid Build Coastguard Worker #define	CLEAR(v)	((v) = 0)
61*9880d681SAndroid Build Coastguard Worker #define	SET0(v, n)	((v) &= ~((unsigned long)1 << (n)))
62*9880d681SAndroid Build Coastguard Worker #define	SET1(v, n)	((v) |= (unsigned long)1 << (n))
63*9880d681SAndroid Build Coastguard Worker #define	ISSET(v, n)	(((v) & ((unsigned long)1 << (n))) != 0)
64*9880d681SAndroid Build Coastguard Worker #define	ASSIGN(d, s)	((d) = (s))
65*9880d681SAndroid Build Coastguard Worker #define	EQ(a, b)	((a) == (b))
66*9880d681SAndroid Build Coastguard Worker #define	STATEVARS	long dummy	/* dummy version */
67*9880d681SAndroid Build Coastguard Worker #define	STATESETUP(m, n)	/* nothing */
68*9880d681SAndroid Build Coastguard Worker #define	STATETEARDOWN(m)	/* nothing */
69*9880d681SAndroid Build Coastguard Worker #define	SETUP(v)	((v) = 0)
70*9880d681SAndroid Build Coastguard Worker #define	onestate	long
71*9880d681SAndroid Build Coastguard Worker #define	INIT(o, n)	((o) = (unsigned long)1 << (n))
72*9880d681SAndroid Build Coastguard Worker #define	INC(o)		((o) = (unsigned long)(o) << 1)
73*9880d681SAndroid Build Coastguard Worker #define	ISSTATEIN(v, o)	(((v) & (o)) != 0)
74*9880d681SAndroid Build Coastguard Worker /* some abbreviations; note that some of these know variable names! */
75*9880d681SAndroid Build Coastguard Worker /* do "if I'm here, I can also be there" etc without branches */
76*9880d681SAndroid Build Coastguard Worker #define	FWD(dst, src, n)	((dst) |= ((unsigned long)(src)&(here)) << (n))
77*9880d681SAndroid Build Coastguard Worker #define	BACK(dst, src, n)	((dst) |= ((unsigned long)(src)&(here)) >> (n))
78*9880d681SAndroid Build Coastguard Worker #define	ISSETBACK(v, n)		(((v) & ((unsigned long)here >> (n))) != 0)
79*9880d681SAndroid Build Coastguard Worker /* function names */
80*9880d681SAndroid Build Coastguard Worker #define SNAMES			/* engine.inc looks after details */
81*9880d681SAndroid Build Coastguard Worker 
82*9880d681SAndroid Build Coastguard Worker #include "regengine.inc"
83*9880d681SAndroid Build Coastguard Worker 
84*9880d681SAndroid Build Coastguard Worker /* now undo things */
85*9880d681SAndroid Build Coastguard Worker #undef	states
86*9880d681SAndroid Build Coastguard Worker #undef	CLEAR
87*9880d681SAndroid Build Coastguard Worker #undef	SET0
88*9880d681SAndroid Build Coastguard Worker #undef	SET1
89*9880d681SAndroid Build Coastguard Worker #undef	ISSET
90*9880d681SAndroid Build Coastguard Worker #undef	ASSIGN
91*9880d681SAndroid Build Coastguard Worker #undef	EQ
92*9880d681SAndroid Build Coastguard Worker #undef	STATEVARS
93*9880d681SAndroid Build Coastguard Worker #undef	STATESETUP
94*9880d681SAndroid Build Coastguard Worker #undef	STATETEARDOWN
95*9880d681SAndroid Build Coastguard Worker #undef	SETUP
96*9880d681SAndroid Build Coastguard Worker #undef	onestate
97*9880d681SAndroid Build Coastguard Worker #undef	INIT
98*9880d681SAndroid Build Coastguard Worker #undef	INC
99*9880d681SAndroid Build Coastguard Worker #undef	ISSTATEIN
100*9880d681SAndroid Build Coastguard Worker #undef	FWD
101*9880d681SAndroid Build Coastguard Worker #undef	BACK
102*9880d681SAndroid Build Coastguard Worker #undef	ISSETBACK
103*9880d681SAndroid Build Coastguard Worker #undef	SNAMES
104*9880d681SAndroid Build Coastguard Worker 
105*9880d681SAndroid Build Coastguard Worker /* macros for manipulating states, large version */
106*9880d681SAndroid Build Coastguard Worker #define	states	char *
107*9880d681SAndroid Build Coastguard Worker #define	CLEAR(v)	memset(v, 0, m->g->nstates)
108*9880d681SAndroid Build Coastguard Worker #define	SET0(v, n)	((v)[n] = 0)
109*9880d681SAndroid Build Coastguard Worker #define	SET1(v, n)	((v)[n] = 1)
110*9880d681SAndroid Build Coastguard Worker #define	ISSET(v, n)	((v)[n])
111*9880d681SAndroid Build Coastguard Worker #define	ASSIGN(d, s)	memmove(d, s, m->g->nstates)
112*9880d681SAndroid Build Coastguard Worker #define	EQ(a, b)	(memcmp(a, b, m->g->nstates) == 0)
113*9880d681SAndroid Build Coastguard Worker #define	STATEVARS	long vn; char *space
114*9880d681SAndroid Build Coastguard Worker #define	STATESETUP(m, nv)	{ (m)->space = malloc((nv)*(m)->g->nstates); \
115*9880d681SAndroid Build Coastguard Worker 				if ((m)->space == NULL) return(REG_ESPACE); \
116*9880d681SAndroid Build Coastguard Worker 				(m)->vn = 0; }
117*9880d681SAndroid Build Coastguard Worker #define	STATETEARDOWN(m)	{ free((m)->space); }
118*9880d681SAndroid Build Coastguard Worker #define	SETUP(v)	((v) = &m->space[m->vn++ * m->g->nstates])
119*9880d681SAndroid Build Coastguard Worker #define	onestate	long
120*9880d681SAndroid Build Coastguard Worker #define	INIT(o, n)	((o) = (n))
121*9880d681SAndroid Build Coastguard Worker #define	INC(o)	((o)++)
122*9880d681SAndroid Build Coastguard Worker #define	ISSTATEIN(v, o)	((v)[o])
123*9880d681SAndroid Build Coastguard Worker /* some abbreviations; note that some of these know variable names! */
124*9880d681SAndroid Build Coastguard Worker /* do "if I'm here, I can also be there" etc without branches */
125*9880d681SAndroid Build Coastguard Worker #define	FWD(dst, src, n)	((dst)[here+(n)] |= (src)[here])
126*9880d681SAndroid Build Coastguard Worker #define	BACK(dst, src, n)	((dst)[here-(n)] |= (src)[here])
127*9880d681SAndroid Build Coastguard Worker #define	ISSETBACK(v, n)	((v)[here - (n)])
128*9880d681SAndroid Build Coastguard Worker /* function names */
129*9880d681SAndroid Build Coastguard Worker #define	LNAMES			/* flag */
130*9880d681SAndroid Build Coastguard Worker 
131*9880d681SAndroid Build Coastguard Worker #include "regengine.inc"
132*9880d681SAndroid Build Coastguard Worker 
133*9880d681SAndroid Build Coastguard Worker /*
134*9880d681SAndroid Build Coastguard Worker  - llvm_regexec - interface for matching
135*9880d681SAndroid Build Coastguard Worker  *
136*9880d681SAndroid Build Coastguard Worker  * We put this here so we can exploit knowledge of the state representation
137*9880d681SAndroid Build Coastguard Worker  * when choosing which matcher to call.  Also, by this point the matchers
138*9880d681SAndroid Build Coastguard Worker  * have been prototyped.
139*9880d681SAndroid Build Coastguard Worker  */
140*9880d681SAndroid Build Coastguard Worker int				/* 0 success, REG_NOMATCH failure */
llvm_regexec(const llvm_regex_t * preg,const char * string,size_t nmatch,llvm_regmatch_t pmatch[],int eflags)141*9880d681SAndroid Build Coastguard Worker llvm_regexec(const llvm_regex_t *preg, const char *string, size_t nmatch,
142*9880d681SAndroid Build Coastguard Worker              llvm_regmatch_t pmatch[], int eflags)
143*9880d681SAndroid Build Coastguard Worker {
144*9880d681SAndroid Build Coastguard Worker 	struct re_guts *g = preg->re_g;
145*9880d681SAndroid Build Coastguard Worker #ifdef REDEBUG
146*9880d681SAndroid Build Coastguard Worker #	define	GOODFLAGS(f)	(f)
147*9880d681SAndroid Build Coastguard Worker #else
148*9880d681SAndroid Build Coastguard Worker #	define	GOODFLAGS(f)	((f)&(REG_NOTBOL|REG_NOTEOL|REG_STARTEND))
149*9880d681SAndroid Build Coastguard Worker #endif
150*9880d681SAndroid Build Coastguard Worker 
151*9880d681SAndroid Build Coastguard Worker 	if (preg->re_magic != MAGIC1 || g->magic != MAGIC2)
152*9880d681SAndroid Build Coastguard Worker 		return(REG_BADPAT);
153*9880d681SAndroid Build Coastguard Worker 	assert(!(g->iflags&REGEX_BAD));
154*9880d681SAndroid Build Coastguard Worker 	if (g->iflags&REGEX_BAD)		/* backstop for no-debug case */
155*9880d681SAndroid Build Coastguard Worker 		return(REG_BADPAT);
156*9880d681SAndroid Build Coastguard Worker 	eflags = GOODFLAGS(eflags);
157*9880d681SAndroid Build Coastguard Worker 
158*9880d681SAndroid Build Coastguard Worker 	if (g->nstates <= (long)(CHAR_BIT*sizeof(states1)) && !(eflags&REG_LARGE))
159*9880d681SAndroid Build Coastguard Worker 		return(smatcher(g, string, nmatch, pmatch, eflags));
160*9880d681SAndroid Build Coastguard Worker 	else
161*9880d681SAndroid Build Coastguard Worker 		return(lmatcher(g, string, nmatch, pmatch, eflags));
162*9880d681SAndroid Build Coastguard Worker }
163