From 6bf342e1eb87c656de34d668d7303a7d0138e89a Mon Sep 17 00:00:00 2001 From: Philip Hazel Date: Tue, 23 Jan 2007 15:08:45 +0000 Subject: [PATCH] Installed PCRE release 7.0. --- doc/doc-txt/ChangeLog | 4 +- doc/doc-txt/pcrepattern.txt | 782 ++++--- doc/doc-txt/pcretest.txt | 328 +-- src/scripts/MakeLinks | 3 +- src/src/pcre/ChangeLog | 273 +++ src/src/pcre/LICENCE | 2 +- src/src/pcre/Makefile | 8 +- src/src/pcre/dftables.c | 13 +- src/src/pcre/pcre.h | 16 +- src/src/pcre/pcre_compile.c | 3378 ++++++++++++++++--------------- src/src/pcre/pcre_config.c | 2 +- src/src/pcre/pcre_exec.c | 894 +++++--- src/src/pcre/pcre_fullinfo.c | 2 +- src/src/pcre/pcre_get.c | 3 +- src/src/pcre/pcre_globals.c | 17 +- src/src/pcre/pcre_internal.h | 377 ++-- src/src/pcre/pcre_maketables.c | 4 +- src/src/pcre/pcre_newline.c | 137 ++ src/src/pcre/pcre_printint.src | 82 +- src/src/pcre/pcre_study.c | 117 +- src/src/pcre/pcre_tables.c | 12 +- src/src/pcre/pcre_try_flipped.c | 2 +- src/src/pcre/pcre_version.c | 32 +- src/src/pcre/pcretest.c | 246 ++- src/src/pcre/ucp.h | 13 +- 25 files changed, 4058 insertions(+), 2689 deletions(-) create mode 100644 src/src/pcre/pcre_newline.c diff --git a/doc/doc-txt/ChangeLog b/doc/doc-txt/ChangeLog index b8795a62f..4a7f77c7a 100644 --- a/doc/doc-txt/ChangeLog +++ b/doc/doc-txt/ChangeLog @@ -1,4 +1,4 @@ -$Cambridge: exim/doc/doc-txt/ChangeLog,v 1.461 2007/01/23 14:34:02 ph10 Exp $ +$Cambridge: exim/doc/doc-txt/ChangeLog,v 1.462 2007/01/23 15:08:45 ph10 Exp $ Change log file for Exim from version 4.21 ------------------------------------------- @@ -52,6 +52,8 @@ PH/07 There was a bug in the dovecot authenticator such that the value of PH/08 Added $smtp_count_at_connection_start, deliberately with a long name. +PH/09 Installed PCRE release 7.0. + Exim version 4.66 ----------------- diff --git a/doc/doc-txt/pcrepattern.txt b/doc/doc-txt/pcrepattern.txt index 9712c86b4..f0e98e33c 100644 --- a/doc/doc-txt/pcrepattern.txt +++ b/doc/doc-txt/pcrepattern.txt @@ -1,5 +1,5 @@ This file contains the PCRE man page that describes the regular expressions -supported by PCRE version 6.7. Note that not all of the features are relevant +supported by PCRE version 7.0. Note that not all of the features are relevant in the context of Exim. In particular, the version of PCRE that is compiled with Exim does not include UTF-8 support, there is no mechanism for changing the options with which the PCRE functions are called, and features such as @@ -38,6 +38,9 @@ PCRE REGULAR EXPRESSION DETAILS function, and how it differs from the normal function, are discussed in the pcrematching page. + +CHARACTERS AND METACHARACTERS + A regular expression is a pattern that is matched against a subject string from left to right. Most characters stand for themselves in a pattern, and match the corresponding characters in the subject. As a @@ -62,8 +65,8 @@ PCRE REGULAR EXPRESSION DETAILS There are two different sets of metacharacters: those that are recog- nized anywhere in the pattern except within square brackets, and those - that are recognized in square brackets. Outside square brackets, the - metacharacters are as follows: + that are recognized within square brackets. Outside square brackets, + the metacharacters are as follows: \ general escape character with several uses ^ assert start of string (or line, in multiline mode) @@ -185,7 +188,7 @@ BACKSLASH Inside a character class, or if the decimal number is greater than 9 and there have not been that many capturing subpatterns, PCRE re-reads - up to three octal digits following the backslash, ane uses them to gen- + up to three octal digits following the backslash, and uses them to gen- erate a data character. Any subsequent digits stand for themselves. In non-UTF-8 mode, the value of a character specified in octal must be less than \400. In UTF-8 mode, values up to \777 are permitted. For @@ -212,13 +215,21 @@ BACKSLASH All the sequences that define a single character value can be used both inside and outside character classes. In addition, inside a character class, the sequence \b is interpreted as the backspace character (hex - 08), and the sequence \X is interpreted as the character "X". Outside a - character class, these sequences have different meanings (see below). + 08), and the sequences \R and \X are interpreted as the characters "R" + and "X", respectively. Outside a character class, these sequences have + different meanings (see below). + + Absolute and relative back references + + The sequence \g followed by a positive or negative number, optionally + enclosed in braces, is an absolute or relative back reference. Back + references are discussed later, following the discussion of parenthe- + sized subpatterns. Generic character types - The third use of backslash is for specifying generic character types. - The following are always recognized: + Another use of backslash is for specifying generic character types. The + following are always recognized: \d any decimal digit \D any character that is not a decimal digit @@ -255,6 +266,28 @@ BACKSLASH code character property support is available. The use of locales with Unicode is discouraged. + Newline sequences + + Outside a character class, the escape sequence \R matches any Unicode + newline sequence. This is an extension to Perl. In non-UTF-8 mode \R is + equivalent to the following: + + (?>\r\n|\n|\x0b|\f|\r|\x85) + + This is an example of an "atomic group", details of which are given + below. This particular group matches either the two-character sequence + CR followed by LF, or one of the single characters LF (linefeed, + U+000A), VT (vertical tab, U+000B), FF (formfeed, U+000C), CR (carriage + return, U+000D), or NEL (next line, U+0085). The two-character sequence + is treated as a single unit that cannot be split. + + In UTF-8 mode, two additional characters whose codepoints are greater + than 255 are added: LS (line separator, U+2028) and PS (paragraph sepa- + rator, U+2029). Unicode character property support is not needed for + these characters to be recognized. + + Inside a character class, \R matches the letter "R". + Unicode character properties When PCRE is built with Unicode character property support, three addi- @@ -281,15 +314,15 @@ BACKSLASH Those that are not part of an identified script are lumped together as "Common". The current list of scripts is: - Arabic, Armenian, Bengali, Bopomofo, Braille, Buginese, Buhid, Cana- - dian_Aboriginal, Cherokee, Common, Coptic, Cypriot, Cyrillic, Deseret, - Devanagari, Ethiopic, Georgian, Glagolitic, Gothic, Greek, Gujarati, - Gurmukhi, Han, Hangul, Hanunoo, Hebrew, Hiragana, Inherited, Kannada, - Katakana, Kharoshthi, Khmer, Lao, Latin, Limbu, Linear_B, Malayalam, - Mongolian, Myanmar, New_Tai_Lue, Ogham, Old_Italic, Old_Persian, Oriya, - Osmanya, Runic, Shavian, Sinhala, Syloti_Nagri, Syriac, Tagalog, Tag- - banwa, Tai_Le, Tamil, Telugu, Thaana, Thai, Tibetan, Tifinagh, - Ugaritic, Yi. + Arabic, Armenian, Balinese, Bengali, Bopomofo, Braille, Buginese, + Buhid, Canadian_Aboriginal, Cherokee, Common, Coptic, Cuneiform, + Cypriot, Cyrillic, Deseret, Devanagari, Ethiopic, Georgian, Glagolitic, + Gothic, Greek, Gujarati, Gurmukhi, Han, Hangul, Hanunoo, Hebrew, Hira- + gana, Inherited, Kannada, Katakana, Kharoshthi, Khmer, Lao, Latin, + Limbu, Linear_B, Malayalam, Mongolian, Myanmar, New_Tai_Lue, Nko, + Ogham, Old_Italic, Old_Persian, Oriya, Osmanya, Phags_Pa, Phoenician, + Runic, Shavian, Sinhala, Syloti_Nagri, Syriac, Tagalog, Tagbanwa, + Tai_Le, Tamil, Telugu, Thaana, Thai, Tibetan, Tifinagh, Ugaritic, Yi. Each character has exactly one general category property, specified by a two-letter abbreviation. For compatibility with Perl, negation can be @@ -382,7 +415,7 @@ BACKSLASH Simple assertions - The fourth use of backslash is for certain simple assertions. An asser- + The final use of backslash is for certain simple assertions. An asser- tion specifies a condition that has to be met at a particular point in a match, without consuming any characters from the subject string. The use of subpatterns for more complicated assertions is described below. @@ -390,10 +423,11 @@ BACKSLASH \b matches at a word boundary \B matches when not at a word boundary - \A matches at start of subject - \Z matches at end of subject or before newline at end - \z matches at end of subject - \G matches at first matching position in subject + \A matches at the start of the subject + \Z matches at the end of the subject + also matches before a newline at the end of the subject + \z matches only at the end of the subject + \G matches at the first matching position in the subject These assertions may not appear in character classes (but note that \b has a different meaning, namely the backspace character, inside a char- @@ -490,30 +524,34 @@ FULL STOP (PERIOD, DOT) Outside a character class, a dot in the pattern matches any one charac- ter in the subject string except (by default) a character that signi- fies the end of a line. In UTF-8 mode, the matched character may be - more than one byte long. When a line ending is defined as a single - character (CR or LF), dot never matches that character; when the two- - character sequence CRLF is used, dot does not match CR if it is immedi- - ately followed by LF, but otherwise it matches all characters (includ- - ing isolated CRs and LFs). - - The behaviour of dot with regard to newlines can be changed. If the - PCRE_DOTALL option is set, a dot matches any one character, without - exception. If newline is defined as the two-character sequence CRLF, it - takes two dots to match it. - - The handling of dot is entirely independent of the handling of circum- - flex and dollar, the only relationship being that they both involve + more than one byte long. + + When a line ending is defined as a single character, dot never matches + that character; when the two-character sequence CRLF is used, dot does + not match CR if it is immediately followed by LF, but otherwise it + matches all characters (including isolated CRs and LFs). When any Uni- + code line endings are being recognized, dot does not match CR or LF or + any of the other line ending characters. + + The behaviour of dot with regard to newlines can be changed. If the + PCRE_DOTALL option is set, a dot matches any one character, without + exception. If the two-character sequence CRLF is present in the subject + string, it takes two dots to match it. + + The handling of dot is entirely independent of the handling of circum- + flex and dollar, the only relationship being that they both involve newlines. Dot has no special meaning in a character class. MATCHING A SINGLE BYTE Outside a character class, the escape sequence \C matches any one byte, - both in and out of UTF-8 mode. Unlike a dot, it always matches CR and - LF. The feature is provided in Perl in order to match individual bytes - in UTF-8 mode. Because it breaks up UTF-8 characters into individual - bytes, what remains in the string may be a malformed UTF-8 string. For - this reason, the \C escape sequence is best avoided. + both in and out of UTF-8 mode. Unlike a dot, it always matches any + line-ending characters. The feature is provided in Perl in order to + match individual bytes in UTF-8 mode. Because it breaks up UTF-8 char- + acters into individual bytes, what remains in the string may be a mal- + formed UTF-8 string. For this reason, the \C escape sequence is best + avoided. PCRE does not allow \C to appear in lookbehind assertions (described below), because in UTF-8 mode this would make it impossible to calcu- @@ -560,11 +598,11 @@ SQUARE BRACKETS AND CHARACTER CLASSES PCRE is compiled with Unicode property support as well as with UTF-8 support. - Characters that might indicate line breaks (CR and LF) are never - treated in any special way when matching character classes, whatever - line-ending sequence is in use, and whatever setting of the PCRE_DOTALL - and PCRE_MULTILINE options is used. A class such as [^a] always matches - one of these characters. + Characters that might indicate line breaks are never treated in any + special way when matching character classes, whatever line-ending + sequence is in use, and whatever setting of the PCRE_DOTALL and + PCRE_MULTILINE options is used. A class such as [^a] always matches one + of these characters. The minus (hyphen) character can be used to specify a range of charac- ters in a character class. For example, [d-m] matches any letter @@ -696,26 +734,27 @@ INTERNAL OPTION SETTING PCRE extracts it into the global options (and it will therefore show up in data extracted by the pcre_fullinfo() function). - An option change within a subpattern affects only that part of the cur- - rent pattern that follows it, so + An option change within a subpattern (see below for a description of + subpatterns) affects only that part of the current pattern that follows + it, so (a(?i)b)c matches abc and aBc and no other strings (assuming PCRE_CASELESS is not - used). By this means, options can be made to have different settings - in different parts of the pattern. Any changes made in one alternative - do carry on into subsequent branches within the same subpattern. For + used). By this means, options can be made to have different settings + in different parts of the pattern. Any changes made in one alternative + do carry on into subsequent branches within the same subpattern. For example, (a(?i)b|c) - matches "ab", "aB", "c", and "C", even though when matching "C" the - first branch is abandoned before the option setting. This is because - the effects of option settings happen at compile time. There would be + matches "ab", "aB", "c", and "C", even though when matching "C" the + first branch is abandoned before the option setting. This is because + the effects of option settings happen at compile time. There would be some very weird behaviour otherwise. - The PCRE-specific options PCRE_DUPNAMES, PCRE_UNGREEDY, and PCRE_EXTRA - can be changed in the same way as the Perl-compatible options by using + The PCRE-specific options PCRE_DUPNAMES, PCRE_UNGREEDY, and PCRE_EXTRA + can be changed in the same way as the Perl-compatible options by using the characters J, U and X respectively. @@ -728,18 +767,18 @@ SUBPATTERNS cat(aract|erpillar|) - matches one of the words "cat", "cataract", or "caterpillar". Without - the parentheses, it would match "cataract", "erpillar" or the empty + matches one of the words "cat", "cataract", or "caterpillar". Without + the parentheses, it would match "cataract", "erpillar" or an empty string. - 2. It sets up the subpattern as a capturing subpattern. This means - that, when the whole pattern matches, that portion of the subject + 2. It sets up the subpattern as a capturing subpattern. This means + that, when the whole pattern matches, that portion of the subject string that matched the subpattern is passed back to the caller via the - ovector argument of pcre_exec(). Opening parentheses are counted from - left to right (starting from 1) to obtain numbers for the capturing + ovector argument of pcre_exec(). Opening parentheses are counted from + left to right (starting from 1) to obtain numbers for the capturing subpatterns. - For example, if the string "the red king" is matched against the pat- + For example, if the string "the red king" is matched against the pat- tern the ((red|white) (king|queen)) @@ -747,51 +786,56 @@ SUBPATTERNS the captured substrings are "red king", "red", and "king", and are num- bered 1, 2, and 3, respectively. - The fact that plain parentheses fulfil two functions is not always - helpful. There are often times when a grouping subpattern is required - without a capturing requirement. If an opening parenthesis is followed - by a question mark and a colon, the subpattern does not do any captur- - ing, and is not counted when computing the number of any subsequent - capturing subpatterns. For example, if the string "the white queen" is + The fact that plain parentheses fulfil two functions is not always + helpful. There are often times when a grouping subpattern is required + without a capturing requirement. If an opening parenthesis is followed + by a question mark and a colon, the subpattern does not do any captur- + ing, and is not counted when computing the number of any subsequent + capturing subpatterns. For example, if the string "the white queen" is matched against the pattern the ((?:red|white) (king|queen)) the captured substrings are "white queen" and "queen", and are numbered - 1 and 2. The maximum number of capturing subpatterns is 65535, and the - maximum depth of nesting of all subpatterns, both capturing and non- - capturing, is 200. + 1 and 2. The maximum number of capturing subpatterns is 65535. - As a convenient shorthand, if any option settings are required at the - start of a non-capturing subpattern, the option letters may appear + As a convenient shorthand, if any option settings are required at the + start of a non-capturing subpattern, the option letters may appear between the "?" and the ":". Thus the two patterns (?i:saturday|sunday) (?:(?i)saturday|sunday) match exactly the same set of strings. Because alternative branches are - tried from left to right, and options are not reset until the end of - the subpattern is reached, an option setting in one branch does affect - subsequent branches, so the above patterns match "SUNDAY" as well as + tried from left to right, and options are not reset until the end of + the subpattern is reached, an option setting in one branch does affect + subsequent branches, so the above patterns match "SUNDAY" as well as "Saturday". NAMED SUBPATTERNS - Identifying capturing parentheses by number is simple, but it can be - very hard to keep track of the numbers in complicated regular expres- - sions. Furthermore, if an expression is modified, the numbers may - change. To help with this difficulty, PCRE supports the naming of sub- - patterns, something that Perl does not provide. The Python syntax - (?P...) is used. References to capturing parentheses from other - parts of the pattern, such as backreferences, recursion, and condi- - tions, can be made by name as well as by number. - - Names consist of up to 32 alphanumeric characters and underscores. - Named capturing parentheses are still allocated numbers as well as - names. The PCRE API provides function calls for extracting the name-to- - number translation table from a compiled pattern. There is also a con- - venience function for extracting a captured substring by name. + Identifying capturing parentheses by number is simple, but it can be + very hard to keep track of the numbers in complicated regular expres- + sions. Furthermore, if an expression is modified, the numbers may + change. To help with this difficulty, PCRE supports the naming of sub- + patterns. This feature was not added to Perl until release 5.10. Python + had the feature earlier, and PCRE introduced it at release 4.0, using + the Python syntax. PCRE now supports both the Perl and the Python syn- + tax. + + In PCRE, a subpattern can be named in one of three ways: (?...) + or (?'name'...) as in Perl, or (?P...) as in Python. References + to capturing parentheses from other parts of the pattern, such as back- + references, recursion, and conditions, can be made by name as well as + by number. + + Names consist of up to 32 alphanumeric characters and underscores. + Named capturing parentheses are still allocated numbers as well as + names, exactly as if the names were not present. The PCRE API provides + function calls for extracting the name-to-number translation table from + a compiled pattern. There is also a convenience function for extracting + a captured substring by name. By default, a name must be unique within a pattern, but it is possible to relax this constraint by setting the PCRE_DUPNAMES option at compile @@ -801,15 +845,15 @@ NAMED SUBPATTERNS both cases you want to extract the abbreviation. This pattern (ignoring the line breaks) does the job: - (?PMon|Fri|Sun)(?:day)?| - (?PTue)(?:sday)?| - (?PWed)(?:nesday)?| - (?PThu)(?:rsday)?| - (?PSat)(?:urday)? + (?Mon|Fri|Sun)(?:day)?| + (?Tue)(?:sday)?| + (?Wed)(?:nesday)?| + (?Thu)(?:rsday)?| + (?Sat)(?:urday)? There are five capturing substrings, but only one is ever set after a match. The convenience function for extracting the data by name - returns the substring for the first, and in this example, the only, + returns the substring for the first (and in this example, the only) subpattern of that name that matched. This saves searching to find which numbered subpattern it was. If you make a reference to a non- unique named subpattern from elsewhere in the pattern, the one that @@ -824,9 +868,10 @@ REPETITION following items: a literal data character - the . metacharacter + the dot metacharacter the \C escape sequence the \X escape sequence (in UTF-8 mode with Unicode properties) + the \R escape sequence an escape such as \d that matches a single character a character class a back reference (see next section) @@ -866,8 +911,8 @@ REPETITION The quantifier {0} is permitted, causing the expression to behave as if the previous item and the quantifier were not present. - For convenience (and historical compatibility) the three most common - quantifiers have single-character abbreviations: + For convenience, the three most common quantifiers have single-charac- + ter abbreviations: * is equivalent to {0,} + is equivalent to {1,} @@ -919,7 +964,7 @@ REPETITION which matches one digit by preference, but can match two if that is the only way the rest of the pattern matches. - If the PCRE_UNGREEDY option is set (an option which is not available in + If the PCRE_UNGREEDY option is set (an option that is not available in Perl), the quantifiers are not greedy by default, but individual ones can be made greedy by following them with a question mark. In other words, it inverts the default behaviour. @@ -930,24 +975,25 @@ REPETITION minimum or maximum. If a pattern starts with .* or .{0,} and the PCRE_DOTALL option (equiv- - alent to Perl's /s) is set, thus allowing the . to match newlines, the - pattern is implicitly anchored, because whatever follows will be tried - against every character position in the subject string, so there is no - point in retrying the overall match at any position after the first. - PCRE normally treats such a pattern as though it were preceded by \A. - - In cases where it is known that the subject string contains no new- - lines, it is worth setting PCRE_DOTALL in order to obtain this opti- + alent to Perl's /s) is set, thus allowing the dot to match newlines, + the pattern is implicitly anchored, because whatever follows will be + tried against every character position in the subject string, so there + is no point in retrying the overall match at any position after the + first. PCRE normally treats such a pattern as though it were preceded + by \A. + + In cases where it is known that the subject string contains no new- + lines, it is worth setting PCRE_DOTALL in order to obtain this opti- mization, or alternatively using ^ to indicate anchoring explicitly. - However, there is one situation where the optimization cannot be used. - When .* is inside capturing parentheses that are the subject of a - backreference elsewhere in the pattern, a match at the start may fail, - and a later one succeed. Consider, for example: + However, there is one situation where the optimization cannot be used. + When .* is inside capturing parentheses that are the subject of a + backreference elsewhere in the pattern, a match at the start may fail + where a later one succeeds. Consider, for example: (.*)abc\1 - If the subject is "xyz123abc123" the match point is the fourth charac- + If the subject is "xyz123abc123" the match point is the fourth charac- ter. For this reason, such a pattern is not implicitly anchored. When a capturing subpattern is repeated, the value captured is the sub- @@ -956,8 +1002,8 @@ REPETITION (tweedle[dume]{3}\s*)+ has matched "tweedledum tweedledee" the value of the captured substring - is "tweedledee". However, if there are nested capturing subpatterns, - the corresponding captured values may have been set in previous itera- + is "tweedledee". However, if there are nested capturing subpatterns, + the corresponding captured values may have been set in previous itera- tions. For example, after /(a|(b))+/ @@ -967,12 +1013,13 @@ REPETITION ATOMIC GROUPING AND POSSESSIVE QUANTIFIERS - With both maximizing and minimizing repetition, failure of what follows - normally causes the repeated item to be re-evaluated to see if a dif- - ferent number of repeats allows the rest of the pattern to match. Some- - times it is useful to prevent this, either to change the nature of the - match, or to cause it fail earlier than it otherwise might, when the - author of the pattern knows there is no point in carrying on. + With both maximizing ("greedy") and minimizing ("ungreedy" or "lazy") + repetition, failure of what follows normally causes the repeated item + to be re-evaluated to see if a different number of repeats allows the + rest of the pattern to match. Sometimes it is useful to prevent this, + either to change the nature of the match, or to cause it fail earlier + than it otherwise might, when the author of the pattern knows there is + no point in carrying on. Consider, for example, the pattern \d+foo when applied to the subject line @@ -986,10 +1033,9 @@ ATOMIC GROUPING AND POSSESSIVE QUANTIFIERS the means for specifying that once a subpattern has matched, it is not to be re-evaluated in this way. - If we use atomic grouping for the previous example, the matcher would - give up immediately on failing to match "foo" the first time. The nota- - tion is a kind of special parenthesis, starting with (?> as in this - example: + If we use atomic grouping for the previous example, the matcher gives + up immediately on failing to match "foo" the first time. The notation + is a kind of special parenthesis, starting with (?> as in this example: (?>\d+)foo @@ -1021,63 +1067,95 @@ ATOMIC GROUPING AND POSSESSIVE QUANTIFIERS Possessive quantifiers are always greedy; the setting of the PCRE_UNGREEDY option is ignored. They are a convenient notation for the simpler forms of atomic group. However, there is no difference in the - meaning or processing of a possessive quantifier and the equivalent - atomic group. - - The possessive quantifier syntax is an extension to the Perl syntax. - Jeffrey Friedl originated the idea (and the name) in the first edition - of his book. Mike McCloskey liked it, so implemented it when he built - Sun's Java package, and PCRE copied it from there. - - When a pattern contains an unlimited repeat inside a subpattern that - can itself be repeated an unlimited number of times, the use of an - atomic group is the only way to avoid some failing matches taking a + meaning of a possessive quantifier and the equivalent atomic group, + though there may be a performance difference; possessive quantifiers + should be slightly faster. + + The possessive quantifier syntax is an extension to the Perl 5.8 syn- + tax. Jeffrey Friedl originated the idea (and the name) in the first + edition of his book. Mike McCloskey liked it, so implemented it when he + built Sun's Java package, and PCRE copied it from there. It ultimately + found its way into Perl at release 5.10. + + PCRE has an optimization that automatically "possessifies" certain sim- + ple pattern constructs. For example, the sequence A+B is treated as + A++B because there is no point in backtracking into a sequence of A's + when B must follow. + + When a pattern contains an unlimited repeat inside a subpattern that + can itself be repeated an unlimited number of times, the use of an + atomic group is the only way to avoid some failing matches taking a very long time indeed. The pattern (\D+|<\d+>)*[!?] - matches an unlimited number of substrings that either consist of non- - digits, or digits enclosed in <>, followed by either ! or ?. When it + matches an unlimited number of substrings that either consist of non- + digits, or digits enclosed in <>, followed by either ! or ?. When it matches, it runs quickly. However, if it is applied to aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa - it takes a long time before reporting failure. This is because the - string can be divided between the internal \D+ repeat and the external - * repeat in a large number of ways, and all have to be tried. (The - example uses [!?] rather than a single character at the end, because - both PCRE and Perl have an optimization that allows for fast failure - when a single character is used. They remember the last single charac- - ter that is required for a match, and fail early if it is not present - in the string.) If the pattern is changed so that it uses an atomic + it takes a long time before reporting failure. This is because the + string can be divided between the internal \D+ repeat and the external + * repeat in a large number of ways, and all have to be tried. (The + example uses [!?] rather than a single character at the end, because + both PCRE and Perl have an optimization that allows for fast failure + when a single character is used. They remember the last single charac- + ter that is required for a match, and fail early if it is not present + in the string.) If the pattern is changed so that it uses an atomic group, like this: ((?>\D+)|<\d+>)*[!?] - sequences of non-digits cannot be broken, and failure happens quickly. + sequences of non-digits cannot be broken, and failure happens quickly. BACK REFERENCES Outside a character class, a backslash followed by a digit greater than 0 (and possibly further digits) is a back reference to a capturing sub- - pattern earlier (that is, to its left) in the pattern, provided there + pattern earlier (that is, to its left) in the pattern, provided there have been that many previous capturing left parentheses. However, if the decimal number following the backslash is less than 10, - it is always taken as a back reference, and causes an error only if - there are not that many capturing left parentheses in the entire pat- - tern. In other words, the parentheses that are referenced need not be - to the left of the reference for numbers less than 10. A "forward back - reference" of this type can make sense when a repetition is involved - and the subpattern to the right has participated in an earlier itera- + it is always taken as a back reference, and causes an error only if + there are not that many capturing left parentheses in the entire pat- + tern. In other words, the parentheses that are referenced need not be + to the left of the reference for numbers less than 10. A "forward back + reference" of this type can make sense when a repetition is involved + and the subpattern to the right has participated in an earlier itera- tion. - It is not possible to have a numerical "forward back reference" to sub- - pattern whose number is 10 or more. However, a back reference to any - subpattern is possible using named parentheses (see below). See also - the subsection entitled "Non-printing characters" above for further - details of the handling of digits following a backslash. + It is not possible to have a numerical "forward back reference" to a + subpattern whose number is 10 or more using this syntax because a + sequence such as \50 is interpreted as a character defined in octal. + See the subsection entitled "Non-printing characters" above for further + details of the handling of digits following a backslash. There is no + such problem when named parentheses are used. A back reference to any + subpattern is possible using named parentheses (see below). + + Another way of avoiding the ambiguity inherent in the use of digits + following a backslash is to use the \g escape sequence, which is a fea- + ture introduced in Perl 5.10. This escape must be followed by a posi- + tive or a negative number, optionally enclosed in braces. These exam- + ples are all identical: + + (ring), \1 + (ring), \g1 + (ring), \g{1} + + A positive number specifies an absolute reference without the ambiguity + that is present in the older syntax. It is also useful when literal + digits follow the reference. A negative number is a relative reference. + Consider this example: + + (abc(def)ghi)\g{-1} + + The sequence \g{-1} is a reference to the most recently started captur- + ing subpattern before \g, that is, is it equivalent to \2. Similarly, + \g{-2} would be equivalent to \1. The use of relative references can be + helpful in long patterns, and also in patterns that are created by + joining together fragments that contain references within themselves. A back reference matches whatever actually matched the capturing sub- pattern in the current subject string, rather than anything matching @@ -1096,62 +1174,64 @@ BACK REFERENCES matches "rah rah" and "RAH RAH", but not "RAH rah", even though the original capturing subpattern is matched caselessly. - Back references to named subpatterns use the Python syntax (?P=name). - We could rewrite the above example as follows: + Back references to named subpatterns use the Perl syntax \k or + \k'name' or the Python syntax (?P=name). We could rewrite the above + example in either of the following ways: + (?(?i)rah)\s+\k (?P(?i)rah)\s+(?P=p1) - A subpattern that is referenced by name may appear in the pattern + A subpattern that is referenced by name may appear in the pattern before or after the reference. - There may be more than one back reference to the same subpattern. If a - subpattern has not actually been used in a particular match, any back + There may be more than one back reference to the same subpattern. If a + subpattern has not actually been used in a particular match, any back references to it always fail. For example, the pattern (a|(bc))\2 - always fails if it starts to match "a" rather than "bc". Because there - may be many capturing parentheses in a pattern, all digits following - the backslash are taken as part of a potential back reference number. + always fails if it starts to match "a" rather than "bc". Because there + may be many capturing parentheses in a pattern, all digits following + the backslash are taken as part of a potential back reference number. If the pattern continues with a digit character, some delimiter must be - used to terminate the back reference. If the PCRE_EXTENDED option is - set, this can be whitespace. Otherwise an empty comment (see "Com- + used to terminate the back reference. If the PCRE_EXTENDED option is + set, this can be whitespace. Otherwise an empty comment (see "Com- ments" below) can be used. - A back reference that occurs inside the parentheses to which it refers - fails when the subpattern is first used, so, for example, (a\1) never - matches. However, such references can be useful inside repeated sub- + A back reference that occurs inside the parentheses to which it refers + fails when the subpattern is first used, so, for example, (a\1) never + matches. However, such references can be useful inside repeated sub- patterns. For example, the pattern (a|b\1)+ matches any number of "a"s and also "aba", "ababbaa" etc. At each iter- - ation of the subpattern, the back reference matches the character - string corresponding to the previous iteration. In order for this to - work, the pattern must be such that the first iteration does not need - to match the back reference. This can be done using alternation, as in + ation of the subpattern, the back reference matches the character + string corresponding to the previous iteration. In order for this to + work, the pattern must be such that the first iteration does not need + to match the back reference. This can be done using alternation, as in the example above, or by a quantifier with a minimum of zero. ASSERTIONS - An assertion is a test on the characters following or preceding the - current matching point that does not actually consume any characters. - The simple assertions coded as \b, \B, \A, \G, \Z, \z, ^ and $ are + An assertion is a test on the characters following or preceding the + current matching point that does not actually consume any characters. + The simple assertions coded as \b, \B, \A, \G, \Z, \z, ^ and $ are described above. - More complicated assertions are coded as subpatterns. There are two - kinds: those that look ahead of the current position in the subject - string, and those that look behind it. An assertion subpattern is - matched in the normal way, except that it does not cause the current + More complicated assertions are coded as subpatterns. There are two + kinds: those that look ahead of the current position in the subject + string, and those that look behind it. An assertion subpattern is + matched in the normal way, except that it does not cause the current matching position to be changed. - Assertion subpatterns are not capturing subpatterns, and may not be - repeated, because it makes no sense to assert the same thing several - times. If any kind of assertion contains capturing subpatterns within - it, these are counted for the purposes of numbering the capturing sub- + Assertion subpatterns are not capturing subpatterns, and may not be + repeated, because it makes no sense to assert the same thing several + times. If any kind of assertion contains capturing subpatterns within + it, these are counted for the purposes of numbering the capturing sub- patterns in the whole pattern. However, substring capturing is carried - out only for positive assertions, because it does not make sense for + out only for positive assertions, because it does not make sense for negative assertions. Lookahead assertions @@ -1161,37 +1241,37 @@ ASSERTIONS \w+(?=;) - matches a word followed by a semicolon, but does not include the semi- + matches a word followed by a semicolon, but does not include the semi- colon in the match, and foo(?!bar) - matches any occurrence of "foo" that is not followed by "bar". Note + matches any occurrence of "foo" that is not followed by "bar". Note that the apparently similar pattern (?!foo)bar - does not find an occurrence of "bar" that is preceded by something - other than "foo"; it finds any occurrence of "bar" whatsoever, because + does not find an occurrence of "bar" that is preceded by something + other than "foo"; it finds any occurrence of "bar" whatsoever, because the assertion (?!foo) is always true when the next three characters are "bar". A lookbehind assertion is needed to achieve the other effect. If you want to force a matching failure at some point in a pattern, the - most convenient way to do it is with (?!) because an empty string - always matches, so an assertion that requires there not to be an empty + most convenient way to do it is with (?!) because an empty string + always matches, so an assertion that requires there not to be an empty string must always fail. Lookbehind assertions - Lookbehind assertions start with (?<= for positive assertions and (?.*)(?<=abcd) - - or, equivalently, using the possessive quantifier syntax, - ^.*+(?<=abcd) - there can be no backtracking for the .* item; it can match only the - entire string. The subsequent lookbehind assertion does a single test - on the last four characters. If it fails, the match fails immediately. - For long strings, this approach makes a significant difference to the + there can be no backtracking for the .*+ item; it can match only the + entire string. The subsequent lookbehind assertion does a single test + on the last four characters. If it fails, the match fails immediately. + For long strings, this approach makes a significant difference to the processing time. Using multiple assertions @@ -1261,18 +1337,18 @@ ASSERTIONS (?<=\d{3})(?)...) or (?('name')...) to test for a + used subpattern by name. For compatibility with earlier versions of + PCRE, which had this facility before Perl, the syntax (?(name)...) is + also recognized. However, there is a possible ambiguity with this syn- + tax, because subpattern names may consist entirely of digits. PCRE + looks first for a named subpattern; if it cannot find one and the name + consists entirely of digits, PCRE looks for a subpattern of that num- + ber, which must be greater than zero. Using subpattern names that con- + sist entirely of digits is not recommended. + + Rewriting the above example to use a named subpattern gives this: - (?P \( )? [^()]+ (?(OPEN) \) ) + (? \( )? [^()]+ (?() \) ) + + + Checking for pattern recursion If the condition is the string (R), and there is no subpattern with the - name R, the condition is satisfied if a recursive call to the pattern - or subpattern has been made. At "top level", the condition is false. - This is a PCRE extension. Recursive patterns are described in the next - section. + name R, the condition is true if a recursive call to the whole pattern + or any subpattern has been made. If digits or a name preceded by amper- + sand follow the letter R, for example: + + (?(R3)...) or (?(R&name)...) + + the condition is true if the most recent recursion is into the subpat- + tern whose number or name is given. This condition does not check the + entire recursion stack. + + At "top level", all these recursion test conditions are false. Recur- + sive patterns are described below. + + Defining subpatterns for use by reference only + + If the condition is the string (DEFINE), and there is no subpattern + with the name DEFINE, the condition is always false. In this case, + there may be only one alternative in the subpattern. It is always + skipped if control reaches this point in the pattern; the idea of + DEFINE is that it can be used to define "subroutines" that can be ref- + erenced from elsewhere. (The use of "subroutines" is described below.) + For example, a pattern to match an IPv4 address could be written like + this (ignore whitespace and line breaks): + + (?(DEFINE) (? 2[0-4]\d | 25[0-5] | 1\d\d | [1-9]?\d) ) + \b (?&byte) (\.(?&byte)){3} \b + + The first part of the pattern is a DEFINE group inside which a another + group named "byte" is defined. This matches an individual component of + an IPv4 address (a number less than 256). When matching takes place, + this part of the pattern is skipped because DEFINE acts like a false + condition. + + The rest of the pattern uses references to the named group to match the + four dot-separated components of an IPv4 address, insisting on a word + boundary at each end. - If the condition is not a sequence of digits or (R), it must be an + Assertion conditions + + If the condition is not in any of the above formats, it must be an assertion. This may be a positive or negative lookahead or lookbehind assertion. Consider this pattern, again containing non-significant white space, and with the two alternatives on the second line: @@ -1375,110 +1500,116 @@ RECURSIVE PATTERNS unlimited nested parentheses. Without the use of recursion, the best that can be done is to use a pattern that matches up to some fixed depth of nesting. It is not possible to handle an arbitrary nesting - depth. Perl provides a facility that allows regular expressions to - recurse (amongst other things). It does this by interpolating Perl code - in the expression at run time, and the code can refer to the expression - itself. A Perl pattern to solve the parentheses problem can be created - like this: + depth. + + For some time, Perl has provided a facility that allows regular expres- + sions to recurse (amongst other things). It does this by interpolating + Perl code in the expression at run time, and the code can refer to the + expression itself. A Perl pattern using code interpolation to solve the + parentheses problem can be created like this: $re = qr{\( (?: (?>[^()]+) | (?p{$re}) )* \)}x; The (?p{...}) item interpolates Perl code at run time, and in this case - refers recursively to the pattern in which it appears. Obviously, PCRE - cannot support the interpolation of Perl code. Instead, it supports - some special syntax for recursion of the entire pattern, and also for - individual subpattern recursion. + refers recursively to the pattern in which it appears. + + Obviously, PCRE cannot support the interpolation of Perl code. Instead, + it supports special syntax for recursion of the entire pattern, and + also for individual subpattern recursion. After its introduction in + PCRE and Python, this kind of recursion was introduced into Perl at + release 5.10. - The special item that consists of (? followed by a number greater than + A special item that consists of (? followed by a number greater than zero and a closing parenthesis is a recursive call of the subpattern of - the given number, provided that it occurs inside that subpattern. (If - not, it is a "subroutine" call, which is described in the next sec- - tion.) The special item (?R) is a recursive call of the entire regular - expression. + the given number, provided that it occurs inside that subpattern. (If + not, it is a "subroutine" call, which is described in the next sec- + tion.) The special item (?R) or (?0) is a recursive call of the entire + regular expression. - A recursive subpattern call is always treated as an atomic group. That - is, once it has matched some of the subject string, it is never re- - entered, even if it contains untried alternatives and there is a subse- - quent matching failure. + In PCRE (like Python, but unlike Perl), a recursive subpattern call is + always treated as an atomic group. That is, once it has matched some of + the subject string, it is never re-entered, even if it contains untried + alternatives and there is a subsequent matching failure. - This PCRE pattern solves the nested parentheses problem (assume the + This PCRE pattern solves the nested parentheses problem (assume the PCRE_EXTENDED option is set so that white space is ignored): \( ( (?>[^()]+) | (?R) )* \) - First it matches an opening parenthesis. Then it matches any number of - substrings which can either be a sequence of non-parentheses, or a - recursive match of the pattern itself (that is, a correctly parenthe- + First it matches an opening parenthesis. Then it matches any number of + substrings which can either be a sequence of non-parentheses, or a + recursive match of the pattern itself (that is, a correctly parenthe- sized substring). Finally there is a closing parenthesis. - If this were part of a larger pattern, you would not want to recurse + If this were part of a larger pattern, you would not want to recurse the entire pattern, so instead you could use this: ( \( ( (?>[^()]+) | (?1) )* \) ) - We have put the pattern into parentheses, and caused the recursion to - refer to them instead of the whole pattern. In a larger pattern, keep- - ing track of parenthesis numbers can be tricky. It may be more conve- - nient to use named parentheses instead. For this, PCRE uses (?P>name), - which is an extension to the Python syntax that PCRE uses for named - parentheses (Perl does not provide named parentheses). We could rewrite - the above example as follows: + We have put the pattern into parentheses, and caused the recursion to + refer to them instead of the whole pattern. In a larger pattern, keep- + ing track of parenthesis numbers can be tricky. It may be more conve- + nient to use named parentheses instead. The Perl syntax for this is + (?&name); PCRE's earlier syntax (?P>name) is also supported. We could + rewrite the above example as follows: - (?P \( ( (?>[^()]+) | (?P>pn) )* \) ) + (? \( ( (?>[^()]+) | (?&pn) )* \) ) - This particular example pattern contains nested unlimited repeats, and - so the use of atomic grouping for matching strings of non-parentheses - is important when applying the pattern to strings that do not match. - For example, when this pattern is applied to + If there is more than one subpattern with the same name, the earliest + one is used. This particular example pattern contains nested unlimited + repeats, and so the use of atomic grouping for matching strings of non- + parentheses is important when applying the pattern to strings that do + not match. For example, when this pattern is applied to (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa() - it yields "no match" quickly. However, if atomic grouping is not used, - the match runs for a very long time indeed because there are so many - different ways the + and * repeats can carve up the subject, and all + it yields "no match" quickly. However, if atomic grouping is not used, + the match runs for a very long time indeed because there are so many + different ways the + and * repeats can carve up the subject, and all have to be tested before failure can be reported. At the end of a match, the values set for any capturing subpatterns are those from the outermost level of the recursion at which the subpattern - value is set. If you want to obtain intermediate values, a callout - function can be used (see the next section and the pcrecallout documen- - tation). If the pattern above is matched against + value is set. If you want to obtain intermediate values, a callout + function can be used (see below and the pcrecallout documentation). If + the pattern above is matched against (ab(cd)ef) - the value for the capturing parentheses is "ef", which is the last - value taken on at the top level. If additional parentheses are added, + the value for the capturing parentheses is "ef", which is the last + value taken on at the top level. If additional parentheses are added, giving \( ( ( (?>[^()]+) | (?R) )* ) \) ^ ^ ^ ^ - the string they capture is "ab(cd)ef", the contents of the top level - parentheses. If there are more than 15 capturing parentheses in a pat- + the string they capture is "ab(cd)ef", the contents of the top level + parentheses. If there are more than 15 capturing parentheses in a pat- tern, PCRE has to obtain extra memory to store data during a recursion, - which it does by using pcre_malloc, freeing it via pcre_free after- - wards. If no memory can be obtained, the match fails with the + which it does by using pcre_malloc, freeing it via pcre_free after- + wards. If no memory can be obtained, the match fails with the PCRE_ERROR_NOMEMORY error. - Do not confuse the (?R) item with the condition (R), which tests for - recursion. Consider this pattern, which matches text in angle brack- - ets, allowing for arbitrary nesting. Only digits are allowed in nested - brackets (that is, when recursing), whereas any characters are permit- + Do not confuse the (?R) item with the condition (R), which tests for + recursion. Consider this pattern, which matches text in angle brack- + ets, allowing for arbitrary nesting. Only digits are allowed in nested + brackets (that is, when recursing), whereas any characters are permit- ted at the outer level. < (?: (?(R) \d++ | [^<>]*+) | (?R)) * > - In this pattern, (?(R) is the start of a conditional subpattern, with - two different alternatives for the recursive and non-recursive cases. + In this pattern, (?(R) is the start of a conditional subpattern, with + two different alternatives for the recursive and non-recursive cases. The (?R) item is the actual recursive call. SUBPATTERNS AS SUBROUTINES If the syntax for a recursive subpattern reference (either by number or - by name) is used outside the parentheses to which it refers, it oper- - ates like a subroutine in a programming language. An earlier example + by name) is used outside the parentheses to which it refers, it oper- + ates like a subroutine in a programming language. The "called" subpat- + tern may be defined before or after the reference. An earlier example pointed out that the pattern (sens|respons)e and \1ibility @@ -1489,15 +1620,23 @@ SUBPATTERNS AS SUBROUTINES (sens|respons)e and (?1)ibility is used, it does match "sense and responsibility" as well as the other - two strings. Such references, if given numerically, must follow the - subpattern to which they refer. However, named references can refer to - later subpatterns. + two strings. Another example is given in the discussion of DEFINE + above. Like recursive subpatterns, a "subroutine" call is always treated as an - atomic group. That is, once it has matched some of the subject string, - it is never re-entered, even if it contains untried alternatives and + atomic group. That is, once it has matched some of the subject string, + it is never re-entered, even if it contains untried alternatives and there is a subsequent matching failure. + When a subpattern is used as a subroutine, processing options such as + case-independence are fixed when the subpattern is defined. They cannot + be changed for different calls. For example, consider this pattern: + + (abc)(?i:(?1)) + + It matches "abcabc". It does not match "abcABC" because the change of + processing option does not affect the called subpattern. + CALLOUTS @@ -1533,5 +1672,10 @@ CALLOUTS gether. A complete description of the interface to the callout function is given in the pcrecallout documentation. -Last updated: 06 June 2006 + +SEE ALSO + + pcreapi(3), pcrecallout(3), pcrematching(3), pcre(3). + +Last updated: 06 December 2006 Copyright (c) 1997-2006 University of Cambridge. diff --git a/doc/doc-txt/pcretest.txt b/doc/doc-txt/pcretest.txt index dfa03b80b..1a0424c20 100644 --- a/doc/doc-txt/pcretest.txt +++ b/doc/doc-txt/pcretest.txt @@ -24,18 +24,24 @@ SYNOPSIS OPTIONS + -b Behave as if each regex has the /B (show bytecode) modifier; + the internal form is output after compilation. + -C Output the version number of the PCRE library, and all avail- - able information about the optional features that are + able information about the optional features that are included, and then exit. - -d Behave as if each regex has the /D (debug) modifier; the - internal form is output after compilation. + -d Behave as if each regex has the /D (debug) modifier; the + internal form and information about the compiled pattern is + output after compilation; -d is equivalent to -b -i. -dfa Behave as if each data line contains the \D escape sequence; this causes the alternative matching function, pcre_dfa_exec(), to be used instead of the standard pcre_exec() function (more detail is given below). + -help Output a brief summary these options and then exit. + -i Behave as if each regex has the /I modifier; information about the compiled pattern is given after compilation. @@ -45,10 +51,12 @@ OPTIONS pcretest, -s is a synonym for -m. -o osize Set the number of elements in the output vector that is used - when calling pcre_exec() to be osize. The default value is - 45, which is enough for 14 capturing subexpressions. The vec- - tor size can be changed for individual matching calls by - including \O in the data line (see below). + when calling pcre_exec() or pcre_dfa_exec() to be osize. The + default value is 45, which is enough for 14 capturing subex- + pressions for pcre_exec() or 22 different matches for + pcre_dfa_exec(). The vector size can be changed for individ- + ual matching calls by including \O in the data line (see + below). -p Behave as if each regex has the /P modifier; the POSIX wrap- per API is used to call PCRE. None of the other options has @@ -64,7 +72,13 @@ OPTIONS and output resulting time per compile or match (in millisec- onds). Do not set -m with -t, because you will then get the size output a zillion times, and the timing will be dis- - torted. + torted. You can control the number of iterations that are + used for timing by following -t with a number (as a separate + item on the command line). For example, "-t 1000" would iter- + ate 1000 times. The default is to iterate 500000 times. + + -tm This is like -t except that it times only the matching phase, + not the compile or study phases. DESCRIPTION @@ -82,53 +96,54 @@ DESCRIPTION Each data line is matched separately and independently. If you want to do multi-line matches, you have to use the \n escape sequence (or \r or - \r\n, depending on the newline setting) in a single line of input to - encode the newline characters. There is no limit on the length of data - lines; the input buffer is automatically extended if it is too small. + \r\n, etc., depending on the newline setting) in a single line of input + to encode the newline sequences. There is no limit on the length of + data lines; the input buffer is automatically extended if it is too + small. - An empty line signals the end of the data lines, at which point a new - regular expression is read. The regular expressions are given enclosed + An empty line signals the end of the data lines, at which point a new + regular expression is read. The regular expressions are given enclosed in any non-alphanumeric delimiters other than backslash, for example: /(a|bc)x+yz/ - White space before the initial delimiter is ignored. A regular expres- - sion may be continued over several input lines, in which case the new- - line characters are included within it. It is possible to include the + White space before the initial delimiter is ignored. A regular expres- + sion may be continued over several input lines, in which case the new- + line characters are included within it. It is possible to include the delimiter within the pattern by escaping it, for example /abc\/def/ - If you do so, the escape and the delimiter form part of the pattern, - but since delimiters are always non-alphanumeric, this does not affect - its interpretation. If the terminating delimiter is immediately fol- + If you do so, the escape and the delimiter form part of the pattern, + but since delimiters are always non-alphanumeric, this does not affect + its interpretation. If the terminating delimiter is immediately fol- lowed by a backslash, for example, /abc/\ - then a backslash is added to the end of the pattern. This is done to - provide a way of testing the error condition that arises if a pattern + then a backslash is added to the end of the pattern. This is done to + provide a way of testing the error condition that arises if a pattern finishes with a backslash, because /abc\/ - is interpreted as the first line of a pattern that starts with "abc/", + is interpreted as the first line of a pattern that starts with "abc/", causing pcretest to read the next line as a continuation of the regular expression. PATTERN MODIFIERS - A pattern may be followed by any number of modifiers, which are mostly - single characters. Following Perl usage, these are referred to below - as, for example, "the /i modifier", even though the delimiter of the - pattern need not always be a slash, and no slash is used when writing - modifiers. Whitespace may appear between the final pattern delimiter + A pattern may be followed by any number of modifiers, which are mostly + single characters. Following Perl usage, these are referred to below + as, for example, "the /i modifier", even though the delimiter of the + pattern need not always be a slash, and no slash is used when writing + modifiers. Whitespace may appear between the final pattern delimiter and the first modifier, and between the modifiers themselves. The /i, /m, /s, and /x modifiers set the PCRE_CASELESS, PCRE_MULTILINE, - PCRE_DOTALL, or PCRE_EXTENDED options, respectively, when pcre_com- - pile() is called. These four modifier letters have the same effect as + PCRE_DOTALL, or PCRE_EXTENDED options, respectively, when pcre_com- + pile() is called. These four modifier letters have the same effect as they do in Perl. For example: /caseless/i @@ -147,10 +162,16 @@ PATTERN MODIFIERS / PCRE_NEWLINE_CR / PCRE_NEWLINE_LF / PCRE_NEWLINE_CRLF + / PCRE_NEWLINE_ANY + + Those specifying line ending sequencess are literal strings as shown. + This example sets multiline matching with CRLF as the line ending + sequence: - Those specifying line endings are literal strings as shown. Details of - the meanings of these PCRE options are given in the pcreapi documenta- - tion. + /^abc/m + + Details of the meanings of these PCRE options are given in the pcreapi + documentation. Finding all matches in a string @@ -180,79 +201,80 @@ PATTERN MODIFIERS remainder of the subject string. This is useful for tests where the subject contains multiple copies of the same substring. - The /L modifier must be followed directly by the name of a locale, for + The /B modifier is a debugging feature. It requests that pcretest out- + put a representation of the compiled byte code after compilation. + + The /L modifier must be followed directly by the name of a locale, for example, /pattern/Lfr_FR For this reason, it must be the last modifier. The given locale is set, - pcre_maketables() is called to build a set of character tables for the - locale, and this is then passed to pcre_compile() when compiling the - regular expression. Without an /L modifier, NULL is passed as the - tables pointer; that is, /L applies only to the expression on which it + pcre_maketables() is called to build a set of character tables for the + locale, and this is then passed to pcre_compile() when compiling the + regular expression. Without an /L modifier, NULL is passed as the + tables pointer; that is, /L applies only to the expression on which it appears. - The /I modifier requests that pcretest output information about the - compiled pattern (whether it is anchored, has a fixed first character, - and so on). It does this by calling pcre_fullinfo() after compiling a - pattern. If the pattern is studied, the results of that are also out- + The /I modifier requests that pcretest output information about the + compiled pattern (whether it is anchored, has a fixed first character, + and so on). It does this by calling pcre_fullinfo() after compiling a + pattern. If the pattern is studied, the results of that are also out- put. - The /D modifier is a PCRE debugging feature, which also assumes /I. It - causes the internal form of compiled regular expressions to be output - after compilation. If the pattern was studied, the information returned - is also output. + The /D modifier is a PCRE debugging feature, and is equivalent to /BI, + that is, both the /B and the /I modifiers. The /F modifier causes pcretest to flip the byte order of the fields in - the compiled pattern that contain 2-byte and 4-byte numbers. This - facility is for testing the feature in PCRE that allows it to execute + the compiled pattern that contain 2-byte and 4-byte numbers. This + facility is for testing the feature in PCRE that allows it to execute patterns that were compiled on a host with a different endianness. This - feature is not available when the POSIX interface to PCRE is being - used, that is, when the /P pattern modifier is specified. See also the + feature is not available when the POSIX interface to PCRE is being + used, that is, when the /P pattern modifier is specified. See also the section about saving and reloading compiled patterns below. - The /S modifier causes pcre_study() to be called after the expression + The /S modifier causes pcre_study() to be called after the expression has been compiled, and the results used when the expression is matched. - The /M modifier causes the size of memory block used to hold the com- + The /M modifier causes the size of memory block used to hold the com- piled pattern to be output. - The /P modifier causes pcretest to call PCRE via the POSIX wrapper API - rather than its native API. When this is done, all other modifiers - except /i, /m, and /+ are ignored. REG_ICASE is set if /i is present, - and REG_NEWLINE is set if /m is present. The wrapper functions force - PCRE_DOLLAR_ENDONLY always, and PCRE_DOTALL unless REG_NEWLINE is set. + The /P modifier causes pcretest to call PCRE via the POSIX wrapper API + rather than its native API. When this is done, all other modifiers + except /i, /m, and /+ are ignored. REG_ICASE is set if /i is present, + and REG_NEWLINE is set if /m is present. The wrapper functions force + PCRE_DOLLAR_ENDONLY always, and PCRE_DOTALL unless REG_NEWLINE is set. - The /8 modifier causes pcretest to call PCRE with the PCRE_UTF8 option - set. This turns on support for UTF-8 character handling in PCRE, pro- - vided that it was compiled with this support enabled. This modifier + The /8 modifier causes pcretest to call PCRE with the PCRE_UTF8 option + set. This turns on support for UTF-8 character handling in PCRE, pro- + vided that it was compiled with this support enabled. This modifier also causes any non-printing characters in output strings to be printed using the \x{hh...} notation if they are valid UTF-8 sequences. - If the /? modifier is used with /8, it causes pcretest to call - pcre_compile() with the PCRE_NO_UTF8_CHECK option, to suppress the + If the /? modifier is used with /8, it causes pcretest to call + pcre_compile() with the PCRE_NO_UTF8_CHECK option, to suppress the checking of the string for UTF-8 validity. DATA LINES - Before each data line is passed to pcre_exec(), leading and trailing - whitespace is removed, and it is then scanned for \ escapes. Some of - these are pretty esoteric features, intended for checking out some of - the more complicated features of PCRE. If you are just testing "ordi- - nary" regular expressions, you probably don't need any of these. The + Before each data line is passed to pcre_exec(), leading and trailing + whitespace is removed, and it is then scanned for \ escapes. Some of + these are pretty esoteric features, intended for checking out some of + the more complicated features of PCRE. If you are just testing "ordi- + nary" regular expressions, you probably don't need any of these. The following escapes are recognized: - \a alarm (= BEL) - \b backspace - \e escape - \f formfeed - \n newline + \a alarm (BEL, \x07) + \b backspace (\x08) + \e escape (\x27) + \f formfeed (\x0c) + \n newline (\x0a) \qdd set the PCRE_MATCH_LIMIT limit to dd (any number of digits) - \r carriage return - \t tab - \v vertical tab + \r carriage return (\x0d) + \t tab (\x09) + \v vertical tab (\x0b) \nnn octal character (up to 3 octal digits) \xhh hexadecimal character (up to 2 hex digits) \x{hh...} hexadecimal character, any number of digits @@ -309,12 +331,17 @@ DATA LINES or pcre_dfa_exec() \ pass the PCRE_NEWLINE_CRLF option to pcre_exec() or pcre_dfa_exec() + \ pass the PCRE_NEWLINE_ANY option to pcre_exec() + or pcre_dfa_exec() - The escapes that specify line endings are literal strings, exactly as - shown. A backslash followed by anything else just escapes the anything - else. If the very last character is a backslash, it is ignored. This - gives a way of passing an empty line as data, since a real empty line - terminates the data input. + The escapes that specify line ending sequences are literal strings, + exactly as shown. No more than one newline setting should be present in + any data line. + + A backslash followed by anything else just escapes the anything else. + If the very last character is a backslash, it is ignored. This gives a + way of passing an empty line as data, since a real empty line termi- + nates the data input. If \M is present, pcretest calls pcre_exec() several times, with dif- ferent values in the match_limit and match_limit_recursion fields of @@ -371,7 +398,7 @@ DEFAULT OUTPUT FROM PCRETEST is an example of an interactive pcretest run. $ pcretest - PCRE version 5.00 07-Sep-2004 + PCRE version 7.0 30-Nov-2006 re> /^abc(\d+)/ data> abc123 @@ -382,16 +409,17 @@ DEFAULT OUTPUT FROM PCRETEST If the strings contain any non-printing characters, they are output as \0x escapes, or as \x{...} escapes if the /8 modifier was present on - the pattern. If the pattern has the /+ modifier, the output for sub- - string 0 is followed by the the rest of the subject string, identified - by "0+" like this: + the pattern. See below for the definition of non-printing characters. + If the pattern has the /+ modifier, the output for substring 0 is fol- + lowed by the the rest of the subject string, identified by "0+" like + this: re> /cat/+ data> cataract 0: cat 0+ aract - If the pattern has the /g or /G modifier, the results of successive + If the pattern has the /g or /G modifier, the results of successive matching attempts are output in sequence, like this: re> /\Bi(\w\w)/g @@ -405,24 +433,24 @@ DEFAULT OUTPUT FROM PCRETEST "No match" is output only if the first match attempt fails. - If any of the sequences \C, \G, or \L are present in a data line that - is successfully matched, the substrings extracted by the convenience + If any of the sequences \C, \G, or \L are present in a data line that + is successfully matched, the substrings extracted by the convenience functions are output with C, G, or L after the string number instead of a colon. This is in addition to the normal full list. The string length - (that is, the return from the extraction function) is given in paren- + (that is, the return from the extraction function) is given in paren- theses after each string for \C and \G. - Note that while patterns can be continued over several lines (a plain + Note that whereas patterns can be continued over several lines (a plain ">" prompt is used for continuations), data lines may not. However new- - lines can be included in data by means of the \n escape (or \r or \r\n - for those newline settings). + lines can be included in data by means of the \n escape (or \r, \r\n, + etc., depending on the newline sequence setting). OUTPUT FROM THE ALTERNATIVE MATCHING FUNCTION - When the alternative matching function, pcre_dfa_exec(), is used (by - means of the \D escape sequence or the -dfa command line option), the - output consists of a list of all the matches that start at the first + When the alternative matching function, pcre_dfa_exec(), is used (by + means of the \D escape sequence or the -dfa command line option), the + output consists of a list of all the matches that start at the first point in the subject where there is at least one match. For example: re> /(tang|tangerine|tan)/ @@ -431,11 +459,11 @@ OUTPUT FROM THE ALTERNATIVE MATCHING FUNCTION 1: tang 2: tan - (Using the normal matching function on this data finds only "tang".) - The longest matching string is always given first (and numbered zero). + (Using the normal matching function on this data finds only "tang".) + The longest matching string is always given first (and numbered zero). - If /gP is present on the pattern, the search for further matches - resumes at the end of the longest match. For example: + If /g is present on the pattern, the search for further matches resumes + at the end of the longest match. For example: re> /(tang|tangerine|tan)/g data> yellow tangerine and tangy sultana\D @@ -446,16 +474,16 @@ OUTPUT FROM THE ALTERNATIVE MATCHING FUNCTION 1: tan 0: tan - Since the matching function does not support substring capture, the - escape sequences that are concerned with captured substrings are not + Since the matching function does not support substring capture, the + escape sequences that are concerned with captured substrings are not relevant. RESTARTING AFTER A PARTIAL MATCH When the alternative matching function has given the PCRE_ERROR_PARTIAL - return, indicating that the subject partially matched the pattern, you - can restart the match with additional subject data by means of the \R + return, indicating that the subject partially matched the pattern, you + can restart the match with additional subject data by means of the \R escape sequence. For example: re> /^?(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)$/ @@ -464,30 +492,30 @@ RESTARTING AFTER A PARTIAL MATCH data> n05\R\D 0: n05 - For further information about partial matching, see the pcrepartial + For further information about partial matching, see the pcrepartial documentation. CALLOUTS - If the pattern contains any callout requests, pcretest's callout func- - tion is called during matching. This works with both matching func- + If the pattern contains any callout requests, pcretest's callout func- + tion is called during matching. This works with both matching func- tions. By default, the called function displays the callout number, the - start and current positions in the text at the callout time, and the + start and current positions in the text at the callout time, and the next pattern item to be tested. For example, the output --->pqrabcdef 0 ^ ^ \d - indicates that callout number 0 occurred for a match attempt starting - at the fourth character of the subject string, when the pointer was at - the seventh character of the data, and when the next pattern item was - \d. Just one circumflex is output if the start and current positions + indicates that callout number 0 occurred for a match attempt starting + at the fourth character of the subject string, when the pointer was at + the seventh character of the data, and when the next pattern item was + \d. Just one circumflex is output if the start and current positions are the same. Callouts numbered 255 are assumed to be automatic callouts, inserted as - a result of the /C pattern modifier. In this case, instead of showing - the callout number, the offset in the pattern, preceded by a plus, is + a result of the /C pattern modifier. In this case, instead of showing + the callout number, the offset in the pattern, preceded by a plus, is output. For example: re> /\d?[A-E]\*/C @@ -499,76 +527,94 @@ CALLOUTS +10 ^ ^ 0: E* - The callout function in pcretest returns zero (carry on matching) by - default, but you can use a \C item in a data line (as described above) + The callout function in pcretest returns zero (carry on matching) by + default, but you can use a \C item in a data line (as described above) to change this. - Inserting callouts can be helpful when using pcretest to check compli- - cated regular expressions. For further information about callouts, see + Inserting callouts can be helpful when using pcretest to check compli- + cated regular expressions. For further information about callouts, see the pcrecallout documentation. +NON-PRINTING CHARACTERS + + When pcretest is outputting text in the compiled version of a pattern, + bytes other than 32-126 are always treated as non-printing characters + are are therefore shown as hex escapes. + + When pcretest is outputting text that is a matched part of a subject + string, it behaves in the same way, unless a different locale has been + set for the pattern (using the /L modifier). In this case, the + isprint() function to distinguish printing and non-printing characters. + + SAVING AND RELOADING COMPILED PATTERNS - The facilities described in this section are not available when the + The facilities described in this section are not available when the POSIX inteface to PCRE is being used, that is, when the /P pattern mod- ifier is specified. When the POSIX interface is not in use, you can cause pcretest to write - a compiled pattern to a file, by following the modifiers with > and a + a compiled pattern to a file, by following the modifiers with > and a file name. For example: /pattern/im >/some/file - See the pcreprecompile documentation for a discussion about saving and + See the pcreprecompile documentation for a discussion about saving and re-using compiled patterns. - The data that is written is binary. The first eight bytes are the - length of the compiled pattern data followed by the length of the - optional study data, each written as four bytes in big-endian order - (most significant byte first). If there is no study data (either the + The data that is written is binary. The first eight bytes are the + length of the compiled pattern data followed by the length of the + optional study data, each written as four bytes in big-endian order + (most significant byte first). If there is no study data (either the pattern was not studied, or studying did not return any data), the sec- - ond length is zero. The lengths are followed by an exact copy of the + ond length is zero. The lengths are followed by an exact copy of the compiled pattern. If there is additional study data, this follows imme- - diately after the compiled pattern. After writing the file, pcretest + diately after the compiled pattern. After writing the file, pcretest expects to read a new pattern. A saved pattern can be reloaded into pcretest by specifing < and a file - name instead of a pattern. The name of the file must not contain a < - character, as otherwise pcretest will interpret the line as a pattern + name instead of a pattern. The name of the file must not contain a < + character, as otherwise pcretest will interpret the line as a pattern delimited by < characters. For example: re> and in Windows + builds of pcretest, and changed the call to _setmode() to use _O_BINARY + instead of 0x8000. Made all the #ifdefs test both _WIN32 and WIN32 (not all + of them did). + +10. Originally, pcretest opened its input and output without "b"; then I was + told that "b" was needed in some environments, so it was added for release + 5.0 to both the input and output. (It makes no difference on Unix-like + systems.) Later I was told that it is wrong for the input on Windows. I've + now abstracted the modes into two macros, to make it easier to fiddle with + them, and removed "b" from the input mode under Windows. + +11. Added pkgconfig support for the C++ wrapper library, libpcrecpp. + +12. Added -help and --help to pcretest as an official way of being reminded + of the options. + +13. Removed some redundant semicolons after macro calls in pcrecpparg.h.in + and pcrecpp.cc because they annoy compilers at high warning levels. + +14. A bit of tidying/refactoring in pcre_exec.c in the main bumpalong loop. + +15. Fixed an occurrence of == in configure.ac that should have been = (shell + scripts are not C programs :-) and which was not noticed because it works + on Linux. + +16. pcretest is supposed to handle any length of pattern and data line (as one + line or as a continued sequence of lines) by extending its input buffer if + necessary. This feature was broken for very long pattern lines, leading to + a string of junk being passed to pcre_compile() if the pattern was longer + than about 50K. + +17. I have done a major re-factoring of the way pcre_compile() computes the + amount of memory needed for a compiled pattern. Previously, there was code + that made a preliminary scan of the pattern in order to do this. That was + OK when PCRE was new, but as the facilities have expanded, it has become + harder and harder to keep it in step with the real compile phase, and there + have been a number of bugs (see for example, 4 above). I have now found a + cunning way of running the real compile function in a "fake" mode that + enables it to compute how much memory it would need, while actually only + ever using a few hundred bytes of working memory and without too many + tests of the mode. This should make future maintenance and development + easier. A side effect of this work is that the limit of 200 on the nesting + depth of parentheses has been removed (though this was never a serious + limitation, I suspect). However, there is a downside: pcre_compile() now + runs more slowly than before (30% or more, depending on the pattern). I + hope this isn't a big issue. There is no effect on runtime performance. + +18. Fixed a minor bug in pcretest: if a pattern line was not terminated by a + newline (only possible for the last line of a file) and it was a + pattern that set a locale (followed by /Lsomething), pcretest crashed. + +19. Added additional timing features to pcretest. (1) The -tm option now times + matching only, not compiling. (2) Both -t and -tm can be followed, as a + separate command line item, by a number that specifies the number of + repeats to use when timing. The default is 50000; this gives better + precision, but takes uncomfortably long for very large patterns. + +20. Extended pcre_study() to be more clever in cases where a branch of a + subpattern has no definite first character. For example, (a*|b*)[cd] would + previously give no result from pcre_study(). Now it recognizes that the + first character must be a, b, c, or d. + +21. There was an incorrect error "recursive call could loop indefinitely" if + a subpattern (or the entire pattern) that was being tested for matching an + empty string contained only one non-empty item after a nested subpattern. + For example, the pattern (?>\x{100}*)\d(?R) provoked this error + incorrectly, because the \d was being skipped in the check. + +22. The pcretest program now has a new pattern option /B and a command line + option -b, which is equivalent to adding /B to every pattern. This causes + it to show the compiled bytecode, without the additional information that + -d shows. The effect of -d is now the same as -b with -i (and similarly, /D + is the same as /B/I). + +23. A new optimization is now able automatically to treat some sequences such + as a*b as a*+b. More specifically, if something simple (such as a character + or a simple class like \d) has an unlimited quantifier, and is followed by + something that cannot possibly match the quantified thing, the quantifier + is automatically "possessified". + +24. A recursive reference to a subpattern whose number was greater than 39 + went wrong under certain circumstances in UTF-8 mode. This bug could also + have affected the operation of pcre_study(). + +25. Realized that a little bit of performance could be had by replacing + (c & 0xc0) == 0xc0 with c >= 0xc0 when processing UTF-8 characters. + +26. Timing data from pcretest is now shown to 4 decimal places instead of 3. + +27. Possessive quantifiers such as a++ were previously implemented by turning + them into atomic groups such as ($>a+). Now they have their own opcodes, + which improves performance. This includes the automatically created ones + from 23 above. + +28. A pattern such as (?=(\w+))\1: which simulates an atomic group using a + lookahead was broken if it was not anchored. PCRE was mistakenly expecting + the first matched character to be a colon. This applied both to named and + numbered groups. + +29. The ucpinternal.h header file was missing its idempotency #ifdef. + +30. I was sent a "project" file called libpcre.a.dev which I understand makes + building PCRE on Windows easier, so I have included it in the distribution. + +31. There is now a check in pcretest against a ridiculously large number being + returned by pcre_exec() or pcre_dfa_exec(). If this happens in a /g or /G + loop, the loop is abandoned. + +32. Forward references to subpatterns in conditions such as (?(2)...) where + subpattern 2 is defined later cause pcre_compile() to search forwards in + the pattern for the relevant set of parentheses. This search went wrong + when there were unescaped parentheses in a character class, parentheses + escaped with \Q...\E, or parentheses in a #-comment in /x mode. + +33. "Subroutine" calls and backreferences were previously restricted to + referencing subpatterns earlier in the regex. This restriction has now + been removed. + +34. Added a number of extra features that are going to be in Perl 5.10. On the + whole, these are just syntactic alternatives for features that PCRE had + previously implemented using the Python syntax or my own invention. The + other formats are all retained for compatibility. + + (a) Named groups can now be defined as (?...) or (?'name'...) as well + as (?P...). The new forms, as well as being in Perl 5.10, are + also .NET compatible. + + (b) A recursion or subroutine call to a named group can now be defined as + (?&name) as well as (?P>name). + + (c) A backreference to a named group can now be defined as \k or + \k'name' as well as (?P=name). The new forms, as well as being in Perl + 5.10, are also .NET compatible. + + (d) A conditional reference to a named group can now use the syntax + (?() or (?('name') as well as (?(name). + + (e) A "conditional group" of the form (?(DEFINE)...) can be used to define + groups (named and numbered) that are never evaluated inline, but can be + called as "subroutines" from elsewhere. In effect, the DEFINE condition + is always false. There may be only one alternative in such a group. + + (f) A test for recursion can be given as (?(R1).. or (?(R&name)... as well + as the simple (?(R). The condition is true only if the most recent + recursion is that of the given number or name. It does not search out + through the entire recursion stack. + + (g) The escape \gN or \g{N} has been added, where N is a positive or + negative number, specifying an absolute or relative reference. + +35. Tidied to get rid of some further signed/unsigned compiler warnings and + some "unreachable code" warnings. + +36. Updated the Unicode property tables to Unicode version 5.0.0. Amongst other + things, this adds five new scripts. + +37. Perl ignores orphaned \E escapes completely. PCRE now does the same. + There were also incompatibilities regarding the handling of \Q..\E inside + character classes, for example with patterns like [\Qa\E-\Qz\E] where the + hyphen was adjacent to \Q or \E. I hope I've cleared all this up now. + +38. Like Perl, PCRE detects when an indefinitely repeated parenthesized group + matches an empty string, and forcibly breaks the loop. There were bugs in + this code in non-simple cases. For a pattern such as ^(a()*)* matched + against aaaa the result was just "a" rather than "aaaa", for example. Two + separate and independent bugs (that affected different cases) have been + fixed. + +39. Refactored the code to abolish the use of different opcodes for small + capturing bracket numbers. This is a tidy that I avoided doing when I + removed the limit on the number of capturing brackets for 3.5 back in 2001. + The new approach is not only tidier, it makes it possible to reduce the + memory needed to fix the previous bug (38). + +40. Implemented PCRE_NEWLINE_ANY to recognize any of the Unicode newline + sequences (http://unicode.org/unicode/reports/tr18/) as "newline" when + processing dot, circumflex, or dollar metacharacters, or #-comments in /x + mode. + +41. Add \R to match any Unicode newline sequence, as suggested in the Unicode + report. + +42. Applied patch, originally from Ari Pollak, modified by Google, to allow + copy construction and assignment in the C++ wrapper. + +43. Updated pcregrep to support "--newline=any". In the process, I fixed a + couple of bugs that could have given wrong results in the "--newline=crlf" + case. + +44. Added a number of casts and did some reorganization of signed/unsigned int + variables following suggestions from Dair Grant. Also renamed the variable + "this" as "item" because it is a C++ keyword. + +45. Arranged for dftables to add + + #include "pcre_internal.h" + + to pcre_chartables.c because without it, gcc 4.x may remove the array + definition from the final binary if PCRE is built into a static library and + dead code stripping is activated. + +46. For an unanchored pattern, if a match attempt fails at the start of a + newline sequence, and the newline setting is CRLF or ANY, and the next two + characters are CRLF, advance by two characters instead of one. + + Version 6.7 04-Jul-06 --------------------- diff --git a/src/src/pcre/LICENCE b/src/src/pcre/LICENCE index daea2e48a..58241b2bd 100644 --- a/src/src/pcre/LICENCE +++ b/src/src/pcre/LICENCE @@ -4,7 +4,7 @@ PCRE LICENCE PCRE is a library of functions to support regular expressions whose syntax and semantics are as close as possible to those of the Perl 5 language. -Release 6 of PCRE is distributed under the terms of the "BSD" licence, as +Release 7 of PCRE is distributed under the terms of the "BSD" licence, as specified below. The documentation for PCRE, supplied in the "doc" directory, is distributed under the same terms as the software itself. diff --git a/src/src/pcre/Makefile b/src/src/pcre/Makefile index 067bfcdfb..7079d75f5 100644 --- a/src/src/pcre/Makefile +++ b/src/src/pcre/Makefile @@ -1,4 +1,4 @@ -# $Cambridge: exim/src/src/pcre/Makefile,v 1.6 2006/11/07 16:50:36 ph10 Exp $ +# $Cambridge: exim/src/src/pcre/Makefile,v 1.7 2007/01/23 15:08:45 ph10 Exp $ # Makefile for PCRE (Perl-Compatible Regular Expression) library for use by # Exim. This is a tailored Makefile, not the normal one that comes with the @@ -14,7 +14,7 @@ RANLIB = @true ############################################################################## OBJ = pcre_maketables.o chartables.o pcre_fullinfo.o pcre_get.o \ - pcre_globals.o pcre_compile.o pcre_config.o pcre_exec.o \ + pcre_globals.o pcre_compile.o pcre_config.o pcre_exec.o pcre_newline.o \ pcre_study.o pcre_tables.o pcre_try_flipped.o pcre_version.o all: libpcre.a ../pcretest @@ -61,6 +61,10 @@ pcre_globals.o: pcre_globals.c pcre.h config.h pcre_internal.h Makefile @echo "$(CC) pcre_globals.c" $(FE)$(CC) -c $(CFLAGS) pcre_globals.c +pcre_newline.o: pcre_newline.c pcre.h config.h pcre_internal.h Makefile + @echo "$(CC) pcre_newline.c" + $(FE)$(CC) -c $(CFLAGS) pcre_newline.c + pcre_study.o: pcre_study.c pcre.h config.h pcre_internal.h Makefile @echo "$(CC) pcre_study.c" $(FE)$(CC) -c $(CFLAGS) pcre_study.c diff --git a/src/src/pcre/dftables.c b/src/src/pcre/dftables.c index 0cb5dc29c..bc01eb7f2 100644 --- a/src/src/pcre/dftables.c +++ b/src/src/pcre/dftables.c @@ -1,4 +1,4 @@ -/* $Cambridge: exim/src/src/pcre/dftables.c,v 1.4 2006/11/07 16:50:36 ph10 Exp $ */ +/* $Cambridge: exim/src/src/pcre/dftables.c,v 1.5 2007/01/23 15:08:45 ph10 Exp $ */ /************************************************* * Perl-Compatible Regular Expressions * @@ -88,7 +88,16 @@ fprintf(f, fprintf(f, "This file contains the default tables for characters with codes less than\n" "128 (ASCII characters). These tables are used when no external tables are\n" - "passed to PCRE. */\n\n" + "passed to PCRE.\n\n"); +fprintf(f, + "The following #include is present because without it gcc 4.x may remove\n" + "the array definition from the final binary if PCRE is built into a static\n" + "library and dead code stripping is activated. This leads to link errors.\n" + "Pulling in the header ensures that the array gets flagged as \"someone\n" + "outside this compilation unit might reference this\" and so it will always\n" + "be supplied to the linker. */\n\n" + "#include \"pcre_internal.h\"\n\n"); +fprintf(f, "const unsigned char _pcre_default_tables[] = {\n\n" "/* This table is a lower casing table. */\n\n"); diff --git a/src/src/pcre/pcre.h b/src/src/pcre/pcre.h index 46be6ed7e..6c3f2a50b 100644 --- a/src/src/pcre/pcre.h +++ b/src/src/pcre/pcre.h @@ -1,4 +1,4 @@ -/* $Cambridge: exim/src/src/pcre/pcre.h,v 1.4 2006/11/07 16:50:36 ph10 Exp $ */ +/* $Cambridge: exim/src/src/pcre/pcre.h,v 1.5 2007/01/23 15:08:45 ph10 Exp $ */ /************************************************* * Perl-Compatible Regular Expressions * @@ -7,7 +7,7 @@ /* This is the public header file for the PCRE library, to be #included by applications that call the PCRE functions. - Copyright (c) 1997-2005 University of Cambridge + Copyright (c) 1997-2006 University of Cambridge ----------------------------------------------------------------------------- Redistribution and use in source and binary forms, with or without @@ -54,10 +54,10 @@ and libpcre.pc. The values are not put into configure.ac and substituted here cannot run ./configure. As it now stands, this file need not be edited in that circumstance. */ -#define PCRE_MAJOR 6 -#define PCRE_MINOR 7 +#define PCRE_MAJOR 7 +#define PCRE_MINOR 0 #define PCRE_PRERELEASE -#define PCRE_DATE 04-Jul-2006 +#define PCRE_DATE 18-Dec-2006 /* Win32 uses DLL by default; it needs special stuff for exported functions when building PCRE. */ @@ -120,6 +120,7 @@ extern "C" { #define PCRE_NEWLINE_CR 0x00100000 #define PCRE_NEWLINE_LF 0x00200000 #define PCRE_NEWLINE_CRLF 0x00300000 +#define PCRE_NEWLINE_ANY 0x00400000 /* Exec-time and get/set-time error codes */ @@ -127,7 +128,8 @@ extern "C" { #define PCRE_ERROR_NULL (-2) #define PCRE_ERROR_BADOPTION (-3) #define PCRE_ERROR_BADMAGIC (-4) -#define PCRE_ERROR_UNKNOWN_NODE (-5) +#define PCRE_ERROR_UNKNOWN_OPCODE (-5) +#define PCRE_ERROR_UNKNOWN_NODE (-5) /* For backward compatibility */ #define PCRE_ERROR_NOMEMORY (-6) #define PCRE_ERROR_NOSUBSTRING (-7) #define PCRE_ERROR_MATCHLIMIT (-8) @@ -144,6 +146,8 @@ extern "C" { #define PCRE_ERROR_DFA_WSSIZE (-19) #define PCRE_ERROR_DFA_RECURSE (-20) #define PCRE_ERROR_RECURSIONLIMIT (-21) +#define PCRE_ERROR_NULLWSLIMIT (-22) +#define PCRE_ERROR_BADNEWLINE (-23) /* Request types for pcre_fullinfo() */ diff --git a/src/src/pcre/pcre_compile.c b/src/src/pcre/pcre_compile.c index eaa9550a7..f7a3ebbd2 100644 --- a/src/src/pcre/pcre_compile.c +++ b/src/src/pcre/pcre_compile.c @@ -1,4 +1,4 @@ -/* $Cambridge: exim/src/src/pcre/pcre_compile.c,v 1.3 2006/11/07 16:50:36 ph10 Exp $ */ +/* $Cambridge: exim/src/src/pcre/pcre_compile.c,v 1.4 2007/01/23 15:08:45 ph10 Exp $ */ /************************************************* * Perl-Compatible Regular Expressions * @@ -44,7 +44,11 @@ POSSIBILITY OF SUCH DAMAGE. supporting internal functions that are not used by other modules. */ -#define NLBLOCK cd /* The block containing newline information */ +#define NLBLOCK cd /* Block containing newline information */ +#define PSSTART start_pattern /* Field containing processed string start */ +#define PSEND end_pattern /* Field containing processed string end */ + + #include "pcre_internal.h" @@ -56,18 +60,23 @@ used by pcretest. DEBUG is not defined when building a production library. */ #endif - /************************************************* * Code parameters and static tables * *************************************************/ -/* Maximum number of items on the nested bracket stacks at compile time. This -applies to the nesting of all kinds of parentheses. It does not limit -un-nested, non-capturing parentheses. This number can be made bigger if -necessary - it is used to dimension one int and one unsigned char vector at -compile time. */ +/* This value specifies the size of stack workspace that is used during the +first pre-compile phase that determines how much memory is required. The regex +is partly compiled into this space, but the compiled parts are discarded as +soon as they can be, so that hopefully there will never be an overrun. The code +does, however, check for an overrun. The largest amount I've seen used is 218, +so this number is very generous. + +The same workspace is used during the second, actual compile phase for +remembering forward references to groups so that they can be filled in at the +end. Each entry in this list occupies LINK_SIZE bytes, so even when LINK_SIZE +is 4 there is plenty of room. */ -#define BRASTACK_SIZE 200 +#define COMPILE_WORK_SIZE (4096) /* Table for handling escaped characters in the range '0'-'z'. Positive returns @@ -81,10 +90,10 @@ static const short int escapes[] = { 0, 0, ':', ';', '<', '=', '>', '?', /* 8 - ? */ '@', -ESC_A, -ESC_B, -ESC_C, -ESC_D, -ESC_E, 0, -ESC_G, /* @ - G */ 0, 0, 0, 0, 0, 0, 0, 0, /* H - O */ --ESC_P, -ESC_Q, 0, -ESC_S, 0, 0, 0, -ESC_W, /* P - W */ +-ESC_P, -ESC_Q, -ESC_R, -ESC_S, 0, 0, 0, -ESC_W, /* P - W */ -ESC_X, 0, -ESC_Z, '[', '\\', ']', '^', '_', /* X - _ */ '`', 7, -ESC_b, 0, -ESC_d, ESC_e, ESC_f, 0, /* ` - g */ - 0, 0, 0, 0, 0, 0, ESC_n, 0, /* h - o */ + 0, 0, 0, -ESC_k, 0, 0, ESC_n, 0, /* h - o */ -ESC_p, 0, ESC_r, -ESC_s, ESC_tee, 0, 0, -ESC_w, /* p - w */ 0, 0, -ESC_z /* x - z */ }; @@ -100,7 +109,7 @@ static const short int escapes[] = { /* 78 */ 0, '`', ':', '#', '@', '\'', '=', '"', /* 80 */ 0, 7, -ESC_b, 0, -ESC_d, ESC_e, ESC_f, 0, /* 88 */ 0, 0, 0, '{', 0, 0, 0, 0, -/* 90 */ 0, 0, 0, 'l', 0, ESC_n, 0, -ESC_p, +/* 90 */ 0, 0, -ESC_k, 'l', 0, ESC_n, 0, -ESC_p, /* 98 */ 0, ESC_r, 0, '}', 0, 0, 0, 0, /* A0 */ 0, '~', -ESC_s, ESC_tee, 0, 0, -ESC_w, 0, /* A8 */ 0,-ESC_z, 0, 0, 0, '[', 0, 0, @@ -109,7 +118,7 @@ static const short int escapes[] = { /* C0 */ '{',-ESC_A, -ESC_B, -ESC_C, -ESC_D,-ESC_E, 0, -ESC_G, /* C8 */ 0, 0, 0, 0, 0, 0, 0, 0, /* D0 */ '}', 0, 0, 0, 0, 0, 0, -ESC_P, -/* D8 */-ESC_Q, 0, 0, 0, 0, 0, 0, 0, +/* D8 */-ESC_Q,-ESC_R, 0, 0, 0, 0, 0, 0, /* E0 */ '\\', 0, -ESC_S, 0, 0, 0, -ESC_W, -ESC_X, /* E8 */ 0,-ESC_Z, 0, 0, 0, 0, 0, 0, /* F0 */ 0, 0, 0, 0, 0, 0, 0, 0, @@ -158,8 +167,13 @@ static const int posix_class_maps[] = { }; +#define STRING(a) # a +#define XSTRING(s) STRING(s) + /* The texts of compile-time error messages. These are "char *" because they -are passed to the outside world. */ +are passed to the outside world. Do not ever re-use any error number, because +they are documented. Always add a new error instead. Messages marked DEAD below +are no longer used. */ static const char *error_texts[] = { "no error", @@ -174,7 +188,7 @@ static const char *error_texts[] = { "range out of order in character class", "nothing to repeat", /* 10 */ - "operand of unlimited repeat could match the empty string", + "operand of unlimited repeat could match the empty string", /** DEAD **/ "internal error: unexpected repeat", "unrecognized character after (?", "POSIX named classes are supported only within a class", @@ -184,7 +198,7 @@ static const char *error_texts[] = { "erroffset passed as NULL", "unknown option bit(s) set", "missing ) after comment", - "parentheses nested too deeply", + "parentheses nested too deeply", /** DEAD **/ /* 20 */ "regular expression too large", "failed to get memory", @@ -201,7 +215,7 @@ static const char *error_texts[] = { "unknown POSIX class name", "POSIX collating elements are not supported", "this version of PCRE is not compiled with PCRE_UTF8 support", - "spare error", + "spare error", /** DEAD **/ "character value in \\x{...} sequence is too large", /* 35 */ "invalid condition (?(0)", @@ -212,18 +226,25 @@ static const char *error_texts[] = { /* 40 */ "recursive call could loop indefinitely", "unrecognized character after (?P", - "syntax error after (?P", + "syntax error in subpattern name (missing terminator)", "two named subpatterns have the same name", "invalid UTF-8 string", /* 45 */ "support for \\P, \\p, and \\X has not been compiled", "malformed \\P or \\p sequence", "unknown property name after \\P or \\p", - "subpattern name is too long (maximum 32 characters)", - "too many named subpatterns (maximum 10,000)", + "subpattern name is too long (maximum " XSTRING(MAX_NAME_SIZE) " characters)", + "too many named subpatterns (maximum " XSTRING(MAX_NAME_COUNT) ")", /* 50 */ "repeated subpattern is too long", - "octal value is greater than \\377 (not in UTF-8 mode)" + "octal value is greater than \\377 (not in UTF-8 mode)", + "internal error: overran compiling workspace", + "internal error: previously-checked referenced subpattern not found", + "DEFINE group contains more than one branch", + /* 55 */ + "repeating a DEFINE group is not allowed", + "inconsistent NEWLINE options", + "\\g is not followed by an (optionally braced) non-zero number" }; @@ -354,8 +375,8 @@ static const unsigned char ebcdic_chartab[] = { /* chartable partial dup */ /* Definition to allow mutual recursion */ static BOOL - compile_regex(int, int, int *, uschar **, const uschar **, int *, BOOL, int, - int *, int *, branch_chain *, compile_data *); + compile_regex(int, int, uschar **, const uschar **, int *, BOOL, int, int *, + int *, branch_chain *, compile_data *, int *); @@ -365,9 +386,11 @@ static BOOL /* This function is called when a \ has been encountered. It either returns a positive value for a simple escape such as \n, or a negative value which -encodes one of the more complicated things such as \d. When UTF-8 is enabled, -a positive value greater than 255 may be returned. On entry, ptr is pointing at -the \. On exit, it is on the final character of the escape sequence. +encodes one of the more complicated things such as \d. A backreference to group +n is returned as -(ESC_REF + n); ESC_REF is the highest ESC_xxx macro. When +UTF-8 is enabled, a positive value greater than 255 may be returned. On entry, +ptr is pointing at the \. On exit, it is on the final character of the escape +sequence. Arguments: ptrptr points to the pattern position pointer @@ -414,6 +437,8 @@ else if ((i = escapes[c - 0x48]) != 0) c = i; else { const uschar *oldptr; + BOOL braced, negated; + switch (c) { /* A number of Perl escapes are not handled by PCRE. We give an explicit @@ -427,6 +452,48 @@ else *errorcodeptr = ERR37; break; + /* \g must be followed by a number, either plain or braced. If positive, it + is an absolute backreference. If negative, it is a relative backreference. + This is a Perl 5.10 feature. */ + + case 'g': + if (ptr[1] == '{') + { + braced = TRUE; + ptr++; + } + else braced = FALSE; + + if (ptr[1] == '-') + { + negated = TRUE; + ptr++; + } + else negated = FALSE; + + c = 0; + while ((digitab[ptr[1]] & ctype_digit) != 0) + c = c * 10 + *(++ptr) - '0'; + + if (c == 0 || (braced && *(++ptr) != '}')) + { + *errorcodeptr = ERR57; + return 0; + } + + if (negated) + { + if (c > bracount) + { + *errorcodeptr = ERR15; + return 0; + } + c = bracount - (c - 1); + } + + c = -(ESC_REF + c); + break; + /* The handling of escape sequences consisting of a string of digits starting with one that is not zero is not straightforward. By experiment, the way Perl works seems to be as follows: @@ -534,7 +601,9 @@ else } break; - /* Other special escapes not starting with a digit are straightforward */ + /* For \c, a following letter is upper-cased; then the 0x40 bit is flipped. + This coding is ASCII-specific, but then the whole concept of \cx is + ASCII-specific. (However, an EBCDIC equivalent has now been added.) */ case 'c': c = *(++ptr); @@ -544,10 +613,6 @@ else return 0; } - /* A letter is upper-cased; then the 0x40 bit is flipped. This coding - is ASCII-specific, but then the whole concept of \cx is ASCII-specific. - (However, an EBCDIC equivalent has now been added.) */ - #if !EBCDIC /* ASCII coding */ if (c >= 'a' && c <= 'z') c -= 32; c ^= 0x40; @@ -774,43 +839,111 @@ return p; /************************************************* -* Find forward referenced named subpattern * +* Find forward referenced subpattern * *************************************************/ -/* This function scans along a pattern looking for capturing subpatterns, and -counting them. If it finds a named pattern that matches the name it is given, -it returns its number. This is used for forward references to named -subpatterns. We know that if (?P< is encountered, the name will be terminated -by '>' because that is checked in the first pass. +/* This function scans along a pattern's text looking for capturing +subpatterns, and counting them. If it finds a named pattern that matches the +name it is given, it returns its number. Alternatively, if the name is NULL, it +returns when it reaches a given numbered subpattern. This is used for forward +references to subpatterns. We know that if (?P< is encountered, the name will +be terminated by '>' because that is checked in the first pass. Arguments: - pointer current position in the pattern - count current count of capturing parens - name name to seek - namelen name length + ptr current position in the pattern + count current count of capturing parens so far encountered + name name to seek, or NULL if seeking a numbered subpattern + lorn name length, or subpattern number if name is NULL + xmode TRUE if we are in /x mode Returns: the number of the named subpattern, or -1 if not found */ static int -find_named_parens(const uschar *ptr, int count, const uschar *name, int namelen) +find_parens(const uschar *ptr, int count, const uschar *name, int lorn, + BOOL xmode) { const uschar *thisname; + for (; *ptr != 0; ptr++) { - if (*ptr == '\\' && ptr[1] != 0) { ptr++; continue; } + int term; + + /* Skip over backslashed characters and also entire \Q...\E */ + + if (*ptr == '\\') + { + if (*(++ptr) == 0) return -1; + if (*ptr == 'Q') for (;;) + { + while (*(++ptr) != 0 && *ptr != '\\'); + if (*ptr == 0) return -1; + if (*(++ptr) == 'E') break; + } + continue; + } + + /* Skip over character classes */ + + if (*ptr == '[') + { + while (*(++ptr) != ']') + { + if (*ptr == '\\') + { + if (*(++ptr) == 0) return -1; + if (*ptr == 'Q') for (;;) + { + while (*(++ptr) != 0 && *ptr != '\\'); + if (*ptr == 0) return -1; + if (*(++ptr) == 'E') break; + } + continue; + } + } + continue; + } + + /* Skip comments in /x mode */ + + if (xmode && *ptr == '#') + { + while (*(++ptr) != 0 && *ptr != '\n'); + if (*ptr == 0) return -1; + continue; + } + + /* An opening parens must now be a real metacharacter */ + if (*ptr != '(') continue; - if (ptr[1] != '?') { count++; continue; } - if (ptr[2] == '(') { ptr += 2; continue; } - if (ptr[2] != 'P' || ptr[3] != '<') continue; + if (ptr[1] != '?') + { + count++; + if (name == NULL && count == lorn) return count; + continue; + } + + ptr += 2; + if (*ptr == 'P') ptr++; /* Allow optional P */ + + /* We have to disambiguate (? */ + + if ((*ptr != '<' || ptr[1] == '!' || ptr[1] == '=') && + *ptr != '\'') + continue; + count++; - ptr += 4; + + if (name == NULL && count == lorn) return count; + term = *ptr++; + if (term == '<') term = '>'; thisname = ptr; - while (*ptr != '>') ptr++; - if (namelen == ptr - thisname && - strncmp((char *)name, (char*)thisname, namelen) == 0) + while (*ptr != term) ptr++; + if (name != NULL && lorn == ptr - thisname && + strncmp((const char *)name, (const char *)thisname, lorn) == 0) return count; } + return -1; } @@ -865,7 +998,8 @@ for (;;) case OP_CALLOUT: case OP_CREF: - case OP_BRANUMBER: + case OP_RREF: + case OP_DEF: code += _pcre_OP_lengths[*code]; break; @@ -910,14 +1044,14 @@ for (;;) { int d; register int op = *cc; - if (op >= OP_BRA) op = OP_BRA; switch (op) { + case OP_CBRA: case OP_BRA: case OP_ONCE: case OP_COND: - d = find_fixedlength(cc, options); + d = find_fixedlength(cc + ((op == OP_CBRA)? 2:0), options); if (d < 0) return d; branchlength += d; do cc += GET(cc, 1); while (*cc == OP_ALT); @@ -952,8 +1086,9 @@ for (;;) /* Skip over things that don't match chars */ case OP_REVERSE: - case OP_BRANUMBER: case OP_CREF: + case OP_RREF: + case OP_DEF: case OP_OPT: case OP_CALLOUT: case OP_SOD: @@ -1097,21 +1232,18 @@ for (;;) if (c == OP_XCLASS) code += GET(code, 1); - /* Handle bracketed group */ + /* Handle capturing bracket */ - else if (c > OP_BRA) + else if (c == OP_CBRA) { - int n = c - OP_BRA; - if (n > EXTRACT_BASIC_MAX) n = GET2(code, 2+LINK_SIZE); + int n = GET2(code, 1+LINK_SIZE); if (n == number) return (uschar *)code; - code += _pcre_OP_lengths[OP_BRA]; + code += _pcre_OP_lengths[c]; } - /* Otherwise, we get the item's length from the table. In UTF-8 mode, opcodes - that are followed by a character may be followed by a multi-byte character. - The length in the table is a minimum, so we have to scan along to skip the - extra bytes. All opcodes are less than 128, so we can use relatively - efficient code. */ + /* In UTF-8 mode, opcodes that are followed by a character may be followed by + a multi-byte character. The length in the table is a minimum, so we have to + arrange to skip the extra bytes. */ else { @@ -1123,13 +1255,17 @@ for (;;) case OP_EXACT: case OP_UPTO: case OP_MINUPTO: + case OP_POSUPTO: case OP_STAR: case OP_MINSTAR: + case OP_POSSTAR: case OP_PLUS: case OP_MINPLUS: + case OP_POSPLUS: case OP_QUERY: case OP_MINQUERY: - while ((*code & 0xc0) == 0x80) code++; + case OP_POSQUERY: + if (code[-1] >= 0xc0) code += _pcre_utf8_table4[code[-1] & 0x3f]; break; } } @@ -1167,18 +1303,10 @@ for (;;) if (c == OP_XCLASS) code += GET(code, 1); - /* All bracketed groups have the same length. */ - - else if (c > OP_BRA) - { - code += _pcre_OP_lengths[OP_BRA]; - } - /* Otherwise, we get the item's length from the table. In UTF-8 mode, opcodes that are followed by a character may be followed by a multi-byte character. - The length in the table is a minimum, so we have to scan along to skip the - extra bytes. All opcodes are less than 128, so we can use relatively - efficient code. */ + The length in the table is a minimum, so we have to arrange to skip the extra + bytes. */ else { @@ -1190,13 +1318,17 @@ for (;;) case OP_EXACT: case OP_UPTO: case OP_MINUPTO: + case OP_POSUPTO: case OP_STAR: case OP_MINSTAR: + case OP_POSSTAR: case OP_PLUS: case OP_MINPLUS: + case OP_POSPLUS: case OP_QUERY: case OP_MINQUERY: - while ((*code & 0xc0) == 0x80) code++; + case OP_POSQUERY: + if (code[-1] >= 0xc0) code += _pcre_utf8_table4[code[-1] & 0x3f]; break; } } @@ -1210,10 +1342,11 @@ for (;;) *************************************************/ /* This function scans through a branch of a compiled pattern to see whether it -can match the empty string or not. It is called only from could_be_empty() -below. Note that first_significant_code() skips over assertions. If we hit an -unclosed bracket, we return "empty" - this means we've struck an inner bracket -whose current branch will already have been scanned. +can match the empty string or not. It is called from could_be_empty() +below and from compile_branch() when checking for an unlimited repeat of a +group that can match nothing. Note that first_significant_code() skips over +assertions. If we hit an unclosed bracket, we return "empty" - this means we've +struck an inner bracket whose current branch will already have been scanned. Arguments: code points to start of search @@ -1227,7 +1360,7 @@ static BOOL could_be_empty_branch(const uschar *code, const uschar *endcode, BOOL utf8) { register int c; -for (code = first_significant_code(code + 1 + LINK_SIZE, NULL, 0, TRUE); +for (code = first_significant_code(code + _pcre_OP_lengths[*code], NULL, 0, TRUE); code < endcode; code = first_significant_code(code + _pcre_OP_lengths[c], NULL, 0, TRUE)) { @@ -1235,7 +1368,7 @@ for (code = first_significant_code(code + 1 + LINK_SIZE, NULL, 0, TRUE); c = *code; - if (c >= OP_BRA) + if (c == OP_BRA || c == OP_CBRA || c == OP_ONCE) { BOOL empty_branch; if (GET(code, 1) == 0) return TRUE; /* Hit unclosed bracket */ @@ -1251,11 +1384,18 @@ for (code = first_significant_code(code + 1 + LINK_SIZE, NULL, 0, TRUE); } while (*code == OP_ALT); if (!empty_branch) return FALSE; /* All branches are non-empty */ - code += 1 + LINK_SIZE; - c = *code; + + /* Move past the KET and fudge things so that the increment in the "for" + above has no effect. */ + + c = OP_END; + code += 1 + LINK_SIZE - _pcre_OP_lengths[c]; + continue; } - else switch (c) + /* Handle the other opcodes */ + + switch (c) { /* Check for quantifiers after a class */ @@ -1311,12 +1451,15 @@ for (code = first_significant_code(code + 1 + LINK_SIZE, NULL, 0, TRUE); case OP_NOT: case OP_PLUS: case OP_MINPLUS: + case OP_POSPLUS: case OP_EXACT: case OP_NOTPLUS: case OP_NOTMINPLUS: + case OP_NOTPOSPLUS: case OP_NOTEXACT: case OP_TYPEPLUS: case OP_TYPEMINPLUS: + case OP_TYPEPOSPLUS: case OP_TYPEEXACT: return FALSE; @@ -1328,16 +1471,19 @@ for (code = first_significant_code(code + 1 + LINK_SIZE, NULL, 0, TRUE); case OP_ALT: return TRUE; - /* In UTF-8 mode, STAR, MINSTAR, QUERY, MINQUERY, UPTO, and MINUPTO may be - followed by a multibyte character */ + /* In UTF-8 mode, STAR, MINSTAR, POSSTAR, QUERY, MINQUERY, POSQUERY, UPTO, + MINUPTO, and POSUPTO may be followed by a multibyte character */ #ifdef SUPPORT_UTF8 case OP_STAR: case OP_MINSTAR: + case OP_POSSTAR: case OP_QUERY: case OP_MINQUERY: + case OP_POSQUERY: case OP_UPTO: case OP_MINUPTO: + case OP_POSUPTO: if (utf8) while ((code[2] & 0xc0) == 0x80) code++; break; #endif @@ -1455,26 +1601,57 @@ earlier groups that are outside the current group). However, when a group is optional (i.e. the minimum quantifier is zero), OP_BRAZERO 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 is the job of this function. Before it is called, the -partially compiled regex must be temporarily terminated with OP_END. +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 utf8 TRUE in UTF-8 mode cd contains pointers to tables etc. + save_hwm the hwm forward reference pointer at the start of the group Returns: nothing */ static void -adjust_recurse(uschar *group, int adjust, BOOL utf8, compile_data *cd) +adjust_recurse(uschar *group, int adjust, BOOL utf8, compile_data *cd, + uschar *save_hwm) { uschar *ptr = group; while ((ptr = (uschar *)find_recurse(ptr, utf8)) != NULL) { - int offset = GET(ptr, 1); - if (cd->start_code + offset >= group) PUT(ptr, 1, offset + adjust); + int offset; + uschar *hc; + + /* See if this recursion is on the forward reference list. If so, adjust the + reference. */ + + for (hc = save_hwm; hc < cd->hwm; hc += LINK_SIZE) + { + offset = GET(hc, 0); + if (cd->start_code + offset == ptr + 1) + { + PUT(hc, 0, offset + adjust); + break; + } + } + + /* Otherwise, adjust the recursion offset if it's after the start of this + group. */ + + if (hc >= cd->hwm) + { + offset = GET(ptr, 1); + if (cd->start_code + offset >= group) PUT(ptr, 1, offset + adjust); + } + ptr += 1 + LINK_SIZE; } } @@ -1553,12 +1730,13 @@ Yield: TRUE when range returned; FALSE when no more */ static BOOL -get_othercase_range(int *cptr, int d, int *ocptr, int *odptr) +get_othercase_range(unsigned int *cptr, unsigned int d, unsigned int *ocptr, + unsigned int *odptr) { -int c, othercase, next; +unsigned int c, othercase, next; for (c = *cptr; c <= d; c++) - { if ((othercase = _pcre_ucp_othercase(c)) >= 0) break; } + { if ((othercase = _pcre_ucp_othercase(c)) != NOTACHAR) break; } if (c > d) return FALSE; @@ -1579,17 +1757,249 @@ return TRUE; #endif /* SUPPORT_UCP */ + +/************************************************* +* Check if auto-possessifying is possible * +*************************************************/ + +/* This function is called for unlimited repeats of certain items, to see +whether the next thing could possibly match the repeated item. If not, it makes +sense to automatically possessify the repeated item. + +Arguments: + op_code the repeated op code + this data for this item, depends on the opcode + utf8 TRUE in UTF-8 mode + utf8_char used for utf8 character bytes, NULL if not relevant + ptr next character in pattern + options options bits + cd contains pointers to tables etc. + +Returns: TRUE if possessifying is wanted +*/ + +static BOOL +check_auto_possessive(int op_code, int item, BOOL utf8, uschar *utf8_char, + const uschar *ptr, int options, compile_data *cd) +{ +int next; + +/* Skip whitespace and comments in extended mode */ + +if ((options & PCRE_EXTENDED) != 0) + { + for (;;) + { + while ((cd->ctypes[*ptr] & ctype_space) != 0) ptr++; + if (*ptr == '#') + { + while (*(++ptr) != 0) + if (IS_NEWLINE(ptr)) { ptr += cd->nllen; break; } + } + else break; + } + } + +/* If the next item is one that we can handle, get its value. A non-negative +value is a character, a negative value is an escape value. */ + +if (*ptr == '\\') + { + int temperrorcode = 0; + next = check_escape(&ptr, &temperrorcode, cd->bracount, options, FALSE); + if (temperrorcode != 0) return FALSE; + ptr++; /* Point after the escape sequence */ + } + +else if ((cd->ctypes[*ptr] & ctype_meta) == 0) + { +#ifdef SUPPORT_UTF8 + if (utf8) { GETCHARINC(next, ptr); } else +#endif + next = *ptr++; + } + +else return FALSE; + +/* Skip whitespace and comments in extended mode */ + +if ((options & PCRE_EXTENDED) != 0) + { + for (;;) + { + while ((cd->ctypes[*ptr] & ctype_space) != 0) ptr++; + if (*ptr == '#') + { + while (*(++ptr) != 0) + if (IS_NEWLINE(ptr)) { ptr += cd->nllen; break; } + } + else break; + } + } + +/* If the next thing is itself optional, we have to give up. */ + +if (*ptr == '*' || *ptr == '?' || strncmp((char *)ptr, "{0,", 3) == 0) + return FALSE; + +/* Now compare the next item with the previous opcode. If the previous is a +positive single character match, "item" either contains the character or, if +"item" is greater than 127 in utf8 mode, the character's bytes are in +utf8_char. */ + + +/* Handle cases when the next item is a character. */ + +if (next >= 0) switch(op_code) + { + case OP_CHAR: +#ifdef SUPPORT_UTF8 + if (utf8 && item > 127) { GETCHAR(item, utf8_char); } +#endif + return item != next; + + /* For CHARNC (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. */ + + case OP_CHARNC: +#ifdef SUPPORT_UTF8 + if (utf8 && item > 127) { GETCHAR(item, utf8_char); } +#endif + if (item == next) return FALSE; +#ifdef SUPPORT_UTF8 + if (utf8) + { + unsigned int othercase; + if (next < 128) othercase = cd->fcc[next]; else +#ifdef SUPPORT_UCP + othercase = _pcre_ucp_othercase((unsigned int)next); +#else + othercase = NOTACHAR; +#endif + return (unsigned int)item != othercase; + } + else +#endif /* SUPPORT_UTF8 */ + return (item != cd->fcc[next]); /* Non-UTF-8 mode */ + + /* For OP_NOT, "item" must be a single-byte character. */ + + case OP_NOT: + if (next < 0) return FALSE; /* Not a character */ + if (item == next) return TRUE; + if ((options & PCRE_CASELESS) == 0) return FALSE; +#ifdef SUPPORT_UTF8 + if (utf8) + { + unsigned int othercase; + if (next < 128) othercase = cd->fcc[next]; else +#ifdef SUPPORT_UCP + othercase = _pcre_ucp_othercase(next); +#else + othercase = NOTACHAR; +#endif + return (unsigned int)item == othercase; + } + else +#endif /* SUPPORT_UTF8 */ + return (item == cd->fcc[next]); /* Non-UTF-8 mode */ + + case OP_DIGIT: + return next > 127 || (cd->ctypes[next] & ctype_digit) == 0; + + case OP_NOT_DIGIT: + return next <= 127 && (cd->ctypes[next] & ctype_digit) != 0; + + case OP_WHITESPACE: + return next > 127 || (cd->ctypes[next] & ctype_space) == 0; + + case OP_NOT_WHITESPACE: + return next <= 127 && (cd->ctypes[next] & ctype_space) != 0; + + case OP_WORDCHAR: + return next > 127 || (cd->ctypes[next] & ctype_word) == 0; + + case OP_NOT_WORDCHAR: + return next <= 127 && (cd->ctypes[next] & ctype_word) != 0; + + default: + return FALSE; + } + + +/* Handle the case when the next item is \d, \s, etc. */ + +switch(op_code) + { + case OP_CHAR: + case OP_CHARNC: +#ifdef SUPPORT_UTF8 + if (utf8 && item > 127) { GETCHAR(item, utf8_char); } +#endif + switch(-next) + { + case ESC_d: + return item > 127 || (cd->ctypes[item] & ctype_digit) == 0; + + case ESC_D: + return item <= 127 && (cd->ctypes[item] & ctype_digit) != 0; + + case ESC_s: + return item > 127 || (cd->ctypes[item] & ctype_space) == 0; + + case ESC_S: + return item <= 127 && (cd->ctypes[item] & ctype_space) != 0; + + case ESC_w: + return item > 127 || (cd->ctypes[item] & ctype_word) == 0; + + case ESC_W: + return item <= 127 && (cd->ctypes[item] & ctype_word) != 0; + + default: + return FALSE; + } + + case OP_DIGIT: + return next == -ESC_D || next == -ESC_s || next == -ESC_W; + + case OP_NOT_DIGIT: + return next == -ESC_d; + + case OP_WHITESPACE: + return next == -ESC_S || next == -ESC_d || next == -ESC_w; + + case OP_NOT_WHITESPACE: + return next == -ESC_s; + + case OP_WORDCHAR: + return next == -ESC_W || next == -ESC_s; + + case OP_NOT_WORDCHAR: + return next == -ESC_w || next == -ESC_d; + + default: + return FALSE; + } + +/* Control does not reach here */ +} + + + /************************************************* * Compile one branch * *************************************************/ -/* Scan the pattern, compiling it into the code vector. If the options are +/* Scan the pattern, compiling it into the a vector. If the options are changed during the branch, the pointer is used to change the external options -bits. +bits. This function is used during the pre-compile phase when we are trying +to find out the amount of memory needed, as well as during the real compile +phase. The value of lengthptr distinguishes the two phases. Arguments: optionsptr pointer to the option bits - brackets points to number of extracting brackets used codeptr points to the pointer to the current code point ptrptr points to the current pattern pointer errorcodeptr points to error code variable @@ -1597,15 +2007,17 @@ Arguments: reqbyteptr set to the last literal character required, else < 0 bcptr points to current branch chain cd contains pointers to tables etc. + lengthptr NULL during the real compile phase + points to length accumulator during pre-compile phase Returns: TRUE on success FALSE, with *errorcodeptr set non-zero on error */ static BOOL -compile_branch(int *optionsptr, int *brackets, uschar **codeptr, - const uschar **ptrptr, int *errorcodeptr, int *firstbyteptr, - int *reqbyteptr, branch_chain *bcptr, compile_data *cd) +compile_branch(int *optionsptr, uschar **codeptr, const uschar **ptrptr, + int *errorcodeptr, int *firstbyteptr, int *reqbyteptr, branch_chain *bcptr, + compile_data *cd, int *lengthptr) { int repeat_type, op_type; int repeat_min = 0, repeat_max = 0; /* To please picky compilers */ @@ -1616,8 +2028,11 @@ int zeroreqbyte, zerofirstbyte; int req_caseopt, reqvary, tempreqvary; int options = *optionsptr; int after_manual_callout = 0; +int length_prevgroup = 0; register int c; register uschar *code = *codeptr; +uschar *last_code = code; +uschar *orig_code = code; uschar *tempcode; BOOL inescq = FALSE; BOOL groupsetfirstbyte = FALSE; @@ -1625,6 +2040,7 @@ const uschar *ptr = *ptrptr; const uschar *tempptr; uschar *previous = NULL; uschar *previous_callout = NULL; +uschar *save_hwm = NULL; uschar classbits[32]; #ifdef SUPPORT_UTF8 @@ -1634,6 +2050,11 @@ uschar *class_utf8data; uschar utf8_char[6]; #else BOOL utf8 = FALSE; +uschar *utf8_char = NULL; +#endif + +#ifdef DEBUG +if (lengthptr != NULL) DPRINTF((">> start branch\n")); #endif /* Set up the default and non-default settings for greediness */ @@ -1667,6 +2088,7 @@ for (;; ptr++) BOOL negate_class; BOOL possessive_quantifier; BOOL is_quantifier; + BOOL is_recurse; int class_charcount; int class_lastchar; int newoptions; @@ -1674,13 +2096,68 @@ for (;; ptr++) int skipbytes; int subreqbyte; int subfirstbyte; + int terminator; int mclength; uschar mcbuffer[8]; - /* Next byte in the pattern */ + /* Get next byte in the pattern */ c = *ptr; + /* If we are in the pre-compile phase, accumulate the length used for the + previous cycle of this loop. */ + + if (lengthptr != NULL) + { +#ifdef DEBUG + if (code > cd->hwm) cd->hwm = code; /* High water info */ +#endif + if (code > cd->start_workspace + COMPILE_WORK_SIZE) /* Check for overrun */ + { + *errorcodeptr = ERR52; + goto FAILED; + } + + /* There is at least one situation where code goes backwards: this is the + case of a zero quantifier after a class (e.g. [ab]{0}). At compile time, + the class is simply eliminated. However, it is created first, so we have to + allow memory for it. Therefore, don't ever reduce the length at this point. + */ + + if (code < last_code) code = last_code; + *lengthptr += code - last_code; + DPRINTF(("length=%d added %d c=%c\n", *lengthptr, code - last_code, c)); + + /* If "previous" is set and it is not at the start of the work space, move + it back to there, in order to avoid filling up the work space. Otherwise, + if "previous" is NULL, reset the current code pointer to the start. */ + + if (previous != NULL) + { + if (previous > orig_code) + { + memmove(orig_code, previous, code - previous); + code -= previous - orig_code; + previous = orig_code; + } + } + else code = orig_code; + + /* Remember where this code item starts so we can pick up the length + next time round. */ + + last_code = code; + } + + /* In the real compile phase, just check the workspace used by the forward + reference list. */ + + else if (cd->hwm > cd->start_workspace + COMPILE_WORK_SIZE) + { + *errorcodeptr = ERR52; + goto FAILED; + } + /* If in \Q...\E, check for the end; if not, we have a literal */ if (inescq && c != 0) @@ -1695,7 +2172,8 @@ for (;; ptr++) { if (previous_callout != NULL) { - complete_callout(previous_callout, ptr, cd); + if (lengthptr == NULL) /* Don't attempt in pre-compile phase */ + complete_callout(previous_callout, ptr, cd); previous_callout = NULL; } if ((options & PCRE_AUTO_CALLOUT) != 0) @@ -1716,7 +2194,8 @@ for (;; ptr++) if (!is_quantifier && previous_callout != NULL && after_manual_callout-- <= 0) { - complete_callout(previous_callout, ptr, cd); + if (lengthptr == NULL) /* Don't attempt in pre-compile phase */ + complete_callout(previous_callout, ptr, cd); previous_callout = NULL; } @@ -1727,12 +2206,12 @@ for (;; ptr++) if ((cd->ctypes[c] & ctype_space) != 0) continue; if (c == '#') { - while (*(++ptr) != 0) if (IS_NEWLINE(ptr)) break; - if (*ptr != 0) + while (*(++ptr) != 0) { - ptr += cd->nllen - 1; - continue; + if (IS_NEWLINE(ptr)) { ptr += cd->nllen - 1; break; } } + if (*ptr != 0) continue; + /* Else fall through to handle end of string */ c = 0; } @@ -1748,17 +2227,23 @@ for (;; ptr++) switch(c) { - /* The branch terminates at end of string, |, or ). */ - - case 0: - case '|': + /* ===================================================================*/ + case 0: /* The branch terminates at string end */ + case '|': /* or | or ) */ case ')': *firstbyteptr = firstbyte; *reqbyteptr = reqbyte; *codeptr = code; *ptrptr = ptr; + if (lengthptr != NULL) + { + *lengthptr += code - last_code; /* To include callout length */ + DPRINTF((">> end branch\n")); + } return TRUE; + + /* ===================================================================*/ /* Handle single-character metacharacters. In multiline mode, ^ disables the setting of any following char as a first character. */ @@ -1787,6 +2272,8 @@ for (;; ptr++) *code++ = OP_ANY; break; + + /* ===================================================================*/ /* Character classes. If the included characters are all < 256, we build a 32-byte bitmap of the permitted characters, except in the special case where there is only one such character. For negated classes, we build the @@ -1825,32 +2312,32 @@ for (;; ptr++) } /* Keep a count of chars with values < 256 so that we can optimize the case - of just a single character (as long as it's < 256). For higher valued UTF-8 - characters, we don't yet do any optimization. */ + of just a single character (as long as it's < 256). However, For higher + valued UTF-8 characters, we don't yet do any optimization. */ class_charcount = 0; class_lastchar = -1; + /* Initialize the 32-char bit map to all zeros. We build the map in a + temporary bit of memory, in case the class contains only 1 character (less + than 256), because in that case the compiled code doesn't use the bit map. + */ + + memset(classbits, 0, 32 * sizeof(uschar)); + #ifdef SUPPORT_UTF8 class_utf8 = FALSE; /* No chars >= 256 */ - class_utf8data = code + LINK_SIZE + 34; /* For UTF-8 items */ + class_utf8data = code + LINK_SIZE + 2; /* For UTF-8 items */ #endif - /* Initialize the 32-char bit map to all zeros. We have to build the - map in a temporary bit of store, in case the class contains only 1 - character (< 256), because in that case the compiled code doesn't use the - bit map. */ - - memset(classbits, 0, 32 * sizeof(uschar)); - /* Process characters until ] is reached. By writing this as a "do" it - means that an initial ] is taken as a data character. The first pass - through the regex checked the overall syntax, so we don't need to be very - strict here. At the start of the loop, c contains the first byte of the - character. */ + means that an initial ] is taken as a data character. At the start of the + loop, c contains the first byte of the character. */ - do + if (c != 0) do { + const uschar *oldptr; + #ifdef SUPPORT_UTF8 if (utf8 && c > 127) { /* Braces are required because the */ @@ -1862,13 +2349,13 @@ for (;; ptr++) if (inescq) { - if (c == '\\' && ptr[1] == 'E') + if (c == '\\' && ptr[1] == 'E') /* If we are at \E */ { - inescq = FALSE; - ptr++; - continue; + inescq = FALSE; /* Reset literal state */ + ptr++; /* Skip the 'E' */ + continue; /* Carry on with next */ } - else goto LONE_SINGLE_CHARACTER; + goto CHECK_RANGE; /* Could be range if \E follows */ } /* Handle POSIX class names. Perl allows a negation extension of the @@ -1959,19 +2446,20 @@ for (;; ptr++) } /* Backslash may introduce a single character, or it may introduce one - of the specials, which just set a flag. Escaped items are checked for - validity in the pre-compiling pass. The sequence \b is a special case. - Inside a class (and only there) it is treated as backspace. Elsewhere - it marks a word boundary. Other escapes have preset maps ready to - or into the one we are building. We assume they have more than one + of the specials, which just set a flag. The sequence \b is a special + case. Inside a class (and only there) it is treated as backspace. + Elsewhere it marks a word boundary. Other escapes have preset maps ready + to or into the one we are building. We assume they have more than one character in them, so set class_charcount bigger than one. */ if (c == '\\') { - c = check_escape(&ptr, errorcodeptr, *brackets, options, TRUE); + c = check_escape(&ptr, errorcodeptr, cd->bracount, options, TRUE); + if (*errorcodeptr != 0) goto FAILED; if (-c == ESC_b) c = '\b'; /* \b is backslash in a class */ else if (-c == ESC_X) c = 'X'; /* \X is literal X in a class */ + else if (-c == ESC_R) c = 'R'; /* \R is literal R in a class */ else if (-c == ESC_Q) /* Handle start of quoted string */ { if (ptr[1] == '\\' && ptr[2] == 'E') @@ -1986,7 +2474,10 @@ for (;; ptr++) { register const uschar *cbits = cd->cbits; class_charcount += 2; /* Greater than 1 is what matters */ - switch (-c) + + /* Save time by not doing this in the pre-compile phase. */ + + if (lengthptr == NULL) switch (-c) { case ESC_d: for (c = 0; c < 32; c++) classbits[c] |= cbits[c+cbit_digit]; @@ -2014,52 +2505,91 @@ for (;; ptr++) classbits[1] |= 0x08; /* Perl 5.004 onwards omits VT from \s */ continue; -#ifdef SUPPORT_UCP - case ESC_p: - case ESC_P: - { - BOOL negated; - int pdata; - int ptype = get_ucp(&ptr, &negated, &pdata, errorcodeptr); - if (ptype < 0) goto FAILED; - class_utf8 = TRUE; - *class_utf8data++ = ((-c == ESC_p) != negated)? - XCL_PROP : XCL_NOTPROP; - *class_utf8data++ = ptype; - *class_utf8data++ = pdata; - class_charcount -= 2; /* Not a < 256 character */ - } + case ESC_E: /* Perl ignores an orphan \E */ continue; -#endif - - /* Unrecognized escapes are faulted if PCRE is running in its - strict mode. By default, for compatibility with Perl, they are - treated as literals. */ - default: - if ((options & PCRE_EXTRA) != 0) - { - *errorcodeptr = ERR7; - goto FAILED; - } - c = *ptr; /* The final character */ - class_charcount -= 2; /* Undo the default count from above */ + default: /* Not recognized; fall through */ + break; /* Need "default" setting to stop compiler warning. */ } - } - /* Fall through if we have a single character (c >= 0). This may be - > 256 in UTF-8 mode. */ + /* In the pre-compile phase, just do the recognition. */ - } /* End of backslash handling */ + else if (c == -ESC_d || c == -ESC_D || c == -ESC_w || + c == -ESC_W || c == -ESC_s || c == -ESC_S) continue; + + /* We need to deal with \P and \p in both phases. */ + +#ifdef SUPPORT_UCP + if (-c == ESC_p || -c == ESC_P) + { + BOOL negated; + int pdata; + int ptype = get_ucp(&ptr, &negated, &pdata, errorcodeptr); + if (ptype < 0) goto FAILED; + class_utf8 = TRUE; + *class_utf8data++ = ((-c == ESC_p) != negated)? + XCL_PROP : XCL_NOTPROP; + *class_utf8data++ = ptype; + *class_utf8data++ = pdata; + class_charcount -= 2; /* Not a < 256 character */ + continue; + } +#endif + /* Unrecognized escapes are faulted if PCRE is running in its + strict mode. By default, for compatibility with Perl, they are + treated as literals. */ + + if ((options & PCRE_EXTRA) != 0) + { + *errorcodeptr = ERR7; + goto FAILED; + } + + class_charcount -= 2; /* Undo the default count from above */ + c = *ptr; /* Get the final character and fall through */ + } + + /* Fall through if we have a single character (c >= 0). This may be + greater than 256 in UTF-8 mode. */ + + } /* End of backslash handling */ /* A single character may be followed by '-' to form a range. However, Perl does not permit ']' to be the end of the range. A '-' character - here is treated as a literal. */ + at the end is treated as a literal. Perl ignores orphaned \E sequences + entirely. The code for handling \Q and \E is messy. */ + + CHECK_RANGE: + while (ptr[1] == '\\' && ptr[2] == 'E') + { + inescq = FALSE; + ptr += 2; + } + + oldptr = ptr; - if (ptr[1] == '-' && ptr[2] != ']') + if (!inescq && ptr[1] == '-') { int d; ptr += 2; + while (*ptr == '\\' && ptr[1] == 'E') ptr += 2; + + /* If we hit \Q (not followed by \E) at this point, go into escaped + mode. */ + + while (*ptr == '\\' && ptr[1] == 'Q') + { + ptr += 2; + if (*ptr == '\\' && ptr[1] == 'E') { ptr += 2; continue; } + inescq = TRUE; + break; + } + + if (*ptr == 0 || (!inescq && *ptr == ']')) + { + ptr = oldptr; + goto LONE_SINGLE_CHARACTER; + } #ifdef SUPPORT_UTF8 if (utf8) @@ -2074,27 +2604,34 @@ for (;; ptr++) not any of the other escapes. Perl 5.6 treats a hyphen as a literal in such circumstances. */ - if (d == '\\') + if (!inescq && d == '\\') { - const uschar *oldptr = ptr; - d = check_escape(&ptr, errorcodeptr, *brackets, options, TRUE); + d = check_escape(&ptr, errorcodeptr, cd->bracount, options, TRUE); + if (*errorcodeptr != 0) goto FAILED; - /* \b is backslash; \X is literal X; any other special means the '-' - was literal */ + /* \b is backslash; \X is literal X; \R is literal R; any other + special means the '-' was literal */ if (d < 0) { if (d == -ESC_b) d = '\b'; - else if (d == -ESC_X) d = 'X'; else + else if (d == -ESC_X) d = 'X'; + else if (d == -ESC_R) d = 'R'; else { - ptr = oldptr - 2; + ptr = oldptr; goto LONE_SINGLE_CHARACTER; /* A few lines below */ } } } - /* The check that the two values are in the correct order happens in - the pre-pass. Optimize one-character ranges */ + /* Check that the two values are in the correct order. Optimize + one-character ranges */ + + if (d < c) + { + *errorcodeptr = ERR8; + goto FAILED; + } if (d == c) goto LONE_SINGLE_CHARACTER; /* A few lines below */ @@ -2115,9 +2652,9 @@ for (;; ptr++) #ifdef SUPPORT_UCP if ((options & PCRE_CASELESS) != 0) { - int occ, ocd; - int cc = c; - int origd = d; + unsigned int occ, ocd; + unsigned int cc = c; + unsigned int origd = d; while (get_othercase_range(&cc, origd, &occ, &ocd)) { if (occ >= c && ocd <= d) continue; /* Skip embedded ranges */ @@ -2175,7 +2712,12 @@ for (;; ptr++) ranges that lie entirely within 0-127 when there is UCP support; else for partial ranges without UCP support. */ - for (; c <= d; c++) + class_charcount += d - c + 1; + class_lastchar = d; + + /* We can save a bit of time by skipping this in the pre-compile. */ + + if (lengthptr == NULL) for (; c <= d; c++) { classbits[c/8] |= (1 << (c&7)); if ((options & PCRE_CASELESS) != 0) @@ -2183,8 +2725,6 @@ for (;; ptr++) int uc = cd->fcc[c]; /* flip case */ classbits[uc/8] |= (1 << (uc&7)); } - class_charcount++; /* in case a one-char range */ - class_lastchar = c; } continue; /* Go get the next char in the class */ @@ -2208,8 +2748,8 @@ for (;; ptr++) #ifdef SUPPORT_UCP if ((options & PCRE_CASELESS) != 0) { - int othercase; - if ((othercase = _pcre_ucp_othercase(c)) >= 0) + unsigned int othercase; + if ((othercase = _pcre_ucp_othercase(c)) != NOTACHAR) { *class_utf8data++ = XCL_SINGLE; class_utf8data += _pcre_ord2utf8(othercase, class_utf8data); @@ -2234,10 +2774,15 @@ for (;; ptr++) } } - /* Loop until ']' reached; the check for end of string happens inside the - loop. This "while" is the end of the "do" above. */ + /* Loop until ']' reached. This "while" is the end of the "do" above. */ - while ((c = *(++ptr)) != ']' || inescq); + while ((c = *(++ptr)) != 0 && (c != ']' || inescq)); + + if (c == 0) /* Missing terminating ']' */ + { + *errorcodeptr = ERR6; + goto FAILED; + } /* If class_charcount is 1, we saw precisely one character whose value is less than 256. In non-UTF-8 mode we can always optimize. In UTF-8 mode, we @@ -2301,7 +2846,7 @@ for (;; ptr++) /* If there are characters with values > 255, we have to compile an extended class, with its own opcode. If there are no characters < 256, - we can omit the bitmap. */ + we can omit the bitmap in the actual compiled code. */ #ifdef SUPPORT_UTF8 if (class_utf8) @@ -2311,24 +2856,17 @@ for (;; ptr++) code += LINK_SIZE; *code = negate_class? XCL_NOT : 0; - /* If the map is required, install it, and move on to the end of - the extra data */ + /* If the map is required, move up the extra data to make room for it; + otherwise just move the code pointer to the end of the extra data. */ if (class_charcount > 0) { *code++ |= XCL_MAP; + memmove(code + 32, code, class_utf8data - code); memcpy(code, classbits, 32); - code = class_utf8data; - } - - /* If the map is not required, slide down the extra data. */ - - else - { - int len = class_utf8data - (code + 33); - memmove(code + 1, code + 33, len); - code += len + 1; + code = class_utf8data + 32; } + else code = class_utf8data; /* Now fill in the complete length of the item */ @@ -2345,7 +2883,8 @@ for (;; ptr++) if (negate_class) { *code++ = OP_NCLASS; - for (c = 0; c < 32; c++) code[c] = ~classbits[c]; + if (lengthptr == NULL) /* Save time in the pre-compile phase */ + for (c = 0; c < 32; c++) code[c] = ~classbits[c]; } else { @@ -2355,6 +2894,8 @@ for (;; ptr++) code += 32; break; + + /* ===================================================================*/ /* Various kinds of repeat; '{' is not necessarily a quantifier, but this has been tested above. */ @@ -2422,20 +2963,6 @@ for (;; ptr++) } else repeat_type = greedy_default; - /* If previous was a recursion, we need to wrap it inside brackets so that - it can be replicated if necessary. */ - - if (*previous == OP_RECURSE) - { - memmove(previous + 1 + LINK_SIZE, previous, 1 + LINK_SIZE); - code += 1 + LINK_SIZE; - *previous = OP_BRA; - PUT(previous, 1, code - previous); - *code = OP_KET; - PUT(code, 1, code - previous); - code += 1 + LINK_SIZE; - } - /* If previous was a character match, abolish the item and generate a repeat item instead. If a char item has a minumum of more than one, ensure that it is set in reqbyte - it might not be if a sequence such as x{3} is @@ -2469,18 +2996,40 @@ for (;; ptr++) if (repeat_min > 1) reqbyte = c | req_caseopt | cd->req_varyopt; } + /* 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, c, utf8, utf8_char, ptr + 1, + options, cd)) + { + repeat_type = 0; /* Force greedy */ + possessive_quantifier = TRUE; + } + goto OUTPUT_SINGLE_REPEAT; /* Code shared with single character types */ } /* If previous was a single negated character ([^a] or similar), we use one of the special opcodes, replacing it. The code is shared with single- character repeats by setting opt_type to add a suitable offset into - repeat_type. OP_NOT is currently used only for single-byte chars. */ + repeat_type. We can also test for auto-possessification. OP_NOT is + currently used only for single-byte chars. */ else if (*previous == OP_NOT) { op_type = OP_NOTSTAR - OP_STAR; /* Use "not" opcodes */ c = previous[1]; + if (!possessive_quantifier && + repeat_max < 0 && + check_auto_possessive(OP_NOT, c, utf8, NULL, ptr + 1, options, cd)) + { + repeat_type = 0; /* Force greedy */ + possessive_quantifier = TRUE; + } goto OUTPUT_SINGLE_REPEAT; } @@ -2498,6 +3047,14 @@ for (;; ptr++) op_type = OP_TYPESTAR - OP_STAR; /* Use type opcodes */ c = *previous; + if (!possessive_quantifier && + repeat_max < 0 && + check_auto_possessive(c, 0, utf8, NULL, ptr + 1, options, cd)) + { + repeat_type = 0; /* Force greedy */ + possessive_quantifier = TRUE; + } + OUTPUT_SINGLE_REPEAT: if (*previous == OP_PROP || *previous == OP_NOTPROP) { @@ -2538,7 +3095,7 @@ for (;; ptr++) } /* A repeat minimum of 1 is optimized into some special cases. If the - maximum is unlimited, we use OP_PLUS. Otherwise, the original item it + maximum is unlimited, we use OP_PLUS. Otherwise, the original item is left in place and, if the maximum is greater than 1, we use OP_UPTO with one less than the maximum. */ @@ -2591,7 +3148,8 @@ for (;; ptr++) } /* Else insert an UPTO if the max is greater than the min, again - preceded by the character, for the previously inserted code. */ + preceded by the character, for the previously inserted code. If the + UPTO is just for 1 instance, we can use QUERY instead. */ else if (repeat_max != repeat_min) { @@ -2610,8 +3168,16 @@ for (;; ptr++) *code++ = prop_value; } repeat_max -= repeat_min; - *code++ = OP_UPTO + repeat_type; - PUT2INC(code, 0, repeat_max); + + if (repeat_max == 1) + { + *code++ = OP_QUERY + repeat_type; + } + else + { + *code++ = OP_UPTO + repeat_type; + PUT2INC(code, 0, repeat_max); + } } } @@ -2678,14 +3244,30 @@ for (;; ptr++) /* If previous was a bracket group, we may have to replicate it in certain cases. */ - else if (*previous >= OP_BRA || *previous == OP_ONCE || - *previous == OP_COND) + else if (*previous == OP_BRA || *previous == OP_CBRA || + *previous == OP_ONCE || *previous == OP_COND) { register int i; int ketoffset = 0; int len = code - previous; uschar *bralink = NULL; + /* Repeating a DEFINE group is pointless */ + + if (*previous == OP_COND && previous[LINK_SIZE+1] == OP_DEF) + { + *errorcodeptr = ERR55; + goto FAILED; + } + + /* This is a paranoid check to stop integer overflow later on */ + + if (len > MAX_DUPLENGTH) + { + *errorcodeptr = ERR50; + goto FAILED; + } + /* If the maximum repeat count is unlimited, find the end of the bracket by scanning through from the start, and compute the offset back to it from the current code pointer. There may be an OP_OPT setting following @@ -2720,13 +3302,14 @@ for (;; ptr++) /* If the maximum is 1 or unlimited, we just have to stick in the BRAZERO and do no more at this point. However, we do need to adjust any OP_RECURSE calls inside the group that refer to the group itself or - any internal group, because the offset is from the start of the whole - regex. Temporarily terminate the pattern while doing this. */ + any internal or forward referenced group, because the offset is from + the start of the whole regex. Temporarily terminate the pattern while + doing this. */ if (repeat_max <= 1) { *code = OP_END; - adjust_recurse(previous, 1, utf8, cd); + adjust_recurse(previous, 1, utf8, cd, save_hwm); memmove(previous+1, previous, len); code++; *previous++ = OP_BRAZERO + repeat_type; @@ -2744,7 +3327,7 @@ for (;; ptr++) { int offset; *code = OP_END; - adjust_recurse(previous, 2 + LINK_SIZE, utf8, cd); + adjust_recurse(previous, 2 + LINK_SIZE, utf8, cd, save_hwm); memmove(previous + 2 + LINK_SIZE, previous, len); code += 2 + LINK_SIZE; *previous++ = OP_BRAZERO + repeat_type; @@ -2764,19 +3347,41 @@ for (;; ptr++) /* If the minimum is greater than zero, replicate the group as many times as necessary, and adjust the maximum to the number of subsequent copies that we need. If we set a first char from the group, and didn't - set a required char, copy the latter from the former. */ + set a required char, copy the latter from the former. If there are any + forward reference subroutine calls in the group, there will be entries on + the workspace list; replicate these with an appropriate increment. */ else { if (repeat_min > 1) { - if (groupsetfirstbyte && reqbyte < 0) reqbyte = firstbyte; - for (i = 1; i < repeat_min; i++) + /* In the pre-compile phase, we don't actually do the replication. We + just adjust the length as if we had. */ + + if (lengthptr != NULL) + *lengthptr += (repeat_min - 1)*length_prevgroup; + + /* This is compiling for real */ + + else { - memcpy(code, previous, len); - code += len; + if (groupsetfirstbyte && reqbyte < 0) reqbyte = firstbyte; + for (i = 1; i < repeat_min; i++) + { + uschar *hc; + uschar *this_hwm = cd->hwm; + memcpy(code, previous, len); + for (hc = save_hwm; hc < this_hwm; hc += LINK_SIZE) + { + PUT(cd->hwm, 0, GET(hc, 0) + len); + cd->hwm += LINK_SIZE; + } + save_hwm = this_hwm; + code += len; + } } } + if (repeat_max > 0) repeat_max -= repeat_min; } @@ -2784,12 +3389,27 @@ for (;; ptr++) the maximum is limited, it replicates the group in a nested fashion, remembering the bracket starts on a stack. In the case of a zero minimum, the first one was set up above. In all cases the repeat_max now specifies - the number of additional copies needed. */ + the number of additional copies needed. Again, we must remember to + replicate entries on the forward reference list. */ if (repeat_max >= 0) { - for (i = repeat_max - 1; i >= 0; i--) + /* In the pre-compile phase, we don't actually do the replication. We + just adjust the length as if we had. For each repetition we must add 1 + to the length for BRAZERO and for all but the last repetition we must + add 2 + 2*LINKSIZE to allow for the nesting that occurs. */ + + if (lengthptr != NULL && repeat_max > 0) + *lengthptr += repeat_max * (length_prevgroup + 1 + 2 + 2*LINK_SIZE) - + 2 - 2*LINK_SIZE; /* Last one doesn't nest */ + + /* This is compiling for real */ + + else for (i = repeat_max - 1; i >= 0; i--) { + uschar *hc; + uschar *this_hwm = cd->hwm; + *code++ = OP_BRAZERO + repeat_type; /* All but the final copy start a new nesting, maintaining the @@ -2805,6 +3425,12 @@ for (;; ptr++) } memcpy(code, previous, len); + for (hc = save_hwm; hc < this_hwm; hc += LINK_SIZE) + { + PUT(cd->hwm, 0, GET(hc, 0) + len + ((i != 0)? 2+LINK_SIZE : 1)); + cd->hwm += LINK_SIZE; + } + save_hwm = this_hwm; code += len; } @@ -2827,9 +3453,34 @@ for (;; ptr++) /* If the maximum is unlimited, set a repeater in the final copy. We can't just offset backwards from the current code point, because we don't know if there's been an options resetting after the ket. The - correct offset was computed above. */ + correct offset was computed above. - else code[-ketoffset] = OP_KETRMAX + repeat_type; + Then, when we are doing the actual compile phase, check to see whether + this group is a non-atomic one that could match an empty string. If so, + convert the initial operator to the S form (e.g. OP_BRA -> OP_SBRA) so + that runtime checking can be done. [This check is also applied to + atomic groups at runtime, but in a different way.] */ + + else + { + uschar *ketcode = code - ketoffset; + uschar *bracode = ketcode - GET(ketcode, 1); + *ketcode = OP_KETRMAX + repeat_type; + if (lengthptr == NULL && *bracode != OP_ONCE) + { + uschar *scode = bracode; + do + { + if (could_be_empty_branch(scode, ketcode, utf8)) + { + *bracode += OP_SBRA - OP_BRA; + break; + } + scode += GET(scode, 1); + } + while (*scode == OP_ALT); + } + } } /* Else there's some kind of shambles */ @@ -2840,22 +3491,53 @@ for (;; ptr++) goto FAILED; } - /* If the character following a repeat is '+', we wrap the entire repeated - item inside OP_ONCE brackets. This is just syntactic sugar, taken from - Sun's Java package. The repeated item starts at tempcode, not at previous, - which might be the first part of a string whose (former) last char we - repeated. However, we don't support '+' after a greediness '?'. */ + /* If the character following a repeat is '+', or if certain optimization + tests above succeeded, possessive_quantifier is TRUE. For some of the + simpler opcodes, there is an special alternative opcode for this. For + anything else, we wrap the entire repeated item inside OP_ONCE brackets. + The '+' notation is just syntactic sugar, taken from Sun's Java package, + but the special opcodes can optimize it a bit. The repeated item starts at + tempcode, not at previous, which 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. */ if (possessive_quantifier) { - int len = code - tempcode; - memmove(tempcode + 1+LINK_SIZE, tempcode, len); - code += 1 + LINK_SIZE; - len += 1 + LINK_SIZE; - tempcode[0] = OP_ONCE; - *code++ = OP_KET; - PUTINC(code, 0, len); - PUT(tempcode, 1, len); + int len; + if (*tempcode == OP_EXACT || *tempcode == OP_TYPEEXACT || + *tempcode == OP_NOTEXACT) + tempcode += _pcre_OP_lengths[*tempcode]; + len = code - tempcode; + if (len > 0) switch (*tempcode) + { + case OP_STAR: *tempcode = OP_POSSTAR; break; + case OP_PLUS: *tempcode = OP_POSPLUS; break; + case OP_QUERY: *tempcode = OP_POSQUERY; break; + case OP_UPTO: *tempcode = OP_POSUPTO; break; + + case OP_TYPESTAR: *tempcode = OP_TYPEPOSSTAR; break; + case OP_TYPEPLUS: *tempcode = OP_TYPEPOSPLUS; break; + case OP_TYPEQUERY: *tempcode = OP_TYPEPOSQUERY; break; + case OP_TYPEUPTO: *tempcode = OP_TYPEPOSUPTO; break; + + case OP_NOTSTAR: *tempcode = OP_NOTPOSSTAR; break; + case OP_NOTPLUS: *tempcode = OP_NOTPOSPLUS; break; + case OP_NOTQUERY: *tempcode = OP_NOTPOSQUERY; break; + case OP_NOTUPTO: *tempcode = OP_NOTPOSUPTO; break; + + default: + memmove(tempcode + 1+LINK_SIZE, tempcode, len); + code += 1 + LINK_SIZE; + len += 1 + LINK_SIZE; + tempcode[0] = OP_ONCE; + *code++ = OP_KET; + PUTINC(code, 0, len); + PUT(tempcode, 1, len); + break; + } } /* In all case we no longer have a previous item. We also set the @@ -2868,162 +3550,275 @@ for (;; ptr++) break; - /* Start of nested bracket sub-expression, or comment or lookahead or - lookbehind or option setting or condition. First deal with special things - that can come after a bracket; all are introduced by ?, and the appearance - of any of them means that this is not a referencing group. They were - checked for validity in the first pass over the string, so we don't have to - check for syntax errors here. */ + /* ===================================================================*/ + /* Start of nested parenthesized sub-expression, or comment or lookahead or + lookbehind or option setting or condition or all the other extended + parenthesis forms. First deal with the specials; all are introduced by ?, + and the appearance of any of them means that this is not a capturing + group. */ case '(': newoptions = options; skipbytes = 0; + bravalue = OP_CBRA; + save_hwm = cd->hwm; if (*(++ptr) == '?') { - int set, unset; + int i, set, unset, namelen; int *optset; + const uschar *name; + uschar *slot; switch (*(++ptr)) { case '#': /* Comment; skip to ket */ ptr++; - while (*ptr != ')') ptr++; + while (*ptr != 0 && *ptr != ')') ptr++; + if (*ptr == 0) + { + *errorcodeptr = ERR18; + goto FAILED; + } continue; - case ':': /* Non-extracting bracket */ + + /* ------------------------------------------------------------ */ + case ':': /* Non-capturing bracket */ bravalue = OP_BRA; ptr++; break; + + /* ------------------------------------------------------------ */ case '(': bravalue = OP_COND; /* Conditional group */ - /* A condition can be a number, referring to a numbered group, a name, - referring to a named group, 'R', referring to recursion, or an - assertion. There are two unfortunate ambiguities, caused by history. - (a) 'R' can be the recursive thing or the name 'R', and (b) a number - could be a name that consists of digits. In both cases, we look for a - name first; if not found, we try the other cases. If the first - character after (?( is a word character, we know the rest up to ) will - also be word characters because the syntax was checked in the first - pass. */ - - if ((cd->ctypes[ptr[1]] & ctype_word) != 0) + /* A condition can be an assertion, a number (referring to a numbered + group), a name (referring to a named group), or 'R', referring to + recursion. R and R&name are also permitted for recursion tests. + + There are several syntaxes for testing a named group: (?(name)) is used + by Python; Perl 5.10 onwards uses (?() or (?('name')). + + There are two unfortunate ambiguities, caused by history. (a) 'R' can + be the recursive thing or the name 'R' (and similarly for 'R' followed + by digits), and (b) a number could be a name that consists of digits. + In both cases, we look for a name first; if not found, we try the other + cases. */ + + /* For conditions that are assertions, check the syntax, and then exit + the switch. This will take control down to where bracketed groups, + including assertions, are processed. */ + + if (ptr[1] == '?' && (ptr[2] == '=' || ptr[2] == '!' || ptr[2] == '<')) + break; + + /* Most other conditions use OP_CREF (a couple change to OP_RREF + below), and all need to skip 3 bytes at the start of the group. */ + + code[1+LINK_SIZE] = OP_CREF; + skipbytes = 3; + + /* Check for a test for recursion in a named group. */ + + if (ptr[1] == 'R' && ptr[2] == '&') { - int i, namelen; - int condref = 0; - const uschar *name; - uschar *slot = cd->name_table; + terminator = -1; + ptr += 2; + code[1+LINK_SIZE] = OP_RREF; /* Change the type of test */ + } - /* This is needed for all successful cases. */ + /* Check for a test for a named group's having been set, using the Perl + syntax (?() or (?('name') */ - skipbytes = 3; + else if (ptr[1] == '<') + { + terminator = '>'; + ptr++; + } + else if (ptr[1] == '\'') + { + terminator = '\''; + ptr++; + } + else terminator = 0; - /* Read the name, but also get it as a number if it's all digits */ + /* We now expect to read a name; any thing else is an error */ - name = ++ptr; - while (*ptr != ')') - { - if (condref >= 0) - condref = ((digitab[*ptr] & ctype_digit) != 0)? - condref * 10 + *ptr - '0' : -1; - ptr++; - } - namelen = ptr - name; + if ((cd->ctypes[ptr[1]] & ctype_word) == 0) + { + ptr += 1; /* To get the right offset */ + *errorcodeptr = ERR28; + goto FAILED; + } + + /* Read the name, but also get it as a number if it's all digits */ + + recno = 0; + name = ++ptr; + while ((cd->ctypes[*ptr] & ctype_word) != 0) + { + if (recno >= 0) + recno = ((digitab[*ptr] & ctype_digit) != 0)? + recno * 10 + *ptr - '0' : -1; ptr++; + } + namelen = ptr - name; - for (i = 0; i < cd->names_found; i++) - { - if (strncmp((char *)name, (char *)slot+2, namelen) == 0) break; - slot += cd->name_entry_size; - } + if ((terminator > 0 && *ptr++ != terminator) || *ptr++ != ')') + { + ptr--; /* Error offset */ + *errorcodeptr = ERR26; + goto FAILED; + } - /* Found a previous named subpattern */ + /* Do no further checking in the pre-compile phase. */ - if (i < cd->names_found) - { - condref = GET2(slot, 0); - code[1+LINK_SIZE] = OP_CREF; - PUT2(code, 2+LINK_SIZE, condref); - } + if (lengthptr != NULL) break; - /* Search the pattern for a forward reference */ + /* In the real compile we do the work of looking for the actual + reference. */ - else if ((i = find_named_parens(ptr, *brackets, name, namelen)) > 0) - { - code[1+LINK_SIZE] = OP_CREF; - PUT2(code, 2+LINK_SIZE, i); - } + slot = cd->name_table; + for (i = 0; i < cd->names_found; i++) + { + if (strncmp((char *)name, (char *)slot+2, namelen) == 0) break; + slot += cd->name_entry_size; + } - /* Check for 'R' for recursion */ + /* Found a previous named subpattern */ - else if (namelen == 1 && *name == 'R') - { - code[1+LINK_SIZE] = OP_CREF; - PUT2(code, 2+LINK_SIZE, CREF_RECURSE); - } + if (i < cd->names_found) + { + recno = GET2(slot, 0); + PUT2(code, 2+LINK_SIZE, recno); + } - /* Check for a subpattern number */ + /* Search the pattern for a forward reference */ - else if (condref > 0) - { - code[1+LINK_SIZE] = OP_CREF; - PUT2(code, 2+LINK_SIZE, condref); - } + else if ((i = find_parens(ptr, cd->bracount, name, namelen, + (options & PCRE_EXTENDED) != 0)) > 0) + { + PUT2(code, 2+LINK_SIZE, i); + } - /* Either an unidentified subpattern, or a reference to (?(0) */ + /* If terminator == 0 it means that the name followed directly after + the opening parenthesis [e.g. (?(abc)...] and in this case there are + some further alternatives to try. For the cases where terminator != 0 + [things like (?(... or (?('name')... or (?(R&name)... ] we have + now checked all the possibilities, so give an error. */ - else + else if (terminator != 0) + { + *errorcodeptr = ERR15; + goto FAILED; + } + + /* Check for (?(R) for recursion. Allow digits after R to specify a + specific group number. */ + + else if (*name == 'R') + { + recno = 0; + for (i = 1; i < namelen; i++) { - *errorcodeptr = (condref == 0)? ERR35: ERR15; - goto FAILED; + if ((digitab[name[i]] & ctype_digit) == 0) + { + *errorcodeptr = ERR15; + goto FAILED; + } + recno = recno * 10 + name[i] - '0'; } + if (recno == 0) recno = RREF_ANY; + code[1+LINK_SIZE] = OP_RREF; /* Change test type */ + PUT2(code, 2+LINK_SIZE, recno); + } + + /* Similarly, check for the (?(DEFINE) "condition", which is always + false. */ + + else if (namelen == 6 && strncmp((char *)name, "DEFINE", 6) == 0) + { + code[1+LINK_SIZE] = OP_DEF; + skipbytes = 1; + } + + /* Check for the "name" actually being a subpattern number. */ + + else if (recno > 0) + { + PUT2(code, 2+LINK_SIZE, recno); } - /* For conditions that are assertions, we just fall through, having - set bravalue above. */ + /* Either an unidentified subpattern, or a reference to (?(0) */ + else + { + *errorcodeptr = (recno == 0)? ERR35: ERR15; + goto FAILED; + } break; + + /* ------------------------------------------------------------ */ case '=': /* Positive lookahead */ bravalue = OP_ASSERT; ptr++; break; + + /* ------------------------------------------------------------ */ case '!': /* Negative lookahead */ bravalue = OP_ASSERT_NOT; ptr++; break; - case '<': /* Lookbehinds */ - switch (*(++ptr)) + + /* ------------------------------------------------------------ */ + case '<': /* Lookbehind or named define */ + switch (ptr[1]) { case '=': /* Positive lookbehind */ bravalue = OP_ASSERTBACK; - ptr++; + ptr += 2; break; case '!': /* Negative lookbehind */ bravalue = OP_ASSERTBACK_NOT; - ptr++; + ptr += 2; break; + + default: /* Could be name define, else bad */ + if ((cd->ctypes[ptr[1]] & ctype_word) != 0) goto DEFINE_NAME; + ptr++; /* Correct offset for error */ + *errorcodeptr = ERR24; + goto FAILED; } break; + + /* ------------------------------------------------------------ */ case '>': /* One-time brackets */ bravalue = OP_ONCE; ptr++; break; + + /* ------------------------------------------------------------ */ case 'C': /* Callout - may be followed by digits; */ previous_callout = code; /* Save for later completion */ after_manual_callout = 1; /* Skip one item before completing */ - *code++ = OP_CALLOUT; /* Already checked that the terminating */ - { /* closing parenthesis is present. */ + *code++ = OP_CALLOUT; + { int n = 0; while ((digitab[*(++ptr)] & ctype_digit) != 0) n = n * 10 + *ptr - '0'; + if (*ptr != ')') + { + *errorcodeptr = ERR39; + goto FAILED; + } if (n > 255) { *errorcodeptr = ERR38; @@ -3037,134 +3832,232 @@ for (;; ptr++) previous = NULL; continue; - case 'P': /* Named subpattern handling */ - if (*(++ptr) == '<') /* Definition */ + + /* ------------------------------------------------------------ */ + case 'P': /* Python-style named subpattern handling */ + if (*(++ptr) == '=' || *ptr == '>') /* Reference or recursion */ + { + is_recurse = *ptr == '>'; + terminator = ')'; + goto NAMED_REF_OR_RECURSE; + } + else if (*ptr != '<') /* Test for Python-style definition */ { - int i, namelen; - uschar *slot = cd->name_table; - const uschar *name; /* Don't amalgamate; some compilers */ - name = ++ptr; /* grumble at autoincrement in declaration */ + *errorcodeptr = ERR41; + goto FAILED; + } + /* Fall through to handle (?P< as (?< is handled */ - while (*ptr++ != '>'); - namelen = ptr - name - 1; - for (i = 0; i < cd->names_found; i++) + /* ------------------------------------------------------------ */ + DEFINE_NAME: /* Come here from (?< handling */ + case '\'': + { + terminator = (*ptr == '<')? '>' : '\''; + name = ++ptr; + + while ((cd->ctypes[*ptr] & ctype_word) != 0) ptr++; + namelen = ptr - name; + + /* In the pre-compile phase, just do a syntax check. */ + + if (lengthptr != NULL) { - int crc = memcmp(name, slot+2, namelen); - if (crc == 0) + if (*ptr != terminator) + { + *errorcodeptr = ERR42; + goto FAILED; + } + if (cd->names_found >= MAX_NAME_COUNT) + { + *errorcodeptr = ERR49; + goto FAILED; + } + if (namelen + 3 > cd->name_entry_size) { - if (slot[2+namelen] == 0) + cd->name_entry_size = namelen + 3; + if (namelen > MAX_NAME_SIZE) { - if ((options & PCRE_DUPNAMES) == 0) - { - *errorcodeptr = ERR43; - goto FAILED; - } + *errorcodeptr = ERR48; + goto FAILED; } - else crc = -1; /* Current name is substring */ } - if (crc < 0) + } + + /* In the real compile, create the entry in the table */ + + else + { + slot = cd->name_table; + for (i = 0; i < cd->names_found; i++) { - memmove(slot + cd->name_entry_size, slot, - (cd->names_found - i) * cd->name_entry_size); - break; + int crc = memcmp(name, slot+2, namelen); + if (crc == 0) + { + if (slot[2+namelen] == 0) + { + if ((options & PCRE_DUPNAMES) == 0) + { + *errorcodeptr = ERR43; + goto FAILED; + } + } + else crc = -1; /* Current name is substring */ + } + if (crc < 0) + { + memmove(slot + cd->name_entry_size, slot, + (cd->names_found - i) * cd->name_entry_size); + break; + } + slot += cd->name_entry_size; } - slot += cd->name_entry_size; - } - PUT2(slot, 0, *brackets + 1); - memcpy(slot + 2, name, namelen); - slot[2+namelen] = 0; - cd->names_found++; - goto NUMBERED_GROUP; + PUT2(slot, 0, cd->bracount + 1); + memcpy(slot + 2, name, namelen); + slot[2+namelen] = 0; + } } - if (*ptr == '=' || *ptr == '>') /* Reference or recursion */ - { - int i, namelen; - int type = *ptr++; - const uschar *name = ptr; - uschar *slot = cd->name_table; + /* In both cases, count the number of names we've encountered. */ - while (*ptr != ')') ptr++; - namelen = ptr - name; + ptr++; /* Move past > or ' */ + cd->names_found++; + goto NUMBERED_GROUP; - for (i = 0; i < cd->names_found; i++) - { - if (strncmp((char *)name, (char *)slot+2, namelen) == 0) break; - slot += cd->name_entry_size; - } + + /* ------------------------------------------------------------ */ + case '&': /* Perl recursion/subroutine syntax */ + terminator = ')'; + is_recurse = TRUE; + /* Fall through */ + + /* We come here from the Python syntax above that handles both + references (?P=name) and recursion (?P>name), as well as falling + through from the Perl recursion syntax (?&name). */ + + NAMED_REF_OR_RECURSE: + name = ++ptr; + while ((cd->ctypes[*ptr] & ctype_word) != 0) ptr++; + namelen = ptr - name; + + /* In the pre-compile phase, do a syntax check and set a dummy + reference number. */ + + if (lengthptr != NULL) + { + if (*ptr != terminator) + { + *errorcodeptr = ERR42; + goto FAILED; + } + if (namelen > MAX_NAME_SIZE) + { + *errorcodeptr = ERR48; + goto FAILED; + } + recno = 0; + } + + /* In the real compile, seek the name in the table */ + + else + { + slot = cd->name_table; + for (i = 0; i < cd->names_found; i++) + { + if (strncmp((char *)name, (char *)slot+2, namelen) == 0) break; + slot += cd->name_entry_size; + } if (i < cd->names_found) /* Back reference */ { recno = GET2(slot, 0); } else if ((recno = /* Forward back reference */ - find_named_parens(ptr, *brackets, name, namelen)) <= 0) + find_parens(ptr, cd->bracount, name, namelen, + (options & PCRE_EXTENDED) != 0)) <= 0) { *errorcodeptr = ERR15; goto FAILED; } + } - if (type == '>') goto HANDLE_RECURSION; /* A few lines below */ - - /* Back reference */ + /* In both phases, we can now go to the code than handles numerical + recursion or backreferences. */ - previous = code; - *code++ = OP_REF; - PUT2INC(code, 0, recno); - cd->backref_map |= (recno < 32)? (1 << recno) : 1; - if (recno > cd->top_backref) cd->top_backref = recno; - continue; - } + if (is_recurse) goto HANDLE_RECURSION; + else goto HANDLE_REFERENCE; - /* Should never happen */ - break; - case 'R': /* Pattern recursion */ + /* ------------------------------------------------------------ */ + case 'R': /* Recursion */ ptr++; /* Same as (?0) */ /* Fall through */ - /* Recursion or "subroutine" call */ - case '0': case '1': case '2': case '3': case '4': - case '5': case '6': case '7': case '8': case '9': + /* ------------------------------------------------------------ */ + case '0': case '1': case '2': case '3': case '4': /* Recursion or */ + case '5': case '6': case '7': case '8': case '9': /* subroutine */ { const uschar *called; recno = 0; while((digitab[*ptr] & ctype_digit) != 0) recno = recno * 10 + *ptr++ - '0'; + if (*ptr != ')') + { + *errorcodeptr = ERR29; + goto FAILED; + } /* Come here from code above that handles a named recursion */ HANDLE_RECURSION: previous = code; + called = cd->start_code; - /* Find the bracket that is being referenced. Temporarily end the - regex in case it doesn't exist. */ + /* When we are actually compiling, find the bracket that is being + referenced. Temporarily end the regex in case it doesn't exist before + this point. If we end up with a forward reference, first check that + the bracket does occur later so we can give the error (and position) + now. Then remember this forward reference in the workspace so it can + be filled in at the end. */ - *code = OP_END; - called = (recno == 0)? cd->start_code : - find_bracket(cd->start_code, utf8, recno); - if (called == NULL) + if (lengthptr == NULL) { - *errorcodeptr = ERR15; - goto FAILED; - } + *code = OP_END; + if (recno != 0) called = find_bracket(cd->start_code, utf8, recno); - /* If the subpattern is still open, this is a recursive call. We - check to see if this is a left recursion that could loop for ever, - and diagnose that case. */ + /* Forward reference */ - if (GET(called, 1) == 0 && could_be_empty(called, code, bcptr, utf8)) - { - *errorcodeptr = ERR40; - goto FAILED; + if (called == NULL) + { + if (find_parens(ptr, cd->bracount, NULL, recno, + (options & PCRE_EXTENDED) != 0) < 0) + { + *errorcodeptr = ERR15; + goto FAILED; + } + called = cd->start_code + recno; + PUTINC(cd->hwm, 0, code + 2 + LINK_SIZE - cd->start_code); + } + + /* If not a forward reference, and the subpattern is still open, + this is a recursive call. We check to see if this is a left + recursion that could loop for ever, and diagnose that case. */ + + else if (GET(called, 1) == 0 && + could_be_empty(called, code, bcptr, utf8)) + { + *errorcodeptr = ERR40; + goto FAILED; + } } /* Insert the recursion/subroutine item, automatically wrapped inside - "once" brackets. */ + "once" brackets. Set up a "previous group" length so that a + subsequent quantifier will work. */ *code = OP_ONCE; PUT(code, 1, 2 + 2*LINK_SIZE); @@ -3177,12 +4070,18 @@ for (;; ptr++) *code = OP_KET; PUT(code, 1, 2 + 2*LINK_SIZE); code += 1 + LINK_SIZE; + + length_prevgroup = 3 + 3*LINK_SIZE; } + + /* Can't determine a first byte now */ + + if (firstbyte == REQ_UNSET) firstbyte = REQ_NONE; continue; - /* Character after (? not specially recognized */ - default: /* Option setting */ + /* ------------------------------------------------------------ */ + default: /* Other characters: check option setting */ set = unset = 0; optset = &set; @@ -3192,13 +4091,21 @@ for (;; ptr++) { case '-': optset = &unset; break; + case 'J': /* Record that it changed in the external options */ + *optset |= PCRE_DUPNAMES; + cd->external_options |= PCRE_JCHANGED; + break; + case 'i': *optset |= PCRE_CASELESS; break; - case 'J': *optset |= PCRE_DUPNAMES; break; case 'm': *optset |= PCRE_MULTILINE; break; case 's': *optset |= PCRE_DOTALL; break; case 'x': *optset |= PCRE_EXTENDED; break; case 'U': *optset |= PCRE_UNGREEDY; break; case 'X': *optset |= PCRE_EXTRA; break; + + default: *errorcodeptr = ERR12; + ptr--; /* Correct the offset */ + goto FAILED; } } @@ -3207,32 +4114,54 @@ for (;; ptr++) newoptions = (options | set) & (~unset); /* If the options ended with ')' this is not the start of a nested - group with option changes, so the options change at this level. Compile - code to change the ims options if this setting actually changes any of - them. We also pass the new setting back so that it can be put at the - start of any following branches, and when this group ends (if we are in - a group), a resetting item can be compiled. - - Note that if this item is right at the start of the pattern, the - options will have been abstracted and made global, so there will be no - change to compile. */ + group with option changes, so the options change at this level. If this + item is right at the start of the pattern, the options can be + abstracted and made external in the pre-compile phase, and ignored in + the compile phase. This can be helpful when matching -- for instance in + caseless checking of required bytes. + + If the code pointer is not (cd->start_code + 1 + LINK_SIZE), we are + definitely *not* at the start of the pattern because something has been + compiled. In the pre-compile phase, however, the code pointer can have + that value after the start, because it gets reset as code is discarded + during the pre-compile. However, this can happen only at top level - if + we are within parentheses, the starting BRA will still be present. At + any parenthesis level, the length value can be used to test if anything + has been compiled at that level. Thus, a test for both these conditions + is necessary to ensure we correctly detect the start of the pattern in + both phases. + + If we are not at the pattern start, compile code to change the ims + options if this setting actually changes any of them. We also pass the + new setting back so that it can be put at the start of any following + branches, and when this group ends (if we are in a group), a resetting + item can be compiled. */ if (*ptr == ')') { - if ((options & PCRE_IMS) != (newoptions & PCRE_IMS)) + if (code == cd->start_code + 1 + LINK_SIZE && + (lengthptr == NULL || *lengthptr == 2 + 2*LINK_SIZE)) { - *code++ = OP_OPT; - *code++ = newoptions & PCRE_IMS; + cd->external_options = newoptions; + options = newoptions; } + else + { + if ((options & PCRE_IMS) != (newoptions & PCRE_IMS)) + { + *code++ = OP_OPT; + *code++ = newoptions & PCRE_IMS; + } - /* Change options at this level, and pass them back for use - in subsequent branches. Reset the greedy defaults and the case - value for firstbyte and reqbyte. */ + /* Change options at this level, and pass them back for use + in subsequent branches. Reset the greedy defaults and the case + value for firstbyte and reqbyte. */ - *optionsptr = options = newoptions; - greedy_default = ((newoptions & PCRE_UNGREEDY) != 0); - greedy_non_default = greedy_default ^ 1; - req_caseopt = ((options & PCRE_CASELESS) != 0)? REQ_CASELESS : 0; + *optionsptr = options = newoptions; + greedy_default = ((newoptions & PCRE_UNGREEDY) != 0); + greedy_non_default = greedy_default ^ 1; + req_caseopt = ((options & PCRE_CASELESS) != 0)? REQ_CASELESS : 0; + } previous = NULL; /* This item can't be repeated */ continue; /* It is complete */ @@ -3245,58 +4174,56 @@ for (;; ptr++) bravalue = OP_BRA; ptr++; - } - } + } /* End of switch for character following (? */ + } /* End of (? handling */ - /* If PCRE_NO_AUTO_CAPTURE is set, all unadorned brackets become - non-capturing and behave like (?:...) brackets */ + /* Opening parenthesis not followed by '?'. If PCRE_NO_AUTO_CAPTURE is set, + all unadorned brackets become non-capturing and behave like (?:...) + brackets. */ else if ((options & PCRE_NO_AUTO_CAPTURE) != 0) { bravalue = OP_BRA; } - /* Else we have a referencing group; adjust the opcode. If the bracket - number is greater than EXTRACT_BASIC_MAX, we set the opcode one higher, and - arrange for the true number to follow later, in an OP_BRANUMBER item. */ + /* Else we have a capturing group. */ else { NUMBERED_GROUP: - if (++(*brackets) > EXTRACT_BASIC_MAX) - { - bravalue = OP_BRA + EXTRACT_BASIC_MAX + 1; - code[1+LINK_SIZE] = OP_BRANUMBER; - PUT2(code, 2+LINK_SIZE, *brackets); - skipbytes = 3; - } - else bravalue = OP_BRA + *brackets; + cd->bracount += 1; + PUT2(code, 1+LINK_SIZE, cd->bracount); + skipbytes = 2; } - /* Process nested bracketed re. Assertions may not be repeated, but other - kinds can be. We copy code into a non-register variable in order to be able - to pass its address because some compilers complain otherwise. Pass in a - new setting for the ims options if they have changed. */ + /* Process nested bracketed regex. Assertions may not be repeated, but + other kinds can be. All their opcodes are >= OP_ONCE. We copy code into a + non-register variable in order to be able to pass its address because some + compilers complain otherwise. Pass in a new setting for the ims options if + they have changed. */ previous = (bravalue >= OP_ONCE)? code : NULL; *code = bravalue; tempcode = code; tempreqvary = cd->req_varyopt; /* Save value before bracket */ + length_prevgroup = 0; /* Initialize for pre-compile phase */ if (!compile_regex( newoptions, /* The complete new option state */ options & PCRE_IMS, /* The previous ims option state */ - brackets, /* Extracting bracket count */ &tempcode, /* Where to put code (updated) */ &ptr, /* Input pointer (updated) */ errorcodeptr, /* Where to put an error message */ (bravalue == OP_ASSERTBACK || bravalue == OP_ASSERTBACK_NOT), /* TRUE if back assert */ - skipbytes, /* Skip over OP_COND/OP_BRANUMBER */ + skipbytes, /* Skip over bracket number */ &subfirstbyte, /* For possible first char */ &subreqbyte, /* For possible last char */ bcptr, /* Current branch chain */ - cd)) /* Tables block */ + cd, /* Tables block */ + (lengthptr == NULL)? NULL : /* Actual compile phase */ + &length_prevgroup /* Pre-compile phase */ + )) goto FAILED; /* At the end of compiling, code is still pointing to the start of the @@ -3305,9 +4232,9 @@ for (;; ptr++) is on the bracket. */ /* If this is a conditional bracket, check that there are no more than - two branches in the group. */ + two branches in the group, or just one if it's a DEFINE group. */ - else if (bravalue == OP_COND) + if (bravalue == OP_COND) { uschar *tc = code; int condcount = 0; @@ -3318,29 +4245,77 @@ for (;; ptr++) } while (*tc != OP_KET); - if (condcount > 2) + /* A DEFINE group is never obeyed inline (the "condition" is always + false). It must have only one branch. */ + + if (code[LINK_SIZE+1] == OP_DEF) { - *errorcodeptr = ERR27; - goto FAILED; + if (condcount > 1) + { + *errorcodeptr = ERR54; + goto FAILED; + } + bravalue = OP_DEF; /* Just a flag to suppress char handling below */ + } + + /* A "normal" conditional group. If there is just one branch, we must not + make use of its firstbyte or reqbyte, because this is equivalent to an + empty second branch. */ + + else + { + if (condcount > 2) + { + *errorcodeptr = ERR27; + goto FAILED; + } + if (condcount == 1) subfirstbyte = subreqbyte = REQ_NONE; } + } - /* If there is just one branch, we must not make use of its firstbyte or - reqbyte, because this is equivalent to an empty second branch. */ + /* Error if hit end of pattern */ + + if (*ptr != ')') + { + *errorcodeptr = ERR14; + goto FAILED; + } + + /* In the pre-compile phase, update the length by the length of the nested + group, less the brackets at either end. Then reduce the compiled code to + just the brackets so that it doesn't use much memory if it is duplicated by + a quantifier. */ - if (condcount == 1) subfirstbyte = subreqbyte = REQ_NONE; + if (lengthptr != NULL) + { + *lengthptr += length_prevgroup - 2 - 2*LINK_SIZE; + code++; + PUTINC(code, 0, 1 + LINK_SIZE); + *code++ = OP_KET; + PUTINC(code, 0, 1 + LINK_SIZE); } - /* Handle updating of the required and first characters. Update for normal - brackets of all kinds, and conditions with two branches (see code above). - If the bracket is followed by a quantifier with zero repeat, we have to - back off. Hence the definition of zeroreqbyte and zerofirstbyte outside the - main loop so that they can be accessed for the back off. */ + /* Otherwise update the main code pointer to the end of the group. */ + + else code = tempcode; + + /* For a DEFINE group, required and first character settings are not + relevant. */ + + if (bravalue == OP_DEF) break; + + /* Handle updating of the required and first characters for other types of + group. Update for normal brackets of all kinds, and conditions with two + branches (see code above). If the bracket is followed by a quantifier with + zero repeat, we have to back off. Hence the definition of zeroreqbyte and + zerofirstbyte outside the main loop so that they can be accessed for the + back off. */ zeroreqbyte = reqbyte; zerofirstbyte = firstbyte; groupsetfirstbyte = FALSE; - if (bravalue >= OP_BRA || bravalue == OP_ONCE || bravalue == OP_COND) + if (bravalue >= OP_ONCE) { /* If we have not yet set a firstbyte in this branch, take it from the subpattern, remembering that it was set here so that a repeat of more @@ -3381,35 +4356,22 @@ for (;; ptr++) firstbyte, looking for an asserted first char. */ else if (bravalue == OP_ASSERT && subreqbyte >= 0) reqbyte = subreqbyte; + break; /* End of processing '(' */ - /* Now update the main code pointer to the end of the group. */ - - code = tempcode; - - /* Error if hit end of pattern */ - - if (*ptr != ')') - { - *errorcodeptr = ERR14; - goto FAILED; - } - break; - /* Check \ for being a real metacharacter; if not, fall through and handle - it as a data character at the start of a string. Escape items are checked - for validity in the pre-compiling pass. */ - - case '\\': - tempptr = ptr; - c = check_escape(&ptr, errorcodeptr, *brackets, options, FALSE); - - /* Handle metacharacters introduced by \. For ones like \d, the ESC_ values + /* ===================================================================*/ + /* Handle metasequences introduced by \. For ones like \d, the ESC_ values are arranged to be the negation of the corresponding OP_values. For the back references, the values are ESC_REF plus the reference number. Only back references and those types that consume a character may be repeated. We can test for values between ESC_b and ESC_Z for the latter; this may have to change if any new ones are ever created. */ + case '\\': + tempptr = ptr; + c = check_escape(&ptr, errorcodeptr, cd->bracount, options, FALSE); + if (*errorcodeptr != 0) goto FAILED; + if (c < 0) { if (-c == ESC_Q) /* Handle start of quoted string */ @@ -3419,6 +4381,8 @@ for (;; ptr++) continue; } + if (-c == ESC_E) continue; /* Perl ignores an orphan \E */ + /* For metasequences that actually match a character, we disable the setting of a first character if it hasn't already been set. */ @@ -3430,18 +4394,33 @@ for (;; ptr++) zerofirstbyte = firstbyte; zeroreqbyte = reqbyte; - /* Back references are handled specially */ + /* \k or \k'name' is a back reference by name (Perl syntax) */ + + if (-c == ESC_k && (ptr[1] == '<' || ptr[1] == '\'')) + { + is_recurse = FALSE; + terminator = (*(++ptr) == '<')? '>' : '\''; + goto NAMED_REF_OR_RECURSE; + } + + /* Back references are handled specially; must disable firstbyte if + not set to cope with cases like (?=(\w+))\1: which would otherwise set + ':' later. */ if (-c >= ESC_REF) { - int number = -c - ESC_REF; + recno = -c - ESC_REF; + + HANDLE_REFERENCE: /* Come here from named backref handling */ + if (firstbyte == REQ_UNSET) firstbyte = REQ_NONE; previous = code; *code++ = OP_REF; - PUT2INC(code, 0, number); + PUT2INC(code, 0, recno); + cd->backref_map |= (recno < 32)? (1 << recno) : 1; + if (recno > cd->top_backref) cd->top_backref = recno; } - /* So are Unicode property matches, if supported. We know that get_ucp - won't fail because it was tested in the pre-pass. */ + /* So are Unicode property matches, if supported. */ #ifdef SUPPORT_UCP else if (-c == ESC_P || -c == ESC_p) @@ -3449,15 +4428,26 @@ for (;; ptr++) BOOL negated; int pdata; int ptype = get_ucp(&ptr, &negated, &pdata, errorcodeptr); + if (ptype < 0) goto FAILED; previous = code; *code++ = ((-c == ESC_p) != negated)? OP_PROP : OP_NOTPROP; *code++ = ptype; *code++ = pdata; } +#else + + /* If Unicode properties are not supported, \X, \P, and \p are not + allowed. */ + + else if (-c == ESC_X || -c == ESC_P || -c == ESC_p) + { + *errorcodeptr = ERR45; + goto FAILED; + } #endif - /* For the rest, we can obtain the OP value by negating the escape - value */ + /* For the rest (including \X when Unicode properties are supported), we + can obtain the OP value by negating the escape value. */ else { @@ -3481,9 +4471,10 @@ for (;; ptr++) mcbuffer[0] = c; mclength = 1; } - goto ONE_CHAR; + + /* ===================================================================*/ /* Handle a literal character. It is guaranteed not to be whitespace or # when the extended flag is set. If we are in UTF-8 mode, it may be a multi-byte literal character. */ @@ -3494,7 +4485,7 @@ for (;; ptr++) mcbuffer[0] = c; #ifdef SUPPORT_UTF8 - if (utf8 && (c & 0xc0) == 0xc0) + if (utf8 && c >= 0xc0) { while ((ptr[1] & 0xc0) == 0x80) mcbuffer[mclength++] = *(++ptr); @@ -3545,6 +4536,7 @@ for (;; ptr++) } } /* end of big loop */ + /* Control never reaches here by falling through, only by a goto for all the error states. Pass back the position in the pattern so that it can be displayed to the user for diagnosing the error. */ @@ -3561,35 +4553,40 @@ return FALSE; * Compile sequence of alternatives * *************************************************/ -/* On entry, ptr is pointing past the bracket character, but on return -it points to the closing bracket, or vertical bar, or end of string. -The code variable is pointing at the byte into which the BRA operator has been -stored. If the ims options are changed at the start (for a (?ims: group) or -during any branch, we need to insert an OP_OPT item at the start of every -following branch to ensure they get set correctly at run time, and also pass -the new options into every subsequent branch compile. +/* On entry, ptr is pointing past the bracket character, but on return it +points to the closing bracket, or vertical bar, or end of string. The code +variable is pointing at the byte into which the BRA operator has been stored. +If the ims options are changed at the start (for a (?ims: group) or during any +branch, we need to insert an OP_OPT item at the start of every following branch +to ensure they get set correctly at run time, and also pass the new options +into every subsequent branch compile. + +This function is used during the pre-compile phase when we are trying to find +out the amount of memory needed, as well as during the real compile phase. The +value of lengthptr distinguishes the two phases. Argument: options option bits, including any changes for this subpattern oldims previous settings of ims option bits - brackets -> int containing the number of extracting brackets used codeptr -> the address of the current code pointer ptrptr -> the address of the current pattern pointer errorcodeptr -> pointer to error code variable lookbehind TRUE if this is a lookbehind assertion - skipbytes skip this many bytes at start (for OP_COND, OP_BRANUMBER) + skipbytes skip this many bytes at start (for brackets and OP_COND) firstbyteptr place to put the first required character, or a negative number reqbyteptr place to put the last required character, or a negative number bcptr pointer to the chain of currently open branches cd points to the data block with tables pointers etc. + lengthptr NULL during the real compile phase + points to length accumulator during pre-compile phase -Returns: TRUE on success +Returns: TRUE on success */ static BOOL -compile_regex(int options, int oldims, int *brackets, uschar **codeptr, - const uschar **ptrptr, int *errorcodeptr, BOOL lookbehind, int skipbytes, - int *firstbyteptr, int *reqbyteptr, branch_chain *bcptr, compile_data *cd) +compile_regex(int options, int oldims, uschar **codeptr, const uschar **ptrptr, + int *errorcodeptr, BOOL lookbehind, int skipbytes, int *firstbyteptr, + int *reqbyteptr, branch_chain *bcptr, compile_data *cd, int *lengthptr) { const uschar *ptr = *ptrptr; uschar *code = *codeptr; @@ -3598,6 +4595,7 @@ uschar *start_bracket = code; uschar *reverse_count = NULL; int firstbyte, reqbyte; int branchfirstbyte, branchreqbyte; +int length; branch_chain bc; bc.outer = bcptr; @@ -3605,6 +4603,20 @@ bc.current = code; firstbyte = reqbyte = REQ_UNSET; +/* Accumulate the length for use in the pre-compile phase. Start with the +length of the BRA and KET and any extra bytes that are required at the +beginning. We accumulate in a local variable to save frequent testing of +lenthptr for NULL. We cannot do this by looking at the value of code at the +start and end of each alternative, because compiled items are discarded during +the pre-compile phase so that the work space is not exceeded. */ + +length = 2 + 2*LINK_SIZE + skipbytes; + +/* WARNING: If the above line is changed for any reason, you must also change +the code that abstracts option settings at the start of the pattern and makes +them global. It tests the value of length for (2 + 2*LINK_SIZE) in the +pre-compile phase to find out whether anything has yet been compiled or not. */ + /* Offset is set zero to mark that this bracket is still open */ PUT(code, 1, 0); @@ -3620,6 +4632,7 @@ for (;;) { *code++ = OP_OPT; *code++ = options & PCRE_IMS; + length += 2; } /* Set up dummy OP_REVERSE if lookbehind assertion */ @@ -3629,73 +4642,80 @@ for (;;) *code++ = OP_REVERSE; reverse_count = code; PUTINC(code, 0, 0); + length += 1 + LINK_SIZE; } - /* Now compile the branch */ + /* Now compile the branch; in the pre-compile phase its length gets added + into the length. */ - if (!compile_branch(&options, brackets, &code, &ptr, errorcodeptr, - &branchfirstbyte, &branchreqbyte, &bc, cd)) + if (!compile_branch(&options, &code, &ptr, errorcodeptr, &branchfirstbyte, + &branchreqbyte, &bc, cd, (lengthptr == NULL)? NULL : &length)) { *ptrptr = ptr; return FALSE; } - /* If this is the first branch, the firstbyte and reqbyte values for the - branch become the values for the regex. */ + /* In the real compile phase, there is some post-processing to be done. */ - if (*last_branch != OP_ALT) + if (lengthptr == NULL) { - firstbyte = branchfirstbyte; - reqbyte = branchreqbyte; - } + /* If this is the first branch, the firstbyte and reqbyte values for the + branch become the values for the regex. */ - /* If this is not the first branch, the first char and reqbyte have to - match the values from all the previous branches, except that if the previous - value for reqbyte didn't have REQ_VARY set, it can still match, and we set - REQ_VARY for the regex. */ + if (*last_branch != OP_ALT) + { + firstbyte = branchfirstbyte; + reqbyte = branchreqbyte; + } - else - { - /* If we previously had a firstbyte, but it doesn't match the new branch, - we have to abandon the firstbyte for the regex, but if there was previously - no reqbyte, it takes on the value of the old firstbyte. */ + /* If this is not the first branch, the first char and reqbyte have to + match the values from all the previous branches, except that if the + previous value for reqbyte didn't have REQ_VARY set, it can still match, + and we set REQ_VARY for the regex. */ - if (firstbyte >= 0 && firstbyte != branchfirstbyte) + else { - if (reqbyte < 0) reqbyte = firstbyte; - firstbyte = REQ_NONE; - } + /* If we previously had a firstbyte, but it doesn't match the new branch, + we have to abandon the firstbyte for the regex, but if there was + previously no reqbyte, it takes on the value of the old firstbyte. */ + + if (firstbyte >= 0 && firstbyte != branchfirstbyte) + { + if (reqbyte < 0) reqbyte = firstbyte; + firstbyte = REQ_NONE; + } - /* If we (now or from before) have no firstbyte, a firstbyte from the - branch becomes a reqbyte if there isn't a branch reqbyte. */ + /* If we (now or from before) have no firstbyte, a firstbyte from the + branch becomes a reqbyte if there isn't a branch reqbyte. */ - if (firstbyte < 0 && branchfirstbyte >= 0 && branchreqbyte < 0) - branchreqbyte = branchfirstbyte; + if (firstbyte < 0 && branchfirstbyte >= 0 && branchreqbyte < 0) + branchreqbyte = branchfirstbyte; - /* Now ensure that the reqbytes match */ + /* Now ensure that the reqbytes match */ - if ((reqbyte & ~REQ_VARY) != (branchreqbyte & ~REQ_VARY)) - reqbyte = REQ_NONE; - else reqbyte |= branchreqbyte; /* To "or" REQ_VARY */ - } + if ((reqbyte & ~REQ_VARY) != (branchreqbyte & ~REQ_VARY)) + reqbyte = REQ_NONE; + else reqbyte |= branchreqbyte; /* To "or" REQ_VARY */ + } - /* If lookbehind, check that this branch matches a fixed-length string, - and put the length into the OP_REVERSE item. Temporarily mark the end of - the branch with OP_END. */ + /* If lookbehind, check that this branch matches a fixed-length string, and + put the length into the OP_REVERSE item. Temporarily mark the end of the + branch with OP_END. */ - if (lookbehind) - { - int length; - *code = OP_END; - length = find_fixedlength(last_branch, options); - DPRINTF(("fixed length = %d\n", length)); - if (length < 0) + if (lookbehind) { - *errorcodeptr = (length == -2)? ERR36 : ERR25; - *ptrptr = ptr; - return FALSE; + int fixed_length; + *code = OP_END; + fixed_length = find_fixedlength(last_branch, options); + DPRINTF(("fixed length = %d\n", fixed_length)); + if (fixed_length < 0) + { + *errorcodeptr = (fixed_length == -2)? ERR36 : ERR25; + *ptrptr = ptr; + return FALSE; + } + PUT(reverse_count, 0, fixed_length); } - PUT(reverse_count, 0, length); } /* Reached end of expression, either ')' or end of pattern. Go back through @@ -3709,15 +4729,15 @@ for (;;) if (*ptr != '|') { - int length = code - last_branch; + int branch_length = code - last_branch; do { int prev_length = GET(last_branch, 1); - PUT(last_branch, 1, length); - length = prev_length; - last_branch -= length; + PUT(last_branch, 1, branch_length); + branch_length = prev_length; + last_branch -= branch_length; } - while (length > 0); + while (branch_length > 0); /* Fill in the ket */ @@ -3731,6 +4751,7 @@ for (;;) { *code++ = OP_OPT; *code++ = oldims; + length += 2; } /* Set values to pass back */ @@ -3739,6 +4760,7 @@ for (;;) *ptrptr = ptr; *firstbyteptr = firstbyte; *reqbyteptr = reqbyte; + if (lengthptr != NULL) *lengthptr += length; return TRUE; } @@ -3752,6 +4774,7 @@ for (;;) bc.current = last_branch = code; code += 1 + LINK_SIZE; ptr++; + length += 1 + LINK_SIZE; } /* Control never reaches here */ } @@ -3802,24 +4825,29 @@ is_anchored(register const uschar *code, int *options, unsigned int bracket_map, unsigned int backref_map) { do { - const uschar *scode = - first_significant_code(code + 1+LINK_SIZE, options, PCRE_MULTILINE, FALSE); + const uschar *scode = first_significant_code(code + _pcre_OP_lengths[*code], + options, PCRE_MULTILINE, FALSE); register int op = *scode; + /* Non-capturing brackets */ + + if (op == OP_BRA) + { + if (!is_anchored(scode, options, bracket_map, backref_map)) return FALSE; + } + /* Capturing brackets */ - if (op > OP_BRA) + else if (op == OP_CBRA) { - int new_map; - op -= OP_BRA; - if (op > EXTRACT_BASIC_MAX) op = GET2(scode, 2+LINK_SIZE); - new_map = bracket_map | ((op < 32)? (1 << op) : 1); + int n = GET2(scode, 1+LINK_SIZE); + int new_map = bracket_map | ((n < 32)? (1 << n) : 1); if (!is_anchored(scode, options, new_map, backref_map)) return FALSE; } /* Other brackets */ - else if (op == OP_BRA || op == OP_ASSERT || op == OP_ONCE || op == OP_COND) + else if (op == OP_ASSERT || op == OP_ONCE || op == OP_COND) { if (!is_anchored(scode, options, bracket_map, backref_map)) return FALSE; } @@ -3827,7 +4855,8 @@ do { /* .* is not anchored unless DOTALL is set and it isn't in brackets that are or may be referenced. */ - else if ((op == OP_TYPESTAR || op == OP_TYPEMINSTAR) && + else if ((op == OP_TYPESTAR || op == OP_TYPEMINSTAR || + op == OP_TYPEPOSSTAR) && (*options & PCRE_DOTALL) != 0) { if (scode[1] != OP_ANY || (bracket_map & backref_map) != 0) return FALSE; @@ -3872,30 +4901,35 @@ is_startline(const uschar *code, unsigned int bracket_map, unsigned int backref_map) { do { - const uschar *scode = first_significant_code(code + 1+LINK_SIZE, NULL, 0, - FALSE); + const uschar *scode = first_significant_code(code + _pcre_OP_lengths[*code], + NULL, 0, FALSE); register int op = *scode; + /* Non-capturing brackets */ + + if (op == OP_BRA) + { + if (!is_startline(scode, bracket_map, backref_map)) return FALSE; + } + /* Capturing brackets */ - if (op > OP_BRA) + else if (op == OP_CBRA) { - int new_map; - op -= OP_BRA; - if (op > EXTRACT_BASIC_MAX) op = GET2(scode, 2+LINK_SIZE); - new_map = bracket_map | ((op < 32)? (1 << op) : 1); + int n = GET2(scode, 1+LINK_SIZE); + int new_map = bracket_map | ((n < 32)? (1 << n) : 1); if (!is_startline(scode, new_map, backref_map)) return FALSE; } /* Other brackets */ - else if (op == OP_BRA || op == OP_ASSERT || op == OP_ONCE || op == OP_COND) + else if (op == OP_ASSERT || op == OP_ONCE || op == OP_COND) { if (!is_startline(scode, bracket_map, backref_map)) return FALSE; } /* .* means "start at start or after \n" if it isn't in brackets that may be referenced. */ - else if (op == OP_TYPESTAR || op == OP_TYPEMINSTAR) + else if (op == OP_TYPESTAR || op == OP_TYPEMINSTAR || op == OP_TYPEPOSSTAR) { if (scode[1] != OP_ANY || (bracket_map & backref_map) != 0) return FALSE; } @@ -3944,14 +4978,13 @@ do { first_significant_code(code + 1+LINK_SIZE, options, PCRE_CASELESS, TRUE); register int op = *scode; - if (op >= OP_BRA) op = OP_BRA; - switch(op) { default: return -1; case OP_BRA: + case OP_CBRA: case OP_ASSERT: case OP_ONCE: case OP_COND: @@ -3967,6 +5000,7 @@ do { case OP_CHARNC: case OP_PLUS: case OP_MINPLUS: + case OP_POSPLUS: if (!inassert) return -1; if (c < 0) { @@ -4015,37 +5049,36 @@ return pcre_compile2(pattern, options, NULL, errorptr, erroroffset, tables); } - PCRE_DATA_SCOPE pcre * pcre_compile2(const char *pattern, int options, int *errorcodeptr, const char **errorptr, int *erroroffset, const unsigned char *tables) { real_pcre *re; -int length = 1 + LINK_SIZE; /* For initial BRA plus length */ -int c, firstbyte, reqbyte, newline; -int bracount = 0; -int branch_extra = 0; -int branch_newextra; -int item_count = -1; -int name_count = 0; -int max_name_size = 0; -int lastitemlength = 0; +int length = 1; /* For final END opcode */ +int firstbyte, reqbyte, newline; int errorcode = 0; #ifdef SUPPORT_UTF8 BOOL utf8; -BOOL class_utf8; #endif -BOOL inescq = FALSE; -BOOL capturing; -unsigned int brastackptr = 0; size_t size; uschar *code; const uschar *codestart; const uschar *ptr; compile_data compile_block; compile_data *cd = &compile_block; -int brastack[BRASTACK_SIZE]; -uschar bralenstack[BRASTACK_SIZE]; + +/* This space is used for "compiling" into during the first phase, when we are +computing the amount of memory that is needed. Compiled items are thrown away +as soon as possible, so that a fairly large buffer should be sufficient for +this purpose. The same space is used in the second phase for remembering where +to fill in forward references to subpatterns. */ + +uschar cworkspace[COMPILE_WORK_SIZE]; + + +/* Set this early so that early errors get offset 0. */ + +ptr = (const uschar *)pattern; /* We can't pass back an error message if errorptr is NULL; I guess the best we can do is just return NULL, but we can set a code value if there is a code @@ -4078,7 +5111,7 @@ if (utf8 && (options & PCRE_NO_UTF8_CHECK) == 0 && (*erroroffset = _pcre_valid_utf8((uschar *)pattern, -1)) >= 0) { errorcode = ERR44; - goto PCRE_EARLY_ERROR_RETURN; + goto PCRE_UTF8_ERROR_RETURN; } #else if ((options & PCRE_UTF8) != 0) @@ -4102,34 +5135,43 @@ cd->fcc = tables + fcc_offset; cd->cbits = tables + cbits_offset; cd->ctypes = tables + ctypes_offset; -/* Handle different types of newline. The two bits give four cases. The current -code allows for one- or two-byte sequences. */ +/* Handle different types of newline. The three bits give seven cases. The +current code allows for fixed one- or two-byte sequences, plus "any". */ -switch (options & PCRE_NEWLINE_CRLF) +switch (options & (PCRE_NEWLINE_CRLF | PCRE_NEWLINE_ANY)) { - default: newline = NEWLINE; break; /* Compile-time default */ + case 0: newline = NEWLINE; break; /* Compile-time default */ case PCRE_NEWLINE_CR: newline = '\r'; break; case PCRE_NEWLINE_LF: newline = '\n'; break; case PCRE_NEWLINE_CR+ PCRE_NEWLINE_LF: newline = ('\r' << 8) | '\n'; break; + case PCRE_NEWLINE_ANY: newline = -1; break; + default: errorcode = ERR56; goto PCRE_EARLY_ERROR_RETURN; } -if (newline > 255) +if (newline < 0) { - cd->nllen = 2; - cd->nl[0] = (newline >> 8) & 255; - cd->nl[1] = newline & 255; + cd->nltype = NLTYPE_ANY; } else { - cd->nllen = 1; - cd->nl[0] = newline; + cd->nltype = NLTYPE_FIXED; + if (newline > 255) + { + cd->nllen = 2; + cd->nl[0] = (newline >> 8) & 255; + cd->nl[1] = newline & 255; + } + else + { + cd->nllen = 1; + cd->nl[0] = newline; + } } -/* Maximum back reference and backref bitmap. This is updated for numeric -references during the first pass, but for named references during the actual -compile pass. The bitmap records up to 31 back references to help in deciding -whether (.*) can be treated as anchored or not. */ +/* Maximum back reference and backref bitmap. The bitmap records up to 31 back +references to help in deciding whether (.*) can be treated as anchored or not. +*/ cd->top_backref = 0; cd->backref_map = 0; @@ -4139,1041 +5181,151 @@ cd->backref_map = 0; DPRINTF(("------------------------------------------------------------------\n")); DPRINTF(("%s\n", pattern)); -/* The first thing to do is to make a pass over the pattern to compute the -amount of store required to hold the compiled code. This does not have to be -perfect as long as errors are overestimates. At the same time we can detect any -flag settings right at the start, and extract them. Make an attempt to correct -for any counted white space if an "extended" flag setting appears late in the -pattern. We can't be so clever for #-comments. */ +/* Pretend to compile the pattern while actually just accumulating the length +of memory required. This behaviour is triggered by passing a non-NULL final +argument to compile_regex(). We pass a block of workspace (cworkspace) for it +to compile parts of the pattern into; the compiled code is discarded when it is +no longer needed, so hopefully this workspace will never overflow, though there +is a test for its doing so. */ -ptr = (const uschar *)(pattern - 1); -while ((c = *(++ptr)) != 0) - { - int min, max; - int class_optcount; - int bracket_length; - int duplength; +cd->bracount = 0; +cd->names_found = 0; +cd->name_entry_size = 0; +cd->name_table = NULL; +cd->start_workspace = cworkspace; +cd->start_code = cworkspace; +cd->hwm = cworkspace; +cd->start_pattern = (const uschar *)pattern; +cd->end_pattern = (const uschar *)(pattern + strlen(pattern)); +cd->req_varyopt = 0; +cd->nopartial = FALSE; +cd->external_options = options; - /* If we are inside a \Q...\E sequence, all chars are literal */ +/* Now do the pre-compile. On error, errorcode will be set non-zero, so we +don't need to look at the result of the function here. The initial options have +been put into the cd block so that they can be changed if an option setting is +found within the regex right at the beginning. Bringing initial option settings +outside can help speed up starting point checks. */ - if (inescq) - { - if ((options & PCRE_AUTO_CALLOUT) != 0) length += 2 + 2*LINK_SIZE; - goto NORMAL_CHAR; - } +code = cworkspace; +*code = OP_BRA; +(void)compile_regex(cd->external_options, cd->external_options & PCRE_IMS, + &code, &ptr, &errorcode, FALSE, 0, &firstbyte, &reqbyte, NULL, cd, &length); +if (errorcode != 0) goto PCRE_EARLY_ERROR_RETURN; - /* Otherwise, first check for ignored whitespace and comments */ +DPRINTF(("end pre-compile: length=%d workspace=%d\n", length, + cd->hwm - cworkspace)); - if ((options & PCRE_EXTENDED) != 0) - { - if ((cd->ctypes[c] & ctype_space) != 0) continue; - if (c == '#') - { - while (*(++ptr) != 0) if (IS_NEWLINE(ptr)) break; - if (*ptr != 0) - { - ptr += cd->nllen - 1; - continue; - } - break; /* End loop at end of pattern */ - } - } +if (length > MAX_PATTERN_SIZE) + { + errorcode = ERR20; + goto PCRE_EARLY_ERROR_RETURN; + } - item_count++; /* Is zero for the first non-comment item */ +/* Compute the size of data block needed and get it, either from malloc or +externally provided function. Integer overflow should no longer be possible +because nowadays we limit the maximum value of cd->names_found and +cd->name_entry_size. */ - /* Allow space for auto callout before every item except quantifiers. */ +size = length + sizeof(real_pcre) + cd->names_found * (cd->name_entry_size + 3); +re = (real_pcre *)(pcre_malloc)(size); - if ((options & PCRE_AUTO_CALLOUT) != 0 && - c != '*' && c != '+' && c != '?' && - (c != '{' || !is_counted_repeat(ptr + 1))) - length += 2 + 2*LINK_SIZE; +if (re == NULL) + { + errorcode = ERR21; + goto PCRE_EARLY_ERROR_RETURN; + } - switch(c) - { - /* A backslashed item may be an escaped data character or it may be a - character type. */ +/* Put in the magic number, and save the sizes, initial options, and character +table pointer. NULL is used for the default character tables. The nullpad field +is at the end; it's there to help in the case when a regex compiled on a system +with 4-byte pointers is run on another with 8-byte pointers. */ - case '\\': - c = check_escape(&ptr, &errorcode, bracount, options, FALSE); - if (errorcode != 0) goto PCRE_ERROR_RETURN; +re->magic_number = MAGIC_NUMBER; +re->size = size; +re->options = cd->external_options; +re->dummy1 = 0; +re->first_byte = 0; +re->req_byte = 0; +re->name_table_offset = sizeof(real_pcre); +re->name_entry_size = cd->name_entry_size; +re->name_count = cd->names_found; +re->ref_count = 0; +re->tables = (tables == _pcre_default_tables)? NULL : tables; +re->nullpad = NULL; - lastitemlength = 1; /* Default length of last item for repeats */ +/* The starting points of the name/number translation table and of the code are +passed around in the compile data block. The start/end pattern and initial +options are already set from the pre-compile phase, as is the name_entry_size +field. Reset the bracket count and the names_found field. Also reset the hwm +field; this time it's used for remembering forward references to subpatterns. +*/ - if (c >= 0) /* Data character */ - { - length += 2; /* For a one-byte character */ +cd->bracount = 0; +cd->names_found = 0; +cd->name_table = (uschar *)re + re->name_table_offset; +codestart = cd->name_table + re->name_entry_size * re->name_count; +cd->start_code = codestart; +cd->hwm = cworkspace; +cd->req_varyopt = 0; +cd->nopartial = FALSE; -#ifdef SUPPORT_UTF8 - if (utf8 && c > 127) - { - int i; - for (i = 0; i < _pcre_utf8_table1_size; i++) - if (c <= _pcre_utf8_table1[i]) break; - length += i; - lastitemlength += i; - } -#endif +/* Set up a starting, non-extracting bracket, then compile the expression. On +error, errorcode will be set non-zero, so we don't need to look at the result +of the function here. */ - continue; - } +ptr = (const uschar *)pattern; +code = (uschar *)codestart; +*code = OP_BRA; +(void)compile_regex(re->options, re->options & PCRE_IMS, &code, &ptr, + &errorcode, FALSE, 0, &firstbyte, &reqbyte, NULL, cd, NULL); +re->top_bracket = cd->bracount; +re->top_backref = cd->top_backref; - /* If \Q, enter "literal" mode */ +if (cd->nopartial) re->options |= PCRE_NOPARTIAL; - if (-c == ESC_Q) - { - inescq = TRUE; - continue; - } +/* If not reached end of pattern on success, there's an excess bracket. */ - /* \X is supported only if Unicode property support is compiled */ +if (errorcode == 0 && *ptr != 0) errorcode = ERR22; -#ifndef SUPPORT_UCP - if (-c == ESC_X) - { - errorcode = ERR45; - goto PCRE_ERROR_RETURN; - } -#endif +/* Fill in the terminating state and check for disastrous overflow, but +if debugging, leave the test till after things are printed out. */ - /* \P and \p are for Unicode properties, but only when the support has - been compiled. Each item needs 3 bytes. */ +*code++ = OP_END; - else if (-c == ESC_P || -c == ESC_p) - { -#ifdef SUPPORT_UCP - BOOL negated; - BOOL pdata; - length += 3; - lastitemlength = 3; - if (get_ucp(&ptr, &negated, &pdata, &errorcode) < 0) - goto PCRE_ERROR_RETURN; - continue; -#else - errorcode = ERR45; - goto PCRE_ERROR_RETURN; -#endif - } - - /* Other escapes need one byte */ - - length++; - - /* A back reference needs an additional 2 bytes, plus either one or 5 - bytes for a repeat. We also need to keep the value of the highest - back reference. */ - - if (c <= -ESC_REF) - { - int refnum = -c - ESC_REF; - cd->backref_map |= (refnum < 32)? (1 << refnum) : 1; - if (refnum > cd->top_backref) - cd->top_backref = refnum; - length += 2; /* For single back reference */ - if (ptr[1] == '{' && is_counted_repeat(ptr+2)) - { - ptr = read_repeat_counts(ptr+2, &min, &max, &errorcode); - if (errorcode != 0) goto PCRE_ERROR_RETURN; - if ((min == 0 && (max == 1 || max == -1)) || - (min == 1 && max == -1)) - length++; - else length += 5; - if (ptr[1] == '?') ptr++; - } - } - continue; - - case '^': /* Single-byte metacharacters */ - case '.': - case '$': - length++; - lastitemlength = 1; - continue; - - case '*': /* These repeats won't be after brackets; */ - case '+': /* those are handled separately */ - case '?': - length++; - goto POSESSIVE; /* A few lines below */ - - /* This covers the cases of braced repeats after a single char, metachar, - class, or back reference. */ - - case '{': - if (!is_counted_repeat(ptr+1)) goto NORMAL_CHAR; - ptr = read_repeat_counts(ptr+1, &min, &max, &errorcode); - if (errorcode != 0) goto PCRE_ERROR_RETURN; - - /* These special cases just insert one extra opcode */ - - if ((min == 0 && (max == 1 || max == -1)) || - (min == 1 && max == -1)) - length++; - - /* These cases might insert additional copies of a preceding character. */ - - else - { - if (min != 1) - { - length -= lastitemlength; /* Uncount the original char or metachar */ - if (min > 0) length += 3 + lastitemlength; - } - length += lastitemlength + ((max > 0)? 3 : 1); - } - - if (ptr[1] == '?') ptr++; /* Needs no extra length */ - - POSESSIVE: /* Test for possessive quantifier */ - if (ptr[1] == '+') - { - ptr++; - length += 2 + 2*LINK_SIZE; /* Allow for atomic brackets */ - } - continue; - - /* An alternation contains an offset to the next branch or ket. If any ims - options changed in the previous branch(es), and/or if we are in a - lookbehind assertion, extra space will be needed at the start of the - branch. This is handled by branch_extra. */ - - case '|': - length += 1 + LINK_SIZE + branch_extra; - continue; - - /* A character class uses 33 characters provided that all the character - values are less than 256. Otherwise, it uses a bit map for low valued - characters, and individual items for others. Don't worry about character - types that aren't allowed in classes - they'll get picked up during the - compile. A character class that contains only one single-byte character - uses 2 or 3 bytes, depending on whether it is negated or not. Notice this - where we can. (In UTF-8 mode we can do this only for chars < 128.) */ - - case '[': - if (*(++ptr) == '^') - { - class_optcount = 10; /* Greater than one */ - ptr++; - } - else class_optcount = 0; - -#ifdef SUPPORT_UTF8 - class_utf8 = FALSE; -#endif - - /* Written as a "do" so that an initial ']' is taken as data */ - - if (*ptr != 0) do - { - /* Inside \Q...\E everything is literal except \E */ - - if (inescq) - { - if (*ptr != '\\' || ptr[1] != 'E') goto GET_ONE_CHARACTER; - inescq = FALSE; - ptr += 1; - continue; - } - - /* Outside \Q...\E, check for escapes */ - - if (*ptr == '\\') - { - c = check_escape(&ptr, &errorcode, bracount, options, TRUE); - if (errorcode != 0) goto PCRE_ERROR_RETURN; - - /* \b is backspace inside a class; \X is literal */ - - if (-c == ESC_b) c = '\b'; - else if (-c == ESC_X) c = 'X'; - - /* \Q enters quoting mode */ - - else if (-c == ESC_Q) - { - inescq = TRUE; - continue; - } - - /* Handle escapes that turn into characters */ - - if (c >= 0) goto NON_SPECIAL_CHARACTER; - - /* Escapes that are meta-things. The normal ones just affect the - bit map, but Unicode properties require an XCLASS extended item. */ - - else - { - class_optcount = 10; /* \d, \s etc; make sure > 1 */ -#ifdef SUPPORT_UTF8 - if (-c == ESC_p || -c == ESC_P) - { - if (!class_utf8) - { - class_utf8 = TRUE; - length += LINK_SIZE + 2; - } - length += 3; - } -#endif - } - } - - /* Check the syntax for POSIX stuff. The bits we actually handle are - checked during the real compile phase. */ - - else if (*ptr == '[' && - (ptr[1] == ':' || ptr[1] == '.' || ptr[1] == '=') && - check_posix_syntax(ptr, &ptr, cd)) - { - ptr++; - class_optcount = 10; /* Make sure > 1 */ - } - - /* Anything else increments the possible optimization count. We have to - detect ranges here so that we can compute the number of extra ranges for - caseless wide characters when UCP support is available. If there are wide - characters, we are going to have to use an XCLASS, even for single - characters. */ - - else - { - int d; - - GET_ONE_CHARACTER: - -#ifdef SUPPORT_UTF8 - if (utf8) - { - int extra = 0; - GETCHARLEN(c, ptr, extra); - ptr += extra; - } - else c = *ptr; -#else - c = *ptr; -#endif - - /* Come here from handling \ above when it escapes to a char value */ - - NON_SPECIAL_CHARACTER: - class_optcount++; - - d = -1; - if (ptr[1] == '-') - { - uschar const *hyptr = ptr++; - if (ptr[1] == '\\') - { - ptr++; - d = check_escape(&ptr, &errorcode, bracount, options, TRUE); - if (errorcode != 0) goto PCRE_ERROR_RETURN; - if (-d == ESC_b) d = '\b'; /* backspace */ - else if (-d == ESC_X) d = 'X'; /* literal X in a class */ - } - else if (ptr[1] != 0 && ptr[1] != ']') - { - ptr++; -#ifdef SUPPORT_UTF8 - if (utf8) - { - int extra = 0; - GETCHARLEN(d, ptr, extra); - ptr += extra; - } - else -#endif - d = *ptr; - } - if (d < 0) ptr = hyptr; /* go back to hyphen as data */ - } - - /* If d >= 0 we have a range. In UTF-8 mode, if the end is > 255, or > - 127 for caseless matching, we will need to use an XCLASS. */ - - if (d >= 0) - { - class_optcount = 10; /* Ensure > 1 */ - if (d < c) - { - errorcode = ERR8; - goto PCRE_ERROR_RETURN; - } - -#ifdef SUPPORT_UTF8 - if (utf8 && (d > 255 || ((options & PCRE_CASELESS) != 0 && d > 127))) - { - uschar buffer[6]; - if (!class_utf8) /* Allow for XCLASS overhead */ - { - class_utf8 = TRUE; - length += LINK_SIZE + 2; - } - -#ifdef SUPPORT_UCP - /* If we have UCP support, find out how many extra ranges are - needed to map the other case of characters within this range. We - have to mimic the range optimization here, because extending the - range upwards might push d over a boundary that makes is use - another byte in the UTF-8 representation. */ - - if ((options & PCRE_CASELESS) != 0) - { - int occ, ocd; - int cc = c; - int origd = d; - while (get_othercase_range(&cc, origd, &occ, &ocd)) - { - if (occ >= c && ocd <= d) continue; /* Skip embedded */ - - if (occ < c && ocd >= c - 1) /* Extend the basic range */ - { /* if there is overlap, */ - c = occ; /* noting that if occ < c */ - continue; /* we can't have ocd > d */ - } /* because a subrange is */ - if (ocd > d && occ <= d + 1) /* always shorter than */ - { /* the basic range. */ - d = ocd; - continue; - } - - /* An extra item is needed */ - - length += 1 + _pcre_ord2utf8(occ, buffer) + - ((occ == ocd)? 0 : _pcre_ord2utf8(ocd, buffer)); - } - } -#endif /* SUPPORT_UCP */ - - /* The length of the (possibly extended) range */ - - length += 1 + _pcre_ord2utf8(c, buffer) + _pcre_ord2utf8(d, buffer); - } -#endif /* SUPPORT_UTF8 */ - - } - - /* We have a single character. There is nothing to be done unless we - are in UTF-8 mode. If the char is > 255, or 127 when caseless, we must - allow for an XCL_SINGLE item, doubled for caselessness if there is UCP - support. */ - - else - { -#ifdef SUPPORT_UTF8 - if (utf8 && (c > 255 || ((options & PCRE_CASELESS) != 0 && c > 127))) - { - uschar buffer[6]; - class_optcount = 10; /* Ensure > 1 */ - if (!class_utf8) /* Allow for XCLASS overhead */ - { - class_utf8 = TRUE; - length += LINK_SIZE + 2; - } -#ifdef SUPPORT_UCP - length += (((options & PCRE_CASELESS) != 0)? 2 : 1) * - (1 + _pcre_ord2utf8(c, buffer)); -#else /* SUPPORT_UCP */ - length += 1 + _pcre_ord2utf8(c, buffer); -#endif /* SUPPORT_UCP */ - } -#endif /* SUPPORT_UTF8 */ - } - } - } - while (*(++ptr) != 0 && (inescq || *ptr != ']')); /* Concludes "do" above */ - - if (*ptr == 0) /* Missing terminating ']' */ - { - errorcode = ERR6; - goto PCRE_ERROR_RETURN; - } - - /* We can optimize when there was only one optimizable character. Repeats - for positive and negated single one-byte chars are handled by the general - code. Here, we handle repeats for the class opcodes. */ - - if (class_optcount == 1) length += 3; else - { - length += 33; - - /* A repeat needs either 1 or 5 bytes. If it is a possessive quantifier, - we also need extra for wrapping the whole thing in a sub-pattern. */ - - if (*ptr != 0 && ptr[1] == '{' && is_counted_repeat(ptr+2)) - { - ptr = read_repeat_counts(ptr+2, &min, &max, &errorcode); - if (errorcode != 0) goto PCRE_ERROR_RETURN; - if ((min == 0 && (max == 1 || max == -1)) || - (min == 1 && max == -1)) - length++; - else length += 5; - if (ptr[1] == '+') - { - ptr++; - length += 2 + 2*LINK_SIZE; - } - else if (ptr[1] == '?') ptr++; - } - } - continue; - - /* Brackets may be genuine groups or special things */ - - case '(': - branch_newextra = 0; - bracket_length = 1 + LINK_SIZE; - capturing = FALSE; - - /* Handle special forms of bracket, which all start (? */ - - if (ptr[1] == '?') - { - int set, unset; - int *optset; - - switch (c = ptr[2]) - { - /* Skip over comments entirely */ - case '#': - ptr += 3; - while (*ptr != 0 && *ptr != ')') ptr++; - if (*ptr == 0) - { - errorcode = ERR18; - goto PCRE_ERROR_RETURN; - } - continue; - - /* Non-referencing groups and lookaheads just move the pointer on, and - then behave like a non-special bracket, except that they don't increment - the count of extracting brackets. Ditto for the "once only" bracket, - which is in Perl from version 5.005. */ - - case ':': - case '=': - case '!': - case '>': - ptr += 2; - break; - - /* Named subpatterns are an extension copied from Python */ - - case 'P': - ptr += 3; - - /* Handle the definition of a named subpattern */ - - if (*ptr == '<') - { - const uschar *p; /* Don't amalgamate; some compilers */ - p = ++ptr; /* grumble at autoincrement in declaration */ - while ((cd->ctypes[*ptr] & ctype_word) != 0) ptr++; - if (*ptr != '>') - { - errorcode = ERR42; - goto PCRE_ERROR_RETURN; - } - name_count++; - if (name_count > MAX_NAME_COUNT) - { - errorcode = ERR49; - goto PCRE_ERROR_RETURN; - } - if (ptr - p > max_name_size) - { - max_name_size = (ptr - p); - if (max_name_size > MAX_NAME_SIZE) - { - errorcode = ERR48; - goto PCRE_ERROR_RETURN; - } - } - capturing = TRUE; /* Named parentheses are always capturing */ - break; /* Go handle capturing parentheses */ - } - - /* Handle back references and recursive calls to named subpatterns */ - - if (*ptr == '=' || *ptr == '>') - { - length += 3 + 3*LINK_SIZE; /* Allow for the automatic "once" */ - while ((cd->ctypes[*(++ptr)] & ctype_word) != 0); - if (*ptr != ')') - { - errorcode = ERR42; - goto PCRE_ERROR_RETURN; - } - goto RECURSE_CHECK_QUANTIFIED; - } - - /* Unknown character after (?P */ - - errorcode = ERR41; - goto PCRE_ERROR_RETURN; - - /* (?R) specifies a recursive call to the regex, which is an extension - to provide the facility which can be obtained by (?p{perl-code}) in - Perl 5.6. In Perl 5.8 this has become (??{perl-code}). - - From PCRE 4.00, items such as (?3) specify subroutine-like "calls" to - the appropriate numbered brackets. This includes both recursive and - non-recursive calls. (?R) is now synonymous with (?0). */ - - case 'R': - ptr++; - - case '0': case '1': case '2': case '3': case '4': - case '5': case '6': case '7': case '8': case '9': - ptr += 2; - if (c != 'R') - while ((digitab[*(++ptr)] & ctype_digit) != 0); - if (*ptr != ')') - { - errorcode = ERR29; - goto PCRE_ERROR_RETURN; - } - length += 3 + 3*LINK_SIZE; /* Allows for the automatic "once" */ - - /* If this item is quantified, it will get wrapped inside brackets so - as to use the code for quantified brackets. We jump down and use the - code that handles this for real brackets. Come here from code for - named recursions/subroutines. */ - - RECURSE_CHECK_QUANTIFIED: - if (ptr[1] == '+' || ptr[1] == '*' || ptr[1] == '?' || ptr[1] == '{') - { - length += 2 + 2 * LINK_SIZE; /* to make bracketed */ - duplength = 5 + 3 * LINK_SIZE; - goto HANDLE_QUANTIFIED_BRACKETS; - } - continue; - - /* (?C) is an extension which provides "callout" - to provide a bit of - the functionality of the Perl (?{...}) feature. An optional number may - follow (default is zero). */ - - case 'C': - ptr += 2; - while ((digitab[*(++ptr)] & ctype_digit) != 0); - if (*ptr != ')') - { - errorcode = ERR39; - goto PCRE_ERROR_RETURN; - } - length += 2 + 2*LINK_SIZE; - continue; - - /* Lookbehinds are in Perl from version 5.005 */ - - case '<': - ptr += 3; - if (*ptr == '=' || *ptr == '!') - { - branch_newextra = 1 + LINK_SIZE; - length += 1 + LINK_SIZE; /* For the first branch */ - break; - } - errorcode = ERR24; - goto PCRE_ERROR_RETURN; - - /* Conditionals are in Perl from version 5.005. The bracket must either - be followed by a number (for bracket reference) or by an assertion - group. PCRE extends this by allowing a name to reference a named group; - unfortunately, previously 'R' was implemented for a recursion test. - When this is compiled, we look for the named group 'R' first. At this - point we just do a basic syntax check. */ - - case '(': - if ((cd->ctypes[ptr[3]] & ctype_word) != 0) - { - ptr += 4; - length += 3; - while ((cd->ctypes[*ptr] & ctype_word) != 0) ptr++; - if (*ptr != ')') - { - errorcode = ERR26; - goto PCRE_ERROR_RETURN; - } - } - else /* An assertion must follow */ - { - ptr++; /* Can treat like ':' as far as spacing is concerned */ - if (ptr[2] != '?' || - (ptr[3] != '=' && ptr[3] != '!' && ptr[3] != '<') ) - { - ptr += 2; /* To get right offset in message */ - errorcode = ERR28; - goto PCRE_ERROR_RETURN; - } - } - break; - - /* Else loop checking valid options until ) is met. Anything else is an - error. If we are without any brackets, i.e. at top level, the settings - act as if specified in the options, so massage the options immediately. - This is for backward compatibility with Perl 5.004. */ - - default: - set = unset = 0; - optset = &set; - ptr += 2; - - for (;; ptr++) - { - c = *ptr; - switch (c) - { - case 'i': - *optset |= PCRE_CASELESS; - continue; - - case 'J': - *optset |= PCRE_DUPNAMES; - options |= PCRE_JCHANGED; /* Record that it changed */ - continue; - - case 'm': - *optset |= PCRE_MULTILINE; - continue; - - case 's': - *optset |= PCRE_DOTALL; - continue; - - case 'x': - *optset |= PCRE_EXTENDED; - continue; - - case 'X': - *optset |= PCRE_EXTRA; - continue; - - case 'U': - *optset |= PCRE_UNGREEDY; - continue; - - case '-': - optset = &unset; - continue; - - /* A termination by ')' indicates an options-setting-only item; if - this is at the very start of the pattern (indicated by item_count - being zero), we use it to set the global options. This is helpful - when analyzing the pattern for first characters, etc. Otherwise - nothing is done here and it is handled during the compiling - process. - - We allow for more than one options setting at the start. If such - settings do not change the existing options, nothing is compiled. - However, we must leave space just in case something is compiled. - This can happen for pathological sequences such as (?i)(?-i) - because the global options will end up with -i set. The space is - small and not significant. (Before I did this there was a reported - bug with (?i)(?-i) in a machine-generated pattern.) - - [Historical note: Up to Perl 5.8, options settings at top level - were always global settings, wherever they appeared in the pattern. - That is, they were equivalent to an external setting. From 5.8 - onwards, they apply only to what follows (which is what you might - expect).] */ - - case ')': - if (item_count == 0) - { - options = (options | set) & (~unset); - set = unset = 0; /* To save length */ - item_count--; /* To allow for several */ - length += 2; - } - - /* Fall through */ - - /* A termination by ':' indicates the start of a nested group with - the given options set. This is again handled at compile time, but - we must allow for compiled space if any of the ims options are - set. We also have to allow for resetting space at the end of - the group, which is why 4 is added to the length and not just 2. - If there are several changes of options within the same group, this - will lead to an over-estimate on the length, but this shouldn't - matter very much. We also have to allow for resetting options at - the start of any alternations, which we do by setting - branch_newextra to 2. */ - - case ':': - if (((set|unset) & PCRE_IMS) != 0) - { - length += 4; - branch_newextra = 2; - } - goto END_OPTIONS; - - /* Unrecognized option character */ - - default: - errorcode = ERR12; - goto PCRE_ERROR_RETURN; - } - } - - /* If we hit a closing bracket, that's it - this is a freestanding - option-setting. We need to ensure that branch_extra is updated if - necessary. The only values branch_newextra can have here are 0 or 2. - If the value is 2, then branch_extra must either be 2 or 5, depending - on whether this is a lookbehind group or not. */ - - END_OPTIONS: - if (c == ')') - { - if (branch_newextra == 2 && - (branch_extra == 0 || branch_extra == 1+LINK_SIZE)) - branch_extra += branch_newextra; - continue; - } - - /* If options were terminated by ':' control comes here. This is a - non-capturing group with an options change. There is nothing more that - needs to be done because "capturing" is already set FALSE by default; - we can just fall through. */ - - } - } - - /* Ordinary parentheses, not followed by '?', are capturing unless - PCRE_NO_AUTO_CAPTURE is set. */ - - else capturing = (options & PCRE_NO_AUTO_CAPTURE) == 0; - - /* Capturing brackets must be counted so we can process escapes in a - Perlish way. If the number exceeds EXTRACT_BASIC_MAX we are going to need - an additional 3 bytes of memory per capturing bracket. */ - - if (capturing) - { - bracount++; - if (bracount > EXTRACT_BASIC_MAX) bracket_length += 3; - } - - /* Save length for computing whole length at end if there's a repeat that - requires duplication of the group. Also save the current value of - branch_extra, and start the new group with the new value. If non-zero, this - will either be 2 for a (?imsx: group, or 3 for a lookbehind assertion. */ - - if (brastackptr >= sizeof(brastack)/sizeof(int)) - { - errorcode = ERR19; - goto PCRE_ERROR_RETURN; - } - - bralenstack[brastackptr] = branch_extra; - branch_extra = branch_newextra; - - brastack[brastackptr++] = length; - length += bracket_length; - continue; - - /* Handle ket. Look for subsequent max/min; for certain sets of values we - have to replicate this bracket up to that many times. If brastackptr is - 0 this is an unmatched bracket which will generate an error, but take care - not to try to access brastack[-1] when computing the length and restoring - the branch_extra value. */ - - case ')': - length += 1 + LINK_SIZE; - if (brastackptr > 0) - { - duplength = length - brastack[--brastackptr]; - branch_extra = bralenstack[brastackptr]; - /* This is a paranoid check to stop integer overflow later on */ - if (duplength > MAX_DUPLENGTH) - { - errorcode = ERR50; - goto PCRE_ERROR_RETURN; - } - } - else duplength = 0; - - /* The following code is also used when a recursion such as (?3) is - followed by a quantifier, because in that case, it has to be wrapped inside - brackets so that the quantifier works. The value of duplength must be - set before arrival. */ - - HANDLE_QUANTIFIED_BRACKETS: - - /* Leave ptr at the final char; for read_repeat_counts this happens - automatically; for the others we need an increment. */ - - if ((c = ptr[1]) == '{' && is_counted_repeat(ptr+2)) - { - ptr = read_repeat_counts(ptr+2, &min, &max, &errorcode); - if (errorcode != 0) goto PCRE_ERROR_RETURN; - } - else if (c == '*') { min = 0; max = -1; ptr++; } - else if (c == '+') { min = 1; max = -1; ptr++; } - else if (c == '?') { min = 0; max = 1; ptr++; } - else { min = 1; max = 1; } - - /* If the minimum is zero, we have to allow for an OP_BRAZERO before the - group, and if the maximum is greater than zero, we have to replicate - maxval-1 times; each replication acquires an OP_BRAZERO plus a nesting - bracket set. */ - - if (min == 0) - { - length++; - if (max > 0) length += (max - 1) * (duplength + 3 + 2*LINK_SIZE); - } - - /* When the minimum is greater than zero, we have to replicate up to - minval-1 times, with no additions required in the copies. Then, if there - is a limited maximum we have to replicate up to maxval-1 times allowing - for a BRAZERO item before each optional copy and nesting brackets for all - but one of the optional copies. */ - - else - { - length += (min - 1) * duplength; - if (max > min) /* Need this test as max=-1 means no limit */ - length += (max - min) * (duplength + 3 + 2*LINK_SIZE) - - (2 + 2*LINK_SIZE); - } - - /* Allow space for once brackets for "possessive quantifier" */ - - if (ptr[1] == '+') - { - ptr++; - length += 2 + 2*LINK_SIZE; - } - continue; - - /* Non-special character. It won't be space or # in extended mode, so it is - always a genuine character. If we are in a \Q...\E sequence, check for the - end; if not, we have a literal. */ - - default: - NORMAL_CHAR: - - if (inescq && c == '\\' && ptr[1] == 'E') - { - inescq = FALSE; - ptr++; - continue; - } - - length += 2; /* For a one-byte character */ - lastitemlength = 1; /* Default length of last item for repeats */ - - /* In UTF-8 mode, check for additional bytes. */ - -#ifdef SUPPORT_UTF8 - if (utf8 && (c & 0xc0) == 0xc0) - { - while ((ptr[1] & 0xc0) == 0x80) /* Can't flow over the end */ - { /* because the end is marked */ - lastitemlength++; /* by a zero byte. */ - length++; - ptr++; - } - } +#ifndef DEBUG +if (code - codestart > length) errorcode = ERR23; #endif - continue; - } - } - -length += 2 + LINK_SIZE; /* For final KET and END */ - -if ((options & PCRE_AUTO_CALLOUT) != 0) - length += 2 + 2*LINK_SIZE; /* For final callout */ +/* Fill in any forward references that are required. */ -if (length > MAX_PATTERN_SIZE) +while (errorcode == 0 && cd->hwm > cworkspace) { - errorcode = ERR20; - goto PCRE_EARLY_ERROR_RETURN; + int offset, recno; + const uschar *groupptr; + cd->hwm -= LINK_SIZE; + offset = GET(cd->hwm, 0); + recno = GET(codestart, offset); + groupptr = find_bracket(codestart, (re->options & PCRE_UTF8) != 0, recno); + if (groupptr == NULL) errorcode = ERR53; + else PUT(((uschar *)codestart), offset, groupptr - codestart); } -/* Compute the size of data block needed and get it, either from malloc or -externally provided function. Integer overflow should no longer be possible -because nowadays we limit the maximum value of name_count and max_name size. */ - -size = length + sizeof(real_pcre) + name_count * (max_name_size + 3); -re = (real_pcre *)(pcre_malloc)(size); - -if (re == NULL) - { - errorcode = ERR21; - goto PCRE_EARLY_ERROR_RETURN; - } - -/* Put in the magic number, and save the sizes, options, and character table -pointer. NULL is used for the default character tables. The nullpad field is at -the end; it's there to help in the case when a regex compiled on a system with -4-byte pointers is run on another with 8-byte pointers. */ - -re->magic_number = MAGIC_NUMBER; -re->size = size; -re->options = options; -re->dummy1 = 0; -re->name_table_offset = sizeof(real_pcre); -re->name_entry_size = max_name_size + 3; -re->name_count = name_count; -re->ref_count = 0; -re->tables = (tables == _pcre_default_tables)? NULL : tables; -re->nullpad = NULL; - -/* The starting points of the name/number translation table and of the code are -passed around in the compile data block. */ - -cd->names_found = 0; -cd->name_entry_size = max_name_size + 3; -cd->name_table = (uschar *)re + re->name_table_offset; -codestart = cd->name_table + re->name_entry_size * re->name_count; -cd->start_code = codestart; -cd->start_pattern = (const uschar *)pattern; -cd->req_varyopt = 0; -cd->nopartial = FALSE; - -/* Set up a starting, non-extracting bracket, then compile the expression. On -error, errorcode will be set non-zero, so we don't need to look at the result -of the function here. */ - -ptr = (const uschar *)pattern; -code = (uschar *)codestart; -*code = OP_BRA; -bracount = 0; -(void)compile_regex(options, options & PCRE_IMS, &bracount, &code, &ptr, - &errorcode, FALSE, 0, &firstbyte, &reqbyte, NULL, cd); -re->top_bracket = bracount; -re->top_backref = cd->top_backref; - -if (cd->nopartial) re->options |= PCRE_NOPARTIAL; - -/* If not reached end of pattern on success, there's an excess bracket. */ - -if (errorcode == 0 && *ptr != 0) errorcode = ERR22; - -/* Fill in the terminating state and check for disastrous overflow, but -if debugging, leave the test till after things are printed out. */ - -*code++ = OP_END; - -#ifndef DEBUG -if (code - codestart > length) errorcode = ERR23; -#endif - /* Give an error if there's back reference to a non-existent capturing subpattern. */ -if (re->top_backref > re->top_bracket) errorcode = ERR15; +if (errorcode == 0 && re->top_backref > re->top_bracket) errorcode = ERR15; /* Failed to compile, or error while post-processing */ if (errorcode != 0) { (pcre_free)(re); - PCRE_ERROR_RETURN: - *erroroffset = ptr - (const uschar *)pattern; PCRE_EARLY_ERROR_RETURN: + *erroroffset = ptr - (const uschar *)pattern; +#ifdef SUPPORT_UTF8 + PCRE_UTF8_ERROR_RETURN: +#endif *errorptr = error_texts[errorcode]; if (errorcodeptr != NULL) *errorcodeptr = errorcode; return NULL; @@ -5183,15 +5335,15 @@ if (errorcode != 0) the pattern is anchored by virtue of ^ characters or \A or anything else (such as starting with .* when DOTALL is set). -Otherwise, if we know what the first character has to be, save it, because that +Otherwise, if we know what the first byte has to be, save it, because that speeds up unanchored matches no end. If not, see if we can set the PCRE_STARTLINE flag. This is helpful for multiline matches when all branches start with ^. and also when all branches start with .* for non-DOTALL matches. */ -if ((options & PCRE_ANCHORED) == 0) +if ((re->options & PCRE_ANCHORED) == 0) { - int temp_options = options; + int temp_options = re->options; /* May get changed during these scans */ if (is_anchored(codestart, &temp_options, 0, cd->backref_map)) re->options |= PCRE_ANCHORED; else @@ -5276,7 +5428,7 @@ if (code - codestart > length) if (errorcodeptr != NULL) *errorcodeptr = ERR23; return NULL; } -#endif +#endif /* DEBUG */ return (pcre *)re; } diff --git a/src/src/pcre/pcre_config.c b/src/src/pcre/pcre_config.c index e20450eea..582cf4a81 100644 --- a/src/src/pcre/pcre_config.c +++ b/src/src/pcre/pcre_config.c @@ -1,4 +1,4 @@ -/* $Cambridge: exim/src/src/pcre/pcre_config.c,v 1.3 2006/11/07 16:50:36 ph10 Exp $ */ +/* $Cambridge: exim/src/src/pcre/pcre_config.c,v 1.4 2007/01/23 15:08:45 ph10 Exp $ */ /************************************************* * Perl-Compatible Regular Expressions * diff --git a/src/src/pcre/pcre_exec.c b/src/src/pcre/pcre_exec.c index 4484eea20..27cd52fec 100644 --- a/src/src/pcre/pcre_exec.c +++ b/src/src/pcre/pcre_exec.c @@ -1,4 +1,4 @@ -/* $Cambridge: exim/src/src/pcre/pcre_exec.c,v 1.3 2006/11/07 16:50:36 ph10 Exp $ */ +/* $Cambridge: exim/src/src/pcre/pcre_exec.c,v 1.4 2007/01/23 15:08:45 ph10 Exp $ */ /************************************************* * Perl-Compatible Regular Expressions * @@ -44,25 +44,22 @@ POSSIBILITY OF SUCH DAMAGE. pattern matching using an NFA algorithm, trying to mimic Perl as closely as possible. There are also some static supporting functions. */ -#define NLBLOCK md /* The block containing newline information */ -#include "pcre_internal.h" +#define NLBLOCK md /* Block containing newline information */ +#define PSSTART start_subject /* Field containing processed string start */ +#define PSEND end_subject /* Field containing processed string end */ +#include "pcre_internal.h" -/* Structure for building a chain of data that actually lives on the -stack, for holding the values of the subject pointer at the start of each -subpattern, so as to detect when an empty string has been matched by a -subpattern - to break infinite loops. When NO_RECURSE is set, these blocks -are on the heap, not on the stack. */ +/* The chain of eptrblocks for tail recursions uses memory in stack workspace, +obtained at top level, the size of which is defined by EPTR_WORK_SIZE. */ -typedef struct eptrblock { - struct eptrblock *epb_prev; - USPTR epb_saved_eptr; -} eptrblock; +#define EPTR_WORK_SIZE (1000) /* Flag bits for the match() function */ -#define match_condassert 0x01 /* Called to check a condition assertion */ -#define match_isgroup 0x02 /* Set if start of bracketed group */ +#define match_condassert 0x01 /* Called to check a condition assertion */ +#define match_cbegroup 0x02 /* Could-be-empty unlimited repeat group */ +#define match_tail_recursed 0x04 /* Tail recursive call */ /* Non-error returns from the match() function. Error returns are externally defined PCRE_ERROR_xxx codes, which are all negative. */ @@ -103,7 +100,7 @@ Returns: nothing static void pchars(const uschar *p, int length, BOOL is_subject, match_data *md) { -int c; +unsigned int c; if (is_subject && length > md->end_subject - p) length = md->end_subject - p; while (length-- > 0) if (isprint(c = *(p++))) printf("%c", c); else printf("\\x%02x", c); @@ -293,7 +290,6 @@ typedef struct heapframe { BOOL Xcur_is_word; BOOL Xcondition; - BOOL Xminimize; BOOL Xprev_is_word; unsigned long int Xoriginal_ims; @@ -305,11 +301,10 @@ typedef struct heapframe { int Xprop_category; int Xprop_chartype; int Xprop_script; - int *Xprop_test_variable; #endif int Xctype; - int Xfc; + unsigned int Xfc; int Xfi; int Xlength; int Xmax; @@ -342,10 +337,7 @@ typedef struct heapframe { * Match from current position * *************************************************/ -/* On entry ecode points to the first opcode, and eptr to the first character -in the subject string, while eptrb holds the value of eptr at the start of the -last bracketed group - used for breaking infinite loops matching zero-length -strings. This function is called recursively in many circumstances. Whenever it +/* This function is called recursively in many circumstances. Whenever it returns a negative (error) response, the outer incarnation must also return the same response. @@ -355,8 +347,8 @@ performance. Tests using gcc on a SPARC disproved this; in the first case, it made performance worse. Arguments: - eptr pointer in subject - ecode position in code + eptr pointer to current character in subject + ecode pointer to current position in compiled code offset_top current top pointer md pointer to "static" info for the match ims current /i, /m, and /s options @@ -364,7 +356,9 @@ Arguments: brackets - for testing for empty matches flags can contain match_condassert - this is an assertion condition - match_isgroup - this is the start of a bracketed group + match_cbegroup - this is the start of an unlimited repeat + group that can match an empty string + match_tail_recursed - this is a tail_recursed group rdepth the recursion depth Returns: MATCH_MATCH if matched ) these values are >= 0 @@ -379,14 +373,16 @@ match(REGISTER USPTR eptr, REGISTER const uschar *ecode, int flags, unsigned int rdepth) { /* These variables do not need to be preserved over recursion in this function, -so they can be ordinary variables in all cases. Mark them with "register" -because they are used a lot in loops. */ +so they can be ordinary variables in all cases. Mark some of them with +"register" because they are used a lot in loops. */ register int rrc; /* Returns from recursive calls */ register int i; /* Used for loops not involving calls to RMATCH() */ -register unsigned int c; /* Character values not kept over RMATCH() calls */ +register unsigned int c; /* Character values not kept over RMATCH() calls */ register BOOL utf8; /* Local copy of UTF-8 flag for speed */ +BOOL minimize, possessive; /* Quantifier options */ + /* When recursion is not being used, all "local" variables that have to be preserved over calls to RMATCH() are part of a "frame" which is obtained from heap storage. Set up the top-level frame here; others are obtained from the @@ -436,7 +432,6 @@ HEAP_RECURSE: #define cur_is_word frame->Xcur_is_word #define condition frame->Xcondition -#define minimize frame->Xminimize #define prev_is_word frame->Xprev_is_word #define original_ims frame->Xoriginal_ims @@ -448,7 +443,6 @@ HEAP_RECURSE: #define prop_category frame->Xprop_category #define prop_chartype frame->Xprop_chartype #define prop_script frame->Xprop_script -#define prop_test_variable frame->Xprop_test_variable #endif #define ctype frame->Xctype @@ -472,7 +466,7 @@ HEAP_RECURSE: get preserved during recursion in the normal way. In this environment, fi and i, and fc and c, can be the same variables. */ -#else +#else /* NO_RECURSE not defined */ #define fi i #define fc c @@ -491,7 +485,6 @@ recursion_info new_recursive; /* within blocks below are for variables */ /* that do not have to be preserved over */ BOOL cur_is_word; /* a recursive call to RMATCH(). */ BOOL condition; -BOOL minimize; BOOL prev_is_word; unsigned long int original_ims; @@ -503,7 +496,6 @@ int prop_fail_result; int prop_category; int prop_chartype; int prop_script; -int *prop_test_variable; #endif int ctype; @@ -518,7 +510,7 @@ int save_offset1, save_offset2, save_offset3; int stacksave[REC_STACK_SAVE_MAX]; eptrblock newptrb; -#endif +#endif /* NO_RECURSE */ /* These statements are here to stop the compiler complaining about unitialized variables. */ @@ -526,9 +518,9 @@ variables. */ #ifdef SUPPORT_UCP prop_value = 0; prop_fail_result = 0; -prop_test_variable = NULL; #endif + /* This label is used for tail recursion, which is used in a few cases even when NO_RECURSE is not defined, in order to reduce the amount of stack that is used. Thanks to Ian Taylor for noticing this possibility and sending the @@ -558,24 +550,34 @@ utf8 = md->utf8; /* Local copy of the flag */ utf8 = FALSE; #endif -/* At the start of a bracketed group, add the current subject pointer to the -stack of such pointers, to be re-instated at the end of the group when we hit -the closing ket. When match() is called in other circumstances, we don't add to -this stack. */ +/* At the start of a group with an unlimited repeat that may match an empty +string, the match_cbegroup flag is set. When this is the case, add the current +subject pointer to the chain of such remembered pointers, to be checked when we +hit the closing ket, in order to break infinite loops that match no characters. +When match() is called in other circumstances, don't add to the chain. If this +is a tail recursion, use a block from the workspace, as the one on the stack is +already used. */ -if ((flags & match_isgroup) != 0) +if ((flags & match_cbegroup) != 0) { - newptrb.epb_prev = eptrb; - newptrb.epb_saved_eptr = eptr; - eptrb = &newptrb; + eptrblock *p; + if ((flags & match_tail_recursed) != 0) + { + if (md->eptrn >= EPTR_WORK_SIZE) RRETURN(PCRE_ERROR_NULLWSLIMIT); + p = md->eptrchain + md->eptrn++; + } + else p = &newptrb; + p->epb_saved_eptr = eptr; + p->epb_prev = eptrb; + eptrb = p; } -/* Now start processing the operations. */ +/* Now start processing the opcodes. */ for (;;) { + minimize = possessive = FALSE; op = *ecode; - minimize = FALSE; /* For partial matching, remember if we ever hit the end of the subject after matching at least one subject character. */ @@ -585,33 +587,30 @@ for (;;) eptr > md->start_match) md->hitend = TRUE; - /* Opening capturing bracket. If there is space in the offset vector, save - the current subject position in the working slot at the top of the vector. We - mustn't change the current values of the data slot, because they may be set - from a previous iteration of this group, and be referred to by a reference - inside the group. - - If the bracket fails to match, we need to restore this value and also the - values of the final offsets, in case they were set by a previous iteration of - the same bracket. - - If there isn't enough space in the offset vector, treat this as if it were a - non-capturing bracket. Don't worry about setting the flag for the error case - here; that is handled in the code for KET. */ - - if (op > OP_BRA) + switch(op) { - number = op - OP_BRA; - - /* For extended extraction brackets (large number), we have to fish out the - number from a dummy opcode at the start. */ - - if (number > EXTRACT_BASIC_MAX) - number = GET2(ecode, 2+LINK_SIZE); + /* Handle a capturing bracket. If there is space in the offset vector, save + the current subject position in the working slot at the top of the vector. + We mustn't change the current values of the data slot, because they may be + set from a previous iteration of this group, and be referred to by a + reference inside the group. + + If the bracket fails to match, we need to restore this value and also the + values of the final offsets, in case they were set by a previous iteration + of the same bracket. + + If there isn't enough space in the offset vector, treat this as if it were + a non-capturing bracket. Don't worry about setting the flag for the error + case here; that is handled in the code for KET. */ + + case OP_CBRA: + case OP_SCBRA: + number = GET2(ecode, 1+LINK_SIZE); offset = number << 1; #ifdef DEBUG - printf("start bracket %d subject=", number); + printf("start bracket %d\n", number); + printf("subject="); pchars(eptr, 16, TRUE, md); printf("\n"); #endif @@ -626,10 +625,11 @@ for (;;) DPRINTF(("saving %d %d %d\n", save_offset1, save_offset2, save_offset3)); md->offset_vector[md->offset_end - number] = eptr - md->start_subject; + flags = (op == OP_SCBRA)? match_cbegroup : 0; do { - RMATCH(rrc, eptr, ecode + 1 + LINK_SIZE, offset_top, md, ims, eptrb, - match_isgroup); + RMATCH(rrc, eptr, ecode + _pcre_OP_lengths[*ecode], offset_top, md, + ims, eptrb, flags); if (rrc != MATCH_NOMATCH) RRETURN(rrc); md->capture_last = save_capture_last; ecode += GET(ecode, 1); @@ -645,39 +645,35 @@ for (;;) RRETURN(MATCH_NOMATCH); } - /* Insufficient room for saving captured contents */ + /* Insufficient room for saving captured contents. Treat as a non-capturing + bracket. */ - else op = OP_BRA; - } + DPRINTF(("insufficient capture room: treat as non-capturing\n")); - /* Other types of node can be handled by a switch */ - - switch(op) - { - case OP_BRA: /* Non-capturing bracket: optimized */ - DPRINTF(("start bracket 0\n")); - - /* Loop for all the alternatives */ + /* Non-capturing bracket. Loop for all the alternatives. When we get to the + final alternative within the brackets, we would return the result of a + recursive call to match() whatever happened. We can reduce stack usage by + turning this into a tail recursion. */ + case OP_BRA: + case OP_SBRA: + DPRINTF(("start non-capturing bracket\n")); + flags = (op >= OP_SBRA)? match_cbegroup : 0; for (;;) { - /* When we get to the final alternative within the brackets, we would - return the result of a recursive call to match() whatever happened. We - can reduce stack usage by turning this into a tail recursion. */ - if (ecode[GET(ecode, 1)] != OP_ALT) - { - ecode += 1 + LINK_SIZE; - flags = match_isgroup; - DPRINTF(("bracket 0 tail recursion\n")); - goto TAIL_RECURSE; - } + { + ecode += _pcre_OP_lengths[*ecode]; + flags |= match_tail_recursed; + DPRINTF(("bracket 0 tail recursion\n")); + goto TAIL_RECURSE; + } /* For non-final alternatives, continue the loop for a NOMATCH result; otherwise return. */ - RMATCH(rrc, eptr, ecode + 1 + LINK_SIZE, offset_top, md, ims, eptrb, - match_isgroup); + RMATCH(rrc, eptr, ecode + _pcre_OP_lengths[*ecode], offset_top, md, ims, + eptrb, flags); if (rrc != MATCH_NOMATCH) RRETURN(rrc); ecode += GET(ecode, 1); } @@ -690,54 +686,72 @@ for (;;) obeyed, we can use tail recursion to avoid using another stack frame. */ case OP_COND: - if (ecode[LINK_SIZE+1] == OP_CREF) /* Condition extract or recurse test */ + case OP_SCOND: + if (ecode[LINK_SIZE+1] == OP_RREF) /* Recursion test */ + { + offset = GET2(ecode, LINK_SIZE + 2); /* Recursion group number*/ + condition = md->recursive != NULL && + (offset == RREF_ANY || offset == md->recursive->group_num); + ecode += condition? 3 : GET(ecode, 1); + } + + else if (ecode[LINK_SIZE+1] == OP_CREF) /* Group used test */ { offset = GET2(ecode, LINK_SIZE+2) << 1; /* Doubled ref number */ - condition = (offset == CREF_RECURSE * 2)? - (md->recursive != NULL) : - (offset < offset_top && md->offset_vector[offset] >= 0); - ecode += condition? (LINK_SIZE + 4) : (LINK_SIZE + 1 + GET(ecode, 1)); - flags = match_isgroup; - goto TAIL_RECURSE; + condition = offset < offset_top && md->offset_vector[offset] >= 0; + ecode += condition? 3 : GET(ecode, 1); + } + + else if (ecode[LINK_SIZE+1] == OP_DEF) /* DEFINE - always false */ + { + condition = FALSE; + ecode += GET(ecode, 1); } /* The condition is an assertion. Call match() to evaluate it - setting - the final argument TRUE causes it to stop at the end of an assertion. */ + the final argument match_condassert causes it to stop at the end of an + assertion. */ else { RMATCH(rrc, eptr, ecode + 1 + LINK_SIZE, offset_top, md, ims, NULL, - match_condassert | match_isgroup); + match_condassert); if (rrc == MATCH_MATCH) { - ecode += 1 + LINK_SIZE + GET(ecode, LINK_SIZE+2); + condition = TRUE; + ecode += 1 + LINK_SIZE + GET(ecode, LINK_SIZE + 2); while (*ecode == OP_ALT) ecode += GET(ecode, 1); } else if (rrc != MATCH_NOMATCH) { RRETURN(rrc); /* Need braces because of following else */ } - else ecode += GET(ecode, 1); + else + { + condition = FALSE; + ecode += GET(ecode, 1); + } + } - /* We are now at the branch that is to be obeyed. As there is only one, - we can use tail recursion to avoid using another stack frame. */ + /* We are now at the branch that is to be obeyed. As there is only one, + we can use tail recursion to avoid using another stack frame. If the second + alternative doesn't exist, we can just plough on. */ + if (condition || *ecode == OP_ALT) + { ecode += 1 + LINK_SIZE; - flags = match_isgroup; + flags = match_tail_recursed | ((op == OP_SCOND)? match_cbegroup : 0); goto TAIL_RECURSE; } - /* Control never reaches here */ - - /* Skip over conditional reference or large extraction number data if - encountered. */ - - case OP_CREF: - case OP_BRANUMBER: - ecode += 3; + else + { + ecode += 1 + LINK_SIZE; + } break; - /* End of the pattern. If we are in a recursion, we should restore the - offsets appropriately and continue from after the call. */ + + /* End of the pattern. If we are in a top-level recursion, we should + restore the offsets appropriately and continue from after the call. */ case OP_END: if (md->recursive != NULL && md->recursive->group_num == 0) @@ -779,8 +793,7 @@ for (;;) case OP_ASSERTBACK: do { - RMATCH(rrc, eptr, ecode + 1 + LINK_SIZE, offset_top, md, ims, NULL, - match_isgroup); + RMATCH(rrc, eptr, ecode + 1 + LINK_SIZE, offset_top, md, ims, NULL, 0); if (rrc == MATCH_MATCH) break; if (rrc != MATCH_NOMATCH) RRETURN(rrc); ecode += GET(ecode, 1); @@ -806,8 +819,7 @@ for (;;) case OP_ASSERTBACK_NOT: do { - RMATCH(rrc, eptr, ecode + 1 + LINK_SIZE, offset_top, md, ims, NULL, - match_isgroup); + RMATCH(rrc, eptr, ecode + 1 + LINK_SIZE, offset_top, md, ims, NULL, 0); if (rrc == MATCH_MATCH) RRETURN(MATCH_NOMATCH); if (rrc != MATCH_NOMATCH) RRETURN(rrc); ecode += GET(ecode,1); @@ -828,8 +840,8 @@ for (;;) #ifdef SUPPORT_UTF8 if (utf8) { - c = GET(ecode,1); - for (i = 0; i < c; i++) + i = GET(ecode, 1); + while (i-- > 0) { eptr--; if (eptr < md->start_subject) RRETURN(MATCH_NOMATCH); @@ -842,7 +854,7 @@ for (;;) /* No UTF-8 support, or not in UTF-8 mode: count is byte count */ { - eptr -= GET(ecode,1); + eptr -= GET(ecode, 1); if (eptr < md->start_subject) RRETURN(MATCH_NOMATCH); } @@ -899,13 +911,8 @@ for (;;) case OP_RECURSE: { callpat = md->start_code + GET(ecode, 1); - new_recursive.group_num = *callpat - OP_BRA; - - /* For extended extraction brackets (large number), we have to fish out - the number from a dummy opcode at the start. */ - - if (new_recursive.group_num > EXTRACT_BASIC_MAX) - new_recursive.group_num = GET2(callpat, 2+LINK_SIZE); + new_recursive.group_num = (callpat == md->start_code)? 0 : + GET2(callpat, 1 + LINK_SIZE); /* Add to "recursing stack" */ @@ -938,10 +945,11 @@ for (;;) restore the offset and recursion data. */ DPRINTF(("Recursing into group %d\n", new_recursive.group_num)); + flags = (*callpat >= OP_SBRA)? match_cbegroup : 0; do { - RMATCH(rrc, eptr, callpat + 1 + LINK_SIZE, offset_top, md, ims, - eptrb, match_isgroup); + RMATCH(rrc, eptr, callpat + _pcre_OP_lengths[*callpat], offset_top, + md, ims, eptrb, flags); if (rrc == MATCH_MATCH) { DPRINTF(("Recursion matched\n")); @@ -985,7 +993,7 @@ for (;;) do { RMATCH(rrc, eptr, ecode + 1 + LINK_SIZE, offset_top, md, ims, - eptrb, match_isgroup); + eptrb, 0); if (rrc == MATCH_MATCH) break; if (rrc != MATCH_NOMATCH) RRETURN(rrc); ecode += GET(ecode,1); @@ -999,7 +1007,7 @@ for (;;) /* Continue as from after the assertion, updating the offsets high water mark, since extracts may have been taken. */ - do ecode += GET(ecode,1); while (*ecode == OP_ALT); + do ecode += GET(ecode, 1); while (*ecode == OP_ALT); offset_top = md->end_offset_top; eptr = md->end_match_ptr; @@ -1033,15 +1041,15 @@ for (;;) RMATCH(rrc, eptr, ecode + 1 + LINK_SIZE, offset_top, md, ims, eptrb, 0); if (rrc != MATCH_NOMATCH) RRETURN(rrc); ecode = prev; - flags = match_isgroup; + flags = match_tail_recursed; goto TAIL_RECURSE; } else /* OP_KETRMAX */ { - RMATCH(rrc, eptr, prev, offset_top, md, ims, eptrb, match_isgroup); + RMATCH(rrc, eptr, prev, offset_top, md, ims, eptrb, match_cbegroup); if (rrc != MATCH_NOMATCH) RRETURN(rrc); ecode += 1 + LINK_SIZE; - flags = 0; + flags = match_tail_recursed; goto TAIL_RECURSE; } /* Control never gets here */ @@ -1062,38 +1070,44 @@ for (;;) case OP_BRAZERO: { next = ecode+1; - RMATCH(rrc, eptr, next, offset_top, md, ims, eptrb, match_isgroup); + RMATCH(rrc, eptr, next, offset_top, md, ims, eptrb, 0); if (rrc != MATCH_NOMATCH) RRETURN(rrc); do next += GET(next,1); while (*next == OP_ALT); - ecode = next + 1+LINK_SIZE; + ecode = next + 1 + LINK_SIZE; } break; case OP_BRAMINZERO: { next = ecode+1; - do next += GET(next,1); while (*next == OP_ALT); - RMATCH(rrc, eptr, next + 1+LINK_SIZE, offset_top, md, ims, eptrb, - match_isgroup); + do next += GET(next, 1); while (*next == OP_ALT); + RMATCH(rrc, eptr, next + 1+LINK_SIZE, offset_top, md, ims, eptrb, 0); if (rrc != MATCH_NOMATCH) RRETURN(rrc); ecode++; } break; - /* End of a group, repeated or non-repeating. If we are at the end of - an assertion "group", stop matching and return MATCH_MATCH, but record the - current high water mark for use by positive assertions. Do this also - for the "once" (not-backup up) groups. */ + /* End of a group, repeated or non-repeating. */ case OP_KET: case OP_KETRMIN: case OP_KETRMAX: prev = ecode - GET(ecode, 1); - saved_eptr = eptrb->epb_saved_eptr; - /* Back up the stack of bracket start pointers. */ + /* If this was a group that remembered the subject start, in order to break + infinite repeats of empty string matches, retrieve the subject start from + the chain. Otherwise, set it NULL. */ + + if (*prev >= OP_SBRA) + { + saved_eptr = eptrb->epb_saved_eptr; /* Value at start of group */ + eptrb = eptrb->epb_prev; /* Backup to previous group */ + } + else saved_eptr = NULL; - eptrb = eptrb->epb_prev; + /* If we are at the end of an assertion group, stop matching and return + MATCH_MATCH, but record the current high water mark for use by positive + assertions. Do this also for the "once" (atomic) groups. */ if (*prev == OP_ASSERT || *prev == OP_ASSERT_NOT || *prev == OP_ASSERTBACK || *prev == OP_ASSERTBACK_NOT || @@ -1104,18 +1118,15 @@ for (;;) RRETURN(MATCH_MATCH); } - /* In all other cases except a conditional group we have to check the - group number back at the start and if necessary complete handling an - extraction by setting the offsets and bumping the high water mark. */ + /* For capturing groups we have to check the group number back at the start + and if necessary complete handling an extraction by setting the offsets and + bumping the high water mark. Note that whole-pattern recursion is coded as + a recurse into group 0, so it won't be picked up here. Instead, we catch it + when the OP_END is reached. Other recursion is handled here. */ - if (*prev != OP_COND) + if (*prev == OP_CBRA || *prev == OP_SCBRA) { - number = *prev - OP_BRA; - - /* For extended extraction brackets (large number), we have to fish out - the number from a dummy opcode at the start. */ - - if (number > EXTRACT_BASIC_MAX) number = GET2(prev, 2+LINK_SIZE); + number = GET2(prev, 1+LINK_SIZE); offset = number << 1; #ifdef DEBUG @@ -1123,42 +1134,34 @@ for (;;) printf("\n"); #endif - /* Test for a numbered group. This includes groups called as a result - of recursion. Note that whole-pattern recursion is coded as a recurse - into group 0, so it won't be picked up here. Instead, we catch it when - the OP_END is reached. */ - - if (number > 0) + md->capture_last = number; + if (offset >= md->offset_max) md->offset_overflow = TRUE; else { - md->capture_last = number; - if (offset >= md->offset_max) md->offset_overflow = TRUE; else - { - md->offset_vector[offset] = - md->offset_vector[md->offset_end - number]; - md->offset_vector[offset+1] = eptr - md->start_subject; - if (offset_top <= offset) offset_top = offset + 2; - } + md->offset_vector[offset] = + md->offset_vector[md->offset_end - number]; + md->offset_vector[offset+1] = eptr - md->start_subject; + if (offset_top <= offset) offset_top = offset + 2; + } - /* Handle a recursively called group. Restore the offsets - appropriately and continue from after the call. */ + /* Handle a recursively called group. Restore the offsets + appropriately and continue from after the call. */ - if (md->recursive != NULL && md->recursive->group_num == number) - { - recursion_info *rec = md->recursive; - DPRINTF(("Recursion (%d) succeeded - continuing\n", number)); - md->recursive = rec->prevrec; - md->start_match = rec->save_start; - memcpy(md->offset_vector, rec->offset_save, - rec->saved_max * sizeof(int)); - ecode = rec->after_call; - ims = original_ims; - break; - } + if (md->recursive != NULL && md->recursive->group_num == number) + { + recursion_info *rec = md->recursive; + DPRINTF(("Recursion (%d) succeeded - continuing\n", number)); + md->recursive = rec->prevrec; + md->start_match = rec->save_start; + memcpy(md->offset_vector, rec->offset_save, + rec->saved_max * sizeof(int)); + ecode = rec->after_call; + ims = original_ims; + break; } } - /* Reset the value of the ims flags, in case they got changed during - the group. */ + /* For both capturing and non-capturing groups, reset the value of the ims + flags, in case they got changed during the group. */ ims = original_ims; DPRINTF(("ims reset to %02lx\n", ims)); @@ -1179,20 +1182,22 @@ for (;;) preceding bracket, in the appropriate order. In the second case, we can use tail recursion to avoid using another stack frame. */ + flags = (*prev >= OP_SBRA)? match_cbegroup : 0; + if (*ecode == OP_KETRMIN) { RMATCH(rrc, eptr, ecode + 1+LINK_SIZE, offset_top, md, ims, eptrb, 0); if (rrc != MATCH_NOMATCH) RRETURN(rrc); ecode = prev; - flags = match_isgroup; + flags |= match_tail_recursed; goto TAIL_RECURSE; } else /* OP_KETRMAX */ { - RMATCH(rrc, eptr, prev, offset_top, md, ims, eptrb, match_isgroup); + RMATCH(rrc, eptr, prev, offset_top, md, ims, eptrb, flags); if (rrc != MATCH_NOMATCH) RRETURN(rrc); ecode += 1 + LINK_SIZE; - flags = 0; + flags = match_tail_recursed; goto TAIL_RECURSE; } /* Control never gets here */ @@ -1204,9 +1209,7 @@ for (;;) if ((ims & PCRE_MULTILINE) != 0) { if (eptr != md->start_subject && - (eptr == md->end_subject || - eptr < md->start_subject + md->nllen || - !IS_NEWLINE(eptr - md->nllen))) + (eptr == md->end_subject || !WAS_NEWLINE(eptr))) RRETURN(MATCH_NOMATCH); ecode++; break; @@ -1246,7 +1249,7 @@ for (;;) if (!md->endonly) { if (eptr != md->end_subject && - (eptr != md->end_subject - md->nllen || !IS_NEWLINE(eptr))) + (!IS_NEWLINE(eptr) || eptr != md->end_subject - md->nllen)) RRETURN(MATCH_NOMATCH); ecode++; break; @@ -1265,7 +1268,7 @@ for (;;) case OP_EODN: if (eptr != md->end_subject && - (eptr != md->end_subject - md->nllen || !IS_NEWLINE(eptr))) + (!IS_NEWLINE(eptr) || eptr != md->end_subject - md->nllen)) RRETURN(MATCH_NOMATCH); ecode++; break; @@ -1321,8 +1324,7 @@ for (;;) case OP_ANY: if ((ims & PCRE_DOTALL) == 0) { - if (eptr <= md->end_subject - md->nllen && IS_NEWLINE(eptr)) - RRETURN(MATCH_NOMATCH); + if (IS_NEWLINE(eptr)) RRETURN(MATCH_NOMATCH); } if (eptr++ >= md->end_subject) RRETURN(MATCH_NOMATCH); if (utf8) @@ -1416,6 +1418,26 @@ for (;;) ecode++; break; + case OP_ANYNL: + if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH); + GETCHARINCTEST(c, eptr); + switch(c) + { + default: RRETURN(MATCH_NOMATCH); + case 0x000d: + if (eptr < md->end_subject && *eptr == 0x0a) eptr++; + break; + case 0x000a: + case 0x000b: + case 0x000c: + case 0x0085: + case 0x2028: + case 0x2029: + break; + } + ecode++; + break; + #ifdef SUPPORT_UCP /* Check the next character by Unicode property. We will get here only if the support is in the binary; otherwise a compile-time error occurs. */ @@ -1458,7 +1480,6 @@ for (;;) default: RRETURN(PCRE_ERROR_INTERNAL); - break; } ecode += 3; @@ -1928,7 +1949,7 @@ for (;;) else { - int dc; + unsigned int dc; GETCHARINC(dc, eptr); ecode += length; @@ -1955,13 +1976,17 @@ for (;;) } break; - /* Match a single character repeatedly; different opcodes share code. */ + /* Match a single character repeatedly. */ case OP_EXACT: min = max = GET2(ecode, 1); ecode += 3; goto REPEATCHAR; + case OP_POSUPTO: + possessive = TRUE; + /* Fall through */ + case OP_UPTO: case OP_MINUPTO: min = 0; @@ -1970,6 +1995,27 @@ for (;;) ecode += 3; goto REPEATCHAR; + case OP_POSSTAR: + possessive = TRUE; + min = 0; + max = INT_MAX; + ecode++; + goto REPEATCHAR; + + case OP_POSPLUS: + possessive = TRUE; + min = 1; + max = INT_MAX; + ecode++; + goto REPEATCHAR; + + case OP_POSQUERY: + possessive = TRUE; + min = 0; + max = 1; + ecode++; + goto REPEATCHAR; + case OP_STAR: case OP_MINSTAR: case OP_PLUS: @@ -2005,10 +2051,9 @@ for (;;) uschar occhars[8]; #ifdef SUPPORT_UCP - int othercase; + unsigned int othercase; if ((ims & PCRE_CASELESS) != 0 && - (othercase = _pcre_ucp_othercase(fc)) >= 0 && - othercase >= 0) + (othercase = _pcre_ucp_othercase(fc)) != NOTACHAR) oclength = _pcre_ord2utf8(othercase, occhars); #endif /* SUPPORT_UCP */ @@ -2044,7 +2089,8 @@ for (;;) } /* Control never gets here */ } - else + + else /* Maximize */ { pp = eptr; for (i = min; i < max; i++) @@ -2058,6 +2104,8 @@ for (;;) eptr += oclength; } } + + if (possessive) continue; while (eptr >= pp) { RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0); @@ -2112,7 +2160,7 @@ for (;;) } /* Control never gets here */ } - else + else /* Maximize */ { pp = eptr; for (i = min; i < max; i++) @@ -2120,6 +2168,7 @@ for (;;) if (eptr >= md->end_subject || fc != md->lcc[*eptr]) break; eptr++; } + if (possessive) continue; while (eptr >= pp) { RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0); @@ -2148,7 +2197,7 @@ for (;;) } /* Control never gets here */ } - else + else /* Maximize */ { pp = eptr; for (i = min; i < max; i++) @@ -2156,6 +2205,7 @@ for (;;) if (eptr >= md->end_subject || fc != *eptr) break; eptr++; } + if (possessive) continue; while (eptr >= pp) { RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0); @@ -2208,6 +2258,34 @@ for (;;) ecode += 3; goto REPEATNOTCHAR; + case OP_NOTPOSSTAR: + possessive = TRUE; + min = 0; + max = INT_MAX; + ecode++; + goto REPEATNOTCHAR; + + case OP_NOTPOSPLUS: + possessive = TRUE; + min = 1; + max = INT_MAX; + ecode++; + goto REPEATNOTCHAR; + + case OP_NOTPOSQUERY: + possessive = TRUE; + min = 0; + max = 1; + ecode++; + goto REPEATNOTCHAR; + + case OP_NOTPOSUPTO: + possessive = TRUE; + min = 0; + max = GET2(ecode, 1); + ecode += 3; + goto REPEATNOTCHAR; + case OP_NOTSTAR: case OP_NOTMINSTAR: case OP_NOTPLUS: @@ -2247,7 +2325,7 @@ for (;;) /* UTF-8 mode */ if (utf8) { - register int d; + register unsigned int d; for (i = 1; i <= min; i++) { GETCHARINC(d, eptr); @@ -2272,7 +2350,7 @@ for (;;) /* UTF-8 mode */ if (utf8) { - register int d; + register unsigned int d; for (fi = min;; fi++) { RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0); @@ -2308,7 +2386,7 @@ for (;;) /* UTF-8 mode */ if (utf8) { - register int d; + register unsigned int d; for (i = min; i < max; i++) { int len = 1; @@ -2318,7 +2396,8 @@ for (;;) if (fc == d) break; eptr += len; } - for(;;) + if (possessive) continue; + for(;;) { RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0); if (rrc != MATCH_NOMATCH) RRETURN(rrc); @@ -2335,6 +2414,7 @@ for (;;) if (eptr >= md->end_subject || fc == md->lcc[*eptr]) break; eptr++; } + if (possessive) continue; while (eptr >= pp) { RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0); @@ -2356,7 +2436,7 @@ for (;;) /* UTF-8 mode */ if (utf8) { - register int d; + register unsigned int d; for (i = 1; i <= min; i++) { GETCHARINC(d, eptr); @@ -2379,7 +2459,7 @@ for (;;) /* UTF-8 mode */ if (utf8) { - register int d; + register unsigned int d; for (fi = min;; fi++) { RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0); @@ -2414,7 +2494,7 @@ for (;;) /* UTF-8 mode */ if (utf8) { - register int d; + register unsigned int d; for (i = min; i < max; i++) { int len = 1; @@ -2423,6 +2503,7 @@ for (;;) if (fc == d) break; eptr += len; } + if (possessive) continue; for(;;) { RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0); @@ -2440,6 +2521,7 @@ for (;;) if (eptr >= md->end_subject || fc == *eptr) break; eptr++; } + if (possessive) continue; while (eptr >= pp) { RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0); @@ -2471,6 +2553,34 @@ for (;;) ecode += 3; goto REPEATTYPE; + case OP_TYPEPOSSTAR: + possessive = TRUE; + min = 0; + max = INT_MAX; + ecode++; + goto REPEATTYPE; + + case OP_TYPEPOSPLUS: + possessive = TRUE; + min = 1; + max = INT_MAX; + ecode++; + goto REPEATTYPE; + + case OP_TYPEPOSQUERY: + possessive = TRUE; + min = 0; + max = 1; + ecode++; + goto REPEATTYPE; + + case OP_TYPEPOSUPTO: + possessive = TRUE; + min = 0; + max = GET2(ecode, 1); + ecode += 3; + goto REPEATTYPE; + case OP_TYPESTAR: case OP_TYPEMINSTAR: case OP_TYPEPLUS: @@ -2573,7 +2683,6 @@ for (;;) default: RRETURN(PCRE_ERROR_INTERNAL); - break; } } @@ -2613,9 +2722,7 @@ for (;;) for (i = 1; i <= min; i++) { if (eptr >= md->end_subject || - ((ims & PCRE_DOTALL) == 0 && - eptr <= md->end_subject - md->nllen && - IS_NEWLINE(eptr))) + ((ims & PCRE_DOTALL) == 0 && IS_NEWLINE(eptr))) RRETURN(MATCH_NOMATCH); eptr++; while (eptr < md->end_subject && (*eptr & 0xc0) == 0x80) eptr++; @@ -2626,6 +2733,28 @@ for (;;) eptr += min; break; + case OP_ANYNL: + for (i = 1; i <= min; i++) + { + if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH); + GETCHARINC(c, eptr); + switch(c) + { + default: RRETURN(MATCH_NOMATCH); + case 0x000d: + if (eptr < md->end_subject && *eptr == 0x0a) eptr++; + break; + case 0x000a: + case 0x000b: + case 0x000c: + case 0x0085: + case 0x2028: + case 0x2029: + break; + } + } + break; + case OP_NOT_DIGIT: for (i = 1; i <= min; i++) { @@ -2694,7 +2823,8 @@ for (;;) #endif /* SUPPORT_UTF8 */ /* Code for the non-UTF-8 case for minimum matching of operators other - than OP_PROP and OP_NOTPROP. */ + than OP_PROP and OP_NOTPROP. We can assume that there are the minimum + number of bytes present, as this was tested above. */ switch(ctype) { @@ -2703,8 +2833,7 @@ for (;;) { for (i = 1; i <= min; i++) { - if (eptr <= md->end_subject - md->nllen && IS_NEWLINE(eptr)) - RRETURN(MATCH_NOMATCH); + if (IS_NEWLINE(eptr)) RRETURN(MATCH_NOMATCH); eptr++; } } @@ -2715,6 +2844,28 @@ for (;;) eptr += min; break; + /* Because of the CRLF case, we can't assume the minimum number of + bytes are present in this case. */ + + case OP_ANYNL: + for (i = 1; i <= min; i++) + { + if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH); + switch(*eptr++) + { + default: RRETURN(MATCH_NOMATCH); + case 0x000d: + if (eptr < md->end_subject && *eptr == 0x0a) eptr++; + break; + case 0x000a: + case 0x000b: + case 0x000c: + case 0x0085: + break; + } + } + break; + case OP_NOT_DIGIT: for (i = 1; i <= min; i++) if ((md->ctypes[*eptr++] & ctype_digit) != 0) RRETURN(MATCH_NOMATCH); @@ -2776,7 +2927,7 @@ for (;;) GETCHARINC(c, eptr); if (prop_fail_result) RRETURN(MATCH_NOMATCH); } - break; + /* Control never gets here */ case PT_LAMP: for (fi = min;; fi++) @@ -2791,7 +2942,7 @@ for (;;) prop_chartype == ucp_Lt) == prop_fail_result) RRETURN(MATCH_NOMATCH); } - break; + /* Control never gets here */ case PT_GC: for (fi = min;; fi++) @@ -2804,7 +2955,7 @@ for (;;) if ((prop_category == prop_value) == prop_fail_result) RRETURN(MATCH_NOMATCH); } - break; + /* Control never gets here */ case PT_PC: for (fi = min;; fi++) @@ -2817,7 +2968,7 @@ for (;;) if ((prop_chartype == prop_value) == prop_fail_result) RRETURN(MATCH_NOMATCH); } - break; + /* Control never gets here */ case PT_SC: for (fi = min;; fi++) @@ -2830,11 +2981,10 @@ for (;;) if ((prop_script == prop_value) == prop_fail_result) RRETURN(MATCH_NOMATCH); } - break; + /* Control never gets here */ default: RRETURN(PCRE_ERROR_INTERNAL); - break; } } @@ -2878,7 +3028,7 @@ for (;;) if (rrc != MATCH_NOMATCH) RRETURN(rrc); if (fi >= max || eptr >= md->end_subject || (ctype == OP_ANY && (ims & PCRE_DOTALL) == 0 && - eptr <= md->end_subject - md->nllen && IS_NEWLINE(eptr))) + IS_NEWLINE(eptr))) RRETURN(MATCH_NOMATCH); GETCHARINC(c, eptr); @@ -2890,6 +3040,23 @@ for (;;) case OP_ANYBYTE: break; + case OP_ANYNL: + switch(c) + { + default: RRETURN(MATCH_NOMATCH); + case 0x000d: + if (eptr < md->end_subject && *eptr == 0x0a) eptr++; + break; + case 0x000a: + case 0x000b: + case 0x000c: + case 0x0085: + case 0x2028: + case 0x2029: + break; + } + break; + case OP_NOT_DIGIT: if (c < 256 && (md->ctypes[c] & ctype_digit) != 0) RRETURN(MATCH_NOMATCH); @@ -2934,8 +3101,7 @@ for (;;) RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0); if (rrc != MATCH_NOMATCH) RRETURN(rrc); if (fi >= max || eptr >= md->end_subject || - ((ims & PCRE_DOTALL) == 0 && - eptr <= md->end_subject - md->nllen && IS_NEWLINE(eptr))) + ((ims & PCRE_DOTALL) == 0 && IS_NEWLINE(eptr))) RRETURN(MATCH_NOMATCH); c = *eptr++; @@ -2947,6 +3113,21 @@ for (;;) case OP_ANYBYTE: break; + case OP_ANYNL: + switch(c) + { + default: RRETURN(MATCH_NOMATCH); + case 0x000d: + if (eptr < md->end_subject && *eptr == 0x0a) eptr++; + break; + case 0x000a: + case 0x000b: + case 0x000c: + case 0x0085: + break; + } + break; + case OP_NOT_DIGIT: if ((md->ctypes[c] & ctype_digit) != 0) RRETURN(MATCH_NOMATCH); break; @@ -2979,7 +3160,7 @@ for (;;) /* Control never gets here */ } - /* If maximizing it is worth using inline code for speed, doing the type + /* If maximizing, it is worth using inline code for speed, doing the type test once at the start (i.e. keep it out of the loop). Again, keep the UTF-8 and UCP stuff separate. */ @@ -3060,6 +3241,7 @@ for (;;) /* eptr is now past the end of the maximum run */ + if (possessive) continue; for(;;) { RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0); @@ -3095,6 +3277,7 @@ for (;;) /* eptr is now past the end of the maximum run */ + if (possessive) continue; for(;;) { RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0); @@ -3137,9 +3320,7 @@ for (;;) { for (i = min; i < max; i++) { - if (eptr >= md->end_subject || - (eptr <= md->end_subject - md->nllen && IS_NEWLINE(eptr))) - break; + if (eptr >= md->end_subject || IS_NEWLINE(eptr)) break; eptr++; while (eptr < md->end_subject && (*eptr & 0xc0) == 0x80) eptr++; } @@ -3163,9 +3344,7 @@ for (;;) { for (i = min; i < max; i++) { - if (eptr >= md->end_subject || - (eptr <= md->end_subject - md->nllen && IS_NEWLINE(eptr))) - break; + if (eptr >= md->end_subject || IS_NEWLINE(eptr)) break; eptr++; } break; @@ -3173,7 +3352,8 @@ for (;;) else { c = max - min; - if (c > md->end_subject - eptr) c = md->end_subject - eptr; + if (c > (unsigned int)(md->end_subject - eptr)) + c = md->end_subject - eptr; eptr += c; } } @@ -3183,10 +3363,32 @@ for (;;) case OP_ANYBYTE: c = max - min; - if (c > md->end_subject - eptr) c = md->end_subject - eptr; + if (c > (unsigned int)(md->end_subject - eptr)) + c = md->end_subject - eptr; eptr += c; break; + case OP_ANYNL: + for (i = min; i < max; i++) + { + int len = 1; + if (eptr >= md->end_subject) break; + GETCHARLEN(c, eptr, len); + if (c == 0x000d) + { + if (++eptr >= md->end_subject) break; + if (*eptr == 0x000a) eptr++; + } + else + { + if (c != 0x000a && c != 0x000b && c != 0x000c && + c != 0x0085 && c != 0x2028 && c != 0x2029) + break; + eptr += len; + } + } + break; + case OP_NOT_DIGIT: for (i = min; i < max; i++) { @@ -3259,6 +3461,7 @@ for (;;) /* eptr is now past the end of the maximum run */ + if (possessive) continue; for(;;) { RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0); @@ -3279,9 +3482,7 @@ for (;;) { for (i = min; i < max; i++) { - if (eptr >= md->end_subject || - (eptr <= md->end_subject - md->nllen && IS_NEWLINE(eptr))) - break; + if (eptr >= md->end_subject || IS_NEWLINE(eptr)) break; eptr++; } break; @@ -3290,10 +3491,30 @@ for (;;) case OP_ANYBYTE: c = max - min; - if (c > md->end_subject - eptr) c = md->end_subject - eptr; + if (c > (unsigned int)(md->end_subject - eptr)) + c = md->end_subject - eptr; eptr += c; break; + case OP_ANYNL: + for (i = min; i < max; i++) + { + if (eptr >= md->end_subject) break; + c = *eptr; + if (c == 0x000d) + { + if (++eptr >= md->end_subject) break; + if (*eptr == 0x000a) eptr++; + } + else + { + if (c != 0x000a && c != 0x000b && c != 0x000c && c != 0x0085) + break; + eptr++; + } + } + break; + case OP_NOT_DIGIT: for (i = min; i < max; i++) { @@ -3354,6 +3575,7 @@ for (;;) /* eptr is now past the end of the maximum run */ + if (possessive) continue; while (eptr >= pp) { RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0); @@ -3368,14 +3590,12 @@ for (;;) } /* Control never gets here */ - /* There's been some horrible disaster. Since all codes > OP_BRA are - for capturing brackets, and there shouldn't be any gaps between 0 and - OP_BRA, arrival here can only mean there is something seriously wrong - in the code above or the OP_xxx definitions. */ + /* There's been some horrible disaster. Arrival here can only mean there is + something seriously wrong in the code above or the OP_xxx definitions. */ default: DPRINTF(("Unknown opcode %d\n", *ecode)); - RRETURN(PCRE_ERROR_UNKNOWN_NODE); + RRETURN(PCRE_ERROR_UNKNOWN_OPCODE); } /* Do not stick any code in here without much thought; it is assumed @@ -3413,7 +3633,6 @@ Undefine all the macros that were defined above to handle this. */ #undef cur_is_word #undef condition -#undef minimize #undef prev_is_word #undef original_ims @@ -3486,6 +3705,7 @@ BOOL startline; BOOL firstline; BOOL first_byte_caseless = FALSE; BOOL req_byte_caseless = FALSE; +BOOL utf8; match_data match_block; match_data *md = &match_block; const uschar *tables; @@ -3493,6 +3713,7 @@ const uschar *start_bits = NULL; USPTR start_match = (USPTR)subject + start_offset; USPTR end_subject; USPTR req_byte_ptr = start_match - 1; +eptrblock eptrchain[EPTR_WORK_SIZE]; pcre_study_data internal_study; const pcre_study_data *study; @@ -3569,7 +3790,7 @@ md->end_subject = md->start_subject + length; end_subject = md->end_subject; md->endonly = (re->options & PCRE_DOLLAR_ENDONLY) != 0; -md->utf8 = (re->options & PCRE_UTF8) != 0; +utf8 = md->utf8 = (re->options & PCRE_UTF8) != 0; md->notbol = (options & PCRE_NOTBOL) != 0; md->noteol = (options & PCRE_NOTEOL) != 0; @@ -3578,6 +3799,7 @@ md->partial = (options & PCRE_PARTIAL) != 0; md->hitend = FALSE; md->recursive = NULL; /* No recursion at top level */ +md->eptrchain = eptrchain; /* Make workspace generally available */ md->lcc = tables + lcc_offset; md->ctypes = tables + ctypes_offset; @@ -3585,26 +3807,36 @@ md->ctypes = tables + ctypes_offset; /* Handle different types of newline. The two bits give four cases. If nothing is set at run time, whatever was used at compile time applies. */ -switch ((((options & PCRE_NEWLINE_CRLF) == 0)? re->options : options) & - PCRE_NEWLINE_CRLF) +switch ((((options & PCRE_NEWLINE_BITS) == 0)? re->options : options) & + PCRE_NEWLINE_BITS) { - default: newline = NEWLINE; break; /* Compile-time default */ + case 0: newline = NEWLINE; break; /* Compile-time default */ case PCRE_NEWLINE_CR: newline = '\r'; break; case PCRE_NEWLINE_LF: newline = '\n'; break; case PCRE_NEWLINE_CR+ PCRE_NEWLINE_LF: newline = ('\r' << 8) | '\n'; break; + case PCRE_NEWLINE_ANY: newline = -1; break; + default: return PCRE_ERROR_BADNEWLINE; } -if (newline > 255) +if (newline < 0) { - md->nllen = 2; - md->nl[0] = (newline >> 8) & 255; - md->nl[1] = newline & 255; + md->nltype = NLTYPE_ANY; } else { - md->nllen = 1; - md->nl[0] = newline; + md->nltype = NLTYPE_FIXED; + if (newline > 255) + { + md->nllen = 2; + md->nl[0] = (newline >> 8) & 255; + md->nl[1] = newline & 255; + } + else + { + md->nllen = 1; + md->nl[0] = newline; + } } /* Partial matching is supported only for a restricted set of regexes at the @@ -3617,7 +3849,7 @@ if (md->partial && (re->options & PCRE_NOPARTIAL) != 0) back the character offset. */ #ifdef SUPPORT_UTF8 -if (md->utf8 && (options & PCRE_NO_UTF8_CHECK) == 0) +if (utf8 && (options & PCRE_NO_UTF8_CHECK) == 0) { if (_pcre_valid_utf8((uschar *)subject, length) >= 0) return PCRE_ERROR_BADUTF8; @@ -3709,10 +3941,13 @@ if ((re->options & PCRE_REQCHSET) != 0) req_byte2 = (tables + fcc_offset)[req_byte]; /* case flipped */ } + +/* ==========================================================================*/ + /* Loop for handling unanchored repeated matching attempts; for anchored regexs the loop runs just once. */ -do +for(;;) { USPTR save_end_subject = end_subject; @@ -3727,14 +3962,14 @@ do /* Advance to a unique first char if possible. If firstline is TRUE, the start of the match is constrained to the first line of a multiline string. - Implement this by temporarily adjusting end_subject so that we stop scanning - at a newline. If the match fails at the newline, later code breaks this loop. - */ + That is, the match must be before or at the first newline. Implement this by + temporarily adjusting end_subject so that we stop scanning at a newline. If + the match fails at the newline, later code breaks this loop. */ if (firstline) { USPTR t = start_match; - while (t <= save_end_subject - md->nllen && !IS_NEWLINE(t)) t++; + while (t < md->end_subject && !IS_NEWLINE(t)) t++; end_subject = t; } @@ -3755,11 +3990,9 @@ do else if (startline) { - if (start_match >= md->start_subject + md->nllen + - start_offset) + if (start_match > md->start_subject + start_offset) { - while (start_match <= end_subject && - !IS_NEWLINE(start_match - md->nllen)) + while (start_match <= end_subject && !WAS_NEWLINE(start_match)) start_match++; } } @@ -3795,8 +4028,8 @@ do HOWEVER: when the subject string is very, very long, searching to its end can take a long time, and give bad performance on quite ordinary patterns. This - showed up when somebody was matching /^C/ on a 32-megabyte string... so we - don't do this when the string is sufficiently long. + showed up when somebody was matching something like /^\d+C/ on a 32-megabyte + string... so we don't do this when the string is sufficiently long. ALSO: this processing is disabled when partial matching is requested. */ @@ -3828,9 +4061,14 @@ do } } - /* If we can't find the required character, break the matching loop */ + /* If we can't find the required character, break the matching loop, + forcing a match failure. */ - if (p >= end_subject) break; + if (p >= end_subject) + { + rc = MATCH_NOMATCH; + break; + } /* If we have found the required character, save the point where we found it, so that we don't search again next time round the loop if @@ -3840,49 +4078,70 @@ do } } - /* When a match occurs, substrings will be set for all internal extractions; - we just need to set up the whole thing as substring 0 before returning. If - there were too many extractions, set the return code to zero. In the case - where we had to get some local store to hold offsets for backreferences, copy - those back references that we can. In this case there need not be overflow - if certain parts of the pattern were not used. */ + /* OK, we can now run the match. */ md->start_match = start_match; md->match_call_count = 0; + md->eptrn = 0; /* Next free eptrchain slot */ + rc = match(start_match, md->start_code, 2, md, ims, NULL, 0, 0); - rc = match(start_match, md->start_code, 2, md, ims, NULL, match_isgroup, 0); + /* Any return other than MATCH_NOMATCH breaks the loop. */ - /* When the result is no match, if the subject's first character was a - newline and the PCRE_FIRSTLINE option is set, break (which will return - PCRE_ERROR_NOMATCH). The option requests that a match occur before the first - newline in the subject. Otherwise, advance the pointer to the next character - and continue - but the continuation will actually happen only when the - pattern is not anchored. */ + if (rc != MATCH_NOMATCH) break; - if (rc == MATCH_NOMATCH) - { - if (firstline && - start_match <= md->end_subject - md->nllen && - IS_NEWLINE(start_match)) - break; - start_match++; + /* If PCRE_FIRSTLINE is set, the match must happen before or at the first + newline in the subject (though it may continue over the newline). Therefore, + if we have just failed to match, starting at a newline, do not continue. */ + + if (firstline && IS_NEWLINE(start_match)) break; + + /* Advance the match position by one character. */ + + start_match++; #ifdef SUPPORT_UTF8 - if (md->utf8) - while(start_match < end_subject && (*start_match & 0xc0) == 0x80) - start_match++; + if (utf8) + while(start_match < end_subject && (*start_match & 0xc0) == 0x80) + start_match++; #endif - continue; - } - if (rc != MATCH_MATCH) - { - DPRINTF((">>>> error: returning %d\n", rc)); - return rc; - } + /* Break the loop if the pattern is anchored or if we have passed the end of + the subject. */ + + if (anchored || start_match > end_subject) break; + + /* If we have just passed a CR and the newline option is CRLF or ANY, and we + are now at a LF, advance the match position by one more character. */ + + if (start_match[-1] == '\r' && + (md->nltype == NLTYPE_ANY || md->nllen == 2) && + start_match < end_subject && + *start_match == '\n') + start_match++; + + } /* End of for(;;) "bumpalong" loop */ + +/* ==========================================================================*/ + +/* We reach here when rc is not MATCH_NOMATCH, or if one of the stopping +conditions is true: - /* We have a match! Copy the offset information from temporary store if - necessary */ +(1) The pattern is anchored; +(2) We are past the end of the subject; + +(3) PCRE_FIRSTLINE is set and we have failed to match at a newline, because + this option requests that a match occur at or before the first newline in + the subject. + +When we have a match and the offset vector is big enough to deal with any +backreferences, captured substring offsets will already be set up. In the case +where we had to get some local store to hold offsets for backreference +processing, copy those that we can. In this case there need not be overflow if +certain parts of the pattern were not used, even though there are more +capturing parentheses than vector slots. */ + +if (rc == MATCH_MATCH) + { if (using_temporary_offsets) { if (offsetcount >= 4) @@ -3891,15 +4150,18 @@ do (offsetcount - 2) * sizeof(int)); DPRINTF(("Copied offsets from temporary memory\n")); } - if (md->end_offset_top > offsetcount) - md->offset_overflow = TRUE; - + if (md->end_offset_top > offsetcount) md->offset_overflow = TRUE; DPRINTF(("Freeing temporary memory\n")); (pcre_free)(md->offset_vector); } + /* Set the return code to the number of captured strings, or 0 if there are + too many to fit into the vector. */ + rc = md->offset_overflow? 0 : md->end_offset_top/2; + /* If there is space, set up the whole thing as substring 0. */ + if (offsetcount < 2) rc = 0; else { offsets[0] = start_match - md->start_subject; @@ -3910,9 +4172,8 @@ do return rc; } -/* This "while" is the end of the "do" above */ - -while (!anchored && start_match <= end_subject); +/* Control gets here if there has been an error, or if the overall match +attempt has failed at all permitted starting positions. */ if (using_temporary_offsets) { @@ -3920,7 +4181,12 @@ if (using_temporary_offsets) (pcre_free)(md->offset_vector); } -if (md->partial && md->hitend) +if (rc != MATCH_NOMATCH) + { + DPRINTF((">>>> error: returning %d\n", rc)); + return rc; + } +else if (md->partial && md->hitend) { DPRINTF((">>>> returning PCRE_ERROR_PARTIAL\n")); return PCRE_ERROR_PARTIAL; diff --git a/src/src/pcre/pcre_fullinfo.c b/src/src/pcre/pcre_fullinfo.c index 244afd9c7..5eddcd71e 100644 --- a/src/src/pcre/pcre_fullinfo.c +++ b/src/src/pcre/pcre_fullinfo.c @@ -1,4 +1,4 @@ -/* $Cambridge: exim/src/src/pcre/pcre_fullinfo.c,v 1.3 2006/11/07 16:50:36 ph10 Exp $ */ +/* $Cambridge: exim/src/src/pcre/pcre_fullinfo.c,v 1.4 2007/01/23 15:08:45 ph10 Exp $ */ /************************************************* * Perl-Compatible Regular Expressions * diff --git a/src/src/pcre/pcre_get.c b/src/src/pcre/pcre_get.c index 835ce0d97..0dcd5d69b 100644 --- a/src/src/pcre/pcre_get.c +++ b/src/src/pcre/pcre_get.c @@ -1,4 +1,4 @@ -/* $Cambridge: exim/src/src/pcre/pcre_get.c,v 1.3 2006/11/07 16:50:36 ph10 Exp $ */ +/* $Cambridge: exim/src/src/pcre/pcre_get.c,v 1.4 2007/01/23 15:08:45 ph10 Exp $ */ /************************************************* * Perl-Compatible Regular Expressions * @@ -438,7 +438,6 @@ pcre_get_named_substring(const pcre *code, const char *subject, int *ovector, int n = get_first_set(code, stringname, ovector); if (n <= 0) return n; return pcre_get_substring(subject, ovector, stringcount, n, stringptr); - } diff --git a/src/src/pcre/pcre_globals.c b/src/src/pcre/pcre_globals.c index 90ff487da..2c3a80b67 100644 --- a/src/src/pcre/pcre_globals.c +++ b/src/src/pcre/pcre_globals.c @@ -1,4 +1,4 @@ -/* $Cambridge: exim/src/src/pcre/pcre_globals.c,v 1.3 2006/11/07 16:50:36 ph10 Exp $ */ +/* $Cambridge: exim/src/src/pcre/pcre_globals.c,v 1.4 2007/01/23 15:08:45 ph10 Exp $ */ /************************************************* * Perl-Compatible Regular Expressions * @@ -53,6 +53,18 @@ differently, and global variables are not used (see pcre.in). */ #ifndef VPCOMPAT + +/************************************************************************** +This code used to be here for use when compiling as a C++ library. However, +according to Dair Grant it is not needed: " + + Including 'extern "C"' in the declaration generates an "initialized and + declared `extern'" warning from gcc 4.0.1. Since we include pcre_internal.h, + which includes pcre.h, which declares these prototypes within an extern "C" {} + block, we shouldn't need the prefix here. + +So, from Release 7.0 I have cut this out. + #ifdef __cplusplus extern "C" void *(*pcre_malloc)(size_t) = malloc; extern "C" void (*pcre_free)(void *) = free; @@ -60,12 +72,13 @@ extern "C" void *(*pcre_stack_malloc)(size_t) = malloc; extern "C" void (*pcre_stack_free)(void *) = free; extern "C" int (*pcre_callout)(pcre_callout_block *) = NULL; #else +**************************************************************************/ + void *(*pcre_malloc)(size_t) = malloc; void (*pcre_free)(void *) = free; void *(*pcre_stack_malloc)(size_t) = malloc; void (*pcre_stack_free)(void *) = free; int (*pcre_callout)(pcre_callout_block *) = NULL; #endif -#endif /* End of pcre_globals.c */ diff --git a/src/src/pcre/pcre_internal.h b/src/src/pcre/pcre_internal.h index 0eec49e4c..20172832f 100644 --- a/src/src/pcre/pcre_internal.h +++ b/src/src/pcre/pcre_internal.h @@ -1,4 +1,4 @@ -/* $Cambridge: exim/src/src/pcre/pcre_internal.h,v 1.3 2006/11/07 16:50:36 ph10 Exp $ */ +/* $Cambridge: exim/src/src/pcre/pcre_internal.h,v 1.4 2007/01/23 15:08:45 ph10 Exp $ */ /************************************************* * Perl-Compatible Regular Expressions * @@ -56,12 +56,16 @@ functions whose names all begin with "_pcre_". */ /* Use a macro for debugging printing, 'cause that eliminates the use of #ifdef inline, and there are *still* stupid compilers about that don't like indented pre-processor statements, or at least there were when I first wrote this. After -all, it had only been about 10 years then... */ +all, it had only been about 10 years then... +It turns out that the Mac Debugging.h header also defines the macro DPRINTF, so +be absolutely sure we get our version. */ + +#undef DPRINTF #ifdef DEBUG #define DPRINTF(p) printf p #else -#define DPRINTF(p) /*nothing*/ +#define DPRINTF(p) /* Nothing */ #endif @@ -116,13 +120,48 @@ Unix, where it is defined in sys/types, so use "uschar" instead. */ typedef unsigned char uschar; -/* PCRE is able to support 3 different kinds of newline (CR, LF, CRLF). The -following macro is used to package up testing for newlines. NLBLOCK is defined -in the various modules to indicate in which datablock the parameters exist. */ +/* This is an unsigned int value that no character can ever have. UTF-8 +characters only go up to 0x7fffffff (though Unicode doesn't go beyond +0x0010ffff). */ + +#define NOTACHAR 0xffffffff + +/* PCRE is able to support several different kinds of newline (CR, LF, CRLF, +and "all" at present). The following macros are used to package up testing for +newlines. NLBLOCK, PSSTART, and PSEND are defined in the various modules to +indicate in which datablock the parameters exist, and what the start/end of +string field names are. */ + +#define NLTYPE_FIXED 0 /* Newline is a fixed length string */ +#define NLTYPE_ANY 1 /* Newline is any Unicode line ending */ + +/* This macro checks for a newline at the given position */ #define IS_NEWLINE(p) \ - ((p)[0] == NLBLOCK->nl[0] && \ - (NLBLOCK->nllen == 1 || (p)[1] == NLBLOCK->nl[1])) + ((NLBLOCK->nltype != NLTYPE_FIXED)? \ + ((p) < NLBLOCK->PSEND && \ + _pcre_is_newline((p), NLBLOCK->PSEND, &(NLBLOCK->nllen), utf8) \ + ) \ + : \ + ((p) <= NLBLOCK->PSEND - NLBLOCK->nllen && \ + (p)[0] == NLBLOCK->nl[0] && \ + (NLBLOCK->nllen == 1 || (p)[1] == NLBLOCK->nl[1]) \ + ) \ + ) + +/* This macro checks for a newline immediately preceding the given position */ + +#define WAS_NEWLINE(p) \ + ((NLBLOCK->nltype != NLTYPE_FIXED)? \ + ((p) > NLBLOCK->PSSTART && \ + _pcre_was_newline((p), NLBLOCK->PSSTART, &(NLBLOCK->nllen), utf8) \ + ) \ + : \ + ((p) >= NLBLOCK->PSSTART + NLBLOCK->nllen && \ + (p)[-NLBLOCK->nllen] == NLBLOCK->nl[0] && \ + (NLBLOCK->nllen == 1 || (p)[-NLBLOCK->nllen+1] == NLBLOCK->nl[1]) \ + ) \ + ) /* When PCRE is compiled as a C++ library, the subject pointer can be replaced with a custom type. This makes it possible, for example, to allow pcre_exec() @@ -280,7 +319,7 @@ we know we are in UTF-8 mode. */ #define GETCHAR(c, eptr) \ c = *eptr; \ - if ((c & 0xc0) == 0xc0) \ + if (c >= 0xc0) \ { \ int gcii; \ int gcaa = _pcre_utf8_table4[c & 0x3f]; /* Number of additional bytes */ \ @@ -298,7 +337,7 @@ pointer. */ #define GETCHARTEST(c, eptr) \ c = *eptr; \ - if (utf8 && (c & 0xc0) == 0xc0) \ + if (utf8 && c >= 0xc0) \ { \ int gcii; \ int gcaa = _pcre_utf8_table4[c & 0x3f]; /* Number of additional bytes */ \ @@ -316,7 +355,7 @@ know we are in UTF-8 mode. */ #define GETCHARINC(c, eptr) \ c = *eptr++; \ - if ((c & 0xc0) == 0xc0) \ + if (c >= 0xc0) \ { \ int gcaa = _pcre_utf8_table4[c & 0x3f]; /* Number of additional bytes */ \ int gcss = 6*gcaa; \ @@ -332,7 +371,7 @@ know we are in UTF-8 mode. */ #define GETCHARINCTEST(c, eptr) \ c = *eptr++; \ - if (utf8 && (c & 0xc0) == 0xc0) \ + if (utf8 && c >= 0xc0) \ { \ int gcaa = _pcre_utf8_table4[c & 0x3f]; /* Number of additional bytes */ \ int gcss = 6*gcaa; \ @@ -349,7 +388,7 @@ if there are extra bytes. This is called when we know we are in UTF-8 mode. */ #define GETCHARLEN(c, eptr, len) \ c = *eptr; \ - if ((c & 0xc0) == 0xc0) \ + if (c >= 0xc0) \ { \ int gcii; \ int gcaa = _pcre_utf8_table4[c & 0x3f]; /* Number of additional bytes */ \ @@ -402,20 +441,21 @@ bits. */ /* Masks for identifying the public options that are permitted at compile time, run time, or study time, respectively. */ +#define PCRE_NEWLINE_BITS (PCRE_NEWLINE_CR|PCRE_NEWLINE_LF|PCRE_NEWLINE_ANY) + #define PUBLIC_OPTIONS \ (PCRE_CASELESS|PCRE_EXTENDED|PCRE_ANCHORED|PCRE_MULTILINE| \ PCRE_DOTALL|PCRE_DOLLAR_ENDONLY|PCRE_EXTRA|PCRE_UNGREEDY|PCRE_UTF8| \ PCRE_NO_AUTO_CAPTURE|PCRE_NO_UTF8_CHECK|PCRE_AUTO_CALLOUT|PCRE_FIRSTLINE| \ - PCRE_DUPNAMES|PCRE_NEWLINE_CR|PCRE_NEWLINE_LF) + PCRE_DUPNAMES|PCRE_NEWLINE_BITS) #define PUBLIC_EXEC_OPTIONS \ (PCRE_ANCHORED|PCRE_NOTBOL|PCRE_NOTEOL|PCRE_NOTEMPTY|PCRE_NO_UTF8_CHECK| \ - PCRE_PARTIAL|PCRE_NEWLINE_CR|PCRE_NEWLINE_LF) + PCRE_PARTIAL|PCRE_NEWLINE_BITS) #define PUBLIC_DFA_EXEC_OPTIONS \ (PCRE_ANCHORED|PCRE_NOTBOL|PCRE_NOTEOL|PCRE_NOTEMPTY|PCRE_NO_UTF8_CHECK| \ - PCRE_PARTIAL|PCRE_DFA_SHORTEST|PCRE_DFA_RESTART|PCRE_NEWLINE_CR| \ - PCRE_NEWLINE_LF) + PCRE_PARTIAL|PCRE_DFA_SHORTEST|PCRE_DFA_RESTART|PCRE_NEWLINE_BITS) #define PUBLIC_STUDY_OPTIONS 0 /* None defined */ @@ -447,9 +487,7 @@ typedef int BOOL; #define FALSE 0 #define TRUE 1 -/* Escape items that are just an encoding of a particular data value. Note that -ESC_n is defined as yet another macro, which is set in config.h to either \n -(the default) or \r (which some people want). */ +/* Escape items that are just an encoding of a particular data value. */ #ifndef ESC_e #define ESC_e 27 @@ -460,7 +498,7 @@ ESC_n is defined as yet another macro, which is set in config.h to either \n #endif #ifndef ESC_n -#define ESC_n NEWLINE +#define ESC_n '\n' #endif #ifndef ESC_r @@ -499,21 +537,28 @@ value such as \n. They must have non-zero values, as check_escape() returns their negation. Also, they must appear in the same order as in the opcode definitions below, up to ESC_z. There's a dummy for OP_ANY because it corresponds to "." rather than an escape sequence. The final one must be -ESC_REF as subsequent values are used for \1, \2, \3, etc. There is are two -tests in the code for an escape greater than ESC_b and less than ESC_Z to -detect the types that may be repeated. These are the types that consume -characters. If any new escapes are put in between that don't consume a +ESC_REF as subsequent values are used for backreferences (\1, \2, \3, etc). +There are two tests in the code for an escape greater than ESC_b and less than +ESC_Z to detect the types that may be repeated. These are the types that +consume characters. If any new escapes are put in between that don't consume a character, that code will have to change. */ enum { ESC_A = 1, ESC_G, ESC_B, ESC_b, ESC_D, ESC_d, ESC_S, ESC_s, ESC_W, - ESC_w, ESC_dum1, ESC_C, ESC_P, ESC_p, ESC_X, ESC_Z, ESC_z, ESC_E, - ESC_Q, ESC_REF }; + ESC_w, ESC_dum1, ESC_C, ESC_P, ESC_p, ESC_R, ESC_X, ESC_Z, ESC_z, + ESC_E, ESC_Q, ESC_k, ESC_REF }; + /* Opcode table: OP_BRA must be last, as all values >= it are used for brackets that extract substrings. Starting from 1 (i.e. after OP_END), the values up to OP_EOD must correspond in order to the list of escapes immediately above. -Note that whenever this list is updated, the two macro definitions that follow -must also be updated to match. */ + +To keep stored, compiled patterns compatible, new opcodes should be added +immediately before OP_BRA, where (since release 7.0) a gap is left for this +purpose. + +*** NOTE NOTE NOTE *** Whenever this list is updated, the two macro definitions +that follow must also be updated to match. There is also a table called +"coptable" in pcre_dfa_exec.c that must be updated. */ enum { OP_END, /* 0 End of pattern */ @@ -534,110 +579,122 @@ enum { OP_ANYBYTE, /* 12 Match any byte (\C); different to OP_ANY for UTF-8 */ OP_NOTPROP, /* 13 \P (not Unicode property) */ OP_PROP, /* 14 \p (Unicode property) */ - OP_EXTUNI, /* 15 \X (extended Unicode sequence */ - OP_EODN, /* 16 End of data or \n at end of data: \Z. */ - OP_EOD, /* 17 End of data: \z */ - - OP_OPT, /* 18 Set runtime options */ - OP_CIRC, /* 19 Start of line - varies with multiline switch */ - OP_DOLL, /* 20 End of line - varies with multiline switch */ - OP_CHAR, /* 21 Match one character, casefully */ - OP_CHARNC, /* 22 Match one character, caselessly */ - OP_NOT, /* 23 Match one character, not the following one */ - - OP_STAR, /* 24 The maximizing and minimizing versions of */ - OP_MINSTAR, /* 25 all these opcodes must come in pairs, with */ - OP_PLUS, /* 26 the minimizing one second. */ - OP_MINPLUS, /* 27 This first set applies to single characters */ - OP_QUERY, /* 28 */ - OP_MINQUERY, /* 29 */ - OP_UPTO, /* 30 From 0 to n matches */ - OP_MINUPTO, /* 31 */ - OP_EXACT, /* 32 Exactly n matches */ - - OP_NOTSTAR, /* 33 The maximizing and minimizing versions of */ - OP_NOTMINSTAR, /* 34 all these opcodes must come in pairs, with */ - OP_NOTPLUS, /* 35 the minimizing one second. */ - OP_NOTMINPLUS, /* 36 This set applies to "not" single characters */ - OP_NOTQUERY, /* 37 */ - OP_NOTMINQUERY, /* 38 */ - OP_NOTUPTO, /* 39 From 0 to n matches */ - OP_NOTMINUPTO, /* 40 */ - OP_NOTEXACT, /* 41 Exactly n matches */ - - OP_TYPESTAR, /* 42 The maximizing and minimizing versions of */ - OP_TYPEMINSTAR, /* 43 all these opcodes must come in pairs, with */ - OP_TYPEPLUS, /* 44 the minimizing one second. These codes must */ - OP_TYPEMINPLUS, /* 45 be in exactly the same order as those above. */ - OP_TYPEQUERY, /* 46 This set applies to character types such as \d */ - OP_TYPEMINQUERY, /* 47 */ - OP_TYPEUPTO, /* 48 From 0 to n matches */ - OP_TYPEMINUPTO, /* 49 */ - OP_TYPEEXACT, /* 50 Exactly n matches */ - - OP_CRSTAR, /* 51 The maximizing and minimizing versions of */ - OP_CRMINSTAR, /* 52 all these opcodes must come in pairs, with */ - OP_CRPLUS, /* 53 the minimizing one second. These codes must */ - OP_CRMINPLUS, /* 54 be in exactly the same order as those above. */ - OP_CRQUERY, /* 55 These are for character classes and back refs */ - OP_CRMINQUERY, /* 56 */ - OP_CRRANGE, /* 57 These are different to the three sets above. */ - OP_CRMINRANGE, /* 58 */ - - OP_CLASS, /* 59 Match a character class, chars < 256 only */ - OP_NCLASS, /* 60 Same, but the bitmap was created from a negative + OP_ANYNL, /* 15 \R (any newline sequence) */ + OP_EXTUNI, /* 16 \X (extended Unicode sequence */ + OP_EODN, /* 17 End of data or \n at end of data: \Z. */ + OP_EOD, /* 18 End of data: \z */ + + OP_OPT, /* 19 Set runtime options */ + OP_CIRC, /* 20 Start of line - varies with multiline switch */ + OP_DOLL, /* 21 End of line - varies with multiline switch */ + OP_CHAR, /* 22 Match one character, casefully */ + OP_CHARNC, /* 23 Match one character, caselessly */ + OP_NOT, /* 24 Match one character, not the following one */ + + OP_STAR, /* 25 The maximizing and minimizing versions of */ + OP_MINSTAR, /* 26 these six opcodes must come in pairs, with */ + OP_PLUS, /* 27 the minimizing one second. */ + OP_MINPLUS, /* 28 This first set applies to single characters.*/ + OP_QUERY, /* 29 */ + OP_MINQUERY, /* 30 */ + + OP_UPTO, /* 31 From 0 to n matches */ + OP_MINUPTO, /* 32 */ + OP_EXACT, /* 33 Exactly n matches */ + + OP_POSSTAR, /* 34 Possessified star */ + OP_POSPLUS, /* 35 Possessified plus */ + OP_POSQUERY, /* 36 Posesssified query */ + OP_POSUPTO, /* 37 Possessified upto */ + + OP_NOTSTAR, /* 38 The maximizing and minimizing versions of */ + OP_NOTMINSTAR, /* 39 these six opcodes must come in pairs, with */ + OP_NOTPLUS, /* 40 the minimizing one second. They must be in */ + OP_NOTMINPLUS, /* 41 exactly the same order as those above. */ + OP_NOTQUERY, /* 42 This set applies to "not" single characters. */ + OP_NOTMINQUERY, /* 43 */ + + OP_NOTUPTO, /* 44 From 0 to n matches */ + OP_NOTMINUPTO, /* 45 */ + OP_NOTEXACT, /* 46 Exactly n matches */ + + OP_NOTPOSSTAR, /* 47 Possessified versions */ + OP_NOTPOSPLUS, /* 48 */ + OP_NOTPOSQUERY, /* 49 */ + OP_NOTPOSUPTO, /* 50 */ + + OP_TYPESTAR, /* 51 The maximizing and minimizing versions of */ + OP_TYPEMINSTAR, /* 52 these six opcodes must come in pairs, with */ + OP_TYPEPLUS, /* 53 the minimizing one second. These codes must */ + OP_TYPEMINPLUS, /* 54 be in exactly the same order as those above. */ + OP_TYPEQUERY, /* 55 This set applies to character types such as \d */ + OP_TYPEMINQUERY, /* 56 */ + + OP_TYPEUPTO, /* 57 From 0 to n matches */ + OP_TYPEMINUPTO, /* 58 */ + OP_TYPEEXACT, /* 59 Exactly n matches */ + + OP_TYPEPOSSTAR, /* 60 Possessified versions */ + OP_TYPEPOSPLUS, /* 61 */ + OP_TYPEPOSQUERY, /* 62 */ + OP_TYPEPOSUPTO, /* 63 */ + + OP_CRSTAR, /* 64 The maximizing and minimizing versions of */ + OP_CRMINSTAR, /* 65 all these opcodes must come in pairs, with */ + OP_CRPLUS, /* 66 the minimizing one second. These codes must */ + OP_CRMINPLUS, /* 67 be in exactly the same order as those above. */ + OP_CRQUERY, /* 68 These are for character classes and back refs */ + OP_CRMINQUERY, /* 69 */ + OP_CRRANGE, /* 70 These are different to the three sets above. */ + OP_CRMINRANGE, /* 71 */ + + OP_CLASS, /* 72 Match a character class, chars < 256 only */ + OP_NCLASS, /* 73 Same, but the bitmap was created from a negative class - the difference is relevant only when a UTF-8 character > 255 is encountered. */ - OP_XCLASS, /* 61 Extended class for handling UTF-8 chars within the + OP_XCLASS, /* 74 Extended class for handling UTF-8 chars within the class. This does both positive and negative. */ - OP_REF, /* 62 Match a back reference */ - OP_RECURSE, /* 63 Match a numbered subpattern (possibly recursive) */ - OP_CALLOUT, /* 64 Call out to external function if provided */ - - OP_ALT, /* 65 Start of alternation */ - OP_KET, /* 66 End of group that doesn't have an unbounded repeat */ - OP_KETRMAX, /* 67 These two must remain together and in this */ - OP_KETRMIN, /* 68 order. They are for groups the repeat for ever. */ + OP_REF, /* 75 Match a back reference */ + OP_RECURSE, /* 76 Match a numbered subpattern (possibly recursive) */ + OP_CALLOUT, /* 77 Call out to external function if provided */ - /* The assertions must come before ONCE and COND */ + OP_ALT, /* 78 Start of alternation */ + OP_KET, /* 79 End of group that doesn't have an unbounded repeat */ + OP_KETRMAX, /* 80 These two must remain together and in this */ + OP_KETRMIN, /* 81 order. They are for groups the repeat for ever. */ - OP_ASSERT, /* 69 Positive lookahead */ - OP_ASSERT_NOT, /* 70 Negative lookahead */ - OP_ASSERTBACK, /* 71 Positive lookbehind */ - OP_ASSERTBACK_NOT, /* 72 Negative lookbehind */ - OP_REVERSE, /* 73 Move pointer back - used in lookbehind assertions */ + /* The assertions must come before BRA, CBRA, ONCE, and COND.*/ - /* ONCE and COND must come after the assertions, with ONCE first, as there's - a test for >= ONCE for a subpattern that isn't an assertion. */ + OP_ASSERT, /* 82 Positive lookahead */ + OP_ASSERT_NOT, /* 83 Negative lookahead */ + OP_ASSERTBACK, /* 84 Positive lookbehind */ + OP_ASSERTBACK_NOT, /* 85 Negative lookbehind */ + OP_REVERSE, /* 86 Move pointer back - used in lookbehind assertions */ - OP_ONCE, /* 74 Once matched, don't back up into the subpattern */ - OP_COND, /* 75 Conditional group */ - OP_CREF, /* 76 Used to hold an extraction string number (cond ref) */ + /* ONCE, BRA, CBRA, and COND must come after the assertions, with ONCE first, + as there's a test for >= ONCE for a subpattern that isn't an assertion. */ - OP_BRAZERO, /* 77 These two must remain together and in this */ - OP_BRAMINZERO, /* 78 order. */ + OP_ONCE, /* 87 Atomic group */ + OP_BRA, /* 88 Start of non-capturing bracket */ + OP_CBRA, /* 89 Start of capturing bracket */ + OP_COND, /* 90 Conditional group */ - OP_BRANUMBER, /* 79 Used for extracting brackets whose number is greater - than can fit into an opcode. */ - - OP_BRA /* 80 This and greater values are used for brackets that - extract substrings up to EXTRACT_BASIC_MAX. After - that, use is made of OP_BRANUMBER. */ -}; + /* These three must follow the previous three, in the same order. There's a + check for >= SBRA to distinguish the two sets. */ -/* WARNING WARNING WARNING: There is an implicit assumption in pcre.c and -study.c that all opcodes are less than 128 in value. This makes handling UTF-8 -character sequences easier. */ + OP_SBRA, /* 91 Start of non-capturing bracket, check empty */ + OP_SCBRA, /* 92 Start of capturing bracket, check empty */ + OP_SCOND, /* 93 Conditional group, check empty */ -/* The highest extraction number before we have to start using additional -bytes. (Originally PCRE didn't have support for extraction counts highter than -this number.) The value is limited by the number of opcodes left after OP_BRA, -i.e. 255 - OP_BRA. We actually set it a bit lower to leave room for additional -opcodes. */ + OP_CREF, /* 94 Used to hold a capture number as condition */ + OP_RREF, /* 95 Used to hold a recursion number as condition */ + OP_DEF, /* 96 The DEFINE condition */ -#define EXTRACT_BASIC_MAX 100 + OP_BRAZERO, /* 97 These two must remain together and in this */ + OP_BRAMINZERO /* 98 order. */ +}; /* This macro defines textual names for all the opcodes. These are used only @@ -646,17 +703,21 @@ for debugging. The macro is referenced only in pcre_printint.c. */ #define OP_NAME_LIST \ "End", "\\A", "\\G", "\\B", "\\b", "\\D", "\\d", \ "\\S", "\\s", "\\W", "\\w", "Any", "Anybyte", \ - "notprop", "prop", "extuni", \ + "notprop", "prop", "anynl", "extuni", \ "\\Z", "\\z", \ "Opt", "^", "$", "char", "charnc", "not", \ "*", "*?", "+", "+?", "?", "??", "{", "{", "{", \ + "*+","++", "?+", "{", \ "*", "*?", "+", "+?", "?", "??", "{", "{", "{", \ + "*+","++", "?+", "{", \ "*", "*?", "+", "+?", "?", "??", "{", "{", "{", \ + "*+","++", "?+", "{", \ "*", "*?", "+", "+?", "?", "??", "{", "{", \ "class", "nclass", "xclass", "Ref", "Recurse", "Callout", \ "Alt", "Ket", "KetRmax", "KetRmin", "Assert", "Assert not", \ - "AssertB", "AssertB not", "Reverse", "Once", "Cond", "Cond ref",\ - "Brazero", "Braminzero", "Branumber", "Bra" + "AssertB", "AssertB not", "Reverse", \ + "Once", "Bra 0", "Bra", "Cond", "SBra 0", "SBra", "SCond", \ + "Cond ref", "Cond rec", "Cond def", "Brazero", "Braminzero" /* This macro defines the length of fixed length operations in the compiled @@ -672,7 +733,7 @@ in UTF-8 mode. The code that uses this table must know about such things. */ 1, /* End */ \ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* \A, \G, \B, \B, \D, \d, \S, \s, \W, \w */ \ 1, 1, /* Any, Anybyte */ \ - 3, 3, 1, /* NOTPROP, PROP, EXTUNI */ \ + 3, 3, 1, 1, /* NOTPROP, PROP, EXTUNI, ANYNL */ \ 1, 1, 2, 1, 1, /* \Z, \z, Opt, ^, $ */ \ 2, /* Char - the minimum length */ \ 2, /* Charnc - the minimum length */ \ @@ -680,12 +741,15 @@ in UTF-8 mode. The code that uses this table must know about such things. */ /* Positive single-char repeats ** These are */ \ 2, 2, 2, 2, 2, 2, /* *, *?, +, +?, ?, ?? ** minima in */ \ 4, 4, 4, /* upto, minupto, exact ** UTF-8 mode */ \ + 2, 2, 2, 4, /* *+, ++, ?+, upto+ */ \ /* Negative single-char repeats - only for chars < 256 */ \ 2, 2, 2, 2, 2, 2, /* NOT *, *?, +, +?, ?, ?? */ \ 4, 4, 4, /* NOT upto, minupto, exact */ \ + 2, 2, 2, 4, /* Possessive *, +, ?, upto */ \ /* Positive type repeats */ \ 2, 2, 2, 2, 2, 2, /* Type *, *?, +, +?, ?, ?? */ \ 4, 4, 4, /* Type upto, minupto, exact */ \ + 2, 2, 2, 4, /* Possessive *+, ++, ?+, upto+ */ \ /* Character class & ref repeats */ \ 1, 1, 1, 1, 1, 1, /* *, *?, +, +?, ?, ?? */ \ 5, 5, /* CRRANGE, CRMINRANGE */ \ @@ -704,17 +768,22 @@ in UTF-8 mode. The code that uses this table must know about such things. */ 1+LINK_SIZE, /* Assert behind */ \ 1+LINK_SIZE, /* Assert behind not */ \ 1+LINK_SIZE, /* Reverse */ \ - 1+LINK_SIZE, /* Once */ \ + 1+LINK_SIZE, /* ONCE */ \ + 1+LINK_SIZE, /* BRA */ \ + 3+LINK_SIZE, /* CBRA */ \ 1+LINK_SIZE, /* COND */ \ + 1+LINK_SIZE, /* SBRA */ \ + 3+LINK_SIZE, /* SCBRA */ \ + 1+LINK_SIZE, /* SCOND */ \ 3, /* CREF */ \ + 3, /* RREF */ \ + 1, /* DEF */ \ 1, 1, /* BRAZERO, BRAMINZERO */ \ - 3, /* BRANUMBER */ \ - 1+LINK_SIZE /* BRA */ \ -/* A magic value for OP_CREF to indicate the "in recursion" condition. */ +/* A magic value for OP_RREF to indicate the "any recursion" condition. */ -#define CREF_RECURSE 0xffff +#define RREF_ANY 0xffff /* Error code numbers. They are given names so that they can more easily be tracked. */ @@ -724,7 +793,7 @@ enum { ERR0, ERR1, ERR2, ERR3, ERR4, ERR5, ERR6, ERR7, ERR8, ERR9, ERR20, ERR21, ERR22, ERR23, ERR24, ERR25, ERR26, ERR27, ERR28, ERR29, ERR30, ERR31, ERR32, ERR33, ERR34, ERR35, ERR36, ERR37, ERR38, ERR39, ERR40, ERR41, ERR42, ERR43, ERR44, ERR45, ERR46, ERR47, ERR48, ERR49, - ERR50, ERR51 }; + ERR50, ERR51, ERR52, ERR53, ERR54, ERR55, ERR56, ERR57 }; /* The real format of the start of the pcre block; the index of names and the code vector run on as long as necessary after the end. We store an explicit @@ -779,17 +848,23 @@ typedef struct compile_data { const uschar *fcc; /* Points to case-flipping table */ const uschar *cbits; /* Points to character type table */ const uschar *ctypes; /* Points to table of type maps */ + const uschar *start_workspace;/* The start of working space */ const uschar *start_code; /* The start of the compiled code */ const uschar *start_pattern; /* The start of the pattern */ + const uschar *end_pattern; /* The end of the pattern */ + uschar *hwm; /* High watermark of workspace */ uschar *name_table; /* The name/number table */ int names_found; /* Number of entries so far */ int name_entry_size; /* Size of each entry */ + int bracount; /* Count of capturing parens */ int top_backref; /* Maximum back reference */ unsigned int backref_map; /* Bitmap of low back refs */ + int external_options; /* External (initial) options */ int req_varyopt; /* "After variable item" flag for reqbyte */ BOOL nopartial; /* Set TRUE if partial won't work */ - int nllen; /* 1 or 2 for newline string length */ - uschar nl[4]; /* Newline string */ + int nltype; /* Newline type */ + int nllen; /* Newline string length */ + uschar nl[4]; /* Newline string when fixed length */ } compile_data; /* Structure for maintaining a chain of pointers to the currently incomplete @@ -822,6 +897,16 @@ This isn't used for a "normal" compilation of pcre. */ struct heapframe; +/* Structure for building a chain of data for holding the values of the subject +pointer at the start of each subpattern, so as to detect when an empty string +has been matched by a subpattern - to break infinite loops. */ + +typedef struct eptrblock { + struct eptrblock *epb_prev; + USPTR epb_saved_eptr; +} eptrblock; + + /* Structure for passing "static" information around between the functions doing traditional NFA matching, so that they are thread-safe. */ @@ -832,8 +917,9 @@ typedef struct match_data { int *offset_vector; /* Offset vector */ int offset_end; /* One past the end */ int offset_max; /* The maximum usable for return data */ - int nllen; /* 1 or 2 for newline string length */ - uschar nl[4]; /* Newline string */ + int nltype; /* Newline type */ + int nllen; /* Newline string length */ + uschar nl[4]; /* Newline string when fixed */ const uschar *lcc; /* Points to lower casing table */ const uschar *ctypes; /* Points to table of type maps */ BOOL offset_overflow; /* Set if too many extractions */ @@ -852,6 +938,8 @@ typedef struct match_data { int end_offset_top; /* Highwater mark at end of match */ int capture_last; /* Most recent capture number */ int start_offset; /* The start offset value */ + eptrblock *eptrchain; /* Chain of eptrblocks for tail recursions */ + int eptrn; /* Next free eptrblock */ recursion_info *recursive; /* Linked list of recursion data */ void *callout_data; /* To pass back to callouts */ struct heapframe *thisframe; /* Used only when compiling for no recursion */ @@ -867,8 +955,9 @@ typedef struct dfa_match_data { const uschar *tables; /* Character tables */ int moptions; /* Match options */ int poptions; /* Pattern options */ - int nllen; /* 1 or 2 for newline string length */ - uschar nl[4]; /* Newline string */ + int nltype; /* Newline type */ + int nllen; /* Newline string length */ + uschar nl[4]; /* Newline string when fixed */ void *callout_data; /* To pass back to callouts */ } dfa_match_data; @@ -939,13 +1028,17 @@ extern const uschar _pcre_OP_lengths[]; one of the exported public functions. They have to be "external" in the C sense, but are not part of the PCRE public API. */ -extern int _pcre_ord2utf8(int, uschar *); -extern real_pcre * _pcre_try_flipped(const real_pcre *, real_pcre *, - const pcre_study_data *, pcre_study_data *); -extern int _pcre_ucp_findprop(const unsigned int, int *, int *); -extern int _pcre_ucp_othercase(const int); -extern int _pcre_valid_utf8(const uschar *, int); -extern BOOL _pcre_xclass(int, const uschar *); +extern BOOL _pcre_is_newline(const uschar *, const uschar *, int *, + BOOL); +extern int _pcre_ord2utf8(int, uschar *); +extern real_pcre *_pcre_try_flipped(const real_pcre *, real_pcre *, + const pcre_study_data *, pcre_study_data *); +extern int _pcre_ucp_findprop(const unsigned int, int *, int *); +extern unsigned int _pcre_ucp_othercase(const unsigned int); +extern int _pcre_valid_utf8(const uschar *, int); +extern BOOL _pcre_was_newline(const uschar *, const uschar *, int *, + BOOL); +extern BOOL _pcre_xclass(int, const uschar *); #endif diff --git a/src/src/pcre/pcre_maketables.c b/src/src/pcre/pcre_maketables.c index 3f6ec8415..b78019efc 100644 --- a/src/src/pcre/pcre_maketables.c +++ b/src/src/pcre/pcre_maketables.c @@ -1,4 +1,4 @@ -/* $Cambridge: exim/src/src/pcre/pcre_maketables.c,v 1.3 2006/11/07 16:50:36 ph10 Exp $ */ +/* $Cambridge: exim/src/src/pcre/pcre_maketables.c,v 1.4 2007/01/23 15:08:45 ph10 Exp $ */ /************************************************* * Perl-Compatible Regular Expressions * @@ -132,7 +132,7 @@ for (i = 0; i < 256; i++) meta-character, which in this sense is any character that terminates a run of data characters. */ - if (strchr("*+?{^.$|()[", i) != 0) x += ctype_meta; + if (strchr("\\*+?{^.$|()[", i) != 0) x += ctype_meta; *p++ = x; } diff --git a/src/src/pcre/pcre_newline.c b/src/src/pcre/pcre_newline.c new file mode 100644 index 000000000..26cf0fe1f --- /dev/null +++ b/src/src/pcre/pcre_newline.c @@ -0,0 +1,137 @@ +/* $Cambridge: exim/src/src/pcre/pcre_newline.c,v 1.1 2007/01/23 15:08:45 ph10 Exp $ */ + +/************************************************* +* Perl-Compatible Regular Expressions * +*************************************************/ + +/* PCRE is a library of functions to support regular expressions whose syntax +and semantics are as close as possible to those of the Perl 5 language. + + Written by Philip Hazel + Copyright (c) 1997-2006 University of Cambridge + +----------------------------------------------------------------------------- +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + * Neither the name of the University of Cambridge nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. +----------------------------------------------------------------------------- +*/ + + +/* This module contains internal functions for testing newlines when more than +one kind of newline is to be recognized. When a newline is found, its length is +returned. In principle, we could implement several newline "types", each +referring to a different set of newline characters. At present, PCRE supports +only NLTYPE_FIXED, which gets handled without these functions, and NLTYPE_ALL, +so for now the type isn't passed into the functions. It can easily be added +later if required. The full list of Unicode newline characters is taken from +http://unicode.org/unicode/reports/tr18/. */ + + +#include "pcre_internal.h" + + + +/************************************************* +* Check for newline at given position * +*************************************************/ + +/* It is guaranteed that the initial value of ptr is less than the end of the +string that is being processed. + +Arguments: + ptr pointer to possible newline + endptr pointer to the end of the string + lenptr where to return the length + utf8 TRUE if in utf8 mode + +Returns: TRUE or FALSE +*/ + +BOOL +_pcre_is_newline(const uschar *ptr, const uschar *endptr, int *lenptr, + BOOL utf8) +{ +int c; +if (utf8) { GETCHAR(c, ptr); } else c = *ptr; +switch(c) + { + case 0x000a: /* LF */ + case 0x000b: /* VT */ + case 0x000c: *lenptr = 1; return TRUE; /* FF */ + case 0x000d: *lenptr = (ptr < endptr - 1 && ptr[1] == 0x0a)? 2 : 1; + return TRUE; /* CR */ + case 0x0085: *lenptr = utf8? 2 : 1; return TRUE; /* NEL */ + case 0x2028: /* LS */ + case 0x2029: *lenptr = 3; return TRUE; /* PS */ + default: return FALSE; + } +} + + + +/************************************************* +* Check for newline at previous position * +*************************************************/ + +/* It is guaranteed that the initial value of ptr is greater than the start of +the string that is being processed. + +Arguments: + ptr pointer to possible newline + startptr pointer to the start of the string + lenptr where to return the length + utf8 TRUE if in utf8 mode + +Returns: TRUE or FALSE +*/ + +BOOL +_pcre_was_newline(const uschar *ptr, const uschar *startptr, int *lenptr, + BOOL utf8) +{ +int c; +ptr--; +if (utf8) + { + BACKCHAR(ptr); + GETCHAR(c, ptr); + } +else c = *ptr; +switch(c) + { + case 0x000a: *lenptr = (ptr > startptr && ptr[-1] == 0x0d)? 2 : 1; + return TRUE; /* LF */ + case 0x000b: /* VT */ + case 0x000c: /* FF */ + case 0x000d: *lenptr = 1; return TRUE; /* CR */ + case 0x0085: *lenptr = utf8? 2 : 1; return TRUE; /* NEL */ + case 0x2028: /* LS */ + case 0x2029: *lenptr = 3; return TRUE; /* PS */ + default: return FALSE; + } +} + +/* End of pcre_newline.c */ diff --git a/src/src/pcre/pcre_printint.src b/src/src/pcre/pcre_printint.src index 4144adfb2..e5407f81b 100644 --- a/src/src/pcre/pcre_printint.src +++ b/src/src/pcre/pcre_printint.src @@ -1,4 +1,4 @@ -/* $Cambridge: exim/src/src/pcre/pcre_printint.src,v 1.1 2006/11/07 16:50:36 ph10 Exp $ */ +/* $Cambridge: exim/src/src/pcre/pcre_printint.src,v 1.2 2007/01/23 15:08:45 ph10 Exp $ */ /************************************************* * Perl-Compatible Regular Expressions * @@ -51,9 +51,19 @@ local functions. This source file is used in two places: compiled regex for debugging purposes. */ +/* Macro that decides whether a character should be output as a literal or in +hexadecimal. We don't use isprint() because that can vary from system to system +(even without the use of locales) and we want the output always to be the same, +for testing purposes. This macro is used in pcretest as well as in this file. */ + +#define PRINTABLE(c) ((c) >= 32 && (c) < 127) + +/* The table of operator names. */ + static const char *OP_names[] = { OP_NAME_LIST }; + /************************************************* * Print single- or multi-byte character * *************************************************/ @@ -65,7 +75,7 @@ int c = *ptr; if (!utf8 || (c & 0xc0) != 0xc0) { - if (isprint(c)) fprintf(f, "%c", c); else fprintf(f, "\\x%02x", c); + if (PRINTABLE(c)) fprintf(f, "%c", c); else fprintf(f, "\\x%02x", c); return 0; } else @@ -162,16 +172,6 @@ for(;;) fprintf(f, "%3d ", (int)(code - codestart)); - if (*code >= OP_BRA) - { - if (*code - OP_BRA > EXTRACT_BASIC_MAX) - fprintf(f, "%3d Bra extra\n", GET(code, 1)); - else - fprintf(f, "%3d Bra %d\n", GET(code, 1), *code - OP_BRA); - code += _pcre_OP_lengths[OP_BRA]; - continue; - } - switch(*code) { case OP_END: @@ -205,6 +205,14 @@ for(;;) fprintf(f, "\n"); continue; + case OP_CBRA: + case OP_SCBRA: + fprintf(f, "%3d %s %d", GET(code, 1), OP_names[*code], + GET2(code, 1+LINK_SIZE)); + break; + + case OP_BRA: + case OP_SBRA: case OP_KETRMAX: case OP_KETRMIN: case OP_ALT: @@ -215,33 +223,45 @@ for(;;) case OP_ASSERTBACK_NOT: case OP_ONCE: case OP_COND: + case OP_SCOND: case OP_REVERSE: fprintf(f, "%3d %s", GET(code, 1), OP_names[*code]); break; - case OP_BRANUMBER: - printf("%3d %s", GET2(code, 1), OP_names[*code]); + case OP_CREF: + fprintf(f, "%3d %s", GET2(code,1), OP_names[*code]); break; - case OP_CREF: - if (GET2(code, 1) == CREF_RECURSE) - fprintf(f, " Cond recurse"); + case OP_RREF: + c = GET2(code, 1); + if (c == RREF_ANY) + fprintf(f, " Cond recurse any"); else - fprintf(f, "%3d %s", GET2(code,1), OP_names[*code]); + fprintf(f, " Cond recurse %d", c); + break; + + case OP_DEF: + fprintf(f, " Cond def"); break; case OP_STAR: case OP_MINSTAR: + case OP_POSSTAR: case OP_PLUS: case OP_MINPLUS: + case OP_POSPLUS: case OP_QUERY: case OP_MINQUERY: + case OP_POSQUERY: case OP_TYPESTAR: case OP_TYPEMINSTAR: + case OP_TYPEPOSSTAR: case OP_TYPEPLUS: case OP_TYPEMINPLUS: + case OP_TYPEPOSPLUS: case OP_TYPEQUERY: case OP_TYPEMINQUERY: + case OP_TYPEPOSQUERY: fprintf(f, " "); if (*code >= OP_TYPESTAR) { @@ -259,17 +279,20 @@ for(;;) case OP_EXACT: case OP_UPTO: case OP_MINUPTO: + case OP_POSUPTO: fprintf(f, " "); extra = print_char(f, code+3, utf8); fprintf(f, "{"); - if (*code != OP_EXACT) fprintf(f, ","); + if (*code != OP_EXACT) fprintf(f, "0,"); fprintf(f, "%d}", GET2(code,1)); if (*code == OP_MINUPTO) fprintf(f, "?"); + else if (*code == OP_POSUPTO) fprintf(f, "+"); break; case OP_TYPEEXACT: case OP_TYPEUPTO: case OP_TYPEMINUPTO: + case OP_TYPEPOSUPTO: fprintf(f, " %s", OP_names[code[3]]); if (code[3] == OP_PROP || code[3] == OP_NOTPROP) { @@ -280,20 +303,26 @@ for(;;) if (*code != OP_TYPEEXACT) fprintf(f, "0,"); fprintf(f, "%d}", GET2(code,1)); if (*code == OP_TYPEMINUPTO) fprintf(f, "?"); + else if (*code == OP_TYPEPOSUPTO) fprintf(f, "+"); break; case OP_NOT: - if (isprint(c = code[1])) fprintf(f, " [^%c]", c); + c = code[1]; + if (PRINTABLE(c)) fprintf(f, " [^%c]", c); else fprintf(f, " [^\\x%02x]", c); break; case OP_NOTSTAR: case OP_NOTMINSTAR: + case OP_NOTPOSSTAR: case OP_NOTPLUS: case OP_NOTMINPLUS: + case OP_NOTPOSPLUS: case OP_NOTQUERY: case OP_NOTMINQUERY: - if (isprint(c = code[1])) fprintf(f, " [^%c]", c); + case OP_NOTPOSQUERY: + c = code[1]; + if (PRINTABLE(c)) fprintf(f, " [^%c]", c); else fprintf(f, " [^\\x%02x]", c); fprintf(f, "%s", OP_names[*code]); break; @@ -301,11 +330,14 @@ for(;;) case OP_NOTEXACT: case OP_NOTUPTO: case OP_NOTMINUPTO: - if (isprint(c = code[3])) fprintf(f, " [^%c]{", c); + case OP_NOTPOSUPTO: + c = code[3]; + if (PRINTABLE(c)) fprintf(f, " [^%c]{", c); else fprintf(f, " [^\\x%02x]{", c); if (*code != OP_NOTEXACT) fprintf(f, "0,"); fprintf(f, "%d}", GET2(code,1)); if (*code == OP_NOTMINUPTO) fprintf(f, "?"); + else if (*code == OP_NOTPOSUPTO) fprintf(f, "+"); break; case OP_RECURSE: @@ -365,12 +397,14 @@ for(;;) for (j = i+1; j < 256; j++) if ((ccode[j/8] & (1 << (j&7))) == 0) break; if (i == '-' || i == ']') fprintf(f, "\\"); - if (isprint(i)) fprintf(f, "%c", i); else fprintf(f, "\\x%02x", i); + if (PRINTABLE(i)) fprintf(f, "%c", i); + else fprintf(f, "\\x%02x", i); if (--j > i) { if (j != i + 1) fprintf(f, "-"); if (j == '-' || j == ']') fprintf(f, "\\"); - if (isprint(j)) fprintf(f, "%c", j); else fprintf(f, "\\x%02x", j); + if (PRINTABLE(j)) fprintf(f, "%c", j); + else fprintf(f, "\\x%02x", j); } i = j; } diff --git a/src/src/pcre/pcre_study.c b/src/src/pcre/pcre_study.c index 62990940a..ae312f272 100644 --- a/src/src/pcre/pcre_study.c +++ b/src/src/pcre/pcre_study.c @@ -1,4 +1,4 @@ -/* $Cambridge: exim/src/src/pcre/pcre_study.c,v 1.3 2006/11/07 16:50:36 ph10 Exp $ */ +/* $Cambridge: exim/src/src/pcre/pcre_study.c,v 1.4 2007/01/23 15:08:45 ph10 Exp $ */ /************************************************* * Perl-Compatible Regular Expressions * @@ -47,6 +47,11 @@ supporting functions. */ #include "pcre_internal.h" +/* Returns from set_start_bits() */ + +enum { SSB_FAIL, SSB_DONE, SSB_CONTINUE }; + + /************************************************* * Set a bit and maybe its alternate case * *************************************************/ @@ -74,12 +79,16 @@ if (caseless && (cd->ctypes[c] & ctype_letter) != 0) /************************************************* -* Create bitmap of starting chars * +* Create bitmap of starting bytes * *************************************************/ -/* This function scans a compiled unanchored expression and attempts to build a -bitmap of the set of initial characters. If it can't, it returns FALSE. As time -goes by, we may be able to get more clever at doing this. +/* This function scans a compiled unanchored expression recursively and +attempts to build a bitmap of the set of possible starting bytes. As time goes +by, we may be able to get more clever at doing this. The SSB_CONTINUE return is +useful for parenthesized groups in patterns such as (a*)b where the group +provides some optional starting bytes but scanning must continue at the outer +level to find at least one mandatory byte. At the outermost level, this +function fails unless the result is SSB_DONE. Arguments: code points to an expression @@ -88,14 +97,17 @@ Arguments: utf8 TRUE if in UTF-8 mode cd the block with char table pointers -Returns: TRUE if table built, FALSE otherwise +Returns: SSB_FAIL => Failed to find any starting bytes + SSB_DONE => Found mandatory starting bytes + SSB_CONTINUE => Found optional starting bytes */ -static BOOL +static int set_start_bits(const uschar *code, uschar *start_bits, BOOL caseless, BOOL utf8, compile_data *cd) { register int c; +int yield = SSB_DONE; #if 0 /* ========================================================================= */ @@ -116,36 +128,60 @@ volatile int dummy; do { - const uschar *tcode = code + 1 + LINK_SIZE; + const uschar *tcode = code + (((int)*code == OP_CBRA)? 3:1) + LINK_SIZE; BOOL try_next = TRUE; - while (try_next) + while (try_next) /* Loop for items in this branch */ { - /* If a branch starts with a bracket or a positive lookahead assertion, - recurse to set bits from within them. That's all for this branch. */ - - if ((int)*tcode >= OP_BRA || *tcode == OP_ASSERT) + int rc; + switch(*tcode) { - if (!set_start_bits(tcode, start_bits, caseless, utf8, cd)) - return FALSE; - try_next = FALSE; - } + /* Fail if we reach something we don't understand */ - else switch(*tcode) - { default: - return FALSE; + return SSB_FAIL; + + /* If we hit a bracket or a positive lookahead assertion, recurse to set + bits from within the subpattern. If it can't find anything, we have to + give up. If it finds some mandatory character(s), we are done for this + branch. Otherwise, carry on scanning after the subpattern. */ + + case OP_BRA: + case OP_SBRA: + case OP_CBRA: + case OP_SCBRA: + case OP_ONCE: + case OP_ASSERT: + rc = set_start_bits(tcode, start_bits, caseless, utf8, cd); + if (rc == SSB_FAIL) return SSB_FAIL; + if (rc == SSB_DONE) try_next = FALSE; else + { + do tcode += GET(tcode, 1); while (*tcode == OP_ALT); + tcode += 1 + LINK_SIZE; + } + break; - /* Skip over callout */ + /* If we hit ALT or KET, it means we haven't found anything mandatory in + this branch, though we might have found something optional. For ALT, we + continue with the next alternative, but we have to arrange that the final + result from subpattern is SSB_CONTINUE rather than SSB_DONE. For KET, + return SSB_CONTINUE: if this is the top level, that indicates failure, + but after a nested subpattern, it causes scanning to continue. */ - case OP_CALLOUT: - tcode += 2 + 2*LINK_SIZE; + case OP_ALT: + yield = SSB_CONTINUE; + try_next = FALSE; break; - /* Skip over extended extraction bracket number */ + case OP_KET: + case OP_KETRMAX: + case OP_KETRMIN: + return SSB_CONTINUE; - case OP_BRANUMBER: - tcode += 3; + /* Skip over callout */ + + case OP_CALLOUT: + tcode += 2 + 2*LINK_SIZE; break; /* Skip over lookbehind and negative lookahead assertions */ @@ -154,7 +190,7 @@ do case OP_ASSERTBACK: case OP_ASSERTBACK_NOT: do tcode += GET(tcode, 1); while (*tcode == OP_ALT); - tcode += 1+LINK_SIZE; + tcode += 1 + LINK_SIZE; break; /* Skip over an option setting, changing the caseless flag */ @@ -168,27 +204,30 @@ do case OP_BRAZERO: case OP_BRAMINZERO: - if (!set_start_bits(++tcode, start_bits, caseless, utf8, cd)) - return FALSE; + if (set_start_bits(++tcode, start_bits, caseless, utf8, cd) == SSB_FAIL) + return SSB_FAIL; /* ========================================================================= See the comment at the head of this function concerning the next line, which was an old fudge for the benefit of OS/2. dummy = 1; ========================================================================= */ do tcode += GET(tcode,1); while (*tcode == OP_ALT); - tcode += 1+LINK_SIZE; + tcode += 1 + LINK_SIZE; break; /* Single-char * or ? sets the bit and tries the next item */ case OP_STAR: case OP_MINSTAR: + case OP_POSSTAR: case OP_QUERY: case OP_MINQUERY: + case OP_POSQUERY: set_bit(start_bits, tcode[1], caseless, cd); tcode += 2; #ifdef SUPPORT_UTF8 - if (utf8) while ((*tcode & 0xc0) == 0x80) tcode++; + if (utf8 && tcode[-1] >= 0xc0) + tcode += _pcre_utf8_table4[tcode[-1] & 0x3f]; #endif break; @@ -196,10 +235,12 @@ do case OP_UPTO: case OP_MINUPTO: + case OP_POSUPTO: set_bit(start_bits, tcode[3], caseless, cd); tcode += 4; #ifdef SUPPORT_UTF8 - if (utf8) while ((*tcode & 0xc0) == 0x80) tcode++; + if (utf8 && tcode[-1] >= 0xc0) + tcode += _pcre_utf8_table4[tcode[-1] & 0x3f]; #endif break; @@ -212,6 +253,7 @@ do case OP_CHARNC: case OP_PLUS: case OP_MINPLUS: + case OP_POSPLUS: set_bit(start_bits, tcode[1], caseless, cd); try_next = FALSE; break; @@ -285,16 +327,19 @@ do case OP_TYPEUPTO: case OP_TYPEMINUPTO: + case OP_TYPEPOSUPTO: tcode += 2; /* Fall through */ case OP_TYPESTAR: case OP_TYPEMINSTAR: + case OP_TYPEPOSSTAR: case OP_TYPEQUERY: case OP_TYPEMINQUERY: + case OP_TYPEPOSQUERY: switch(tcode[1]) { case OP_ANY: - return FALSE; + return SSB_FAIL; case OP_NOT_DIGIT: for (c = 0; c < 32; c++) @@ -420,7 +465,7 @@ do code += GET(code, 1); /* Advance to next branch */ } while (*code == OP_ALT); -return TRUE; +return yield; } @@ -494,8 +539,8 @@ compile_block.ctypes = tables + ctypes_offset; /* See if we can find a fixed set of initial characters for the pattern. */ memset(start_bits, 0, 32 * sizeof(uschar)); -if (!set_start_bits(code, start_bits, (re->options & PCRE_CASELESS) != 0, - (re->options & PCRE_UTF8) != 0, &compile_block)) return NULL; +if (set_start_bits(code, start_bits, (re->options & PCRE_CASELESS) != 0, + (re->options & PCRE_UTF8) != 0, &compile_block) != SSB_DONE) return NULL; /* Get a pcre_extra block and a pcre_study_data block. The study data is put in the latter, which is pointed to by the former, which may also get additional diff --git a/src/src/pcre/pcre_tables.c b/src/src/pcre/pcre_tables.c index 0cb530a0b..1f4649f76 100644 --- a/src/src/pcre/pcre_tables.c +++ b/src/src/pcre/pcre_tables.c @@ -1,4 +1,4 @@ -/* $Cambridge: exim/src/src/pcre/pcre_tables.c,v 1.3 2006/11/07 16:50:36 ph10 Exp $ */ +/* $Cambridge: exim/src/src/pcre/pcre_tables.c,v 1.4 2007/01/23 15:08:45 ph10 Exp $ */ /************************************************* * Perl-Compatible Regular Expressions * @@ -74,9 +74,8 @@ first byte of a character, indexed by the number of additional bytes. */ const int _pcre_utf8_table2[] = { 0, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc}; const int _pcre_utf8_table3[] = { 0xff, 0x1f, 0x0f, 0x07, 0x03, 0x01}; -/* Table of the number of extra characters, indexed by the first character -masked with 0x3f. The highest number for a valid UTF-8 character is in fact -0x3d. */ +/* Table of the number of extra bytes, indexed by the first byte masked with +0x3f. The highest number for a valid UTF-8 first byte is in fact 0x3d. */ const uschar _pcre_utf8_table4[] = { 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, @@ -91,6 +90,7 @@ const ucp_type_table _pcre_utt[] = { { "Any", PT_ANY, 0 }, { "Arabic", PT_SC, ucp_Arabic }, { "Armenian", PT_SC, ucp_Armenian }, + { "Balinese", PT_SC, ucp_Balinese }, { "Bengali", PT_SC, ucp_Bengali }, { "Bopomofo", PT_SC, ucp_Bopomofo }, { "Braille", PT_SC, ucp_Braille }, @@ -106,6 +106,7 @@ const ucp_type_table _pcre_utt[] = { { "Common", PT_SC, ucp_Common }, { "Coptic", PT_SC, ucp_Coptic }, { "Cs", PT_PC, ucp_Cs }, + { "Cuneiform", PT_SC, ucp_Cuneiform }, { "Cypriot", PT_SC, ucp_Cypriot }, { "Cyrillic", PT_SC, ucp_Cyrillic }, { "Deseret", PT_SC, ucp_Deseret }, @@ -148,6 +149,7 @@ const ucp_type_table _pcre_utt[] = { { "N", PT_GC, ucp_N }, { "Nd", PT_PC, ucp_Nd }, { "New_Tai_Lue", PT_SC, ucp_New_Tai_Lue }, + { "Nko", PT_SC, ucp_Nko }, { "Nl", PT_PC, ucp_Nl }, { "No", PT_PC, ucp_No }, { "Ogham", PT_SC, ucp_Ogham }, @@ -160,6 +162,8 @@ const ucp_type_table _pcre_utt[] = { { "Pd", PT_PC, ucp_Pd }, { "Pe", PT_PC, ucp_Pe }, { "Pf", PT_PC, ucp_Pf }, + { "Phags_Pa", PT_SC, ucp_Phags_Pa }, + { "Phoenician", PT_SC, ucp_Phoenician }, { "Pi", PT_PC, ucp_Pi }, { "Po", PT_PC, ucp_Po }, { "Ps", PT_PC, ucp_Ps }, diff --git a/src/src/pcre/pcre_try_flipped.c b/src/src/pcre/pcre_try_flipped.c index 6466f23e1..d0b652037 100644 --- a/src/src/pcre/pcre_try_flipped.c +++ b/src/src/pcre/pcre_try_flipped.c @@ -1,4 +1,4 @@ -/* $Cambridge: exim/src/src/pcre/pcre_try_flipped.c,v 1.3 2006/11/07 16:50:36 ph10 Exp $ */ +/* $Cambridge: exim/src/src/pcre/pcre_try_flipped.c,v 1.4 2007/01/23 15:08:45 ph10 Exp $ */ /************************************************* * Perl-Compatible Regular Expressions * diff --git a/src/src/pcre/pcre_version.c b/src/src/pcre/pcre_version.c index 47f889f1d..1a30fe014 100644 --- a/src/src/pcre/pcre_version.c +++ b/src/src/pcre/pcre_version.c @@ -1,4 +1,4 @@ -/* $Cambridge: exim/src/src/pcre/pcre_version.c,v 1.3 2006/11/07 16:50:36 ph10 Exp $ */ +/* $Cambridge: exim/src/src/pcre/pcre_version.c,v 1.4 2007/01/23 15:08:45 ph10 Exp $ */ /************************************************* * Perl-Compatible Regular Expressions * @@ -51,16 +51,38 @@ string that identifies the PCRE version that is in use. */ * Return version string * *************************************************/ +/* These macros are the standard way of turning unquoted text into C strings. +They allow macros like PCRE_MAJOR to be defined without quotes, which is +convenient for user programs that want to test its value. */ + #define STRING(a) # a #define XSTRING(s) STRING(s) +/* A problem turned up with PCRE_PRERELEASE, which is defined empty for +production releases. Originally, it was used naively in this code: + + return XSTRING(PCRE_MAJOR) + "." XSTRING(PCRE_MINOR) + XSTRING(PCRE_PRERELEASE) + " " XSTRING(PCRE_DATE); + +However, when PCRE_PRERELEASE is empty, this leads to an attempted expansion of +STRING(). The C standard states: "If (before argument substitution) any +argument consists of no preprocessing tokens, the behavior is undefined." It +turns out the gcc treats this case as a single empty string - which is what we +really want - but Visual C grumbles about the lack of an argument for the +macro. Unfortunately, both are within their rights. To cope with both ways of +handling this, I had resort to some messy hackery that does a test at run time. +I could find no way of detecting that a macro is defined as an empty string at +pre-processor time. This hack uses a standard trick for avoiding calling +the STRING macro with an empty argument when doing the test. */ + PCRE_DATA_SCOPE const char * pcre_version(void) { -return XSTRING(PCRE_MAJOR) - "." XSTRING(PCRE_MINOR) - XSTRING(PCRE_PRERELEASE) - " " XSTRING(PCRE_DATE); +return (XSTRING(Z PCRE_PRERELEASE)[1] == 0)? + XSTRING(PCRE_MAJOR.PCRE_MINOR PCRE_DATE) : + XSTRING(PCRE_MAJOR.PCRE_MINOR) XSTRING(PCRE_PRERELEASE PCRE_DATE); } /* End of pcre_version.c */ diff --git a/src/src/pcre/pcretest.c b/src/src/pcre/pcretest.c index 4b7664665..a572a9dfc 100644 --- a/src/src/pcre/pcretest.c +++ b/src/src/pcre/pcretest.c @@ -1,4 +1,4 @@ -/* $Cambridge: exim/src/src/pcre/pcretest.c,v 1.5 2006/12/19 14:38:38 ph10 Exp $ */ +/* $Cambridge: exim/src/src/pcre/pcretest.c,v 1.6 2007/01/23 15:08:45 ph10 Exp $ */ /************************************************* * PCRE testing program * @@ -46,11 +46,29 @@ POSSIBILITY OF SUCH DAMAGE. #include #include -#ifndef _WIN32 -#include -#include + +/* A number of things vary for Windows builds. Originally, pcretest opened its +input and output without "b"; then I was told that "b" was needed in some +environments, so it was added for release 5.0 to both the input and output. (It +makes no difference on Unix-like systems.) Later I was told that it is wrong +for the input on Windows. I've now abstracted the modes into two macros that +are set here, to make it easier to fiddle with them, and removed "b" from the +input mode under Windows. */ + +#if defined(_WIN32) || defined(WIN32) +#include /* For _setmode() */ +#include /* For _O_BINARY */ +#define INPUT_MODE "r" +#define OUTPUT_MODE "wb" + +#else +#include /* These two includes are needed */ +#include /* for setrlimit(). */ +#define INPUT_MODE "rb" +#define OUTPUT_MODE "wb" #endif + #define PCRE_SPY /* For Win32 build, import data, not export */ /* We include pcre_internal.h because we need the internal info for displaying @@ -77,10 +95,18 @@ symbols to prevent clashes. */ /* We also need the pcre_printint() function for printing out compiled patterns. This function is in a separate file so that it can be included in -pcre_compile.c when that module is compiled with debugging enabled. */ +pcre_compile.c when that module is compiled with debugging enabled. + +The definition of the macro PRINTABLE, which determines whether to print an +output character as-is or as a hex value when showing compiled patterns, is +contained in this file. We uses it here also, in cases when the locale has not +been explicitly changed, so as to get consistent output from systems that +differ in their output from isprint() even in the "C" locale. */ #include "pcre_printint.src" +#define PRINTHEX(c) (locale_set? isprint(c) : PRINTABLE(c)) + /* It is possible to compile this test program without including support for testing the POSIX interface, though this is not available via the standard @@ -106,6 +132,8 @@ function (define NOINFOCHECK). */ #endif #endif +/* This is the default loop count for timing. */ + #define LOOPREPEAT 500000 /* Static variables */ @@ -117,6 +145,7 @@ static int callout_extra; static int callout_fail_count; static int callout_fail_id; static int first_callout; +static int locale_set = 0; static int show_malloc; static int use_utf8; static size_t gotten_store; @@ -160,6 +189,7 @@ uschar *here = start; for (;;) { int rlen = buffer_size - (here - buffer); + if (rlen > 1000) { int dlen; @@ -216,7 +246,7 @@ return NULL; /* Control never gets here */ /* We don't use strtoul() because SunOS4 doesn't have it. Rather than mess around with conditional compilation, just do the job by hand. It is only used -for unpicking the -o argument, so just keep it simple. +for unpicking arguments, so just keep it simple. Arguments: str string to be converted @@ -360,16 +390,19 @@ while (length-- > 0) { length -= rc - 1; p += rc; - if (c < 256 && isprint(c)) + if (PRINTHEX(c)) { if (f != NULL) fprintf(f, "%c", c); yield++; } else { - int n; - if (f != NULL) fprintf(f, "\\x{%02x}%n", c, &n); - yield += n; + int n = 4; + if (f != NULL) fprintf(f, "\\x{%02x}", c); + yield += (n <= 0x000000ff)? 2 : + (n <= 0x00000fff)? 3 : + (n <= 0x0000ffff)? 4 : + (n <= 0x000fffff)? 5 : 6; } continue; } @@ -378,7 +411,8 @@ while (length-- > 0) /* Not UTF-8, or malformed UTF-8 */ - if (isprint(c = *(p++))) + c = *p++; + if (PRINTHEX(c)) { if (f != NULL) fprintf(f, "%c", c); yield++; @@ -621,7 +655,7 @@ return count; *************************************************/ /* This is used both at compile and run-time to check for escapes, where -xxx is LF, CR, or CRLF. Print a message and return 0 if there is no match. +xxx is LF, CR, CRLF, or ANY. Print a message and return 0 if there is no match. Arguments: p points after the leading '<' @@ -636,12 +670,45 @@ check_newline(uschar *p, FILE *f) if (strncmp((char *)p, "cr>", 3) == 0) return PCRE_NEWLINE_CR; if (strncmp((char *)p, "lf>", 3) == 0) return PCRE_NEWLINE_LF; if (strncmp((char *)p, "crlf>", 5) == 0) return PCRE_NEWLINE_CRLF; +if (strncmp((char *)p, "any>", 4) == 0) return PCRE_NEWLINE_ANY; fprintf(f, "Unknown newline type at: <%s\n", p); return 0; } +/************************************************* +* Usage function * +*************************************************/ + +static void +usage(void) +{ +printf("Usage: pcretest [options] [ []]\n"); +printf(" -b show compiled code (bytecode)\n"); +printf(" -C show PCRE compile-time options and exit\n"); +printf(" -d debug: show compiled code and information (-b and -i)\n"); +#if !defined NODFA +printf(" -dfa force DFA matching for all subjects\n"); +#endif +printf(" -help show usage information\n"); +printf(" -i show information about compiled patterns\n" + " -m output memory used information\n" + " -o set size of offsets vector to \n"); +#if !defined NOPOSIX +printf(" -p use POSIX interface\n"); +#endif +printf(" -q quiet: do not output PCRE version number at start\n"); +printf(" -S set stack size to megabytes\n"); +printf(" -s output store (memory) used information\n" + " -t time compilation and execution\n"); +printf(" -t time compilation and execution, repeating times\n"); +printf(" -tm time execution (matching) only\n"); +printf(" -tm time execution (matching) only, repeating times\n"); +} + + + /************************************************* * Main Program * *************************************************/ @@ -657,6 +724,7 @@ int options = 0; int study_options = 0; int op = 1; int timeit = 0; +int timeitm = 0; int showinfo = 0; int showstore = 0; int quiet = 0; @@ -688,16 +756,19 @@ buffer = (unsigned char *)malloc(buffer_size); dbuffer = (unsigned char *)malloc(buffer_size); pbuffer = (unsigned char *)malloc(buffer_size); -/* The outfile variable is static so that new_malloc can use it. The _setmode() -stuff is some magic that I don't understand, but which apparently does good -things in Windows. It's related to line terminations. */ - -#if defined(_WIN32) || defined(WIN32) -_setmode( _fileno( stdout ), 0x8000 ); -#endif /* defined(_WIN32) || defined(WIN32) */ +/* The outfile variable is static so that new_malloc can use it. */ outfile = stdout; +/* The following _setmode() stuff is some Windows magic that tells its runtime +library to translate CRLF into a single LF character. At least, that's what +I've been told: never having used Windows I take this all on trust. Originally +it set 0x8000, but then I was advised that _O_BINARY was better. */ + +#if defined(_WIN32) || defined(WIN32) +_setmode( _fileno( stdout ), _O_BINARY ); +#endif + /* Scan options */ while (argc > 1 && argv[op][0] == '-') @@ -706,8 +777,8 @@ while (argc > 1 && argv[op][0] == '-') if (strcmp(argv[op], "-s") == 0 || strcmp(argv[op], "-m") == 0) showstore = 1; - else if (strcmp(argv[op], "-t") == 0) timeit = 1; else if (strcmp(argv[op], "-q") == 0) quiet = 1; + else if (strcmp(argv[op], "-b") == 0) debug = 1; else if (strcmp(argv[op], "-i") == 0) showinfo = 1; else if (strcmp(argv[op], "-d") == 0) showinfo = debug = 1; #if !defined NODFA @@ -720,11 +791,25 @@ while (argc > 1 && argv[op][0] == '-') op++; argc--; } + else if (strcmp(argv[op], "-t") == 0 || strcmp(argv[op], "-tm") == 0) + { + int both = argv[op][2] == 0; + int temp; + if (argc > 2 && (temp = get_value((unsigned char *)argv[op+1], &endptr), + *endptr == 0)) + { + timeitm = temp; + op++; + argc--; + } + else timeitm = LOOPREPEAT; + if (both) timeit = timeitm; + } else if (strcmp(argv[op], "-S") == 0 && argc > 2 && ((stack_size = get_value((unsigned char *)argv[op+1], &endptr)), *endptr == 0)) { -#ifdef _WIN32 +#if defined(_WIN32) || defined(WIN32) printf("PCRE: -S not supported on this OS\n"); exit(1); #else @@ -756,7 +841,8 @@ while (argc > 1 && argv[op][0] == '-') printf(" %sUnicode properties support\n", rc? "" : "No "); (void)pcre_config(PCRE_CONFIG_NEWLINE, &rc); printf(" Newline sequence is %s\n", (rc == '\r')? "CR" : - (rc == '\n')? "LF" : "CRLF"); + (rc == '\n')? "LF" : (rc == ('\r'<<8 | '\n'))? "CRLF" : + (rc == -1)? "ANY" : "???"); (void)pcre_config(PCRE_CONFIG_LINK_SIZE, &rc); printf(" Internal link size = %d\n", rc); (void)pcre_config(PCRE_CONFIG_POSIX_MALLOC_THRESHOLD, &rc); @@ -769,24 +855,16 @@ while (argc > 1 && argv[op][0] == '-') printf(" Match recursion uses %s\n", rc? "stack" : "heap"); exit(0); } + else if (strcmp(argv[op], "-help") == 0 || + strcmp(argv[op], "--help") == 0) + { + usage(); + goto EXIT; + } else { printf("** Unknown or malformed option %s\n", argv[op]); - printf("Usage: pcretest [options] [ []]\n"); - printf(" -C show PCRE compile-time options and exit\n"); - printf(" -d debug: show compiled code; implies -i\n"); -#if !defined NODFA - printf(" -dfa force DFA matching for all subjects\n"); -#endif - printf(" -i show information about compiled pattern\n" - " -m output memory used information\n" - " -o set size of offsets vector to \n"); -#if !defined NOPOSIX - printf(" -p use POSIX interface\n"); -#endif - printf(" -S set stack size to megabytes\n"); - printf(" -s output store (memory) used information\n" - " -t time compilation and execution\n"); + usage(); yield = 1; goto EXIT; } @@ -810,7 +888,7 @@ if (offsets == NULL) if (argc > 1) { - infile = fopen(argv[op], "rb"); + infile = fopen(argv[op], INPUT_MODE); if (infile == NULL) { printf("** Failed to open %s\n", argv[op]); @@ -821,7 +899,7 @@ if (argc > 1) if (argc > 2) { - outfile = fopen(argv[op+1], "wb"); + outfile = fopen(argv[op+1], OUTPUT_MODE); if (outfile == NULL) { printf("** Failed to open %s\n", argv[op+1]); @@ -866,7 +944,7 @@ while (!done) int do_showinfo = showinfo; int do_showrest = 0; int do_flip = 0; - int erroroffset, len, delimiter; + int erroroffset, len, delimiter, poffset; use_utf8 = 0; @@ -976,6 +1054,7 @@ while (!done) } pp = p; + poffset = p - buffer; for(;;) { @@ -996,6 +1075,11 @@ while (!done) if (infile != stdin) fprintf(outfile, "%s", (char *)pp); } + /* The buffer may have moved while being extended; reset the start of data + pointer to the correct relative point in the buffer. */ + + p = buffer + poffset; + /* If the first character after the delimiter is backslash, make the pattern end with backslash. This is purely to provide a way of testing for the error message when a pattern ends with backslash. */ @@ -1027,6 +1111,7 @@ while (!done) case '+': do_showrest = 1; break; case 'A': options |= PCRE_ANCHORED; break; + case 'B': do_debug = 1; break; case 'C': options |= PCRE_AUTO_CALLOUT; break; case 'D': do_debug = do_showinfo = 1; break; case 'E': options |= PCRE_DOLLAR_ENDONLY; break; @@ -1049,14 +1134,16 @@ while (!done) case 'L': ppp = pp; - /* The '\r' test here is so that it works on Windows */ - while (*ppp != '\n' && *ppp != '\r' && *ppp != ' ') ppp++; + /* The '\r' test here is so that it works on Windows. */ + /* The '0' test is just in case this is an unterminated line. */ + while (*ppp != 0 && *ppp != '\n' && *ppp != '\r' && *ppp != ' ') ppp++; *ppp = 0; if (setlocale(LC_CTYPE, (const char *)pp) == NULL) { fprintf(outfile, "** Failed to set locale \"%s\"\n", pp); goto SKIP_DATA; } + locale_set = 1; tables = pcre_maketables(); pp = ppp; break; @@ -1123,19 +1210,19 @@ while (!done) #endif /* !defined NOPOSIX */ { - if (timeit) + if (timeit > 0) { register int i; clock_t time_taken; clock_t start_time = clock(); - for (i = 0; i < LOOPREPEAT; i++) + for (i = 0; i < timeit; i++) { re = pcre_compile((char *)p, options, &error, &erroroffset, tables); if (re != NULL) free(re); } time_taken = clock() - start_time; - fprintf(outfile, "Compile time %.3f milliseconds\n", - (((double)time_taken * 1000.0) / (double)LOOPREPEAT) / + fprintf(outfile, "Compile time %.4f milliseconds\n", + (((double)time_taken * 1000.0) / (double)timeit) / (double)CLOCKS_PER_SEC); } @@ -1187,17 +1274,17 @@ while (!done) if (do_study) { - if (timeit) + if (timeit > 0) { register int i; clock_t time_taken; clock_t start_time = clock(); - for (i = 0; i < LOOPREPEAT; i++) + for (i = 0; i < timeit; i++) extra = pcre_study(re, study_options, &error); time_taken = clock() - start_time; if (extra != NULL) free(extra); - fprintf(outfile, " Study time %.3f milliseconds\n", - (((double)time_taken * 1000.0) / (double)LOOPREPEAT) / + fprintf(outfile, " Study time %.4f milliseconds\n", + (((double)time_taken * 1000.0) / (double)timeit) / (double)CLOCKS_PER_SEC); } extra = pcre_study(re, study_options, &error); @@ -1240,6 +1327,12 @@ while (!done) SHOW_INFO: + if (do_debug) + { + fprintf(outfile, "------------------------------------------------------------------\n"); + pcre_printint(re, outfile); + } + if (do_showinfo) { unsigned long int get_options, all_options; @@ -1250,12 +1343,6 @@ while (!done) int nameentrysize, namecount; const uschar *nametable; - if (do_debug) - { - fprintf(outfile, "------------------------------------------------------------------\n"); - pcre_printint(re, outfile); - } - new_info(re, NULL, PCRE_INFO_OPTIONS, &get_options); new_info(re, NULL, PCRE_INFO_SIZE, &size); new_info(re, NULL, PCRE_INFO_CAPTURECOUNT, &count); @@ -1334,7 +1421,7 @@ while (!done) ((get_options & PCRE_NO_UTF8_CHECK) != 0)? " no_utf8_check" : "", ((get_options & PCRE_DUPNAMES) != 0)? " dupnames" : ""); - switch (get_options & PCRE_NEWLINE_CRLF) + switch (get_options & PCRE_NEWLINE_BITS) { case PCRE_NEWLINE_CR: fprintf(outfile, "Forced newline sequence: CR\n"); @@ -1348,6 +1435,10 @@ while (!done) fprintf(outfile, "Forced newline sequence: CRLF\n"); break; + case PCRE_NEWLINE_ANY: + fprintf(outfile, "Forced newline sequence: ANY\n"); + break; + default: break; } @@ -1365,7 +1456,7 @@ while (!done) int ch = first_char & 255; const char *caseless = ((first_char & REQ_CASELESS) == 0)? "" : " (caseless)"; - if (isprint(ch)) + if (PRINTHEX(ch)) fprintf(outfile, "First char = \'%c\'%s\n", ch, caseless); else fprintf(outfile, "First char = %d%s\n", ch, caseless); @@ -1380,7 +1471,7 @@ while (!done) int ch = need_char & 255; const char *caseless = ((need_char & REQ_CASELESS) == 0)? "" : " (caseless)"; - if (isprint(ch)) + if (PRINTHEX(ch)) fprintf(outfile, "Need char = \'%c\'%s\n", ch, caseless); else fprintf(outfile, "Need char = %d%s\n", ch, caseless); @@ -1416,7 +1507,7 @@ while (!done) fprintf(outfile, "\n "); c = 2; } - if (isprint(i) && i != ' ') + if (PRINTHEX(i) && i != ' ') { fprintf(outfile, "%c ", i); c += 2; @@ -1475,6 +1566,7 @@ while (!done) strerror(errno)); } else fprintf(outfile, "Study data written to %s\n", to_file); + } } fclose(f); @@ -1873,7 +1965,7 @@ while (!done) for (;; gmatched++) /* Loop for /g or /G */ { - if (timeit) + if (timeitm > 0) { register int i; clock_t time_taken; @@ -1883,7 +1975,7 @@ while (!done) if (all_use_dfa || use_dfa) { int workspace[1000]; - for (i = 0; i < LOOPREPEAT; i++) + for (i = 0; i < timeitm; i++) count = pcre_dfa_exec(re, NULL, (char *)bptr, len, start_offset, options | g_notempty, use_offsets, use_size_offsets, workspace, sizeof(workspace)/sizeof(int)); @@ -1891,13 +1983,13 @@ while (!done) else #endif - for (i = 0; i < LOOPREPEAT; i++) + for (i = 0; i < timeitm; i++) count = pcre_exec(re, extra, (char *)bptr, len, start_offset, options | g_notempty, use_offsets, use_size_offsets); time_taken = clock() - start_time; - fprintf(outfile, "Execute time %.3f milliseconds\n", - (((double)time_taken * 1000.0) / (double)LOOPREPEAT) / + fprintf(outfile, "Execute time %.4f milliseconds\n", + (((double)time_taken * 1000.0) / (double)timeitm) / (double)CLOCKS_PER_SEC); } @@ -1973,7 +2065,28 @@ while (!done) if (count >= 0) { - int i; + int i, maxcount; + +#if !defined NODFA + if (all_use_dfa || use_dfa) maxcount = use_size_offsets/2; else +#endif + maxcount = use_size_offsets/3; + + /* This is a check against a lunatic return value. */ + + if (count > maxcount) + { + fprintf(outfile, + "** PCRE error: returned count %d is too big for offset size %d\n", + count, use_size_offsets); + count = use_size_offsets/3; + if (do_g || do_G) + { + fprintf(outfile, "** /%c loop abandoned\n", do_g? 'g' : 'G'); + do_g = do_G = FALSE; /* Break g/G loop */ + } + } + for (i = 0; i < count * 2; i += 2) { if (use_offsets[i] < 0) @@ -2172,6 +2285,7 @@ while (!done) { new_free((void *)tables); setlocale(LC_CTYPE, "C"); + locale_set = 0; } } diff --git a/src/src/pcre/ucp.h b/src/src/pcre/ucp.h index 20fcff52c..5dcd10520 100644 --- a/src/src/pcre/ucp.h +++ b/src/src/pcre/ucp.h @@ -1,4 +1,4 @@ -/* $Cambridge: exim/src/src/pcre/ucp.h,v 1.3 2006/11/07 16:50:36 ph10 Exp $ */ +/* $Cambridge: exim/src/src/pcre/ucp.h,v 1.4 2007/01/23 15:08:45 ph10 Exp $ */ /************************************************* * Unicode Property Table handler * @@ -8,7 +8,9 @@ #define _UCP_H /* This file contains definitions of the property values that are returned by -the function _pcre_ucp_findprop(). */ +the function _pcre_ucp_findprop(). New values that are added for new releases +of Unicode should always be at the end of each enum, for backwards +compatibility. */ /* These are the general character categories. */ @@ -120,7 +122,12 @@ enum { ucp_Tibetan, ucp_Tifinagh, ucp_Ugaritic, - ucp_Yi + ucp_Yi, + ucp_Balinese, /* New for Unicode 5.0.0 */ + ucp_Cuneiform, /* New for Unicode 5.0.0 */ + ucp_Nko, /* New for Unicode 5.0.0 */ + ucp_Phags_Pa, /* New for Unicode 5.0.0 */ + ucp_Phoenician /* New for Unicode 5.0.0 */ }; #endif -- 2.30.2