Coverage Report

Created: 2023-12-08 06:48

/src/clamav/libclamav/regex/regex2.h
Line
Count
Source (jump to first uncovered line)
1
/*  $OpenBSD: regex2.h,v 1.12 2021/01/03 17:07:58 tb Exp $  */
2
3
/*-
4
 * Copyright (c) 1992, 1993, 1994 Henry Spencer.
5
 * Copyright (c) 1992, 1993, 1994
6
 *  The Regents of the University of California.  All rights reserved.
7
 *
8
 * This code is derived from software contributed to Berkeley by
9
 * Henry Spencer.
10
 *
11
 * Redistribution and use in source and binary forms, with or without
12
 * modification, are permitted provided that the following conditions
13
 * are met:
14
 * 1. Redistributions of source code must retain the above copyright
15
 *    notice, this list of conditions and the following disclaimer.
16
 * 2. Redistributions in binary form must reproduce the above copyright
17
 *    notice, this list of conditions and the following disclaimer in the
18
 *    documentation and/or other materials provided with the distribution.
19
 * 3. Neither the name of the University nor the names of its contributors
20
 *    may be used to endorse or promote products derived from this software
21
 *    without specific prior written permission.
22
 *
23
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33
 * SUCH DAMAGE.
34
 *
35
 *  @(#)regex2.h  8.4 (Berkeley) 3/20/94
36
 */
37
38
/*
39
 * internals of regex_t
40
 */
41
200k
#define MAGIC1  ((('r'^0200)<<8) | 'e')
42
43
/*
44
 * The internal representation is a *strip*, a sequence of
45
 * operators ending with an endmarker.  (Some terminology etc. is a
46
 * historical relic of earlier versions which used multiple strips.)
47
 * Certain oddities in the representation are there to permit running
48
 * the machinery backwards; in particular, any deviation from sequential
49
 * flow must be marked at both its source and its destination.  Some
50
 * fine points:
51
 *
52
 * - OPLUS_ and O_PLUS are *inside* the loop they create.
53
 * - OQUEST_ and O_QUEST are *outside* the bypass they create.
54
 * - OCH_ and O_CH are *outside* the multi-way branch they create, while
55
 *   OOR1 and OOR2 are respectively the end and the beginning of one of
56
 *   the branches.  Note that there is an implicit OOR2 following OCH_
57
 *   and an implicit OOR1 preceding O_CH.
58
 *
59
 * In state representations, an operator's bit is on to signify a state
60
 * immediately *preceding* "execution" of that operator.
61
 */
62
typedef unsigned long sop;  /* strip operator */
63
typedef long sopno;
64
9.77G
#define OPRMASK 0xf8000000LU
65
476M
#define OPDMASK 0x07ffffffLU
66
6.47G
#define OPSHIFT ((unsigned)27)
67
9.77G
#define OP(n) ((n)&OPRMASK)
68
476M
#define OPND(n) ((n)&OPDMASK)
69
9.85M
#define SOP(op, opnd) ((op)|(opnd))
70
/* operators         meaning  operand     */
71
/*            (back, fwd are offsets) */
72
4.54G
#define OEND  (1LU<<OPSHIFT)  /* endmarker  -     */
73
1.48G
#define OCHAR (2LU<<OPSHIFT)  /* character  unsigned char   */
74
0
#define OBOL  (3LU<<OPSHIFT)  /* left anchor  -     */
75
0
#define OEOL  (4LU<<OPSHIFT)  /* right anchor -     */
76
0
#define OANY  (5LU<<OPSHIFT)  /* .    -     */
77
0
#define OANYOF  (6LU<<OPSHIFT)  /* [...]  set number    */
78
0
#define OBACK_  (7LU<<OPSHIFT)  /* begin \d paren number    */
79
0
#define O_BACK  (8LU<<OPSHIFT)  /* end \d paren number    */
80
9.39M
#define OPLUS_  (9LU<<OPSHIFT)  /* + prefix fwd to suffix   */
81
9.07M
#define O_PLUS  (10LU<<OPSHIFT)  /* + suffix back to prefix    */
82
1.44M
#define OQUEST_ (11LU<<OPSHIFT)  /* ? prefix fwd to suffix   */
83
164M
#define O_QUEST (12LU<<OPSHIFT)  /* ? suffix back to prefix    */
84
76.8M
#define OLPAREN (13LU<<OPSHIFT)  /* (    fwd to )    */
85
153M
#define ORPAREN (14LU<<OPSHIFT)  /* )    back to (   */
86
21.2M
#define OCH_  (15LU<<OPSHIFT)  /* begin choice fwd to OOR2   */
87
0
#define OOR1  (16LU<<OPSHIFT)  /* | pt. 1  back to OOR1 or OCH_  */
88
19.8M
#define OOR2  (17LU<<OPSHIFT)  /* | pt. 2  fwd to OOR2 or O_CH */
89
120M
#define O_CH  (18LU<<OPSHIFT)  /* end choice back to OOR1    */
90
0
#define OBOW  (19LU<<OPSHIFT)  /* begin word -     */
91
0
#define OEOW  (20LU<<OPSHIFT)  /* end word -     */
92
93
/*
94
 * Structure for [] character-set representation.  Character sets are
95
 * done as bit vectors, grouped 8 to a byte vector for compactness.
96
 * The individual set therefore has both a pointer to the byte vector
97
 * and a mask to pick out the relevant bit of each byte.  A hash code
98
 * simplifies testing whether two sets could be identical.
99
 *
100
 * This will get trickier for multicharacter collating elements.  As
101
 * preliminary hooks for dealing with such things, we also carry along
102
 * a string of multi-character elements, and decide the size of the
103
 * vectors at run time.
104
 */
105
typedef struct {
106
  uch *ptr;   /* -> uch [csetsize] */
107
  uch mask;   /* bit within array */
108
  uch hash;   /* hash code */
109
} cset;
110
111
static inline void
112
CHadd(cset *cs, char c)
113
8.39M
{
114
8.39M
  cs->ptr[(uch)c] |= cs->mask;
115
8.39M
  cs->hash += c;
116
8.39M
}
regcomp.c:CHadd
Line
Count
Source
113
8.39M
{
114
8.39M
  cs->ptr[(uch)c] |= cs->mask;
115
8.39M
  cs->hash += c;
116
8.39M
}
Unexecuted instantiation: regexec.c:CHadd
Unexecuted instantiation: regfree.c:CHadd
117
118
static inline void
119
CHsub(cset *cs, char c)
120
137M
{
121
137M
  cs->ptr[(uch)c] &= ~cs->mask;
122
137M
  cs->hash -= c;
123
137M
}
regcomp.c:CHsub
Line
Count
Source
120
137M
{
121
137M
  cs->ptr[(uch)c] &= ~cs->mask;
122
137M
  cs->hash -= c;
123
137M
}
Unexecuted instantiation: regexec.c:CHsub
Unexecuted instantiation: regfree.c:CHsub
124
125
static inline int
126
CHIN(const cset *cs, char c)
127
722M
{
128
722M
  return (cs->ptr[(uch)c] & cs->mask) != 0;
129
722M
}
regcomp.c:CHIN
Line
Count
Source
127
722M
{
128
722M
  return (cs->ptr[(uch)c] & cs->mask) != 0;
129
722M
}
Unexecuted instantiation: regexec.c:CHIN
Unexecuted instantiation: regfree.c:CHIN
130
131
/*
132
 * main compiled-expression structure
133
 */
134
struct re_guts {
135
192k
#   define  MAGIC2  ((('R'^0200)<<8)|'E')
136
  sop *strip;   /* malloced area for strip */
137
  cset *sets;   /* -> cset [ncsets] */
138
  uch *setbits;   /* -> uch[csetsize][ncsets/CHAR_BIT] */
139
  int magic;
140
  int csetsize;   /* number of bits in a cset vector */
141
  int ncsets;   /* number of csets in use */
142
  int cflags;   /* copy of regcomp() cflags argument */
143
  sopno nstates;    /* = number of sops */
144
  sopno firststate; /* the initial OEND (normally 0) */
145
  sopno laststate;  /* the final OEND */
146
  int iflags;   /* internal flags */
147
54.5k
#   define  USEBOL  01  /* used ^ */
148
53.5k
#   define  USEEOL  02  /* used $ */
149
96.2k
#   define  REGEX_BAD 04  /* something wrong */
150
  int nbol;   /* number of ^ used */
151
  int neol;   /* number of $ used */
152
  char *must;   /* match must contain this string */
153
  int mlen;   /* length of must */
154
  int backrefs;   /* does it use back references? */
155
  size_t nsub;    /* copy of re_nsub */
156
  sopno nplus;    /* how deep does it nest +s? */
157
};
158
159
/* misc utilities */
160
96.2k
#define OUT (CHAR_MAX+1)  /* a non-character value */
161
0
#define ISWORD(c) (isalnum((c)&0xff) || (c) == '_')