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) 1998 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 |
49 |
#include "internal.h" |
#include "internal.h" |
50 |
|
|
51 |
|
|
52 |
|
/* Allow compilation as C++ source code, should anybody want to do that. */ |
53 |
|
|
54 |
|
#ifdef __cplusplus |
55 |
|
#define class pcre_class |
56 |
|
#endif |
57 |
|
|
58 |
|
|
59 |
/* 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 */ |
60 |
|
|
61 |
static char rep_min[] = { 0, 0, 1, 1, 0, 0 }; |
static const char rep_min[] = { 0, 0, 1, 1, 0, 0 }; |
62 |
static char rep_max[] = { 0, 0, 0, 0, 1, 1 }; |
static const char rep_max[] = { 0, 0, 0, 0, 1, 1 }; |
63 |
|
|
64 |
/* Text forms of OP_ values and things, for debugging */ |
/* Text forms of OP_ values and things, for debugging (not all used) */ |
65 |
|
|
66 |
#ifdef DEBUG |
#ifdef DEBUG |
67 |
static const char *OP_names[] = { |
static const char *OP_names[] = { |
72 |
"*", "*?", "+", "+?", "?", "??", "{", "{", "{", |
"*", "*?", "+", "+?", "?", "??", "{", "{", "{", |
73 |
"*", "*?", "+", "+?", "?", "??", "{", "{", "{", |
"*", "*?", "+", "+?", "?", "??", "{", "{", "{", |
74 |
"*", "*?", "+", "+?", "?", "??", "{", "{", |
"*", "*?", "+", "+?", "?", "??", "{", "{", |
75 |
"class", "Ref", |
"class", "negclass", "Ref", |
76 |
"Alt", "Ket", "KetRmax", "KetRmin", "Assert", "Assert not", "Once", |
"Alt", "Ket", "KetRmax", "KetRmin", "Assert", "Assert not", "Once", |
77 |
"Brazero", "Braminzero", "Bra" |
"Brazero", "Braminzero", "Bra" |
78 |
}; |
}; |
83 |
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 |
84 |
is invalid. */ |
is invalid. */ |
85 |
|
|
86 |
static short int escapes[] = { |
static const short int escapes[] = { |
87 |
0, 0, 0, 0, 0, 0, 0, 0, /* 0 - 7 */ |
0, 0, 0, 0, 0, 0, 0, 0, /* 0 - 7 */ |
88 |
0, 0, ':', ';', '<', '=', '>', '?', /* 8 - ? */ |
0, 0, ':', ';', '<', '=', '>', '?', /* 8 - ? */ |
89 |
'@', -ESC_A, -ESC_B, 0, -ESC_D, 0, 0, 0, /* @ - G */ |
'@', -ESC_A, -ESC_B, 0, -ESC_D, 0, 0, 0, /* @ - G */ |
98 |
|
|
99 |
/* Definition to allow mutual recursion */ |
/* Definition to allow mutual recursion */ |
100 |
|
|
101 |
static BOOL compile_regex(int, int *, uschar **, const uschar **, const char **); |
static BOOL |
102 |
|
compile_regex(int, int *, uschar **, const uschar **, const char **); |
103 |
|
|
104 |
/* Structure for passing "static" information around between the functions |
/* Structure for passing "static" information around between the functions |
105 |
doing the matching, so that they are thread-safe. */ |
doing the matching, so that they are thread-safe. */ |
264 |
case OP_KETRMIN: |
case OP_KETRMIN: |
265 |
return TRUE; |
return TRUE; |
266 |
|
|
267 |
|
/* Skip over entire bracket groups with zero lower bound */ |
268 |
|
|
269 |
|
case OP_BRAZERO: |
270 |
|
case OP_BRAMINZERO: |
271 |
|
cc++; |
272 |
|
/* Fall through */ |
273 |
|
|
274 |
/* Skip over assertive subpatterns */ |
/* Skip over assertive subpatterns */ |
275 |
|
|
276 |
case OP_ASSERT: |
case OP_ASSERT: |
285 |
case OP_EOD: |
case OP_EOD: |
286 |
case OP_CIRC: |
case OP_CIRC: |
287 |
case OP_DOLL: |
case OP_DOLL: |
|
case OP_BRAZERO: |
|
|
case OP_BRAMINZERO: |
|
288 |
case OP_NOT_WORD_BOUNDARY: |
case OP_NOT_WORD_BOUNDARY: |
289 |
case OP_WORD_BOUNDARY: |
case OP_WORD_BOUNDARY: |
290 |
cc++; |
cc++; |
319 |
/* Check a class or a back reference for a zero minimum */ |
/* Check a class or a back reference for a zero minimum */ |
320 |
|
|
321 |
case OP_CLASS: |
case OP_CLASS: |
322 |
|
case OP_NEGCLASS: |
323 |
case OP_REF: |
case OP_REF: |
324 |
cc += (*cc == OP_REF)? 2 : 33; |
cc += (*cc == OP_REF)? 2 : 33; |
325 |
|
|
684 |
|
|
685 |
case '[': |
case '[': |
686 |
previous = code; |
previous = code; |
|
*code++ = OP_CLASS; |
|
687 |
|
|
688 |
/* If the first character is '^', set the negation flag */ |
/* If the first character is '^', set the negation flag, and use a |
689 |
|
different opcode. This only matters if caseless matching is specified at |
690 |
|
runtime. */ |
691 |
|
|
692 |
if ((c = *(++ptr)) == '^') |
if ((c = *(++ptr)) == '^') |
693 |
{ |
{ |
694 |
negate_class = TRUE; |
negate_class = TRUE; |
695 |
|
*code++ = OP_NEGCLASS; |
696 |
c = *(++ptr); |
c = *(++ptr); |
697 |
} |
} |
698 |
else negate_class = FALSE; |
else |
699 |
|
{ |
700 |
|
negate_class = FALSE; |
701 |
|
*code++ = OP_CLASS; |
702 |
|
} |
703 |
|
|
704 |
/* Keep a count of chars so that we can optimize the case of just a single |
/* Keep a count of chars so that we can optimize the case of just a single |
705 |
character. */ |
character. */ |
1035 |
/* If previous was a character class or a back reference, we put the repeat |
/* If previous was a character class or a back reference, we put the repeat |
1036 |
stuff after it. */ |
stuff after it. */ |
1037 |
|
|
1038 |
else if (*previous == OP_CLASS || *previous == OP_REF) |
else if (*previous == OP_CLASS || *previous == OP_NEGCLASS || |
1039 |
|
*previous == OP_REF) |
1040 |
{ |
{ |
1041 |
if (repeat_min == 0 && repeat_max == -1) |
if (repeat_min == 0 && repeat_max == -1) |
1042 |
*code++ = OP_CRSTAR + repeat_type; |
*code++ = OP_CRSTAR + repeat_type; |
1308 |
the next state. */ |
the next state. */ |
1309 |
|
|
1310 |
previous[1] = length; |
previous[1] = length; |
1311 |
ptr--; |
if (length < 255) ptr--; |
1312 |
break; |
break; |
1313 |
} |
} |
1314 |
} /* end of big loop */ |
} /* end of big loop */ |
2062 |
case OP_MINUPTO: |
case OP_MINUPTO: |
2063 |
if (isprint(c = code[3])) printf(" %c{", c); |
if (isprint(c = code[3])) printf(" %c{", c); |
2064 |
else printf(" \\x%02x{", c); |
else printf(" \\x%02x{", c); |
2065 |
if (*code != OP_EXACT) printf(","); |
if (*code != OP_EXACT) printf("0,"); |
2066 |
printf("%d}", (code[1] << 8) + code[2]); |
printf("%d}", (code[1] << 8) + code[2]); |
2067 |
if (*code == OP_MINUPTO) printf("?"); |
if (*code == OP_MINUPTO) printf("?"); |
2068 |
code += 3; |
code += 3; |
2111 |
goto CLASS_REF_REPEAT; |
goto CLASS_REF_REPEAT; |
2112 |
|
|
2113 |
case OP_CLASS: |
case OP_CLASS: |
2114 |
|
case OP_NEGCLASS: |
2115 |
{ |
{ |
2116 |
int i, min, max; |
int i, min, max; |
2117 |
|
|
2118 |
code++; |
if (*code++ == OP_CLASS) printf(" ["); |
2119 |
printf(" ["); |
else printf(" ^["); |
2120 |
|
|
2121 |
for (i = 0; i < 256; i++) |
for (i = 0; i < 256; i++) |
2122 |
{ |
{ |
2736 |
item to see if there is repeat information following. Then obey similar |
item to see if there is repeat information following. Then obey similar |
2737 |
code to character type repeats - written out again for speed. If caseless |
code to character type repeats - written out again for speed. If caseless |
2738 |
matching was set at runtime but not at compile time, we have to check both |
matching was set at runtime but not at compile time, we have to check both |
2739 |
versions of a character. */ |
versions of a character, and we have to behave differently for positive and |
2740 |
|
negative classes. This is the only time where OP_CLASS and OP_NEGCLASS are |
2741 |
|
treated differently. */ |
2742 |
|
|
2743 |
case OP_CLASS: |
case OP_CLASS: |
2744 |
|
case OP_NEGCLASS: |
2745 |
{ |
{ |
2746 |
|
BOOL nasty_case = *ecode == OP_NEGCLASS && md->runtime_caseless; |
2747 |
const uschar *data = ecode + 1; /* Save for matching */ |
const uschar *data = ecode + 1; /* Save for matching */ |
2748 |
ecode += 33; /* Advance past the item */ |
ecode += 33; /* Advance past the item */ |
2749 |
|
|
2772 |
break; |
break; |
2773 |
|
|
2774 |
default: /* No repeat follows */ |
default: /* No repeat follows */ |
2775 |
if (eptr >= md->end_subject) return FALSE; |
min = max = 1; |
2776 |
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; |
|
2777 |
} |
} |
2778 |
|
|
2779 |
/* First, ensure the minimum number of matches are present. */ |
/* First, ensure the minimum number of matches are present. */ |
2782 |
{ |
{ |
2783 |
if (eptr >= md->end_subject) return FALSE; |
if (eptr >= md->end_subject) return FALSE; |
2784 |
c = *eptr++; |
c = *eptr++; |
2785 |
if ((data[c/8] & (1 << (c&7))) != 0) continue; |
|
2786 |
if (md->runtime_caseless) |
/* Either not runtime caseless, or it was a positive class. For |
2787 |
|
runtime caseless, continue if either case is in the map. */ |
2788 |
|
|
2789 |
|
if (!nasty_case) |
2790 |
{ |
{ |
2791 |
|
if ((data[c/8] & (1 << (c&7))) != 0) continue; |
2792 |
|
if (md->runtime_caseless) |
2793 |
|
{ |
2794 |
|
c = pcre_fcc[c]; |
2795 |
|
if ((data[c/8] & (1 << (c&7))) != 0) continue; |
2796 |
|
} |
2797 |
|
} |
2798 |
|
|
2799 |
|
/* Runtime caseless and it was a negative class. Continue only if |
2800 |
|
both cases are in the map. */ |
2801 |
|
|
2802 |
|
else |
2803 |
|
{ |
2804 |
|
if ((data[c/8] & (1 << (c&7))) == 0) return FALSE; |
2805 |
c = pcre_fcc[c]; |
c = pcre_fcc[c]; |
2806 |
if ((data[c/8] & (1 << (c&7))) != 0) continue; |
if ((data[c/8] & (1 << (c&7))) != 0) continue; |
2807 |
} |
} |
2808 |
|
|
2809 |
return FALSE; |
return FALSE; |
2810 |
} |
} |
2811 |
|
|
2824 |
if (match(eptr, ecode, offset_top, md)) return TRUE; |
if (match(eptr, ecode, offset_top, md)) return TRUE; |
2825 |
if (i >= max || eptr >= md->end_subject) return FALSE; |
if (i >= max || eptr >= md->end_subject) return FALSE; |
2826 |
c = *eptr++; |
c = *eptr++; |
2827 |
if ((data[c/8] & (1 << (c&7))) != 0) continue; |
|
2828 |
if (md->runtime_caseless) |
/* Either not runtime caseless, or it was a positive class. For |
2829 |
|
runtime caseless, continue if either case is in the map. */ |
2830 |
|
|
2831 |
|
if (!nasty_case) |
2832 |
|
{ |
2833 |
|
if ((data[c/8] & (1 << (c&7))) != 0) continue; |
2834 |
|
if (md->runtime_caseless) |
2835 |
|
{ |
2836 |
|
c = pcre_fcc[c]; |
2837 |
|
if ((data[c/8] & (1 << (c&7))) != 0) continue; |
2838 |
|
} |
2839 |
|
} |
2840 |
|
|
2841 |
|
/* Runtime caseless and it was a negative class. Continue only if |
2842 |
|
both cases are in the map. */ |
2843 |
|
|
2844 |
|
else |
2845 |
{ |
{ |
2846 |
|
if ((data[c/8] & (1 << (c&7))) == 0) return FALSE; |
2847 |
c = pcre_fcc[c]; |
c = pcre_fcc[c]; |
2848 |
if ((data[c/8] & (1 << (c&7))) != 0) continue; |
if ((data[c/8] & (1 << (c&7))) != 0) continue; |
2849 |
} |
} |
2850 |
|
|
2851 |
return FALSE; |
return FALSE; |
2852 |
} |
} |
2853 |
/* Control never gets here */ |
/* Control never gets here */ |
2862 |
{ |
{ |
2863 |
if (eptr >= md->end_subject) break; |
if (eptr >= md->end_subject) break; |
2864 |
c = *eptr; |
c = *eptr; |
2865 |
if ((data[c/8] & (1 << (c&7))) != 0) continue; |
|
2866 |
if (md->runtime_caseless) |
/* Either not runtime caseless, or it was a positive class. For |
2867 |
|
runtime caseless, continue if either case is in the map. */ |
2868 |
|
|
2869 |
|
if (!nasty_case) |
2870 |
|
{ |
2871 |
|
if ((data[c/8] & (1 << (c&7))) != 0) continue; |
2872 |
|
if (md->runtime_caseless) |
2873 |
|
{ |
2874 |
|
c = pcre_fcc[c]; |
2875 |
|
if ((data[c/8] & (1 << (c&7))) != 0) continue; |
2876 |
|
} |
2877 |
|
} |
2878 |
|
|
2879 |
|
/* Runtime caseless and it was a negative class. Continue only if |
2880 |
|
both cases are in the map. */ |
2881 |
|
|
2882 |
|
else |
2883 |
{ |
{ |
2884 |
|
if ((data[c/8] & (1 << (c&7))) == 0) break; |
2885 |
c = pcre_fcc[c]; |
c = pcre_fcc[c]; |
2886 |
if ((data[c/8] & (1 << (c&7))) != 0) continue; |
if ((data[c/8] & (1 << (c&7))) != 0) continue; |
2887 |
} |
} |
2888 |
|
|
2889 |
break; |
break; |
2890 |
} |
} |
2891 |
|
|
3372 |
since it's needed only for the extension \X option, and with any luck, a good |
since it's needed only for the extension \X option, and with any luck, a good |
3373 |
compiler will spot the tail recursion and compile it efficiently. |
compiler will spot the tail recursion and compile it efficiently. |
3374 |
|
|
3375 |
Arguments: The block containing the match data |
Arguments: |
3376 |
Returns: The return from setjump() |
eptr pointer in subject |
3377 |
|
ecode position in code |
3378 |
|
offset_top current top pointer |
3379 |
|
md pointer to "static" info for the match |
3380 |
|
|
3381 |
|
Returns: TRUE if matched |
3382 |
*/ |
*/ |
3383 |
|
|
3384 |
static int |
static BOOL |
3385 |
my_setjmp(match_data *match_block) |
match_with_setjmp(const uschar *eptr, const uschar *ecode, int offset_top, |
3386 |
|
match_data *match_block) |
3387 |
{ |
{ |
3388 |
return setjmp(match_block->fail_env); |
return setjmp(match_block->fail_env) == 0 && |
3389 |
|
match(eptr, ecode, offset_top, match_block); |
3390 |
} |
} |
3391 |
|
|
3392 |
|
|
3418 |
pcre_exec(const pcre *external_re, const pcre_extra *external_extra, |
pcre_exec(const pcre *external_re, const pcre_extra *external_extra, |
3419 |
const char *subject, int length, int options, int *offsets, int offsetcount) |
const char *subject, int length, int options, int *offsets, int offsetcount) |
3420 |
{ |
{ |
3421 |
int resetcount; |
int resetcount, ocount; |
|
int ocount = offsetcount; |
|
3422 |
int first_char = -1; |
int first_char = -1; |
3423 |
match_data match_block; |
match_data match_block; |
3424 |
const uschar *start_bits = NULL; |
const uschar *start_bits = NULL; |
3426 |
const uschar *end_subject; |
const uschar *end_subject; |
3427 |
const real_pcre *re = (const real_pcre *)external_re; |
const real_pcre *re = (const real_pcre *)external_re; |
3428 |
const real_pcre_extra *extra = (const real_pcre_extra *)external_extra; |
const real_pcre_extra *extra = (const real_pcre_extra *)external_extra; |
3429 |
|
BOOL using_temporary_offsets = FALSE; |
3430 |
BOOL anchored = ((re->options | options) & PCRE_ANCHORED) != 0; |
BOOL anchored = ((re->options | options) & PCRE_ANCHORED) != 0; |
3431 |
BOOL startline = (re->options & PCRE_STARTLINE) != 0; |
BOOL startline = (re->options & PCRE_STARTLINE) != 0; |
3432 |
|
|
3455 |
|
|
3456 |
/* 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 |
3457 |
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. |
3458 |
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 |
3459 |
multiple of 2. */ |
of 2. */ |
3460 |
|
|
3461 |
ocount &= (-2); |
ocount = offsetcount & (-2); |
3462 |
if (re->top_backref > 0 && re->top_backref + 1 >= ocount/2) |
if (re->top_backref > 0 && re->top_backref >= ocount/2) |
3463 |
{ |
{ |
3464 |
ocount = re->top_backref * 2 + 2; |
ocount = re->top_backref * 2 + 2; |
3465 |
match_block.offset_vector = (pcre_malloc)(ocount * sizeof(int)); |
match_block.offset_vector = (int *)(pcre_malloc)(ocount * sizeof(int)); |
3466 |
if (match_block.offset_vector == NULL) return PCRE_ERROR_NOMEMORY; |
if (match_block.offset_vector == NULL) return PCRE_ERROR_NOMEMORY; |
3467 |
|
using_temporary_offsets = TRUE; |
3468 |
DPRINTF(("Got memory to hold back references\n")); |
DPRINTF(("Got memory to hold back references\n")); |
3469 |
} |
} |
3470 |
else match_block.offset_vector = offsets; |
else match_block.offset_vector = offsets; |
3579 |
it unless PCRE_EXTRA is set, since only in that case is the "cut" operation |
it unless PCRE_EXTRA is set, since only in that case is the "cut" operation |
3580 |
enabled. */ |
enabled. */ |
3581 |
|
|
3582 |
if (((re->options & PCRE_EXTRA) != 0 && my_setjmp(&match_block) != 0) || |
if ((re->options & PCRE_EXTRA) != 0) |
3583 |
!match(start_match, re->code, 2, &match_block)) |
{ |
3584 |
continue; |
if (!match_with_setjmp(start_match, re->code, 2, &match_block)) |
3585 |
|
continue; |
3586 |
|
} |
3587 |
|
else if (!match(start_match, re->code, 2, &match_block)) continue; |
3588 |
|
|
3589 |
/* Copy the offset information from temporary store if necessary */ |
/* Copy the offset information from temporary store if necessary */ |
3590 |
|
|
3591 |
if (ocount != offsetcount) |
if (using_temporary_offsets) |
3592 |
{ |
{ |
3593 |
if (offsetcount >= 4) |
if (offsetcount >= 4) |
3594 |
{ |
{ |
3595 |
memcpy(offsets + 2, match_block.offset_vector + 2, |
memcpy(offsets + 2, match_block.offset_vector + 2, |
3596 |
(offsetcount - 2) * sizeof(int)); |
(offsetcount - 2) * sizeof(int)); |
3597 |
DPRINTF(("Copied offsets; freeing temporary memory\n")); |
DPRINTF(("Copied offsets from temporary memory\n")); |
3598 |
} |
} |
3599 |
if (match_block.end_offset_top > offsetcount) |
if (match_block.end_offset_top > offsetcount) |
3600 |
match_block.offset_overflow = TRUE; |
match_block.offset_overflow = TRUE; |
3618 |
match_block.errorcode == PCRE_ERROR_NOMATCH && |
match_block.errorcode == PCRE_ERROR_NOMATCH && |
3619 |
start_match++ < end_subject); |
start_match++ < end_subject); |
3620 |
|
|
3621 |
|
if (using_temporary_offsets) |
3622 |
|
{ |
3623 |
|
DPRINTF(("Freeing temporary memory\n")); |
3624 |
|
(pcre_free)(match_block.offset_vector); |
3625 |
|
} |
3626 |
|
|
3627 |
DPRINTF((">>>> returning %d\n", match_block.errorcode)); |
DPRINTF((">>>> returning %d\n", match_block.errorcode)); |
3628 |
|
|
3629 |
return match_block.errorcode; |
return match_block.errorcode; |