Parent Directory
|
Revision Log
|
Patch
revision 7 by nigel, Sat Feb 24 21:38:09 2007 UTC | revision 31 by nigel, Sat Feb 24 21:38:57 2007 UTC | |
---|---|---|
# | Line 9 the file Tech.Notes for some information | Line 9 the file Tech.Notes for some information |
9 | ||
10 | Written by: Philip Hazel <ph10@cam.ac.uk> | Written by: Philip Hazel <ph10@cam.ac.uk> |
11 | ||
12 | Copyright (c) 1997 University of Cambridge | Copyright (c) 1997-1999 University of Cambridge |
13 | ||
14 | ----------------------------------------------------------------------------- | ----------------------------------------------------------------------------- |
15 | Permission is granted to anyone to use this software for any purpose on any | Permission is granted to anyone to use this software for any purpose on any |
# | Line 25 restrictions: | Line 25 restrictions: |
25 | ||
26 | 3. Altered versions must be plainly marked as such, and must not be | 3. Altered versions must be plainly marked as such, and must not be |
27 | misrepresented as being the original software. | misrepresented as being the original software. |
28 | ||
29 | 4. If PCRE is embedded in any software that is released under the GNU | |
30 | General Purpose Licence (GPL), then the terms of that licence shall | |
31 | supersede any condition above with which it is incompatible. | |
32 | ----------------------------------------------------------------------------- | ----------------------------------------------------------------------------- |
33 | */ | */ |
34 | ||
# | Line 33 restrictions: | Line 37 restrictions: |
37 | ||
38 | /* #define DEBUG */ | /* #define DEBUG */ |
39 | ||
40 | /* Use a macro for debugging printing, 'cause that eliminates the use of #ifdef | |
41 | inline, and there are *still* stupid compilers about that don't like indented | |
42 | pre-processor statements. I suppose it's only been 10 years... */ | |
43 | ||
44 | #ifdef DEBUG | |
45 | #define DPRINTF(p) printf p | |
46 | #else | |
47 | #define DPRINTF(p) /*nothing*/ | |
48 | #endif | |
49 | ||
50 | /* Include the internals header, which itself includes Standard C headers plus | /* Include the internals header, which itself includes Standard C headers plus |
51 | the external pcre header. */ | the external pcre header. */ |
# | Line 40 the external pcre header. */ | Line 53 the external pcre header. */ |
53 | #include "internal.h" | #include "internal.h" |
54 | ||
55 | ||
56 | /* Allow compilation as C++ source code, should anybody want to do that. */ | |
57 | ||
58 | #ifdef __cplusplus | |
59 | #define class pcre_class | |
60 | #endif | |
61 | ||
62 | ||
63 | /* Number of items on the nested bracket stacks at compile time. This should | |
64 | not be set greater than 200. */ | |
65 | ||
66 | #define BRASTACK_SIZE 200 | |
67 | ||
68 | ||
69 | /* Min and max values for the common repeats; for the maxima, 0 => infinity */ | /* Min and max values for the common repeats; for the maxima, 0 => infinity */ |
70 | ||
71 | static char rep_min[] = { 0, 0, 1, 1, 0, 0 }; | static const char rep_min[] = { 0, 0, 1, 1, 0, 0 }; |
72 | static char rep_max[] = { 0, 0, 0, 0, 1, 1 }; | static const char rep_max[] = { 0, 0, 0, 0, 1, 1 }; |
73 | ||
74 | /* Text forms of OP_ values and things, for debugging */ | /* Text forms of OP_ values and things, for debugging (not all used) */ |
75 | ||
76 | #ifdef DEBUG | #ifdef DEBUG |
77 | static const char *OP_names[] = { | static const char *OP_names[] = { |
78 | "End", "\\A", "\\B", "\\b", "\\D", "\\d", | "End", "\\A", "\\B", "\\b", "\\D", "\\d", |
79 | "\\S", "\\s", "\\W", "\\w", "Cut", "\\Z", "^", "$", "Any", "chars", | "\\S", "\\s", "\\W", "\\w", "\\Z", "\\z", |
80 | "not", | "Opt", "^", "$", "Any", "chars", "not", |
81 | "*", "*?", "+", "+?", "?", "??", "{", "{", "{", | "*", "*?", "+", "+?", "?", "??", "{", "{", "{", |
82 | "*", "*?", "+", "+?", "?", "??", "{", "{", "{", | "*", "*?", "+", "+?", "?", "??", "{", "{", "{", |
83 | "*", "*?", "+", "+?", "?", "??", "{", "{", "{", | "*", "*?", "+", "+?", "?", "??", "{", "{", "{", |
84 | "*", "*?", "+", "+?", "?", "??", "{", "{", | "*", "*?", "+", "+?", "?", "??", "{", "{", |
85 | "class", "Ref", | "class", "Ref", |
86 | "Alt", "Ket", "KetRmax", "KetRmin", "Assert", "Assert not", "Once", | "Alt", "Ket", "KetRmax", "KetRmin", "Assert", "Assert not", |
87 | "AssertB", "AssertB not", "Reverse", "Once", "Cond", "Cref", | |
88 | "Brazero", "Braminzero", "Bra" | "Brazero", "Braminzero", "Bra" |
89 | }; | }; |
90 | #endif | #endif |
# | Line 67 are simple data values; negative values | Line 94 are simple data values; negative values |
94 | on. Zero means further processing is needed (for things like \x), or the escape | on. Zero means further processing is needed (for things like \x), or the escape |
95 | is invalid. */ | is invalid. */ |
96 | ||
97 | static short int escapes[] = { | static const short int escapes[] = { |
98 | 0, 0, 0, 0, 0, 0, 0, 0, /* 0 - 7 */ | 0, 0, 0, 0, 0, 0, 0, 0, /* 0 - 7 */ |
99 | 0, 0, ':', ';', '<', '=', '>', '?', /* 8 - ? */ | 0, 0, ':', ';', '<', '=', '>', '?', /* 8 - ? */ |
100 | '@', -ESC_A, -ESC_B, 0, -ESC_D, 0, 0, 0, /* @ - G */ | '@', -ESC_A, -ESC_B, 0, -ESC_D, 0, 0, 0, /* @ - G */ |
# | Line 77 static short int escapes[] = { | Line 104 static short int escapes[] = { |
104 | '`', 7, -ESC_b, 0, -ESC_d, 27, '\f', 0, /* ` - g */ | '`', 7, -ESC_b, 0, -ESC_d, 27, '\f', 0, /* ` - g */ |
105 | 0, 0, 0, 0, 0, 0, '\n', 0, /* h - o */ | 0, 0, 0, 0, 0, 0, '\n', 0, /* h - o */ |
106 | 0, 0, '\r', -ESC_s, '\t', 0, 0, -ESC_w, /* p - w */ | 0, 0, '\r', -ESC_s, '\t', 0, 0, -ESC_w, /* p - w */ |
107 | 0, 0, 0 /* x - z */ | 0, 0, -ESC_z /* x - z */ |
108 | }; | }; |
109 | ||
110 | /* Definition to allow mutual recursion */ | /* Definition to allow mutual recursion */ |
111 | ||
112 | static BOOL compile_regex(int, int *, uschar **, const uschar **, const char **); | static BOOL |
113 | compile_regex(int, int, int *, uschar **, const uschar **, const char **, | |
114 | /* Structure for passing "static" information around between the functions | BOOL, int, compile_data *); |
doing the matching, so that they are thread-safe. */ | ||
typedef struct match_data { | ||
int errorcode; /* As it says */ | ||
int *offset_vector; /* Offset vector */ | ||
int offset_end; /* One past the end */ | ||
BOOL offset_overflow; /* Set if too many extractions */ | ||
BOOL caseless; /* Case-independent flag */ | ||
BOOL runtime_caseless; /* Caseless forced at run time */ | ||
BOOL multiline; /* Multiline flag */ | ||
BOOL notbol; /* NOTBOL flag */ | ||
BOOL noteol; /* NOTEOL flag */ | ||
BOOL dotall; /* Dot matches any char */ | ||
BOOL endonly; /* Dollar not before final \n */ | ||
const uschar *start_subject; /* Start of the subject string */ | ||
const uschar *end_subject; /* End of the subject string */ | ||
jmp_buf fail_env; /* Environment for longjump() break out */ | ||
const uschar *end_match_ptr; /* Subject position at end match */ | ||
int end_offset_top; /* Highwater mark at end of match */ | ||
} match_data; | ||
115 | ||
116 | ||
117 | ||
# | Line 124 void (*pcre_free)(void *) = free; | Line 131 void (*pcre_free)(void *) = free; |
131 | ||
132 | ||
133 | /************************************************* | /************************************************* |
134 | * Default character tables * | |
135 | *************************************************/ | |
136 | ||
137 | /* A default set of character tables is included in the PCRE binary. Its source | |
138 | is built by the maketables auxiliary program, which uses the default C ctypes | |
139 | functions, and put in the file chartables.c. These tables are used by PCRE | |
140 | whenever the caller of pcre_compile() does not provide an alternate set of | |
141 | tables. */ | |
142 | ||
143 | #include "chartables.c" | |
144 | ||
145 | ||
146 | ||
147 | /************************************************* | |
148 | * Return version string * | * Return version string * |
149 | *************************************************/ | *************************************************/ |
150 | ||
# | Line 187 Arguments: | Line 208 Arguments: |
208 | Returns: nothing | Returns: nothing |
209 | */ | */ |
210 | ||
211 | static pchars(uschar *p, int length, BOOL is_subject, match_data *md) | static void |
212 | pchars(const uschar *p, int length, BOOL is_subject, match_data *md) | |
213 | { | { |
214 | int c; | int c; |
215 | if (is_subject && length > md->end_subject - p) length = md->end_subject - p; | if (is_subject && length > md->end_subject - p) length = md->end_subject - p; |
# | Line 200 while (length-- > 0) | Line 222 while (length-- > 0) |
222 | ||
223 | ||
224 | /************************************************* | /************************************************* |
* Check subpattern for empty operand * | ||
*************************************************/ | ||
/* This function checks a bracketed subpattern to see if any of the paths | ||
through it could match an empty string. This is used to diagnose an error if | ||
such a subpattern is followed by a quantifier with an unlimited upper bound. | ||
Argument: | ||
code points to the opening bracket | ||
Returns: TRUE or FALSE | ||
*/ | ||
static BOOL | ||
could_be_empty(uschar *code) | ||
{ | ||
do { | ||
uschar *cc = code + 3; | ||
/* Scan along the opcodes for this branch; as soon as we find something | ||
that matches a non-empty string, break out and advance to test the next | ||
branch. If we get to the end of the branch, return TRUE for the whole | ||
sub-expression. */ | ||
for (;;) | ||
{ | ||
/* Test an embedded subpattern; if it could not be empty, break the | ||
loop. Otherwise carry on in the branch. */ | ||
if ((int)(*cc) >= OP_BRA || (int)(*cc) == OP_ONCE) | ||
{ | ||
if (!could_be_empty(cc)) break; | ||
do cc += (cc[1] << 8) + cc[2]; while (*cc == OP_ALT); | ||
cc += 3; | ||
} | ||
else switch (*cc) | ||
{ | ||
/* Reached end of a branch: the subpattern may match the empty string */ | ||
case OP_ALT: | ||
case OP_KET: | ||
case OP_KETRMAX: | ||
case OP_KETRMIN: | ||
return TRUE; | ||
/* Skip over assertive subpatterns */ | ||
case OP_ASSERT: | ||
case OP_ASSERT_NOT: | ||
do cc += (cc[1] << 8) + cc[2]; while (*cc == OP_ALT); | ||
cc += 3; | ||
break; | ||
/* Skip over things that don't match chars */ | ||
case OP_SOD: | ||
case OP_EOD: | ||
case OP_CIRC: | ||
case OP_DOLL: | ||
case OP_BRAZERO: | ||
case OP_BRAMINZERO: | ||
case OP_NOT_WORD_BOUNDARY: | ||
case OP_WORD_BOUNDARY: | ||
cc++; | ||
break; | ||
/* Skip over simple repeats with zero lower bound */ | ||
case OP_STAR: | ||
case OP_MINSTAR: | ||
case OP_QUERY: | ||
case OP_MINQUERY: | ||
case OP_NOTSTAR: | ||
case OP_NOTMINSTAR: | ||
case OP_NOTQUERY: | ||
case OP_NOTMINQUERY: | ||
case OP_TYPESTAR: | ||
case OP_TYPEMINSTAR: | ||
case OP_TYPEQUERY: | ||
case OP_TYPEMINQUERY: | ||
cc += 2; | ||
break; | ||
/* Skip over UPTOs (lower bound is zero) */ | ||
case OP_UPTO: | ||
case OP_MINUPTO: | ||
case OP_TYPEUPTO: | ||
case OP_TYPEMINUPTO: | ||
cc += 4; | ||
break; | ||
/* Check a class or a back reference for a zero minimum */ | ||
case OP_CLASS: | ||
case OP_REF: | ||
cc += (*cc == OP_REF)? 2 : 33; | ||
switch (*cc) | ||
{ | ||
case OP_CRSTAR: | ||
case OP_CRMINSTAR: | ||
case OP_CRQUERY: | ||
case OP_CRMINQUERY: | ||
cc++; | ||
break; | ||
case OP_CRRANGE: | ||
case OP_CRMINRANGE: | ||
if ((cc[1] << 8) + cc[2] != 0) goto NEXT_BRANCH; | ||
cc += 3; | ||
break; | ||
default: | ||
goto NEXT_BRANCH; | ||
} | ||
break; | ||
/* Anything else matches at least one character */ | ||
default: | ||
goto NEXT_BRANCH; | ||
} | ||
} | ||
NEXT_BRANCH: | ||
code += (code[1] << 8) + code[2]; | ||
} | ||
while (*code == OP_ALT); | ||
/* No branches match the empty string */ | ||
return FALSE; | ||
} | ||
/************************************************* | ||
225 | * Handle escapes * | * Handle escapes * |
226 | *************************************************/ | *************************************************/ |
227 | ||
# | Line 354 Arguments: | Line 237 Arguments: |
237 | bracount number of previous extracting brackets | bracount number of previous extracting brackets |
238 | options the options bits | options the options bits |
239 | isclass TRUE if inside a character class | isclass TRUE if inside a character class |
240 | cd pointer to char tables block | |
241 | ||
242 | Returns: zero or positive => a data character | Returns: zero or positive => a data character |
243 | negative => a special escape sequence | negative => a special escape sequence |
# | Line 362 Returns: zero or positive => a data | Line 246 Returns: zero or positive => a data |
246 | ||
247 | static int | static int |
248 | check_escape(const uschar **ptrptr, const char **errorptr, int bracount, | check_escape(const uschar **ptrptr, const char **errorptr, int bracount, |
249 | int options, BOOL isclass) | int options, BOOL isclass, compile_data *cd) |
250 | { | { |
251 | const uschar *ptr = *ptrptr; | const uschar *ptr = *ptrptr; |
252 | int c = *(++ptr) & 255; /* Ensure > 0 on signed-char systems */ | int c = *(++ptr) & 255; /* Ensure > 0 on signed-char systems */ |
# | Line 405 else | Line 289 else |
289 | { | { |
290 | oldptr = ptr; | oldptr = ptr; |
291 | c -= '0'; | c -= '0'; |
292 | while ((pcre_ctypes[ptr[1]] & ctype_digit) != 0) | while ((cd->ctypes[ptr[1]] & ctype_digit) != 0) |
293 | c = c * 10 + *(++ptr) - '0'; | c = c * 10 + *(++ptr) - '0'; |
294 | if (c < 10 || c <= bracount) | if (c < 10 || c <= bracount) |
295 | { | { |
# | Line 431 else | Line 315 else |
315 | ||
316 | case '0': | case '0': |
317 | c -= '0'; | c -= '0'; |
318 | while(i++ < 2 && (pcre_ctypes[ptr[1]] & ctype_digit) != 0 && | while(i++ < 2 && (cd->ctypes[ptr[1]] & ctype_digit) != 0 && |
319 | ptr[1] != '8' && ptr[1] != '9') | ptr[1] != '8' && ptr[1] != '9') |
320 | c = c * 8 + *(++ptr) - '0'; | c = c * 8 + *(++ptr) - '0'; |
321 | break; | break; |
# | Line 440 else | Line 324 else |
324 | ||
325 | case 'x': | case 'x': |
326 | c = 0; | c = 0; |
327 | while (i++ < 2 && (pcre_ctypes[ptr[1]] & ctype_xdigit) != 0) | while (i++ < 2 && (cd->ctypes[ptr[1]] & ctype_xdigit) != 0) |
328 | { | { |
329 | ptr++; | ptr++; |
330 | c = c * 16 + pcre_lcc[*ptr] - | c = c * 16 + cd->lcc[*ptr] - |
331 | (((pcre_ctypes[*ptr] & ctype_digit) != 0)? '0' : 'W'); | (((cd->ctypes[*ptr] & ctype_digit) != 0)? '0' : 'W'); |
332 | } | } |
333 | break; | break; |
334 | ||
# | Line 458 else | Line 342 else |
342 | ||
343 | /* A letter is upper-cased; then the 0x40 bit is flipped */ | /* A letter is upper-cased; then the 0x40 bit is flipped */ |
344 | ||
345 | if (c >= 'a' && c <= 'z') c = pcre_fcc[c]; | if (c >= 'a' && c <= 'z') c = cd->fcc[c]; |
346 | c ^= 0x40; | c ^= 0x40; |
347 | break; | break; |
348 | ||
349 | /* PCRE_EXTRA enables extensions to Perl in the matter of escapes. Any | /* PCRE_EXTRA enables extensions to Perl in the matter of escapes. Any |
350 | other alphameric following \ is an error if PCRE_EXTRA was set; otherwise, | other alphameric following \ is an error if PCRE_EXTRA was set; otherwise, |
351 | for Perl compatibility, it is a literal. */ | for Perl compatibility, it is a literal. This code looks a bit odd, but |
352 | there used to be some cases other than the default, and there may be again | |
353 | in future, so I haven't "optimized" it. */ | |
354 | ||
355 | default: | default: |
356 | if ((options & PCRE_EXTRA) != 0) switch(c) | if ((options & PCRE_EXTRA) != 0) switch(c) |
357 | { | { |
case 'X': | ||
c = -ESC_X; /* This could be a lookup if it ever got into Perl */ | ||
break; | ||
358 | default: | default: |
359 | *errorptr = ERR3; | *errorptr = ERR3; |
360 | break; | break; |
# | Line 498 where the ddds are digits. | Line 380 where the ddds are digits. |
380 | ||
381 | Arguments: | Arguments: |
382 | p pointer to the first char after '{' | p pointer to the first char after '{' |
383 | cd pointer to char tables block | |
384 | ||
385 | Returns: TRUE or FALSE | Returns: TRUE or FALSE |
386 | */ | */ |
387 | ||
388 | static BOOL | static BOOL |
389 | is_counted_repeat(const uschar *p) | is_counted_repeat(const uschar *p, compile_data *cd) |
390 | { | { |
391 | if ((pcre_ctypes[*p++] & ctype_digit) == 0) return FALSE; | if ((cd->ctypes[*p++] & ctype_digit) == 0) return FALSE; |
392 | while ((pcre_ctypes[*p] & ctype_digit) != 0) p++; | while ((cd->ctypes[*p] & ctype_digit) != 0) p++; |
393 | if (*p == '}') return TRUE; | if (*p == '}') return TRUE; |
394 | ||
395 | if (*p++ != ',') return FALSE; | if (*p++ != ',') return FALSE; |
396 | if (*p == '}') return TRUE; | if (*p == '}') return TRUE; |
397 | ||
398 | if ((pcre_ctypes[*p++] & ctype_digit) == 0) return FALSE; | if ((cd->ctypes[*p++] & ctype_digit) == 0) return FALSE; |
399 | while ((pcre_ctypes[*p] & ctype_digit) != 0) p++; | while ((cd->ctypes[*p] & ctype_digit) != 0) p++; |
400 | return (*p == '}'); | return (*p == '}'); |
401 | } | } |
402 | ||
# | Line 533 Arguments: | Line 416 Arguments: |
416 | maxp pointer to int for max | maxp pointer to int for max |
417 | returned as -1 if no max | returned as -1 if no max |
418 | errorptr points to pointer to error message | errorptr points to pointer to error message |
419 | cd pointer to character tables clock | |
420 | ||
421 | Returns: pointer to '}' on success; | Returns: pointer to '}' on success; |
422 | current ptr on error, with errorptr set | current ptr on error, with errorptr set |
423 | */ | */ |
424 | ||
425 | static const uschar * | static const uschar * |
426 | read_repeat_counts(const uschar *p, int *minp, int *maxp, const char **errorptr) | read_repeat_counts(const uschar *p, int *minp, int *maxp, |
427 | const char **errorptr, compile_data *cd) | |
428 | { | { |
429 | int min = 0; | int min = 0; |
430 | int max = -1; | int max = -1; |
431 | ||
432 | while ((pcre_ctypes[*p] & ctype_digit) != 0) min = min * 10 + *p++ - '0'; | while ((cd->ctypes[*p] & ctype_digit) != 0) min = min * 10 + *p++ - '0'; |
433 | ||
434 | if (*p == '}') max = min; else | if (*p == '}') max = min; else |
435 | { | { |
436 | if (*(++p) != '}') | if (*(++p) != '}') |
437 | { | { |
438 | max = 0; | max = 0; |
439 | while((pcre_ctypes[*p] & ctype_digit) != 0) max = max * 10 + *p++ - '0'; | while((cd->ctypes[*p] & ctype_digit) != 0) max = max * 10 + *p++ - '0'; |
440 | if (max < min) | if (max < min) |
441 | { | { |
442 | *errorptr = ERR4; | *errorptr = ERR4; |
# | Line 576 return p; | Line 461 return p; |
461 | ||
462 | ||
463 | /************************************************* | /************************************************* |
464 | * Find the fixed length of a pattern * | |
465 | *************************************************/ | |
466 | ||
467 | /* Scan a pattern and compute the fixed length of subject that will match it, | |
468 | if the length is fixed. This is needed for dealing with backward assertions. | |
469 | ||
470 | Arguments: | |
471 | code points to the start of the pattern (the bracket) | |
472 | ||
473 | Returns: the fixed length, or -1 if there is no fixed length | |
474 | */ | |
475 | ||
476 | static int | |
477 | find_fixedlength(uschar *code) | |
478 | { | |
479 | int length = -1; | |
480 | ||
481 | register int branchlength = 0; | |
482 | register uschar *cc = code + 3; | |
483 | ||
484 | /* Scan along the opcodes for this branch. If we get to the end of the | |
485 | branch, check the length against that of the other branches. */ | |
486 | ||
487 | for (;;) | |
488 | { | |
489 | int d; | |
490 | register int op = *cc; | |
491 | if (op >= OP_BRA) op = OP_BRA; | |
492 | ||
493 | switch (op) | |
494 | { | |
495 | case OP_BRA: | |
496 | case OP_ONCE: | |
497 | case OP_COND: | |
498 | d = find_fixedlength(cc); | |
499 | if (d < 0) return -1; | |
500 | branchlength += d; | |
501 | do cc += (cc[1] << 8) + cc[2]; while (*cc == OP_ALT); | |
502 | cc += 3; | |
503 | break; | |
504 | ||
505 | /* Reached end of a branch; if it's a ket it is the end of a nested | |
506 | call. If it's ALT it is an alternation in a nested call. If it is | |
507 | END it's the end of the outer call. All can be handled by the same code. */ | |
508 | ||
509 | case OP_ALT: | |
510 | case OP_KET: | |
511 | case OP_KETRMAX: | |
512 | case OP_KETRMIN: | |
513 | case OP_END: | |
514 | if (length < 0) length = branchlength; | |
515 | else if (length != branchlength) return -1; | |
516 | if (*cc != OP_ALT) return length; | |
517 | cc += 3; | |
518 | branchlength = 0; | |
519 | break; | |
520 | ||
521 | /* Skip over assertive subpatterns */ | |
522 | ||
523 | case OP_ASSERT: | |
524 | case OP_ASSERT_NOT: | |
525 | case OP_ASSERTBACK: | |
526 | case OP_ASSERTBACK_NOT: | |
527 | do cc += (cc[1] << 8) + cc[2]; while (*cc == OP_ALT); | |
528 | cc += 3; | |
529 | break; | |
530 | ||
531 | /* Skip over things that don't match chars */ | |
532 | ||
533 | case OP_REVERSE: | |
534 | cc++; | |
535 | ||
536 | case OP_CREF: | |
537 | case OP_OPT: | |
538 | cc++; | |
539 | /* Fall through */ | |
540 | ||
541 | case OP_SOD: | |
542 | case OP_EOD: | |
543 | case OP_EODN: | |
544 | case OP_CIRC: | |
545 | case OP_DOLL: | |
546 | case OP_NOT_WORD_BOUNDARY: | |
547 | case OP_WORD_BOUNDARY: | |
548 | cc++; | |
549 | break; | |
550 | ||
551 | /* Handle char strings */ | |
552 | ||
553 | case OP_CHARS: | |
554 | branchlength += *(++cc); | |
555 | cc += *cc + 1; | |
556 | break; | |
557 | ||
558 | /* Handle exact repetitions */ | |
559 | ||
560 | case OP_EXACT: | |
561 | case OP_TYPEEXACT: | |
562 | branchlength += (cc[1] << 8) + cc[2]; | |
563 | cc += 4; | |
564 | break; | |
565 | ||
566 | /* Handle single-char matchers */ | |
567 | ||
568 | case OP_NOT_DIGIT: | |
569 | case OP_DIGIT: | |
570 | case OP_NOT_WHITESPACE: | |
571 | case OP_WHITESPACE: | |
572 | case OP_NOT_WORDCHAR: | |
573 | case OP_WORDCHAR: | |
574 | case OP_ANY: | |
575 | branchlength++; | |
576 | cc++; | |
577 | break; | |
578 | ||
579 | ||
580 | /* Check a class for variable quantification */ | |
581 | ||
582 | case OP_CLASS: | |
583 | cc += (*cc == OP_REF)? 2 : 33; | |
584 | ||
585 | switch (*cc) | |
586 | { | |
587 | case OP_CRSTAR: | |
588 | case OP_CRMINSTAR: | |
589 | case OP_CRQUERY: | |
590 | case OP_CRMINQUERY: | |
591 | return -1; | |
592 | ||
593 | case OP_CRRANGE: | |
594 | case OP_CRMINRANGE: | |
595 | if ((cc[1] << 8) + cc[2] != (cc[3] << 8) + cc[4]) return -1; | |
596 | branchlength += (cc[1] << 8) + cc[2]; | |
597 | cc += 5; | |
598 | break; | |
599 | ||
600 | default: | |
601 | branchlength++; | |
602 | } | |
603 | break; | |
604 | ||
605 | /* Anything else is variable length */ | |
606 | ||
607 | default: | |
608 | return -1; | |
609 | } | |
610 | } | |
611 | /* Control never gets here */ | |
612 | } | |
613 | ||
614 | ||
615 | ||
616 | ||
617 | /************************************************* | |
618 | * Compile one branch * | * Compile one branch * |
619 | *************************************************/ | *************************************************/ |
620 | ||
621 | /* Scan the pattern, compiling it into the code vector. | /* Scan the pattern, compiling it into the code vector. |
622 | ||
623 | Arguments: | Arguments: |
624 | options the option bits | options the option bits |
625 | bracket points to number of brackets used | brackets points to number of brackets used |
626 | code points to the pointer to the current code point | code points to the pointer to the current code point |
627 | ptrptr points to the current pattern pointer | ptrptr points to the current pattern pointer |
628 | errorptr points to pointer to error message | errorptr points to pointer to error message |
629 | optchanged set to the value of the last OP_OPT item compiled | |
630 | cd contains pointers to tables | |
631 | ||
632 | Returns: TRUE on success | Returns: TRUE on success |
633 | FALSE, with *errorptr set on error | FALSE, with *errorptr set on error |
634 | */ | */ |
635 | ||
636 | static BOOL | static BOOL |
637 | compile_branch(int options, int *brackets, uschar **codeptr, | compile_branch(int options, int *brackets, uschar **codeptr, |
638 | const uschar **ptrptr, const char **errorptr) | const uschar **ptrptr, const char **errorptr, int *optchanged, |
639 | compile_data *cd) | |
640 | { | { |
641 | int repeat_type, op_type; | int repeat_type, op_type; |
642 | int repeat_min, repeat_max; | int repeat_min, repeat_max; |
643 | int bravalue, length; | int bravalue, length; |
644 | int greedy_default, greedy_non_default; | |
645 | register int c; | register int c; |
646 | register uschar *code = *codeptr; | register uschar *code = *codeptr; |
647 | uschar *tempcode; | |
648 | const uschar *ptr = *ptrptr; | const uschar *ptr = *ptrptr; |
649 | const uschar *oldptr; | const uschar *tempptr; |
650 | uschar *previous = NULL; | uschar *previous = NULL; |
651 | uschar class[32]; | uschar class[32]; |
652 | ||
653 | /* Set up the default and non-default settings for greediness */ | |
654 | ||
655 | greedy_default = ((options & PCRE_UNGREEDY) != 0); | |
656 | greedy_non_default = greedy_default ^ 1; | |
657 | ||
658 | /* Switch on next character until the end of the branch */ | /* Switch on next character until the end of the branch */ |
659 | ||
660 | for (;; ptr++) | for (;; ptr++) |
661 | { | { |
662 | BOOL negate_class; | BOOL negate_class; |
663 | int class_charcount; | int class_charcount; |
664 | int class_lastchar; | int class_lastchar; |
665 | int newoptions; | |
666 | int condref; | |
667 | ||
668 | c = *ptr; | c = *ptr; |
669 | if ((options & PCRE_EXTENDED) != 0) | if ((options & PCRE_EXTENDED) != 0) |
670 | { | { |
671 | if ((pcre_ctypes[c] & ctype_space) != 0) continue; | if ((cd->ctypes[c] & ctype_space) != 0) continue; |
672 | if (c == '#') | if (c == '#') |
673 | { | { |
674 | while ((c = *(++ptr)) != 0 && c != '\n'); | while ((c = *(++ptr)) != 0 && c != '\n'); |
# | Line 662 for (;; ptr++) | Line 713 for (;; ptr++) |
713 | previous = code; | previous = code; |
714 | *code++ = OP_CLASS; | *code++ = OP_CLASS; |
715 | ||
716 | /* If the first character is '^', set the negation flag */ | /* If the first character is '^', set the negation flag and skip it. */ |
717 | ||
718 | if ((c = *(++ptr)) == '^') | if ((c = *(++ptr)) == '^') |
719 | { | { |
# | Line 705 for (;; ptr++) | Line 756 for (;; ptr++) |
756 | ||
757 | if (c == '\\') | if (c == '\\') |
758 | { | { |
759 | c = check_escape(&ptr, errorptr, *brackets, options, TRUE); | c = check_escape(&ptr, errorptr, *brackets, options, TRUE, cd); |
760 | if (-c == ESC_b) c = '\b'; | if (-c == ESC_b) c = '\b'; |
761 | else if (c < 0) | else if (c < 0) |
762 | { | { |
763 | register const uschar *cbits = cd->cbits; | |
764 | class_charcount = 10; | class_charcount = 10; |
765 | switch (-c) | switch (-c) |
766 | { | { |
767 | case ESC_d: | case ESC_d: |
768 | for (c = 0; c < 32; c++) class[c] |= pcre_cbits[c+cbit_digit]; | for (c = 0; c < 32; c++) class[c] |= cbits[c+cbit_digit]; |
769 | continue; | continue; |
770 | ||
771 | case ESC_D: | case ESC_D: |
772 | for (c = 0; c < 32; c++) class[c] |= ~pcre_cbits[c+cbit_digit]; | for (c = 0; c < 32; c++) class[c] |= ~cbits[c+cbit_digit]; |
773 | continue; | continue; |
774 | ||
775 | case ESC_w: | case ESC_w: |
776 | for (c = 0; c < 32; c++) | for (c = 0; c < 32; c++) |
777 | class[c] |= (pcre_cbits[c] | pcre_cbits[c+cbit_word]); | class[c] |= (cbits[c+cbit_digit] | cbits[c+cbit_word]); |
778 | continue; | continue; |
779 | ||
780 | case ESC_W: | case ESC_W: |
781 | for (c = 0; c < 32; c++) | for (c = 0; c < 32; c++) |
782 | class[c] |= ~(pcre_cbits[c] | pcre_cbits[c+cbit_word]); | class[c] |= ~(cbits[c+cbit_digit] | cbits[c+cbit_word]); |
783 | continue; | continue; |
784 | ||
785 | case ESC_s: | case ESC_s: |
786 | for (c = 0; c < 32; c++) class[c] |= pcre_cbits[c+cbit_space]; | for (c = 0; c < 32; c++) class[c] |= cbits[c+cbit_space]; |
787 | continue; | continue; |
788 | ||
789 | case ESC_S: | case ESC_S: |
790 | for (c = 0; c < 32; c++) class[c] |= ~pcre_cbits[c+cbit_space]; | for (c = 0; c < 32; c++) class[c] |= ~cbits[c+cbit_space]; |
791 | continue; | continue; |
792 | ||
793 | default: | default: |
# | Line 767 for (;; ptr++) | Line 819 for (;; ptr++) |
819 | ||
820 | if (d == '\\') | if (d == '\\') |
821 | { | { |
822 | d = check_escape(&ptr, errorptr, *brackets, options, TRUE); | d = check_escape(&ptr, errorptr, *brackets, options, TRUE, cd); |
823 | if (d < 0) | if (d < 0) |
824 | { | { |
825 | if (d == -ESC_b) d = '\b'; else | if (d == -ESC_b) d = '\b'; else |
# | Line 789 for (;; ptr++) | Line 841 for (;; ptr++) |
841 | class[c/8] |= (1 << (c&7)); | class[c/8] |= (1 << (c&7)); |
842 | if ((options & PCRE_CASELESS) != 0) | if ((options & PCRE_CASELESS) != 0) |
843 | { | { |
844 | int uc = pcre_fcc[c]; /* flip case */ | int uc = cd->fcc[c]; /* flip case */ |
845 | class[uc/8] |= (1 << (uc&7)); | class[uc/8] |= (1 << (uc&7)); |
846 | } | } |
847 | class_charcount++; /* in case a one-char range */ | class_charcount++; /* in case a one-char range */ |
# | Line 804 for (;; ptr++) | Line 856 for (;; ptr++) |
856 | class [c/8] |= (1 << (c&7)); | class [c/8] |= (1 << (c&7)); |
857 | if ((options & PCRE_CASELESS) != 0) | if ((options & PCRE_CASELESS) != 0) |
858 | { | { |
859 | c = pcre_fcc[c]; /* flip case */ | c = cd->fcc[c]; /* flip case */ |
860 | class[c/8] |= (1 << (c&7)); | class[c/8] |= (1 << (c&7)); |
861 | } | } |
862 | class_charcount++; | class_charcount++; |
# | Line 851 for (;; ptr++) | Line 903 for (;; ptr++) |
903 | /* Various kinds of repeat */ | /* Various kinds of repeat */ |
904 | ||
905 | case '{': | case '{': |
906 | if (!is_counted_repeat(ptr+1)) goto NORMAL_CHAR; | if (!is_counted_repeat(ptr+1, cd)) goto NORMAL_CHAR; |
907 | ptr = read_repeat_counts(ptr+1, &repeat_min, &repeat_max, errorptr); | ptr = read_repeat_counts(ptr+1, &repeat_min, &repeat_max, errorptr, cd); |
908 | if (*errorptr != NULL) goto FAILED; | if (*errorptr != NULL) goto FAILED; |
909 | goto REPEAT; | goto REPEAT; |
910 | ||
# | Line 877 for (;; ptr++) | Line 929 for (;; ptr++) |
929 | goto FAILED; | goto FAILED; |
930 | } | } |
931 | ||
932 | /* If the next character is '?' this is a minimizing repeat. Advance to the | /* If the next character is '?' this is a minimizing repeat, by default, |
933 | but if PCRE_UNGREEDY is set, it works the other way round. Advance to the | |
934 | next character. */ | next character. */ |
935 | ||
936 | if (ptr[1] == '?') { repeat_type = 1; ptr++; } else repeat_type = 0; | if (ptr[1] == '?') |
937 | { repeat_type = greedy_non_default; ptr++; } | |
938 | else repeat_type = greedy_default; | |
939 | ||
940 | /* If the maximum is zero then the minimum must also be zero; Perl allows | /* If the maximum is zero then the minimum must also be zero; Perl allows |
941 | this case, so we do too - by simply omitting the item altogether. */ | this case, so we do too - by simply omitting the item altogether. */ |
# | Line 925 for (;; ptr++) | Line 980 for (;; ptr++) |
980 | create a suitable repeat item. The code is shared with single-character | create a suitable repeat item. The code is shared with single-character |
981 | repeats by adding a suitable offset into repeat_type. */ | repeats by adding a suitable offset into repeat_type. */ |
982 | ||
983 | else if ((int)*previous < OP_EOD || *previous == OP_ANY) | else if ((int)*previous < OP_EODN || *previous == OP_ANY) |
984 | { | { |
985 | op_type = OP_TYPESTAR - OP_STAR; /* Use type opcodes */ | op_type = OP_TYPESTAR - OP_STAR; /* Use type opcodes */ |
986 | c = *previous; | c = *previous; |
# | Line 969 for (;; ptr++) | Line 1024 for (;; ptr++) |
1024 | /* If the mininum is 1 and the previous item was a character string, | /* If the mininum is 1 and the previous item was a character string, |
1025 | we either have to put back the item that got cancelled if the string | we either have to put back the item that got cancelled if the string |
1026 | length was 1, or add the character back onto the end of a longer | length was 1, or add the character back onto the end of a longer |
1027 | string. For a character type nothing need be done; it will just get put | string. For a character type nothing need be done; it will just get |
1028 | back naturally. */ | put back naturally. Note that the final character is always going to |
1029 | get added below. */ | |
1030 | ||
1031 | else if (*previous == OP_CHARS) | else if (*previous == OP_CHARS) |
1032 | { | { |
1033 | if (code == previous) code += 2; else previous[1]++; | if (code == previous) code += 2; else previous[1]++; |
1034 | } | } |
1035 | ||
1036 | /* Insert an UPTO if the max is greater than the min. */ | /* For a single negated character we also have to put back the |
1037 | item that got cancelled. */ | |
1038 | ||
1039 | else if (*previous == OP_NOT) code++; | |
1040 | ||
1041 | /* If the maximum is unlimited, insert an OP_STAR. */ | |
1042 | ||
1043 | if (repeat_max < 0) | |
1044 | { | |
1045 | *code++ = c; | |
1046 | *code++ = OP_STAR + repeat_type; | |
1047 | } | |
1048 | ||
1049 | /* Else insert an UPTO if the max is greater than the min. */ | |
1050 | ||
1051 | if (repeat_max != repeat_min) | else if (repeat_max != repeat_min) |
1052 | { | { |
1053 | *code++ = c; | *code++ = c; |
1054 | repeat_max -= repeat_min; | repeat_max -= repeat_min; |
# | Line 1017 for (;; ptr++) | Line 1086 for (;; ptr++) |
1086 | } | } |
1087 | ||
1088 | /* If previous was a bracket group, we may have to replicate it in certain | /* If previous was a bracket group, we may have to replicate it in certain |
1089 | cases. If the maximum repeat count is unlimited, check that the bracket | cases. */ |
group cannot match the empty string, and diagnose an error if it can. */ | ||
1090 | ||
1091 | else if ((int)*previous >= OP_BRA) | else if ((int)*previous >= OP_BRA || (int)*previous == OP_ONCE || |
1092 | (int)*previous == OP_COND) | |
1093 | { | { |
1094 | int i; | register int i; |
1095 | int length = code - previous; | int ketoffset = 0; |
1096 | int len = code - previous; | |
1097 | uschar *bralink = NULL; | |
1098 | ||
1099 | /* If the maximum repeat count is unlimited, find the end of the bracket | |
1100 | by scanning through from the start, and compute the offset back to it | |
1101 | from the current code pointer. There may be an OP_OPT setting following | |
1102 | the final KET, so we can't find the end just by going back from the code | |
1103 | pointer. */ | |
1104 | ||
1105 | if (repeat_max == -1) | |
1106 | { | |
1107 | register uschar *ket = previous; | |
1108 | do ket += (ket[1] << 8) + ket[2]; while (*ket != OP_KET); | |
1109 | ketoffset = code - ket; | |
1110 | } | |
1111 | ||
1112 | /* The case of a zero minimum is special because of the need to stick | |
1113 | OP_BRAZERO in front of it, and because the group appears once in the | |
1114 | data, whereas in other cases it appears the minimum number of times. For | |
1115 | this reason, it is simplest to treat this case separately, as otherwise | |
1116 | the code gets far too mess. There are several special subcases when the | |
1117 | minimum is zero. */ | |
1118 | ||
1119 | if (repeat_max == -1 && could_be_empty(previous)) | if (repeat_min == 0) |
1120 | { | { |
1121 | *errorptr = ERR10; | /* If the maximum is also zero, we just omit the group from the output |
1122 | goto FAILED; | altogether. */ |
1123 | ||
1124 | if (repeat_max == 0) | |
1125 | { | |
1126 | code = previous; | |
1127 | previous = NULL; | |
1128 | break; | |
1129 | } | |
1130 | ||
1131 | /* If the maximum is 1 or unlimited, we just have to stick in the | |
1132 | BRAZERO and do no more at this point. */ | |
1133 | ||
1134 | if (repeat_max <= 1) | |
1135 | { | |
1136 | memmove(previous+1, previous, len); | |
1137 | code++; | |
1138 | *previous++ = OP_BRAZERO + repeat_type; | |
1139 | } | |
1140 | ||
1141 | /* If the maximum is greater than 1 and limited, we have to replicate | |
1142 | in a nested fashion, sticking OP_BRAZERO before each set of brackets. | |
1143 | The first one has to be handled carefully because it's the original | |
1144 | copy, which has to be moved up. The remainder can be handled by code | |
1145 | that is common with the non-zero minimum case below. We just have to | |
1146 | adjust the value or repeat_max, since one less copy is required. */ | |
1147 | ||
1148 | else | |
1149 | { | |
1150 | int offset; | |
1151 | memmove(previous+4, previous, len); | |
1152 | code += 4; | |
1153 | *previous++ = OP_BRAZERO + repeat_type; | |
1154 | *previous++ = OP_BRA; | |
1155 | ||
1156 | /* We chain together the bracket offset fields that have to be | |
1157 | filled in later when the ends of the brackets are reached. */ | |
1158 | ||
1159 | offset = (bralink == NULL)? 0 : previous - bralink; | |
1160 | bralink = previous; | |
1161 | *previous++ = offset >> 8; | |
1162 | *previous++ = offset & 255; | |
1163 | } | |
1164 | ||
1165 | repeat_max--; | |
1166 | } | |
1167 | ||
1168 | /* If the minimum is greater than zero, replicate the group as many | |
1169 | times as necessary, and adjust the maximum to the number of subsequent | |
1170 | copies that we need. */ | |
1171 | ||
1172 | else | |
1173 | { | |
1174 | for (i = 1; i < repeat_min; i++) | |
1175 | { | |
1176 | memcpy(code, previous, len); | |
1177 | code += len; | |
1178 | } | |
1179 | if (repeat_max > 0) repeat_max -= repeat_min; | |
1180 | } | |
1181 | ||
1182 | /* This code is common to both the zero and non-zero minimum cases. If | |
1183 | the maximum is limited, it replicates the group in a nested fashion, | |
1184 | remembering the bracket starts on a stack. In the case of a zero minimum, | |
1185 | the first one was set up above. In all cases the repeat_max now specifies | |
1186 | the number of additional copies needed. */ | |
1187 | ||
1188 | if (repeat_max >= 0) | |
1189 | { | |
1190 | for (i = repeat_max - 1; i >= 0; i--) | |
1191 | { | |
1192 | *code++ = OP_BRAZERO + repeat_type; | |
1193 | ||
1194 | /* All but the final copy start a new nesting, maintaining the | |
1195 | chain of brackets outstanding. */ | |
1196 | ||
1197 | if (i != 0) | |
1198 | { | |
1199 | int offset; | |
1200 | *code++ = OP_BRA; | |
1201 | offset = (bralink == NULL)? 0 : code - bralink; | |
1202 | bralink = code; | |
1203 | *code++ = offset >> 8; | |
1204 | *code++ = offset & 255; | |
1205 | } | |
1206 | ||
1207 | memcpy(code, previous, len); | |
1208 | code += len; | |
1209 | } | |
1210 | ||
1211 | /* Now chain through the pending brackets, and fill in their length | |
1212 | fields (which are holding the chain links pro tem). */ | |
1213 | ||
1214 | while (bralink != NULL) | |
1215 | { | |
1216 | int oldlinkoffset; | |
1217 | int offset = code - bralink + 1; | |
1218 | uschar *bra = code - offset; | |
1219 | oldlinkoffset = (bra[1] << 8) + bra[2]; | |
1220 | bralink = (oldlinkoffset == 0)? NULL : bralink - oldlinkoffset; | |
1221 | *code++ = OP_KET; | |
1222 | *code++ = bra[1] = offset >> 8; | |
1223 | *code++ = bra[2] = (offset & 255); | |
1224 | } | |
1225 | } | } |
1226 | ||
1227 | /* If the maximum is unlimited, set a repeater in the final copy. We | |
1228 | can't just offset backwards from the current code point, because we | |
1229 | don't know if there's been an options resetting after the ket. The | |
1230 | correct offset was computed above. */ | |
1231 | ||
1232 | else code[-ketoffset] = OP_KETRMAX + repeat_type; | |
1233 | ||
1234 | ||
1235 | #ifdef NEVER | |
1236 | /* If the minimum is greater than zero, and the maximum is unlimited or | /* If the minimum is greater than zero, and the maximum is unlimited or |
1237 | equal to the minimum, the first copy remains where it is, and is | equal to the minimum, the first copy remains where it is, and is |
1238 | replicated up to the minimum number of times. This case includes the + | replicated up to the minimum number of times. This case includes the + |
# | Line 1040 for (;; ptr++) | Line 1242 for (;; ptr++) |
1242 | { | { |
1243 | for (i = 1; i < repeat_min; i++) | for (i = 1; i < repeat_min; i++) |
1244 | { | { |
1245 | memcpy(code, previous, length); | memcpy(code, previous, len); |
1246 | code += length; | code += len; |
1247 | } | } |
1248 | } | } |
1249 | ||
# | Line 1053 for (;; ptr++) | Line 1255 for (;; ptr++) |
1255 | { | { |
1256 | if (repeat_min == 0) | if (repeat_min == 0) |
1257 | { | { |
1258 | memmove(previous+1, previous, length); | memmove(previous+1, previous, len); |
1259 | code++; | code++; |
1260 | *previous++ = OP_BRAZERO + repeat_type; | *previous++ = OP_BRAZERO + repeat_type; |
1261 | } | } |
1262 | ||
1263 | for (i = 1; i < repeat_min; i++) | for (i = 1; i < repeat_min; i++) |
1264 | { | { |
1265 | memcpy(code, previous, length); | memcpy(code, previous, len); |
1266 | code += length; | code += len; |
1267 | } | } |
1268 | ||
1269 | for (i = (repeat_min > 0)? repeat_min : 1; i < repeat_max; i++) | for (i = (repeat_min > 0)? repeat_min : 1; i < repeat_max; i++) |
1270 | { | { |
1271 | *code++ = OP_BRAZERO + repeat_type; | *code++ = OP_BRAZERO + repeat_type; |
1272 | memcpy(code, previous, length); | memcpy(code, previous, len); |
1273 | code += length; | code += len; |
1274 | } | } |
1275 | } | } |
1276 | ||
1277 | /* If the maximum is unlimited, set a repeater in the final copy. */ | /* If the maximum is unlimited, set a repeater in the final copy. We |
1278 | can't just offset backwards from the current code point, because we | |
1279 | don't know if there's been an options resetting after the ket. The | |
1280 | correct offset was computed above. */ | |
1281 | ||
1282 | if (repeat_max == -1) code[-ketoffset] = OP_KETRMAX + repeat_type; | |
1283 | #endif | |
1284 | ||
1285 | ||
if (repeat_max == -1) code[-3] = OP_KETRMAX + repeat_type; | ||
1286 | } | } |
1287 | ||
1288 | /* Else there's some kind of shambles */ | /* Else there's some kind of shambles */ |
# | Line 1091 for (;; ptr++) | Line 1299 for (;; ptr++) |
1299 | break; | break; |
1300 | ||
1301 | ||
1302 | /* Start of nested bracket sub-expression, or comment or lookahead. | /* Start of nested bracket sub-expression, or comment or lookahead or |
1303 | First deal with special things that can come after a bracket; all are | lookbehind or option setting or condition. First deal with special things |
1304 | introduced by ?, and the appearance of any of them means that this is not a | that can come after a bracket; all are introduced by ?, and the appearance |
1305 | referencing group. They were checked for validity in the first pass over | of any of them means that this is not a referencing group. They were |
1306 | the string, so we don't have to check for syntax errors here. */ | checked for validity in the first pass over the string, so we don't have to |
1307 | check for syntax errors here. */ | |
1308 | ||
1309 | case '(': | case '(': |
1310 | previous = code; /* Only real brackets can be repeated */ | newoptions = options; |
1311 | condref = -1; | |
1312 | ||
1313 | if (*(++ptr) == '?') | if (*(++ptr) == '?') |
1314 | { | { |
1315 | bravalue = OP_BRA; | int set, unset; |
1316 | int *optset; | |
1317 | ||
1318 | switch (*(++ptr)) | switch (*(++ptr)) |
1319 | { | { |
1320 | case '#': | case '#': /* Comment; skip to ket */ |
case 'i': | ||
case 'm': | ||
case 's': | ||
case 'x': | ||
1321 | ptr++; | ptr++; |
1322 | while (*ptr != ')') ptr++; | while (*ptr != ')') ptr++; |
previous = NULL; | ||
1323 | continue; | continue; |
1324 | ||
1325 | case ':': /* Non-extracting bracket */ | case ':': /* Non-extracting bracket */ |
1326 | bravalue = OP_BRA; | |
1327 | ptr++; | ptr++; |
1328 | break; | break; |
1329 | ||
1330 | case '=': /* Assertions can't be repeated */ | case '(': |
1331 | bravalue = OP_COND; /* Conditional group */ | |
1332 | if ((cd->ctypes[*(++ptr)] & ctype_digit) != 0) | |
1333 | { | |
1334 | condref = *ptr - '0'; | |
1335 | while (*(++ptr) != ')') condref = condref*10 + *ptr - '0'; | |
1336 | ptr++; | |
1337 | } | |
1338 | else ptr--; | |
1339 | break; | |
1340 | ||
1341 | case '=': /* Positive lookahead */ | |
1342 | bravalue = OP_ASSERT; | bravalue = OP_ASSERT; |
1343 | ptr++; | ptr++; |
previous = NULL; | ||
1344 | break; | break; |
1345 | ||
1346 | case '!': | case '!': /* Negative lookahead */ |
1347 | bravalue = OP_ASSERT_NOT; | bravalue = OP_ASSERT_NOT; |
1348 | ptr++; | ptr++; |
previous = NULL; | ||
1349 | break; | break; |
1350 | ||
1351 | case '>': /* "Match once" brackets */ | case '<': /* Lookbehinds */ |
1352 | if ((options & PCRE_EXTRA) != 0) /* Not yet standard */ | switch (*(++ptr)) |
1353 | { | { |
1354 | bravalue = OP_ONCE; | case '=': /* Positive lookbehind */ |
1355 | bravalue = OP_ASSERTBACK; | |
1356 | ptr++; | ptr++; |
previous = NULL; | ||
1357 | break; | break; |
1358 | ||
1359 | case '!': /* Negative lookbehind */ | |
1360 | bravalue = OP_ASSERTBACK_NOT; | |
1361 | ptr++; | |
1362 | break; | |
1363 | ||
1364 | default: /* Syntax error */ | |
1365 | *errorptr = ERR24; | |
1366 | goto FAILED; | |
1367 | } | } |
1368 | /* Else fall through */ | break; |
1369 | ||
1370 | default: | case '>': /* One-time brackets */ |
1371 | *errorptr = ERR12; | bravalue = OP_ONCE; |
1372 | goto FAILED; | ptr++; |
1373 | break; | |
1374 | ||
1375 | default: /* Option setting */ | |
1376 | set = unset = 0; | |
1377 | optset = &set; | |
1378 | ||
1379 | while (*ptr != ')' && *ptr != ':') | |
1380 | { | |
1381 | switch (*ptr++) | |
1382 | { | |
1383 | case '-': optset = &unset; break; | |
1384 | ||
1385 | case 'i': *optset |= PCRE_CASELESS; break; | |
1386 | case 'm': *optset |= PCRE_MULTILINE; break; | |
1387 | case 's': *optset |= PCRE_DOTALL; break; | |
1388 | case 'x': *optset |= PCRE_EXTENDED; break; | |
1389 | case 'U': *optset |= PCRE_UNGREEDY; break; | |
1390 | case 'X': *optset |= PCRE_EXTRA; break; | |
1391 | ||
1392 | default: | |
1393 | *errorptr = ERR12; | |
1394 | goto FAILED; | |
1395 | } | |
1396 | } | |
1397 | ||
1398 | /* Set up the changed option bits, but don't change anything yet. */ | |
1399 | ||
1400 | newoptions = (options | set) & (~unset); | |
1401 | ||
1402 | /* If the options ended with ')' this is not the start of a nested | |
1403 | group with option changes, so the options change at this level. At top | |
1404 | level there is nothing else to be done (the options will in fact have | |
1405 | been set from the start of compiling as a result of the first pass) but | |
1406 | at an inner level we must compile code to change the ims options if | |
1407 | necessary, and pass the new setting back so that it can be put at the | |
1408 | start of any following branches, and when this group ends, a resetting | |
1409 | item can be compiled. */ | |
1410 | ||
1411 | if (*ptr == ')') | |
1412 | { | |
1413 | if ((options & PCRE_INGROUP) != 0 && | |
1414 | (options & PCRE_IMS) != (newoptions & PCRE_IMS)) | |
1415 | { | |
1416 | *code++ = OP_OPT; | |
1417 | *code++ = *optchanged = newoptions & PCRE_IMS; | |
1418 | } | |
1419 | options = newoptions; /* Change options at this level */ | |
1420 | previous = NULL; /* This item can't be repeated */ | |
1421 | continue; /* It is complete */ | |
1422 | } | |
1423 | ||
1424 | /* If the options ended with ':' we are heading into a nested group | |
1425 | with possible change of options. Such groups are non-capturing and are | |
1426 | not assertions of any kind. All we need to do is skip over the ':'; | |
1427 | the newoptions value is handled below. */ | |
1428 | ||
1429 | bravalue = OP_BRA; | |
1430 | ptr++; | |
1431 | } | } |
1432 | } | } |
1433 | ||
1434 | /* Else we have a referencing group */ | /* Else we have a referencing group; adjust the opcode. */ |
1435 | ||
1436 | else | else |
1437 | { | { |
# | Line 1159 for (;; ptr++) | Line 1443 for (;; ptr++) |
1443 | bravalue = OP_BRA + *brackets; | bravalue = OP_BRA + *brackets; |
1444 | } | } |
1445 | ||
1446 | /* Process nested bracketed re; at end pointer is on the bracket. We copy | /* Process nested bracketed re. Assertions may not be repeated, but other |
1447 | code into a non-register variable in order to be able to pass its address | kinds can be. We copy code into a non-register variable in order to be able |
1448 | because some compilers complain otherwise. */ | to pass its address because some compilers complain otherwise. Pass in a |
1449 | new setting for the ims options if they have changed. */ | |
1450 | ||
1451 | previous = (bravalue >= OP_ONCE)? code : NULL; | |
1452 | *code = bravalue; | *code = bravalue; |
1453 | tempcode = code; | |
1454 | ||
1455 | if (!compile_regex( | |
1456 | options | PCRE_INGROUP, /* Set for all nested groups */ | |
1457 | ((options & PCRE_IMS) != (newoptions & PCRE_IMS))? | |
1458 | newoptions & PCRE_IMS : -1, /* Pass ims options if changed */ | |
1459 | brackets, /* Bracket level */ | |
1460 | &tempcode, /* Where to put code (updated) */ | |
1461 | &ptr, /* Input pointer (updated) */ | |
1462 | errorptr, /* Where to put an error message */ | |
1463 | (bravalue == OP_ASSERTBACK || | |
1464 | bravalue == OP_ASSERTBACK_NOT), /* TRUE if back assert */ | |
1465 | condref, /* Condition reference number */ | |
1466 | cd)) /* Tables block */ | |
1467 | goto FAILED; | |
1468 | ||
1469 | /* At the end of compiling, code is still pointing to the start of the | |
1470 | group, while tempcode has been updated to point past the end of the group | |
1471 | and any option resetting that may follow it. The pattern pointer (ptr) | |
1472 | is on the bracket. */ | |
1473 | ||
1474 | /* If this is a conditional bracket, check that there are no more than | |
1475 | two branches in the group. */ | |
1476 | ||
1477 | if (bravalue == OP_COND) | |
1478 | { | { |
1479 | uschar *mcode = code; | int branchcount = 0; |
1480 | if (!compile_regex(options, brackets, &mcode, &ptr, errorptr)) | uschar *tc = code; |
1481 | ||
1482 | do { | |
1483 | branchcount++; | |
1484 | tc += (tc[1] << 8) | tc[2]; | |
1485 | } | |
1486 | while (*tc != OP_KET); | |
1487 | ||
1488 | if (branchcount > 2) | |
1489 | { | |
1490 | *errorptr = ERR27; | |
1491 | goto FAILED; | goto FAILED; |
1492 | code = mcode; | } |
1493 | } | } |
1494 | ||
1495 | /* Now update the main code pointer to the end of the group. */ | |
1496 | ||
1497 | code = tempcode; | |
1498 | ||
1499 | /* Error if hit end of pattern */ | |
1500 | ||
1501 | if (*ptr != ')') | if (*ptr != ')') |
1502 | { | { |
1503 | *errorptr = ERR14; | *errorptr = ERR14; |
# | Line 1183 for (;; ptr++) | Line 1510 for (;; ptr++) |
1510 | for validity in the pre-compiling pass. */ | for validity in the pre-compiling pass. */ |
1511 | ||
1512 | case '\\': | case '\\': |
1513 | oldptr = ptr; | tempptr = ptr; |
1514 | c = check_escape(&ptr, errorptr, *brackets, options, FALSE); | c = check_escape(&ptr, errorptr, *brackets, options, FALSE, cd); |
1515 | ||
1516 | /* Handle metacharacters introduced by \. For ones like \d, the ESC_ values | /* Handle metacharacters introduced by \. For ones like \d, the ESC_ values |
1517 | are arranged to be the negation of the corresponding OP_values. For the | are arranged to be the negation of the corresponding OP_values. For the |
# | Line 1197 for (;; ptr++) | Line 1524 for (;; ptr++) |
1524 | { | { |
1525 | if (-c >= ESC_REF) | if (-c >= ESC_REF) |
1526 | { | { |
int refnum = -c - ESC_REF; | ||
if (*brackets < refnum) | ||
{ | ||
*errorptr = ERR15; | ||
goto FAILED; | ||
} | ||
1527 | previous = code; | previous = code; |
1528 | *code++ = OP_REF; | *code++ = OP_REF; |
1529 | *code++ = refnum; | *code++ = -c - ESC_REF; |
1530 | } | } |
1531 | else | else |
1532 | { | { |
1533 | previous = (-c > ESC_b && -c < ESC_X)? code : NULL; | previous = (-c > ESC_b && -c < ESC_Z)? code : NULL; |
1534 | *code++ = -c; | *code++ = -c; |
1535 | } | } |
1536 | continue; | continue; |
# | Line 1217 for (;; ptr++) | Line 1538 for (;; ptr++) |
1538 | ||
1539 | /* Data character: reset and fall through */ | /* Data character: reset and fall through */ |
1540 | ||
1541 | ptr = oldptr; | ptr = tempptr; |
1542 | c = '\\'; | c = '\\'; |
1543 | ||
1544 | /* Handle a run of data characters until a metacharacter is encountered. | /* Handle a run of data characters until a metacharacter is encountered. |
# | Line 1235 for (;; ptr++) | Line 1556 for (;; ptr++) |
1556 | { | { |
1557 | if ((options & PCRE_EXTENDED) != 0) | if ((options & PCRE_EXTENDED) != 0) |
1558 | { | { |
1559 | if ((pcre_ctypes[c] & ctype_space) != 0) continue; | if ((cd->ctypes[c] & ctype_space) != 0) continue; |
1560 | if (c == '#') | if (c == '#') |
1561 | { | { |
1562 | while ((c = *(++ptr)) != 0 && c != '\n'); | while ((c = *(++ptr)) != 0 && c != '\n'); |
# | Line 1250 for (;; ptr++) | Line 1571 for (;; ptr++) |
1571 | ||
1572 | if (c == '\\') | if (c == '\\') |
1573 | { | { |
1574 | oldptr = ptr; | tempptr = ptr; |
1575 | c = check_escape(&ptr, errorptr, *brackets, options, FALSE); | c = check_escape(&ptr, errorptr, *brackets, options, FALSE, cd); |
1576 | if (c < 0) { ptr = oldptr; break; } | if (c < 0) { ptr = tempptr; break; } |
1577 | } | } |
1578 | ||
1579 | /* Ordinary character or single-char escape */ | /* Ordinary character or single-char escape */ |
# | Line 1263 for (;; ptr++) | Line 1584 for (;; ptr++) |
1584 | ||
1585 | /* This "while" is the end of the "do" above. */ | /* This "while" is the end of the "do" above. */ |
1586 | ||
1587 | while (length < 255 && (pcre_ctypes[c = *(++ptr)] & ctype_meta) == 0); | while (length < 255 && (cd->ctypes[c = *(++ptr)] & ctype_meta) == 0); |
1588 | ||
1589 | /* Compute the length and set it in the data vector, and advance to | /* Compute the length and set it in the data vector, and advance to |
1590 | the next state. */ | the next state. */ |
1591 | ||
1592 | previous[1] = length; | previous[1] = length; |
1593 | ptr--; | if (length < 255) ptr--; |
1594 | break; | break; |
1595 | } | } |
1596 | } /* end of big loop */ | } /* end of big loop */ |
# | Line 1293 return FALSE; | Line 1614 return FALSE; |
1614 | /* On entry, ptr is pointing past the bracket character, but on return | /* On entry, ptr is pointing past the bracket character, but on return |
1615 | it points to the closing bracket, or vertical bar, or end of string. | it points to the closing bracket, or vertical bar, or end of string. |
1616 | The code variable is pointing at the byte into which the BRA operator has been | The code variable is pointing at the byte into which the BRA operator has been |
1617 | stored. | stored. If the ims options are changed at the start (for a (?ims: group) or |
1618 | during any branch, we need to insert an OP_OPT item at the start of every | |
1619 | following branch to ensure they get set correctly at run time, and also pass | |
1620 | the new options into every subsequent branch compile. | |
1621 | ||
1622 | Argument: | Argument: |
1623 | options the option bits | options the option bits |
1624 | brackets -> int containing the number of extracting brackets used | optchanged new ims options to set as if (?ims) were at the start, or -1 |
1625 | codeptr -> the address of the current code pointer | for no change |
1626 | ptrptr -> the address of the current pattern pointer | brackets -> int containing the number of extracting brackets used |
1627 | errorptr -> pointer to error message | codeptr -> the address of the current code pointer |
1628 | ptrptr -> the address of the current pattern pointer | |
1629 | errorptr -> pointer to error message | |
1630 | lookbehind TRUE if this is a lookbehind assertion | |
1631 | condref > 0 for OPT_CREF setting at start of conditional group | |
1632 | cd points to the data block with tables pointers | |
1633 | ||
1634 | Returns: TRUE on success | Returns: TRUE on success |
1635 | */ | */ |
1636 | ||
1637 | static BOOL | static BOOL |
1638 | compile_regex(int options, int *brackets, uschar **codeptr, | compile_regex(int options, int optchanged, int *brackets, uschar **codeptr, |
1639 | const uschar **ptrptr, const char **errorptr) | const uschar **ptrptr, const char **errorptr, BOOL lookbehind, int condref, |
1640 | compile_data *cd) | |
1641 | { | { |
1642 | const uschar *ptr = *ptrptr; | const uschar *ptr = *ptrptr; |
1643 | uschar *code = *codeptr; | uschar *code = *codeptr; |
1644 | uschar *last_branch = code; | |
1645 | uschar *start_bracket = code; | uschar *start_bracket = code; |
1646 | uschar *reverse_count = NULL; | |
1647 | int oldoptions = options & PCRE_IMS; | |
1648 | ||
1649 | code += 3; | |
1650 | ||
1651 | /* At the start of a reference-based conditional group, insert the reference | |
1652 | number as an OP_CREF item. */ | |
1653 | ||
1654 | if (condref > 0) | |
1655 | { | |
1656 | *code++ = OP_CREF; | |
1657 | *code++ = condref; | |
1658 | } | |
1659 | ||
1660 | /* Loop for each alternative branch */ | |
1661 | ||
1662 | for (;;) | for (;;) |
1663 | { | { |
1664 | int length; | int length; |
uschar *last_branch = code; | ||
1665 | ||
1666 | code += 3; | /* Handle change of options */ |
1667 | if (!compile_branch(options, brackets, &code, &ptr, errorptr)) | |
1668 | if (optchanged >= 0) | |
1669 | { | |
1670 | *code++ = OP_OPT; | |
1671 | *code++ = optchanged; | |
1672 | options = (options & ~PCRE_IMS) | optchanged; | |
1673 | } | |
1674 | ||
1675 | /* Set up dummy OP_REVERSE if lookbehind assertion */ | |
1676 | ||
1677 | if (lookbehind) | |
1678 | { | |
1679 | *code++ = OP_REVERSE; | |
1680 | reverse_count = code; | |
1681 | *code++ = 0; | |
1682 | *code++ = 0; | |
1683 | } | |
1684 | ||
1685 | /* Now compile the branch */ | |
1686 | ||
1687 | if (!compile_branch(options,brackets,&code,&ptr,errorptr,&optchanged,cd)) | |
1688 | { | { |
1689 | *ptrptr = ptr; | *ptrptr = ptr; |
1690 | return FALSE; | return FALSE; |
# | Line 1331 for (;;) | Line 1696 for (;;) |
1696 | last_branch[1] = length >> 8; | last_branch[1] = length >> 8; |
1697 | last_branch[2] = length & 255; | last_branch[2] = length & 255; |
1698 | ||
1699 | /* If lookbehind, check that this branch matches a fixed-length string, | |
1700 | and put the length into the OP_REVERSE item. Temporarily mark the end of | |
1701 | the branch with OP_END. */ | |
1702 | ||
1703 | if (lookbehind) | |
1704 | { | |
1705 | *code = OP_END; | |
1706 | length = find_fixedlength(last_branch); | |
1707 | DPRINTF(("fixed length = %d\n", length)); | |
1708 | if (length < 0) | |
1709 | { | |
1710 | *errorptr = ERR25; | |
1711 | *ptrptr = ptr; | |
1712 | return FALSE; | |
1713 | } | |
1714 | reverse_count[0] = (length >> 8); | |
1715 | reverse_count[1] = length & 255; | |
1716 | } | |
1717 | ||
1718 | /* Reached end of expression, either ')' or end of pattern. Insert a | /* Reached end of expression, either ')' or end of pattern. Insert a |
1719 | terminating ket and the length of the whole bracketed item, and return, | terminating ket and the length of the whole bracketed item, and return, |
1720 | leaving the pointer at the terminating char. */ | leaving the pointer at the terminating char. If any of the ims options |
1721 | were changed inside the group, compile a resetting op-code following. */ | |
1722 | ||
1723 | if (*ptr != '|') | if (*ptr != '|') |
1724 | { | { |
# | Line 1341 for (;;) | Line 1726 for (;;) |
1726 | *code++ = OP_KET; | *code++ = OP_KET; |
1727 | *code++ = length >> 8; | *code++ = length >> 8; |
1728 | *code++ = length & 255; | *code++ = length & 255; |
1729 | if (optchanged >= 0) | |
1730 | { | |
1731 | *code++ = OP_OPT; | |
1732 | *code++ = oldoptions; | |
1733 | } | |
1734 | *codeptr = code; | *codeptr = code; |
1735 | *ptrptr = ptr; | *ptrptr = ptr; |
1736 | return TRUE; | return TRUE; |
# | Line 1349 for (;;) | Line 1739 for (;;) |
1739 | /* Another branch follows; insert an "or" node and advance the pointer. */ | /* Another branch follows; insert an "or" node and advance the pointer. */ |
1740 | ||
1741 | *code = OP_ALT; | *code = OP_ALT; |
1742 | last_branch = code; | |
1743 | code += 3; | |
1744 | ptr++; | ptr++; |
1745 | } | } |
1746 | /* Control never reaches here */ | /* Control never reaches here */ |
# | Line 1356 for (;;) | Line 1748 for (;;) |
1748 | ||
1749 | ||
1750 | ||
1751 | ||
1752 | /************************************************* | |
1753 | * Find first significant op code * | |
1754 | *************************************************/ | |
1755 | ||
1756 | /* This is called by several functions that scan a compiled expression looking | |
1757 | for a fixed first character, or an anchoring op code etc. It skips over things | |
1758 | that do not influence this. For one application, a change of caseless option is | |
1759 | important. | |
1760 | ||
1761 | Arguments: | |
1762 | code pointer to the start of the group | |
1763 | options pointer to external options | |
1764 | optbit the option bit whose changing is significant, or | |
1765 | zero if none are | |
1766 | optstop TRUE to return on option change, otherwise change the options | |
1767 | value and continue | |
1768 | ||
1769 | Returns: pointer to the first significant opcode | |
1770 | */ | |
1771 | ||
1772 | static const uschar* | |
1773 | first_significant_code(const uschar *code, int *options, int optbit, | |
1774 | BOOL optstop) | |
1775 | { | |
1776 | for (;;) | |
1777 | { | |
1778 | switch ((int)*code) | |
1779 | { | |
1780 | case OP_OPT: | |
1781 | if (optbit > 0 && ((int)code[1] & optbit) != (*options & optbit)) | |
1782 | { | |
1783 | if (optstop) return code; | |
1784 | *options = (int)code[1]; | |
1785 | } | |
1786 | code += 2; | |
1787 | break; | |
1788 | ||
1789 | case OP_CREF: | |
1790 | code += 2; | |
1791 | break; | |
1792 | ||
1793 | case OP_ASSERT_NOT: | |
1794 | case OP_ASSERTBACK: | |
1795 | case OP_ASSERTBACK_NOT: | |
1796 | do code += (code[1] << 8) + code[2]; while (*code == OP_ALT); | |
1797 | code += 3; | |
1798 | break; | |
1799 | ||
1800 | default: | |
1801 | return code; | |
1802 | } | |
1803 | } | |
1804 | /* Control never reaches here */ | |
1805 | } | |
1806 | ||
1807 | ||
1808 | ||
1809 | ||
1810 | /************************************************* | /************************************************* |
1811 | * Check for anchored expression * | * Check for anchored expression * |
1812 | *************************************************/ | *************************************************/ |
# | Line 1370 A branch is also implicitly anchored if | Line 1821 A branch is also implicitly anchored if |
1821 | the rest of the pattern at all possible matching points, so there is no point | the rest of the pattern at all possible matching points, so there is no point |
1822 | trying them again. | trying them again. |
1823 | ||
1824 | Argument: points to start of expression (the bracket) | Arguments: |
1825 | Returns: TRUE or FALSE | code points to start of expression (the bracket) |
1826 | options points to the options setting | |
1827 | ||
1828 | Returns: TRUE or FALSE | |
1829 | */ | */ |
1830 | ||
1831 | static BOOL | static BOOL |
1832 | is_anchored(register const uschar *code, BOOL multiline) | is_anchored(register const uschar *code, int *options) |
1833 | { | { |
1834 | do { | do { |
1835 | int op = (int)code[3]; | const uschar *scode = first_significant_code(code + 3, options, |
1836 | if (op >= OP_BRA || op == OP_ASSERT || op == OP_ONCE) | PCRE_MULTILINE, FALSE); |
1837 | { if (!is_anchored(code+3, multiline)) return FALSE; } | register int op = *scode; |
1838 | if (op >= OP_BRA || op == OP_ASSERT || op == OP_ONCE || op == OP_COND) | |
1839 | { if (!is_anchored(scode, options)) return FALSE; } | |
1840 | else if (op == OP_TYPESTAR || op == OP_TYPEMINSTAR) | else if (op == OP_TYPESTAR || op == OP_TYPEMINSTAR) |
1841 | { if (code[4] != OP_ANY) return FALSE; } | { if (scode[1] != OP_ANY) return FALSE; } |
1842 | else if (op != OP_SOD && (multiline || op != OP_CIRC)) return FALSE; | else if (op != OP_SOD && |
1843 | ((*options & PCRE_MULTILINE) != 0 || op != OP_CIRC)) | |
1844 | return FALSE; | |
1845 | code += (code[1] << 8) + code[2]; | code += (code[1] << 8) + code[2]; |
1846 | } | } |
1847 | while (*code == OP_ALT); | while (*code == OP_ALT); |
# | Line 1407 static BOOL | Line 1865 static BOOL |
1865 | is_startline(const uschar *code) | is_startline(const uschar *code) |
1866 | { | { |
1867 | do { | do { |
1868 | if ((int)code[3] >= OP_BRA || code[3] == OP_ASSERT) | const uschar *scode = first_significant_code(code + 3, NULL, 0, FALSE); |
1869 | { if (!is_startline(code+3)) return FALSE; } | register int op = *scode; |
1870 | else if (code[3] != OP_CIRC) return FALSE; | if (op >= OP_BRA || op == OP_ASSERT || op == OP_ONCE || op == OP_COND) |
1871 | { if (!is_startline(scode)) return FALSE; } | |
1872 | else if (op != OP_CIRC) return FALSE; | |
1873 | code += (code[1] << 8) + code[2]; | code += (code[1] << 8) + code[2]; |
1874 | } | } |
1875 | while (*code == OP_ALT); | while (*code == OP_ALT); |
# | Line 1428 Consider each alternative branch. If the | Line 1888 Consider each alternative branch. If the |
1888 | a bracket all of whose alternatives start with the same char (recurse ad lib), | a bracket all of whose alternatives start with the same char (recurse ad lib), |
1889 | then we return that char, otherwise -1. | then we return that char, otherwise -1. |
1890 | ||
1891 | Argument: points to start of expression (the bracket) | Arguments: |
1892 | Returns: -1 or the fixed first char | code points to start of expression (the bracket) |
1893 | options pointer to the options (used to check casing changes) | |
1894 | ||
1895 | Returns: -1 or the fixed first char | |
1896 | */ | */ |
1897 | ||
1898 | static int | static int |
1899 | find_firstchar(uschar *code) | find_firstchar(const uschar *code, int *options) |
1900 | { | { |
1901 | register int c = -1; | register int c = -1; |
1902 | do | do { |
1903 | { | int d; |
1904 | register int charoffset = 4; | const uschar *scode = first_significant_code(code + 3, options, |
1905 | PCRE_CASELESS, TRUE); | |
1906 | if ((int)code[3] >= OP_BRA || code[3] == OP_ASSERT) | register int op = *scode; |
1907 | { | |
1908 | register int d; | if (op >= OP_BRA) op = OP_BRA; |
1909 | if ((d = find_firstchar(code+3)) < 0) return -1; | |
1910 | if (c < 0) c = d; else if (c != d) return -1; | switch(op) |
1911 | } | { |
1912 | default: | |
1913 | else switch(code[3]) | return -1; |
1914 | { | |
1915 | default: | case OP_BRA: |
1916 | return -1; | case OP_ASSERT: |
1917 | case OP_ONCE: | |
1918 | case OP_EXACT: /* Fall through */ | case OP_COND: |
1919 | charoffset++; | if ((d = find_firstchar(scode, options)) < 0) return -1; |
1920 | if (c < 0) c = d; else if (c != d) return -1; | |
1921 | case OP_CHARS: /* Fall through */ | break; |
1922 | charoffset++; | |
1923 | case OP_EXACT: /* Fall through */ | |
1924 | scode++; | |
1925 | ||
1926 | case OP_CHARS: /* Fall through */ | |
1927 | scode++; | |
1928 | ||
1929 | case OP_PLUS: | |
1930 | case OP_MINPLUS: | |
1931 | if (c < 0) c = scode[1]; else if (c != scode[1]) return -1; | |
1932 | break; | |
1933 | } | |
1934 | ||
1935 | case OP_PLUS: | code += (code[1] << 8) + code[2]; |
1936 | case OP_MINPLUS: | } |
if (c < 0) c = code[charoffset]; else if (c != code[charoffset]) return -1; | ||
break; | ||
} | ||
code += (code[1] << 8) + code[2]; | ||
} | ||
1937 | while (*code == OP_ALT); | while (*code == OP_ALT); |
1938 | return c; | return c; |
1939 | } | } |
1940 | ||
1941 | ||
1942 | ||
1943 | ||
1944 | ||
1945 | /************************************************* | /************************************************* |
1946 | * Compile a Regular Expression * | * Compile a Regular Expression * |
1947 | *************************************************/ | *************************************************/ |
# | Line 1483 Arguments: | Line 1954 Arguments: |
1954 | options various option bits | options various option bits |
1955 | errorptr pointer to pointer to error text | errorptr pointer to pointer to error text |
1956 | erroroffset ptr offset in pattern where error was detected | erroroffset ptr offset in pattern where error was detected |
1957 | tables pointer to character tables or NULL | |
1958 | ||
1959 | Returns: pointer to compiled data block, or NULL on error, | Returns: pointer to compiled data block, or NULL on error, |
1960 | with errorptr and erroroffset set | with errorptr and erroroffset set |
# | Line 1490 Returns: pointer to compiled data | Line 1962 Returns: pointer to compiled data |
1962 | ||
1963 | pcre * | pcre * |
1964 | pcre_compile(const char *pattern, int options, const char **errorptr, | pcre_compile(const char *pattern, int options, const char **errorptr, |
1965 | int *erroroffset) | int *erroroffset, const unsigned char *tables) |
1966 | { | { |
1967 | real_pcre *re; | real_pcre *re; |
int spaces = 0; | ||
1968 | int length = 3; /* For initial BRA plus length */ | int length = 3; /* For initial BRA plus length */ |
1969 | int runlength; | int runlength; |
1970 | int c, size; | int c, size; |
1971 | int bracount = 0; | int bracount = 0; |
int brastack[200]; | ||
1972 | int top_backref = 0; | int top_backref = 0; |
1973 | int branch_extra = 0; | |
1974 | int branch_newextra; | |
1975 | unsigned int brastackptr = 0; | unsigned int brastackptr = 0; |
1976 | uschar *code; | uschar *code; |
1977 | const uschar *ptr; | const uschar *ptr; |
1978 | compile_data compile_block; | |
1979 | int brastack[BRASTACK_SIZE]; | |
1980 | uschar bralenstack[BRASTACK_SIZE]; | |
1981 | ||
1982 | #ifdef DEBUG | #ifdef DEBUG |
1983 | uschar *code_base, *code_end; | uschar *code_base, *code_end; |
# | Line 1529 if ((options & ~PUBLIC_OPTIONS) != 0) | Line 2004 if ((options & ~PUBLIC_OPTIONS) != 0) |
2004 | return NULL; | return NULL; |
2005 | } | } |
2006 | ||
2007 | #ifdef DEBUG | /* Set up pointers to the individual character tables */ |
2008 | printf("------------------------------------------------------------------\n"); | |
2009 | printf("%s\n", pattern); | if (tables == NULL) tables = pcre_default_tables; |
2010 | #endif | compile_block.lcc = tables + lcc_offset; |
2011 | compile_block.fcc = tables + fcc_offset; | |
2012 | compile_block.cbits = tables + cbits_offset; | |
2013 | compile_block.ctypes = tables + ctypes_offset; | |
2014 | ||
2015 | /* Reflect pattern for debugging output */ | |
2016 | ||
2017 | DPRINTF(("------------------------------------------------------------------\n")); | |
2018 | DPRINTF(("%s\n", pattern)); | |
2019 | ||
2020 | /* The first thing to do is to make a pass over the pattern to compute the | /* The first thing to do is to make a pass over the pattern to compute the |
2021 | amount of store required to hold the compiled code. This does not have to be | amount of store required to hold the compiled code. This does not have to be |
# | Line 1547 while ((c = *(++ptr)) != 0) | Line 2030 while ((c = *(++ptr)) != 0) |
2030 | int min, max; | int min, max; |
2031 | int class_charcount; | int class_charcount; |
2032 | ||
2033 | if ((pcre_ctypes[c] & ctype_space) != 0) | if ((options & PCRE_EXTENDED) != 0) |
{ | ||
if ((options & PCRE_EXTENDED) != 0) continue; | ||
spaces++; | ||
} | ||
if (c == '#' && (options & PCRE_EXTENDED) != 0) | ||
2034 | { | { |
2035 | while ((c = *(++ptr)) != 0 && c != '\n'); | if ((compile_block.ctypes[c] & ctype_space) != 0) continue; |
2036 | continue; | if (c == '#') |
2037 | { | |
2038 | while ((c = *(++ptr)) != 0 && c != '\n'); | |
2039 | continue; | |
2040 | } | |
2041 | } | } |
2042 | ||
2043 | switch(c) | switch(c) |
# | Line 1569 while ((c = *(++ptr)) != 0) | Line 2050 while ((c = *(++ptr)) != 0) |
2050 | case '\\': | case '\\': |
2051 | { | { |
2052 | const uschar *save_ptr = ptr; | const uschar *save_ptr = ptr; |
2053 | c = check_escape(&ptr, errorptr, bracount, options, FALSE); | c = check_escape(&ptr, errorptr, bracount, options, FALSE, &compile_block); |
2054 | if (*errorptr != NULL) goto PCRE_ERROR_RETURN; | if (*errorptr != NULL) goto PCRE_ERROR_RETURN; |
2055 | if (c >= 0) | if (c >= 0) |
2056 | { | { |
# | Line 1589 while ((c = *(++ptr)) != 0) | Line 2070 while ((c = *(++ptr)) != 0) |
2070 | int refnum = -c - ESC_REF; | int refnum = -c - ESC_REF; |
2071 | if (refnum > top_backref) top_backref = refnum; | if (refnum > top_backref) top_backref = refnum; |
2072 | length++; /* For single back reference */ | length++; /* For single back reference */ |
2073 | if (ptr[1] == '{' && is_counted_repeat(ptr+2)) | if (ptr[1] == '{' && is_counted_repeat(ptr+2, &compile_block)) |
2074 | { | { |
2075 | ptr = read_repeat_counts(ptr+2, &min, &max, errorptr); | ptr = read_repeat_counts(ptr+2, &min, &max, errorptr, &compile_block); |
2076 | if (*errorptr != NULL) goto PCRE_ERROR_RETURN; | if (*errorptr != NULL) goto PCRE_ERROR_RETURN; |
2077 | if ((min == 0 && (max == 1 || max == -1)) || | if ((min == 0 && (max == 1 || max == -1)) || |
2078 | (min == 1 && max == -1)) | (min == 1 && max == -1)) |
# | Line 1615 while ((c = *(++ptr)) != 0) | Line 2096 while ((c = *(++ptr)) != 0) |
2096 | or back reference. */ | or back reference. */ |
2097 | ||
2098 | case '{': | case '{': |
2099 | if (!is_counted_repeat(ptr+1)) goto NORMAL_CHAR; | if (!is_counted_repeat(ptr+1, &compile_block)) goto NORMAL_CHAR; |
2100 | ptr = read_repeat_counts(ptr+1, &min, &max, errorptr); | ptr = read_repeat_counts(ptr+1, &min, &max, errorptr, &compile_block); |
2101 | if (*errorptr != NULL) goto PCRE_ERROR_RETURN; | if (*errorptr != NULL) goto PCRE_ERROR_RETURN; |
2102 | if ((min == 0 && (max == 1 || max == -1)) || | if ((min == 0 && (max == 1 || max == -1)) || |
2103 | (min == 1 && max == -1)) | (min == 1 && max == -1)) |
# | Line 1630 while ((c = *(++ptr)) != 0) | Line 2111 while ((c = *(++ptr)) != 0) |
2111 | if (ptr[1] == '?') ptr++; | if (ptr[1] == '?') ptr++; |
2112 | continue; | continue; |
2113 | ||
2114 | /* An alternation contains an offset to the next branch or ket. */ | /* An alternation contains an offset to the next branch or ket. If any ims |
2115 | options changed in the previous branch(es), and/or if we are in a | |
2116 | lookbehind assertion, extra space will be needed at the start of the | |
2117 | branch. This is handled by branch_extra. */ | |
2118 | ||
2119 | case '|': | case '|': |
2120 | length += 3; | length += 3 + branch_extra; |
2121 | continue; | continue; |
2122 | ||
2123 | /* A character class uses 33 characters. Don't worry about character types | /* A character class uses 33 characters. Don't worry about character types |
# | Line 1647 while ((c = *(++ptr)) != 0) | Line 2132 while ((c = *(++ptr)) != 0) |
2132 | { | { |
2133 | if (*ptr == '\\') | if (*ptr == '\\') |
2134 | { | { |
2135 | int c = check_escape(&ptr, errorptr, bracount, options, TRUE); | int ch = check_escape(&ptr, errorptr, bracount, options, TRUE, |
2136 | &compile_block); | |
2137 | if (*errorptr != NULL) goto PCRE_ERROR_RETURN; | if (*errorptr != NULL) goto PCRE_ERROR_RETURN; |
2138 | if (-c == ESC_b) class_charcount++; else class_charcount = 10; | if (-ch == ESC_b) class_charcount++; else class_charcount = 10; |
2139 | } | } |
2140 | else class_charcount++; | else class_charcount++; |
2141 | ptr++; | ptr++; |
# | Line 1664 while ((c = *(++ptr)) != 0) | Line 2150 while ((c = *(++ptr)) != 0) |
2150 | ||
2151 | /* A repeat needs either 1 or 5 bytes. */ | /* A repeat needs either 1 or 5 bytes. */ |
2152 | ||
2153 | if (ptr[1] == '{' && is_counted_repeat(ptr+2)) | if (*ptr != 0 && ptr[1] == '{' && is_counted_repeat(ptr+2, &compile_block)) |
2154 | { | { |
2155 | ptr = read_repeat_counts(ptr+2, &min, &max, errorptr); | ptr = read_repeat_counts(ptr+2, &min, &max, errorptr, &compile_block); |
2156 | if (*errorptr != NULL) goto PCRE_ERROR_RETURN; | if (*errorptr != NULL) goto PCRE_ERROR_RETURN; |
2157 | if ((min == 0 && (max == 1 || max == -1)) || | if ((min == 0 && (max == 1 || max == -1)) || |
2158 | (min == 1 && max == -1)) | (min == 1 && max == -1)) |
# | Line 1679 while ((c = *(++ptr)) != 0) | Line 2165 while ((c = *(++ptr)) != 0) |
2165 | ||
2166 | /* Brackets may be genuine groups or special things */ | /* Brackets may be genuine groups or special things */ |
2167 | ||
2168 | case '(': | case '(': |
2169 | branch_newextra = 0; | |
2170 | ||
2171 | /* Handle special forms of bracket, which all start (? */ | |
2172 | ||
2173 | if (ptr[1] == '?') | |
2174 | { | |
2175 | int set, unset; | |
2176 | int *optset; | |
2177 | ||
2178 | switch (c = ptr[2]) | |
2179 | { | |
2180 | /* Skip over comments entirely */ | |
2181 | case '#': | |
2182 | ptr += 3; | |
2183 | while (*ptr != 0 && *ptr != ')') ptr++; | |
2184 | if (*ptr == 0) | |
2185 | { | |
2186 | *errorptr = ERR18; | |
2187 | goto PCRE_ERROR_RETURN; | |
2188 | } | |
2189 | continue; | |
2190 | ||
2191 | /* Non-referencing groups and lookaheads just move the pointer on, and | |
2192 | then behave like a non-special bracket, except that they don't increment | |
2193 | the count of extracting brackets. Ditto for the "once only" bracket, | |
2194 | which is in Perl from version 5.005. */ | |
2195 | ||
2196 | case ':': | |
2197 | case '=': | |
2198 | case '!': | |
2199 | case '>': | |
2200 | ptr += 2; | |
2201 | break; | |
2202 | ||
2203 | /* Lookbehinds are in Perl from version 5.005 */ | |
2204 | ||
2205 | case '<': | |
2206 | if (ptr[3] == '=' || ptr[3] == '!') | |
2207 | { | |
2208 | ptr += 3; | |
2209 | branch_newextra = 3; | |
2210 | length += 3; /* For the first branch */ | |
2211 | break; | |
2212 | } | |
2213 | *errorptr = ERR24; | |
2214 | goto PCRE_ERROR_RETURN; | |
2215 | ||
2216 | /* Conditionals are in Perl from version 5.005. The bracket must either | |
2217 | be followed by a number (for bracket reference) or by an assertion | |
2218 | group. */ | |
2219 | ||
2220 | case '(': | |
2221 | if ((compile_block.ctypes[ptr[3]] & ctype_digit) != 0) | |
2222 | { | |
2223 | ptr += 4; | |
2224 | length += 2; | |
2225 | while ((compile_block.ctypes[*ptr] & ctype_digit) != 0) ptr++; | |
2226 | if (*ptr != ')') | |
2227 | { | |
2228 | *errorptr = ERR26; | |
2229 | goto PCRE_ERROR_RETURN; | |
2230 | } | |
2231 | } | |
2232 | else /* An assertion must follow */ | |
2233 | { | |
2234 | ptr++; /* Can treat like ':' as far as spacing is concerned */ | |
2235 | ||
2236 | if (ptr[2] != '?' || strchr("=!<", ptr[3]) == NULL) | |
2237 | { | |
2238 | ptr += 2; /* To get right offset in message */ | |
2239 | *errorptr = ERR28; | |
2240 | goto PCRE_ERROR_RETURN; | |
2241 | } | |
2242 | } | |
2243 | break; | |
2244 | ||
2245 | /* Else loop checking valid options until ) is met. Anything else is an | |
2246 | error. If we are without any brackets, i.e. at top level, the settings | |
2247 | act as if specified in the options, so massage the options immediately. | |
2248 | This is for backward compatibility with Perl 5.004. */ | |
2249 | ||
2250 | default: | |
2251 | set = unset = 0; | |
2252 | optset = &set; | |
2253 | ptr += 2; | |
2254 | ||
2255 | for (;; ptr++) | |
2256 | { | |
2257 | c = *ptr; | |
2258 | switch (c) | |
2259 | { | |
2260 | case 'i': | |
2261 | *optset |= PCRE_CASELESS; | |
2262 | continue; | |
2263 | ||
2264 | case 'm': | |
2265 | *optset |= PCRE_MULTILINE; | |
2266 | continue; | |
2267 | ||
2268 | case 's': | |
2269 | *optset |= PCRE_DOTALL; | |
2270 | continue; | |
2271 | ||
2272 | case 'x': | |
2273 | *optset |= PCRE_EXTENDED; | |
2274 | continue; | |
2275 | ||
2276 | case 'X': | |
2277 | *optset |= PCRE_EXTRA; | |
2278 | continue; | |
2279 | ||
2280 | case 'U': | |
2281 | *optset |= PCRE_UNGREEDY; | |
2282 | continue; | |
2283 | ||
2284 | /* Handle special forms of bracket, which all start (? */ | case '-': |
2285 | optset = &unset; | |
2286 | continue; | |
2287 | ||
2288 | if (ptr[1] == '?') switch (c = ptr[2]) | /* A termination by ')' indicates an options-setting-only item; |
2289 | { | this is global at top level; otherwise nothing is done here and |
2290 | /* Skip over comments entirely */ | it is handled during the compiling process on a per-bracket-group |
2291 | case '#': | basis. */ |
ptr += 3; | ||
while (*ptr != 0 && *ptr != ')') ptr++; | ||
if (*ptr == 0) | ||
{ | ||
*errorptr = ERR18; | ||
goto PCRE_ERROR_RETURN; | ||
} | ||
continue; | ||
2292 | ||
2293 | /* Non-referencing groups and lookaheads just move the pointer on, and | case ')': |
2294 | then behave like a non-special bracket, except that they don't increment | if (brastackptr == 0) |
2295 | the count of extracting brackets. */ | { |
2296 | options = (options | set) & (~unset); | |
2297 | case ':': | set = unset = 0; /* To save length */ |
2298 | case '=': | } |
2299 | case '!': | /* Fall through */ |
ptr += 2; | ||
break; | ||
2300 | ||
2301 | /* Ditto for the "once only" bracket, allowed only if the extra bit | /* A termination by ':' indicates the start of a nested group with |
2302 | is set. */ | the given options set. This is again handled at compile time, but |
2303 | we must allow for compiled space if any of the ims options are | |
2304 | set. We also have to allow for resetting space at the end of | |
2305 | the group, which is why 4 is added to the length and not just 2. | |
2306 | If there are several changes of options within the same group, this | |
2307 | will lead to an over-estimate on the length, but this shouldn't | |
2308 | matter very much. We also have to allow for resetting options at | |
2309 | the start of any alternations, which we do by setting | |
2310 | branch_newextra to 2. */ | |
2311 | ||
2312 | case '>': | case ':': |
2313 | if ((options & PCRE_EXTRA) != 0) | if (((set|unset) & PCRE_IMS) != 0) |
2314 | { | { |
2315 | ptr += 2; | length += 4; |
2316 | break; | branch_newextra = 2; |
2317 | } | } |
2318 | /* Else fall thourh */ | goto END_OPTIONS; |
2319 | ||
2320 | /* Else loop setting valid options until ) is met. Anything else is an | /* Unrecognized option character */ |
error. */ | ||
2321 | ||
2322 | default: | default: |
2323 | ptr += 2; | *errorptr = ERR12; |
2324 | for (;; ptr++) | goto PCRE_ERROR_RETURN; |
2325 | { | } |
if ((c = *ptr) == 'i') | ||
{ | ||
options |= PCRE_CASELESS; | ||
continue; | ||
} | ||
else if ((c = *ptr) == 'm') | ||
{ | ||
options |= PCRE_MULTILINE; | ||
continue; | ||
} | ||
else if (c == 's') | ||
{ | ||
options |= PCRE_DOTALL; | ||
continue; | ||
2326 | } | } |
2327 | else if (c == 'x') | |
2328 | /* If we hit a closing bracket, that's it - this is a freestanding | |
2329 | option-setting. We need to ensure that branch_extra is updated if | |
2330 | necessary. The only values branch_newextra can have here are 0 or 2. | |
2331 | If the value is 2, then branch_extra must either be 2 or 5, depending | |
2332 | on whether this is a lookbehind group or not. */ | |
2333 | ||
2334 | END_OPTIONS: | |
2335 | if (c == ')') | |
2336 | { | { |
2337 | options |= PCRE_EXTENDED; | if (branch_newextra == 2 && (branch_extra == 0 || branch_extra == 3)) |
2338 | length -= spaces; /* Already counted spaces */ | branch_extra += branch_newextra; |
2339 | continue; | continue; |
2340 | } | } |
else if (c == ')') break; | ||
2341 | ||
2342 | *errorptr = ERR12; | /* If options were terminated by ':' control comes here. Fall through |
2343 | goto PCRE_ERROR_RETURN; | to handle the group below. */ |
2344 | } | } |
continue; /* End of this bracket handling */ | ||
2345 | } | } |
2346 | ||
2347 | /* Extracting brackets must be counted so we can process escapes in a | /* Extracting brackets must be counted so we can process escapes in a |
# | Line 1759 while ((c = *(++ptr)) != 0) | Line 2350 while ((c = *(++ptr)) != 0) |
2350 | else bracount++; | else bracount++; |
2351 | ||
2352 | /* Non-special forms of bracket. Save length for computing whole length | /* Non-special forms of bracket. Save length for computing whole length |
2353 | at end if there's a repeat that requires duplication of the group. */ | at end if there's a repeat that requires duplication of the group. Also |
2354 | save the current value of branch_extra, and start the new group with | |
2355 | the new value. If non-zero, this will either be 2 for a (?imsx: group, or 3 | |
2356 | for a lookbehind assertion. */ | |
2357 | ||
2358 | if (brastackptr >= sizeof(brastack)/sizeof(int)) | if (brastackptr >= sizeof(brastack)/sizeof(int)) |
2359 | { | { |
# | Line 1767 while ((c = *(++ptr)) != 0) | Line 2361 while ((c = *(++ptr)) != 0) |
2361 | goto PCRE_ERROR_RETURN; | goto PCRE_ERROR_RETURN; |
2362 | } | } |
2363 | ||
2364 | bralenstack[brastackptr] = branch_extra; | |
2365 | branch_extra = branch_newextra; | |
2366 | ||
2367 | brastack[brastackptr++] = length; | brastack[brastackptr++] = length; |
2368 | length += 3; | length += 3; |
2369 | continue; | continue; |
2370 | ||
2371 | /* Handle ket. Look for subsequent max/min; for certain sets of values we | /* Handle ket. Look for subsequent max/min; for certain sets of values we |
2372 | have to replicate this bracket up to that many times. */ | have to replicate this bracket up to that many times. If brastackptr is |
2373 | 0 this is an unmatched bracket which will generate an error, but take care | |
2374 | not to try to access brastack[-1] when computing the length and restoring | |
2375 | the branch_extra value. */ | |
2376 | ||
2377 | case ')': | case ')': |
2378 | length += 3; | length += 3; |
2379 | { | { |
2380 | int min = 1; | int minval = 1; |
2381 | int max = 1; | int maxval = 1; |
2382 | int duplength = length - brastack[--brastackptr]; | int duplength; |
2383 | ||
2384 | if (brastackptr > 0) | |
2385 | { | |
2386 | duplength = length - brastack[--brastackptr]; | |
2387 | branch_extra = bralenstack[brastackptr]; | |
2388 | } | |
2389 | else duplength = 0; | |
2390 | ||
2391 | /* Leave ptr at the final char; for read_repeat_counts this happens | /* Leave ptr at the final char; for read_repeat_counts this happens |
2392 | automatically; for the others we need an increment. */ | automatically; for the others we need an increment. */ |
2393 | ||
2394 | if ((c = ptr[1]) == '{' && is_counted_repeat(ptr+2)) | if ((c = ptr[1]) == '{' && is_counted_repeat(ptr+2, &compile_block)) |
2395 | { | { |
2396 | ptr = read_repeat_counts(ptr+2, &min, &max, errorptr); | ptr = read_repeat_counts(ptr+2, &minval, &maxval, errorptr, |
2397 | &compile_block); | |
2398 | if (*errorptr != NULL) goto PCRE_ERROR_RETURN; | if (*errorptr != NULL) goto PCRE_ERROR_RETURN; |
2399 | } | } |
2400 | else if (c == '*') { min = 0; max = -1; ptr++; } | else if (c == '*') { minval = 0; maxval = -1; ptr++; } |
2401 | else if (c == '+') { max = -1; ptr++; } | else if (c == '+') { maxval = -1; ptr++; } |
2402 | else if (c == '?') { min = 0; ptr++; } | else if (c == '?') { minval = 0; ptr++; } |
2403 | ||
2404 | /* If there is a minimum > 1 we have to replicate up to min-1 times; if | /* If the minimum is zero, we have to allow for an OP_BRAZERO before the |
2405 | there is a limited maximum we have to replicate up to max-1 times and | group, and if the maximum is greater than zero, we have to replicate |
2406 | allow for a BRAZERO item before each optional copy, as we also have to | maxval-1 times; each replication acquires an OP_BRAZERO plus a nesting |
2407 | do before the first copy if the minimum is zero. */ | bracket set - hence the 7. */ |
2408 | ||
2409 | if (min == 0) length++; | if (minval == 0) |
2410 | else if (min > 1) length += (min - 1) * duplength; | { |
2411 | if (max > min) length += (max - min) * (duplength + 1); | length++; |
2412 | } | if (maxval > 0) length += (maxval - 1) * (duplength + 7); |
2413 | } | |
2414 | ||
2415 | /* When the minimum is greater than zero, 1 we have to replicate up to | |
2416 | minval-1 times, with no additions required in the copies. Then, if | |
2417 | there is a limited maximum we have to replicate up to maxval-1 times | |
2418 | allowing for a BRAZERO item before each optional copy and nesting | |
2419 | brackets for all but one of the optional copies. */ | |
2420 | ||
2421 | else | |
2422 | { | |
2423 | length += (minval - 1) * duplength; | |
2424 | if (maxval > minval) /* Need this test as maxval=-1 means no limit */ | |
2425 | length += (maxval - minval) * (duplength + 7) - 6; | |
2426 | } | |
2427 | } | |
2428 | continue; | continue; |
2429 | ||
2430 | /* Non-special character. For a run of such characters the length required | /* Non-special character. For a run of such characters the length required |
# | Line 1816 while ((c = *(++ptr)) != 0) | Line 2438 while ((c = *(++ptr)) != 0) |
2438 | runlength = 0; | runlength = 0; |
2439 | do | do |
2440 | { | { |
2441 | if ((pcre_ctypes[c] & ctype_space) != 0) | if ((options & PCRE_EXTENDED) != 0) |
{ | ||
if ((options & PCRE_EXTENDED) != 0) continue; | ||
spaces++; | ||
} | ||
if (c == '#' && (options & PCRE_EXTENDED) != 0) | ||
2442 | { | { |
2443 | while ((c = *(++ptr)) != 0 && c != '\n'); | if ((compile_block.ctypes[c] & ctype_space) != 0) continue; |
2444 | continue; | if (c == '#') |
2445 | { | |
2446 | while ((c = *(++ptr)) != 0 && c != '\n'); | |
2447 | continue; | |
2448 | } | |
2449 | } | } |
2450 | ||
2451 | /* Backslash may introduce a data char or a metacharacter; stop the | /* Backslash may introduce a data char or a metacharacter; stop the |
# | Line 1834 while ((c = *(++ptr)) != 0) | Line 2454 while ((c = *(++ptr)) != 0) |
2454 | if (c == '\\') | if (c == '\\') |
2455 | { | { |
2456 | const uschar *saveptr = ptr; | const uschar *saveptr = ptr; |
2457 | c = check_escape(&ptr, errorptr, bracount, options, FALSE); | c = check_escape(&ptr, errorptr, bracount, options, FALSE, |
2458 | &compile_block); | |
2459 | if (*errorptr != NULL) goto PCRE_ERROR_RETURN; | if (*errorptr != NULL) goto PCRE_ERROR_RETURN; |
2460 | if (c < 0) { ptr = saveptr; break; } | if (c < 0) { ptr = saveptr; break; } |
2461 | } | } |
# | Line 1846 while ((c = *(++ptr)) != 0) | Line 2467 while ((c = *(++ptr)) != 0) |
2467 | ||
2468 | /* This "while" is the end of the "do" above. */ | /* This "while" is the end of the "do" above. */ |
2469 | ||
2470 | while (runlength < 255 && (pcre_ctypes[c = *(++ptr)] & ctype_meta) == 0); | while (runlength < 255 && |
2471 | (compile_block.ctypes[c = *(++ptr)] & ctype_meta) == 0); | |
2472 | ||
2473 | ptr--; | ptr--; |
2474 | length += runlength; | length += runlength; |
# | Line 1863 if (length > 65539) | Line 2485 if (length > 65539) |
2485 | } | } |
2486 | ||
2487 | /* Compute the size of data block needed and get it, either from malloc or | /* Compute the size of data block needed and get it, either from malloc or |
2488 | externally provided function. Put in the magic number and the options. */ | externally provided function. We specify "code[0]" in the offsetof() expression |
2489 | rather than just "code", because it has been reported that one broken compiler | |
2490 | fails on "code" because it is also an independent variable. It should make no | |
2491 | difference to the value of the offsetof(). */ | |
2492 | ||
2493 | size = length + offsetof(real_pcre, code); | size = length + offsetof(real_pcre, code[0]); |
2494 | re = (real_pcre *)(pcre_malloc)(size); | re = (real_pcre *)(pcre_malloc)(size); |
2495 | ||
2496 | if (re == NULL) | if (re == NULL) |
# | Line 1874 if (re == NULL) | Line 2499 if (re == NULL) |
2499 | return NULL; | return NULL; |
2500 | } | } |
2501 | ||
2502 | /* Put in the magic number and the options. */ | |
2503 | ||
2504 | re->magic_number = MAGIC_NUMBER; | re->magic_number = MAGIC_NUMBER; |
2505 | re->options = options; | re->options = options; |
2506 | re->tables = tables; | |
2507 | ||
2508 | /* Set up a starting, non-extracting bracket, then compile the expression. On | /* Set up a starting, non-extracting bracket, then compile the expression. On |
2509 | error, *errorptr will be set non-NULL, so we don't need to look at the result | error, *errorptr will be set non-NULL, so we don't need to look at the result |
# | Line 1885 ptr = (const uschar *)pattern; | Line 2513 ptr = (const uschar *)pattern; |
2513 | code = re->code; | code = re->code; |
2514 | *code = OP_BRA; | *code = OP_BRA; |
2515 | bracount = 0; | bracount = 0; |
2516 | (void)compile_regex(options, &bracount, &code, &ptr, errorptr); | (void)compile_regex(options, -1, &bracount, &code, &ptr, errorptr, FALSE, -1, |
2517 | &compile_block); | |
2518 | re->top_bracket = bracount; | re->top_bracket = bracount; |
2519 | re->top_backref = top_backref; | re->top_backref = top_backref; |
2520 | ||
# | Line 1902 if debugging, leave the test till after | Line 2531 if debugging, leave the test till after |
2531 | if (code - re->code > length) *errorptr = ERR23; | if (code - re->code > length) *errorptr = ERR23; |
2532 | #endif | #endif |
2533 | ||
2534 | /* Give an error if there's back reference to a non-existent capturing | |
2535 | subpattern. */ | |
2536 | ||
2537 | if (top_backref > re->top_bracket) *errorptr = ERR15; | |
2538 | ||
2539 | /* Failed to compile */ | /* Failed to compile */ |
2540 | ||
2541 | if (*errorptr != NULL) | if (*errorptr != NULL) |
# | Line 1920 to set the PCRE_STARTLINE flag if all br | Line 2554 to set the PCRE_STARTLINE flag if all br |
2554 | ||
2555 | if ((options & PCRE_ANCHORED) == 0) | if ((options & PCRE_ANCHORED) == 0) |
2556 | { | { |
2557 | if (is_anchored(re->code, (options & PCRE_MULTILINE) != 0)) | int temp_options = options; |
2558 | if (is_anchored(re->code, &temp_options)) | |
2559 | re->options |= PCRE_ANCHORED; | re->options |= PCRE_ANCHORED; |
2560 | else | else |
2561 | { | { |
2562 | int c = find_firstchar(re->code); | int ch = find_firstchar(re->code, &temp_options); |
2563 | if (c >= 0) | if (ch >= 0) |
2564 | { | { |
2565 | re->first_char = c; | re->first_char = ch; |
2566 | re->options |= PCRE_FIRSTSET; | re->options |= PCRE_FIRSTSET; |
2567 | } | } |
2568 | else if (is_startline(re->code)) | else if (is_startline(re->code)) |
# | Line 1939 if ((options & PCRE_ANCHORED) == 0) | Line 2574 if ((options & PCRE_ANCHORED) == 0) |
2574 | ||
2575 | #ifdef DEBUG | #ifdef DEBUG |
2576 | ||
2577 | printf("Length = %d top_bracket = %d top_backref=%d\n", | printf("Length = %d top_bracket = %d top_backref = %d\n", |
2578 | length, re->top_bracket, re->top_backref); | length, re->top_bracket, re->top_backref); |
2579 | ||
2580 | if (re->options != 0) | if (re->options != 0) |
2581 | { | { |
2582 | printf("%s%s%s%s%s%s%s\n", | printf("%s%s%s%s%s%s%s%s\n", |
2583 | ((re->options & PCRE_ANCHORED) != 0)? "anchored " : "", | ((re->options & PCRE_ANCHORED) != 0)? "anchored " : "", |
2584 | ((re->options & PCRE_CASELESS) != 0)? "caseless " : "", | ((re->options & PCRE_CASELESS) != 0)? "caseless " : "", |
2585 | ((re->options & PCRE_EXTENDED) != 0)? "extended " : "", | ((re->options & PCRE_EXTENDED) != 0)? "extended " : "", |
2586 | ((re->options & PCRE_MULTILINE) != 0)? "multiline " : "", | ((re->options & PCRE_MULTILINE) != 0)? "multiline " : "", |
2587 | ((re->options & PCRE_DOTALL) != 0)? "dotall " : "", | ((re->options & PCRE_DOTALL) != 0)? "dotall " : "", |
2588 | ((re->options & PCRE_DOLLAR_ENDONLY) != 0)? "endonly " : "", | ((re->options & PCRE_DOLLAR_ENDONLY) != 0)? "endonly " : "", |
2589 | ((re->options & PCRE_EXTRA) != 0)? "extra " : ""); | ((re->options & PCRE_EXTRA) != 0)? "extra " : "", |
2590 | ((re->options & PCRE_UNGREEDY) != 0)? "ungreedy " : ""); | |
2591 | } | } |
2592 | ||
2593 | if ((re->options & PCRE_FIRSTSET) != 0) | if ((re->options & PCRE_FIRSTSET) != 0) |
# | Line 1977 while (code < code_end) | Line 2613 while (code < code_end) |
2613 | ||
2614 | else switch(*code) | else switch(*code) |
2615 | { | { |
2616 | case OP_OPT: | |
2617 | printf(" %.2x %s", code[1], OP_names[*code]); | |
2618 | code++; | |
2619 | break; | |
2620 | ||
2621 | case OP_COND: | |
2622 | printf("%3d Cond", (code[1] << 8) + code[2]); | |
2623 | code += 2; | |
2624 | break; | |
2625 | ||
2626 | case OP_CREF: | |
2627 | printf(" %.2d %s", code[1], OP_names[*code]); | |
2628 | code++; | |
2629 | break; | |
2630 | ||
2631 | case OP_CHARS: | case OP_CHARS: |
2632 | charlength = *(++code); | charlength = *(++code); |
2633 | printf("%3d ", charlength); | printf("%3d ", charlength); |
# | Line 1990 while (code < code_end) | Line 2641 while (code < code_end) |
2641 | case OP_KET: | case OP_KET: |
2642 | case OP_ASSERT: | case OP_ASSERT: |
2643 | case OP_ASSERT_NOT: | case OP_ASSERT_NOT: |
2644 | case OP_ASSERTBACK: | |
2645 | case OP_ASSERTBACK_NOT: | |
2646 | case OP_ONCE: | case OP_ONCE: |
2647 | printf("%3d %s", (code[1] << 8) + code[2], OP_names[*code]); | printf("%3d %s", (code[1] << 8) + code[2], OP_names[*code]); |
2648 | code += 2; | code += 2; |
2649 | break; | break; |
2650 | ||
2651 | case OP_REVERSE: | |
2652 | printf("%3d %s", (code[1] << 8) + code[2], OP_names[*code]); | |
2653 | code += 2; | |
2654 | break; | |
2655 | ||
2656 | case OP_STAR: | case OP_STAR: |
2657 | case OP_MINSTAR: | case OP_MINSTAR: |
2658 | case OP_PLUS: | case OP_PLUS: |
# | Line 2019 while (code < code_end) | Line 2677 while (code < code_end) |
2677 | case OP_MINUPTO: | case OP_MINUPTO: |
2678 | if (isprint(c = code[3])) printf(" %c{", c); | if (isprint(c = code[3])) printf(" %c{", c); |
2679 | else printf(" \\x%02x{", c); | else printf(" \\x%02x{", c); |
2680 | if (*code != OP_EXACT) printf(","); | if (*code != OP_EXACT) printf("0,"); |
2681 | printf("%d}", (code[1] << 8) + code[2]); | printf("%d}", (code[1] << 8) + code[2]); |
2682 | if (*code == OP_MINUPTO) printf("?"); | if (*code == OP_MINUPTO) printf("?"); |
2683 | code += 3; | code += 3; |
# | Line 2064 while (code < code_end) | Line 2722 while (code < code_end) |
2722 | ||
2723 | case OP_REF: | case OP_REF: |
2724 | printf(" \\%d", *(++code)); | printf(" \\%d", *(++code)); |
2725 | break; | code ++; |
2726 | goto CLASS_REF_REPEAT; | |
2727 | ||
2728 | case OP_CLASS: | case OP_CLASS: |
2729 | { | { |
2730 | int i, min, max; | int i, min, max; |
2731 | code++; | code++; |
2732 | printf(" ["); | printf(" ["); |
2733 | ||
# | Line 2094 while (code < code_end) | Line 2752 while (code < code_end) |
2752 | printf("]"); | printf("]"); |
2753 | code += 32; | code += 32; |
2754 | ||
2755 | CLASS_REF_REPEAT: | |
2756 | ||
2757 | switch(*code) | switch(*code) |
2758 | { | { |
2759 | case OP_CRSTAR: | case OP_CRSTAR: |
# | Line 2151 return (pcre *)re; | Line 2811 return (pcre *)re; |
2811 | ||
2812 | ||
2813 | /************************************************* | /************************************************* |
* Match a character type * | ||
*************************************************/ | ||
/* Not used in all the places it might be as it's sometimes faster | ||
to put the code inline. | ||
Arguments: | ||
type the character type | ||
c the character | ||
dotall the dotall flag | ||
Returns: TRUE if character is of the type | ||
*/ | ||
static BOOL | ||
match_type(int type, int c, BOOL dotall) | ||
{ | ||
#ifdef DEBUG | ||
if (isprint(c)) printf("matching subject %c against ", c); | ||
else printf("matching subject \\x%02x against ", c); | ||
printf("%s\n", OP_names[type]); | ||
#endif | ||
switch(type) | ||
{ | ||
case OP_ANY: return dotall || c != '\n'; | ||
case OP_NOT_DIGIT: return (pcre_ctypes[c] & ctype_digit) == 0; | ||
case OP_DIGIT: return (pcre_ctypes[c] & ctype_digit) != 0; | ||
case OP_NOT_WHITESPACE: return (pcre_ctypes[c] & ctype_space) == 0; | ||
case OP_WHITESPACE: return (pcre_ctypes[c] & ctype_space) != 0; | ||
case OP_NOT_WORDCHAR: return (pcre_ctypes[c] & ctype_word) == 0; | ||
case OP_WORDCHAR: return (pcre_ctypes[c] & ctype_word) != 0; | ||
} | ||
return FALSE; | ||
} | ||
/************************************************* | ||
2814 | * Match a back-reference * | * Match a back-reference * |
2815 | *************************************************/ | *************************************************/ |
2816 | ||
2817 | /* If a back reference hasn't been set, the match fails. | /* If a back reference hasn't been set, the length that is passed is greater |
2818 | than the number of characters left in the string, so the match fails. | |
2819 | ||
2820 | Arguments: | Arguments: |
2821 | number reference number | offset index into the offset vector |
2822 | eptr points into the subject | eptr points into the subject |
2823 | length length to be matched | length length to be matched |
2824 | md points to match data block | md points to match data block |
2825 | ims the ims flags | |
2826 | ||
2827 | Returns: TRUE if matched | Returns: TRUE if matched |
2828 | */ | */ |
2829 | ||
2830 | static BOOL | static BOOL |
2831 | match_ref(int number, register const uschar *eptr, int length, match_data *md) | match_ref(int offset, register const uschar *eptr, int length, match_data *md, |
2832 | int ims) | |
2833 | { | { |
2834 | const uschar *p = md->start_subject + md->offset_vector[number]; | const uschar *p = md->start_subject + md->offset_vector[offset]; |
2835 | ||
2836 | #ifdef DEBUG | #ifdef DEBUG |
2837 | if (eptr >= md->end_subject) | if (eptr >= md->end_subject) |
# | Line 2225 printf("\n"); | Line 2848 printf("\n"); |
2848 | ||
2849 | /* Always fail if not enough characters left */ | /* Always fail if not enough characters left */ |
2850 | ||
2851 | if (length > md->end_subject - p) return FALSE; | if (length > md->end_subject - eptr) return FALSE; |
2852 | ||
2853 | /* Separate the caselesss case for speed */ | /* Separate the caselesss case for speed */ |
2854 | ||
2855 | if (md->caseless) | if ((ims & PCRE_CASELESS) != 0) |
2856 | { while (length-- > 0) if (pcre_lcc[*p++] != pcre_lcc[*eptr++]) return FALSE; } | { |
2857 | while (length-- > 0) | |
2858 | if (md->lcc[*p++] != md->lcc[*eptr++]) return FALSE; | |
2859 | } | |
2860 | else | else |
2861 | { while (length-- > 0) if (*p++ != *eptr++) return FALSE; } | { while (length-- > 0) if (*p++ != *eptr++) return FALSE; } |
2862 | ||
# | Line 2243 return TRUE; | Line 2869 return TRUE; |
2869 | * Match from current position * | * Match from current position * |
2870 | *************************************************/ | *************************************************/ |
2871 | ||
2872 | /* On entry ecode points to the first opcode, and eptr to the first character. | /* On entry ecode points to the first opcode, and eptr to the first character |
2873 | in the subject string, while eptrb holds the value of eptr at the start of the | |
2874 | last bracketed group - used for breaking infinite loops matching zero-length | |
2875 | strings. | |
2876 | ||
2877 | Arguments: | Arguments: |
2878 | eptr pointer in subject | eptr pointer in subject |
2879 | ecode position in code | ecode position in code |
2880 | offset_top current top pointer | offset_top current top pointer |
2881 | md pointer to "static" info for the match | md pointer to "static" info for the match |
2882 | ims current /i, /m, and /s options | |
2883 | condassert TRUE if called to check a condition assertion | |
2884 | eptrb eptr at start of last bracket | |
2885 | ||
2886 | Returns: TRUE if matched | Returns: TRUE if matched |
2887 | */ | */ |
2888 | ||
2889 | static BOOL | static BOOL |
2890 | match(register const uschar *eptr, register const uschar *ecode, int offset_top, | match(register const uschar *eptr, register const uschar *ecode, |
2891 | match_data *md) | int offset_top, match_data *md, int ims, BOOL condassert, const uschar *eptrb) |
2892 | { | { |
2893 | int original_ims = ims; /* Save for resetting on ')' */ | |
2894 | ||
2895 | for (;;) | for (;;) |
2896 | { | { |
2897 | int op = (int)*ecode; | |
2898 | int min, max, ctype; | int min, max, ctype; |
2899 | register int i; | register int i; |
2900 | register int c; | register int c; |
2901 | BOOL minimize = FALSE; | BOOL minimize = FALSE; |
2902 | ||
2903 | /* Opening bracket. Check the alternative branches in turn, failing if none | /* Opening capturing bracket. If there is space in the offset vector, save |
2904 | match. We have to set the start offset if required and there is space | the current subject position in the working slot at the top of the vector. We |
2905 | in the offset vector so that it is available for subsequent back references | mustn't change the current values of the data slot, because they may be set |
2906 | if the bracket matches. However, if the bracket fails, we must put back the | from a previous iteration of this group, and be referred to by a reference |
2907 | previous value of both offsets in case they were set by a previous copy of | inside the group. |
2908 | the same bracket. Don't worry about setting the flag for the error case here; | |
2909 | that is handled in the code for KET. */ | If the bracket fails to match, we need to restore this value and also the |
2910 | values of the final offsets, in case they were set by a previous iteration of | |
2911 | the same bracket. | |
2912 | ||
2913 | If there isn't enough space in the offset vector, treat this as if it were a | |
2914 | non-capturing bracket. Don't worry about setting the flag for the error case | |
2915 | here; that is handled in the code for KET. */ | |
2916 | ||
2917 | if ((int)*ecode >= OP_BRA) | if (op > OP_BRA) |
2918 | { | { |
2919 | int number = (*ecode - OP_BRA) << 1; | int number = op - OP_BRA; |
2920 | int save_offset1 = 0, save_offset2 = 0; | int offset = number << 1; |
2921 | ||
2922 | #ifdef DEBUG | #ifdef DEBUG |
2923 | printf("start bracket %d\n", number/2); | printf("start bracket %d subject=", number); |
2924 | #endif | pchars(eptr, 16, TRUE, md); |
2925 | printf("\n"); | |
2926 | #endif | |
2927 | ||
2928 | if (number > 0 && number < md->offset_end) | if (offset < md->offset_max) |
2929 | { | { |
2930 | save_offset1 = md->offset_vector[number]; | int save_offset1 = md->offset_vector[offset]; |
2931 | save_offset2 = md->offset_vector[number+1]; | int save_offset2 = md->offset_vector[offset+1]; |
2932 | md->offset_vector[number] = eptr - md->start_subject; | int save_offset3 = md->offset_vector[md->offset_end - number]; |
2933 | ||
2934 | DPRINTF(("saving %d %d %d\n", save_offset1, save_offset2, save_offset3)); | |
2935 | md->offset_vector[md->offset_end - number] = eptr - md->start_subject; | |
2936 | ||
2937 | do | |
2938 | { | |
2939 | if (match(eptr, ecode+3, offset_top, md, ims, FALSE, eptr)) return TRUE; | |
2940 | ecode += (ecode[1] << 8) + ecode[2]; | |
2941 | } | |
2942 | while (*ecode == OP_ALT); | |
2943 | ||
2944 | #ifdef DEBUG | DPRINTF(("bracket %d failed\n", number)); |
2945 | printf("saving %d %d\n", save_offset1, save_offset2); | |
2946 | #endif | md->offset_vector[offset] = save_offset1; |
2947 | md->offset_vector[offset+1] = save_offset2; | |
2948 | md->offset_vector[md->offset_end - number] = save_offset3; | |
2949 | return FALSE; | |
2950 | } | } |
2951 | ||
2952 | /* Recurse for all the alternatives. */ | /* Insufficient room for saving captured contents */ |
2953 | ||
2954 | else op = OP_BRA; | |
2955 | } | |
2956 | ||
2957 | /* Other types of node can be handled by a switch */ | |
2958 | ||
2959 | switch(op) | |
2960 | { | |
2961 | case OP_BRA: /* Non-capturing bracket: optimized */ | |
2962 | DPRINTF(("start bracket 0\n")); | |
2963 | do | do |
2964 | { | { |
2965 | if (match(eptr, ecode+3, offset_top, md)) return TRUE; | if (match(eptr, ecode+3, offset_top, md, ims, FALSE, eptr)) return TRUE; |
2966 | ecode += (ecode[1] << 8) + ecode[2]; | ecode += (ecode[1] << 8) + ecode[2]; |
2967 | } | } |
2968 | while (*ecode == OP_ALT); | while (*ecode == OP_ALT); |
2969 | DPRINTF(("bracket 0 failed\n")); | |
2970 | return FALSE; | |
2971 | ||
2972 | #ifdef DEBUG | /* Conditional group: compilation checked that there are no more than |
2973 | printf("bracket %d failed\n", number/2); | two branches. If the condition is false, skipping the first branch takes us |
2974 | #endif | past the end if there is only one branch, but that's OK because that is |
2975 | exactly what going to the ket would do. */ | |
2976 | ||
2977 | case OP_COND: | |
2978 | if (ecode[3] == OP_CREF) /* Condition is extraction test */ | |
2979 | { | |
2980 | int offset = ecode[4] << 1; /* Doubled reference number */ | |
2981 | return match(eptr, | |
2982 | ecode + ((offset < offset_top && md->offset_vector[offset] >= 0)? | |
2983 | 5 : 3 + (ecode[1] << 8) + ecode[2]), | |
2984 | offset_top, md, ims, FALSE, eptr); | |
2985 | } | |
2986 | ||
2987 | /* The condition is an assertion. Call match() to evaluate it - setting | |
2988 | the final argument TRUE causes it to stop at the end of an assertion. */ | |
2989 | ||
2990 | if (number > 0 && number < md->offset_end) | else |
2991 | { | { |
2992 | md->offset_vector[number] = save_offset1; | if (match(eptr, ecode+3, offset_top, md, ims, TRUE, NULL)) |
2993 | md->offset_vector[number+1] = save_offset2; | { |
2994 | ecode += 3 + (ecode[4] << 8) + ecode[5]; | |
2995 | while (*ecode == OP_ALT) ecode += (ecode[1] << 8) + ecode[2]; | |
2996 | } | |
2997 | else ecode += (ecode[1] << 8) + ecode[2]; | |
2998 | return match(eptr, ecode+3, offset_top, md, ims, FALSE, eptr); | |
2999 | } | } |
3000 | /* Control never reaches here */ | |
3001 | ||
3002 | return FALSE; | /* Skip over conditional reference data if encountered (should not be) */ |
} | ||
3003 | ||
3004 | /* Other types of node can be handled by a switch */ | case OP_CREF: |
3005 | ecode += 2; | |
3006 | break; | |
3007 | ||
3008 | /* End of the pattern */ | |
3009 | ||
switch(*ecode) | ||
{ | ||
3010 | case OP_END: | case OP_END: |
3011 | md->end_match_ptr = eptr; /* Record where we ended */ | md->end_match_ptr = eptr; /* Record where we ended */ |
3012 | md->end_offset_top = offset_top; /* and how many extracts were taken */ | md->end_offset_top = offset_top; /* and how many extracts were taken */ |
3013 | return TRUE; | return TRUE; |
3014 | ||
3015 | /* The equivalent of Prolog's "cut" - if the rest doesn't match, the | /* Change option settings */ |
whole thing doesn't match, so we have to get out via a longjmp(). */ | ||
3016 | ||
3017 | case OP_CUT: | case OP_OPT: |
3018 | if (match(eptr, ecode+1, offset_top, md)) return TRUE; | ims = ecode[1]; |
3019 | longjmp(md->fail_env, 1); | ecode += 2; |
3020 | DPRINTF(("ims set to %02x\n", ims)); | |
3021 | break; | |
3022 | ||
3023 | /* Assertion brackets. Check the alternative branches in turn - the | /* Assertion brackets. Check the alternative branches in turn - the |
3024 | matching won't pass the KET for an assertion. If any one branch matches, | matching won't pass the KET for an assertion. If any one branch matches, |
3025 | the assertion is true. */ | the assertion is true. Lookbehind assertions have an OP_REVERSE item at the |
3026 | start of each branch to move the current point backwards, so the code at | |
3027 | this level is identical to the lookahead case. */ | |
3028 | ||
3029 | case OP_ASSERT: | case OP_ASSERT: |
3030 | case OP_ASSERTBACK: | |
3031 | do | do |
3032 | { | { |
3033 | if (match(eptr, ecode+3, offset_top, md)) break; | if (match(eptr, ecode+3, offset_top, md, ims, FALSE, NULL)) break; |
3034 | ecode += (ecode[1] << 8) + ecode[2]; | ecode += (ecode[1] << 8) + ecode[2]; |
3035 | } | } |
3036 | while (*ecode == OP_ALT); | while (*ecode == OP_ALT); |
3037 | if (*ecode == OP_KET) return FALSE; | if (*ecode == OP_KET) return FALSE; |
3038 | ||
3039 | /* If checking an assertion for a condition, return TRUE. */ | |
3040 | ||
3041 | if (condassert) return TRUE; | |
3042 | ||
3043 | /* Continue from after the assertion, updating the offsets high water | /* Continue from after the assertion, updating the offsets high water |
3044 | mark, since extracts may have been taken during the assertion. */ | mark, since extracts may have been taken during the assertion. */ |
3045 | ||
# | Line 2355 for (;;) | Line 3051 for (;;) |
3051 | /* Negative assertion: all branches must fail to match */ | /* Negative assertion: all branches must fail to match */ |
3052 | ||
3053 | case OP_ASSERT_NOT: | case OP_ASSERT_NOT: |
3054 | case OP_ASSERTBACK_NOT: | |
3055 | do | do |
3056 | { | { |
3057 | if (match(eptr, ecode+3, offset_top, md)) return FALSE; | if (match(eptr, ecode+3, offset_top, md, ims, FALSE, NULL)) return FALSE; |
3058 | ecode += (ecode[1] << 8) + ecode[2]; | ecode += (ecode[1] << 8) + ecode[2]; |
3059 | } | } |
3060 | while (*ecode == OP_ALT); | while (*ecode == OP_ALT); |
3061 | ||
3062 | if (condassert) return TRUE; | |
3063 | ecode += 3; | ecode += 3; |
3064 | continue; | continue; |
3065 | ||
3066 | /* Move the subject pointer back. This occurs only at the start of | |
3067 | each branch of a lookbehind assertion. If we are too close to the start to | |
3068 | move back, this match function fails. */ | |
3069 | ||
3070 | case OP_REVERSE: | |
3071 | eptr -= (ecode[1] << 8) + ecode[2]; | |
3072 | if (eptr < md->start_subject) return FALSE; | |
3073 | ecode += 3; | |
3074 | break; | |
3075 | ||
3076 | ||
3077 | /* "Once" brackets are like assertion brackets except that after a match, | /* "Once" brackets are like assertion brackets except that after a match, |
3078 | the point in the subject string is not moved back. Thus there can never be | the point in the subject string is not moved back. Thus there can never be |
3079 | a move back into the brackets. Check the alternative branches in turn - the | a move back into the brackets. Check the alternative branches in turn - the |
3080 | matching won't pass the KET for this kind of subpattern. If any one branch | matching won't pass the KET for this kind of subpattern. If any one branch |
3081 | matches, we carry on, leaving the subject pointer. */ | matches, we carry on as at the end of a normal bracket, leaving the subject |
3082 | pointer. */ | |
3083 | ||
3084 | case OP_ONCE: | case OP_ONCE: |
do | ||
3085 | { | { |
3086 | if (match(eptr, ecode+3, offset_top, md)) break; | const uschar *prev = ecode; |
ecode += (ecode[1] << 8) + ecode[2]; | ||
} | ||
while (*ecode == OP_ALT); | ||
if (*ecode == OP_KET) return FALSE; | ||
3087 | ||
3088 | /* Continue as from after the assertion, updating the offsets high water | do |
3089 | mark, since extracts may have been taken. */ | { |
3090 | if (match(eptr, ecode+3, offset_top, md, ims, FALSE, eptr)) break; | |
3091 | ecode += (ecode[1] << 8) + ecode[2]; | |
3092 | } | |
3093 | while (*ecode == OP_ALT); | |
3094 | ||
3095 | do ecode += (ecode[1] << 8) + ecode[2]; while (*ecode == OP_ALT); | /* If hit the end of the group (which could be repeated), fail */ |
3096 | ecode += 3; | |
3097 | offset_top = md->end_offset_top; | if (*ecode != OP_ONCE && *ecode != OP_ALT) return FALSE; |
3098 | eptr = md->end_match_ptr; | |
3099 | continue; | /* Continue as from after the assertion, updating the offsets high water |
3100 | mark, since extracts may have been taken. */ | |
3101 | ||
3102 | do ecode += (ecode[1] << 8) + ecode[2]; while (*ecode == OP_ALT); | |
3103 | ||
3104 | offset_top = md->end_offset_top; | |
3105 | eptr = md->end_match_ptr; | |
3106 | ||
3107 | /* For a non-repeating ket, just continue at this level. This also | |
3108 | happens for a repeating ket if no characters were matched in the group. | |
3109 | This is the forcible breaking of infinite loops as implemented in Perl | |
3110 | 5.005. If there is an options reset, it will get obeyed in the normal | |
3111 | course of events. */ | |
3112 | ||
3113 | if (*ecode == OP_KET || eptr == eptrb) | |
3114 | { | |
3115 | ecode += 3; | |
3116 | break; | |
3117 | } | |
3118 | ||
3119 | /* The repeating kets try the rest of the pattern or restart from the | |
3120 | preceding bracket, in the appropriate order. We need to reset any options | |
3121 | that changed within the bracket before re-running it, so check the next | |
3122 | opcode. */ | |
3123 | ||
3124 | if (ecode[3] == OP_OPT) | |
3125 | { | |
3126 | ims = (ims & ~PCRE_IMS) | ecode[4]; | |
3127 | DPRINTF(("ims set to %02x at group repeat\n", ims)); | |
3128 | } | |
3129 | ||
3130 | if (*ecode == OP_KETRMIN) | |
3131 | { | |
3132 | if (match(eptr, ecode+3, offset_top, md, ims, FALSE, eptr) || | |
3133 | match(eptr, prev, offset_top, md, ims, FALSE, eptr)) return TRUE; | |
3134 | } | |
3135 | else /* OP_KETRMAX */ | |
3136 | { | |
3137 | if (match(eptr, prev, offset_top, md, ims, FALSE, eptr) || | |
3138 | match(eptr, ecode+3, offset_top, md, ims, FALSE, eptr)) return TRUE; | |
3139 | } | |
3140 | } | |
3141 | return FALSE; | |
3142 | ||
3143 | /* An alternation is the end of a branch; scan along to find the end of the | /* An alternation is the end of a branch; scan along to find the end of the |
3144 | bracketed group and go to there. */ | bracketed group and go to there. */ |
# | Line 2404 for (;;) | Line 3156 for (;;) |
3156 | case OP_BRAZERO: | case OP_BRAZERO: |
3157 | { | { |
3158 | const uschar *next = ecode+1; | const uschar *next = ecode+1; |
3159 | if (match(eptr, next, offset_top, md)) return TRUE; | if (match(eptr, next, offset_top, md, ims, FALSE, eptr)) return TRUE; |
3160 | do next += (next[1] << 8) + next[2]; while (*next == OP_ALT); | do next += (next[1] << 8) + next[2]; while (*next == OP_ALT); |
3161 | ecode = next + 3; | ecode = next + 3; |
3162 | } | } |
# | Line 2414 for (;;) | Line 3166 for (;;) |
3166 | { | { |
3167 | const uschar *next = ecode+1; | const uschar *next = ecode+1; |
3168 | do next += (next[1] << 8) + next[2]; while (*next == OP_ALT); | do next += (next[1] << 8) + next[2]; while (*next == OP_ALT); |
3169 | if (match(eptr, next+3, offset_top, md)) return TRUE; | if (match(eptr, next+3, offset_top, md, ims, FALSE, eptr)) return TRUE; |
3170 | ecode++; | ecode++; |
3171 | } | } |
3172 | break;; | break; |
3173 | ||
3174 | /* End of a group, repeated or non-repeating. If we are at the end of | /* End of a group, repeated or non-repeating. If we are at the end of |
3175 | an assertion "group", stop matching and return TRUE, but record the | an assertion "group", stop matching and return TRUE, but record the |
3176 | current high water mark for use by positive assertions. */ | current high water mark for use by positive assertions. Do this also |
3177 | for the "once" (not-backup up) groups. */ | |
3178 | ||
3179 | case OP_KET: | case OP_KET: |
3180 | case OP_KETRMIN: | case OP_KETRMIN: |
3181 | case OP_KETRMAX: | case OP_KETRMAX: |
3182 | { | { |
int number; | ||
3183 | const uschar *prev = ecode - (ecode[1] << 8) - ecode[2]; | const uschar *prev = ecode - (ecode[1] << 8) - ecode[2]; |
3184 | ||
3185 | if (*prev == OP_ASSERT || *prev == OP_ASSERT_NOT || *prev == OP_ONCE) | if (*prev == OP_ASSERT || *prev == OP_ASSERT_NOT || |
3186 | *prev == OP_ASSERTBACK || *prev == OP_ASSERTBACK_NOT || | |
3187 | *prev == OP_ONCE) | |
3188 | { | { |
3189 | md->end_match_ptr = eptr; /* For ONCE */ | md->end_match_ptr = eptr; /* For ONCE */ |
3190 | md->end_offset_top = offset_top; | md->end_offset_top = offset_top; |
3191 | return TRUE; | return TRUE; |
3192 | } | } |
3193 | ||
3194 | /* In all other cases we have to check the group number back at the | /* In all other cases except a conditional group we have to check the |
3195 | start and if necessary complete handling an extraction by setting the | group number back at the start and if necessary complete handling an |
3196 | final offset and bumping the high water mark. */ | extraction by setting the offsets and bumping the high water mark. */ |
3197 | ||
3198 | number = (*prev - OP_BRA) << 1; | if (*prev != OP_COND) |
3199 | { | |
3200 | int number = *prev - OP_BRA; | |
3201 | int offset = number << 1; | |
3202 | ||
3203 | #ifdef DEBUG | DPRINTF(("end bracket %d\n", number)); |
printf("end bracket %d\n", number/2); | ||
#endif | ||
3204 | ||
3205 | if (number > 0) | if (number > 0) |
{ | ||
if (number >= md->offset_end) md->offset_overflow = TRUE; else | ||
3206 | { | { |
3207 | md->offset_vector[number+1] = eptr - md->start_subject; | if (offset >= md->offset_max) md->offset_overflow = TRUE; else |
3208 | if (offset_top <= number) offset_top = number + 2; | { |
3209 | md->offset_vector[offset] = | |
3210 | md->offset_vector[md->offset_end - number]; | |
3211 | md->offset_vector[offset+1] = eptr - md->start_subject; | |
3212 | if (offset_top <= offset) offset_top = offset + 2; | |
3213 | } | |
3214 | } | } |
3215 | } | } |
3216 | ||
3217 | /* For a non-repeating ket, just advance to the next node and continue at | /* Reset the value of the ims flags, in case they got changed during |
3218 | this level. */ | the group. */ |
3219 | ||
3220 | ims = original_ims; | |
3221 | DPRINTF(("ims reset to %02x\n", ims)); | |
3222 | ||
3223 | if (*ecode == OP_KET) | /* For a non-repeating ket, just continue at this level. This also |
3224 | happens for a repeating ket if no characters were matched in the group. | |
3225 | This is the forcible breaking of infinite loops as implemented in Perl | |
3226 | 5.005. If there is an options reset, it will get obeyed in the normal | |
3227 | course of events. */ | |
3228 | ||
3229 | if (*ecode == OP_KET || eptr == eptrb) | |
3230 | { | { |
3231 | ecode += 3; | ecode += 3; |
3232 | break; | break; |
# | Line 2470 for (;;) | Line 3237 for (;;) |
3237 | ||
3238 | if (*ecode == OP_KETRMIN) | if (*ecode == OP_KETRMIN) |
3239 | { | { |
3240 | if (match(eptr, ecode+3, offset_top, md) || | if (match(eptr, ecode+3, offset_top, md, ims, FALSE, eptr) || |
3241 | match(eptr, prev, offset_top, md)) return TRUE; | match(eptr, prev, offset_top, md, ims, FALSE, eptr)) return TRUE; |
3242 | } | } |
3243 | else /* OP_KETRMAX */ | else /* OP_KETRMAX */ |
3244 | { | { |
3245 | if (match(eptr, prev, offset_top, md) || | if (match(eptr, prev, offset_top, md, ims, FALSE, eptr) || |
3246 | match(eptr, ecode+3, offset_top, md)) return TRUE; | match(eptr, ecode+3, offset_top, md, ims, FALSE, eptr)) return TRUE; |
3247 | } | } |
3248 | } | } |
3249 | return FALSE; | return FALSE; |
# | Line 2485 for (;;) | Line 3252 for (;;) |
3252 | ||
3253 | case OP_CIRC: | case OP_CIRC: |
3254 | if (md->notbol && eptr == md->start_subject) return FALSE; | if (md->notbol && eptr == md->start_subject) return FALSE; |
3255 | if (md->multiline) | if ((ims & PCRE_MULTILINE) != 0) |
3256 | { | { |
3257 | if (eptr != md->start_subject && eptr[-1] != '\n') return FALSE; | if (eptr != md->start_subject && eptr[-1] != '\n') return FALSE; |
3258 | ecode++; | ecode++; |
# | Line 2500 for (;;) | Line 3267 for (;;) |
3267 | ecode++; | ecode++; |
3268 | break; | break; |
3269 | ||
3270 | /* Assert before internal newline if multiline, or before | /* Assert before internal newline if multiline, or before a terminating |
3271 | a terminating newline unless endonly is set, else end of subject unless | newline unless endonly is set, else end of subject unless noteol is set. */ |
noteol is set. */ | ||
3272 | ||
3273 | case OP_DOLL: | case OP_DOLL: |
3274 | if (md->noteol && eptr >= md->end_subject) return FALSE; | if ((ims & PCRE_MULTILINE) != 0) |
if (md->multiline) | ||
3275 | { | { |
3276 | if (eptr < md->end_subject && *eptr != '\n') return FALSE; | if (eptr < md->end_subject) { if (*eptr != '\n') return FALSE; } |
3277 | else { if (md->noteol) return FALSE; } | |
3278 | ecode++; | ecode++; |
3279 | break; | break; |
3280 | } | } |
3281 | else if (!md->endonly) | else |
3282 | { | { |
3283 | if (eptr < md->end_subject - 1 || | if (md->noteol) return FALSE; |
3284 | (eptr == md->end_subject - 1 && *eptr != '\n')) return FALSE; | if (!md->endonly) |
3285 | ecode++; | { |
3286 | break; | if (eptr < md->end_subject - 1 || |
3287 | (eptr == md->end_subject - 1 && *eptr != '\n')) return FALSE; | |
3288 | ||
3289 | ecode++; | |
3290 | break; | |
3291 | } | |
3292 | } | } |
3293 | /* ... else fall through */ | /* ... else fall through */ |
3294 | ||
3295 | /* End of subject assertion */ | /* End of subject assertion (\z) */ |
3296 | ||
3297 | case OP_EOD: | case OP_EOD: |
3298 | if (eptr < md->end_subject) return FALSE; | if (eptr < md->end_subject) return FALSE; |
3299 | ecode++; | ecode++; |
3300 | break; | break; |
3301 | ||
3302 | /* End of subject or ending \n assertion (\Z) */ | |
3303 | ||
3304 | case OP_EODN: | |
3305 | if (eptr < md->end_subject - 1 || | |
3306 | (eptr == md->end_subject - 1 && *eptr != '\n')) return FALSE; | |
3307 | ecode++; | |
3308 | break; | |
3309 | ||
3310 | /* Word boundary assertions */ | /* Word boundary assertions */ |
3311 | ||
3312 | case OP_NOT_WORD_BOUNDARY: | case OP_NOT_WORD_BOUNDARY: |
3313 | case OP_WORD_BOUNDARY: | case OP_WORD_BOUNDARY: |
3314 | { | { |
3315 | BOOL prev_is_word = (eptr != md->start_subject) && | BOOL prev_is_word = (eptr != md->start_subject) && |
3316 | ((pcre_ctypes[eptr[-1]] & ctype_word) != 0); | ((md->ctypes[eptr[-1]] & ctype_word) != 0); |
3317 | BOOL cur_is_word = (eptr < md->end_subject) && | BOOL cur_is_word = (eptr < md->end_subject) && |
3318 | ((pcre_ctypes[*eptr] & ctype_word) != 0); | ((md->ctypes[*eptr] & ctype_word) != 0); |
3319 | if ((*ecode++ == OP_WORD_BOUNDARY)? | if ((*ecode++ == OP_WORD_BOUNDARY)? |
3320 | cur_is_word == prev_is_word : cur_is_word != prev_is_word) | cur_is_word == prev_is_word : cur_is_word != prev_is_word) |
3321 | return FALSE; | return FALSE; |
# | Line 2546 for (;;) | Line 3325 for (;;) |
3325 | /* Match a single character type; inline for speed */ | /* Match a single character type; inline for speed */ |
3326 | ||
3327 | case OP_ANY: | case OP_ANY: |
3328 | if (!md->dotall && eptr < md->end_subject && *eptr == '\n') return FALSE; | if ((ims & PCRE_DOTALL) == 0 && eptr < md->end_subject && *eptr == '\n') |
3329 | return FALSE; | |
3330 | if (eptr++ >= md->end_subject) return FALSE; | if (eptr++ >= md->end_subject) return FALSE; |
3331 | ecode++; | ecode++; |
3332 | break; | break; |
3333 | ||
3334 | case OP_NOT_DIGIT: | case OP_NOT_DIGIT: |
3335 | if (eptr >= md->end_subject || (pcre_ctypes[*eptr++] & ctype_digit) != 0) | if (eptr >= md->end_subject || |
3336 | (md->ctypes[*eptr++] & ctype_digit) != 0) | |
3337 | return FALSE; | return FALSE; |
3338 | ecode++; | ecode++; |
3339 | break; | break; |
3340 | ||
3341 | case OP_DIGIT: | case OP_DIGIT: |
3342 | if (eptr >= md->end_subject || (pcre_ctypes[*eptr++] & ctype_digit) == 0) | if (eptr >= md->end_subject || |
3343 | (md->ctypes[*eptr++] & ctype_digit) == 0) | |
3344 | return FALSE; | return FALSE; |
3345 | ecode++; | ecode++; |
3346 | break; | break; |
3347 | ||
3348 | case OP_NOT_WHITESPACE: | case OP_NOT_WHITESPACE: |
3349 | if (eptr >= md->end_subject || (pcre_ctypes[*eptr++] & ctype_space) != 0) | if (eptr >= md->end_subject || |
3350 | (md->ctypes[*eptr++] & ctype_space) != 0) | |
3351 | return FALSE; | return FALSE; |
3352 | ecode++; | ecode++; |
3353 | break; | break; |
3354 | ||
3355 | case OP_WHITESPACE: | case OP_WHITESPACE: |
3356 | if (eptr >= md->end_subject || (pcre_ctypes[*eptr++] & ctype_space) == 0) | if (eptr >= md->end_subject || |
3357 | (md->ctypes[*eptr++] & ctype_space) == 0) | |
3358 | return FALSE; | return FALSE; |
3359 | ecode++; | ecode++; |
3360 | break; | break; |
3361 | ||
3362 | case OP_NOT_WORDCHAR: | case OP_NOT_WORDCHAR: |
3363 | if (eptr >= md->end_subject || (pcre_ctypes[*eptr++] & ctype_word) != 0) | if (eptr >= md->end_subject || |
3364 | (md->ctypes[*eptr++] & ctype_word) != 0) | |
3365 | return FALSE; | return FALSE; |
3366 | ecode++; | ecode++; |
3367 | break; | break; |
3368 | ||
3369 | case OP_WORDCHAR: | case OP_WORDCHAR: |
3370 | if (eptr >= md->end_subject || (pcre_ctypes[*eptr++] & ctype_word) == 0) | if (eptr >= md->end_subject || |
3371 | (md->ctypes[*eptr++] & ctype_word) == 0) | |
3372 | return FALSE; | return FALSE; |
3373 | ecode++; | ecode++; |
3374 | break; | break; |
# | Line 2598 for (;;) | Line 3384 for (;;) |
3384 | case OP_REF: | case OP_REF: |
3385 | { | { |
3386 | int length; | int length; |
3387 | int number = ecode[1] << 1; /* Doubled reference number */ | int offset = ecode[1] << 1; /* Doubled reference number */ |
3388 | ecode += 2; /* Advance past the item */ | ecode += 2; /* Advance past the item */ |
3389 | ||
3390 | if (number >= offset_top || md->offset_vector[number] < 0) | /* If the reference is unset, set the length to be longer than the amount |
3391 | { | of subject left; this ensures that every attempt at a match fails. We |
3392 | md->errorcode = PCRE_ERROR_BADREF; | can't just fail here, because of the possibility of quantifiers with zero |
3393 | return FALSE; | minima. */ |
3394 | } | |
3395 | length = (offset >= offset_top || md->offset_vector[offset] < 0)? | |
3396 | md->end_subject - eptr + 1 : | |
3397 | md->offset_vector[offset+1] - md->offset_vector[offset]; | |
3398 | ||
3399 | length = md->offset_vector[number+1] - md->offset_vector[number]; | /* Set up for repetition, or handle the non-repeated case */ |
3400 | ||
3401 | switch (*ecode) | switch (*ecode) |
3402 | { | { |
# | Line 2634 for (;;) | Line 3423 for (;;) |
3423 | break; | break; |
3424 | ||
3425 | default: /* No repeat follows */ | default: /* No repeat follows */ |
3426 | if (!match_ref(number, eptr, length, md)) return FALSE; | if (!match_ref(offset, eptr, length, md, ims)) return FALSE; |
3427 | eptr += length; | eptr += length; |
3428 | continue; /* With the main loop */ | continue; /* With the main loop */ |
3429 | } | } |
# | Line 2650 for (;;) | Line 3439 for (;;) |
3439 | ||
3440 | for (i = 1; i <= min; i++) | for (i = 1; i <= min; i++) |
3441 | { | { |
3442 | if (!match_ref(number, eptr, length, md)) return FALSE; | if (!match_ref(offset, eptr, length, md, ims)) return FALSE; |
3443 | eptr += length; | eptr += length; |
3444 | } | } |
3445 | ||
# | Line 2665 for (;;) | Line 3454 for (;;) |
3454 | { | { |
3455 | for (i = min;; i++) | for (i = min;; i++) |
3456 | { | { |
3457 | if (match(eptr, ecode, offset_top, md)) return TRUE; | if (match(eptr, ecode, offset_top, md, ims, FALSE, eptrb)) |
3458 | if (i >= max || !match_ref(number, eptr, length, md)) | return TRUE; |
3459 | if (i >= max || !match_ref(offset, eptr, length, md, ims)) | |
3460 | return FALSE; | return FALSE; |
3461 | eptr += length; | eptr += length; |
3462 | } | } |
# | Line 2680 for (;;) | Line 3470 for (;;) |
3470 | const uschar *pp = eptr; | const uschar *pp = eptr; |
3471 | for (i = min; i < max; i++) | for (i = min; i < max; i++) |
3472 | { | { |
3473 | if (!match_ref(number, eptr, length, md)) break; | if (!match_ref(offset, eptr, length, md, ims)) break; |
3474 | eptr += length; | eptr += length; |
3475 | } | } |
3476 | while (eptr >= pp) | while (eptr >= pp) |
3477 | { | { |
3478 | if (match(eptr, ecode, offset_top, md)) return TRUE; | if (match(eptr, ecode, offset_top, md, ims, FALSE, eptrb)) |
3479 | return TRUE; | |
3480 | eptr -= length; | eptr -= length; |
3481 | } | } |
3482 | return FALSE; | return FALSE; |
# | Line 2693 for (;;) | Line 3484 for (;;) |
3484 | } | } |
3485 | /* Control never gets here */ | /* Control never gets here */ |
3486 | ||
3487 | ||
3488 | ||
3489 | /* Match a character class, possibly repeatedly. Look past the end of the | /* Match a character class, possibly repeatedly. Look past the end of the |
3490 | item to see if there is repeat information following. Then obey similar | item to see if there is repeat information following. Then obey similar |
3491 | code to character type repeats - written out again for speed. If caseless | code to character type repeats - written out again for speed. */ |
matching was set at runtime but not at compile time, we have to check both | ||
versions of a character. */ | ||
3492 | ||
3493 | case OP_CLASS: | case OP_CLASS: |
3494 | { | { |
# | Line 2729 for (;;) | Line 3520 for (;;) |
3520 | break; | break; |
3521 | ||
3522 | default: /* No repeat follows */ | default: /* No repeat follows */ |
3523 | if (eptr >= md->end_subject) return FALSE; | min = max = 1; |
3524 | c = *eptr++; | break; |
if ((data[c/8] & (1 << (c&7))) != 0) continue; /* With main loop */ | ||
if (md->runtime_caseless) | ||
{ | ||
c = pcre_fcc[c]; | ||
if ((data[c/8] & (1 << (c&7))) != 0) continue; /* With main loop */ | ||
} | ||
return FALSE; | ||
3525 | } | } |
3526 | ||
3527 | /* First, ensure the minimum number of matches are present. */ | /* First, ensure the minimum number of matches are present. */ |
# | Line 2747 for (;;) | Line 3531 for (;;) |
3531 | if (eptr >= md->end_subject) return FALSE; | if (eptr >= md->end_subject) return FALSE; |
3532 | c = *eptr++; | c = *eptr++; |
3533 | if ((data[c/8] & (1 << (c&7))) != 0) continue; | if ((data[c/8] & (1 << (c&7))) != 0) continue; |
if (md->runtime_caseless) | ||
{ | ||
c = pcre_fcc[c]; | ||
if ((data[c/8] & (1 << (c&7))) != 0) continue; | ||
} | ||
3534 | return FALSE; | return FALSE; |
3535 | } | } |
3536 | ||
# | Line 2767 for (;;) | Line 3546 for (;;) |
3546 | { | { |
3547 | for (i = min;; i++) | for (i = min;; i++) |
3548 | { | { |
3549 | if (match(eptr, ecode, offset_top, md)) return TRUE; | if (match(eptr, ecode, offset_top, md, ims, FALSE, eptrb)) |
3550 | return TRUE; | |
3551 | if (i >= max || eptr >= md->end_subject) return FALSE; | if (i >= max || eptr >= md->end_subject) return FALSE; |
3552 | c = *eptr++; | c = *eptr++; |
3553 | if ((data[c/8] & (1 << (c&7))) != 0) continue; | if ((data[c/8] & (1 << (c&7))) != 0) continue; |
if (md->runtime_caseless) | ||
{ | ||
c = pcre_fcc[c]; | ||
if ((data[c/8] & (1 << (c&7))) != 0) continue; | ||
} | ||
3554 | return FALSE; | return FALSE; |
3555 | } | } |
3556 | /* Control never gets here */ | /* Control never gets here */ |
# | Line 2791 for (;;) | Line 3566 for (;;) |
3566 | if (eptr >= md->end_subject) break; | if (eptr >= md->end_subject) break; |
3567 | c = *eptr; | c = *eptr; |
3568 | if ((data[c/8] & (1 << (c&7))) != 0) continue; | if ((data[c/8] & (1 << (c&7))) != 0) continue; |
if (md->runtime_caseless) | ||
{ | ||
c = pcre_fcc[c]; | ||
if ((data[c/8] & (1 << (c&7))) != 0) continue; | ||
} | ||
3569 | break; | break; |
3570 | } | } |
3571 | ||
3572 | while (eptr >= pp) | while (eptr >= pp) |
3573 | if (match(eptr--, ecode, offset_top, md)) return TRUE; | if (match(eptr--, ecode, offset_top, md, ims, FALSE, eptrb)) |
3574 | return TRUE; | |
3575 | return FALSE; | return FALSE; |
3576 | } | } |
3577 | } | } |
# | Line 2813 for (;;) | Line 3584 for (;;) |
3584 | register int length = ecode[1]; | register int length = ecode[1]; |
3585 | ecode += 2; | ecode += 2; |
3586 | ||
3587 | #ifdef DEBUG | #ifdef DEBUG /* Sigh. Some compilers never learn. */ |
3588 | if (eptr >= md->end_subject) | if (eptr >= md->end_subject) |
3589 | printf("matching subject <null> against pattern "); | printf("matching subject <null> against pattern "); |
3590 | else | else |
# | Line 2824 for (;;) | Line 3595 for (;;) |
3595 | } | } |
3596 | pchars(ecode, length, FALSE, md); | pchars(ecode, length, FALSE, md); |
3597 | printf("\n"); | printf("\n"); |
3598 | #endif | #endif |
3599 | ||
3600 | if (length > md->end_subject - eptr) return FALSE; | if (length > md->end_subject - eptr) return FALSE; |
3601 | if (md->caseless) | if ((ims & PCRE_CASELESS) != 0) |
3602 | { | { |
3603 | while (length-- > 0) if (pcre_lcc[*ecode++] != pcre_lcc[*eptr++]) return FALSE; | while (length-- > 0) |
3604 | if (md->lcc[*ecode++] != md->lcc[*eptr++]) | |
3605 | return FALSE; | |
3606 | } | } |
3607 | else | else |
3608 | { | { |
# | Line 2881 for (;;) | Line 3654 for (;;) |
3654 | maximum. Alternatively, if maximizing, find the maximum number of | maximum. Alternatively, if maximizing, find the maximum number of |
3655 | characters and work backwards. */ | characters and work backwards. */ |
3656 | ||
3657 | #ifdef DEBUG | DPRINTF(("matching %c{%d,%d} against subject %.*s\n", c, min, max, |
3658 | printf("matching %c{%d,%d} against subject %.*s\n", c, min, max, | max, eptr)); |
max, eptr); | ||
#endif | ||
3659 | ||
3660 | if (md->caseless) | if ((ims & PCRE_CASELESS) != 0) |
3661 | { | { |
3662 | c = pcre_lcc[c]; | c = md->lcc[c]; |
3663 | for (i = 1; i <= min; i++) if (c != pcre_lcc[*eptr++]) return FALSE; | for (i = 1; i <= min; i++) |
3664 | if (c != md->lcc[*eptr++]) return FALSE; | |
3665 | if (min == max) continue; | if (min == max) continue; |
3666 | if (minimize) | if (minimize) |
3667 | { | { |
3668 | for (i = min;; i++) | for (i = min;; i++) |
3669 | { | { |
3670 | if (match(eptr, ecode, offset_top, md)) return TRUE; | if (match(eptr, ecode, offset_top, md, ims, FALSE, eptrb)) |
3671 | if (i >= max || eptr >= md->end_subject || c != pcre_lcc[*eptr++]) | return TRUE; |
3672 | if (i >= max || eptr >= md->end_subject || | |
3673 | c != md->lcc[*eptr++]) | |
3674 | return FALSE; | return FALSE; |
3675 | } | } |
3676 | /* Control never gets here */ | /* Control never gets here */ |
# | Line 2906 for (;;) | Line 3680 for (;;) |
3680 | const uschar *pp = eptr; | const uschar *pp = eptr; |
3681 | for (i = min; i < max; i++) | for (i = min; i < max; i++) |
3682 | { | { |
3683 | if (eptr >= md->end_subject || c != pcre_lcc[*eptr]) break; | if (eptr >= md->end_subject || c != md->lcc[*eptr]) break; |
3684 | eptr++; | eptr++; |
3685 | } | } |
3686 | while (eptr >= pp) | while (eptr >= pp) |
3687 | if (match(eptr--, ecode, offset_top, md)) return TRUE; | if (match(eptr--, ecode, offset_top, md, ims, FALSE, eptrb)) |
3688 | return TRUE; | |
3689 | return FALSE; | return FALSE; |
3690 | } | } |
3691 | /* Control never gets here */ | /* Control never gets here */ |
# | Line 2926 for (;;) | Line 3701 for (;;) |
3701 | { | { |
3702 | for (i = min;; i++) | for (i = min;; i++) |
3703 | { | { |
3704 | if (match(eptr, ecode, offset_top, md)) return TRUE; | if (match(eptr, ecode, offset_top, md, ims, FALSE, eptrb)) |
3705 | return TRUE; | |
3706 | if (i >= max || eptr >= md->end_subject || c != *eptr++) return FALSE; | if (i >= max || eptr >= md->end_subject || c != *eptr++) return FALSE; |
3707 | } | } |
3708 | /* Control never gets here */ | /* Control never gets here */ |
# | Line 2940 for (;;) | Line 3716 for (;;) |
3716 | eptr++; | eptr++; |
3717 | } | } |
3718 | while (eptr >= pp) | while (eptr >= pp) |
3719 | if (match(eptr--, ecode, offset_top, md)) return TRUE; | if (match(eptr--, ecode, offset_top, md, ims, FALSE, eptrb)) |
3720 | return TRUE; | |
3721 | return FALSE; | return FALSE; |
3722 | } | } |
3723 | } | } |
# | Line 2949 for (;;) | Line 3726 for (;;) |
3726 | /* Match a negated single character */ | /* Match a negated single character */ |
3727 | ||
3728 | case OP_NOT: | case OP_NOT: |
3729 | if (eptr > md->end_subject) return FALSE; | if (eptr >= md->end_subject) return FALSE; |
3730 | ecode++; | ecode++; |
3731 | if (md->caseless) | if ((ims & PCRE_CASELESS) != 0) |
3732 | { | { |
3733 | if (pcre_lcc[*ecode++] == pcre_lcc[*eptr++]) return FALSE; | if (md->lcc[*ecode++] == md->lcc[*eptr++]) return FALSE; |
3734 | } | } |
3735 | else | else |
3736 | { | { |
# | Line 3008 for (;;) | Line 3785 for (;;) |
3785 | maximum. Alternatively, if maximizing, find the maximum number of | maximum. Alternatively, if maximizing, find the maximum number of |
3786 | characters and work backwards. */ | characters and work backwards. */ |
3787 | ||
3788 | #ifdef DEBUG | DPRINTF(("negative matching %c{%d,%d} against subject %.*s\n", c, min, max, |
3789 | printf("negative matching %c{%d,%d} against subject %.*s\n", c, min, max, | max, eptr)); |
max, eptr); | ||
#endif | ||
3790 | ||
3791 | if (md->caseless) | if ((ims & PCRE_CASELESS) != 0) |
3792 | { | { |
3793 | c = pcre_lcc[c]; | c = md->lcc[c]; |
3794 | for (i = 1; i <= min; i++) if (c == pcre_lcc[*eptr++]) return FALSE; | for (i = 1; i <= min; i++) |
3795 | if (c == md->lcc[*eptr++]) return FALSE; | |
3796 | if (min == max) continue; | if (min == max) continue; |
3797 | if (minimize) | if (minimize) |
3798 | { | { |
3799 | for (i = min;; i++) | for (i = min;; i++) |
3800 | { | { |
3801 | if (match(eptr, ecode, offset_top, md)) return TRUE; | if (match(eptr, ecode, offset_top, md, ims, FALSE, eptrb)) |
3802 | if (i >= max || eptr >= md->end_subject || c == pcre_lcc[*eptr++]) | return TRUE; |
3803 | if (i >= max || eptr >= md->end_subject || | |
3804 | c == md->lcc[*eptr++]) | |
3805 | return FALSE; | return FALSE; |
3806 | } | } |
3807 | /* Control never gets here */ | /* Control never gets here */ |
# | Line 3033 for (;;) | Line 3811 for (;;) |
3811 | const uschar *pp = eptr; | const uschar *pp = eptr; |
3812 | for (i = min; i < max; i++) | for (i = min; i < max; i++) |
3813 | { | { |
3814 | if (eptr >= md->end_subject || c == pcre_lcc[*eptr]) break; | if (eptr >= md->end_subject || c == md->lcc[*eptr]) break; |
3815 | eptr++; | eptr++; |
3816 | } | } |
3817 | while (eptr >= pp) | while (eptr >= pp) |
3818 | if (match(eptr--, ecode, offset_top, md)) return TRUE; | if (match(eptr--, ecode, offset_top, md, ims, FALSE, eptrb)) |
3819 | return TRUE; | |
3820 | return FALSE; | return FALSE; |
3821 | } | } |
3822 | /* Control never gets here */ | /* Control never gets here */ |
# | Line 3053 for (;;) | Line 3832 for (;;) |
3832 | { | { |
3833 | for (i = min;; i++) | for (i = min;; i++) |
3834 | { | { |
3835 | if (match(eptr, ecode, offset_top, md)) return TRUE; | if (match(eptr, ecode, offset_top, md, ims, FALSE, eptrb)) |
3836 | return TRUE; | |
3837 | if (i >= max || eptr >= md->end_subject || c == *eptr++) return FALSE; | if (i >= max || eptr >= md->end_subject || c == *eptr++) return FALSE; |
3838 | } | } |
3839 | /* Control never gets here */ | /* Control never gets here */ |
# | Line 3067 for (;;) | Line 3847 for (;;) |
3847 | eptr++; | eptr++; |
3848 | } | } |
3849 | while (eptr >= pp) | while (eptr >= pp) |
3850 | if (match(eptr--, ecode, offset_top, md)) return TRUE; | if (match(eptr--, ecode, offset_top, md, ims, FALSE, eptrb)) |
3851 | return TRUE; | |
3852 | return FALSE; | return FALSE; |
3853 | } | } |
3854 | } | } |
# | Line 3117 for (;;) | Line 3898 for (;;) |
3898 | if (min > 0) switch(ctype) | if (min > 0) switch(ctype) |
3899 | { | { |
3900 | case OP_ANY: | case OP_ANY: |
3901 | if (!md->dotall) | if ((ims & PCRE_DOTALL) == 0) |
3902 | { for (i = 1; i <= min; i++) if (*eptr++ == '\n') return FALSE; } | { for (i = 1; i <= min; i++) if (*eptr++ == '\n') return FALSE; } |
3903 | else eptr += min; | else eptr += min; |
3904 | break; | break; |
3905 | ||
3906 | case OP_NOT_DIGIT: | case OP_NOT_DIGIT: |
3907 | for (i = 1; i <= min; i++) | for (i = 1; i <= min; i++) |
3908 | if ((pcre_ctypes[*eptr++] & ctype_digit) != 0) return FALSE; | if ((md->ctypes[*eptr++] & ctype_digit) != 0) return FALSE; |
3909 | break; | break; |
3910 | ||
3911 | case OP_DIGIT: | case OP_DIGIT: |
3912 | for (i = 1; i <= min; i++) | for (i = 1; i <= min; i++) |
3913 | if ((pcre_ctypes[*eptr++] & ctype_digit) == 0) return FALSE; | if ((md->ctypes[*eptr++] & ctype_digit) == 0) return FALSE; |
3914 | break; | break; |
3915 | ||
3916 | case OP_NOT_WHITESPACE: | case OP_NOT_WHITESPACE: |
3917 | for (i = 1; i <= min; i++) | for (i = 1; i <= min; i++) |
3918 | if ((pcre_ctypes[*eptr++] & ctype_space) != 0) return FALSE; | if ((md->ctypes[*eptr++] & ctype_space) != 0) return FALSE; |
3919 | break; | break; |
3920 | ||
3921 | case OP_WHITESPACE: | case OP_WHITESPACE: |
3922 | for (i = 1; i <= min; i++) | for (i = 1; i <= min; i++) |
3923 | if ((pcre_ctypes[*eptr++] & ctype_space) == 0) return FALSE; | if ((md->ctypes[*eptr++] & ctype_space) == 0) return FALSE; |
3924 | break; | break; |
3925 | ||
3926 | case OP_NOT_WORDCHAR: | case OP_NOT_WORDCHAR: |
3927 | for (i = 1; i <= min; i++) if ((pcre_ctypes[*eptr++] & ctype_word) != 0) | for (i = 1; i <= min; i++) |
3928 | return FALSE; | if ((md->ctypes[*eptr++] & ctype_word) != 0) |
3929 | return FALSE; | |
3930 | break; | break; |
3931 | ||
3932 | case OP_WORDCHAR: | case OP_WORDCHAR: |
3933 | for (i = 1; i <= min; i++) if ((pcre_ctypes[*eptr++] & ctype_word) == 0) | for (i = 1; i <= min; i++) |
3934 | return FALSE; | if ((md->ctypes[*eptr++] & ctype_word) == 0) |
3935 | return FALSE; | |
3936 | break; | break; |
3937 | } | } |
3938 | ||
# | Line 3158 for (;;) | Line 3941 for (;;) |
3941 | if (min == max) continue; | if (min == max) continue; |
3942 | ||
3943 | /* If minimizing, we have to test the rest of the pattern before each | /* If minimizing, we have to test the rest of the pattern before each |
3944 | subsequent match, so inlining isn't much help; just use the function. */ | subsequent match. */ |
3945 | ||
3946 | if (minimize) | if (minimize) |
3947 | { | { |
3948 | for (i = min;; i++) | for (i = min;; i++) |
3949 | { | { |
3950 | if (match(eptr, ecode, offset_top, md)) return TRUE; | if (match(eptr, ecode, offset_top, md, ims, FALSE, eptrb)) return TRUE; |
3951 | if (i >= max || eptr >= md->end_subject || | if (i >= max || eptr >= md->end_subject) return FALSE; |
3952 | !match_type(ctype, *eptr++, md->dotall)) | |
3953 | return FALSE; | c = *eptr++; |
3954 | switch(ctype) | |
3955 | { | |
3956 | case OP_ANY: | |
3957 | if ((ims & PCRE_DOTALL) == 0 && c == '\n') return FALSE; | |
3958 | break; | |
3959 | ||
3960 | case OP_NOT_DIGIT: | |
3961 | if ((md->ctypes[c] & ctype_digit) != 0) return FALSE; | |
3962 | break; | |
3963 | ||
3964 | case OP_DIGIT: | |
3965 | if ((md->ctypes[c] & ctype_digit) == 0) return FALSE; | |
3966 | break; | |
3967 | ||
3968 | case OP_NOT_WHITESPACE: | |
3969 | if ((md->ctypes[c] & ctype_space) != 0) return FALSE; | |
3970 | break; | |
3971 | ||
3972 | case OP_WHITESPACE: | |
3973 | if ((md->ctypes[c] & ctype_space) == 0) return FALSE; | |
3974 | break; | |
3975 | ||
3976 | case OP_NOT_WORDCHAR: | |
3977 | if ((md->ctypes[c] & ctype_word) != 0) return FALSE; | |
3978 | break; | |
3979 | ||
3980 | case OP_WORDCHAR: | |
3981 | if ((md->ctypes[c] & ctype_word) == 0) return FALSE; | |
3982 | break; | |
3983 | } | |
3984 | } | } |
3985 | /* Control never gets here */ | /* Control never gets here */ |
3986 | } | } |
# | Line 3181 for (;;) | Line 3994 for (;;) |
3994 | switch(ctype) | switch(ctype) |
3995 | { | { |
3996 | case OP_ANY: | case OP_ANY: |
3997 | if (!md->dotall) | if ((ims & PCRE_DOTALL) == 0) |
3998 | { | { |
3999 | for (i = min; i < max; i++) | for (i = min; i < max; i++) |
4000 | { | { |
# | Line 3200 for (;;) | Line 4013 for (;;) |
4013 | case OP_NOT_DIGIT: | case OP_NOT_DIGIT: |
4014 | for (i = min; i < max; i++) | for (i = min; i < max; i++) |
4015 | { | { |
4016 | if (eptr >= md->end_subject || (pcre_ctypes[*eptr] & ctype_digit) != 0) | if (eptr >= md->end_subject || (md->ctypes[*eptr] & ctype_digit) != 0) |
4017 | break; | break; |
4018 | eptr++; | eptr++; |
4019 | } | } |
# | Line 3209 for (;;) | Line 4022 for (;;) |
4022 | case OP_DIGIT: | case OP_DIGIT: |
4023 | for (i = min; i < max; i++) | for (i = min; i < max; i++) |
4024 | { | { |
4025 | if (eptr >= md->end_subject || (pcre_ctypes[*eptr] & ctype_digit) == 0) | if (eptr >= md->end_subject || (md->ctypes[*eptr] & ctype_digit) == 0) |
4026 | break; | break; |
4027 | eptr++; | eptr++; |
4028 | } | } |
# | Line 3218 for (;;) | Line 4031 for (;;) |
4031 | case OP_NOT_WHITESPACE: | case OP_NOT_WHITESPACE: |
4032 | for (i = min; i < max; i++) | for (i = min; i < max; i++) |
4033 | { | { |
4034 | if (eptr >= md->end_subject || (pcre_ctypes[*eptr] & ctype_space) != 0) | if (eptr >= md->end_subject || (md->ctypes[*eptr] & ctype_space) != 0) |
4035 | break; | break; |
4036 | eptr++; | eptr++; |
4037 | } | } |
# | Line 3227 for (;;) | Line 4040 for (;;) |
4040 | case OP_WHITESPACE: | case OP_WHITESPACE: |
4041 | for (i = min; i < max; i++) | for (i = min; i < max; i++) |
4042 | { | { |
4043 | if (eptr >= md->end_subject || (pcre_ctypes[*eptr] & ctype_space) == 0) | if (eptr >= md->end_subject || (md->ctypes[*eptr] & ctype_space) == 0) |
4044 | break; | break; |
4045 | eptr++; | eptr++; |
4046 | } | } |
# | Line 3236 for (;;) | Line 4049 for (;;) |
4049 | case OP_NOT_WORDCHAR: | case OP_NOT_WORDCHAR: |
4050 | for (i = min; i < max; i++) | for (i = min; i < max; i++) |
4051 | { | { |
4052 | if (eptr >= md->end_subject || (pcre_ctypes[*eptr] & ctype_word) != 0) | if (eptr >= md->end_subject || (md->ctypes[*eptr] & ctype_word) != 0) |
4053 | break; | break; |
4054 | eptr++; | eptr++; |
4055 | } | } |
# | Line 3245 for (;;) | Line 4058 for (;;) |
4058 | case OP_WORDCHAR: | case OP_WORDCHAR: |
4059 | for (i = min; i < max; i++) | for (i = min; i < max; i++) |
4060 | { | { |
4061 | if (eptr >= md->end_subject || (pcre_ctypes[*eptr] & ctype_word) == 0) | if (eptr >= md->end_subject || (md->ctypes[*eptr] & ctype_word) == 0) |
4062 | break; | break; |
4063 | eptr++; | eptr++; |
4064 | } | } |
# | Line 3253 for (;;) | Line 4066 for (;;) |
4066 | } | } |
4067 | ||
4068 | while (eptr >= pp) | while (eptr >= pp) |
4069 | if (match(eptr--, ecode, offset_top, md)) return TRUE; | if (match(eptr--, ecode, offset_top, md, ims, FALSE, eptrb)) |
4070 | return TRUE; | |
4071 | return FALSE; | return FALSE; |
4072 | } | } |
4073 | /* Control never gets here */ | /* Control never gets here */ |
# | Line 3261 for (;;) | Line 4075 for (;;) |
4075 | /* There's been some horrible disaster. */ | /* There's been some horrible disaster. */ |
4076 | ||
4077 | default: | default: |
4078 | #ifdef DEBUG | DPRINTF(("Unknown opcode %d\n", *ecode)); |
printf("Unknown opcode %d\n", *ecode); | ||
#endif | ||
4079 | md->errorcode = PCRE_ERROR_UNKNOWN_NODE; | md->errorcode = PCRE_ERROR_UNKNOWN_NODE; |
4080 | return FALSE; | return FALSE; |
4081 | } | } |
# | Line 3278 for (;;) | Line 4090 for (;;) |
4090 | ||
4091 | ||
4092 | ||
4093 | ||
4094 | /************************************************* | /************************************************* |
4095 | * Execute a Regular Expression * | * Execute a Regular Expression * |
4096 | *************************************************/ | *************************************************/ |
# | Line 3305 int | Line 4118 int |
4118 | pcre_exec(const pcre *external_re, const pcre_extra *external_extra, | pcre_exec(const pcre *external_re, const pcre_extra *external_extra, |
4119 | const char *subject, int length, int options, int *offsets, int offsetcount) | const char *subject, int length, int options, int *offsets, int offsetcount) |
4120 | { | { |
4121 | int resetcount; | int resetcount, ocount; |
int ocount = offsetcount; | ||
4122 | int first_char = -1; | int first_char = -1; |
4123 | int ims = 0; | |
4124 | match_data match_block; | match_data match_block; |
4125 | const uschar *start_bits = NULL; | const uschar *start_bits = NULL; |
4126 | const uschar *start_match = (uschar *)subject; | const uschar *start_match = (const uschar *)subject; |
4127 | const uschar *end_subject; | const uschar *end_subject; |
4128 | const real_pcre *re = (const real_pcre *)external_re; | const real_pcre *re = (const real_pcre *)external_re; |
4129 | const real_pcre_extra *extra = (const real_pcre_extra *)external_extra; | const real_pcre_extra *extra = (const real_pcre_extra *)external_extra; |
4130 | BOOL using_temporary_offsets = FALSE; | |
4131 | BOOL anchored = ((re->options | options) & PCRE_ANCHORED) != 0; | BOOL anchored = ((re->options | options) & PCRE_ANCHORED) != 0; |
4132 | BOOL startline = (re->options & PCRE_STARTLINE) != 0; | BOOL startline = (re->options & PCRE_STARTLINE) != 0; |
4133 | ||
# | Line 3327 match_block.start_subject = (const uscha | Line 4141 match_block.start_subject = (const uscha |
4141 | match_block.end_subject = match_block.start_subject + length; | match_block.end_subject = match_block.start_subject + length; |
4142 | end_subject = match_block.end_subject; | end_subject = match_block.end_subject; |
4143 | ||
4144 | match_block.caseless = ((re->options | options) & PCRE_CASELESS) != 0; | match_block.endonly = (re->options & PCRE_DOLLAR_ENDONLY) != 0; |
match_block.runtime_caseless = match_block.caseless && | ||
(re->options & PCRE_CASELESS) == 0; | ||
match_block.multiline = ((re->options | options) & PCRE_MULTILINE) != 0; | ||
match_block.dotall = ((re->options | options) & PCRE_DOTALL) != 0; | ||
match_block.endonly = ((re->options | options) & PCRE_DOLLAR_ENDONLY) != 0; | ||
4145 | ||
4146 | match_block.notbol = (options & PCRE_NOTBOL) != 0; | match_block.notbol = (options & PCRE_NOTBOL) != 0; |
4147 | match_block.noteol = (options & PCRE_NOTEOL) != 0; | match_block.noteol = (options & PCRE_NOTEOL) != 0; |
4148 | ||
4149 | match_block.errorcode = PCRE_ERROR_NOMATCH; /* Default error */ | match_block.errorcode = PCRE_ERROR_NOMATCH; /* Default error */ |
4150 | ||
4151 | match_block.lcc = re->tables + lcc_offset; | |
4152 | match_block.ctypes = re->tables + ctypes_offset; | |
4153 | ||
4154 | /* The ims options can vary during the matching as a result of the presence | |
4155 | of (?ims) items in the pattern. They are kept in a local variable so that | |
4156 | restoring at the exit of a group is easy. */ | |
4157 | ||
4158 | ims = re->options & (PCRE_CASELESS|PCRE_MULTILINE|PCRE_DOTALL); | |
4159 | ||
4160 | /* If the expression has got more back references than the offsets supplied can | /* If the expression has got more back references than the offsets supplied can |
4161 | hold, we get a temporary bit of working store to use during the matching. | hold, we get a temporary bit of working store to use during the matching. |
4162 | Otherwise, we can use the vector supplied, rounding down the size of it to a | Otherwise, we can use the vector supplied, rounding down its size to a multiple |
4163 | multiple of 2. */ | of 3. */ |
4164 | ||
4165 | ocount = offsetcount - (offsetcount % 3); | |
4166 | ||
4167 | ocount &= (-2); | if (re->top_backref > 0 && re->top_backref >= ocount/3) |
if (re->top_backref > 0 && re->top_backref + 1 >= ocount/2) | ||
4168 | { | { |
4169 | ocount = re->top_backref * 2 + 2; | ocount = re->top_backref * 3 + 3; |
4170 | match_block.offset_vector = (pcre_malloc)(ocount * sizeof(int)); | match_block.offset_vector = (int *)(pcre_malloc)(ocount * sizeof(int)); |
4171 | if (match_block.offset_vector == NULL) return PCRE_ERROR_NOMEMORY; | if (match_block.offset_vector == NULL) return PCRE_ERROR_NOMEMORY; |
4172 | #ifdef DEBUG | using_temporary_offsets = TRUE; |
4173 | printf("Got memory to hold back references\n"); | DPRINTF(("Got memory to hold back references\n")); |
#endif | ||
4174 | } | } |
4175 | else match_block.offset_vector = offsets; | else match_block.offset_vector = offsets; |
4176 | ||
4177 | match_block.offset_end = ocount; | match_block.offset_end = ocount; |
4178 | match_block.offset_max = (2*ocount)/3; | |
4179 | match_block.offset_overflow = FALSE; | match_block.offset_overflow = FALSE; |
4180 | ||
4181 | /* Compute the minimum number of offsets that we need to reset each time. Doing | /* Compute the minimum number of offsets that we need to reset each time. Doing |
# | Line 3367 in the pattern. */ | Line 4185 in the pattern. */ |
4185 | resetcount = 2 + re->top_bracket * 2; | resetcount = 2 + re->top_bracket * 2; |
4186 | if (resetcount > offsetcount) resetcount = ocount; | if (resetcount > offsetcount) resetcount = ocount; |
4187 | ||
4188 | /* If MULTILINE is set at exec time but was not set at compile time, and the | /* Reset the working variable associated with each extraction. These should |
4189 | anchored flag is set, we must re-check because a setting provoked by ^ in the | never be used unless previously set, but they get saved and restored, and so we |
4190 | pattern is not right in multi-line mode. Calling is_anchored() again here does | initialize them to avoid reading uninitialized locations. */ |
the right check, because multiline is now set. If it now yields FALSE, the | ||
expression must have had ^ starting some of its branches. Check to see if | ||
that is true for *all* branches, and if so, set the startline flag. */ | ||
4191 | ||
4192 | if (match_block. multiline && anchored && (re->options & PCRE_MULTILINE) == 0 && | if (match_block.offset_vector != NULL) |
!is_anchored(re->code, match_block.multiline)) | ||
4193 | { | { |
4194 | anchored = FALSE; | register int *iptr = match_block.offset_vector + ocount; |
4195 | if (is_startline(re->code)) startline = TRUE; | register int *iend = iptr - resetcount/2 + 1; |
4196 | while (--iptr >= iend) *iptr = -1; | |
4197 | } | } |
4198 | ||
4199 | /* Set up the first character to match, if available. The first_char value is | /* Set up the first character to match, if available. The first_char value is |
4200 | never set for an anchored regular expression, but the anchoring may be forced | never set for an anchored regular expression, but the anchoring may be forced |
4201 | at run time, so we have to test for anchoring. The first char may be unset for | at run time, so we have to test for anchoring. The first char may be unset for |
4202 | an unanchored pattern, of course. If there's no first char and the pattern was | an unanchored pattern, of course. If there's no first char and the pattern was |
4203 | studied, the may be a bitmap of possible first characters. However, we can | studied, there may be a bitmap of possible first characters. */ |
use this only if the caseless state of the studying was correct. */ | ||
4204 | ||
4205 | if (!anchored) | if (!anchored) |
4206 | { | { |
4207 | if ((re->options & PCRE_FIRSTSET) != 0) | if ((re->options & PCRE_FIRSTSET) != 0) |
4208 | { | { |
4209 | first_char = re->first_char; | first_char = re->first_char; |
4210 | if (match_block.caseless) first_char = pcre_lcc[first_char]; | if ((ims & PCRE_CASELESS) != 0) first_char = match_block.lcc[first_char]; |
4211 | } | } |
4212 | else | else |
4213 | if (!startline && extra != NULL && | if (!startline && extra != NULL && |
4214 | (extra->options & PCRE_STUDY_MAPPED) != 0 && | (extra->options & PCRE_STUDY_MAPPED) != 0) |
((extra->options & PCRE_STUDY_CASELESS) != 0) == match_block.caseless) | ||
4215 | start_bits = extra->start_bits; | start_bits = extra->start_bits; |
4216 | } | } |
4217 | ||
# | Line 3406 if (!anchored) | Line 4219 if (!anchored) |
4219 | ||
4220 | do | do |
4221 | { | { |
4222 | int rc; | |
4223 | register int *iptr = match_block.offset_vector; | register int *iptr = match_block.offset_vector; |
4224 | register int *iend = iptr + resetcount; | register int *iend = iptr + resetcount; |
4225 | ||
# | Line 3417 do | Line 4231 do |
4231 | ||
4232 | if (first_char >= 0) | if (first_char >= 0) |
4233 | { | { |
4234 | if (match_block.caseless) | if ((ims & PCRE_CASELESS) != 0) |
4235 | while (start_match < end_subject && pcre_lcc[*start_match] != first_char) | while (start_match < end_subject && |
4236 | match_block.lcc[*start_match] != first_char) | |
4237 | start_match++; | start_match++; |
4238 | else | else |
4239 | while (start_match < end_subject && *start_match != first_char) | while (start_match < end_subject && *start_match != first_char) |
# | Line 3447 do | Line 4262 do |
4262 | } | } |
4263 | } | } |
4264 | ||
4265 | #ifdef DEBUG | #ifdef DEBUG /* Sigh. Some compilers never learn. */ |
4266 | printf(">>>> Match against: "); | printf(">>>> Match against: "); |
4267 | pchars(start_match, end_subject - start_match, TRUE, &match_block); | pchars(start_match, end_subject - start_match, TRUE, &match_block); |
4268 | printf("\n"); | printf("\n"); |
4269 | #endif | #endif |
4270 | ||
4271 | /* When a match occurs, substrings will be set for all internal extractions; | /* When a match occurs, substrings will be set for all internal extractions; |
4272 | we just need to set up the whole thing as substring 0 before returning. If | we just need to set up the whole thing as substring 0 before returning. If |
4273 | there were too many extractions, set the return code to zero. In the case | there were too many extractions, set the return code to zero. In the case |
4274 | where we had to get some local store to hold offsets for backreferences, copy | where we had to get some local store to hold offsets for backreferences, copy |
4275 | those back references that we can. In this case there need not be overflow | those back references that we can. In this case there need not be overflow |
4276 | if certain parts of the pattern were not used. | if certain parts of the pattern were not used. */ |
4277 | ||
4278 | if (!match(start_match, re->code, 2, &match_block, ims, FALSE, start_match)) | |
4279 | continue; | |
4280 | ||
4281 | Before starting the match, we have to set up a longjmp() target to enable | /* Copy the offset information from temporary store if necessary */ |
the "cut" operation to fail a match completely without backtracking. */ | ||
4282 | ||
4283 | if (setjmp(match_block.fail_env) == 0 && | if (using_temporary_offsets) |
match(start_match, re->code, 2, &match_block)) | ||
4284 | { | { |
4285 | int rc; | if (offsetcount >= 4) |
if (ocount != offsetcount) | ||
4286 | { | { |
4287 | if (offsetcount >= 4) | memcpy(offsets + 2, match_block.offset_vector + 2, |
4288 | { | (offsetcount - 2) * sizeof(int)); |
4289 | memcpy(offsets + 2, match_block.offset_vector + 2, | DPRINTF(("Copied offsets from temporary memory\n")); |
(offsetcount - 2) * sizeof(int)); | ||
#ifdef DEBUG | ||
printf("Copied offsets; freeing temporary memory\n"); | ||
#endif | ||
} | ||
if (match_block.end_offset_top > offsetcount) | ||
match_block.offset_overflow = TRUE; | ||
#ifdef DEBUG | ||
printf("Freeing temporary memory\n"); | ||
#endif | ||
(pcre_free)(match_block.offset_vector); | ||
4290 | } | } |
4291 | if (match_block.end_offset_top > offsetcount) | |
4292 | match_block.offset_ove |