970N/A/************************************************* 970N/A* Perl-Compatible Regular Expressions * 970N/A*************************************************/ 970N/A/* PCRE is a library of functions to support regular expressions whose syntax 970N/Aand semantics are as close as possible to those of the Perl 5 language. 970N/A Written by Philip Hazel 970N/A Copyright (c) 1997-2012 University of Cambridge 970N/A----------------------------------------------------------------------------- 970N/ARedistribution and use in source and binary forms, with or without 970N/Amodification, are permitted provided that the following conditions are met: 970N/A * Redistributions of source code must retain the above copyright notice, 970N/A this list of conditions and the following disclaimer. 970N/A * Redistributions in binary form must reproduce the above copyright 970N/A notice, this list of conditions and the following disclaimer in the 970N/A documentation and/or other materials provided with the distribution. 970N/A * Neither the name of the University of Cambridge nor the names of its 970N/A contributors may be used to endorse or promote products derived from 970N/A this software without specific prior written permission. 970N/ATHIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 970N/AAND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 970N/AIMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 970N/AARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 970N/ALIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 970N/ACONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 970N/ASUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 970N/AINTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 970N/ACONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 970N/AARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 970N/APOSSIBILITY OF SUCH DAMAGE. 970N/A----------------------------------------------------------------------------- 970N/A/* This module contains the external function pcre_study(), along with local 970N/Asupporting functions. */ 970N/A/* Returns from set_start_bits() */ 970N/A/************************************************* 970N/A* Find the minimum subject length for a group * 970N/A*************************************************/ 970N/A/* Scan a parenthesized group and compute the minimum length of subject that 970N/Ais needed to match it. This is a lower bound; it does not mean there is a 970N/Astring of that length that matches. In UTF8 mode, the result is in characters 970N/A re compiled pattern block 970N/A code pointer to start of group (the bracket) 970N/A startcode pointer to start of the whole pattern's code 970N/A options the compiling options 970N/AReturns: the minimum length 970N/A -1 if \C in UTF-8 mode or (*ACCEPT) was encountered 970N/A -2 internal error (missing capturing bracket) 970N/A -3 internal error (opcode not listed) 970N/A/* PCRE_UTF16 has the same value as PCRE_UTF8. */ 970N/A/* Scan along the opcodes for this branch. If we get to the end of the 970N/Abranch, check the length against that of the other branches. */ 970N/A /* If there is only one branch in a condition, the implied branch has zero 970N/A length, so we don't add anything. This covers the DEFINE "condition" 970N/A /* Otherwise we can fall through and treat it the same as any other 970N/A /* ACCEPT makes things far too complicated; we have to give up. */ 970N/A /* Reached end of a branch; if it's a ket it is the end of a nested 970N/A call. If it's ALT it is an alternation in a nested call. If it is END it's 970N/A the end of the outer call. All can be handled by the same code. If an 970N/A ACCEPT was previously encountered, use the length that was in force at that 970N/A time, and pass back the shortest ACCEPT length. */ 970N/A /* Skip over assertive subpatterns */ 970N/A /* Skip over things that don't match chars */ 970N/A /* Skip over a subpattern that has a {0} or {0,x} quantifier */ 970N/A /* Handle literal characters and + repetitions */ 970N/A /* Handle exact repetitions. The count is already in characters, but we 970N/A need to skip over a multibyte character in UTF8 mode. */ 970N/A /* Handle single-char non-literal matchers */ 970N/A /* "Any newline" might match two characters, but it also might match just 970N/A /* The single-byte matcher means we can't proceed in UTF-8 mode. (In 970N/A non-UTF-8 mode \C will actually be turned into OP_ALLANY, so won't ever 970N/A appear, but leave the code, just in case.) */ 970N/A /* For repeated character types, we have to test for \p and \P, which have 970N/A an extra two bytes of parameters. */ 970N/A /* Check a class for variable quantification */ 970N/A /* The original code caused an unsigned overflow in 64 bit systems, 970N/A so now we use a conditional statement. */ 970N/A /* Backreferences and subroutine calls are treated in the same way: we find 970N/A the minimum length for the subpattern. A recursion, however, causes an 970N/A a flag to be set that causes the length of this branch to be ignored. The 970N/A logic is that a recursion can only make sense if there is another 970N/A alternation that stops the recursing. That will provide the minimum length 970N/A (when no recursion happens). A backreference within the group that it is 970N/A referencing behaves in the same way. 970N/A If PCRE_JAVASCRIPT_COMPAT is set, a backreference to an unset bracket 970N/A matches an empty string (by default it causes a matching failure), so in 970N/A that case we must set the minimum length to zero. */ 970N/A /* Handle repeated back references */ /* We can easily detect direct recursion, but not mutual recursion. This is caught by a recursion depth count. */ /* Anything else does not or need not match a character. We can get the item's length from the table, but for those that can match zero occurrences of a character, we must take special action for UTF-8 characters. As it happens, the "NOT" versions of these opcodes are used at present only for ASCII characters, so they could be omitted from this list. However, in future that may change, so we include them here so as not to leave a gotcha for a future maintainer. */ /* Skip these, but we need to add in the name length. */ /* The remaining opcodes are just skipped over. */ /* This should not occur: we list all opcodes explicitly so that when new ones get added they are properly considered. */ /* Control never gets here */ /************************************************* * Set a bit and maybe its alternate case * *************************************************/ /* Given a character, set its first byte's bit in the table, and also the corresponding bit for the other version of a letter if we are caseless. In UTF-8 mode, for characters greater than 127, we can only do the caseless thing when Unicode property support is available. start_bits points to the bit map p points to the character caseless the caseless flag cd the block with char table pointers utf TRUE for UTF-8 / UTF-16 / UTF-32 mode Returns: pointer after the character #
endif /* Not SUPPORT_UCP */#
else /* Not SUPPORT_UTF */(
void)(
utf);
/* Stops warning for unused parameter *//* Not UTF-8 mode, or character is less than 127. */ #
endif /* COMPILE_PCRE8 */#
else /* Not SUPPORT_UTF */(
void)(
utf);
/* Stops warning for unused parameter *//************************************************* * Set bits for a positive character type * *************************************************/ /* This function sets starting bits for a character type. In UTF-8 mode, we can only do a direct setting for bytes less than 128, as otherwise there can be confusion with bytes in the middle of UTF-8 characters. In a "traditional" environment, the tables will only recognize ASCII characters anyway, but in at least one Windows environment, some higher bytes bits were set in the tables. So we deal with that case by considering the UTF-8 encoding. start_bits the starting bitmap cbit type the type of character wanted table_limit 32 for non-UTF-8; 16 for UTF-8 cd the block with char table pointers for (c =
128; c <
256; c++)
if ((
cd->
cbits[c/
8] & (
1 << (c&
7))) != 0)
/************************************************* * Set bits for a negative character type * *************************************************/ /* This function sets starting bits for a negative character type such as \D. In UTF-8 mode, we can only do a direct setting for bytes less than 128, as otherwise there can be confusion with bytes in the middle of UTF-8 characters. Unlike in the positive case, where we can set appropriate starting bits for specific high-valued UTF-8 characters, in this case we have to set the bits for all high-valued characters. The lowest is 0xc2, but we overkill by starting at 0xc0 (192) for simplicity. start_bits the starting bitmap cbit type the type of character wanted table_limit 32 for non-UTF-8; 16 for UTF-8 cd the block with char table pointers /************************************************* * Create bitmap of starting bytes * *************************************************/ /* This function scans a compiled unanchored expression recursively and attempts to build a bitmap of the set of possible starting bytes. As time goes by, we may be able to get more clever at doing this. The SSB_CONTINUE return is useful for parenthesized groups in patterns such as (a*)b where the group provides some optional starting bytes but scanning must continue at the outer level to find at least one mandatory byte. At the outermost level, this function fails unless the result is SSB_DONE. code points to an expression start_bits points to a 32-byte table, initialized to 0 utf TRUE if in UTF-8 / UTF-16 / UTF-32 mode cd the block with char table pointers Returns: SSB_FAIL => Failed to find any starting bytes SSB_DONE => Found mandatory starting bytes SSB_CONTINUE => Found optional starting bytes SSB_UNKNOWN => Hit an unrecognized opcode /* ========================================================================= */ /* The following comment and code was inserted in January 1999. In May 2006, when it was observed to cause compiler warnings about unused values, I took it out again. If anybody is still using OS/2, they will have to put it back /* This next statement and the later reference to dummy are here in order to trick the optimizer of the IBM C compiler for OS/2 into generating correct code. Apparently IBM isn't going to fix the problem, and we would rather not disable optimization (in this module it actually makes a big difference, and the pcre module can use all the optimization it can get). */ /* ========================================================================= */ while (
try_next)
/* Loop for items in this branch */ /* If we reach something we don't understand, it means a new opcode has been created that hasn't been added to this code. Hopefully this problem will be discovered during testing. */ /* Fail for a valid opcode that implies no starting bits. */ /* A "real" property test implies no starting bits, but the fake property PT_CLIST identifies a list of characters. These lists are short, as they are used for characters with more than one "other case", so there is no point in recognizing them for OP_NOTPROP. */ /* We can ignore word boundary tests. */ /* If we hit a bracket or a positive lookahead assertion, recurse to set bits from within the subpattern. If it can't find anything, we have to give up. If it finds some mandatory character(s), we are done for this branch. Otherwise, carry on scanning after the subpattern. */ /* If we hit ALT or KET, it means we haven't found anything mandatory in this branch, though we might have found something optional. For ALT, we continue with the next alternative, but we have to arrange that the final result from subpattern is SSB_CONTINUE rather than SSB_DONE. For KET, return SSB_CONTINUE: if this is the top level, that indicates failure, but after a nested subpattern, it causes scanning to continue. */ /* Skip over lookbehind and negative lookahead assertions */ /* BRAZERO does the bracket, but carries on. */ /* ========================================================================= See the comment at the head of this function concerning the next line, which was an old fudge for the benefit of OS/2. ========================================================================= */ /* SKIPZERO skips the bracket. */ /* Single-char * or ? sets the bit and tries the next item */ /* Single-char upto sets the bit and tries the next */ /* At least one single char sets the bit and stops */ /* Special spacing and line-terminating items. These recognize specific lists of characters. The difference between VSPACE and ANYNL is that the latter can match the two-character CRLF sequence, but that is not relevant for finding the first character, so their code here is SET_BIT(
0xE1);
/* For U+1680, U+180E */ SET_BIT(
0xE2);
/* For U+2000 - U+200A, U+202F, U+205F */ SET_BIT(
0xFF);
/* For characters > 255 */ #
endif /* COMPILE_PCRE[8|16|32] */ SET_BIT(
0xFF);
/* For characters > 255 */ #
endif /* COMPILE_PCRE[16|32] */ SET_BIT(
0xE2);
/* For U+2028, U+2029 */ SET_BIT(
0xFF);
/* For characters > 255 */ #
endif /* COMPILE_PCRE[8|16|32] */ SET_BIT(
0xFF);
/* For characters > 255 */ /* Single character types set the bits and stop. Note that if PCRE_UCP is set, we do not see these op codes because \d etc are converted to properties. Therefore, these apply in the case when only characters less than 256 are recognized to match the types. */ /* The cbit_space table has vertical tab as whitespace; we no longer have to play fancy tricks because Perl added VT to its whitespace at release 5.18. PCRE added it at release 8.34. */ /* One or more character type fudges the pointer and restarts, knowing it will hit a single character type and stop there. */ /* Zero or more repeats of character types set the bits and then SET_BIT(
0xE1);
/* For U+1680, U+180E */ SET_BIT(
0xE2);
/* For U+2000 - U+200A, U+202F, U+205F */ SET_BIT(
0xFF);
/* For characters > 255 */ #
endif /* COMPILE_PCRE[8|16|32] */ SET_BIT(
0xE2);
/* For U+2028, U+2029 */ SET_BIT(
0xFF);
/* For characters > 255 */ #
endif /* COMPILE_PCRE16 */ /* The cbit_space table has vertical tab as whitespace; we no longer have to play fancy tricks because Perl added VT to its whitespace at release 5.18. PCRE added it at release 8.34. */ /* Character class where all the information is in a bit map: set the bits and either carry on or not, according to the repeat count. If it was a negative class, and we are operating with UTF-8 characters, any byte with a value >= 0xc4 is a potentially valid starter because it starts a character with a value > 255. */ start_bits[
24] |=
0xf0;
/* Bits for 0xc4 - 0xc8 */ SET_BIT(
0xFF);
/* For characters > 255 */ /* In UTF-8 mode, the bits in a bit map correspond to character values, not to byte values. However, the bit map we are constructing is for byte values. So we have to do a conversion for characters whose value is > 127. In fact, there are only two possible starting bytes for characters in the range 128 - 255. */ for (c =
128; c <
256; c++)
if ((
map[c/
8] && (
1 << (c&
7))) != 0)
int d = (c >>
6) |
0xc0;
/* Set bit for this starter */ start_bits[d/
8] |= (
1 << (d&
7));
/* and then skip on to the */ c = (c &
0xc0) +
0x40 -
1;
/* next relevant character. */ /* In non-UTF-8 mode, the two bit maps are completely compatible. */ /* Advance past the bit map, and act on what follows. For a zero minimum repeat, continue; otherwise stop processing. */ break;
/* End of bitmap class handling */ }
/* End of try_next loop *//************************************************* * Study a compiled expression * *************************************************/ /* This function is handed a compiled expression that it must study to produce information that will speed up the matching. It returns a pcre[16]_extra block which then gets handed back to pcre_exec(). re points to the compiled expression options contains option bits errorptr points to where to place error messages; Returns: pointer to a pcre[16]_extra block, with study_data filled in and the appropriate flags set; NULL on error or if no optimization possible *
errorptr =
"argument is not a compiled regular expression";
*
errorptr =
"argument not compiled in 8 bit mode";
*
errorptr =
"argument not compiled in 16 bit mode";
*
errorptr =
"argument not compiled in 32 bit mode";
*
errorptr =
"unknown or incorrect option bit(s) set";
/* For an anchored pattern, or an unanchored pattern that has a first char, or a multiline pattern that matches only at "line starts", there is no point in seeking a list of starting bytes. */ /* Set the character tables in the block that is passed around */ /* See if we can find a fixed set of initial characters for the pattern. */ *
errorptr =
"internal error: opcode not recognized";
/* Find the minimum length of subject string. */ case -
2: *
errorptr =
"internal error: missing capturing bracket";
return NULL;
case -
3: *
errorptr =
"internal error: opcode not recognized";
return NULL;
/* If a set of starting bytes has been identified, or if the minimum length is greater than zero, or if JIT optimization has been requested, or if PCRE_STUDY_EXTRA_NEEDED is set, get a pcre[16]_extra block and a pcre_study_data block. The study data is put in the latter, which is pointed to by the former, which may also get additional data set later by the calling program. At the moment, the size of pcre_study_data is fixed. We nevertheless save it in a field for returning via the pcre_fullinfo() function so that if it becomes variable in the future, we don't have to change that code. */ /* Set the start bits always, to avoid unset memory errors if the study data is written to a file, but set the flag only if any of the bits are set, to save time looking when none are. */ printf(
"%3d: %02x%s", i *
8, *
ptr++, ((i +
1) &
0x7) != 0?
" " :
"\n");
/* Always set the minlength value in the block, because the JIT compiler makes use of it. However, don't set the bit unless the length is greater than zero - the interpretive pcre_exec() and pcre_dfa_exec() needn't waste time checking the zero case. */ /* If JIT support was compiled and requested, attempt the JIT compilation. If no starting bytes were found, and the minimum length is zero, and JIT compilation fails, abandon the extra block and return NULL, unless PCRE_STUDY_EXTRA_NEEDED is set. */ /************************************************* *************************************************/ /* This function frees the memory that was obtained by pcre_study(). Argument: a pointer to the pcre[16]_extra block