| 1 |
nigel |
3 |
/************************************************* |
| 2 |
|
|
* Perl-Compatible Regular Expressions * |
| 3 |
|
|
*************************************************/ |
| 4 |
|
|
|
| 5 |
|
|
|
| 6 |
nigel |
5 |
#define PCRE_VERSION "1.01 19-Nov-1997" |
| 7 |
nigel |
3 |
|
| 8 |
|
|
|
| 9 |
|
|
/* This is a library of functions to support regular expressions whose syntax |
| 10 |
|
|
and semantics are as close as possible to those of the Perl 5 language. See |
| 11 |
|
|
the file Tech.Notes for some information on the internals. |
| 12 |
|
|
|
| 13 |
|
|
Written by: Philip Hazel <ph10@cam.ac.uk> |
| 14 |
|
|
|
| 15 |
|
|
Copyright (c) 1997 University of Cambridge |
| 16 |
|
|
|
| 17 |
|
|
----------------------------------------------------------------------------- |
| 18 |
|
|
Permission is granted to anyone to use this software for any purpose on any |
| 19 |
|
|
computer system, and to redistribute it freely, subject to the following |
| 20 |
|
|
restrictions: |
| 21 |
|
|
|
| 22 |
|
|
1. This software is distributed in the hope that it will be useful, |
| 23 |
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 24 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 25 |
|
|
|
| 26 |
|
|
2. The origin of this software must not be misrepresented, either by |
| 27 |
|
|
explicit claim or by omission. |
| 28 |
|
|
|
| 29 |
|
|
3. Altered versions must be plainly marked as such, and must not be |
| 30 |
|
|
misrepresented as being the original software. |
| 31 |
|
|
----------------------------------------------------------------------------- |
| 32 |
|
|
*/ |
| 33 |
|
|
|
| 34 |
|
|
/* This header contains definitions that are shared between the different |
| 35 |
|
|
modules, but which are not relevant to the outside. */ |
| 36 |
|
|
|
| 37 |
|
|
/* To cope with SunOS4 and other systems that lack memmove() but have bcopy(), |
| 38 |
|
|
define a macro for memmove() if USE_BCOPY is defined. */ |
| 39 |
|
|
|
| 40 |
|
|
#ifdef USE_BCOPY |
| 41 |
|
|
#define memmove(a, b, c) bcopy(b, a, c) |
| 42 |
|
|
#endif |
| 43 |
|
|
|
| 44 |
|
|
/* Standard C headers plus the external interface definition */ |
| 45 |
|
|
|
| 46 |
|
|
#include <ctype.h> |
| 47 |
|
|
#include <limits.h> |
| 48 |
|
|
#include <setjmp.h> |
| 49 |
|
|
#include <stddef.h> |
| 50 |
|
|
#include <stdio.h> |
| 51 |
|
|
#include <stdlib.h> |
| 52 |
|
|
#include <string.h> |
| 53 |
|
|
#include "pcre.h" |
| 54 |
|
|
|
| 55 |
|
|
/* Private options flags start at the most significant end of the two bytes. |
| 56 |
|
|
The public options defined in pcre.h start at the least significant end. Make |
| 57 |
|
|
sure they don't overlap! */ |
| 58 |
|
|
|
| 59 |
|
|
#define PCRE_FIRSTSET 0x8000 /* first_char is set */ |
| 60 |
|
|
#define PCRE_STARTLINE 0x4000 /* start after \n for multiline */ |
| 61 |
|
|
#define PCRE_COMPILED_CASELESS 0x2000 /* like it says */ |
| 62 |
|
|
|
| 63 |
|
|
/* Options for the "extra" block produced by pcre_study(). */ |
| 64 |
|
|
|
| 65 |
|
|
#define PCRE_STUDY_CASELESS 0x01 /* study was caseless */ |
| 66 |
|
|
#define PCRE_STUDY_MAPPED 0x02 /* a map of starting chars exists */ |
| 67 |
|
|
|
| 68 |
|
|
/* Masks for identifying the public options: all permitted at compile time, |
| 69 |
|
|
only some permitted at run or study time. */ |
| 70 |
|
|
|
| 71 |
|
|
#define PUBLIC_OPTIONS \ |
| 72 |
|
|
(PCRE_CASELESS|PCRE_EXTENDED|PCRE_ANCHORED|PCRE_MULTILINE| \ |
| 73 |
|
|
PCRE_DOTALL|PCRE_DOLLAR_ENDONLY|PCRE_EXTRA) |
| 74 |
|
|
|
| 75 |
|
|
#define PUBLIC_EXEC_OPTIONS \ |
| 76 |
|
|
(PCRE_CASELESS|PCRE_ANCHORED|PCRE_MULTILINE|PCRE_NOTBOL|PCRE_NOTEOL| \ |
| 77 |
|
|
PCRE_DOTALL|PCRE_DOLLAR_ENDONLY) |
| 78 |
|
|
|
| 79 |
|
|
#define PUBLIC_STUDY_OPTIONS (PCRE_CASELESS) |
| 80 |
|
|
|
| 81 |
|
|
/* Magic number to provide a small check against being handed junk. */ |
| 82 |
|
|
|
| 83 |
|
|
#define MAGIC_NUMBER 0x50435245 /* 'PCRE' */ |
| 84 |
|
|
|
| 85 |
|
|
/* Miscellaneous definitions */ |
| 86 |
|
|
|
| 87 |
|
|
typedef int BOOL; |
| 88 |
|
|
|
| 89 |
|
|
#define FALSE 0 |
| 90 |
|
|
#define TRUE 1 |
| 91 |
|
|
|
| 92 |
|
|
/* These are escaped items that aren't just an encoding of a particular data |
| 93 |
|
|
value such as \n. They must have non-zero values, as check_escape() returns |
| 94 |
|
|
their negation. Also, they must appear in the same order as in the opcode |
| 95 |
|
|
definitions below, up to ESC_Z. The final one must be ESC_REF as subsequent |
| 96 |
|
|
values are used for \1, \2, \3, etc. There is a test in the code for an escape |
| 97 |
|
|
greater than ESC_b and less than ESC_X to detect the types that may be |
| 98 |
|
|
repeated. If any new escapes are put in-between that don't consume a character, |
| 99 |
|
|
that code will have to change. */ |
| 100 |
|
|
|
| 101 |
|
|
enum { ESC_A = 1, ESC_B, ESC_b, ESC_D, ESC_d, ESC_S, ESC_s, ESC_W, ESC_w, |
| 102 |
|
|
|
| 103 |
|
|
/* These are not Perl escapes, so can't appear in the */ |
| 104 |
|
|
ESC_X, /* simple table-lookup because they must be conditional */ |
| 105 |
|
|
/* on PCRE_EXTRA. */ |
| 106 |
|
|
ESC_Z, |
| 107 |
|
|
ESC_REF }; |
| 108 |
|
|
|
| 109 |
|
|
/* Opcode table: OP_BRA must be last, as all values >= it are used for brackets |
| 110 |
|
|
that extract substrings. Starting from 1 (i.e. after OP_END), the values up to |
| 111 |
|
|
OP_EOL must correspond in order to the list of escapes immediately above. */ |
| 112 |
|
|
|
| 113 |
|
|
enum { |
| 114 |
|
|
OP_END, /* End of pattern */ |
| 115 |
|
|
|
| 116 |
|
|
/* Values corresponding to backslashed metacharacters */ |
| 117 |
|
|
|
| 118 |
|
|
OP_SOD, /* Start of data: \A */ |
| 119 |
|
|
OP_NOT_WORD_BOUNDARY, /* \B */ |
| 120 |
|
|
OP_WORD_BOUNDARY, /* \b */ |
| 121 |
|
|
OP_NOT_DIGIT, /* \D */ |
| 122 |
|
|
OP_DIGIT, /* \d */ |
| 123 |
|
|
OP_NOT_WHITESPACE, /* \S */ |
| 124 |
|
|
OP_WHITESPACE, /* \s */ |
| 125 |
|
|
OP_NOT_WORDCHAR, /* \W */ |
| 126 |
|
|
OP_WORDCHAR, /* \w */ |
| 127 |
|
|
OP_CUT, /* The analogue of Prolog's "cut" operation (extension) */ |
| 128 |
|
|
OP_EOD, /* End of data: or \Z. This must always be the last |
| 129 |
|
|
of the backslashed meta values. */ |
| 130 |
|
|
|
| 131 |
|
|
OP_CIRC, /* Start of line - varies with multiline switch */ |
| 132 |
|
|
OP_DOLL, /* End of line - varies with multiline switch */ |
| 133 |
|
|
OP_ANY, /* Match any character */ |
| 134 |
|
|
OP_CHARS, /* Match string of characters */ |
| 135 |
|
|
OP_NOT, /* Match anything but the following char */ |
| 136 |
|
|
|
| 137 |
|
|
OP_STAR, /* The maximizing and minimizing versions of */ |
| 138 |
|
|
OP_MINSTAR, /* all these opcodes must come in pairs, with */ |
| 139 |
|
|
OP_PLUS, /* the minimizing one second. */ |
| 140 |
|
|
OP_MINPLUS, /* This first set applies to single characters */ |
| 141 |
|
|
OP_QUERY, |
| 142 |
|
|
OP_MINQUERY, |
| 143 |
|
|
OP_UPTO, /* From 0 to n matches */ |
| 144 |
|
|
OP_MINUPTO, |
| 145 |
|
|
OP_EXACT, /* Exactly n matches */ |
| 146 |
|
|
|
| 147 |
|
|
OP_NOTSTAR, /* The maximizing and minimizing versions of */ |
| 148 |
|
|
OP_NOTMINSTAR, /* all these opcodes must come in pairs, with */ |
| 149 |
|
|
OP_NOTPLUS, /* the minimizing one second. */ |
| 150 |
|
|
OP_NOTMINPLUS, /* This first set applies to "not" single characters */ |
| 151 |
|
|
OP_NOTQUERY, |
| 152 |
|
|
OP_NOTMINQUERY, |
| 153 |
|
|
OP_NOTUPTO, /* From 0 to n matches */ |
| 154 |
|
|
OP_NOTMINUPTO, |
| 155 |
|
|
OP_NOTEXACT, /* Exactly n matches */ |
| 156 |
|
|
|
| 157 |
|
|
OP_TYPESTAR, /* The maximizing and minimizing versions of */ |
| 158 |
|
|
OP_TYPEMINSTAR, /* all these opcodes must come in pairs, with */ |
| 159 |
|
|
OP_TYPEPLUS, /* the minimizing one second. These codes must */ |
| 160 |
|
|
OP_TYPEMINPLUS, /* be in exactly the same order as those above. */ |
| 161 |
|
|
OP_TYPEQUERY, /* This set applies to character types such as \d */ |
| 162 |
|
|
OP_TYPEMINQUERY, |
| 163 |
|
|
OP_TYPEUPTO, /* From 0 to n matches */ |
| 164 |
|
|
OP_TYPEMINUPTO, |
| 165 |
|
|
OP_TYPEEXACT, /* Exactly n matches */ |
| 166 |
|
|
|
| 167 |
|
|
OP_CRSTAR, /* The maximizing and minimizing versions of */ |
| 168 |
|
|
OP_CRMINSTAR, /* all these opcodes must come in pairs, with */ |
| 169 |
|
|
OP_CRPLUS, /* the minimizing one second. These codes must */ |
| 170 |
|
|
OP_CRMINPLUS, /* be in exactly the same order as those above. */ |
| 171 |
|
|
OP_CRQUERY, /* These are for character classes and back refs */ |
| 172 |
|
|
OP_CRMINQUERY, |
| 173 |
|
|
OP_CRRANGE, /* These are different to the three seta above. */ |
| 174 |
|
|
OP_CRMINRANGE, |
| 175 |
|
|
|
| 176 |
|
|
OP_CLASS, /* Match a character class */ |
| 177 |
|
|
OP_REF, /* Match a back reference */ |
| 178 |
|
|
|
| 179 |
|
|
OP_ALT, /* Start of alternation */ |
| 180 |
|
|
OP_KET, /* End of group that doesn't have an unbounded repeat */ |
| 181 |
|
|
OP_KETRMAX, /* These two must remain together and in this */ |
| 182 |
|
|
OP_KETRMIN, /* order. They are for groups the repeat for ever. */ |
| 183 |
|
|
|
| 184 |
|
|
OP_ASSERT, |
| 185 |
|
|
OP_ASSERT_NOT, |
| 186 |
|
|
OP_ONCE, /* Once matched, don't back up into the subpattern */ |
| 187 |
|
|
|
| 188 |
|
|
OP_BRAZERO, /* These two must remain together and in this */ |
| 189 |
|
|
OP_BRAMINZERO, /* order. */ |
| 190 |
|
|
|
| 191 |
|
|
OP_BRA /* This and greater values are used for brackets that |
| 192 |
|
|
extract substrings. */ |
| 193 |
|
|
}; |
| 194 |
|
|
|
| 195 |
|
|
/* The highest extraction number. This is limited by the number of opcodes |
| 196 |
|
|
left after OP_BRA, i.e. 255 - OP_BRA. We actually set it somewhat lower. */ |
| 197 |
|
|
|
| 198 |
|
|
#define EXTRACT_MAX 99 |
| 199 |
|
|
|
| 200 |
|
|
/* The texts of compile-time error messages are defined as macros here so that |
| 201 |
|
|
they can be accessed by the POSIX wrapper and converted into error codes. Yes, |
| 202 |
|
|
I could have used error codes in the first place, but didn't feel like changing |
| 203 |
|
|
just to accommodate the POSIX wrapper. */ |
| 204 |
|
|
|
| 205 |
|
|
#define ERR1 "\\ at end of pattern" |
| 206 |
|
|
#define ERR2 "\\c at end of pattern" |
| 207 |
|
|
#define ERR3 "unrecognized character follows \\" |
| 208 |
|
|
#define ERR4 "numbers out of order in {} quantifier" |
| 209 |
|
|
#define ERR5 "number too big in {} quantifier" |
| 210 |
|
|
#define ERR6 "missing terminating ] for character class" |
| 211 |
|
|
#define ERR7 "invalid escape sequence in character class" |
| 212 |
|
|
#define ERR8 "range out of order in character class" |
| 213 |
|
|
#define ERR9 "nothing to repeat" |
| 214 |
|
|
#define ERR10 "operand of unlimited repeat could match the empty string" |
| 215 |
|
|
#define ERR11 "internal error: unexpected repeat" |
| 216 |
|
|
#define ERR12 "unrecognized character after (?" |
| 217 |
|
|
#define ERR13 "too many capturing parenthesized sub-patterns" |
| 218 |
|
|
#define ERR14 "missing )" |
| 219 |
|
|
#define ERR15 "back reference to non-existent subpattern" |
| 220 |
|
|
#define ERR16 "erroffset passed as NULL" |
| 221 |
|
|
#define ERR17 "unknown option bit(s) set" |
| 222 |
|
|
#define ERR18 "missing ) after comment" |
| 223 |
|
|
#define ERR19 "too many sets of parentheses" |
| 224 |
|
|
#define ERR20 "regular expression too large" |
| 225 |
|
|
#define ERR21 "failed to get memory" |
| 226 |
|
|
#define ERR22 "unmatched brackets" |
| 227 |
|
|
#define ERR23 "internal error: code overflow" |
| 228 |
|
|
|
| 229 |
|
|
/* All character handling must be done as unsigned characters. Otherwise there |
| 230 |
|
|
are problems with top-bit-set characters and functions such as isspace(). |
| 231 |
|
|
However, we leave the interface to the outside world as char *, because that |
| 232 |
|
|
should make things easier for callers. We define a short type for unsigned char |
| 233 |
|
|
to save lots of typing. I tried "uchar", but it causes problems on Digital |
| 234 |
|
|
Unix, where it is defined in sys/types, so use "uschar" instead. */ |
| 235 |
|
|
|
| 236 |
|
|
typedef unsigned char uschar; |
| 237 |
|
|
|
| 238 |
|
|
/* The real format of the start of the pcre block; the actual code vector |
| 239 |
|
|
runs on as long as necessary after the end. */ |
| 240 |
|
|
|
| 241 |
|
|
typedef struct real_pcre { |
| 242 |
|
|
unsigned int magic_number; |
| 243 |
|
|
unsigned short int options; |
| 244 |
|
|
unsigned char top_bracket; |
| 245 |
|
|
unsigned char top_backref; |
| 246 |
|
|
unsigned char first_char; |
| 247 |
|
|
unsigned char code[1]; |
| 248 |
|
|
} real_pcre; |
| 249 |
|
|
|
| 250 |
|
|
/* The real format of the extra block returned by pcre_study(). */ |
| 251 |
|
|
|
| 252 |
|
|
typedef struct real_pcre_extra { |
| 253 |
|
|
unsigned char options; |
| 254 |
|
|
unsigned char start_bits[32]; |
| 255 |
|
|
} real_pcre_extra; |
| 256 |
|
|
|
| 257 |
|
|
/* Global tables from chartables.c */ |
| 258 |
|
|
|
| 259 |
|
|
extern uschar pcre_lcc[]; |
| 260 |
|
|
extern uschar pcre_fcc[]; |
| 261 |
|
|
extern uschar pcre_cbits[]; |
| 262 |
|
|
extern uschar pcre_ctypes[]; |
| 263 |
|
|
|
| 264 |
|
|
/* Bit definitions for entries in pcre_ctypes[]. */ |
| 265 |
|
|
|
| 266 |
|
|
#define ctype_space 0x01 |
| 267 |
|
|
#define ctype_letter 0x02 |
| 268 |
|
|
#define ctype_digit 0x04 |
| 269 |
|
|
#define ctype_xdigit 0x08 |
| 270 |
|
|
#define ctype_word 0x10 /* alphameric or '_' */ |
| 271 |
|
|
#define ctype_meta 0x80 /* regexp meta char or zero (end pattern) */ |
| 272 |
|
|
|
| 273 |
|
|
/* Offsets for the bitmap tables */ |
| 274 |
|
|
|
| 275 |
|
|
#define cbit_digit 0 |
| 276 |
|
|
#define cbit_letter 32 |
| 277 |
|
|
#define cbit_word 64 |
| 278 |
|
|
#define cbit_space 96 |
| 279 |
|
|
#define cbit_length 128 /* Length of the cbits table */ |
| 280 |
|
|
|
| 281 |
|
|
/* End of internal.h */ |