xref: /illumos-gate/usr/src/lib/libc/port/regex/regex2.h (revision 14247eb6)
1 /*
2  * Copyright 2023 Bill Sommerfeld <sommerfeld@hamachi.org>
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  * First, the stuff that ends up in the outside-world include file
37  * typedef off_t regoff_t;
38  * typedef struct {
39  *	int re_magic;
40  *	size_t re_nsub;		// number of parenthesized subexpressions
41  *	const char *re_endp;	// end pointer for REG_PEND
42  *	struct re_guts *re_g;	// none of your business :-)
43  * } regex_t;
44  * typedef struct {
45  *	regoff_t rm_so;		// start of match
46  *	regoff_t rm_eo;		// end of match
47  * } regmatch_t;
48  */
49 /*
50  * internals of regex_t
51  */
52 #define	MAGIC1	((('r'^0200)<<8) | 'e')
53 
54 /*
55  * The internal representation is a *strip*, a sequence of
56  * operators ending with an endmarker.  (Some terminology etc. is a
57  * historical relic of earlier versions which used multiple strips.)
58  * Certain oddities in the representation are there to permit running
59  * the machinery backwards; in particular, any deviation from sequential
60  * flow must be marked at both its source and its destination.  Some
61  * fine points:
62  *
63  * - OPLUS_ and O_PLUS are *inside* the loop they create.
64  * - OQUEST_ and O_QUEST are *outside* the bypass they create.
65  * - OCH_ and O_CH are *outside* the multi-way branch they create, while
66  *   OOR1 and OOR2 are respectively the end and the beginning of one of
67  *   the branches.  Note that there is an implicit OOR2 following OCH_
68  *   and an implicit OOR1 preceding O_CH.
69  *
70  * In state representations, an operator's bit is on to signify a state
71  * immediately *preceding* "execution" of that operator.
72  */
73 typedef unsigned int sop;	/* strip operator */
74 typedef unsigned int sopno;
75 #define	OPRMASK	0xf8000000U
76 #define	OPDMASK	0x07ffffffU
77 #define	OPSHIFT	((unsigned)27)
78 #define	OP(n)	((n)&OPRMASK)
79 #define	OPND(n)	((n)&OPDMASK)
80 #define	SOP(op, opnd)	((op)|(opnd))
81 /* operators			   meaning	operand			*/
82 /*						(back, fwd are offsets)	*/
83 #define	OEND	(1U<<OPSHIFT)	/* endmarker	-			*/
84 #define	OCHAR	(2U<<OPSHIFT)	/* character	wide character		*/
85 #define	OBOL	(3U<<OPSHIFT)	/* left anchor	-			*/
86 #define	OEOL	(4U<<OPSHIFT)	/* right anchor	-			*/
87 #define	OANY	(5U<<OPSHIFT)	/* .		-			*/
88 #define	OANYOF	(6U<<OPSHIFT)	/* [...]	set number		*/
89 #define	OBACK_	(7U<<OPSHIFT)	/* begin \d	paren number		*/
90 #define	O_BACK	(8U<<OPSHIFT)	/* end \d	paren number		*/
91 #define	OPLUS_	(9U<<OPSHIFT)	/* + prefix	fwd to suffix		*/
92 #define	O_PLUS	(10U<<OPSHIFT)	/* + suffix	back to prefix		*/
93 #define	OQUEST_	(11U<<OPSHIFT)	/* ? prefix	fwd to suffix		*/
94 #define	O_QUEST	(12U<<OPSHIFT)	/* ? suffix	back to prefix		*/
95 #define	OLPAREN	(13U<<OPSHIFT)	/* (		fwd to )		*/
96 #define	ORPAREN	(14U<<OPSHIFT)	/* )		back to (		*/
97 #define	OCH_	(15U<<OPSHIFT)	/* begin choice	fwd to OOR2		*/
98 #define	OOR1	(16U<<OPSHIFT)	/* | pt. 1	back to OOR1 or OCH_	*/
99 #define	OOR2	(17U<<OPSHIFT)	/* | pt. 2	fwd to OOR2 or O_CH	*/
100 #define	O_CH	(18U<<OPSHIFT)	/* end choice	back to OOR1		*/
101 #define	OBOW	(19U<<OPSHIFT)	/* begin word	-			*/
102 #define	OEOW	(20U<<OPSHIFT)	/* end word	-			*/
103 
104 /*
105  * Structures for [] character-set representation.
106  */
107 typedef struct {
108 	wint_t		min;
109 	wint_t		max;
110 } crange;
111 typedef struct {
112 	unsigned char	bmp[NC_MAX / 8];
113 	wctype_t	*types;
114 	unsigned int	ntypes;
115 	wint_t		*wides;
116 	unsigned int	nwides;
117 	crange		*ranges;
118 	unsigned int	nranges;
119 	int		invert;
120 	int		icase;
121 } cset;
122 
123 static inline int
CHIN1(int nc,cset * cs,wint_t ch)124 CHIN1(int nc, cset *cs, wint_t ch)
125 {
126 	unsigned int i;
127 
128 	assert(ch >= 0);
129 	if (ch < nc)
130 		return (((cs->bmp[ch >> 3] & (1 << (ch & 7))) != 0) ^
131 		    cs->invert);
132 	for (i = 0; i < cs->nwides; i++) {
133 		if (cs->icase) {
134 			if (ch == towlower(cs->wides[i]) ||
135 			    ch == towupper(cs->wides[i]))
136 				return (!cs->invert);
137 		} else if (ch == cs->wides[i])
138 			return (!cs->invert);
139 	}
140 	for (i = 0; i < cs->nranges; i++)
141 		if (cs->ranges[i].min <= ch && ch <= cs->ranges[i].max)
142 			return (!cs->invert);
143 	for (i = 0; i < cs->ntypes; i++)
144 		if (iswctype(ch, cs->types[i]))
145 			return (!cs->invert);
146 	return (cs->invert);
147 }
148 
149 static inline int
CHIN(int nc,cset * cs,wint_t ch)150 CHIN(int nc, cset *cs, wint_t ch)
151 {
152 
153 	assert(ch >= 0);
154 	if (ch < nc)
155 		return (((cs->bmp[ch >> 3] & (1 << (ch & 7))) != 0) ^
156 		    cs->invert);
157 	else if (cs->icase)
158 		return (CHIN1(nc, cs, ch) || CHIN1(nc, cs, towlower(ch)) ||
159 		    CHIN1(nc, cs, towupper(ch)));
160 	else
161 		return (CHIN1(nc, cs, ch));
162 }
163 
164 /*
165  * main compiled-expression structure
166  */
167 struct re_guts {
168 	int magic;
169 #define	MAGIC2	((('R'^0200)<<8)|'E')
170 	sop *strip;		/* malloced area for strip */
171 	unsigned int ncsets;	/* number of csets in use */
172 	cset *sets;		/* -> cset [ncsets] */
173 	int cflags;		/* copy of regcomp() cflags argument */
174 	sopno nstates;		/* = number of sops */
175 	sopno firststate;	/* the initial OEND (normally 0) */
176 	sopno laststate;	/* the final OEND */
177 	int iflags;		/* internal flags */
178 #define	USEBOL	01	/* used ^ */
179 #define	USEEOL	02	/* used $ */
180 #define	BAD	04	/* something wrong */
181 	int nbol;		/* number of ^ used */
182 	int neol;		/* number of $ used */
183 	char *must;		/* match must contain this string */
184 	int moffset;		/* latest point at which must may be located */
185 	int *charjump;		/* Boyer-Moore char jump table */
186 	int *matchjump;		/* Boyer-Moore match jump table */
187 	int mlen;		/* length of must */
188 	size_t nsub;		/* copy of re_nsub */
189 	int backrefs;		/* does it use back references? */
190 	sopno nplus;		/* how deep does it nest +s? */
191 	unsigned int mb_cur_max;
192 };
193 
194 /* misc utilities */
195 #define	OUT	(CHAR_MIN - 1)	/* a non-character value */
196 #define	IGN	(CHAR_MIN - 2)
197 #define	ISWORD(c)	(iswalnum((uch)(c)) || (c) == '_')
198