1
/*************************************************
2
* Perl-Compatible Regular Expressions *
3
*************************************************/
6
/* This is a library of functions to support regular expressions whose syntax
7
and semantics are as close as possible to those of the Perl 5 language. See
8
the file Tech.Notes for some information on the internals.
10
Written by: Philip Hazel <ph10@cam.ac.uk>
12
Copyright (c) 1997-2001 University of Cambridge
14
-----------------------------------------------------------------------------
15
Permission is granted to anyone to use this software for any purpose on any
16
computer system, and to redistribute it freely, subject to the following
19
1. This software is distributed in the hope that it will be useful,
20
but WITHOUT ANY WARRANTY; without even the implied warranty of
21
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
23
2. The origin of this software must not be misrepresented, either by
24
explicit claim or by omission.
26
3. Altered versions must be plainly marked as such, and must not be
27
misrepresented as being the original software.
29
4. If PCRE is embedded in any software that is released under the GNU
30
General Purpose Licence (GPL), then the terms of that licence shall
31
supersede any condition above with which it is incompatible.
32
-----------------------------------------------------------------------------
35
/* This header contains definitions that are shared between the different
36
modules, but which are not relevant to the outside. */
38
/* Get the definitions provided by running "configure" */
45
/* To cope with SunOS4 and other systems that lack memmove() but have bcopy(),
46
define a macro for memmove() if HAVE_MEMMOVE is false, provided that HAVE_BCOPY
47
is set. Otherwise, include an emulating function for those systems that have
48
neither (there some non-Unix environments where this is the case). This assumes
49
that all calls to memmove are moving strings upwards in store, which is the
53
#undef memmove /* some systems may have a macro */
55
#define memmove(a, b, c) bcopy(b, a, c)
58
pcre_memmove(unsigned char *dest, const unsigned char *src, size_t n)
63
for (i = 0; i < n; ++i) *(--dest) = *(--src);
65
#define memmove(a, b, c) pcre_memmove(a, b, c)
69
/* Standard C headers plus the external interface definition */
79
/* In case there is no definition of offsetof() provided - though any proper
80
Standard C system should have one. */
83
#define offsetof(p_type,field) ((size_t)&(((p_type *)0)->field))
86
/* These are the public options that can change during matching. */
88
#define PCRE_IMS (PCRE_CASELESS|PCRE_MULTILINE|PCRE_DOTALL)
90
/* Private options flags start at the most significant end of the four bytes,
91
but skip the top bit so we can use ints for convenience without getting tangled
92
with negative values. The public options defined in pcre.h start at the least
93
significant end. Make sure they don't overlap, though now that we have expanded
94
to four bytes there is plenty of space. */
96
#define PCRE_FIRSTSET 0x40000000 /* first_char is set */
97
#define PCRE_REQCHSET 0x20000000 /* req_char is set */
98
#define PCRE_STARTLINE 0x10000000 /* start after \n for multiline */
99
#define PCRE_INGROUP 0x08000000 /* compiling inside a group */
100
#define PCRE_ICHANGED 0x04000000 /* i option changes within regex */
102
/* Options for the "extra" block produced by pcre_study(). */
104
#define PCRE_STUDY_MAPPED 0x01 /* a map of starting chars exists */
106
/* Masks for identifying the public options which are permitted at compile
107
time, run time or study time, respectively. */
109
#define PUBLIC_OPTIONS \
110
(PCRE_CASELESS|PCRE_EXTENDED|PCRE_ANCHORED|PCRE_MULTILINE| \
111
PCRE_DOTALL|PCRE_DOLLAR_ENDONLY|PCRE_EXTRA|PCRE_UNGREEDY|PCRE_UTF8)
113
#define PUBLIC_EXEC_OPTIONS \
114
(PCRE_ANCHORED|PCRE_NOTBOL|PCRE_NOTEOL|PCRE_NOTEMPTY)
116
#define PUBLIC_STUDY_OPTIONS 0 /* None defined */
118
/* Magic number to provide a small check against being handed junk. */
120
#define MAGIC_NUMBER 0x50435245UL /* 'PCRE' */
122
/* Miscellaneous definitions */
129
/* Escape items that are just an encoding of a particular data value. Note that
130
ESC_N is defined as yet another macro, which is set in config.h to either \n
131
(the default) or \r (which some people want). */
142
#define ESC_N NEWLINE
153
/* These are escaped items that aren't just an encoding of a particular data
154
value such as \n. They must have non-zero values, as check_escape() returns
155
their negation. Also, they must appear in the same order as in the opcode
156
definitions below, up to ESC_z. The final one must be ESC_REF as subsequent
157
values are used for \1, \2, \3, etc. There is a test in the code for an escape
158
greater than ESC_b and less than ESC_Z to detect the types that may be
159
repeated. If any new escapes are put in-between that don't consume a character,
160
that code will have to change. */
162
enum { ESC_A = 1, ESC_B, ESC_b, ESC_D, ESC_d, ESC_S, ESC_s, ESC_W, ESC_w,
163
ESC_Z, ESC_z, ESC_REF };
165
/* Opcode table: OP_BRA must be last, as all values >= it are used for brackets
166
that extract substrings. Starting from 1 (i.e. after OP_END), the values up to
167
OP_EOD must correspond in order to the list of escapes immediately above. */
170
OP_END, /* End of pattern */
172
/* Values corresponding to backslashed metacharacters */
174
OP_SOD, /* Start of data: \A */
175
OP_NOT_WORD_BOUNDARY, /* \B */
176
OP_WORD_BOUNDARY, /* \b */
177
OP_NOT_DIGIT, /* \D */
179
OP_NOT_WHITESPACE, /* \S */
180
OP_WHITESPACE, /* \s */
181
OP_NOT_WORDCHAR, /* \W */
182
OP_WORDCHAR, /* \w */
183
OP_EODN, /* End of data or \n at end of data: \Z. */
184
OP_EOD, /* End of data: \z */
186
OP_OPT, /* Set runtime options */
187
OP_CIRC, /* Start of line - varies with multiline switch */
188
OP_DOLL, /* End of line - varies with multiline switch */
189
OP_ANY, /* Match any character */
190
OP_CHARS, /* Match string of characters */
191
OP_NOT, /* Match anything but the following char */
193
OP_STAR, /* The maximizing and minimizing versions of */
194
OP_MINSTAR, /* all these opcodes must come in pairs, with */
195
OP_PLUS, /* the minimizing one second. */
196
OP_MINPLUS, /* This first set applies to single characters */
199
OP_UPTO, /* From 0 to n matches */
201
OP_EXACT, /* Exactly n matches */
203
OP_NOTSTAR, /* The maximizing and minimizing versions of */
204
OP_NOTMINSTAR, /* all these opcodes must come in pairs, with */
205
OP_NOTPLUS, /* the minimizing one second. */
206
OP_NOTMINPLUS, /* This first set applies to "not" single characters */
209
OP_NOTUPTO, /* From 0 to n matches */
211
OP_NOTEXACT, /* Exactly n matches */
213
OP_TYPESTAR, /* The maximizing and minimizing versions of */
214
OP_TYPEMINSTAR, /* all these opcodes must come in pairs, with */
215
OP_TYPEPLUS, /* the minimizing one second. These codes must */
216
OP_TYPEMINPLUS, /* be in exactly the same order as those above. */
217
OP_TYPEQUERY, /* This set applies to character types such as \d */
219
OP_TYPEUPTO, /* From 0 to n matches */
221
OP_TYPEEXACT, /* Exactly n matches */
223
OP_CRSTAR, /* The maximizing and minimizing versions of */
224
OP_CRMINSTAR, /* all these opcodes must come in pairs, with */
225
OP_CRPLUS, /* the minimizing one second. These codes must */
226
OP_CRMINPLUS, /* be in exactly the same order as those above. */
227
OP_CRQUERY, /* These are for character classes and back refs */
229
OP_CRRANGE, /* These are different to the three seta above. */
232
OP_CLASS, /* Match a character class */
233
OP_REF, /* Match a back reference */
234
OP_RECURSE, /* Match this pattern recursively */
236
OP_ALT, /* Start of alternation */
237
OP_KET, /* End of group that doesn't have an unbounded repeat */
238
OP_KETRMAX, /* These two must remain together and in this */
239
OP_KETRMIN, /* order. They are for groups the repeat for ever. */
241
/* The assertions must come before ONCE and COND */
243
OP_ASSERT, /* Positive lookahead */
244
OP_ASSERT_NOT, /* Negative lookahead */
245
OP_ASSERTBACK, /* Positive lookbehind */
246
OP_ASSERTBACK_NOT, /* Negative lookbehind */
247
OP_REVERSE, /* Move pointer back - used in lookbehind assertions */
249
/* ONCE and COND must come after the assertions, with ONCE first, as there's
250
a test for >= ONCE for a subpattern that isn't an assertion. */
252
OP_ONCE, /* Once matched, don't back up into the subpattern */
253
OP_COND, /* Conditional group */
254
OP_CREF, /* Used to hold an extraction string number (cond ref) */
256
OP_BRAZERO, /* These two must remain together and in this */
257
OP_BRAMINZERO, /* order. */
259
OP_BRANUMBER, /* Used for extracting brackets whose number is greater
260
than can fit into an opcode. */
262
OP_BRA /* This and greater values are used for brackets that
263
extract substrings up to a basic limit. After that,
264
use is made of OP_BRANUMBER. */
267
/* The highest extraction number before we have to start using additional
268
bytes. (Originally PCRE didn't have support for extraction counts highter than
269
this number.) The value is limited by the number of opcodes left after OP_BRA,
270
i.e. 255 - OP_BRA. We actually set it a bit lower to leave room for additional
273
#define EXTRACT_BASIC_MAX 150
275
/* The texts of compile-time error messages are defined as macros here so that
276
they can be accessed by the POSIX wrapper and converted into error codes. Yes,
277
I could have used error codes in the first place, but didn't feel like changing
278
just to accommodate the POSIX wrapper. */
280
#define ERR1 "\\ at end of pattern"
281
#define ERR2 "\\c at end of pattern"
282
#define ERR3 "unrecognized character follows \\"
283
#define ERR4 "numbers out of order in {} quantifier"
284
#define ERR5 "number too big in {} quantifier"
285
#define ERR6 "missing terminating ] for character class"
286
#define ERR7 "invalid escape sequence in character class"
287
#define ERR8 "range out of order in character class"
288
#define ERR9 "nothing to repeat"
289
#define ERR10 "operand of unlimited repeat could match the empty string"
290
#define ERR11 "internal error: unexpected repeat"
291
#define ERR12 "unrecognized character after (?"
292
#define ERR13 "unused error"
293
#define ERR14 "missing )"
294
#define ERR15 "back reference to non-existent subpattern"
295
#define ERR16 "erroffset passed as NULL"
296
#define ERR17 "unknown option bit(s) set"
297
#define ERR18 "missing ) after comment"
298
#define ERR19 "parentheses nested too deeply"
299
#define ERR20 "regular expression too large"
300
#define ERR21 "failed to get memory"
301
#define ERR22 "unmatched parentheses"
302
#define ERR23 "internal error: code overflow"
303
#define ERR24 "unrecognized character after (?<"
304
#define ERR25 "lookbehind assertion is not fixed length"
305
#define ERR26 "malformed number after (?("
306
#define ERR27 "conditional group contains more than two branches"
307
#define ERR28 "assertion expected after (?("
308
#define ERR29 "(?p must be followed by )"
309
#define ERR30 "unknown POSIX class name"
310
#define ERR31 "POSIX collating elements are not supported"
311
#define ERR32 "this version of PCRE is not compiled with PCRE_UTF8 support"
312
#define ERR33 "characters with values > 255 are not yet supported in classes"
313
#define ERR34 "character value in \\x{...} sequence is too large"
314
#define ERR35 "invalid condition (?(0)"
316
/* All character handling must be done as unsigned characters. Otherwise there
317
are problems with top-bit-set characters and functions such as isspace().
318
However, we leave the interface to the outside world as char *, because that
319
should make things easier for callers. We define a short type for unsigned char
320
to save lots of typing. I tried "uchar", but it causes problems on Digital
321
Unix, where it is defined in sys/types, so use "uschar" instead. */
323
typedef unsigned char uschar;
325
/* The real format of the start of the pcre block; the actual code vector
326
runs on as long as necessary after the end. */
328
typedef struct real_pcre {
329
unsigned long int magic_number;
331
const unsigned char *tables;
332
unsigned long int options;
333
unsigned short int top_bracket;
334
unsigned short int top_backref;
340
/* The real format of the extra block returned by pcre_study(). */
342
typedef struct real_pcre_extra {
344
uschar start_bits[32];
348
/* Structure for passing "static" information around between the functions
349
doing the compiling, so that they are thread-safe. */
351
typedef struct compile_data {
352
const uschar *lcc; /* Points to lower casing table */
353
const uschar *fcc; /* Points to case-flipping table */
354
const uschar *cbits; /* Points to character type table */
355
const uschar *ctypes; /* Points to table of type maps */
358
/* Structure for passing "static" information around between the functions
359
doing the matching, so that they are thread-safe. */
361
typedef struct match_data {
362
int errorcode; /* As it says */
363
int *offset_vector; /* Offset vector */
364
int offset_end; /* One past the end */
365
int offset_max; /* The maximum usable for return data */
366
const uschar *lcc; /* Points to lower casing table */
367
const uschar *ctypes; /* Points to table of type maps */
368
BOOL offset_overflow; /* Set if too many extractions */
369
BOOL notbol; /* NOTBOL flag */
370
BOOL noteol; /* NOTEOL flag */
371
BOOL utf8; /* UTF8 flag */
372
BOOL endonly; /* Dollar not before final \n */
373
BOOL notempty; /* Empty string match not wanted */
374
const uschar *start_pattern; /* For use when recursing */
375
const uschar *start_subject; /* Start of the subject string */
376
const uschar *end_subject; /* End of the subject string */
377
const uschar *start_match; /* Start of this match attempt */
378
const uschar *end_match_ptr; /* Subject position at end match */
379
int end_offset_top; /* Highwater mark at end of match */
382
/* Bit definitions for entries in the pcre_ctypes table. */
384
#define ctype_space 0x01
385
#define ctype_letter 0x02
386
#define ctype_digit 0x04
387
#define ctype_xdigit 0x08
388
#define ctype_word 0x10 /* alphameric or '_' */
389
#define ctype_meta 0x80 /* regexp meta char or zero (end pattern) */
391
/* Offsets for the bitmap tables in pcre_cbits. Each table contains a set
392
of bits for a class map. Some classes are built by combining these tables. */
394
#define cbit_space 0 /* [:space:] or \s */
395
#define cbit_xdigit 32 /* [:xdigit:] */
396
#define cbit_digit 64 /* [:digit:] or \d */
397
#define cbit_upper 96 /* [:upper:] */
398
#define cbit_lower 128 /* [:lower:] */
399
#define cbit_word 160 /* [:word:] or \w */
400
#define cbit_graph 192 /* [:graph:] */
401
#define cbit_print 224 /* [:print:] */
402
#define cbit_punct 256 /* [:punct:] */
403
#define cbit_cntrl 288 /* [:cntrl:] */
404
#define cbit_length 320 /* Length of the cbits table */
406
/* Offsets of the various tables from the base tables pointer, and
410
#define fcc_offset 256
411
#define cbits_offset 512
412
#define ctypes_offset (cbits_offset + cbit_length)
413
#define tables_length (ctypes_offset + 256)
415
/* End of internal.h */