pcre.c
pennmush-1.8.3p3/src/pcre.c · 303.5 KB
From the archive pennmush-1.8.3p3.tar.gz
Download the archive « Back to the archive contents
/*************************************************
* 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-2005 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.
-----------------------------------------------------------------------------
*/
/* Modified by Shawn Wagner for PennMUSH to fit in one file and remove
things we don't use, like a bunch of API functions and utf-8
support. If you want the full thing, see http://www.pcre.org. */
/* Modified by Alan Schwartz for PennMUSH to change the use of
* 'isblank' as a variable (reported to Philip Hazel for pcre 4.5) */
#include "config.h"
#include <ctype.h>
#include <limits.h>
#include <setjmp.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "pcre.h"
#include "confmagic.h"
#undef min
#undef max
/* Bits of PCRE's conf.h */
#define NEWLINE '\n'
#define LINK_SIZE 2
#define MATCH_LIMIT 100000
#define NO_RECURSE
#define EBCDIC 0
/* Bits of pcre_internal.h */
#define PCRE_DEFINITION /* Win32 __declspec(export) trigger for .dll */
typedef unsigned char uschar;
/* We need to have types that specify unsigned 16-bit and 32-bit integers. We
cannot determine these outside the compilation (e.g. by running a program as
part of "configure") because PCRE is often cross-compiled for use on other
systems. Instead we make use of the maximum sizes that are available at
preprocessor time in standard C environments. */
#if USHRT_MAX == 65535
typedef unsigned short pcre_uint16;
#elif UINT_MAX == 65535
typedef unsigned int pcre_uint16;
#else
#error Cannot determine a type for 16-bit unsigned integers
#endif
#if UINT_MAX == 4294967295
typedef unsigned int pcre_uint32;
#elif ULONG_MAX == 4294967295
typedef unsigned long int pcre_uint32;
#else
#error Cannot determine a type for 32-bit unsigned integers
#endif
/* PCRE keeps offsets in its compiled code as 2-byte quantities (always stored
in big-endian order) by default. These are used, for example, to link from the
start of a subpattern to its alternatives and its end. The use of 2 bytes per
offset limits the size of the compiled regex to around 64K, which is big enough
for almost everybody. However, I received a request for an even bigger limit.
For this reason, and also to make the code easier to maintain, the storing and
loading of offsets from the byte string is now handled by the macros that are
defined here.
The macros are controlled by the value of LINK_SIZE. This defaults to 2 in
the config.h file, but can be overridden by using -D on the command line. This
is automated on Unix systems via the "configure" command. */
#define LINK_SIZE 2
#define PUT(a,n,d) \
(a[n] = (d) >> 8), \
(a[(n)+1] = (d) & 255)
#define GET(a,n) \
(((a)[n] << 8) | (a)[(n)+1])
#define MAX_PATTERN_SIZE (1 << 16)
/* Convenience macro defined in terms of the others */
#define PUTINC(a,n,d) PUT(a,n,d), a += LINK_SIZE
/* PCRE uses some other 2-byte quantities that do not change when the size of
offsets changes. There are used for repeat counts and for other things such as
capturing parenthesis numbers in back references. */
#define PUT2(a,n,d) \
a[n] = (d) >> 8; \
a[(n)+1] = (d) & 255
#define GET2(a,n) \
(((a)[n] << 8) | (a)[(n)+1])
#define PUT2INC(a,n,d) PUT2(a,n,d), a += 2
#define GETCHAR(c, eptr) c = *eptr;
#define GETCHARTEST(c, eptr) c = *eptr;
#define GETCHARINC(c, eptr) c = *eptr++;
#define GETCHARINCTEST(c, eptr) c = *eptr++;
#define GETCHARLEN(c, eptr, len) c = *eptr;
#define BACKCHAR(eptr)
/* These are the public options that can change during matching. */
#define PCRE_IMS (PCRE_CASELESS|PCRE_MULTILINE|PCRE_DOTALL)
/* Private options flags start at the most significant end of the four bytes,
but skip the top bit so we can use ints for convenience without getting tangled
with negative values. The public options defined in pcre.h start at the least
significant end. Make sure they don't overlap! */
#define PCRE_FIRSTSET 0x40000000 /* first_byte is set */
#define PCRE_REQCHSET 0x20000000 /* req_byte is set */
#define PCRE_STARTLINE 0x10000000 /* start after \n for multiline */
#define PCRE_ICHANGED 0x08000000 /* i option changes within regex */
#define PCRE_NOPARTIAL 0x04000000 /* can't use partial with this regex */
/* Options for the "extra" block produced by pcre_study(). */
#define PCRE_STUDY_MAPPED 0x01 /* a map of starting chars exists */
/* Masks for identifying the public options that are permitted at compile
time, run time, or study time, respectively. */
#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)
#define PUBLIC_EXEC_OPTIONS \
(PCRE_ANCHORED|PCRE_NOTBOL|PCRE_NOTEOL|PCRE_NOTEMPTY|PCRE_NO_UTF8_CHECK| \
PCRE_PARTIAL)
#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)
#define PUBLIC_STUDY_OPTIONS 0 /* None defined */
/* Magic number to provide a small check against being handed junk. Also used
to detect whether a pattern was compiled on a host of different endianness. */
#define MAGIC_NUMBER 0x50435245UL /* 'PCRE' */
/* Negative values for the firstchar and reqchar variables */
#define REQ_UNSET (-2)
#define REQ_NONE (-1)
/* The maximum remaining length of subject we are prepared to search for a
req_byte match. */
#define REQ_BYTE_MAX 1000
/* Flags added to firstbyte or reqbyte; a "non-literal" item is either a
variable-length repeat, or a anything other than literal characters. */
#define REQ_CASELESS 0x0100 /* indicates caselessness */
#define REQ_VARY 0x0200 /* reqbyte followed non-literal item */
/* Miscellaneous definitions */
typedef bool BOOL;
#define FALSE false
#define TRUE true
/* 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). */
#ifndef ESC_e
#define ESC_e 27
#endif
#ifndef ESC_f
#define ESC_f '\f'
#endif
#ifndef ESC_n
#define ESC_n NEWLINE
#endif
#ifndef ESC_r
#define ESC_r '\r'
#endif
/* We can't officially use ESC_t because it is a POSIX reserved identifier
(presumably because of all the others like size_t). */
#ifndef ESC_tee
#define ESC_tee '\t'
#endif
/* These are escaped items that aren't just an encoding of a particular data
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
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
};
/* Flag bits and data types for the extended class (OP_XCLASS) for classes that
contain UTF-8 characters with values greater than 255. */
#define XCL_NOT 0x01 /* Flag: this is a negative class */
#define XCL_MAP 0x02 /* Flag: a 32-byte map is present */
#define XCL_END 0 /* Marks end of individual items */
#define XCL_SINGLE 1 /* Single item (one multibyte char) follows */
#define XCL_RANGE 2 /* A range (two multibyte chars) follows */
#define XCL_PROP 3 /* Unicode property (one property code) follows */
#define XCL_NOTPROP 4 /* Unicode inverted property (ditto) */
/* 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. */
enum {
OP_END, /* 0 End of pattern */
/* Values corresponding to backslashed metacharacters */
OP_SOD, /* 1 Start of data: \A */
OP_SOM, /* 2 Start of match (subject + offset): \G */
OP_NOT_WORD_BOUNDARY, /* 3 \B */
OP_WORD_BOUNDARY, /* 4 \b */
OP_NOT_DIGIT, /* 5 \D */
OP_DIGIT, /* 6 \d */
OP_NOT_WHITESPACE, /* 7 \S */
OP_WHITESPACE, /* 8 \s */
OP_NOT_WORDCHAR, /* 9 \W */
OP_WORDCHAR, /* 10 \w */
OP_ANY, /* 11 Match any character */
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 anything but the following char */
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
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
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. */
/* The assertions must come before ONCE and COND */
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 */
/* 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_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) */
OP_BRAZERO, /* 77 These two must remain together and in this */
OP_BRAMINZERO, /* 78 order. */
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. */
};
/* 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. */
/* 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. */
#define EXTRACT_BASIC_MAX 100
/* This macro defines textual names for all the opcodes. These are used only
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", \
"\\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"
/* This macro defines the length of fixed length operations in the compiled
regex. The lengths are used when searching for specific things, and also in the
debugging printing of a compiled regex. We use a macro so that it can be
defined close to the definitions of the opcodes themselves.
As things have been extended, some of these are no longer fixed lenths, but are
minima instead. For example, the length of a single-character repeat may vary
in UTF-8 mode. The code that uses this table must know about such things. */
#define OP_LENGTHS \
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 */ \
2, 2, 1, /* NOTPROP, PROP, EXTUNI */ \
1, 1, 2, 1, 1, /* \Z, \z, Opt, ^, $ */ \
2, /* Char - the minimum length */ \
2, /* Charnc - the minimum length */ \
2, /* not */ \
/* Positive single-char repeats ** These are */ \
2, 2, 2, 2, 2, 2, /* *, *?, +, +?, ?, ?? ** minima in */ \
4, 4, 4, /* upto, minupto, exact ** UTF-8 mode */ \
/* Negative single-char repeats - only for chars < 256 */ \
2, 2, 2, 2, 2, 2, /* NOT *, *?, +, +?, ?, ?? */ \
4, 4, 4, /* NOT upto, minupto, exact */ \
/* Positive type repeats */ \
2, 2, 2, 2, 2, 2, /* Type *, *?, +, +?, ?, ?? */ \
4, 4, 4, /* Type upto, minupto, exact */ \
/* Character class & ref repeats */ \
1, 1, 1, 1, 1, 1, /* *, *?, +, +?, ?, ?? */ \
5, 5, /* CRRANGE, CRMINRANGE */ \
33, /* CLASS */ \
33, /* NCLASS */ \
0, /* XCLASS - variable length */ \
3, /* REF */ \
1+LINK_SIZE, /* RECURSE */ \
2+2*LINK_SIZE, /* CALLOUT */ \
1+LINK_SIZE, /* Alt */ \
1+LINK_SIZE, /* Ket */ \
1+LINK_SIZE, /* KetRmax */ \
1+LINK_SIZE, /* KetRmin */ \
1+LINK_SIZE, /* Assert */ \
1+LINK_SIZE, /* Assert not */ \
1+LINK_SIZE, /* Assert behind */ \
1+LINK_SIZE, /* Assert behind not */ \
1+LINK_SIZE, /* Reverse */ \
1+LINK_SIZE, /* Once */ \
1+LINK_SIZE, /* COND */ \
3, /* CREF */ \
1, 1, /* BRAZERO, BRAMINZERO */ \
3, /* BRANUMBER */ \
1+LINK_SIZE /* BRA */ \
/* A magic value for OP_CREF to indicate the "in recursion" condition. */
#define CREF_RECURSE 0xffff
/* Error code numbers. They are given names so that they can more easily be
tracked. */
enum { ERR0, ERR1, ERR2, ERR3, ERR4, ERR5, ERR6, ERR7, ERR8, ERR9,
ERR10, ERR11, ERR12, ERR13, ERR14, ERR15, ERR16, ERR17, ERR18, ERR19,
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
};
/* 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
offset to the name table so that if a regex is compiled on one host, saved, and
then run on another where the size of pointers is different, all might still
be well. For the case of compiled-on-4 and run-on-8, we include an extra
pointer that is always NULL. For future-proofing, a few dummy fields were
originally included - even though you can never get this planning right - but
there is only one left now.
NOTE NOTE NOTE:
Because people can now save and re-use compiled patterns, any additions to this
structure should be made at the end, and something earlier (e.g. a new
flag in the options or one of the dummy fields) should indicate that the new
fields are present. Currently PCRE always sets the dummy fields to zero.
NOTE NOTE NOTE:
*/
typedef struct real_pcre {
pcre_uint32 magic_number;
pcre_uint32 size; /* Total that was malloced */
pcre_uint32 options;
pcre_uint32 dummy1; /* For future use, maybe */
pcre_uint16 top_bracket;
pcre_uint16 top_backref;
pcre_uint16 first_byte;
pcre_uint16 req_byte;
pcre_uint16 name_table_offset; /* Offset to name table that follows */
pcre_uint16 name_entry_size; /* Size of any name items */
pcre_uint16 name_count; /* Number of name items */
pcre_uint16 ref_count; /* Reference count */
const unsigned char *tables; /* Pointer to tables or NULL for std */
const unsigned char *nullpad; /* NULL padding */
} real_pcre;
/* The format of the block used to store data from pcre_study(). The same
remark (see NOTE above) about extending this structure applies. */
typedef struct pcre_study_data {
pcre_uint32 size; /* Total that was malloced */
pcre_uint32 options;
uschar start_bits[32];
} pcre_study_data;
/* Structure for passing "static" information around between the functions
doing the compiling, so that they are thread-safe. */
typedef struct compile_data {
const uschar *lcc; /* Points to lower casing table */
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_code; /* The start of the compiled code */
const uschar *start_pattern; /* The start of the pattern */
uschar *name_table; /* The name/number table */
int names_found; /* Number of entries so far */
int name_entry_size; /* Size of each entry */
int top_backref; /* Maximum back reference */
unsigned int backref_map; /* Bitmap of low back refs */
int req_varyopt; /* "After variable item" flag for reqbyte */
BOOL nopartial; /* Set TRUE if partial won't work */
} compile_data;
/* Structure for maintaining a chain of pointers to the currently incomplete
branches, for testing for left recursion. */
typedef struct branch_chain {
struct branch_chain *outer;
uschar *current;
} branch_chain;
/* Structure for items in a linked list that represents an explicit recursive
call within the pattern. */
typedef struct recursion_info {
struct recursion_info *prevrec; /* Previous recursion record (or NULL) */
int group_num; /* Number of group that was called */
const uschar *after_call; /* "Return value": points after the call in the expr */
const uschar *save_start; /* Old value of md->start_match */
int *offset_save; /* Pointer to start of saved offsets */
int saved_max; /* Number of saved offsets */
} recursion_info;
/* When compiling in a mode that doesn't use recursive calls to match(),
a structure is used to remember local variables on the heap. It is defined in
pcre.c, close to the match() function, so that it is easy to keep it in step
with any changes of local variable. However, the pointer to the current frame
must be saved in some "static" place over a longjmp(). We declare the
structure here so that we can put a pointer in the match_data structure.
NOTE: This isn't used for a "normal" compilation of pcre. */
struct heapframe;
/* Structure for passing "static" information around between the functions
doing traditional NFA matching, so that they are thread-safe. */
typedef struct match_data {
unsigned long int match_call_count; /* As it says */
unsigned long int match_limit; /* As it says */
int *offset_vector; /* Offset vector */
int offset_end; /* One past the end */
int offset_max; /* The maximum usable for return data */
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 */
BOOL notbol; /* NOTBOL flag */
BOOL noteol; /* NOTEOL flag */
BOOL utf8; /* UTF8 flag */
BOOL endonly; /* Dollar not before final \n */
BOOL notempty; /* Empty string match not wanted */
BOOL partial; /* PARTIAL flag */
BOOL hitend; /* Hit the end of the subject at some point */
const uschar *start_code; /* For use when recursing */
const uschar *start_subject; /* Start of the subject string */
const uschar *end_subject; /* End of the subject string */
const uschar *start_match; /* Start of this match attempt */
const uschar *end_match_ptr; /* Subject position at end match */
int end_offset_top; /* Highwater mark at end of match */
int capture_last; /* Most recent capture number */
int start_offset; /* The start offset value */
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 */
} match_data;
/* A similar structure is used for the same purpose by the DFA matching
functions. */
typedef struct dfa_match_data {
const uschar *start_code; /* Start of the compiled pattern */
const uschar *start_subject; /* Start of the subject string */
const uschar *end_subject; /* End of subject string */
const uschar *tables; /* Character tables */
int moptions; /* Match options */
int poptions; /* Pattern options */
void *callout_data; /* To pass back to callouts */
} dfa_match_data;
/* Bit definitions for entries in the pcre_ctypes table. */
#define ctype_space 0x01
#define ctype_letter 0x02
#define ctype_digit 0x04
#define ctype_xdigit 0x08
#define ctype_word 0x10 /* alphameric or '_' */
#define ctype_meta 0x80 /* regexp meta char or zero (end pattern) */
/* Offsets for the bitmap tables in pcre_cbits. Each table contains a set
of bits for a class map. Some classes are built by combining these tables. */
#define cbit_space 0 /* [:space:] or \s */
#define cbit_xdigit 32 /* [:xdigit:] */
#define cbit_digit 64 /* [:digit:] or \d */
#define cbit_upper 96 /* [:upper:] */
#define cbit_lower 128 /* [:lower:] */
#define cbit_word 160 /* [:word:] or \w */
#define cbit_graph 192 /* [:graph:] */
#define cbit_print 224 /* [:print:] */
#define cbit_punct 256 /* [:punct:] */
#define cbit_cntrl 288 /* [:cntrl:] */
#define cbit_length 320 /* Length of the cbits table */
/* Offsets of the various tables from the base tables pointer, and
total length. */
#define lcc_offset 0
#define fcc_offset 256
#define cbits_offset 512
#define ctypes_offset (cbits_offset + cbit_length)
#define tables_length (ctypes_offset + 256)
/* Layout of the UCP type table that translates property names into codes for
pcre_ucp_findchar(). */
typedef struct {
const char *name;
int value;
} ucp_type_table;
/* Bits of pcre_globals.c */
int (*pcre_callout) (pcre_callout_block *) = NULL;
/* pcre_chartables.c */
/*************************************************
* Perl-Compatible Regular Expressions *
*************************************************/
/* This file is automatically written by the dftables auxiliary
program. If you edit it by hand, you might like to edit the Makefile to
prevent its ever being regenerated.
This file contains the default tables for characters with codes less than
128 (ASCII characters). These tables are used when no external tables are
passed to PCRE. */
const unsigned char _pcre_default_tables[] = {
/* This table is a lower casing table. */
0, 1, 2, 3, 4, 5, 6, 7,
8, 9, 10, 11, 12, 13, 14, 15,
16, 17, 18, 19, 20, 21, 22, 23,
24, 25, 26, 27, 28, 29, 30, 31,
32, 33, 34, 35, 36, 37, 38, 39,
40, 41, 42, 43, 44, 45, 46, 47,
48, 49, 50, 51, 52, 53, 54, 55,
56, 57, 58, 59, 60, 61, 62, 63,
64, 97, 98, 99, 100, 101, 102, 103,
104, 105, 106, 107, 108, 109, 110, 111,
112, 113, 114, 115, 116, 117, 118, 119,
120, 121, 122, 91, 92, 93, 94, 95,
96, 97, 98, 99, 100, 101, 102, 103,
104, 105, 106, 107, 108, 109, 110, 111,
112, 113, 114, 115, 116, 117, 118, 119,
120, 121, 122, 123, 124, 125, 126, 127,
128, 129, 130, 131, 132, 133, 134, 135,
136, 137, 138, 139, 140, 141, 142, 143,
144, 145, 146, 147, 148, 149, 150, 151,
152, 153, 154, 155, 156, 157, 158, 159,
160, 161, 162, 163, 164, 165, 166, 167,
168, 169, 170, 171, 172, 173, 174, 175,
176, 177, 178, 179, 180, 181, 182, 183,
184, 185, 186, 187, 188, 189, 190, 191,
192, 193, 194, 195, 196, 197, 198, 199,
200, 201, 202, 203, 204, 205, 206, 207,
208, 209, 210, 211, 212, 213, 214, 215,
216, 217, 218, 219, 220, 221, 222, 223,
224, 225, 226, 227, 228, 229, 230, 231,
232, 233, 234, 235, 236, 237, 238, 239,
240, 241, 242, 243, 244, 245, 246, 247,
248, 249, 250, 251, 252, 253, 254, 255,
/* This table is a case flipping table. */
0, 1, 2, 3, 4, 5, 6, 7,
8, 9, 10, 11, 12, 13, 14, 15,
16, 17, 18, 19, 20, 21, 22, 23,
24, 25, 26, 27, 28, 29, 30, 31,
32, 33, 34, 35, 36, 37, 38, 39,
40, 41, 42, 43, 44, 45, 46, 47,
48, 49, 50, 51, 52, 53, 54, 55,
56, 57, 58, 59, 60, 61, 62, 63,
64, 97, 98, 99, 100, 101, 102, 103,
104, 105, 106, 107, 108, 109, 110, 111,
112, 113, 114, 115, 116, 117, 118, 119,
120, 121, 122, 91, 92, 93, 94, 95,
96, 65, 66, 67, 68, 69, 70, 71,
72, 73, 74, 75, 76, 77, 78, 79,
80, 81, 82, 83, 84, 85, 86, 87,
88, 89, 90, 123, 124, 125, 126, 127,
128, 129, 130, 131, 132, 133, 134, 135,
136, 137, 138, 139, 140, 141, 142, 143,
144, 145, 146, 147, 148, 149, 150, 151,
152, 153, 154, 155, 156, 157, 158, 159,
160, 161, 162, 163, 164, 165, 166, 167,
168, 169, 170, 171, 172, 173, 174, 175,
176, 177, 178, 179, 180, 181, 182, 183,
184, 185, 186, 187, 188, 189, 190, 191,
192, 193, 194, 195, 196, 197, 198, 199,
200, 201, 202, 203, 204, 205, 206, 207,
208, 209, 210, 211, 212, 213, 214, 215,
216, 217, 218, 219, 220, 221, 222, 223,
224, 225, 226, 227, 228, 229, 230, 231,
232, 233, 234, 235, 236, 237, 238, 239,
240, 241, 242, 243, 244, 245, 246, 247,
248, 249, 250, 251, 252, 253, 254, 255,
/* This table contains bit maps for various character classes.
Each map is 32 bytes long and the bits run from the least
significant end of each byte. The classes that have their own
maps are: space, xdigit, digit, upper, lower, word, graph
print, punct, and cntrl. Other classes are built from combinations. */
0x00, 0x3e, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x03,
0x7e, 0x00, 0x00, 0x00, 0x7e, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x03,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0xfe, 0xff, 0xff, 0x07, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0xfe, 0xff, 0xff, 0x07,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x03,
0xfe, 0xff, 0xff, 0x87, 0xfe, 0xff, 0xff, 0x07,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0xfe, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0xfe, 0xff, 0x00, 0xfc,
0x01, 0x00, 0x00, 0xf8, 0x01, 0x00, 0x00, 0x78,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* This table identifies various classes of character by individual bits:
0x01 white space character
0x02 letter
0x04 decimal digit
0x08 hexadecimal digit
0x10 alphanumeric or '_'
0x80 regular expression metacharacter or binary zero
*/
0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0- 7 */
0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x00, /* 8- 15 */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 16- 23 */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 24- 31 */
0x01, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, /* - ' */
0x80, 0x80, 0x80, 0x80, 0x00, 0x00, 0x80, 0x00, /* ( - / */
0x1c, 0x1c, 0x1c, 0x1c, 0x1c, 0x1c, 0x1c, 0x1c, /* 0 - 7 */
0x1c, 0x1c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, /* 8 - ? */
0x00, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x12, /* @ - G */
0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, /* H - O */
0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, /* P - W */
0x12, 0x12, 0x12, 0x80, 0x00, 0x00, 0x80, 0x10, /* X - _ */
0x00, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x12, /* ` - g */
0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, /* h - o */
0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, /* p - w */
0x12, 0x12, 0x12, 0x80, 0x80, 0x00, 0x00, 0x00, /* x -127 */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 128-135 */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 136-143 */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 144-151 */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 152-159 */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 160-167 */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 168-175 */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 176-183 */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 184-191 */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 192-199 */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 200-207 */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 208-215 */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 216-223 */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 224-231 */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 232-239 */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 240-247 */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
}; /* 248-255 */
/* End of chartables.c */
/* Bits of pcre_tables.c */
/* This module contains some fixed tables that are used by more than one of the
PCRE code modules. The tables are also #included by the pcretest program, which
uses macros to change their names from _pcre_xxx to xxxx, thereby avoiding name
clashes with the library. */
/* Table of sizes for the fixed-length opcodes. It's defined in a macro so that
the definition is next to the definition of the opcodes in internal.h. */
const uschar _pcre_OP_lengths[] = { OP_LENGTHS };
/* End of pcre_tables.c */
/* pcre_try_flipped.c */
/* This module contains an internal function that tests a compiled pattern to
see if it was compiled with the opposite endianness. If so, it uses an
auxiliary local function to flip the appropriate bytes. */
/*************************************************
* Flip bytes in an integer *
*************************************************/
/* This function is called when the magic number in a regex doesn't match, in
order to flip its bytes to see if we are dealing with a pattern that was
compiled on a host of different endianness. If so, this function is used to
flip other byte values.
Arguments:
value the number to flip
n the number of bytes to flip (assumed to be 2 or 4)
Returns: the flipped value
*/
static long int
byteflip(long int value, int n)
{
if (n == 2)
return ((value & 0x00ff) << 8) | ((value & 0xff00) >> 8);
return ((value & 0x000000ff) << 24) |
((value & 0x0000ff00) << 8) |
((value & 0x00ff0000) >> 8) | ((value & 0xff000000) >> 24);
}
/*************************************************
* Test for a byte-flipped compiled regex *
*************************************************/
/* This function is called from pcre_exec(), pcre_dfa_exec(), and also from
pcre_fullinfo(). Its job is to test whether the regex is byte-flipped - that
is, it was compiled on a system of opposite endianness. The function is called
only when the native MAGIC_NUMBER test fails. If the regex is indeed flipped,
we flip all the relevant values into a different data block, and return it.
Arguments:
re points to the regex
study points to study data, or NULL
internal_re points to a new regex block
internal_study points to a new study block
Returns: the new block if is is indeed a byte-flipped regex
NULL if it is not
*/
real_pcre *_pcre_try_flipped(const real_pcre *re, real_pcre *internal_re,
const pcre_study_data *study,
pcre_study_data *internal_study);
real_pcre *
_pcre_try_flipped(const real_pcre *re, real_pcre *internal_re,
const pcre_study_data *study, pcre_study_data *internal_study)
{
if (byteflip(re->magic_number, sizeof(re->magic_number)) != MAGIC_NUMBER)
return NULL;
*internal_re = *re; /* To copy other fields */
internal_re->size = byteflip(re->size, sizeof(re->size));
internal_re->options = byteflip(re->options, sizeof(re->options));
internal_re->top_bracket =
(pcre_uint16) byteflip(re->top_bracket, sizeof(re->top_bracket));
internal_re->top_backref =
(pcre_uint16) byteflip(re->top_backref, sizeof(re->top_backref));
internal_re->first_byte =
(pcre_uint16) byteflip(re->first_byte, sizeof(re->first_byte));
internal_re->req_byte =
(pcre_uint16) byteflip(re->req_byte, sizeof(re->req_byte));
internal_re->name_table_offset =
(pcre_uint16) byteflip(re->name_table_offset,
sizeof(re->name_table_offset));
internal_re->name_entry_size =
(pcre_uint16) byteflip(re->name_entry_size, sizeof(re->name_entry_size));
internal_re->name_count =
(pcre_uint16) byteflip(re->name_count, sizeof(re->name_count));
if (study != NULL) {
*internal_study = *study; /* To copy other fields */
internal_study->size = byteflip(study->size, sizeof(study->size));
internal_study->options = byteflip(study->options, sizeof(study->options));
}
return internal_re;
}
/* End of pcre_tryflipped.c */
/* pcre_fullinfo.c */
/* This module contains the external function pcre_fullinfo(), which returns
information about a compiled pattern. */
/*************************************************
* Return info about compiled pattern *
*************************************************/
/* This is a newer "info" function which has an extensible interface so
that additional items can be added compatibly.
Arguments:
argument_re points to compiled code
extra_data points extra data, or NULL
what what information is required
where where to put the information
Returns: 0 if data returned, negative on error
*/
int
pcre_fullinfo(const pcre * argument_re, const pcre_extra * extra_data, int what,
void *where)
{
real_pcre internal_re;
pcre_study_data internal_study;
const real_pcre *re = (const real_pcre *) argument_re;
const pcre_study_data *study = NULL;
if (re == NULL || where == NULL)
return PCRE_ERROR_NULL;
if (extra_data != NULL && (extra_data->flags & PCRE_EXTRA_STUDY_DATA) != 0)
study = (const pcre_study_data *) extra_data->study_data;
if (re->magic_number != MAGIC_NUMBER) {
re = _pcre_try_flipped(re, &internal_re, study, &internal_study);
if (re == NULL)
return PCRE_ERROR_BADMAGIC;
if (study != NULL)
study = &internal_study;
}
switch (what) {
case PCRE_INFO_OPTIONS:
*((unsigned long int *) where) = re->options & PUBLIC_OPTIONS;
break;
case PCRE_INFO_SIZE:
*((size_t *) where) = re->size;
break;
case PCRE_INFO_STUDYSIZE:
*((size_t *) where) = (study == NULL) ? 0 : study->size;
break;
case PCRE_INFO_CAPTURECOUNT:
*((int *) where) = re->top_bracket;
break;
case PCRE_INFO_BACKREFMAX:
*((int *) where) = re->top_backref;
break;
case PCRE_INFO_FIRSTBYTE:
*((int *) where) =
((re->options & PCRE_FIRSTSET) != 0) ? re->first_byte :
((re->options & PCRE_STARTLINE) != 0) ? -1 : -2;
break;
/* Make sure we pass back the pointer to the bit vector in the external
block, not the internal copy (with flipped integer fields). */
case PCRE_INFO_FIRSTTABLE:
*((const uschar **) where) =
(study != NULL && (study->options & PCRE_STUDY_MAPPED) != 0) ?
((const pcre_study_data *) extra_data->study_data)->start_bits : NULL;
break;
case PCRE_INFO_LASTLITERAL:
*((int *) where) = ((re->options & PCRE_REQCHSET) != 0) ? re->req_byte : -1;
break;
case PCRE_INFO_NAMEENTRYSIZE:
*((int *) where) = re->name_entry_size;
break;
case PCRE_INFO_NAMECOUNT:
*((int *) where) = re->name_count;
break;
case PCRE_INFO_NAMETABLE:
*((const uschar **) where) = (const uschar *) re + re->name_table_offset;
break;
case PCRE_INFO_DEFAULT_TABLES:
*((const uschar **) where) = (const uschar *) (_pcre_default_tables);
break;
default:
return PCRE_ERROR_BADOPTION;
}
return 0;
}
/* End of pcre_fullinfo.c */
/* pcre_get.c */
/* This module contains some convenience functions for extracting substrings
from the subject string after a regex match has succeeded. The original idea
for these functions came from Scott Wimer. */
/*************************************************
* Find number for named string *
*************************************************/
/* This function is used by the two extraction functions below, as well
as being generally available.
Arguments:
code the compiled regex
stringname the name whose number is required
Returns: the number of the named parentheses, or a negative number
(PCRE_ERROR_NOSUBSTRING) if not found
*/
int
pcre_get_stringnumber(const pcre * code, const char *stringname)
{
int rc;
int entrysize;
int top, bot;
uschar *nametable;
if ((rc = pcre_fullinfo(code, NULL, PCRE_INFO_NAMECOUNT, &top)) != 0)
return rc;
if (top <= 0)
return PCRE_ERROR_NOSUBSTRING;
if ((rc =
pcre_fullinfo(code, NULL, PCRE_INFO_NAMEENTRYSIZE, &entrysize)) != 0)
return rc;
if ((rc = pcre_fullinfo(code, NULL, PCRE_INFO_NAMETABLE, &nametable)) != 0)
return rc;
bot = 0;
while (top > bot) {
int mid = (top + bot) / 2;
uschar *entry = nametable + entrysize * mid;
int c = strcmp(stringname, (char *) (entry + 2));
if (c == 0)
return (entry[0] << 8) + entry[1];
if (c > 0)
bot = mid + 1;
else
top = mid;
}
return PCRE_ERROR_NOSUBSTRING;
}
/*************************************************
* Copy captured string to given buffer *
*************************************************/
/* This function copies a single captured substring into a given buffer.
Note that we use memcpy() rather than strncpy() in case there are binary zeros
in the string.
Arguments:
subject the subject string that was matched
ovector pointer to the offsets table
stringcount the number of substrings that were captured
(i.e. the yield of the pcre_exec call, unless
that was zero, in which case it should be 1/3
of the offset table size)
stringnumber the number of the required substring
buffer where to put the substring
size the size of the buffer
Returns: if successful:
the length of the copied string, not including the zero
that is put on the end; can be zero
if not successful:
PCRE_ERROR_NOMEMORY (-6) buffer too small
PCRE_ERROR_NOSUBSTRING (-7) no such captured substring
*/
int
pcre_copy_substring(const char *subject, int *ovector, int stringcount,
int stringnumber, char *buffer, int size)
{
int yield;
if (stringnumber < 0 || stringnumber >= stringcount)
return PCRE_ERROR_NOSUBSTRING;
stringnumber *= 2;
yield = ovector[stringnumber + 1] - ovector[stringnumber];
if (size < yield + 1)
return PCRE_ERROR_NOMEMORY;
memcpy(buffer, subject + ovector[stringnumber], yield);
buffer[yield] = 0;
return yield;
}
/*************************************************
* Copy named captured string to given buffer *
*************************************************/
/* This function copies a single captured substring into a given buffer,
identifying it by name.
Arguments:
code the compiled regex
subject the subject string that was matched
ovector pointer to the offsets table
stringcount the number of substrings that were captured
(i.e. the yield of the pcre_exec call, unless
that was zero, in which case it should be 1/3
of the offset table size)
stringname the name of the required substring
buffer where to put the substring
size the size of the buffer
Returns: if successful:
the length of the copied string, not including the zero
that is put on the end; can be zero
if not successful:
PCRE_ERROR_NOMEMORY (-6) buffer too small
PCRE_ERROR_NOSUBSTRING (-7) no such captured substring
*/
int
pcre_copy_named_substring(const pcre * code, const char *subject, int *ovector,
int stringcount, const char *stringname, char *buffer,
int size)
{
int n = pcre_get_stringnumber(code, stringname);
if (n <= 0)
return n;
return pcre_copy_substring(subject, ovector, stringcount, n, buffer, size);
}
/*************************************************
* Copy all captured strings to new store *
*************************************************/
/* This function gets one chunk of store and builds a list of pointers and all
of the captured substrings in it. A NULL pointer is put on the end of the list.
Arguments:
subject the subject string that was matched
ovector pointer to the offsets table
stringcount the number of substrings that were captured
(i.e. the yield of the pcre_exec call, unless
that was zero, in which case it should be 1/3
of the offset table size)
listptr set to point to the list of pointers
Returns: if successful: 0
if not successful:
PCRE_ERROR_NOMEMORY (-6) failed to get store
*/
int
pcre_get_substring_list(const char *subject, int *ovector, int stringcount,
const char ***listptr);
int
pcre_get_substring_list(const char *subject, int *ovector, int stringcount,
const char ***listptr)
{
int i;
int size = sizeof(char *);
int double_count = stringcount * 2;
char **stringlist;
char *p;
for (i = 0; i < double_count; i += 2)
size += sizeof(char *) + ovector[i + 1] - ovector[i] + 1;
stringlist = (char **) malloc(size);
if (stringlist == NULL)
return PCRE_ERROR_NOMEMORY;
*listptr = (const char **) stringlist;
p = (char *) (stringlist + stringcount + 1);
for (i = 0; i < double_count; i += 2) {
int len = ovector[i + 1] - ovector[i];
memcpy(p, subject + ovector[i], len);
*stringlist++ = p;
p += len;
*p++ = 0;
}
*stringlist = NULL;
return 0;
}
/*************************************************
* Free store obtained by get_substring_list *
*************************************************/
/* This function exists for the benefit of people calling PCRE from non-C
programs that can call its functions, but not free() or free() directly.
Argument: the result of a previous pcre_get_substring_list()
Returns: nothing
*/
void
pcre_free_substring_list(const char **pointer);
void
pcre_free_substring_list(const char **pointer)
{
free((void *) pointer);
}
/*************************************************
* Copy captured string to new store *
*************************************************/
/* This function copies a single captured substring into a piece of new
store
Arguments:
subject the subject string that was matched
ovector pointer to the offsets table
stringcount the number of substrings that were captured
(i.e. the yield of the pcre_exec call, unless
that was zero, in which case it should be 1/3
of the offset table size)
stringnumber the number of the required substring
stringptr where to put a pointer to the substring
Returns: if successful:
the length of the string, not including the zero that
is put on the end; can be zero
if not successful:
PCRE_ERROR_NOMEMORY (-6) failed to get store
PCRE_ERROR_NOSUBSTRING (-7) substring not present
*/
int
pcre_get_substring(const char *subject, int *ovector, int stringcount,
int stringnumber, const char **stringptr);
int
pcre_get_substring(const char *subject, int *ovector, int stringcount,
int stringnumber, const char **stringptr)
{
int yield;
char *substring;
if (stringnumber < 0 || stringnumber >= stringcount)
return PCRE_ERROR_NOSUBSTRING;
stringnumber *= 2;
yield = ovector[stringnumber + 1] - ovector[stringnumber];
substring = (char *) malloc(yield + 1);
if (substring == NULL)
return PCRE_ERROR_NOMEMORY;
memcpy(substring, subject + ovector[stringnumber], yield);
substring[yield] = 0;
*stringptr = substring;
return yield;
}
/*************************************************
* Copy named captured string to new store *
*************************************************/
/* This function copies a single captured substring, identified by name, into
new store.
Arguments:
code the compiled regex
subject the subject string that was matched
ovector pointer to the offsets table
stringcount the number of substrings that were captured
(i.e. the yield of the pcre_exec call, unless
that was zero, in which case it should be 1/3
of the offset table size)
stringname the name of the required substring
stringptr where to put the pointer
Returns: if successful:
the length of the copied string, not including the zero
that is put on the end; can be zero
if not successful:
PCRE_ERROR_NOMEMORY (-6) couldn't get memory
PCRE_ERROR_NOSUBSTRING (-7) no such captured substring
*/
int
pcre_get_named_substring(const pcre * code, const char *subject, int *ovector,
int stringcount, const char *stringname,
const char **stringptr);
int
pcre_get_named_substring(const pcre * code, const char *subject, int *ovector,
int stringcount, const char *stringname,
const char **stringptr)
{
int n = pcre_get_stringnumber(code, stringname);
if (n <= 0)
return n;
return pcre_get_substring(subject, ovector, stringcount, n, stringptr);
}
/*************************************************
* Free store obtained by get_substring *
*************************************************/
/* This function exists for the benefit of people calling PCRE from non-C
programs that can call its functions, but not free() or free() directly.
Argument: the result of a previous pcre_get_substring()
Returns: nothing
*/
void
pcre_free_substring(const char *pointer);
void
pcre_free_substring(const char *pointer)
{
free((void *) pointer);
}
/* End of pcre_get.c */
/* pcre_maketables.c */
/* This module contains the external function pcre_maketables(), which builds
character tables for PCRE in the current locale. The file is compiled on its
own as part of the PCRE library. However, it is also included in the
compilation of dftables.c, in which case the macro DFTABLES is defined. */
/*************************************************
* Create PCRE character tables *
*************************************************/
/* This function builds a set of character tables for use by PCRE and returns
a pointer to them. They are build using the ctype functions, and consequently
their contents will depend upon the current locale setting. When compiled as
part of the library, the store is obtained via pcre_malloc(), but when compiled
inside dftables, use malloc().
Arguments: none
Returns: pointer to the contiguous block of data
*/
const unsigned char *
pcre_maketables(void)
{
unsigned char *yield, *p;
int i;
yield = (unsigned char *) malloc(tables_length);
if (yield == NULL)
return NULL;
p = yield;
/* First comes the lower casing table */
for (i = 0; i < 256; i++)
*p++ = tolower(i);
/* Next the case-flipping table */
for (i = 0; i < 256; i++)
*p++ = islower(i) ? toupper(i) : tolower(i);
/* Then the character class tables. Don't try to be clever and save effort
on exclusive ones - in some locales things may be different. Note that the
table for "space" includes everything "isspace" gives, including VT in the
default locale. This makes it work for the POSIX class [:space:]. */
memset(p, 0, cbit_length);
for (i = 0; i < 256; i++) {
if (isdigit(i)) {
p[cbit_digit + i / 8] |= 1 << (i & 7);
p[cbit_word + i / 8] |= 1 << (i & 7);
}
if (isupper(i)) {
p[cbit_upper + i / 8] |= 1 << (i & 7);
p[cbit_word + i / 8] |= 1 << (i & 7);
}
if (islower(i)) {
p[cbit_lower + i / 8] |= 1 << (i & 7);
p[cbit_word + i / 8] |= 1 << (i & 7);
}
if (i == '_')
p[cbit_word + i / 8] |= 1 << (i & 7);
if (isspace(i))
p[cbit_space + i / 8] |= 1 << (i & 7);
if (isxdigit(i))
p[cbit_xdigit + i / 8] |= 1 << (i & 7);
if (isgraph(i))
p[cbit_graph + i / 8] |= 1 << (i & 7);
if (isprint(i))
p[cbit_print + i / 8] |= 1 << (i & 7);
if (ispunct(i))
p[cbit_punct + i / 8] |= 1 << (i & 7);
if (iscntrl(i))
p[cbit_cntrl + i / 8] |= 1 << (i & 7);
}
p += cbit_length;
/* Finally, the character type table. In this, we exclude VT from the white
space chars, because Perl doesn't recognize it as such for \s and for comments
within regexes. */
for (i = 0; i < 256; i++) {
int x = 0;
if (i != 0x0b && isspace(i))
x += ctype_space;
if (isalpha(i))
x += ctype_letter;
if (isdigit(i))
x += ctype_digit;
if (isxdigit(i))
x += ctype_xdigit;
if (isalnum(i) || i == '_')
x += ctype_word;
/* Note: strchr includes the terminating zero in the characters it considers.
In this instance, that is ok because we want binary zero to be flagged as a
meta-character, which in this sense is any character that terminates a run
of data characters. */
if (strchr("*+?{^.$|()[", i) != 0)
x += ctype_meta;
*p++ = x;
}
return yield;
}
/* End of pcre_maketables.c */
/* pcre_study.c */
/* This module contains the external function pcre_study(), along with local
supporting functions. */
/*************************************************
* Set a bit and maybe its alternate case *
*************************************************/
/* Given a character, set its bit in the table, and also the bit for the other
version of a letter if we are caseless.
Arguments:
start_bits points to the bit map
c is the character
caseless the caseless flag
cd the block with char table pointers
Returns: nothing
*/
static void
set_bit(uschar *start_bits, unsigned int c, BOOL caseless, compile_data *cd)
{
start_bits[c / 8] |= (1 << (c & 7));
if (caseless && (cd->ctypes[c] & ctype_letter) != 0)
start_bits[cd->fcc[c] / 8] |= (1 << (cd->fcc[c] & 7));
}
/*************************************************
* Create bitmap of starting chars *
*************************************************/
/* 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.
Arguments:
code points to an expression
start_bits points to a 32-byte table, initialized to 0
caseless the current state of the caseless flag
utf8 TRUE if in UTF-8 mode
cd the block with char table pointers
Returns: TRUE if table built, FALSE otherwise
*/
static BOOL
set_start_bits(const uschar *code, uschar *start_bits, BOOL caseless,
BOOL utf8, compile_data *cd)
{
register int c;
/* This next statement and the later reference to dummy are here in order to
trick the optimizer of the IBM C compiler for OS/2 into generating correct
code. Apparently IBM isn't going to fix the problem, and we would rather not
disable optimization (in this module it actually makes a big difference, and
the pcre module can use all the optimization it can get). */
volatile int dummy;
do {
const uschar *tcode = code + 1 + LINK_SIZE;
BOOL try_next = TRUE;
while (try_next) {
/* 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) {
if (!set_start_bits(tcode, start_bits, caseless, utf8, cd))
return FALSE;
try_next = FALSE;
}
else
switch (*tcode) {
default:
return FALSE;
/* Skip over callout */
case OP_CALLOUT:
tcode += 2 + 2 * LINK_SIZE;
break;
/* Skip over extended extraction bracket number */
case OP_BRANUMBER:
tcode += 3;
break;
/* Skip over lookbehind and negative lookahead assertions */
case OP_ASSERT_NOT:
case OP_ASSERTBACK:
case OP_ASSERTBACK_NOT:
do
tcode += GET(tcode, 1);
while (*tcode == OP_ALT);
tcode += 1 + LINK_SIZE;
break;
/* Skip over an option setting, changing the caseless flag */
case OP_OPT:
caseless = (tcode[1] & PCRE_CASELESS) != 0;
tcode += 2;
break;
/* BRAZERO does the bracket, but carries on. */
case OP_BRAZERO:
case OP_BRAMINZERO:
if (!set_start_bits(++tcode, start_bits, caseless, utf8, cd))
return FALSE;
dummy = 1;
do
tcode += GET(tcode, 1);
while (*tcode == OP_ALT);
tcode += 1 + LINK_SIZE;
break;
/* Single-char * or ? sets the bit and tries the next item */
case OP_STAR:
case OP_MINSTAR:
case OP_QUERY:
case OP_MINQUERY:
set_bit(start_bits, tcode[1], caseless, cd);
tcode += 2;
break;
/* Single-char upto sets the bit and tries the next */
case OP_UPTO:
case OP_MINUPTO:
set_bit(start_bits, tcode[3], caseless, cd);
tcode += 4;
break;
/* At least one single char sets the bit and stops */
case OP_EXACT: /* Fall through */
tcode += 2;
case OP_CHAR:
case OP_CHARNC:
case OP_PLUS:
case OP_MINPLUS:
set_bit(start_bits, tcode[1], caseless, cd);
try_next = FALSE;
break;
/* Single character type sets the bits and stops */
case OP_NOT_DIGIT:
for (c = 0; c < 32; c++)
start_bits[c] |= ~cd->cbits[c + cbit_digit];
try_next = FALSE;
break;
case OP_DIGIT:
for (c = 0; c < 32; c++)
start_bits[c] |= cd->cbits[c + cbit_digit];
try_next = FALSE;
break;
case OP_NOT_WHITESPACE:
for (c = 0; c < 32; c++)
start_bits[c] |= ~cd->cbits[c + cbit_space];
try_next = FALSE;
break;
case OP_WHITESPACE:
for (c = 0; c < 32; c++)
start_bits[c] |= cd->cbits[c + cbit_space];
try_next = FALSE;
break;
case OP_NOT_WORDCHAR:
for (c = 0; c < 32; c++)
start_bits[c] |= ~cd->cbits[c + cbit_word];
try_next = FALSE;
break;
case OP_WORDCHAR:
for (c = 0; c < 32; c++)
start_bits[c] |= cd->cbits[c + cbit_word];
try_next = FALSE;
break;
/* One or more character type fudges the pointer and restarts, knowing
it will hit a single character type and stop there. */
case OP_TYPEPLUS:
case OP_TYPEMINPLUS:
tcode++;
break;
case OP_TYPEEXACT:
tcode += 3;
break;
/* Zero or more repeats of character types set the bits and then
try again. */
case OP_TYPEUPTO:
case OP_TYPEMINUPTO:
tcode += 2; /* Fall through */
case OP_TYPESTAR:
case OP_TYPEMINSTAR:
case OP_TYPEQUERY:
case OP_TYPEMINQUERY:
switch (tcode[1]) {
case OP_ANY:
return FALSE;
case OP_NOT_DIGIT:
for (c = 0; c < 32; c++)
start_bits[c] |= ~cd->cbits[c + cbit_digit];
break;
case OP_DIGIT:
for (c = 0; c < 32; c++)
start_bits[c] |= cd->cbits[c + cbit_digit];
break;
case OP_NOT_WHITESPACE:
for (c = 0; c < 32; c++)
start_bits[c] |= ~cd->cbits[c + cbit_space];
break;
case OP_WHITESPACE:
for (c = 0; c < 32; c++)
start_bits[c] |= cd->cbits[c + cbit_space];
break;
case OP_NOT_WORDCHAR:
for (c = 0; c < 32; c++)
start_bits[c] |= ~cd->cbits[c + cbit_word];
break;
case OP_WORDCHAR:
for (c = 0; c < 32; c++)
start_bits[c] |= cd->cbits[c + cbit_word];
break;
}
tcode += 2;
break;
/* Character class where all the information is in a bit map: set the
bits and either carry on or not, according to the repeat count. If it was
a negative class, and we are operating with UTF-8 characters, any byte
with a value >= 0xc4 is a potentially valid starter because it starts a
character with a value > 255. */
case OP_NCLASS:
if (utf8) {
start_bits[24] |= 0xf0; /* Bits for 0xc4 - 0xc8 */
memset(start_bits + 25, 0xff, 7); /* Bits for 0xc9 - 0xff */
}
/* Fall through */
case OP_CLASS:
{
tcode++;
/* In UTF-8 mode, the bits in a bit map correspond to character
values, not to byte values. However, the bit map we are constructing is
for byte values. So we have to do a conversion for characters whose
value is > 127. In fact, there are only two possible starting bytes for
characters in the range 128 - 255. */
if (utf8) {
for (c = 0; c < 16; c++)
start_bits[c] |= tcode[c];
for (c = 128; c < 256; c++) {
if ((tcode[c / 8] && (1 << (c & 7))) != 0) {
int d = (c >> 6) | 0xc0; /* Set bit for this starter */
start_bits[d / 8] |= (1 << (d & 7)); /* and then skip on to the */
c = (c & 0xc0) + 0x40 - 1; /* next relevant character. */
}
}
}
/* In non-UTF-8 mode, the two bit maps are completely compatible. */
else {
for (c = 0; c < 32; c++)
start_bits[c] |= tcode[c];
}
/* Advance past the bit map, and act on what follows */
tcode += 32;
switch (*tcode) {
case OP_CRSTAR:
case OP_CRMINSTAR:
case OP_CRQUERY:
case OP_CRMINQUERY:
tcode++;
break;
case OP_CRRANGE:
case OP_CRMINRANGE:
if (((tcode[1] << 8) + tcode[2]) == 0)
tcode += 5;
else
try_next = FALSE;
break;
default:
try_next = FALSE;
break;
}
}
break; /* End of bitmap class handling */
} /* End of switch */
} /* End of try_next loop */
code += GET(code, 1); /* Advance to next branch */
}
while (*code == OP_ALT);
return TRUE;
}
/*************************************************
* Study a compiled expression *
*************************************************/
/* This function is handed a compiled expression that it must study to produce
information that will speed up the matching. It returns a pcre_extra block
which then gets handed back to pcre_exec().
Arguments:
re points to the compiled expression
options contains option bits
errorptr points to where to place error messages;
set NULL unless error
Returns: pointer to a pcre_extra block, with study_data filled in and the
appropriate flag set;
NULL on error or if no optimization possible
*/
pcre_extra *
pcre_study(const pcre * external_re, int options, const char **errorptr)
{
uschar start_bits[32];
pcre_extra *extra;
pcre_study_data *study;
const uschar *tables;
const real_pcre *re = (const real_pcre *) external_re;
uschar *code = (uschar *) re + re->name_table_offset +
(re->name_count * re->name_entry_size);
compile_data compile_block;
*errorptr = NULL;
if (re == NULL || re->magic_number != MAGIC_NUMBER) {
*errorptr = "argument is not a compiled regular expression";
return NULL;
}
if ((options & ~PUBLIC_STUDY_OPTIONS) != 0) {
*errorptr = "unknown or incorrect option bit(s) set";
return NULL;
}
/* For an anchored pattern, or an unanchored pattern that has a first char, or
a multiline pattern that matches only at "line starts", no further processing
at present. */
if ((re->options & (PCRE_ANCHORED | PCRE_FIRSTSET | PCRE_STARTLINE)) != 0)
return NULL;
/* Set the character tables in the block that is passed around */
tables = re->tables;
if (tables == NULL)
(void) pcre_fullinfo(external_re, NULL, PCRE_INFO_DEFAULT_TABLES,
(void *) (&tables));
compile_block.lcc = tables + lcc_offset;
compile_block.fcc = tables + fcc_offset;
compile_block.cbits = tables + cbits_offset;
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;
/* 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
data set later by the calling program. At the moment, the size of
pcre_study_data is fixed. We nevertheless save it in a field for returning via
the pcre_fullinfo() function so that if it becomes variable in the future, we
don't have to change that code. */
extra = (pcre_extra *) malloc(sizeof(pcre_extra) + sizeof(pcre_study_data));
if (extra == NULL) {
*errorptr = "failed to get memory";
return NULL;
}
study = (pcre_study_data *) ((char *) extra + sizeof(pcre_extra));
extra->flags = PCRE_EXTRA_STUDY_DATA;
extra->study_data = study;
study->size = sizeof(pcre_study_data);
study->options = PCRE_STUDY_MAPPED;
memcpy(study->start_bits, start_bits, sizeof(start_bits));
return extra;
}
/* End of pcre_study.c */
/* pcre_compile.c */
/* This module contains the external function pcre_compile(), along with
supporting internal functions that are not used by other modules. */
/*************************************************
* 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. */
#define BRASTACK_SIZE 200
/* Table for handling escaped characters in the range '0'-'z'. Positive returns
are simple data values; negative values are for special things like \d and so
on. Zero means further processing is needed (for things like \x), or the escape
is invalid. */
#if !EBCDIC /* This is the "normal" table for ASCII systems */
static const short int escapes[] = {
0, 0, 0, 0, 0, 0, 0, 0, /* 0 - 7 */
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_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 */
-ESC_p, 0, ESC_r, -ESC_s, ESC_tee, 0, 0, -ESC_w, /* p - w */
0, 0, -ESC_z /* x - z */
};
#else /* This is the "abnormal" table for EBCDIC systems */
static const short int escapes[] = {
/* 48 */ 0, 0, 0, '.', '<', '(', '+', '|',
/* 50 */ '&', 0, 0, 0, 0, 0, 0, 0,
/* 58 */ 0, 0, '!', '$', '*', ')', ';', '~',
/* 60 */ '-', '/', 0, 0, 0, 0, 0, 0,
/* 68 */ 0, 0, '|', ',', '%', '_', '>', '?',
/* 70 */ 0, 0, 0, 0, 0, 0, 0, 0,
/* 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,
/* 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,
/* B0 */ 0, 0, 0, 0, 0, 0, 0, 0,
/* B8 */ 0, 0, 0, 0, 0, ']', '=', '-',
/* 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,
/* 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,
/* F8 */ 0, 0, 0, 0, 0, 0, 0, 0
};
#endif
/* Tables of names of POSIX character classes and their lengths. The list is
terminated by a zero length entry. The first three must be alpha, upper, lower,
as this is assumed for handling case independence. */
static const char *const posix_names[] = {
"alpha", "lower", "upper",
"alnum", "ascii", "blank", "cntrl", "digit", "graph",
"print", "punct", "space", "word", "xdigit"
};
static const uschar posix_name_lengths[] = {
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 4, 6, 0
};
/* Table of class bit maps for each POSIX class; up to three may be combined
to form the class. The table for [:blank:] is dynamically modified to remove
the vertical space characters. */
static const int posix_class_maps[] = {
cbit_lower, cbit_upper, -1, /* alpha */
cbit_lower, -1, -1, /* lower */
cbit_upper, -1, -1, /* upper */
cbit_digit, cbit_lower, cbit_upper, /* alnum */
cbit_print, cbit_cntrl, -1, /* ascii */
cbit_space, -1, -1, /* blank - a GNU extension */
cbit_cntrl, -1, -1, /* cntrl */
cbit_digit, -1, -1, /* digit */
cbit_graph, -1, -1, /* graph */
cbit_print, -1, -1, /* print */
cbit_punct, -1, -1, /* punct */
cbit_space, -1, -1, /* space */
cbit_word, -1, -1, /* word - a Perl extension */
cbit_xdigit, -1, -1 /* xdigit */
};
/* The texts of compile-time error messages. These are "char *" because they
are passed to the outside world. */
static const char *error_texts[] = {
"no error",
"\\ at end of pattern",
"\\c at end of pattern",
"unrecognized character follows \\",
"numbers out of order in {} quantifier",
/* 5 */
"number too big in {} quantifier",
"missing terminating ] for character class",
"invalid escape sequence in character class",
"range out of order in character class",
"nothing to repeat",
/* 10 */
"operand of unlimited repeat could match the empty string",
"internal error: unexpected repeat",
"unrecognized character after (?",
"POSIX named classes are supported only within a class",
"missing )",
/* 15 */
"reference to non-existent subpattern",
"erroffset passed as NULL",
"unknown option bit(s) set",
"missing ) after comment",
"parentheses nested too deeply",
/* 20 */
"regular expression too large",
"failed to get memory",
"unmatched parentheses",
"internal error: code overflow",
"unrecognized character after (?<",
/* 25 */
"lookbehind assertion is not fixed length",
"malformed number after (?(",
"conditional group contains more than two branches",
"assertion expected after (?(",
"(?R or (?digits must be followed by )",
/* 30 */
"unknown POSIX class name",
"POSIX collating elements are not supported",
"this version of PCRE is not compiled with PCRE_UTF8 support",
"spare error",
"character value in \\x{...} sequence is too large",
/* 35 */
"invalid condition (?(0)",
"\\C not allowed in lookbehind assertion",
"PCRE does not support \\L, \\l, \\N, \\U, or \\u",
"number after (?C is > 255",
"closing ) for (?C expected",
/* 40 */
"recursive call could loop indefinitely",
"unrecognized character after (?P",
"syntax error after (?P",
"two named groups 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"
};
/* Table to identify digits and hex digits. This is used when compiling
patterns. Note that the tables in chartables are dependent on the locale, and
may mark arbitrary characters as digits - but the PCRE compiling code expects
to handle only 0-9, a-z, and A-Z as digits when compiling. That is why we have
a private table here. It costs 256 bytes, but it is a lot faster than doing
character value tests (at least in some simple cases I timed), and in some
applications one wants PCRE to compile efficiently as well as match
efficiently.
For convenience, we use the same bit definitions as in chartables:
0x04 decimal digit
0x08 hexadecimal digit
Then we can use ctype_digit and ctype_xdigit in the code. */
#if !EBCDIC /* This is the "normal" case, for ASCII systems */
static const unsigned char digitab[] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0- 7 */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 8- 15 */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 16- 23 */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 24- 31 */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* - ' */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ( - / */
0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, /* 0 - 7 */
0x0c, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 8 - ? */
0x00, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x00, /* @ - G */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* H - O */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* P - W */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* X - _ */
0x00, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x00, /* ` - g */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* h - o */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* p - w */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* x -127 */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 128-135 */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 136-143 */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 144-151 */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 152-159 */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 160-167 */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 168-175 */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 176-183 */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 184-191 */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 192-199 */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 200-207 */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 208-215 */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 216-223 */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 224-231 */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 232-239 */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 240-247 */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
}; /* 248-255 */
#else /* This is the "abnormal" case, for EBCDIC systems */
static const unsigned char digitab[] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0- 7 0 */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 8- 15 */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 16- 23 10 */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 24- 31 */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 32- 39 20 */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 40- 47 */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 48- 55 30 */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 56- 63 */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* - 71 40 */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 72- | */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* & - 87 50 */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 88- ¬ */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* - -103 60 */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 104- ? */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 112-119 70 */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 120- " */
0x00, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x00, /* 128- g 80 */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* h -143 */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 144- p 90 */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* q -159 */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 160- x A0 */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* y -175 */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ^ -183 B0 */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 184-191 */
0x00, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x00, /* { - G C0 */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* H -207 */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* } - P D0 */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* Q -223 */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* \ - X E0 */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* Y -239 */
0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, /* 0 - 7 F0 */
0x0c, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
}; /* 8 -255 */
static const unsigned char ebcdic_chartab[] = { /* chartable partial dup */
0x80, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, /* 0- 7 */
0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, /* 8- 15 */
0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, /* 16- 23 */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 24- 31 */
0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, /* 32- 39 */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 40- 47 */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 48- 55 */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 56- 63 */
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* - 71 */
0x00, 0x00, 0x00, 0x80, 0x00, 0x80, 0x80, 0x80, /* 72- | */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* & - 87 */
0x00, 0x00, 0x00, 0x80, 0x80, 0x80, 0x00, 0x00, /* 88- ¬ */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* - -103 */
0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x80, /* 104- ? */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 112-119 */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 120- " */
0x00, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x12, /* 128- g */
0x12, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* h -143 */
0x00, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, /* 144- p */
0x12, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* q -159 */
0x00, 0x00, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, /* 160- x */
0x12, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* y -175 */
0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ^ -183 */
0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, /* 184-191 */
0x80, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x12, /* { - G */
0x12, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* H -207 */
0x00, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, /* } - P */
0x12, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* Q -223 */
0x00, 0x00, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, /* \ - X */
0x12, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* Y -239 */
0x1c, 0x1c, 0x1c, 0x1c, 0x1c, 0x1c, 0x1c, 0x1c, /* 0 - 7 */
0x1c, 0x1c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
}; /* 8 -255 */
#endif
/* Definition to allow mutual recursion */
static BOOL
compile_regex(int, int, int *, uschar **, const uschar **, int *, BOOL, int,
int *, int *, branch_chain *, compile_data *);
/*************************************************
* Handle escapes *
*************************************************/
/* 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.
Arguments:
ptrptr points to the pattern position pointer
errorcodeptr points to the errorcode variable
bracount number of previous extracting brackets
options the options bits
isclass TRUE if inside a character class
Returns: zero or positive => a data character
negative => a special escape sequence
on error, errorptr is set
*/
static int
check_escape(const uschar **ptrptr, int *errorcodeptr, int bracount,
int options, BOOL isclass)
{
const uschar *ptr = *ptrptr;
int c, i;
/* If backslash is at the end of the pattern, it's an error. */
c = *(++ptr);
if (c == 0)
*errorcodeptr = ERR1;
/* Non-alphamerics are literals. For digits or letters, do an initial lookup in
a table. A non-zero result is something that can be returned immediately.
Otherwise further processing may be required. */
#if !EBCDIC /* ASCII coding */
else if (c < '0' || c > 'z') {
} /* Not alphameric */
else if ((i = escapes[c - '0']) != 0)
c = i;
#else /* EBCDIC coding */
else if (c < 'a' || (ebcdic_chartab[c] & 0x0E) == 0) {
} /* Not alphameric */
else if ((i = escapes[c - 0x48]) != 0)
c = i;
#endif
/* Escapes that need further processing, or are illegal. */
else {
const uschar *oldptr;
switch (c) {
/* A number of Perl escapes are not handled by PCRE. We give an explicit
error. */
case 'l':
case 'L':
case 'N':
case 'u':
case 'U':
*errorcodeptr = ERR37;
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:
Outside a character class, the digits are read as a decimal number. If the
number is less than 10, or if there are that many previous extracting
left brackets, then it is a back reference. Otherwise, up to three octal
digits are read to form an escaped byte. Thus \123 is likely to be octal
123 (cf \0123, which is octal 012 followed by the literal 3). If the octal
value is greater than 377, the least significant 8 bits are taken. Inside a
character class, \ followed by a digit is always an octal number. */
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
if (!isclass) {
oldptr = ptr;
c -= '0';
while ((digitab[ptr[1]] & ctype_digit) != 0)
c = c * 10 + *(++ptr) - '0';
if (c < 10 || c <= bracount) {
c = -(ESC_REF + c);
break;
}
ptr = oldptr; /* Put the pointer back and fall through */
}
/* Handle an octal number following \. If the first digit is 8 or 9, Perl
generates a binary zero byte and treats the digit as a following literal.
Thus we have to pull back the pointer by one. */
if ((c = *ptr) >= '8') {
ptr--;
c = 0;
break;
}
/* \0 always starts an octal number, but we may drop through to here with a
larger first octal digit. */
case '0':
c -= '0';
while (i++ < 2 && ptr[1] >= '0' && ptr[1] <= '7')
c = c * 8 + *(++ptr) - '0';
c &= 255; /* Take least significant 8 bits */
break;
/* \x is complicated when UTF-8 is enabled. \x{ddd} is a character number
which can be greater than 0xff, but only if the ddd are hex digits. */
case 'x':
/* Read just a single hex char */
c = 0;
while (i++ < 2 && (digitab[ptr[1]] & ctype_xdigit) != 0) {
int cc; /* Some compilers don't like ++ */
cc = *(++ptr); /* in initializers */
#if !EBCDIC /* ASCII coding */
if (cc >= 'a')
cc -= 32; /* Convert to upper case */
c = c * 16 + cc - ((cc < 'A') ? '0' : ('A' - 10));
#else /* EBCDIC coding */
if (cc <= 'z')
cc += 64; /* Convert to upper case */
c = c * 16 + cc - ((cc >= '0') ? '0' : ('A' - 10));
#endif
}
break;
/* Other special escapes not starting with a digit are straightforward */
case 'c':
c = *(++ptr);
if (c == 0) {
*errorcodeptr = ERR2;
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;
#else /* EBCDIC coding */
if (c >= 'a' && c <= 'z')
c += 64;
c ^= 0xC0;
#endif
break;
/* PCRE_EXTRA enables extensions to Perl in the matter of escapes. Any
other alphameric following \ is an error if PCRE_EXTRA was set; otherwise,
for Perl compatibility, it is a literal. This code looks a bit odd, but
there used to be some cases other than the default, and there may be again
in future, so I haven't "optimized" it. */
default:
if ((options & PCRE_EXTRA) != 0)
switch (c) {
default:
*errorcodeptr = ERR3;
break;
}
break;
}
}
*ptrptr = ptr;
return c;
}
#ifdef SUPPORT_UCP
/*************************************************
* Handle \P and \p *
*************************************************/
/* This function is called after \P or \p has been encountered, provided that
PCRE is compiled with support for Unicode properties. On entry, ptrptr is
pointing at the P or p. On exit, it is pointing at the final character of the
escape sequence.
Argument:
ptrptr points to the pattern position pointer
negptr points to a boolean that is set TRUE for negation else FALSE
errorcodeptr points to the error code variable
Returns: value from ucp_type_table, or -1 for an invalid type
*/
static int
get_ucp(const uschar **ptrptr, BOOL *negptr, int *errorcodeptr)
{
int c, i, bot, top;
const uschar *ptr = *ptrptr;
char name[4];
c = *(++ptr);
if (c == 0)
goto ERROR_RETURN;
*negptr = FALSE;
/* \P or \p can be followed by a one- or two-character name in {}, optionally
preceded by ^ for negation. */
if (c == '{') {
if (ptr[1] == '^') {
*negptr = TRUE;
ptr++;
}
for (i = 0; i <= 2; i++) {
c = *(++ptr);
if (c == 0)
goto ERROR_RETURN;
if (c == '}')
break;
name[i] = c;
}
if (c != '}') { /* Try to distinguish error cases */
while (*(++ptr) != 0 && *ptr != '}') ;
if (*ptr == '}')
goto UNKNOWN_RETURN;
else
goto ERROR_RETURN;
}
name[i] = 0;
}
/* Otherwise there is just one following character */
else {
name[0] = c;
name[1] = 0;
}
*ptrptr = ptr;
/* Search for a recognized property name using binary chop */
bot = 0;
top = _pcre_utt_size;
while (bot < top) {
i = (bot + top) / 2;
c = strcmp(name, _pcre_utt[i].name);
if (c == 0)
return _pcre_utt[i].value;
if (c > 0)
bot = i + 1;
else
top = i;
}
UNKNOWN_RETURN:
*errorcodeptr = ERR47;
*ptrptr = ptr;
return -1;
ERROR_RETURN:
*errorcodeptr = ERR46;
*ptrptr = ptr;
return -1;
}
#endif
/*************************************************
* Check for counted repeat *
*************************************************/
/* This function is called when a '{' is encountered in a place where it might
start a quantifier. It looks ahead to see if it really is a quantifier or not.
It is only a quantifier if it is one of the forms {ddd} {ddd,} or {ddd,ddd}
where the ddds are digits.
Arguments:
p pointer to the first char after '{'
Returns: TRUE or FALSE
*/
static BOOL
is_counted_repeat(const uschar *p)
{
if ((digitab[*p++] & ctype_digit) == 0)
return FALSE;
while ((digitab[*p] & ctype_digit) != 0)
p++;
if (*p == '}')
return TRUE;
if (*p++ != ',')
return FALSE;
if (*p == '}')
return TRUE;
if ((digitab[*p++] & ctype_digit) == 0)
return FALSE;
while ((digitab[*p] & ctype_digit) != 0)
p++;
return (*p == '}');
}
/*************************************************
* Read repeat counts *
*************************************************/
/* Read an item of the form {n,m} and return the values. This is called only
after is_counted_repeat() has confirmed that a repeat-count quantifier exists,
so the syntax is guaranteed to be correct, but we need to check the values.
Arguments:
p pointer to first char after '{'
minp pointer to int for min
maxp pointer to int for max
returned as -1 if no max
errorcodeptr points to error code variable
Returns: pointer to '}' on success;
current ptr on error, with errorcodeptr set non-zero
*/
static const uschar *
read_repeat_counts(const uschar *p, int *minp, int *maxp, int *errorcodeptr)
{
int min = 0;
int max = -1;
/* Read the minimum value and do a paranoid check: a negative value indicates
an integer overflow. */
while ((digitab[*p] & ctype_digit) != 0)
min = min * 10 + *p++ - '0';
if (min < 0 || min > 65535) {
*errorcodeptr = ERR5;
return p;
}
/* Read the maximum value if there is one, and again do a paranoid on its size.
Also, max must not be less than min. */
if (*p == '}')
max = min;
else {
if (*(++p) != '}') {
max = 0;
while ((digitab[*p] & ctype_digit) != 0)
max = max * 10 + *p++ - '0';
if (max < 0 || max > 65535) {
*errorcodeptr = ERR5;
return p;
}
if (max < min) {
*errorcodeptr = ERR4;
return p;
}
}
}
/* Fill in the required variables, and pass back the pointer to the terminating
'}'. */
*minp = min;
*maxp = max;
return p;
}
/*************************************************
* Find first significant op code *
*************************************************/
/* This is called by several functions that scan a compiled expression looking
for a fixed first character, or an anchoring op code etc. It skips over things
that do not influence this. For some calls, a change of option is important.
For some calls, it makes sense to skip negative forward and all backward
assertions, and also the \b assertion; for others it does not.
Arguments:
code pointer to the start of the group
options pointer to external options
optbit the option bit whose changing is significant, or
zero if none are
skipassert TRUE if certain assertions are to be skipped
Returns: pointer to the first significant opcode
*/
static const uschar *
first_significant_code(const uschar *code, int *options, int optbit,
BOOL skipassert)
{
for (;;) {
switch ((int) *code) {
case OP_OPT:
if (optbit > 0 && ((int) code[1] & optbit) != (*options & optbit))
*options = (int) code[1];
code += 2;
break;
case OP_ASSERT_NOT:
case OP_ASSERTBACK:
case OP_ASSERTBACK_NOT:
if (!skipassert)
return code;
do
code += GET(code, 1);
while (*code == OP_ALT);
code += _pcre_OP_lengths[*code];
break;
case OP_WORD_BOUNDARY:
case OP_NOT_WORD_BOUNDARY:
if (!skipassert)
return code;
/* Fall through */
case OP_CALLOUT:
case OP_CREF:
case OP_BRANUMBER:
code += _pcre_OP_lengths[*code];
break;
default:
return code;
}
}
/* Control never reaches here */
}
/*************************************************
* Find the fixed length of a pattern *
*************************************************/
/* Scan a pattern and compute the fixed length of subject that will match it,
if the length is fixed. This is needed for dealing with backward assertions.
In UTF8 mode, the result is in characters rather than bytes.
Arguments:
code points to the start of the pattern (the bracket)
options the compiling options
Returns: the fixed length, or -1 if there is no fixed length,
or -2 if \C was encountered
*/
static int
find_fixedlength(uschar *code, int options)
{
int length = -1;
register int branchlength = 0;
register uschar *cc = code + 1 + LINK_SIZE;
/* Scan along the opcodes for this branch. If we get to the end of the
branch, check the length against that of the other branches. */
for (;;) {
int d;
register int op = *cc;
if (op >= OP_BRA)
op = OP_BRA;
switch (op) {
case OP_BRA:
case OP_ONCE:
case OP_COND:
d = find_fixedlength(cc, options);
if (d < 0)
return d;
branchlength += d;
do
cc += GET(cc, 1);
while (*cc == OP_ALT);
cc += 1 + LINK_SIZE;
break;
/* Reached end of a branch; if it's a ket it is the end of a nested
call. If it's ALT it is an alternation in a nested call. If it is
END it's the end of the outer call. All can be handled by the same code. */
case OP_ALT:
case OP_KET:
case OP_KETRMAX:
case OP_KETRMIN:
case OP_END:
if (length < 0)
length = branchlength;
else if (length != branchlength)
return -1;
if (*cc != OP_ALT)
return length;
cc += 1 + LINK_SIZE;
branchlength = 0;
break;
/* Skip over assertive subpatterns */
case OP_ASSERT:
case OP_ASSERT_NOT:
case OP_ASSERTBACK:
case OP_ASSERTBACK_NOT:
do
cc += GET(cc, 1);
while (*cc == OP_ALT);
/* Fall through */
/* Skip over things that don't match chars */
case OP_REVERSE:
case OP_BRANUMBER:
case OP_CREF:
case OP_OPT:
case OP_CALLOUT:
case OP_SOD:
case OP_SOM:
case OP_EOD:
case OP_EODN:
case OP_CIRC:
case OP_DOLL:
case OP_NOT_WORD_BOUNDARY:
case OP_WORD_BOUNDARY:
cc += _pcre_OP_lengths[*cc];
break;
/* Handle literal characters */
case OP_CHAR:
case OP_CHARNC:
branchlength++;
cc += 2;
break;
/* Handle exact repetitions. The count is already in characters, but we
Showing the first 100 KB of 303.5 KB. Download the archive for the full file.