6 |
and semantics are as close as possible to those of the Perl 5 language. |
and semantics are as close as possible to those of the Perl 5 language. |
7 |
|
|
8 |
Written by Philip Hazel |
Written by Philip Hazel |
9 |
Copyright (c) 1997-2007 University of Cambridge |
Copyright (c) 1997-2008 University of Cambridge |
10 |
|
|
11 |
----------------------------------------------------------------------------- |
----------------------------------------------------------------------------- |
12 |
Redistribution and use in source and binary forms, with or without |
Redistribution and use in source and binary forms, with or without |
45 |
|
|
46 |
|
|
47 |
#ifdef HAVE_CONFIG_H |
#ifdef HAVE_CONFIG_H |
48 |
#include <config.h> |
#include "config.h" |
49 |
#endif |
#endif |
50 |
|
|
51 |
#define NLBLOCK md /* Block containing newline information */ |
#define NLBLOCK md /* Block containing newline information */ |
84 |
small value. ***NOTE*** If the start of this table is modified, the two tables |
small value. ***NOTE*** If the start of this table is modified, the two tables |
85 |
that follow must also be modified. */ |
that follow must also be modified. */ |
86 |
|
|
87 |
static uschar coptable[] = { |
static const uschar coptable[] = { |
88 |
0, /* End */ |
0, /* End */ |
89 |
0, 0, 0, 0, 0, /* \A, \G, \K, \B, \b */ |
0, 0, 0, 0, 0, /* \A, \G, \K, \B, \b */ |
90 |
0, 0, 0, 0, 0, 0, /* \D, \d, \S, \s, \W, \w */ |
0, 0, 0, 0, 0, 0, /* \D, \d, \S, \s, \W, \w */ |
91 |
0, 0, /* Any, Anybyte */ |
0, 0, 0, /* Any, AllAny, Anybyte */ |
92 |
0, 0, 0, /* NOTPROP, PROP, EXTUNI */ |
0, 0, 0, /* NOTPROP, PROP, EXTUNI */ |
93 |
0, 0, 0, 0, 0, /* \R, \H, \h, \V, \v */ |
0, 0, 0, 0, 0, /* \R, \H, \h, \V, \v */ |
94 |
0, 0, 0, 0, 0, /* \Z, \z, Opt, ^, $ */ |
0, 0, 0, 0, 0, /* \Z, \z, Opt, ^, $ */ |
130 |
0, /* CREF */ |
0, /* CREF */ |
131 |
0, /* RREF */ |
0, /* RREF */ |
132 |
0, /* DEF */ |
0, /* DEF */ |
133 |
0, 0 /* BRAZERO, BRAMINZERO */ |
0, 0, /* BRAZERO, BRAMINZERO */ |
134 |
|
0, 0, 0, 0, /* PRUNE, SKIP, THEN, COMMIT */ |
135 |
|
0, 0, 0 /* FAIL, ACCEPT, SKIPZERO */ |
136 |
}; |
}; |
137 |
|
|
138 |
/* These 2 tables allow for compact code for testing for \D, \d, \S, \s, \W, |
/* These 2 tables allow for compact code for testing for \D, \d, \S, \s, \W, |
139 |
and \w */ |
and \w */ |
140 |
|
|
141 |
static uschar toptable1[] = { |
static const uschar toptable1[] = { |
142 |
0, 0, 0, 0, 0, 0, |
0, 0, 0, 0, 0, 0, |
143 |
ctype_digit, ctype_digit, |
ctype_digit, ctype_digit, |
144 |
ctype_space, ctype_space, |
ctype_space, ctype_space, |
145 |
ctype_word, ctype_word, |
ctype_word, ctype_word, |
146 |
0 /* OP_ANY */ |
0, 0 /* OP_ANY, OP_ALLANY */ |
147 |
}; |
}; |
148 |
|
|
149 |
static uschar toptable2[] = { |
static const uschar toptable2[] = { |
150 |
0, 0, 0, 0, 0, 0, |
0, 0, 0, 0, 0, 0, |
151 |
ctype_digit, 0, |
ctype_digit, 0, |
152 |
ctype_space, 0, |
ctype_space, 0, |
153 |
ctype_word, 0, |
ctype_word, 0, |
154 |
1 /* OP_ANY */ |
1, 1 /* OP_ANY, OP_ALLANY */ |
155 |
}; |
}; |
156 |
|
|
157 |
|
|
223 |
rlevel function call recursion level |
rlevel function call recursion level |
224 |
recursing regex recursive call level |
recursing regex recursive call level |
225 |
|
|
226 |
Returns: > 0 => |
Returns: > 0 => number of match offset pairs placed in offsets |
227 |
= 0 => |
= 0 => offsets overflowed; longest matches are present |
228 |
-1 => failed to match |
-1 => failed to match |
229 |
< -1 => some kind of unexpected problem |
< -1 => some kind of unexpected problem |
230 |
|
|
694 |
break; |
break; |
695 |
|
|
696 |
/*-----------------------------------------------------------------*/ |
/*-----------------------------------------------------------------*/ |
697 |
|
case OP_SKIPZERO: |
698 |
|
code += 1 + GET(code, 2); |
699 |
|
while (*code == OP_ALT) code += GET(code, 1); |
700 |
|
ADD_ACTIVE(code - start_code + 1 + LINK_SIZE, 0); |
701 |
|
break; |
702 |
|
|
703 |
|
/*-----------------------------------------------------------------*/ |
704 |
case OP_CIRC: |
case OP_CIRC: |
705 |
if ((ptr == start_subject && (md->moptions & PCRE_NOTBOL) == 0) || |
if ((ptr == start_subject && (md->moptions & PCRE_NOTBOL) == 0) || |
706 |
((ims & PCRE_MULTILINE) != 0 && |
((ims & PCRE_MULTILINE) != 0 && |
739 |
|
|
740 |
/*-----------------------------------------------------------------*/ |
/*-----------------------------------------------------------------*/ |
741 |
case OP_ANY: |
case OP_ANY: |
742 |
if (clen > 0 && ((ims & PCRE_DOTALL) != 0 || !IS_NEWLINE(ptr))) |
if (clen > 0 && !IS_NEWLINE(ptr)) |
743 |
|
{ ADD_NEW(state_offset + 1, 0); } |
744 |
|
break; |
745 |
|
|
746 |
|
/*-----------------------------------------------------------------*/ |
747 |
|
case OP_ALLANY: |
748 |
|
if (clen > 0) |
749 |
{ ADD_NEW(state_offset + 1, 0); } |
{ ADD_NEW(state_offset + 1, 0); } |
750 |
break; |
break; |
751 |
|
|
865 |
/* ========================================================================== */ |
/* ========================================================================== */ |
866 |
/* These opcodes likewise inspect the subject character, but have an |
/* These opcodes likewise inspect the subject character, but have an |
867 |
argument that is not a data character. It is one of these opcodes: |
argument that is not a data character. It is one of these opcodes: |
868 |
OP_ANY, OP_DIGIT, OP_NOT_DIGIT, OP_WHITESPACE, OP_NOT_SPACE, OP_WORDCHAR, |
OP_ANY, OP_ALLANY, OP_DIGIT, OP_NOT_DIGIT, OP_WHITESPACE, OP_NOT_SPACE, |
869 |
OP_NOT_WORDCHAR. The value is loaded into d. */ |
OP_WORDCHAR, OP_NOT_WORDCHAR. The value is loaded into d. */ |
870 |
|
|
871 |
case OP_TYPEPLUS: |
case OP_TYPEPLUS: |
872 |
case OP_TYPEMINPLUS: |
case OP_TYPEMINPLUS: |
877 |
{ |
{ |
878 |
if ((c >= 256 && d != OP_DIGIT && d != OP_WHITESPACE && d != OP_WORDCHAR) || |
if ((c >= 256 && d != OP_DIGIT && d != OP_WHITESPACE && d != OP_WORDCHAR) || |
879 |
(c < 256 && |
(c < 256 && |
880 |
(d != OP_ANY || |
(d != OP_ANY || !IS_NEWLINE(ptr)) && |
|
(ims & PCRE_DOTALL) != 0 || |
|
|
!IS_NEWLINE(ptr) |
|
|
) && |
|
881 |
((ctypes[c] & toptable1[d]) ^ toptable2[d]) != 0)) |
((ctypes[c] & toptable1[d]) ^ toptable2[d]) != 0)) |
882 |
{ |
{ |
883 |
if (count > 0 && codevalue == OP_TYPEPOSPLUS) |
if (count > 0 && codevalue == OP_TYPEPOSPLUS) |
900 |
{ |
{ |
901 |
if ((c >= 256 && d != OP_DIGIT && d != OP_WHITESPACE && d != OP_WORDCHAR) || |
if ((c >= 256 && d != OP_DIGIT && d != OP_WHITESPACE && d != OP_WORDCHAR) || |
902 |
(c < 256 && |
(c < 256 && |
903 |
(d != OP_ANY || |
(d != OP_ANY || !IS_NEWLINE(ptr)) && |
|
(ims & PCRE_DOTALL) != 0 || |
|
|
!IS_NEWLINE(ptr) |
|
|
) && |
|
904 |
((ctypes[c] & toptable1[d]) ^ toptable2[d]) != 0)) |
((ctypes[c] & toptable1[d]) ^ toptable2[d]) != 0)) |
905 |
{ |
{ |
906 |
if (codevalue == OP_TYPEPOSQUERY) |
if (codevalue == OP_TYPEPOSQUERY) |
922 |
{ |
{ |
923 |
if ((c >= 256 && d != OP_DIGIT && d != OP_WHITESPACE && d != OP_WORDCHAR) || |
if ((c >= 256 && d != OP_DIGIT && d != OP_WHITESPACE && d != OP_WORDCHAR) || |
924 |
(c < 256 && |
(c < 256 && |
925 |
(d != OP_ANY || |
(d != OP_ANY || !IS_NEWLINE(ptr)) && |
|
(ims & PCRE_DOTALL) != 0 || |
|
|
!IS_NEWLINE(ptr) |
|
|
) && |
|
926 |
((ctypes[c] & toptable1[d]) ^ toptable2[d]) != 0)) |
((ctypes[c] & toptable1[d]) ^ toptable2[d]) != 0)) |
927 |
{ |
{ |
928 |
if (codevalue == OP_TYPEPOSSTAR) |
if (codevalue == OP_TYPEPOSSTAR) |
942 |
{ |
{ |
943 |
if ((c >= 256 && d != OP_DIGIT && d != OP_WHITESPACE && d != OP_WORDCHAR) || |
if ((c >= 256 && d != OP_DIGIT && d != OP_WHITESPACE && d != OP_WORDCHAR) || |
944 |
(c < 256 && |
(c < 256 && |
945 |
(d != OP_ANY || |
(d != OP_ANY || !IS_NEWLINE(ptr)) && |
|
(ims & PCRE_DOTALL) != 0 || |
|
|
!IS_NEWLINE(ptr) |
|
|
) && |
|
946 |
((ctypes[c] & toptable1[d]) ^ toptable2[d]) != 0)) |
((ctypes[c] & toptable1[d]) ^ toptable2[d]) != 0)) |
947 |
{ |
{ |
948 |
if (++count >= GET2(code, 1)) |
if (++count >= GET2(code, 1)) |
963 |
{ |
{ |
964 |
if ((c >= 256 && d != OP_DIGIT && d != OP_WHITESPACE && d != OP_WORDCHAR) || |
if ((c >= 256 && d != OP_DIGIT && d != OP_WHITESPACE && d != OP_WORDCHAR) || |
965 |
(c < 256 && |
(c < 256 && |
966 |
(d != OP_ANY || |
(d != OP_ANY || !IS_NEWLINE(ptr)) && |
|
(ims & PCRE_DOTALL) != 0 || |
|
|
!IS_NEWLINE(ptr) |
|
|
) && |
|
967 |
((ctypes[c] & toptable1[d]) ^ toptable2[d]) != 0)) |
((ctypes[c] & toptable1[d]) ^ toptable2[d]) != 0)) |
968 |
{ |
{ |
969 |
if (codevalue == OP_TYPEPOSUPTO) |
if (codevalue == OP_TYPEPOSUPTO) |
1078 |
int ncount = 0; |
int ncount = 0; |
1079 |
switch (c) |
switch (c) |
1080 |
{ |
{ |
|
case 0x000d: |
|
|
if (ptr + 1 < end_subject && ptr[1] == 0x0a) ncount = 1; |
|
|
/* Fall through */ |
|
|
case 0x000a: |
|
1081 |
case 0x000b: |
case 0x000b: |
1082 |
case 0x000c: |
case 0x000c: |
1083 |
case 0x0085: |
case 0x0085: |
1084 |
case 0x2028: |
case 0x2028: |
1085 |
case 0x2029: |
case 0x2029: |
1086 |
|
if ((md->moptions & PCRE_BSR_ANYCRLF) != 0) break; |
1087 |
|
goto ANYNL01; |
1088 |
|
|
1089 |
|
case 0x000d: |
1090 |
|
if (ptr + 1 < end_subject && ptr[1] == 0x0a) ncount = 1; |
1091 |
|
/* Fall through */ |
1092 |
|
|
1093 |
|
ANYNL01: |
1094 |
|
case 0x000a: |
1095 |
if (count > 0 && codevalue == OP_ANYNL_EXTRA + OP_TYPEPOSPLUS) |
if (count > 0 && codevalue == OP_ANYNL_EXTRA + OP_TYPEPOSPLUS) |
1096 |
{ |
{ |
1097 |
active_count--; /* Remove non-match possibility */ |
active_count--; /* Remove non-match possibility */ |
1100 |
count++; |
count++; |
1101 |
ADD_NEW_DATA(-state_offset, count, ncount); |
ADD_NEW_DATA(-state_offset, count, ncount); |
1102 |
break; |
break; |
1103 |
|
|
1104 |
default: |
default: |
1105 |
break; |
break; |
1106 |
} |
} |
1317 |
int ncount = 0; |
int ncount = 0; |
1318 |
switch (c) |
switch (c) |
1319 |
{ |
{ |
|
case 0x000d: |
|
|
if (ptr + 1 < end_subject && ptr[1] == 0x0a) ncount = 1; |
|
|
/* Fall through */ |
|
|
case 0x000a: |
|
1320 |
case 0x000b: |
case 0x000b: |
1321 |
case 0x000c: |
case 0x000c: |
1322 |
case 0x0085: |
case 0x0085: |
1323 |
case 0x2028: |
case 0x2028: |
1324 |
case 0x2029: |
case 0x2029: |
1325 |
|
if ((md->moptions & PCRE_BSR_ANYCRLF) != 0) break; |
1326 |
|
goto ANYNL02; |
1327 |
|
|
1328 |
|
case 0x000d: |
1329 |
|
if (ptr + 1 < end_subject && ptr[1] == 0x0a) ncount = 1; |
1330 |
|
/* Fall through */ |
1331 |
|
|
1332 |
|
ANYNL02: |
1333 |
|
case 0x000a: |
1334 |
if (codevalue == OP_ANYNL_EXTRA + OP_TYPEPOSSTAR || |
if (codevalue == OP_ANYNL_EXTRA + OP_TYPEPOSSTAR || |
1335 |
codevalue == OP_ANYNL_EXTRA + OP_TYPEPOSQUERY) |
codevalue == OP_ANYNL_EXTRA + OP_TYPEPOSQUERY) |
1336 |
{ |
{ |
1339 |
} |
} |
1340 |
ADD_NEW_DATA(-(state_offset + count), 0, ncount); |
ADD_NEW_DATA(-(state_offset + count), 0, ncount); |
1341 |
break; |
break; |
1342 |
|
|
1343 |
default: |
default: |
1344 |
break; |
break; |
1345 |
} |
} |
1555 |
int ncount = 0; |
int ncount = 0; |
1556 |
switch (c) |
switch (c) |
1557 |
{ |
{ |
|
case 0x000d: |
|
|
if (ptr + 1 < end_subject && ptr[1] == 0x0a) ncount = 1; |
|
|
/* Fall through */ |
|
|
case 0x000a: |
|
1558 |
case 0x000b: |
case 0x000b: |
1559 |
case 0x000c: |
case 0x000c: |
1560 |
case 0x0085: |
case 0x0085: |
1561 |
case 0x2028: |
case 0x2028: |
1562 |
case 0x2029: |
case 0x2029: |
1563 |
|
if ((md->moptions & PCRE_BSR_ANYCRLF) != 0) break; |
1564 |
|
goto ANYNL03; |
1565 |
|
|
1566 |
|
case 0x000d: |
1567 |
|
if (ptr + 1 < end_subject && ptr[1] == 0x0a) ncount = 1; |
1568 |
|
/* Fall through */ |
1569 |
|
|
1570 |
|
ANYNL03: |
1571 |
|
case 0x000a: |
1572 |
if (codevalue == OP_ANYNL_EXTRA + OP_TYPEPOSUPTO) |
if (codevalue == OP_ANYNL_EXTRA + OP_TYPEPOSUPTO) |
1573 |
{ |
{ |
1574 |
active_count--; /* Remove non-match possibility */ |
active_count--; /* Remove non-match possibility */ |
1579 |
else |
else |
1580 |
{ ADD_NEW_DATA(-state_offset, count, ncount); } |
{ ADD_NEW_DATA(-state_offset, count, ncount); } |
1581 |
break; |
break; |
1582 |
|
|
1583 |
default: |
default: |
1584 |
break; |
break; |
1585 |
} |
} |
1760 |
case OP_ANYNL: |
case OP_ANYNL: |
1761 |
if (clen > 0) switch(c) |
if (clen > 0) switch(c) |
1762 |
{ |
{ |
|
case 0x000a: |
|
1763 |
case 0x000b: |
case 0x000b: |
1764 |
case 0x000c: |
case 0x000c: |
1765 |
case 0x0085: |
case 0x0085: |
1766 |
case 0x2028: |
case 0x2028: |
1767 |
case 0x2029: |
case 0x2029: |
1768 |
|
if ((md->moptions & PCRE_BSR_ANYCRLF) != 0) break; |
1769 |
|
|
1770 |
|
case 0x000a: |
1771 |
ADD_NEW(state_offset + 1, 0); |
ADD_NEW(state_offset + 1, 0); |
1772 |
break; |
break; |
1773 |
|
|
1774 |
case 0x000d: |
case 0x000d: |
1775 |
if (ptr + 1 < end_subject && ptr[1] == 0x0a) |
if (ptr + 1 < end_subject && ptr[1] == 0x0a) |
1776 |
{ |
{ |
2160 |
|
|
2161 |
/* ========================================================================== */ |
/* ========================================================================== */ |
2162 |
/* These are the opcodes for fancy brackets of various kinds. We have |
/* These are the opcodes for fancy brackets of various kinds. We have |
2163 |
to use recursion in order to handle them. */ |
to use recursion in order to handle them. The "always failing" assersion |
2164 |
|
(?!) is optimised when compiling to OP_FAIL, so we have to support that, |
2165 |
|
though the other "backtracking verbs" are not supported. */ |
2166 |
|
|
2167 |
|
case OP_FAIL: |
2168 |
|
break; |
2169 |
|
|
2170 |
case OP_ASSERT: |
case OP_ASSERT: |
2171 |
case OP_ASSERT_NOT: |
case OP_ASSERT_NOT: |
2598 |
md->moptions = options; |
md->moptions = options; |
2599 |
md->poptions = re->options; |
md->poptions = re->options; |
2600 |
|
|
2601 |
|
/* If the BSR option is not set at match time, copy what was set |
2602 |
|
at compile time. */ |
2603 |
|
|
2604 |
|
if ((md->moptions & (PCRE_BSR_ANYCRLF|PCRE_BSR_UNICODE)) == 0) |
2605 |
|
{ |
2606 |
|
if ((re->options & (PCRE_BSR_ANYCRLF|PCRE_BSR_UNICODE)) != 0) |
2607 |
|
md->moptions |= re->options & (PCRE_BSR_ANYCRLF|PCRE_BSR_UNICODE); |
2608 |
|
#ifdef BSR_ANYCRLF |
2609 |
|
else md->moptions |= PCRE_BSR_ANYCRLF; |
2610 |
|
#endif |
2611 |
|
} |
2612 |
|
|
2613 |
/* Handle different types of newline. The three bits give eight cases. If |
/* Handle different types of newline. The three bits give eight cases. If |
2614 |
nothing is set at run time, whatever was used at compile time applies. */ |
nothing is set at run time, whatever was used at compile time applies. */ |
2615 |
|
|
2680 |
used in a loop when finding where to start. */ |
used in a loop when finding where to start. */ |
2681 |
|
|
2682 |
lcc = md->tables + lcc_offset; |
lcc = md->tables + lcc_offset; |
2683 |
startline = (re->options & PCRE_STARTLINE) != 0; |
startline = (re->flags & PCRE_STARTLINE) != 0; |
2684 |
firstline = (re->options & PCRE_FIRSTLINE) != 0; |
firstline = (re->options & PCRE_FIRSTLINE) != 0; |
2685 |
|
|
2686 |
/* Set up the first character to match, if available. The first_byte value is |
/* Set up the first character to match, if available. The first_byte value is |
2691 |
|
|
2692 |
if (!anchored) |
if (!anchored) |
2693 |
{ |
{ |
2694 |
if ((re->options & PCRE_FIRSTSET) != 0) |
if ((re->flags & PCRE_FIRSTSET) != 0) |
2695 |
{ |
{ |
2696 |
first_byte = re->first_byte & 255; |
first_byte = re->first_byte & 255; |
2697 |
if ((first_byte_caseless = ((re->first_byte & REQ_CASELESS) != 0)) == TRUE) |
if ((first_byte_caseless = ((re->first_byte & REQ_CASELESS) != 0)) == TRUE) |
2708 |
/* For anchored or unanchored matches, there may be a "last known required |
/* For anchored or unanchored matches, there may be a "last known required |
2709 |
character" set. */ |
character" set. */ |
2710 |
|
|
2711 |
if ((re->options & PCRE_REQCHSET) != 0) |
if ((re->flags & PCRE_REQCHSET) != 0) |
2712 |
{ |
{ |
2713 |
req_byte = re->req_byte & 255; |
req_byte = re->req_byte & 255; |
2714 |
req_byte_caseless = (re->req_byte & REQ_CASELESS) != 0; |
req_byte_caseless = (re->req_byte & REQ_CASELESS) != 0; |
2878 |
} |
} |
2879 |
if (current_subject > end_subject) break; |
if (current_subject > end_subject) break; |
2880 |
|
|
2881 |
/* If we have just passed a CR and the newline option is CRLF or ANY or |
/* If we have just passed a CR and we are now at a LF, and the pattern does |
2882 |
ANYCRLF, and we are now at a LF, advance the match position by one more |
not contain any explicit matches for \r or \n, and the newline option is CRLF |
2883 |
character. */ |
or ANY or ANYCRLF, advance the match position by one more character. */ |
2884 |
|
|
2885 |
if (current_subject[-1] == '\r' && |
if (current_subject[-1] == '\r' && |
2886 |
(md->nltype == NLTYPE_ANY || |
current_subject < end_subject && |
2887 |
md->nltype == NLTYPE_ANYCRLF || |
*current_subject == '\n' && |
2888 |
md->nllen == 2) && |
(re->flags & PCRE_HASCRORLF) == 0 && |
2889 |
current_subject < end_subject && |
(md->nltype == NLTYPE_ANY || |
2890 |
*current_subject == '\n') |
md->nltype == NLTYPE_ANYCRLF || |
2891 |
|
md->nllen == 2)) |
2892 |
current_subject++; |
current_subject++; |
2893 |
|
|
2894 |
} /* "Bumpalong" loop */ |
} /* "Bumpalong" loop */ |