| 1 |
knight |
71 |
/* $Id$ */ |
| 2 |
adx |
59 |
|
| 3 |
|
|
/************************************************* |
| 4 |
|
|
* Perl-Compatible Regular Expressions * |
| 5 |
|
|
*************************************************/ |
| 6 |
|
|
|
| 7 |
|
|
/* PCRE is a library of functions to support regular expressions whose syntax |
| 8 |
|
|
and semantics are as close as possible to those of the Perl 5 language. |
| 9 |
|
|
|
| 10 |
|
|
Written by Philip Hazel |
| 11 |
|
|
Copyright (c) 1997-2005 University of Cambridge |
| 12 |
|
|
|
| 13 |
|
|
----------------------------------------------------------------------------- |
| 14 |
|
|
Redistribution and use in source and binary forms, with or without |
| 15 |
|
|
modification, are permitted provided that the following conditions are met: |
| 16 |
|
|
|
| 17 |
|
|
* Redistributions of source code must retain the above copyright notice, |
| 18 |
|
|
this list of conditions and the following disclaimer. |
| 19 |
|
|
|
| 20 |
|
|
* Redistributions in binary form must reproduce the above copyright |
| 21 |
|
|
notice, this list of conditions and the following disclaimer in the |
| 22 |
|
|
documentation and/or other materials provided with the distribution. |
| 23 |
|
|
|
| 24 |
|
|
* Neither the name of the University of Cambridge nor the names of its |
| 25 |
|
|
contributors may be used to endorse or promote products derived from |
| 26 |
|
|
this software without specific prior written permission. |
| 27 |
|
|
|
| 28 |
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 29 |
|
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 30 |
|
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 31 |
|
|
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 32 |
|
|
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 33 |
|
|
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 34 |
|
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 35 |
|
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 36 |
|
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 37 |
|
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 38 |
|
|
POSSIBILITY OF SUCH DAMAGE. |
| 39 |
|
|
----------------------------------------------------------------------------- |
| 40 |
|
|
*/ |
| 41 |
|
|
|
| 42 |
|
|
|
| 43 |
|
|
/* This module contains the external function pcre_compile(), along with |
| 44 |
|
|
supporting internal functions that are not used by other modules. */ |
| 45 |
|
|
|
| 46 |
|
|
|
| 47 |
|
|
#include "pcre_internal.h" |
| 48 |
|
|
|
| 49 |
|
|
|
| 50 |
|
|
/************************************************* |
| 51 |
|
|
* Code parameters and static tables * |
| 52 |
|
|
*************************************************/ |
| 53 |
|
|
|
| 54 |
|
|
/* Maximum number of items on the nested bracket stacks at compile time. This |
| 55 |
|
|
applies to the nesting of all kinds of parentheses. It does not limit |
| 56 |
|
|
un-nested, non-capturing parentheses. This number can be made bigger if |
| 57 |
|
|
necessary - it is used to dimension one int and one unsigned char vector at |
| 58 |
|
|
compile time. */ |
| 59 |
|
|
|
| 60 |
|
|
#define BRASTACK_SIZE 200 |
| 61 |
|
|
|
| 62 |
|
|
/* Table of sizes for the fixed-length opcodes. It's defined in a macro so that |
| 63 |
|
|
the definition is next to the definition of the opcodes in internal.h. */ |
| 64 |
|
|
|
| 65 |
|
|
static const uschar _pcre_OP_lengths[] = { OP_LENGTHS }; |
| 66 |
|
|
|
| 67 |
|
|
|
| 68 |
|
|
/* Table for handling escaped characters in the range '0'-'z'. Positive returns |
| 69 |
|
|
are simple data values; negative values are for special things like \d and so |
| 70 |
|
|
on. Zero means further processing is needed (for things like \x), or the escape |
| 71 |
|
|
is invalid. */ |
| 72 |
|
|
|
| 73 |
|
|
/* This is the "normal" table for ASCII systems */ |
| 74 |
|
|
static const short int escapes[] = { |
| 75 |
|
|
0, 0, 0, 0, 0, 0, 0, 0, /* 0 - 7 */ |
| 76 |
|
|
0, 0, ':', ';', '<', '=', '>', '?', /* 8 - ? */ |
| 77 |
|
|
'@', -ESC_A, -ESC_B, -ESC_C, -ESC_D, -ESC_E, 0, -ESC_G, /* @ - G */ |
| 78 |
|
|
0, 0, 0, 0, 0, 0, 0, 0, /* H - O */ |
| 79 |
|
|
-ESC_P, -ESC_Q, 0, -ESC_S, 0, 0, 0, -ESC_W, /* P - W */ |
| 80 |
|
|
-ESC_X, 0, -ESC_Z, '[', '\\', ']', '^', '_', /* X - _ */ |
| 81 |
|
|
'`', 7, -ESC_b, 0, -ESC_d, ESC_e, ESC_f, 0, /* ` - g */ |
| 82 |
|
|
0, 0, 0, 0, 0, 0, ESC_n, 0, /* h - o */ |
| 83 |
|
|
-ESC_p, 0, ESC_r, -ESC_s, ESC_tee, 0, 0, -ESC_w, /* p - w */ |
| 84 |
|
|
0, 0, -ESC_z /* x - z */ |
| 85 |
|
|
}; |
| 86 |
|
|
|
| 87 |
|
|
|
| 88 |
|
|
/* Tables of names of POSIX character classes and their lengths. The list is |
| 89 |
|
|
terminated by a zero length entry. The first three must be alpha, upper, lower, |
| 90 |
|
|
as this is assumed for handling case independence. */ |
| 91 |
|
|
|
| 92 |
|
|
static const char *const posix_names[] = { |
| 93 |
|
|
"alpha", "lower", "upper", |
| 94 |
|
|
"alnum", "ascii", "blank", "cntrl", "digit", "graph", |
| 95 |
|
|
"print", "punct", "space", "word", "xdigit" }; |
| 96 |
|
|
|
| 97 |
|
|
static const uschar posix_name_lengths[] = { |
| 98 |
|
|
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 4, 6, 0 }; |
| 99 |
|
|
|
| 100 |
|
|
/* Table of class bit maps for each POSIX class; up to three may be combined |
| 101 |
|
|
to form the class. The table for [:blank:] is dynamically modified to remove |
| 102 |
|
|
the vertical space characters. */ |
| 103 |
|
|
|
| 104 |
|
|
static const int posix_class_maps[] = { |
| 105 |
|
|
cbit_lower, cbit_upper, -1, /* alpha */ |
| 106 |
|
|
cbit_lower, -1, -1, /* lower */ |
| 107 |
|
|
cbit_upper, -1, -1, /* upper */ |
| 108 |
|
|
cbit_digit, cbit_lower, cbit_upper, /* alnum */ |
| 109 |
|
|
cbit_print, cbit_cntrl, -1, /* ascii */ |
| 110 |
|
|
cbit_space, -1, -1, /* blank - a GNU extension */ |
| 111 |
|
|
cbit_cntrl, -1, -1, /* cntrl */ |
| 112 |
|
|
cbit_digit, -1, -1, /* digit */ |
| 113 |
|
|
cbit_graph, -1, -1, /* graph */ |
| 114 |
|
|
cbit_print, -1, -1, /* print */ |
| 115 |
|
|
cbit_punct, -1, -1, /* punct */ |
| 116 |
|
|
cbit_space, -1, -1, /* space */ |
| 117 |
|
|
cbit_word, -1, -1, /* word - a Perl extension */ |
| 118 |
|
|
cbit_xdigit,-1, -1 /* xdigit */ |
| 119 |
|
|
}; |
| 120 |
|
|
|
| 121 |
|
|
|
| 122 |
|
|
/* The texts of compile-time error messages. These are "char *" because they |
| 123 |
|
|
are passed to the outside world. */ |
| 124 |
|
|
|
| 125 |
|
|
static const char *error_texts[] = { |
| 126 |
|
|
"no error", |
| 127 |
|
|
"\\ at end of pattern", |
| 128 |
|
|
"\\c at end of pattern", |
| 129 |
|
|
"unrecognized character follows \\", |
| 130 |
|
|
"numbers out of order in {} quantifier", |
| 131 |
|
|
/* 5 */ |
| 132 |
|
|
"number too big in {} quantifier", |
| 133 |
|
|
"missing terminating ] for character class", |
| 134 |
|
|
"invalid escape sequence in character class", |
| 135 |
|
|
"range out of order in character class", |
| 136 |
|
|
"nothing to repeat", |
| 137 |
|
|
/* 10 */ |
| 138 |
|
|
"operand of unlimited repeat could match the empty string", |
| 139 |
|
|
"internal error: unexpected repeat", |
| 140 |
|
|
"unrecognized character after (?", |
| 141 |
|
|
"POSIX named classes are supported only within a class", |
| 142 |
|
|
"missing )", |
| 143 |
|
|
/* 15 */ |
| 144 |
|
|
"reference to non-existent subpattern", |
| 145 |
|
|
"erroffset passed as NULL", |
| 146 |
|
|
"unknown option bit(s) set", |
| 147 |
|
|
"missing ) after comment", |
| 148 |
|
|
"parentheses nested too deeply", |
| 149 |
|
|
/* 20 */ |
| 150 |
|
|
"regular expression too large", |
| 151 |
|
|
"failed to get memory", |
| 152 |
|
|
"unmatched parentheses", |
| 153 |
|
|
"internal error: code overflow", |
| 154 |
|
|
"unrecognized character after (?<", |
| 155 |
|
|
/* 25 */ |
| 156 |
|
|
"lookbehind assertion is not fixed length", |
| 157 |
|
|
"malformed number after (?(", |
| 158 |
|
|
"conditional group contains more than two branches", |
| 159 |
|
|
"assertion expected after (?(", |
| 160 |
|
|
"(?R or (?digits must be followed by )", |
| 161 |
|
|
/* 30 */ |
| 162 |
|
|
"unknown POSIX class name", |
| 163 |
|
|
"POSIX collating elements are not supported", |
| 164 |
|
|
"this version of PCRE is not compiled with PCRE_UTF8 support", |
| 165 |
|
|
"spare error", |
| 166 |
|
|
"character value in \\x{...} sequence is too large", |
| 167 |
|
|
/* 35 */ |
| 168 |
|
|
"invalid condition (?(0)", |
| 169 |
|
|
"\\C not allowed in lookbehind assertion", |
| 170 |
|
|
"PCRE does not support \\L, \\l, \\N, \\U, or \\u", |
| 171 |
|
|
"number after (?C is > 255", |
| 172 |
|
|
"closing ) for (?C expected", |
| 173 |
|
|
/* 40 */ |
| 174 |
|
|
"recursive call could loop indefinitely", |
| 175 |
|
|
"unrecognized character after (?P", |
| 176 |
|
|
"syntax error after (?P", |
| 177 |
|
|
"two named groups have the same name", |
| 178 |
|
|
"invalid UTF-8 string", |
| 179 |
|
|
/* 45 */ |
| 180 |
|
|
"support for \\P, \\p, and \\X has not been compiled", |
| 181 |
|
|
"malformed \\P or \\p sequence", |
| 182 |
|
|
"unknown property name after \\P or \\p" |
| 183 |
|
|
}; |
| 184 |
|
|
|
| 185 |
|
|
|
| 186 |
|
|
/* Table to identify digits and hex digits. This is used when compiling |
| 187 |
|
|
patterns. Note that the tables in chartables are dependent on the locale, and |
| 188 |
|
|
may mark arbitrary characters as digits - but the PCRE compiling code expects |
| 189 |
|
|
to handle only 0-9, a-z, and A-Z as digits when compiling. That is why we have |
| 190 |
|
|
a private table here. It costs 256 bytes, but it is a lot faster than doing |
| 191 |
|
|
character value tests (at least in some simple cases I timed), and in some |
| 192 |
|
|
applications one wants PCRE to compile efficiently as well as match |
| 193 |
|
|
efficiently. |
| 194 |
|
|
|
| 195 |
|
|
For convenience, we use the same bit definitions as in chartables: |
| 196 |
|
|
|
| 197 |
|
|
0x04 decimal digit |
| 198 |
|
|
0x08 hexadecimal digit |
| 199 |
|
|
|
| 200 |
|
|
Then we can use ctype_digit and ctype_xdigit in the code. */ |
| 201 |
|
|
|
| 202 |
|
|
/* This is the "normal" case, for ASCII systems */ |
| 203 |
|
|
static const unsigned char digitab[] = |
| 204 |
|
|
{ |
| 205 |
|
|
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 0- 7 */ |
| 206 |
|
|
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 8- 15 */ |
| 207 |
|
|
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 16- 23 */ |
| 208 |
|
|
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 24- 31 */ |
| 209 |
|
|
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* - ' */ |
| 210 |
|
|
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* ( - / */ |
| 211 |
|
|
0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c, /* 0 - 7 */ |
| 212 |
|
|
0x0c,0x0c,0x00,0x00,0x00,0x00,0x00,0x00, /* 8 - ? */ |
| 213 |
|
|
0x00,0x08,0x08,0x08,0x08,0x08,0x08,0x00, /* @ - G */ |
| 214 |
|
|
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* H - O */ |
| 215 |
|
|
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* P - W */ |
| 216 |
|
|
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* X - _ */ |
| 217 |
|
|
0x00,0x08,0x08,0x08,0x08,0x08,0x08,0x00, /* ` - g */ |
| 218 |
|
|
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* h - o */ |
| 219 |
|
|
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* p - w */ |
| 220 |
|
|
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* x -127 */ |
| 221 |
|
|
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 128-135 */ |
| 222 |
|
|
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 136-143 */ |
| 223 |
|
|
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 144-151 */ |
| 224 |
|
|
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 152-159 */ |
| 225 |
|
|
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 160-167 */ |
| 226 |
|
|
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 168-175 */ |
| 227 |
|
|
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 176-183 */ |
| 228 |
|
|
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 184-191 */ |
| 229 |
|
|
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 192-199 */ |
| 230 |
|
|
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 200-207 */ |
| 231 |
|
|
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 208-215 */ |
| 232 |
|
|
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 216-223 */ |
| 233 |
|
|
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 224-231 */ |
| 234 |
|
|
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 232-239 */ |
| 235 |
|
|
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 240-247 */ |
| 236 |
|
|
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};/* 248-255 */ |
| 237 |
|
|
|
| 238 |
|
|
|
| 239 |
|
|
/* Definition to allow mutual recursion */ |
| 240 |
|
|
|
| 241 |
|
|
static BOOL |
| 242 |
|
|
compile_regex(int, int, int *, uschar **, const uschar **, int *, BOOL, int, |
| 243 |
|
|
int *, int *, branch_chain *, compile_data *); |
| 244 |
|
|
|
| 245 |
|
|
|
| 246 |
|
|
|
| 247 |
|
|
/************************************************* |
| 248 |
|
|
* Handle escapes * |
| 249 |
|
|
*************************************************/ |
| 250 |
|
|
|
| 251 |
|
|
/* This function is called when a \ has been encountered. It either returns a |
| 252 |
|
|
positive value for a simple escape such as \n, or a negative value which |
| 253 |
|
|
encodes one of the more complicated things such as \d. When UTF-8 is enabled, |
| 254 |
|
|
a positive value greater than 255 may be returned. On entry, ptr is pointing at |
| 255 |
|
|
the \. On exit, it is on the final character of the escape sequence. |
| 256 |
|
|
|
| 257 |
|
|
Arguments: |
| 258 |
|
|
ptrptr points to the pattern position pointer |
| 259 |
|
|
errorcodeptr points to the errorcode variable |
| 260 |
|
|
bracount number of previous extracting brackets |
| 261 |
|
|
options the options bits |
| 262 |
|
|
isclass TRUE if inside a character class |
| 263 |
|
|
|
| 264 |
|
|
Returns: zero or positive => a data character |
| 265 |
|
|
negative => a special escape sequence |
| 266 |
|
|
on error, errorptr is set |
| 267 |
|
|
*/ |
| 268 |
|
|
|
| 269 |
|
|
static int |
| 270 |
|
|
check_escape(const uschar **ptrptr, int *errorcodeptr, int bracount, |
| 271 |
|
|
int options, BOOL isclass) |
| 272 |
|
|
{ |
| 273 |
|
|
const uschar *ptr = *ptrptr; |
| 274 |
|
|
int c, i; |
| 275 |
|
|
|
| 276 |
|
|
/* If backslash is at the end of the pattern, it's an error. */ |
| 277 |
|
|
|
| 278 |
|
|
c = *(++ptr); |
| 279 |
|
|
if (c == 0) *errorcodeptr = ERR1; |
| 280 |
|
|
|
| 281 |
|
|
/* Non-alphamerics are literals. For digits or letters, do an initial lookup in |
| 282 |
|
|
a table. A non-zero result is something that can be returned immediately. |
| 283 |
|
|
Otherwise further processing may be required. */ |
| 284 |
|
|
|
| 285 |
|
|
/* ASCII coding */ |
| 286 |
|
|
else if (c < '0' || c > 'z') {} /* Not alphameric */ |
| 287 |
|
|
else if ((i = escapes[c - '0']) != 0) c = i; |
| 288 |
|
|
|
| 289 |
|
|
/* Escapes that need further processing, or are illegal. */ |
| 290 |
|
|
|
| 291 |
|
|
else |
| 292 |
|
|
{ |
| 293 |
|
|
const uschar *oldptr; |
| 294 |
|
|
switch (c) |
| 295 |
|
|
{ |
| 296 |
|
|
/* A number of Perl escapes are not handled by PCRE. We give an explicit |
| 297 |
|
|
error. */ |
| 298 |
|
|
|
| 299 |
|
|
case 'l': |
| 300 |
|
|
case 'L': |
| 301 |
|
|
case 'N': |
| 302 |
|
|
case 'u': |
| 303 |
|
|
case 'U': |
| 304 |
|
|
*errorcodeptr = ERR37; |
| 305 |
|
|
break; |
| 306 |
|
|
|
| 307 |
|
|
/* The handling of escape sequences consisting of a string of digits |
| 308 |
|
|
starting with one that is not zero is not straightforward. By experiment, |
| 309 |
|
|
the way Perl works seems to be as follows: |
| 310 |
|
|
|
| 311 |
|
|
Outside a character class, the digits are read as a decimal number. If the |
| 312 |
|
|
number is less than 10, or if there are that many previous extracting |
| 313 |
|
|
left brackets, then it is a back reference. Otherwise, up to three octal |
| 314 |
|
|
digits are read to form an escaped byte. Thus \123 is likely to be octal |
| 315 |
|
|
123 (cf \0123, which is octal 012 followed by the literal 3). If the octal |
| 316 |
|
|
value is greater than 377, the least significant 8 bits are taken. Inside a |
| 317 |
|
|
character class, \ followed by a digit is always an octal number. */ |
| 318 |
|
|
|
| 319 |
|
|
case '1': case '2': case '3': case '4': case '5': |
| 320 |
|
|
case '6': case '7': case '8': case '9': |
| 321 |
|
|
|
| 322 |
|
|
if (!isclass) |
| 323 |
|
|
{ |
| 324 |
|
|
oldptr = ptr; |
| 325 |
|
|
c -= '0'; |
| 326 |
|
|
while ((digitab[ptr[1]] & ctype_digit) != 0) |
| 327 |
|
|
c = c * 10 + *(++ptr) - '0'; |
| 328 |
|
|
if (c < 10 || c <= bracount) |
| 329 |
|
|
{ |
| 330 |
|
|
c = -(ESC_REF + c); |
| 331 |
|
|
break; |
| 332 |
|
|
} |
| 333 |
|
|
ptr = oldptr; /* Put the pointer back and fall through */ |
| 334 |
|
|
} |
| 335 |
|
|
|
| 336 |
|
|
/* Handle an octal number following \. If the first digit is 8 or 9, Perl |
| 337 |
|
|
generates a binary zero byte and treats the digit as a following literal. |
| 338 |
|
|
Thus we have to pull back the pointer by one. */ |
| 339 |
|
|
|
| 340 |
|
|
if ((c = *ptr) >= '8') |
| 341 |
|
|
{ |
| 342 |
|
|
ptr--; |
| 343 |
|
|
c = 0; |
| 344 |
|
|
break; |
| 345 |
|
|
} |
| 346 |
|
|
|
| 347 |
|
|
/* \0 always starts an octal number, but we may drop through to here with a |
| 348 |
|
|
larger first octal digit. */ |
| 349 |
|
|
|
| 350 |
|
|
case '0': |
| 351 |
|
|
c -= '0'; |
| 352 |
|
|
while(i++ < 2 && ptr[1] >= '0' && ptr[1] <= '7') |
| 353 |
|
|
c = c * 8 + *(++ptr) - '0'; |
| 354 |
|
|
c &= 255; /* Take least significant 8 bits */ |
| 355 |
|
|
break; |
| 356 |
|
|
|
| 357 |
|
|
/* \x is complicated when UTF-8 is enabled. \x{ddd} is a character number |
| 358 |
|
|
which can be greater than 0xff, but only if the ddd are hex digits. */ |
| 359 |
|
|
|
| 360 |
|
|
case 'x': |
| 361 |
|
|
/* Read just a single hex char */ |
| 362 |
|
|
|
| 363 |
|
|
c = 0; |
| 364 |
|
|
while (i++ < 2 && (digitab[ptr[1]] & ctype_xdigit) != 0) |
| 365 |
|
|
{ |
| 366 |
|
|
int cc; /* Some compilers don't like ++ */ |
| 367 |
|
|
cc = *(++ptr); /* in initializers */ |
| 368 |
|
|
/* ASCII coding */ |
| 369 |
|
|
if (cc >= 'a') cc -= 32; /* Convert to upper case */ |
| 370 |
|
|
c = c * 16 + cc - ((cc < 'A')? '0' : ('A' - 10)); |
| 371 |
|
|
} |
| 372 |
|
|
break; |
| 373 |
|
|
|
| 374 |
|
|
/* Other special escapes not starting with a digit are straightforward */ |
| 375 |
|
|
|
| 376 |
|
|
case 'c': |
| 377 |
|
|
c = *(++ptr); |
| 378 |
|
|
if (c == 0) |
| 379 |
|
|
{ |
| 380 |
|
|
*errorcodeptr = ERR2; |
| 381 |
|
|
return 0; |
| 382 |
|
|
} |
| 383 |
|
|
|
| 384 |
|
|
/* A letter is upper-cased; then the 0x40 bit is flipped. This coding |
| 385 |
|
|
is ASCII-specific, but then the whole concept of \cx is ASCII-specific. |
| 386 |
|
|
*/ |
| 387 |
|
|
|
| 388 |
|
|
/* ASCII coding */ |
| 389 |
|
|
if (c >= 'a' && c <= 'z') c -= 32; |
| 390 |
|
|
c ^= 0x40; |
| 391 |
|
|
break; |
| 392 |
|
|
|
| 393 |
|
|
/* PCRE_EXTRA enables extensions to Perl in the matter of escapes. Any |
| 394 |
|
|
other alphameric following \ is an error if PCRE_EXTRA was set; otherwise, |
| 395 |
|
|
for Perl compatibility, it is a literal. This code looks a bit odd, but |
| 396 |
|
|
there used to be some cases other than the default, and there may be again |
| 397 |
|
|
in future, so I haven't "optimized" it. */ |
| 398 |
|
|
|
| 399 |
|
|
default: |
| 400 |
|
|
if ((options & PCRE_EXTRA) != 0) switch(c) |
| 401 |
|
|
{ |
| 402 |
|
|
default: |
| 403 |
|
|
*errorcodeptr = ERR3; |
| 404 |
|
|
break; |
| 405 |
|
|
} |
| 406 |
|
|
break; |
| 407 |
|
|
} |
| 408 |
|
|
} |
| 409 |
|
|
|
| 410 |
|
|
*ptrptr = ptr; |
| 411 |
|
|
return c; |
| 412 |
|
|
} |
| 413 |
|
|
|
| 414 |
|
|
/************************************************* |
| 415 |
|
|
* Check for counted repeat * |
| 416 |
|
|
*************************************************/ |
| 417 |
|
|
|
| 418 |
|
|
/* This function is called when a '{' is encountered in a place where it might |
| 419 |
|
|
start a quantifier. It looks ahead to see if it really is a quantifier or not. |
| 420 |
|
|
It is only a quantifier if it is one of the forms {ddd} {ddd,} or {ddd,ddd} |
| 421 |
|
|
where the ddds are digits. |
| 422 |
|
|
|
| 423 |
|
|
Arguments: |
| 424 |
|
|
p pointer to the first char after '{' |
| 425 |
|
|
|
| 426 |
|
|
Returns: TRUE or FALSE |
| 427 |
|
|
*/ |
| 428 |
|
|
|
| 429 |
|
|
static BOOL |
| 430 |
|
|
is_counted_repeat(const uschar *p) |
| 431 |
|
|
{ |
| 432 |
|
|
if ((digitab[*p++] & ctype_digit) == 0) return FALSE; |
| 433 |
|
|
while ((digitab[*p] & ctype_digit) != 0) p++; |
| 434 |
|
|
if (*p == '}') return TRUE; |
| 435 |
|
|
|
| 436 |
|
|
if (*p++ != ',') return FALSE; |
| 437 |
|
|
if (*p == '}') return TRUE; |
| 438 |
|
|
|
| 439 |
|
|
if ((digitab[*p++] & ctype_digit) == 0) return FALSE; |
| 440 |
|
|
while ((digitab[*p] & ctype_digit) != 0) p++; |
| 441 |
|
|
|
| 442 |
|
|
return (*p == '}'); |
| 443 |
|
|
} |
| 444 |
|
|
|
| 445 |
|
|
|
| 446 |
|
|
|
| 447 |
|
|
/************************************************* |
| 448 |
|
|
* Read repeat counts * |
| 449 |
|
|
*************************************************/ |
| 450 |
|
|
|
| 451 |
|
|
/* Read an item of the form {n,m} and return the values. This is called only |
| 452 |
|
|
after is_counted_repeat() has confirmed that a repeat-count quantifier exists, |
| 453 |
|
|
so the syntax is guaranteed to be correct, but we need to check the values. |
| 454 |
|
|
|
| 455 |
|
|
Arguments: |
| 456 |
|
|
p pointer to first char after '{' |
| 457 |
|
|
minp pointer to int for min |
| 458 |
|
|
maxp pointer to int for max |
| 459 |
|
|
returned as -1 if no max |
| 460 |
|
|
errorcodeptr points to error code variable |
| 461 |
|
|
|
| 462 |
|
|
Returns: pointer to '}' on success; |
| 463 |
|
|
current ptr on error, with errorcodeptr set non-zero |
| 464 |
|
|
*/ |
| 465 |
|
|
|
| 466 |
|
|
static const uschar * |
| 467 |
|
|
read_repeat_counts(const uschar *p, int *minp, int *maxp, int *errorcodeptr) |
| 468 |
|
|
{ |
| 469 |
|
|
int min = 0; |
| 470 |
|
|
int max = -1; |
| 471 |
|
|
|
| 472 |
|
|
/* Read the minimum value and do a paranoid check: a negative value indicates |
| 473 |
|
|
an integer overflow. */ |
| 474 |
|
|
|
| 475 |
|
|
while ((digitab[*p] & ctype_digit) != 0) min = min * 10 + *p++ - '0'; |
| 476 |
|
|
if (min < 0 || min > 65535) |
| 477 |
|
|
{ |
| 478 |
|
|
*errorcodeptr = ERR5; |
| 479 |
|
|
return p; |
| 480 |
|
|
} |
| 481 |
|
|
|
| 482 |
|
|
/* Read the maximum value if there is one, and again do a paranoid on its size. |
| 483 |
|
|
Also, max must not be less than min. */ |
| 484 |
|
|
|
| 485 |
|
|
if (*p == '}') max = min; else |
| 486 |
|
|
{ |
| 487 |
|
|
if (*(++p) != '}') |
| 488 |
|
|
{ |
| 489 |
|
|
max = 0; |
| 490 |
|
|
while((digitab[*p] & ctype_digit) != 0) max = max * 10 + *p++ - '0'; |
| 491 |
|
|
if (max < 0 || max > 65535) |
| 492 |
|
|
{ |
| 493 |
|
|
*errorcodeptr = ERR5; |
| 494 |
|
|
return p; |
| 495 |
|
|
} |
| 496 |
|
|
if (max < min) |
| 497 |
|
|
{ |
| 498 |
|
|
*errorcodeptr = ERR4; |
| 499 |
|
|
return p; |
| 500 |
|
|
} |
| 501 |
|
|
} |
| 502 |
|
|
} |
| 503 |
|
|
|
| 504 |
|
|
/* Fill in the required variables, and pass back the pointer to the terminating |
| 505 |
|
|
'}'. */ |
| 506 |
|
|
|
| 507 |
|
|
*minp = min; |
| 508 |
|
|
*maxp = max; |
| 509 |
|
|
return p; |
| 510 |
|
|
} |
| 511 |
|
|
|
| 512 |
|
|
|
| 513 |
|
|
|
| 514 |
|
|
/************************************************* |
| 515 |
|
|
* Find first significant op code * |
| 516 |
|
|
*************************************************/ |
| 517 |
|
|
|
| 518 |
|
|
/* This is called by several functions that scan a compiled expression looking |
| 519 |
|
|
for a fixed first character, or an anchoring op code etc. It skips over things |
| 520 |
|
|
that do not influence this. For some calls, a change of option is important. |
| 521 |
|
|
For some calls, it makes sense to skip negative forward and all backward |
| 522 |
|
|
assertions, and also the \b assertion; for others it does not. |
| 523 |
|
|
|
| 524 |
|
|
Arguments: |
| 525 |
|
|
code pointer to the start of the group |
| 526 |
|
|
options pointer to external options |
| 527 |
|
|
optbit the option bit whose changing is significant, or |
| 528 |
|
|
zero if none are |
| 529 |
|
|
skipassert TRUE if certain assertions are to be skipped |
| 530 |
|
|
|
| 531 |
|
|
Returns: pointer to the first significant opcode |
| 532 |
|
|
*/ |
| 533 |
|
|
|
| 534 |
|
|
static const uschar* |
| 535 |
|
|
first_significant_code(const uschar *code, int *options, int optbit, |
| 536 |
|
|
BOOL skipassert) |
| 537 |
|
|
{ |
| 538 |
|
|
for (;;) |
| 539 |
|
|
{ |
| 540 |
|
|
switch ((int)*code) |
| 541 |
|
|
{ |
| 542 |
|
|
case OP_OPT: |
| 543 |
|
|
if (optbit > 0 && ((int)code[1] & optbit) != (*options & optbit)) |
| 544 |
|
|
*options = (int)code[1]; |
| 545 |
|
|
code += 2; |
| 546 |
|
|
break; |
| 547 |
|
|
|
| 548 |
|
|
case OP_ASSERT_NOT: |
| 549 |
|
|
case OP_ASSERTBACK: |
| 550 |
|
|
case OP_ASSERTBACK_NOT: |
| 551 |
|
|
if (!skipassert) return code; |
| 552 |
|
|
do code += GET(code, 1); while (*code == OP_ALT); |
| 553 |
|
|
code += _pcre_OP_lengths[*code]; |
| 554 |
|
|
break; |
| 555 |
|
|
|
| 556 |
|
|
case OP_WORD_BOUNDARY: |
| 557 |
|
|
case OP_NOT_WORD_BOUNDARY: |
| 558 |
|
|
if (!skipassert) return code; |
| 559 |
|
|
/* Fall through */ |
| 560 |
|
|
|
| 561 |
|
|
case OP_CALLOUT: |
| 562 |
|
|
case OP_CREF: |
| 563 |
|
|
case OP_BRANUMBER: |
| 564 |
|
|
code += _pcre_OP_lengths[*code]; |
| 565 |
|
|
break; |
| 566 |
|
|
|
| 567 |
|
|
default: |
| 568 |
|
|
return code; |
| 569 |
|
|
} |
| 570 |
|
|
} |
| 571 |
|
|
/* Control never reaches here */ |
| 572 |
|
|
} |
| 573 |
|
|
|
| 574 |
|
|
|
| 575 |
|
|
|
| 576 |
|
|
|
| 577 |
|
|
/************************************************* |
| 578 |
|
|
* Find the fixed length of a pattern * |
| 579 |
|
|
*************************************************/ |
| 580 |
|
|
|
| 581 |
|
|
/* Scan a pattern and compute the fixed length of subject that will match it, |
| 582 |
|
|
if the length is fixed. This is needed for dealing with backward assertions. |
| 583 |
|
|
In UTF8 mode, the result is in characters rather than bytes. |
| 584 |
|
|
|
| 585 |
|
|
Arguments: |
| 586 |
|
|
code points to the start of the pattern (the bracket) |
| 587 |
|
|
options the compiling options |
| 588 |
|
|
|
| 589 |
|
|
Returns: the fixed length, or -1 if there is no fixed length, |
| 590 |
|
|
or -2 if \C was encountered |
| 591 |
|
|
*/ |
| 592 |
|
|
|
| 593 |
|
|
static int |
| 594 |
|
|
find_fixedlength(uschar *code, int options) |
| 595 |
|
|
{ |
| 596 |
|
|
int length = -1; |
| 597 |
|
|
|
| 598 |
|
|
register int branchlength = 0; |
| 599 |
|
|
register uschar *cc = code + 1 + LINK_SIZE; |
| 600 |
|
|
|
| 601 |
|
|
/* Scan along the opcodes for this branch. If we get to the end of the |
| 602 |
|
|
branch, check the length against that of the other branches. */ |
| 603 |
|
|
|
| 604 |
|
|
for (;;) |
| 605 |
|
|
{ |
| 606 |
|
|
int d; |
| 607 |
|
|
register int op = *cc; |
| 608 |
|
|
if (op >= OP_BRA) op = OP_BRA; |
| 609 |
|
|
|
| 610 |
|
|
switch (op) |
| 611 |
|
|
{ |
| 612 |
|
|
case OP_BRA: |
| 613 |
|
|
case OP_ONCE: |
| 614 |
|
|
case OP_COND: |
| 615 |
|
|
d = find_fixedlength(cc, options); |
| 616 |
|
|
if (d < 0) return d; |
| 617 |
|
|
branchlength += d; |
| 618 |
|
|
do cc += GET(cc, 1); while (*cc == OP_ALT); |
| 619 |
|
|
cc += 1 + LINK_SIZE; |
| 620 |
|
|
break; |
| 621 |
|
|
|
| 622 |
|
|
/* Reached end of a branch; if it's a ket it is the end of a nested |
| 623 |
|
|
call. If it's ALT it is an alternation in a nested call. If it is |
| 624 |
|
|
END it's the end of the outer call. All can be handled by the same code. */ |
| 625 |
|
|
|
| 626 |
|
|
case OP_ALT: |
| 627 |
|
|
case OP_KET: |
| 628 |
|
|
case OP_KETRMAX: |
| 629 |
|
|
case OP_KETRMIN: |
| 630 |
|
|
case OP_END: |
| 631 |
|
|
if (length < 0) length = branchlength; |
| 632 |
|
|
else if (length != branchlength) return -1; |
| 633 |
|
|
if (*cc != OP_ALT) return length; |
| 634 |
|
|
cc += 1 + LINK_SIZE; |
| 635 |
|
|
branchlength = 0; |
| 636 |
|
|
break; |
| 637 |
|
|
|
| 638 |
|
|
/* Skip over assertive subpatterns */ |
| 639 |
|
|
|
| 640 |
|
|
case OP_ASSERT: |
| 641 |
|
|
case OP_ASSERT_NOT: |
| 642 |
|
|
case OP_ASSERTBACK: |
| 643 |
|
|
case OP_ASSERTBACK_NOT: |
| 644 |
|
|
do cc += GET(cc, 1); while (*cc == OP_ALT); |
| 645 |
|
|
/* Fall through */ |
| 646 |
|
|
|
| 647 |
|
|
/* Skip over things that don't match chars */ |
| 648 |
|
|
|
| 649 |
|
|
case OP_REVERSE: |
| 650 |
|
|
case OP_BRANUMBER: |
| 651 |
|
|
case OP_CREF: |
| 652 |
|
|
case OP_OPT: |
| 653 |
|
|
case OP_CALLOUT: |
| 654 |
|
|
case OP_SOD: |
| 655 |
|
|
case OP_SOM: |
| 656 |
|
|
case OP_EOD: |
| 657 |
|
|
case OP_EODN: |
| 658 |
|
|
case OP_CIRC: |
| 659 |
|
|
case OP_DOLL: |
| 660 |
|
|
case OP_NOT_WORD_BOUNDARY: |
| 661 |
|
|
case OP_WORD_BOUNDARY: |
| 662 |
|
|
cc += _pcre_OP_lengths[*cc]; |
| 663 |
|
|
break; |
| 664 |
|
|
|
| 665 |
|
|
/* Handle literal characters */ |
| 666 |
|
|
|
| 667 |
|
|
case OP_CHAR: |
| 668 |
|
|
case OP_CHARNC: |
| 669 |
|
|
branchlength++; |
| 670 |
|
|
cc += 2; |
| 671 |
|
|
break; |
| 672 |
|
|
|
| 673 |
|
|
/* Handle exact repetitions. The count is already in characters, but we |
| 674 |
|
|
need to skip over a multibyte character in UTF8 mode. */ |
| 675 |
|
|
|
| 676 |
|
|
case OP_EXACT: |
| 677 |
|
|
branchlength += GET2(cc,1); |
| 678 |
|
|
cc += 4; |
| 679 |
|
|
break; |
| 680 |
|
|
|
| 681 |
|
|
case OP_TYPEEXACT: |
| 682 |
|
|
branchlength += GET2(cc,1); |
| 683 |
|
|
cc += 4; |
| 684 |
|
|
break; |
| 685 |
|
|
|
| 686 |
|
|
/* Handle single-char matchers */ |
| 687 |
|
|
|
| 688 |
|
|
case OP_PROP: |
| 689 |
|
|
case OP_NOTPROP: |
| 690 |
|
|
cc++; |
| 691 |
|
|
/* Fall through */ |
| 692 |
|
|
|
| 693 |
|
|
case OP_NOT_DIGIT: |
| 694 |
|
|
case OP_DIGIT: |
| 695 |
|
|
case OP_NOT_WHITESPACE: |
| 696 |
|
|
case OP_WHITESPACE: |
| 697 |
|
|
case OP_NOT_WORDCHAR: |
| 698 |
|
|
case OP_WORDCHAR: |
| 699 |
|
|
case OP_ANY: |
| 700 |
|
|
branchlength++; |
| 701 |
|
|
cc++; |
| 702 |
|
|
break; |
| 703 |
|
|
|
| 704 |
|
|
/* The single-byte matcher isn't allowed */ |
| 705 |
|
|
|
| 706 |
|
|
case OP_ANYBYTE: |
| 707 |
|
|
return -2; |
| 708 |
|
|
|
| 709 |
|
|
/* Check a class for variable quantification */ |
| 710 |
|
|
|
| 711 |
|
|
case OP_CLASS: |
| 712 |
|
|
case OP_NCLASS: |
| 713 |
|
|
cc += 33; |
| 714 |
|
|
|
| 715 |
|
|
switch (*cc) |
| 716 |
|
|
{ |
| 717 |
|
|
case OP_CRSTAR: |
| 718 |
|
|
case OP_CRMINSTAR: |
| 719 |
|
|
case OP_CRQUERY: |
| 720 |
|
|
case OP_CRMINQUERY: |
| 721 |
|
|
return -1; |
| 722 |
|
|
|
| 723 |
|
|
case OP_CRRANGE: |
| 724 |
|
|
case OP_CRMINRANGE: |
| 725 |
|
|
if (GET2(cc,1) != GET2(cc,3)) return -1; |
| 726 |
|
|
branchlength += GET2(cc,1); |
| 727 |
|
|
cc += 5; |
| 728 |
|
|
break; |
| 729 |
|
|
|
| 730 |
|
|
default: |
| 731 |
|
|
branchlength++; |
| 732 |
|
|
} |
| 733 |
|
|
break; |
| 734 |
|
|
|
| 735 |
|
|
/* Anything else is variable length */ |
| 736 |
|
|
|
| 737 |
|
|
default: |
| 738 |
|
|
return -1; |
| 739 |
|
|
} |
| 740 |
|
|
} |
| 741 |
|
|
/* Control never gets here */ |
| 742 |
|
|
} |
| 743 |
|
|
|
| 744 |
|
|
|
| 745 |
|
|
|
| 746 |
|
|
|
| 747 |
|
|
/************************************************* |
| 748 |
|
|
* Scan compiled regex for numbered bracket * |
| 749 |
|
|
*************************************************/ |
| 750 |
|
|
|
| 751 |
|
|
/* This little function scans through a compiled pattern until it finds a |
| 752 |
|
|
capturing bracket with the given number. |
| 753 |
|
|
|
| 754 |
|
|
Arguments: |
| 755 |
|
|
code points to start of expression |
| 756 |
|
|
utf8 TRUE in UTF-8 mode |
| 757 |
|
|
number the required bracket number |
| 758 |
|
|
|
| 759 |
|
|
Returns: pointer to the opcode for the bracket, or NULL if not found |
| 760 |
|
|
*/ |
| 761 |
|
|
|
| 762 |
|
|
static const uschar * |
| 763 |
|
|
find_bracket(const uschar *code, BOOL utf8, int number) |
| 764 |
|
|
{ |
| 765 |
|
|
utf8 = utf8; /* Stop pedantic compilers complaining */ |
| 766 |
|
|
|
| 767 |
|
|
for (;;) |
| 768 |
|
|
{ |
| 769 |
|
|
register int c = *code; |
| 770 |
|
|
if (c == OP_END) return NULL; |
| 771 |
|
|
else if (c > OP_BRA) |
| 772 |
|
|
{ |
| 773 |
|
|
int n = c - OP_BRA; |
| 774 |
|
|
if (n > EXTRACT_BASIC_MAX) n = GET2(code, 2+LINK_SIZE); |
| 775 |
|
|
if (n == number) return (uschar *)code; |
| 776 |
|
|
code += _pcre_OP_lengths[OP_BRA]; |
| 777 |
|
|
} |
| 778 |
|
|
else |
| 779 |
|
|
{ |
| 780 |
|
|
code += _pcre_OP_lengths[c]; |
| 781 |
|
|
} |
| 782 |
|
|
} |
| 783 |
|
|
} |
| 784 |
|
|
|
| 785 |
|
|
|
| 786 |
|
|
|
| 787 |
|
|
/************************************************* |
| 788 |
|
|
* Scan compiled regex for recursion reference * |
| 789 |
|
|
*************************************************/ |
| 790 |
|
|
|
| 791 |
|
|
/* This little function scans through a compiled pattern until it finds an |
| 792 |
|
|
instance of OP_RECURSE. |
| 793 |
|
|
|
| 794 |
|
|
Arguments: |
| 795 |
|
|
code points to start of expression |
| 796 |
|
|
utf8 TRUE in UTF-8 mode |
| 797 |
|
|
|
| 798 |
|
|
Returns: pointer to the opcode for OP_RECURSE, or NULL if not found |
| 799 |
|
|
*/ |
| 800 |
|
|
|
| 801 |
|
|
static const uschar * |
| 802 |
|
|
find_recurse(const uschar *code, BOOL utf8) |
| 803 |
|
|
{ |
| 804 |
|
|
utf8 = utf8; /* Stop pedantic compilers complaining */ |
| 805 |
|
|
|
| 806 |
|
|
for (;;) |
| 807 |
|
|
{ |
| 808 |
|
|
register int c = *code; |
| 809 |
|
|
if (c == OP_END) return NULL; |
| 810 |
|
|
else if (c == OP_RECURSE) return code; |
| 811 |
|
|
else if (c > OP_BRA) |
| 812 |
|
|
{ |
| 813 |
|
|
code += _pcre_OP_lengths[OP_BRA]; |
| 814 |
|
|
} |
| 815 |
|
|
else |
| 816 |
|
|
{ |
| 817 |
|
|
code += _pcre_OP_lengths[c]; |
| 818 |
|
|
} |
| 819 |
|
|
} |
| 820 |
|
|
} |
| 821 |
|
|
|
| 822 |
|
|
|
| 823 |
|
|
|
| 824 |
|
|
/************************************************* |
| 825 |
|
|
* Scan compiled branch for non-emptiness * |
| 826 |
|
|
*************************************************/ |
| 827 |
|
|
|
| 828 |
|
|
/* This function scans through a branch of a compiled pattern to see whether it |
| 829 |
|
|
can match the empty string or not. It is called only from could_be_empty() |
| 830 |
|
|
below. Note that first_significant_code() skips over assertions. If we hit an |
| 831 |
|
|
unclosed bracket, we return "empty" - this means we've struck an inner bracket |
| 832 |
|
|
whose current branch will already have been scanned. |
| 833 |
|
|
|
| 834 |
|
|
Arguments: |
| 835 |
|
|
code points to start of search |
| 836 |
|
|
endcode points to where to stop |
| 837 |
|
|
utf8 TRUE if in UTF8 mode |
| 838 |
|
|
|
| 839 |
|
|
Returns: TRUE if what is matched could be empty |
| 840 |
|
|
*/ |
| 841 |
|
|
|
| 842 |
|
|
static BOOL |
| 843 |
|
|
could_be_empty_branch(const uschar *code, const uschar *endcode, BOOL utf8) |
| 844 |
|
|
{ |
| 845 |
|
|
register int c; |
| 846 |
|
|
for (code = first_significant_code(code + 1 + LINK_SIZE, NULL, 0, TRUE); |
| 847 |
|
|
code < endcode; |
| 848 |
|
|
code = first_significant_code(code + _pcre_OP_lengths[c], NULL, 0, TRUE)) |
| 849 |
|
|
{ |
| 850 |
|
|
const uschar *ccode; |
| 851 |
|
|
|
| 852 |
|
|
c = *code; |
| 853 |
|
|
|
| 854 |
|
|
if (c >= OP_BRA) |
| 855 |
|
|
{ |
| 856 |
|
|
BOOL empty_branch; |
| 857 |
|
|
if (GET(code, 1) == 0) return TRUE; /* Hit unclosed bracket */ |
| 858 |
|
|
|
| 859 |
|
|
/* Scan a closed bracket */ |
| 860 |
|
|
|
| 861 |
|
|
empty_branch = FALSE; |
| 862 |
|
|
do |
| 863 |
|
|
{ |
| 864 |
|
|
if (!empty_branch && could_be_empty_branch(code, endcode, utf8)) |
| 865 |
|
|
empty_branch = TRUE; |
| 866 |
|
|
code += GET(code, 1); |
| 867 |
|
|
} |
| 868 |
|
|
while (*code == OP_ALT); |
| 869 |
|
|
if (!empty_branch) return FALSE; /* All branches are non-empty */ |
| 870 |
|
|
code += 1 + LINK_SIZE; |
| 871 |
|
|
c = *code; |
| 872 |
|
|
} |
| 873 |
|
|
|
| 874 |
|
|
else switch (c) |
| 875 |
|
|
{ |
| 876 |
|
|
/* Check for quantifiers after a class */ |
| 877 |
|
|
|
| 878 |
|
|
case OP_CLASS: |
| 879 |
|
|
case OP_NCLASS: |
| 880 |
|
|
ccode = code + 33; |
| 881 |
|
|
|
| 882 |
|
|
switch (*ccode) |
| 883 |
|
|
{ |
| 884 |
|
|
case OP_CRSTAR: /* These could be empty; continue */ |
| 885 |
|
|
case OP_CRMINSTAR: |
| 886 |
|
|
case OP_CRQUERY: |
| 887 |
|
|
case OP_CRMINQUERY: |
| 888 |
|
|
break; |
| 889 |
|
|
|
| 890 |
|
|
default: /* Non-repeat => class must match */ |
| 891 |
|
|
case OP_CRPLUS: /* These repeats aren't empty */ |
| 892 |
|
|
case OP_CRMINPLUS: |
| 893 |
|
|
return FALSE; |
| 894 |
|
|
|
| 895 |
|
|
case OP_CRRANGE: |
| 896 |
|
|
case OP_CRMINRANGE: |
| 897 |
|
|
if (GET2(ccode, 1) > 0) return FALSE; /* Minimum > 0 */ |
| 898 |
|
|
break; |
| 899 |
|
|
} |
| 900 |
|
|
break; |
| 901 |
|
|
|
| 902 |
|
|
/* Opcodes that must match a character */ |
| 903 |
|
|
|
| 904 |
|
|
case OP_PROP: |
| 905 |
|
|
case OP_NOTPROP: |
| 906 |
|
|
case OP_EXTUNI: |
| 907 |
|
|
case OP_NOT_DIGIT: |
| 908 |
|
|
case OP_DIGIT: |
| 909 |
|
|
case OP_NOT_WHITESPACE: |
| 910 |
|
|
case OP_WHITESPACE: |
| 911 |
|
|
case OP_NOT_WORDCHAR: |
| 912 |
|
|
case OP_WORDCHAR: |
| 913 |
|
|
case OP_ANY: |
| 914 |
|
|
case OP_ANYBYTE: |
| 915 |
|
|
case OP_CHAR: |
| 916 |
|
|
case OP_CHARNC: |
| 917 |
|
|
case OP_NOT: |
| 918 |
|
|
case OP_PLUS: |
| 919 |
|
|
case OP_MINPLUS: |
| 920 |
|
|
case OP_EXACT: |
| 921 |
|
|
case OP_NOTPLUS: |
| 922 |
|
|
case OP_NOTMINPLUS: |
| 923 |
|
|
case OP_NOTEXACT: |
| 924 |
|
|
case OP_TYPEPLUS: |
| 925 |
|
|
case OP_TYPEMINPLUS: |
| 926 |
|
|
case OP_TYPEEXACT: |
| 927 |
|
|
return FALSE; |
| 928 |
|
|
|
| 929 |
|
|
/* End of branch */ |
| 930 |
|
|
|
| 931 |
|
|
case OP_KET: |
| 932 |
|
|
case OP_KETRMAX: |
| 933 |
|
|
case OP_KETRMIN: |
| 934 |
|
|
case OP_ALT: |
| 935 |
|
|
return TRUE; |
| 936 |
|
|
} |
| 937 |
|
|
} |
| 938 |
|
|
|
| 939 |
|
|
return TRUE; |
| 940 |
|
|
} |
| 941 |
|
|
|
| 942 |
|
|
|
| 943 |
|
|
|
| 944 |
|
|
/************************************************* |
| 945 |
|
|
* Scan compiled regex for non-emptiness * |
| 946 |
|
|
*************************************************/ |
| 947 |
|
|
|
| 948 |
|
|
/* This function is called to check for left recursive calls. We want to check |
| 949 |
|
|
the current branch of the current pattern to see if it could match the empty |
| 950 |
|
|
string. If it could, we must look outwards for branches at other levels, |
| 951 |
|
|
stopping when we pass beyond the bracket which is the subject of the recursion. |
| 952 |
|
|
|
| 953 |
|
|
Arguments: |
| 954 |
|
|
code points to start of the recursion |
| 955 |
|
|
endcode points to where to stop (current RECURSE item) |
| 956 |
|
|
bcptr points to the chain of current (unclosed) branch starts |
| 957 |
|
|
utf8 TRUE if in UTF-8 mode |
| 958 |
|
|
|
| 959 |
|
|
Returns: TRUE if what is matched could be empty |
| 960 |
|
|
*/ |
| 961 |
|
|
|
| 962 |
|
|
static BOOL |
| 963 |
|
|
could_be_empty(const uschar *code, const uschar *endcode, branch_chain *bcptr, |
| 964 |
|
|
BOOL utf8) |
| 965 |
|
|
{ |
| 966 |
|
|
while (bcptr != NULL && bcptr->current >= code) |
| 967 |
|
|
{ |
| 968 |
|
|
if (!could_be_empty_branch(bcptr->current, endcode, utf8)) return FALSE; |
| 969 |
|
|
bcptr = bcptr->outer; |
| 970 |
|
|
} |
| 971 |
|
|
return TRUE; |
| 972 |
|
|
} |
| 973 |
|
|
|
| 974 |
|
|
|
| 975 |
|
|
|
| 976 |
|
|
/************************************************* |
| 977 |
|
|
* Check for POSIX class syntax * |
| 978 |
|
|
*************************************************/ |
| 979 |
|
|
|
| 980 |
|
|
/* This function is called when the sequence "[:" or "[." or "[=" is |
| 981 |
|
|
encountered in a character class. It checks whether this is followed by an |
| 982 |
|
|
optional ^ and then a sequence of letters, terminated by a matching ":]" or |
| 983 |
|
|
".]" or "=]". |
| 984 |
|
|
|
| 985 |
|
|
Argument: |
| 986 |
|
|
ptr pointer to the initial [ |
| 987 |
|
|
endptr where to return the end pointer |
| 988 |
|
|
cd pointer to compile data |
| 989 |
|
|
|
| 990 |
|
|
Returns: TRUE or FALSE |
| 991 |
|
|
*/ |
| 992 |
|
|
|
| 993 |
|
|
static BOOL |
| 994 |
|
|
check_posix_syntax(const uschar *ptr, const uschar **endptr, compile_data *cd) |
| 995 |
|
|
{ |
| 996 |
|
|
int terminator; /* Don't combine these lines; the Solaris cc */ |
| 997 |
|
|
terminator = *(++ptr); /* compiler warns about "non-constant" initializer. */ |
| 998 |
|
|
if (*(++ptr) == '^') ptr++; |
| 999 |
|
|
while ((cd->ctypes[*ptr] & ctype_letter) != 0) ptr++; |
| 1000 |
|
|
if (*ptr == terminator && ptr[1] == ']') |
| 1001 |
|
|
{ |
| 1002 |
|
|
*endptr = ptr; |
| 1003 |
|
|
return TRUE; |
| 1004 |
|
|
} |
| 1005 |
|
|
return FALSE; |
| 1006 |
|
|
} |
| 1007 |
|
|
|
| 1008 |
|
|
|
| 1009 |
|
|
|
| 1010 |
|
|
|
| 1011 |
|
|
/************************************************* |
| 1012 |
|
|
* Check POSIX class name * |
| 1013 |
|
|
*************************************************/ |
| 1014 |
|
|
|
| 1015 |
|
|
/* This function is called to check the name given in a POSIX-style class entry |
| 1016 |
|
|
such as [:alnum:]. |
| 1017 |
|
|
|
| 1018 |
|
|
Arguments: |
| 1019 |
|
|
ptr points to the first letter |
| 1020 |
|
|
len the length of the name |
| 1021 |
|
|
|
| 1022 |
|
|
Returns: a value representing the name, or -1 if unknown |
| 1023 |
|
|
*/ |
| 1024 |
|
|
|
| 1025 |
|
|
static int |
| 1026 |
|
|
check_posix_name(const uschar *ptr, int len) |
| 1027 |
|
|
{ |
| 1028 |
|
|
register int yield = 0; |
| 1029 |
|
|
while (posix_name_lengths[yield] != 0) |
| 1030 |
|
|
{ |
| 1031 |
|
|
if (len == posix_name_lengths[yield] && |
| 1032 |
|
|
strncmp((const char *)ptr, posix_names[yield], len) == 0) return yield; |
| 1033 |
|
|
yield++; |
| 1034 |
|
|
} |
| 1035 |
|
|
return -1; |
| 1036 |
|
|
} |
| 1037 |
|
|
|
| 1038 |
|
|
|
| 1039 |
|
|
/************************************************* |
| 1040 |
|
|
* Adjust OP_RECURSE items in repeated group * |
| 1041 |
|
|
*************************************************/ |
| 1042 |
|
|
|
| 1043 |
|
|
/* OP_RECURSE items contain an offset from the start of the regex to the group |
| 1044 |
|
|
that is referenced. This means that groups can be replicated for fixed |
| 1045 |
|
|
repetition simply by copying (because the recursion is allowed to refer to |
| 1046 |
|
|
earlier groups that are outside the current group). However, when a group is |
| 1047 |
|
|
optional (i.e. the minimum quantifier is zero), OP_BRAZERO is inserted before |
| 1048 |
|
|
it, after it has been compiled. This means that any OP_RECURSE items within it |
| 1049 |
|
|
that refer to the group itself or any contained groups have to have their |
| 1050 |
|
|
offsets adjusted. That is the job of this function. Before it is called, the |
| 1051 |
|
|
partially compiled regex must be temporarily terminated with OP_END. |
| 1052 |
|
|
|
| 1053 |
|
|
Arguments: |
| 1054 |
|
|
group points to the start of the group |
| 1055 |
|
|
adjust the amount by which the group is to be moved |
| 1056 |
|
|
utf8 TRUE in UTF-8 mode |
| 1057 |
|
|
cd contains pointers to tables etc. |
| 1058 |
|
|
|
| 1059 |
|
|
Returns: nothing |
| 1060 |
|
|
*/ |
| 1061 |
|
|
|
| 1062 |
|
|
static void |
| 1063 |
|
|
adjust_recurse(uschar *group, int adjust, BOOL utf8, compile_data *cd) |
| 1064 |
|
|
{ |
| 1065 |
|
|
uschar *ptr = group; |
| 1066 |
|
|
while ((ptr = (uschar *)find_recurse(ptr, utf8)) != NULL) |
| 1067 |
|
|
{ |
| 1068 |
|
|
int offset = GET(ptr, 1); |
| 1069 |
|
|
if (cd->start_code + offset >= group) PUT(ptr, 1, offset + adjust); |
| 1070 |
|
|
ptr += 1 + LINK_SIZE; |
| 1071 |
|
|
} |
| 1072 |
|
|
} |
| 1073 |
|
|
|
| 1074 |
|
|
|
| 1075 |
|
|
|
| 1076 |
|
|
/************************************************* |
| 1077 |
|
|
* Insert an automatic callout point * |
| 1078 |
|
|
*************************************************/ |
| 1079 |
|
|
|
| 1080 |
|
|
/* This function is called when the PCRE_AUTO_CALLOUT option is set, to insert |
| 1081 |
|
|
callout points before each pattern item. |
| 1082 |
|
|
|
| 1083 |
|
|
Arguments: |
| 1084 |
|
|
code current code pointer |
| 1085 |
|
|
ptr current pattern pointer |
| 1086 |
|
|
cd pointers to tables etc |
| 1087 |
|
|
|
| 1088 |
|
|
Returns: new code pointer |
| 1089 |
|
|
*/ |
| 1090 |
|
|
|
| 1091 |
|
|
static uschar * |
| 1092 |
|
|
auto_callout(uschar *code, const uschar *ptr, compile_data *cd) |
| 1093 |
|
|
{ |
| 1094 |
|
|
*code++ = OP_CALLOUT; |
| 1095 |
|
|
*code++ = 255; |
| 1096 |
|
|
PUT(code, 0, ptr - cd->start_pattern); /* Pattern offset */ |
| 1097 |
|
|
PUT(code, LINK_SIZE, 0); /* Default length */ |
| 1098 |
|
|
return code + 2*LINK_SIZE; |
| 1099 |
|
|
} |
| 1100 |
|
|
|
| 1101 |
|
|
|
| 1102 |
|
|
|
| 1103 |
|
|
/************************************************* |
| 1104 |
|
|
* Complete a callout item * |
| 1105 |
|
|
*************************************************/ |
| 1106 |
|
|
|
| 1107 |
|
|
/* A callout item contains the length of the next item in the pattern, which |
| 1108 |
|
|
we can't fill in till after we have reached the relevant point. This is used |
| 1109 |
|
|
for both automatic and manual callouts. |
| 1110 |
|
|
|
| 1111 |
|
|
Arguments: |
| 1112 |
|
|
previous_callout points to previous callout item |
| 1113 |
|
|
ptr current pattern pointer |
| 1114 |
|
|
cd pointers to tables etc |
| 1115 |
|
|
|
| 1116 |
|
|
Returns: nothing |
| 1117 |
|
|
*/ |
| 1118 |
|
|
|
| 1119 |
|
|
static void |
| 1120 |
|
|
complete_callout(uschar *previous_callout, const uschar *ptr, compile_data *cd) |
| 1121 |
|
|
{ |
| 1122 |
|
|
int length = ptr - cd->start_pattern - GET(previous_callout, 2); |
| 1123 |
|
|
PUT(previous_callout, 2 + LINK_SIZE, length); |
| 1124 |
|
|
} |
| 1125 |
|
|
|
| 1126 |
|
|
|
| 1127 |
|
|
|
| 1128 |
|
|
/************************************************* |
| 1129 |
|
|
* Compile one branch * |
| 1130 |
|
|
*************************************************/ |
| 1131 |
|
|
|
| 1132 |
|
|
/* Scan the pattern, compiling it into the code vector. If the options are |
| 1133 |
|
|
changed during the branch, the pointer is used to change the external options |
| 1134 |
|
|
bits. |
| 1135 |
|
|
|
| 1136 |
|
|
Arguments: |
| 1137 |
|
|
optionsptr pointer to the option bits |
| 1138 |
|
|
brackets points to number of extracting brackets used |
| 1139 |
|
|
codeptr points to the pointer to the current code point |
| 1140 |
|
|
ptrptr points to the current pattern pointer |
| 1141 |
|
|
errorcodeptr points to error code variable |
| 1142 |
|
|
firstbyteptr set to initial literal character, or < 0 (REQ_UNSET, REQ_NONE) |
| 1143 |
|
|
reqbyteptr set to the last literal character required, else < 0 |
| 1144 |
|
|
bcptr points to current branch chain |
| 1145 |
|
|
cd contains pointers to tables etc. |
| 1146 |
|
|
|
| 1147 |
|
|
Returns: TRUE on success |
| 1148 |
|
|
FALSE, with *errorcodeptr set non-zero on error |
| 1149 |
|
|
*/ |
| 1150 |
|
|
|
| 1151 |
|
|
static BOOL |
| 1152 |
|
|
compile_branch(int *optionsptr, int *brackets, uschar **codeptr, |
| 1153 |
|
|
const uschar **ptrptr, int *errorcodeptr, int *firstbyteptr, |
| 1154 |
|
|
int *reqbyteptr, branch_chain *bcptr, compile_data *cd) |
| 1155 |
|
|
{ |
| 1156 |
|
|
int repeat_type, op_type; |
| 1157 |
|
|
int repeat_min = 0, repeat_max = 0; /* To please picky compilers */ |
| 1158 |
|
|
int bravalue = 0; |
| 1159 |
|
|
int greedy_default, greedy_non_default; |
| 1160 |
|
|
int firstbyte, reqbyte; |
| 1161 |
|
|
int zeroreqbyte, zerofirstbyte; |
| 1162 |
|
|
int req_caseopt, reqvary, tempreqvary; |
| 1163 |
|
|
int condcount = 0; |
| 1164 |
|
|
int options = *optionsptr; |
| 1165 |
|
|
int after_manual_callout = 0; |
| 1166 |
|
|
register int c; |
| 1167 |
|
|
register uschar *code = *codeptr; |
| 1168 |
|
|
uschar *tempcode; |
| 1169 |
|
|
BOOL inescq = FALSE; |
| 1170 |
|
|
BOOL groupsetfirstbyte = FALSE; |
| 1171 |
|
|
const uschar *ptr = *ptrptr; |
| 1172 |
|
|
const uschar *tempptr; |
| 1173 |
|
|
uschar *previous = NULL; |
| 1174 |
|
|
uschar *previous_callout = NULL; |
| 1175 |
|
|
uschar classbits[32]; |
| 1176 |
|
|
|
| 1177 |
|
|
BOOL utf8 = FALSE; |
| 1178 |
|
|
|
| 1179 |
|
|
/* Set up the default and non-default settings for greediness */ |
| 1180 |
|
|
|
| 1181 |
|
|
greedy_default = ((options & PCRE_UNGREEDY) != 0); |
| 1182 |
|
|
greedy_non_default = greedy_default ^ 1; |
| 1183 |
|
|
|
| 1184 |
|
|
/* Initialize no first byte, no required byte. REQ_UNSET means "no char |
| 1185 |
|
|
matching encountered yet". It gets changed to REQ_NONE if we hit something that |
| 1186 |
|
|
matches a non-fixed char first char; reqbyte just remains unset if we never |
| 1187 |
|
|
find one. |
| 1188 |
|
|
|
| 1189 |
|
|
When we hit a repeat whose minimum is zero, we may have to adjust these values |
| 1190 |
|
|
to take the zero repeat into account. This is implemented by setting them to |
| 1191 |
|
|
zerofirstbyte and zeroreqbyte when such a repeat is encountered. The individual |
| 1192 |
|
|
item types that can be repeated set these backoff variables appropriately. */ |
| 1193 |
|
|
|
| 1194 |
|
|
firstbyte = reqbyte = zerofirstbyte = zeroreqbyte = REQ_UNSET; |
| 1195 |
|
|
|
| 1196 |
|
|
/* The variable req_caseopt contains either the REQ_CASELESS value or zero, |
| 1197 |
|
|
according to the current setting of the caseless flag. REQ_CASELESS is a bit |
| 1198 |
|
|
value > 255. It is added into the firstbyte or reqbyte variables to record the |
| 1199 |
|
|
case status of the value. This is used only for ASCII characters. */ |
| 1200 |
|
|
|
| 1201 |
|
|
req_caseopt = ((options & PCRE_CASELESS) != 0)? REQ_CASELESS : 0; |
| 1202 |
|
|
|
| 1203 |
|
|
/* Switch on next character until the end of the branch */ |
| 1204 |
|
|
|
| 1205 |
|
|
for (;; ptr++) |
| 1206 |
|
|
{ |
| 1207 |
|
|
BOOL negate_class; |
| 1208 |
|
|
BOOL possessive_quantifier; |
| 1209 |
|
|
BOOL is_quantifier; |
| 1210 |
|
|
int class_charcount; |
| 1211 |
|
|
int class_lastchar; |
| 1212 |
|
|
int newoptions; |
| 1213 |
|
|
int recno; |
| 1214 |
|
|
int skipbytes; |
| 1215 |
|
|
int subreqbyte; |
| 1216 |
|
|
int subfirstbyte; |
| 1217 |
|
|
int mclength; |
| 1218 |
|
|
uschar mcbuffer[8]; |
| 1219 |
|
|
|
| 1220 |
|
|
/* Next byte in the pattern */ |
| 1221 |
|
|
|
| 1222 |
|
|
c = *ptr; |
| 1223 |
|
|
|
| 1224 |
|
|
/* If in \Q...\E, check for the end; if not, we have a literal */ |
| 1225 |
|
|
|
| 1226 |
|
|
if (inescq && c != 0) |
| 1227 |
|
|
{ |
| 1228 |
|
|
if (c == '\\' && ptr[1] == 'E') |
| 1229 |
|
|
{ |
| 1230 |
|
|
inescq = FALSE; |
| 1231 |
|
|
ptr++; |
| 1232 |
|
|
continue; |
| 1233 |
|
|
} |
| 1234 |
|
|
else |
| 1235 |
|
|
{ |
| 1236 |
|
|
if (previous_callout != NULL) |
| 1237 |
|
|
{ |
| 1238 |
|
|
complete_callout(previous_callout, ptr, cd); |
| 1239 |
|
|
previous_callout = NULL; |
| 1240 |
|
|
} |
| 1241 |
|
|
if ((options & PCRE_AUTO_CALLOUT) != 0) |
| 1242 |
|
|
{ |
| 1243 |
|
|
previous_callout = code; |
| 1244 |
|
|
code = auto_callout(code, ptr, cd); |
| 1245 |
|
|
} |
| 1246 |
|
|
goto NORMAL_CHAR; |
| 1247 |
|
|
} |
| 1248 |
|
|
} |
| 1249 |
|
|
|
| 1250 |
|
|
/* Fill in length of a previous callout, except when the next thing is |
| 1251 |
|
|
a quantifier. */ |
| 1252 |
|
|
|
| 1253 |
|
|
is_quantifier = c == '*' || c == '+' || c == '?' || |
| 1254 |
|
|
(c == '{' && is_counted_repeat(ptr+1)); |
| 1255 |
|
|
|
| 1256 |
|
|
if (!is_quantifier && previous_callout != NULL && |
| 1257 |
|
|
after_manual_callout-- <= 0) |
| 1258 |
|
|
{ |
| 1259 |
|
|
complete_callout(previous_callout, ptr, cd); |
| 1260 |
|
|
previous_callout = NULL; |
| 1261 |
|
|
} |
| 1262 |
|
|
|
| 1263 |
|
|
/* In extended mode, skip white space and comments */ |
| 1264 |
|
|
|
| 1265 |
|
|
if ((options & PCRE_EXTENDED) != 0) |
| 1266 |
|
|
{ |
| 1267 |
|
|
if ((cd->ctypes[c] & ctype_space) != 0) continue; |
| 1268 |
|
|
if (c == '#') |
| 1269 |
|
|
{ |
| 1270 |
|
|
/* The space before the ; is to avoid a warning on a silly compiler |
| 1271 |
|
|
on the Macintosh. */ |
| 1272 |
|
|
while ((c = *(++ptr)) != 0 && c != NEWLINE) ; |
| 1273 |
|
|
if (c != 0) continue; /* Else fall through to handle end of string */ |
| 1274 |
|
|
} |
| 1275 |
|
|
} |
| 1276 |
|
|
|
| 1277 |
|
|
/* No auto callout for quantifiers. */ |
| 1278 |
|
|
|
| 1279 |
|
|
if ((options & PCRE_AUTO_CALLOUT) != 0 && !is_quantifier) |
| 1280 |
|
|
{ |
| 1281 |
|
|
previous_callout = code; |
| 1282 |
|
|
code = auto_callout(code, ptr, cd); |
| 1283 |
|
|
} |
| 1284 |
|
|
|
| 1285 |
|
|
switch(c) |
| 1286 |
|
|
{ |
| 1287 |
|
|
/* The branch terminates at end of string, |, or ). */ |
| 1288 |
|
|
|
| 1289 |
|
|
case 0: |
| 1290 |
|
|
case '|': |
| 1291 |
|
|
case ')': |
| 1292 |
|
|
*firstbyteptr = firstbyte; |
| 1293 |
|
|
*reqbyteptr = reqbyte; |
| 1294 |
|
|
*codeptr = code; |
| 1295 |
|
|
*ptrptr = ptr; |
| 1296 |
|
|
return TRUE; |
| 1297 |
|
|
|
| 1298 |
|
|
/* Handle single-character metacharacters. In multiline mode, ^ disables |
| 1299 |
|
|
the setting of any following char as a first character. */ |
| 1300 |
|
|
|
| 1301 |
|
|
case '^': |
| 1302 |
|
|
if ((options & PCRE_MULTILINE) != 0) |
| 1303 |
|
|
{ |
| 1304 |
|
|
if (firstbyte == REQ_UNSET) firstbyte = REQ_NONE; |
| 1305 |
|
|
} |
| 1306 |
|
|
previous = NULL; |
| 1307 |
|
|
*code++ = OP_CIRC; |
| 1308 |
|
|
break; |
| 1309 |
|
|
|
| 1310 |
|
|
case '$': |
| 1311 |
|
|
previous = NULL; |
| 1312 |
|
|
*code++ = OP_DOLL; |
| 1313 |
|
|
break; |
| 1314 |
|
|
|
| 1315 |
|
|
/* There can never be a first char if '.' is first, whatever happens about |
| 1316 |
|
|
repeats. The value of reqbyte doesn't change either. */ |
| 1317 |
|
|
|
| 1318 |
|
|
case '.': |
| 1319 |
|
|
if (firstbyte == REQ_UNSET) firstbyte = REQ_NONE; |
| 1320 |
|
|
zerofirstbyte = firstbyte; |
| 1321 |
|
|
zeroreqbyte = reqbyte; |
| 1322 |
|
|
previous = code; |
| 1323 |
|
|
*code++ = OP_ANY; |
| 1324 |
|
|
break; |
| 1325 |
|
|
|
| 1326 |
|
|
/* Character classes. If the included characters are all < 255 in value, we |
| 1327 |
|
|
build a 32-byte bitmap of the permitted characters, except in the special |
| 1328 |
|
|
case where there is only one such character. For negated classes, we build |
| 1329 |
|
|
the map as usual, then invert it at the end. However, we use a different |
| 1330 |
|
|
opcode so that data characters > 255 can be handled correctly. |
| 1331 |
|
|
|
| 1332 |
|
|
If the class contains characters outside the 0-255 range, a different |
| 1333 |
|
|
opcode is compiled. It may optionally have a bit map for characters < 256, |
| 1334 |
|
|
but those above are are explicitly listed afterwards. A flag byte tells |
| 1335 |
|
|
whether the bitmap is present, and whether this is a negated class or not. |
| 1336 |
|
|
*/ |
| 1337 |
|
|
|
| 1338 |
|
|
case '[': |
| 1339 |
|
|
previous = code; |
| 1340 |
|
|
|
| 1341 |
|
|
/* PCRE supports POSIX class stuff inside a class. Perl gives an error if |
| 1342 |
|
|
they are encountered at the top level, so we'll do that too. */ |
| 1343 |
|
|
|
| 1344 |
|
|
if ((ptr[1] == ':' || ptr[1] == '.' || ptr[1] == '=') && |
| 1345 |
|
|
check_posix_syntax(ptr, &tempptr, cd)) |
| 1346 |
|
|
{ |
| 1347 |
|
|
*errorcodeptr = (ptr[1] == ':')? ERR13 : ERR31; |
| 1348 |
|
|
goto FAILED; |
| 1349 |
|
|
} |
| 1350 |
|
|
|
| 1351 |
|
|
/* If the first character is '^', set the negation flag and skip it. */ |
| 1352 |
|
|
|
| 1353 |
|
|
if ((c = *(++ptr)) == '^') |
| 1354 |
|
|
{ |
| 1355 |
|
|
negate_class = TRUE; |
| 1356 |
|
|
c = *(++ptr); |
| 1357 |
|
|
} |
| 1358 |
|
|
else |
| 1359 |
|
|
{ |
| 1360 |
|
|
negate_class = FALSE; |
| 1361 |
|
|
} |
| 1362 |
|
|
|
| 1363 |
|
|
/* Keep a count of chars with values < 256 so that we can optimize the case |
| 1364 |
|
|
of just a single character (as long as it's < 256). For higher valued UTF-8 |
| 1365 |
|
|
characters, we don't yet do any optimization. */ |
| 1366 |
|
|
|
| 1367 |
|
|
class_charcount = 0; |
| 1368 |
|
|
class_lastchar = -1; |
| 1369 |
|
|
|
| 1370 |
|
|
/* Initialize the 32-char bit map to all zeros. We have to build the |
| 1371 |
|
|
map in a temporary bit of store, in case the class contains only 1 |
| 1372 |
|
|
character (< 256), because in that case the compiled code doesn't use the |
| 1373 |
|
|
bit map. */ |
| 1374 |
|
|
|
| 1375 |
|
|
memset(classbits, 0, 32 * sizeof(uschar)); |
| 1376 |
|
|
|
| 1377 |
|
|
/* Process characters until ] is reached. By writing this as a "do" it |
| 1378 |
|
|
means that an initial ] is taken as a data character. The first pass |
| 1379 |
|
|
through the regex checked the overall syntax, so we don't need to be very |
| 1380 |
|
|
strict here. At the start of the loop, c contains the first byte of the |
| 1381 |
|
|
character. */ |
| 1382 |
|
|
|
| 1383 |
|
|
do |
| 1384 |
|
|
{ |
| 1385 |
|
|
|
| 1386 |
|
|
/* Inside \Q...\E everything is literal except \E */ |
| 1387 |
|
|
|
| 1388 |
|
|
if (inescq) |
| 1389 |
|
|
{ |
| 1390 |
|
|
if (c == '\\' && ptr[1] == 'E') |
| 1391 |
|
|
{ |
| 1392 |
|
|
inescq = FALSE; |
| 1393 |
|
|
ptr++; |
| 1394 |
|
|
continue; |
| 1395 |
|
|
} |
| 1396 |
|
|
else goto LONE_SINGLE_CHARACTER; |
| 1397 |
|
|
} |
| 1398 |
|
|
|
| 1399 |
|
|
/* Handle POSIX class names. Perl allows a negation extension of the |
| 1400 |
|
|
form [:^name:]. A square bracket that doesn't match the syntax is |
| 1401 |
|
|
treated as a literal. We also recognize the POSIX constructions |
| 1402 |
|
|
[.ch.] and [=ch=] ("collating elements") and fault them, as Perl |
| 1403 |
|
|
5.6 and 5.8 do. */ |
| 1404 |
|
|
|
| 1405 |
|
|
if (c == '[' && |
| 1406 |
|
|
(ptr[1] == ':' || ptr[1] == '.' || ptr[1] == '=') && |
| 1407 |
|
|
check_posix_syntax(ptr, &tempptr, cd)) |
| 1408 |
|
|
{ |
| 1409 |
|
|
BOOL local_negate = FALSE; |
| 1410 |
|
|
int posix_class, i; |
| 1411 |
|
|
register const uschar *cbits = cd->cbits; |
| 1412 |
|
|
|
| 1413 |
|
|
if (ptr[1] != ':') |
| 1414 |
|
|
{ |
| 1415 |
|
|
*errorcodeptr = ERR31; |
| 1416 |
|
|
goto FAILED; |
| 1417 |
|
|
} |
| 1418 |
|
|
|
| 1419 |
|
|
ptr += 2; |
| 1420 |
|
|
if (*ptr == '^') |
| 1421 |
|
|
{ |
| 1422 |
|
|
local_negate = TRUE; |
| 1423 |
|
|
ptr++; |
| 1424 |
|
|
} |
| 1425 |
|
|
|
| 1426 |
|
|
posix_class = check_posix_name(ptr, tempptr - ptr); |
| 1427 |
|
|
if (posix_class < 0) |
| 1428 |
|
|
{ |
| 1429 |
|
|
*errorcodeptr = ERR30; |
| 1430 |
|
|
goto FAILED; |
| 1431 |
|
|
} |
| 1432 |
|
|
|
| 1433 |
|
|
/* If matching is caseless, upper and lower are converted to |
| 1434 |
|
|
alpha. This relies on the fact that the class table starts with |
| 1435 |
|
|
alpha, lower, upper as the first 3 entries. */ |
| 1436 |
|
|
|
| 1437 |
|
|
if ((options & PCRE_CASELESS) != 0 && posix_class <= 2) |
| 1438 |
|
|
posix_class = 0; |
| 1439 |
|
|
|
| 1440 |
|
|
/* Or into the map we are building up to 3 of the static class |
| 1441 |
|
|
tables, or their negations. The [:blank:] class sets up the same |
| 1442 |
|
|
chars as the [:space:] class (all white space). We remove the vertical |
| 1443 |
|
|
white space chars afterwards. */ |
| 1444 |
|
|
|
| 1445 |
|
|
posix_class *= 3; |
| 1446 |
|
|
for (i = 0; i < 3; i++) |
| 1447 |
|
|
{ |
| 1448 |
|
|
BOOL blankclass = strncmp((char *)ptr, "blank", 5) == 0; |
| 1449 |
|
|
int taboffset = posix_class_maps[posix_class + i]; |
| 1450 |
|
|
if (taboffset < 0) break; |
| 1451 |
|
|
if (local_negate) |
| 1452 |
|
|
{ |
| 1453 |
|
|
if (i == 0) |
| 1454 |
|
|
for (c = 0; c < 32; c++) classbits[c] |= ~cbits[c+taboffset]; |
| 1455 |
|
|
else |
| 1456 |
|
|
for (c = 0; c < 32; c++) classbits[c] &= ~cbits[c+taboffset]; |
| 1457 |
|
|
if (blankclass) classbits[1] |= 0x3c; |
| 1458 |
|
|
} |
| 1459 |
|
|
else |
| 1460 |
|
|
{ |
| 1461 |
|
|
for (c = 0; c < 32; c++) classbits[c] |= cbits[c+taboffset]; |
| 1462 |
|
|
if (blankclass) classbits[1] &= ~0x3c; |
| 1463 |
|
|
} |
| 1464 |
|
|
} |
| 1465 |
|
|
|
| 1466 |
|
|
ptr = tempptr + 1; |
| 1467 |
|
|
class_charcount = 10; /* Set > 1; assumes more than 1 per class */ |
| 1468 |
|
|
continue; /* End of POSIX syntax handling */ |
| 1469 |
|
|
} |
| 1470 |
|
|
|
| 1471 |
|
|
/* Backslash may introduce a single character, or it may introduce one |
| 1472 |
|
|
of the specials, which just set a flag. Escaped items are checked for |
| 1473 |
|
|
validity in the pre-compiling pass. The sequence \b is a special case. |
| 1474 |
|
|
Inside a class (and only there) it is treated as backspace. Elsewhere |
| 1475 |
|
|
it marks a word boundary. Other escapes have preset maps ready to |
| 1476 |
|
|
or into the one we are building. We assume they have more than one |
| 1477 |
|
|
character in them, so set class_charcount bigger than one. */ |
| 1478 |
|
|
|
| 1479 |
|
|
if (c == '\\') |
| 1480 |
|
|
{ |
| 1481 |
|
|
c = check_escape(&ptr, errorcodeptr, *brackets, options, TRUE); |
| 1482 |
|
|
|
| 1483 |
|
|
if (-c == ESC_b) c = '\b'; /* \b is backslash in a class */ |
| 1484 |
|
|
else if (-c == ESC_X) c = 'X'; /* \X is literal X in a class */ |
| 1485 |
|
|
else if (-c == ESC_Q) /* Handle start of quoted string */ |
| 1486 |
|
|
{ |
| 1487 |
|
|
if (ptr[1] == '\\' && ptr[2] == 'E') |
| 1488 |
|
|
{ |
| 1489 |
|
|
ptr += 2; /* avoid empty string */ |
| 1490 |
|
|
} |
| 1491 |
|
|
else inescq = TRUE; |
| 1492 |
|
|
continue; |
| 1493 |
|
|
} |
| 1494 |
|
|
|
| 1495 |
|
|
if (c < 0) |
| 1496 |
|
|
{ |
| 1497 |
|
|
register const uschar *cbits = cd->cbits; |
| 1498 |
|
|
class_charcount += 2; /* Greater than 1 is what matters */ |
| 1499 |
|
|
switch (-c) |
| 1500 |
|
|
{ |
| 1501 |
|
|
case ESC_d: |
| 1502 |
|
|
for (c = 0; c < 32; c++) classbits[c] |= cbits[c+cbit_digit]; |
| 1503 |
|
|
continue; |
| 1504 |
|
|
|
| 1505 |
|
|
case ESC_D: |
| 1506 |
|
|
for (c = 0; c < 32; c++) classbits[c] |= ~cbits[c+cbit_digit]; |
| 1507 |
|
|
continue; |
| 1508 |
|
|
|
| 1509 |
|
|
case ESC_w: |
| 1510 |
|
|
for (c = 0; c < 32; c++) classbits[c] |= cbits[c+cbit_word]; |
| 1511 |
|
|
continue; |
| 1512 |
|
|
|
| 1513 |
|
|
case ESC_W: |
| 1514 |
|
|
for (c = 0; c < 32; c++) classbits[c] |= ~cbits[c+cbit_word]; |
| 1515 |
|
|
continue; |
| 1516 |
|
|
|
| 1517 |
|
|
case ESC_s: |
| 1518 |
|
|
for (c = 0; c < 32; c++) classbits[c] |= cbits[c+cbit_space]; |
| 1519 |
|
|
classbits[1] &= ~0x08; /* Perl 5.004 onwards omits VT from \s */ |
| 1520 |
|
|
continue; |
| 1521 |
|
|
|
| 1522 |
|
|
case ESC_S: |
| 1523 |
|
|
for (c = 0; c < 32; c++) classbits[c] |= ~cbits[c+cbit_space]; |
| 1524 |
|
|
classbits[1] |= 0x08; /* Perl 5.004 onwards omits VT from \s */ |
| 1525 |
|
|
continue; |
| 1526 |
|
|
|
| 1527 |
|
|
/* Unrecognized escapes are faulted if PCRE is running in its |
| 1528 |
|
|
strict mode. By default, for compatibility with Perl, they are |
| 1529 |
|
|
treated as literals. */ |
| 1530 |
|
|
|
| 1531 |
|
|
default: |
| 1532 |
|
|
if ((options & PCRE_EXTRA) != 0) |
| 1533 |
|
|
{ |
| 1534 |
|
|
*errorcodeptr = ERR7; |
| 1535 |
|
|
goto FAILED; |
| 1536 |
|
|
} |
| 1537 |
|
|
c = *ptr; /* The final character */ |
| 1538 |
|
|
class_charcount -= 2; /* Undo the default count from above */ |
| 1539 |
|
|
} |
| 1540 |
|
|
} |
| 1541 |
|
|
|
| 1542 |
|
|
/* Fall through if we have a single character (c >= 0). This may be |
| 1543 |
|
|
> 256 in UTF-8 mode. */ |
| 1544 |
|
|
|
| 1545 |
|
|
} /* End of backslash handling */ |
| 1546 |
|
|
|
| 1547 |
|
|
/* A single character may be followed by '-' to form a range. However, |
| 1548 |
|
|
Perl does not permit ']' to be the end of the range. A '-' character |
| 1549 |
|
|
here is treated as a literal. */ |
| 1550 |
|
|
|
| 1551 |
|
|
if (ptr[1] == '-' && ptr[2] != ']') |
| 1552 |
|
|
{ |
| 1553 |
|
|
int d; |
| 1554 |
|
|
ptr += 2; |
| 1555 |
|
|
|
| 1556 |
|
|
d = *ptr; /* Not UTF-8 mode */ |
| 1557 |
|
|
|
| 1558 |
|
|
/* The second part of a range can be a single-character escape, but |
| 1559 |
|
|
not any of the other escapes. Perl 5.6 treats a hyphen as a literal |
| 1560 |
|
|
in such circumstances. */ |
| 1561 |
|
|
|
| 1562 |
|
|
if (d == '\\') |
| 1563 |
|
|
{ |
| 1564 |
|
|
const uschar *oldptr = ptr; |
| 1565 |
|
|
d = check_escape(&ptr, errorcodeptr, *brackets, options, TRUE); |
| 1566 |
|
|
|
| 1567 |
|
|
/* \b is backslash; \X is literal X; any other special means the '-' |
| 1568 |
|
|
was literal */ |
| 1569 |
|
|
|
| 1570 |
|
|
if (d < 0) |
| 1571 |
|
|
{ |
| 1572 |
|
|
if (d == -ESC_b) d = '\b'; |
| 1573 |
|
|
else if (d == -ESC_X) d = 'X'; else |
| 1574 |
|
|
{ |
| 1575 |
|
|
ptr = oldptr - 2; |
| 1576 |
|
|
goto LONE_SINGLE_CHARACTER; /* A few lines below */ |
| 1577 |
|
|
} |
| 1578 |
|
|
} |
| 1579 |
|
|
} |
| 1580 |
|
|
|
| 1581 |
|
|
/* The check that the two values are in the correct order happens in |
| 1582 |
|
|
the pre-pass. Optimize one-character ranges */ |
| 1583 |
|
|
|
| 1584 |
|
|
if (d == c) goto LONE_SINGLE_CHARACTER; /* A few lines below */ |
| 1585 |
|
|
|
| 1586 |
|
|
|
| 1587 |
|
|
/* We use the bit map for all cases when not in UTF-8 mode; else |
| 1588 |
|
|
ranges that lie entirely within 0-127 when there is UCP support; else |
| 1589 |
|
|
for partial ranges without UCP support. */ |
| 1590 |
|
|
|
| 1591 |
|
|
for (; c <= d; c++) |
| 1592 |
|
|
{ |
| 1593 |
|
|
classbits[c/8] |= (1 << (c&7)); |
| 1594 |
|
|
if ((options & PCRE_CASELESS) != 0) |
| 1595 |
|
|
{ |
| 1596 |
|
|
int uc = cd->fcc[c]; /* flip case */ |
| 1597 |
|
|
classbits[uc/8] |= (1 << (uc&7)); |
| 1598 |
|
|
} |
| 1599 |
|
|
class_charcount++; /* in case a one-char range */ |
| 1600 |
|
|
class_lastchar = c; |
| 1601 |
|
|
} |
| 1602 |
|
|
|
| 1603 |
|
|
continue; /* Go get the next char in the class */ |
| 1604 |
|
|
} |
| 1605 |
|
|
|
| 1606 |
|
|
/* Handle a lone single character - we can get here for a normal |
| 1607 |
|
|
non-escape char, or after \ that introduces a single character or for an |
| 1608 |
|
|
apparent range that isn't. */ |
| 1609 |
|
|
|
| 1610 |
|
|
LONE_SINGLE_CHARACTER: |
| 1611 |
|
|
|
| 1612 |
|
|
/* Handle a single-byte character */ |
| 1613 |
|
|
{ |
| 1614 |
|
|
classbits[c/8] |= (1 << (c&7)); |
| 1615 |
|
|
if ((options & PCRE_CASELESS) != 0) |
| 1616 |
|
|
{ |
| 1617 |
|
|
c = cd->fcc[c]; /* flip case */ |
| 1618 |
|
|
classbits[c/8] |= (1 << (c&7)); |
| 1619 |
|
|
} |
| 1620 |
|
|
class_charcount++; |
| 1621 |
|
|
class_lastchar = c; |
| 1622 |
|
|
} |
| 1623 |
|
|
} |
| 1624 |
|
|
|
| 1625 |
|
|
/* Loop until ']' reached; the check for end of string happens inside the |
| 1626 |
|
|
loop. This "while" is the end of the "do" above. */ |
| 1627 |
|
|
|
| 1628 |
|
|
while ((c = *(++ptr)) != ']' || inescq); |
| 1629 |
|
|
|
| 1630 |
|
|
/* If class_charcount is 1, we saw precisely one character whose value is |
| 1631 |
|
|
less than 256. In non-UTF-8 mode we can always optimize. In UTF-8 mode, we |
| 1632 |
|
|
can optimize the negative case only if there were no characters >= 128 |
| 1633 |
|
|
because OP_NOT and the related opcodes like OP_NOTSTAR operate on |
| 1634 |
|
|
single-bytes only. This is an historical hangover. Maybe one day we can |
| 1635 |
|
|
tidy these opcodes to handle multi-byte characters. |
| 1636 |
|
|
|
| 1637 |
|
|
The optimization throws away the bit map. We turn the item into a |
| 1638 |
|
|
1-character OP_CHAR[NC] if it's positive, or OP_NOT if it's negative. Note |
| 1639 |
|
|
that OP_NOT does not support multibyte characters. In the positive case, it |
| 1640 |
|
|
can cause firstbyte to be set. Otherwise, there can be no first char if |
| 1641 |
|
|
this item is first, whatever repeat count may follow. In the case of |
| 1642 |
|
|
reqbyte, save the previous value for reinstating. */ |
| 1643 |
|
|
|
| 1644 |
|
|
if (class_charcount == 1) |
| 1645 |
|
|
{ |
| 1646 |
|
|
zeroreqbyte = reqbyte; |
| 1647 |
|
|
|
| 1648 |
|
|
/* The OP_NOT opcode works on one-byte characters only. */ |
| 1649 |
|
|
|
| 1650 |
|
|
if (negate_class) |
| 1651 |
|
|
{ |
| 1652 |
|
|
if (firstbyte == REQ_UNSET) firstbyte = REQ_NONE; |
| 1653 |
|
|
zerofirstbyte = firstbyte; |
| 1654 |
|
|
*code++ = OP_NOT; |
| 1655 |
|
|
*code++ = class_lastchar; |
| 1656 |
|
|
break; |
| 1657 |
|
|
} |
| 1658 |
|
|
|
| 1659 |
|
|
/* For a single, positive character, get the value into mcbuffer, and |
| 1660 |
|
|
then we can handle this with the normal one-character code. */ |
| 1661 |
|
|
|
| 1662 |
|
|
{ |
| 1663 |
|
|
mcbuffer[0] = class_lastchar; |
| 1664 |
|
|
mclength = 1; |
| 1665 |
|
|
} |
| 1666 |
|
|
goto ONE_CHAR; |
| 1667 |
|
|
} /* End of 1-char optimization */ |
| 1668 |
|
|
|
| 1669 |
|
|
/* The general case - not the one-char optimization. If this is the first |
| 1670 |
|
|
thing in the branch, there can be no first char setting, whatever the |
| 1671 |
|
|
repeat count. Any reqbyte setting must remain unchanged after any kind of |
| 1672 |
|
|
repeat. */ |
| 1673 |
|
|
|
| 1674 |
|
|
if (firstbyte == REQ_UNSET) firstbyte = REQ_NONE; |
| 1675 |
|
|
zerofirstbyte = firstbyte; |
| 1676 |
|
|
zeroreqbyte = reqbyte; |
| 1677 |
|
|
|
| 1678 |
|
|
/* If there are no characters > 255, negate the 32-byte map if necessary, |
| 1679 |
|
|
and copy it into the code vector. If this is the first thing in the branch, |
| 1680 |
|
|
there can be no first char setting, whatever the repeat count. Any reqbyte |
| 1681 |
|
|
setting must remain unchanged after any kind of repeat. */ |
| 1682 |
|
|
|
| 1683 |
|
|
if (negate_class) |
| 1684 |
|
|
{ |
| 1685 |
|
|
*code++ = OP_NCLASS; |
| 1686 |
|
|
for (c = 0; c < 32; c++) code[c] = ~classbits[c]; |
| 1687 |
|
|
} |
| 1688 |
|
|
else |
| 1689 |
|
|
{ |
| 1690 |
|
|
*code++ = OP_CLASS; |
| 1691 |
|
|
memcpy(code, classbits, 32); |
| 1692 |
|
|
} |
| 1693 |
|
|
code += 32; |
| 1694 |
|
|
break; |
| 1695 |
|
|
|
| 1696 |
|
|
/* Various kinds of repeat; '{' is not necessarily a quantifier, but this |
| 1697 |
|
|
has been tested above. */ |
| 1698 |
|
|
|
| 1699 |
|
|
case '{': |
| 1700 |
|
|
if (!is_quantifier) goto NORMAL_CHAR; |
| 1701 |
|
|
ptr = read_repeat_counts(ptr+1, &repeat_min, &repeat_max, errorcodeptr); |
| 1702 |
|
|
if (*errorcodeptr != 0) goto FAILED; |
| 1703 |
|
|
goto REPEAT; |
| 1704 |
|
|
|
| 1705 |
|
|
case '*': |
| 1706 |
|
|
repeat_min = 0; |
| 1707 |
|
|
repeat_max = -1; |
| 1708 |
|
|
goto REPEAT; |
| 1709 |
|
|
|
| 1710 |
|
|
case '+': |
| 1711 |
|
|
repeat_min = 1; |
| 1712 |
|
|
repeat_max = -1; |
| 1713 |
|
|
goto REPEAT; |
| 1714 |
|
|
|
| 1715 |
|
|
case '?': |
| 1716 |
|
|
repeat_min = 0; |
| 1717 |
|
|
repeat_max = 1; |
| 1718 |
|
|
|
| 1719 |
|
|
REPEAT: |
| 1720 |
|
|
if (previous == NULL) |
| 1721 |
|
|
{ |
| 1722 |
|
|
*errorcodeptr = ERR9; |
| 1723 |
|
|
goto FAILED; |
| 1724 |
|
|
} |
| 1725 |
|
|
|
| 1726 |
|
|
if (repeat_min == 0) |
| 1727 |
|
|
{ |
| 1728 |
|
|
firstbyte = zerofirstbyte; /* Adjust for zero repeat */ |
| 1729 |
|
|
reqbyte = zeroreqbyte; /* Ditto */ |
| 1730 |
|
|
} |
| 1731 |
|
|
|
| 1732 |
|
|
/* Remember whether this is a variable length repeat */ |
| 1733 |
|
|
|
| 1734 |
|
|
reqvary = (repeat_min == repeat_max)? 0 : REQ_VARY; |
| 1735 |
|
|
|
| 1736 |
|
|
op_type = 0; /* Default single-char op codes */ |
| 1737 |
|
|
possessive_quantifier = FALSE; /* Default not possessive quantifier */ |
| 1738 |
|
|
|
| 1739 |
|
|
/* Save start of previous item, in case we have to move it up to make space |
| 1740 |
|
|
for an inserted OP_ONCE for the additional '+' extension. */ |
| 1741 |
|
|
|
| 1742 |
|
|
tempcode = previous; |
| 1743 |
|
|
|
| 1744 |
|
|
/* If the next character is '+', we have a possessive quantifier. This |
| 1745 |
|
|
implies greediness, whatever the setting of the PCRE_UNGREEDY option. |
| 1746 |
|
|
If the next character is '?' this is a minimizing repeat, by default, |
| 1747 |
|
|
but if PCRE_UNGREEDY is set, it works the other way round. We change the |
| 1748 |
|
|
repeat type to the non-default. */ |
| 1749 |
|
|
|
| 1750 |
|
|
if (ptr[1] == '+') |
| 1751 |
|
|
{ |
| 1752 |
|
|
repeat_type = 0; /* Force greedy */ |
| 1753 |
|
|
possessive_quantifier = TRUE; |
| 1754 |
|
|
ptr++; |
| 1755 |
|
|
} |
| 1756 |
|
|
else if (ptr[1] == '?') |
| 1757 |
|
|
{ |
| 1758 |
|
|
repeat_type = greedy_non_default; |
| 1759 |
|
|
ptr++; |
| 1760 |
|
|
} |
| 1761 |
|
|
else repeat_type = greedy_default; |
| 1762 |
|
|
|
| 1763 |
|
|
/* If previous was a recursion, we need to wrap it inside brackets so that |
| 1764 |
|
|
it can be replicated if necessary. */ |
| 1765 |
|
|
|
| 1766 |
|
|
if (*previous == OP_RECURSE) |
| 1767 |
|
|
{ |
| 1768 |
|
|
memmove(previous + 1 + LINK_SIZE, previous, 1 + LINK_SIZE); |
| 1769 |
|
|
code += 1 + LINK_SIZE; |
| 1770 |
|
|
*previous = OP_BRA; |
| 1771 |
|
|
PUT(previous, 1, code - previous); |
| 1772 |
|
|
*code = OP_KET; |
| 1773 |
|
|
PUT(code, 1, code - previous); |
| 1774 |
|
|
code += 1 + LINK_SIZE; |
| 1775 |
|
|
} |
| 1776 |
|
|
|
| 1777 |
|
|
/* If previous was a character match, abolish the item and generate a |
| 1778 |
|
|
repeat item instead. If a char item has a minumum of more than one, ensure |
| 1779 |
|
|
that it is set in reqbyte - it might not be if a sequence such as x{3} is |
| 1780 |
|
|
the first thing in a branch because the x will have gone into firstbyte |
| 1781 |
|
|
instead. */ |
| 1782 |
|
|
|
| 1783 |
|
|
if (*previous == OP_CHAR || *previous == OP_CHARNC) |
| 1784 |
|
|
{ |
| 1785 |
|
|
/* Handle the case of a single byte - either with no UTF8 support, or |
| 1786 |
|
|
with UTF-8 disabled, or for a UTF-8 character < 128. */ |
| 1787 |
|
|
|
| 1788 |
|
|
{ |
| 1789 |
|
|
c = code[-1]; |
| 1790 |
|
|
if (repeat_min > 1) reqbyte = c | req_caseopt | cd->req_varyopt; |
| 1791 |
|
|
} |
| 1792 |
|
|
|
| 1793 |
|
|
goto OUTPUT_SINGLE_REPEAT; /* Code shared with single character types */ |
| 1794 |
|
|
} |
| 1795 |
|
|
|
| 1796 |
|
|
/* If previous was a single negated character ([^a] or similar), we use |
| 1797 |
|
|
one of the special opcodes, replacing it. The code is shared with single- |
| 1798 |
|
|
character repeats by setting opt_type to add a suitable offset into |
| 1799 |
|
|
repeat_type. OP_NOT is currently used only for single-byte chars. */ |
| 1800 |
|
|
|
| 1801 |
|
|
else if (*previous == OP_NOT) |
| 1802 |
|
|
{ |
| 1803 |
|
|
op_type = OP_NOTSTAR - OP_STAR; /* Use "not" opcodes */ |
| 1804 |
|
|
c = previous[1]; |
| 1805 |
|
|
goto OUTPUT_SINGLE_REPEAT; |
| 1806 |
|
|
} |
| 1807 |
|
|
|
| 1808 |
|
|
/* If previous was a character type match (\d or similar), abolish it and |
| 1809 |
|
|
create a suitable repeat item. The code is shared with single-character |
| 1810 |
|
|
repeats by setting op_type to add a suitable offset into repeat_type. Note |
| 1811 |
|
|
the the Unicode property types will be present only when SUPPORT_UCP is |
| 1812 |
|
|
defined, but we don't wrap the little bits of code here because it just |
| 1813 |
|
|
makes it horribly messy. */ |
| 1814 |
|
|
|
| 1815 |
|
|
else if (*previous < OP_EODN) |
| 1816 |
|
|
{ |
| 1817 |
|
|
uschar *oldcode; |
| 1818 |
|
|
int prop_type; |
| 1819 |
|
|
op_type = OP_TYPESTAR - OP_STAR; /* Use type opcodes */ |
| 1820 |
|
|
c = *previous; |
| 1821 |
|
|
|
| 1822 |
|
|
OUTPUT_SINGLE_REPEAT: |
| 1823 |
|
|
prop_type = (*previous == OP_PROP || *previous == OP_NOTPROP)? |
| 1824 |
|
|
previous[1] : -1; |
| 1825 |
|
|
|
| 1826 |
|
|
oldcode = code; |
| 1827 |
|
|
code = previous; /* Usually overwrite previous item */ |
| 1828 |
|
|
|
| 1829 |
|
|
/* If the maximum is zero then the minimum must also be zero; Perl allows |
| 1830 |
|
|
this case, so we do too - by simply omitting the item altogether. */ |
| 1831 |
|
|
|
| 1832 |
|
|
if (repeat_max == 0) goto END_REPEAT; |
| 1833 |
|
|
|
| 1834 |
|
|
/* All real repeats make it impossible to handle partial matching (maybe |
| 1835 |
|
|
one day we will be able to remove this restriction). */ |
| 1836 |
|
|
|
| 1837 |
|
|
if (repeat_max != 1) cd->nopartial = TRUE; |
| 1838 |
|
|
|
| 1839 |
|
|
/* Combine the op_type with the repeat_type */ |
| 1840 |
|
|
|
| 1841 |
|
|
repeat_type += op_type; |
| 1842 |
|
|
|
| 1843 |
|
|
/* A minimum of zero is handled either as the special case * or ?, or as |
| 1844 |
|
|
an UPTO, with the maximum given. */ |
| 1845 |
|
|
|
| 1846 |
|
|
if (repeat_min == 0) |
| 1847 |
|
|
{ |
| 1848 |
|
|
if (repeat_max == -1) *code++ = OP_STAR + repeat_type; |
| 1849 |
|
|
else if (repeat_max == 1) *code++ = OP_QUERY + repeat_type; |
| 1850 |
|
|
else |
| 1851 |
|
|
{ |
| 1852 |
|
|
*code++ = OP_UPTO + repeat_type; |
| 1853 |
|
|
PUT2INC(code, 0, repeat_max); |
| 1854 |
|
|
} |
| 1855 |
|
|
} |
| 1856 |
|
|
|
| 1857 |
|
|
/* A repeat minimum of 1 is optimized into some special cases. If the |
| 1858 |
|
|
maximum is unlimited, we use OP_PLUS. Otherwise, the original item it |
| 1859 |
|
|
left in place and, if the maximum is greater than 1, we use OP_UPTO with |
| 1860 |
|
|
one less than the maximum. */ |
| 1861 |
|
|
|
| 1862 |
|
|
else if (repeat_min == 1) |
| 1863 |
|
|
{ |
| 1864 |
|
|
if (repeat_max == -1) |
| 1865 |
|
|
*code++ = OP_PLUS + repeat_type; |
| 1866 |
|
|
else |
| 1867 |
|
|
{ |
| 1868 |
|
|
code = oldcode; /* leave previous item in place */ |
| 1869 |
|
|
if (repeat_max == 1) goto END_REPEAT; |
| 1870 |
|
|
*code++ = OP_UPTO + repeat_type; |
| 1871 |
|
|
PUT2INC(code, 0, repeat_max - 1); |
| 1872 |
|
|
} |
| 1873 |
|
|
} |
| 1874 |
|
|
|
| 1875 |
|
|
/* The case {n,n} is just an EXACT, while the general case {n,m} is |
| 1876 |
|
|
handled as an EXACT followed by an UPTO. */ |
| 1877 |
|
|
|
| 1878 |
|
|
else |
| 1879 |
|
|
{ |
| 1880 |
|
|
*code++ = OP_EXACT + op_type; /* NB EXACT doesn't have repeat_type */ |
| 1881 |
|
|
PUT2INC(code, 0, repeat_min); |
| 1882 |
|
|
|
| 1883 |
|
|
/* If the maximum is unlimited, insert an OP_STAR. Before doing so, |
| 1884 |
|
|
we have to insert the character for the previous code. For a repeated |
| 1885 |
|
|
Unicode property match, there is an extra byte that defines the |
| 1886 |
|
|
required property. In UTF-8 mode, long characters have their length in |
| 1887 |
|
|
c, with the 0x80 bit as a flag. */ |
| 1888 |
|
|
|
| 1889 |
|
|
if (repeat_max < 0) |
| 1890 |
|
|
{ |
| 1891 |
|
|
{ |
| 1892 |
|
|
*code++ = c; |
| 1893 |
|
|
if (prop_type >= 0) *code++ = prop_type; |
| 1894 |
|
|
} |
| 1895 |
|
|
*code++ = OP_STAR + repeat_type; |
| 1896 |
|
|
} |
| 1897 |
|
|
|
| 1898 |
|
|
/* Else insert an UPTO if the max is greater than the min, again |
| 1899 |
|
|
preceded by the character, for the previously inserted code. */ |
| 1900 |
|
|
|
| 1901 |
|
|
else if (repeat_max != repeat_min) |
| 1902 |
|
|
{ |
| 1903 |
|
|
*code++ = c; |
| 1904 |
|
|
if (prop_type >= 0) *code++ = prop_type; |
| 1905 |
|
|
repeat_max -= repeat_min; |
| 1906 |
|
|
*code++ = OP_UPTO + repeat_type; |
| 1907 |
|
|
PUT2INC(code, 0, repeat_max); |
| 1908 |
|
|
} |
| 1909 |
|
|
} |
| 1910 |
|
|
|
| 1911 |
|
|
/* The character or character type itself comes last in all cases. */ |
| 1912 |
|
|
*code++ = c; |
| 1913 |
|
|
} |
| 1914 |
|
|
|
| 1915 |
|
|
/* If previous was a character class or a back reference, we put the repeat |
| 1916 |
|
|
stuff after it, but just skip the item if the repeat was {0,0}. */ |
| 1917 |
|
|
|
| 1918 |
|
|
else if (*previous == OP_CLASS || |
| 1919 |
|
|
*previous == OP_NCLASS || |
| 1920 |
|
|
*previous == OP_REF) |
| 1921 |
|
|
{ |
| 1922 |
|
|
if (repeat_max == 0) |
| 1923 |
|
|
{ |
| 1924 |
|
|
code = previous; |
| 1925 |
|
|
goto END_REPEAT; |
| 1926 |
|
|
} |
| 1927 |
|
|
|
| 1928 |
|
|
/* All real repeats make it impossible to handle partial matching (maybe |
| 1929 |
|
|
one day we will be able to remove this restriction). */ |
| 1930 |
|
|
|
| 1931 |
|
|
if (repeat_max != 1) cd->nopartial = TRUE; |
| 1932 |
|
|
|
| 1933 |
|
|
if (repeat_min == 0 && repeat_max == -1) |
| 1934 |
|
|
*code++ = OP_CRSTAR + repeat_type; |
| 1935 |
|
|
else if (repeat_min == 1 && repeat_max == -1) |
| 1936 |
|
|
*code++ = OP_CRPLUS + repeat_type; |
| 1937 |
|
|
else if (repeat_min == 0 && repeat_max == 1) |
| 1938 |
|
|
*code++ = OP_CRQUERY + repeat_type; |
| 1939 |
|
|
else |
| 1940 |
|
|
{ |
| 1941 |
|
|
*code++ = OP_CRRANGE + repeat_type; |
| 1942 |
|
|
PUT2INC(code, 0, repeat_min); |
| 1943 |
|
|
if (repeat_max == -1) repeat_max = 0; /* 2-byte encoding for max */ |
| 1944 |
|
|
PUT2INC(code, 0, repeat_max); |
| 1945 |
|
|
} |
| 1946 |
|
|
} |
| 1947 |
|
|
|
| 1948 |
|
|
/* If previous was a bracket group, we may have to replicate it in certain |
| 1949 |
|
|
cases. */ |
| 1950 |
|
|
|
| 1951 |
|
|
else if (*previous >= OP_BRA || *previous == OP_ONCE || |
| 1952 |
|
|
*previous == OP_COND) |
| 1953 |
|
|
{ |
| 1954 |
|
|
register int i; |
| 1955 |
|
|
int ketoffset = 0; |
| 1956 |
|
|
int len = code - previous; |
| 1957 |
|
|
uschar *bralink = NULL; |
| 1958 |
|
|
|
| 1959 |
|
|
/* If the maximum repeat count is unlimited, find the end of the bracket |
| 1960 |
|
|
by scanning through from the start, and compute the offset back to it |
| 1961 |
|
|
from the current code pointer. There may be an OP_OPT setting following |
| 1962 |
|
|
the final KET, so we can't find the end just by going back from the code |
| 1963 |
|
|
pointer. */ |
| 1964 |
|
|
|
| 1965 |
|
|
if (repeat_max == -1) |
| 1966 |
|
|
{ |
| 1967 |
|
|
register uschar *ket = previous; |
| 1968 |
|
|
do ket += GET(ket, 1); while (*ket != OP_KET); |
| 1969 |
|
|
ketoffset = code - ket; |
| 1970 |
|
|
} |
| 1971 |
|
|
|
| 1972 |
|
|
/* The case of a zero minimum is special because of the need to stick |
| 1973 |
|
|
OP_BRAZERO in front of it, and because the group appears once in the |
| 1974 |
|
|
data, whereas in other cases it appears the minimum number of times. For |
| 1975 |
|
|
this reason, it is simplest to treat this case separately, as otherwise |
| 1976 |
|
|
the code gets far too messy. There are several special subcases when the |
| 1977 |
|
|
minimum is zero. */ |
| 1978 |
|
|
|
| 1979 |
|
|
if (repeat_min == 0) |
| 1980 |
|
|
{ |
| 1981 |
|
|
/* If the maximum is also zero, we just omit the group from the output |
| 1982 |
|
|
altogether. */ |
| 1983 |
|
|
|
| 1984 |
|
|
if (repeat_max == 0) |
| 1985 |
|
|
{ |
| 1986 |
|
|
code = previous; |
| 1987 |
|
|
goto END_REPEAT; |
| 1988 |
|
|
} |
| 1989 |
|
|
|
| 1990 |
|
|
/* If the maximum is 1 or unlimited, we just have to stick in the |
| 1991 |
|
|
BRAZERO and do no more at this point. However, we do need to adjust |
| 1992 |
|
|
any OP_RECURSE calls inside the group that refer to the group itself or |
| 1993 |
|
|
any internal group, because the offset is from the start of the whole |
| 1994 |
|
|
regex. Temporarily terminate the pattern while doing this. */ |
| 1995 |
|
|
|
| 1996 |
|
|
if (repeat_max <= 1) |
| 1997 |
|
|
{ |
| 1998 |
|
|
*code = OP_END; |
| 1999 |
|
|
adjust_recurse(previous, 1, utf8, cd); |
| 2000 |
|
|
memmove(previous+1, previous, len); |
| 2001 |
|
|
code++; |
| 2002 |
|
|
*previous++ = OP_BRAZERO + repeat_type; |
| 2003 |
|
|
} |
| 2004 |
|
|
|
| 2005 |
|
|
/* If the maximum is greater than 1 and limited, we have to replicate |
| 2006 |
|
|
in a nested fashion, sticking OP_BRAZERO before each set of brackets. |
| 2007 |
|
|
The first one has to be handled carefully because it's the original |
| 2008 |
|
|
copy, which has to be moved up. The remainder can be handled by code |
| 2009 |
|
|
that is common with the non-zero minimum case below. We have to |
| 2010 |
|
|
adjust the value or repeat_max, since one less copy is required. Once |
| 2011 |
|
|
again, we may have to adjust any OP_RECURSE calls inside the group. */ |
| 2012 |
|
|
|
| 2013 |
|
|
else |
| 2014 |
|
|
{ |
| 2015 |
|
|
int offset; |
| 2016 |
|
|
*code = OP_END; |
| 2017 |
|
|
adjust_recurse(previous, 2 + LINK_SIZE, utf8, cd); |
| 2018 |
|
|
memmove(previous + 2 + LINK_SIZE, previous, len); |
| 2019 |
|
|
code += 2 + LINK_SIZE; |
| 2020 |
|
|
*previous++ = OP_BRAZERO + repeat_type; |
| 2021 |
|
|
*previous++ = OP_BRA; |
| 2022 |
|
|
|
| 2023 |
|
|
/* We chain together the bracket offset fields that have to be |
| 2024 |
|
|
filled in later when the ends of the brackets are reached. */ |
| 2025 |
|
|
|
| 2026 |
|
|
offset = (bralink == NULL)? 0 : previous - bralink; |
| 2027 |
|
|
bralink = previous; |
| 2028 |
|
|
PUTINC(previous, 0, offset); |
| 2029 |
|
|
} |
| 2030 |
|
|
|
| 2031 |
|
|
repeat_max--; |
| 2032 |
|
|
} |
| 2033 |
|
|
|
| 2034 |
|
|
/* If the minimum is greater than zero, replicate the group as many |
| 2035 |
|
|
times as necessary, and adjust the maximum to the number of subsequent |
| 2036 |
|
|
copies that we need. If we set a first char from the group, and didn't |
| 2037 |
|
|
set a required char, copy the latter from the former. */ |
| 2038 |
|
|
|
| 2039 |
|
|
else |
| 2040 |
|
|
{ |
| 2041 |
|
|
if (repeat_min > 1) |
| 2042 |
|
|
{ |
| 2043 |
|
|
if (groupsetfirstbyte && reqbyte < 0) reqbyte = firstbyte; |
| 2044 |
|
|
for (i = 1; i < repeat_min; i++) |
| 2045 |
|
|
{ |
| 2046 |
|
|
memcpy(code, previous, len); |
| 2047 |
|
|
code += len; |
| 2048 |
|
|
} |
| 2049 |
|
|
} |
| 2050 |
|
|
if (repeat_max > 0) repeat_max -= repeat_min; |
| 2051 |
|
|
} |
| 2052 |
|
|
|
| 2053 |
|
|
/* This code is common to both the zero and non-zero minimum cases. If |
| 2054 |
|
|
the maximum is limited, it replicates the group in a nested fashion, |
| 2055 |
|
|
remembering the bracket starts on a stack. In the case of a zero minimum, |
| 2056 |
|
|
the first one was set up above. In all cases the repeat_max now specifies |
| 2057 |
|
|
the number of additional copies needed. */ |
| 2058 |
|
|
|
| 2059 |
|
|
if (repeat_max >= 0) |
| 2060 |
|
|
{ |
| 2061 |
|
|
for (i = repeat_max - 1; i >= 0; i--) |
| 2062 |
|
|
{ |
| 2063 |
|
|
*code++ = OP_BRAZERO + repeat_type; |
| 2064 |
|
|
|
| 2065 |
|
|
/* All but the final copy start a new nesting, maintaining the |
| 2066 |
|
|
chain of brackets outstanding. */ |
| 2067 |
|
|
|
| 2068 |
|
|
if (i != 0) |
| 2069 |
|
|
{ |
| 2070 |
|
|
int offset; |
| 2071 |
|
|
*code++ = OP_BRA; |
| 2072 |
|
|
offset = (bralink == NULL)? 0 : code - bralink; |
| 2073 |
|
|
bralink = code; |
| 2074 |
|
|
PUTINC(code, 0, offset); |
| 2075 |
|
|
} |
| 2076 |
|
|
|
| 2077 |
|
|
memcpy(code, previous, len); |
| 2078 |
|
|
code += len; |
| 2079 |
|
|
} |
| 2080 |
|
|
|
| 2081 |
|
|
/* Now chain through the pending brackets, and fill in their length |
| 2082 |
|
|
fields (which are holding the chain links pro tem). */ |
| 2083 |
|
|
|
| 2084 |
|
|
while (bralink != NULL) |
| 2085 |
|
|
{ |
| 2086 |
|
|
int oldlinkoffset; |
| 2087 |
|
|
int offset = code - bralink + 1; |
| 2088 |
|
|
uschar *bra = code - offset; |
| 2089 |
|
|
oldlinkoffset = GET(bra, 1); |
| 2090 |
|
|
bralink = (oldlinkoffset == 0)? NULL : bralink - oldlinkoffset; |
| 2091 |
|
|
*code++ = OP_KET; |
| 2092 |
|
|
PUTINC(code, 0, offset); |
| 2093 |
|
|
PUT(bra, 1, offset); |
| 2094 |
|
|
} |
| 2095 |
|
|
} |
| 2096 |
|
|
|
| 2097 |
|
|
/* If the maximum is unlimited, set a repeater in the final copy. We |
| 2098 |
|
|
can't just offset backwards from the current code point, because we |
| 2099 |
|
|
don't know if there's been an options resetting after the ket. The |
| 2100 |
|
|
correct offset was computed above. */ |
| 2101 |
|
|
|
| 2102 |
|
|
else code[-ketoffset] = OP_KETRMAX + repeat_type; |
| 2103 |
|
|
} |
| 2104 |
|
|
|
| 2105 |
|
|
/* Else there's some kind of shambles */ |
| 2106 |
|
|
|
| 2107 |
|
|
else |
| 2108 |
|
|
{ |
| 2109 |
|
|
*errorcodeptr = ERR11; |
| 2110 |
|
|
goto FAILED; |
| 2111 |
|
|
} |
| 2112 |
|
|
|
| 2113 |
|
|
/* If the character following a repeat is '+', we wrap the entire repeated |
| 2114 |
|
|
item inside OP_ONCE brackets. This is just syntactic sugar, taken from |
| 2115 |
|
|
Sun's Java package. The repeated item starts at tempcode, not at previous, |
| 2116 |
|
|
which might be the first part of a string whose (former) last char we |
| 2117 |
|
|
repeated. However, we don't support '+' after a greediness '?'. */ |
| 2118 |
|
|
|
| 2119 |
|
|
if (possessive_quantifier) |
| 2120 |
|
|
{ |
| 2121 |
|
|
int len = code - tempcode; |
| 2122 |
|
|
memmove(tempcode + 1+LINK_SIZE, tempcode, len); |
| 2123 |
|
|
code += 1 + LINK_SIZE; |
| 2124 |
|
|
len += 1 + LINK_SIZE; |
| 2125 |
|
|
tempcode[0] = OP_ONCE; |
| 2126 |
|
|
*code++ = OP_KET; |
| 2127 |
|
|
PUTINC(code, 0, len); |
| 2128 |
|
|
PUT(tempcode, 1, len); |
| 2129 |
|
|
} |
| 2130 |
|
|
|
| 2131 |
|
|
/* In all case we no longer have a previous item. We also set the |
| 2132 |
|
|
"follows varying string" flag for subsequently encountered reqbytes if |
| 2133 |
|
|
it isn't already set and we have just passed a varying length item. */ |
| 2134 |
|
|
|
| 2135 |
|
|
END_REPEAT: |
| 2136 |
|
|
previous = NULL; |
| 2137 |
|
|
cd->req_varyopt |= reqvary; |
| 2138 |
|
|
break; |
| 2139 |
|
|
|
| 2140 |
|
|
|
| 2141 |
|
|
/* Start of nested bracket sub-expression, or comment or lookahead or |
| 2142 |
|
|
lookbehind or option setting or condition. First deal with special things |
| 2143 |
|
|
that can come after a bracket; all are introduced by ?, and the appearance |
| 2144 |
|
|
of any of them means that this is not a referencing group. They were |
| 2145 |
|
|
checked for validity in the first pass over the string, so we don't have to |
| 2146 |
|
|
check for syntax errors here. */ |
| 2147 |
|
|
|
| 2148 |
|
|
case '(': |
| 2149 |
|
|
newoptions = options; |
| 2150 |
|
|
skipbytes = 0; |
| 2151 |
|
|
|
| 2152 |
|
|
if (*(++ptr) == '?') |
| 2153 |
|
|
{ |
| 2154 |
|
|
int set, unset; |
| 2155 |
|
|
int *optset; |
| 2156 |
|
|
|
| 2157 |
|
|
switch (*(++ptr)) |
| 2158 |
|
|
{ |
| 2159 |
|
|
case '#': /* Comment; skip to ket */ |
| 2160 |
|
|
ptr++; |
| 2161 |
|
|
while (*ptr != ')') ptr++; |
| 2162 |
|
|
continue; |
| 2163 |
|
|
|
| 2164 |
|
|
case ':': /* Non-extracting bracket */ |
| 2165 |
|
|
bravalue = OP_BRA; |
| 2166 |
|
|
ptr++; |
| 2167 |
|
|
break; |
| 2168 |
|
|
|
| 2169 |
|
|
case '(': |
| 2170 |
|
|
bravalue = OP_COND; /* Conditional group */ |
| 2171 |
|
|
|
| 2172 |
|
|
/* Condition to test for recursion */ |
| 2173 |
|
|
|
| 2174 |
|
|
if (ptr[1] == 'R') |
| 2175 |
|
|
{ |
| 2176 |
|
|
code[1+LINK_SIZE] = OP_CREF; |
| 2177 |
|
|
PUT2(code, 2+LINK_SIZE, CREF_RECURSE); |
| 2178 |
|
|
skipbytes = 3; |
| 2179 |
|
|
ptr += 3; |
| 2180 |
|
|
} |
| 2181 |
|
|
|
| 2182 |
|
|
/* Condition to test for a numbered subpattern match. We know that |
| 2183 |
|
|
if a digit follows ( then there will just be digits until ) because |
| 2184 |
|
|
the syntax was checked in the first pass. */ |
| 2185 |
|
|
|
| 2186 |
|
|
else if ((digitab[ptr[1]] && ctype_digit) != 0) |
| 2187 |
|
|
{ |
| 2188 |
|
|
int condref; /* Don't amalgamate; some compilers */ |
| 2189 |
|
|
condref = *(++ptr) - '0'; /* grumble at autoincrement in declaration */ |
| 2190 |
|
|
while (*(++ptr) != ')') condref = condref*10 + *ptr - '0'; |
| 2191 |
|
|
if (condref == 0) |
| 2192 |
|
|
{ |
| 2193 |
|
|
*errorcodeptr = ERR35; |
| 2194 |
|
|
goto FAILED; |
| 2195 |
|
|
} |
| 2196 |
|
|
ptr++; |
| 2197 |
|
|
code[1+LINK_SIZE] = OP_CREF; |
| 2198 |
|
|
PUT2(code, 2+LINK_SIZE, condref); |
| 2199 |
|
|
skipbytes = 3; |
| 2200 |
|
|
} |
| 2201 |
|
|
/* For conditions that are assertions, we just fall through, having |
| 2202 |
|
|
set bravalue above. */ |
| 2203 |
|
|
break; |
| 2204 |
|
|
|
| 2205 |
|
|
case '=': /* Positive lookahead */ |
| 2206 |
|
|
bravalue = OP_ASSERT; |
| 2207 |
|
|
ptr++; |
| 2208 |
|
|
break; |
| 2209 |
|
|
|
| 2210 |
|
|
case '!': /* Negative lookahead */ |
| 2211 |
|
|
bravalue = OP_ASSERT_NOT; |
| 2212 |
|
|
ptr++; |
| 2213 |
|
|
break; |
| 2214 |
|
|
|
| 2215 |
|
|
case '<': /* Lookbehinds */ |
| 2216 |
|
|
switch (*(++ptr)) |
| 2217 |
|
|
{ |
| 2218 |
|
|
case '=': /* Positive lookbehind */ |
| 2219 |
|
|
bravalue = OP_ASSERTBACK; |
| 2220 |
|
|
ptr++; |
| 2221 |
|
|
break; |
| 2222 |
|
|
|
| 2223 |
|
|
case '!': /* Negative lookbehind */ |
| 2224 |
|
|
bravalue = OP_ASSERTBACK_NOT; |
| 2225 |
|
|
ptr++; |
| 2226 |
|
|
break; |
| 2227 |
|
|
} |
| 2228 |
|
|
break; |
| 2229 |
|
|
|
| 2230 |
|
|
case '>': /* One-time brackets */ |
| 2231 |
|
|
bravalue = OP_ONCE; |
| 2232 |
|
|
ptr++; |
| 2233 |
|
|
break; |
| 2234 |
|
|
|
| 2235 |
|
|
case 'C': /* Callout - may be followed by digits; */ |
| 2236 |
|
|
previous_callout = code; /* Save for later completion */ |
| 2237 |
|
|
after_manual_callout = 1; /* Skip one item before completing */ |
| 2238 |
|
|
*code++ = OP_CALLOUT; /* Already checked that the terminating */ |
| 2239 |
|
|
{ /* closing parenthesis is present. */ |
| 2240 |
|
|
int n = 0; |
| 2241 |
|
|
while ((digitab[*(++ptr)] & ctype_digit) != 0) |
| 2242 |
|
|
n = n * 10 + *ptr - '0'; |
| 2243 |
|
|
if (n > 255) |
| 2244 |
|
|
{ |
| 2245 |
|
|
*errorcodeptr = ERR38; |
| 2246 |
|
|
goto FAILED; |
| 2247 |
|
|
} |
| 2248 |
|
|
*code++ = n; |
| 2249 |
|
|
PUT(code, 0, ptr - cd->start_pattern + 1); /* Pattern offset */ |
| 2250 |
|
|
PUT(code, LINK_SIZE, 0); /* Default length */ |
| 2251 |
|
|
code += 2 * LINK_SIZE; |
| 2252 |
|
|
} |
| 2253 |
|
|
previous = NULL; |
| 2254 |
|
|
continue; |
| 2255 |
|
|
|
| 2256 |
|
|
case 'P': /* Named subpattern handling */ |
| 2257 |
|
|
if (*(++ptr) == '<') /* Definition */ |
| 2258 |
|
|
{ |
| 2259 |
|
|
int i, namelen; |
| 2260 |
|
|
uschar *slot = cd->name_table; |
| 2261 |
|
|
const uschar *name; /* Don't amalgamate; some compilers */ |
| 2262 |
|
|
name = ++ptr; /* grumble at autoincrement in declaration */ |
| 2263 |
|
|
|
| 2264 |
|
|
while (*ptr++ != '>'); |
| 2265 |
|
|
namelen = ptr - name - 1; |
| 2266 |
|
|
|
| 2267 |
|
|
for (i = 0; i < cd->names_found; i++) |
| 2268 |
|
|
{ |
| 2269 |
|
|
int crc = memcmp(name, slot+2, namelen); |
| 2270 |
|
|
if (crc == 0) |
| 2271 |
|
|
{ |
| 2272 |
|
|
if (slot[2+namelen] == 0) |
| 2273 |
|
|
{ |
| 2274 |
|
|
*errorcodeptr = ERR43; |
| 2275 |
|
|
goto FAILED; |
| 2276 |
|
|
} |
| 2277 |
|
|
crc = -1; /* Current name is substring */ |
| 2278 |
|
|
} |
| 2279 |
|
|
if (crc < 0) |
| 2280 |
|
|
{ |
| 2281 |
|
|
memmove(slot + cd->name_entry_size, slot, |
| 2282 |
|
|
(cd->names_found - i) * cd->name_entry_size); |
| 2283 |
|
|
break; |
| 2284 |
|
|
} |
| 2285 |
|
|
slot += cd->name_entry_size; |
| 2286 |
|
|
} |
| 2287 |
|
|
|
| 2288 |
|
|
PUT2(slot, 0, *brackets + 1); |
| 2289 |
|
|
memcpy(slot + 2, name, namelen); |
| 2290 |
|
|
slot[2+namelen] = 0; |
| 2291 |
|
|
cd->names_found++; |
| 2292 |
|
|
goto NUMBERED_GROUP; |
| 2293 |
|
|
} |
| 2294 |
|
|
|
| 2295 |
|
|
if (*ptr == '=' || *ptr == '>') /* Reference or recursion */ |
| 2296 |
|
|
{ |
| 2297 |
|
|
int i, namelen; |
| 2298 |
|
|
int type = *ptr++; |
| 2299 |
|
|
const uschar *name = ptr; |
| 2300 |
|
|
uschar *slot = cd->name_table; |
| 2301 |
|
|
|
| 2302 |
|
|
while (*ptr != ')') ptr++; |
| 2303 |
|
|
namelen = ptr - name; |
| 2304 |
|
|
|
| 2305 |
|
|
for (i = 0; i < cd->names_found; i++) |
| 2306 |
|
|
{ |
| 2307 |
|
|
if (strncmp((char *)name, (char *)slot+2, namelen) == 0) break; |
| 2308 |
|
|
slot += cd->name_entry_size; |
| 2309 |
|
|
} |
| 2310 |
|
|
if (i >= cd->names_found) |
| 2311 |
|
|
{ |
| 2312 |
|
|
*errorcodeptr = ERR15; |
| 2313 |
|
|
goto FAILED; |
| 2314 |
|
|
} |
| 2315 |
|
|
|
| 2316 |
|
|
recno = GET2(slot, 0); |
| 2317 |
|
|
|
| 2318 |
|
|
if (type == '>') goto HANDLE_RECURSION; /* A few lines below */ |
| 2319 |
|
|
|
| 2320 |
|
|
/* Back reference */ |
| 2321 |
|
|
|
| 2322 |
|
|
previous = code; |
| 2323 |
|
|
*code++ = OP_REF; |
| 2324 |
|
|
PUT2INC(code, 0, recno); |
| 2325 |
|
|
cd->backref_map |= (recno < 32)? (1 << recno) : 1; |
| 2326 |
|
|
if (recno > cd->top_backref) cd->top_backref = recno; |
| 2327 |
|
|
continue; |
| 2328 |
|
|
} |
| 2329 |
|
|
|
| 2330 |
|
|
/* Should never happen */ |
| 2331 |
|
|
break; |
| 2332 |
|
|
|
| 2333 |
|
|
case 'R': /* Pattern recursion */ |
| 2334 |
|
|
ptr++; /* Same as (?0) */ |
| 2335 |
|
|
/* Fall through */ |
| 2336 |
|
|
|
| 2337 |
|
|
/* Recursion or "subroutine" call */ |
| 2338 |
|
|
|
| 2339 |
|
|
case '0': case '1': case '2': case '3': case '4': |
| 2340 |
|
|
case '5': case '6': case '7': case '8': case '9': |
| 2341 |
|
|
{ |
| 2342 |
|
|
const uschar *called; |
| 2343 |
|
|
recno = 0; |
| 2344 |
|
|
while((digitab[*ptr] & ctype_digit) != 0) |
| 2345 |
|
|
recno = recno * 10 + *ptr++ - '0'; |
| 2346 |
|
|
|
| 2347 |
|
|
/* Come here from code above that handles a named recursion */ |
| 2348 |
|
|
|
| 2349 |
|
|
HANDLE_RECURSION: |
| 2350 |
|
|
|
| 2351 |
|
|
previous = code; |
| 2352 |
|
|
|
| 2353 |
|
|
/* Find the bracket that is being referenced. Temporarily end the |
| 2354 |
|
|
regex in case it doesn't exist. */ |
| 2355 |
|
|
|
| 2356 |
|
|
*code = OP_END; |
| 2357 |
|
|
called = (recno == 0)? |
| 2358 |
|
|
cd->start_code : find_bracket(cd->start_code, utf8, recno); |
| 2359 |
|
|
|
| 2360 |
|
|
if (called == NULL) |
| 2361 |
|
|
{ |
| 2362 |
|
|
*errorcodeptr = ERR15; |
| 2363 |
|
|
goto FAILED; |
| 2364 |
|
|
} |
| 2365 |
|
|
|
| 2366 |
|
|
/* If the subpattern is still open, this is a recursive call. We |
| 2367 |
|
|
check to see if this is a left recursion that could loop for ever, |
| 2368 |
|
|
and diagnose that case. */ |
| 2369 |
|
|
|
| 2370 |
|
|
if (GET(called, 1) == 0 && could_be_empty(called, code, bcptr, utf8)) |
| 2371 |
|
|
{ |
| 2372 |
|
|
*errorcodeptr = ERR40; |
| 2373 |
|
|
goto FAILED; |
| 2374 |
|
|
} |
| 2375 |
|
|
|
| 2376 |
|
|
/* Insert the recursion/subroutine item */ |
| 2377 |
|
|
|
| 2378 |
|
|
*code = OP_RECURSE; |
| 2379 |
|
|
PUT(code, 1, called - cd->start_code); |
| 2380 |
|
|
code += 1 + LINK_SIZE; |
| 2381 |
|
|
} |
| 2382 |
|
|
continue; |
| 2383 |
|
|
|
| 2384 |
|
|
/* Character after (? not specially recognized */ |
| 2385 |
|
|
|
| 2386 |
|
|
default: /* Option setting */ |
| 2387 |
|
|
set = unset = 0; |
| 2388 |
|
|
optset = &set; |
| 2389 |
|
|
|
| 2390 |
|
|
while (*ptr != ')' && *ptr != ':') |
| 2391 |
|
|
{ |
| 2392 |
|
|
switch (*ptr++) |
| 2393 |
|
|
{ |
| 2394 |
|
|
case '-': optset = &unset; break; |
| 2395 |
|
|
|
| 2396 |
|
|
case 'i': *optset |= PCRE_CASELESS; break; |
| 2397 |
|
|
case 'm': *optset |= PCRE_MULTILINE; break; |
| 2398 |
|
|
case 's': *optset |= PCRE_DOTALL; break; |
| 2399 |
|
|
case 'x': *optset |= PCRE_EXTENDED; break; |
| 2400 |
|
|
case 'U': *optset |= PCRE_UNGREEDY; break; |
| 2401 |
|
|
case 'X': *optset |= PCRE_EXTRA; break; |
| 2402 |
|
|
} |
| 2403 |
|
|
} |
| 2404 |
|
|
|
| 2405 |
|
|
/* Set up the changed option bits, but don't change anything yet. */ |
| 2406 |
|
|
|
| 2407 |
|
|
newoptions = (options | set) & (~unset); |
| 2408 |
|
|
|
| 2409 |
|
|
/* If the options ended with ')' this is not the start of a nested |
| 2410 |
|
|
group with option changes, so the options change at this level. Compile |
| 2411 |
|
|
code to change the ims options if this setting actually changes any of |
| 2412 |
|
|
them. We also pass the new setting back so that it can be put at the |
| 2413 |
|
|
start of any following branches, and when this group ends (if we are in |
| 2414 |
|
|
a group), a resetting item can be compiled. |
| 2415 |
|
|
|
| 2416 |
|
|
Note that if this item is right at the start of the pattern, the |
| 2417 |
|
|
options will have been abstracted and made global, so there will be no |
| 2418 |
|
|
change to compile. */ |
| 2419 |
|
|
|
| 2420 |
|
|
if (*ptr == ')') |
| 2421 |
|
|
{ |
| 2422 |
|
|
if ((options & PCRE_IMS) != (newoptions & PCRE_IMS)) |
| 2423 |
|
|
{ |
| 2424 |
|
|
*code++ = OP_OPT; |
| 2425 |
|
|
*code++ = newoptions & PCRE_IMS; |
| 2426 |
|
|
} |
| 2427 |
|
|
|
| 2428 |
|
|
/* Change options at this level, and pass them back for use |
| 2429 |
|
|
in subsequent branches. Reset the greedy defaults and the case |
| 2430 |
|
|
value for firstbyte and reqbyte. */ |
| 2431 |
|
|
|
| 2432 |
|
|
*optionsptr = options = newoptions; |
| 2433 |
|
|
greedy_default = ((newoptions & PCRE_UNGREEDY) != 0); |
| 2434 |
|
|
greedy_non_default = greedy_default ^ 1; |
| 2435 |
|
|
req_caseopt = ((options & PCRE_CASELESS) != 0)? REQ_CASELESS : 0; |
| 2436 |
|
|
|
| 2437 |
|
|
previous = NULL; /* This item can't be repeated */ |
| 2438 |
|
|
continue; /* It is complete */ |
| 2439 |
|
|
} |
| 2440 |
|
|
|
| 2441 |
|
|
/* If the options ended with ':' we are heading into a nested group |
| 2442 |
|
|
with possible change of options. Such groups are non-capturing and are |
| 2443 |
|
|
not assertions of any kind. All we need to do is skip over the ':'; |
| 2444 |
|
|
the newoptions value is handled below. */ |
| 2445 |
|
|
|
| 2446 |
|
|
bravalue = OP_BRA; |
| 2447 |
|
|
ptr++; |
| 2448 |
|
|
} |
| 2449 |
|
|
} |
| 2450 |
|
|
|
| 2451 |
|
|
/* If PCRE_NO_AUTO_CAPTURE is set, all unadorned brackets become |
| 2452 |
|
|
non-capturing and behave like (?:...) brackets */ |
| 2453 |
|
|
|
| 2454 |
|
|
else if ((options & PCRE_NO_AUTO_CAPTURE) != 0) |
| 2455 |
|
|
{ |
| 2456 |
|
|
bravalue = OP_BRA; |
| 2457 |
|
|
} |
| 2458 |
|
|
|
| 2459 |
|
|
/* Else we have a referencing group; adjust the opcode. If the bracket |
| 2460 |
|
|
number is greater than EXTRACT_BASIC_MAX, we set the opcode one higher, and |
| 2461 |
|
|
arrange for the true number to follow later, in an OP_BRANUMBER item. */ |
| 2462 |
|
|
|
| 2463 |
|
|
else |
| 2464 |
|
|
{ |
| 2465 |
|
|
NUMBERED_GROUP: |
| 2466 |
|
|
if (++(*brackets) > EXTRACT_BASIC_MAX) |
| 2467 |
|
|
{ |
| 2468 |
|
|
bravalue = OP_BRA + EXTRACT_BASIC_MAX + 1; |
| 2469 |
|
|
code[1+LINK_SIZE] = OP_BRANUMBER; |
| 2470 |
|
|
PUT2(code, 2+LINK_SIZE, *brackets); |
| 2471 |
|
|
skipbytes = 3; |
| 2472 |
|
|
} |
| 2473 |
|
|
else bravalue = OP_BRA + *brackets; |
| 2474 |
|
|
} |
| 2475 |
|
|
|
| 2476 |
|
|
/* Process nested bracketed re. Assertions may not be repeated, but other |
| 2477 |
|
|
kinds can be. We copy code into a non-register variable in order to be able |
| 2478 |
|
|
to pass its address because some compilers complain otherwise. Pass in a |
| 2479 |
|
|
new setting for the ims options if they have changed. */ |
| 2480 |
|
|
|
| 2481 |
|
|
previous = (bravalue >= OP_ONCE)? code : NULL; |
| 2482 |
|
|
*code = bravalue; |
| 2483 |
|
|
tempcode = code; |
| 2484 |
|
|
tempreqvary = cd->req_varyopt; /* Save value before bracket */ |
| 2485 |
|
|
|
| 2486 |
|
|
if (!compile_regex( |
| 2487 |
|
|
newoptions, /* The complete new option state */ |
| 2488 |
|
|
options & PCRE_IMS, /* The previous ims option state */ |
| 2489 |
|
|
brackets, /* Extracting bracket count */ |
| 2490 |
|
|
&tempcode, /* Where to put code (updated) */ |
| 2491 |
|
|
&ptr, /* Input pointer (updated) */ |
| 2492 |
|
|
errorcodeptr, /* Where to put an error message */ |
| 2493 |
|
|
(bravalue == OP_ASSERTBACK || |
| 2494 |
|
|
bravalue == OP_ASSERTBACK_NOT), /* TRUE if back assert */ |
| 2495 |
|
|
skipbytes, /* Skip over OP_COND/OP_BRANUMBER */ |
| 2496 |
|
|
&subfirstbyte, /* For possible first char */ |
| 2497 |
|
|
&subreqbyte, /* For possible last char */ |
| 2498 |
|
|
bcptr, /* Current branch chain */ |
| 2499 |
|
|
cd)) /* Tables block */ |
| 2500 |
|
|
goto FAILED; |
| 2501 |
|
|
|
| 2502 |
|
|
/* At the end of compiling, code is still pointing to the start of the |
| 2503 |
|
|
group, while tempcode has been updated to point past the end of the group |
| 2504 |
|
|
and any option resetting that may follow it. The pattern pointer (ptr) |
| 2505 |
|
|
is on the bracket. */ |
| 2506 |
|
|
|
| 2507 |
|
|
/* If this is a conditional bracket, check that there are no more than |
| 2508 |
|
|
two branches in the group. */ |
| 2509 |
|
|
|
| 2510 |
|
|
else if (bravalue == OP_COND) |
| 2511 |
|
|
{ |
| 2512 |
|
|
uschar *tc = code; |
| 2513 |
|
|
condcount = 0; |
| 2514 |
|
|
|
| 2515 |
|
|
do { |
| 2516 |
|
|
condcount++; |
| 2517 |
|
|
tc += GET(tc,1); |
| 2518 |
|
|
} |
| 2519 |
|
|
while (*tc != OP_KET); |
| 2520 |
|
|
|
| 2521 |
|
|
if (condcount > 2) |
| 2522 |
|
|
{ |
| 2523 |
|
|
*errorcodeptr = ERR27; |
| 2524 |
|
|
goto FAILED; |
| 2525 |
|
|
} |
| 2526 |
|
|
|
| 2527 |
|
|
/* If there is just one branch, we must not make use of its firstbyte or |
| 2528 |
|
|
reqbyte, because this is equivalent to an empty second branch. */ |
| 2529 |
|
|
|
| 2530 |
|
|
if (condcount == 1) subfirstbyte = subreqbyte = REQ_NONE; |
| 2531 |
|
|
} |
| 2532 |
|
|
|
| 2533 |
|
|
/* Handle updating of the required and first characters. Update for normal |
| 2534 |
|
|
brackets of all kinds, and conditions with two branches (see code above). |
| 2535 |
|
|
If the bracket is followed by a quantifier with zero repeat, we have to |
| 2536 |
|
|
back off. Hence the definition of zeroreqbyte and zerofirstbyte outside the |
| 2537 |
|
|
main loop so that they can be accessed for the back off. */ |
| 2538 |
|
|
|
| 2539 |
|
|
zeroreqbyte = reqbyte; |
| 2540 |
|
|
zerofirstbyte = firstbyte; |
| 2541 |
|
|
groupsetfirstbyte = FALSE; |
| 2542 |
|
|
|
| 2543 |
|
|
if (bravalue >= OP_BRA || bravalue == OP_ONCE || bravalue == OP_COND) |
| 2544 |
|
|
{ |
| 2545 |
|
|
/* If we have not yet set a firstbyte in this branch, take it from the |
| 2546 |
|
|
subpattern, remembering that it was set here so that a repeat of more |
| 2547 |
|
|
than one can replicate it as reqbyte if necessary. If the subpattern has |
| 2548 |
|
|
no firstbyte, set "none" for the whole branch. In both cases, a zero |
| 2549 |
|
|
repeat forces firstbyte to "none". */ |
| 2550 |
|
|
|
| 2551 |
|
|
if (firstbyte == REQ_UNSET) |
| 2552 |
|
|
{ |
| 2553 |
|
|
if (subfirstbyte >= 0) |
| 2554 |
|
|
{ |
| 2555 |
|
|
firstbyte = subfirstbyte; |
| 2556 |
|
|
groupsetfirstbyte = TRUE; |
| 2557 |
|
|
} |
| 2558 |
|
|
else firstbyte = REQ_NONE; |
| 2559 |
|
|
zerofirstbyte = REQ_NONE; |
| 2560 |
|
|
} |
| 2561 |
|
|
|
| 2562 |
|
|
/* If firstbyte was previously set, convert the subpattern's firstbyte |
| 2563 |
|
|
into reqbyte if there wasn't one, using the vary flag that was in |
| 2564 |
|
|
existence beforehand. */ |
| 2565 |
|
|
|
| 2566 |
|
|
else if (subfirstbyte >= 0 && subreqbyte < 0) |
| 2567 |
|
|
subreqbyte = subfirstbyte | tempreqvary; |
| 2568 |
|
|
|
| 2569 |
|
|
/* If the subpattern set a required byte (or set a first byte that isn't |
| 2570 |
|
|
really the first byte - see above), set it. */ |
| 2571 |
|
|
|
| 2572 |
|
|
if (subreqbyte >= 0) reqbyte = subreqbyte; |
| 2573 |
|
|
} |
| 2574 |
|
|
|
| 2575 |
|
|
/* For a forward assertion, we take the reqbyte, if set. This can be |
| 2576 |
|
|
helpful if the pattern that follows the assertion doesn't set a different |
| 2577 |
|
|
char. For example, it's useful for /(?=abcde).+/. We can't set firstbyte |
| 2578 |
|
|
for an assertion, however because it leads to incorrect effect for patterns |
| 2579 |
|
|
such as /(?=a)a.+/ when the "real" "a" would then become a reqbyte instead |
| 2580 |
|
|
of a firstbyte. This is overcome by a scan at the end if there's no |
| 2581 |
|
|
firstbyte, looking for an asserted first char. */ |
| 2582 |
|
|
|
| 2583 |
|
|
else if (bravalue == OP_ASSERT && subreqbyte >= 0) reqbyte = subreqbyte; |
| 2584 |
|
|
|
| 2585 |
|
|
/* Now update the main code pointer to the end of the group. */ |
| 2586 |
|
|
|
| 2587 |
|
|
code = tempcode; |
| 2588 |
|
|
|
| 2589 |
|
|
/* Error if hit end of pattern */ |
| 2590 |
|
|
|
| 2591 |
|
|
if (*ptr != ')') |
| 2592 |
|
|
{ |
| 2593 |
|
|
*errorcodeptr = ERR14; |
| 2594 |
|
|
goto FAILED; |
| 2595 |
|
|
} |
| 2596 |
|
|
break; |
| 2597 |
|
|
|
| 2598 |
|
|
/* Check \ for being a real metacharacter; if not, fall through and handle |
| 2599 |
|
|
it as a data character at the start of a string. Escape items are checked |
| 2600 |
|
|
for validity in the pre-compiling pass. */ |
| 2601 |
|
|
|
| 2602 |
|
|
case '\\': |
| 2603 |
|
|
tempptr = ptr; |
| 2604 |
|
|
c = check_escape(&ptr, errorcodeptr, *brackets, options, FALSE); |
| 2605 |
|
|
|
| 2606 |
|
|
/* Handle metacharacters introduced by \. For ones like \d, the ESC_ values |
| 2607 |
|
|
are arranged to be the negation of the corresponding OP_values. For the |
| 2608 |
|
|
back references, the values are ESC_REF plus the reference number. Only |
| 2609 |
|
|
back references and those types that consume a character may be repeated. |
| 2610 |
|
|
We can test for values between ESC_b and ESC_Z for the latter; this may |
| 2611 |
|
|
have to change if any new ones are ever created. */ |
| 2612 |
|
|
|
| 2613 |
|
|
if (c < 0) |
| 2614 |
|
|
{ |
| 2615 |
|
|
if (-c == ESC_Q) /* Handle start of quoted string */ |
| 2616 |
|
|
{ |
| 2617 |
|
|
if (ptr[1] == '\\' && ptr[2] == 'E') ptr += 2; /* avoid empty string */ |
| 2618 |
|
|
else inescq = TRUE; |
| 2619 |
|
|
continue; |
| 2620 |
|
|
} |
| 2621 |
|
|
|
| 2622 |
|
|
/* For metasequences that actually match a character, we disable the |
| 2623 |
|
|
setting of a first character if it hasn't already been set. */ |
| 2624 |
|
|
|
| 2625 |
|
|
if (firstbyte == REQ_UNSET && -c > ESC_b && -c < ESC_Z) |
| 2626 |
|
|
firstbyte = REQ_NONE; |
| 2627 |
|
|
|
| 2628 |
|
|
/* Set values to reset to if this is followed by a zero repeat. */ |
| 2629 |
|
|
|
| 2630 |
|
|
zerofirstbyte = firstbyte; |
| 2631 |
|
|
zeroreqbyte = reqbyte; |
| 2632 |
|
|
|
| 2633 |
|
|
/* Back references are handled specially */ |
| 2634 |
|
|
|
| 2635 |
|
|
if (-c >= ESC_REF) |
| 2636 |
|
|
{ |
| 2637 |
|
|
int number = -c - ESC_REF; |
| 2638 |
|
|
previous = code; |
| 2639 |
|
|
*code++ = OP_REF; |
| 2640 |
|
|
PUT2INC(code, 0, number); |
| 2641 |
|
|
} |
| 2642 |
|
|
|
| 2643 |
|
|
/* For the rest, we can obtain the OP value by negating the escape |
| 2644 |
|
|
value */ |
| 2645 |
|
|
|
| 2646 |
|
|
else |
| 2647 |
|
|
{ |
| 2648 |
|
|
previous = (-c > ESC_b && -c < ESC_Z)? code : NULL; |
| 2649 |
|
|
*code++ = -c; |
| 2650 |
|
|
} |
| 2651 |
|
|
continue; |
| 2652 |
|
|
} |
| 2653 |
|
|
|
| 2654 |
|
|
mcbuffer[0] = c; |
| 2655 |
|
|
mclength = 1; |
| 2656 |
|
|
|
| 2657 |
|
|
goto ONE_CHAR; |
| 2658 |
|
|
|
| 2659 |
|
|
/* Handle a literal character. It is guaranteed not to be whitespace or # |
| 2660 |
|
|
when the extended flag is set. If we are in UTF-8 mode, it may be a |
| 2661 |
|
|
multi-byte literal character. */ |
| 2662 |
|
|
|
| 2663 |
|
|
default: |
| 2664 |
|
|
NORMAL_CHAR: |
| 2665 |
|
|
mclength = 1; |
| 2666 |
|
|
mcbuffer[0] = c; |
| 2667 |
|
|
|
| 2668 |
|
|
/* At this point we have the character's bytes in mcbuffer, and the length |
| 2669 |
|
|
in mclength. When not in UTF-8 mode, the length is always 1. */ |
| 2670 |
|
|
|
| 2671 |
|
|
ONE_CHAR: |
| 2672 |
|
|
previous = code; |
| 2673 |
|
|
*code++ = ((options & PCRE_CASELESS) != 0)? OP_CHARNC : OP_CHAR; |
| 2674 |
|
|
for (c = 0; c < mclength; c++) *code++ = mcbuffer[c]; |
| 2675 |
|
|
|
| 2676 |
|
|
/* Set the first and required bytes appropriately. If no previous first |
| 2677 |
|
|
byte, set it from this character, but revert to none on a zero repeat. |
| 2678 |
|
|
Otherwise, leave the firstbyte value alone, and don't change it on a zero |
| 2679 |
|
|
repeat. */ |
| 2680 |
|
|
|
| 2681 |
|
|
if (firstbyte == REQ_UNSET) |
| 2682 |
|
|
{ |
| 2683 |
|
|
zerofirstbyte = REQ_NONE; |
| 2684 |
|
|
zeroreqbyte = reqbyte; |
| 2685 |
|
|
|
| 2686 |
|
|
/* If the character is more than one byte long, we can set firstbyte |
| 2687 |
|
|
only if it is not to be matched caselessly. */ |
| 2688 |
|
|
|
| 2689 |
|
|
if (mclength == 1 || req_caseopt == 0) |
| 2690 |
|
|
{ |
| 2691 |
|
|
firstbyte = mcbuffer[0] | req_caseopt; |
| 2692 |
|
|
if (mclength != 1) reqbyte = code[-1] | cd->req_varyopt; |
| 2693 |
|
|
} |
| 2694 |
|
|
else firstbyte = reqbyte = REQ_NONE; |
| 2695 |
|
|
} |
| 2696 |
|
|
|
| 2697 |
|
|
/* firstbyte was previously set; we can set reqbyte only the length is |
| 2698 |
|
|
1 or the matching is caseful. */ |
| 2699 |
|
|
|
| 2700 |
|
|
else |
| 2701 |
|
|
{ |
| 2702 |
|
|
zerofirstbyte = firstbyte; |
| 2703 |
|
|
zeroreqbyte = reqbyte; |
| 2704 |
|
|
if (mclength == 1 || req_caseopt == 0) |
| 2705 |
|
|
reqbyte = code[-1] | req_caseopt | cd->req_varyopt; |
| 2706 |
|
|
} |
| 2707 |
|
|
|
| 2708 |
|
|
break; /* End of literal character handling */ |
| 2709 |
|
|
} |
| 2710 |
|
|
} /* end of big loop */ |
| 2711 |
|
|
|
| 2712 |
|
|
/* Control never reaches here by falling through, only by a goto for all the |
| 2713 |
|
|
error states. Pass back the position in the pattern so that it can be displayed |
| 2714 |
|
|
to the user for diagnosing the error. */ |
| 2715 |
|
|
|
| 2716 |
|
|
FAILED: |
| 2717 |
|
|
*ptrptr = ptr; |
| 2718 |
|
|
return FALSE; |
| 2719 |
|
|
} |
| 2720 |
|
|
|
| 2721 |
|
|
|
| 2722 |
|
|
|
| 2723 |
|
|
|
| 2724 |
|
|
/************************************************* |
| 2725 |
|
|
* Compile sequence of alternatives * |
| 2726 |
|
|
*************************************************/ |
| 2727 |
|
|
|
| 2728 |
|
|
/* On entry, ptr is pointing past the bracket character, but on return |
| 2729 |
|
|
it points to the closing bracket, or vertical bar, or end of string. |
| 2730 |
|
|
The code variable is pointing at the byte into which the BRA operator has been |
| 2731 |
|
|
stored. If the ims options are changed at the start (for a (?ims: group) or |
| 2732 |
|
|
during any branch, we need to insert an OP_OPT item at the start of every |
| 2733 |
|
|
following branch to ensure they get set correctly at run time, and also pass |
| 2734 |
|
|
the new options into every subsequent branch compile. |
| 2735 |
|
|
|
| 2736 |
|
|
Argument: |
| 2737 |
|
|
options option bits, including any changes for this subpattern |
| 2738 |
|
|
oldims previous settings of ims option bits |
| 2739 |
|
|
brackets -> int containing the number of extracting brackets used |
| 2740 |
|
|
codeptr -> the address of the current code pointer |
| 2741 |
|
|
ptrptr -> the address of the current pattern pointer |
| 2742 |
|
|
errorcodeptr -> pointer to error code variable |
| 2743 |
|
|
lookbehind TRUE if this is a lookbehind assertion |
| 2744 |
|
|
skipbytes skip this many bytes at start (for OP_COND, OP_BRANUMBER) |
| 2745 |
|
|
firstbyteptr place to put the first required character, or a negative number |
| 2746 |
|
|
reqbyteptr place to put the last required character, or a negative number |
| 2747 |
|
|
bcptr pointer to the chain of currently open branches |
| 2748 |
|
|
cd points to the data block with tables pointers etc. |
| 2749 |
|
|
|
| 2750 |
|
|
Returns: TRUE on success |
| 2751 |
|
|
*/ |
| 2752 |
|
|
|
| 2753 |
|
|
static BOOL |
| 2754 |
|
|
compile_regex(int options, int oldims, int *brackets, uschar **codeptr, |
| 2755 |
|
|
const uschar **ptrptr, int *errorcodeptr, BOOL lookbehind, int skipbytes, |
| 2756 |
|
|
int *firstbyteptr, int *reqbyteptr, branch_chain *bcptr, compile_data *cd) |
| 2757 |
|
|
{ |
| 2758 |
|
|
const uschar *ptr = *ptrptr; |
| 2759 |
|
|
uschar *code = *codeptr; |
| 2760 |
|
|
uschar *last_branch = code; |
| 2761 |
|
|
uschar *start_bracket = code; |
| 2762 |
|
|
uschar *reverse_count = NULL; |
| 2763 |
|
|
int firstbyte, reqbyte; |
| 2764 |
|
|
int branchfirstbyte, branchreqbyte; |
| 2765 |
|
|
branch_chain bc; |
| 2766 |
|
|
|
| 2767 |
|
|
bc.outer = bcptr; |
| 2768 |
|
|
bc.current = code; |
| 2769 |
|
|
|
| 2770 |
|
|
firstbyte = reqbyte = REQ_UNSET; |
| 2771 |
|
|
|
| 2772 |
|
|
/* Offset is set zero to mark that this bracket is still open */ |
| 2773 |
|
|
|
| 2774 |
|
|
PUT(code, 1, 0); |
| 2775 |
|
|
code += 1 + LINK_SIZE + skipbytes; |
| 2776 |
|
|
|
| 2777 |
|
|
/* Loop for each alternative branch */ |
| 2778 |
|
|
|
| 2779 |
|
|
for (;;) |
| 2780 |
|
|
{ |
| 2781 |
|
|
/* Handle a change of ims options at the start of the branch */ |
| 2782 |
|
|
|
| 2783 |
|
|
if ((options & PCRE_IMS) != oldims) |
| 2784 |
|
|
{ |
| 2785 |
|
|
*code++ = OP_OPT; |
| 2786 |
|
|
*code++ = options & PCRE_IMS; |
| 2787 |
|
|
} |
| 2788 |
|
|
|
| 2789 |
|
|
/* Set up dummy OP_REVERSE if lookbehind assertion */ |
| 2790 |
|
|
|
| 2791 |
|
|
if (lookbehind) |
| 2792 |
|
|
{ |
| 2793 |
|
|
*code++ = OP_REVERSE; |
| 2794 |
|
|
reverse_count = code; |
| 2795 |
|
|
PUTINC(code, 0, 0); |
| 2796 |
|
|
} |
| 2797 |
|
|
|
| 2798 |
|
|
/* Now compile the branch */ |
| 2799 |
|
|
|
| 2800 |
|
|
if (!compile_branch(&options, brackets, &code, &ptr, errorcodeptr, |
| 2801 |
|
|
&branchfirstbyte, &branchreqbyte, &bc, cd)) |
| 2802 |
|
|
{ |
| 2803 |
|
|
*ptrptr = ptr; |
| 2804 |
|
|
return FALSE; |
| 2805 |
|
|
} |
| 2806 |
|
|
|
| 2807 |
|
|
/* If this is the first branch, the firstbyte and reqbyte values for the |
| 2808 |
|
|
branch become the values for the regex. */ |
| 2809 |
|
|
|
| 2810 |
|
|
if (*last_branch != OP_ALT) |
| 2811 |
|
|
{ |
| 2812 |
|
|
firstbyte = branchfirstbyte; |
| 2813 |
|
|
reqbyte = branchreqbyte; |
| 2814 |
|
|
} |
| 2815 |
|
|
|
| 2816 |
|
|
/* If this is not the first branch, the first char and reqbyte have to |
| 2817 |
|
|
match the values from all the previous branches, except that if the previous |
| 2818 |
|
|
value for reqbyte didn't have REQ_VARY set, it can still match, and we set |
| 2819 |
|
|
REQ_VARY for the regex. */ |
| 2820 |
|
|
|
| 2821 |
|
|
else |
| 2822 |
|
|
{ |
| 2823 |
|
|
/* If we previously had a firstbyte, but it doesn't match the new branch, |
| 2824 |
|
|
we have to abandon the firstbyte for the regex, but if there was previously |
| 2825 |
|
|
no reqbyte, it takes on the value of the old firstbyte. */ |
| 2826 |
|
|
|
| 2827 |
|
|
if (firstbyte >= 0 && firstbyte != branchfirstbyte) |
| 2828 |
|
|
{ |
| 2829 |
|
|
if (reqbyte < 0) reqbyte = firstbyte; |
| 2830 |
|
|
firstbyte = REQ_NONE; |
| 2831 |
|
|
} |
| 2832 |
|
|
|
| 2833 |
|
|
/* If we (now or from before) have no firstbyte, a firstbyte from the |
| 2834 |
|
|
branch becomes a reqbyte if there isn't a branch reqbyte. */ |
| 2835 |
|
|
|
| 2836 |
|
|
if (firstbyte < 0 && branchfirstbyte >= 0 && branchreqbyte < 0) |
| 2837 |
|
|
branchreqbyte = branchfirstbyte; |
| 2838 |
|
|
|
| 2839 |
|
|
/* Now ensure that the reqbytes match */ |
| 2840 |
|
|
|
| 2841 |
|
|
if ((reqbyte & ~REQ_VARY) != (branchreqbyte & ~REQ_VARY)) |
| 2842 |
|
|
reqbyte = REQ_NONE; |
| 2843 |
|
|
else reqbyte |= branchreqbyte; /* To "or" REQ_VARY */ |
| 2844 |
|
|
} |
| 2845 |
|
|
|
| 2846 |
|
|
/* If lookbehind, check that this branch matches a fixed-length string, |
| 2847 |
|
|
and put the length into the OP_REVERSE item. Temporarily mark the end of |
| 2848 |
|
|
the branch with OP_END. */ |
| 2849 |
|
|
|
| 2850 |
|
|
if (lookbehind) |
| 2851 |
|
|
{ |
| 2852 |
|
|
int length; |
| 2853 |
|
|
*code = OP_END; |
| 2854 |
|
|
length = find_fixedlength(last_branch, options); |
| 2855 |
|
|
DPRINTF(("fixed length = %d\n", length)); |
| 2856 |
|
|
if (length < 0) |
| 2857 |
|
|
{ |
| 2858 |
|
|
*errorcodeptr = (length == -2)? ERR36 : ERR25; |
| 2859 |
|
|
*ptrptr = ptr; |
| 2860 |
|
|
return FALSE; |
| 2861 |
|
|
} |
| 2862 |
|
|
PUT(reverse_count, 0, length); |
| 2863 |
|
|
} |
| 2864 |
|
|
|
| 2865 |
|
|
/* Reached end of expression, either ')' or end of pattern. Go back through |
| 2866 |
|
|
the alternative branches and reverse the chain of offsets, with the field in |
| 2867 |
|
|
the BRA item now becoming an offset to the first alternative. If there are |
| 2868 |
|
|
no alternatives, it points to the end of the group. The length in the |
| 2869 |
|
|
terminating ket is always the length of the whole bracketed item. If any of |
| 2870 |
|
|
the ims options were changed inside the group, compile a resetting op-code |
| 2871 |
|
|
following, except at the very end of the pattern. Return leaving the pointer |
| 2872 |
|
|
at the terminating char. */ |
| 2873 |
|
|
|
| 2874 |
|
|
if (*ptr != '|') |
| 2875 |
|
|
{ |
| 2876 |
|
|
int length = code - last_branch; |
| 2877 |
|
|
do |
| 2878 |
|
|
{ |
| 2879 |
|
|
int prev_length = GET(last_branch, 1); |
| 2880 |
|
|
PUT(last_branch, 1, length); |
| 2881 |
|
|
length = prev_length; |
| 2882 |
|
|
last_branch -= length; |
| 2883 |
|
|
} |
| 2884 |
|
|
while (length > 0); |
| 2885 |
|
|
|
| 2886 |
|
|
/* Fill in the ket */ |
| 2887 |
|
|
|
| 2888 |
|
|
*code = OP_KET; |
| 2889 |
|
|
PUT(code, 1, code - start_bracket); |
| 2890 |
|
|
code += 1 + LINK_SIZE; |
| 2891 |
|
|
|
| 2892 |
|
|
/* Resetting option if needed */ |
| 2893 |
|
|
|
| 2894 |
|
|
if ((options & PCRE_IMS) != oldims && *ptr == ')') |
| 2895 |
|
|
{ |
| 2896 |
|
|
*code++ = OP_OPT; |
| 2897 |
|
|
*code++ = oldims; |
| 2898 |
|
|
} |
| 2899 |
|
|
|
| 2900 |
|
|
/* Set values to pass back */ |
| 2901 |
|
|
|
| 2902 |
|
|
*codeptr = code; |
| 2903 |
|
|
*ptrptr = ptr; |
| 2904 |
|
|
*firstbyteptr = firstbyte; |
| 2905 |
|
|
*reqbyteptr = reqbyte; |
| 2906 |
|
|
return TRUE; |
| 2907 |
|
|
} |
| 2908 |
|
|
|
| 2909 |
|
|
/* Another branch follows; insert an "or" node. Its length field points back |
| 2910 |
|
|
to the previous branch while the bracket remains open. At the end the chain |
| 2911 |
|
|
is reversed. It's done like this so that the start of the bracket has a |
| 2912 |
|
|
zero offset until it is closed, making it possible to detect recursion. */ |
| 2913 |
|
|
|
| 2914 |
|
|
*code = OP_ALT; |
| 2915 |
|
|
PUT(code, 1, code - last_branch); |
| 2916 |
|
|
bc.current = last_branch = code; |
| 2917 |
|
|
code += 1 + LINK_SIZE; |
| 2918 |
|
|
ptr++; |
| 2919 |
|
|
} |
| 2920 |
|
|
/* Control never reaches here */ |
| 2921 |
|
|
} |
| 2922 |
|
|
|
| 2923 |
|
|
|
| 2924 |
|
|
|
| 2925 |
|
|
|
| 2926 |
|
|
/************************************************* |
| 2927 |
|
|
* Check for anchored expression * |
| 2928 |
|
|
*************************************************/ |
| 2929 |
|
|
|
| 2930 |
|
|
/* Try to find out if this is an anchored regular expression. Consider each |
| 2931 |
|
|
alternative branch. If they all start with OP_SOD or OP_CIRC, or with a bracket |
| 2932 |
|
|
all of whose alternatives start with OP_SOD or OP_CIRC (recurse ad lib), then |
| 2933 |
|
|
it's anchored. However, if this is a multiline pattern, then only OP_SOD |
| 2934 |
|
|
counts, since OP_CIRC can match in the middle. |
| 2935 |
|
|
|
| 2936 |
|
|
We can also consider a regex to be anchored if OP_SOM starts all its branches. |
| 2937 |
|
|
This is the code for \G, which means "match at start of match position, taking |
| 2938 |
|
|
into account the match offset". |
| 2939 |
|
|
|
| 2940 |
|
|
A branch is also implicitly anchored if it starts with .* and DOTALL is set, |
| 2941 |
|
|
because that will try the rest of the pattern at all possible matching points, |
| 2942 |
|
|
so there is no point trying again.... er .... |
| 2943 |
|
|
|
| 2944 |
|
|
.... except when the .* appears inside capturing parentheses, and there is a |
| 2945 |
|
|
subsequent back reference to those parentheses. We haven't enough information |
| 2946 |
|
|
to catch that case precisely. |
| 2947 |
|
|
|
| 2948 |
|
|
At first, the best we could do was to detect when .* was in capturing brackets |
| 2949 |
|
|
and the highest back reference was greater than or equal to that level. |
| 2950 |
|
|
However, by keeping a bitmap of the first 31 back references, we can catch some |
| 2951 |
|
|
of the more common cases more precisely. |
| 2952 |
|
|
|
| 2953 |
|
|
Arguments: |
| 2954 |
|
|
code points to start of expression (the bracket) |
| 2955 |
|
|
options points to the options setting |
| 2956 |
|
|
bracket_map a bitmap of which brackets we are inside while testing; this |
| 2957 |
|
|
handles up to substring 31; after that we just have to take |
| 2958 |
|
|
the less precise approach |
| 2959 |
|
|
backref_map the back reference bitmap |
| 2960 |
|
|
|
| 2961 |
|
|
Returns: TRUE or FALSE |
| 2962 |
|
|
*/ |
| 2963 |
|
|
|
| 2964 |
|
|
static BOOL |
| 2965 |
|
|
is_anchored(register const uschar *code, int *options, unsigned int bracket_map, |
| 2966 |
|
|
unsigned int backref_map) |
| 2967 |
|
|
{ |
| 2968 |
|
|
do { |
| 2969 |
|
|
const uschar *scode = |
| 2970 |
|
|
first_significant_code(code + 1+LINK_SIZE, options, PCRE_MULTILINE, FALSE); |
| 2971 |
|
|
register int op = *scode; |
| 2972 |
|
|
|
| 2973 |
|
|
/* Capturing brackets */ |
| 2974 |
|
|
|
| 2975 |
|
|
if (op > OP_BRA) |
| 2976 |
|
|
{ |
| 2977 |
|
|
int new_map; |
| 2978 |
|
|
op -= OP_BRA; |
| 2979 |
|
|
if (op > EXTRACT_BASIC_MAX) op = GET2(scode, 2+LINK_SIZE); |
| 2980 |
|
|
new_map = bracket_map | ((op < 32)? (1 << op) : 1); |
| 2981 |
|
|
if (!is_anchored(scode, options, new_map, backref_map)) return FALSE; |
| 2982 |
|
|
} |
| 2983 |
|
|
|
| 2984 |
|
|
/* Other brackets */ |
| 2985 |
|
|
|
| 2986 |
|
|
else if (op == OP_BRA || op == OP_ASSERT || op == OP_ONCE || op == OP_COND) |
| 2987 |
|
|
{ |
| 2988 |
|
|
if (!is_anchored(scode, options, bracket_map, backref_map)) return FALSE; |
| 2989 |
|
|
} |
| 2990 |
|
|
|
| 2991 |
|
|
/* .* is not anchored unless DOTALL is set and it isn't in brackets that |
| 2992 |
|
|
are or may be referenced. */ |
| 2993 |
|
|
|
| 2994 |
|
|
else if ((op == OP_TYPESTAR || op == OP_TYPEMINSTAR) && |
| 2995 |
|
|
(*options & PCRE_DOTALL) != 0) |
| 2996 |
|
|
{ |
| 2997 |
|
|
if (scode[1] != OP_ANY || (bracket_map & backref_map) != 0) return FALSE; |
| 2998 |
|
|
} |
| 2999 |
|
|
|
| 3000 |
|
|
/* Check for explicit anchoring */ |
| 3001 |
|
|
|
| 3002 |
|
|
else if (op != OP_SOD && op != OP_SOM && |
| 3003 |
|
|
((*options & PCRE_MULTILINE) != 0 || op != OP_CIRC)) |
| 3004 |
|
|
return FALSE; |
| 3005 |
|
|
code += GET(code, 1); |
| 3006 |
|
|
} |
| 3007 |
|
|
while (*code == OP_ALT); /* Loop for each alternative */ |
| 3008 |
|
|
return TRUE; |
| 3009 |
|
|
} |
| 3010 |
|
|
|
| 3011 |
|
|
|
| 3012 |
|
|
|
| 3013 |
|
|
/************************************************* |
| 3014 |
|
|
* Check for starting with ^ or .* * |
| 3015 |
|
|
*************************************************/ |
| 3016 |
|
|
|
| 3017 |
|
|
/* This is called to find out if every branch starts with ^ or .* so that |
| 3018 |
|
|
"first char" processing can be done to speed things up in multiline |
| 3019 |
|
|
matching and for non-DOTALL patterns that start with .* (which must start at |
| 3020 |
|
|
the beginning or after \n). As in the case of is_anchored() (see above), we |
| 3021 |
|
|
have to take account of back references to capturing brackets that contain .* |
| 3022 |
|
|
because in that case we can't make the assumption. |
| 3023 |
|
|
|
| 3024 |
|
|
Arguments: |
| 3025 |
|
|
code points to start of expression (the bracket) |
| 3026 |
|
|
bracket_map a bitmap of which brackets we are inside while testing; this |
| 3027 |
|
|
handles up to substring 31; after that we just have to take |
| 3028 |
|
|
the less precise approach |
| 3029 |
|
|
backref_map the back reference bitmap |
| 3030 |
|
|
|
| 3031 |
|
|
Returns: TRUE or FALSE |
| 3032 |
|
|
*/ |
| 3033 |
|
|
|
| 3034 |
|
|
static BOOL |
| 3035 |
|
|
is_startline(const uschar *code, unsigned int bracket_map, |
| 3036 |
|
|
unsigned int backref_map) |
| 3037 |
|
|
{ |
| 3038 |
|
|
do { |
| 3039 |
|
|
const uschar *scode = first_significant_code(code + 1+LINK_SIZE, NULL, 0, |
| 3040 |
|
|
FALSE); |
| 3041 |
|
|
register int op = *scode; |
| 3042 |
|
|
|
| 3043 |
|
|
/* Capturing brackets */ |
| 3044 |
|
|
|
| 3045 |
|
|
if (op > OP_BRA) |
| 3046 |
|
|
{ |
| 3047 |
|
|
int new_map; |
| 3048 |
|
|
op -= OP_BRA; |
| 3049 |
|
|
if (op > EXTRACT_BASIC_MAX) op = GET2(scode, 2+LINK_SIZE); |
| 3050 |
|
|
new_map = bracket_map | ((op < 32)? (1 << op) : 1); |
| 3051 |
|
|
if (!is_startline(scode, new_map, backref_map)) return FALSE; |
| 3052 |
|
|
} |
| 3053 |
|
|
|
| 3054 |
|
|
/* Other brackets */ |
| 3055 |
|
|
|
| 3056 |
|
|
else if (op == OP_BRA || op == OP_ASSERT || op == OP_ONCE || op == OP_COND) |
| 3057 |
|
|
{ if (!is_startline(scode, bracket_map, backref_map)) return FALSE; } |
| 3058 |
|
|
|
| 3059 |
|
|
/* .* means "start at start or after \n" if it isn't in brackets that |
| 3060 |
|
|
may be referenced. */ |
| 3061 |
|
|
|
| 3062 |
|
|
else if (op == OP_TYPESTAR || op == OP_TYPEMINSTAR) |
| 3063 |
|
|
{ |
| 3064 |
|
|
if (scode[1] != OP_ANY || (bracket_map & backref_map) != 0) return FALSE; |
| 3065 |
|
|
} |
| 3066 |
|
|
|
| 3067 |
|
|
/* Check for explicit circumflex */ |
| 3068 |
|
|
|
| 3069 |
|
|
else if (op != OP_CIRC) return FALSE; |
| 3070 |
|
|
|
| 3071 |
|
|
/* Move on to the next alternative */ |
| 3072 |
|
|
|
| 3073 |
|
|
code += GET(code, 1); |
| 3074 |
|
|
} |
| 3075 |
|
|
while (*code == OP_ALT); /* Loop for each alternative */ |
| 3076 |
|
|
return TRUE; |
| 3077 |
|
|
} |
| 3078 |
|
|
|
| 3079 |
|
|
|
| 3080 |
|
|
|
| 3081 |
|
|
/************************************************* |
| 3082 |
|
|
* Check for asserted fixed first char * |
| 3083 |
|
|
*************************************************/ |
| 3084 |
|
|
|
| 3085 |
|
|
/* During compilation, the "first char" settings from forward assertions are |
| 3086 |
|
|
discarded, because they can cause conflicts with actual literals that follow. |
| 3087 |
|
|
However, if we end up without a first char setting for an unanchored pattern, |
| 3088 |
|
|
it is worth scanning the regex to see if there is an initial asserted first |
| 3089 |
|
|
char. If all branches start with the same asserted char, or with a bracket all |
| 3090 |
|
|
of whose alternatives start with the same asserted char (recurse ad lib), then |
| 3091 |
|
|
we return that char, otherwise -1. |
| 3092 |
|
|
|
| 3093 |
|
|
Arguments: |
| 3094 |
|
|
code points to start of expression (the bracket) |
| 3095 |
|
|
options pointer to the options (used to check casing changes) |
| 3096 |
|
|
inassert TRUE if in an assertion |
| 3097 |
|
|
|
| 3098 |
|
|
Returns: -1 or the fixed first char |
| 3099 |
|
|
*/ |
| 3100 |
|
|
|
| 3101 |
|
|
static int |
| 3102 |
|
|
find_firstassertedchar(const uschar *code, int *options, BOOL inassert) |
| 3103 |
|
|
{ |
| 3104 |
|
|
register int c = -1; |
| 3105 |
|
|
do { |
| 3106 |
|
|
int d; |
| 3107 |
|
|
const uschar *scode = |
| 3108 |
|
|
first_significant_code(code + 1+LINK_SIZE, options, PCRE_CASELESS, TRUE); |
| 3109 |
|
|
register int op = *scode; |
| 3110 |
|
|
|
| 3111 |
|
|
if (op >= OP_BRA) op = OP_BRA; |
| 3112 |
|
|
|
| 3113 |
|
|
switch(op) |
| 3114 |
|
|
{ |
| 3115 |
|
|
default: |
| 3116 |
|
|
return -1; |
| 3117 |
|
|
|
| 3118 |
|
|
case OP_BRA: |
| 3119 |
|
|
case OP_ASSERT: |
| 3120 |
|
|
case OP_ONCE: |
| 3121 |
|
|
case OP_COND: |
| 3122 |
|
|
if ((d = find_firstassertedchar(scode, options, op == OP_ASSERT)) < 0) |
| 3123 |
|
|
return -1; |
| 3124 |
|
|
if (c < 0) c = d; else if (c != d) return -1; |
| 3125 |
|
|
break; |
| 3126 |
|
|
|
| 3127 |
|
|
case OP_EXACT: /* Fall through */ |
| 3128 |
|
|
scode += 2; |
| 3129 |
|
|
|
| 3130 |
|
|
case OP_CHAR: |
| 3131 |
|
|
case OP_CHARNC: |
| 3132 |
|
|
case OP_PLUS: |
| 3133 |
|
|
case OP_MINPLUS: |
| 3134 |
|
|
if (!inassert) return -1; |
| 3135 |
|
|
if (c < 0) |
| 3136 |
|
|
{ |
| 3137 |
|
|
c = scode[1]; |
| 3138 |
|
|
if ((*options & PCRE_CASELESS) != 0) c |= REQ_CASELESS; |
| 3139 |
|
|
} |
| 3140 |
|
|
else if (c != scode[1]) return -1; |
| 3141 |
|
|
break; |
| 3142 |
|
|
} |
| 3143 |
|
|
|
| 3144 |
|
|
code += GET(code, 1); |
| 3145 |
|
|
} |
| 3146 |
|
|
while (*code == OP_ALT); |
| 3147 |
|
|
return c; |
| 3148 |
|
|
} |
| 3149 |
|
|
|
| 3150 |
|
|
|
| 3151 |
|
|
|
| 3152 |
|
|
/************************************************* |
| 3153 |
|
|
* Compile a Regular Expression * |
| 3154 |
|
|
*************************************************/ |
| 3155 |
|
|
|
| 3156 |
|
|
/* This function takes a string and returns a pointer to a block of store |
| 3157 |
|
|
holding a compiled version of the expression. The original API for this |
| 3158 |
|
|
function had no error code return variable; it is retained for backwards |
| 3159 |
|
|
compatibility. The new function is given a new name. |
| 3160 |
|
|
|
| 3161 |
|
|
Arguments: |
| 3162 |
|
|
pattern the regular expression |
| 3163 |
|
|
options various option bits |
| 3164 |
|
|
errorcodeptr pointer to error code variable (pcre_compile2() only) |
| 3165 |
|
|
can be NULL if you don't want a code value |
| 3166 |
|
|
errorptr pointer to pointer to error text |
| 3167 |
|
|
erroroffset ptr offset in pattern where error was detected |
| 3168 |
|
|
tables pointer to character tables or NULL |
| 3169 |
|
|
|
| 3170 |
|
|
Returns: pointer to compiled data block, or NULL on error, |
| 3171 |
|
|
with errorptr and erroroffset set |
| 3172 |
|
|
*/ |
| 3173 |
|
|
|
| 3174 |
|
|
EXPORT pcre * |
| 3175 |
|
|
pcre_compile(const char *pattern, int options, const char **errorptr, |
| 3176 |
|
|
int *erroroffset, const unsigned char *tables) |
| 3177 |
|
|
{ |
| 3178 |
|
|
return pcre_compile2(pattern, options, NULL, errorptr, erroroffset, tables); |
| 3179 |
|
|
} |
| 3180 |
|
|
|
| 3181 |
|
|
|
| 3182 |
|
|
EXPORT pcre * |
| 3183 |
|
|
pcre_compile2(const char *pattern, int options, int *errorcodeptr, |
| 3184 |
|
|
const char **errorptr, int *erroroffset, const unsigned char *tables) |
| 3185 |
|
|
{ |
| 3186 |
|
|
real_pcre *re; |
| 3187 |
|
|
int length = 1 + LINK_SIZE; /* For initial BRA plus length */ |
| 3188 |
|
|
int c, firstbyte, reqbyte; |
| 3189 |
|
|
int bracount = 0; |
| 3190 |
|
|
int branch_extra = 0; |
| 3191 |
|
|
int branch_newextra; |
| 3192 |
|
|
int item_count = -1; |
| 3193 |
|
|
int name_count = 0; |
| 3194 |
|
|
int max_name_size = 0; |
| 3195 |
|
|
int lastitemlength = 0; |
| 3196 |
|
|
int errorcode = 0; |
| 3197 |
|
|
BOOL inescq = FALSE; |
| 3198 |
|
|
BOOL capturing; |
| 3199 |
|
|
unsigned int brastackptr = 0; |
| 3200 |
|
|
size_t size; |
| 3201 |
|
|
uschar *code; |
| 3202 |
|
|
const uschar *codestart; |
| 3203 |
|
|
const uschar *ptr; |
| 3204 |
|
|
compile_data compile_block; |
| 3205 |
|
|
int brastack[BRASTACK_SIZE]; |
| 3206 |
|
|
uschar bralenstack[BRASTACK_SIZE]; |
| 3207 |
|
|
|
| 3208 |
|
|
/* We can't pass back an error message if errorptr is NULL; I guess the best we |
| 3209 |
|
|
can do is just return NULL, but we can set a code value if there is a code |
| 3210 |
|
|
pointer. */ |
| 3211 |
|
|
|
| 3212 |
|
|
if (errorptr == NULL) |
| 3213 |
|
|
{ |
| 3214 |
|
|
if (errorcodeptr != NULL) *errorcodeptr = 99; |
| 3215 |
|
|
return NULL; |
| 3216 |
|
|
} |
| 3217 |
|
|
|
| 3218 |
|
|
*errorptr = NULL; |
| 3219 |
|
|
if (errorcodeptr != NULL) *errorcodeptr = ERR0; |
| 3220 |
|
|
|
| 3221 |
|
|
/* However, we can give a message for this error */ |
| 3222 |
|
|
|
| 3223 |
|
|
if (erroroffset == NULL) |
| 3224 |
|
|
{ |
| 3225 |
|
|
errorcode = ERR16; |
| 3226 |
|
|
goto PCRE_EARLY_ERROR_RETURN; |
| 3227 |
|
|
} |
| 3228 |
|
|
|
| 3229 |
|
|
*erroroffset = 0; |
| 3230 |
|
|
|
| 3231 |
|
|
if ((options & PCRE_UTF8) != 0) |
| 3232 |
|
|
{ |
| 3233 |
|
|
errorcode = ERR32; |
| 3234 |
|
|
goto PCRE_EARLY_ERROR_RETURN; |
| 3235 |
|
|
} |
| 3236 |
|
|
|
| 3237 |
|
|
if ((options & ~PUBLIC_OPTIONS) != 0) |
| 3238 |
|
|
{ |
| 3239 |
|
|
errorcode = ERR17; |
| 3240 |
|
|
goto PCRE_EARLY_ERROR_RETURN; |
| 3241 |
|
|
} |
| 3242 |
|
|
|
| 3243 |
|
|
/* Set up pointers to the individual character tables */ |
| 3244 |
|
|
|
| 3245 |
|
|
if (tables == NULL) tables = _pcre_default_tables; |
| 3246 |
|
|
compile_block.lcc = tables + lcc_offset; |
| 3247 |
|
|
compile_block.fcc = tables + fcc_offset; |
| 3248 |
|
|
compile_block.cbits = tables + cbits_offset; |
| 3249 |
|
|
compile_block.ctypes = tables + ctypes_offset; |
| 3250 |
|
|
|
| 3251 |
|
|
/* Maximum back reference and backref bitmap. This is updated for numeric |
| 3252 |
|
|
references during the first pass, but for named references during the actual |
| 3253 |
|
|
compile pass. The bitmap records up to 31 back references to help in deciding |
| 3254 |
|
|
whether (.*) can be treated as anchored or not. */ |
| 3255 |
|
|
|
| 3256 |
|
|
compile_block.top_backref = 0; |
| 3257 |
|
|
compile_block.backref_map = 0; |
| 3258 |
|
|
|
| 3259 |
|
|
/* Reflect pattern for debugging output */ |
| 3260 |
|
|
|
| 3261 |
|
|
DPRINTF(("------------------------------------------------------------------\n")); |
| 3262 |
|
|
DPRINTF(("%s\n", pattern)); |
| 3263 |
|
|
|
| 3264 |
|
|
/* The first thing to do is to make a pass over the pattern to compute the |
| 3265 |
|
|
amount of store required to hold the compiled code. This does not have to be |
| 3266 |
|
|
perfect as long as errors are overestimates. At the same time we can detect any |
| 3267 |
|
|
flag settings right at the start, and extract them. Make an attempt to correct |
| 3268 |
|
|
for any counted white space if an "extended" flag setting appears late in the |
| 3269 |
|
|
pattern. We can't be so clever for #-comments. */ |
| 3270 |
|
|
|
| 3271 |
|
|
ptr = (const uschar *)(pattern - 1); |
| 3272 |
|
|
while ((c = *(++ptr)) != 0) |
| 3273 |
|
|
{ |
| 3274 |
|
|
int min, max; |
| 3275 |
|
|
int class_optcount; |
| 3276 |
|
|
int bracket_length; |
| 3277 |
|
|
int duplength; |
| 3278 |
|
|
|
| 3279 |
|
|
/* If we are inside a \Q...\E sequence, all chars are literal */ |
| 3280 |
|
|
|
| 3281 |
|
|
if (inescq) |
| 3282 |
|
|
{ |
| 3283 |
|
|
if ((options & PCRE_AUTO_CALLOUT) != 0) length += 2 + 2*LINK_SIZE; |
| 3284 |
|
|
goto NORMAL_CHAR; |
| 3285 |
|
|
} |
| 3286 |
|
|
|
| 3287 |
|
|
/* Otherwise, first check for ignored whitespace and comments */ |
| 3288 |
|
|
|
| 3289 |
|
|
if ((options & PCRE_EXTENDED) != 0) |
| 3290 |
|
|
{ |
| 3291 |
|
|
if ((compile_block.ctypes[c] & ctype_space) != 0) continue; |
| 3292 |
|
|
if (c == '#') |
| 3293 |
|
|
{ |
| 3294 |
|
|
/* The space before the ; is to avoid a warning on a silly compiler |
| 3295 |
|
|
on the Macintosh. */ |
| 3296 |
|
|
while ((c = *(++ptr)) != 0 && c != NEWLINE) ; |
| 3297 |
|
|
if (c == 0) break; |
| 3298 |
|
|
continue; |
| 3299 |
|
|
} |
| 3300 |
|
|
} |
| 3301 |
|
|
|
| 3302 |
|
|
item_count++; /* Is zero for the first non-comment item */ |
| 3303 |
|
|
|
| 3304 |
|
|
/* Allow space for auto callout before every item except quantifiers. */ |
| 3305 |
|
|
|
| 3306 |
|
|
if ((options & PCRE_AUTO_CALLOUT) != 0 && |
| 3307 |
|
|
c != '*' && c != '+' && c != '?' && |
| 3308 |
|
|
(c != '{' || !is_counted_repeat(ptr + 1))) |
| 3309 |
|
|
length += 2 + 2*LINK_SIZE; |
| 3310 |
|
|
|
| 3311 |
|
|
switch(c) |
| 3312 |
|
|
{ |
| 3313 |
|
|
/* A backslashed item may be an escaped data character or it may be a |
| 3314 |
|
|
character type. */ |
| 3315 |
|
|
|
| 3316 |
|
|
case '\\': |
| 3317 |
|
|
c = check_escape(&ptr, &errorcode, bracount, options, FALSE); |
| 3318 |
|
|
if (errorcode != 0) goto PCRE_ERROR_RETURN; |
| 3319 |
|
|
|
| 3320 |
|
|
lastitemlength = 1; /* Default length of last item for repeats */ |
| 3321 |
|
|
|
| 3322 |
|
|
if (c >= 0) /* Data character */ |
| 3323 |
|
|
{ |
| 3324 |
|
|
length += 2; /* For a one-byte character */ |
| 3325 |
|
|
continue; |
| 3326 |
|
|
} |
| 3327 |
|
|
|
| 3328 |
|
|
/* If \Q, enter "literal" mode */ |
| 3329 |
|
|
|
| 3330 |
|
|
if (-c == ESC_Q) |
| 3331 |
|
|
{ |
| 3332 |
|
|
inescq = TRUE; |
| 3333 |
|
|
continue; |
| 3334 |
|
|
} |
| 3335 |
|
|
|
| 3336 |
|
|
/* \X is supported only if Unicode property support is compiled */ |
| 3337 |
|
|
|
| 3338 |
|
|
if (-c == ESC_X) |
| 3339 |
|
|
{ |
| 3340 |
|
|
errorcode = ERR45; |
| 3341 |
|
|
goto PCRE_ERROR_RETURN; |
| 3342 |
|
|
} |
| 3343 |
|
|
|
| 3344 |
|
|
/* \P and \p are for Unicode properties, but only when the support has |
| 3345 |
|
|
been compiled. Each item needs 2 bytes. */ |
| 3346 |
|
|
|
| 3347 |
|
|
else if (-c == ESC_P || -c == ESC_p) |
| 3348 |
|
|
{ |
| 3349 |
|
|
errorcode = ERR45; |
| 3350 |
|
|
goto PCRE_ERROR_RETURN; |
| 3351 |
|
|
} |
| 3352 |
|
|
|
| 3353 |
|
|
/* Other escapes need one byte */ |
| 3354 |
|
|
|
| 3355 |
|
|
length++; |
| 3356 |
|
|
|
| 3357 |
|
|
/* A back reference needs an additional 2 bytes, plus either one or 5 |
| 3358 |
|
|
bytes for a repeat. We also need to keep the value of the highest |
| 3359 |
|
|
back reference. */ |
| 3360 |
|
|
|
| 3361 |
|
|
if (c <= -ESC_REF) |
| 3362 |
|
|
{ |
| 3363 |
|
|
int refnum = -c - ESC_REF; |
| 3364 |
|
|
compile_block.backref_map |= (refnum < 32)? (1 << refnum) : 1; |
| 3365 |
|
|
if (refnum > compile_block.top_backref) |
| 3366 |
|
|
compile_block.top_backref = refnum; |
| 3367 |
|
|
length += 2; /* For single back reference */ |
| 3368 |
|
|
if (ptr[1] == '{' && is_counted_repeat(ptr+2)) |
| 3369 |
|
|
{ |
| 3370 |
|
|
ptr = read_repeat_counts(ptr+2, &min, &max, &errorcode); |
| 3371 |
|
|
if (errorcode != 0) goto PCRE_ERROR_RETURN; |
| 3372 |
|
|
if ((min == 0 && (max == 1 || max == -1)) || |
| 3373 |
|
|
(min == 1 && max == -1)) |
| 3374 |
|
|
length++; |
| 3375 |
|
|
else length += 5; |
| 3376 |
|
|
if (ptr[1] == '?') ptr++; |
| 3377 |
|
|
} |
| 3378 |
|
|
} |
| 3379 |
|
|
continue; |
| 3380 |
|
|
|
| 3381 |
|
|
case '^': /* Single-byte metacharacters */ |
| 3382 |
|
|
case '.': |
| 3383 |
|
|
case '$': |
| 3384 |
|
|
length++; |
| 3385 |
|
|
lastitemlength = 1; |
| 3386 |
|
|
continue; |
| 3387 |
|
|
|
| 3388 |
|
|
case '*': /* These repeats won't be after brackets; */ |
| 3389 |
|
|
case '+': /* those are handled separately */ |
| 3390 |
|
|
case '?': |
| 3391 |
|
|
length++; |
| 3392 |
|
|
goto POSESSIVE; /* A few lines below */ |
| 3393 |
|
|
|
| 3394 |
|
|
/* This covers the cases of braced repeats after a single char, metachar, |
| 3395 |
|
|
class, or back reference. */ |
| 3396 |
|
|
|
| 3397 |
|
|
case '{': |
| 3398 |
|
|
if (!is_counted_repeat(ptr+1)) goto NORMAL_CHAR; |
| 3399 |
|
|
ptr = read_repeat_counts(ptr+1, &min, &max, &errorcode); |
| 3400 |
|
|
if (errorcode != 0) goto PCRE_ERROR_RETURN; |
| 3401 |
|
|
|
| 3402 |
|
|
/* These special cases just insert one extra opcode */ |
| 3403 |
|
|
|
| 3404 |
|
|
if ((min == 0 && (max == 1 || max == -1)) || |
| 3405 |
|
|
(min == 1 && max == -1)) |
| 3406 |
|
|
length++; |
| 3407 |
|
|
|
| 3408 |
|
|
/* These cases might insert additional copies of a preceding character. */ |
| 3409 |
|
|
|
| 3410 |
|
|
else |
| 3411 |
|
|
{ |
| 3412 |
|
|
if (min != 1) |
| 3413 |
|
|
{ |
| 3414 |
|
|
length -= lastitemlength; /* Uncount the original char or metachar */ |
| 3415 |
|
|
if (min > 0) length += 3 + lastitemlength; |
| 3416 |
|
|
} |
| 3417 |
|
|
length += lastitemlength + ((max > 0)? 3 : 1); |
| 3418 |
|
|
} |
| 3419 |
|
|
|
| 3420 |
|
|
if (ptr[1] == '?') ptr++; /* Needs no extra length */ |
| 3421 |
|
|
|
| 3422 |
|
|
POSESSIVE: /* Test for possessive quantifier */ |
| 3423 |
|
|
if (ptr[1] == '+') |
| 3424 |
|
|
{ |
| 3425 |
|
|
ptr++; |
| 3426 |
|
|
length += 2 + 2*LINK_SIZE; /* Allow for atomic brackets */ |
| 3427 |
|
|
} |
| 3428 |
|
|
continue; |
| 3429 |
|
|
|
| 3430 |
|
|
/* An alternation contains an offset to the next branch or ket. If any ims |
| 3431 |
|
|
options changed in the previous branch(es), and/or if we are in a |
| 3432 |
|
|
lookbehind assertion, extra space will be needed at the start of the |
| 3433 |
|
|
branch. This is handled by branch_extra. */ |
| 3434 |
|
|
|
| 3435 |
|
|
case '|': |
| 3436 |
|
|
length += 1 + LINK_SIZE + branch_extra; |
| 3437 |
|
|
continue; |
| 3438 |
|
|
|
| 3439 |
|
|
/* A character class uses 33 characters provided that all the character |
| 3440 |
|
|
values are less than 256. Otherwise, it uses a bit map for low valued |
| 3441 |
|
|
characters, and individual items for others. Don't worry about character |
| 3442 |
|
|
types that aren't allowed in classes - they'll get picked up during the |
| 3443 |
|
|
compile. A character class that contains only one single-byte character |
| 3444 |
|
|
uses 2 or 3 bytes, depending on whether it is negated or not. Notice this |
| 3445 |
|
|
where we can. (In UTF-8 mode we can do this only for chars < 128.) */ |
| 3446 |
|
|
|
| 3447 |
|
|
case '[': |
| 3448 |
|
|
if (*(++ptr) == '^') |
| 3449 |
|
|
{ |
| 3450 |
|
|
class_optcount = 10; /* Greater than one */ |
| 3451 |
|
|
ptr++; |
| 3452 |
|
|
} |
| 3453 |
|
|
else class_optcount = 0; |
| 3454 |
|
|
|
| 3455 |
|
|
/* Written as a "do" so that an initial ']' is taken as data */ |
| 3456 |
|
|
|
| 3457 |
|
|
if (*ptr != 0) do |
| 3458 |
|
|
{ |
| 3459 |
|
|
/* Inside \Q...\E everything is literal except \E */ |
| 3460 |
|
|
|
| 3461 |
|
|
if (inescq) |
| 3462 |
|
|
{ |
| 3463 |
|
|
if (*ptr != '\\' || ptr[1] != 'E') goto GET_ONE_CHARACTER; |
| 3464 |
|
|
inescq = FALSE; |
| 3465 |
|
|
ptr += 1; |
| 3466 |
|
|
continue; |
| 3467 |
|
|
} |
| 3468 |
|
|
|
| 3469 |
|
|
/* Outside \Q...\E, check for escapes */ |
| 3470 |
|
|
|
| 3471 |
|
|
if (*ptr == '\\') |
| 3472 |
|
|
{ |
| 3473 |
|
|
c = check_escape(&ptr, &errorcode, bracount, options, TRUE); |
| 3474 |
|
|
if (errorcode != 0) goto PCRE_ERROR_RETURN; |
| 3475 |
|
|
|
| 3476 |
|
|
/* \b is backspace inside a class; \X is literal */ |
| 3477 |
|
|
|
| 3478 |
|
|
if (-c == ESC_b) c = '\b'; |
| 3479 |
|
|
else if (-c == ESC_X) c = 'X'; |
| 3480 |
|
|
|
| 3481 |
|
|
/* \Q enters quoting mode */ |
| 3482 |
|
|
|
| 3483 |
|
|
else if (-c == ESC_Q) |
| 3484 |
|
|
{ |
| 3485 |
|
|
inescq = TRUE; |
| 3486 |
|
|
continue; |
| 3487 |
|
|
} |
| 3488 |
|
|
|
| 3489 |
|
|
/* Handle escapes that turn into characters */ |
| 3490 |
|
|
|
| 3491 |
|
|
if (c >= 0) goto NON_SPECIAL_CHARACTER; |
| 3492 |
|
|
|
| 3493 |
|
|
/* Escapes that are meta-things. The normal ones just affect the |
| 3494 |
|
|
bit map, but Unicode properties require an XCLASS extended item. */ |
| 3495 |
|
|
|
| 3496 |
|
|
else |
| 3497 |
|
|
{ |
| 3498 |
|
|
class_optcount = 10; /* \d, \s etc; make sure > 1 */ |
| 3499 |
|
|
} |
| 3500 |
|
|
} |
| 3501 |
|
|
|
| 3502 |
|
|
/* Check the syntax for POSIX stuff. The bits we actually handle are |
| 3503 |
|
|
checked during the real compile phase. */ |
| 3504 |
|
|
|
| 3505 |
|
|
else if (*ptr == '[' && check_posix_syntax(ptr, &ptr, &compile_block)) |
| 3506 |
|
|
{ |
| 3507 |
|
|
ptr++; |
| 3508 |
|
|
class_optcount = 10; /* Make sure > 1 */ |
| 3509 |
|
|
} |
| 3510 |
|
|
|
| 3511 |
|
|
/* Anything else increments the possible optimization count. We have to |
| 3512 |
|
|
detect ranges here so that we can compute the number of extra ranges for |
| 3513 |
|
|
caseless wide characters when UCP support is available. If there are wide |
| 3514 |
|
|
characters, we are going to have to use an XCLASS, even for single |
| 3515 |
|
|
characters. */ |
| 3516 |
|
|
|
| 3517 |
|
|
else |
| 3518 |
|
|
{ |
| 3519 |
|
|
int d; |
| 3520 |
|
|
|
| 3521 |
|
|
GET_ONE_CHARACTER: |
| 3522 |
|
|
c = *ptr; |
| 3523 |
|
|
|
| 3524 |
|
|
/* Come here from handling \ above when it escapes to a char value */ |
| 3525 |
|
|
|
| 3526 |
|
|
NON_SPECIAL_CHARACTER: |
| 3527 |
|
|
class_optcount++; |
| 3528 |
|
|
|
| 3529 |
|
|
d = -1; |
| 3530 |
|
|
if (ptr[1] == '-') |
| 3531 |
|
|
{ |
| 3532 |
|
|
uschar const *hyptr = ptr++; |
| 3533 |
|
|
if (ptr[1] == '\\') |
| 3534 |
|
|
{ |
| 3535 |
|
|
ptr++; |
| 3536 |
|
|
d = check_escape(&ptr, &errorcode, bracount, options, TRUE); |
| 3537 |
|
|
if (errorcode != 0) goto PCRE_ERROR_RETURN; |
| 3538 |
|
|
if (-d == ESC_b) d = '\b'; /* backspace */ |
| 3539 |
|
|
else if (-d == ESC_X) d = 'X'; /* literal X in a class */ |
| 3540 |
|
|
} |
| 3541 |
|
|
else if (ptr[1] != 0 && ptr[1] != ']') |
| 3542 |
|
|
{ |
| 3543 |
|
|
ptr++; |
| 3544 |
|
|
d = *ptr; |
| 3545 |
|
|
} |
| 3546 |
|
|
if (d < 0) ptr = hyptr; /* go back to hyphen as data */ |
| 3547 |
|
|
} |
| 3548 |
|
|
|
| 3549 |
|
|
/* If d >= 0 we have a range. In UTF-8 mode, if the end is > 255, or > |
| 3550 |
|
|
127 for caseless matching, we will need to use an XCLASS. */ |
| 3551 |
|
|
|
| 3552 |
|
|
if (d >= 0) |
| 3553 |
|
|
{ |
| 3554 |
|
|
class_optcount = 10; /* Ensure > 1 */ |
| 3555 |
|
|
if (d < c) |
| 3556 |
|
|
{ |
| 3557 |
|
|
errorcode = ERR8; |
| 3558 |
|
|
goto PCRE_ERROR_RETURN; |
| 3559 |
|
|
} |
| 3560 |
|
|
} |
| 3561 |
|
|
} |
| 3562 |
|
|
} |
| 3563 |
|
|
while (*(++ptr) != 0 && (inescq || *ptr != ']')); /* Concludes "do" above */ |
| 3564 |
|
|
|
| 3565 |
|
|
if (*ptr == 0) /* Missing terminating ']' */ |
| 3566 |
|
|
{ |
| 3567 |
|
|
errorcode = ERR6; |
| 3568 |
|
|
goto PCRE_ERROR_RETURN; |
| 3569 |
|
|
} |
| 3570 |
|
|
|
| 3571 |
|
|
/* We can optimize when there was only one optimizable character. Repeats |
| 3572 |
|
|
for positive and negated single one-byte chars are handled by the general |
| 3573 |
|
|
code. Here, we handle repeats for the class opcodes. */ |
| 3574 |
|
|
|
| 3575 |
|
|
if (class_optcount == 1) length += 3; else |
| 3576 |
|
|
{ |
| 3577 |
|
|
length += 33; |
| 3578 |
|
|
|
| 3579 |
|
|
/* A repeat needs either 1 or 5 bytes. If it is a possessive quantifier, |
| 3580 |
|
|
we also need extra for wrapping the whole thing in a sub-pattern. */ |
| 3581 |
|
|
|
| 3582 |
|
|
if (*ptr != 0 && ptr[1] == '{' && is_counted_repeat(ptr+2)) |
| 3583 |
|
|
{ |
| 3584 |
|
|
ptr = read_repeat_counts(ptr+2, &min, &max, &errorcode); |
| 3585 |
|
|
if (errorcode != 0) goto PCRE_ERROR_RETURN; |
| 3586 |
|
|
if ((min == 0 && (max == 1 || max == -1)) || |
| 3587 |
|
|
(min == 1 && max == -1)) |
| 3588 |
|
|
length++; |
| 3589 |
|
|
else length += 5; |
| 3590 |
|
|
if (ptr[1] == '+') |
| 3591 |
|
|
{ |
| 3592 |
|
|
ptr++; |
| 3593 |
|
|
length += 2 + 2*LINK_SIZE; |
| 3594 |
|
|
} |
| 3595 |
|
|
else if (ptr[1] == '?') ptr++; |
| 3596 |
|
|
} |
| 3597 |
|
|
} |
| 3598 |
|
|
continue; |
| 3599 |
|
|
|
| 3600 |
|
|
/* Brackets may be genuine groups or special things */ |
| 3601 |
|
|
|
| 3602 |
|
|
case '(': |
| 3603 |
|
|
branch_newextra = 0; |
| 3604 |
|
|
bracket_length = 1 + LINK_SIZE; |
| 3605 |
|
|
capturing = FALSE; |
| 3606 |
|
|
|
| 3607 |
|
|
/* Handle special forms of bracket, which all start (? */ |
| 3608 |
|
|
|
| 3609 |
|
|
if (ptr[1] == '?') |
| 3610 |
|
|
{ |
| 3611 |
|
|
int set, unset; |
| 3612 |
|
|
int *optset; |
| 3613 |
|
|
|
| 3614 |
|
|
switch (c = ptr[2]) |
| 3615 |
|
|
{ |
| 3616 |
|
|
/* Skip over comments entirely */ |
| 3617 |
|
|
case '#': |
| 3618 |
|
|
ptr += 3; |
| 3619 |
|
|
while (*ptr != 0 && *ptr != ')') ptr++; |
| 3620 |
|
|
if (*ptr == 0) |
| 3621 |
|
|
{ |
| 3622 |
|
|
errorcode = ERR18; |
| 3623 |
|
|
goto PCRE_ERROR_RETURN; |
| 3624 |
|
|
} |
| 3625 |
|
|
continue; |
| 3626 |
|
|
|
| 3627 |
|
|
/* Non-referencing groups and lookaheads just move the pointer on, and |
| 3628 |
|
|
then behave like a non-special bracket, except that they don't increment |
| 3629 |
|
|
the count of extracting brackets. Ditto for the "once only" bracket, |
| 3630 |
|
|
which is in Perl from version 5.005. */ |
| 3631 |
|
|
|
| 3632 |
|
|
case ':': |
| 3633 |
|
|
case '=': |
| 3634 |
|
|
case '!': |
| 3635 |
|
|
case '>': |
| 3636 |
|
|
ptr += 2; |
| 3637 |
|
|
break; |
| 3638 |
|
|
|
| 3639 |
|
|
/* (?R) specifies a recursive call to the regex, which is an extension |
| 3640 |
|
|
to provide the facility which can be obtained by (?p{perl-code}) in |
| 3641 |
|
|
Perl 5.6. In Perl 5.8 this has become (??{perl-code}). |
| 3642 |
|
|
|
| 3643 |
|
|
From PCRE 4.00, items such as (?3) specify subroutine-like "calls" to |
| 3644 |
|
|
the appropriate numbered brackets. This includes both recursive and |
| 3645 |
|
|
non-recursive calls. (?R) is now synonymous with (?0). */ |
| 3646 |
|
|
|
| 3647 |
|
|
case 'R': |
| 3648 |
|
|
ptr++; |
| 3649 |
|
|
|
| 3650 |
|
|
case '0': case '1': case '2': case '3': case '4': |
| 3651 |
|
|
case '5': case '6': case '7': case '8': case '9': |
| 3652 |
|
|
ptr += 2; |
| 3653 |
|
|
if (c != 'R') |
| 3654 |
|
|
while ((digitab[*(++ptr)] & ctype_digit) != 0); |
| 3655 |
|
|
if (*ptr != ')') |
| 3656 |
|
|
{ |
| 3657 |
|
|
errorcode = ERR29; |
| 3658 |
|
|
goto PCRE_ERROR_RETURN; |
| 3659 |
|
|
} |
| 3660 |
|
|
length += 1 + LINK_SIZE; |
| 3661 |
|
|
|
| 3662 |
|
|
/* If this item is quantified, it will get wrapped inside brackets so |
| 3663 |
|
|
as to use the code for quantified brackets. We jump down and use the |
| 3664 |
|
|
code that handles this for real brackets. */ |
| 3665 |
|
|
|
| 3666 |
|
|
if (ptr[1] == '+' || ptr[1] == '*' || ptr[1] == '?' || ptr[1] == '{') |
| 3667 |
|
|
{ |
| 3668 |
|
|
length += 2 + 2 * LINK_SIZE; /* to make bracketed */ |
| 3669 |
|
|
duplength = 5 + 3 * LINK_SIZE; |
| 3670 |
|
|
goto HANDLE_QUANTIFIED_BRACKETS; |
| 3671 |
|
|
} |
| 3672 |
|
|
continue; |
| 3673 |
|
|
|
| 3674 |
|
|
/* (?C) is an extension which provides "callout" - to provide a bit of |
| 3675 |
|
|
the functionality of the Perl (?{...}) feature. An optional number may |
| 3676 |
|
|
follow (default is zero). */ |
| 3677 |
|
|
|
| 3678 |
|
|
case 'C': |
| 3679 |
|
|
ptr += 2; |
| 3680 |
|
|
while ((digitab[*(++ptr)] & ctype_digit) != 0); |
| 3681 |
|
|
if (*ptr != ')') |
| 3682 |
|
|
{ |
| 3683 |
|
|
errorcode = ERR39; |
| 3684 |
|
|
goto PCRE_ERROR_RETURN; |
| 3685 |
|
|
} |
| 3686 |
|
|
length += 2 + 2*LINK_SIZE; |
| 3687 |
|
|
continue; |
| 3688 |
|
|
|
| 3689 |
|
|
/* Named subpatterns are an extension copied from Python */ |
| 3690 |
|
|
|
| 3691 |
|
|
case 'P': |
| 3692 |
|
|
ptr += 3; |
| 3693 |
|
|
|
| 3694 |
|
|
/* Handle the definition of a named subpattern */ |
| 3695 |
|
|
|
| 3696 |
|
|
if (*ptr == '<') |
| 3697 |
|
|
{ |
| 3698 |
|
|
const uschar *p; /* Don't amalgamate; some compilers */ |
| 3699 |
|
|
p = ++ptr; /* grumble at autoincrement in declaration */ |
| 3700 |
|
|
while ((compile_block.ctypes[*ptr] & ctype_word) != 0) ptr++; |
| 3701 |
|
|
if (*ptr != '>') |
| 3702 |
|
|
{ |
| 3703 |
|
|
errorcode = ERR42; |
| 3704 |
|
|
goto PCRE_ERROR_RETURN; |
| 3705 |
|
|
} |
| 3706 |
|
|
name_count++; |
| 3707 |
|
|
if (ptr - p > max_name_size) max_name_size = (ptr - p); |
| 3708 |
|
|
capturing = TRUE; /* Named parentheses are always capturing */ |
| 3709 |
|
|
break; |
| 3710 |
|
|
} |
| 3711 |
|
|
|
| 3712 |
|
|
/* Handle back references and recursive calls to named subpatterns */ |
| 3713 |
|
|
|
| 3714 |
|
|
if (*ptr == '=' || *ptr == '>') |
| 3715 |
|
|
{ |
| 3716 |
|
|
while ((compile_block.ctypes[*(++ptr)] & ctype_word) != 0); |
| 3717 |
|
|
if (*ptr != ')') |
| 3718 |
|
|
{ |
| 3719 |
|
|
errorcode = ERR42; |
| 3720 |
|
|
goto PCRE_ERROR_RETURN; |
| 3721 |
|
|
} |
| 3722 |
|
|
break; |
| 3723 |
|
|
} |
| 3724 |
|
|
|
| 3725 |
|
|
/* Unknown character after (?P */ |
| 3726 |
|
|
|
| 3727 |
|
|
errorcode = ERR41; |
| 3728 |
|
|
goto PCRE_ERROR_RETURN; |
| 3729 |
|
|
|
| 3730 |
|
|
/* Lookbehinds are in Perl from version 5.005 */ |
| 3731 |
|
|
|
| 3732 |
|
|
case '<': |
| 3733 |
|
|
ptr += 3; |
| 3734 |
|
|
if (*ptr == '=' || *ptr == '!') |
| 3735 |
|
|
{ |
| 3736 |
|
|
branch_newextra = 1 + LINK_SIZE; |
| 3737 |
|
|
length += 1 + LINK_SIZE; /* For the first branch */ |
| 3738 |
|
|
break; |
| 3739 |
|
|
} |
| 3740 |
|
|
errorcode = ERR24; |
| 3741 |
|
|
goto PCRE_ERROR_RETURN; |
| 3742 |
|
|
|
| 3743 |
|
|
/* Conditionals are in Perl from version 5.005. The bracket must either |
| 3744 |
|
|
be followed by a number (for bracket reference) or by an assertion |
| 3745 |
|
|
group, or (a PCRE extension) by 'R' for a recursion test. */ |
| 3746 |
|
|
|
| 3747 |
|
|
case '(': |
| 3748 |
|
|
if (ptr[3] == 'R' && ptr[4] == ')') |
| 3749 |
|
|
{ |
| 3750 |
|
|
ptr += 4; |
| 3751 |
|
|
length += 3; |
| 3752 |
|
|
} |
| 3753 |
|
|
else if ((digitab[ptr[3]] & ctype_digit) != 0) |
| 3754 |
|
|
{ |
| 3755 |
|
|
ptr += 4; |
| 3756 |
|
|
length += 3; |
| 3757 |
|
|
while ((digitab[*ptr] & ctype_digit) != 0) ptr++; |
| 3758 |
|
|
if (*ptr != ')') |
| 3759 |
|
|
{ |
| 3760 |
|
|
errorcode = ERR26; |
| 3761 |
|
|
goto PCRE_ERROR_RETURN; |
| 3762 |
|
|
} |
| 3763 |
|
|
} |
| 3764 |
|
|
else /* An assertion must follow */ |
| 3765 |
|
|
{ |
| 3766 |
|
|
ptr++; /* Can treat like ':' as far as spacing is concerned */ |
| 3767 |
|
|
if (ptr[2] != '?' || |
| 3768 |
|
|
(ptr[3] != '=' && ptr[3] != '!' && ptr[3] != '<') ) |
| 3769 |
|
|
{ |
| 3770 |
|
|
ptr += 2; /* To get right offset in message */ |
| 3771 |
|
|
errorcode = ERR28; |
| 3772 |
|
|
goto PCRE_ERROR_RETURN; |
| 3773 |
|
|
} |
| 3774 |
|
|
} |
| 3775 |
|
|
break; |
| 3776 |
|
|
|
| 3777 |
|
|
/* Else loop checking valid options until ) is met. Anything else is an |
| 3778 |
|
|
error. If we are without any brackets, i.e. at top level, the settings |
| 3779 |
|
|
act as if specified in the options, so massage the options immediately. |
| 3780 |
|
|
This is for backward compatibility with Perl 5.004. */ |
| 3781 |
|
|
|
| 3782 |
|
|
default: |
| 3783 |
|
|
set = unset = 0; |
| 3784 |
|
|
optset = &set; |
| 3785 |
|
|
ptr += 2; |
| 3786 |
|
|
|
| 3787 |
|
|
for (;; ptr++) |
| 3788 |
|
|
{ |
| 3789 |
|
|
c = *ptr; |
| 3790 |
|
|
switch (c) |
| 3791 |
|
|
{ |
| 3792 |
|
|
case 'i': |
| 3793 |
|
|
*optset |= PCRE_CASELESS; |
| 3794 |
|
|
continue; |
| 3795 |
|
|
|
| 3796 |
|
|
case 'm': |
| 3797 |
|
|
*optset |= PCRE_MULTILINE; |
| 3798 |
|
|
continue; |
| 3799 |
|
|
|
| 3800 |
|
|
case 's': |
| 3801 |
|
|
*optset |= PCRE_DOTALL; |
| 3802 |
|
|
continue; |
| 3803 |
|
|
|
| 3804 |
|
|
case 'x': |
| 3805 |
|
|
*optset |= PCRE_EXTENDED; |
| 3806 |
|
|
continue; |
| 3807 |
|
|
|
| 3808 |
|
|
case 'X': |
| 3809 |
|
|
*optset |= PCRE_EXTRA; |
| 3810 |
|
|
continue; |
| 3811 |
|
|
|
| 3812 |
|
|
case 'U': |
| 3813 |
|
|
*optset |= PCRE_UNGREEDY; |
| 3814 |
|
|
continue; |
| 3815 |
|
|
|
| 3816 |
|
|
case '-': |
| 3817 |
|
|
optset = &unset; |
| 3818 |
|
|
continue; |
| 3819 |
|
|
|
| 3820 |
|
|
/* A termination by ')' indicates an options-setting-only item; if |
| 3821 |
|
|
this is at the very start of the pattern (indicated by item_count |
| 3822 |
|
|
being zero), we use it to set the global options. This is helpful |
| 3823 |
|
|
when analyzing the pattern for first characters, etc. Otherwise |
| 3824 |
|
|
nothing is done here and it is handled during the compiling |
| 3825 |
|
|
process. |
| 3826 |
|
|
|
| 3827 |
|
|
We allow for more than one options setting at the start. If such |
| 3828 |
|
|
settings do not change the existing options, nothing is compiled. |
| 3829 |
|
|
However, we must leave space just in case something is compiled. |
| 3830 |
|
|
This can happen for pathological sequences such as (?i)(?-i) |
| 3831 |
|
|
because the global options will end up with -i set. The space is |
| 3832 |
|
|
small and not significant. (Before I did this there was a reported |
| 3833 |
|
|
bug with (?i)(?-i) in a machine-generated pattern.) |
| 3834 |
|
|
|
| 3835 |
|
|
[Historical note: Up to Perl 5.8, options settings at top level |
| 3836 |
|
|
were always global settings, wherever they appeared in the pattern. |
| 3837 |
|
|
That is, they were equivalent to an external setting. From 5.8 |
| 3838 |
|
|
onwards, they apply only to what follows (which is what you might |
| 3839 |
|
|
expect).] */ |
| 3840 |
|
|
|
| 3841 |
|
|
case ')': |
| 3842 |
|
|
if (item_count == 0) |
| 3843 |
|
|
{ |
| 3844 |
|
|
options = (options | set) & (~unset); |
| 3845 |
|
|
set = unset = 0; /* To save length */ |
| 3846 |
|
|
item_count--; /* To allow for several */ |
| 3847 |
|
|
length += 2; |
| 3848 |
|
|
} |
| 3849 |
|
|
|
| 3850 |
|
|
/* Fall through */ |
| 3851 |
|
|
|
| 3852 |
|
|
/* A termination by ':' indicates the start of a nested group with |
| 3853 |
|
|
the given options set. This is again handled at compile time, but |
| 3854 |
|
|
we must allow for compiled space if any of the ims options are |
| 3855 |
|
|
set. We also have to allow for resetting space at the end of |
| 3856 |
|
|
the group, which is why 4 is added to the length and not just 2. |
| 3857 |
|
|
If there are several changes of options within the same group, this |
| 3858 |
|
|
will lead to an over-estimate on the length, but this shouldn't |
| 3859 |
|
|
matter very much. We also have to allow for resetting options at |
| 3860 |
|
|
the start of any alternations, which we do by setting |
| 3861 |
|
|
branch_newextra to 2. Finally, we record whether the case-dependent |
| 3862 |
|
|
flag ever changes within the regex. This is used by the "required |
| 3863 |
|
|
character" code. */ |
| 3864 |
|
|
|
| 3865 |
|
|
case ':': |
| 3866 |
|
|
if (((set|unset) & PCRE_IMS) != 0) |
| 3867 |
|
|
{ |
| 3868 |
|
|
length += 4; |
| 3869 |
|
|
branch_newextra = 2; |
| 3870 |
|
|
if (((set|unset) & PCRE_CASELESS) != 0) options |= PCRE_ICHANGED; |
| 3871 |
|
|
} |
| 3872 |
|
|
goto END_OPTIONS; |
| 3873 |
|
|
|
| 3874 |
|
|
/* Unrecognized option character */ |
| 3875 |
|
|
|
| 3876 |
|
|
default: |
| 3877 |
|
|
errorcode = ERR12; |
| 3878 |
|
|
goto PCRE_ERROR_RETURN; |
| 3879 |
|
|
} |
| 3880 |
|
|
} |
| 3881 |
|
|
|
| 3882 |
|
|
/* If we hit a closing bracket, that's it - this is a freestanding |
| 3883 |
|
|
option-setting. We need to ensure that branch_extra is updated if |
| 3884 |
|
|
necessary. The only values branch_newextra can have here are 0 or 2. |
| 3885 |
|
|
If the value is 2, then branch_extra must either be 2 or 5, depending |
| 3886 |
|
|
on whether this is a lookbehind group or not. */ |
| 3887 |
|
|
|
| 3888 |
|
|
END_OPTIONS: |
| 3889 |
|
|
if (c == ')') |
| 3890 |
|
|
{ |
| 3891 |
|
|
if (branch_newextra == 2 && |
| 3892 |
|
|
(branch_extra == 0 || branch_extra == 1+LINK_SIZE)) |
| 3893 |
|
|
branch_extra += branch_newextra; |
| 3894 |
|
|
continue; |
| 3895 |
|
|
} |
| 3896 |
|
|
|
| 3897 |
|
|
/* If options were terminated by ':' control comes here. This is a |
| 3898 |
|
|
non-capturing group with an options change. There is nothing more that |
| 3899 |
|
|
needs to be done because "capturing" is already set FALSE by default; |
| 3900 |
|
|
we can just fall through. */ |
| 3901 |
|
|
|
| 3902 |
|
|
} |
| 3903 |
|
|
} |
| 3904 |
|
|
|
| 3905 |
|
|
/* Ordinary parentheses, not followed by '?', are capturing unless |
| 3906 |
|
|
PCRE_NO_AUTO_CAPTURE is set. */ |
| 3907 |
|
|
|
| 3908 |
|
|
else capturing = (options & PCRE_NO_AUTO_CAPTURE) == 0; |
| 3909 |
|
|
|
| 3910 |
|
|
/* Capturing brackets must be counted so we can process escapes in a |
| 3911 |
|
|
Perlish way. If the number exceeds EXTRACT_BASIC_MAX we are going to need |
| 3912 |
|
|
an additional 3 bytes of memory per capturing bracket. */ |
| 3913 |
|
|
|
| 3914 |
|
|
if (capturing) |
| 3915 |
|
|
{ |
| 3916 |
|
|
bracount++; |
| 3917 |
|
|
if (bracount > EXTRACT_BASIC_MAX) bracket_length += 3; |
| 3918 |
|
|
} |
| 3919 |
|
|
|
| 3920 |
|
|
/* Save length for computing whole length at end if there's a repeat that |
| 3921 |
|
|
requires duplication of the group. Also save the current value of |
| 3922 |
|
|
branch_extra, and start the new group with the new value. If non-zero, this |
| 3923 |
|
|
will either be 2 for a (?imsx: group, or 3 for a lookbehind assertion. */ |
| 3924 |
|
|
|
| 3925 |
|
|
if (brastackptr >= sizeof(brastack)/sizeof(int)) |
| 3926 |
|
|
{ |
| 3927 |
|
|
errorcode = ERR19; |
| 3928 |
|
|
goto PCRE_ERROR_RETURN; |
| 3929 |
|
|
} |
| 3930 |
|
|
|
| 3931 |
|
|
bralenstack[brastackptr] = branch_extra; |
| 3932 |
|
|
branch_extra = branch_newextra; |
| 3933 |
|
|
|
| 3934 |
|
|
brastack[brastackptr++] = length; |
| 3935 |
|
|
length += bracket_length; |
| 3936 |
|
|
continue; |
| 3937 |
|
|
|
| 3938 |
|
|
/* Handle ket. Look for subsequent max/min; for certain sets of values we |
| 3939 |
|
|
have to replicate this bracket up to that many times. If brastackptr is |
| 3940 |
|
|
0 this is an unmatched bracket which will generate an error, but take care |
| 3941 |
|
|
not to try to access brastack[-1] when computing the length and restoring |
| 3942 |
|
|
the branch_extra value. */ |
| 3943 |
|
|
|
| 3944 |
|
|
case ')': |
| 3945 |
|
|
length += 1 + LINK_SIZE; |
| 3946 |
|
|
if (brastackptr > 0) |
| 3947 |
|
|
{ |
| 3948 |
|
|
duplength = length - brastack[--brastackptr]; |
| 3949 |
|
|
branch_extra = bralenstack[brastackptr]; |
| 3950 |
|
|
} |
| 3951 |
|
|
else duplength = 0; |
| 3952 |
|
|
|
| 3953 |
|
|
/* The following code is also used when a recursion such as (?3) is |
| 3954 |
|
|
followed by a quantifier, because in that case, it has to be wrapped inside |
| 3955 |
|
|
brackets so that the quantifier works. The value of duplength must be |
| 3956 |
|
|
set before arrival. */ |
| 3957 |
|
|
|
| 3958 |
|
|
HANDLE_QUANTIFIED_BRACKETS: |
| 3959 |
|
|
|
| 3960 |
|
|
/* Leave ptr at the final char; for read_repeat_counts this happens |
| 3961 |
|
|
automatically; for the others we need an increment. */ |
| 3962 |
|
|
|
| 3963 |
|
|
if ((c = ptr[1]) == '{' && is_counted_repeat(ptr+2)) |
| 3964 |
|
|
{ |
| 3965 |
|
|
ptr = read_repeat_counts(ptr+2, &min, &max, &errorcode); |
| 3966 |
|
|
if (errorcode != 0) goto PCRE_ERROR_RETURN; |
| 3967 |
|
|
} |
| 3968 |
|
|
else if (c == '*') { min = 0; max = -1; ptr++; } |
| 3969 |
|
|
else if (c == '+') { min = 1; max = -1; ptr++; } |
| 3970 |
|
|
else if (c == '?') { min = 0; max = 1; ptr++; } |
| 3971 |
|
|
else { min = 1; max = 1; } |
| 3972 |
|
|
|
| 3973 |
|
|
/* If the minimum is zero, we have to allow for an OP_BRAZERO before the |
| 3974 |
|
|
group, and if the maximum is greater than zero, we have to replicate |
| 3975 |
|
|
maxval-1 times; each replication acquires an OP_BRAZERO plus a nesting |
| 3976 |
|
|
bracket set. */ |
| 3977 |
|
|
|
| 3978 |
|
|
if (min == 0) |
| 3979 |
|
|
{ |
| 3980 |
|
|
length++; |
| 3981 |
|
|
if (max > 0) length += (max - 1) * (duplength + 3 + 2*LINK_SIZE); |
| 3982 |
|
|
} |
| 3983 |
|
|
|
| 3984 |
|
|
/* When the minimum is greater than zero, we have to replicate up to |
| 3985 |
|
|
minval-1 times, with no additions required in the copies. Then, if there |
| 3986 |
|
|
is a limited maximum we have to replicate up to maxval-1 times allowing |
| 3987 |
|
|
for a BRAZERO item before each optional copy and nesting brackets for all |
| 3988 |
|
|
but one of the optional copies. */ |
| 3989 |
|
|
|
| 3990 |
|
|
else |
| 3991 |
|
|
{ |
| 3992 |
|
|
length += (min - 1) * duplength; |
| 3993 |
|
|
if (max > min) /* Need this test as max=-1 means no limit */ |
| 3994 |
|
|
length += (max - min) * (duplength + 3 + 2*LINK_SIZE) |
| 3995 |
|
|
- (2 + 2*LINK_SIZE); |
| 3996 |
|
|
} |
| 3997 |
|
|
|
| 3998 |
|
|
/* Allow space for once brackets for "possessive quantifier" */ |
| 3999 |
|
|
|
| 4000 |
|
|
if (ptr[1] == '+') |
| 4001 |
|
|
{ |
| 4002 |
|
|
ptr++; |
| 4003 |
|
|
length += 2 + 2*LINK_SIZE; |
| 4004 |
|
|
} |
| 4005 |
|
|
continue; |
| 4006 |
|
|
|
| 4007 |
|
|
/* Non-special character. It won't be space or # in extended mode, so it is |
| 4008 |
|
|
always a genuine character. If we are in a \Q...\E sequence, check for the |
| 4009 |
|
|
end; if not, we have a literal. */ |
| 4010 |
|
|
|
| 4011 |
|
|
default: |
| 4012 |
|
|
NORMAL_CHAR: |
| 4013 |
|
|
|
| 4014 |
|
|
if (inescq && c == '\\' && ptr[1] == 'E') |
| 4015 |
|
|
{ |
| 4016 |
|
|
inescq = FALSE; |
| 4017 |
|
|
ptr++; |
| 4018 |
|
|
continue; |
| 4019 |
|
|
} |
| 4020 |
|
|
|
| 4021 |
|
|
length += 2; /* For a one-byte character */ |
| 4022 |
|
|
lastitemlength = 1; /* Default length of last item for repeats */ |
| 4023 |
|
|
|
| 4024 |
|
|
continue; |
| 4025 |
|
|
} |
| 4026 |
|
|
} |
| 4027 |
|
|
|
| 4028 |
|
|
length += 2 + LINK_SIZE; /* For final KET and END */ |
| 4029 |
|
|
|
| 4030 |
|
|
if ((options & PCRE_AUTO_CALLOUT) != 0) |
| 4031 |
|
|
length += 2 + 2*LINK_SIZE; /* For final callout */ |
| 4032 |
|
|
|
| 4033 |
|
|
if (length > MAX_PATTERN_SIZE) |
| 4034 |
|
|
{ |
| 4035 |
|
|
errorcode = ERR20; |
| 4036 |
|
|
goto PCRE_EARLY_ERROR_RETURN; |
| 4037 |
|
|
} |
| 4038 |
|
|
|
| 4039 |
|
|
/* Compute the size of data block needed and get it, either from malloc or |
| 4040 |
|
|
externally provided function. */ |
| 4041 |
|
|
|
| 4042 |
|
|
size = length + sizeof(real_pcre) + name_count * (max_name_size + 3); |
| 4043 |
|
|
re = (real_pcre *)(pcre_malloc)(size); |
| 4044 |
|
|
|
| 4045 |
|
|
if (re == NULL) |
| 4046 |
|
|
{ |
| 4047 |
|
|
errorcode = ERR21; |
| 4048 |
|
|
goto PCRE_EARLY_ERROR_RETURN; |
| 4049 |
|
|
} |
| 4050 |
|
|
|
| 4051 |
|
|
/* Put in the magic number, and save the sizes, options, and character table |
| 4052 |
|
|
pointer. NULL is used for the default character tables. The nullpad field is at |
| 4053 |
|
|
the end; it's there to help in the case when a regex compiled on a system with |
| 4054 |
|
|
4-byte pointers is run on another with 8-byte pointers. */ |
| 4055 |
|
|
|
| 4056 |
|
|
re->magic_number = MAGIC_NUMBER; |
| 4057 |
|
|
re->size = size; |
| 4058 |
|
|
re->options = options; |
| 4059 |
|
|
re->dummy1 = 0; |
| 4060 |
|
|
re->name_table_offset = sizeof(real_pcre); |
| 4061 |
|
|
re->name_entry_size = max_name_size + 3; |
| 4062 |
|
|
re->name_count = name_count; |
| 4063 |
|
|
re->ref_count = 0; |
| 4064 |
|
|
re->tables = (tables == _pcre_default_tables)? NULL : tables; |
| 4065 |
|
|
re->nullpad = NULL; |
| 4066 |
|
|
|
| 4067 |
|
|
/* The starting points of the name/number translation table and of the code are |
| 4068 |
|
|
passed around in the compile data block. */ |
| 4069 |
|
|
|
| 4070 |
|
|
compile_block.names_found = 0; |
| 4071 |
|
|
compile_block.name_entry_size = max_name_size + 3; |
| 4072 |
|
|
compile_block.name_table = (uschar *)re + re->name_table_offset; |
| 4073 |
|
|
codestart = compile_block.name_table + re->name_entry_size * re->name_count; |
| 4074 |
|
|
compile_block.start_code = codestart; |
| 4075 |
|
|
compile_block.start_pattern = (const uschar *)pattern; |
| 4076 |
|
|
compile_block.req_varyopt = 0; |
| 4077 |
|
|
compile_block.nopartial = FALSE; |
| 4078 |
|
|
|
| 4079 |
|
|
/* Set up a starting, non-extracting bracket, then compile the expression. On |
| 4080 |
|
|
error, errorcode will be set non-zero, so we don't need to look at the result |
| 4081 |
|
|
of the function here. */ |
| 4082 |
|
|
|
| 4083 |
|
|
ptr = (const uschar *)pattern; |
| 4084 |
|
|
code = (uschar *)codestart; |
| 4085 |
|
|
*code = OP_BRA; |
| 4086 |
|
|
bracount = 0; |
| 4087 |
|
|
(void)compile_regex(options, options & PCRE_IMS, &bracount, &code, &ptr, |
| 4088 |
|
|
&errorcode, FALSE, 0, &firstbyte, &reqbyte, NULL, &compile_block); |
| 4089 |
|
|
re->top_bracket = bracount; |
| 4090 |
|
|
re->top_backref = compile_block.top_backref; |
| 4091 |
|
|
|
| 4092 |
|
|
if (compile_block.nopartial) re->options |= PCRE_NOPARTIAL; |
| 4093 |
|
|
|
| 4094 |
|
|
/* If not reached end of pattern on success, there's an excess bracket. */ |
| 4095 |
|
|
|
| 4096 |
|
|
if (errorcode == 0 && *ptr != 0) errorcode = ERR22; |
| 4097 |
|
|
|
| 4098 |
|
|
/* Fill in the terminating state and check for disastrous overflow, but |
| 4099 |
|
|
if debugging, leave the test till after things are printed out. */ |
| 4100 |
|
|
|
| 4101 |
|
|
*code++ = OP_END; |
| 4102 |
|
|
|
| 4103 |
|
|
#ifndef DEBUG |
| 4104 |
|
|
if (code - codestart > length) errorcode = ERR23; |
| 4105 |
|
|
#endif |
| 4106 |
|
|
|
| 4107 |
|
|
/* Give an error if there's back reference to a non-existent capturing |
| 4108 |
|
|
subpattern. */ |
| 4109 |
|
|
|
| 4110 |
|
|
if (re->top_backref > re->top_bracket) errorcode = ERR15; |
| 4111 |
|
|
|
| 4112 |
|
|
/* Failed to compile, or error while post-processing */ |
| 4113 |
|
|
|
| 4114 |
|
|
if (errorcode != 0) |
| 4115 |
|
|
{ |
| 4116 |
|
|
(pcre_free)(re); |
| 4117 |
|
|
PCRE_ERROR_RETURN: |
| 4118 |
|
|
*erroroffset = ptr - (const uschar *)pattern; |
| 4119 |
|
|
PCRE_EARLY_ERROR_RETURN: |
| 4120 |
|
|
*errorptr = error_texts[errorcode]; |
| 4121 |
|
|
if (errorcodeptr != NULL) *errorcodeptr = errorcode; |
| 4122 |
|
|
return NULL; |
| 4123 |
|
|
} |
| 4124 |
|
|
|
| 4125 |
|
|
/* If the anchored option was not passed, set the flag if we can determine that |
| 4126 |
|
|
the pattern is anchored by virtue of ^ characters or \A or anything else (such |
| 4127 |
|
|
as starting with .* when DOTALL is set). |
| 4128 |
|
|
|
| 4129 |
|
|
Otherwise, if we know what the first character has to be, save it, because that |
| 4130 |
|
|
speeds up unanchored matches no end. If not, see if we can set the |
| 4131 |
|
|
PCRE_STARTLINE flag. This is helpful for multiline matches when all branches |
| 4132 |
|
|
start with ^. and also when all branches start with .* for non-DOTALL matches. |
| 4133 |
|
|
*/ |
| 4134 |
|
|
|
| 4135 |
|
|
if ((options & PCRE_ANCHORED) == 0) |
| 4136 |
|
|
{ |
| 4137 |
|
|
int temp_options = options; |
| 4138 |
|
|
if (is_anchored(codestart, &temp_options, 0, compile_block.backref_map)) |
| 4139 |
|
|
re->options |= PCRE_ANCHORED; |
| 4140 |
|
|
else |
| 4141 |
|
|
{ |
| 4142 |
|
|
if (firstbyte < 0) |
| 4143 |
|
|
firstbyte = find_firstassertedchar(codestart, &temp_options, FALSE); |
| 4144 |
|
|
if (firstbyte >= 0) /* Remove caseless flag for non-caseable chars */ |
| 4145 |
|
|
{ |
| 4146 |
|
|
int ch = firstbyte & 255; |
| 4147 |
|
|
re->first_byte = ((firstbyte & REQ_CASELESS) != 0 && |
| 4148 |
|
|
compile_block.fcc[ch] == ch)? ch : firstbyte; |
| 4149 |
|
|
re->options |= PCRE_FIRSTSET; |
| 4150 |
|
|
} |
| 4151 |
|
|
else if (is_startline(codestart, 0, compile_block.backref_map)) |
| 4152 |
|
|
re->options |= PCRE_STARTLINE; |
| 4153 |
|
|
} |
| 4154 |
|
|
} |
| 4155 |
|
|
|
| 4156 |
|
|
/* For an anchored pattern, we use the "required byte" only if it follows a |
| 4157 |
|
|
variable length item in the regex. Remove the caseless flag for non-caseable |
| 4158 |
|
|
bytes. */ |
| 4159 |
|
|
|
| 4160 |
|
|
if (reqbyte >= 0 && |
| 4161 |
|
|
((re->options & PCRE_ANCHORED) == 0 || (reqbyte & REQ_VARY) != 0)) |
| 4162 |
|
|
{ |
| 4163 |
|
|
int ch = reqbyte & 255; |
| 4164 |
|
|
re->req_byte = ((reqbyte & REQ_CASELESS) != 0 && |
| 4165 |
|
|
compile_block.fcc[ch] == ch)? (reqbyte & ~REQ_CASELESS) : reqbyte; |
| 4166 |
|
|
re->options |= PCRE_REQCHSET; |
| 4167 |
|
|
} |
| 4168 |
|
|
|
| 4169 |
|
|
return (pcre *)re; |
| 4170 |
|
|
} |
| 4171 |
|
|
|
| 4172 |
|
|
/* End of pcre_compile.c */ |