| 3 |
*************************************************/ |
*************************************************/ |
| 4 |
|
|
| 5 |
|
|
|
#define PCRE_VERSION "2.01 21-Oct-1998" |
|
|
|
|
|
|
|
| 6 |
/* This is a library of functions to support regular expressions whose syntax |
/* 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 |
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. |
the file Tech.Notes for some information on the internals. |
| 9 |
|
|
| 10 |
Written by: Philip Hazel <ph10@cam.ac.uk> |
Written by: Philip Hazel <ph10@cam.ac.uk> |
| 11 |
|
|
| 12 |
Copyright (c) 1998 University of Cambridge |
Copyright (c) 1997-2003 University of Cambridge |
| 13 |
|
|
| 14 |
----------------------------------------------------------------------------- |
----------------------------------------------------------------------------- |
| 15 |
Permission is granted to anyone to use this software for any purpose on any |
Permission is granted to anyone to use this software for any purpose on any |
| 25 |
|
|
| 26 |
3. Altered versions must be plainly marked as such, and must not be |
3. Altered versions must be plainly marked as such, and must not be |
| 27 |
misrepresented as being the original software. |
misrepresented as being the original software. |
| 28 |
|
|
| 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 |
----------------------------------------------------------------------------- |
----------------------------------------------------------------------------- |
| 33 |
*/ |
*/ |
| 34 |
|
|
| 35 |
/* This header contains definitions that are shared between the different |
/* This header contains definitions that are shared between the different |
| 36 |
modules, but which are not relevant to the outside. */ |
modules, but which are not relevant to the outside. */ |
| 37 |
|
|
| 38 |
|
/* Get the definitions provided by running "configure" */ |
| 39 |
|
|
| 40 |
|
#include "config.h" |
| 41 |
|
|
| 42 |
|
/* When compiling for use with the Virtual Pascal compiler, these functions |
| 43 |
|
need to have their names changed. PCRE must be compiled with the -DVPCOMPAT |
| 44 |
|
option on the command line. */ |
| 45 |
|
|
| 46 |
|
#ifdef VPCOMPAT |
| 47 |
|
#define strncmp(s1,s2,m) _strncmp(s1,s2,m) |
| 48 |
|
#define memcpy(d,s,n) _memcpy(d,s,n) |
| 49 |
|
#define memmove(d,s,n) _memmove(d,s,n) |
| 50 |
|
#define memset(s,c,n) _memset(s,c,n) |
| 51 |
|
#else /* VPCOMPAT */ |
| 52 |
|
|
| 53 |
/* To cope with SunOS4 and other systems that lack memmove() but have bcopy(), |
/* To cope with SunOS4 and other systems that lack memmove() but have bcopy(), |
| 54 |
define a macro for memmove() if USE_BCOPY is defined. */ |
define a macro for memmove() if HAVE_MEMMOVE is false, provided that HAVE_BCOPY |
| 55 |
|
is set. Otherwise, include an emulating function for those systems that have |
| 56 |
|
neither (there some non-Unix environments where this is the case). This assumes |
| 57 |
|
that all calls to memmove are moving strings upwards in store, which is the |
| 58 |
|
case in PCRE. */ |
| 59 |
|
|
| 60 |
#ifdef USE_BCOPY |
#if ! HAVE_MEMMOVE |
| 61 |
#undef memmove /* some systems may have a macro */ |
#undef memmove /* some systems may have a macro */ |
| 62 |
|
#if HAVE_BCOPY |
| 63 |
#define memmove(a, b, c) bcopy(b, a, c) |
#define memmove(a, b, c) bcopy(b, a, c) |
| 64 |
|
#else /* HAVE_BCOPY */ |
| 65 |
|
void * |
| 66 |
|
pcre_memmove(unsigned char *dest, const unsigned char *src, size_t n) |
| 67 |
|
{ |
| 68 |
|
int i; |
| 69 |
|
dest += n; |
| 70 |
|
src += n; |
| 71 |
|
for (i = 0; i < n; ++i) *(--dest) = *(--src); |
| 72 |
|
} |
| 73 |
|
#define memmove(a, b, c) pcre_memmove(a, b, c) |
| 74 |
|
#endif /* not HAVE_BCOPY */ |
| 75 |
|
#endif /* not HAVE_MEMMOVE */ |
| 76 |
|
#endif /* not VPCOMPAT */ |
| 77 |
|
|
| 78 |
|
|
| 79 |
|
/* PCRE keeps offsets in its compiled code as 2-byte quantities by default. |
| 80 |
|
These are used, for example, to link from the start of a subpattern to its |
| 81 |
|
alternatives and its end. The use of 2 bytes per offset limits the size of the |
| 82 |
|
compiled regex to around 64K, which is big enough for almost everybody. |
| 83 |
|
However, I received a request for an even bigger limit. For this reason, and |
| 84 |
|
also to make the code easier to maintain, the storing and loading of offsets |
| 85 |
|
from the byte string is now handled by the macros that are defined here. |
| 86 |
|
|
| 87 |
|
The macros are controlled by the value of LINK_SIZE. This defaults to 2 in |
| 88 |
|
the config.h file, but can be overridden by using -D on the command line. This |
| 89 |
|
is automated on Unix systems via the "configure" command. */ |
| 90 |
|
|
| 91 |
|
#if LINK_SIZE == 2 |
| 92 |
|
|
| 93 |
|
#define PUT(a,n,d) \ |
| 94 |
|
(a[n] = (d) >> 8), \ |
| 95 |
|
(a[(n)+1] = (d) & 255) |
| 96 |
|
|
| 97 |
|
#define GET(a,n) \ |
| 98 |
|
(((a)[n] << 8) | (a)[(n)+1]) |
| 99 |
|
|
| 100 |
|
#define MAX_PATTERN_SIZE (1 << 16) |
| 101 |
|
|
| 102 |
|
|
| 103 |
|
#elif LINK_SIZE == 3 |
| 104 |
|
|
| 105 |
|
#define PUT(a,n,d) \ |
| 106 |
|
(a[n] = (d) >> 16), \ |
| 107 |
|
(a[(n)+1] = (d) >> 8), \ |
| 108 |
|
(a[(n)+2] = (d) & 255) |
| 109 |
|
|
| 110 |
|
#define GET(a,n) \ |
| 111 |
|
(((a)[n] << 16) | ((a)[(n)+1] << 8) | (a)[(n)+2]) |
| 112 |
|
|
| 113 |
|
#define MAX_PATTERN_SIZE (1 << 24) |
| 114 |
|
|
| 115 |
|
|
| 116 |
|
#elif LINK_SIZE == 4 |
| 117 |
|
|
| 118 |
|
#define PUT(a,n,d) \ |
| 119 |
|
(a[n] = (d) >> 24), \ |
| 120 |
|
(a[(n)+1] = (d) >> 16), \ |
| 121 |
|
(a[(n)+2] = (d) >> 8), \ |
| 122 |
|
(a[(n)+3] = (d) & 255) |
| 123 |
|
|
| 124 |
|
#define GET(a,n) \ |
| 125 |
|
(((a)[n] << 24) | ((a)[(n)+1] << 16) | ((a)[(n)+2] << 8) | (a)[(n)+3]) |
| 126 |
|
|
| 127 |
|
#define MAX_PATTERN_SIZE (1 << 30) /* Keep it positive */ |
| 128 |
|
|
| 129 |
|
|
| 130 |
|
#else |
| 131 |
|
#error LINK_SIZE must be either 2, 3, or 4 |
| 132 |
#endif |
#endif |
| 133 |
|
|
| 134 |
|
|
| 135 |
|
/* Convenience macro defined in terms of the others */ |
| 136 |
|
|
| 137 |
|
#define PUTINC(a,n,d) PUT(a,n,d), a += LINK_SIZE |
| 138 |
|
|
| 139 |
|
|
| 140 |
|
/* PCRE uses some other 2-byte quantities that do not change when the size of |
| 141 |
|
offsets changes. There are used for repeat counts and for other things such as |
| 142 |
|
capturing parenthesis numbers in back references. */ |
| 143 |
|
|
| 144 |
|
#define PUT2(a,n,d) \ |
| 145 |
|
a[n] = (d) >> 8; \ |
| 146 |
|
a[(n)+1] = (d) & 255 |
| 147 |
|
|
| 148 |
|
#define GET2(a,n) \ |
| 149 |
|
(((a)[n] << 8) | (a)[(n)+1]) |
| 150 |
|
|
| 151 |
|
#define PUT2INC(a,n,d) PUT2(a,n,d), a += 2 |
| 152 |
|
|
| 153 |
|
|
| 154 |
/* Standard C headers plus the external interface definition */ |
/* Standard C headers plus the external interface definition */ |
| 155 |
|
|
| 156 |
#include <ctype.h> |
#include <ctype.h> |
| 159 |
#include <stdio.h> |
#include <stdio.h> |
| 160 |
#include <stdlib.h> |
#include <stdlib.h> |
| 161 |
#include <string.h> |
#include <string.h> |
| 162 |
|
|
| 163 |
|
#ifndef PCRE_SPY |
| 164 |
|
#define PCRE_DEFINITION /* Win32 __declspec(export) trigger for .dll */ |
| 165 |
|
#endif |
| 166 |
|
|
| 167 |
#include "pcre.h" |
#include "pcre.h" |
| 168 |
|
|
| 169 |
/* In case there is no definition of offsetof() provided - though any proper |
/* In case there is no definition of offsetof() provided - though any proper |
| 177 |
|
|
| 178 |
#define PCRE_IMS (PCRE_CASELESS|PCRE_MULTILINE|PCRE_DOTALL) |
#define PCRE_IMS (PCRE_CASELESS|PCRE_MULTILINE|PCRE_DOTALL) |
| 179 |
|
|
| 180 |
/* Private options flags start at the most significant end of the two bytes. |
/* Private options flags start at the most significant end of the four bytes, |
| 181 |
The public options defined in pcre.h start at the least significant end. Make |
but skip the top bit so we can use ints for convenience without getting tangled |
| 182 |
sure they don't overlap! */ |
with negative values. The public options defined in pcre.h start at the least |
| 183 |
|
significant end. Make sure they don't overlap, though now that we have expanded |
| 184 |
#define PCRE_FIRSTSET 0x8000 /* first_char is set */ |
to four bytes there is plenty of space. */ |
| 185 |
#define PCRE_STARTLINE 0x4000 /* start after \n for multiline */ |
|
| 186 |
#define PCRE_INGROUP 0x2000 /* compiling inside a group */ |
#define PCRE_FIRSTSET 0x40000000 /* first_byte is set */ |
| 187 |
|
#define PCRE_REQCHSET 0x20000000 /* req_byte is set */ |
| 188 |
|
#define PCRE_STARTLINE 0x10000000 /* start after \n for multiline */ |
| 189 |
|
#define PCRE_ICHANGED 0x08000000 /* i option changes within regex */ |
| 190 |
|
|
| 191 |
/* Options for the "extra" block produced by pcre_study(). */ |
/* Options for the "extra" block produced by pcre_study(). */ |
| 192 |
|
|
| 197 |
|
|
| 198 |
#define PUBLIC_OPTIONS \ |
#define PUBLIC_OPTIONS \ |
| 199 |
(PCRE_CASELESS|PCRE_EXTENDED|PCRE_ANCHORED|PCRE_MULTILINE| \ |
(PCRE_CASELESS|PCRE_EXTENDED|PCRE_ANCHORED|PCRE_MULTILINE| \ |
| 200 |
PCRE_DOTALL|PCRE_DOLLAR_ENDONLY|PCRE_EXTRA|PCRE_UNGREEDY) |
PCRE_DOTALL|PCRE_DOLLAR_ENDONLY|PCRE_EXTRA|PCRE_UNGREEDY|PCRE_UTF8| \ |
| 201 |
|
PCRE_NO_AUTO_CAPTURE) |
| 202 |
|
|
| 203 |
#define PUBLIC_EXEC_OPTIONS (PCRE_ANCHORED|PCRE_NOTBOL|PCRE_NOTEOL) |
#define PUBLIC_EXEC_OPTIONS \ |
| 204 |
|
(PCRE_ANCHORED|PCRE_NOTBOL|PCRE_NOTEOL|PCRE_NOTEMPTY) |
| 205 |
|
|
| 206 |
#define PUBLIC_STUDY_OPTIONS 0 /* None defined */ |
#define PUBLIC_STUDY_OPTIONS 0 /* None defined */ |
| 207 |
|
|
| 208 |
/* Magic number to provide a small check against being handed junk. */ |
/* Magic number to provide a small check against being handed junk. */ |
| 209 |
|
|
| 210 |
#define MAGIC_NUMBER 0x50435245 /* 'PCRE' */ |
#define MAGIC_NUMBER 0x50435245UL /* 'PCRE' */ |
| 211 |
|
|
| 212 |
|
/* Negative values for the firstchar and reqchar variables */ |
| 213 |
|
|
| 214 |
|
#define REQ_UNSET (-2) |
| 215 |
|
#define REQ_NONE (-1) |
| 216 |
|
|
| 217 |
|
/* Flags added to firstbyte or reqbyte; a "non-literal" item is either a |
| 218 |
|
variable-length repeat, or a anything other than literal characters. */ |
| 219 |
|
|
| 220 |
|
#define REQ_CASELESS 0x0100 /* indicates caselessness */ |
| 221 |
|
#define REQ_VARY 0x0200 /* reqbyte followed non-literal item */ |
| 222 |
|
|
| 223 |
/* Miscellaneous definitions */ |
/* Miscellaneous definitions */ |
| 224 |
|
|
| 227 |
#define FALSE 0 |
#define FALSE 0 |
| 228 |
#define TRUE 1 |
#define TRUE 1 |
| 229 |
|
|
| 230 |
|
/* Escape items that are just an encoding of a particular data value. Note that |
| 231 |
|
ESC_n is defined as yet another macro, which is set in config.h to either \n |
| 232 |
|
(the default) or \r (which some people want). */ |
| 233 |
|
|
| 234 |
|
#ifndef ESC_e |
| 235 |
|
#define ESC_e 27 |
| 236 |
|
#endif |
| 237 |
|
|
| 238 |
|
#ifndef ESC_f |
| 239 |
|
#define ESC_f '\f' |
| 240 |
|
#endif |
| 241 |
|
|
| 242 |
|
#ifndef ESC_n |
| 243 |
|
#define ESC_n NEWLINE |
| 244 |
|
#endif |
| 245 |
|
|
| 246 |
|
#ifndef ESC_r |
| 247 |
|
#define ESC_r '\r' |
| 248 |
|
#endif |
| 249 |
|
|
| 250 |
|
#ifndef ESC_t |
| 251 |
|
#define ESC_t '\t' |
| 252 |
|
#endif |
| 253 |
|
|
| 254 |
/* These are escaped items that aren't just an encoding of a particular data |
/* These are escaped items that aren't just an encoding of a particular data |
| 255 |
value such as \n. They must have non-zero values, as check_escape() returns |
value such as \n. They must have non-zero values, as check_escape() returns |
| 256 |
their negation. Also, they must appear in the same order as in the opcode |
their negation. Also, they must appear in the same order as in the opcode |
| 257 |
definitions below, up to ESC_z. The final one must be ESC_REF as subsequent |
definitions below, up to ESC_z. There's a dummy for OP_ANY because it |
| 258 |
values are used for \1, \2, \3, etc. There is a test in the code for an escape |
corresponds to "." rather than an escape sequence. The final one must be |
| 259 |
greater than ESC_b and less than ESC_X to detect the types that may be |
ESC_REF as subsequent values are used for \1, \2, \3, etc. There is are two |
| 260 |
repeated. If any new escapes are put in-between that don't consume a character, |
tests in the code for an escape greater than ESC_b and less than ESC_Z to |
| 261 |
that code will have to change. */ |
detect the types that may be repeated. These are the types that consume a |
| 262 |
|
character. If any new escapes are put in between that don't consume a |
| 263 |
|
character, that code will have to change. */ |
| 264 |
|
|
| 265 |
|
enum { ESC_A = 1, ESC_G, ESC_B, ESC_b, ESC_D, ESC_d, ESC_S, ESC_s, ESC_W, |
| 266 |
|
ESC_w, ESC_dum1, ESC_C, ESC_Z, ESC_z, ESC_E, ESC_Q, ESC_REF }; |
| 267 |
|
|
| 268 |
|
/* Flag bits and data types for the extended class (OP_XCLASS) for classes that |
| 269 |
|
contain UTF-8 characters with values greater than 255. */ |
| 270 |
|
|
| 271 |
|
#define XCL_NOT 0x01 /* Flag: this is a negative class */ |
| 272 |
|
#define XCL_MAP 0x02 /* Flag: a 32-byte map is present */ |
| 273 |
|
|
| 274 |
|
#define XCL_END 0 /* Marks end of individual items */ |
| 275 |
|
#define XCL_SINGLE 1 /* Single item (one multibyte char) follows */ |
| 276 |
|
#define XCL_RANGE 2 /* A range (two multibyte chars) follows */ |
| 277 |
|
|
|
enum { ESC_A = 1, ESC_B, ESC_b, ESC_D, ESC_d, ESC_S, ESC_s, ESC_W, ESC_w, |
|
|
ESC_Z, ESC_z, ESC_REF }; |
|
| 278 |
|
|
| 279 |
/* Opcode table: OP_BRA must be last, as all values >= it are used for brackets |
/* Opcode table: OP_BRA must be last, as all values >= it are used for brackets |
| 280 |
that extract substrings. Starting from 1 (i.e. after OP_END), the values up to |
that extract substrings. Starting from 1 (i.e. after OP_END), the values up to |
| 281 |
OP_EOD must correspond in order to the list of escapes immediately above. */ |
OP_EOD must correspond in order to the list of escapes immediately above. |
| 282 |
|
Note that whenever this list is updated, the two macro definitions that follow |
| 283 |
|
must also be updated to match. */ |
| 284 |
|
|
| 285 |
enum { |
enum { |
| 286 |
OP_END, /* End of pattern */ |
OP_END, /* 0 End of pattern */ |
| 287 |
|
|
| 288 |
/* Values corresponding to backslashed metacharacters */ |
/* Values corresponding to backslashed metacharacters */ |
| 289 |
|
|
| 290 |
OP_SOD, /* Start of data: \A */ |
OP_SOD, /* 1 Start of data: \A */ |
| 291 |
OP_NOT_WORD_BOUNDARY, /* \B */ |
OP_SOM, /* 2 Start of match (subject + offset): \G */ |
| 292 |
OP_WORD_BOUNDARY, /* \b */ |
OP_NOT_WORD_BOUNDARY, /* 3 \B */ |
| 293 |
OP_NOT_DIGIT, /* \D */ |
OP_WORD_BOUNDARY, /* 4 \b */ |
| 294 |
OP_DIGIT, /* \d */ |
OP_NOT_DIGIT, /* 5 \D */ |
| 295 |
OP_NOT_WHITESPACE, /* \S */ |
OP_DIGIT, /* 6 \d */ |
| 296 |
OP_WHITESPACE, /* \s */ |
OP_NOT_WHITESPACE, /* 7 \S */ |
| 297 |
OP_NOT_WORDCHAR, /* \W */ |
OP_WHITESPACE, /* 8 \s */ |
| 298 |
OP_WORDCHAR, /* \w */ |
OP_NOT_WORDCHAR, /* 9 \W */ |
| 299 |
OP_EODN, /* End of data or \n at end of data: \Z. */ |
OP_WORDCHAR, /* 10 \w */ |
| 300 |
OP_EOD, /* End of data: \z */ |
OP_ANY, /* 11 Match any character */ |
| 301 |
|
OP_ANYBYTE, /* 12 Match any byte (\C); different to OP_ANY for UTF-8 */ |
| 302 |
OP_OPT, /* Set runtime options */ |
OP_EODN, /* 13 End of data or \n at end of data: \Z. */ |
| 303 |
OP_CIRC, /* Start of line - varies with multiline switch */ |
OP_EOD, /* 14 End of data: \z */ |
| 304 |
OP_DOLL, /* End of line - varies with multiline switch */ |
|
| 305 |
OP_ANY, /* Match any character */ |
OP_OPT, /* 15 Set runtime options */ |
| 306 |
OP_CHARS, /* Match string of characters */ |
OP_CIRC, /* 16 Start of line - varies with multiline switch */ |
| 307 |
OP_NOT, /* Match anything but the following char */ |
OP_DOLL, /* 17 End of line - varies with multiline switch */ |
| 308 |
|
OP_CHARS, /* 18 Match string of characters */ |
| 309 |
OP_STAR, /* The maximizing and minimizing versions of */ |
OP_NOT, /* 19 Match anything but the following char */ |
| 310 |
OP_MINSTAR, /* all these opcodes must come in pairs, with */ |
|
| 311 |
OP_PLUS, /* the minimizing one second. */ |
OP_STAR, /* 20 The maximizing and minimizing versions of */ |
| 312 |
OP_MINPLUS, /* This first set applies to single characters */ |
OP_MINSTAR, /* 21 all these opcodes must come in pairs, with */ |
| 313 |
OP_QUERY, |
OP_PLUS, /* 22 the minimizing one second. */ |
| 314 |
OP_MINQUERY, |
OP_MINPLUS, /* 23 This first set applies to single characters */ |
| 315 |
OP_UPTO, /* From 0 to n matches */ |
OP_QUERY, /* 24 */ |
| 316 |
OP_MINUPTO, |
OP_MINQUERY, /* 25 */ |
| 317 |
OP_EXACT, /* Exactly n matches */ |
OP_UPTO, /* 26 From 0 to n matches */ |
| 318 |
|
OP_MINUPTO, /* 27 */ |
| 319 |
OP_NOTSTAR, /* The maximizing and minimizing versions of */ |
OP_EXACT, /* 28 Exactly n matches */ |
| 320 |
OP_NOTMINSTAR, /* all these opcodes must come in pairs, with */ |
|
| 321 |
OP_NOTPLUS, /* the minimizing one second. */ |
OP_NOTSTAR, /* 29 The maximizing and minimizing versions of */ |
| 322 |
OP_NOTMINPLUS, /* This first set applies to "not" single characters */ |
OP_NOTMINSTAR, /* 30 all these opcodes must come in pairs, with */ |
| 323 |
OP_NOTQUERY, |
OP_NOTPLUS, /* 31 the minimizing one second. */ |
| 324 |
OP_NOTMINQUERY, |
OP_NOTMINPLUS, /* 32 This set applies to "not" single characters */ |
| 325 |
OP_NOTUPTO, /* From 0 to n matches */ |
OP_NOTQUERY, /* 33 */ |
| 326 |
OP_NOTMINUPTO, |
OP_NOTMINQUERY, /* 34 */ |
| 327 |
OP_NOTEXACT, /* Exactly n matches */ |
OP_NOTUPTO, /* 35 From 0 to n matches */ |
| 328 |
|
OP_NOTMINUPTO, /* 36 */ |
| 329 |
OP_TYPESTAR, /* The maximizing and minimizing versions of */ |
OP_NOTEXACT, /* 37 Exactly n matches */ |
| 330 |
OP_TYPEMINSTAR, /* all these opcodes must come in pairs, with */ |
|
| 331 |
OP_TYPEPLUS, /* the minimizing one second. These codes must */ |
OP_TYPESTAR, /* 38 The maximizing and minimizing versions of */ |
| 332 |
OP_TYPEMINPLUS, /* be in exactly the same order as those above. */ |
OP_TYPEMINSTAR, /* 39 all these opcodes must come in pairs, with */ |
| 333 |
OP_TYPEQUERY, /* This set applies to character types such as \d */ |
OP_TYPEPLUS, /* 40 the minimizing one second. These codes must */ |
| 334 |
OP_TYPEMINQUERY, |
OP_TYPEMINPLUS, /* 41 be in exactly the same order as those above. */ |
| 335 |
OP_TYPEUPTO, /* From 0 to n matches */ |
OP_TYPEQUERY, /* 42 This set applies to character types such as \d */ |
| 336 |
OP_TYPEMINUPTO, |
OP_TYPEMINQUERY, /* 43 */ |
| 337 |
OP_TYPEEXACT, /* Exactly n matches */ |
OP_TYPEUPTO, /* 44 From 0 to n matches */ |
| 338 |
|
OP_TYPEMINUPTO, /* 45 */ |
| 339 |
OP_CRSTAR, /* The maximizing and minimizing versions of */ |
OP_TYPEEXACT, /* 46 Exactly n matches */ |
| 340 |
OP_CRMINSTAR, /* all these opcodes must come in pairs, with */ |
|
| 341 |
OP_CRPLUS, /* the minimizing one second. These codes must */ |
OP_CRSTAR, /* 47 The maximizing and minimizing versions of */ |
| 342 |
OP_CRMINPLUS, /* be in exactly the same order as those above. */ |
OP_CRMINSTAR, /* 48 all these opcodes must come in pairs, with */ |
| 343 |
OP_CRQUERY, /* These are for character classes and back refs */ |
OP_CRPLUS, /* 49 the minimizing one second. These codes must */ |
| 344 |
OP_CRMINQUERY, |
OP_CRMINPLUS, /* 50 be in exactly the same order as those above. */ |
| 345 |
OP_CRRANGE, /* These are different to the three seta above. */ |
OP_CRQUERY, /* 51 These are for character classes and back refs */ |
| 346 |
OP_CRMINRANGE, |
OP_CRMINQUERY, /* 52 */ |
| 347 |
|
OP_CRRANGE, /* 53 These are different to the three seta above. */ |
| 348 |
OP_CLASS, /* Match a character class */ |
OP_CRMINRANGE, /* 54 */ |
| 349 |
OP_REF, /* Match a back reference */ |
|
| 350 |
|
OP_CLASS, /* 55 Match a character class, chars < 256 only */ |
| 351 |
OP_ALT, /* Start of alternation */ |
OP_NCLASS, /* 56 Same, but the bitmap was created from a negative |
| 352 |
OP_KET, /* End of group that doesn't have an unbounded repeat */ |
class - the difference is relevant only when a UTF-8 |
| 353 |
OP_KETRMAX, /* These two must remain together and in this */ |
character > 255 is encountered. */ |
| 354 |
OP_KETRMIN, /* order. They are for groups the repeat for ever. */ |
|
| 355 |
|
OP_XCLASS, /* 56 Extended class for handling UTF-8 chars within the |
| 356 |
|
class. This does both positive and negative. */ |
| 357 |
|
|
| 358 |
|
OP_REF, /* 57 Match a back reference */ |
| 359 |
|
OP_RECURSE, /* 58 Match a numbered subpattern (possibly recursive) */ |
| 360 |
|
OP_CALLOUT, /* 59 Call out to external function if provided */ |
| 361 |
|
|
| 362 |
|
OP_ALT, /* 60 Start of alternation */ |
| 363 |
|
OP_KET, /* 61 End of group that doesn't have an unbounded repeat */ |
| 364 |
|
OP_KETRMAX, /* 62 These two must remain together and in this */ |
| 365 |
|
OP_KETRMIN, /* 63 order. They are for groups the repeat for ever. */ |
| 366 |
|
|
| 367 |
/* The assertions must come before ONCE and COND */ |
/* The assertions must come before ONCE and COND */ |
| 368 |
|
|
| 369 |
OP_ASSERT, /* Positive lookahead */ |
OP_ASSERT, /* 64 Positive lookahead */ |
| 370 |
OP_ASSERT_NOT, /* Negative lookahead */ |
OP_ASSERT_NOT, /* 65 Negative lookahead */ |
| 371 |
OP_ASSERTBACK, /* Positive lookbehind */ |
OP_ASSERTBACK, /* 66 Positive lookbehind */ |
| 372 |
OP_ASSERTBACK_NOT, /* Negative lookbehind */ |
OP_ASSERTBACK_NOT, /* 67 Negative lookbehind */ |
| 373 |
OP_REVERSE, /* Move pointer back - used in lookbehind assertions */ |
OP_REVERSE, /* 68 Move pointer back - used in lookbehind assertions */ |
| 374 |
|
|
| 375 |
/* ONCE and COND must come after the assertions, with ONCE first, as there's |
/* ONCE and COND must come after the assertions, with ONCE first, as there's |
| 376 |
a test for >= ONCE for a subpattern that isn't an assertion. */ |
a test for >= ONCE for a subpattern that isn't an assertion. */ |
| 377 |
|
|
| 378 |
OP_ONCE, /* Once matched, don't back up into the subpattern */ |
OP_ONCE, /* 69 Once matched, don't back up into the subpattern */ |
| 379 |
OP_COND, /* Conditional group */ |
OP_COND, /* 70 Conditional group */ |
| 380 |
OP_CREF, /* Used to hold an extraction string number */ |
OP_CREF, /* 71 Used to hold an extraction string number (cond ref) */ |
| 381 |
|
|
| 382 |
|
OP_BRAZERO, /* 72 These two must remain together and in this */ |
| 383 |
|
OP_BRAMINZERO, /* 73 order. */ |
| 384 |
|
|
| 385 |
|
OP_BRANUMBER, /* 74 Used for extracting brackets whose number is greater |
| 386 |
|
than can fit into an opcode. */ |
| 387 |
|
|
| 388 |
|
OP_BRA /* 75 This and greater values are used for brackets that |
| 389 |
|
extract substrings up to a basic limit. After that, |
| 390 |
|
use is made of OP_BRANUMBER. */ |
| 391 |
|
}; |
| 392 |
|
|
| 393 |
OP_BRAZERO, /* These two must remain together and in this */ |
/* WARNING: There is an implicit assumption in study.c that all opcodes are |
| 394 |
OP_BRAMINZERO, /* order. */ |
less than 128 in value. This makes handling UTF-8 character sequences easier. |
| 395 |
|
*/ |
| 396 |
|
|
|
OP_BRA /* This and greater values are used for brackets that |
|
|
extract substrings. */ |
|
|
}; |
|
| 397 |
|
|
| 398 |
/* The highest extraction number. This is limited by the number of opcodes |
/* This macro defines textual names for all the opcodes. There are used only |
| 399 |
left after OP_BRA, i.e. 255 - OP_BRA. We actually set it somewhat lower. */ |
for debugging, in pcre.c when DEBUG is defined, and also in pcretest.c. The |
| 400 |
|
macro is referenced only in printint.c. */ |
| 401 |
|
|
| 402 |
|
#define OP_NAME_LIST \ |
| 403 |
|
"End", "\\A", "\\G", "\\B", "\\b", "\\D", "\\d", \ |
| 404 |
|
"\\S", "\\s", "\\W", "\\w", "Any", "Anybyte", "\\Z", "\\z", \ |
| 405 |
|
"Opt", "^", "$", "chars", "not", \ |
| 406 |
|
"*", "*?", "+", "+?", "?", "??", "{", "{", "{", \ |
| 407 |
|
"*", "*?", "+", "+?", "?", "??", "{", "{", "{", \ |
| 408 |
|
"*", "*?", "+", "+?", "?", "??", "{", "{", "{", \ |
| 409 |
|
"*", "*?", "+", "+?", "?", "??", "{", "{", \ |
| 410 |
|
"class", "nclass", "xclass", "Ref", "Recurse", "Callout", \ |
| 411 |
|
"Alt", "Ket", "KetRmax", "KetRmin", "Assert", "Assert not", \ |
| 412 |
|
"AssertB", "AssertB not", "Reverse", "Once", "Cond", "Cond ref",\ |
| 413 |
|
"Brazero", "Braminzero", "Branumber", "Bra" |
| 414 |
|
|
| 415 |
|
|
| 416 |
|
/* This macro defines the length of fixed length operations in the compiled |
| 417 |
|
regex. The lengths are used when searching for specific things, and also in the |
| 418 |
|
debugging printing of a compiled regex. We use a macro so that it can be |
| 419 |
|
incorporated both into pcre.c and pcretest.c without being publicly exposed. |
| 420 |
|
|
| 421 |
|
As things have been extended, some of these are no longer fixed lenths, but are |
| 422 |
|
minima instead. For example, the length of a single-character repeat may vary |
| 423 |
|
in UTF-8 mode. The code that uses this table must know about such things. */ |
| 424 |
|
|
| 425 |
|
#define OP_LENGTHS \ |
| 426 |
|
1, /* End */ \ |
| 427 |
|
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* \A, \G, \B, \B, \D, \d, \S, \s, \W, \w */ \ |
| 428 |
|
1, 1, 1, 1, 2, 1, 1, /* Any, Anybyte, \Z, \z, Opt, ^, $ */ \ |
| 429 |
|
2, /* Chars - the minimum length */ \ |
| 430 |
|
2, /* not */ \ |
| 431 |
|
/* Positive single-char repeats */ \ |
| 432 |
|
2, 2, 2, 2, 2, 2, /* *, *?, +, +?, ?, ?? ** These are */ \ |
| 433 |
|
4, 4, 4, /* upto, minupto, exact ** minima */ \ |
| 434 |
|
/* Negative single-char repeats */ \ |
| 435 |
|
2, 2, 2, 2, 2, 2, /* NOT *, *?, +, +?, ?, ?? */ \ |
| 436 |
|
4, 4, 4, /* NOT upto, minupto, exact */ \ |
| 437 |
|
/* Positive type repeats */ \ |
| 438 |
|
2, 2, 2, 2, 2, 2, /* Type *, *?, +, +?, ?, ?? */ \ |
| 439 |
|
4, 4, 4, /* Type upto, minupto, exact */ \ |
| 440 |
|
/* Character class & ref repeats */ \ |
| 441 |
|
1, 1, 1, 1, 1, 1, /* *, *?, +, +?, ?, ?? */ \ |
| 442 |
|
5, 5, /* CRRANGE, CRMINRANGE */ \ |
| 443 |
|
33, /* CLASS */ \ |
| 444 |
|
33, /* NCLASS */ \ |
| 445 |
|
0, /* XCLASS - variable length */ \ |
| 446 |
|
3, /* REF */ \ |
| 447 |
|
1+LINK_SIZE, /* RECURSE */ \ |
| 448 |
|
2, /* CALLOUT */ \ |
| 449 |
|
1+LINK_SIZE, /* Alt */ \ |
| 450 |
|
1+LINK_SIZE, /* Ket */ \ |
| 451 |
|
1+LINK_SIZE, /* KetRmax */ \ |
| 452 |
|
1+LINK_SIZE, /* KetRmin */ \ |
| 453 |
|
1+LINK_SIZE, /* Assert */ \ |
| 454 |
|
1+LINK_SIZE, /* Assert not */ \ |
| 455 |
|
1+LINK_SIZE, /* Assert behind */ \ |
| 456 |
|
1+LINK_SIZE, /* Assert behind not */ \ |
| 457 |
|
1+LINK_SIZE, /* Reverse */ \ |
| 458 |
|
1+LINK_SIZE, /* Once */ \ |
| 459 |
|
1+LINK_SIZE, /* COND */ \ |
| 460 |
|
3, /* CREF */ \ |
| 461 |
|
1, 1, /* BRAZERO, BRAMINZERO */ \ |
| 462 |
|
3, /* BRANUMBER */ \ |
| 463 |
|
1+LINK_SIZE /* BRA */ \ |
| 464 |
|
|
| 465 |
|
|
| 466 |
|
/* The highest extraction number before we have to start using additional |
| 467 |
|
bytes. (Originally PCRE didn't have support for extraction counts highter than |
| 468 |
|
this number.) The value is limited by the number of opcodes left after OP_BRA, |
| 469 |
|
i.e. 255 - OP_BRA. We actually set it a bit lower to leave room for additional |
| 470 |
|
opcodes. */ |
| 471 |
|
|
| 472 |
|
#define EXTRACT_BASIC_MAX 150 |
| 473 |
|
|
| 474 |
|
/* A magic value for OP_CREF to indicate the "in recursion" condition. */ |
| 475 |
|
|
| 476 |
#define EXTRACT_MAX 99 |
#define CREF_RECURSE 0xffff |
| 477 |
|
|
| 478 |
/* The texts of compile-time error messages are defined as macros here so that |
/* The texts of compile-time error messages are defined as macros here so that |
| 479 |
they can be accessed by the POSIX wrapper and converted into error codes. Yes, |
they can be accessed by the POSIX wrapper and converted into error codes. Yes, |
| 492 |
#define ERR10 "operand of unlimited repeat could match the empty string" |
#define ERR10 "operand of unlimited repeat could match the empty string" |
| 493 |
#define ERR11 "internal error: unexpected repeat" |
#define ERR11 "internal error: unexpected repeat" |
| 494 |
#define ERR12 "unrecognized character after (?" |
#define ERR12 "unrecognized character after (?" |
| 495 |
#define ERR13 "too many capturing parenthesized sub-patterns" |
#define ERR13 "POSIX named classes are supported only within a class" |
| 496 |
#define ERR14 "missing )" |
#define ERR14 "missing )" |
| 497 |
#define ERR15 "back reference to non-existent subpattern" |
#define ERR15 "reference to non-existent subpattern" |
| 498 |
#define ERR16 "erroffset passed as NULL" |
#define ERR16 "erroffset passed as NULL" |
| 499 |
#define ERR17 "unknown option bit(s) set" |
#define ERR17 "unknown option bit(s) set" |
| 500 |
#define ERR18 "missing ) after comment" |
#define ERR18 "missing ) after comment" |
| 501 |
#define ERR19 "too many sets of parentheses" |
#define ERR19 "parentheses nested too deeply" |
| 502 |
#define ERR20 "regular expression too large" |
#define ERR20 "regular expression too large" |
| 503 |
#define ERR21 "failed to get memory" |
#define ERR21 "failed to get memory" |
| 504 |
#define ERR22 "unmatched parentheses" |
#define ERR22 "unmatched parentheses" |
| 508 |
#define ERR26 "malformed number after (?(" |
#define ERR26 "malformed number after (?(" |
| 509 |
#define ERR27 "conditional group contains more than two branches" |
#define ERR27 "conditional group contains more than two branches" |
| 510 |
#define ERR28 "assertion expected after (?(" |
#define ERR28 "assertion expected after (?(" |
| 511 |
|
#define ERR29 "(?R or (?digits must be followed by )" |
| 512 |
|
#define ERR30 "unknown POSIX class name" |
| 513 |
|
#define ERR31 "POSIX collating elements are not supported" |
| 514 |
|
#define ERR32 "this version of PCRE is not compiled with PCRE_UTF8 support" |
| 515 |
|
#define ERR33 "spare error" |
| 516 |
|
#define ERR34 "character value in \\x{...} sequence is too large" |
| 517 |
|
#define ERR35 "invalid condition (?(0)" |
| 518 |
|
#define ERR36 "\\C not allowed in lookbehind assertion" |
| 519 |
|
#define ERR37 "PCRE does not support \\L, \\l, \\N, \\P, \\p, \\U, \\u, or \\X" |
| 520 |
|
#define ERR38 "number after (?C is > 255" |
| 521 |
|
#define ERR39 "closing ) for (?C expected" |
| 522 |
|
#define ERR40 "recursive call could loop indefinitely" |
| 523 |
|
#define ERR41 "unrecognized character after (?P" |
| 524 |
|
#define ERR42 "syntax error after (?P" |
| 525 |
|
#define ERR43 "two named groups have the same name" |
| 526 |
|
|
| 527 |
/* All character handling must be done as unsigned characters. Otherwise there |
/* All character handling must be done as unsigned characters. Otherwise there |
| 528 |
are problems with top-bit-set characters and functions such as isspace(). |
are problems with top-bit-set characters and functions such as isspace(). |
| 533 |
|
|
| 534 |
typedef unsigned char uschar; |
typedef unsigned char uschar; |
| 535 |
|
|
| 536 |
/* The real format of the start of the pcre block; the actual code vector |
/* The real format of the start of the pcre block; the index of names and the |
| 537 |
runs on as long as necessary after the end. */ |
code vector run on as long as necessary after the end. */ |
| 538 |
|
|
| 539 |
typedef struct real_pcre { |
typedef struct real_pcre { |
| 540 |
unsigned int magic_number; |
unsigned long int magic_number; |
| 541 |
const unsigned char *tables; |
size_t size; /* Total that was malloced */ |
| 542 |
unsigned short int options; |
const unsigned char *tables; /* Pointer to tables */ |
| 543 |
unsigned char top_bracket; |
unsigned long int options; |
| 544 |
unsigned char top_backref; |
unsigned short int top_bracket; |
| 545 |
unsigned char first_char; |
unsigned short int top_backref; |
| 546 |
unsigned char code[1]; |
unsigned short int first_byte; |
| 547 |
|
unsigned short int req_byte; |
| 548 |
|
unsigned short int name_entry_size; /* Size of any name items; 0 => none */ |
| 549 |
|
unsigned short int name_count; /* Number of name items */ |
| 550 |
} real_pcre; |
} real_pcre; |
| 551 |
|
|
| 552 |
/* The real format of the extra block returned by pcre_study(). */ |
/* The format of the block used to store data from pcre_study(). */ |
|
|
|
|
typedef struct real_pcre_extra { |
|
|
unsigned char options; |
|
|
unsigned char start_bits[32]; |
|
|
} real_pcre_extra; |
|
| 553 |
|
|
| 554 |
|
typedef struct pcre_study_data { |
| 555 |
|
size_t size; /* Total that was malloced */ |
| 556 |
|
uschar options; |
| 557 |
|
uschar start_bits[32]; |
| 558 |
|
} pcre_study_data; |
| 559 |
|
|
| 560 |
/* Structure for passing "static" information around between the functions |
/* Structure for passing "static" information around between the functions |
| 561 |
doing the compiling, so that they are thread-safe. */ |
doing the compiling, so that they are thread-safe. */ |
| 562 |
|
|
| 563 |
typedef struct compile_data { |
typedef struct compile_data { |
| 564 |
const uschar *lcc; /* Points to lower casing table */ |
const uschar *lcc; /* Points to lower casing table */ |
| 565 |
const uschar *fcc; /* Points to case-flippint table */ |
const uschar *fcc; /* Points to case-flipping table */ |
| 566 |
const uschar *cbits; /* Points to character type table */ |
const uschar *cbits; /* Points to character type table */ |
| 567 |
const uschar *ctypes; /* Points to table of type maps */ |
const uschar *ctypes; /* Points to table of type maps */ |
| 568 |
|
const uschar *start_code; /* The start of the compiled code */ |
| 569 |
|
uschar *name_table; /* The name/number table */ |
| 570 |
|
int names_found; /* Number of entries so far */ |
| 571 |
|
int name_entry_size; /* Size of each entry */ |
| 572 |
|
int top_backref; /* Maximum back reference */ |
| 573 |
|
unsigned int backref_map; /* Bitmap of low back refs */ |
| 574 |
|
int req_varyopt; /* "After variable item" flag for reqbyte */ |
| 575 |
} compile_data; |
} compile_data; |
| 576 |
|
|
| 577 |
|
/* Structure for maintaining a chain of pointers to the currently incomplete |
| 578 |
|
branches, for testing for left recursion. */ |
| 579 |
|
|
| 580 |
|
typedef struct branch_chain { |
| 581 |
|
struct branch_chain *outer; |
| 582 |
|
uschar *current; |
| 583 |
|
} branch_chain; |
| 584 |
|
|
| 585 |
|
/* Structure for items in a linked list that represents an explicit recursive |
| 586 |
|
call within the pattern. */ |
| 587 |
|
|
| 588 |
|
typedef struct recursion_info { |
| 589 |
|
struct recursion_info *prev; /* Previous recursion record (or NULL) */ |
| 590 |
|
int group_num; /* Number of group that was called */ |
| 591 |
|
const uschar *after_call; /* "Return value": points after the call in the expr */ |
| 592 |
|
const uschar *save_start; /* Old value of md->start_match */ |
| 593 |
|
int *offset_save; /* Pointer to start of saved offsets */ |
| 594 |
|
int saved_max; /* Number of saved offsets */ |
| 595 |
|
} recursion_info; |
| 596 |
|
|
| 597 |
/* Structure for passing "static" information around between the functions |
/* Structure for passing "static" information around between the functions |
| 598 |
doing the matching, so that they are thread-safe. */ |
doing the matching, so that they are thread-safe. */ |
| 599 |
|
|
| 600 |
typedef struct match_data { |
typedef struct match_data { |
| 601 |
int errorcode; /* As it says */ |
int match_call_count; /* As it says */ |
| 602 |
|
unsigned long int match_limit;/* As it says */ |
| 603 |
int *offset_vector; /* Offset vector */ |
int *offset_vector; /* Offset vector */ |
| 604 |
int offset_end; /* One past the end */ |
int offset_end; /* One past the end */ |
| 605 |
int offset_max; /* The maximum usable for return data */ |
int offset_max; /* The maximum usable for return data */ |
| 608 |
BOOL offset_overflow; /* Set if too many extractions */ |
BOOL offset_overflow; /* Set if too many extractions */ |
| 609 |
BOOL notbol; /* NOTBOL flag */ |
BOOL notbol; /* NOTBOL flag */ |
| 610 |
BOOL noteol; /* NOTEOL flag */ |
BOOL noteol; /* NOTEOL flag */ |
| 611 |
|
BOOL utf8; /* UTF8 flag */ |
| 612 |
BOOL endonly; /* Dollar not before final \n */ |
BOOL endonly; /* Dollar not before final \n */ |
| 613 |
|
BOOL notempty; /* Empty string match not wanted */ |
| 614 |
|
const uschar *start_code; /* For use when recursing */ |
| 615 |
const uschar *start_subject; /* Start of the subject string */ |
const uschar *start_subject; /* Start of the subject string */ |
| 616 |
const uschar *end_subject; /* End of the subject string */ |
const uschar *end_subject; /* End of the subject string */ |
| 617 |
|
const uschar *start_match; /* Start of this match attempt */ |
| 618 |
const uschar *end_match_ptr; /* Subject position at end match */ |
const uschar *end_match_ptr; /* Subject position at end match */ |
| 619 |
int end_offset_top; /* Highwater mark at end of match */ |
int end_offset_top; /* Highwater mark at end of match */ |
| 620 |
|
int capture_last; /* Most recent capture number */ |
| 621 |
|
int start_offset; /* The start offset value */ |
| 622 |
|
recursion_info *recursive; /* Linked list of recursion data */ |
| 623 |
|
void *callout_data; /* To pass back to callouts */ |
| 624 |
} match_data; |
} match_data; |
| 625 |
|
|
| 626 |
/* Bit definitions for entries in the pcre_ctypes table. */ |
/* Bit definitions for entries in the pcre_ctypes table. */ |
| 633 |
#define ctype_meta 0x80 /* regexp meta char or zero (end pattern) */ |
#define ctype_meta 0x80 /* regexp meta char or zero (end pattern) */ |
| 634 |
|
|
| 635 |
/* Offsets for the bitmap tables in pcre_cbits. Each table contains a set |
/* Offsets for the bitmap tables in pcre_cbits. Each table contains a set |
| 636 |
of bits for a class map. */ |
of bits for a class map. Some classes are built by combining these tables. */ |
| 637 |
|
|
| 638 |
#define cbit_digit 0 /* for \d */ |
#define cbit_space 0 /* [:space:] or \s */ |
| 639 |
#define cbit_word 32 /* for \w */ |
#define cbit_xdigit 32 /* [:xdigit:] */ |
| 640 |
#define cbit_space 64 /* for \s */ |
#define cbit_digit 64 /* [:digit:] or \d */ |
| 641 |
#define cbit_length 96 /* Length of the cbits table */ |
#define cbit_upper 96 /* [:upper:] */ |
| 642 |
|
#define cbit_lower 128 /* [:lower:] */ |
| 643 |
|
#define cbit_word 160 /* [:word:] or \w */ |
| 644 |
|
#define cbit_graph 192 /* [:graph:] */ |
| 645 |
|
#define cbit_print 224 /* [:print:] */ |
| 646 |
|
#define cbit_punct 256 /* [:punct:] */ |
| 647 |
|
#define cbit_cntrl 288 /* [:cntrl:] */ |
| 648 |
|
#define cbit_length 320 /* Length of the cbits table */ |
| 649 |
|
|
| 650 |
/* Offsets of the various tables from the base tables pointer, and |
/* Offsets of the various tables from the base tables pointer, and |
| 651 |
total length. */ |
total length. */ |