blob: 213a90b3bb8c48f9d99171ec90dffa776beb73a0 [file] [log] [blame]
Elliott Hughes88e4e602022-11-08 02:51:00 +00001/* $NetBSD: regexec.c,v 1.26 2021/02/26 19:24:47 christos Exp $ */
Elliott Hughescc213f82012-08-14 15:32:42 -07002
3/*-
Elliott Hughes88e4e602022-11-08 02:51:00 +00004 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 * Copyright (c) 1992, 1993, 1994 Henry Spencer.
Elliott Hughescc213f82012-08-14 15:32:42 -07007 * Copyright (c) 1992, 1993, 1994
8 * The Regents of the University of California. All rights reserved.
9 *
10 * This code is derived from software contributed to Berkeley by
11 * Henry Spencer.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 *
37 * @(#)regexec.c 8.3 (Berkeley) 3/20/94
38 */
39
Elliott Hughes88e4e602022-11-08 02:51:00 +000040#if HAVE_NBTOOL_CONFIG_H
41#include "nbtool_config.h"
42#endif
Elliott Hughescc213f82012-08-14 15:32:42 -070043
44#include <sys/cdefs.h>
Elliott Hughescc213f82012-08-14 15:32:42 -070045#if 0
46static char sccsid[] = "@(#)regexec.c 8.3 (Berkeley) 3/20/94";
Elliott Hughes88e4e602022-11-08 02:51:00 +000047__FBSDID("$FreeBSD: head/lib/libc/regex/regexec.c 326025 2017-11-20 19:49:47Z pfg $");
Elliott Hughescc213f82012-08-14 15:32:42 -070048#endif
Elliott Hughes88e4e602022-11-08 02:51:00 +000049__RCSID("$NetBSD: regexec.c,v 1.26 2021/02/26 19:24:47 christos Exp $");
Elliott Hughescc213f82012-08-14 15:32:42 -070050
51/*
52 * the outer shell of regexec()
53 *
Elliott Hughes88e4e602022-11-08 02:51:00 +000054 * This file includes engine.c three times, after muchos fiddling with the
Elliott Hughescc213f82012-08-14 15:32:42 -070055 * macros that code uses. This lets the same code operate on two different
Elliott Hughes88e4e602022-11-08 02:51:00 +000056 * representations for state sets and characters.
Elliott Hughescc213f82012-08-14 15:32:42 -070057 */
Elliott Hughescc213f82012-08-14 15:32:42 -070058
Elliott Hughes88e4e602022-11-08 02:51:00 +000059#ifndef LIBHACK
60#include "namespace.h"
61#endif
62#include <sys/types.h>
Elliott Hughescc213f82012-08-14 15:32:42 -070063#include <stdio.h>
64#include <stdlib.h>
65#include <string.h>
Elliott Hughes88e4e602022-11-08 02:51:00 +000066#include <limits.h>
67#include <ctype.h>
Elliott Hughescc213f82012-08-14 15:32:42 -070068#include <regex.h>
69
Elliott Hughes88e4e602022-11-08 02:51:00 +000070#if defined(__weak_alias) && !defined(LIBHACK)
Elliott Hughescc213f82012-08-14 15:32:42 -070071__weak_alias(regexec,_regexec)
72#endif
73
74#include "utils.h"
75#include "regex2.h"
76
Elliott Hughes88e4e602022-11-08 02:51:00 +000077static __inline size_t
78xmbrtowc(wint_t *wi, const char *s, size_t n, mbstate_t *mbs, wint_t dummy)
79{
80#ifdef NLS
81 size_t nr;
82 wchar_t wc;
83
84 nr = mbrtowc(&wc, s, n, mbs);
85 if (wi != NULL)
86 *wi = wc;
87 if (nr == 0)
88 return (1);
89 else if (nr == (size_t)-1 || nr == (size_t)-2) {
90 memset(mbs, 0, sizeof(*mbs));
91 if (wi != NULL)
92 *wi = dummy;
93 return (1);
94 } else
95 return (nr);
96#else
97 if (wi)
98 *wi = *s;
99 return 1;
100#endif
101}
102
103static __inline size_t
104xmbrtowc_dummy(wint_t *wi,
105 const char *s,
106 size_t n __unused,
107 mbstate_t *mbs __unused,
108 wint_t dummy __unused)
109{
110
111 if (wi != NULL)
112 *wi = (unsigned char)*s;
113 return (1);
114}
115
Elliott Hughescc213f82012-08-14 15:32:42 -0700116/* macros for manipulating states, small version */
Elliott Hughes88e4e602022-11-08 02:51:00 +0000117#define states long
118#define states1 states /* for later use in regexec() decision */
Elliott Hughescc213f82012-08-14 15:32:42 -0700119#define CLEAR(v) ((v) = 0)
120#define SET0(v, n) ((v) &= ~((unsigned long)1 << (n)))
121#define SET1(v, n) ((v) |= (unsigned long)1 << (n))
122#define ISSET(v, n) (((v) & ((unsigned long)1 << (n))) != 0)
123#define ASSIGN(d, s) ((d) = (s))
124#define EQ(a, b) ((a) == (b))
Elliott Hughes88e4e602022-11-08 02:51:00 +0000125#define STATEVARS long dummy /* dummy version */
Elliott Hughescc213f82012-08-14 15:32:42 -0700126#define STATESETUP(m, n) /* nothing */
127#define STATETEARDOWN(m) /* nothing */
128#define SETUP(v) ((v) = 0)
Elliott Hughes88e4e602022-11-08 02:51:00 +0000129#define onestate long
Elliott Hughescc213f82012-08-14 15:32:42 -0700130#define INIT(o, n) ((o) = (unsigned long)1 << (n))
131#define INC(o) ((o) <<= 1)
132#define ISSTATEIN(v, o) (((v) & (o)) != 0)
133/* some abbreviations; note that some of these know variable names! */
134/* do "if I'm here, I can also be there" etc without branches */
135#define FWD(dst, src, n) ((dst) |= ((unsigned long)(src)&(here)) << (n))
136#define BACK(dst, src, n) ((dst) |= ((unsigned long)(src)&(here)) >> (n))
137#define ISSETBACK(v, n) (((v) & ((unsigned long)here >> (n))) != 0)
Elliott Hughes88e4e602022-11-08 02:51:00 +0000138/* no multibyte support */
139#define XMBRTOWC xmbrtowc_dummy
140#define ZAPSTATE(mbs) ((void)(mbs))
Elliott Hughescc213f82012-08-14 15:32:42 -0700141/* function names */
142#define SNAMES /* engine.c looks after details */
143
144#include "engine.c"
145
146/* now undo things */
147#undef states
148#undef CLEAR
149#undef SET0
150#undef SET1
151#undef ISSET
152#undef ASSIGN
153#undef EQ
154#undef STATEVARS
155#undef STATESETUP
156#undef STATETEARDOWN
157#undef SETUP
158#undef onestate
159#undef INIT
160#undef INC
161#undef ISSTATEIN
162#undef FWD
163#undef BACK
164#undef ISSETBACK
165#undef SNAMES
Elliott Hughes88e4e602022-11-08 02:51:00 +0000166#undef XMBRTOWC
167#undef ZAPSTATE
Elliott Hughescc213f82012-08-14 15:32:42 -0700168
169/* macros for manipulating states, large version */
170#define states char *
Elliott Hughes88e4e602022-11-08 02:51:00 +0000171#define CLEAR(v) memset(v, 0, m->g->nstates)
Elliott Hughescc213f82012-08-14 15:32:42 -0700172#define SET0(v, n) ((v)[n] = 0)
173#define SET1(v, n) ((v)[n] = 1)
174#define ISSET(v, n) ((v)[n])
Elliott Hughes88e4e602022-11-08 02:51:00 +0000175#define ASSIGN(d, s) memcpy(d, s, m->g->nstates)
176#define EQ(a, b) (memcmp(a, b, m->g->nstates) == 0)
177#define STATEVARS long vn; char *space
178#define STATESETUP(m, nv) { (m)->space = malloc((nv)*(m)->g->nstates); \
179 if ((m)->space == NULL) return(REG_ESPACE); \
180 (m)->vn = 0; }
181#define STATETEARDOWN(m) { free((m)->space); }
182#define SETUP(v) ((v) = &m->space[m->vn++ * m->g->nstates])
183#define onestate long
184#define INIT(o, n) ((o) = (n))
Elliott Hughescc213f82012-08-14 15:32:42 -0700185#define INC(o) ((o)++)
186#define ISSTATEIN(v, o) ((v)[o])
187/* some abbreviations; note that some of these know variable names! */
188/* do "if I'm here, I can also be there" etc without branches */
189#define FWD(dst, src, n) ((dst)[here+(n)] |= (src)[here])
190#define BACK(dst, src, n) ((dst)[here-(n)] |= (src)[here])
191#define ISSETBACK(v, n) ((v)[here - (n)])
Elliott Hughes88e4e602022-11-08 02:51:00 +0000192/* no multibyte support */
193#define XMBRTOWC xmbrtowc_dummy
194#define ZAPSTATE(mbs) ((void)(mbs))
Elliott Hughescc213f82012-08-14 15:32:42 -0700195/* function names */
196#define LNAMES /* flag */
197
198#include "engine.c"
199
Elliott Hughes88e4e602022-11-08 02:51:00 +0000200/* multibyte character & large states version */
201#undef LNAMES
202#undef XMBRTOWC
203#undef ZAPSTATE
204#define XMBRTOWC xmbrtowc
205#define ZAPSTATE(mbs) memset((mbs), 0, sizeof(*(mbs)))
206#define MNAMES
207
208#include "engine.c"
209
Elliott Hughescc213f82012-08-14 15:32:42 -0700210/*
211 - regexec - interface for matching
212 = extern int regexec(const regex_t *, const char *, size_t, \
213 = regmatch_t [], int);
214 = #define REG_NOTBOL 00001
215 = #define REG_NOTEOL 00002
216 = #define REG_STARTEND 00004
217 = #define REG_TRACE 00400 // tracing of execution
218 = #define REG_LARGE 01000 // force large representation
219 = #define REG_BACKR 02000 // force use of backref code
220 *
221 * We put this here so we can exploit knowledge of the state representation
222 * when choosing which matcher to call. Also, by this point the matchers
223 * have been prototyped.
224 */
225int /* 0 success, REG_NOMATCH failure */
Elliott Hughes88e4e602022-11-08 02:51:00 +0000226regexec(const regex_t * __restrict preg,
227 const char * __restrict string,
228 size_t nmatch,
229 regmatch_t pmatch[__restrict],
230 int eflags)
Elliott Hughescc213f82012-08-14 15:32:42 -0700231{
232 struct re_guts *g = preg->re_g;
Elliott Hughescc213f82012-08-14 15:32:42 -0700233#ifdef REDEBUG
234# define GOODFLAGS(f) (f)
235#else
236# define GOODFLAGS(f) ((f)&(REG_NOTBOL|REG_NOTEOL|REG_STARTEND))
237#endif
Elliott Hughescc213f82012-08-14 15:32:42 -0700238 _DIAGASSERT(preg != NULL);
239 _DIAGASSERT(string != NULL);
240
241 if (preg->re_magic != MAGIC1 || g->magic != MAGIC2)
242 return(REG_BADPAT);
243 assert(!(g->iflags&BAD));
244 if (g->iflags&BAD) /* backstop for no-debug case */
245 return(REG_BADPAT);
246 eflags = GOODFLAGS(eflags);
247
Elliott Hughes88e4e602022-11-08 02:51:00 +0000248 if (MB_CUR_MAX > 1)
249 return(mmatcher(g, string, nmatch, pmatch, eflags));
250 else if (g->nstates <= CHAR_BIT*sizeof(states1) && !(eflags&REG_LARGE))
251 return(smatcher(g, string, nmatch, pmatch, eflags));
Elliott Hughescc213f82012-08-14 15:32:42 -0700252 else
Elliott Hughes88e4e602022-11-08 02:51:00 +0000253 return(lmatcher(g, string, nmatch, pmatch, eflags));
Elliott Hughescc213f82012-08-14 15:32:42 -0700254}