1 /*************************************************
2 * Perl-Compatible Regular Expressions *
3 *************************************************/
5 /* PCRE is a library of functions to support regular expressions whose syntax
6 and semantics are as close as possible to those of the Perl 5 language.
8 Written by Philip Hazel
9 Copyright (c) 1997-2012 University of Cambridge
11 -----------------------------------------------------------------------------
12 Redistribution and use in source and binary forms, with or without
13 modification, are permitted provided that the following conditions are met:
15 * Redistributions of source code must retain the above copyright notice,
16 this list of conditions and the following disclaimer.
18 * Redistributions in binary form must reproduce the above copyright
19 notice, this list of conditions and the following disclaimer in the
20 documentation and/or other materials provided with the distribution.
22 * Neither the name of the University of Cambridge nor the names of its
23 contributors may be used to endorse or promote products derived from
24 this software without specific prior written permission.
26 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
27 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
30 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 POSSIBILITY OF SUCH DAMAGE.
37 -----------------------------------------------------------------------------
41 /* This module contains the external function pcre_study(), along with local
42 supporting functions. */
47 #include "pcre_internal.h"
49 #define SET_BIT(c) start_bits[c/8] |= (1 << (c&7))
51 /* Returns from set_start_bits() */
53 enum { SSB_FAIL
, SSB_DONE
, SSB_CONTINUE
, SSB_UNKNOWN
};
57 /*************************************************
58 * Find the minimum subject length for a group *
59 *************************************************/
61 /* Scan a parenthesized group and compute the minimum length of subject that
62 is needed to match it. This is a lower bound; it does not mean there is a
63 string of that length that matches. In UTF8 mode, the result is in characters
67 code pointer to start of group (the bracket)
68 startcode pointer to start of the whole pattern
69 options the compiling options
72 Returns: the minimum length
73 -1 if \C in UTF-8 mode or (*ACCEPT) was encountered
74 -2 internal error (missing capturing bracket)
75 -3 internal error (opcode not listed)
79 find_minlength(const pcre_uchar
*code
, const pcre_uchar
*startcode
, int options
,
83 /* PCRE_UTF16 has the same value as PCRE_UTF8. */
84 BOOL utf
= (options
& PCRE_UTF8
) != 0;
85 BOOL had_recurse
= FALSE
;
87 pcre_uchar
*cc
= (pcre_uchar
*)code
+ 1 + LINK_SIZE
;
89 if (*code
== OP_CBRA
|| *code
== OP_SCBRA
||
90 *code
== OP_CBRAPOS
|| *code
== OP_SCBRAPOS
) cc
+= IMM2_SIZE
;
92 /* Scan along the opcodes for this branch. If we get to the end of the
93 branch, check the length against that of the other branches. */
106 /* If there is only one branch in a condition, the implied branch has zero
107 length, so we don't add anything. This covers the DEFINE "condition"
110 cs
= cc
+ GET(cc
, 1);
113 cc
= cs
+ 1 + LINK_SIZE
;
117 /* Otherwise we can fall through and treat it the same as any other
130 d
= find_minlength(cc
, startcode
, options
, recurse_depth
);
133 do cc
+= GET(cc
, 1); while (*cc
== OP_ALT
);
137 /* ACCEPT makes things far too complicated; we have to give up. */
140 case OP_ASSERT_ACCEPT
:
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. */
155 if (length
< 0 || (!had_recurse
&& branchlength
< length
))
156 length
= branchlength
;
157 if (op
!= OP_ALT
) return length
;
163 /* Skip over assertive subpatterns */
168 case OP_ASSERTBACK_NOT
:
169 do cc
+= GET(cc
, 1); while (*cc
== OP_ALT
);
172 /* Skip over things that don't match chars */
189 case OP_NOT_WORD_BOUNDARY
:
190 case OP_WORD_BOUNDARY
:
191 cc
+= PRIV(OP_lengths
)[*cc
];
194 /* Skip over a subpattern that has a {0} or {0,x} quantifier */
200 cc
+= PRIV(OP_lengths
)[*cc
];
201 do cc
+= GET(cc
, 1); while (*cc
== OP_ALT
);
205 /* Handle literal characters and + repetitions */
226 if (utf
&& HAS_EXTRALEN(cc
[-1])) cc
+= GET_EXTRALEN(cc
[-1]);
234 cc
+= (cc
[1] == OP_PROP
|| cc
[1] == OP_NOTPROP
)? 4 : 2;
237 /* Handle exact repetitions. The count is already in characters, but we
238 need to skip over a multibyte character in UTF8 mode. */
244 branchlength
+= GET2(cc
,1);
247 if (utf
&& HAS_EXTRALEN(cc
[-1])) cc
+= GET_EXTRALEN(cc
[-1]);
252 branchlength
+= GET2(cc
,1);
253 cc
+= 2 + IMM2_SIZE
+ ((cc
[1 + IMM2_SIZE
] == OP_PROP
254 || cc
[1 + IMM2_SIZE
] == OP_NOTPROP
)? 2 : 0);
257 /* Handle single-char non-literal matchers */
266 case OP_NOT_WHITESPACE
:
268 case OP_NOT_WORDCHAR
:
281 /* "Any newline" might match two characters, but it also might match just
289 /* The single-byte matcher means we can't proceed in UTF-8 mode. (In
290 non-UTF-8 mode \C will actually be turned into OP_ALLANY, so won't ever
291 appear, but leave the code, just in case.) */
301 /* For repeated character types, we have to test for \p and \P, which have
302 an extra two bytes of parameters. */
307 case OP_TYPEMINQUERY
:
309 case OP_TYPEPOSQUERY
:
310 if (cc
[1] == OP_PROP
|| cc
[1] == OP_NOTPROP
) cc
+= 2;
311 cc
+= PRIV(OP_lengths
)[op
];
317 if (cc
[1 + IMM2_SIZE
] == OP_PROP
318 || cc
[1 + IMM2_SIZE
] == OP_NOTPROP
) cc
+= 2;
319 cc
+= PRIV(OP_lengths
)[op
];
322 /* Check a class for variable quantification */
324 #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
326 cc
+= GET(cc
, 1) - PRIV(OP_lengths
)[OP_CLASS
];
332 cc
+= PRIV(OP_lengths
)[OP_CLASS
];
350 branchlength
+= GET2(cc
,1);
351 cc
+= 1 + 2 * IMM2_SIZE
;
360 /* Backreferences and subroutine calls are treated in the same way: we find
361 the minimum length for the subpattern. A recursion, however, causes an
362 a flag to be set that causes the length of this branch to be ignored. The
363 logic is that a recursion can only make sense if there is another
364 alternation that stops the recursing. That will provide the minimum length
365 (when no recursion happens). A backreference within the group that it is
366 referencing behaves in the same way.
368 If PCRE_JAVASCRIPT_COMPAT is set, a backreference to an unset bracket
369 matches an empty string (by default it causes a matching failure), so in
370 that case we must set the minimum length to zero. */
374 if ((options
& PCRE_JAVASCRIPT_COMPAT
) == 0)
376 ce
= cs
= (pcre_uchar
*)PRIV(find_bracket
)(startcode
, utf
, GET2(cc
, 1));
377 if (cs
== NULL
) return -2;
378 do ce
+= GET(ce
, 1); while (*ce
== OP_ALT
);
379 if (cc
> cs
&& cc
< ce
)
386 d
= find_minlength(cs
, startcode
, options
, recurse_depth
);
392 /* Handle repeated back references */
413 cc
+= 1 + 2 * IMM2_SIZE
;
421 branchlength
+= min
* d
;
424 /* We can easily detect direct recursion, but not mutual recursion. This is
425 caught by a recursion depth count. */
428 cs
= ce
= (pcre_uchar
*)startcode
+ GET(cc
, 1);
429 do ce
+= GET(ce
, 1); while (*ce
== OP_ALT
);
430 if ((cc
> cs
&& cc
< ce
) || recurse_depth
> 10)
434 branchlength
+= find_minlength(cs
, startcode
, options
, recurse_depth
+ 1);
439 /* Anything else does not or need not match a character. We can get the
440 item's length from the table, but for those that can match zero occurrences
441 of a character, we must take special action for UTF-8 characters. As it
442 happens, the "NOT" versions of these opcodes are used at present only for
443 ASCII characters, so they could be omitted from this list. However, in
444 future that may change, so we include them here so as not to leave a
445 gotcha for a future maintainer. */
480 case OP_NOTMINQUERYI
:
484 case OP_NOTPOSQUERYI
:
486 cc
+= PRIV(OP_lengths
)[op
];
488 if (utf
&& HAS_EXTRALEN(cc
[-1])) cc
+= GET_EXTRALEN(cc
[-1]);
492 /* Skip these, but we need to add in the name length. */
498 cc
+= PRIV(OP_lengths
)[op
] + cc
[1];
501 /* The remaining opcodes are just skipped over. */
510 cc
+= PRIV(OP_lengths
)[op
];
513 /* This should not occur: we list all opcodes explicitly so that when
514 new ones get added they are properly considered. */
520 /* Control never gets here */
525 /*************************************************
526 * Set a bit and maybe its alternate case *
527 *************************************************/
529 /* Given a character, set its first byte's bit in the table, and also the
530 corresponding bit for the other version of a letter if we are caseless. In
531 UTF-8 mode, for characters greater than 127, we can only do the caseless thing
532 when Unicode property support is available.
535 start_bits points to the bit map
536 p points to the character
537 caseless the caseless flag
538 cd the block with char table pointers
539 utf TRUE for UTF-8 / UTF-16 mode
541 Returns: pointer after the character
544 static const pcre_uchar
*
545 set_table_bit(pcre_uint8
*start_bits
, const pcre_uchar
*p
, BOOL caseless
,
546 compile_data
*cd
, BOOL utf
)
561 c
= UCD_OTHERCASE(c
);
562 (void)PRIV(ord2utf
)(c
, buff
);
570 /* Not UTF-8 mode, or character is less than 127. */
572 if (caseless
&& (cd
->ctypes
[c
] & ctype_letter
) != 0) SET_BIT(cd
->fcc
[c
]);
576 #ifdef COMPILE_PCRE16
591 c
= UCD_OTHERCASE(c
);
601 if (caseless
&& (cd
->ctypes
[c
] & ctype_letter
) != 0) SET_BIT(cd
->fcc
[c
]);
608 /*************************************************
609 * Set bits for a positive character type *
610 *************************************************/
612 /* This function sets starting bits for a character type. In UTF-8 mode, we can
613 only do a direct setting for bytes less than 128, as otherwise there can be
614 confusion with bytes in the middle of UTF-8 characters. In a "traditional"
615 environment, the tables will only recognize ASCII characters anyway, but in at
616 least one Windows environment, some higher bytes bits were set in the tables.
617 So we deal with that case by considering the UTF-8 encoding.
620 start_bits the starting bitmap
621 cbit type the type of character wanted
622 table_limit 32 for non-UTF-8; 16 for UTF-8
623 cd the block with char table pointers
629 set_type_bits(pcre_uint8
*start_bits
, int cbit_type
, int table_limit
,
633 for (c
= 0; c
< table_limit
; c
++) start_bits
[c
] |= cd
->cbits
[c
+cbit_type
];
634 #if defined SUPPORT_UTF && defined COMPILE_PCRE8
635 if (table_limit
== 32) return;
636 for (c
= 128; c
< 256; c
++)
638 if ((cd
->cbits
[c
/8] & (1 << (c
&7))) != 0)
641 (void)PRIV(ord2utf
)(c
, buff
);
649 /*************************************************
650 * Set bits for a negative character type *
651 *************************************************/
653 /* This function sets starting bits for a negative character type such as \D.
654 In UTF-8 mode, we can only do a direct setting for bytes less than 128, as
655 otherwise there can be confusion with bytes in the middle of UTF-8 characters.
656 Unlike in the positive case, where we can set appropriate starting bits for
657 specific high-valued UTF-8 characters, in this case we have to set the bits for
658 all high-valued characters. The lowest is 0xc2, but we overkill by starting at
659 0xc0 (192) for simplicity.
662 start_bits the starting bitmap
663 cbit type the type of character wanted
664 table_limit 32 for non-UTF-8; 16 for UTF-8
665 cd the block with char table pointers
671 set_nottype_bits(pcre_uint8
*start_bits
, int cbit_type
, int table_limit
,
675 for (c
= 0; c
< table_limit
; c
++) start_bits
[c
] |= ~cd
->cbits
[c
+cbit_type
];
676 #if defined SUPPORT_UTF && defined COMPILE_PCRE8
677 if (table_limit
!= 32) for (c
= 24; c
< 32; c
++) start_bits
[c
] = 0xff;
683 /*************************************************
684 * Create bitmap of starting bytes *
685 *************************************************/
687 /* This function scans a compiled unanchored expression recursively and
688 attempts to build a bitmap of the set of possible starting bytes. As time goes
689 by, we may be able to get more clever at doing this. The SSB_CONTINUE return is
690 useful for parenthesized groups in patterns such as (a*)b where the group
691 provides some optional starting bytes but scanning must continue at the outer
692 level to find at least one mandatory byte. At the outermost level, this
693 function fails unless the result is SSB_DONE.
696 code points to an expression
697 start_bits points to a 32-byte table, initialized to 0
698 utf TRUE if in UTF-8 / UTF-16 mode
699 cd the block with char table pointers
701 Returns: SSB_FAIL => Failed to find any starting bytes
702 SSB_DONE => Found mandatory starting bytes
703 SSB_CONTINUE => Found optional starting bytes
704 SSB_UNKNOWN => Hit an unrecognized opcode
708 set_start_bits(const pcre_uchar
*code
, pcre_uint8
*start_bits
, BOOL utf
,
712 int yield
= SSB_DONE
;
713 #if defined SUPPORT_UTF && defined COMPILE_PCRE8
714 int table_limit
= utf
? 16:32;
716 int table_limit
= 32;
720 /* ========================================================================= */
721 /* The following comment and code was inserted in January 1999. In May 2006,
722 when it was observed to cause compiler warnings about unused values, I took it
723 out again. If anybody is still using OS/2, they will have to put it back
726 /* This next statement and the later reference to dummy are here in order to
727 trick the optimizer of the IBM C compiler for OS/2 into generating correct
728 code. Apparently IBM isn't going to fix the problem, and we would rather not
729 disable optimization (in this module it actually makes a big difference, and
730 the pcre module can use all the optimization it can get). */
733 /* ========================================================================= */
738 BOOL try_next
= TRUE
;
739 const pcre_uchar
*tcode
= code
+ 1 + LINK_SIZE
;
741 if (*code
== OP_CBRA
|| *code
== OP_SCBRA
||
742 *code
== OP_CBRAPOS
|| *code
== OP_SCBRAPOS
) tcode
+= IMM2_SIZE
;
744 while (try_next
) /* Loop for items in this branch */
750 /* If we reach something we don't understand, it means a new opcode has
751 been created that hasn't been added to this code. Hopefully this problem
752 will be discovered during testing. */
757 /* Fail for a valid opcode that implies no starting bits. */
760 case OP_ASSERT_ACCEPT
:
787 case OP_NOTMINQUERYI
:
797 case OP_NOTPOSQUERYI
:
828 #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
833 /* We can ignore word boundary tests. */
835 case OP_WORD_BOUNDARY
:
836 case OP_NOT_WORD_BOUNDARY
:
840 /* If we hit a bracket or a positive lookahead assertion, recurse to set
841 bits from within the subpattern. If it can't find anything, we have to
842 give up. If it finds some mandatory character(s), we are done for this
843 branch. Otherwise, carry on scanning after the subpattern. */
856 rc
= set_start_bits(tcode
, start_bits
, utf
, cd
);
857 if (rc
== SSB_FAIL
|| rc
== SSB_UNKNOWN
) return rc
;
858 if (rc
== SSB_DONE
) try_next
= FALSE
; else
860 do tcode
+= GET(tcode
, 1); while (*tcode
== OP_ALT
);
861 tcode
+= 1 + LINK_SIZE
;
865 /* If we hit ALT or KET, it means we haven't found anything mandatory in
866 this branch, though we might have found something optional. For ALT, we
867 continue with the next alternative, but we have to arrange that the final
868 result from subpattern is SSB_CONTINUE rather than SSB_DONE. For KET,
869 return SSB_CONTINUE: if this is the top level, that indicates failure,
870 but after a nested subpattern, it causes scanning to continue. */
873 yield
= SSB_CONTINUE
;
883 /* Skip over callout */
886 tcode
+= 2 + 2*LINK_SIZE
;
889 /* Skip over lookbehind and negative lookahead assertions */
893 case OP_ASSERTBACK_NOT
:
894 do tcode
+= GET(tcode
, 1); while (*tcode
== OP_ALT
);
895 tcode
+= 1 + LINK_SIZE
;
898 /* BRAZERO does the bracket, but carries on. */
903 rc
= set_start_bits(++tcode
, start_bits
, utf
, cd
);
904 if (rc
== SSB_FAIL
|| rc
== SSB_UNKNOWN
) return rc
;
905 /* =========================================================================
906 See the comment at the head of this function concerning the next line,
907 which was an old fudge for the benefit of OS/2.
909 ========================================================================= */
910 do tcode
+= GET(tcode
,1); while (*tcode
== OP_ALT
);
911 tcode
+= 1 + LINK_SIZE
;
914 /* SKIPZERO skips the bracket. */
918 do tcode
+= GET(tcode
,1); while (*tcode
== OP_ALT
);
919 tcode
+= 1 + LINK_SIZE
;
922 /* Single-char * or ? sets the bit and tries the next item */
930 tcode
= set_table_bit(start_bits
, tcode
+ 1, FALSE
, cd
, utf
);
939 tcode
= set_table_bit(start_bits
, tcode
+ 1, TRUE
, cd
, utf
);
942 /* Single-char upto sets the bit and tries the next */
947 tcode
= set_table_bit(start_bits
, tcode
+ 1 + IMM2_SIZE
, FALSE
, cd
, utf
);
953 tcode
= set_table_bit(start_bits
, tcode
+ 1 + IMM2_SIZE
, TRUE
, cd
, utf
);
956 /* At least one single char sets the bit and stops */
965 (void)set_table_bit(start_bits
, tcode
+ 1, FALSE
, cd
, utf
);
976 (void)set_table_bit(start_bits
, tcode
+ 1, TRUE
, cd
, utf
);
980 /* Special spacing and line-terminating items. These recognize specific
981 lists of characters. The difference between VSPACE and ANYNL is that the
982 latter can match the two-character CRLF sequence, but that is not
983 relevant for finding the first character, so their code here is
993 SET_BIT(0xC2); /* For U+00A0 */
994 SET_BIT(0xE1); /* For U+1680, U+180E */
995 SET_BIT(0xE2); /* For U+2000 - U+200A, U+202F, U+205F */
996 SET_BIT(0xE3); /* For U+3000 */
998 #ifdef COMPILE_PCRE16
1000 SET_BIT(0xFF); /* For characters > 255 */
1004 #endif /* SUPPORT_UTF */
1007 #ifdef COMPILE_PCRE16
1008 SET_BIT(0xFF); /* For characters > 255 */
1023 #ifdef COMPILE_PCRE8
1024 SET_BIT(0xC2); /* For U+0085 */
1025 SET_BIT(0xE2); /* For U+2028, U+2029 */
1027 #ifdef COMPILE_PCRE16
1029 SET_BIT(0xFF); /* For characters > 255 */
1033 #endif /* SUPPORT_UTF */
1036 #ifdef COMPILE_PCRE16
1037 SET_BIT(0xFF); /* For characters > 255 */
1043 /* Single character types set the bits and stop. Note that if PCRE_UCP
1044 is set, we do not see these op codes because \d etc are converted to
1045 properties. Therefore, these apply in the case when only characters less
1046 than 256 are recognized to match the types. */
1049 set_nottype_bits(start_bits
, cbit_digit
, table_limit
, cd
);
1054 set_type_bits(start_bits
, cbit_digit
, table_limit
, cd
);
1058 /* The cbit_space table has vertical tab as whitespace; we have to
1059 ensure it is set as not whitespace. */
1061 case OP_NOT_WHITESPACE
:
1062 set_nottype_bits(start_bits
, cbit_space
, table_limit
, cd
);
1063 start_bits
[1] |= 0x08;
1067 /* The cbit_space table has vertical tab as whitespace; we have to
1068 not set it from the table. */
1071 c
= start_bits
[1]; /* Save in case it was already set */
1072 set_type_bits(start_bits
, cbit_space
, table_limit
, cd
);
1073 start_bits
[1] = (start_bits
[1] & ~0x08) | c
;
1077 case OP_NOT_WORDCHAR
:
1078 set_nottype_bits(start_bits
, cbit_word
, table_limit
, cd
);
1083 set_type_bits(start_bits
, cbit_word
, table_limit
, cd
);
1087 /* One or more character type fudges the pointer and restarts, knowing
1088 it will hit a single character type and stop there. */
1091 case OP_TYPEMINPLUS
:
1092 case OP_TYPEPOSPLUS
:
1097 tcode
+= 1 + IMM2_SIZE
;
1100 /* Zero or more repeats of character types set the bits and then
1104 case OP_TYPEMINUPTO
:
1105 case OP_TYPEPOSUPTO
:
1106 tcode
+= IMM2_SIZE
; /* Fall through */
1109 case OP_TYPEMINSTAR
:
1110 case OP_TYPEPOSSTAR
:
1112 case OP_TYPEMINQUERY
:
1113 case OP_TYPEPOSQUERY
:
1127 #ifdef COMPILE_PCRE8
1128 SET_BIT(0xC2); /* For U+00A0 */
1129 SET_BIT(0xE1); /* For U+1680, U+180E */
1130 SET_BIT(0xE2); /* For U+2000 - U+200A, U+202F, U+205F */
1131 SET_BIT(0xE3); /* For U+3000 */
1133 #ifdef COMPILE_PCRE16
1135 SET_BIT(0xFF); /* For characters > 255 */
1139 #endif /* SUPPORT_UTF */
1152 #ifdef COMPILE_PCRE8
1153 SET_BIT(0xC2); /* For U+0085 */
1154 SET_BIT(0xE2); /* For U+2028, U+2029 */
1156 #ifdef COMPILE_PCRE16
1158 SET_BIT(0xFF); /* For characters > 255 */
1162 #endif /* SUPPORT_UTF */
1167 set_nottype_bits(start_bits
, cbit_digit
, table_limit
, cd
);
1171 set_type_bits(start_bits
, cbit_digit
, table_limit
, cd
);
1174 /* The cbit_space table has vertical tab as whitespace; we have to
1175 ensure it gets set as not whitespace. */
1177 case OP_NOT_WHITESPACE
:
1178 set_nottype_bits(start_bits
, cbit_space
, table_limit
, cd
);
1179 start_bits
[1] |= 0x08;
1182 /* The cbit_space table has vertical tab as whitespace; we have to
1183 avoid setting it. */
1186 c
= start_bits
[1]; /* Save in case it was already set */
1187 set_type_bits(start_bits
, cbit_space
, table_limit
, cd
);
1188 start_bits
[1] = (start_bits
[1] & ~0x08) | c
;
1191 case OP_NOT_WORDCHAR
:
1192 set_nottype_bits(start_bits
, cbit_word
, table_limit
, cd
);
1196 set_type_bits(start_bits
, cbit_word
, table_limit
, cd
);
1203 /* Character class where all the information is in a bit map: set the
1204 bits and either carry on or not, according to the repeat count. If it was
1205 a negative class, and we are operating with UTF-8 characters, any byte
1206 with a value >= 0xc4 is a potentially valid starter because it starts a
1207 character with a value > 255. */
1210 #if defined SUPPORT_UTF && defined COMPILE_PCRE8
1213 start_bits
[24] |= 0xf0; /* Bits for 0xc4 - 0xc8 */
1214 memset(start_bits
+25, 0xff, 7); /* Bits for 0xc9 - 0xff */
1217 #ifdef COMPILE_PCRE16
1218 SET_BIT(0xFF); /* For characters > 255 */
1226 map
= (pcre_uint8
*)tcode
;
1228 /* In UTF-8 mode, the bits in a bit map correspond to character
1229 values, not to byte values. However, the bit map we are constructing is
1230 for byte values. So we have to do a conversion for characters whose
1231 value is > 127. In fact, there are only two possible starting bytes for
1232 characters in the range 128 - 255. */
1234 #if defined SUPPORT_UTF && defined COMPILE_PCRE8
1237 for (c
= 0; c
< 16; c
++) start_bits
[c
] |= map
[c
];
1238 for (c
= 128; c
< 256; c
++)
1240 if ((map
[c
/8] && (1 << (c
&7))) != 0)
1242 int d
= (c
>> 6) | 0xc0; /* Set bit for this starter */
1243 start_bits
[d
/8] |= (1 << (d
&7)); /* and then skip on to the */
1244 c
= (c
& 0xc0) + 0x40 - 1; /* next relevant character. */
1251 /* In non-UTF-8 mode, the two bit maps are completely compatible. */
1252 for (c
= 0; c
< 32; c
++) start_bits
[c
] |= map
[c
];
1255 /* Advance past the bit map, and act on what follows. For a zero
1256 minimum repeat, continue; otherwise stop processing. */
1258 tcode
+= 32 / sizeof(pcre_uchar
);
1270 if (GET2(tcode
, 1) == 0) tcode
+= 1 + 2 * IMM2_SIZE
;
1271 else try_next
= FALSE
;
1279 break; /* End of bitmap class handling */
1281 } /* End of switch */
1282 } /* End of try_next loop */
1284 code
+= GET(code
, 1); /* Advance to next branch */
1286 while (*code
== OP_ALT
);
1294 /*************************************************
1295 * Study a compiled expression *
1296 *************************************************/
1298 /* This function is handed a compiled expression that it must study to produce
1299 information that will speed up the matching. It returns a pcre[16]_extra block
1300 which then gets handed back to pcre_exec().
1303 re points to the compiled expression
1304 options contains option bits
1305 errorptr points to where to place error messages;
1306 set NULL unless error
1308 Returns: pointer to a pcre[16]_extra block, with study_data filled in and
1309 the appropriate flags set;
1310 NULL on error or if no optimization possible
1313 #ifdef COMPILE_PCRE8
1314 PCRE_EXP_DEFN pcre_extra
* PCRE_CALL_CONVENTION
1315 pcre_study(const pcre
*external_re
, int options
, const char **errorptr
)
1317 PCRE_EXP_DEFN pcre16_extra
* PCRE_CALL_CONVENTION
1318 pcre16_study(const pcre16
*external_re
, int options
, const char **errorptr
)
1322 BOOL bits_set
= FALSE
;
1323 pcre_uint8 start_bits
[32];
1324 PUBL(extra
) *extra
= NULL
;
1325 pcre_study_data
*study
;
1326 const pcre_uint8
*tables
;
1328 compile_data compile_block
;
1329 const REAL_PCRE
*re
= (const REAL_PCRE
*)external_re
;
1333 if (re
== NULL
|| re
->magic_number
!= MAGIC_NUMBER
)
1335 *errorptr
= "argument is not a compiled regular expression";
1339 if ((re
->flags
& PCRE_MODE
) == 0)
1341 #ifdef COMPILE_PCRE8
1342 *errorptr
= "argument is compiled in 16 bit mode";
1344 *errorptr
= "argument is compiled in 8 bit mode";
1349 if ((options
& ~PUBLIC_STUDY_OPTIONS
) != 0)
1351 *errorptr
= "unknown or incorrect option bit(s) set";
1355 code
= (pcre_uchar
*)re
+ re
->name_table_offset
+
1356 (re
->name_count
* re
->name_entry_size
);
1358 /* For an anchored pattern, or an unanchored pattern that has a first char, or
1359 a multiline pattern that matches only at "line starts", there is no point in
1360 seeking a list of starting bytes. */
1362 if ((re
->options
& PCRE_ANCHORED
) == 0 &&
1363 (re
->flags
& (PCRE_FIRSTSET
|PCRE_STARTLINE
)) == 0)
1367 /* Set the character tables in the block that is passed around */
1369 tables
= re
->tables
;
1371 #ifdef COMPILE_PCRE8
1373 (void)pcre_fullinfo(external_re
, NULL
, PCRE_INFO_DEFAULT_TABLES
,
1377 (void)pcre16_fullinfo(external_re
, NULL
, PCRE_INFO_DEFAULT_TABLES
,
1381 compile_block
.lcc
= tables
+ lcc_offset
;
1382 compile_block
.fcc
= tables
+ fcc_offset
;
1383 compile_block
.cbits
= tables
+ cbits_offset
;
1384 compile_block
.ctypes
= tables
+ ctypes_offset
;
1386 /* See if we can find a fixed set of initial characters for the pattern. */
1388 memset(start_bits
, 0, 32 * sizeof(pcre_uint8
));
1389 rc
= set_start_bits(code
, start_bits
, (re
->options
& PCRE_UTF8
) != 0,
1391 bits_set
= rc
== SSB_DONE
;
1392 if (rc
== SSB_UNKNOWN
)
1394 *errorptr
= "internal error: opcode not recognized";
1399 /* Find the minimum length of subject string. */
1401 switch(min
= find_minlength(code
, code
, re
->options
, 0))
1403 case -2: *errorptr
= "internal error: missing capturing bracket"; return NULL
;
1404 case -3: *errorptr
= "internal error: opcode not recognized"; return NULL
;
1408 /* If a set of starting bytes has been identified, or if the minimum length is
1409 greater than zero, or if JIT optimization has been requested, get a
1410 pcre[16]_extra block and a pcre_study_data block. The study data is put in the
1411 latter, which is pointed to by the former, which may also get additional data
1412 set later by the calling program. At the moment, the size of pcre_study_data
1413 is fixed. We nevertheless save it in a field for returning via the
1414 pcre_fullinfo() function so that if it becomes variable in the future,
1415 we don't have to change that code. */
1417 if (bits_set
|| min
> 0
1419 || (options
& (PCRE_STUDY_JIT_COMPILE
| PCRE_STUDY_JIT_PARTIAL_SOFT_COMPILE
1420 | PCRE_STUDY_JIT_PARTIAL_HARD_COMPILE
)) != 0
1424 extra
= (PUBL(extra
) *)(PUBL(malloc
))
1425 (sizeof(PUBL(extra
)) + sizeof(pcre_study_data
));
1428 *errorptr
= "failed to get memory";
1432 study
= (pcre_study_data
*)((char *)extra
+ sizeof(PUBL(extra
)));
1433 extra
->flags
= PCRE_EXTRA_STUDY_DATA
;
1434 extra
->study_data
= study
;
1436 study
->size
= sizeof(pcre_study_data
);
1439 /* Set the start bits always, to avoid unset memory errors if the
1440 study data is written to a file, but set the flag only if any of the bits
1441 are set, to save time looking when none are. */
1445 study
->flags
|= PCRE_STUDY_MAPPED
;
1446 memcpy(study
->start_bits
, start_bits
, sizeof(start_bits
));
1448 else memset(study
->start_bits
, 0, 32 * sizeof(pcre_uint8
));
1453 pcre_uint8
*ptr
= start_bits
;
1456 printf("Start bits:\n");
1457 for (i
= 0; i
< 32; i
++)
1458 printf("%3d: %02x%s", i
* 8, *ptr
++, ((i
+ 1) & 0x7) != 0? " " : "\n");
1462 /* Always set the minlength value in the block, because the JIT compiler
1463 makes use of it. However, don't set the bit unless the length is greater than
1464 zero - the interpretive pcre_exec() and pcre_dfa_exec() needn't waste time
1465 checking the zero case. */
1469 study
->flags
|= PCRE_STUDY_MINLEN
;
1470 study
->minlength
= min
;
1472 else study
->minlength
= 0;
1474 /* If JIT support was compiled and requested, attempt the JIT compilation.
1475 If no starting bytes were found, and the minimum length is zero, and JIT
1476 compilation fails, abandon the extra block and return NULL. */
1479 extra
->executable_jit
= NULL
;
1480 if ((options
& PCRE_STUDY_JIT_COMPILE
) != 0)
1481 PRIV(jit_compile
)(re
, extra
, JIT_COMPILE
);
1482 if ((options
& PCRE_STUDY_JIT_PARTIAL_SOFT_COMPILE
) != 0)
1483 PRIV(jit_compile
)(re
, extra
, JIT_PARTIAL_SOFT_COMPILE
);
1484 if ((options
& PCRE_STUDY_JIT_PARTIAL_HARD_COMPILE
) != 0)
1485 PRIV(jit_compile
)(re
, extra
, JIT_PARTIAL_HARD_COMPILE
);
1487 if (study
->flags
== 0 && (extra
->flags
& PCRE_EXTRA_EXECUTABLE_JIT
) == 0)
1489 #ifdef COMPILE_PCRE8
1490 pcre_free_study(extra
);
1492 #ifdef COMPILE_PCRE16
1493 pcre16_free_study(extra
);
1504 /*************************************************
1505 * Free the study data *
1506 *************************************************/
1508 /* This function frees the memory that was obtained by pcre_study().
1510 Argument: a pointer to the pcre[16]_extra block
1514 #ifdef COMPILE_PCRE8
1516 pcre_free_study(pcre_extra
*extra
)
1519 pcre16_free_study(pcre16_extra
*extra
)
1525 if ((extra
->flags
& PCRE_EXTRA_EXECUTABLE_JIT
) != 0 &&
1526 extra
->executable_jit
!= NULL
)
1527 PRIV(jit_free
)(extra
->executable_jit
);
1532 /* End of pcre_study.c */