1 /*
   2  * Copyright (c) 1992, 1993, 1994 Henry Spencer.
   3  * Copyright (c) 1992, 1993, 1994
   4  *      The Regents of the University of California.  All rights reserved.
   5  *
   6  * This code is derived from software contributed to Berkeley by
   7  * Henry Spencer.
   8  *
   9  * Redistribution and use in source and binary forms, with or without
  10  * modification, are permitted provided that the following conditions
  11  * are met:
  12  * 1. Redistributions of source code must retain the above copyright
  13  *    notice, this list of conditions and the following disclaimer.
  14  * 2. Redistributions in binary form must reproduce the above copyright
  15  *    notice, this list of conditions and the following disclaimer in the
  16  *    documentation and/or other materials provided with the distribution.
  17  * 3. Neither the name of the University nor the names of its contributors
  18  *    may be used to endorse or promote products derived from this software
  19  *    without specific prior written permission.
  20  *
  21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31  * SUCH DAMAGE.
  32  */
  33 
  34 /*
  35  * First, the stuff that ends up in the outside-world include file
  36  * typedef off_t regoff_t;
  37  * typedef struct {
  38  *      int re_magic;
  39  *      size_t re_nsub;         // number of parenthesized subexpressions
  40  *      const char *re_endp;    // end pointer for REG_PEND
  41  *      struct re_guts *re_g;   // none of your business :-)
  42  * } regex_t;
  43  * typedef struct {
  44  *      regoff_t rm_so;         // start of match
  45  *      regoff_t rm_eo;         // end of match
  46  * } regmatch_t;
  47  */
  48 /*
  49  * internals of regex_t
  50  */
  51 #define MAGIC1  ((('r'^0200)<<8) | 'e')
  52 
  53 /*
  54  * The internal representation is a *strip*, a sequence of
  55  * operators ending with an endmarker.  (Some terminology etc. is a
  56  * historical relic of earlier versions which used multiple strips.)
  57  * Certain oddities in the representation are there to permit running
  58  * the machinery backwards; in particular, any deviation from sequential
  59  * flow must be marked at both its source and its destination.  Some
  60  * fine points:
  61  *
  62  * - OPLUS_ and O_PLUS are *inside* the loop they create.
  63  * - OQUEST_ and O_QUEST are *outside* the bypass they create.
  64  * - OCH_ and O_CH are *outside* the multi-way branch they create, while
  65  *   OOR1 and OOR2 are respectively the end and the beginning of one of
  66  *   the branches.  Note that there is an implicit OOR2 following OCH_
  67  *   and an implicit OOR1 preceding O_CH.
  68  *
  69  * In state representations, an operator's bit is on to signify a state
  70  * immediately *preceding* "execution" of that operator.
  71  */
  72 typedef unsigned int sop;       /* strip operator */
  73 typedef unsigned int sopno;
  74 #define OPRMASK 0xf8000000U
  75 #define OPDMASK 0x07ffffffU
  76 #define OPSHIFT ((unsigned)27)
  77 #define OP(n)   ((n)&OPRMASK)
  78 #define OPND(n) ((n)&OPDMASK)
  79 #define SOP(op, opnd)   ((op)|(opnd))
  80 /* operators                       meaning      operand                 */
  81 /*                                              (back, fwd are offsets) */
  82 #define OEND    (1U<<OPSHIFT)     /* endmarker    -                       */
  83 #define OCHAR   (2U<<OPSHIFT)     /* character    wide character          */
  84 #define OBOL    (3U<<OPSHIFT)     /* left anchor  -                       */
  85 #define OEOL    (4U<<OPSHIFT)     /* right anchor -                       */
  86 #define OANY    (5U<<OPSHIFT)     /* .            -                       */
  87 #define OANYOF  (6U<<OPSHIFT)     /* [...]        set number              */
  88 #define OBACK_  (7U<<OPSHIFT)     /* begin \d     paren number            */
  89 #define O_BACK  (8U<<OPSHIFT)     /* end \d       paren number            */
  90 #define OPLUS_  (9U<<OPSHIFT)     /* + prefix     fwd to suffix           */
  91 #define O_PLUS  (10U<<OPSHIFT)    /* + suffix     back to prefix          */
  92 #define OQUEST_ (11U<<OPSHIFT)    /* ? prefix     fwd to suffix           */
  93 #define O_QUEST (12U<<OPSHIFT)    /* ? suffix     back to prefix          */
  94 #define OLPAREN (13U<<OPSHIFT)    /* (            fwd to )                */
  95 #define ORPAREN (14U<<OPSHIFT)    /* )            back to (               */
  96 #define OCH_    (15U<<OPSHIFT)    /* begin choice fwd to OOR2             */
  97 #define OOR1    (16U<<OPSHIFT)    /* | pt. 1      back to OOR1 or OCH_    */
  98 #define OOR2    (17U<<OPSHIFT)    /* | pt. 2      fwd to OOR2 or O_CH     */
  99 #define O_CH    (18U<<OPSHIFT)    /* end choice   back to OOR1            */
 100 #define OBOW    (19U<<OPSHIFT)    /* begin word   -                       */
 101 #define OEOW    (20U<<OPSHIFT)    /* end word     -                       */
 102 
 103 /*
 104  * Structures for [] character-set representation.
 105  */
 106 typedef struct {
 107         wint_t          min;
 108         wint_t          max;
 109 } crange;
 110 typedef struct {
 111         unsigned char   bmp[NC / 8];
 112         wctype_t        *types;
 113         unsigned int    ntypes;
 114         wint_t          *wides;
 115         unsigned int    nwides;
 116         crange          *ranges;
 117         unsigned int    nranges;
 118         int             invert;
 119         int             icase;
 120 } cset;
 121 
 122 static int
 123 CHIN1(cset *cs, wint_t ch)
 124 {
 125         unsigned int i;
 126 
 127         assert(ch >= 0);
 128         if (ch < NC)
 129                 return (((cs->bmp[ch >> 3] & (1 << (ch & 7))) != 0) ^
 130                     cs->invert);
 131         for (i = 0; i < cs->nwides; i++)
 132                 if (ch == cs->wides[i])
 133                         return (!cs->invert);
 134         for (i = 0; i < cs->nranges; i++)
 135                 if (cs->ranges[i].min <= ch && ch <= cs->ranges[i].max)
 136                         return (!cs->invert);
 137         for (i = 0; i < cs->ntypes; i++)
 138                 if (iswctype(ch, cs->types[i]))
 139                         return (!cs->invert);
 140         return (cs->invert);
 141 }
 142 
 143 static int
 144 CHIN(cset *cs, wint_t ch)
 145 {
 146 
 147         assert(ch >= 0);
 148         if (ch < NC)
 149                 return (((cs->bmp[ch >> 3] & (1 << (ch & 7))) != 0) ^
 150                     cs->invert);
 151         else if (cs->icase)
 152                 return (CHIN1(cs, ch) || CHIN1(cs, towlower(ch)) ||
 153                     CHIN1(cs, towupper(ch)));
 154         else
 155                 return (CHIN1(cs, ch));
 156 }
 157 
 158 /*
 159  * main compiled-expression structure
 160  */
 161 struct re_guts {
 162         int magic;
 163 #define MAGIC2  ((('R'^0200)<<8)|'E')
 164         sop *strip;             /* malloced area for strip */
 165         unsigned int ncsets;    /* number of csets in use */
 166         cset *sets;             /* -> cset [ncsets] */
 167         int cflags;             /* copy of regcomp() cflags argument */
 168         sopno nstates;          /* = number of sops */
 169         sopno firststate;       /* the initial OEND (normally 0) */
 170         sopno laststate;        /* the final OEND */
 171         int iflags;             /* internal flags */
 172 #define USEBOL  01      /* used ^ */
 173 #define USEEOL  02      /* used $ */
 174 #define BAD     04      /* something wrong */
 175         int nbol;               /* number of ^ used */
 176         int neol;               /* number of $ used */
 177         char *must;             /* match must contain this string */
 178         int moffset;            /* latest point at which must may be located */
 179         int *charjump;          /* Boyer-Moore char jump table */
 180         int *matchjump;         /* Boyer-Moore match jump table */
 181         int mlen;               /* length of must */
 182         size_t nsub;            /* copy of re_nsub */
 183         int backrefs;           /* does it use back references? */
 184         sopno nplus;            /* how deep does it nest +s? */
 185 };
 186 
 187 /* misc utilities */
 188 #define OUT     (CHAR_MIN - 1)  /* a non-character value */
 189 #define IGN     (CHAR_MIN - 2)
 190 #define ISWORD(c)       (iswalnum((uch)(c)) || (c) == '_')