Parent Directory
|
Revision Log
|
Patch
revision 1361 by ph10, Fri Sep 6 17:47:32 2013 UTC | revision 1538 by ph10, Sun Mar 29 11:22:24 2015 UTC | |
---|---|---|
# | Line 6 | Line 6 |
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-2013 University of Cambridge | Copyright (c) 1997-2014 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 |
# | Line 47 supporting internal functions that are n | Line 47 supporting internal functions that are n |
47 | #endif | #endif |
48 | ||
49 | #define NLBLOCK cd /* Block containing newline information */ | #define NLBLOCK cd /* Block containing newline information */ |
50 | #define PSSTART start_pattern /* Field containing processed string start */ | #define PSSTART start_pattern /* Field containing pattern start */ |
51 | #define PSEND end_pattern /* Field containing processed string end */ | #define PSEND end_pattern /* Field containing pattern end */ |
52 | ||
53 | #include "pcre_internal.h" | #include "pcre_internal.h" |
54 | ||
# | Line 115 kicks in at the same number of forward r | Line 115 kicks in at the same number of forward r |
115 | #define COMPILE_WORK_SIZE (2048*LINK_SIZE) | #define COMPILE_WORK_SIZE (2048*LINK_SIZE) |
116 | #define COMPILE_WORK_SIZE_MAX (100*COMPILE_WORK_SIZE) | #define COMPILE_WORK_SIZE_MAX (100*COMPILE_WORK_SIZE) |
117 | ||
118 | /* This value determines the size of the initial vector that is used for | /* This value determines the size of the initial vector that is used for |
119 | remembering named groups during the pre-compile. It is allocated on the stack, | remembering named groups during the pre-compile. It is allocated on the stack, |
120 | but if it is too small, it is expanded using malloc(), in a similar way to the | but if it is too small, it is expanded using malloc(), in a similar way to the |
121 | workspace. The value is the number of slots in the list. */ | workspace. The value is the number of slots in the list. */ |
122 | ||
123 | #define NAMED_GROUP_LIST_SIZE 20 | #define NAMED_GROUP_LIST_SIZE 20 |
# | Line 260 static const verbitem verbs[] = { | Line 260 static const verbitem verbs[] = { |
260 | static const int verbcount = sizeof(verbs)/sizeof(verbitem); | static const int verbcount = sizeof(verbs)/sizeof(verbitem); |
261 | ||
262 | ||
263 | /* Substitutes for [[:<:]] and [[:>:]], which mean start and end of word in | |
264 | another regex library. */ | |
265 | ||
266 | static const pcre_uchar sub_start_of_word[] = { | |
267 | CHAR_BACKSLASH, CHAR_b, CHAR_LEFT_PARENTHESIS, CHAR_QUESTION_MARK, | |
268 | CHAR_EQUALS_SIGN, CHAR_BACKSLASH, CHAR_w, CHAR_RIGHT_PARENTHESIS, '\0' }; | |
269 | ||
270 | static const pcre_uchar sub_end_of_word[] = { | |
271 | CHAR_BACKSLASH, CHAR_b, CHAR_LEFT_PARENTHESIS, CHAR_QUESTION_MARK, | |
272 | CHAR_LESS_THAN_SIGN, CHAR_EQUALS_SIGN, CHAR_BACKSLASH, CHAR_w, | |
273 | CHAR_RIGHT_PARENTHESIS, '\0' }; | |
274 | ||
275 | ||
276 | /* Tables of names of POSIX character classes and their lengths. The names are | /* Tables of names of POSIX character classes and their lengths. The names are |
277 | now all in a single string, to reduce the number of relocations when a shared | now all in a single string, to reduce the number of relocations when a shared |
278 | library is dynamically loaded. The list of lengths is terminated by a zero | library is dynamically loaded. The list of lengths is terminated by a zero |
279 | length entry. The first three must be alpha, lower, upper, as this is assumed | length entry. The first three must be alpha, lower, upper, as this is assumed |
280 | for handling case independence. */ | for handling case independence. The indices for graph, print, and punct are |
281 | needed, so identify them. */ | |
282 | ||
283 | static const char posix_names[] = | static const char posix_names[] = |
284 | STRING_alpha0 STRING_lower0 STRING_upper0 STRING_alnum0 | STRING_alpha0 STRING_lower0 STRING_upper0 STRING_alnum0 |
# | Line 275 static const char posix_names[] = | Line 289 static const char posix_names[] = |
289 | static const pcre_uint8 posix_name_lengths[] = { | static const pcre_uint8 posix_name_lengths[] = { |
290 | 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 4, 6, 0 }; | 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 4, 6, 0 }; |
291 | ||
292 | #define PC_GRAPH 8 | |
293 | #define PC_PRINT 9 | |
294 | #define PC_PUNCT 10 | |
295 | ||
296 | ||
297 | /* Table of class bit maps for each POSIX class. Each class is formed from a | /* Table of class bit maps for each POSIX class. Each class is formed from a |
298 | base map, with an optional addition or removal of another map. Then, for some | base map, with an optional addition or removal of another map. Then, for some |
299 | classes, there is some additional tweaking: for [:blank:] the vertical space | classes, there is some additional tweaking: for [:blank:] the vertical space |
# | Line 302 static const int posix_class_maps[] = { | Line 321 static const int posix_class_maps[] = { |
321 | cbit_xdigit,-1, 0 /* xdigit */ | cbit_xdigit,-1, 0 /* xdigit */ |
322 | }; | }; |
323 | ||
324 | /* Table of substitutes for \d etc when PCRE_UCP is set. The POSIX class | /* Table of substitutes for \d etc when PCRE_UCP is set. They are replaced by |
325 | substitutes must be in the order of the names, defined above, and there are | Unicode property escapes. */ |
both positive and negative cases. NULL means no substitute. */ | ||
326 | ||
327 | #ifdef SUPPORT_UCP | #ifdef SUPPORT_UCP |
328 | static const pcre_uchar string_PNd[] = { | static const pcre_uchar string_PNd[] = { |
# | Line 329 static const pcre_uchar string_pXwd[] = | Line 347 static const pcre_uchar string_pXwd[] = |
347 | static const pcre_uchar *substitutes[] = { | static const pcre_uchar *substitutes[] = { |
348 | string_PNd, /* \D */ | string_PNd, /* \D */ |
349 | string_pNd, /* \d */ | string_pNd, /* \d */ |
350 | string_PXsp, /* \S */ /* NOTE: Xsp is Perl space */ | string_PXsp, /* \S */ /* Xsp is Perl space, but from 8.34, Perl */ |
351 | string_pXsp, /* \s */ | string_pXsp, /* \s */ /* space and POSIX space are the same. */ |
352 | string_PXwd, /* \W */ | string_PXwd, /* \W */ |
353 | string_pXwd /* \w */ | string_pXwd /* \w */ |
354 | }; | }; |
355 | ||
356 | /* The POSIX class substitutes must be in the order of the POSIX class names, | |
357 | defined above, and there are both positive and negative cases. NULL means no | |
358 | general substitute of a Unicode property escape (\p or \P). However, for some | |
359 | POSIX classes (e.g. graph, print, punct) a special property code is compiled | |
360 | directly. */ | |
361 | ||
362 | static const pcre_uchar string_pL[] = { | static const pcre_uchar string_pL[] = { |
363 | CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET, | CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET, |
364 | CHAR_L, CHAR_RIGHT_CURLY_BRACKET, '\0' }; | CHAR_L, CHAR_RIGHT_CURLY_BRACKET, '\0' }; |
# | Line 382 static const pcre_uchar *posix_substitut | Line 406 static const pcre_uchar *posix_substitut |
406 | NULL, /* graph */ | NULL, /* graph */ |
407 | NULL, /* print */ | NULL, /* print */ |
408 | NULL, /* punct */ | NULL, /* punct */ |
409 | string_pXps, /* space */ /* NOTE: Xps is POSIX space */ | string_pXps, /* space */ /* Xps is POSIX space, but from 8.34 */ |
410 | string_pXwd, /* word */ | string_pXwd, /* word */ /* Perl and POSIX space are the same */ |
411 | NULL, /* xdigit */ | NULL, /* xdigit */ |
412 | /* Negated cases */ | /* Negated cases */ |
413 | string_PL, /* ^alpha */ | string_PL, /* ^alpha */ |
# | Line 397 static const pcre_uchar *posix_substitut | Line 421 static const pcre_uchar *posix_substitut |
421 | NULL, /* ^graph */ | NULL, /* ^graph */ |
422 | NULL, /* ^print */ | NULL, /* ^print */ |
423 | NULL, /* ^punct */ | NULL, /* ^punct */ |
424 | string_PXps, /* ^space */ /* NOTE: Xps is POSIX space */ | string_PXps, /* ^space */ /* Xps is POSIX space, but from 8.34 */ |
425 | string_PXwd, /* ^word */ | string_PXwd, /* ^word */ /* Perl and POSIX space are the same */ |
426 | NULL /* ^xdigit */ | NULL /* ^xdigit */ |
427 | }; | }; |
428 | #define POSIX_SUBSIZE (sizeof(posix_substitutes) / sizeof(pcre_uchar *)) | #define POSIX_SUBSIZE (sizeof(posix_substitutes) / sizeof(pcre_uchar *)) |
# | Line 462 static const char error_texts[] = | Line 486 static const char error_texts[] = |
486 | "POSIX collating elements are not supported\0" | "POSIX collating elements are not supported\0" |
487 | "this version of PCRE is compiled without UTF support\0" | "this version of PCRE is compiled without UTF support\0" |
488 | "spare error\0" /** DEAD **/ | "spare error\0" /** DEAD **/ |
489 | "character value in \\x{...} sequence is too large\0" | "character value in \\x{} or \\o{} is too large\0" |
490 | /* 35 */ | /* 35 */ |
491 | "invalid condition (?(0)\0" | "invalid condition (?(0)\0" |
492 | "\\C not allowed in lookbehind assertion\0" | "\\C not allowed in lookbehind assertion\0" |
# | Line 516 static const char error_texts[] = | Line 540 static const char error_texts[] = |
540 | "character value in \\u.... sequence is too large\0" | "character value in \\u.... sequence is too large\0" |
541 | "invalid UTF-32 string\0" | "invalid UTF-32 string\0" |
542 | "setting UTF is disabled by the application\0" | "setting UTF is disabled by the application\0" |
543 | "non-hex character in \\x{} (closing brace missing?)\0" | |
544 | /* 80 */ | |
545 | "non-octal character in \\o{} (closing brace missing?)\0" | |
546 | "missing opening brace after \\o\0" | |
547 | "parentheses are too deeply nested\0" | |
548 | "invalid range in character class\0" | |
549 | "group name must start with a non-digit\0" | |
550 | /* 85 */ | |
551 | "parentheses are too deeply nested (stack check)\0" | |
552 | "digits missing in \\x{} or \\o{}\0" | |
553 | ; | ; |
554 | ||
555 | /* Table to identify digits and hex digits. This is used when compiling | /* Table to identify digits and hex digits. This is used when compiling |
# | Line 655 static const pcre_uint8 ebcdic_chartab[] | Line 689 static const pcre_uint8 ebcdic_chartab[] |
689 | #endif | #endif |
690 | ||
691 | ||
692 | /* This table is used to check whether auto-possessification is possible | |
693 | between adjacent character-type opcodes. The left-hand (repeated) opcode is | |
694 | used to select the row, and the right-hand opcode is use to select the column. | |
695 | A value of 1 means that auto-possessification is OK. For example, the second | |
696 | value in the first row means that \D+\d can be turned into \D++\d. | |
697 | ||
698 | The Unicode property types (\P and \p) have to be present to fill out the table | |
699 | because of what their opcode values are, but the table values should always be | |
700 | zero because property types are handled separately in the code. The last four | |
701 | columns apply to items that cannot be repeated, so there is no need to have | |
702 | rows for them. Note that OP_DIGIT etc. are generated only when PCRE_UCP is | |
703 | *not* set. When it is set, \d etc. are converted into OP_(NOT_)PROP codes. */ | |
704 | ||
705 | #define APTROWS (LAST_AUTOTAB_LEFT_OP - FIRST_AUTOTAB_OP + 1) | |
706 | #define APTCOLS (LAST_AUTOTAB_RIGHT_OP - FIRST_AUTOTAB_OP + 1) | |
707 | ||
708 | static const pcre_uint8 autoposstab[APTROWS][APTCOLS] = { | |
709 | /* \D \d \S \s \W \w . .+ \C \P \p \R \H \h \V \v \X \Z \z $ $M */ | |
710 | { 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 }, /* \D */ | |
711 | { 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1 }, /* \d */ | |
712 | { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1 }, /* \S */ | |
713 | { 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 }, /* \s */ | |
714 | { 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 }, /* \W */ | |
715 | { 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1 }, /* \w */ | |
716 | { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0 }, /* . */ | |
717 | { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 }, /* .+ */ | |
718 | { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 }, /* \C */ | |
719 | { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, /* \P */ | |
720 | { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, /* \p */ | |
721 | { 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0 }, /* \R */ | |
722 | { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0 }, /* \H */ | |
723 | { 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0 }, /* \h */ | |
724 | { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0 }, /* \V */ | |
725 | { 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0 }, /* \v */ | |
726 | { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 } /* \X */ | |
727 | }; | |
728 | ||
729 | ||
730 | /* This table is used to check whether auto-possessification is possible | |
731 | between adjacent Unicode property opcodes (OP_PROP and OP_NOTPROP). The | |
732 | left-hand (repeated) opcode is used to select the row, and the right-hand | |
733 | opcode is used to select the column. The values are as follows: | |
734 | ||
735 | 0 Always return FALSE (never auto-possessify) | |
736 | 1 Character groups are distinct (possessify if both are OP_PROP) | |
737 | 2 Check character categories in the same group (general or particular) | |
738 | 3 TRUE if the two opcodes are not the same (PROP vs NOTPROP) | |
739 | ||
740 | 4 Check left general category vs right particular category | |
741 | 5 Check right general category vs left particular category | |
742 | ||
743 | 6 Left alphanum vs right general category | |
744 | 7 Left space vs right general category | |
745 | 8 Left word vs right general category | |
746 | ||
747 | 9 Right alphanum vs left general category | |
748 | 10 Right space vs left general category | |
749 | 11 Right word vs left general category | |
750 | ||
751 | 12 Left alphanum vs right particular category | |
752 | 13 Left space vs right particular category | |
753 | 14 Left word vs right particular category | |
754 | ||
755 | 15 Right alphanum vs left particular category | |
756 | 16 Right space vs left particular category | |
757 | 17 Right word vs left particular category | |
758 | */ | |
759 | ||
760 | static const pcre_uint8 propposstab[PT_TABSIZE][PT_TABSIZE] = { | |
761 | /* ANY LAMP GC PC SC ALNUM SPACE PXSPACE WORD CLIST UCNC */ | |
762 | { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, /* PT_ANY */ | |
763 | { 0, 3, 0, 0, 0, 3, 1, 1, 0, 0, 0 }, /* PT_LAMP */ | |
764 | { 0, 0, 2, 4, 0, 9, 10, 10, 11, 0, 0 }, /* PT_GC */ | |
765 | { 0, 0, 5, 2, 0, 15, 16, 16, 17, 0, 0 }, /* PT_PC */ | |
766 | { 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0 }, /* PT_SC */ | |
767 | { 0, 3, 6, 12, 0, 3, 1, 1, 0, 0, 0 }, /* PT_ALNUM */ | |
768 | { 0, 1, 7, 13, 0, 1, 3, 3, 1, 0, 0 }, /* PT_SPACE */ | |
769 | { 0, 1, 7, 13, 0, 1, 3, 3, 1, 0, 0 }, /* PT_PXSPACE */ | |
770 | { 0, 0, 8, 14, 0, 0, 1, 1, 3, 0, 0 }, /* PT_WORD */ | |
771 | { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, /* PT_CLIST */ | |
772 | { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3 } /* PT_UCNC */ | |
773 | }; | |
774 | ||
775 | /* This table is used to check whether auto-possessification is possible | |
776 | between adjacent Unicode property opcodes (OP_PROP and OP_NOTPROP) when one | |
777 | specifies a general category and the other specifies a particular category. The | |
778 | row is selected by the general category and the column by the particular | |
779 | category. The value is 1 if the particular category is not part of the general | |
780 | category. */ | |
781 | ||
782 | static const pcre_uint8 catposstab[7][30] = { | |
783 | /* Cc Cf Cn Co Cs Ll Lm Lo Lt Lu Mc Me Mn Nd Nl No Pc Pd Pe Pf Pi Po Ps Sc Sk Sm So Zl Zp Zs */ | |
784 | { 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, /* C */ | |
785 | { 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, /* L */ | |
786 | { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, /* M */ | |
787 | { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, /* N */ | |
788 | { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1 }, /* P */ | |
789 | { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1 }, /* S */ | |
790 | { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0 } /* Z */ | |
791 | }; | |
792 | ||
793 | /* This table is used when checking ALNUM, (PX)SPACE, SPACE, and WORD against | |
794 | a general or particular category. The properties in each row are those | |
795 | that apply to the character set in question. Duplication means that a little | |
796 | unnecessary work is done when checking, but this keeps things much simpler | |
797 | because they can all use the same code. For more details see the comment where | |
798 | this table is used. | |
799 | ||
800 | Note: SPACE and PXSPACE used to be different because Perl excluded VT from | |
801 | "space", but from Perl 5.18 it's included, so both categories are treated the | |
802 | same here. */ | |
803 | ||
804 | static const pcre_uint8 posspropstab[3][4] = { | |
805 | { ucp_L, ucp_N, ucp_N, ucp_Nl }, /* ALNUM, 3rd and 4th values redundant */ | |
806 | { ucp_Z, ucp_Z, ucp_C, ucp_Cc }, /* SPACE and PXSPACE, 2nd value redundant */ | |
807 | { ucp_L, ucp_N, ucp_P, ucp_Po } /* WORD */ | |
808 | }; | |
809 | ||
810 | /* This table is used when converting repeating opcodes into possessified | |
811 | versions as a result of an explicit possessive quantifier such as ++. A zero | |
812 | value means there is no possessified version - in those cases the item in | |
813 | question must be wrapped in ONCE brackets. The table is truncated at OP_CALLOUT | |
814 | because all relevant opcodes are less than that. */ | |
815 | ||
816 | static const pcre_uint8 opcode_possessify[] = { | |
817 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0 - 15 */ | |
818 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 16 - 31 */ | |
819 | ||
820 | 0, /* NOTI */ | |
821 | OP_POSSTAR, 0, /* STAR, MINSTAR */ | |
822 | OP_POSPLUS, 0, /* PLUS, MINPLUS */ | |
823 | OP_POSQUERY, 0, /* QUERY, MINQUERY */ | |
824 | OP_POSUPTO, 0, /* UPTO, MINUPTO */ | |
825 | 0, /* EXACT */ | |
826 | 0, 0, 0, 0, /* POS{STAR,PLUS,QUERY,UPTO} */ | |
827 | ||
828 | OP_POSSTARI, 0, /* STARI, MINSTARI */ | |
829 | OP_POSPLUSI, 0, /* PLUSI, MINPLUSI */ | |
830 | OP_POSQUERYI, 0, /* QUERYI, MINQUERYI */ | |
831 | OP_POSUPTOI, 0, /* UPTOI, MINUPTOI */ | |
832 | 0, /* EXACTI */ | |
833 | 0, 0, 0, 0, /* POS{STARI,PLUSI,QUERYI,UPTOI} */ | |
834 | ||
835 | OP_NOTPOSSTAR, 0, /* NOTSTAR, NOTMINSTAR */ | |
836 | OP_NOTPOSPLUS, 0, /* NOTPLUS, NOTMINPLUS */ | |
837 | OP_NOTPOSQUERY, 0, /* NOTQUERY, NOTMINQUERY */ | |
838 | OP_NOTPOSUPTO, 0, /* NOTUPTO, NOTMINUPTO */ | |
839 | 0, /* NOTEXACT */ | |
840 | 0, 0, 0, 0, /* NOTPOS{STAR,PLUS,QUERY,UPTO} */ | |
841 | ||
842 | OP_NOTPOSSTARI, 0, /* NOTSTARI, NOTMINSTARI */ | |
843 | OP_NOTPOSPLUSI, 0, /* NOTPLUSI, NOTMINPLUSI */ | |
844 | OP_NOTPOSQUERYI, 0, /* NOTQUERYI, NOTMINQUERYI */ | |
845 | OP_NOTPOSUPTOI, 0, /* NOTUPTOI, NOTMINUPTOI */ | |
846 | 0, /* NOTEXACTI */ | |
847 | 0, 0, 0, 0, /* NOTPOS{STARI,PLUSI,QUERYI,UPTOI} */ | |
848 | ||
849 | OP_TYPEPOSSTAR, 0, /* TYPESTAR, TYPEMINSTAR */ | |
850 | OP_TYPEPOSPLUS, 0, /* TYPEPLUS, TYPEMINPLUS */ | |
851 | OP_TYPEPOSQUERY, 0, /* TYPEQUERY, TYPEMINQUERY */ | |
852 | OP_TYPEPOSUPTO, 0, /* TYPEUPTO, TYPEMINUPTO */ | |
853 | 0, /* TYPEEXACT */ | |
854 | 0, 0, 0, 0, /* TYPEPOS{STAR,PLUS,QUERY,UPTO} */ | |
855 | ||
856 | OP_CRPOSSTAR, 0, /* CRSTAR, CRMINSTAR */ | |
857 | OP_CRPOSPLUS, 0, /* CRPLUS, CRMINPLUS */ | |
858 | OP_CRPOSQUERY, 0, /* CRQUERY, CRMINQUERY */ | |
859 | OP_CRPOSRANGE, 0, /* CRRANGE, CRMINRANGE */ | |
860 | 0, 0, 0, 0, /* CRPOS{STAR,PLUS,QUERY,RANGE} */ | |
861 | ||
862 | 0, 0, 0, /* CLASS, NCLASS, XCLASS */ | |
863 | 0, 0, /* REF, REFI */ | |
864 | 0, 0, /* DNREF, DNREFI */ | |
865 | 0, 0 /* RECURSE, CALLOUT */ | |
866 | }; | |
867 | ||
868 | ||
869 | ||
870 | /************************************************* | /************************************************* |
871 | * Find an error text * | * Find an error text * |
# | Line 682 return s; | Line 893 return s; |
893 | } | } |
894 | ||
895 | ||
896 | ||
897 | /************************************************* | /************************************************* |
898 | * Expand the workspace * | * Expand the workspace * |
899 | *************************************************/ | *************************************************/ |
# | Line 759 return (*p == CHAR_RIGHT_CURLY_BRACKET); | Line 971 return (*p == CHAR_RIGHT_CURLY_BRACKET); |
971 | *************************************************/ | *************************************************/ |
972 | ||
973 | /* This function is called when a \ has been encountered. It either returns a | /* This function is called when a \ has been encountered. It either returns a |
974 | positive value for a simple escape such as \n, or 0 for a data character | positive value for a simple escape such as \n, or 0 for a data character which |
975 | which will be placed in chptr. A backreference to group n is returned as | will be placed in chptr. A backreference to group n is returned as negative n. |
976 | negative n. When UTF-8 is enabled, a positive value greater than 255 may | When UTF-8 is enabled, a positive value greater than 255 may be returned in |
977 | be returned in chptr. | chptr. On entry, ptr is pointing at the \. On exit, it is on the final |
978 | On entry,ptr is pointing at the \. On exit, it is on the final character of the | character of the escape sequence. |
escape sequence. | ||
979 | ||
980 | Arguments: | Arguments: |
981 | ptrptr points to the pattern position pointer | ptrptr points to the pattern position pointer |
982 | chptr points to the data character | chptr points to a returned data character |
983 | errorcodeptr points to the errorcode variable | errorcodeptr points to the errorcode variable |
984 | bracount number of previous extracting brackets | bracount number of previous extracting brackets |
985 | options the options bits | options the options bits |
# | Line 972 else | Line 1183 else |
1183 | break; | break; |
1184 | ||
1185 | /* The handling of escape sequences consisting of a string of digits | /* The handling of escape sequences consisting of a string of digits |
1186 | starting with one that is not zero is not straightforward. By experiment, | starting with one that is not zero is not straightforward. Perl has changed |
1187 | the way Perl works seems to be as follows: | over the years. Nowadays \g{} for backreferences and \o{} for octal are |
1188 | recommended to avoid the ambiguities in the old syntax. | |
1189 | ||
1190 | Outside a character class, the digits are read as a decimal number. If the | Outside a character class, the digits are read as a decimal number. If the |
1191 | number is less than 10, or if there are that many previous extracting | number is less than 8 (used to be 10), or if there are that many previous |
1192 | left brackets, then it is a back reference. Otherwise, up to three octal | extracting left brackets, then it is a back reference. Otherwise, up to |
1193 | digits are read to form an escaped byte. Thus \123 is likely to be octal | three octal digits are read to form an escaped byte. Thus \123 is likely to |
1194 | 123 (cf \0123, which is octal 012 followed by the literal 3). If the octal | be octal 123 (cf \0123, which is octal 012 followed by the literal 3). If |
1195 | value is greater than 377, the least significant 8 bits are taken. Inside a | the octal value is greater than 377, the least significant 8 bits are |
1196 | character class, \ followed by a digit is always an octal number. */ | taken. \8 and \9 are treated as the literal characters 8 and 9. |
1197 | ||
1198 | Inside a character class, \ followed by a digit is always either a literal | |
1199 | 8 or 9 or an octal number. */ | |
1200 | ||
1201 | case CHAR_1: case CHAR_2: case CHAR_3: case CHAR_4: case CHAR_5: | case CHAR_1: case CHAR_2: case CHAR_3: case CHAR_4: case CHAR_5: |
1202 | case CHAR_6: case CHAR_7: case CHAR_8: case CHAR_9: | case CHAR_6: case CHAR_7: case CHAR_8: case CHAR_9: |
# | Line 1008 else | Line 1223 else |
1223 | *errorcodeptr = ERR61; | *errorcodeptr = ERR61; |
1224 | break; | break; |
1225 | } | } |
1226 | if (s < 10 || s <= bracount) | if (s < 8 || s <= bracount) /* Check for back reference */ |
1227 | { | { |
1228 | escape = -s; | escape = -s; |
1229 | break; | break; |
# | Line 1016 else | Line 1231 else |
1231 | ptr = oldptr; /* Put the pointer back and fall through */ | ptr = oldptr; /* Put the pointer back and fall through */ |
1232 | } | } |
1233 | ||
1234 | /* Handle an octal number following \. If the first digit is 8 or 9, Perl | /* Handle a digit following \ when the number is not a back reference. If |
1235 | generates a binary zero byte and treats the digit as a following literal. | the first digit is 8 or 9, Perl used to generate a binary zero byte and |
1236 | Thus we have to pull back the pointer by one. */ | then treat the digit as a following literal. At least by Perl 5.18 this |
1237 | changed so as not to insert the binary zero. */ | |
1238 | ||
1239 | if ((c = *ptr) >= CHAR_8) | if ((c = *ptr) >= CHAR_8) break; |
1240 | { | |
1241 | ptr--; | /* Fall through with a digit less than 8 */ |
c = 0; | ||
break; | ||
} | ||
1242 | ||
1243 | /* \0 always starts an octal number, but we may drop through to here with a | /* \0 always starts an octal number, but we may drop through to here with a |
1244 | larger first octal digit. The original code used just to take the least | larger first octal digit. The original code used just to take the least |
# | Line 1042 else | Line 1255 else |
1255 | #endif | #endif |
1256 | break; | break; |
1257 | ||
1258 | /* \x is complicated. \x{ddd} is a character number which can be greater | /* \o is a relatively new Perl feature, supporting a more general way of |
1259 | than 0xff in utf or non-8bit mode, but only if the ddd are hex digits. | specifying character codes in octal. The only supported form is \o{ddd}. */ |
1260 | If not, { is treated as a data character. */ | |
1261 | case CHAR_o: | |
1262 | if (ptr[1] != CHAR_LEFT_CURLY_BRACKET) *errorcodeptr = ERR81; else | |
1263 | if (ptr[2] == CHAR_RIGHT_CURLY_BRACKET) *errorcodeptr = ERR86; else | |
1264 | { | |
1265 | ptr += 2; | |
1266 | c = 0; | |
1267 | overflow = FALSE; | |
1268 | while (*ptr >= CHAR_0 && *ptr <= CHAR_7) | |
1269 | { | |
1270 | register pcre_uint32 cc = *ptr++; | |
1271 | if (c == 0 && cc == CHAR_0) continue; /* Leading zeroes */ | |
1272 | #ifdef COMPILE_PCRE32 | |
1273 | if (c >= 0x20000000l) { overflow = TRUE; break; } | |
1274 | #endif | |
1275 | c = (c << 3) + cc - CHAR_0 ; | |
1276 | #if defined COMPILE_PCRE8 | |
1277 | if (c > (utf ? 0x10ffffU : 0xffU)) { overflow = TRUE; break; } | |
1278 | #elif defined COMPILE_PCRE16 | |
1279 | if (c > (utf ? 0x10ffffU : 0xffffU)) { overflow = TRUE; break; } | |
1280 | #elif defined COMPILE_PCRE32 | |
1281 | if (utf && c > 0x10ffffU) { overflow = TRUE; break; } | |
1282 | #endif | |
1283 | } | |
1284 | if (overflow) | |
1285 | { | |
1286 | while (*ptr >= CHAR_0 && *ptr <= CHAR_7) ptr++; | |
1287 | *errorcodeptr = ERR34; | |
1288 | } | |
1289 | else if (*ptr == CHAR_RIGHT_CURLY_BRACKET) | |
1290 | { | |
1291 | if (utf && c >= 0xd800 && c <= 0xdfff) *errorcodeptr = ERR73; | |
1292 | } | |
1293 | else *errorcodeptr = ERR80; | |
1294 | } | |
1295 | break; | |
1296 | ||
1297 | /* \x is complicated. In JavaScript, \x must be followed by two hexadecimal | |
1298 | numbers. Otherwise it is a lowercase x letter. */ | |
1299 | ||
1300 | case CHAR_x: | case CHAR_x: |
1301 | if ((options & PCRE_JAVASCRIPT_COMPAT) != 0) | if ((options & PCRE_JAVASCRIPT_COMPAT) != 0) |
1302 | { | { |
/* In JavaScript, \x must be followed by two hexadecimal numbers. | ||
Otherwise it is a lowercase x letter. */ | ||
1303 | if (MAX_255(ptr[1]) && (digitab[ptr[1]] & ctype_xdigit) != 0 | if (MAX_255(ptr[1]) && (digitab[ptr[1]] & ctype_xdigit) != 0 |
1304 | && MAX_255(ptr[2]) && (digitab[ptr[2]] & ctype_xdigit) != 0) | && MAX_255(ptr[2]) && (digitab[ptr[2]] & ctype_xdigit) != 0) |
1305 | { | { |
# | Line 1067 else | Line 1316 else |
1316 | #endif | #endif |
1317 | } | } |
1318 | } | } |
1319 | break; | } /* End JavaScript handling */ |
} | ||
1320 | ||
1321 | if (ptr[1] == CHAR_LEFT_CURLY_BRACKET) | /* Handle \x in Perl's style. \x{ddd} is a character number which can be |
1322 | { | greater than 0xff in utf or non-8bit mode, but only if the ddd are hex |
1323 | const pcre_uchar *pt = ptr + 2; | digits. If not, { used to be treated as a data character. However, Perl |
1324 | seems to read hex digits up to the first non-such, and ignore the rest, so | |
1325 | that, for example \x{zz} matches a binary zero. This seems crazy, so PCRE | |
1326 | now gives an error. */ | |
1327 | ||
1328 | c = 0; | else |
1329 | overflow = FALSE; | { |
1330 | while (MAX_255(*pt) && (digitab[*pt] & ctype_xdigit) != 0) | if (ptr[1] == CHAR_LEFT_CURLY_BRACKET) |
1331 | { | { |
1332 | register pcre_uint32 cc = *pt++; | ptr += 2; |
1333 | if (c == 0 && cc == CHAR_0) continue; /* Leading zeroes */ | if (*ptr == CHAR_RIGHT_CURLY_BRACKET) |
1334 | { | |
1335 | *errorcodeptr = ERR86; | |
1336 | break; | |
1337 | } | |
1338 | c = 0; | |
1339 | overflow = FALSE; | |
1340 | while (MAX_255(*ptr) && (digitab[*ptr] & ctype_xdigit) != 0) | |
1341 | { | |
1342 | register pcre_uint32 cc = *ptr++; | |
1343 | if (c == 0 && cc == CHAR_0) continue; /* Leading zeroes */ | |
1344 | ||
1345 | #ifdef COMPILE_PCRE32 | #ifdef COMPILE_PCRE32 |
1346 | if (c >= 0x10000000l) { overflow = TRUE; break; } | if (c >= 0x10000000l) { overflow = TRUE; break; } |
1347 | #endif | #endif |
1348 | ||
1349 | #ifndef EBCDIC /* ASCII/UTF-8 coding */ | #ifndef EBCDIC /* ASCII/UTF-8 coding */ |
1350 | if (cc >= CHAR_a) cc -= 32; /* Convert to upper case */ | if (cc >= CHAR_a) cc -= 32; /* Convert to upper case */ |
1351 | c = (c << 4) + cc - ((cc < CHAR_A)? CHAR_0 : (CHAR_A - 10)); | c = (c << 4) + cc - ((cc < CHAR_A)? CHAR_0 : (CHAR_A - 10)); |
1352 | #else /* EBCDIC coding */ | #else /* EBCDIC coding */ |
1353 | if (cc >= CHAR_a && cc <= CHAR_z) cc += 64; /* Convert to upper case */ | if (cc >= CHAR_a && cc <= CHAR_z) cc += 64; /* Convert to upper case */ |
1354 | c = (c << 4) + cc - ((cc >= CHAR_0)? CHAR_0 : (CHAR_A - 10)); | c = (c << 4) + cc - ((cc >= CHAR_0)? CHAR_0 : (CHAR_A - 10)); |
1355 | #endif | #endif |
1356 | ||
1357 | #if defined COMPILE_PCRE8 | #if defined COMPILE_PCRE8 |
1358 | if (c > (utf ? 0x10ffffU : 0xffU)) { overflow = TRUE; break; } | if (c > (utf ? 0x10ffffU : 0xffU)) { overflow = TRUE; break; } |
1359 | #elif defined COMPILE_PCRE16 | #elif defined COMPILE_PCRE16 |
1360 | if (c > (utf ? 0x10ffffU : 0xffffU)) { overflow = TRUE; break; } | if (c > (utf ? 0x10ffffU : 0xffffU)) { overflow = TRUE; break; } |
1361 | #elif defined COMPILE_PCRE32 | #elif defined COMPILE_PCRE32 |
1362 | if (utf && c > 0x10ffffU) { overflow = TRUE; break; } | if (utf && c > 0x10ffffU) { overflow = TRUE; break; } |
1363 | #endif | #endif |
1364 | } | } |
1365 | ||
1366 | if (overflow) | if (overflow) |
1367 | { | { |
1368 | while (MAX_255(*pt) && (digitab[*pt] & ctype_xdigit) != 0) pt++; | while (MAX_255(*ptr) && (digitab[*ptr] & ctype_xdigit) != 0) ptr++; |
1369 | *errorcodeptr = ERR34; | *errorcodeptr = ERR34; |
1370 | } | } |
1371 | ||
1372 | if (*pt == CHAR_RIGHT_CURLY_BRACKET) | else if (*ptr == CHAR_RIGHT_CURLY_BRACKET) |
1373 | { | { |
1374 | if (utf && c >= 0xd800 && c <= 0xdfff) *errorcodeptr = ERR73; | if (utf && c >= 0xd800 && c <= 0xdfff) *errorcodeptr = ERR73; |
1375 | ptr = pt; | } |
break; | ||
} | ||
1376 | ||
1377 | /* If the sequence of hex digits does not end with '}', then we don't | /* If the sequence of hex digits does not end with '}', give an error. |
1378 | recognize this construct; fall through to the normal \x handling. */ | We used just to recognize this construct and fall through to the normal |
1379 | } | \x handling, but nowadays Perl gives an error, which seems much more |
1380 | sensible, so we do too. */ | |
1381 | ||
1382 | /* Read just a single-byte hex-defined char */ | else *errorcodeptr = ERR79; |
1383 | } /* End of \x{} processing */ | |
1384 | ||
1385 | c = 0; | /* Read a single-byte hex-defined char (up to two hex digits after \x) */ |
1386 | while (i++ < 2 && MAX_255(ptr[1]) && (digitab[ptr[1]] & ctype_xdigit) != 0) | |
1387 | { | else |
1388 | pcre_uint32 cc; /* Some compilers don't like */ | { |
1389 | cc = *(++ptr); /* ++ in initializers */ | c = 0; |
1390 | while (i++ < 2 && MAX_255(ptr[1]) && (digitab[ptr[1]] & ctype_xdigit) != 0) | |
1391 | { | |
1392 | pcre_uint32 cc; /* Some compilers don't like */ | |
1393 | cc = *(++ptr); /* ++ in initializers */ | |
1394 | #ifndef EBCDIC /* ASCII/UTF-8 coding */ | #ifndef EBCDIC /* ASCII/UTF-8 coding */ |
1395 | if (cc >= CHAR_a) cc -= 32; /* Convert to upper case */ | if (cc >= CHAR_a) cc -= 32; /* Convert to upper case */ |
1396 | c = c * 16 + cc - ((cc < CHAR_A)? CHAR_0 : (CHAR_A - 10)); | c = c * 16 + cc - ((cc < CHAR_A)? CHAR_0 : (CHAR_A - 10)); |
1397 | #else /* EBCDIC coding */ | #else /* EBCDIC coding */ |
1398 | if (cc <= CHAR_z) cc += 64; /* Convert to upper case */ | if (cc <= CHAR_z) cc += 64; /* Convert to upper case */ |
1399 | c = c * 16 + cc - ((cc >= CHAR_0)? CHAR_0 : (CHAR_A - 10)); | c = c * 16 + cc - ((cc >= CHAR_0)? CHAR_0 : (CHAR_A - 10)); |
1400 | #endif | #endif |
1401 | } | } |
1402 | } /* End of \xdd handling */ | |
1403 | } /* End of Perl-style \x handling */ | |
1404 | break; | break; |
1405 | ||
1406 | /* For \c, a following letter is upper-cased; then the 0x40 bit is flipped. | /* For \c, a following letter is upper-cased; then the 0x40 bit is flipped. |
# | Line 1199 if ((options & PCRE_UCP) != 0 && escape | Line 1466 if ((options & PCRE_UCP) != 0 && escape |
1466 | return escape; | return escape; |
1467 | } | } |
1468 | ||
1469 | ||
1470 | ||
1471 | #ifdef SUPPORT_UCP | #ifdef SUPPORT_UCP |
1472 | /************************************************* | /************************************************* |
1473 | * Handle \P and \p * | * Handle \P and \p * |
# | Line 1296 return FALSE; | Line 1565 return FALSE; |
1565 | ||
1566 | ||
1567 | ||
1568 | /************************************************* | /************************************************* |
1569 | * Read repeat counts * | * Read repeat counts * |
1570 | *************************************************/ | *************************************************/ |
# | Line 1322 read_repeat_counts(const pcre_uchar *p, | Line 1590 read_repeat_counts(const pcre_uchar *p, |
1590 | int min = 0; | int min = 0; |
1591 | int max = -1; | int max = -1; |
1592 | ||
1593 | /* Read the minimum value and do a paranoid check: a negative value indicates | while (IS_DIGIT(*p)) |
an integer overflow. */ | ||
while (IS_DIGIT(*p)) min = min * 10 + (int)(*p++ - CHAR_0); | ||
if (min < 0 || min > 65535) | ||
1594 | { | { |
1595 | *errorcodeptr = ERR5; | min = min * 10 + (int)(*p++ - CHAR_0); |
1596 | return p; | if (min > 65535) |
1597 | { | |
1598 | *errorcodeptr = ERR5; | |
1599 | return p; | |
1600 | } | |
1601 | } | } |
1602 | ||
/* Read the maximum value if there is one, and again do a paranoid on its size. | ||
Also, max must not be less than min. */ | ||
1603 | if (*p == CHAR_RIGHT_CURLY_BRACKET) max = min; else | if (*p == CHAR_RIGHT_CURLY_BRACKET) max = min; else |
1604 | { | { |
1605 | if (*(++p) != CHAR_RIGHT_CURLY_BRACKET) | if (*(++p) != CHAR_RIGHT_CURLY_BRACKET) |
1606 | { | { |
1607 | max = 0; | max = 0; |
1608 | while(IS_DIGIT(*p)) max = max * 10 + (int)(*p++ - CHAR_0); | while(IS_DIGIT(*p)) |
if (max < 0 || max > 65535) | ||
1609 | { | { |
1610 | *errorcodeptr = ERR5; | max = max * 10 + (int)(*p++ - CHAR_0); |
1611 | return p; | if (max > 65535) |
1612 | { | |
1613 | *errorcodeptr = ERR5; | |
1614 | return p; | |
1615 | } | |
1616 | } | } |
1617 | if (max < min) | if (max < min) |
1618 | { | { |
# | Line 1354 if (*p == CHAR_RIGHT_CURLY_BRACKET) max | Line 1622 if (*p == CHAR_RIGHT_CURLY_BRACKET) max |
1622 | } | } |
1623 | } | } |
1624 | ||
/* Fill in the required variables, and pass back the pointer to the terminating | ||
'}'. */ | ||
1625 | *minp = min; | *minp = min; |
1626 | *maxp = max; | *maxp = max; |
1627 | return p; | return p; |
# | Line 1403 for (;;) | Line 1668 for (;;) |
1668 | ||
1669 | case OP_CALLOUT: | case OP_CALLOUT: |
1670 | case OP_CREF: | case OP_CREF: |
1671 | case OP_NCREF: | case OP_DNCREF: |
1672 | case OP_RREF: | case OP_RREF: |
1673 | case OP_NRREF: | case OP_DNRREF: |
1674 | case OP_DEF: | case OP_DEF: |
1675 | code += PRIV(OP_lengths)[*code]; | code += PRIV(OP_lengths)[*code]; |
1676 | break; | break; |
# | Line 1419 for (;;) | Line 1684 for (;;) |
1684 | ||
1685 | ||
1686 | ||
1687 | /************************************************* | /************************************************* |
1688 | * Find the fixed length of a branch * | * Find the fixed length of a branch * |
1689 | *************************************************/ | *************************************************/ |
# | Line 1543 for (;;) | Line 1807 for (;;) |
1807 | case OP_COMMIT: | case OP_COMMIT: |
1808 | case OP_CREF: | case OP_CREF: |
1809 | case OP_DEF: | case OP_DEF: |
1810 | case OP_DNCREF: | |
1811 | case OP_DNRREF: | |
1812 | case OP_DOLL: | case OP_DOLL: |
1813 | case OP_DOLLM: | case OP_DOLLM: |
1814 | case OP_EOD: | case OP_EOD: |
1815 | case OP_EODN: | case OP_EODN: |
1816 | case OP_FAIL: | case OP_FAIL: |
case OP_NCREF: | ||
case OP_NRREF: | ||
1817 | case OP_NOT_WORD_BOUNDARY: | case OP_NOT_WORD_BOUNDARY: |
1818 | case OP_PRUNE: | case OP_PRUNE: |
1819 | case OP_REVERSE: | case OP_REVERSE: |
# | Line 1644 for (;;) | Line 1908 for (;;) |
1908 | ||
1909 | switch (*cc) | switch (*cc) |
1910 | { | { |
case OP_CRPLUS: | ||
case OP_CRMINPLUS: | ||
1911 | case OP_CRSTAR: | case OP_CRSTAR: |
1912 | case OP_CRMINSTAR: | case OP_CRMINSTAR: |
1913 | case OP_CRPLUS: | |
1914 | case OP_CRMINPLUS: | |
1915 | case OP_CRQUERY: | case OP_CRQUERY: |
1916 | case OP_CRMINQUERY: | case OP_CRMINQUERY: |
1917 | case OP_CRPOSSTAR: | |
1918 | case OP_CRPOSPLUS: | |
1919 | case OP_CRPOSQUERY: | |
1920 | return -1; | return -1; |
1921 | ||
1922 | case OP_CRRANGE: | case OP_CRRANGE: |
1923 | case OP_CRMINRANGE: | case OP_CRMINRANGE: |
1924 | case OP_CRPOSRANGE: | |
1925 | if (GET2(cc,1) != GET2(cc,1+IMM2_SIZE)) return -1; | if (GET2(cc,1) != GET2(cc,1+IMM2_SIZE)) return -1; |
1926 | branchlength += (int)GET2(cc,1); | branchlength += (int)GET2(cc,1); |
1927 | cc += 1 + 2 * IMM2_SIZE; | cc += 1 + 2 * IMM2_SIZE; |
# | Line 1760 for (;;) | Line 2028 for (;;) |
2028 | ||
2029 | ||
2030 | ||
2031 | /************************************************* | /************************************************* |
2032 | * Scan compiled regex for specific bracket * | * Scan compiled regex for specific bracket * |
2033 | *************************************************/ | *************************************************/ |
# | Line 1862 for (;;) | Line 2129 for (;;) |
2129 | { | { |
2130 | case OP_CHAR: | case OP_CHAR: |
2131 | case OP_CHARI: | case OP_CHARI: |
2132 | case OP_NOT: | |
2133 | case OP_NOTI: | |
2134 | case OP_EXACT: | case OP_EXACT: |
2135 | case OP_EXACTI: | case OP_EXACTI: |
2136 | case OP_NOTEXACT: | |
2137 | case OP_NOTEXACTI: | |
2138 | case OP_UPTO: | case OP_UPTO: |
2139 | case OP_UPTOI: | case OP_UPTOI: |
2140 | case OP_NOTUPTO: | |
2141 | case OP_NOTUPTOI: | |
2142 | case OP_MINUPTO: | case OP_MINUPTO: |
2143 | case OP_MINUPTOI: | case OP_MINUPTOI: |
2144 | case OP_NOTMINUPTO: | |
2145 | case OP_NOTMINUPTOI: | |
2146 | case OP_POSUPTO: | case OP_POSUPTO: |
2147 | case OP_POSUPTOI: | case OP_POSUPTOI: |
2148 | case OP_NOTPOSUPTO: | |
2149 | case OP_NOTPOSUPTOI: | |
2150 | case OP_STAR: | case OP_STAR: |
2151 | case OP_STARI: | case OP_STARI: |
2152 | case OP_NOTSTAR: | |
2153 | case OP_NOTSTARI: | |
2154 | case OP_MINSTAR: | case OP_MINSTAR: |
2155 | case OP_MINSTARI: | case OP_MINSTARI: |
2156 | case OP_NOTMINSTAR: | |
2157 | case OP_NOTMINSTARI: | |
2158 | case OP_POSSTAR: | case OP_POSSTAR: |
2159 | case OP_POSSTARI: | case OP_POSSTARI: |
2160 | case OP_NOTPOSSTAR: | |
2161 | case OP_NOTPOSSTARI: | |
2162 | case OP_PLUS: | case OP_PLUS: |
2163 | case OP_PLUSI: | case OP_PLUSI: |
2164 | case OP_NOTPLUS: | |
2165 | case OP_NOTPLUSI: | |
2166 | case OP_MINPLUS: | case OP_MINPLUS: |
2167 | case OP_MINPLUSI: | case OP_MINPLUSI: |
2168 | case OP_NOTMINPLUS: | |
2169 | case OP_NOTMINPLUSI: | |
2170 | case OP_POSPLUS: | case OP_POSPLUS: |
2171 | case OP_POSPLUSI: | case OP_POSPLUSI: |
2172 | case OP_NOTPOSPLUS: | |
2173 | case OP_NOTPOSPLUSI: | |
2174 | case OP_QUERY: | case OP_QUERY: |
2175 | case OP_QUERYI: | case OP_QUERYI: |
2176 | case OP_NOTQUERY: | |
2177 | case OP_NOTQUERYI: | |
2178 | case OP_MINQUERY: | case OP_MINQUERY: |
2179 | case OP_MINQUERYI: | case OP_MINQUERYI: |
2180 | case OP_NOTMINQUERY: | |
2181 | case OP_NOTMINQUERYI: | |
2182 | case OP_POSQUERY: | case OP_POSQUERY: |
2183 | case OP_POSQUERYI: | case OP_POSQUERYI: |
2184 | case OP_NOTPOSQUERY: | |
2185 | case OP_NOTPOSQUERYI: | |
2186 | if (HAS_EXTRALEN(code[-1])) code += GET_EXTRALEN(code[-1]); | if (HAS_EXTRALEN(code[-1])) code += GET_EXTRALEN(code[-1]); |
2187 | break; | break; |
2188 | } | } |
# | Line 2107 for (code = first_significant_code(code | Line 2402 for (code = first_significant_code(code |
2402 | if (c == OP_RECURSE) | if (c == OP_RECURSE) |
2403 | { | { |
2404 | const pcre_uchar *scode = cd->start_code + GET(code, 1); | const pcre_uchar *scode = cd->start_code + GET(code, 1); |
2405 | const pcre_uchar *endgroup = scode; | |
2406 | BOOL empty_branch; | BOOL empty_branch; |
2407 | ||
2408 | /* Test for forward reference or uncompleted reference. This is disabled | /* Test for forward reference or uncompleted reference. This is disabled |
# | Line 2121 for (code = first_significant_code(code | Line 2417 for (code = first_significant_code(code |
2417 | if (GET(scode, 1) == 0) return TRUE; /* Unclosed */ | if (GET(scode, 1) == 0) return TRUE; /* Unclosed */ |
2418 | } | } |
2419 | ||
2420 | /* If we are scanning a completed pattern, there are no forward references | /* If the reference is to a completed group, we need to detect whether this |
2421 | and all groups are complete. We need to detect whether this is a recursive | is a recursive call, as otherwise there will be an infinite loop. If it is |
2422 | call, as otherwise there will be an infinite loop. If it is a recursion, | a recursion, just skip over it. Simple recursions are easily detected. For |
2423 | just skip over it. Simple recursions are easily detected. For mutual | mutual recursions we keep a chain on the stack. */ |
recursions we keep a chain on the stack. */ | ||
2424 | ||
2425 | do endgroup += GET(endgroup, 1); while (*endgroup == OP_ALT); | |
2426 | if (code >= scode && code <= endgroup) continue; /* Simple recursion */ | |
2427 | else | else |
2428 | { | { |
2429 | recurse_check *r = recurses; | recurse_check *r = recurses; |
const pcre_uchar *endgroup = scode; | ||
do endgroup += GET(endgroup, 1); while (*endgroup == OP_ALT); | ||
if (code >= scode && code <= endgroup) continue; /* Simple recursion */ | ||
2430 | for (r = recurses; r != NULL; r = r->prev) | for (r = recurses; r != NULL; r = r->prev) |
2431 | if (r->group == scode) break; | if (r->group == scode) break; |
2432 | if (r != NULL) continue; /* Mutual recursion */ | if (r != NULL) continue; /* Mutual recursion */ |
# | Line 2205 for (code = first_significant_code(code | Line 2497 for (code = first_significant_code(code |
2497 | empty_branch = FALSE; | empty_branch = FALSE; |
2498 | do | do |
2499 | { | { |
2500 | if (!empty_branch && could_be_empty_branch(code, endcode, utf, cd, NULL)) | if (!empty_branch && could_be_empty_branch(code, endcode, utf, cd, |
2501 | empty_branch = TRUE; | recurses)) empty_branch = TRUE; |
2502 | code += GET(code, 1); | code += GET(code, 1); |
2503 | } | } |
2504 | while (*code == OP_ALT); | while (*code == OP_ALT); |
# | Line 2247 for (code = first_significant_code(code | Line 2539 for (code = first_significant_code(code |
2539 | case OP_CRMINSTAR: | case OP_CRMINSTAR: |
2540 | case OP_CRQUERY: | case OP_CRQUERY: |
2541 | case OP_CRMINQUERY: | case OP_CRMINQUERY: |
2542 | case OP_CRPOSSTAR: | |
2543 | case OP_CRPOSQUERY: | |
2544 | break; | break; |
2545 | ||
2546 | default: /* Non-repeat => class must match */ | default: /* Non-repeat => class must match */ |
2547 | case OP_CRPLUS: /* These repeats aren't empty */ | case OP_CRPLUS: /* These repeats aren't empty */ |
2548 | case OP_CRMINPLUS: | case OP_CRMINPLUS: |
2549 | case OP_CRPOSPLUS: | |
2550 | return FALSE; | return FALSE; |
2551 | ||
2552 | case OP_CRRANGE: | case OP_CRRANGE: |
2553 | case OP_CRMINRANGE: | case OP_CRMINRANGE: |
2554 | case OP_CRPOSRANGE: | |
2555 | if (GET2(ccode, 1) > 0) return FALSE; /* Minimum > 0 */ | if (GET2(ccode, 1) > 0) return FALSE; /* Minimum > 0 */ |
2556 | break; | break; |
2557 | } | } |
# | Line 2462 return TRUE; | Line 2758 return TRUE; |
2758 | ||
2759 | ||
2760 | /************************************************* | /************************************************* |
2761 | * Check for POSIX class syntax * | * Base opcode of repeated opcodes * |
2762 | *************************************************/ | *************************************************/ |
2763 | ||
2764 | /* This function is called when the sequence "[:" or "[." or "[=" is | /* Returns the base opcode for repeated single character type opcodes. If the |
2765 | encountered in a character class. It checks whether this is followed by a | opcode is not a repeated character type, it returns with the original value. |
sequence of characters terminated by a matching ":]" or ".]" or "=]". If we | ||
reach an unescaped ']' without the special preceding character, return FALSE. | ||
Originally, this function only recognized a sequence of letters between the | ||
terminators, but it seems that Perl recognizes any sequence of characters, | ||
though of course unknown POSIX names are subsequently rejected. Perl gives an | ||
"Unknown POSIX class" error for [:f\oo:] for example, where previously PCRE | ||
didn't consider this to be a POSIX class. Likewise for [:1234:]. | ||
The problem in trying to be exactly like Perl is in the handling of escapes. We | ||
have to be sure that [abc[:x\]pqr] is *not* treated as containing a POSIX | ||
class, but [abc[:x\]pqr:]] is (so that an error can be generated). The code | ||
below handles the special case of \], but does not try to do any other escape | ||
processing. This makes it different from Perl for cases such as [:l\ower:] | ||
where Perl recognizes it as the POSIX class "lower" but PCRE does not recognize | ||
"l\ower". This is a lesser evil that not diagnosing bad classes when Perl does, | ||
I think. | ||
A user pointed out that PCRE was rejecting [:a[:digit:]] whereas Perl was not. | ||
It seems that the appearance of a nested POSIX class supersedes an apparent | ||
external class. For example, [:a[:digit:]b:] matches "a", "b", ":", or | ||
a digit. | ||
In Perl, unescaped square brackets may also appear as part of class names. For | ||
example, [:a[:abc]b:] gives unknown POSIX class "[:abc]b:]". However, for | ||
[:a[:abc]b][b:] it gives unknown POSIX class "[:abc]b][b:]", which does not | ||
seem right at all. PCRE does not allow closing square brackets in POSIX class | ||
names. | ||
Arguments: | ||
ptr pointer to the initial [ | ||
endptr where to return the end pointer | ||
2766 | ||
2767 | Returns: TRUE or FALSE | Arguments: c opcode |
2768 | Returns: base opcode for the type | |
2769 | */ | */ |
2770 | ||
2771 | static BOOL | static pcre_uchar |
2772 | check_posix_syntax(const pcre_uchar *ptr, const pcre_uchar **endptr) | get_repeat_base(pcre_uchar c) |
2773 | { | { |
2774 | pcre_uchar terminator; /* Don't combine these lines; the Solaris cc */ | return (c > OP_TYPEPOSUPTO)? c : |
2775 | terminator = *(++ptr); /* compiler warns about "non-constant" initializer. */ | (c >= OP_TYPESTAR)? OP_TYPESTAR : |
2776 | for (++ptr; *ptr != CHAR_NULL; ptr++) | (c >= OP_NOTSTARI)? OP_NOTSTARI : |
2777 | { | (c >= OP_NOTSTAR)? OP_NOTSTAR : |
2778 | if (*ptr == CHAR_BACKSLASH && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET) | (c >= OP_STARI)? OP_STARI : |
2779 | ptr++; | OP_STAR; |
else if (*ptr == CHAR_RIGHT_SQUARE_BRACKET) return FALSE; | ||
else | ||
{ | ||
if (*ptr == terminator && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET) | ||
{ | ||
*endptr = ptr; | ||
return TRUE; | ||
} | ||
if (*ptr == CHAR_LEFT_SQUARE_BRACKET && | ||
(ptr[1] == CHAR_COLON || ptr[1] == CHAR_DOT || | ||
ptr[1] == CHAR_EQUALS_SIGN) && | ||
check_posix_syntax(ptr, endptr)) | ||
return FALSE; | ||
} | ||
} | ||
return FALSE; | ||
2780 | } | } |
2781 | ||
2782 | ||
2783 | ||
2784 | #ifdef SUPPORT_UCP | |
2785 | /************************************************* | /************************************************* |
2786 | * Check POSIX class name * | * Check a character and a property * |
2787 | *************************************************/ | *************************************************/ |
2788 | ||
2789 | /* This function is called to check the name given in a POSIX-style class entry | /* This function is called by check_auto_possessive() when a property item |
2790 | such as [:alnum:]. | is adjacent to a fixed character. |
2791 | ||
2792 | Arguments: | Arguments: |
2793 | ptr points to the first letter | c the character |
2794 | len the length of the name | ptype the property type |
2795 | pdata the data for the type | |
2796 | negated TRUE if it's a negated property (\P or \p{^) | |
2797 | ||
2798 | Returns: a value representing the name, or -1 if unknown | Returns: TRUE if auto-possessifying is OK |
2799 | */ | */ |
2800 | ||
2801 | static int | static BOOL |
2802 | check_posix_name(const pcre_uchar *ptr, int len) | check_char_prop(pcre_uint32 c, unsigned int ptype, unsigned int pdata, |
2803 | BOOL negated) | |
2804 | { | { |
2805 | const char *pn = posix_names; | const pcre_uint32 *p; |
2806 | register int yield = 0; | const ucd_record *prop = GET_UCD(c); |
2807 | while (posix_name_lengths[yield] != 0) | |
2808 | switch(ptype) | |
2809 | { | { |
2810 | if (len == posix_name_lengths[yield] && | case PT_LAMP: |
2811 | STRNCMP_UC_C8(ptr, pn, (unsigned int)len) == 0) return yield; | return (prop->chartype == ucp_Lu || |
2812 | pn += posix_name_lengths[yield] + 1; | prop->chartype == ucp_Ll || |
2813 | yield++; | prop->chartype == ucp_Lt) == negated; |
} | ||
return -1; | ||
} | ||
2814 | ||
2815 | case PT_GC: | |
2816 | return (pdata == PRIV(ucp_gentype)[prop->chartype]) == negated; | |
2817 | ||
2818 | /************************************************* | case PT_PC: |
2819 | * Adjust OP_RECURSE items in repeated group * | return (pdata == prop->chartype) == negated; |
*************************************************/ | ||
2820 | ||
2821 | /* OP_RECURSE items contain an offset from the start of the regex to the group | case PT_SC: |
2822 | that is referenced. This means that groups can be replicated for fixed | return (pdata == prop->script) == negated; |
repetition simply by copying (because the recursion is allowed to refer to | ||
earlier groups that are outside the current group). However, when a group is | ||
optional (i.e. the minimum quantifier is zero), OP_BRAZERO or OP_SKIPZERO is | ||
inserted before it, after it has been compiled. This means that any OP_RECURSE | ||
items within it that refer to the group itself or any contained groups have to | ||
have their offsets adjusted. That one of the jobs of this function. Before it | ||
is called, the partially compiled regex must be temporarily terminated with | ||
OP_END. | ||
This function has been extended with the possibility of forward references for | ||
recursions and subroutine calls. It must also check the list of such references | ||
for the group we are dealing with. If it finds that one of the recursions in | ||
the current group is on this list, it adjusts the offset in the list, not the | ||
value in the reference (which is a group number). | ||
Arguments: | ||
group points to the start of the group | ||
adjust the amount by which the group is to be moved | ||
utf TRUE in UTF-8 / UTF-16 / UTF-32 mode | ||
cd contains pointers to tables etc. | ||
save_hwm the hwm forward reference pointer at the start of the group | ||
Returns: nothing | ||
*/ | ||
2823 | ||
2824 | static void | /* These are specials */ |
adjust_recurse(pcre_uchar *group, int adjust, BOOL utf, compile_data *cd, | ||
pcre_uchar *save_hwm) | ||
{ | ||
pcre_uchar *ptr = group; | ||
2825 | ||
2826 | while ((ptr = (pcre_uchar *)find_recurse(ptr, utf)) != NULL) | case PT_ALNUM: |
2827 | { | return (PRIV(ucp_gentype)[prop->chartype] == ucp_L || |
2828 | int offset; | PRIV(ucp_gentype)[prop->chartype] == ucp_N) == negated; |
pcre_uchar *hc; | ||
2829 | ||
2830 | /* See if this recursion is on the forward reference list. If so, adjust the | /* Perl space used to exclude VT, but from Perl 5.18 it is included, which |
2831 | reference. */ | means that Perl space and POSIX space are now identical. PCRE was changed |
2832 | at release 8.34. */ | |
2833 | ||
2834 | for (hc = save_hwm; hc < cd->hwm; hc += LINK_SIZE) | case PT_SPACE: /* Perl space */ |
2835 | case PT_PXSPACE: /* POSIX space */ | |
2836 | switch(c) | |
2837 | { | { |
2838 | offset = (int)GET(hc, 0); | HSPACE_CASES: |
2839 | if (cd->start_code + offset == ptr + 1) | VSPACE_CASES: |
2840 | { | return negated; |
2841 | PUT(hc, 0, offset + adjust); | |
2842 | break; | default: |
2843 | } | return (PRIV(ucp_gentype)[prop->chartype] == ucp_Z) == negated; |
2844 | } | } |
2845 | break; /* Control never reaches here */ | |
2846 | ||
2847 | /* Otherwise, adjust the recursion offset if it's after the start of this | case PT_WORD: |
2848 | group. */ | return (PRIV(ucp_gentype)[prop->chartype] == ucp_L || |
2849 | PRIV(ucp_gentype)[prop->chartype] == ucp_N || | |
2850 | c == CHAR_UNDERSCORE) == negated; | |
2851 | ||
2852 | if (hc >= cd->hwm) | case PT_CLIST: |
2853 | p = PRIV(ucd_caseless_sets) + prop->caseset; | |
2854 | for (;;) | |
2855 | { | { |
2856 | offset = (int)GET(ptr, 1); | if (c < *p) return !negated; |
2857 | if (cd->start_code + offset >= group) PUT(ptr, 1, offset + adjust); | if (c == *p++) return negated; |
2858 | } | } |
2859 | break; /* Control never reaches here */ | |
ptr += 1 + LINK_SIZE; | ||
2860 | } | } |
2861 | ||
2862 | return FALSE; | |
2863 | } | } |
2864 | #endif /* SUPPORT_UCP */ | |
2865 | ||
2866 | ||
2867 | ||
2868 | /************************************************* | /************************************************* |
2869 | * Insert an automatic callout point * | * Fill the character property list * |
2870 | *************************************************/ | *************************************************/ |
2871 | ||
2872 | /* This function is called when the PCRE_AUTO_CALLOUT option is set, to insert | /* Checks whether the code points to an opcode that can take part in auto- |
2873 | callout points before each pattern item. | possessification, and if so, fills a list with its properties. |
2874 | ||
2875 | Arguments: | Arguments: |
2876 | code current code pointer | code points to start of expression |
2877 | ptr current pattern pointer | utf TRUE if in UTF-8 / UTF-16 / UTF-32 mode |
2878 | cd pointers to tables etc | fcc points to case-flipping table |
2879 | list points to output list | |
2880 | list[0] will be filled with the opcode | |
2881 | list[1] will be non-zero if this opcode | |
2882 | can match an empty character string | |
2883 | list[2..7] depends on the opcode | |
2884 | ||
2885 | Returns: new code pointer | Returns: points to the start of the next opcode if *code is accepted |
2886 | NULL if *code is not accepted | |
2887 | */ | */ |
2888 | ||
2889 | static pcre_uchar * | static const pcre_uchar * |
2890 | auto_callout(pcre_uchar *code, const pcre_uchar *ptr, compile_data *cd) | get_chr_property_list(const pcre_uchar *code, BOOL utf, |
2891 | const pcre_uint8 *fcc, pcre_uint32 *list) | |
2892 | { | { |
2893 | *code++ = OP_CALLOUT; | pcre_uchar c = *code; |
2894 | *code++ = 255; | pcre_uchar base; |
2895 | PUT(code, 0, (int)(ptr - cd->start_pattern)); /* Pattern offset */ | const pcre_uchar *end; |
2896 | PUT(code, LINK_SIZE, 0); /* Default length */ | pcre_uint32 chr; |
return code + 2 * LINK_SIZE; | ||
} | ||
2897 | ||
2898 | #ifdef SUPPORT_UCP | |
2899 | pcre_uint32 *clist_dest; | |
2900 | const pcre_uint32 *clist_src; | |
2901 | #else | |
2902 | utf = utf; /* Suppress "unused parameter" compiler warning */ | |
2903 | #endif | |
2904 | ||
2905 | list[0] = c; | |
2906 | list[1] = FALSE; | |
2907 | code++; | |
2908 | ||
2909 | /************************************************* | if (c >= OP_STAR && c <= OP_TYPEPOSUPTO) |
2910 | * Complete a callout item * | { |
2911 | *************************************************/ | base = get_repeat_base(c); |
2912 | c -= (base - OP_STAR); | |
2913 | ||
2914 | /* A callout item contains the length of the next item in the pattern, which | if (c == OP_UPTO || c == OP_MINUPTO || c == OP_EXACT || c == OP_POSUPTO) |
2915 | we can't fill in till after we have reached the relevant point. This is used | code += IMM2_SIZE; |
for both automatic and manual callouts. | ||
2916 | ||
2917 | Arguments: | list[1] = (c != OP_PLUS && c != OP_MINPLUS && c != OP_EXACT && c != OP_POSPLUS); |
previous_callout points to previous callout item | ||
ptr current pattern pointer | ||
cd pointers to tables etc | ||
2918 | ||
2919 | Returns: nothing | switch(base) |
2920 | */ | { |
2921 | case OP_STAR: | |
2922 | list[0] = OP_CHAR; | |
2923 | break; | |
2924 | ||
2925 | static void | case OP_STARI: |
2926 | complete_callout(pcre_uchar *previous_callout, const pcre_uchar *ptr, compile_data *cd) | list[0] = OP_CHARI; |
2927 | { | break; |
int length = (int)(ptr - cd->start_pattern - GET(previous_callout, 2)); | ||
PUT(previous_callout, 2 + LINK_SIZE, length); | ||
} | ||
2928 | ||
2929 | case OP_NOTSTAR: | |
2930 | list[0] = OP_NOT; | |
2931 | break; | |
2932 | ||
2933 | case OP_NOTSTARI: | |
2934 | list[0] = OP_NOTI; | |
2935 | break; | |
2936 | ||
2937 | #ifdef SUPPORT_UCP | case OP_TYPESTAR: |
2938 | /************************************************* | list[0] = *code; |
2939 | * Get othercase range * | code++; |
2940 | *************************************************/ | break; |
2941 | } | |
2942 | c = list[0]; | |
2943 | } | |
2944 | ||
2945 | /* This function is passed the start and end of a class range, in UTF-8 mode | switch(c) |
2946 | with UCP support. It searches up the characters, looking for ranges of | { |
2947 | characters in the "other" case. Each call returns the next one, updating the | case OP_NOT_DIGIT: |
2948 | start address. A character with multiple other cases is returned on its own | case OP_DIGIT: |
2949 | with a special return value. | case OP_NOT_WHITESPACE: |
2950 | case OP_WHITESPACE: | |
2951 | case OP_NOT_WORDCHAR: | |
2952 | case OP_WORDCHAR: | |
2953 | case OP_ANY: | |
2954 | case OP_ALLANY: | |
2955 | case OP_ANYNL: | |
2956 | case OP_NOT_HSPACE: | |
2957 | case OP_HSPACE: | |
2958 | case OP_NOT_VSPACE: | |
2959 | case OP_VSPACE: | |
2960 | case OP_EXTUNI: | |
2961 | case OP_EODN: | |
2962 | case OP_EOD: | |
2963 | case OP_DOLL: | |
2964 | case OP_DOLLM: | |
2965 | return code; | |
2966 | ||
2967 | Arguments: | case OP_CHAR: |
2968 | cptr points to starting character value; updated | case OP_NOT: |
2969 | d end value | GETCHARINCTEST(chr, code); |
2970 | ocptr where to put start of othercase range | list[2] = chr; |
2971 | odptr where to put end of othercase range | list[3] = NOTACHAR; |
2972 | return code; | |
2973 | ||
2974 | Yield: -1 when no more | case OP_CHARI: |
2975 | 0 when a range is returned | case OP_NOTI: |
2976 | >0 the CASESET offset for char with multiple other cases | list[0] = (c == OP_CHARI) ? OP_CHAR : OP_NOT; |
2977 | in this case, ocptr contains the original | GETCHARINCTEST(chr, code); |
2978 | */ | list[2] = chr; |
2979 | ||
2980 | static int | #ifdef SUPPORT_UCP |
2981 | get_othercase_range(pcre_uint32 *cptr, pcre_uint32 d, pcre_uint32 *ocptr, | if (chr < 128 || (chr < 256 && !utf)) |
2982 | pcre_uint32 *odptr) | list[3] = fcc[chr]; |
2983 | { | else |
2984 | pcre_uint32 c, othercase, next; | list[3] = UCD_OTHERCASE(chr); |
2985 | unsigned int co; | #elif defined SUPPORT_UTF || !defined COMPILE_PCRE8 |
2986 | list[3] = (chr < 256) ? fcc[chr] : chr; | |
2987 | #else | |
2988 | list[3] = fcc[chr]; | |
2989 | #endif | |
2990 | ||
2991 | /* Find the first character that has an other case. If it has multiple other | /* The othercase might be the same value. */ |
cases, return its case offset value. */ | ||
2992 | ||
2993 | for (c = *cptr; c <= d; c++) | if (chr == list[3]) |
2994 | { | list[3] = NOTACHAR; |
2995 | if ((co = UCD_CASESET(c)) != 0) | else |
2996 | list[4] = NOTACHAR; | |
2997 | return code; | |
2998 | ||
2999 | #ifdef SUPPORT_UCP | |
3000 | case OP_PROP: | |
3001 | case OP_NOTPROP: | |
3002 | if (code[0] != PT_CLIST) | |
3003 | { | { |
3004 | *ocptr = c++; /* Character that has the set */ | list[2] = code[0]; |
3005 | *cptr = c; /* Rest of input range */ | list[3] = code[1]; |
3006 | return (int)co; | return code + 2; |
3007 | } | } |
if ((othercase = UCD_OTHERCASE(c)) != c) break; | ||
} | ||
3008 | ||
3009 | if (c > d) return -1; /* Reached end of range */ | /* Convert only if we have enough space. */ |
3010 | ||
3011 | *ocptr = othercase; | clist_src = PRIV(ucd_caseless_sets) + code[1]; |
3012 | next = othercase + 1; | clist_dest = list + 2; |
3013 | code += 2; | |
3014 | ||
3015 | for (++c; c <= d; c++) | do { |
3016 | { | if (clist_dest >= list + 8) |
3017 | if (UCD_OTHERCASE(c) != next) break; | { |
3018 | next++; | /* Early return if there is not enough space. This should never |
3019 | } | happen, since all clists are shorter than 5 character now. */ |
3020 | list[2] = code[0]; | |
3021 | list[3] = code[1]; | |
3022 | return code; | |
3023 | } | |
3024 | *clist_dest++ = *clist_src; | |
3025 | } | |
3026 | while(*clist_src++ != NOTACHAR); | |
3027 | ||
3028 | *odptr = next - 1; /* End of othercase range */ | /* All characters are stored. The terminating NOTACHAR |
3029 | *cptr = c; /* Rest of input range */ | is copied form the clist itself. */ |
3030 | return 0; | |
3031 | list[0] = (c == OP_PROP) ? OP_CHAR : OP_NOT; | |
3032 | return code; | |
3033 | #endif | |
3034 | ||
3035 | case OP_NCLASS: | |
3036 | case OP_CLASS: | |
3037 | #if defined SUPPORT_UTF || !defined COMPILE_PCRE8 | |
3038 | case OP_XCLASS: | |
3039 | if (c == OP_XCLASS) | |
3040 | end = code + GET(code, 0) - 1; | |
3041 | else | |
3042 | #endif | |
3043 | end = code + 32 / sizeof(pcre_uchar); | |
3044 | ||
3045 | switch(*end) | |
3046 | { | |
3047 | case OP_CRSTAR: | |
3048 | case OP_CRMINSTAR: | |
3049 | case OP_CRQUERY: | |
3050 | case OP_CRMINQUERY: | |
3051 | case OP_CRPOSSTAR: | |
3052 | case OP_CRPOSQUERY: | |
3053 | list[1] = TRUE; | |
3054 | end++; | |
3055 | break; | |
3056 | ||
3057 | case OP_CRPLUS: | |
3058 | case OP_CRMINPLUS: | |
3059 | case OP_CRPOSPLUS: | |
3060 | end++; | |
3061 | break; | |
3062 | ||
3063 | case OP_CRRANGE: | |
3064 | case OP_CRMINRANGE: | |
3065 | case OP_CRPOSRANGE: | |
3066 | list[1] = (GET2(end, 1) == 0); | |
3067 | end += 1 + 2 * IMM2_SIZE; | |
3068 | break; | |
3069 | } | |
3070 | list[2] = (pcre_uint32)(end - code); | |
3071 | return end; | |
3072 | } | |
3073 | return NULL; /* Opcode not accepted */ | |
3074 | } | } |
3075 | ||
3076 | ||
3077 | ||
3078 | /************************************************* | /************************************************* |
3079 | * Check a character and a property * | * Scan further character sets for match * |
3080 | *************************************************/ | *************************************************/ |
3081 | ||
3082 | /* This function is called by check_auto_possessive() when a property item | /* Checks whether the base and the current opcode have a common character, in |
3083 | is adjacent to a fixed character. | which case the base cannot be possessified. |
3084 | ||
3085 | Arguments: | Arguments: |
3086 | c the character | code points to the byte code |
3087 | ptype the property type | utf TRUE in UTF-8 / UTF-16 / UTF-32 mode |
3088 | pdata the data for the type | cd static compile data |
3089 | negated TRUE if it's a negated property (\P or \p{^) | base_list the data list of the base opcode |
3090 | ||
3091 | Returns: TRUE if auto-possessifying is OK | Returns: TRUE if the auto-possessification is possible |
3092 | */ | */ |
3093 | ||
3094 | static BOOL | static BOOL |
3095 | check_char_prop(pcre_uint32 c, unsigned int ptype, unsigned int pdata, BOOL negated) | compare_opcodes(const pcre_uchar *code, BOOL utf, const compile_data *cd, |
3096 | const pcre_uint32 *base_list, const pcre_uchar *base_end, int *rec_limit) | |
3097 | { | { |
3098 | #ifdef SUPPORT_UCP | pcre_uchar c; |
3099 | const pcre_uint32 *p; | pcre_uint32 list[8]; |
3100 | const pcre_uint32 *chr_ptr; | |
3101 | const pcre_uint32 *ochr_ptr; | |
3102 | const pcre_uint32 *list_ptr; | |
3103 | const pcre_uchar *next_code; | |
3104 | #if defined SUPPORT_UTF || !defined COMPILE_PCRE8 | |
3105 | const pcre_uchar *xclass_flags; | |
3106 | #endif | #endif |
3107 | const pcre_uint8 *class_bitset; | |
3108 | const pcre_uint8 *set1, *set2, *set_end; | |
3109 | pcre_uint32 chr; | |
3110 | BOOL accepted, invert_bits; | |
3111 | BOOL entered_a_group = FALSE; | |
3112 | ||
3113 | if (*rec_limit == 0) return FALSE; | |
3114 | --(*rec_limit); | |
3115 | ||
3116 | /* Note: the base_list[1] contains whether the current opcode has greedy | |
3117 | (represented by a non-zero value) quantifier. This is a different from | |
3118 | other character type lists, which stores here that the character iterator | |
3119 | matches to an empty string (also represented by a non-zero value). */ | |
3120 | ||
3121 | const ucd_record *prop = GET_UCD(c); | for(;;) |
switch(ptype) | ||
3122 | { | { |
3123 | case PT_LAMP: | /* All operations move the code pointer forward. |
3124 | return (prop->chartype == ucp_Lu || | Therefore infinite recursions are not possible. */ |
prop->chartype == ucp_Ll || | ||
prop->chartype == ucp_Lt) == negated; | ||
3125 | ||
3126 | case PT_GC: | c = *code; |
return (pdata == PRIV(ucp_gentype)[prop->chartype]) == negated; | ||
3127 | ||
3128 | case PT_PC: | /* Skip over callouts */ |
return (pdata == prop->chartype) == negated; | ||
3129 | ||
3130 | case PT_SC: | if (c == OP_CALLOUT) |
3131 | return (pdata == prop->script) == negated; | { |
3132 | code += PRIV(OP_lengths)[c]; | |
3133 | continue; | |
3134 | } | |
3135 | ||
3136 | /* These are specials */ | if (c == OP_ALT) |
3137 | { | |
3138 | do code += GET(code, 1); while (*code == OP_ALT); | |
3139 | c = *code; | |
3140 | } | |
3141 | ||
3142 | case PT_ALNUM: | switch(c) |
3143 | return (PRIV(ucp_gentype)[prop->chartype] == ucp_L || | { |
3144 | PRIV(ucp_gentype)[prop->chartype] == ucp_N) == negated; | case OP_END: |
3145 | case OP_KETRPOS: | |
3146 | /* TRUE only in greedy case. The non-greedy case could be replaced by | |
3147 | an OP_EXACT, but it is probably not worth it. (And note that OP_EXACT | |
3148 | uses more memory, which we cannot get at this stage.) */ | |
3149 | ||
3150 | case PT_SPACE: /* Perl space */ | return base_list[1] != 0; |
return (PRIV(ucp_gentype)[prop->chartype] == ucp_Z || | ||
c == CHAR_HT || c == CHAR_NL || c == CHAR_FF || c == CHAR_CR) | ||
== negated; | ||
3151 | ||
3152 | case PT_PXSPACE: /* POSIX space */ | case OP_KET: |
3153 | return (PRIV(ucp_gentype)[prop->chartype] == ucp_Z || | /* If the bracket is capturing, and referenced by an OP_RECURSE, or |
3154 | c == CHAR_HT || c == CHAR_NL || c == CHAR_VT || | it is an atomic sub-pattern (assert, once, etc.) the non-greedy case |
3155 | c == CHAR_FF || c == CHAR_CR) | cannot be converted to a possessive form. */ |
3156 | == negated; | |
3157 | if (base_list[1] == 0) return FALSE; | |
3158 | ||
3159 | switch(*(code - GET(code, 1))) | |
3160 | { | |
3161 | case OP_ASSERT: | |
3162 | case OP_ASSERT_NOT: | |
3163 | case OP_ASSERTBACK: | |
3164 | case OP_ASSERTBACK_NOT: | |
3165 | case OP_ONCE: | |
3166 | case OP_ONCE_NC: | |
3167 | /* Atomic sub-patterns and assertions can always auto-possessify their | |
3168 | last iterator. However, if the group was entered as a result of checking | |
3169 | a previous iterator, this is not possible. */ | |
3170 | ||
3171 | case PT_WORD: | return !entered_a_group; |
3172 | return (PRIV(ucp_gentype)[prop->chartype] == ucp_L || | } |
PRIV(ucp_gentype)[prop->chartype] == ucp_N || | ||
c == CHAR_UNDERSCORE) == negated; | ||
3173 | ||
3174 | #ifdef SUPPORT_UCP | code += PRIV(OP_lengths)[c]; |
3175 | case PT_CLIST: | continue; |
3176 | p = PRIV(ucd_caseless_sets) + prop->caseset; | |
3177 | for (;;) | case OP_ONCE: |
3178 | case OP_ONCE_NC: | |
3179 | case OP_BRA: | |
3180 | case OP_CBRA: | |
3181 | next_code = code + GET(code, 1); | |
3182 | code += PRIV(OP_lengths)[c]; | |
3183 | ||
3184 | while (*next_code == OP_ALT) | |
3185 | { | |
3186 | if (!compare_opcodes(code, utf, cd, base_list, base_end, rec_limit)) | |
3187 | return FALSE; | |
3188 | code = next_code + 1 + LINK_SIZE; | |
3189 | next_code += GET(next_code, 1); | |
3190 | } | |
3191 | ||
3192 | entered_a_group = TRUE; | |
3193 | continue; | |
3194 | ||
3195 | case OP_BRAZERO: | |
3196 | case OP_BRAMINZERO: | |
3197 | ||
3198 | next_code = code + 1; | |
3199 | if (*next_code != OP_BRA && *next_code != OP_CBRA | |
3200 | && *next_code != OP_ONCE && *next_code != OP_ONCE_NC) return FALSE; | |
3201 | ||
3202 | do next_code += GET(next_code, 1); while (*next_code == OP_ALT); | |
3203 | ||
3204 | /* The bracket content will be checked by the | |
3205 | OP_BRA/OP_CBRA case above. */ | |
3206 | next_code += 1 + LINK_SIZE; | |
3207 | if (!compare_opcodes(next_code, utf, cd, base_list, base_end, rec_limit)) | |
3208 | return FALSE; | |
3209 | ||
3210 | code += PRIV(OP_lengths)[c]; | |
3211 | continue; | |
3212 | ||
3213 | default: | |
3214 | break; | |
3215 | } | |
3216 | ||
3217 | /* Check for a supported opcode, and load its properties. */ | |
3218 | ||
3219 | code = get_chr_property_list(code, utf, cd->fcc, list); | |
3220 | if (code == NULL) return FALSE; /* Unsupported */ | |
3221 | ||
3222 | /* If either opcode is a small character list, set pointers for comparing | |
3223 | characters from that list with another list, or with a property. */ | |
3224 | ||
3225 | if (base_list[0] == OP_CHAR) | |
3226 | { | { |
3227 | if (c < *p) return !negated; | chr_ptr = base_list + 2; |
3228 | if (c == *p++) return negated; | list_ptr = list; |
3229 | } | } |
3230 | break; /* Control never reaches here */ | else if (list[0] == OP_CHAR) |
3231 | { | |
3232 | chr_ptr = list + 2; | |
3233 | list_ptr = base_list; | |
3234 | } | |
3235 | ||
3236 | /* Character bitsets can also be compared to certain opcodes. */ | |
3237 | ||
3238 | else if (base_list[0] == OP_CLASS || list[0] == OP_CLASS | |
3239 | #ifdef COMPILE_PCRE8 | |
3240 | /* In 8 bit, non-UTF mode, OP_CLASS and OP_NCLASS are the same. */ | |
3241 | || (!utf && (base_list[0] == OP_NCLASS || list[0] == OP_NCLASS)) | |
3242 | #endif | |
3243 | ) | |
3244 | { | |
3245 | #ifdef COMPILE_PCRE8 | |
3246 | if (base_list[0] == OP_CLASS || (!utf && base_list[0] == OP_NCLASS)) | |
3247 | #else | |
3248 | if (base_list[0] == OP_CLASS) | |
3249 | #endif | |
3250 | { | |
3251 | set1 = (pcre_uint8 *)(base_end - base_list[2]); | |
3252 | list_ptr = list; | |
3253 | } | |
3254 | else | |
3255 | { | |
3256 | set1 = (pcre_uint8 *)(code - list[2]); | |
3257 | list_ptr = base_list; | |
3258 | } | |
3259 | ||
3260 | invert_bits = FALSE; | |
3261 | switch(list_ptr[0]) | |
3262 | { | |
3263 | case OP_CLASS: | |
3264 | case OP_NCLASS: | |
3265 | set2 = (pcre_uint8 *) | |
3266 | ((list_ptr == list ? code : base_end) - list_ptr[2]); | |
3267 | break; | |
3268 | ||
3269 | #if defined SUPPORT_UTF || !defined COMPILE_PCRE8 | |
3270 | case OP_XCLASS: | |
3271 | xclass_flags = (list_ptr == list ? code : base_end) - list_ptr[2] + LINK_SIZE; | |
3272 | if ((*xclass_flags & XCL_HASPROP) != 0) return FALSE; | |
3273 | if ((*xclass_flags & XCL_MAP) == 0) | |
3274 | { | |
3275 | /* No bits are set for characters < 256. */ | |
3276 | if (list[1] == 0) return TRUE; | |
3277 | /* Might be an empty repeat. */ | |
3278 | continue; | |
3279 | } | |
3280 | set2 = (pcre_uint8 *)(xclass_flags + 1); | |
3281 | break; | |
3282 | #endif | #endif |
3283 | ||
3284 | case OP_NOT_DIGIT: | |
3285 | invert_bits = TRUE; | |
3286 | /* Fall through */ | |
3287 | case OP_DIGIT: | |
3288 | set2 = (pcre_uint8 *)(cd->cbits + cbit_digit); | |
3289 | break; | |
3290 | ||
3291 | case OP_NOT_WHITESPACE: | |
3292 | invert_bits = TRUE; | |
3293 | /* Fall through */ | |
3294 | case OP_WHITESPACE: | |
3295 | set2 = (pcre_uint8 *)(cd->cbits + cbit_space); | |
3296 | break; | |
3297 | ||
3298 | case OP_NOT_WORDCHAR: | |
3299 | invert_bits = TRUE; | |
3300 | /* Fall through */ | |
3301 | case OP_WORDCHAR: | |
3302 | set2 = (pcre_uint8 *)(cd->cbits + cbit_word); | |
3303 | break; | |
3304 | ||
3305 | default: | |
3306 | return FALSE; | |
3307 | } | |
3308 | ||
3309 | /* Because the sets are unaligned, we need | |
3310 | to perform byte comparison here. */ | |
3311 | set_end = set1 + 32; | |
3312 | if (invert_bits) | |
3313 | { | |
3314 | do | |
3315 | { | |
3316 | if ((*set1++ & ~(*set2++)) != 0) return FALSE; | |
3317 | } | |
3318 | while (set1 < set_end); | |
3319 | } | |
3320 | else | |
3321 | { | |
3322 | do | |
3323 | { | |
3324 | if ((*set1++ & *set2++) != 0) return FALSE; | |
3325 | } | |
3326 | while (set1 < set_end); | |
3327 | } | |
3328 | ||
3329 | if (list[1] == 0) return TRUE; | |
3330 | /* Might be an empty repeat. */ | |
3331 | continue; | |
3332 | } | |
3333 | ||
3334 | /* Some property combinations also acceptable. Unicode property opcodes are | |
3335 | processed specially; the rest can be handled with a lookup table. */ | |
3336 | ||
3337 | else | |
3338 | { | |
3339 | pcre_uint32 leftop, rightop; | |
3340 | ||
3341 | leftop = base_list[0]; | |
3342 | rightop = list[0]; | |
3343 | ||
3344 | #ifdef SUPPORT_UCP | |
3345 | accepted = FALSE; /* Always set in non-unicode case. */ | |
3346 | if (leftop == OP_PROP || leftop == OP_NOTPROP) | |
3347 | { | |
3348 | if (rightop == OP_EOD) | |
3349 | accepted = TRUE; | |
3350 | else if (rightop == OP_PROP || rightop == OP_NOTPROP) | |
3351 | { | |
3352 | int n; | |
3353 | const pcre_uint8 *p; | |
3354 | BOOL same = leftop == rightop; | |
3355 | BOOL lisprop = leftop == OP_PROP; | |
3356 | BOOL risprop = rightop == OP_PROP; | |
3357 | BOOL bothprop = lisprop && risprop; | |
3358 | ||
3359 | /* There's a table that specifies how each combination is to be | |
3360 | processed: | |
3361 | 0 Always return FALSE (never auto-possessify) | |
3362 | 1 Character groups are distinct (possessify if both are OP_PROP) | |
3363 | 2 Check character categories in the same group (general or particular) | |
3364 | 3 Return TRUE if the two opcodes are not the same | |
3365 | ... see comments below | |
3366 | */ | |
3367 | ||
3368 | n = propposstab[base_list[2]][list[2]]; | |
3369 | switch(n) | |
3370 | { | |
3371 | case 0: break; | |
3372 | case 1: accepted = bothprop; break; | |
3373 | case 2: accepted = (base_list[3] == list[3]) != same; break; | |
3374 | case 3: accepted = !same; break; | |
3375 | ||
3376 | case 4: /* Left general category, right particular category */ | |
3377 | accepted = risprop && catposstab[base_list[3]][list[3]] == same; | |
3378 | break; | |
3379 | ||
3380 | case 5: /* Right general category, left particular category */ | |
3381 | accepted = lisprop && catposstab[list[3]][base_list[3]] == same; | |
3382 | break; | |
3383 | ||
3384 | /* This code is logically tricky. Think hard before fiddling with it. | |
3385 | The posspropstab table has four entries per row. Each row relates to | |
3386 | one of PCRE's special properties such as ALNUM or SPACE or WORD. | |
3387 | Only WORD actually needs all four entries, but using repeats for the | |
3388 | others means they can all use the same code below. | |
3389 | ||
3390 | The first two entries in each row are Unicode general categories, and | |
3391 | apply always, because all the characters they include are part of the | |
3392 | PCRE character set. The third and fourth entries are a general and a | |
3393 | particular category, respectively, that include one or more relevant | |
3394 | characters. One or the other is used, depending on whether the check | |
3395 | is for a general or a particular category. However, in both cases the | |
3396 | category contains more characters than the specials that are defined | |
3397 | for the property being tested against. Therefore, it cannot be used | |
3398 | in a NOTPROP case. | |
3399 | ||
3400 | Example: the row for WORD contains ucp_L, ucp_N, ucp_P, ucp_Po. | |
3401 | Underscore is covered by ucp_P or ucp_Po. */ | |
3402 | ||
3403 | case 6: /* Left alphanum vs right general category */ | |
3404 | case 7: /* Left space vs right general category */ | |
3405 | case 8: /* Left word vs right general category */ | |
3406 | p = posspropstab[n-6]; | |
3407 | accepted = risprop && lisprop == | |
3408 | (list[3] != p[0] && | |
3409 | list[3] != p[1] && | |
3410 | (list[3] != p[2] || !lisprop)); | |
3411 | break; | |
3412 | ||
3413 | case 9: /* Right alphanum vs left general category */ | |
3414 | case 10: /* Right space vs left general category */ | |
3415 | case 11: /* Right word vs left general category */ | |
3416 | p = posspropstab[n-9]; | |
3417 | accepted = lisprop && risprop == | |
3418 | (base_list[3] != p[0] && | |
3419 | base_list[3] != p[1] && | |
3420 | (base_list[3] != p[2] || !risprop)); | |
3421 | break; | |
3422 | ||
3423 | case 12: /* Left alphanum vs right particular category */ | |
3424 | case 13: /* Left space vs right particular category */ | |
3425 | case 14: /* Left word vs right particular category */ | |
3426 | p = posspropstab[n-12]; | |
3427 | accepted = risprop && lisprop == | |
3428 | (catposstab[p[0]][list[3]] && | |
3429 | catposstab[p[1]][list[3]] && | |
3430 | (list[3] != p[3] || !lisprop)); | |
3431 | break; | |
3432 | ||
3433 | case 15: /* Right alphanum vs left particular category */ | |
3434 | case 16: /* Right space vs left particular category */ | |
3435 | case 17: /* Right word vs left particular category */ | |
3436 | p = posspropstab[n-15]; | |
3437 | accepted = lisprop && risprop == | |
3438 | (catposstab[p[0]][base_list[3]] && | |
3439 | catposstab[p[1]][base_list[3]] && | |
3440 | (base_list[3] != p[3] || !risprop)); | |
3441 | break; | |
3442 | } | |
3443 | } | |
3444 | } | |
3445 | ||
3446 | else | |
3447 | #endif /* SUPPORT_UCP */ | |
3448 | ||
3449 | accepted = leftop >= FIRST_AUTOTAB_OP && leftop <= LAST_AUTOTAB_LEFT_OP && | |
3450 | rightop >= FIRST_AUTOTAB_OP && rightop <= LAST_AUTOTAB_RIGHT_OP && | |
3451 | autoposstab[leftop - FIRST_AUTOTAB_OP][rightop - FIRST_AUTOTAB_OP]; | |
3452 | ||
3453 | if (!accepted) return FALSE; | |
3454 | ||
3455 | if (list[1] == 0) return TRUE; | |
3456 | /* Might be an empty repeat. */ | |
3457 | continue; | |
3458 | } | |
3459 | ||
3460 | /* Control reaches here only if one of the items is a small character list. | |
3461 | All characters are checked against the other side. */ | |
3462 | ||
3463 | do | |
3464 | { | |
3465 | chr = *chr_ptr; | |
3466 | ||
3467 | switch(list_ptr[0]) | |
3468 | { | |
3469 | case OP_CHAR: | |
3470 | ochr_ptr = list_ptr + 2; | |
3471 | do | |
3472 | { | |
3473 | if (chr == *ochr_ptr) return FALSE; | |
3474 | ochr_ptr++; | |
3475 | } | |
3476 | while(*ochr_ptr != NOTACHAR); | |
3477 | break; | |
3478 | ||
3479 | case OP_NOT: | |
3480 | ochr_ptr = list_ptr + 2; | |
3481 | do | |
3482 | { | |
3483 | if (chr == *ochr_ptr) | |
3484 | break; | |
3485 | ochr_ptr++; | |
3486 | } | |
3487 | while(*ochr_ptr != NOTACHAR); | |
3488 | if (*ochr_ptr == NOTACHAR) return FALSE; /* Not found */ | |
3489 | break; | |
3490 | ||
3491 | /* Note that OP_DIGIT etc. are generated only when PCRE_UCP is *not* | |
3492 | set. When it is set, \d etc. are converted into OP_(NOT_)PROP codes. */ | |
3493 | ||
3494 | case OP_DIGIT: | |
3495 | if (chr < 256 && (cd->ctypes[chr] & ctype_digit) != 0) return FALSE; | |
3496 | break; | |
3497 | ||
3498 | case OP_NOT_DIGIT: | |
3499 | if (chr > 255 || (cd->ctypes[chr] & ctype_digit) == 0) return FALSE; | |
3500 | break; | |
3501 | ||
3502 | case OP_WHITESPACE: | |
3503 | if (chr < 256 && (cd->ctypes[chr] & ctype_space) != 0) return FALSE; | |
3504 | break; | |
3505 | ||
3506 | case OP_NOT_WHITESPACE: | |
3507 | if (chr > 255 || (cd->ctypes[chr] & ctype_space) == 0) return FALSE; | |
3508 | break; | |
3509 | ||
3510 | case OP_WORDCHAR: | |
3511 | if (chr < 255 && (cd->ctypes[chr] & ctype_word) != 0) return FALSE; | |
3512 | break; | |
3513 | ||
3514 | case OP_NOT_WORDCHAR: | |
3515 | if (chr > 255 || (cd->ctypes[chr] & ctype_word) == 0) return FALSE; | |
3516 | break; | |
3517 | ||
3518 | case OP_HSPACE: | |
3519 | switch(chr) | |
3520 | { | |
3521 | HSPACE_CASES: return FALSE; | |
3522 | default: break; | |
3523 | } | |
3524 | break; | |
3525 | ||
3526 | case OP_NOT_HSPACE: | |
3527 | switch(chr) | |
3528 | { | |
3529 | HSPACE_CASES: break; | |
3530 | default: return FALSE; | |
3531 | } | |
3532 | break; | |
3533 | ||
3534 | case OP_ANYNL: | |
3535 | case OP_VSPACE: | |
3536 | switch(chr) | |
3537 | { | |
3538 | VSPACE_CASES: return FALSE; | |
3539 | default: break; | |
3540 | } | |
3541 | break; | |
3542 | ||
3543 | case OP_NOT_VSPACE: | |
3544 | switch(chr) | |
3545 | { | |
3546 | VSPACE_CASES: break; | |
3547 | default: return FALSE; | |
3548 | } | |
3549 | break; | |
3550 | ||
3551 | case OP_DOLL: | |
3552 | case OP_EODN: | |
3553 | switch (chr) | |
3554 | { | |
3555 | case CHAR_CR: | |
3556 | case CHAR_LF: | |
3557 | case CHAR_VT: | |
3558 | case CHAR_FF: | |
3559 | case CHAR_NEL: | |
3560 | #ifndef EBCDIC | |
3561 | case 0x2028: | |
3562 | case 0x2029: | |
3563 | #endif /* Not EBCDIC */ | |
3564 | return FALSE; | |
3565 | } | |
3566 | break; | |
3567 | ||
3568 | case OP_EOD: /* Can always possessify before \z */ | |
3569 | break; | |
3570 | ||
3571 | #ifdef SUPPORT_UCP | |
3572 | case OP_PROP: | |
3573 | case OP_NOTPROP: | |
3574 | if (!check_char_prop(chr, list_ptr[2], list_ptr[3], | |
3575 | list_ptr[0] == OP_NOTPROP)) | |
3576 | return FALSE; | |
3577 | break; | |
3578 | #endif | |
3579 | ||
3580 | case OP_NCLASS: | |
3581 | if (chr > 255) return FALSE; | |
3582 | /* Fall through */ | |
3583 | ||
3584 | case OP_CLASS: | |
3585 | if (chr > 255) break; | |
3586 | class_bitset = (pcre_uint8 *) | |
3587 | ((list_ptr == list ? code : base_end) - list_ptr[2]); | |
3588 | if ((class_bitset[chr >> 3] & (1 << (chr & 7))) != 0) return FALSE; | |
3589 | break; | |
3590 | ||
3591 | #if defined SUPPORT_UTF || !defined COMPILE_PCRE8 | |
3592 | case OP_XCLASS: | |
3593 | if (PRIV(xclass)(chr, (list_ptr == list ? code : base_end) - | |
3594 | list_ptr[2] + LINK_SIZE, utf)) return FALSE; | |
3595 | break; | |
3596 | #endif | |
3597 | ||
3598 | default: | |
3599 | return FALSE; | |
3600 | } | |
3601 | ||
3602 | chr_ptr++; | |
3603 | } | |
3604 | while(*chr_ptr != NOTACHAR); | |
3605 | ||
3606 | /* At least one character must be matched from this opcode. */ | |
3607 | ||
3608 | if (list[1] == 0) return TRUE; | |
3609 | } | } |
3610 | ||
3611 | return FALSE; | /* Control never reaches here. There used to be a fail-save return FALSE; here, |
3612 | but some compilers complain about an unreachable statement. */ | |
3613 | ||
3614 | } | } |
#endif /* SUPPORT_UCP */ | ||
3615 | ||
3616 | ||
3617 | ||
3618 | /************************************************* | /************************************************* |
3619 | * Check if auto-possessifying is possible * | * Scan compiled regex for auto-possession * |
3620 | *************************************************/ | *************************************************/ |
3621 | ||
3622 | /* This function is called for unlimited repeats of certain items, to see | /* Replaces single character iterations with their possessive alternatives |
3623 | whether the next thing could possibly match the repeated item. If not, it makes | if appropriate. This function modifies the compiled opcode! |
sense to automatically possessify the repeated item. | ||
3624 | ||
3625 | Arguments: | Arguments: |
3626 | previous pointer to the repeated opcode | code points to start of the byte code |
3627 | utf TRUE in UTF-8 / UTF-16 / UTF-32 mode | utf TRUE in UTF-8 / UTF-16 / UTF-32 mode |
3628 | ptr next character in pattern | cd static compile data |
options options bits | ||
cd contains pointers to tables etc. | ||
3629 | ||
3630 | Returns: TRUE if possessifying is wanted | Returns: nothing |
3631 | */ | */ |
3632 | ||
3633 | static BOOL | static void |
3634 | check_auto_possessive(const pcre_uchar *previous, BOOL utf, | auto_possessify(pcre_uchar *code, BOOL utf, const compile_data *cd) |
const pcre_uchar *ptr, int options, compile_data *cd) | ||
3635 | { | { |
3636 | pcre_uint32 c = NOTACHAR; | register pcre_uchar c; |
3637 | pcre_uint32 next; | const pcre_uchar *end; |
3638 | int escape; | pcre_uchar *repeat_opcode; |
3639 | pcre_uchar op_code = *previous++; | pcre_uint32 list[8]; |
3640 | int rec_limit; | |
/* Skip whitespace and comments in extended mode */ | ||
3641 | ||
3642 | if ((options & PCRE_EXTENDED) != 0) | for (;;) |
3643 | { | { |
3644 | for (;;) | c = *code; |
3645 | ||
3646 | /* When a pattern with bad UTF-8 encoding is compiled with NO_UTF_CHECK, | |
3647 | it may compile without complaining, but may get into a loop here if the code | |
3648 | pointer points to a bad value. This is, of course a documentated possibility, | |
3649 | when NO_UTF_CHECK is set, so it isn't a bug, but we can detect this case and | |
3650 | just give up on this optimization. */ | |
3651 | ||
3652 | if (c >= OP_TABLE_LENGTH) return; | |
3653 | ||
3654 | if (c >= OP_STAR && c <= OP_TYPEPOSUPTO) | |
3655 | { | { |
3656 | while (MAX_255(*ptr) && (cd->ctypes[*ptr] & ctype_space) != 0) ptr++; | c -= get_repeat_base(c) - OP_STAR; |
3657 | if (*ptr == CHAR_NUMBER_SIGN) | end = (c <= OP_MINUPTO) ? |
3658 | get_chr_property_list(code, utf, cd->fcc, list) : NULL; | |
3659 | list[1] = c == OP_STAR || c == OP_PLUS || c == OP_QUERY || c == OP_UPTO; | |
3660 | ||
3661 | rec_limit = 10000; | |
3662 | if (end != NULL && compare_opcodes(end, utf, cd, list, end, &rec_limit)) | |
3663 | { | { |
3664 | ptr++; | switch(c) |
while (*ptr != CHAR_NULL) | ||
3665 | { | { |
3666 | if (IS_NEWLINE(ptr)) { ptr += cd->nllen; break; } | case OP_STAR: |
3667 | ptr++; | *code += OP_POSSTAR - OP_STAR; |
3668 | #ifdef SUPPORT_UTF | break; |
if (utf) FORWARDCHAR(ptr); | ||
#endif | ||
} | ||
} | ||
else break; | ||
} | ||
} | ||
3669 | ||
3670 | /* If the next item is one that we can handle, get its value. A non-negative | case OP_MINSTAR: |
3671 | value is a character, a negative value is an escape value. */ | *code += OP_POSSTAR - OP_MINSTAR; |
3672 | break; | |
3673 | ||
3674 | if (*ptr == CHAR_BACKSLASH) | case OP_PLUS: |
3675 | { | *code += OP_POSPLUS - OP_PLUS; |
3676 | int temperrorcode = 0; | break; |
escape = check_escape(&ptr, &next, &temperrorcode, cd->bracount, options, | ||
FALSE); | ||
if (temperrorcode != 0) return FALSE; | ||
ptr++; /* Point after the escape sequence */ | ||
} | ||
else if (!MAX_255(*ptr) || (cd->ctypes[*ptr] & ctype_meta) == 0) | ||
{ | ||
escape = 0; | ||
#ifdef SUPPORT_UTF | ||
if (utf) { GETCHARINC(next, ptr); } else | ||
#endif | ||
next = *ptr++; | ||
} | ||
else return FALSE; | ||
3677 | ||
3678 | /* Skip whitespace and comments in extended mode */ | case OP_MINPLUS: |
3679 | *code += OP_POSPLUS - OP_MINPLUS; | |
3680 | break; | |
3681 | ||
3682 | if ((options & PCRE_EXTENDED) != 0) | case OP_QUERY: |
3683 | { | *code += OP_POSQUERY - OP_QUERY; |
3684 | for (;;) | break; |
3685 | ||
3686 | case OP_MINQUERY: | |
3687 | *code += OP_POSQUERY - OP_MINQUERY; | |
3688 | break; | |
3689 | ||
3690 | case OP_UPTO: | |
3691 | *code += OP_POSUPTO - OP_UPTO; | |
3692 | break; | |
3693 | ||
3694 | case OP_MINUPTO: | |
3695 | *code += OP_POSUPTO - OP_MINUPTO; | |
3696 | break; | |
3697 | } | |
3698 | } | |
3699 | c = *code; | |
3700 | } | |
3701 | else if (c == OP_CLASS || c == OP_NCLASS || c == OP_XCLASS) | |
3702 | { | { |
3703 | while (MAX_255(*ptr) && (cd->ctypes[*ptr] & ctype_space) != 0) ptr++; | #if defined SUPPORT_UTF || !defined COMPILE_PCRE8 |
3704 | if (*ptr == CHAR_NUMBER_SIGN) | if (c == OP_XCLASS) |
3705 | repeat_opcode = code + GET(code, 1); | |
3706 | else | |
3707 | #endif | |
3708 | repeat_opcode = code + 1 + (32 / sizeof(pcre_uchar)); | |
3709 | ||
3710 | c = *repeat_opcode; | |
3711 | if (c >= OP_CRSTAR && c <= OP_CRMINRANGE) | |
3712 | { | { |
3713 | ptr++; | /* end must not be NULL. */ |
3714 | while (*ptr != CHAR_NULL) | end = get_chr_property_list(code, utf, cd->fcc, list); |
3715 | ||
3716 | list[1] = (c & 1) == 0; | |
3717 | ||
3718 | rec_limit = 10000; | |
3719 | if (compare_opcodes(end, utf, cd, list, end, &rec_limit)) | |
3720 | { | { |
3721 | if (IS_NEWLINE(ptr)) { ptr += cd->nllen; break; } | switch (c) |
3722 | ptr++; | { |
3723 | #ifdef SUPPORT_UTF | case OP_CRSTAR: |
3724 | if (utf) FORWARDCHAR(ptr); | case OP_CRMINSTAR: |
3725 | #endif | *repeat_opcode = OP_CRPOSSTAR; |
3726 | break; | |
3727 | ||
3728 | case OP_CRPLUS: | |
3729 | case OP_CRMINPLUS: | |
3730 | *repeat_opcode = OP_CRPOSPLUS; | |
3731 | break; | |
3732 | ||
3733 | case OP_CRQUERY: | |
3734 | case OP_CRMINQUERY: | |
3735 | *repeat_opcode = OP_CRPOSQUERY; | |
3736 | break; | |
3737 | ||
3738 | case OP_CRRANGE: | |
3739 | case OP_CRMINRANGE: | |
3740 | *repeat_opcode = OP_CRPOSRANGE; | |
3741 | break; | |
3742 | } | |
3743 | } | } |
3744 | } | } |
3745 | else break; | c = *code; |
3746 | } | } |
} | ||
3747 | ||
3748 | /* If the next thing is itself optional, we have to give up. */ | switch(c) |
3749 | { | |
3750 | case OP_END: | |
3751 | return; | |
3752 | ||
3753 | if (*ptr == CHAR_ASTERISK || *ptr == CHAR_QUESTION_MARK || | case OP_TYPESTAR: |
3754 | STRNCMP_UC_C8(ptr, STR_LEFT_CURLY_BRACKET STR_0 STR_COMMA, 3) == 0) | case OP_TYPEMINSTAR: |
3755 | return FALSE; | case OP_TYPEPLUS: |
3756 | case OP_TYPEMINPLUS: | |
3757 | case OP_TYPEQUERY: | |
3758 | case OP_TYPEMINQUERY: | |
3759 | case OP_TYPEPOSSTAR: | |
3760 | case OP_TYPEPOSPLUS: | |
3761 | case OP_TYPEPOSQUERY: | |
3762 | if (code[1] == OP_PROP || code[1] == OP_NOTPROP) code += 2; | |
3763 | break; | |
3764 | ||
3765 | /* If the previous item is a character, get its value. */ | case OP_TYPEUPTO: |
3766 | case OP_TYPEMINUPTO: | |
3767 | case OP_TYPEEXACT: | |
3768 | case OP_TYPEPOSUPTO: | |
3769 | if (code[1 + IMM2_SIZE] == OP_PROP || code[1 + IMM2_SIZE] == OP_NOTPROP) | |
3770 | code += 2; | |
3771 | break; | |
3772 | ||
3773 | if (op_code == OP_CHAR || op_code == OP_CHARI || | #if defined SUPPORT_UTF || !defined COMPILE_PCRE8 |
3774 | op_code == OP_NOT || op_code == OP_NOTI) | case OP_XCLASS: |
3775 | { | code += GET(code, 1); |
3776 | #ifdef SUPPORT_UTF | break; |
GETCHARTEST(c, previous); | ||
#else | ||
c = *previous; | ||
3777 | #endif | #endif |
} | ||
3778 | ||
3779 | /* Now compare the next item with the previous opcode. First, handle cases when | case OP_MARK: |
3780 | the next item is a character. */ | case OP_PRUNE_ARG: |
3781 | case OP_SKIP_ARG: | |
3782 | case OP_THEN_ARG: | |
3783 | code += code[1]; | |
3784 | break; | |
3785 | } | |
3786 | ||
3787 | if (escape == 0) | /* Add in the fixed length from the table */ |
{ | ||
/* For a caseless UTF match, the next character may have more than one other | ||
case, which maps to the special PT_CLIST property. Check this first. */ | ||
3788 | ||
3789 | #ifdef SUPPORT_UCP | code += PRIV(OP_lengths)[c]; |
if (utf && c != NOTACHAR && (options & PCRE_CASELESS) != 0) | ||
{ | ||
unsigned int ocs = UCD_CASESET(next); | ||
if (ocs > 0) return check_char_prop(c, PT_CLIST, ocs, op_code >= OP_NOT); | ||
} | ||
#endif | ||
3790 | ||
3791 | switch(op_code) | /* In UTF-8 mode, opcodes that are followed by a character may be followed by |
3792 | a multi-byte character. The length in the table is a minimum, so we have to | |
3793 | arrange to skip the extra bytes. */ | |
3794 | ||
3795 | #if defined SUPPORT_UTF && !defined COMPILE_PCRE32 | |
3796 | if (utf) switch(c) | |
3797 | { | { |
3798 | case OP_CHAR: | case OP_CHAR: |
return c != next; | ||
/* For CHARI (caseless character) we must check the other case. If we have | ||
Unicode property support, we can use it to test the other case of | ||
high-valued characters. We know that next can have only one other case, | ||
because multi-other-case characters are dealt with above. */ | ||
3799 | case OP_CHARI: | case OP_CHARI: |
3800 | if (c == next) return FALSE; | case OP_NOT: |
3801 | #ifdef SUPPORT_UTF | case OP_NOTI: |
3802 | if (utf) | case OP_STAR: |
3803 | { | case OP_MINSTAR: |
3804 | pcre_uint32 othercase; | case OP_PLUS: |
3805 | if (next < 128) othercase = cd->fcc[next]; else | case OP_MINPLUS: |
3806 | #ifdef SUPPORT_UCP | case OP_QUERY: |
3807 | othercase = UCD_OTHERCASE(next); | case OP_MINQUERY: |
3808 | case OP_UPTO: | |
3809 | case OP_MINUPTO: | |
3810 | case OP_EXACT: | |
3811 | case OP_POSSTAR: | |
3812 | case OP_POSPLUS: | |
3813 | case OP_POSQUERY: | |
3814 | case OP_POSUPTO: | |
3815 | case OP_STARI: | |
3816 | case OP_MINSTARI: | |
3817 | case OP_PLUSI: | |
3818 | case OP_MINPLUSI: | |
3819 | case OP_QUERYI: | |
3820 | case OP_MINQUERYI: | |
3821 | case OP_UPTOI: | |
3822 | case OP_MINUPTOI: | |
3823 | case OP_EXACTI: | |
3824 | case OP_POSSTARI: | |
3825 | case OP_POSPLUSI: | |
3826 | case OP_POSQUERYI: | |
3827 | case OP_POSUPTOI: | |
3828 | case OP_NOTSTAR: | |
3829 | case OP_NOTMINSTAR: | |
3830 | case OP_NOTPLUS: | |
3831 | case OP_NOTMINPLUS: | |
3832 | case OP_NOTQUERY: | |
3833 | case OP_NOTMINQUERY: | |
3834 | case OP_NOTUPTO: | |
3835 | case OP_NOTMINUPTO: | |
3836 | case OP_NOTEXACT: | |
3837 | case OP_NOTPOSSTAR: | |
3838 | case OP_NOTPOSPLUS: | |
3839 | case OP_NOTPOSQUERY: | |
3840 | case OP_NOTPOSUPTO: | |
3841 | case OP_NOTSTARI: | |
3842 | case OP_NOTMINSTARI: | |
3843 | case OP_NOTPLUSI: | |
3844 | case OP_NOTMINPLUSI: | |
3845 | case OP_NOTQUERYI: | |
3846 | case OP_NOTMINQUERYI: | |
3847 | case OP_NOTUPTOI: | |
3848 | case OP_NOTMINUPTOI: | |
3849 | case OP_NOTEXACTI: | |
3850 | case OP_NOTPOSSTARI: | |
3851 | case OP_NOTPOSPLUSI: | |
3852 | case OP_NOTPOSQUERYI: | |
3853 | case OP_NOTPOSUPTOI: | |
3854 | if (HAS_EXTRALEN(code[-1])) code += GET_EXTRALEN(code[-1]); | |
3855 | break; | |
3856 | } | |
3857 | #else | #else |
3858 | othercase = NOTACHAR; | (void)(utf); /* Keep compiler happy by referencing function argument */ |
3859 | #endif | #endif |
3860 | return c != othercase; | } |
3861 | } | } |
else | ||
#endif /* SUPPORT_UTF */ | ||
return (c != TABLE_GET(next, cd->fcc, next)); /* Not UTF */ | ||
3862 | ||
case OP_NOT: | ||
return c == next; | ||
3863 | ||
3864 | case OP_NOTI: | |
3865 | if (c == next) return TRUE; | /************************************************* |
3866 | #ifdef SUPPORT_UTF | * Check for POSIX class syntax * |
3867 | if (utf) | *************************************************/ |
3868 | ||
3869 | /* This function is called when the sequence "[:" or "[." or "[=" is | |
3870 | encountered in a character class. It checks whether this is followed by a | |
3871 | sequence of characters terminated by a matching ":]" or ".]" or "=]". If we | |
3872 | reach an unescaped ']' without the special preceding character, return FALSE. | |
3873 | ||
3874 | Originally, this function only recognized a sequence of letters between the | |
3875 | terminators, but it seems that Perl recognizes any sequence of characters, | |
3876 | though of course unknown POSIX names are subsequently rejected. Perl gives an | |
3877 | "Unknown POSIX class" error for [:f\oo:] for example, where previously PCRE | |
3878 | didn't consider this to be a POSIX class. Likewise for [:1234:]. | |
3879 | ||
3880 | The problem in trying to be exactly like Perl is in the handling of escapes. We | |
3881 | have to be sure that [abc[:x\]pqr] is *not* treated as containing a POSIX | |
3882 | class, but [abc[:x\]pqr:]] is (so that an error can be generated). The code | |
3883 | below handles the special case of \], but does not try to do any other escape | |
3884 | processing. This makes it different from Perl for cases such as [:l\ower:] | |
3885 | where Perl recognizes it as the POSIX class "lower" but PCRE does not recognize | |
3886 | "l\ower". This is a lesser evil than not diagnosing bad classes when Perl does, | |
3887 | I think. | |
3888 | ||
3889 | A user pointed out that PCRE was rejecting [:a[:digit:]] whereas Perl was not. | |
3890 | It seems that the appearance of a nested POSIX class supersedes an apparent | |
3891 | external class. For example, [:a[:digit:]b:] matches "a", "b", ":", or | |
3892 | a digit. | |
3893 | ||
3894 | In Perl, unescaped square brackets may also appear as part of class names. For | |
3895 | example, [:a[:abc]b:] gives unknown POSIX class "[:abc]b:]". However, for | |
3896 | [:a[:abc]b][b:] it gives unknown POSIX class "[:abc]b][b:]", which does not | |
3897 | seem right at all. PCRE does not allow closing square brackets in POSIX class | |
3898 | names. | |
3899 | ||
3900 | Arguments: | |
3901 | ptr pointer to the initial [ | |
3902 | endptr where to return the end pointer | |
3903 | ||
3904 | Returns: TRUE or FALSE | |
3905 | */ | |
3906 | ||
3907 | static BOOL | |
3908 | check_posix_syntax(const pcre_uchar *ptr, const pcre_uchar **endptr) | |
3909 | { | |
3910 | pcre_uchar terminator; /* Don't combine these lines; the Solaris cc */ | |
3911 | terminator = *(++ptr); /* compiler warns about "non-constant" initializer. */ | |
3912 | for (++ptr; *ptr != CHAR_NULL; ptr++) | |
3913 | { | |
3914 | if (*ptr == CHAR_BACKSLASH && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET) | |
3915 | ptr++; | |
3916 | else if (*ptr == CHAR_RIGHT_SQUARE_BRACKET) return FALSE; | |
3917 | else | |
3918 | { | |
3919 | if (*ptr == terminator && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET) | |
3920 | { | { |
3921 | pcre_uint32 othercase; | *endptr = ptr; |
3922 | if (next < 128) othercase = cd->fcc[next]; else | return TRUE; |
#ifdef SUPPORT_UCP | ||
othercase = UCD_OTHERCASE(next); | ||
#else | ||
othercase = NOTACHAR; | ||
#endif | ||
return c == othercase; | ||
3923 | } | } |
3924 | else | if (*ptr == CHAR_LEFT_SQUARE_BRACKET && |
3925 | #endif /* SUPPORT_UTF */ | (ptr[1] == CHAR_COLON || ptr[1] == CHAR_DOT || |
3926 | return (c == TABLE_GET(next, cd->fcc, next)); /* Not UTF */ | ptr[1] == CHAR_EQUALS_SIGN) && |
3927 | check_posix_syntax(ptr, endptr)) | |
3928 | return FALSE; | |
3929 | } | |
3930 | } | |
3931 | return FALSE; | |
3932 | } | |
3933 | ||
/* Note that OP_DIGIT etc. are generated only when PCRE_UCP is *not* set. | ||
When it is set, \d etc. are converted into OP_(NOT_)PROP codes. */ | ||
3934 | ||
case OP_DIGIT: | ||
return next > 255 || (cd->ctypes[next] & ctype_digit) == 0; | ||
3935 | ||
case OP_NOT_DIGIT: | ||
return next <= 255 && (cd->ctypes[next] & ctype_digit) != 0; | ||
3936 | ||
3937 | case OP_WHITESPACE: | /************************************************* |
3938 | return next > 255 || (cd->ctypes[next] & ctype_space) == 0; | * Check POSIX class name * |
3939 | *************************************************/ | |
3940 | ||
3941 | case OP_NOT_WHITESPACE: | /* This function is called to check the name given in a POSIX-style class entry |
3942 | return next <= 255 && (cd->ctypes[next] & ctype_space) != 0; | such as [:alnum:]. |
3943 | ||
3944 | case OP_WORDCHAR: | Arguments: |
3945 | return next > 255 || (cd->ctypes[next] & ctype_word) == 0; | ptr points to the first letter |
3946 | len the length of the name | |
3947 | ||
3948 | case OP_NOT_WORDCHAR: | Returns: a value representing the name, or -1 if unknown |
3949 | return next <= 255 && (cd->ctypes[next] & ctype_word) != 0; | */ |
3950 | ||
3951 | case OP_HSPACE: | static int |
3952 | case OP_NOT_HSPACE: | check_posix_name(const pcre_uchar *ptr, int len) |
3953 | switch(next) | { |
3954 | { | const char *pn = posix_names; |
3955 | HSPACE_CASES: | register int yield = 0; |
3956 | return op_code == OP_NOT_HSPACE; | while (posix_name_lengths[yield] != 0) |
3957 | { | |
3958 | if (len == posix_name_lengths[yield] && | |
3959 | STRNCMP_UC_C8(ptr, pn, (unsigned int)len) == 0) return yield; | |
3960 | pn += posix_name_lengths[yield] + 1; | |
3961 | yield++; | |
3962 | } | |
3963 | return -1; | |
3964 | } | |
3965 | ||
default: | ||
return op_code != OP_NOT_HSPACE; | ||
} | ||
3966 | ||
3967 | case OP_ANYNL: | /************************************************* |
3968 | case OP_VSPACE: | * Adjust OP_RECURSE items in repeated group * |
3969 | case OP_NOT_VSPACE: | *************************************************/ |
switch(next) | ||
{ | ||
VSPACE_CASES: | ||
return op_code == OP_NOT_VSPACE; | ||
3970 | ||
3971 | default: | /* OP_RECURSE items contain an offset from the start of the regex to the group |
3972 | return op_code != OP_NOT_VSPACE; | that is referenced. This means that groups can be replicated for fixed |
3973 | } | repetition simply by copying (because the recursion is allowed to refer to |
3974 | earlier groups that are outside the current group). However, when a group is | |
3975 | optional (i.e. the minimum quantifier is zero), OP_BRAZERO or OP_SKIPZERO is | |
3976 | inserted before it, after it has been compiled. This means that any OP_RECURSE | |
3977 | items within it that refer to the group itself or any contained groups have to | |
3978 | have their offsets adjusted. That one of the jobs of this function. Before it | |
3979 | is called, the partially compiled regex must be temporarily terminated with | |
3980 | OP_END. | |
3981 | ||
3982 | #ifdef SUPPORT_UCP | This function has been extended with the possibility of forward references for |
3983 | case OP_PROP: | recursions and subroutine calls. It must also check the list of such references |
3984 | return check_char_prop(next, previous[0], previous[1], FALSE); | for the group we are dealing with. If it finds that one of the recursions in |
3985 | the current group is on this list, it adjusts the offset in the list, not the | |
3986 | value in the reference (which is a group number). | |
3987 | ||
3988 | case OP_NOTPROP: | Arguments: |
3989 | return check_char_prop(next, previous[0], previous[1], TRUE); | group points to the start of the group |
3990 | #endif | adjust the amount by which the group is to be moved |
3991 | utf TRUE in UTF-8 / UTF-16 / UTF-32 mode | |
3992 | cd contains pointers to tables etc. | |
3993 | save_hwm_offset the hwm forward reference offset at the start of the group | |
3994 | ||
3995 | default: | Returns: nothing |
3996 | return FALSE; | */ |
} | ||
} | ||
3997 | ||
3998 | /* Handle the case when the next item is \d, \s, etc. Note that when PCRE_UCP | static void |
3999 | is set, \d turns into ESC_du rather than ESC_d, etc., so ESC_d etc. are | adjust_recurse(pcre_uchar *group, int adjust, BOOL utf, compile_data *cd, |
4000 | generated only when PCRE_UCP is *not* set, that is, when only ASCII | size_t save_hwm_offset) |
4001 | characteristics are recognized. Similarly, the opcodes OP_DIGIT etc. are | { |
4002 | replaced by OP_PROP codes when PCRE_UCP is set. */ | pcre_uchar *ptr = group; |
4003 | ||
4004 | switch(op_code) | while ((ptr = (pcre_uchar *)find_recurse(ptr, utf)) != NULL) |
4005 | { | { |
4006 | case OP_CHAR: | int offset; |
4007 | case OP_CHARI: | pcre_uchar *hc; |
4008 | switch(escape) | |
4009 | /* See if this recursion is on the forward reference list. If so, adjust the | |
4010 | reference. */ | |
4011 | ||
4012 | for (hc = (pcre_uchar *)cd->start_workspace + save_hwm_offset; hc < cd->hwm; | |
4013 | hc += LINK_SIZE) | |
4014 | { | { |
4015 | case ESC_d: | offset = (int)GET(hc, 0); |
4016 | return c > 255 || (cd->ctypes[c] & ctype_digit) == 0; | if (cd->start_code + offset == ptr + 1) |
4017 | { | |
4018 | PUT(hc, 0, offset + adjust); | |
4019 | break; | |
4020 | } | |
4021 | } | |
4022 | ||
4023 | case ESC_D: | /* Otherwise, adjust the recursion offset if it's after the start of this |
4024 | return c <= 255 && (cd->ctypes[c] & ctype_digit) != 0; | group. */ |
4025 | ||
4026 | case ESC_s: | if (hc >= cd->hwm) |
4027 | return c > 255 || (cd->ctypes[c] & ctype_space) == 0; | { |
4028 | offset = (int)GET(ptr, 1); | |
4029 | if (cd->start_code + offset >= group) PUT(ptr, 1, offset + adjust); | |
4030 | } | |
4031 | ||
4032 | case ESC_S: | ptr += 1 + LINK_SIZE; |
4033 | return c <= 255 && (cd->ctypes[c] & ctype_space) != 0; | } |
4034 | } | |
4035 | ||
case ESC_w: | ||
return c > 255 || (cd->ctypes[c] & ctype_word) == 0; | ||
4036 | ||
case ESC_W: | ||
return c <= 255 && (cd->ctypes[c] & ctype_word) != 0; | ||
4037 | ||
4038 | case ESC_h: | /************************************************* |
4039 | case ESC_H: | * Insert an automatic callout point * |
4040 | switch(c) | *************************************************/ |
{ | ||
HSPACE_CASES: | ||
return escape != ESC_h; | ||
4041 | ||
4042 | default: | /* This function is called when the PCRE_AUTO_CALLOUT option is set, to insert |
4043 | return escape == ESC_h; | callout points before each pattern item. |
} | ||
4044 | ||
4045 | case ESC_v: | Arguments: |
4046 | case ESC_V: | code current code pointer |
4047 | switch(c) | ptr current pattern pointer |
4048 | { | cd pointers to tables etc |
VSPACE_CASES: | ||
return escape != ESC_v; | ||
4049 | ||
4050 | default: | Returns: new code pointer |
4051 | return escape == ESC_v; | */ |
} | ||
4052 | ||
4053 | /* When PCRE_UCP is set, these values get generated for \d etc. Find | static pcre_uchar * |
4054 | their substitutions and process them. The result will always be either | auto_callout(pcre_uchar *code, const pcre_uchar *ptr, compile_data *cd) |
4055 | ESC_p or ESC_P. Then fall through to process those values. */ | { |
4056 | *code++ = OP_CALLOUT; | |
4057 | *code++ = 255; | |
4058 | PUT(code, 0, (int)(ptr - cd->start_pattern)); /* Pattern offset */ | |
4059 | PUT(code, LINK_SIZE, 0); /* Default length */ | |
4060 | return code + 2 * LINK_SIZE; | |
4061 | } | |
4062 | ||
#ifdef SUPPORT_UCP | ||
case ESC_du: | ||
case ESC_DU: | ||
case ESC_wu: | ||
case ESC_WU: | ||
case ESC_su: | ||
case ESC_SU: | ||
{ | ||
int temperrorcode = 0; | ||
ptr = substitutes[escape - ESC_DU]; | ||
escape = check_escape(&ptr, &next, &temperrorcode, 0, options, FALSE); | ||
if (temperrorcode != 0) return FALSE; | ||
ptr++; /* For compatibility */ | ||
} | ||
/* Fall through */ | ||
4063 | ||
case ESC_p: | ||
case ESC_P: | ||
{ | ||
unsigned int ptype = 0, pdata = 0; | ||
int errorcodeptr; | ||
BOOL negated; | ||
4064 | ||
4065 | ptr--; /* Make ptr point at the p or P */ | /************************************************* |
4066 | if (!get_ucp(&ptr, &negated, &ptype, &pdata, &errorcodeptr)) | * Complete a callout item * |
4067 | return FALSE; | *************************************************/ |
ptr++; /* Point past the final curly ket */ | ||
4068 | ||
4069 | /* If the property item is optional, we have to give up. (When generated | /* A callout item contains the length of the next item in the pattern, which |
4070 | from \d etc by PCRE_UCP, this test will have been applied much earlier, | we can't fill in till after we have reached the relevant point. This is used |
4071 | to the original \d etc. At this point, ptr will point to a zero byte. */ | for both automatic and manual callouts. |
4072 | ||
4073 | if (*ptr == CHAR_ASTERISK || *ptr == CHAR_QUESTION_MARK || | Arguments: |
4074 | STRNCMP_UC_C8(ptr, STR_LEFT_CURLY_BRACKET STR_0 STR_COMMA, 3) == 0) | previous_callout points to previous callout item |
4075 | return FALSE; | ptr current pattern pointer |
4076 | cd pointers to tables etc | |
4077 | ||
4078 | /* Do the property check. */ | Returns: nothing |
4079 | */ | |
4080 | ||
4081 | return check_char_prop(c, ptype, pdata, (escape == ESC_P) != negated); | static void |
4082 | } | complete_callout(pcre_uchar *previous_callout, const pcre_uchar *ptr, compile_data *cd) |
4083 | #endif | { |
4084 | int length = (int)(ptr - cd->start_pattern - GET(previous_callout, 2)); | |
4085 | PUT(previous_callout, 2 + LINK_SIZE, length); | |
4086 | } | |
4087 | ||
default: | ||
return FALSE; | ||
} | ||
4088 | ||
/* In principle, support for Unicode properties should be integrated here as | ||
well. It means re-organizing the above code so as to get hold of the property | ||
values before switching on the op-code. However, I wonder how many patterns | ||
combine ASCII \d etc with Unicode properties? (Note that if PCRE_UCP is set, | ||
these op-codes are never generated.) */ | ||
4089 | ||
4090 | case OP_DIGIT: | #ifdef SUPPORT_UCP |
4091 | return escape == ESC_D || escape == ESC_s || escape == ESC_W || | /************************************************* |
4092 | escape == ESC_h || escape == ESC_v || escape == ESC_R; | * Get othercase range * |
4093 | *************************************************/ | |
4094 | ||
4095 | case OP_NOT_DIGIT: | /* This function is passed the start and end of a class range, in UTF-8 mode |
4096 | return escape == ESC_d; | with UCP support. It searches up the characters, looking for ranges of |
4097 | characters in the "other" case. Each call returns the next one, updating the | |
4098 | start address. A character with multiple other cases is returned on its own | |
4099 | with a special return value. | |
4100 | ||
4101 | case OP_WHITESPACE: | Arguments: |
4102 | return escape == ESC_S || escape == ESC_d || escape == ESC_w; | cptr points to starting character value; updated |
4103 | d end value | |
4104 | ocptr where to put start of othercase range | |
4105 | odptr where to put end of othercase range | |
4106 | ||
4107 | case OP_NOT_WHITESPACE: | Yield: -1 when no more |
4108 | return escape == ESC_s || escape == ESC_h || escape == ESC_v || escape == ESC_R; | 0 when a range is returned |
4109 | >0 the CASESET offset for char with multiple other cases | |
4110 | in this case, ocptr contains the original | |
4111 | */ | |
4112 | ||
4113 | case OP_HSPACE: | static int |
4114 | return escape == ESC_S || escape == ESC_H || escape == ESC_d || | get_othercase_range(pcre_uint32 *cptr, pcre_uint32 d, pcre_uint32 *ocptr, |
4115 | escape == ESC_w || escape == ESC_v || escape == ESC_R; | pcre_uint32 *odptr) |
4116 | { | |
4117 | pcre_uint32 c, othercase, next; | |
4118 | unsigned int co; | |
4119 | ||
4120 | case OP_NOT_HSPACE: | /* Find the first character that has an other case. If it has multiple other |
4121 | return escape == ESC_h; | cases, return its case offset value. */ |
4122 | ||
4123 | /* Can't have \S in here because VT matches \S (Perl anomaly) */ | for (c = *cptr; c <= d; c++) |
4124 | case OP_ANYNL: | { |
4125 | case OP_VSPACE: | if ((co = UCD_CASESET(c)) != 0) |
4126 | return escape == ESC_V || escape == ESC_d || escape == ESC_w; | { |
4127 | *ocptr = c++; /* Character that has the set */ | |
4128 | *cptr = c; /* Rest of input range */ | |
4129 | return (int)co; | |
4130 | } | |
4131 | if ((othercase = UCD_OTHERCASE(c)) != c) break; | |
4132 | } | |
4133 | ||
4134 | case OP_NOT_VSPACE: | if (c > d) return -1; /* Reached end of range */ |
return escape == ESC_v || escape == ESC_R; | ||
4135 | ||
4136 | case OP_WORDCHAR: | /* Found a character that has a single other case. Search for the end of the |
4137 | return escape == ESC_W || escape == ESC_s || escape == ESC_h || | range, which is either the end of the input range, or a character that has zero |
4138 | escape == ESC_v || escape == ESC_R; | or more than one other cases. */ |
4139 | ||
4140 | case OP_NOT_WORDCHAR: | *ocptr = othercase; |
4141 | return escape == ESC_w || escape == ESC_d; | next = othercase + 1; |
4142 | ||
4143 | default: | for (++c; c <= d; c++) |
4144 | return FALSE; | { |
4145 | if ((co = UCD_CASESET(c)) != 0 || UCD_OTHERCASE(c) != next) break; | |
4146 | next++; | |
4147 | } | } |
4148 | ||
4149 | /* Control does not reach here */ | *odptr = next - 1; /* End of othercase range */ |
4150 | *cptr = c; /* Rest of input range */ | |
4151 | return 0; | |
4152 | } | } |
4153 | #endif /* SUPPORT_UCP */ | |
4154 | ||
4155 | ||
4156 | ||
# | Line 3240 add_to_class(pcre_uint8 *classbits, pcre | Line 4180 add_to_class(pcre_uint8 *classbits, pcre |
4180 | compile_data *cd, pcre_uint32 start, pcre_uint32 end) | compile_data *cd, pcre_uint32 start, pcre_uint32 end) |
4181 | { | { |
4182 | pcre_uint32 c; | pcre_uint32 c; |
4183 | pcre_uint32 classbits_end = (end <= 0xff ? end : 0xff); | |
4184 | int n8 = 0; | int n8 = 0; |
4185 | ||
4186 | /* If caseless matching is required, scan the range and process alternate | /* If caseless matching is required, scan the range and process alternate |
# | Line 3274 if ((options & PCRE_CASELESS) != 0) | Line 4215 if ((options & PCRE_CASELESS) != 0) |
4215 | range. Otherwise, use a recursive call to add the additional range. */ | range. Otherwise, use a recursive call to add the additional range. */ |
4216 | ||
4217 | else if (oc < start && od >= start - 1) start = oc; /* Extend downwards */ | else if (oc < start && od >= start - 1) start = oc; /* Extend downwards */ |
4218 | else if (od > end && oc <= end + 1) end = od; /* Extend upwards */ | else if (od > end && oc <= end + 1) |
4219 | { | |
4220 | end = od; /* Extend upwards */ | |
4221 | if (end > classbits_end) classbits_end = (end <= 0xff ? end : 0xff); | |
4222 | } | |
4223 | else n8 += add_to_class(classbits, uchardptr, options, cd, oc, od); | else n8 += add_to_class(classbits, uchardptr, options, cd, oc, od); |
4224 | } | } |
4225 | } | } |
# | Line 3283 if ((options & PCRE_CASELESS) != 0) | Line 4228 if ((options & PCRE_CASELESS) != 0) |
4228 | ||
4229 | /* Not UTF-mode, or no UCP */ | /* Not UTF-mode, or no UCP */ |
4230 | ||
4231 | for (c = start; c <= end && c < 256; c++) | for (c = start; c <= classbits_end; c++) |
4232 | { | { |
4233 | SETBIT(classbits, cd->fcc[c]); | SETBIT(classbits, cd->fcc[c]); |
4234 | n8++; | n8++; |
# | Line 3308 in all cases. */ | Line 4253 in all cases. */ |
4253 | ||
4254 | #endif /* COMPILE_PCRE[8|16] */ | #endif /* COMPILE_PCRE[8|16] */ |
4255 | ||
4256 | /* If all characters are less than 256, use the bit map. Otherwise use extra | /* Use the bitmap for characters < 256. Otherwise use extra data.*/ |
data. */ | ||
4257 | ||
4258 | if (end < 0x100) | for (c = start; c <= classbits_end; c++) |
4259 | { | { |
4260 | for (c = start; c <= end; c++) | /* Regardless of start, c will always be <= 255. */ |
4261 | { | SETBIT(classbits, c); |
4262 | n8++; | n8++; |
SETBIT(classbits, c); | ||
} | ||
4263 | } | } |
4264 | ||
4265 | else | #if defined SUPPORT_UTF || !defined COMPILE_PCRE8 |
4266 | if (start <= 0xff) start = 0xff + 1; | |
4267 | ||
4268 | if (end >= start) | |
4269 | { | { |
4270 | pcre_uchar *uchardata = *uchardptr; | pcre_uchar *uchardata = *uchardptr; |
4271 | #ifdef SUPPORT_UTF | #ifdef SUPPORT_UTF |
4272 | if ((options & PCRE_UTF8) != 0) /* All UTFs use the same flag bit */ | if ((options & PCRE_UTF8) != 0) /* All UTFs use the same flag bit */ |
4273 | { | { |
# | Line 3363 else | Line 4307 else |
4307 | ||
4308 | *uchardptr = uchardata; /* Updata extra data pointer */ | *uchardptr = uchardata; /* Updata extra data pointer */ |
4309 | } | } |
4310 | #endif /* SUPPORT_UTF || !COMPILE_PCRE8 */ | |
4311 | ||
4312 | return n8; /* Number of 8-bit characters */ | return n8; /* Number of 8-bit characters */ |
4313 | } | } |
# | Line 3514 const pcre_uchar *tempptr; | Line 4459 const pcre_uchar *tempptr; |
4459 | const pcre_uchar *nestptr = NULL; | const pcre_uchar *nestptr = NULL; |
4460 | pcre_uchar *previous = NULL; | pcre_uchar *previous = NULL; |
4461 | pcre_uchar *previous_callout = NULL; | pcre_uchar *previous_callout = NULL; |
4462 | pcre_uchar *save_hwm = NULL; | size_t save_hwm_offset = 0; |
4463 | pcre_uint8 classbits[32]; | pcre_uint8 classbits[32]; |
4464 | ||
4465 | /* We can fish out the UTF-8 setting once and for all into a BOOL, but we | /* We can fish out the UTF-8 setting once and for all into a BOOL, but we |
# | Line 3584 for (;; ptr++) | Line 4529 for (;; ptr++) |
4529 | BOOL reset_bracount; | BOOL reset_bracount; |
4530 | int class_has_8bitchar; | int class_has_8bitchar; |
4531 | int class_one_char; | int class_one_char; |
4532 | #if defined SUPPORT_UTF || !defined COMPILE_PCRE8 | |
4533 | BOOL xclass_has_prop; | |
4534 | #endif | |
4535 | int newoptions; | int newoptions; |
4536 | int recno; | int recno; |
4537 | int refsign; | int refsign; |
# | Line 3701 for (;; ptr++) | Line 4649 for (;; ptr++) |
4649 | } | } |
4650 | goto NORMAL_CHAR; | goto NORMAL_CHAR; |
4651 | } | } |
4652 | /* Control does not reach here. */ | |
4653 | } | } |
4654 | ||
4655 | /* Fill in length of a previous callout, except when the next thing is | /* In extended mode, skip white space and comments. We need a loop in order |
4656 | a quantifier. */ | to check for more white space and more comments after a comment. */ |
is_quantifier = | ||
c == CHAR_ASTERISK || c == CHAR_PLUS || c == CHAR_QUESTION_MARK || | ||
(c == CHAR_LEFT_CURLY_BRACKET && is_counted_repeat(ptr+1)); | ||
if (!is_quantifier && previous_callout != NULL && | ||
after_manual_callout-- <= 0) | ||
{ | ||
if (lengthptr == NULL) /* Don't attempt in pre-compile phase */ | ||
complete_callout(previous_callout, ptr, cd); | ||
previous_callout = NULL; | ||
} | ||
/* In extended mode, skip white space and comments. */ | ||
4657 | ||
4658 | if ((options & PCRE_EXTENDED) != 0) | if ((options & PCRE_EXTENDED) != 0) |
4659 | { | { |
4660 | if (MAX_255(*ptr) && (cd->ctypes[c] & ctype_space) != 0) continue; | for (;;) |
if (c == CHAR_NUMBER_SIGN) | ||
4661 | { | { |
4662 | while (MAX_255(c) && (cd->ctypes[c] & ctype_space) != 0) c = *(++ptr); | |
4663 | if (c != CHAR_NUMBER_SIGN) break; | |
4664 | ptr++; | ptr++; |
4665 | while (*ptr != CHAR_NULL) | while (*ptr != CHAR_NULL) |
4666 | { | { |
4667 | if (IS_NEWLINE(ptr)) { ptr += cd->nllen - 1; break; } | if (IS_NEWLINE(ptr)) /* For non-fixed-length newline cases, */ |
4668 | { /* IS_NEWLINE sets cd->nllen. */ | |
4669 | ptr += cd->nllen; | |
4670 | break; | |
4671 | } | |
4672 | ptr++; | ptr++; |
4673 | #ifdef SUPPORT_UTF | #ifdef SUPPORT_UTF |
4674 | if (utf) FORWARDCHAR(ptr); | if (utf) FORWARDCHAR(ptr); |
4675 | #endif | #endif |
4676 | } | } |
4677 | if (*ptr != CHAR_NULL) continue; | c = *ptr; /* Either NULL or the char after a newline */ |
/* Else fall through to handle end of string */ | ||
c = 0; | ||
4678 | } | } |
4679 | } | } |
4680 | ||
4681 | /* No auto callout for quantifiers. */ | /* See if the next thing is a quantifier. */ |
4682 | ||
4683 | if ((options & PCRE_AUTO_CALLOUT) != 0 && !is_quantifier) | is_quantifier = |
4684 | c == CHAR_ASTERISK || c == CHAR_PLUS || c == CHAR_QUESTION_MARK || | |
4685 | (c == CHAR_LEFT_CURLY_BRACKET && is_counted_repeat(ptr+1)); | |
4686 | ||
4687 | /* Fill in length of a previous callout, except when the next thing is a | |
4688 | quantifier or when processing a property substitution string in UCP mode. */ | |
4689 | ||
4690 | if (!is_quantifier && previous_callout != NULL && nestptr == NULL && | |
4691 | after_manual_callout-- <= 0) | |
4692 | { | |
4693 | if (lengthptr == NULL) /* Don't attempt in pre-compile phase */ | |
4694 | complete_callout(previous_callout, ptr, cd); | |
4695 | previous_callout = NULL; | |
4696 | } | |
4697 | ||
4698 | /* Create auto callout, except for quantifiers, or while processing property | |
4699 | strings that are substituted for \w etc in UCP mode. */ | |
4700 | ||
4701 | if ((options & PCRE_AUTO_CALLOUT) != 0 && !is_quantifier && nestptr == NULL) | |
4702 | { | { |
4703 | previous_callout = code; | previous_callout = code; |
4704 | code = auto_callout(code, ptr, cd); | code = auto_callout(code, ptr, cd); |
4705 | } | } |
4706 | ||
4707 | /* Process the next pattern item. */ | |
4708 | ||
4709 | switch(c) | switch(c) |
4710 | { | { |
4711 | /* ===================================================================*/ | /* ===================================================================*/ |
4712 | case 0: /* The branch terminates at string end */ | case CHAR_NULL: /* The branch terminates at string end */ |
4713 | case CHAR_VERTICAL_LINE: /* or | or ) */ | case CHAR_VERTICAL_LINE: /* or | or ) */ |
4714 | case CHAR_RIGHT_PARENTHESIS: | case CHAR_RIGHT_PARENTHESIS: |
4715 | *firstcharptr = firstchar; | *firstcharptr = firstchar; |
# | Line 3782 for (;; ptr++) | Line 4739 for (;; ptr++) |
4739 | previous = NULL; | previous = NULL; |
4740 | if ((options & PCRE_MULTILINE) != 0) | if ((options & PCRE_MULTILINE) != 0) |
4741 | { | { |
4742 | if (firstcharflags == REQ_UNSET) firstcharflags = REQ_NONE; | if (firstcharflags == REQ_UNSET) |
4743 | zerofirstcharflags = firstcharflags = REQ_NONE; | |
4744 | *code++ = OP_CIRCM; | *code++ = OP_CIRCM; |
4745 | } | } |
4746 | else *code++ = OP_CIRC; | else *code++ = OP_CIRC; |
# | Line 3830 for (;; ptr++) | Line 4788 for (;; ptr++) |
4788 | } | } |
4789 | goto NORMAL_CHAR; | goto NORMAL_CHAR; |
4790 | ||
4791 | /* In another (POSIX) regex library, the ugly syntax [[:<:]] and [[:>:]] is | |
4792 | used for "start of word" and "end of word". As these are otherwise illegal | |
4793 | sequences, we don't break anything by recognizing them. They are replaced | |
4794 | by \b(?=\w) and \b(?<=\w) respectively. Sequences like [a[:<:]] are | |
4795 | erroneous and are handled by the normal code below. */ | |
4796 | ||
4797 | case CHAR_LEFT_SQUARE_BRACKET: | case CHAR_LEFT_SQUARE_BRACKET: |
4798 | if (STRNCMP_UC_C8(ptr+1, STRING_WEIRD_STARTWORD, 6) == 0) | |
4799 | { | |
4800 | nestptr = ptr + 7; | |
4801 | ptr = sub_start_of_word - 1; | |
4802 | continue; | |
4803 | } | |
4804 | ||
4805 | if (STRNCMP_UC_C8(ptr+1, STRING_WEIRD_ENDWORD, 6) == 0) | |
4806 | { | |
4807 | nestptr = ptr + 7; | |
4808 | ptr = sub_end_of_word - 1; | |
4809 | continue; | |
4810 | } | |
4811 | ||
4812 | /* Handle a real character class. */ | |
4813 | ||
4814 | previous = code; | previous = code; |
4815 | ||
4816 | /* PCRE supports POSIX class stuff inside a class. Perl gives an error if | /* PCRE supports POSIX class stuff inside a class. Perl gives an error if |
# | Line 3887 for (;; ptr++) | Line 4867 for (;; ptr++) |
4867 | ||
4868 | should_flip_negation = FALSE; | should_flip_negation = FALSE; |
4869 | ||
4870 | /* Extended class (xclass) will be used when characters > 255 | |
4871 | might match. */ | |
4872 | ||
4873 | #if defined SUPPORT_UTF || !defined COMPILE_PCRE8 | |
4874 | xclass = FALSE; | |
4875 | class_uchardata = code + LINK_SIZE + 2; /* For XCLASS items */ | |
4876 | class_uchardata_base = class_uchardata; /* Save the start */ | |
4877 | #endif | |
4878 | ||
4879 | /* For optimization purposes, we track some properties of the class: | /* For optimization purposes, we track some properties of the class: |
4880 | class_has_8bitchar will be non-zero if the class contains at least one < | class_has_8bitchar will be non-zero if the class contains at least one < |
4881 | 256 character; class_one_char will be 1 if the class contains just one | 256 character; class_one_char will be 1 if the class contains just one |
4882 | character. */ | character; xclass_has_prop will be TRUE if unicode property checks |
4883 | are present in the class. */ | |
4884 | ||
4885 | class_has_8bitchar = 0; | class_has_8bitchar = 0; |
4886 | class_one_char = 0; | class_one_char = 0; |
4887 | #if defined SUPPORT_UTF || !defined COMPILE_PCRE8 | |
4888 | xclass_has_prop = FALSE; | |
4889 | #endif | |
4890 | ||
4891 | /* Initialize the 32-char bit map to all zeros. We build the map in a | /* Initialize the 32-char bit map to all zeros. We build the map in a |
4892 | temporary bit of memory, in case the class contains fewer than two | temporary bit of memory, in case the class contains fewer than two |
# | Line 3902 for (;; ptr++) | Line 4895 for (;; ptr++) |
4895 | ||
4896 | memset(classbits, 0, 32 * sizeof(pcre_uint8)); | memset(classbits, 0, 32 * sizeof(pcre_uint8)); |
4897 | ||
#if defined SUPPORT_UTF || !defined COMPILE_PCRE8 | ||
xclass = FALSE; | ||
class_uchardata = code + LINK_SIZE + 2; /* For XCLASS items */ | ||
class_uchardata_base = class_uchardata; /* Save the start */ | ||
#endif | ||
4898 | /* Process characters until ] is reached. By writing this as a "do" it | /* Process characters until ] is reached. By writing this as a "do" it |
4899 | means that an initial ] is taken as a data character. At the start of the | means that an initial ] is taken as a data character. At the start of the |
4900 | loop, c contains the first byte of the character. */ | loop, c contains the first byte of the character. */ |
# | Line 3933 for (;; ptr++) | Line 4920 for (;; ptr++) |
4920 | if (lengthptr != NULL && class_uchardata > class_uchardata_base) | if (lengthptr != NULL && class_uchardata > class_uchardata_base) |
4921 | { | { |
4922 | xclass = TRUE; | xclass = TRUE; |
4923 | *lengthptr += class_uchardata - class_uchardata_base; | *lengthptr += (int)(class_uchardata - class_uchardata_base); |
4924 | class_uchardata = class_uchardata_base; | class_uchardata = class_uchardata_base; |
4925 | } | } |
4926 | #endif | #endif |
# | Line 3995 for (;; ptr++) | Line 4982 for (;; ptr++) |
4982 | posix_class = 0; | posix_class = 0; |
4983 | ||
4984 | /* When PCRE_UCP is set, some of the POSIX classes are converted to | /* When PCRE_UCP is set, some of the POSIX classes are converted to |
4985 | different escape sequences that use Unicode properties. */ | different escape sequences that use Unicode properties \p or \P. Others |
4986 | that are not available via \p or \P generate XCL_PROP/XCL_NOTPROP | |
4987 | directly. */ | |
4988 | ||
4989 | #ifdef SUPPORT_UCP | #ifdef SUPPORT_UCP |
4990 | if ((options & PCRE_UCP) != 0) | if ((options & PCRE_UCP) != 0) |
4991 | { | { |
4992 | unsigned int ptype = 0; | |
4993 | int pc = posix_class + ((local_negate)? POSIX_SUBSIZE/2 : 0); | int pc = posix_class + ((local_negate)? POSIX_SUBSIZE/2 : 0); |
4994 | ||
4995 | /* The posix_substitutes table specifies which POSIX classes can be | |
4996 | converted to \p or \P items. */ | |
4997 | ||
4998 | if (posix_substitutes[pc] != NULL) | if (posix_substitutes[pc] != NULL) |
4999 | { | { |
5000 | nestptr = tempptr + 1; | nestptr = tempptr + 1; |
5001 | ptr = posix_substitutes[pc] - 1; | ptr = posix_substitutes[pc] - 1; |
5002 | continue; | continue; |
5003 | } | } |
5004 | ||
5005 | /* There are three other classes that generate special property calls | |
5006 | that are recognized only in an XCLASS. */ | |
5007 | ||
5008 | else switch(posix_class) | |
5009 | { | |
5010 | case PC_GRAPH: | |
5011 | ptype = PT_PXGRAPH; | |
5012 | /* Fall through */ | |
5013 | case PC_PRINT: | |
5014 | if (ptype == 0) ptype = PT_PXPRINT; | |
5015 | /* Fall through */ | |
5016 | case PC_PUNCT: | |
5017 | if (ptype == 0) ptype = PT_PXPUNCT; | |
5018 | *class_uchardata++ = local_negate? XCL_NOTPROP : XCL_PROP; | |
5019 | *class_uchardata++ = ptype; | |
5020 | *class_uchardata++ = 0; | |
5021 | xclass_has_prop = TRUE; | |
5022 | ptr = tempptr + 1; | |
5023 | continue; | |
5024 | ||
5025 | /* For all other POSIX classes, no special action is taken in UCP | |
5026 | mode. Fall through to the non_UCP case. */ | |
5027 | ||
5028 | default: | |
5029 | break; | |
5030 | } | |
5031 | } | } |
5032 | #endif | #endif |
5033 | /* In the non-UCP case, we build the bit map for the POSIX class in a | /* In the non-UCP case, or when UCP makes no difference, we build the |
5034 | chunk of local store because we may be adding and subtracting from it, | bit map for the POSIX class in a chunk of local store because we may be |
5035 | and we don't want to subtract bits that may be in the main map already. | adding and subtracting from it, and we don't want to subtract bits that |
5036 | At the end we or the result into the bit map that is being built. */ | may be in the main map already. At the end we or the result into the |
5037 | bit map that is being built. */ | |
5038 | ||
5039 | posix_class *= 3; | posix_class *= 3; |
5040 | ||
# | Line 4129 for (;; ptr++) | Line 5151 for (;; ptr++) |
5151 | for (c = 0; c < 32; c++) classbits[c] |= ~cbits[c+cbit_word]; | for (c = 0; c < 32; c++) classbits[c] |= ~cbits[c+cbit_word]; |
5152 | continue; | continue; |
5153 | ||
5154 | /* Perl 5.004 onwards omits VT from \s, but we must preserve it | /* Perl 5.004 onwards omitted VT from \s, but restored it at Perl |
5155 | if it was previously set by something earlier in the character | 5.18. Before PCRE 8.34, we had to preserve the VT bit if it was |
5156 | class. Luckily, the value of CHAR_VT is 0x0b in both ASCII and | previously set by something earlier in the character class. |
5157 | EBCDIC, so we lazily just adjust the appropriate bit. */ | Luckily, the value of CHAR_VT is 0x0b in both ASCII and EBCDIC, so |
5158 | we could just adjust the appropriate bit. From PCRE 8.34 we no | |
5159 | longer treat \s and \S specially. */ | |
5160 | ||
5161 | case ESC_s: | case ESC_s: |
5162 | classbits[0] |= cbits[cbit_space]; | for (c = 0; c < 32; c++) classbits[c] |= cbits[c+cbit_space]; |
classbits[1] |= cbits[cbit_space+1] & ~0x08; | ||
for (c = 2; c < 32; c++) classbits[c] |= cbits[c+cbit_space]; | ||
5163 | continue; | continue; |
5164 | ||
5165 | case ESC_S: | case ESC_S: |
5166 | should_flip_negation = TRUE; | should_flip_negation = TRUE; |
5167 | for (c = 0; c < 32; c++) classbits[c] |= ~cbits[c+cbit_space]; | for (c = 0; c < 32; c++) classbits[c] |= ~cbits[c+cbit_space]; |
classbits[1] |= 0x08; /* Perl 5.004 onwards omits VT from \s */ | ||
5168 | continue; | continue; |
5169 | ||
5170 | /* The rest apply in both UCP and non-UCP cases. */ | /* The rest apply in both UCP and non-UCP cases. */ |
# | Line 4180 for (;; ptr++) | Line 5201 for (;; ptr++) |
5201 | XCL_PROP : XCL_NOTPROP; | XCL_PROP : XCL_NOTPROP; |
5202 | *class_uchardata++ = ptype; | *class_uchardata++ = ptype; |
5203 | *class_uchardata++ = pdata; | *class_uchardata++ = pdata; |
5204 | xclass_has_prop = TRUE; | |
5205 | class_has_8bitchar--; /* Undo! */ | class_has_8bitchar--; /* Undo! */ |
5206 | continue; | continue; |
5207 | } | } |
# | Line 4265 for (;; ptr++) | Line 5287 for (;; ptr++) |
5287 | #endif | #endif |
5288 | d = *ptr; /* Not UTF-8 mode */ | d = *ptr; /* Not UTF-8 mode */ |
5289 | ||
5290 | /* The second part of a range can be a single-character escape, but | /* The second part of a range can be a single-character escape |
5291 | not any of the other escapes. Perl 5.6 treats a hyphen as a literal | sequence, but not any of the other escapes. Perl treats a hyphen as a |
5292 | in such circumstances. */ | literal in such circumstances. However, in Perl's warning mode, a |
5293 | warning is given, so PCRE now faults it as it is almost certainly a | |
5294 | mistake on the user's part. */ | |
5295 | ||
5296 | if (!inescq && d == CHAR_BACKSLASH) | if (!inescq) |
5297 | { | { |
5298 | int descape; | if (d == CHAR_BACKSLASH) |
5299 | descape = check_escape(&ptr, &d, errorcodeptr, cd->bracount, options, TRUE); | { |
5300 | if (*errorcodeptr != 0) goto FAILED; | int descape; |
5301 | descape = check_escape(&ptr, &d, errorcodeptr, cd->bracount, options, TRUE); | |
5302 | if (*errorcodeptr != 0) goto FAILED; | |
5303 | ||
5304 | /* \b is backspace; any other special means the '-' was literal. */ | /* 0 means a character was put into d; \b is backspace; any other |
5305 | special causes an error. */ | |
5306 | ||
5307 | if (descape != 0) | if (descape != 0) |
{ | ||
if (descape == ESC_b) d = CHAR_BS; else | ||
5308 | { | { |
5309 | ptr = oldptr; | if (descape == ESC_b) d = CHAR_BS; else |
5310 | goto CLASS_SINGLE_CHARACTER; /* A few lines below */ | { |
5311 | *errorcodeptr = ERR83; | |
5312 | goto FAILED; | |
5313 | } | |
5314 | } | } |
5315 | } | } |
5316 | ||
5317 | /* A hyphen followed by a POSIX class is treated in the same way. */ | |
5318 | ||
5319 | else if (d == CHAR_LEFT_SQUARE_BRACKET && | |
5320 | (ptr[1] == CHAR_COLON || ptr[1] == CHAR_DOT || | |
5321 | ptr[1] == CHAR_EQUALS_SIGN) && | |
5322 | check_posix_syntax(ptr, &tempptr)) | |
5323 | { | |
5324 | *errorcodeptr = ERR83; | |
5325 | goto FAILED; | |
5326 | } | |
5327 | } | } |
5328 | ||
5329 | /* Check that the two values are in the correct order. Optimize | /* Check that the two values are in the correct order. Optimize |
# | Line 4331 for (;; ptr++) | Line 5370 for (;; ptr++) |
5370 | whatever repeat count may follow. In the case of reqchar, save the | whatever repeat count may follow. In the case of reqchar, save the |
5371 | previous value for reinstating. */ | previous value for reinstating. */ |
5372 | ||
5373 | if (class_one_char == 1 && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET) | if (!inescq && class_one_char == 1 && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET) |
5374 | { | { |
5375 | ptr++; | ptr++; |
5376 | zeroreqchar = reqchar; | zeroreqchar = reqchar; |
# | Line 4457 for (;; ptr++) | Line 5496 for (;; ptr++) |
5496 | *code++ = OP_XCLASS; | *code++ = OP_XCLASS; |
5497 | code += LINK_SIZE; | code += LINK_SIZE; |
5498 | *code = negate_class? XCL_NOT:0; | *code = negate_class? XCL_NOT:0; |
5499 | if (xclass_has_prop) *code |= XCL_HASPROP; | |
5500 | ||
5501 | /* If the map is required, move up the extra data to make room for it; | /* If the map is required, move up the extra data to make room for it; |
5502 | otherwise just move the code pointer to the end of the extra data. */ | otherwise just move the code pointer to the end of the extra data. */ |
# | Line 4466 for (;; ptr++) | Line 5506 for (;; ptr++) |
5506 | *code++ |= XCL_MAP; | *code++ |= XCL_MAP; |
5507 | memmove(code + (32 / sizeof(pcre_uchar)), code, | memmove(code + (32 / sizeof(pcre_uchar)), code, |
5508 | IN_UCHARS(class_uchardata - code)); | IN_UCHARS(class_uchardata - code)); |
5509 | if (negate_class && !xclass_has_prop) | |
5510 | for (c = 0; c < 32; c++) classbits[c] = ~classbits[c]; | |
5511 | memcpy(code, classbits, 32); | memcpy(code, classbits, 32); |
5512 | code = class_uchardata + (32 / sizeof(pcre_uchar)); | code = class_uchardata + (32 / sizeof(pcre_uchar)); |
5513 | } | } |
# | Line 4478 for (;; ptr++) | Line 5520 for (;; ptr++) |
5520 | } | } |
5521 | #endif | #endif |
5522 | ||
5523 | /* Even though any XCLASS list is now discarded, we must allow for | |
5524 | its memory. */ | |
5525 | ||
5526 | if (lengthptr != NULL) | |
5527 | *lengthptr += (int)(class_uchardata - class_uchardata_base); | |
5528 | ||
5529 | /* If there are no characters > 255, or they are all to be included or | /* If there are no characters > 255, or they are all to be included or |
5530 | excluded, set the opcode to OP_CLASS or OP_NCLASS, depending on whether the | excluded, set the opcode to OP_CLASS or OP_NCLASS, depending on whether the |
5531 | whole class was negated and whether there were negative specials such as \S | whole class was negated and whether there were negative specials such as \S |
# | Line 4548 for (;; ptr++) | Line 5596 for (;; ptr++) |
5596 | ||
5597 | tempcode = previous; | tempcode = previous; |
5598 | ||
5599 | /* Before checking for a possessive quantifier, we must skip over | |
5600 | whitespace and comments in extended mode because Perl allows white space at | |
5601 | this point. */ | |
5602 | ||
5603 | if ((options & PCRE_EXTENDED) != 0) | |
5604 | { | |
5605 | const pcre_uchar *p = ptr + 1; | |
5606 | for (;;) | |
5607 | { | |
5608 | while (MAX_255(*p) && (cd->ctypes[*p] & ctype_space) != 0) p++; | |
5609 | if (*p != CHAR_NUMBER_SIGN) break; | |
5610 | p++; | |
5611 | while (*p != CHAR_NULL) | |
5612 | { | |
5613 | if (IS_NEWLINE(p)) /* For non-fixed-length newline cases, */ | |
5614 | { /* IS_NEWLINE sets cd->nllen. */ | |
5615 | p += cd->nllen; | |
5616 | break; | |
5617 | } | |
5618 | p++; | |
5619 | #ifdef SUPPORT_UTF | |
5620 | if (utf) FORWARDCHAR(p); | |
5621 | #endif | |
5622 | } /* Loop for comment characters */ | |
5623 | } /* Loop for multiple comments */ | |
5624 | ptr = p - 1; /* Character before the next significant one. */ | |
5625 | } | |
5626 | ||
5627 | /* If the next character is '+', we have a possessive quantifier. This | /* If the next character is '+', we have a possessive quantifier. This |
5628 | implies greediness, whatever the setting of the PCRE_UNGREEDY option. | implies greediness, whatever the setting of the PCRE_UNGREEDY option. |
5629 | If the next character is '?' this is a minimizing repeat, by default, | If the next character is '?' this is a minimizing repeat, by default, |
# | Line 4642 for (;; ptr++) | Line 5718 for (;; ptr++) |
5718 | } | } |
5719 | } | } |
5720 | ||
/* If the repetition is unlimited, it pays to see if the next thing on | ||
the line is something that cannot possibly match this character. If so, | ||
automatically possessifying this item gains some performance in the case | ||
where the match fails. */ | ||
if (!possessive_quantifier && | ||
repeat_max < 0 && | ||
check_auto_possessive(previous, utf, ptr + 1, options, cd)) | ||
{ | ||
repeat_type = 0; /* Force greedy */ | ||
possessive_quantifier = TRUE; | ||
} | ||
5721 | goto OUTPUT_SINGLE_REPEAT; /* Code shared with single character types */ | goto OUTPUT_SINGLE_REPEAT; /* Code shared with single character types */ |
5722 | } | } |
5723 | ||
# | Line 4672 for (;; ptr++) | Line 5735 for (;; ptr++) |
5735 | op_type = OP_TYPESTAR - OP_STAR; /* Use type opcodes */ | op_type = OP_TYPESTAR - OP_STAR; /* Use type opcodes */ |
5736 | c = *previous; | c = *previous; |
5737 | ||
if (!possessive_quantifier && | ||
repeat_max < 0 && | ||
check_auto_possessive(previous, utf, ptr + 1, options, cd)) | ||
{ | ||
repeat_type = 0; /* Force greedy */ | ||
possessive_quantifier = TRUE; | ||
} | ||
5738 | OUTPUT_SINGLE_REPEAT: | OUTPUT_SINGLE_REPEAT: |
5739 | if (*previous == OP_PROP || *previous == OP_NOTPROP) | if (*previous == OP_PROP || *previous == OP_NOTPROP) |
5740 | { | { |
# | Line 4861 for (;; ptr++) | Line 5916 for (;; ptr++) |
5916 | opcodes such as BRA and CBRA, as this is the place where they get converted | opcodes such as BRA and CBRA, as this is the place where they get converted |
5917 | into the more special varieties such as BRAPOS and SBRA. A test for >= | into the more special varieties such as BRAPOS and SBRA. A test for >= |
5918 | OP_ASSERT and <= OP_COND includes ASSERT, ASSERT_NOT, ASSERTBACK, | OP_ASSERT and <= OP_COND includes ASSERT, ASSERT_NOT, ASSERTBACK, |
5919 | ASSERTBACK_NOT, ONCE, BRA, CBRA, and COND. Originally, PCRE did not allow | ASSERTBACK_NOT, ONCE, ONCE_NC, BRA, BRAPOS, CBRA, CBRAPOS, and COND. |
5920 | repetition of assertions, but now it does, for Perl compatibility. */ | Originally, PCRE did not allow repetition of assertions, but now it does, |
5921 | for Perl compatibility. */ | |
5922 | ||
5923 | else if (*previous >= OP_ASSERT && *previous <= OP_COND) | else if (*previous >= OP_ASSERT && *previous <= OP_COND) |
5924 | { | { |
# | Line 4880 for (;; ptr++) | Line 5936 for (;; ptr++) |
5936 | /* There is no sense in actually repeating assertions. The only potential | /* There is no sense in actually repeating assertions. The only potential |
5937 | use of repetition is in cases when the assertion is optional. Therefore, | use of repetition is in cases when the assertion is optional. Therefore, |
5938 | if the minimum is greater than zero, just ignore the repeat. If the | if the minimum is greater than zero, just ignore the repeat. If the |
5939 | maximum is not not zero or one, set it to 1. */ | maximum is not zero or one, set it to 1. */ |
5940 | ||
5941 | if (*previous < OP_ONCE) /* Assertion */ | if (*previous < OP_ONCE) /* Assertion */ |
5942 | { | { |
# | Line 4922 for (;; ptr++) | Line 5978 for (;; ptr++) |
5978 | if (repeat_max <= 1) /* Covers 0, 1, and unlimited */ | if (repeat_max <= 1) /* Covers 0, 1, and unlimited */ |
5979 | { | { |
5980 | *code = OP_END; | *code = OP_END; |
5981 | adjust_recurse(previous, 1, utf, cd, save_hwm); | adjust_recurse(previous, 1, utf, cd, save_hwm_offset); |
5982 | memmove(previous + 1, previous, IN_UCHARS(len)); | memmove(previous + 1, previous, IN_UCHARS(len)); |
5983 | code++; | code++; |
5984 | if (repeat_max == 0) | if (repeat_max == 0) |
# | Line 4946 for (;; ptr++) | Line 6002 for (;; ptr++) |
6002 | { | { |
6003 | int offset; | int offset; |
6004 | *code = OP_END; | *code = OP_END; |
6005 | adjust_recurse(previous, 2 + LINK_SIZE, utf, cd, save_hwm); | adjust_recurse(previous, 2 + LINK_SIZE, utf, cd, save_hwm_offset); |
6006 | memmove(previous + 2 + LINK_SIZE, previous, IN_UCHARS(len)); | memmove(previous + 2 + LINK_SIZE, previous, IN_UCHARS(len)); |
6007 | code += 2 + LINK_SIZE; | code += 2 + LINK_SIZE; |
6008 | *previous++ = OP_BRAZERO + repeat_type; | *previous++ = OP_BRAZERO + repeat_type; |
# | Line 5009 for (;; ptr++) | Line 6065 for (;; ptr++) |
6065 | for (i = 1; i < repeat_min; i++) | for (i = 1; i < repeat_min; i++) |
6066 | { | { |
6067 | pcre_uchar *hc; | pcre_uchar *hc; |
6068 | pcre_uchar *this_hwm = cd->hwm; | size_t this_hwm_offset = cd->hwm - cd->start_workspace; |
6069 | memcpy(code, previous, IN_UCHARS(len)); | memcpy(code, previous, IN_UCHARS(len)); |
6070 | ||
6071 | while (cd->hwm > cd->start_workspace + cd->workspace_size - | while (cd->hwm > cd->start_workspace + cd->workspace_size - |
6072 | WORK_SIZE_SAFETY_MARGIN - (this_hwm - save_hwm)) | WORK_SIZE_SAFETY_MARGIN - |
6073 | (this_hwm_offset - save_hwm_offset)) | |
6074 | { | { |
int save_offset = save_hwm - cd->start_workspace; | ||
int this_offset = this_hwm - cd->start_workspace; | ||
6075 | *errorcodeptr = expand_workspace(cd); | *errorcodeptr = expand_workspace(cd); |
6076 | if (*errorcodeptr != 0) goto FAILED; | if (*errorcodeptr != 0) goto FAILED; |
save_hwm = (pcre_uchar *)cd->start_workspace + save_offset; | ||
this_hwm = (pcre_uchar *)cd->start_workspace + this_offset; | ||
6077 | } | } |
6078 | ||
6079 | for (hc = save_hwm; hc < this_hwm; hc += LINK_SIZE) | for (hc = (pcre_uchar *)cd->start_workspace + save_hwm_offset; |
6080 | hc < (pcre_uchar *)cd->start_workspace + this_hwm_offset; | |
6081 | hc += LINK_SIZE) | |
6082 | { | { |
6083 | PUT(cd->hwm, 0, GET(hc, 0) + len); | PUT(cd->hwm, 0, GET(hc, 0) + len); |
6084 | cd->hwm += LINK_SIZE; | cd->hwm += LINK_SIZE; |
6085 | } | } |
6086 | save_hwm = this_hwm; | save_hwm_offset = this_hwm_offset; |
6087 | code += len; | code += len; |
6088 | } | } |
6089 | } | } |
# | Line 5073 for (;; ptr++) | Line 6128 for (;; ptr++) |
6128 | else for (i = repeat_max - 1; i >= 0; i--) | else for (i = repeat_max - 1; i >= 0; i--) |
6129 | { | { |
6130 | pcre_uchar *hc; | pcre_uchar *hc; |
6131 | pcre_uchar *this_hwm = cd->hwm; | size_t this_hwm_offset = cd->hwm - cd->start_workspace; |
6132 | ||
6133 | *code++ = OP_BRAZERO + repeat_type; | *code++ = OP_BRAZERO + repeat_type; |
6134 | ||
# | Line 5095 for (;; ptr++) | Line 6150 for (;; ptr++) |
6150 | copying them. */ | copying them. */ |
6151 | ||
6152 | while (cd->hwm > cd->start_workspace + cd->workspace_size - | while (cd->hwm > cd->start_workspace + cd->workspace_size - |
6153 | WORK_SIZE_SAFETY_MARGIN - (this_hwm - save_hwm)) | WORK_SIZE_SAFETY_MARGIN - |
6154 | (this_hwm_offset - save_hwm_offset)) | |
6155 | { | { |
int save_offset = save_hwm - cd->start_workspace; | ||
int this_offset = this_hwm - cd->start_workspace; | ||
6156 | *errorcodeptr = expand_workspace(cd); | *errorcodeptr = expand_workspace(cd); |
6157 | if (*errorcodeptr != 0) goto FAILED; | if (*errorcodeptr != 0) goto FAILED; |
save_hwm = (pcre_uchar *)cd->start_workspace + save_offset; | ||
this_hwm = (pcre_uchar *)cd->start_workspace + this_offset; | ||
6158 | } | } |
6159 | ||
6160 | for (hc = save_hwm; hc < this_hwm; hc += LINK_SIZE) | for (hc = (pcre_uchar *)cd->start_workspace + save_hwm_offset; |
6161 | hc < (pcre_uchar *)cd->start_workspace + this_hwm_offset; | |
6162 | hc += LINK_SIZE) | |
6163 | { | { |
6164 | PUT(cd->hwm, 0, GET(hc, 0) + len + ((i != 0)? 2+LINK_SIZE : 1)); | PUT(cd->hwm, 0, GET(hc, 0) + len + ((i != 0)? 2+LINK_SIZE : 1)); |
6165 | cd->hwm += LINK_SIZE; | cd->hwm += LINK_SIZE; |
6166 | } | } |
6167 | save_hwm = this_hwm; | save_hwm_offset = this_hwm_offset; |
6168 | code += len; | code += len; |
6169 | } | } |
6170 | ||
# | Line 5206 for (;; ptr++) | Line 6260 for (;; ptr++) |
6260 | { | { |
6261 | int nlen = (int)(code - bracode); | int nlen = (int)(code - bracode); |
6262 | *code = OP_END; | *code = OP_END; |
6263 | adjust_recurse(bracode, 1 + LINK_SIZE, utf, cd, save_hwm); | adjust_recurse(bracode, 1 + LINK_SIZE, utf, cd, save_hwm_offset); |
6264 | memmove(bracode + 1 + LINK_SIZE, bracode, IN_UCHARS(nlen)); | memmove(bracode + 1 + LINK_SIZE, bracode, IN_UCHARS(nlen)); |
6265 | code += 1 + LINK_SIZE; | code += 1 + LINK_SIZE; |
6266 | nlen += 1 + LINK_SIZE; | nlen += 1 + LINK_SIZE; |
# | Line 5253 for (;; ptr++) | Line 6307 for (;; ptr++) |
6307 | goto FAILED; | goto FAILED; |
6308 | } | } |
6309 | ||
6310 | /* If the character following a repeat is '+', or if certain optimization | /* If the character following a repeat is '+', possessive_quantifier is |
6311 | tests above succeeded, possessive_quantifier is TRUE. For some opcodes, | TRUE. For some opcodes, there are special alternative opcodes for this |
6312 | there are special alternative opcodes for this case. For anything else, we | case. For anything else, we wrap the entire repeated item inside OP_ONCE |
6313 | wrap the entire repeated item inside OP_ONCE brackets. Logically, the '+' | brackets. Logically, the '+' notation is just syntactic sugar, taken from |
6314 | notation is just syntactic sugar, taken from Sun's Java package, but the | Sun's Java package, but the special opcodes can optimize it. |
special opcodes can optimize it. | ||
6315 | ||
6316 | Some (but not all) possessively repeated subpatterns have already been | Some (but not all) possessively repeated subpatterns have already been |
6317 | completely handled in the code just above. For them, possessive_quantifier | completely handled in the code just above. For them, possessive_quantifier |
6318 | is always FALSE at this stage. | is always FALSE at this stage. Note that the repeated item starts at |
6319 | tempcode, not at previous, which might be the first part of a string whose | |
6320 | Note that the repeated item starts at tempcode, not at previous, which | (former) last char we repeated. */ |
might be the first part of a string whose (former) last char we repeated. | ||
Possessifying an 'exact' quantifier has no effect, so we can ignore it. But | ||
an 'upto' may follow. We skip over an 'exact' item, and then test the | ||
length of what remains before proceeding. */ | ||
6321 | ||
6322 | if (possessive_quantifier) | if (possessive_quantifier) |
6323 | { | { |
6324 | int len; | int len; |
6325 | ||
6326 | if (*tempcode == OP_TYPEEXACT) | /* Possessifying an EXACT quantifier has no effect, so we can ignore it. |
6327 | However, QUERY, STAR, or UPTO may follow (for quantifiers such as {5,6}, | |
6328 | {5,}, or {5,10}). We skip over an EXACT item; if the length of what | |
6329 | remains is greater than zero, there's a further opcode that can be | |
6330 | handled. If not, do nothing, leaving the EXACT alone. */ | |
6331 | ||
6332 | switch(*tempcode) | |
6333 | { | |
6334 | case OP_TYPEEXACT: | |
6335 | tempcode += PRIV(OP_lengths)[*tempcode] + | tempcode += PRIV(OP_lengths)[*tempcode] + |
6336 | ((tempcode[1 + IMM2_SIZE] == OP_PROP | ((tempcode[1 + IMM2_SIZE] == OP_PROP |
6337 | || tempcode[1 + IMM2_SIZE] == OP_NOTPROP)? 2 : 0); | || tempcode[1 + IMM2_SIZE] == OP_NOTPROP)? 2 : 0); |
6338 | break; | |
6339 | ||
6340 | else if (*tempcode == OP_EXACT || *tempcode == OP_NOTEXACT) | /* CHAR opcodes are used for exacts whose count is 1. */ |
6341 | { | |
6342 | case OP_CHAR: | |
6343 | case OP_CHARI: | |
6344 | case OP_NOT: | |
6345 | case OP_NOTI: | |
6346 | case OP_EXACT: | |
6347 | case OP_EXACTI: | |
6348 | case OP_NOTEXACT: | |
6349 | case OP_NOTEXACTI: | |
6350 | tempcode += PRIV(OP_lengths)[*tempcode]; | tempcode += PRIV(OP_lengths)[*tempcode]; |
6351 | #ifdef SUPPORT_UTF | #ifdef SUPPORT_UTF |
6352 | if (utf && HAS_EXTRALEN(tempcode[-1])) | if (utf && HAS_EXTRALEN(tempcode[-1])) |
6353 | tempcode += GET_EXTRALEN(tempcode[-1]); | tempcode += GET_EXTRALEN(tempcode[-1]); |
6354 | #endif | #endif |
6355 | break; | |
6356 | ||
6357 | /* For the class opcodes, the repeat operator appears at the end; | |
6358 | adjust tempcode to point to it. */ | |
6359 | ||
6360 | case OP_CLASS: | |
6361 | case OP_NCLASS: | |
6362 | tempcode += 1 + 32/sizeof(pcre_uchar); | |
6363 | break; | |
6364 | ||
6365 | #if defined SUPPORT_UTF || !defined COMPILE_PCRE8 | |
6366 | case OP_XCLASS: | |
6367 | tempcode += GET(tempcode, 1); | |
6368 | break; | |
6369 | #endif | |
6370 | } | } |
6371 | ||
6372 | /* If tempcode is equal to code (which points to the end of the repeated | |
6373 | item), it means we have skipped an EXACT item but there is no following | |
6374 | QUERY, STAR, or UPTO; the value of len will be 0, and we do nothing. In | |
6375 | all other cases, tempcode will be pointing to the repeat opcode, and will | |
6376 | be less than code, so the value of len will be greater than 0. */ | |
6377 | ||
6378 | len = (int)(code - tempcode); | len = (int)(code - tempcode); |
6379 | if (len > 0) | |
6380 | { | |
6381 | unsigned int repcode = *tempcode; | |
6382 | ||
6383 | /* There is a table for possessifying opcodes, all of which are less | |
6384 | than OP_CALLOUT. A zero entry means there is no possessified version. | |
6385 | */ | |
6386 | ||
6387 | if (repcode < OP_CALLOUT && opcode_possessify[repcode] > 0) | |
6388 | *tempcode = opcode_possessify[repcode]; | |
6389 | ||
6390 | /* For opcode without a special possessified version, wrap the item in | |
6391 | ONCE brackets. Because we are moving code along, we must ensure that any | |
6392 | pending recursive references are updated. */ | |
6393 | ||
6394 | else | |
6395 | { | |
6396 | *code = OP_END; | |
6397 | adjust_recurse(tempcode, 1 + LINK_SIZE, utf, cd, save_hwm_offset); | |
6398 | memmove(tempcode + 1 + LINK_SIZE, tempcode, IN_UCHARS(len)); | |
6399 | code += 1 + LINK_SIZE; | |
6400 | len += 1 + LINK_SIZE; | |
6401 | tempcode[0] = OP_ONCE; | |
6402 | *code++ = OP_KET; | |
6403 | PUTINC(code, 0, len); | |
6404 | PUT(tempcode, 1, len); | |
6405 | } | |
6406 | } | |
6407 | ||
6408 | #ifdef NEVER | |
6409 | if (len > 0) switch (*tempcode) | if (len > 0) switch (*tempcode) |
6410 | { | { |
6411 | case OP_STAR: *tempcode = OP_POSSTAR; break; | case OP_STAR: *tempcode = OP_POSSTAR; break; |
# | Line 5317 for (;; ptr++) | Line 6433 for (;; ptr++) |
6433 | case OP_TYPEQUERY: *tempcode = OP_TYPEPOSQUERY; break; | case OP_TYPEQUERY: *tempcode = OP_TYPEPOSQUERY; break; |
6434 | case OP_TYPEUPTO: *tempcode = OP_TYPEPOSUPTO; break; | case OP_TYPEUPTO: *tempcode = OP_TYPEPOSUPTO; break; |
6435 | ||
6436 | case OP_CRSTAR: *tempcode = OP_CRPOSSTAR; break; | |
6437 | case OP_CRPLUS: *tempcode = OP_CRPOSPLUS; break; | |
6438 | case OP_CRQUERY: *tempcode = OP_CRPOSQUERY; break; | |
6439 | case OP_CRRANGE: *tempcode = OP_CRPOSRANGE; break; | |
6440 | ||
6441 | /* Because we are moving code along, we must ensure that any | /* Because we are moving code along, we must ensure that any |
6442 | pending recursive references are updated. */ | pending recursive references are updated. */ |
6443 | ||
6444 | default: | default: |
6445 | *code = OP_END; | *code = OP_END; |
6446 | adjust_recurse(tempcode, 1 + LINK_SIZE, utf, cd, save_hwm); | adjust_recurse(tempcode, 1 + LINK_SIZE, utf, cd, save_hwm_offset); |
6447 | memmove(tempcode + 1 + LINK_SIZE, tempcode, IN_UCHARS(len)); | memmove(tempcode + 1 + LINK_SIZE, tempcode, IN_UCHARS(len)); |
6448 | code += 1 + LINK_SIZE; | code += 1 + LINK_SIZE; |
6449 | len += 1 + LINK_SIZE; | len += 1 + LINK_SIZE; |
# | Line 5332 for (;; ptr++) | Line 6453 for (;; ptr++) |
6453 | PUT(tempcode, 1, len); | PUT(tempcode, 1, len); |
6454 | break; | break; |
6455 | } | } |
6456 | #endif | |
6457 | } | } |
6458 | ||
6459 | /* In all case we no longer have a previous item. We also set the | /* In all case we no longer have a previous item. We also set the |
# | Line 5350 for (;; ptr++) | Line 6472 for (;; ptr++) |
6472 | parenthesis forms. */ | parenthesis forms. */ |
6473 | ||
6474 | case CHAR_LEFT_PARENTHESIS: | case CHAR_LEFT_PARENTHESIS: |
6475 | newoptions = options; | ptr++; |
skipbytes = 0; | ||
bravalue = OP_CBRA; | ||
save_hwm = cd->hwm; | ||
reset_bracount = FALSE; | ||
6476 | ||
6477 | /* First deal with various "verbs" that can be introduced by '*'. */ | /* First deal with comments. Putting this code right at the start ensures |
6478 | that comments have no bad side effects. */ | |
6479 | ||
6480 | if (ptr[0] == CHAR_QUESTION_MARK && ptr[1] == CHAR_NUMBER_SIGN) | |
6481 | { | |
6482 | ptr += 2; | |
6483 | while (*ptr != CHAR_NULL && *ptr != CHAR_RIGHT_PARENTHESIS) ptr++; | |
6484 | if (*ptr == CHAR_NULL) | |
6485 | { | |
6486 | *errorcodeptr = ERR18; | |
6487 | goto FAILED; | |
6488 | } | |
6489 | continue; | |
6490 | } | |
6491 | ||
6492 | /* Now deal with various "verbs" that can be introduced by '*'. */ | |
6493 | ||
ptr++; | ||
6494 | if (ptr[0] == CHAR_ASTERISK && (ptr[1] == ':' | if (ptr[0] == CHAR_ASTERISK && (ptr[1] == ':' |
6495 | || (MAX_255(ptr[1]) && ((cd->ctypes[ptr[1]] & ctype_letter) != 0)))) | || (MAX_255(ptr[1]) && ((cd->ctypes[ptr[1]] & ctype_letter) != 0)))) |
6496 | { | { |
# | Line 5479 for (;; ptr++) | Line 6611 for (;; ptr++) |
6611 | goto FAILED; | goto FAILED; |
6612 | } | } |
6613 | ||
6614 | /* Initialize for "real" parentheses */ | |
6615 | ||
6616 | newoptions = options; | |
6617 | skipbytes = 0; | |
6618 | bravalue = OP_CBRA; | |
6619 | save_hwm_offset = cd->hwm - cd->start_workspace; | |
6620 | reset_bracount = FALSE; | |
6621 | ||
6622 | /* Deal with the extended parentheses; all are introduced by '?', and the | /* Deal with the extended parentheses; all are introduced by '?', and the |
6623 | appearance of any of them means that this is not a capturing group. */ | appearance of any of them means that this is not a capturing group. */ |
6624 | ||
6625 | else if (*ptr == CHAR_QUESTION_MARK) | if (*ptr == CHAR_QUESTION_MARK) |
6626 | { | { |
6627 | int i, set, unset, namelen; | int i, set, unset, namelen; |
6628 | int *optset; | int *optset; |
# | Line 5491 for (;; ptr++) | Line 6631 for (;; ptr++) |
6631 | ||
6632 | switch (*(++ptr)) | switch (*(++ptr)) |
6633 | { | { |
case CHAR_NUMBER_SIGN: /* Comment; skip to ket */ | ||
ptr++; | ||
while (*ptr != CHAR_NULL && *ptr != CHAR_RIGHT_PARENTHESIS) ptr++; | ||
if (*ptr == CHAR_NULL) | ||
{ | ||
*errorcodeptr = ERR18; | ||
goto FAILED; | ||
} | ||
continue; | ||
6634 | /* ------------------------------------------------------------ */ | /* ------------------------------------------------------------ */ |
6635 | case CHAR_VERTICAL_LINE: /* Reset capture count for each branch */ | case CHAR_VERTICAL_LINE: /* Reset capture count for each branch */ |
6636 | reset_bracount = TRUE; | reset_bracount = TRUE; |
# | Line 5520 for (;; ptr++) | Line 6649 for (;; ptr++) |
6649 | tempptr = ptr; | tempptr = ptr; |
6650 | ||
6651 | /* A condition can be an assertion, a number (referring to a numbered | /* A condition can be an assertion, a number (referring to a numbered |
6652 | group), a name (referring to a named group), or 'R', referring to | group's having been set), a name (referring to a named group), or 'R', |
6653 | recursion. R<digits> and R&name are also permitted for recursion tests. | referring to recursion. R<digits> and R&name are also permitted for |
6654 | recursion tests. | |
6655 | There are several syntaxes for testing a named group: (?(name)) is used | |
6656 | by Python; Perl 5.10 onwards uses (?(<name>) or (?('name')). | There are ways of testing a named group: (?(name)) is used by Python; |
6657 | Perl 5.10 onwards uses (?(<name>) or (?('name')). | |
6658 | There are two unfortunate ambiguities, caused by history. (a) 'R' can | |
6659 | be the recursive thing or the name 'R' (and similarly for 'R' followed | There is one unfortunate ambiguity, caused by history. 'R' can be the |
6660 | by digits), and (b) a number could be a name that consists of digits. | recursive thing or the name 'R' (and similarly for 'R' followed by |
6661 | In both cases, we look for a name first; if not found, we try the other | digits). We look for a name first; if not found, we try the other case. |
cases. | ||
6662 | ||
6663 | For compatibility with auto-callouts, we allow a callout to be | For compatibility with auto-callouts, we allow a callout to be |
6664 | specified before a condition that is an assertion. First, check for the | specified before a condition that is an assertion. First, check for the |
# | Line 5552 for (;; ptr++) | Line 6680 for (;; ptr++) |
6680 | (tempptr[2] == CHAR_EQUALS_SIGN || | (tempptr[2] == CHAR_EQUALS_SIGN || |
6681 | tempptr[2] == CHAR_EXCLAMATION_MARK || | tempptr[2] == CHAR_EXCLAMATION_MARK || |
6682 | tempptr[2] == CHAR_LESS_THAN_SIGN)) | tempptr[2] == CHAR_LESS_THAN_SIGN)) |
6683 | { | |
6684 | cd->iscondassert = TRUE; | |
6685 | break; | break; |
6686 | } | |
6687 | ||
6688 | /* Most other conditions use OP_CREF (a couple change to OP_RREF | /* Other conditions use OP_CREF/OP_DNCREF/OP_RREF/OP_DNRREF, and all |
6689 | below), and all need to skip 1+IMM2_SIZE bytes at the start of the group. */ | need to skip at least 1+IMM2_SIZE bytes at the start of the group. */ |
6690 | ||
6691 | code[1+LINK_SIZE] = OP_CREF; | code[1+LINK_SIZE] = OP_CREF; |
6692 | skipbytes = 1+IMM2_SIZE; | skipbytes = 1+IMM2_SIZE; |
6693 | refsign = -1; | refsign = -1; /* => not a number */ |
6694 | namelen = -1; /* => not a name; must set to avoid warning */ | |
6695 | name = NULL; /* Always set to avoid warning */ | |
6696 | recno = 0; /* Always set to avoid warning */ | |
6697 | ||
6698 | /* Check for a test for recursion in a named group. */ | /* Check for a test for recursion in a named group. */ |
6699 | ||
6700 | if (ptr[1] == CHAR_R && ptr[2] == CHAR_AMPERSAND) | ptr++; |
6701 | if (*ptr == CHAR_R && ptr[1] == CHAR_AMPERSAND) | |
6702 | { | { |
6703 | terminator = -1; | terminator = -1; |
6704 | ptr += 2; | ptr += 2; |
# | Line 5571 for (;; ptr++) | Line 6706 for (;; ptr++) |
6706 | } | } |
6707 | ||
6708 | /* Check for a test for a named group's having been set, using the Perl | /* Check for a test for a named group's having been set, using the Perl |
6709 | syntax (?(<name>) or (?('name') */ | syntax (?(<name>) or (?('name'), and also allow for the original PCRE |
6710 | syntax of (?(name) or for (?(+n), (?(-n), and just (?(n). */ | |
6711 | ||
6712 | else if (ptr[1] == CHAR_LESS_THAN_SIGN) | else if (*ptr == CHAR_LESS_THAN_SIGN) |
6713 | { | { |
6714 | terminator = CHAR_GREATER_THAN_SIGN; | terminator = CHAR_GREATER_THAN_SIGN; |
6715 | ptr++; | ptr++; |
6716 | } | } |
6717 | else if (ptr[1] == CHAR_APOSTROPHE) | else if (*ptr == CHAR_APOSTROPHE) |
6718 | { | { |
6719 | terminator = CHAR_APOSTROPHE; | terminator = CHAR_APOSTROPHE; |