/*-1* SPDX-License-Identifier: BSD-3-Clause2*3* Copyright (c) 1992, 1993, 1994 Henry Spencer.4* Copyright (c) 1992, 1993, 19945* The Regents of the University of California. All rights reserved.6*7* This code is derived from software contributed to Berkeley by8* Henry Spencer.9*10* Redistribution and use in source and binary forms, with or without11* modification, are permitted provided that the following conditions12* are met:13* 1. Redistributions of source code must retain the above copyright14* notice, this list of conditions and the following disclaimer.15* 2. Redistributions in binary form must reproduce the above copyright16* notice, this list of conditions and the following disclaimer in the17* documentation and/or other materials provided with the distribution.18* 3. Neither the name of the University nor the names of its contributors19* may be used to endorse or promote products derived from this software20* without specific prior written permission.21*22* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND23* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE24* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE25* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE26* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL27* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS28* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)29* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT30* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY31* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF32* SUCH DAMAGE.33*/3435/*36* the outer shell of regexec()37*38* This file includes engine.c three times, after muchos fiddling with the39* macros that code uses. This lets the same code operate on two different40* representations for state sets and characters.41*/42#include <sys/types.h>43#include <stdio.h>44#include <stdlib.h>45#include <string.h>46#include <limits.h>47#include <ctype.h>48#include <regex.h>49#include <wchar.h>50#include <wctype.h>5152#include "utils.h"53#include "regex2.h"5455static int nope __unused = 0; /* for use in asserts; shuts lint up */5657static __inline size_t58xmbrtowc(wint_t *wi, const char *s, size_t n, mbstate_t *mbs, wint_t dummy)59{60size_t nr;61wchar_t wc;6263nr = mbrtowc(&wc, s, n, mbs);64if (wi != NULL)65*wi = wc;66if (nr == 0)67return (1);68else if (nr == (size_t)-1 || nr == (size_t)-2) {69memset(mbs, 0, sizeof(*mbs));70if (wi != NULL)71*wi = dummy;72return (1);73} else74return (nr);75}7677static __inline size_t78xmbrtowc_dummy(wint_t *wi,79const char *s,80size_t n __unused,81mbstate_t *mbs __unused,82wint_t dummy __unused)83{8485if (wi != NULL)86*wi = (unsigned char)*s;87return (1);88}8990/* macros for manipulating states, small version */91#define states1 long /* for later use in regexec() decision */92#define states states193#define CLEAR(v) ((v) = 0)94#define SET0(v, n) ((v) &= ~((unsigned long)1 << (n)))95#define SET1(v, n) ((v) |= (unsigned long)1 << (n))96#define ISSET(v, n) (((v) & ((unsigned long)1 << (n))) != 0)97#define ASSIGN(d, s) ((d) = (s))98#define EQ(a, b) ((a) == (b))99#define STATEVARS long dummy /* dummy version */100#define STATESETUP(m, n) /* nothing */101#define STATETEARDOWN(m) /* nothing */102#define SETUP(v) ((v) = 0)103#define onestate long104#define INIT(o, n) ((o) = (unsigned long)1 << (n))105#define INC(o) ((o) <<= 1)106#define ISSTATEIN(v, o) (((v) & (o)) != 0)107/* some abbreviations; note that some of these know variable names! */108/* do "if I'm here, I can also be there" etc without branches */109#define FWD(dst, src, n) ((dst) |= ((unsigned long)(src)&(here)) << (n))110#define BACK(dst, src, n) ((dst) |= ((unsigned long)(src)&(here)) >> (n))111#define ISSETBACK(v, n) (((v) & ((unsigned long)here >> (n))) != 0)112/* no multibyte support */113#define XMBRTOWC xmbrtowc_dummy114#define ZAPSTATE(mbs) ((void)(mbs))115/* function names */116#define SNAMES /* engine.c looks after details */117118#include "engine.c"119120/* now undo things */121#undef states122#undef CLEAR123#undef SET0124#undef SET1125#undef ISSET126#undef ASSIGN127#undef EQ128#undef STATEVARS129#undef STATESETUP130#undef STATETEARDOWN131#undef SETUP132#undef onestate133#undef INIT134#undef INC135#undef ISSTATEIN136#undef FWD137#undef BACK138#undef ISSETBACK139#undef SNAMES140#undef XMBRTOWC141#undef ZAPSTATE142143/* macros for manipulating states, large version */144#define states char *145#define CLEAR(v) memset(v, 0, m->g->nstates)146#define SET0(v, n) ((v)[n] = 0)147#define SET1(v, n) ((v)[n] = 1)148#define ISSET(v, n) ((v)[n])149#define ASSIGN(d, s) memcpy(d, s, m->g->nstates)150#define EQ(a, b) (memcmp(a, b, m->g->nstates) == 0)151#define STATEVARS long vn; char *space152#define STATESETUP(m, nv) { (m)->space = malloc((nv)*(m)->g->nstates); \153if ((m)->space == NULL) return(REG_ESPACE); \154(m)->vn = 0; }155#define STATETEARDOWN(m) { free((m)->space); }156#define SETUP(v) ((v) = &m->space[m->vn++ * m->g->nstates])157#define onestate long158#define INIT(o, n) ((o) = (n))159#define INC(o) ((o)++)160#define ISSTATEIN(v, o) ((v)[o])161/* some abbreviations; note that some of these know variable names! */162/* do "if I'm here, I can also be there" etc without branches */163#define FWD(dst, src, n) ((dst)[here+(n)] |= (src)[here])164#define BACK(dst, src, n) ((dst)[here-(n)] |= (src)[here])165#define ISSETBACK(v, n) ((v)[here - (n)])166/* no multibyte support */167#define XMBRTOWC xmbrtowc_dummy168#define ZAPSTATE(mbs) ((void)(mbs))169/* function names */170#define LNAMES /* flag */171172#include "engine.c"173174/* multibyte character & large states version */175#undef LNAMES176#undef XMBRTOWC177#undef ZAPSTATE178#define XMBRTOWC xmbrtowc179#define ZAPSTATE(mbs) memset((mbs), 0, sizeof(*(mbs)))180#define MNAMES181182#include "engine.c"183184/*185- regexec - interface for matching186= extern int regexec(const regex_t *, const char *, size_t, \187= regmatch_t [], int);188= #define REG_NOTBOL 00001189= #define REG_NOTEOL 00002190= #define REG_STARTEND 00004191= #define REG_TRACE 00400 // tracing of execution192= #define REG_LARGE 01000 // force large representation193= #define REG_BACKR 02000 // force use of backref code194*195* We put this here so we can exploit knowledge of the state representation196* when choosing which matcher to call. Also, by this point the matchers197* have been prototyped.198*/199int /* 0 success, REG_NOMATCH failure */200regexec(const regex_t * __restrict preg,201const char * __restrict string,202size_t nmatch,203regmatch_t pmatch[__restrict],204int eflags)205{206struct re_guts *g = preg->re_g;207#ifdef REDEBUG208# define GOODFLAGS(f) (f)209#else210# define GOODFLAGS(f) ((f)&(REG_NOTBOL|REG_NOTEOL|REG_STARTEND))211#endif212213if (preg->re_magic != MAGIC1 || g->magic != MAGIC2)214return(REG_BADPAT);215assert(!(g->iflags&BAD));216if (g->iflags&BAD) /* backstop for no-debug case */217return(REG_BADPAT);218eflags = GOODFLAGS(eflags);219220if (MB_CUR_MAX > 1)221return(mmatcher(g, string, nmatch, pmatch, eflags));222else if (g->nstates <= CHAR_BIT*sizeof(states1) && !(eflags®_LARGE))223return(smatcher(g, string, nmatch, pmatch, eflags));224else225return(lmatcher(g, string, nmatch, pmatch, eflags));226}227228229