| 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-2012 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 |
| 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 |
had_accept pointer to flag for (*ACCEPT) encountered |
int RECURSE depth |
|
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) |
-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 pcre_uchar *code, const pcre_uchar *startcode, int options, |
| 82 |
BOOL *had_accept_ptr, int recurse_depth) |
int recurse_depth) |
| 83 |
{ |
{ |
| 84 |
int length = -1; |
int length = -1; |
| 85 |
BOOL utf8 = (options & PCRE_UTF8) != 0; |
/* PCRE_UTF16 has the same value as PCRE_UTF8. */ |
| 86 |
|
BOOL utf = (options & PCRE_UTF8) != 0; |
| 87 |
BOOL had_recurse = FALSE; |
BOOL had_recurse = FALSE; |
| 88 |
register int branchlength = 0; |
register int branchlength = 0; |
| 89 |
register uschar *cc = (uschar *)code + 1 + LINK_SIZE; |
register pcre_uchar *cc = (pcre_uchar *)code + 1 + LINK_SIZE; |
| 90 |
|
|
| 91 |
if (*code == OP_CBRA || *code == OP_SCBRA || |
if (*code == OP_CBRA || *code == OP_SCBRA || |
| 92 |
*code == OP_CBRAPOS || *code == OP_SCBRAPOS) cc += 2; |
*code == OP_CBRAPOS || *code == OP_SCBRAPOS) cc += IMM2_SIZE; |
| 93 |
|
|
| 94 |
/* 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 |
| 95 |
branch, check the length against that of the other branches. */ |
branch, check the length against that of the other branches. */ |
| 97 |
for (;;) |
for (;;) |
| 98 |
{ |
{ |
| 99 |
int d, min; |
int d, min; |
| 100 |
uschar *cs, *ce; |
pcre_uchar *cs, *ce; |
| 101 |
register int op = *cc; |
register int op = *cc; |
| 102 |
|
|
| 103 |
switch (op) |
switch (op) |
| 128 |
case OP_BRAPOS: |
case OP_BRAPOS: |
| 129 |
case OP_SBRAPOS: |
case OP_SBRAPOS: |
| 130 |
case OP_ONCE: |
case OP_ONCE: |
| 131 |
d = find_minlength(cc, startcode, options, had_accept_ptr, recurse_depth); |
case OP_ONCE_NC: |
| 132 |
|
d = find_minlength(cc, startcode, options, recurse_depth); |
| 133 |
if (d < 0) return d; |
if (d < 0) return d; |
| 134 |
branchlength += d; |
branchlength += d; |
|
if (*had_accept_ptr) return branchlength; |
|
| 135 |
do cc += GET(cc, 1); while (*cc == OP_ALT); |
do cc += GET(cc, 1); while (*cc == OP_ALT); |
| 136 |
cc += 1 + LINK_SIZE; |
cc += 1 + LINK_SIZE; |
| 137 |
break; |
break; |
| 138 |
|
|
| 139 |
/* 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. */ |
|
call. If it's ALT it is an alternation in a nested call. If it is END it's |
|
|
the end of the outer call. All can be handled by the same code. If it is |
|
|
ACCEPT, it is essentially the same as END, but we set a flag so that |
|
|
counting stops. */ |
|
| 140 |
|
|
| 141 |
case OP_ACCEPT: |
case OP_ACCEPT: |
| 142 |
case OP_ASSERT_ACCEPT: |
case OP_ASSERT_ACCEPT: |
| 143 |
*had_accept_ptr = TRUE; |
return -1; |
| 144 |
/* Fall through */ |
|
| 145 |
|
/* Reached end of a branch; if it's a ket it is the end of a nested |
| 146 |
|
call. If it's ALT it is an alternation in a nested call. If it is END it's |
| 147 |
|
the end of the outer call. All can be handled by the same code. If an |
| 148 |
|
ACCEPT was previously encountered, use the length that was in force at that |
| 149 |
|
time, and pass back the shortest ACCEPT length. */ |
| 150 |
|
|
| 151 |
case OP_ALT: |
case OP_ALT: |
| 152 |
case OP_KET: |
case OP_KET: |
| 153 |
case OP_KETRMAX: |
case OP_KETRMAX: |
| 190 |
case OP_DOLLM: |
case OP_DOLLM: |
| 191 |
case OP_NOT_WORD_BOUNDARY: |
case OP_NOT_WORD_BOUNDARY: |
| 192 |
case OP_WORD_BOUNDARY: |
case OP_WORD_BOUNDARY: |
| 193 |
cc += _pcre_OP_lengths[*cc]; |
cc += PRIV(OP_lengths)[*cc]; |
| 194 |
break; |
break; |
| 195 |
|
|
| 196 |
/* Skip over a subpattern that has a {0} or {0,x} quantifier */ |
/* Skip over a subpattern that has a {0} or {0,x} quantifier */ |
| 199 |
case OP_BRAMINZERO: |
case OP_BRAMINZERO: |
| 200 |
case OP_BRAPOSZERO: |
case OP_BRAPOSZERO: |
| 201 |
case OP_SKIPZERO: |
case OP_SKIPZERO: |
| 202 |
cc += _pcre_OP_lengths[*cc]; |
cc += PRIV(OP_lengths)[*cc]; |
| 203 |
do cc += GET(cc, 1); while (*cc == OP_ALT); |
do cc += GET(cc, 1); while (*cc == OP_ALT); |
| 204 |
cc += 1 + LINK_SIZE; |
cc += 1 + LINK_SIZE; |
| 205 |
break; |
break; |
| 224 |
case OP_NOTPOSPLUSI: |
case OP_NOTPOSPLUSI: |
| 225 |
branchlength++; |
branchlength++; |
| 226 |
cc += 2; |
cc += 2; |
| 227 |
#ifdef SUPPORT_UTF8 |
#ifdef SUPPORT_UTF |
| 228 |
if (utf8 && cc[-1] >= 0xc0) cc += _pcre_utf8_table4[cc[-1] & 0x3f]; |
if (utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]); |
| 229 |
#endif |
#endif |
| 230 |
break; |
break; |
| 231 |
|
|
| 244 |
case OP_NOTEXACT: |
case OP_NOTEXACT: |
| 245 |
case OP_NOTEXACTI: |
case OP_NOTEXACTI: |
| 246 |
branchlength += GET2(cc,1); |
branchlength += GET2(cc,1); |
| 247 |
cc += 4; |
cc += 2 + IMM2_SIZE; |
| 248 |
#ifdef SUPPORT_UTF8 |
#ifdef SUPPORT_UTF |
| 249 |
if (utf8 && cc[-1] >= 0xc0) cc += _pcre_utf8_table4[cc[-1] & 0x3f]; |
if (utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]); |
| 250 |
#endif |
#endif |
| 251 |
break; |
break; |
| 252 |
|
|
| 253 |
case OP_TYPEEXACT: |
case OP_TYPEEXACT: |
| 254 |
branchlength += GET2(cc,1); |
branchlength += GET2(cc,1); |
| 255 |
cc += (cc[3] == OP_PROP || cc[3] == OP_NOTPROP)? 6 : 4; |
cc += 2 + IMM2_SIZE + ((cc[1 + IMM2_SIZE] == OP_PROP |
| 256 |
|
|| cc[1 + IMM2_SIZE] == OP_NOTPROP)? 2 : 0); |
| 257 |
break; |
break; |
| 258 |
|
|
| 259 |
/* Handle single-char non-literal matchers */ |
/* Handle single-char non-literal matchers */ |
| 288 |
cc++; |
cc++; |
| 289 |
break; |
break; |
| 290 |
|
|
| 291 |
/* The single-byte matcher means we can't proceed in UTF-8 mode */ |
/* The single-byte matcher means we can't proceed in UTF-8 mode. (In |
| 292 |
|
non-UTF-8 mode \C will actually be turned into OP_ALLANY, so won't ever |
| 293 |
|
appear, but leave the code, just in case.) */ |
| 294 |
|
|
| 295 |
case OP_ANYBYTE: |
case OP_ANYBYTE: |
| 296 |
#ifdef SUPPORT_UTF8 |
#ifdef SUPPORT_UTF |
| 297 |
if (utf8) return -1; |
if (utf) return -1; |
| 298 |
#endif |
#endif |
| 299 |
branchlength++; |
branchlength++; |
| 300 |
cc++; |
cc++; |
| 310 |
case OP_TYPEPOSSTAR: |
case OP_TYPEPOSSTAR: |
| 311 |
case OP_TYPEPOSQUERY: |
case OP_TYPEPOSQUERY: |
| 312 |
if (cc[1] == OP_PROP || cc[1] == OP_NOTPROP) cc += 2; |
if (cc[1] == OP_PROP || cc[1] == OP_NOTPROP) cc += 2; |
| 313 |
cc += _pcre_OP_lengths[op]; |
cc += PRIV(OP_lengths)[op]; |
| 314 |
break; |
break; |
| 315 |
|
|
| 316 |
case OP_TYPEUPTO: |
case OP_TYPEUPTO: |
| 317 |
case OP_TYPEMINUPTO: |
case OP_TYPEMINUPTO: |
| 318 |
case OP_TYPEPOSUPTO: |
case OP_TYPEPOSUPTO: |
| 319 |
if (cc[3] == OP_PROP || cc[3] == OP_NOTPROP) cc += 2; |
if (cc[1 + IMM2_SIZE] == OP_PROP |
| 320 |
cc += _pcre_OP_lengths[op]; |
|| cc[1 + IMM2_SIZE] == OP_NOTPROP) cc += 2; |
| 321 |
|
cc += PRIV(OP_lengths)[op]; |
| 322 |
break; |
break; |
| 323 |
|
|
| 324 |
/* Check a class for variable quantification */ |
/* Check a class for variable quantification */ |
| 325 |
|
|
| 326 |
#ifdef SUPPORT_UTF8 |
#if defined SUPPORT_UTF || !defined COMPILE_PCRE8 |
| 327 |
case OP_XCLASS: |
case OP_XCLASS: |
| 328 |
cc += GET(cc, 1) - 33; |
cc += GET(cc, 1) - PRIV(OP_lengths)[OP_CLASS]; |
| 329 |
/* Fall through */ |
/* Fall through */ |
| 330 |
#endif |
#endif |
| 331 |
|
|
| 332 |
case OP_CLASS: |
case OP_CLASS: |
| 333 |
case OP_NCLASS: |
case OP_NCLASS: |
| 334 |
cc += 33; |
cc += PRIV(OP_lengths)[OP_CLASS]; |
| 335 |
|
|
| 336 |
switch (*cc) |
switch (*cc) |
| 337 |
{ |
{ |
| 350 |
case OP_CRRANGE: |
case OP_CRRANGE: |
| 351 |
case OP_CRMINRANGE: |
case OP_CRMINRANGE: |
| 352 |
branchlength += GET2(cc,1); |
branchlength += GET2(cc,1); |
| 353 |
cc += 5; |
cc += 1 + 2 * IMM2_SIZE; |
| 354 |
break; |
break; |
| 355 |
|
|
| 356 |
default: |
default: |
| 375 |
case OP_REFI: |
case OP_REFI: |
| 376 |
if ((options & PCRE_JAVASCRIPT_COMPAT) == 0) |
if ((options & PCRE_JAVASCRIPT_COMPAT) == 0) |
| 377 |
{ |
{ |
| 378 |
ce = cs = (uschar *)_pcre_find_bracket(startcode, utf8, GET2(cc, 1)); |
ce = cs = (pcre_uchar *)PRIV(find_bracket)(startcode, utf, GET2(cc, 1)); |
| 379 |
if (cs == NULL) return -2; |
if (cs == NULL) return -2; |
| 380 |
do ce += GET(ce, 1); while (*ce == OP_ALT); |
do ce += GET(ce, 1); while (*ce == OP_ALT); |
| 381 |
if (cc > cs && cc < ce) |
if (cc > cs && cc < ce) |
| 385 |
} |
} |
| 386 |
else |
else |
| 387 |
{ |
{ |
| 388 |
d = find_minlength(cs, startcode, options, had_accept_ptr, |
d = find_minlength(cs, startcode, options, recurse_depth); |
|
recurse_depth); |
|
|
*had_accept_ptr = FALSE; |
|
| 389 |
} |
} |
| 390 |
} |
} |
| 391 |
else d = 0; |
else d = 0; |
| 392 |
cc += 3; |
cc += 1 + IMM2_SIZE; |
| 393 |
|
|
| 394 |
/* Handle repeated back references */ |
/* Handle repeated back references */ |
| 395 |
|
|
| 412 |
case OP_CRRANGE: |
case OP_CRRANGE: |
| 413 |
case OP_CRMINRANGE: |
case OP_CRMINRANGE: |
| 414 |
min = GET2(cc, 1); |
min = GET2(cc, 1); |
| 415 |
cc += 5; |
cc += 1 + 2 * IMM2_SIZE; |
| 416 |
break; |
break; |
| 417 |
|
|
| 418 |
default: |
default: |
| 427 |
caught by a recursion depth count. */ |
caught by a recursion depth count. */ |
| 428 |
|
|
| 429 |
case OP_RECURSE: |
case OP_RECURSE: |
| 430 |
cs = ce = (uschar *)startcode + GET(cc, 1); |
cs = ce = (pcre_uchar *)startcode + GET(cc, 1); |
|
if (cs == NULL) return -2; |
|
| 431 |
do ce += GET(ce, 1); while (*ce == OP_ALT); |
do ce += GET(ce, 1); while (*ce == OP_ALT); |
| 432 |
if ((cc > cs && cc < ce) || recurse_depth > 10) |
if ((cc > cs && cc < ce) || recurse_depth > 10) |
| 433 |
had_recurse = TRUE; |
had_recurse = TRUE; |
| 434 |
else |
else |
| 435 |
{ |
{ |
| 436 |
branchlength += find_minlength(cs, startcode, options, had_accept_ptr, |
branchlength += find_minlength(cs, startcode, options, recurse_depth + 1); |
|
recurse_depth + 1); |
|
|
*had_accept_ptr = FALSE; |
|
| 437 |
} |
} |
| 438 |
cc += 1 + LINK_SIZE; |
cc += 1 + LINK_SIZE; |
| 439 |
break; |
break; |
| 485 |
case OP_NOTPOSQUERY: |
case OP_NOTPOSQUERY: |
| 486 |
case OP_NOTPOSQUERYI: |
case OP_NOTPOSQUERYI: |
| 487 |
|
|
| 488 |
cc += _pcre_OP_lengths[op]; |
cc += PRIV(OP_lengths)[op]; |
| 489 |
#ifdef SUPPORT_UTF8 |
#ifdef SUPPORT_UTF |
| 490 |
if (utf8 && cc[-1] >= 0xc0) cc += _pcre_utf8_table4[cc[-1] & 0x3f]; |
if (utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]); |
| 491 |
#endif |
#endif |
| 492 |
break; |
break; |
| 493 |
|
|
| 496 |
case OP_MARK: |
case OP_MARK: |
| 497 |
case OP_PRUNE_ARG: |
case OP_PRUNE_ARG: |
| 498 |
case OP_SKIP_ARG: |
case OP_SKIP_ARG: |
|
cc += _pcre_OP_lengths[op] + cc[1]; |
|
|
break; |
|
|
|
|
| 499 |
case OP_THEN_ARG: |
case OP_THEN_ARG: |
| 500 |
cc += _pcre_OP_lengths[op] + cc[1+LINK_SIZE]; |
cc += PRIV(OP_lengths)[op] + cc[1]; |
| 501 |
break; |
break; |
| 502 |
|
|
| 503 |
/* The remaining opcodes are just skipped over. */ |
/* The remaining opcodes are just skipped over. */ |
| 509 |
case OP_SET_SOM: |
case OP_SET_SOM: |
| 510 |
case OP_SKIP: |
case OP_SKIP: |
| 511 |
case OP_THEN: |
case OP_THEN: |
| 512 |
cc += _pcre_OP_lengths[op]; |
cc += PRIV(OP_lengths)[op]; |
| 513 |
break; |
break; |
| 514 |
|
|
| 515 |
/* This should not occur: we list all opcodes explicitly so that when |
/* This should not occur: we list all opcodes explicitly so that when |
| 538 |
p points to the character |
p points to the character |
| 539 |
caseless the caseless flag |
caseless the caseless flag |
| 540 |
cd the block with char table pointers |
cd the block with char table pointers |
| 541 |
utf8 TRUE for UTF-8 mode |
utf TRUE for UTF-8 / UTF-16 mode |
| 542 |
|
|
| 543 |
Returns: pointer after the character |
Returns: pointer after the character |
| 544 |
*/ |
*/ |
| 545 |
|
|
| 546 |
static const uschar * |
static const pcre_uchar * |
| 547 |
set_table_bit(uschar *start_bits, const uschar *p, BOOL caseless, |
set_table_bit(pcre_uint8 *start_bits, const pcre_uchar *p, BOOL caseless, |
| 548 |
compile_data *cd, BOOL utf8) |
compile_data *cd, BOOL utf) |
| 549 |
{ |
{ |
| 550 |
unsigned int c = *p; |
unsigned int c = *p; |
| 551 |
|
|
| 552 |
|
#ifdef COMPILE_PCRE8 |
| 553 |
SET_BIT(c); |
SET_BIT(c); |
| 554 |
|
|
| 555 |
#ifdef SUPPORT_UTF8 |
#ifdef SUPPORT_UTF |
| 556 |
if (utf8 && c > 127) |
if (utf && c > 127) |
| 557 |
{ |
{ |
| 558 |
GETCHARINC(c, p); |
GETCHARINC(c, p); |
| 559 |
#ifdef SUPPORT_UCP |
#ifdef SUPPORT_UCP |
| 560 |
if (caseless) |
if (caseless) |
| 561 |
{ |
{ |
| 562 |
uschar buff[8]; |
pcre_uchar buff[6]; |
| 563 |
c = UCD_OTHERCASE(c); |
c = UCD_OTHERCASE(c); |
| 564 |
(void)_pcre_ord2utf8(c, buff); |
(void)PRIV(ord2utf)(c, buff); |
| 565 |
SET_BIT(buff[0]); |
SET_BIT(buff[0]); |
| 566 |
} |
} |
| 567 |
#endif |
#endif |
| 573 |
|
|
| 574 |
if (caseless && (cd->ctypes[c] & ctype_letter) != 0) SET_BIT(cd->fcc[c]); |
if (caseless && (cd->ctypes[c] & ctype_letter) != 0) SET_BIT(cd->fcc[c]); |
| 575 |
return p + 1; |
return p + 1; |
| 576 |
|
#endif |
| 577 |
|
|
| 578 |
|
#ifdef COMPILE_PCRE16 |
| 579 |
|
if (c > 0xff) |
| 580 |
|
{ |
| 581 |
|
c = 0xff; |
| 582 |
|
caseless = FALSE; |
| 583 |
|
} |
| 584 |
|
SET_BIT(c); |
| 585 |
|
|
| 586 |
|
#ifdef SUPPORT_UTF |
| 587 |
|
if (utf && c > 127) |
| 588 |
|
{ |
| 589 |
|
GETCHARINC(c, p); |
| 590 |
|
#ifdef SUPPORT_UCP |
| 591 |
|
if (caseless) |
| 592 |
|
{ |
| 593 |
|
c = UCD_OTHERCASE(c); |
| 594 |
|
if (c > 0xff) |
| 595 |
|
c = 0xff; |
| 596 |
|
SET_BIT(c); |
| 597 |
|
} |
| 598 |
|
#endif |
| 599 |
|
return p; |
| 600 |
|
} |
| 601 |
|
#endif |
| 602 |
|
|
| 603 |
|
if (caseless && (cd->ctypes[c] & ctype_letter) != 0) SET_BIT(cd->fcc[c]); |
| 604 |
|
return p + 1; |
| 605 |
|
#endif |
| 606 |
} |
} |
| 607 |
|
|
| 608 |
|
|
| 628 |
*/ |
*/ |
| 629 |
|
|
| 630 |
static void |
static void |
| 631 |
set_type_bits(uschar *start_bits, int cbit_type, int table_limit, |
set_type_bits(pcre_uint8 *start_bits, int cbit_type, int table_limit, |
| 632 |
compile_data *cd) |
compile_data *cd) |
| 633 |
{ |
{ |
| 634 |
register int c; |
register int c; |
| 635 |
for (c = 0; c < table_limit; c++) start_bits[c] |= cd->cbits[c+cbit_type]; |
for (c = 0; c < table_limit; c++) start_bits[c] |= cd->cbits[c+cbit_type]; |
| 636 |
|
#if defined SUPPORT_UTF && defined COMPILE_PCRE8 |
| 637 |
if (table_limit == 32) return; |
if (table_limit == 32) return; |
| 638 |
for (c = 128; c < 256; c++) |
for (c = 128; c < 256; c++) |
| 639 |
{ |
{ |
| 640 |
if ((cd->cbits[c/8] & (1 << (c&7))) != 0) |
if ((cd->cbits[c/8] & (1 << (c&7))) != 0) |
| 641 |
{ |
{ |
| 642 |
uschar buff[8]; |
pcre_uchar buff[6]; |
| 643 |
(void)_pcre_ord2utf8(c, buff); |
(void)PRIV(ord2utf)(c, buff); |
| 644 |
SET_BIT(buff[0]); |
SET_BIT(buff[0]); |
| 645 |
} |
} |
| 646 |
} |
} |
| 647 |
|
#endif |
| 648 |
} |
} |
| 649 |
|
|
| 650 |
|
|
| 670 |
*/ |
*/ |
| 671 |
|
|
| 672 |
static void |
static void |
| 673 |
set_nottype_bits(uschar *start_bits, int cbit_type, int table_limit, |
set_nottype_bits(pcre_uint8 *start_bits, int cbit_type, int table_limit, |
| 674 |
compile_data *cd) |
compile_data *cd) |
| 675 |
{ |
{ |
| 676 |
register int c; |
register int c; |
| 677 |
for (c = 0; c < table_limit; c++) start_bits[c] |= ~cd->cbits[c+cbit_type]; |
for (c = 0; c < table_limit; c++) start_bits[c] |= ~cd->cbits[c+cbit_type]; |
| 678 |
|
#if defined SUPPORT_UTF && defined COMPILE_PCRE8 |
| 679 |
if (table_limit != 32) for (c = 24; c < 32; c++) start_bits[c] = 0xff; |
if (table_limit != 32) for (c = 24; c < 32; c++) start_bits[c] = 0xff; |
| 680 |
|
#endif |
| 681 |
} |
} |
| 682 |
|
|
| 683 |
|
|
| 697 |
Arguments: |
Arguments: |
| 698 |
code points to an expression |
code points to an expression |
| 699 |
start_bits points to a 32-byte table, initialized to 0 |
start_bits points to a 32-byte table, initialized to 0 |
| 700 |
utf8 TRUE if in UTF-8 mode |
utf TRUE if in UTF-8 / UTF-16 mode |
| 701 |
cd the block with char table pointers |
cd the block with char table pointers |
| 702 |
|
|
| 703 |
Returns: SSB_FAIL => Failed to find any starting bytes |
Returns: SSB_FAIL => Failed to find any starting bytes |
| 707 |
*/ |
*/ |
| 708 |
|
|
| 709 |
static int |
static int |
| 710 |
set_start_bits(const uschar *code, uschar *start_bits, BOOL utf8, |
set_start_bits(const pcre_uchar *code, pcre_uint8 *start_bits, BOOL utf, |
| 711 |
compile_data *cd) |
compile_data *cd) |
| 712 |
{ |
{ |
| 713 |
register int c; |
register int c; |
| 714 |
int yield = SSB_DONE; |
int yield = SSB_DONE; |
| 715 |
int table_limit = utf8? 16:32; |
#if defined SUPPORT_UTF && defined COMPILE_PCRE8 |
| 716 |
|
int table_limit = utf? 16:32; |
| 717 |
|
#else |
| 718 |
|
int table_limit = 32; |
| 719 |
|
#endif |
| 720 |
|
|
| 721 |
#if 0 |
#if 0 |
| 722 |
/* ========================================================================= */ |
/* ========================================================================= */ |
| 738 |
do |
do |
| 739 |
{ |
{ |
| 740 |
BOOL try_next = TRUE; |
BOOL try_next = TRUE; |
| 741 |
const uschar *tcode = code + 1 + LINK_SIZE; |
const pcre_uchar *tcode = code + 1 + LINK_SIZE; |
| 742 |
|
|
| 743 |
if (*code == OP_CBRA || *code == OP_SCBRA || |
if (*code == OP_CBRA || *code == OP_SCBRA || |
| 744 |
*code == OP_CBRAPOS || *code == OP_SCBRAPOS) tcode += 2; |
*code == OP_CBRAPOS || *code == OP_SCBRAPOS) tcode += IMM2_SIZE; |
| 745 |
|
|
| 746 |
while (try_next) /* Loop for items in this branch */ |
while (try_next) /* Loop for items in this branch */ |
| 747 |
{ |
{ |
| 810 |
case OP_NOTUPTOI: |
case OP_NOTUPTOI: |
| 811 |
case OP_NOT_HSPACE: |
case OP_NOT_HSPACE: |
| 812 |
case OP_NOT_VSPACE: |
case OP_NOT_VSPACE: |
|
case OP_NOT_WORD_BOUNDARY: |
|
| 813 |
case OP_NRREF: |
case OP_NRREF: |
| 814 |
case OP_PROP: |
case OP_PROP: |
| 815 |
case OP_PRUNE: |
case OP_PRUNE: |
| 827 |
case OP_SOM: |
case OP_SOM: |
| 828 |
case OP_THEN: |
case OP_THEN: |
| 829 |
case OP_THEN_ARG: |
case OP_THEN_ARG: |
| 830 |
case OP_WORD_BOUNDARY: |
#if defined SUPPORT_UTF || !defined COMPILE_PCRE8 |
| 831 |
case OP_XCLASS: |
case OP_XCLASS: |
| 832 |
|
#endif |
| 833 |
return SSB_FAIL; |
return SSB_FAIL; |
| 834 |
|
|
| 835 |
|
/* We can ignore word boundary tests. */ |
| 836 |
|
|
| 837 |
|
case OP_WORD_BOUNDARY: |
| 838 |
|
case OP_NOT_WORD_BOUNDARY: |
| 839 |
|
tcode++; |
| 840 |
|
break; |
| 841 |
|
|
| 842 |
/* 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 |
| 843 |
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 |
| 844 |
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 |
| 853 |
case OP_CBRAPOS: |
case OP_CBRAPOS: |
| 854 |
case OP_SCBRAPOS: |
case OP_SCBRAPOS: |
| 855 |
case OP_ONCE: |
case OP_ONCE: |
| 856 |
|
case OP_ONCE_NC: |
| 857 |
case OP_ASSERT: |
case OP_ASSERT: |
| 858 |
rc = set_start_bits(tcode, start_bits, utf8, cd); |
rc = set_start_bits(tcode, start_bits, utf, cd); |
| 859 |
if (rc == SSB_FAIL || rc == SSB_UNKNOWN) return rc; |
if (rc == SSB_FAIL || rc == SSB_UNKNOWN) return rc; |
| 860 |
if (rc == SSB_DONE) try_next = FALSE; else |
if (rc == SSB_DONE) try_next = FALSE; else |
| 861 |
{ |
{ |
| 902 |
case OP_BRAZERO: |
case OP_BRAZERO: |
| 903 |
case OP_BRAMINZERO: |
case OP_BRAMINZERO: |
| 904 |
case OP_BRAPOSZERO: |
case OP_BRAPOSZERO: |
| 905 |
rc = set_start_bits(++tcode, start_bits, utf8, cd); |
rc = set_start_bits(++tcode, start_bits, utf, cd); |
| 906 |
if (rc == SSB_FAIL || rc == SSB_UNKNOWN) return rc; |
if (rc == SSB_FAIL || rc == SSB_UNKNOWN) return rc; |
| 907 |
/* ========================================================================= |
/* ========================================================================= |
| 908 |
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, |
| 929 |
case OP_QUERY: |
case OP_QUERY: |
| 930 |
case OP_MINQUERY: |
case OP_MINQUERY: |
| 931 |
case OP_POSQUERY: |
case OP_POSQUERY: |
| 932 |
tcode = set_table_bit(start_bits, tcode + 1, FALSE, cd, utf8); |
tcode = set_table_bit(start_bits, tcode + 1, FALSE, cd, utf); |
| 933 |
break; |
break; |
| 934 |
|
|
| 935 |
case OP_STARI: |
case OP_STARI: |
| 938 |
case OP_QUERYI: |
case OP_QUERYI: |
| 939 |
case OP_MINQUERYI: |
case OP_MINQUERYI: |
| 940 |
case OP_POSQUERYI: |
case OP_POSQUERYI: |
| 941 |
tcode = set_table_bit(start_bits, tcode + 1, TRUE, cd, utf8); |
tcode = set_table_bit(start_bits, tcode + 1, TRUE, cd, utf); |
| 942 |
break; |
break; |
| 943 |
|
|
| 944 |
/* Single-char upto sets the bit and tries the next */ |
/* Single-char upto sets the bit and tries the next */ |
| 946 |
case OP_UPTO: |
case OP_UPTO: |
| 947 |
case OP_MINUPTO: |
case OP_MINUPTO: |
| 948 |
case OP_POSUPTO: |
case OP_POSUPTO: |
| 949 |
tcode = set_table_bit(start_bits, tcode + 3, FALSE, cd, utf8); |
tcode = set_table_bit(start_bits, tcode + 1 + IMM2_SIZE, FALSE, cd, utf); |
| 950 |
break; |
break; |
| 951 |
|
|
| 952 |
case OP_UPTOI: |
case OP_UPTOI: |
| 953 |
case OP_MINUPTOI: |
case OP_MINUPTOI: |
| 954 |
case OP_POSUPTOI: |
case OP_POSUPTOI: |
| 955 |
tcode = set_table_bit(start_bits, tcode + 3, TRUE, cd, utf8); |
tcode = set_table_bit(start_bits, tcode + 1 + IMM2_SIZE, TRUE, cd, utf); |
| 956 |
break; |
break; |
| 957 |
|
|
| 958 |
/* At least one single char sets the bit and stops */ |
/* At least one single char sets the bit and stops */ |
| 959 |
|
|
| 960 |
case OP_EXACT: |
case OP_EXACT: |
| 961 |
tcode += 2; |
tcode += IMM2_SIZE; |
| 962 |
/* Fall through */ |
/* Fall through */ |
| 963 |
case OP_CHAR: |
case OP_CHAR: |
| 964 |
case OP_PLUS: |
case OP_PLUS: |
| 965 |
case OP_MINPLUS: |
case OP_MINPLUS: |
| 966 |
case OP_POSPLUS: |
case OP_POSPLUS: |
| 967 |
(void)set_table_bit(start_bits, tcode + 1, FALSE, cd, utf8); |
(void)set_table_bit(start_bits, tcode + 1, FALSE, cd, utf); |
| 968 |
try_next = FALSE; |
try_next = FALSE; |
| 969 |
break; |
break; |
| 970 |
|
|
| 971 |
case OP_EXACTI: |
case OP_EXACTI: |
| 972 |
tcode += 2; |
tcode += IMM2_SIZE; |
| 973 |
/* Fall through */ |
/* Fall through */ |
| 974 |
case OP_CHARI: |
case OP_CHARI: |
| 975 |
case OP_PLUSI: |
case OP_PLUSI: |
| 976 |
case OP_MINPLUSI: |
case OP_MINPLUSI: |
| 977 |
case OP_POSPLUSI: |
case OP_POSPLUSI: |
| 978 |
(void)set_table_bit(start_bits, tcode + 1, TRUE, cd, utf8); |
(void)set_table_bit(start_bits, tcode + 1, TRUE, cd, utf); |
| 979 |
try_next = FALSE; |
try_next = FALSE; |
| 980 |
break; |
break; |
| 981 |
|
|
| 988 |
case OP_HSPACE: |
case OP_HSPACE: |
| 989 |
SET_BIT(0x09); |
SET_BIT(0x09); |
| 990 |
SET_BIT(0x20); |
SET_BIT(0x20); |
| 991 |
if (utf8) |
#ifdef SUPPORT_UTF |
| 992 |
|
if (utf) |
| 993 |
{ |
{ |
| 994 |
|
#ifdef COMPILE_PCRE8 |
| 995 |
SET_BIT(0xC2); /* For U+00A0 */ |
SET_BIT(0xC2); /* For U+00A0 */ |
| 996 |
SET_BIT(0xE1); /* For U+1680, U+180E */ |
SET_BIT(0xE1); /* For U+1680, U+180E */ |
| 997 |
SET_BIT(0xE2); /* For U+2000 - U+200A, U+202F, U+205F */ |
SET_BIT(0xE2); /* For U+2000 - U+200A, U+202F, U+205F */ |
| 998 |
SET_BIT(0xE3); /* For U+3000 */ |
SET_BIT(0xE3); /* For U+3000 */ |
| 999 |
|
#endif |
| 1000 |
|
#ifdef COMPILE_PCRE16 |
| 1001 |
|
SET_BIT(0xA0); |
| 1002 |
|
SET_BIT(0xFF); /* For characters > 255 */ |
| 1003 |
|
#endif |
| 1004 |
|
} |
| 1005 |
|
else |
| 1006 |
|
#endif /* SUPPORT_UTF */ |
| 1007 |
|
{ |
| 1008 |
|
SET_BIT(0xA0); |
| 1009 |
|
#ifdef COMPILE_PCRE16 |
| 1010 |
|
SET_BIT(0xFF); /* For characters > 255 */ |
| 1011 |
|
#endif |
| 1012 |
} |
} |
|
else SET_BIT(0xA0); |
|
| 1013 |
try_next = FALSE; |
try_next = FALSE; |
| 1014 |
break; |
break; |
| 1015 |
|
|
| 1019 |
SET_BIT(0x0B); |
SET_BIT(0x0B); |
| 1020 |
SET_BIT(0x0C); |
SET_BIT(0x0C); |
| 1021 |
SET_BIT(0x0D); |
SET_BIT(0x0D); |
| 1022 |
if (utf8) |
#ifdef SUPPORT_UTF |
| 1023 |
|
if (utf) |
| 1024 |
{ |
{ |
| 1025 |
|
#ifdef COMPILE_PCRE8 |
| 1026 |
SET_BIT(0xC2); /* For U+0085 */ |
SET_BIT(0xC2); /* For U+0085 */ |
| 1027 |
SET_BIT(0xE2); /* For U+2028, U+2029 */ |
SET_BIT(0xE2); /* For U+2028, U+2029 */ |
| 1028 |
|
#endif |
| 1029 |
|
#ifdef COMPILE_PCRE16 |
| 1030 |
|
SET_BIT(0x85); |
| 1031 |
|
SET_BIT(0xFF); /* For characters > 255 */ |
| 1032 |
|
#endif |
| 1033 |
|
} |
| 1034 |
|
else |
| 1035 |
|
#endif /* SUPPORT_UTF */ |
| 1036 |
|
{ |
| 1037 |
|
SET_BIT(0x85); |
| 1038 |
|
#ifdef COMPILE_PCRE16 |
| 1039 |
|
SET_BIT(0xFF); /* For characters > 255 */ |
| 1040 |
|
#endif |
| 1041 |
} |
} |
|
else SET_BIT(0x85); |
|
| 1042 |
try_next = FALSE; |
try_next = FALSE; |
| 1043 |
break; |
break; |
| 1044 |
|
|
| 1096 |
break; |
break; |
| 1097 |
|
|
| 1098 |
case OP_TYPEEXACT: |
case OP_TYPEEXACT: |
| 1099 |
tcode += 3; |
tcode += 1 + IMM2_SIZE; |
| 1100 |
break; |
break; |
| 1101 |
|
|
| 1102 |
/* Zero or more repeats of character types set the bits and then |
/* Zero or more repeats of character types set the bits and then |
| 1105 |
case OP_TYPEUPTO: |
case OP_TYPEUPTO: |
| 1106 |
case OP_TYPEMINUPTO: |
case OP_TYPEMINUPTO: |
| 1107 |
case OP_TYPEPOSUPTO: |
case OP_TYPEPOSUPTO: |
| 1108 |
tcode += 2; /* Fall through */ |
tcode += IMM2_SIZE; /* Fall through */ |
| 1109 |
|
|
| 1110 |
case OP_TYPESTAR: |
case OP_TYPESTAR: |
| 1111 |
case OP_TYPEMINSTAR: |
case OP_TYPEMINSTAR: |
| 1123 |
case OP_HSPACE: |
case OP_HSPACE: |
| 1124 |
SET_BIT(0x09); |
SET_BIT(0x09); |
| 1125 |
SET_BIT(0x20); |
SET_BIT(0x20); |
| 1126 |
if (utf8) |
#ifdef COMPILE_PCRE8 |
| 1127 |
|
if (utf) |
| 1128 |
{ |
{ |
| 1129 |
|
#ifdef COMPILE_PCRE8 |
| 1130 |
SET_BIT(0xC2); /* For U+00A0 */ |
SET_BIT(0xC2); /* For U+00A0 */ |
| 1131 |
SET_BIT(0xE1); /* For U+1680, U+180E */ |
SET_BIT(0xE1); /* For U+1680, U+180E */ |
| 1132 |
SET_BIT(0xE2); /* For U+2000 - U+200A, U+202F, U+205F */ |
SET_BIT(0xE2); /* For U+2000 - U+200A, U+202F, U+205F */ |
| 1133 |
SET_BIT(0xE3); /* For U+3000 */ |
SET_BIT(0xE3); /* For U+3000 */ |
| 1134 |
|
#endif |
| 1135 |
|
#ifdef COMPILE_PCRE16 |
| 1136 |
|
SET_BIT(0xA0); |
| 1137 |
|
SET_BIT(0xFF); /* For characters > 255 */ |
| 1138 |
|
#endif |
| 1139 |
} |
} |
| 1140 |
else SET_BIT(0xA0); |
else |
| 1141 |
|
#endif /* SUPPORT_UTF */ |
| 1142 |
|
SET_BIT(0xA0); |
| 1143 |
break; |
break; |
| 1144 |
|
|
| 1145 |
case OP_ANYNL: |
case OP_ANYNL: |
| 1148 |
SET_BIT(0x0B); |
SET_BIT(0x0B); |
| 1149 |
SET_BIT(0x0C); |
SET_BIT(0x0C); |
| 1150 |
SET_BIT(0x0D); |
SET_BIT(0x0D); |
| 1151 |
if (utf8) |
#ifdef COMPILE_PCRE8 |
| 1152 |
|
if (utf) |
| 1153 |
{ |
{ |
| 1154 |
|
#ifdef COMPILE_PCRE8 |
| 1155 |
SET_BIT(0xC2); /* For U+0085 */ |
SET_BIT(0xC2); /* For U+0085 */ |
| 1156 |
SET_BIT(0xE2); /* For U+2028, U+2029 */ |
SET_BIT(0xE2); /* For U+2028, U+2029 */ |
| 1157 |
|
#endif |
| 1158 |
|
#ifdef COMPILE_PCRE16 |
| 1159 |
|
SET_BIT(0x85); |
| 1160 |
|
SET_BIT(0xFF); /* For characters > 255 */ |
| 1161 |
|
#endif |
| 1162 |
} |
} |
| 1163 |
else SET_BIT(0x85); |
else |
| 1164 |
|
#endif /* SUPPORT_UTF */ |
| 1165 |
|
SET_BIT(0x85); |
| 1166 |
break; |
break; |
| 1167 |
|
|
| 1168 |
case OP_NOT_DIGIT: |
case OP_NOT_DIGIT: |
| 1209 |
character with a value > 255. */ |
character with a value > 255. */ |
| 1210 |
|
|
| 1211 |
case OP_NCLASS: |
case OP_NCLASS: |
| 1212 |
#ifdef SUPPORT_UTF8 |
#if defined SUPPORT_UTF && defined COMPILE_PCRE8 |
| 1213 |
if (utf8) |
if (utf) |
| 1214 |
{ |
{ |
| 1215 |
start_bits[24] |= 0xf0; /* Bits for 0xc4 - 0xc8 */ |
start_bits[24] |= 0xf0; /* Bits for 0xc4 - 0xc8 */ |
| 1216 |
memset(start_bits+25, 0xff, 7); /* Bits for 0xc9 - 0xff */ |
memset(start_bits+25, 0xff, 7); /* Bits for 0xc9 - 0xff */ |
| 1217 |
} |
} |
| 1218 |
#endif |
#endif |
| 1219 |
|
#ifdef COMPILE_PCRE16 |
| 1220 |
|
SET_BIT(0xFF); /* For characters > 255 */ |
| 1221 |
|
#endif |
| 1222 |
/* Fall through */ |
/* Fall through */ |
| 1223 |
|
|
| 1224 |
case OP_CLASS: |
case OP_CLASS: |
| 1225 |
{ |
{ |
| 1226 |
|
pcre_uint8 *map; |
| 1227 |
tcode++; |
tcode++; |
| 1228 |
|
map = (pcre_uint8 *)tcode; |
| 1229 |
|
|
| 1230 |
/* In UTF-8 mode, the bits in a bit map correspond to character |
/* In UTF-8 mode, the bits in a bit map correspond to character |
| 1231 |
values, not to byte values. However, the bit map we are constructing is |
values, not to byte values. However, the bit map we are constructing is |
| 1233 |
value is > 127. In fact, there are only two possible starting bytes for |
value is > 127. In fact, there are only two possible starting bytes for |
| 1234 |
characters in the range 128 - 255. */ |
characters in the range 128 - 255. */ |
| 1235 |
|
|
| 1236 |
#ifdef SUPPORT_UTF8 |
#if defined SUPPORT_UTF && defined COMPILE_PCRE8 |
| 1237 |
if (utf8) |
if (utf) |
| 1238 |
{ |
{ |
| 1239 |
for (c = 0; c < 16; c++) start_bits[c] |= tcode[c]; |
for (c = 0; c < 16; c++) start_bits[c] |= map[c]; |
| 1240 |
for (c = 128; c < 256; c++) |
for (c = 128; c < 256; c++) |
| 1241 |
{ |
{ |
| 1242 |
if ((tcode[c/8] && (1 << (c&7))) != 0) |
if ((map[c/8] && (1 << (c&7))) != 0) |
| 1243 |
{ |
{ |
| 1244 |
int d = (c >> 6) | 0xc0; /* Set bit for this starter */ |
int d = (c >> 6) | 0xc0; /* Set bit for this starter */ |
| 1245 |
start_bits[d/8] |= (1 << (d&7)); /* and then skip on to the */ |
start_bits[d/8] |= (1 << (d&7)); /* and then skip on to the */ |
| 1247 |
} |
} |
| 1248 |
} |
} |
| 1249 |
} |
} |
|
|
|
|
/* In non-UTF-8 mode, the two bit maps are completely compatible. */ |
|
|
|
|
| 1250 |
else |
else |
| 1251 |
#endif |
#endif |
| 1252 |
{ |
{ |
| 1253 |
for (c = 0; c < 32; c++) start_bits[c] |= tcode[c]; |
/* In non-UTF-8 mode, the two bit maps are completely compatible. */ |
| 1254 |
|
for (c = 0; c < 32; c++) start_bits[c] |= map[c]; |
| 1255 |
} |
} |
| 1256 |
|
|
| 1257 |
/* Advance past the bit map, and act on what follows. For a zero |
/* Advance past the bit map, and act on what follows. For a zero |
| 1258 |
minimum repeat, continue; otherwise stop processing. */ |
minimum repeat, continue; otherwise stop processing. */ |
| 1259 |
|
|
| 1260 |
tcode += 32; |
tcode += 32 / sizeof(pcre_uchar); |
| 1261 |
switch (*tcode) |
switch (*tcode) |
| 1262 |
{ |
{ |
| 1263 |
case OP_CRSTAR: |
case OP_CRSTAR: |
| 1269 |
|
|
| 1270 |
case OP_CRRANGE: |
case OP_CRRANGE: |
| 1271 |
case OP_CRMINRANGE: |
case OP_CRMINRANGE: |
| 1272 |
if (((tcode[1] << 8) + tcode[2]) == 0) tcode += 5; |
if (GET2(tcode, 1) == 0) tcode += 1 + 2 * IMM2_SIZE; |
| 1273 |
else try_next = FALSE; |
else try_next = FALSE; |
| 1274 |
break; |
break; |
| 1275 |
|
|
| 1298 |
*************************************************/ |
*************************************************/ |
| 1299 |
|
|
| 1300 |
/* This function is handed a compiled expression that it must study to produce |
/* This function is handed a compiled expression that it must study to produce |
| 1301 |
information that will speed up the matching. It returns a pcre_extra block |
information that will speed up the matching. It returns a pcre[16]_extra block |
| 1302 |
which then gets handed back to pcre_exec(). |
which then gets handed back to pcre_exec(). |
| 1303 |
|
|
| 1304 |
Arguments: |
Arguments: |
| 1307 |
errorptr points to where to place error messages; |
errorptr points to where to place error messages; |
| 1308 |
set NULL unless error |
set NULL unless error |
| 1309 |
|
|
| 1310 |
Returns: pointer to a pcre_extra block, with study_data filled in and the |
Returns: pointer to a pcre[16]_extra block, with study_data filled in and |
| 1311 |
appropriate flags set; |
the appropriate flags set; |
| 1312 |
NULL on error or if no optimization possible |
NULL on error or if no optimization possible |
| 1313 |
*/ |
*/ |
| 1314 |
|
|
| 1315 |
|
#ifdef COMPILE_PCRE8 |
| 1316 |
PCRE_EXP_DEFN pcre_extra * PCRE_CALL_CONVENTION |
PCRE_EXP_DEFN pcre_extra * PCRE_CALL_CONVENTION |
| 1317 |
pcre_study(const pcre *external_re, int options, const char **errorptr) |
pcre_study(const pcre *external_re, int options, const char **errorptr) |
| 1318 |
|
#else |
| 1319 |
|
PCRE_EXP_DEFN pcre16_extra * PCRE_CALL_CONVENTION |
| 1320 |
|
pcre16_study(const pcre *external_re, int options, const char **errorptr) |
| 1321 |
|
#endif |
| 1322 |
{ |
{ |
| 1323 |
int min; |
int min; |
| 1324 |
BOOL bits_set = FALSE; |
BOOL bits_set = FALSE; |
| 1325 |
BOOL had_accept = FALSE; |
pcre_uint8 start_bits[32]; |
| 1326 |
uschar start_bits[32]; |
PUBL(extra) *extra = NULL; |
|
pcre_extra *extra; |
|
| 1327 |
pcre_study_data *study; |
pcre_study_data *study; |
| 1328 |
const uschar *tables; |
const pcre_uint8 *tables; |
| 1329 |
uschar *code; |
pcre_uchar *code; |
| 1330 |
compile_data compile_block; |
compile_data compile_block; |
| 1331 |
const real_pcre *re = (const real_pcre *)external_re; |
const real_pcre *re = (const real_pcre *)external_re; |
| 1332 |
|
|
| 1338 |
return NULL; |
return NULL; |
| 1339 |
} |
} |
| 1340 |
|
|
| 1341 |
|
if ((re->flags & PCRE_MODE) == 0) |
| 1342 |
|
{ |
| 1343 |
|
#ifdef COMPILE_PCRE8 |
| 1344 |
|
*errorptr = "argument is compiled in 16 bit mode"; |
| 1345 |
|
#else |
| 1346 |
|
*errorptr = "argument is compiled in 8 bit mode"; |
| 1347 |
|
#endif |
| 1348 |
|
return NULL; |
| 1349 |
|
} |
| 1350 |
|
|
| 1351 |
if ((options & ~PUBLIC_STUDY_OPTIONS) != 0) |
if ((options & ~PUBLIC_STUDY_OPTIONS) != 0) |
| 1352 |
{ |
{ |
| 1353 |
*errorptr = "unknown or incorrect option bit(s) set"; |
*errorptr = "unknown or incorrect option bit(s) set"; |
| 1354 |
return NULL; |
return NULL; |
| 1355 |
} |
} |
| 1356 |
|
|
| 1357 |
code = (uschar *)re + re->name_table_offset + |
code = (pcre_uchar *)re + re->name_table_offset + |
| 1358 |
(re->name_count * re->name_entry_size); |
(re->name_count * re->name_entry_size); |
| 1359 |
|
|
| 1360 |
/* For an anchored pattern, or an unanchored pattern that has a first char, or |
/* For an anchored pattern, or an unanchored pattern that has a first char, or |
| 1369 |
/* Set the character tables in the block that is passed around */ |
/* Set the character tables in the block that is passed around */ |
| 1370 |
|
|
| 1371 |
tables = re->tables; |
tables = re->tables; |
| 1372 |
|
|
| 1373 |
|
#ifdef COMPILE_PCRE8 |
| 1374 |
if (tables == NULL) |
if (tables == NULL) |
| 1375 |
(void)pcre_fullinfo(external_re, NULL, PCRE_INFO_DEFAULT_TABLES, |
(void)pcre_fullinfo(external_re, NULL, PCRE_INFO_DEFAULT_TABLES, |
| 1376 |
(void *)(&tables)); |
(void *)(&tables)); |
| 1377 |
|
#else |
| 1378 |
|
if (tables == NULL) |
| 1379 |
|
(void)pcre16_fullinfo(external_re, NULL, PCRE_INFO_DEFAULT_TABLES, |
| 1380 |
|
(void *)(&tables)); |
| 1381 |
|
#endif |
| 1382 |
|
|
| 1383 |
compile_block.lcc = tables + lcc_offset; |
compile_block.lcc = tables + lcc_offset; |
| 1384 |
compile_block.fcc = tables + fcc_offset; |
compile_block.fcc = tables + fcc_offset; |
| 1387 |
|
|
| 1388 |
/* 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. */ |
| 1389 |
|
|
| 1390 |
memset(start_bits, 0, 32 * sizeof(uschar)); |
memset(start_bits, 0, 32 * sizeof(pcre_uint8)); |
| 1391 |
rc = set_start_bits(code, start_bits, (re->options & PCRE_UTF8) != 0, |
rc = set_start_bits(code, start_bits, (re->options & PCRE_UTF8) != 0, |
| 1392 |
&compile_block); |
&compile_block); |
| 1393 |
bits_set = rc == SSB_DONE; |
bits_set = rc == SSB_DONE; |
| 1394 |
if (rc == SSB_UNKNOWN) *errorptr = "internal error: opcode not recognized"; |
if (rc == SSB_UNKNOWN) |
| 1395 |
|
{ |
| 1396 |
|
*errorptr = "internal error: opcode not recognized"; |
| 1397 |
|
return NULL; |
| 1398 |
|
} |
| 1399 |
} |
} |
| 1400 |
|
|
| 1401 |
/* Find the minimum length of subject string. */ |
/* Find the minimum length of subject string. */ |
| 1402 |
|
|
| 1403 |
switch(min = find_minlength(code, code, re->options, &had_accept, 0)) |
switch(min = find_minlength(code, code, re->options, 0)) |
| 1404 |
{ |
{ |
| 1405 |
case -2: *errorptr = "internal error: missing capturing bracket"; break; |
case -2: *errorptr = "internal error: missing capturing bracket"; return NULL; |
| 1406 |
case -3: *errorptr = "internal error: opcode not recognized"; break; |
case -3: *errorptr = "internal error: opcode not recognized"; return NULL; |
| 1407 |
default: break; |
default: break; |
| 1408 |
} |
} |
| 1409 |
|
|
| 1410 |
/* Return NULL if there's been an error or if no optimization is possible. */ |
/* If a set of starting bytes has been identified, or if the minimum length is |
| 1411 |
|
greater than zero, or if JIT optimization has been requested, get a |
| 1412 |
if (*errorptr != NULL || (!bits_set && min < 0)) return NULL; |
pcre[16]_extra block and a pcre_study_data block. The study data is put in the |
| 1413 |
|
latter, which is pointed to by the former, which may also get additional data |
| 1414 |
|
set later by the calling program. At the moment, the size of pcre_study_data |
| 1415 |
|
is fixed. We nevertheless save it in a field for returning via the |
| 1416 |
|
pcre_fullinfo() function so that if it becomes variable in the future, |
| 1417 |
|
we don't have to change that code. */ |
| 1418 |
|
|
| 1419 |
|
if (bits_set || min > 0 |
| 1420 |
|
#ifdef SUPPORT_JIT |
| 1421 |
|
|| (options & PCRE_STUDY_JIT_COMPILE) != 0 |
| 1422 |
|
#endif |
| 1423 |
|
) |
| 1424 |
|
{ |
| 1425 |
|
extra = (PUBL(extra) *)(PUBL(malloc)) |
| 1426 |
|
(sizeof(PUBL(extra)) + sizeof(pcre_study_data)); |
| 1427 |
|
if (extra == NULL) |
| 1428 |
|
{ |
| 1429 |
|
*errorptr = "failed to get memory"; |
| 1430 |
|
return NULL; |
| 1431 |
|
} |
| 1432 |
|
|
| 1433 |
/* Get a pcre_extra block and a pcre_study_data block. The study data is put in |
study = (pcre_study_data *)((char *)extra + sizeof(PUBL(extra))); |
| 1434 |
the latter, which is pointed to by the former, which may also get additional |
extra->flags = PCRE_EXTRA_STUDY_DATA; |
| 1435 |
data set later by the calling program. At the moment, the size of |
extra->study_data = study; |
| 1436 |
pcre_study_data is fixed. We nevertheless save it in a field for returning via |
|
| 1437 |
the pcre_fullinfo() function so that if it becomes variable in the future, we |
study->size = sizeof(pcre_study_data); |
| 1438 |
don't have to change that code. */ |
study->flags = 0; |
| 1439 |
|
|
| 1440 |
|
/* Set the start bits always, to avoid unset memory errors if the |
| 1441 |
|
study data is written to a file, but set the flag only if any of the bits |
| 1442 |
|
are set, to save time looking when none are. */ |
| 1443 |
|
|
| 1444 |
extra = (pcre_extra *)(pcre_malloc) |
if (bits_set) |
| 1445 |
(sizeof(pcre_extra) + sizeof(pcre_study_data)); |
{ |
| 1446 |
|
study->flags |= PCRE_STUDY_MAPPED; |
| 1447 |
|
memcpy(study->start_bits, start_bits, sizeof(start_bits)); |
| 1448 |
|
} |
| 1449 |
|
else memset(study->start_bits, 0, 32 * sizeof(pcre_uint8)); |
| 1450 |
|
|
| 1451 |
if (extra == NULL) |
#ifdef PCRE_DEBUG |
| 1452 |
{ |
if (bits_set) |
| 1453 |
*errorptr = "failed to get memory"; |
{ |
| 1454 |
return NULL; |
pcre_uint8 *ptr = (pcre_uint32 *)start_bits; |
| 1455 |
} |
int i; |
| 1456 |
|
|
| 1457 |
study = (pcre_study_data *)((char *)extra + sizeof(pcre_extra)); |
printf("Start bits:\n"); |
| 1458 |
extra->flags = PCRE_EXTRA_STUDY_DATA; |
for (i = 0; i < 32; i++) |
| 1459 |
extra->study_data = study; |
printf("%3d: %02x%s", i * 8, *ptr++, ((i + 1) & 0x7) != 0? " " : "\n"); |
| 1460 |
|
} |
| 1461 |
|
#endif |
| 1462 |
|
|
| 1463 |
study->size = sizeof(pcre_study_data); |
/* Always set the minlength value in the block, because the JIT compiler |
| 1464 |
study->flags = 0; |
makes use of it. However, don't set the bit unless the length is greater than |
| 1465 |
|
zero - the interpretive pcre_exec() and pcre_dfa_exec() needn't waste time |
| 1466 |
|
checking the zero case. */ |
| 1467 |
|
|
| 1468 |
if (bits_set) |
if (min > 0) |
| 1469 |
{ |
{ |
| 1470 |
study->flags |= PCRE_STUDY_MAPPED; |
study->flags |= PCRE_STUDY_MINLEN; |
| 1471 |
memcpy(study->start_bits, start_bits, sizeof(start_bits)); |
study->minlength = min; |
| 1472 |
} |
} |
| 1473 |
|
else study->minlength = 0; |
| 1474 |
|
|
| 1475 |
if (min >= 0) |
/* If JIT support was compiled and requested, attempt the JIT compilation. |
| 1476 |
{ |
If no starting bytes were found, and the minimum length is zero, and JIT |
| 1477 |
study->flags |= PCRE_STUDY_MINLEN; |
compilation fails, abandon the extra block and return NULL. */ |
| 1478 |
study->minlength = min; |
|
| 1479 |
|
#ifdef SUPPORT_JIT |
| 1480 |
|
extra->executable_jit = NULL; |
| 1481 |
|
if ((options & PCRE_STUDY_JIT_COMPILE) != 0) PRIV(jit_compile)(re, extra); |
| 1482 |
|
if (study->flags == 0 && (extra->flags & PCRE_EXTRA_EXECUTABLE_JIT) == 0) |
| 1483 |
|
{ |
| 1484 |
|
#ifdef COMPILE_PCRE8 |
| 1485 |
|
pcre_free_study(extra); |
| 1486 |
|
#endif |
| 1487 |
|
#ifdef COMPILE_PCRE16 |
| 1488 |
|
pcre16_free_study(extra); |
| 1489 |
|
#endif |
| 1490 |
|
extra = NULL; |
| 1491 |
|
} |
| 1492 |
|
#endif |
| 1493 |
} |
} |
| 1494 |
|
|
| 1495 |
return extra; |
return extra; |
| 1496 |
} |
} |
| 1497 |
|
|
| 1498 |
|
|
| 1499 |
|
/************************************************* |
| 1500 |
|
* Free the study data * |
| 1501 |
|
*************************************************/ |
| 1502 |
|
|
| 1503 |
|
/* This function frees the memory that was obtained by pcre_study(). |
| 1504 |
|
|
| 1505 |
|
Argument: a pointer to the pcre[16]_extra block |
| 1506 |
|
Returns: nothing |
| 1507 |
|
*/ |
| 1508 |
|
|
| 1509 |
|
#ifdef COMPILE_PCRE8 |
| 1510 |
|
PCRE_EXP_DEFN void |
| 1511 |
|
pcre_free_study(pcre_extra *extra) |
| 1512 |
|
#else |
| 1513 |
|
PCRE_EXP_DEFN void |
| 1514 |
|
pcre16_free_study(pcre16_extra *extra) |
| 1515 |
|
#endif |
| 1516 |
|
{ |
| 1517 |
|
if (extra == NULL) |
| 1518 |
|
return; |
| 1519 |
|
#ifdef SUPPORT_JIT |
| 1520 |
|
if ((extra->flags & PCRE_EXTRA_EXECUTABLE_JIT) != 0 && |
| 1521 |
|
extra->executable_jit != NULL) |
| 1522 |
|
PRIV(jit_free)(extra->executable_jit); |
| 1523 |
|
#endif |
| 1524 |
|
PUBL(free)(extra); |
| 1525 |
|
} |
| 1526 |
|
|
| 1527 |
/* End of pcre_study.c */ |
/* End of pcre_study.c */ |