| 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-2010 University of Cambridge |
Copyright (c) 1997-2011 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 |
| 408 |
"different names for subpatterns of the same number are not allowed\0" |
"different names for subpatterns of the same number are not allowed\0" |
| 409 |
"(*MARK) must have an argument\0" |
"(*MARK) must have an argument\0" |
| 410 |
"this version of PCRE is not compiled with PCRE_UCP support\0" |
"this version of PCRE is not compiled with PCRE_UCP support\0" |
| 411 |
|
"\\c must be followed by an ASCII character\0" |
| 412 |
; |
; |
| 413 |
|
|
| 414 |
/* Table to identify digits and hex digits. This is used when compiling |
/* Table to identify digits and hex digits. This is used when compiling |
| 545 |
/* Definition to allow mutual recursion */ |
/* Definition to allow mutual recursion */ |
| 546 |
|
|
| 547 |
static BOOL |
static BOOL |
| 548 |
compile_regex(int, int, uschar **, const uschar **, int *, BOOL, BOOL, int, |
compile_regex(int, uschar **, const uschar **, int *, BOOL, BOOL, int, int *, |
| 549 |
int *, int *, branch_chain *, compile_data *, int *); |
int *, branch_chain *, compile_data *, int *); |
| 550 |
|
|
| 551 |
|
|
| 552 |
|
|
| 842 |
break; |
break; |
| 843 |
|
|
| 844 |
/* For \c, a following letter is upper-cased; then the 0x40 bit is flipped. |
/* For \c, a following letter is upper-cased; then the 0x40 bit is flipped. |
| 845 |
This coding is ASCII-specific, but then the whole concept of \cx is |
An error is given if the byte following \c is not an ASCII character. This |
| 846 |
|
coding is ASCII-specific, but then the whole concept of \cx is |
| 847 |
ASCII-specific. (However, an EBCDIC equivalent has now been added.) */ |
ASCII-specific. (However, an EBCDIC equivalent has now been added.) */ |
| 848 |
|
|
| 849 |
case CHAR_c: |
case CHAR_c: |
| 853 |
*errorcodeptr = ERR2; |
*errorcodeptr = ERR2; |
| 854 |
break; |
break; |
| 855 |
} |
} |
| 856 |
|
#ifndef EBCDIC /* ASCII/UTF-8 coding */ |
| 857 |
#ifndef EBCDIC /* ASCII/UTF-8 coding */ |
if (c > 127) /* Excludes all non-ASCII in either mode */ |
| 858 |
|
{ |
| 859 |
|
*errorcodeptr = ERR68; |
| 860 |
|
break; |
| 861 |
|
} |
| 862 |
if (c >= CHAR_a && c <= CHAR_z) c -= 32; |
if (c >= CHAR_a && c <= CHAR_z) c -= 32; |
| 863 |
c ^= 0x40; |
c ^= 0x40; |
| 864 |
#else /* EBCDIC coding */ |
#else /* EBCDIC coding */ |
| 865 |
if (c >= CHAR_a && c <= CHAR_z) c += 64; |
if (c >= CHAR_a && c <= CHAR_z) c += 64; |
| 866 |
c ^= 0xC0; |
c ^= 0xC0; |
| 867 |
#endif |
#endif |
| 1105 |
start at a parenthesis. It scans along a pattern's text looking for capturing |
start at a parenthesis. It scans along a pattern's text looking for capturing |
| 1106 |
subpatterns, and counting them. If it finds a named pattern that matches the |
subpatterns, and counting them. If it finds a named pattern that matches the |
| 1107 |
name it is given, it returns its number. Alternatively, if the name is NULL, it |
name it is given, it returns its number. Alternatively, if the name is NULL, it |
| 1108 |
returns when it reaches a given numbered subpattern. We know that if (?P< is |
returns when it reaches a given numbered subpattern. Recursion is used to keep |
| 1109 |
encountered, the name will be terminated by '>' because that is checked in the |
track of subpatterns that reset the capturing group numbers - the (?| feature. |
| 1110 |
first pass. Recursion is used to keep track of subpatterns that reset the |
|
| 1111 |
capturing group numbers - the (?| feature. |
This function was originally called only from the second pass, in which we know |
| 1112 |
|
that if (?< or (?' or (?P< is encountered, the name will be correctly |
| 1113 |
|
terminated because that is checked in the first pass. There is now one call to |
| 1114 |
|
this function in the first pass, to check for a recursive back reference by |
| 1115 |
|
name (so that we can make the whole group atomic). In this case, we need check |
| 1116 |
|
only up to the current position in the pattern, and that is still OK because |
| 1117 |
|
and previous occurrences will have been checked. To make this work, the test |
| 1118 |
|
for "end of pattern" is a check against cd->end_pattern in the main loop, |
| 1119 |
|
instead of looking for a binary zero. This means that the special first-pass |
| 1120 |
|
call can adjust cd->end_pattern temporarily. (Checks for binary zero while |
| 1121 |
|
processing items within the loop are OK, because afterwards the main loop will |
| 1122 |
|
terminate.) |
| 1123 |
|
|
| 1124 |
Arguments: |
Arguments: |
| 1125 |
ptrptr address of the current character pointer (updated) |
ptrptr address of the current character pointer (updated) |
| 1127 |
name name to seek, or NULL if seeking a numbered subpattern |
name name to seek, or NULL if seeking a numbered subpattern |
| 1128 |
lorn name length, or subpattern number if name is NULL |
lorn name length, or subpattern number if name is NULL |
| 1129 |
xmode TRUE if we are in /x mode |
xmode TRUE if we are in /x mode |
| 1130 |
|
utf8 TRUE if we are in UTF-8 mode |
| 1131 |
count pointer to the current capturing subpattern number (updated) |
count pointer to the current capturing subpattern number (updated) |
| 1132 |
|
|
| 1133 |
Returns: the number of the named subpattern, or -1 if not found |
Returns: the number of the named subpattern, or -1 if not found |
| 1135 |
|
|
| 1136 |
static int |
static int |
| 1137 |
find_parens_sub(uschar **ptrptr, compile_data *cd, const uschar *name, int lorn, |
find_parens_sub(uschar **ptrptr, compile_data *cd, const uschar *name, int lorn, |
| 1138 |
BOOL xmode, int *count) |
BOOL xmode, BOOL utf8, int *count) |
| 1139 |
{ |
{ |
| 1140 |
uschar *ptr = *ptrptr; |
uschar *ptr = *ptrptr; |
| 1141 |
int start_count = *count; |
int start_count = *count; |
| 1220 |
} |
} |
| 1221 |
|
|
| 1222 |
/* Past any initial parenthesis handling, scan for parentheses or vertical |
/* Past any initial parenthesis handling, scan for parentheses or vertical |
| 1223 |
bars. */ |
bars. Stop if we get to cd->end_pattern. Note that this is important for the |
| 1224 |
|
first-pass call when this value is temporarily adjusted to stop at the current |
| 1225 |
|
position. So DO NOT change this to a test for binary zero. */ |
| 1226 |
|
|
| 1227 |
for (; *ptr != 0; ptr++) |
for (; ptr < cd->end_pattern; ptr++) |
| 1228 |
{ |
{ |
| 1229 |
/* Skip over backslashed characters and also entire \Q...\E */ |
/* Skip over backslashed characters and also entire \Q...\E */ |
| 1230 |
|
|
| 1298 |
|
|
| 1299 |
if (xmode && *ptr == CHAR_NUMBER_SIGN) |
if (xmode && *ptr == CHAR_NUMBER_SIGN) |
| 1300 |
{ |
{ |
| 1301 |
while (*(++ptr) != 0 && *ptr != CHAR_NL) {}; |
ptr++; |
| 1302 |
|
while (*ptr != 0) |
| 1303 |
|
{ |
| 1304 |
|
if (IS_NEWLINE(ptr)) { ptr += cd->nllen - 1; break; } |
| 1305 |
|
ptr++; |
| 1306 |
|
#ifdef SUPPORT_UTF8 |
| 1307 |
|
if (utf8) while ((*ptr & 0xc0) == 0x80) ptr++; |
| 1308 |
|
#endif |
| 1309 |
|
} |
| 1310 |
if (*ptr == 0) goto FAIL_EXIT; |
if (*ptr == 0) goto FAIL_EXIT; |
| 1311 |
continue; |
continue; |
| 1312 |
} |
} |
| 1315 |
|
|
| 1316 |
if (*ptr == CHAR_LEFT_PARENTHESIS) |
if (*ptr == CHAR_LEFT_PARENTHESIS) |
| 1317 |
{ |
{ |
| 1318 |
int rc = find_parens_sub(&ptr, cd, name, lorn, xmode, count); |
int rc = find_parens_sub(&ptr, cd, name, lorn, xmode, utf8, count); |
| 1319 |
if (rc > 0) return rc; |
if (rc > 0) return rc; |
| 1320 |
if (*ptr == 0) goto FAIL_EXIT; |
if (*ptr == 0) goto FAIL_EXIT; |
| 1321 |
} |
} |
| 1361 |
name name to seek, or NULL if seeking a numbered subpattern |
name name to seek, or NULL if seeking a numbered subpattern |
| 1362 |
lorn name length, or subpattern number if name is NULL |
lorn name length, or subpattern number if name is NULL |
| 1363 |
xmode TRUE if we are in /x mode |
xmode TRUE if we are in /x mode |
| 1364 |
|
utf8 TRUE if we are in UTF-8 mode |
| 1365 |
|
|
| 1366 |
Returns: the number of the found subpattern, or -1 if not found |
Returns: the number of the found subpattern, or -1 if not found |
| 1367 |
*/ |
*/ |
| 1368 |
|
|
| 1369 |
static int |
static int |
| 1370 |
find_parens(compile_data *cd, const uschar *name, int lorn, BOOL xmode) |
find_parens(compile_data *cd, const uschar *name, int lorn, BOOL xmode, |
| 1371 |
|
BOOL utf8) |
| 1372 |
{ |
{ |
| 1373 |
uschar *ptr = (uschar *)cd->start_pattern; |
uschar *ptr = (uschar *)cd->start_pattern; |
| 1374 |
int count = 0; |
int count = 0; |
| 1381 |
|
|
| 1382 |
for (;;) |
for (;;) |
| 1383 |
{ |
{ |
| 1384 |
rc = find_parens_sub(&ptr, cd, name, lorn, xmode, &count); |
rc = find_parens_sub(&ptr, cd, name, lorn, xmode, utf8, &count); |
| 1385 |
if (rc > 0 || *ptr++ == 0) break; |
if (rc > 0 || *ptr++ == 0) break; |
| 1386 |
} |
} |
| 1387 |
|
|
| 1397 |
|
|
| 1398 |
/* This is called by several functions that scan a compiled expression looking |
/* This is called by several functions that scan a compiled expression looking |
| 1399 |
for a fixed first character, or an anchoring op code etc. It skips over things |
for a fixed first character, or an anchoring op code etc. It skips over things |
| 1400 |
that do not influence this. For some calls, a change of option is important. |
that do not influence this. For some calls, it makes sense to skip negative |
| 1401 |
For some calls, it makes sense to skip negative forward and all backward |
forward and all backward assertions, and also the \b assertion; for others it |
| 1402 |
assertions, and also the \b assertion; for others it does not. |
does not. |
| 1403 |
|
|
| 1404 |
Arguments: |
Arguments: |
| 1405 |
code pointer to the start of the group |
code pointer to the start of the group |
|
options pointer to external options |
|
|
optbit the option bit whose changing is significant, or |
|
|
zero if none are |
|
| 1406 |
skipassert TRUE if certain assertions are to be skipped |
skipassert TRUE if certain assertions are to be skipped |
| 1407 |
|
|
| 1408 |
Returns: pointer to the first significant opcode |
Returns: pointer to the first significant opcode |
| 1409 |
*/ |
*/ |
| 1410 |
|
|
| 1411 |
static const uschar* |
static const uschar* |
| 1412 |
first_significant_code(const uschar *code, int *options, int optbit, |
first_significant_code(const uschar *code, BOOL skipassert) |
|
BOOL skipassert) |
|
| 1413 |
{ |
{ |
| 1414 |
for (;;) |
for (;;) |
| 1415 |
{ |
{ |
| 1416 |
switch ((int)*code) |
switch ((int)*code) |
| 1417 |
{ |
{ |
|
case OP_OPT: |
|
|
if (optbit > 0 && ((int)code[1] & optbit) != (*options & optbit)) |
|
|
*options = (int)code[1]; |
|
|
code += 2; |
|
|
break; |
|
|
|
|
| 1418 |
case OP_ASSERT_NOT: |
case OP_ASSERT_NOT: |
| 1419 |
case OP_ASSERTBACK: |
case OP_ASSERTBACK: |
| 1420 |
case OP_ASSERTBACK_NOT: |
case OP_ASSERTBACK_NOT: |
| 1464 |
|
|
| 1465 |
Arguments: |
Arguments: |
| 1466 |
code points to the start of the pattern (the bracket) |
code points to the start of the pattern (the bracket) |
| 1467 |
options the compiling options |
utf8 TRUE in UTF-8 mode |
| 1468 |
atend TRUE if called when the pattern is complete |
atend TRUE if called when the pattern is complete |
| 1469 |
cd the "compile data" structure |
cd the "compile data" structure |
| 1470 |
|
|
| 1475 |
*/ |
*/ |
| 1476 |
|
|
| 1477 |
static int |
static int |
| 1478 |
find_fixedlength(uschar *code, int options, BOOL atend, compile_data *cd) |
find_fixedlength(uschar *code, BOOL utf8, BOOL atend, compile_data *cd) |
| 1479 |
{ |
{ |
| 1480 |
int length = -1; |
int length = -1; |
| 1481 |
|
|
| 1492 |
register int op = *cc; |
register int op = *cc; |
| 1493 |
switch (op) |
switch (op) |
| 1494 |
{ |
{ |
| 1495 |
|
/* We only need to continue for OP_CBRA (normal capturing bracket) and |
| 1496 |
|
OP_BRA (normal non-capturing bracket) because the other variants of these |
| 1497 |
|
opcodes are all concerned with unlimited repeated groups, which of course |
| 1498 |
|
are not of fixed length. They will cause a -1 response from the default |
| 1499 |
|
case of this switch. */ |
| 1500 |
|
|
| 1501 |
case OP_CBRA: |
case OP_CBRA: |
| 1502 |
case OP_BRA: |
case OP_BRA: |
| 1503 |
case OP_ONCE: |
case OP_ONCE: |
| 1504 |
case OP_COND: |
case OP_COND: |
| 1505 |
d = find_fixedlength(cc + ((op == OP_CBRA)? 2:0), options, atend, cd); |
d = find_fixedlength(cc + ((op == OP_CBRA)? 2:0), utf8, atend, cd); |
| 1506 |
if (d < 0) return d; |
if (d < 0) return d; |
| 1507 |
branchlength += d; |
branchlength += d; |
| 1508 |
do cc += GET(cc, 1); while (*cc == OP_ALT); |
do cc += GET(cc, 1); while (*cc == OP_ALT); |
| 1511 |
|
|
| 1512 |
/* Reached end of a branch; if it's a ket it is the end of a nested |
/* Reached end of a branch; if it's a ket it is the end of a nested |
| 1513 |
call. If it's ALT it is an alternation in a nested call. If it is |
call. If it's ALT it is an alternation in a nested call. If it is |
| 1514 |
END it's the end of the outer call. All can be handled by the same code. */ |
END it's the end of the outer call. All can be handled by the same code. |
| 1515 |
|
Note that we must not include the OP_KETRxxx opcodes here, because they |
| 1516 |
|
all imply an unlimited repeat. */ |
| 1517 |
|
|
| 1518 |
case OP_ALT: |
case OP_ALT: |
| 1519 |
case OP_KET: |
case OP_KET: |
|
case OP_KETRMAX: |
|
|
case OP_KETRMIN: |
|
| 1520 |
case OP_END: |
case OP_END: |
| 1521 |
if (length < 0) length = branchlength; |
if (length < 0) length = branchlength; |
| 1522 |
else if (length != branchlength) return -1; |
else if (length != branchlength) return -1; |
| 1534 |
cs = ce = (uschar *)cd->start_code + GET(cc, 1); /* Start subpattern */ |
cs = ce = (uschar *)cd->start_code + GET(cc, 1); /* Start subpattern */ |
| 1535 |
do ce += GET(ce, 1); while (*ce == OP_ALT); /* End subpattern */ |
do ce += GET(ce, 1); while (*ce == OP_ALT); /* End subpattern */ |
| 1536 |
if (cc > cs && cc < ce) return -1; /* Recursion */ |
if (cc > cs && cc < ce) return -1; /* Recursion */ |
| 1537 |
d = find_fixedlength(cs + 2, options, atend, cd); |
d = find_fixedlength(cs + 2, utf8, atend, cd); |
| 1538 |
if (d < 0) return d; |
if (d < 0) return d; |
| 1539 |
branchlength += d; |
branchlength += d; |
| 1540 |
cc += 1 + LINK_SIZE; |
cc += 1 + LINK_SIZE; |
| 1557 |
case OP_RREF: |
case OP_RREF: |
| 1558 |
case OP_NRREF: |
case OP_NRREF: |
| 1559 |
case OP_DEF: |
case OP_DEF: |
|
case OP_OPT: |
|
| 1560 |
case OP_CALLOUT: |
case OP_CALLOUT: |
| 1561 |
case OP_SOD: |
case OP_SOD: |
| 1562 |
case OP_SOM: |
case OP_SOM: |
| 1564 |
case OP_EOD: |
case OP_EOD: |
| 1565 |
case OP_EODN: |
case OP_EODN: |
| 1566 |
case OP_CIRC: |
case OP_CIRC: |
| 1567 |
|
case OP_CIRCM: |
| 1568 |
case OP_DOLL: |
case OP_DOLL: |
| 1569 |
|
case OP_DOLLM: |
| 1570 |
case OP_NOT_WORD_BOUNDARY: |
case OP_NOT_WORD_BOUNDARY: |
| 1571 |
case OP_WORD_BOUNDARY: |
case OP_WORD_BOUNDARY: |
| 1572 |
cc += _pcre_OP_lengths[*cc]; |
cc += _pcre_OP_lengths[*cc]; |
| 1575 |
/* Handle literal characters */ |
/* Handle literal characters */ |
| 1576 |
|
|
| 1577 |
case OP_CHAR: |
case OP_CHAR: |
| 1578 |
case OP_CHARNC: |
case OP_CHARI: |
| 1579 |
case OP_NOT: |
case OP_NOT: |
| 1580 |
|
case OP_NOTI: |
| 1581 |
branchlength++; |
branchlength++; |
| 1582 |
cc += 2; |
cc += 2; |
| 1583 |
#ifdef SUPPORT_UTF8 |
#ifdef SUPPORT_UTF8 |
| 1584 |
if ((options & PCRE_UTF8) != 0 && cc[-1] >= 0xc0) |
if (utf8 && cc[-1] >= 0xc0) cc += _pcre_utf8_table4[cc[-1] & 0x3f]; |
|
cc += _pcre_utf8_table4[cc[-1] & 0x3f]; |
|
| 1585 |
#endif |
#endif |
| 1586 |
break; |
break; |
| 1587 |
|
|
| 1592 |
branchlength += GET2(cc,1); |
branchlength += GET2(cc,1); |
| 1593 |
cc += 4; |
cc += 4; |
| 1594 |
#ifdef SUPPORT_UTF8 |
#ifdef SUPPORT_UTF8 |
| 1595 |
if ((options & PCRE_UTF8) != 0 && cc[-1] >= 0xc0) |
if (utf8 && cc[-1] >= 0xc0) cc += _pcre_utf8_table4[cc[-1] & 0x3f]; |
|
cc += _pcre_utf8_table4[cc[-1] & 0x3f]; |
|
| 1596 |
#endif |
#endif |
| 1597 |
break; |
break; |
| 1598 |
|
|
| 1712 |
|
|
| 1713 |
/* Handle capturing bracket */ |
/* Handle capturing bracket */ |
| 1714 |
|
|
| 1715 |
else if (c == OP_CBRA) |
else if (c == OP_CBRA || c == OP_SCBRA || |
| 1716 |
|
c == OP_CBRAPOS || c == OP_SCBRAPOS) |
| 1717 |
{ |
{ |
| 1718 |
int n = GET2(code, 1+LINK_SIZE); |
int n = GET2(code, 1+LINK_SIZE); |
| 1719 |
if (n == number) return (uschar *)code; |
if (n == number) return (uschar *)code; |
| 1771 |
if (utf8) switch(c) |
if (utf8) switch(c) |
| 1772 |
{ |
{ |
| 1773 |
case OP_CHAR: |
case OP_CHAR: |
| 1774 |
case OP_CHARNC: |
case OP_CHARI: |
| 1775 |
case OP_EXACT: |
case OP_EXACT: |
| 1776 |
|
case OP_EXACTI: |
| 1777 |
case OP_UPTO: |
case OP_UPTO: |
| 1778 |
|
case OP_UPTOI: |
| 1779 |
case OP_MINUPTO: |
case OP_MINUPTO: |
| 1780 |
|
case OP_MINUPTOI: |
| 1781 |
case OP_POSUPTO: |
case OP_POSUPTO: |
| 1782 |
|
case OP_POSUPTOI: |
| 1783 |
case OP_STAR: |
case OP_STAR: |
| 1784 |
|
case OP_STARI: |
| 1785 |
case OP_MINSTAR: |
case OP_MINSTAR: |
| 1786 |
|
case OP_MINSTARI: |
| 1787 |
case OP_POSSTAR: |
case OP_POSSTAR: |
| 1788 |
|
case OP_POSSTARI: |
| 1789 |
case OP_PLUS: |
case OP_PLUS: |
| 1790 |
|
case OP_PLUSI: |
| 1791 |
case OP_MINPLUS: |
case OP_MINPLUS: |
| 1792 |
|
case OP_MINPLUSI: |
| 1793 |
case OP_POSPLUS: |
case OP_POSPLUS: |
| 1794 |
|
case OP_POSPLUSI: |
| 1795 |
case OP_QUERY: |
case OP_QUERY: |
| 1796 |
|
case OP_QUERYI: |
| 1797 |
case OP_MINQUERY: |
case OP_MINQUERY: |
| 1798 |
|
case OP_MINQUERYI: |
| 1799 |
case OP_POSQUERY: |
case OP_POSQUERY: |
| 1800 |
|
case OP_POSQUERYI: |
| 1801 |
if (code[-1] >= 0xc0) code += _pcre_utf8_table4[code[-1] & 0x3f]; |
if (code[-1] >= 0xc0) code += _pcre_utf8_table4[code[-1] & 0x3f]; |
| 1802 |
break; |
break; |
| 1803 |
} |
} |
| 1890 |
if (utf8) switch(c) |
if (utf8) switch(c) |
| 1891 |
{ |
{ |
| 1892 |
case OP_CHAR: |
case OP_CHAR: |
| 1893 |
case OP_CHARNC: |
case OP_CHARI: |
| 1894 |
case OP_EXACT: |
case OP_EXACT: |
| 1895 |
|
case OP_EXACTI: |
| 1896 |
case OP_UPTO: |
case OP_UPTO: |
| 1897 |
|
case OP_UPTOI: |
| 1898 |
case OP_MINUPTO: |
case OP_MINUPTO: |
| 1899 |
|
case OP_MINUPTOI: |
| 1900 |
case OP_POSUPTO: |
case OP_POSUPTO: |
| 1901 |
|
case OP_POSUPTOI: |
| 1902 |
case OP_STAR: |
case OP_STAR: |
| 1903 |
|
case OP_STARI: |
| 1904 |
case OP_MINSTAR: |
case OP_MINSTAR: |
| 1905 |
|
case OP_MINSTARI: |
| 1906 |
case OP_POSSTAR: |
case OP_POSSTAR: |
| 1907 |
|
case OP_POSSTARI: |
| 1908 |
case OP_PLUS: |
case OP_PLUS: |
| 1909 |
|
case OP_PLUSI: |
| 1910 |
case OP_MINPLUS: |
case OP_MINPLUS: |
| 1911 |
|
case OP_MINPLUSI: |
| 1912 |
case OP_POSPLUS: |
case OP_POSPLUS: |
| 1913 |
|
case OP_POSPLUSI: |
| 1914 |
case OP_QUERY: |
case OP_QUERY: |
| 1915 |
|
case OP_QUERYI: |
| 1916 |
case OP_MINQUERY: |
case OP_MINQUERY: |
| 1917 |
|
case OP_MINQUERYI: |
| 1918 |
case OP_POSQUERY: |
case OP_POSQUERY: |
| 1919 |
|
case OP_POSQUERYI: |
| 1920 |
if (code[-1] >= 0xc0) code += _pcre_utf8_table4[code[-1] & 0x3f]; |
if (code[-1] >= 0xc0) code += _pcre_utf8_table4[code[-1] & 0x3f]; |
| 1921 |
break; |
break; |
| 1922 |
} |
} |
| 1955 |
compile_data *cd) |
compile_data *cd) |
| 1956 |
{ |
{ |
| 1957 |
register int c; |
register int c; |
| 1958 |
for (code = first_significant_code(code + _pcre_OP_lengths[*code], NULL, 0, TRUE); |
for (code = first_significant_code(code + _pcre_OP_lengths[*code], TRUE); |
| 1959 |
code < endcode; |
code < endcode; |
| 1960 |
code = first_significant_code(code + _pcre_OP_lengths[c], NULL, 0, TRUE)) |
code = first_significant_code(code + _pcre_OP_lengths[c], TRUE)) |
| 1961 |
{ |
{ |
| 1962 |
const uschar *ccode; |
const uschar *ccode; |
| 1963 |
|
|
| 1973 |
continue; |
continue; |
| 1974 |
} |
} |
| 1975 |
|
|
|
/* Groups with zero repeats can of course be empty; skip them. */ |
|
|
|
|
|
if (c == OP_BRAZERO || c == OP_BRAMINZERO || c == OP_SKIPZERO) |
|
|
{ |
|
|
code += _pcre_OP_lengths[c]; |
|
|
do code += GET(code, 1); while (*code == OP_ALT); |
|
|
c = *code; |
|
|
continue; |
|
|
} |
|
|
|
|
| 1976 |
/* For a recursion/subroutine call, if its end has been reached, which |
/* For a recursion/subroutine call, if its end has been reached, which |
| 1977 |
implies a subroutine call, we can scan it. */ |
implies a subroutine call, we can scan it. */ |
| 1978 |
|
|
| 1995 |
continue; |
continue; |
| 1996 |
} |
} |
| 1997 |
|
|
| 1998 |
|
/* Groups with zero repeats can of course be empty; skip them. */ |
| 1999 |
|
|
| 2000 |
|
if (c == OP_BRAZERO || c == OP_BRAMINZERO || c == OP_SKIPZERO || |
| 2001 |
|
c == OP_BRAPOSZERO) |
| 2002 |
|
{ |
| 2003 |
|
code += _pcre_OP_lengths[c]; |
| 2004 |
|
do code += GET(code, 1); while (*code == OP_ALT); |
| 2005 |
|
c = *code; |
| 2006 |
|
continue; |
| 2007 |
|
} |
| 2008 |
|
|
| 2009 |
|
/* A nested group that is already marked as "could be empty" can just be |
| 2010 |
|
skipped. */ |
| 2011 |
|
|
| 2012 |
|
if (c == OP_SBRA || c == OP_SBRAPOS || |
| 2013 |
|
c == OP_SCBRA || c == OP_SCBRAPOS) |
| 2014 |
|
{ |
| 2015 |
|
do code += GET(code, 1); while (*code == OP_ALT); |
| 2016 |
|
c = *code; |
| 2017 |
|
continue; |
| 2018 |
|
} |
| 2019 |
|
|
| 2020 |
/* For other groups, scan the branches. */ |
/* For other groups, scan the branches. */ |
| 2021 |
|
|
| 2022 |
if (c == OP_BRA || c == OP_CBRA || c == OP_ONCE || c == OP_COND) |
if (c == OP_BRA || c == OP_BRAPOS || |
| 2023 |
|
c == OP_CBRA || c == OP_CBRAPOS || |
| 2024 |
|
c == OP_ONCE || c == OP_COND) |
| 2025 |
{ |
{ |
| 2026 |
BOOL empty_branch; |
BOOL empty_branch; |
| 2027 |
if (GET(code, 1) == 0) return TRUE; /* Hit unclosed bracket */ |
if (GET(code, 1) == 0) return TRUE; /* Hit unclosed bracket */ |
| 2108 |
case OP_ALLANY: |
case OP_ALLANY: |
| 2109 |
case OP_ANYBYTE: |
case OP_ANYBYTE: |
| 2110 |
case OP_CHAR: |
case OP_CHAR: |
| 2111 |
case OP_CHARNC: |
case OP_CHARI: |
| 2112 |
case OP_NOT: |
case OP_NOT: |
| 2113 |
|
case OP_NOTI: |
| 2114 |
case OP_PLUS: |
case OP_PLUS: |
| 2115 |
case OP_MINPLUS: |
case OP_MINPLUS: |
| 2116 |
case OP_POSPLUS: |
case OP_POSPLUS: |
| 2150 |
case OP_KET: |
case OP_KET: |
| 2151 |
case OP_KETRMAX: |
case OP_KETRMAX: |
| 2152 |
case OP_KETRMIN: |
case OP_KETRMIN: |
| 2153 |
|
case OP_KETRPOS: |
| 2154 |
case OP_ALT: |
case OP_ALT: |
| 2155 |
return TRUE; |
return TRUE; |
| 2156 |
|
|
| 2159 |
|
|
| 2160 |
#ifdef SUPPORT_UTF8 |
#ifdef SUPPORT_UTF8 |
| 2161 |
case OP_STAR: |
case OP_STAR: |
| 2162 |
|
case OP_STARI: |
| 2163 |
case OP_MINSTAR: |
case OP_MINSTAR: |
| 2164 |
|
case OP_MINSTARI: |
| 2165 |
case OP_POSSTAR: |
case OP_POSSTAR: |
| 2166 |
|
case OP_POSSTARI: |
| 2167 |
case OP_QUERY: |
case OP_QUERY: |
| 2168 |
|
case OP_QUERYI: |
| 2169 |
case OP_MINQUERY: |
case OP_MINQUERY: |
| 2170 |
|
case OP_MINQUERYI: |
| 2171 |
case OP_POSQUERY: |
case OP_POSQUERY: |
| 2172 |
|
case OP_POSQUERYI: |
| 2173 |
if (utf8 && code[1] >= 0xc0) code += _pcre_utf8_table4[code[1] & 0x3f]; |
if (utf8 && code[1] >= 0xc0) code += _pcre_utf8_table4[code[1] & 0x3f]; |
| 2174 |
break; |
break; |
| 2175 |
|
|
| 2176 |
case OP_UPTO: |
case OP_UPTO: |
| 2177 |
|
case OP_UPTOI: |
| 2178 |
case OP_MINUPTO: |
case OP_MINUPTO: |
| 2179 |
|
case OP_MINUPTOI: |
| 2180 |
case OP_POSUPTO: |
case OP_POSUPTO: |
| 2181 |
|
case OP_POSUPTOI: |
| 2182 |
if (utf8 && code[3] >= 0xc0) code += _pcre_utf8_table4[code[3] & 0x3f]; |
if (utf8 && code[3] >= 0xc0) code += _pcre_utf8_table4[code[3] & 0x3f]; |
| 2183 |
break; |
break; |
| 2184 |
#endif |
#endif |
| 2593 |
while ((cd->ctypes[*ptr] & ctype_space) != 0) ptr++; |
while ((cd->ctypes[*ptr] & ctype_space) != 0) ptr++; |
| 2594 |
if (*ptr == CHAR_NUMBER_SIGN) |
if (*ptr == CHAR_NUMBER_SIGN) |
| 2595 |
{ |
{ |
| 2596 |
while (*(++ptr) != 0) |
ptr++; |
| 2597 |
|
while (*ptr != 0) |
| 2598 |
|
{ |
| 2599 |
if (IS_NEWLINE(ptr)) { ptr += cd->nllen; break; } |
if (IS_NEWLINE(ptr)) { ptr += cd->nllen; break; } |
| 2600 |
|
ptr++; |
| 2601 |
|
#ifdef SUPPORT_UTF8 |
| 2602 |
|
if (utf8) while ((*ptr & 0xc0) == 0x80) ptr++; |
| 2603 |
|
#endif |
| 2604 |
|
} |
| 2605 |
} |
} |
| 2606 |
else break; |
else break; |
| 2607 |
} |
} |
| 2637 |
while ((cd->ctypes[*ptr] & ctype_space) != 0) ptr++; |
while ((cd->ctypes[*ptr] & ctype_space) != 0) ptr++; |
| 2638 |
if (*ptr == CHAR_NUMBER_SIGN) |
if (*ptr == CHAR_NUMBER_SIGN) |
| 2639 |
{ |
{ |
| 2640 |
while (*(++ptr) != 0) |
ptr++; |
| 2641 |
|
while (*ptr != 0) |
| 2642 |
|
{ |
| 2643 |
if (IS_NEWLINE(ptr)) { ptr += cd->nllen; break; } |
if (IS_NEWLINE(ptr)) { ptr += cd->nllen; break; } |
| 2644 |
|
ptr++; |
| 2645 |
|
#ifdef SUPPORT_UTF8 |
| 2646 |
|
if (utf8) while ((*ptr & 0xc0) == 0x80) ptr++; |
| 2647 |
|
#endif |
| 2648 |
|
} |
| 2649 |
} |
} |
| 2650 |
else break; |
else break; |
| 2651 |
} |
} |
| 2670 |
#endif |
#endif |
| 2671 |
return c != next; |
return c != next; |
| 2672 |
|
|
| 2673 |
/* For CHARNC (caseless character) we must check the other case. If we have |
/* For CHARI (caseless character) we must check the other case. If we have |
| 2674 |
Unicode property support, we can use it to test the other case of |
Unicode property support, we can use it to test the other case of |
| 2675 |
high-valued characters. */ |
high-valued characters. */ |
| 2676 |
|
|
| 2677 |
case OP_CHARNC: |
case OP_CHARI: |
| 2678 |
#ifdef SUPPORT_UTF8 |
#ifdef SUPPORT_UTF8 |
| 2679 |
GETCHARTEST(c, previous); |
GETCHARTEST(c, previous); |
| 2680 |
#else |
#else |
| 2697 |
#endif /* SUPPORT_UTF8 */ |
#endif /* SUPPORT_UTF8 */ |
| 2698 |
return (c != cd->fcc[next]); /* Non-UTF-8 mode */ |
return (c != cd->fcc[next]); /* Non-UTF-8 mode */ |
| 2699 |
|
|
| 2700 |
/* For OP_NOT, its data is always a single-byte character. */ |
/* For OP_NOT and OP_NOTI, the data is always a single-byte character. These |
| 2701 |
|
opcodes are not used for multi-byte characters, because they are coded using |
| 2702 |
|
an XCLASS instead. */ |
| 2703 |
|
|
| 2704 |
case OP_NOT: |
case OP_NOT: |
| 2705 |
|
return (c = *previous) == next; |
| 2706 |
|
|
| 2707 |
|
case OP_NOTI: |
| 2708 |
if ((c = *previous) == next) return TRUE; |
if ((c = *previous) == next) return TRUE; |
|
if ((options & PCRE_CASELESS) == 0) return FALSE; |
|
| 2709 |
#ifdef SUPPORT_UTF8 |
#ifdef SUPPORT_UTF8 |
| 2710 |
if (utf8) |
if (utf8) |
| 2711 |
{ |
{ |
| 2810 |
switch(op_code) |
switch(op_code) |
| 2811 |
{ |
{ |
| 2812 |
case OP_CHAR: |
case OP_CHAR: |
| 2813 |
case OP_CHARNC: |
case OP_CHARI: |
| 2814 |
#ifdef SUPPORT_UTF8 |
#ifdef SUPPORT_UTF8 |
| 2815 |
GETCHARTEST(c, previous); |
GETCHARTEST(c, previous); |
| 2816 |
#else |
#else |
| 3222 |
if ((cd->ctypes[c] & ctype_space) != 0) continue; |
if ((cd->ctypes[c] & ctype_space) != 0) continue; |
| 3223 |
if (c == CHAR_NUMBER_SIGN) |
if (c == CHAR_NUMBER_SIGN) |
| 3224 |
{ |
{ |
| 3225 |
while (*(++ptr) != 0) |
ptr++; |
| 3226 |
|
while (*ptr != 0) |
| 3227 |
{ |
{ |
| 3228 |
if (IS_NEWLINE(ptr)) { ptr += cd->nllen - 1; break; } |
if (IS_NEWLINE(ptr)) { ptr += cd->nllen - 1; break; } |
| 3229 |
|
ptr++; |
| 3230 |
|
#ifdef SUPPORT_UTF8 |
| 3231 |
|
if (utf8) while ((*ptr & 0xc0) == 0x80) ptr++; |
| 3232 |
|
#endif |
| 3233 |
} |
} |
| 3234 |
if (*ptr != 0) continue; |
if (*ptr != 0) continue; |
| 3235 |
|
|
| 3274 |
the setting of any following char as a first character. */ |
the setting of any following char as a first character. */ |
| 3275 |
|
|
| 3276 |
case CHAR_CIRCUMFLEX_ACCENT: |
case CHAR_CIRCUMFLEX_ACCENT: |
| 3277 |
|
previous = NULL; |
| 3278 |
if ((options & PCRE_MULTILINE) != 0) |
if ((options & PCRE_MULTILINE) != 0) |
| 3279 |
{ |
{ |
| 3280 |
if (firstbyte == REQ_UNSET) firstbyte = REQ_NONE; |
if (firstbyte == REQ_UNSET) firstbyte = REQ_NONE; |
| 3281 |
|
*code++ = OP_CIRCM; |
| 3282 |
} |
} |
| 3283 |
previous = NULL; |
else *code++ = OP_CIRC; |
|
*code++ = OP_CIRC; |
|
| 3284 |
break; |
break; |
| 3285 |
|
|
| 3286 |
case CHAR_DOLLAR_SIGN: |
case CHAR_DOLLAR_SIGN: |
| 3287 |
previous = NULL; |
previous = NULL; |
| 3288 |
*code++ = OP_DOLL; |
*code++ = ((options & PCRE_MULTILINE) != 0)? OP_DOLLM : OP_DOLL; |
| 3289 |
break; |
break; |
| 3290 |
|
|
| 3291 |
/* There can never be a first char if '.' is first, whatever happens about |
/* There can never be a first char if '.' is first, whatever happens about |
| 3605 |
for (c = 0; c < 32; c++) classbits[c] |= ~cbits[c+cbit_word]; |
for (c = 0; c < 32; c++) classbits[c] |= ~cbits[c+cbit_word]; |
| 3606 |
continue; |
continue; |
| 3607 |
|
|
| 3608 |
|
/* Perl 5.004 onwards omits VT from \s, but we must preserve it |
| 3609 |
|
if it was previously set by something earlier in the character |
| 3610 |
|
class. */ |
| 3611 |
|
|
| 3612 |
case ESC_s: |
case ESC_s: |
| 3613 |
for (c = 0; c < 32; c++) classbits[c] |= cbits[c+cbit_space]; |
classbits[0] |= cbits[cbit_space]; |
| 3614 |
classbits[1] &= ~0x08; /* Perl 5.004 onwards omits VT from \s */ |
classbits[1] |= cbits[cbit_space+1] & ~0x08; |
| 3615 |
|
for (c = 2; c < 32; c++) classbits[c] |= cbits[c+cbit_space]; |
| 3616 |
continue; |
continue; |
| 3617 |
|
|
| 3618 |
case ESC_S: |
case ESC_S: |
| 4031 |
|
|
| 4032 |
In UTF-8 mode, we can optimize the negative case only if there were no |
In UTF-8 mode, we can optimize the negative case only if there were no |
| 4033 |
characters >= 128 because OP_NOT and the related opcodes like OP_NOTSTAR |
characters >= 128 because OP_NOT and the related opcodes like OP_NOTSTAR |
| 4034 |
operate on single-bytes only. This is an historical hangover. Maybe one day |
operate on single-bytes characters only. This is an historical hangover. |
| 4035 |
we can tidy these opcodes to handle multi-byte characters. |
Maybe one day we can tidy these opcodes to handle multi-byte characters. |
| 4036 |
|
|
| 4037 |
The optimization throws away the bit map. We turn the item into a |
The optimization throws away the bit map. We turn the item into a |
| 4038 |
1-character OP_CHAR[NC] if it's positive, or OP_NOT if it's negative. Note |
1-character OP_CHAR[I] if it's positive, or OP_NOT[I] if it's negative. |
| 4039 |
that OP_NOT does not support multibyte characters. In the positive case, it |
Note that OP_NOT[I] does not support multibyte characters. In the positive |
| 4040 |
can cause firstbyte to be set. Otherwise, there can be no first char if |
case, it can cause firstbyte to be set. Otherwise, there can be no first |
| 4041 |
this item is first, whatever repeat count may follow. In the case of |
char if this item is first, whatever repeat count may follow. In the case |
| 4042 |
reqbyte, save the previous value for reinstating. */ |
of reqbyte, save the previous value for reinstating. */ |
| 4043 |
|
|
| 4044 |
#ifdef SUPPORT_UTF8 |
#ifdef SUPPORT_UTF8 |
| 4045 |
if (class_charcount == 1 && !class_utf8 && |
if (class_charcount == 1 && !class_utf8 && |
| 4050 |
{ |
{ |
| 4051 |
zeroreqbyte = reqbyte; |
zeroreqbyte = reqbyte; |
| 4052 |
|
|
| 4053 |
/* The OP_NOT opcode works on one-byte characters only. */ |
/* The OP_NOT[I] opcodes work on one-byte characters only. */ |
| 4054 |
|
|
| 4055 |
if (negate_class) |
if (negate_class) |
| 4056 |
{ |
{ |
| 4057 |
if (firstbyte == REQ_UNSET) firstbyte = REQ_NONE; |
if (firstbyte == REQ_UNSET) firstbyte = REQ_NONE; |
| 4058 |
zerofirstbyte = firstbyte; |
zerofirstbyte = firstbyte; |
| 4059 |
*code++ = OP_NOT; |
*code++ = ((options & PCRE_CASELESS) != 0)? OP_NOTI: OP_NOT; |
| 4060 |
*code++ = class_lastchar; |
*code++ = class_lastchar; |
| 4061 |
break; |
break; |
| 4062 |
} |
} |
| 4214 |
the first thing in a branch because the x will have gone into firstbyte |
the first thing in a branch because the x will have gone into firstbyte |
| 4215 |
instead. */ |
instead. */ |
| 4216 |
|
|
| 4217 |
if (*previous == OP_CHAR || *previous == OP_CHARNC) |
if (*previous == OP_CHAR || *previous == OP_CHARI) |
| 4218 |
{ |
{ |
| 4219 |
|
op_type = (*previous == OP_CHAR)? 0 : OP_STARI - OP_STAR; |
| 4220 |
|
|
| 4221 |
/* Deal with UTF-8 characters that take up more than one byte. It's |
/* Deal with UTF-8 characters that take up more than one byte. It's |
| 4222 |
easier to write this out separately than try to macrify it. Use c to |
easier to write this out separately than try to macrify it. Use c to |
| 4223 |
hold the length of the character in bytes, plus 0x80 to flag that it's a |
hold the length of the character in bytes, plus 0x80 to flag that it's a |
| 4262 |
/* If previous was a single negated character ([^a] or similar), we use |
/* If previous was a single negated character ([^a] or similar), we use |
| 4263 |
one of the special opcodes, replacing it. The code is shared with single- |
one of the special opcodes, replacing it. The code is shared with single- |
| 4264 |
character repeats by setting opt_type to add a suitable offset into |
character repeats by setting opt_type to add a suitable offset into |
| 4265 |
repeat_type. We can also test for auto-possessification. OP_NOT is |
repeat_type. We can also test for auto-possessification. OP_NOT and OP_NOTI |
| 4266 |
currently used only for single-byte chars. */ |
are currently used only for single-byte chars. */ |
| 4267 |
|
|
| 4268 |
else if (*previous == OP_NOT) |
else if (*previous == OP_NOT || *previous == OP_NOTI) |
| 4269 |
{ |
{ |
| 4270 |
op_type = OP_NOTSTAR - OP_STAR; /* Use "not" opcodes */ |
op_type = ((*previous == OP_NOT)? OP_NOTSTAR : OP_NOTSTARI) - OP_STAR; |
| 4271 |
c = previous[1]; |
c = previous[1]; |
| 4272 |
if (!possessive_quantifier && |
if (!possessive_quantifier && |
| 4273 |
repeat_max < 0 && |
repeat_max < 0 && |
| 4464 |
#ifdef SUPPORT_UTF8 |
#ifdef SUPPORT_UTF8 |
| 4465 |
*previous == OP_XCLASS || |
*previous == OP_XCLASS || |
| 4466 |
#endif |
#endif |
| 4467 |
*previous == OP_REF) |
*previous == OP_REF || |
| 4468 |
|
*previous == OP_REFI) |
| 4469 |
{ |
{ |
| 4470 |
if (repeat_max == 0) |
if (repeat_max == 0) |
| 4471 |
{ |
{ |
| 4499 |
} |
} |
| 4500 |
|
|
| 4501 |
/* 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 |
| 4502 |
cases. */ |
cases. Note that at this point we can encounter only the "basic" BRA and |
| 4503 |
|
KET opcodes, as this is the place where they get converted into the more |
| 4504 |
|
special varieties. */ |
| 4505 |
|
|
| 4506 |
else if (*previous == OP_BRA || *previous == OP_CBRA || |
else if (*previous == OP_BRA || *previous == OP_CBRA || |
| 4507 |
*previous == OP_ONCE || *previous == OP_COND) |
*previous == OP_ONCE || *previous == OP_COND) |
| 4508 |
{ |
{ |
| 4509 |
register int i; |
register int i; |
|
int ketoffset = 0; |
|
| 4510 |
int len = (int)(code - previous); |
int len = (int)(code - previous); |
| 4511 |
uschar *bralink = NULL; |
uschar *bralink = NULL; |
| 4512 |
|
uschar *brazeroptr = NULL; |
| 4513 |
|
|
| 4514 |
/* Repeating a DEFINE group is pointless */ |
/* Repeating a DEFINE group is pointless */ |
| 4515 |
|
|
| 4516 |
if (*previous == OP_COND && previous[LINK_SIZE+1] == OP_DEF) |
if (*previous == OP_COND && previous[LINK_SIZE+1] == OP_DEF) |
| 4519 |
goto FAILED; |
goto FAILED; |
| 4520 |
} |
} |
| 4521 |
|
|
|
/* If the maximum repeat count is unlimited, find the end of the bracket |
|
|
by scanning through from the start, and compute the offset back to it |
|
|
from the current code pointer. There may be an OP_OPT setting following |
|
|
the final KET, so we can't find the end just by going back from the code |
|
|
pointer. */ |
|
|
|
|
|
if (repeat_max == -1) |
|
|
{ |
|
|
register uschar *ket = previous; |
|
|
do ket += GET(ket, 1); while (*ket != OP_KET); |
|
|
ketoffset = (int)(code - ket); |
|
|
} |
|
|
|
|
| 4522 |
/* The case of a zero minimum is special because of the need to stick |
/* The case of a zero minimum is special because of the need to stick |
| 4523 |
OP_BRAZERO in front of it, and because the group appears once in the |
OP_BRAZERO in front of it, and because the group appears once in the |
| 4524 |
data, whereas in other cases it appears the minimum number of times. For |
data, whereas in other cases it appears the minimum number of times. For |
| 4560 |
*previous++ = OP_SKIPZERO; |
*previous++ = OP_SKIPZERO; |
| 4561 |
goto END_REPEAT; |
goto END_REPEAT; |
| 4562 |
} |
} |
| 4563 |
|
brazeroptr = previous; /* Save for possessive optimizing */ |
| 4564 |
*previous++ = OP_BRAZERO + repeat_type; |
*previous++ = OP_BRAZERO + repeat_type; |
| 4565 |
} |
} |
| 4566 |
|
|
| 4725 |
} |
} |
| 4726 |
} |
} |
| 4727 |
|
|
| 4728 |
/* If the maximum is unlimited, set a repeater in the final copy. We |
/* If the maximum is unlimited, set a repeater in the final copy. For |
| 4729 |
can't just offset backwards from the current code point, because we |
ONCE brackets, that's all we need to do. |
| 4730 |
don't know if there's been an options resetting after the ket. The |
|
| 4731 |
correct offset was computed above. |
Otherwise, if the quantifier was possessive, we convert the BRA code to |
| 4732 |
|
the POS form, and the KET code to KETRPOS. (It turns out to be convenient |
| 4733 |
|
at runtime to detect this kind of subpattern at both the start and at the |
| 4734 |
|
end.) The use of special opcodes makes it possible to reduce greatly the |
| 4735 |
|
stack usage in pcre_exec(). If the group is preceded by OP_BRAZERO, |
| 4736 |
|
convert this to OP_BRAPOSZERO. Then cancel the possessive flag so that |
| 4737 |
|
the default action below, of wrapping everything inside atomic brackets, |
| 4738 |
|
does not happen. |
| 4739 |
|
|
| 4740 |
Then, when we are doing the actual compile phase, check to see whether |
Then, when we are doing the actual compile phase, check to see whether |
| 4741 |
this group is a non-atomic one that could match an empty string. If so, |
this group is one that could match an empty string. If so, convert the |
| 4742 |
convert the initial operator to the S form (e.g. OP_BRA -> OP_SBRA) so |
initial operator to the S form (e.g. OP_BRA -> OP_SBRA) so that runtime |
| 4743 |
that runtime checking can be done. [This check is also applied to |
checking can be done. [This check is also applied to ONCE groups at |
| 4744 |
atomic groups at runtime, but in a different way.] */ |
runtime, but in a different way.] */ |
| 4745 |
|
|
| 4746 |
else |
else |
| 4747 |
{ |
{ |
| 4748 |
uschar *ketcode = code - ketoffset; |
uschar *ketcode = code - 1 - LINK_SIZE; |
| 4749 |
uschar *bracode = ketcode - GET(ketcode, 1); |
uschar *bracode = ketcode - GET(ketcode, 1); |
| 4750 |
*ketcode = OP_KETRMAX + repeat_type; |
|
| 4751 |
if (lengthptr == NULL && *bracode != OP_ONCE) |
if (*bracode == OP_ONCE) |
| 4752 |
|
*ketcode = OP_KETRMAX + repeat_type; |
| 4753 |
|
else |
| 4754 |
{ |
{ |
| 4755 |
uschar *scode = bracode; |
if (possessive_quantifier) |
| 4756 |
do |
{ |
| 4757 |
|
*bracode += 1; /* Switch to xxxPOS opcodes */ |
| 4758 |
|
*ketcode = OP_KETRPOS; |
| 4759 |
|
if (brazeroptr != NULL) *brazeroptr = OP_BRAPOSZERO; |
| 4760 |
|
possessive_quantifier = FALSE; |
| 4761 |
|
} |
| 4762 |
|
else *ketcode = OP_KETRMAX + repeat_type; |
| 4763 |
|
|
| 4764 |
|
if (lengthptr == NULL) |
| 4765 |
{ |
{ |
| 4766 |
if (could_be_empty_branch(scode, ketcode, utf8, cd)) |
uschar *scode = bracode; |
| 4767 |
|
do |
| 4768 |
{ |
{ |
| 4769 |
*bracode += OP_SBRA - OP_BRA; |
if (could_be_empty_branch(scode, ketcode, utf8, cd)) |
| 4770 |
break; |
{ |
| 4771 |
|
*bracode += OP_SBRA - OP_BRA; |
| 4772 |
|
break; |
| 4773 |
|
} |
| 4774 |
|
scode += GET(scode, 1); |
| 4775 |
} |
} |
| 4776 |
scode += GET(scode, 1); |
while (*scode == OP_ALT); |
| 4777 |
} |
} |
|
while (*scode == OP_ALT); |
|
| 4778 |
} |
} |
| 4779 |
} |
} |
| 4780 |
} |
} |
| 4795 |
} |
} |
| 4796 |
|
|
| 4797 |
/* If the character following a repeat is '+', or if certain optimization |
/* If the character following a repeat is '+', or if certain optimization |
| 4798 |
tests above succeeded, possessive_quantifier is TRUE. For some of the |
tests above succeeded, possessive_quantifier is TRUE. For some opcodes, |
| 4799 |
simpler opcodes, there is an special alternative opcode for this. For |
there are special alternative opcodes for this case. For anything else, we |
| 4800 |
anything else, we wrap the entire repeated item inside OP_ONCE brackets. |
wrap the entire repeated item inside OP_ONCE brackets. Logically, the '+' |
| 4801 |
The '+' notation is just syntactic sugar, taken from Sun's Java package, |
notation is just syntactic sugar, taken from Sun's Java package, but the |
| 4802 |
but the special opcodes can optimize it a bit. The repeated item starts at |
special opcodes can optimize it. |
| 4803 |
tempcode, not at previous, which might be the first part of a string whose |
|
| 4804 |
(former) last char we repeated. |
Possessively repeated subpatterns have already been handled in the code |
| 4805 |
|
just above, so possessive_quantifier is always FALSE for them at this |
| 4806 |
|
stage. |
| 4807 |
|
|
| 4808 |
|
Note that the repeated item starts at tempcode, not at previous, which |
| 4809 |
|
might be the first part of a string whose (former) last char we repeated. |
| 4810 |
|
|
| 4811 |
Possessifying an 'exact' quantifier has no effect, so we can ignore it. But |
Possessifying an 'exact' quantifier has no effect, so we can ignore it. But |
| 4812 |
an 'upto' may follow. We skip over an 'exact' item, and then test the |
an 'upto' may follow. We skip over an 'exact' item, and then test the |
| 4837 |
case OP_QUERY: *tempcode = OP_POSQUERY; break; |
case OP_QUERY: *tempcode = OP_POSQUERY; break; |
| 4838 |
case OP_UPTO: *tempcode = OP_POSUPTO; break; |
case OP_UPTO: *tempcode = OP_POSUPTO; break; |
| 4839 |
|
|
| 4840 |
case OP_TYPESTAR: *tempcode = OP_TYPEPOSSTAR; break; |
case OP_STARI: *tempcode = OP_POSSTARI; break; |
| 4841 |
case OP_TYPEPLUS: *tempcode = OP_TYPEPOSPLUS; break; |
case OP_PLUSI: *tempcode = OP_POSPLUSI; break; |
| 4842 |
case OP_TYPEQUERY: *tempcode = OP_TYPEPOSQUERY; break; |
case OP_QUERYI: *tempcode = OP_POSQUERYI; break; |
| 4843 |
case OP_TYPEUPTO: *tempcode = OP_TYPEPOSUPTO; break; |
case OP_UPTOI: *tempcode = OP_POSUPTOI; break; |
| 4844 |
|
|
| 4845 |
case OP_NOTSTAR: *tempcode = OP_NOTPOSSTAR; break; |
case OP_NOTSTAR: *tempcode = OP_NOTPOSSTAR; break; |
| 4846 |
case OP_NOTPLUS: *tempcode = OP_NOTPOSPLUS; break; |
case OP_NOTPLUS: *tempcode = OP_NOTPOSPLUS; break; |
| 4847 |
case OP_NOTQUERY: *tempcode = OP_NOTPOSQUERY; break; |
case OP_NOTQUERY: *tempcode = OP_NOTPOSQUERY; break; |
| 4848 |
case OP_NOTUPTO: *tempcode = OP_NOTPOSUPTO; break; |
case OP_NOTUPTO: *tempcode = OP_NOTPOSUPTO; break; |
| 4849 |
|
|
| 4850 |
|
case OP_NOTSTARI: *tempcode = OP_NOTPOSSTARI; break; |
| 4851 |
|
case OP_NOTPLUSI: *tempcode = OP_NOTPOSPLUSI; break; |
| 4852 |
|
case OP_NOTQUERYI: *tempcode = OP_NOTPOSQUERYI; break; |
| 4853 |
|
case OP_NOTUPTOI: *tempcode = OP_NOTPOSUPTOI; break; |
| 4854 |
|
|
| 4855 |
|
case OP_TYPESTAR: *tempcode = OP_TYPEPOSSTAR; break; |
| 4856 |
|
case OP_TYPEPLUS: *tempcode = OP_TYPEPOSPLUS; break; |
| 4857 |
|
case OP_TYPEQUERY: *tempcode = OP_TYPEPOSQUERY; break; |
| 4858 |
|
case OP_TYPEUPTO: *tempcode = OP_TYPEPOSUPTO; break; |
| 4859 |
|
|
| 4860 |
/* Because we are moving code along, we must ensure that any |
/* Because we are moving code along, we must ensure that any |
| 4861 |
pending recursive references are updated. */ |
pending recursive references are updated. */ |
| 4862 |
|
|
| 4957 |
if (*code++ == OP_THEN) |
if (*code++ == OP_THEN) |
| 4958 |
{ |
{ |
| 4959 |
PUT(code, 0, code - bcptr->current_branch - 1); |
PUT(code, 0, code - bcptr->current_branch - 1); |
| 4960 |
code += LINK_SIZE; |
code += LINK_SIZE; |
| 4961 |
} |
} |
| 4962 |
} |
} |
| 4963 |
|
|
| 4964 |
else |
else |
| 4972 |
if (*code++ == OP_THEN_ARG) |
if (*code++ == OP_THEN_ARG) |
| 4973 |
{ |
{ |
| 4974 |
PUT(code, 0, code - bcptr->current_branch - 1); |
PUT(code, 0, code - bcptr->current_branch - 1); |
| 4975 |
code += LINK_SIZE; |
code += LINK_SIZE; |
| 4976 |
} |
} |
| 4977 |
*code++ = arglen; |
*code++ = arglen; |
| 4978 |
memcpy(code, arg, arglen); |
memcpy(code, arg, arglen); |
| 4979 |
code += arglen; |
code += arglen; |
| 5167 |
/* Search the pattern for a forward reference */ |
/* Search the pattern for a forward reference */ |
| 5168 |
|
|
| 5169 |
else if ((i = find_parens(cd, name, namelen, |
else if ((i = find_parens(cd, name, namelen, |
| 5170 |
(options & PCRE_EXTENDED) != 0)) > 0) |
(options & PCRE_EXTENDED) != 0, utf8)) > 0) |
| 5171 |
{ |
{ |
| 5172 |
PUT2(code, 2+LINK_SIZE, i); |
PUT2(code, 2+LINK_SIZE, i); |
| 5173 |
code[1+LINK_SIZE]++; |
code[1+LINK_SIZE]++; |
| 5468 |
while ((cd->ctypes[*ptr] & ctype_word) != 0) ptr++; |
while ((cd->ctypes[*ptr] & ctype_word) != 0) ptr++; |
| 5469 |
namelen = (int)(ptr - name); |
namelen = (int)(ptr - name); |
| 5470 |
|
|
| 5471 |
/* In the pre-compile phase, do a syntax check and set a dummy |
/* In the pre-compile phase, do a syntax check. We used to just set |
| 5472 |
reference number. */ |
a dummy reference number, because it was not used in the first pass. |
| 5473 |
|
However, with the change of recursive back references to be atomic, |
| 5474 |
|
we have to look for the number so that this state can be identified, as |
| 5475 |
|
otherwise the incorrect length is computed. If it's not a backwards |
| 5476 |
|
reference, the dummy number will do. */ |
| 5477 |
|
|
| 5478 |
if (lengthptr != NULL) |
if (lengthptr != NULL) |
| 5479 |
{ |
{ |
| 5480 |
|
const uschar *temp; |
| 5481 |
|
|
| 5482 |
if (namelen == 0) |
if (namelen == 0) |
| 5483 |
{ |
{ |
| 5484 |
*errorcodeptr = ERR62; |
*errorcodeptr = ERR62; |
| 5494 |
*errorcodeptr = ERR48; |
*errorcodeptr = ERR48; |
| 5495 |
goto FAILED; |
goto FAILED; |
| 5496 |
} |
} |
| 5497 |
recno = 0; |
|
| 5498 |
|
/* The name table does not exist in the first pass, so we cannot |
| 5499 |
|
do a simple search as in the code below. Instead, we have to scan the |
| 5500 |
|
pattern to find the number. It is important that we scan it only as |
| 5501 |
|
far as we have got because the syntax of named subpatterns has not |
| 5502 |
|
been checked for the rest of the pattern, and find_parens() assumes |
| 5503 |
|
correct syntax. In any case, it's a waste of resources to scan |
| 5504 |
|
further. We stop the scan at the current point by temporarily |
| 5505 |
|
adjusting the value of cd->endpattern. */ |
| 5506 |
|
|
| 5507 |
|
temp = cd->end_pattern; |
| 5508 |
|
cd->end_pattern = ptr; |
| 5509 |
|
recno = find_parens(cd, name, namelen, |
| 5510 |
|
(options & PCRE_EXTENDED) != 0, utf8); |
| 5511 |
|
cd->end_pattern = temp; |
| 5512 |
|
if (recno < 0) recno = 0; /* Forward ref; set dummy number */ |
| 5513 |
} |
} |
| 5514 |
|
|
| 5515 |
/* In the real compile, seek the name in the table. We check the name |
/* In the real compile, seek the name in the table. We check the name |
| 5534 |
} |
} |
| 5535 |
else if ((recno = /* Forward back reference */ |
else if ((recno = /* Forward back reference */ |
| 5536 |
find_parens(cd, name, namelen, |
find_parens(cd, name, namelen, |
| 5537 |
(options & PCRE_EXTENDED) != 0)) <= 0) |
(options & PCRE_EXTENDED) != 0, utf8)) <= 0) |
| 5538 |
{ |
{ |
| 5539 |
*errorcodeptr = ERR15; |
*errorcodeptr = ERR15; |
| 5540 |
goto FAILED; |
goto FAILED; |
| 5645 |
if (called == NULL) |
if (called == NULL) |
| 5646 |
{ |
{ |
| 5647 |
if (find_parens(cd, NULL, recno, |
if (find_parens(cd, NULL, recno, |
| 5648 |
(options & PCRE_EXTENDED) != 0) < 0) |
(options & PCRE_EXTENDED) != 0, utf8) < 0) |
| 5649 |
{ |
{ |
| 5650 |
*errorcodeptr = ERR15; |
*errorcodeptr = ERR15; |
| 5651 |
goto FAILED; |
goto FAILED; |
| 5748 |
is necessary to ensure we correctly detect the start of the pattern in |
is necessary to ensure we correctly detect the start of the pattern in |
| 5749 |
both phases. |
both phases. |
| 5750 |
|
|
| 5751 |
If we are not at the pattern start, compile code to change the ims |
If we are not at the pattern start, reset the greedy defaults and the |
| 5752 |
options if this setting actually changes any of them, and reset the |
case value for firstbyte and reqbyte. */ |
|
greedy defaults and the case value for firstbyte and reqbyte. */ |
|
| 5753 |
|
|
| 5754 |
if (*ptr == CHAR_RIGHT_PARENTHESIS) |
if (*ptr == CHAR_RIGHT_PARENTHESIS) |
| 5755 |
{ |
{ |
| 5760 |
} |
} |
| 5761 |
else |
else |
| 5762 |
{ |
{ |
|
if ((options & PCRE_IMS) != (newoptions & PCRE_IMS)) |
|
|
{ |
|
|
*code++ = OP_OPT; |
|
|
*code++ = newoptions & PCRE_IMS; |
|
|
} |
|
| 5763 |
greedy_default = ((newoptions & PCRE_UNGREEDY) != 0); |
greedy_default = ((newoptions & PCRE_UNGREEDY) != 0); |
| 5764 |
greedy_non_default = greedy_default ^ 1; |
greedy_non_default = greedy_default ^ 1; |
| 5765 |
req_caseopt = ((newoptions & PCRE_CASELESS) != 0)? REQ_CASELESS : 0; |
req_caseopt = ((newoptions & PCRE_CASELESS) != 0)? REQ_CASELESS : 0; |
| 5766 |
} |
} |
| 5767 |
|
|
| 5768 |
/* Change options at this level, and pass them back for use |
/* Change options at this level, and pass them back for use |
| 5769 |
in subsequent branches. When not at the start of the pattern, this |
in subsequent branches. */ |
|
information is also necessary so that a resetting item can be |
|
|
compiled at the end of a group (if we are in a group). */ |
|
| 5770 |
|
|
| 5771 |
*optionsptr = options = newoptions; |
*optionsptr = options = newoptions; |
| 5772 |
previous = NULL; /* This item can't be repeated */ |
previous = NULL; /* This item can't be repeated */ |
| 5804 |
|
|
| 5805 |
/* Process nested bracketed regex. Assertions may not be repeated, but |
/* Process nested bracketed regex. Assertions may not be repeated, but |
| 5806 |
other kinds can be. All their opcodes are >= OP_ONCE. We copy code into a |
other kinds can be. All their opcodes are >= OP_ONCE. We copy code into a |
| 5807 |
non-register variable in order to be able to pass its address because some |
non-register variable (tempcode) in order to be able to pass its address |
| 5808 |
compilers complain otherwise. Pass in a new setting for the ims options if |
because some compilers complain otherwise. */ |
|
they have changed. */ |
|
| 5809 |
|
|
| 5810 |
previous = (bravalue >= OP_ONCE)? code : NULL; |
previous = (bravalue >= OP_ONCE)? code : NULL; |
| 5811 |
*code = bravalue; |
*code = bravalue; |
| 5815 |
|
|
| 5816 |
if (!compile_regex( |
if (!compile_regex( |
| 5817 |
newoptions, /* The complete new option state */ |
newoptions, /* The complete new option state */ |
|
options & PCRE_IMS, /* The previous ims option state */ |
|
| 5818 |
&tempcode, /* Where to put code (updated) */ |
&tempcode, /* Where to put code (updated) */ |
| 5819 |
&ptr, /* Input pointer (updated) */ |
&ptr, /* Input pointer (updated) */ |
| 5820 |
errorcodeptr, /* Where to put an error message */ |
errorcodeptr, /* Where to put an error message */ |
| 5901 |
goto FAILED; |
goto FAILED; |
| 5902 |
} |
} |
| 5903 |
*lengthptr += length_prevgroup - 2 - 2*LINK_SIZE; |
*lengthptr += length_prevgroup - 2 - 2*LINK_SIZE; |
| 5904 |
*code++ = OP_BRA; |
code++; /* This already contains bravalue */ |
| 5905 |
PUTINC(code, 0, 1 + LINK_SIZE); |
PUTINC(code, 0, 1 + LINK_SIZE); |
| 5906 |
*code++ = OP_KET; |
*code++ = OP_KET; |
| 5907 |
PUTINC(code, 0, 1 + LINK_SIZE); |
PUTINC(code, 0, 1 + LINK_SIZE); |
| 6093 |
HANDLE_REFERENCE: /* Come here from named backref handling */ |
HANDLE_REFERENCE: /* Come here from named backref handling */ |
| 6094 |
if (firstbyte == REQ_UNSET) firstbyte = REQ_NONE; |
if (firstbyte == REQ_UNSET) firstbyte = REQ_NONE; |
| 6095 |
previous = code; |
previous = code; |
| 6096 |
*code++ = OP_REF; |
*code++ = ((options & PCRE_CASELESS) != 0)? OP_REFI : OP_REF; |
| 6097 |
PUT2INC(code, 0, recno); |
PUT2INC(code, 0, recno); |
| 6098 |
cd->backref_map |= (recno < 32)? (1 << recno) : 1; |
cd->backref_map |= (recno < 32)? (1 << recno) : 1; |
| 6099 |
if (recno > cd->top_backref) cd->top_backref = recno; |
if (recno > cd->top_backref) cd->top_backref = recno; |
| 6201 |
|
|
| 6202 |
ONE_CHAR: |
ONE_CHAR: |
| 6203 |
previous = code; |
previous = code; |
| 6204 |
*code++ = ((options & PCRE_CASELESS) != 0)? OP_CHARNC : OP_CHAR; |
*code++ = ((options & PCRE_CASELESS) != 0)? OP_CHARI : OP_CHAR; |
| 6205 |
for (c = 0; c < mclength; c++) *code++ = mcbuffer[c]; |
for (c = 0; c < mclength; c++) *code++ = mcbuffer[c]; |
| 6206 |
|
|
| 6207 |
/* Remember if \r or \n were seen */ |
/* Remember if \r or \n were seen */ |
| 6265 |
/* On entry, ptr is pointing past the bracket character, but on return it |
/* On entry, ptr is pointing past the bracket character, but on return it |
| 6266 |
points to the closing bracket, or vertical bar, or end of string. The code |
points to the closing bracket, or vertical bar, or end of string. The code |
| 6267 |
variable is pointing at the byte into which the BRA operator has been stored. |
variable is pointing at the byte into which the BRA operator has been stored. |
|
If the ims options are changed at the start (for a (?ims: group) or during any |
|
|
branch, we need to insert an OP_OPT item at the start of every following branch |
|
|
to ensure they get set correctly at run time, and also pass the new options |
|
|
into every subsequent branch compile. |
|
|
|
|
| 6268 |
This function is used during the pre-compile phase when we are trying to find |
This function is used during the pre-compile phase when we are trying to find |
| 6269 |
out the amount of memory needed, as well as during the real compile phase. The |
out the amount of memory needed, as well as during the real compile phase. The |
| 6270 |
value of lengthptr distinguishes the two phases. |
value of lengthptr distinguishes the two phases. |
| 6271 |
|
|
| 6272 |
Arguments: |
Arguments: |
| 6273 |
options option bits, including any changes for this subpattern |
options option bits, including any changes for this subpattern |
|
oldims previous settings of ims option bits |
|
| 6274 |
codeptr -> the address of the current code pointer |
codeptr -> the address of the current code pointer |
| 6275 |
ptrptr -> the address of the current pattern pointer |
ptrptr -> the address of the current pattern pointer |
| 6276 |
errorcodeptr -> pointer to error code variable |
errorcodeptr -> pointer to error code variable |
| 6288 |
*/ |
*/ |
| 6289 |
|
|
| 6290 |
static BOOL |
static BOOL |
| 6291 |
compile_regex(int options, int oldims, uschar **codeptr, const uschar **ptrptr, |
compile_regex(int options, uschar **codeptr, const uschar **ptrptr, |
| 6292 |
int *errorcodeptr, BOOL lookbehind, BOOL reset_bracount, int skipbytes, |
int *errorcodeptr, BOOL lookbehind, BOOL reset_bracount, int skipbytes, |
| 6293 |
int *firstbyteptr, int *reqbyteptr, branch_chain *bcptr, compile_data *cd, |
int *firstbyteptr, int *reqbyteptr, branch_chain *bcptr, compile_data *cd, |
| 6294 |
int *lengthptr) |
int *lengthptr) |
| 6305 |
int length; |
int length; |
| 6306 |
int orig_bracount; |
int orig_bracount; |
| 6307 |
int max_bracount; |
int max_bracount; |
|
int old_external_options = cd->external_options; |
|
| 6308 |
branch_chain bc; |
branch_chain bc; |
| 6309 |
|
|
| 6310 |
bc.outer = bcptr; |
bc.outer = bcptr; |
| 6328 |
|
|
| 6329 |
/* If this is a capturing subpattern, add to the chain of open capturing items |
/* If this is a capturing subpattern, add to the chain of open capturing items |
| 6330 |
so that we can detect them if (*ACCEPT) is encountered. This is also used to |
so that we can detect them if (*ACCEPT) is encountered. This is also used to |
| 6331 |
detect groups that contain recursive back references to themselves. */ |
detect groups that contain recursive back references to themselves. Note that |
| 6332 |
|
only OP_CBRA need be tested here; changing this opcode to one of its variants, |
| 6333 |
|
e.g. OP_SCBRAPOS, happens later, after the group has been compiled. */ |
| 6334 |
|
|
| 6335 |
if (*code == OP_CBRA) |
if (*code == OP_CBRA) |
| 6336 |
{ |
{ |
| 6356 |
|
|
| 6357 |
if (reset_bracount) cd->bracount = orig_bracount; |
if (reset_bracount) cd->bracount = orig_bracount; |
| 6358 |
|
|
|
/* Handle a change of ims options at the start of the branch */ |
|
|
|
|
|
if ((options & PCRE_IMS) != oldims) |
|
|
{ |
|
|
*code++ = OP_OPT; |
|
|
*code++ = options & PCRE_IMS; |
|
|
length += 2; |
|
|
} |
|
|
|
|
| 6359 |
/* Set up dummy OP_REVERSE if lookbehind assertion */ |
/* Set up dummy OP_REVERSE if lookbehind assertion */ |
| 6360 |
|
|
| 6361 |
if (lookbehind) |
if (lookbehind) |
| 6376 |
return FALSE; |
return FALSE; |
| 6377 |
} |
} |
| 6378 |
|
|
|
/* If the external options have changed during this branch, it means that we |
|
|
are at the top level, and a leading option setting has been encountered. We |
|
|
need to re-set the original option values to take account of this so that, |
|
|
during the pre-compile phase, we know to allow for a re-set at the start of |
|
|
subsequent branches. */ |
|
|
|
|
|
if (old_external_options != cd->external_options) |
|
|
oldims = cd->external_options & PCRE_IMS; |
|
|
|
|
| 6379 |
/* Keep the highest bracket count in case (?| was used and some branch |
/* Keep the highest bracket count in case (?| was used and some branch |
| 6380 |
has fewer than the rest. */ |
has fewer than the rest. */ |
| 6381 |
|
|
| 6436 |
{ |
{ |
| 6437 |
int fixed_length; |
int fixed_length; |
| 6438 |
*code = OP_END; |
*code = OP_END; |
| 6439 |
fixed_length = find_fixedlength(last_branch, options, FALSE, cd); |
fixed_length = find_fixedlength(last_branch, (options & PCRE_UTF8) != 0, |
| 6440 |
|
FALSE, cd); |
| 6441 |
DPRINTF(("fixed length = %d\n", fixed_length)); |
DPRINTF(("fixed length = %d\n", fixed_length)); |
| 6442 |
if (fixed_length == -3) |
if (fixed_length == -3) |
| 6443 |
{ |
{ |
| 6458 |
of offsets, with the field in the BRA item now becoming an offset to the |
of offsets, with the field in the BRA item now becoming an offset to the |
| 6459 |
first alternative. If there are no alternatives, it points to the end of the |
first alternative. If there are no alternatives, it points to the end of the |
| 6460 |
group. The length in the terminating ket is always the length of the whole |
group. The length in the terminating ket is always the length of the whole |
| 6461 |
bracketed item. If any of the ims options were changed inside the group, |
bracketed item. Return leaving the pointer at the terminating char. */ |
|
compile a resetting op-code following, except at the very end of the pattern. |
|
|
Return leaving the pointer at the terminating char. */ |
|
| 6462 |
|
|
| 6463 |
if (*ptr != CHAR_VERTICAL_LINE) |
if (*ptr != CHAR_VERTICAL_LINE) |
| 6464 |
{ |
{ |
| 6502 |
cd->open_caps = cd->open_caps->next; |
cd->open_caps = cd->open_caps->next; |
| 6503 |
} |
} |
| 6504 |
|
|
|
/* Reset options if needed. */ |
|
|
|
|
|
if ((options & PCRE_IMS) != oldims && *ptr == CHAR_RIGHT_PARENTHESIS) |
|
|
{ |
|
|
*code++ = OP_OPT; |
|
|
*code++ = oldims; |
|
|
length += 2; |
|
|
} |
|
|
|
|
| 6505 |
/* Retain the highest bracket number, in case resetting was used. */ |
/* Retain the highest bracket number, in case resetting was used. */ |
| 6506 |
|
|
| 6507 |
cd->bracount = max_bracount; |
cd->bracount = max_bracount; |
| 6561 |
/* Try to find out if this is an anchored regular expression. Consider each |
/* Try to find out if this is an anchored regular expression. Consider each |
| 6562 |
alternative branch. If they all start with OP_SOD or OP_CIRC, or with a bracket |
alternative branch. If they all start with OP_SOD or OP_CIRC, or with a bracket |
| 6563 |
all of whose alternatives start with OP_SOD or OP_CIRC (recurse ad lib), then |
all of whose alternatives start with OP_SOD or OP_CIRC (recurse ad lib), then |
| 6564 |
it's anchored. However, if this is a multiline pattern, then only OP_SOD |
it's anchored. However, if this is a multiline pattern, then only OP_SOD will |
| 6565 |
counts, since OP_CIRC can match in the middle. |
be found, because ^ generates OP_CIRCM in that mode. |
| 6566 |
|
|
| 6567 |
We can also consider a regex to be anchored if OP_SOM starts all its branches. |
We can also consider a regex to be anchored if OP_SOM starts all its branches. |
| 6568 |
This is the code for \G, which means "match at start of match position, taking |
This is the code for \G, which means "match at start of match position, taking |
| 6583 |
|
|
| 6584 |
Arguments: |
Arguments: |
| 6585 |
code points to start of expression (the bracket) |
code points to start of expression (the bracket) |
|
options points to the options setting |
|
| 6586 |
bracket_map a bitmap of which brackets we are inside while testing; this |
bracket_map a bitmap of which brackets we are inside while testing; this |
| 6587 |
handles up to substring 31; after that we just have to take |
handles up to substring 31; after that we just have to take |
| 6588 |
the less precise approach |
the less precise approach |
| 6592 |
*/ |
*/ |
| 6593 |
|
|
| 6594 |
static BOOL |
static BOOL |
| 6595 |
is_anchored(register const uschar *code, int *options, unsigned int bracket_map, |
is_anchored(register const uschar *code, unsigned int bracket_map, |
| 6596 |
unsigned int backref_map) |
unsigned int backref_map) |
| 6597 |
{ |
{ |
| 6598 |
do { |
do { |
| 6599 |
const uschar *scode = first_significant_code(code + _pcre_OP_lengths[*code], |
const uschar *scode = first_significant_code(code + _pcre_OP_lengths[*code], |
| 6600 |
options, PCRE_MULTILINE, FALSE); |
FALSE); |
| 6601 |
register int op = *scode; |
register int op = *scode; |
| 6602 |
|
|
| 6603 |
/* Non-capturing brackets */ |
/* Non-capturing brackets */ |
| 6604 |
|
|
| 6605 |
if (op == OP_BRA) |
if (op == OP_BRA || op == OP_BRAPOS || |
| 6606 |
|
op == OP_SBRA || op == OP_SBRAPOS) |
| 6607 |
{ |
{ |
| 6608 |
if (!is_anchored(scode, options, bracket_map, backref_map)) return FALSE; |
if (!is_anchored(scode, bracket_map, backref_map)) return FALSE; |
| 6609 |
} |
} |
| 6610 |
|
|
| 6611 |
/* Capturing brackets */ |
/* Capturing brackets */ |
| 6612 |
|
|
| 6613 |
else if (op == OP_CBRA) |
else if (op == OP_CBRA || op == OP_CBRAPOS || |
| 6614 |
|
op == OP_SCBRA || op == OP_SCBRAPOS) |
| 6615 |
{ |
{ |
| 6616 |
int n = GET2(scode, 1+LINK_SIZE); |
int n = GET2(scode, 1+LINK_SIZE); |
| 6617 |
int new_map = bracket_map | ((n < 32)? (1 << n) : 1); |
int new_map = bracket_map | ((n < 32)? (1 << n) : 1); |
| 6618 |
if (!is_anchored(scode, options, new_map, backref_map)) return FALSE; |
if (!is_anchored(scode, new_map, backref_map)) return FALSE; |
| 6619 |
} |
} |
| 6620 |
|
|
| 6621 |
/* Other brackets */ |
/* Other brackets */ |
| 6622 |
|
|
| 6623 |
else if (op == OP_ASSERT || op == OP_ONCE || op == OP_COND) |
else if (op == OP_ASSERT || op == OP_ONCE || op == OP_COND) |
| 6624 |
{ |
{ |
| 6625 |
if (!is_anchored(scode, options, bracket_map, backref_map)) return FALSE; |
if (!is_anchored(scode, bracket_map, backref_map)) return FALSE; |
| 6626 |
} |
} |
| 6627 |
|
|
| 6628 |
/* .* is not anchored unless DOTALL is set (which generates OP_ALLANY) and |
/* .* is not anchored unless DOTALL is set (which generates OP_ALLANY) and |
| 6637 |
|
|
| 6638 |
/* Check for explicit anchoring */ |
/* Check for explicit anchoring */ |
| 6639 |
|
|
| 6640 |
else if (op != OP_SOD && op != OP_SOM && |
else if (op != OP_SOD && op != OP_SOM && op != OP_CIRC) return FALSE; |
|
((*options & PCRE_MULTILINE) != 0 || op != OP_CIRC)) |
|
|
return FALSE; |
|
| 6641 |
code += GET(code, 1); |
code += GET(code, 1); |
| 6642 |
} |
} |
| 6643 |
while (*code == OP_ALT); /* Loop for each alternative */ |
while (*code == OP_ALT); /* Loop for each alternative */ |
| 6673 |
{ |
{ |
| 6674 |
do { |
do { |
| 6675 |
const uschar *scode = first_significant_code(code + _pcre_OP_lengths[*code], |
const uschar *scode = first_significant_code(code + _pcre_OP_lengths[*code], |
| 6676 |
NULL, 0, FALSE); |
FALSE); |
| 6677 |
register int op = *scode; |
register int op = *scode; |
| 6678 |
|
|
| 6679 |
/* If we are at the start of a conditional assertion group, *both* the |
/* If we are at the start of a conditional assertion group, *both* the |
| 6700 |
scode += 1 + LINK_SIZE; |
scode += 1 + LINK_SIZE; |
| 6701 |
break; |
break; |
| 6702 |
} |
} |
| 6703 |
scode = first_significant_code(scode, NULL, 0, FALSE); |
scode = first_significant_code(scode, FALSE); |
| 6704 |
op = *scode; |
op = *scode; |
| 6705 |
} |
} |
| 6706 |
|
|
| 6707 |
/* Non-capturing brackets */ |
/* Non-capturing brackets */ |
| 6708 |
|
|
| 6709 |
if (op == OP_BRA) |
if (op == OP_BRA || op == OP_BRAPOS || |
| 6710 |
|
op == OP_SBRA || op == OP_SBRAPOS) |
| 6711 |
{ |
{ |
| 6712 |
if (!is_startline(scode, bracket_map, backref_map)) return FALSE; |
if (!is_startline(scode, bracket_map, backref_map)) return FALSE; |
| 6713 |
} |
} |
| 6714 |
|
|
| 6715 |
/* Capturing brackets */ |
/* Capturing brackets */ |
| 6716 |
|
|
| 6717 |
else if (op == OP_CBRA) |
else if (op == OP_CBRA || op == OP_CBRAPOS || |
| 6718 |
|
op == OP_SCBRA || op == OP_SCBRAPOS) |
| 6719 |
{ |
{ |
| 6720 |
int n = GET2(scode, 1+LINK_SIZE); |
int n = GET2(scode, 1+LINK_SIZE); |
| 6721 |
int new_map = bracket_map | ((n < 32)? (1 << n) : 1); |
int new_map = bracket_map | ((n < 32)? (1 << n) : 1); |
| 6739 |
|
|
| 6740 |
/* Check for explicit circumflex */ |
/* Check for explicit circumflex */ |
| 6741 |
|
|
| 6742 |
else if (op != OP_CIRC) return FALSE; |
else if (op != OP_CIRC && op != OP_CIRCM) return FALSE; |
| 6743 |
|
|
| 6744 |
/* Move on to the next alternative */ |
/* Move on to the next alternative */ |
| 6745 |
|
|
| 6765 |
|
|
| 6766 |
Arguments: |
Arguments: |
| 6767 |
code points to start of expression (the bracket) |
code points to start of expression (the bracket) |
|
options pointer to the options (used to check casing changes) |
|
| 6768 |
inassert TRUE if in an assertion |
inassert TRUE if in an assertion |
| 6769 |
|
|
| 6770 |
Returns: -1 or the fixed first char |
Returns: -1 or the fixed first char |
| 6771 |
*/ |
*/ |
| 6772 |
|
|
| 6773 |
static int |
static int |
| 6774 |
find_firstassertedchar(const uschar *code, int *options, BOOL inassert) |
find_firstassertedchar(const uschar *code, BOOL inassert) |
| 6775 |
{ |
{ |
| 6776 |
register int c = -1; |
register int c = -1; |
| 6777 |
do { |
do { |
| 6778 |
int d; |
int d; |
| 6779 |
const uschar *scode = |
int xl = (*code == OP_CBRA || *code == OP_SCBRA || |
| 6780 |
first_significant_code(code + 1+LINK_SIZE, options, PCRE_CASELESS, TRUE); |
*code == OP_CBRAPOS || *code == OP_SCBRAPOS)? 2:0; |
| 6781 |
|
const uschar *scode = first_significant_code(code + 1+LINK_SIZE + xl, TRUE); |
| 6782 |
register int op = *scode; |
register int op = *scode; |
| 6783 |
|
|
| 6784 |
switch(op) |
switch(op) |
| 6787 |
return -1; |
return -1; |
| 6788 |
|
|
| 6789 |
case OP_BRA: |
case OP_BRA: |
| 6790 |
|
case OP_BRAPOS: |
| 6791 |
case OP_CBRA: |
case OP_CBRA: |
| 6792 |
|
case OP_SCBRA: |
| 6793 |
|
case OP_CBRAPOS: |
| 6794 |
|
case OP_SCBRAPOS: |
| 6795 |
case OP_ASSERT: |
case OP_ASSERT: |
| 6796 |
case OP_ONCE: |
case OP_ONCE: |
| 6797 |
case OP_COND: |
case OP_COND: |
| 6798 |
if ((d = find_firstassertedchar(scode, options, op == OP_ASSERT)) < 0) |
if ((d = find_firstassertedchar(scode, op == OP_ASSERT)) < 0) |
| 6799 |
return -1; |
return -1; |
| 6800 |
if (c < 0) c = d; else if (c != d) return -1; |
if (c < 0) c = d; else if (c != d) return -1; |
| 6801 |
break; |
break; |
| 6802 |
|
|
| 6803 |
case OP_EXACT: /* Fall through */ |
case OP_EXACT: |
| 6804 |
scode += 2; |
scode += 2; |
| 6805 |
|
/* Fall through */ |
| 6806 |
|
|
| 6807 |
case OP_CHAR: |
case OP_CHAR: |
|
case OP_CHARNC: |
|
| 6808 |
case OP_PLUS: |
case OP_PLUS: |
| 6809 |
case OP_MINPLUS: |
case OP_MINPLUS: |
| 6810 |
case OP_POSPLUS: |
case OP_POSPLUS: |
| 6811 |
if (!inassert) return -1; |
if (!inassert) return -1; |
| 6812 |
if (c < 0) |
if (c < 0) c = scode[1]; |
| 6813 |
{ |
else if (c != scode[1]) return -1; |
| 6814 |
c = scode[1]; |
break; |
| 6815 |
if ((*options & PCRE_CASELESS) != 0) c |= REQ_CASELESS; |
|
| 6816 |
} |
case OP_EXACTI: |
| 6817 |
else if (c != scode[1]) return -1; |
scode += 2; |
| 6818 |
|
/* Fall through */ |
| 6819 |
|
|
| 6820 |
|
case OP_CHARI: |
| 6821 |
|
case OP_PLUSI: |
| 6822 |
|
case OP_MINPLUSI: |
| 6823 |
|
case OP_POSPLUSI: |
| 6824 |
|
if (!inassert) return -1; |
| 6825 |
|
if (c < 0) c = scode[1] | REQ_CASELESS; |
| 6826 |
|
else if (c != scode[1]) return -1; |
| 6827 |
break; |
break; |
| 6828 |
} |
} |
| 6829 |
|
|
| 6946 |
{ skipatstart += 7; options |= PCRE_UTF8; continue; } |
{ skipatstart += 7; options |= PCRE_UTF8; continue; } |
| 6947 |
else if (strncmp((char *)(ptr+skipatstart+2), STRING_UCP_RIGHTPAR, 4) == 0) |
else if (strncmp((char *)(ptr+skipatstart+2), STRING_UCP_RIGHTPAR, 4) == 0) |
| 6948 |
{ skipatstart += 6; options |= PCRE_UCP; continue; } |
{ skipatstart += 6; options |= PCRE_UCP; continue; } |
| 6949 |
|
else if (strncmp((char *)(ptr+skipatstart+2), STRING_NO_START_OPT_RIGHTPAR, 13) == 0) |
| 6950 |
|
{ skipatstart += 15; options |= PCRE_NO_START_OPTIMIZE; continue; } |
| 6951 |
|
|
| 6952 |
if (strncmp((char *)(ptr+skipatstart+2), STRING_CR_RIGHTPAR, 3) == 0) |
if (strncmp((char *)(ptr+skipatstart+2), STRING_CR_RIGHTPAR, 3) == 0) |
| 6953 |
{ skipatstart += 5; newnl = PCRE_NEWLINE_CR; } |
{ skipatstart += 5; newnl = PCRE_NEWLINE_CR; } |
| 6974 |
|
|
| 6975 |
utf8 = (options & PCRE_UTF8) != 0; |
utf8 = (options & PCRE_UTF8) != 0; |
| 6976 |
|
|
| 6977 |
/* Can't support UTF8 unless PCRE has been compiled to include the code. */ |
/* Can't support UTF8 unless PCRE has been compiled to include the code. The |
| 6978 |
|
return of an error code from _pcre_valid_utf8() is a new feature, introduced in |
| 6979 |
|
release 8.13. It is passed back from pcre_[dfa_]exec(), but at the moment is |
| 6980 |
|
not used here. */ |
| 6981 |
|
|
| 6982 |
#ifdef SUPPORT_UTF8 |
#ifdef SUPPORT_UTF8 |
| 6983 |
if (utf8 && (options & PCRE_NO_UTF8_CHECK) == 0 && |
if (utf8 && (options & PCRE_NO_UTF8_CHECK) == 0 && |
| 6984 |
(*erroroffset = _pcre_valid_utf8((USPTR)pattern, -1)) >= 0) |
(errorcode = _pcre_valid_utf8((USPTR)pattern, -1, erroroffset)) != 0) |
| 6985 |
{ |
{ |
| 6986 |
errorcode = ERR44; |
errorcode = ERR44; |
| 6987 |
goto PCRE_EARLY_ERROR_RETURN2; |
goto PCRE_EARLY_ERROR_RETURN2; |
| 7097 |
ptr += skipatstart; |
ptr += skipatstart; |
| 7098 |
code = cworkspace; |
code = cworkspace; |
| 7099 |
*code = OP_BRA; |
*code = OP_BRA; |
| 7100 |
(void)compile_regex(cd->external_options, cd->external_options & PCRE_IMS, |
(void)compile_regex(cd->external_options, &code, &ptr, &errorcode, FALSE, |
| 7101 |
&code, &ptr, &errorcode, FALSE, FALSE, 0, &firstbyte, &reqbyte, NULL, cd, |
FALSE, 0, &firstbyte, &reqbyte, NULL, cd, &length); |
|
&length); |
|
| 7102 |
if (errorcode != 0) goto PCRE_EARLY_ERROR_RETURN; |
if (errorcode != 0) goto PCRE_EARLY_ERROR_RETURN; |
| 7103 |
|
|
| 7104 |
DPRINTF(("end pre-compile: length=%d workspace=%d\n", length, |
DPRINTF(("end pre-compile: length=%d workspace=%d\n", length, |
| 7170 |
ptr = (const uschar *)pattern + skipatstart; |
ptr = (const uschar *)pattern + skipatstart; |
| 7171 |
code = (uschar *)codestart; |
code = (uschar *)codestart; |
| 7172 |
*code = OP_BRA; |
*code = OP_BRA; |
| 7173 |
(void)compile_regex(re->options, re->options & PCRE_IMS, &code, &ptr, |
(void)compile_regex(re->options, &code, &ptr, &errorcode, FALSE, FALSE, 0, |
| 7174 |
&errorcode, FALSE, FALSE, 0, &firstbyte, &reqbyte, NULL, cd, NULL); |
&firstbyte, &reqbyte, NULL, cd, NULL); |
| 7175 |
re->top_bracket = cd->bracount; |
re->top_bracket = cd->bracount; |
| 7176 |
re->top_backref = cd->top_backref; |
re->top_backref = cd->top_backref; |
| 7177 |
re->flags = cd->external_flags; |
re->flags = cd->external_flags; |
| 7237 |
uschar *be = cc - 1 - LINK_SIZE + GET(cc, -LINK_SIZE); |
uschar *be = cc - 1 - LINK_SIZE + GET(cc, -LINK_SIZE); |
| 7238 |
int end_op = *be; |
int end_op = *be; |
| 7239 |
*be = OP_END; |
*be = OP_END; |
| 7240 |
fixed_length = find_fixedlength(cc, re->options, TRUE, cd); |
fixed_length = find_fixedlength(cc, (re->options & PCRE_UTF8) != 0, TRUE, |
| 7241 |
|
cd); |
| 7242 |
*be = end_op; |
*be = end_op; |
| 7243 |
DPRINTF(("fixed length = %d\n", fixed_length)); |
DPRINTF(("fixed length = %d\n", fixed_length)); |
| 7244 |
if (fixed_length < 0) |
if (fixed_length < 0) |
| 7277 |
|
|
| 7278 |
if ((re->options & PCRE_ANCHORED) == 0) |
if ((re->options & PCRE_ANCHORED) == 0) |
| 7279 |
{ |
{ |
| 7280 |
int temp_options = re->options; /* May get changed during these scans */ |
if (is_anchored(codestart, 0, cd->backref_map)) |
|
if (is_anchored(codestart, &temp_options, 0, cd->backref_map)) |
|
| 7281 |
re->options |= PCRE_ANCHORED; |
re->options |= PCRE_ANCHORED; |
| 7282 |
else |
else |
| 7283 |
{ |
{ |
| 7284 |
if (firstbyte < 0) |
if (firstbyte < 0) |
| 7285 |
firstbyte = find_firstassertedchar(codestart, &temp_options, FALSE); |
firstbyte = find_firstassertedchar(codestart, FALSE); |
| 7286 |
if (firstbyte >= 0) /* Remove caseless flag for non-caseable chars */ |
if (firstbyte >= 0) /* Remove caseless flag for non-caseable chars */ |
| 7287 |
{ |
{ |
| 7288 |
int ch = firstbyte & 255; |
int ch = firstbyte & 255; |