| 52 |
|
|
| 53 |
/* Returns from set_start_bits() */ |
/* Returns from set_start_bits() */ |
| 54 |
|
|
| 55 |
enum { SSB_FAIL, SSB_DONE, SSB_CONTINUE }; |
enum { SSB_FAIL, SSB_DONE, SSB_CONTINUE, SSB_UNKNOWN }; |
| 56 |
|
|
| 57 |
|
|
| 58 |
|
|
| 66 |
rather than bytes. |
rather than bytes. |
| 67 |
|
|
| 68 |
Arguments: |
Arguments: |
| 69 |
code pointer to start of group (the bracket) |
code pointer to start of group (the bracket) |
| 70 |
startcode pointer to start of the whole pattern |
startcode pointer to start of the whole pattern |
| 71 |
options the compiling options |
options the compiling options |
| 72 |
|
int RECURSE depth |
| 73 |
|
|
| 74 |
Returns: the minimum length |
Returns: the minimum length |
| 75 |
-1 if \C was encountered |
-1 if \C in UTF-8 mode or (*ACCEPT) was encountered |
| 76 |
-2 internal error (missing capturing bracket) |
-2 internal error (missing capturing bracket) |
| 77 |
|
-3 internal error (opcode not listed) |
| 78 |
*/ |
*/ |
| 79 |
|
|
| 80 |
static int |
static int |
| 81 |
find_minlength(const uschar *code, const uschar *startcode, int options) |
find_minlength(const uschar *code, const uschar *startcode, int options, |
| 82 |
|
int recurse_depth) |
| 83 |
{ |
{ |
| 84 |
int length = -1; |
int length = -1; |
| 85 |
BOOL utf8 = (options & PCRE_UTF8) != 0; |
BOOL utf8 = (options & PCRE_UTF8) != 0; |
| 87 |
register int branchlength = 0; |
register int branchlength = 0; |
| 88 |
register uschar *cc = (uschar *)code + 1 + LINK_SIZE; |
register uschar *cc = (uschar *)code + 1 + LINK_SIZE; |
| 89 |
|
|
| 90 |
if (*code == OP_CBRA || *code == OP_SCBRA) cc += 2; |
if (*code == OP_CBRA || *code == OP_SCBRA || |
| 91 |
|
*code == OP_CBRAPOS || *code == OP_SCBRAPOS) cc += 2; |
| 92 |
|
|
| 93 |
/* Scan along the opcodes for this branch. If we get to the end of the |
/* Scan along the opcodes for this branch. If we get to the end of the |
| 94 |
branch, check the length against that of the other branches. */ |
branch, check the length against that of the other branches. */ |
| 122 |
case OP_SCBRA: |
case OP_SCBRA: |
| 123 |
case OP_BRA: |
case OP_BRA: |
| 124 |
case OP_SBRA: |
case OP_SBRA: |
| 125 |
|
case OP_CBRAPOS: |
| 126 |
|
case OP_SCBRAPOS: |
| 127 |
|
case OP_BRAPOS: |
| 128 |
|
case OP_SBRAPOS: |
| 129 |
case OP_ONCE: |
case OP_ONCE: |
| 130 |
d = find_minlength(cc, startcode, options); |
d = find_minlength(cc, startcode, options, recurse_depth); |
| 131 |
if (d < 0) return d; |
if (d < 0) return d; |
| 132 |
branchlength += d; |
branchlength += d; |
| 133 |
do cc += GET(cc, 1); while (*cc == OP_ALT); |
do cc += GET(cc, 1); while (*cc == OP_ALT); |
| 134 |
cc += 1 + LINK_SIZE; |
cc += 1 + LINK_SIZE; |
| 135 |
break; |
break; |
| 136 |
|
|
| 137 |
/* Reached end of a branch; if it's a ket it is the end of a nested |
/* ACCEPT makes things far too complicated; we have to give up. */ |
| 138 |
call. If it's ALT it is an alternation in a nested call. If it is |
|
| 139 |
END it's the end of the outer call. All can be handled by the same code. */ |
case OP_ACCEPT: |
| 140 |
|
case OP_ASSERT_ACCEPT: |
| 141 |
|
return -1; |
| 142 |
|
|
| 143 |
|
/* Reached end of a branch; if it's a ket it is the end of a nested |
| 144 |
|
call. If it's ALT it is an alternation in a nested call. If it is END it's |
| 145 |
|
the end of the outer call. All can be handled by the same code. If an |
| 146 |
|
ACCEPT was previously encountered, use the length that was in force at that |
| 147 |
|
time, and pass back the shortest ACCEPT length. */ |
| 148 |
|
|
| 149 |
case OP_ALT: |
case OP_ALT: |
| 150 |
case OP_KET: |
case OP_KET: |
| 151 |
case OP_KETRMAX: |
case OP_KETRMAX: |
| 152 |
case OP_KETRMIN: |
case OP_KETRMIN: |
| 153 |
|
case OP_KETRPOS: |
| 154 |
case OP_END: |
case OP_END: |
| 155 |
if (length < 0 || (!had_recurse && branchlength < length)) |
if (length < 0 || (!had_recurse && branchlength < length)) |
| 156 |
length = branchlength; |
length = branchlength; |
| 157 |
if (*cc != OP_ALT) return length; |
if (op != OP_ALT) return length; |
| 158 |
cc += 1 + LINK_SIZE; |
cc += 1 + LINK_SIZE; |
| 159 |
branchlength = 0; |
branchlength = 0; |
| 160 |
had_recurse = FALSE; |
had_recurse = FALSE; |
| 177 |
case OP_RREF: |
case OP_RREF: |
| 178 |
case OP_NRREF: |
case OP_NRREF: |
| 179 |
case OP_DEF: |
case OP_DEF: |
|
case OP_OPT: |
|
| 180 |
case OP_CALLOUT: |
case OP_CALLOUT: |
| 181 |
case OP_SOD: |
case OP_SOD: |
| 182 |
case OP_SOM: |
case OP_SOM: |
| 183 |
case OP_EOD: |
case OP_EOD: |
| 184 |
case OP_EODN: |
case OP_EODN: |
| 185 |
case OP_CIRC: |
case OP_CIRC: |
| 186 |
|
case OP_CIRCM: |
| 187 |
case OP_DOLL: |
case OP_DOLL: |
| 188 |
|
case OP_DOLLM: |
| 189 |
case OP_NOT_WORD_BOUNDARY: |
case OP_NOT_WORD_BOUNDARY: |
| 190 |
case OP_WORD_BOUNDARY: |
case OP_WORD_BOUNDARY: |
| 191 |
cc += _pcre_OP_lengths[*cc]; |
cc += _pcre_OP_lengths[*cc]; |
| 195 |
|
|
| 196 |
case OP_BRAZERO: |
case OP_BRAZERO: |
| 197 |
case OP_BRAMINZERO: |
case OP_BRAMINZERO: |
| 198 |
|
case OP_BRAPOSZERO: |
| 199 |
case OP_SKIPZERO: |
case OP_SKIPZERO: |
| 200 |
cc += _pcre_OP_lengths[*cc]; |
cc += _pcre_OP_lengths[*cc]; |
| 201 |
do cc += GET(cc, 1); while (*cc == OP_ALT); |
do cc += GET(cc, 1); while (*cc == OP_ALT); |
| 205 |
/* Handle literal characters and + repetitions */ |
/* Handle literal characters and + repetitions */ |
| 206 |
|
|
| 207 |
case OP_CHAR: |
case OP_CHAR: |
| 208 |
case OP_CHARNC: |
case OP_CHARI: |
| 209 |
case OP_NOT: |
case OP_NOT: |
| 210 |
|
case OP_NOTI: |
| 211 |
case OP_PLUS: |
case OP_PLUS: |
| 212 |
|
case OP_PLUSI: |
| 213 |
case OP_MINPLUS: |
case OP_MINPLUS: |
| 214 |
|
case OP_MINPLUSI: |
| 215 |
case OP_POSPLUS: |
case OP_POSPLUS: |
| 216 |
|
case OP_POSPLUSI: |
| 217 |
case OP_NOTPLUS: |
case OP_NOTPLUS: |
| 218 |
|
case OP_NOTPLUSI: |
| 219 |
case OP_NOTMINPLUS: |
case OP_NOTMINPLUS: |
| 220 |
|
case OP_NOTMINPLUSI: |
| 221 |
case OP_NOTPOSPLUS: |
case OP_NOTPOSPLUS: |
| 222 |
|
case OP_NOTPOSPLUSI: |
| 223 |
branchlength++; |
branchlength++; |
| 224 |
cc += 2; |
cc += 2; |
| 225 |
#ifdef SUPPORT_UTF8 |
#ifdef SUPPORT_UTF8 |
| 238 |
need to skip over a multibyte character in UTF8 mode. */ |
need to skip over a multibyte character in UTF8 mode. */ |
| 239 |
|
|
| 240 |
case OP_EXACT: |
case OP_EXACT: |
| 241 |
|
case OP_EXACTI: |
| 242 |
case OP_NOTEXACT: |
case OP_NOTEXACT: |
| 243 |
|
case OP_NOTEXACTI: |
| 244 |
branchlength += GET2(cc,1); |
branchlength += GET2(cc,1); |
| 245 |
cc += 4; |
cc += 4; |
| 246 |
#ifdef SUPPORT_UTF8 |
#ifdef SUPPORT_UTF8 |
| 277 |
cc++; |
cc++; |
| 278 |
break; |
break; |
| 279 |
|
|
| 280 |
/* "Any newline" might match two characters */ |
/* "Any newline" might match two characters, but it also might match just |
| 281 |
|
one. */ |
| 282 |
|
|
| 283 |
case OP_ANYNL: |
case OP_ANYNL: |
| 284 |
branchlength += 2; |
branchlength += 1; |
| 285 |
cc++; |
cc++; |
| 286 |
break; |
break; |
| 287 |
|
|
| 366 |
that case we must set the minimum length to zero. */ |
that case we must set the minimum length to zero. */ |
| 367 |
|
|
| 368 |
case OP_REF: |
case OP_REF: |
| 369 |
|
case OP_REFI: |
| 370 |
if ((options & PCRE_JAVASCRIPT_COMPAT) == 0) |
if ((options & PCRE_JAVASCRIPT_COMPAT) == 0) |
| 371 |
{ |
{ |
| 372 |
ce = cs = (uschar *)_pcre_find_bracket(startcode, utf8, GET2(cc, 1)); |
ce = cs = (uschar *)_pcre_find_bracket(startcode, utf8, GET2(cc, 1)); |
| 377 |
d = 0; |
d = 0; |
| 378 |
had_recurse = TRUE; |
had_recurse = TRUE; |
| 379 |
} |
} |
| 380 |
else d = find_minlength(cs, startcode, options); |
else |
| 381 |
|
{ |
| 382 |
|
d = find_minlength(cs, startcode, options, recurse_depth); |
| 383 |
|
} |
| 384 |
} |
} |
| 385 |
else d = 0; |
else d = 0; |
| 386 |
cc += 3; |
cc += 3; |
| 397 |
cc++; |
cc++; |
| 398 |
break; |
break; |
| 399 |
|
|
| 400 |
|
case OP_CRPLUS: |
| 401 |
|
case OP_CRMINPLUS: |
| 402 |
|
min = 1; |
| 403 |
|
cc++; |
| 404 |
|
break; |
| 405 |
|
|
| 406 |
case OP_CRRANGE: |
case OP_CRRANGE: |
| 407 |
case OP_CRMINRANGE: |
case OP_CRMINRANGE: |
| 408 |
min = GET2(cc, 1); |
min = GET2(cc, 1); |
| 417 |
branchlength += min * d; |
branchlength += min * d; |
| 418 |
break; |
break; |
| 419 |
|
|
| 420 |
|
/* We can easily detect direct recursion, but not mutual recursion. This is |
| 421 |
|
caught by a recursion depth count. */ |
| 422 |
|
|
| 423 |
case OP_RECURSE: |
case OP_RECURSE: |
| 424 |
cs = ce = (uschar *)startcode + GET(cc, 1); |
cs = ce = (uschar *)startcode + GET(cc, 1); |
| 425 |
if (cs == NULL) return -2; |
if (cs == NULL) return -2; |
| 426 |
do ce += GET(ce, 1); while (*ce == OP_ALT); |
do ce += GET(ce, 1); while (*ce == OP_ALT); |
| 427 |
if (cc > cs && cc < ce) |
if ((cc > cs && cc < ce) || recurse_depth > 10) |
| 428 |
had_recurse = TRUE; |
had_recurse = TRUE; |
| 429 |
else |
else |
| 430 |
branchlength += find_minlength(cs, startcode, options); |
{ |
| 431 |
|
branchlength += find_minlength(cs, startcode, options, recurse_depth + 1); |
| 432 |
|
} |
| 433 |
cc += 1 + LINK_SIZE; |
cc += 1 + LINK_SIZE; |
| 434 |
break; |
break; |
| 435 |
|
|
| 436 |
/* Anything else does not or need not match a character. We can get the |
/* Anything else does not or need not match a character. We can get the |
| 437 |
item's length from the table, but for those that can match zero occurrences |
item's length from the table, but for those that can match zero occurrences |
| 438 |
of a character, we must take special action for UTF-8 characters. */ |
of a character, we must take special action for UTF-8 characters. As it |
| 439 |
|
happens, the "NOT" versions of these opcodes are used at present only for |
| 440 |
|
ASCII characters, so they could be omitted from this list. However, in |
| 441 |
|
future that may change, so we include them here so as not to leave a |
| 442 |
|
gotcha for a future maintainer. */ |
| 443 |
|
|
| 444 |
case OP_UPTO: |
case OP_UPTO: |
| 445 |
|
case OP_UPTOI: |
| 446 |
case OP_NOTUPTO: |
case OP_NOTUPTO: |
| 447 |
|
case OP_NOTUPTOI: |
| 448 |
case OP_MINUPTO: |
case OP_MINUPTO: |
| 449 |
|
case OP_MINUPTOI: |
| 450 |
case OP_NOTMINUPTO: |
case OP_NOTMINUPTO: |
| 451 |
|
case OP_NOTMINUPTOI: |
| 452 |
case OP_POSUPTO: |
case OP_POSUPTO: |
| 453 |
|
case OP_POSUPTOI: |
| 454 |
|
case OP_NOTPOSUPTO: |
| 455 |
|
case OP_NOTPOSUPTOI: |
| 456 |
|
|
| 457 |
case OP_STAR: |
case OP_STAR: |
| 458 |
|
case OP_STARI: |
| 459 |
|
case OP_NOTSTAR: |
| 460 |
|
case OP_NOTSTARI: |
| 461 |
case OP_MINSTAR: |
case OP_MINSTAR: |
| 462 |
|
case OP_MINSTARI: |
| 463 |
case OP_NOTMINSTAR: |
case OP_NOTMINSTAR: |
| 464 |
|
case OP_NOTMINSTARI: |
| 465 |
case OP_POSSTAR: |
case OP_POSSTAR: |
| 466 |
|
case OP_POSSTARI: |
| 467 |
case OP_NOTPOSSTAR: |
case OP_NOTPOSSTAR: |
| 468 |
|
case OP_NOTPOSSTARI: |
| 469 |
|
|
| 470 |
case OP_QUERY: |
case OP_QUERY: |
| 471 |
|
case OP_QUERYI: |
| 472 |
|
case OP_NOTQUERY: |
| 473 |
|
case OP_NOTQUERYI: |
| 474 |
case OP_MINQUERY: |
case OP_MINQUERY: |
| 475 |
|
case OP_MINQUERYI: |
| 476 |
case OP_NOTMINQUERY: |
case OP_NOTMINQUERY: |
| 477 |
|
case OP_NOTMINQUERYI: |
| 478 |
case OP_POSQUERY: |
case OP_POSQUERY: |
| 479 |
|
case OP_POSQUERYI: |
| 480 |
case OP_NOTPOSQUERY: |
case OP_NOTPOSQUERY: |
| 481 |
|
case OP_NOTPOSQUERYI: |
| 482 |
|
|
| 483 |
cc += _pcre_OP_lengths[op]; |
cc += _pcre_OP_lengths[op]; |
| 484 |
#ifdef SUPPORT_UTF8 |
#ifdef SUPPORT_UTF8 |
| 485 |
if (utf8 && cc[-1] >= 0xc0) cc += _pcre_utf8_table4[cc[-1] & 0x3f]; |
if (utf8 && cc[-1] >= 0xc0) cc += _pcre_utf8_table4[cc[-1] & 0x3f]; |
| 498 |
cc += _pcre_OP_lengths[op] + cc[1+LINK_SIZE]; |
cc += _pcre_OP_lengths[op] + cc[1+LINK_SIZE]; |
| 499 |
break; |
break; |
| 500 |
|
|
| 501 |
/* For the record, these are the opcodes that are matched by "default": |
/* The remaining opcodes are just skipped over. */ |
|
OP_ACCEPT, OP_CLOSE, OP_COMMIT, OP_FAIL, OP_PRUNE, OP_SET_SOM, OP_SKIP, |
|
|
OP_THEN. */ |
|
| 502 |
|
|
| 503 |
default: |
case OP_CLOSE: |
| 504 |
|
case OP_COMMIT: |
| 505 |
|
case OP_FAIL: |
| 506 |
|
case OP_PRUNE: |
| 507 |
|
case OP_SET_SOM: |
| 508 |
|
case OP_SKIP: |
| 509 |
|
case OP_THEN: |
| 510 |
cc += _pcre_OP_lengths[op]; |
cc += _pcre_OP_lengths[op]; |
| 511 |
break; |
break; |
| 512 |
|
|
| 513 |
|
/* This should not occur: we list all opcodes explicitly so that when |
| 514 |
|
new ones get added they are properly considered. */ |
| 515 |
|
|
| 516 |
|
default: |
| 517 |
|
return -3; |
| 518 |
} |
} |
| 519 |
} |
} |
| 520 |
/* Control never gets here */ |
/* Control never gets here */ |
| 660 |
Arguments: |
Arguments: |
| 661 |
code points to an expression |
code points to an expression |
| 662 |
start_bits points to a 32-byte table, initialized to 0 |
start_bits points to a 32-byte table, initialized to 0 |
|
caseless the current state of the caseless flag |
|
| 663 |
utf8 TRUE if in UTF-8 mode |
utf8 TRUE if in UTF-8 mode |
| 664 |
cd the block with char table pointers |
cd the block with char table pointers |
| 665 |
|
|
| 666 |
Returns: SSB_FAIL => Failed to find any starting bytes |
Returns: SSB_FAIL => Failed to find any starting bytes |
| 667 |
SSB_DONE => Found mandatory starting bytes |
SSB_DONE => Found mandatory starting bytes |
| 668 |
SSB_CONTINUE => Found optional starting bytes |
SSB_CONTINUE => Found optional starting bytes |
| 669 |
|
SSB_UNKNOWN => Hit an unrecognized opcode |
| 670 |
*/ |
*/ |
| 671 |
|
|
| 672 |
static int |
static int |
| 673 |
set_start_bits(const uschar *code, uschar *start_bits, BOOL caseless, |
set_start_bits(const uschar *code, uschar *start_bits, BOOL utf8, |
| 674 |
BOOL utf8, compile_data *cd) |
compile_data *cd) |
| 675 |
{ |
{ |
| 676 |
register int c; |
register int c; |
| 677 |
int yield = SSB_DONE; |
int yield = SSB_DONE; |
| 696 |
|
|
| 697 |
do |
do |
| 698 |
{ |
{ |
|
const uschar *tcode = code + (((int)*code == OP_CBRA)? 3:1) + LINK_SIZE; |
|
| 699 |
BOOL try_next = TRUE; |
BOOL try_next = TRUE; |
| 700 |
|
const uschar *tcode = code + 1 + LINK_SIZE; |
| 701 |
|
|
| 702 |
|
if (*code == OP_CBRA || *code == OP_SCBRA || |
| 703 |
|
*code == OP_CBRAPOS || *code == OP_SCBRAPOS) tcode += 2; |
| 704 |
|
|
| 705 |
while (try_next) /* Loop for items in this branch */ |
while (try_next) /* Loop for items in this branch */ |
| 706 |
{ |
{ |
| 707 |
int rc; |
int rc; |
| 708 |
|
|
| 709 |
switch(*tcode) |
switch(*tcode) |
| 710 |
{ |
{ |
| 711 |
/* Fail if we reach something we don't understand */ |
/* If we reach something we don't understand, it means a new opcode has |
| 712 |
|
been created that hasn't been added to this code. Hopefully this problem |
| 713 |
|
will be discovered during testing. */ |
| 714 |
|
|
| 715 |
default: |
default: |
| 716 |
|
return SSB_UNKNOWN; |
| 717 |
|
|
| 718 |
|
/* Fail for a valid opcode that implies no starting bits. */ |
| 719 |
|
|
| 720 |
|
case OP_ACCEPT: |
| 721 |
|
case OP_ASSERT_ACCEPT: |
| 722 |
|
case OP_ALLANY: |
| 723 |
|
case OP_ANY: |
| 724 |
|
case OP_ANYBYTE: |
| 725 |
|
case OP_CIRC: |
| 726 |
|
case OP_CIRCM: |
| 727 |
|
case OP_CLOSE: |
| 728 |
|
case OP_COMMIT: |
| 729 |
|
case OP_COND: |
| 730 |
|
case OP_CREF: |
| 731 |
|
case OP_DEF: |
| 732 |
|
case OP_DOLL: |
| 733 |
|
case OP_DOLLM: |
| 734 |
|
case OP_END: |
| 735 |
|
case OP_EOD: |
| 736 |
|
case OP_EODN: |
| 737 |
|
case OP_EXTUNI: |
| 738 |
|
case OP_FAIL: |
| 739 |
|
case OP_MARK: |
| 740 |
|
case OP_NCREF: |
| 741 |
|
case OP_NOT: |
| 742 |
|
case OP_NOTEXACT: |
| 743 |
|
case OP_NOTEXACTI: |
| 744 |
|
case OP_NOTI: |
| 745 |
|
case OP_NOTMINPLUS: |
| 746 |
|
case OP_NOTMINPLUSI: |
| 747 |
|
case OP_NOTMINQUERY: |
| 748 |
|
case OP_NOTMINQUERYI: |
| 749 |
|
case OP_NOTMINSTAR: |
| 750 |
|
case OP_NOTMINSTARI: |
| 751 |
|
case OP_NOTMINUPTO: |
| 752 |
|
case OP_NOTMINUPTOI: |
| 753 |
|
case OP_NOTPLUS: |
| 754 |
|
case OP_NOTPLUSI: |
| 755 |
|
case OP_NOTPOSPLUS: |
| 756 |
|
case OP_NOTPOSPLUSI: |
| 757 |
|
case OP_NOTPOSQUERY: |
| 758 |
|
case OP_NOTPOSQUERYI: |
| 759 |
|
case OP_NOTPOSSTAR: |
| 760 |
|
case OP_NOTPOSSTARI: |
| 761 |
|
case OP_NOTPOSUPTO: |
| 762 |
|
case OP_NOTPOSUPTOI: |
| 763 |
|
case OP_NOTPROP: |
| 764 |
|
case OP_NOTQUERY: |
| 765 |
|
case OP_NOTQUERYI: |
| 766 |
|
case OP_NOTSTAR: |
| 767 |
|
case OP_NOTSTARI: |
| 768 |
|
case OP_NOTUPTO: |
| 769 |
|
case OP_NOTUPTOI: |
| 770 |
|
case OP_NOT_HSPACE: |
| 771 |
|
case OP_NOT_VSPACE: |
| 772 |
|
case OP_NRREF: |
| 773 |
|
case OP_PROP: |
| 774 |
|
case OP_PRUNE: |
| 775 |
|
case OP_PRUNE_ARG: |
| 776 |
|
case OP_RECURSE: |
| 777 |
|
case OP_REF: |
| 778 |
|
case OP_REFI: |
| 779 |
|
case OP_REVERSE: |
| 780 |
|
case OP_RREF: |
| 781 |
|
case OP_SCOND: |
| 782 |
|
case OP_SET_SOM: |
| 783 |
|
case OP_SKIP: |
| 784 |
|
case OP_SKIP_ARG: |
| 785 |
|
case OP_SOD: |
| 786 |
|
case OP_SOM: |
| 787 |
|
case OP_THEN: |
| 788 |
|
case OP_THEN_ARG: |
| 789 |
|
case OP_XCLASS: |
| 790 |
return SSB_FAIL; |
return SSB_FAIL; |
| 791 |
|
|
| 792 |
|
/* We can ignore word boundary tests. */ |
| 793 |
|
|
| 794 |
|
case OP_WORD_BOUNDARY: |
| 795 |
|
case OP_NOT_WORD_BOUNDARY: |
| 796 |
|
tcode++; |
| 797 |
|
break; |
| 798 |
|
|
| 799 |
/* If we hit a bracket or a positive lookahead assertion, recurse to set |
/* If we hit a bracket or a positive lookahead assertion, recurse to set |
| 800 |
bits from within the subpattern. If it can't find anything, we have to |
bits from within the subpattern. If it can't find anything, we have to |
| 801 |
give up. If it finds some mandatory character(s), we are done for this |
give up. If it finds some mandatory character(s), we are done for this |
| 805 |
case OP_SBRA: |
case OP_SBRA: |
| 806 |
case OP_CBRA: |
case OP_CBRA: |
| 807 |
case OP_SCBRA: |
case OP_SCBRA: |
| 808 |
|
case OP_BRAPOS: |
| 809 |
|
case OP_SBRAPOS: |
| 810 |
|
case OP_CBRAPOS: |
| 811 |
|
case OP_SCBRAPOS: |
| 812 |
case OP_ONCE: |
case OP_ONCE: |
| 813 |
case OP_ASSERT: |
case OP_ASSERT: |
| 814 |
rc = set_start_bits(tcode, start_bits, caseless, utf8, cd); |
rc = set_start_bits(tcode, start_bits, utf8, cd); |
| 815 |
if (rc == SSB_FAIL) return SSB_FAIL; |
if (rc == SSB_FAIL || rc == SSB_UNKNOWN) return rc; |
| 816 |
if (rc == SSB_DONE) try_next = FALSE; else |
if (rc == SSB_DONE) try_next = FALSE; else |
| 817 |
{ |
{ |
| 818 |
do tcode += GET(tcode, 1); while (*tcode == OP_ALT); |
do tcode += GET(tcode, 1); while (*tcode == OP_ALT); |
| 835 |
case OP_KET: |
case OP_KET: |
| 836 |
case OP_KETRMAX: |
case OP_KETRMAX: |
| 837 |
case OP_KETRMIN: |
case OP_KETRMIN: |
| 838 |
|
case OP_KETRPOS: |
| 839 |
return SSB_CONTINUE; |
return SSB_CONTINUE; |
| 840 |
|
|
| 841 |
/* Skip over callout */ |
/* Skip over callout */ |
| 853 |
tcode += 1 + LINK_SIZE; |
tcode += 1 + LINK_SIZE; |
| 854 |
break; |
break; |
| 855 |
|
|
|
/* Skip over an option setting, changing the caseless flag */ |
|
|
|
|
|
case OP_OPT: |
|
|
caseless = (tcode[1] & PCRE_CASELESS) != 0; |
|
|
tcode += 2; |
|
|
break; |
|
|
|
|
| 856 |
/* BRAZERO does the bracket, but carries on. */ |
/* BRAZERO does the bracket, but carries on. */ |
| 857 |
|
|
| 858 |
case OP_BRAZERO: |
case OP_BRAZERO: |
| 859 |
case OP_BRAMINZERO: |
case OP_BRAMINZERO: |
| 860 |
if (set_start_bits(++tcode, start_bits, caseless, utf8, cd) == SSB_FAIL) |
case OP_BRAPOSZERO: |
| 861 |
return SSB_FAIL; |
rc = set_start_bits(++tcode, start_bits, utf8, cd); |
| 862 |
|
if (rc == SSB_FAIL || rc == SSB_UNKNOWN) return rc; |
| 863 |
/* ========================================================================= |
/* ========================================================================= |
| 864 |
See the comment at the head of this function concerning the next line, |
See the comment at the head of this function concerning the next line, |
| 865 |
which was an old fudge for the benefit of OS/2. |
which was an old fudge for the benefit of OS/2. |
| 885 |
case OP_QUERY: |
case OP_QUERY: |
| 886 |
case OP_MINQUERY: |
case OP_MINQUERY: |
| 887 |
case OP_POSQUERY: |
case OP_POSQUERY: |
| 888 |
tcode = set_table_bit(start_bits, tcode + 1, caseless, cd, utf8); |
tcode = set_table_bit(start_bits, tcode + 1, FALSE, cd, utf8); |
| 889 |
|
break; |
| 890 |
|
|
| 891 |
|
case OP_STARI: |
| 892 |
|
case OP_MINSTARI: |
| 893 |
|
case OP_POSSTARI: |
| 894 |
|
case OP_QUERYI: |
| 895 |
|
case OP_MINQUERYI: |
| 896 |
|
case OP_POSQUERYI: |
| 897 |
|
tcode = set_table_bit(start_bits, tcode + 1, TRUE, cd, utf8); |
| 898 |
break; |
break; |
| 899 |
|
|
| 900 |
/* Single-char upto sets the bit and tries the next */ |
/* Single-char upto sets the bit and tries the next */ |
| 902 |
case OP_UPTO: |
case OP_UPTO: |
| 903 |
case OP_MINUPTO: |
case OP_MINUPTO: |
| 904 |
case OP_POSUPTO: |
case OP_POSUPTO: |
| 905 |
tcode = set_table_bit(start_bits, tcode + 3, caseless, cd, utf8); |
tcode = set_table_bit(start_bits, tcode + 3, FALSE, cd, utf8); |
| 906 |
|
break; |
| 907 |
|
|
| 908 |
|
case OP_UPTOI: |
| 909 |
|
case OP_MINUPTOI: |
| 910 |
|
case OP_POSUPTOI: |
| 911 |
|
tcode = set_table_bit(start_bits, tcode + 3, TRUE, cd, utf8); |
| 912 |
break; |
break; |
| 913 |
|
|
| 914 |
/* At least one single char sets the bit and stops */ |
/* At least one single char sets the bit and stops */ |
| 915 |
|
|
| 916 |
case OP_EXACT: /* Fall through */ |
case OP_EXACT: |
| 917 |
tcode += 2; |
tcode += 2; |
| 918 |
|
/* Fall through */ |
| 919 |
case OP_CHAR: |
case OP_CHAR: |
|
case OP_CHARNC: |
|
| 920 |
case OP_PLUS: |
case OP_PLUS: |
| 921 |
case OP_MINPLUS: |
case OP_MINPLUS: |
| 922 |
case OP_POSPLUS: |
case OP_POSPLUS: |
| 923 |
(void)set_table_bit(start_bits, tcode + 1, caseless, cd, utf8); |
(void)set_table_bit(start_bits, tcode + 1, FALSE, cd, utf8); |
| 924 |
|
try_next = FALSE; |
| 925 |
|
break; |
| 926 |
|
|
| 927 |
|
case OP_EXACTI: |
| 928 |
|
tcode += 2; |
| 929 |
|
/* Fall through */ |
| 930 |
|
case OP_CHARI: |
| 931 |
|
case OP_PLUSI: |
| 932 |
|
case OP_MINPLUSI: |
| 933 |
|
case OP_POSPLUSI: |
| 934 |
|
(void)set_table_bit(start_bits, tcode + 1, TRUE, cd, utf8); |
| 935 |
try_next = FALSE; |
try_next = FALSE; |
| 936 |
break; |
break; |
| 937 |
|
|
| 1161 |
for (c = 0; c < 32; c++) start_bits[c] |= tcode[c]; |
for (c = 0; c < 32; c++) start_bits[c] |= tcode[c]; |
| 1162 |
} |
} |
| 1163 |
|
|
| 1164 |
/* Advance past the bit map, and act on what follows */ |
/* Advance past the bit map, and act on what follows. For a zero |
| 1165 |
|
minimum repeat, continue; otherwise stop processing. */ |
| 1166 |
|
|
| 1167 |
tcode += 32; |
tcode += 32; |
| 1168 |
switch (*tcode) |
switch (*tcode) |
| 1198 |
|
|
| 1199 |
|
|
| 1200 |
|
|
| 1201 |
|
|
| 1202 |
|
|
| 1203 |
/************************************************* |
/************************************************* |
| 1204 |
* Study a compiled expression * |
* Study a compiled expression * |
| 1205 |
*************************************************/ |
*************************************************/ |
| 1225 |
int min; |
int min; |
| 1226 |
BOOL bits_set = FALSE; |
BOOL bits_set = FALSE; |
| 1227 |
uschar start_bits[32]; |
uschar start_bits[32]; |
| 1228 |
pcre_extra *extra; |
pcre_extra *extra = NULL; |
| 1229 |
pcre_study_data *study; |
pcre_study_data *study; |
| 1230 |
const uschar *tables; |
const uschar *tables; |
| 1231 |
uschar *code; |
uschar *code; |
| 1256 |
if ((re->options & PCRE_ANCHORED) == 0 && |
if ((re->options & PCRE_ANCHORED) == 0 && |
| 1257 |
(re->flags & (PCRE_FIRSTSET|PCRE_STARTLINE)) == 0) |
(re->flags & (PCRE_FIRSTSET|PCRE_STARTLINE)) == 0) |
| 1258 |
{ |
{ |
| 1259 |
|
int rc; |
| 1260 |
|
|
| 1261 |
/* Set the character tables in the block that is passed around */ |
/* Set the character tables in the block that is passed around */ |
| 1262 |
|
|
| 1263 |
tables = re->tables; |
tables = re->tables; |
| 1273 |
/* See if we can find a fixed set of initial characters for the pattern. */ |
/* See if we can find a fixed set of initial characters for the pattern. */ |
| 1274 |
|
|
| 1275 |
memset(start_bits, 0, 32 * sizeof(uschar)); |
memset(start_bits, 0, 32 * sizeof(uschar)); |
| 1276 |
bits_set = set_start_bits(code, start_bits, |
rc = set_start_bits(code, start_bits, (re->options & PCRE_UTF8) != 0, |
| 1277 |
(re->options & PCRE_CASELESS) != 0, (re->options & PCRE_UTF8) != 0, |
&compile_block); |
| 1278 |
&compile_block) == SSB_DONE; |
bits_set = rc == SSB_DONE; |
| 1279 |
|
if (rc == SSB_UNKNOWN) |
| 1280 |
|
{ |
| 1281 |
|
*errorptr = "internal error: opcode not recognized"; |
| 1282 |
|
return NULL; |
| 1283 |
|
} |
| 1284 |
} |
} |
| 1285 |
|
|
| 1286 |
/* Find the minimum length of subject string. */ |
/* Find the minimum length of subject string. */ |
| 1287 |
|
|
| 1288 |
min = find_minlength(code, code, re->options); |
switch(min = find_minlength(code, code, re->options, 0)) |
| 1289 |
|
{ |
| 1290 |
|
case -2: *errorptr = "internal error: missing capturing bracket"; return NULL; |
| 1291 |
|
case -3: *errorptr = "internal error: opcode not recognized"; return NULL; |
| 1292 |
|
default: break; |
| 1293 |
|
} |
| 1294 |
|
|
| 1295 |
|
/* If a set of starting bytes has been identified, or if the minimum length is |
| 1296 |
|
greater than zero, or if JIT optimization has been requested, get a pcre_extra |
| 1297 |
|
block and a pcre_study_data block. The study data is put in the latter, which |
| 1298 |
|
is pointed to by the former, which may also get additional data set later by |
| 1299 |
|
the calling program. At the moment, the size of pcre_study_data is fixed. We |
| 1300 |
|
nevertheless save it in a field for returning via the pcre_fullinfo() function |
| 1301 |
|
so that if it becomes variable in the future, we don't have to change that |
| 1302 |
|
code. */ |
| 1303 |
|
|
| 1304 |
|
if (bits_set || min > 0 |
| 1305 |
|
#ifdef SUPPORT_JIT |
| 1306 |
|
|| (options & PCRE_STUDY_JIT_COMPILE) != 0 |
| 1307 |
|
#endif |
| 1308 |
|
) |
| 1309 |
|
{ |
| 1310 |
|
extra = (pcre_extra *)(pcre_malloc) |
| 1311 |
|
(sizeof(pcre_extra) + sizeof(pcre_study_data)); |
| 1312 |
|
if (extra == NULL) |
| 1313 |
|
{ |
| 1314 |
|
*errorptr = "failed to get memory"; |
| 1315 |
|
return NULL; |
| 1316 |
|
} |
| 1317 |
|
|
| 1318 |
/* Return NULL if no optimization is possible. */ |
study = (pcre_study_data *)((char *)extra + sizeof(pcre_extra)); |
| 1319 |
|
extra->flags = PCRE_EXTRA_STUDY_DATA; |
| 1320 |
|
extra->study_data = study; |
| 1321 |
|
|
| 1322 |
if (!bits_set && min < 0) return NULL; |
study->size = sizeof(pcre_study_data); |
| 1323 |
|
study->flags = 0; |
| 1324 |
|
|
| 1325 |
/* Get a pcre_extra block and a pcre_study_data block. The study data is put in |
if (bits_set) |
| 1326 |
the latter, which is pointed to by the former, which may also get additional |
{ |
| 1327 |
data set later by the calling program. At the moment, the size of |
study->flags |= PCRE_STUDY_MAPPED; |
| 1328 |
pcre_study_data is fixed. We nevertheless save it in a field for returning via |
memcpy(study->start_bits, start_bits, sizeof(start_bits)); |
| 1329 |
the pcre_fullinfo() function so that if it becomes variable in the future, we |
} |
|
don't have to change that code. */ |
|
| 1330 |
|
|
| 1331 |
extra = (pcre_extra *)(pcre_malloc) |
/* Always set the minlength value in the block, because the JIT compiler |
| 1332 |
(sizeof(pcre_extra) + sizeof(pcre_study_data)); |
makes use of it. However, don't set the bit unless the length is greater than |
| 1333 |
|
zero - the interpretive pcre_exec() and pcre_dfa_exec() needn't waste time |
| 1334 |
|
checking the zero case. */ |
| 1335 |
|
|
| 1336 |
if (extra == NULL) |
if (min > 0) |
| 1337 |
{ |
{ |
| 1338 |
*errorptr = "failed to get memory"; |
study->flags |= PCRE_STUDY_MINLEN; |
| 1339 |
return NULL; |
study->minlength = min; |
| 1340 |
|
} |
| 1341 |
|
else study->minlength = 0; |
| 1342 |
|
|
| 1343 |
|
/* If JIT support was compiled and requested, attempt the JIT compilation. |
| 1344 |
|
If no starting bytes were found, and the minimum length is zero, and JIT |
| 1345 |
|
compilation fails, abandon the extra block and return NULL. */ |
| 1346 |
|
|
| 1347 |
|
#ifdef SUPPORT_JIT |
| 1348 |
|
extra->executable_jit = NULL; |
| 1349 |
|
if ((options & PCRE_STUDY_JIT_COMPILE) != 0) _pcre_jit_compile(re, extra); |
| 1350 |
|
if (study->flags == 0 && (extra->flags & PCRE_EXTRA_EXECUTABLE_JIT) == 0) |
| 1351 |
|
{ |
| 1352 |
|
pcre_free_study(extra); |
| 1353 |
|
extra = NULL; |
| 1354 |
|
} |
| 1355 |
|
#endif |
| 1356 |
} |
} |
| 1357 |
|
|
| 1358 |
study = (pcre_study_data *)((char *)extra + sizeof(pcre_extra)); |
return extra; |
| 1359 |
extra->flags = PCRE_EXTRA_STUDY_DATA; |
} |
|
extra->study_data = study; |
|
| 1360 |
|
|
|
study->size = sizeof(pcre_study_data); |
|
|
study->flags = 0; |
|
| 1361 |
|
|
| 1362 |
if (bits_set) |
/************************************************* |
| 1363 |
{ |
* Free the study data * |
| 1364 |
study->flags |= PCRE_STUDY_MAPPED; |
*************************************************/ |
|
memcpy(study->start_bits, start_bits, sizeof(start_bits)); |
|
|
} |
|
| 1365 |
|
|
| 1366 |
if (min >= 0) |
/* This function frees the memory that was obtained by pcre_study(). |
|
{ |
|
|
study->flags |= PCRE_STUDY_MINLEN; |
|
|
study->minlength = min; |
|
|
} |
|
| 1367 |
|
|
| 1368 |
return extra; |
Argument: a pointer to the pcre_extra block |
| 1369 |
|
Returns: nothing |
| 1370 |
|
*/ |
| 1371 |
|
|
| 1372 |
|
PCRE_EXP_DEFN void |
| 1373 |
|
pcre_free_study(pcre_extra *extra) |
| 1374 |
|
{ |
| 1375 |
|
#ifdef SUPPORT_JIT |
| 1376 |
|
if ((extra->flags & PCRE_EXTRA_EXECUTABLE_JIT) != 0 && |
| 1377 |
|
extra->executable_jit != NULL) |
| 1378 |
|
_pcre_jit_free(extra->executable_jit); |
| 1379 |
|
#endif |
| 1380 |
|
pcre_free(extra); |
| 1381 |
} |
} |
| 1382 |
|
|
| 1383 |
/* End of pcre_study.c */ |
/* End of pcre_study.c */ |