Path: blob/master/thirdparty/pcre2/src/pcre2_intmodedep.h
9898 views
/*************************************************1* Perl-Compatible Regular Expressions *2*************************************************/34/* PCRE is a library of functions to support regular expressions whose syntax5and semantics are as close as possible to those of the Perl 5 language.67Written by Philip Hazel8Original API code Copyright (c) 1997-2012 University of Cambridge9New API code Copyright (c) 2016-2024 University of Cambridge1011-----------------------------------------------------------------------------12Redistribution and use in source and binary forms, with or without13modification, are permitted provided that the following conditions are met:1415* Redistributions of source code must retain the above copyright notice,16this list of conditions and the following disclaimer.1718* Redistributions in binary form must reproduce the above copyright19notice, this list of conditions and the following disclaimer in the20documentation and/or other materials provided with the distribution.2122* Neither the name of the University of Cambridge nor the names of its23contributors may be used to endorse or promote products derived from24this software without specific prior written permission.2526THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"27AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE28IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE29ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE30LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR31CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF32SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS33INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN34CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)35ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE36POSSIBILITY OF SUCH DAMAGE.37-----------------------------------------------------------------------------38*/394041/* This module contains mode-dependent macro and structure definitions. The42file is #included by pcre2_internal.h if PCRE2_CODE_UNIT_WIDTH is defined.43These mode-dependent items are kept in a separate file so that they can also be44#included multiple times for different code unit widths by pcre2test in order45to have access to the hidden structures at all supported widths.4647Some of the mode-dependent macros are required at different widths for48different parts of the pcre2test code (in particular, the included49pcre2_printint.c file). We undefine them here so that they can be re-defined for50multiple inclusions. Not all of these are used in pcre2test, but it's easier51just to undefine them all. */5253#undef ACROSSCHAR54#undef BACKCHAR55#undef BYTES2CU56#undef CHMAX_25557#undef CU2BYTES58#undef FORWARDCHAR59#undef FORWARDCHARTEST60#undef GET61#undef GET262#undef GETCHAR63#undef GETCHARINC64#undef GETCHARINCTEST65#undef GETCHARLEN66#undef GETCHARLENTEST67#undef GETCHARTEST68#undef GET_EXTRALEN69#undef HAS_EXTRALEN70#undef IMM2_SIZE71#undef MAX_25572#undef MAX_MARK73#undef MAX_PATTERN_SIZE74#undef MAX_UTF_SINGLE_CU75#undef NOT_FIRSTCU76#undef PUT77#undef PUT278#undef PUT2INC79#undef PUTCHAR80#undef PUTINC81#undef TABLE_GET82838485/* -------------------------- MACROS ----------------------------- */8687/* PCRE keeps offsets in its compiled code as at least 16-bit quantities88(always stored in big-endian order in 8-bit mode) by default. These are used,89for example, to link from the start of a subpattern to its alternatives and its90end. The use of 16 bits per offset limits the size of an 8-bit compiled regex91to around 64K, which is big enough for almost everybody. However, I received a92request for an even bigger limit. For this reason, and also to make the code93easier to maintain, the storing and loading of offsets from the compiled code94unit string is now handled by the macros that are defined here.9596The macros are controlled by the value of LINK_SIZE. This defaults to 2, but97values of 3 or 4 are also supported. */9899/* ------------------- 8-bit support ------------------ */100101#if PCRE2_CODE_UNIT_WIDTH == 8102103#if LINK_SIZE == 2104#define PUT(a,n,d) \105(a[n] = (PCRE2_UCHAR)((d) >> 8)), \106(a[(n)+1] = (PCRE2_UCHAR)((d) & 255))107#define GET(a,n) \108(unsigned int)(((a)[n] << 8) | (a)[(n)+1])109#define MAX_PATTERN_SIZE (1 << 16)110111#elif LINK_SIZE == 3112#define PUT(a,n,d) \113(a[n] = (PCRE2_UCHAR)((d) >> 16)), \114(a[(n)+1] = (PCRE2_UCHAR)((d) >> 8)), \115(a[(n)+2] = (PCRE2_UCHAR)((d) & 255))116#define GET(a,n) \117(unsigned int)(((a)[n] << 16) | ((a)[(n)+1] << 8) | (a)[(n)+2])118#define MAX_PATTERN_SIZE (1 << 24)119120#elif LINK_SIZE == 4121#define PUT(a,n,d) \122(a[n] = (PCRE2_UCHAR)((d) >> 24)), \123(a[(n)+1] = (PCRE2_UCHAR)((d) >> 16)), \124(a[(n)+2] = (PCRE2_UCHAR)((d) >> 8)), \125(a[(n)+3] = (PCRE2_UCHAR)((d) & 255))126#define GET(a,n) \127(unsigned int)(((a)[n] << 24) | ((a)[(n)+1] << 16) | ((a)[(n)+2] << 8) | (a)[(n)+3])128#define MAX_PATTERN_SIZE (1 << 30) /* Keep it positive */129130#else131#error LINK_SIZE must be 2, 3, or 4132#endif133134135/* ------------------- 16-bit support ------------------ */136137#elif PCRE2_CODE_UNIT_WIDTH == 16138139#if LINK_SIZE == 2140#undef LINK_SIZE141#define LINK_SIZE 1142#define PUT(a,n,d) \143(a[n] = (PCRE2_UCHAR)(d))144#define GET(a,n) \145(a[n])146#define MAX_PATTERN_SIZE (1 << 16)147148#elif LINK_SIZE == 3 || LINK_SIZE == 4149#undef LINK_SIZE150#define LINK_SIZE 2151#define PUT(a,n,d) \152(a[n] = (PCRE2_UCHAR)((d) >> 16)), \153(a[(n)+1] = (PCRE2_UCHAR)((d) & 65535))154#define GET(a,n) \155(unsigned int)(((a)[n] << 16) | (a)[(n)+1])156#define MAX_PATTERN_SIZE (1 << 30) /* Keep it positive */157158#else159#error LINK_SIZE must be 2, 3, or 4160#endif161162163/* ------------------- 32-bit support ------------------ */164165#elif PCRE2_CODE_UNIT_WIDTH == 32166#undef LINK_SIZE167#define LINK_SIZE 1168#define PUT(a,n,d) \169(a[n] = (d))170#define GET(a,n) \171(a[n])172#define MAX_PATTERN_SIZE (1 << 30) /* Keep it positive */173174#else175#error Unsupported compiling mode176#endif177178179/* --------------- Other mode-specific macros ----------------- */180181/* PCRE uses some other (at least) 16-bit quantities that do not change when182the size of offsets changes. There are used for repeat counts and for other183things such as capturing parenthesis numbers in back references.184185Define the number of code units required to hold a 16-bit count/offset, and186macros to load and store such a value. For reasons that I do not understand,187the expression in the 8-bit GET2 macro is treated by gcc as a signed188expression, even when a is declared as unsigned. It seems that any kind of189arithmetic results in a signed value. Hence the cast. */190191#if PCRE2_CODE_UNIT_WIDTH == 8192#define IMM2_SIZE 2193#define GET2(a,n) (unsigned int)(((a)[n] << 8) | (a)[(n)+1])194#define PUT2(a,n,d) a[n] = (d) >> 8, a[(n)+1] = (d) & 255195196#else /* Code units are 16 or 32 bits */197#define IMM2_SIZE 1198#define GET2(a,n) a[n]199#define PUT2(a,n,d) a[n] = d200#endif201202/* Other macros that are different for 8-bit mode. The MAX_255 macro checks203whether its argument, which is assumed to be one code unit, is less than 256.204The CHMAX_255 macro does not assume one code unit. The maximum length of a MARK205name must fit in one code unit; currently it is set to 255 or 65535. The206TABLE_GET macro is used to access elements of tables containing exactly 256207items. Its argument is a code unit. When code points can be greater than 255, a208check is needed before accessing these tables. */209210#if PCRE2_CODE_UNIT_WIDTH == 8211#define MAX_255(c) TRUE212#define MAX_MARK ((1u << 8) - 1)213#define TABLE_GET(c, table, default) ((table)[c])214#ifdef SUPPORT_UNICODE215#define SUPPORT_WIDE_CHARS216#define CHMAX_255(c) ((c) <= 255u)217#else218#define CHMAX_255(c) TRUE219#endif /* SUPPORT_UNICODE */220221#else /* Code units are 16 or 32 bits */222#define CHMAX_255(c) ((c) <= 255u)223#define MAX_255(c) ((c) <= 255u)224#define MAX_MARK ((1u << 16) - 1)225#define SUPPORT_WIDE_CHARS226#define TABLE_GET(c, table, default) (MAX_255(c)? ((table)[c]):(default))227#endif228229230/* ----------------- Character-handling macros ----------------- */231232/* There is a proposed future special "UTF-21" mode, in which only the lowest23321 bits of a 32-bit character are interpreted as UTF, with the remaining 11234high-order bits available to the application for other uses. In preparation for235the future implementation of this mode, there are macros that load a data item236and, if in this special mode, mask it to 21 bits. These macros all have names237starting with UCHAR21. In all other modes, including the normal 32-bit238library, the macros all have the same simple definitions. When the new mode is239implemented, it is expected that these definitions will be varied appropriately240using #ifdef when compiling the library that supports the special mode. */241242#define UCHAR21(eptr) (*(eptr))243#define UCHAR21TEST(eptr) (*(eptr))244#define UCHAR21INC(eptr) (*(eptr)++)245#define UCHAR21INCTEST(eptr) (*(eptr)++)246247/* When UTF encoding is being used, a character is no longer just a single248byte in 8-bit mode or a single short in 16-bit mode. The macros for character249handling generate simple sequences when used in the basic mode, and more250complicated ones for UTF characters. GETCHARLENTEST and other macros are not251used when UTF is not supported. To make sure they can never even appear when252UTF support is omitted, we don't even define them. */253254#ifndef SUPPORT_UNICODE255256/* #define MAX_UTF_SINGLE_CU */257/* #define HAS_EXTRALEN(c) */258/* #define GET_EXTRALEN(c) */259/* #define NOT_FIRSTCU(c) */260#define GETCHAR(c, eptr) c = *eptr;261#define GETCHARTEST(c, eptr) c = *eptr;262#define GETCHARINC(c, eptr) c = *eptr++;263#define GETCHARINCTEST(c, eptr) c = *eptr++;264#define GETCHARLEN(c, eptr, len) c = *eptr;265#define PUTCHAR(c, p) (*p = c, 1)266/* #define GETCHARLENTEST(c, eptr, len) */267/* #define BACKCHAR(eptr) */268/* #define FORWARDCHAR(eptr) */269/* #define FORWARCCHARTEST(eptr,end) */270/* #define ACROSSCHAR(condition, eptr, action) */271272#else /* SUPPORT_UNICODE */273274/* ------------------- 8-bit support ------------------ */275276#if PCRE2_CODE_UNIT_WIDTH == 8277#define MAYBE_UTF_MULTI /* UTF chars may use multiple code units */278279/* The largest UTF code point that can be encoded as a single code unit. */280281#define MAX_UTF_SINGLE_CU 127282283/* Tests whether the code point needs extra characters to decode. */284285#define HAS_EXTRALEN(c) HASUTF8EXTRALEN(c)286287/* Returns with the additional number of characters if IS_MULTICHAR(c) is TRUE.288Otherwise it has an undefined behaviour. */289290#define GET_EXTRALEN(c) (PRIV(utf8_table4)[(c) & 0x3fu])291292/* Returns TRUE, if the given value is not the first code unit of a UTF293sequence. */294295#define NOT_FIRSTCU(c) (((c) & 0xc0u) == 0x80u)296297/* Get the next UTF-8 character, not advancing the pointer. This is called when298we know we are in UTF-8 mode. */299300#define GETCHAR(c, eptr) \301c = *eptr; \302if (c >= 0xc0u) GETUTF8(c, eptr);303304/* Get the next UTF-8 character, testing for UTF-8 mode, and not advancing the305pointer. */306307#define GETCHARTEST(c, eptr) \308c = *eptr; \309if (utf && c >= 0xc0u) GETUTF8(c, eptr);310311/* Get the next UTF-8 character, advancing the pointer. This is called when we312know we are in UTF-8 mode. */313314#define GETCHARINC(c, eptr) \315c = *eptr++; \316if (c >= 0xc0u) GETUTF8INC(c, eptr);317318/* Get the next character, testing for UTF-8 mode, and advancing the pointer.319This is called when we don't know if we are in UTF-8 mode. */320321#define GETCHARINCTEST(c, eptr) \322c = *eptr++; \323if (utf && c >= 0xc0u) GETUTF8INC(c, eptr);324325/* Get the next UTF-8 character, not advancing the pointer, incrementing length326if there are extra bytes. This is called when we know we are in UTF-8 mode. */327328#define GETCHARLEN(c, eptr, len) \329c = *eptr; \330if (c >= 0xc0u) GETUTF8LEN(c, eptr, len);331332/* Get the next UTF-8 character, testing for UTF-8 mode, not advancing the333pointer, incrementing length if there are extra bytes. This is called when we334do not know if we are in UTF-8 mode. */335336#define GETCHARLENTEST(c, eptr, len) \337c = *eptr; \338if (utf && c >= 0xc0u) GETUTF8LEN(c, eptr, len);339340/* If the pointer is not at the start of a character, move it back until341it is. This is called only in UTF-8 mode - we don't put a test within the macro342because almost all calls are already within a block of UTF-8 only code. */343344#define BACKCHAR(eptr) while((*eptr & 0xc0u) == 0x80u) eptr--345346/* Same as above, just in the other direction. */347#define FORWARDCHAR(eptr) while((*eptr & 0xc0u) == 0x80u) eptr++348#define FORWARDCHARTEST(eptr,end) while(eptr < end && (*eptr & 0xc0u) == 0x80u) eptr++349350/* Same as above, but it allows a fully customizable form. */351#define ACROSSCHAR(condition, eptr, action) \352while((condition) && ((*eptr) & 0xc0u) == 0x80u) action353354/* Deposit a character into memory, returning the number of code units. */355356#define PUTCHAR(c, p) ((utf && c > MAX_UTF_SINGLE_CU)? \357PRIV(ord2utf)(c,p) : (*p = c, 1))358359360/* ------------------- 16-bit support ------------------ */361362#elif PCRE2_CODE_UNIT_WIDTH == 16363#define MAYBE_UTF_MULTI /* UTF chars may use multiple code units */364365/* The largest UTF code point that can be encoded as a single code unit. */366367#define MAX_UTF_SINGLE_CU 65535368369/* Tests whether the code point needs extra characters to decode. */370371#define HAS_EXTRALEN(c) (((c) & 0xfc00u) == 0xd800u)372373/* Returns with the additional number of characters if IS_MULTICHAR(c) is TRUE.374Otherwise it has an undefined behaviour. */375376#define GET_EXTRALEN(c) 1377378/* Returns TRUE, if the given value is not the first code unit of a UTF379sequence. */380381#define NOT_FIRSTCU(c) (((c) & 0xfc00u) == 0xdc00u)382383/* Base macro to pick up the low surrogate of a UTF-16 character, not384advancing the pointer. */385386#define GETUTF16(c, eptr) \387{ c = (((c & 0x3ffu) << 10) | (eptr[1] & 0x3ffu)) + 0x10000u; }388389/* Get the next UTF-16 character, not advancing the pointer. This is called when390we know we are in UTF-16 mode. */391392#define GETCHAR(c, eptr) \393c = *eptr; \394if ((c & 0xfc00u) == 0xd800u) GETUTF16(c, eptr);395396/* Get the next UTF-16 character, testing for UTF-16 mode, and not advancing the397pointer. */398399#define GETCHARTEST(c, eptr) \400c = *eptr; \401if (utf && (c & 0xfc00u) == 0xd800u) GETUTF16(c, eptr);402403/* Base macro to pick up the low surrogate of a UTF-16 character, advancing404the pointer. */405406#define GETUTF16INC(c, eptr) \407{ c = (((c & 0x3ffu) << 10) | (*eptr++ & 0x3ffu)) + 0x10000u; }408409/* Get the next UTF-16 character, advancing the pointer. This is called when we410know we are in UTF-16 mode. */411412#define GETCHARINC(c, eptr) \413c = *eptr++; \414if ((c & 0xfc00u) == 0xd800u) GETUTF16INC(c, eptr);415416/* Get the next character, testing for UTF-16 mode, and advancing the pointer.417This is called when we don't know if we are in UTF-16 mode. */418419#define GETCHARINCTEST(c, eptr) \420c = *eptr++; \421if (utf && (c & 0xfc00u) == 0xd800u) GETUTF16INC(c, eptr);422423/* Base macro to pick up the low surrogate of a UTF-16 character, not424advancing the pointer, incrementing the length. */425426#define GETUTF16LEN(c, eptr, len) \427{ c = (((c & 0x3ffu) << 10) | (eptr[1] & 0x3ffu)) + 0x10000u; len++; }428429/* Get the next UTF-16 character, not advancing the pointer, incrementing430length if there is a low surrogate. This is called when we know we are in431UTF-16 mode. */432433#define GETCHARLEN(c, eptr, len) \434c = *eptr; \435if ((c & 0xfc00u) == 0xd800u) GETUTF16LEN(c, eptr, len);436437/* Get the next UTF-16 character, testing for UTF-16 mode, not advancing the438pointer, incrementing length if there is a low surrogate. This is called when439we do not know if we are in UTF-16 mode. */440441#define GETCHARLENTEST(c, eptr, len) \442c = *eptr; \443if (utf && (c & 0xfc00u) == 0xd800u) GETUTF16LEN(c, eptr, len);444445/* If the pointer is not at the start of a character, move it back until446it is. This is called only in UTF-16 mode - we don't put a test within the447macro because almost all calls are already within a block of UTF-16 only448code. */449450#define BACKCHAR(eptr) if ((*eptr & 0xfc00u) == 0xdc00u) eptr--451452/* Same as above, just in the other direction. */453#define FORWARDCHAR(eptr) if ((*eptr & 0xfc00u) == 0xdc00u) eptr++454#define FORWARDCHARTEST(eptr,end) if (eptr < end && (*eptr & 0xfc00u) == 0xdc00u) eptr++455456/* Same as above, but it allows a fully customizable form. */457#define ACROSSCHAR(condition, eptr, action) \458if ((condition) && ((*eptr) & 0xfc00u) == 0xdc00u) action459460/* Deposit a character into memory, returning the number of code units. */461462#define PUTCHAR(c, p) ((utf && c > MAX_UTF_SINGLE_CU)? \463PRIV(ord2utf)(c,p) : (*p = c, 1))464465466/* ------------------- 32-bit support ------------------ */467468#else469470/* These are trivial for the 32-bit library, since all UTF-32 characters fit471into one PCRE2_UCHAR unit. */472473#define MAX_UTF_SINGLE_CU (0x10ffffu)474#define HAS_EXTRALEN(c) (0)475#define GET_EXTRALEN(c) (0)476#define NOT_FIRSTCU(c) (0)477478/* Get the next UTF-32 character, not advancing the pointer. This is called when479we know we are in UTF-32 mode. */480481#define GETCHAR(c, eptr) \482c = *(eptr);483484/* Get the next UTF-32 character, testing for UTF-32 mode, and not advancing the485pointer. */486487#define GETCHARTEST(c, eptr) \488c = *(eptr);489490/* Get the next UTF-32 character, advancing the pointer. This is called when we491know we are in UTF-32 mode. */492493#define GETCHARINC(c, eptr) \494c = *((eptr)++);495496/* Get the next character, testing for UTF-32 mode, and advancing the pointer.497This is called when we don't know if we are in UTF-32 mode. */498499#define GETCHARINCTEST(c, eptr) \500c = *((eptr)++);501502/* Get the next UTF-32 character, not advancing the pointer, not incrementing503length (since all UTF-32 is of length 1). This is called when we know we are in504UTF-32 mode. */505506#define GETCHARLEN(c, eptr, len) \507GETCHAR(c, eptr)508509/* Get the next UTF-32character, testing for UTF-32 mode, not advancing the510pointer, not incrementing the length (since all UTF-32 is of length 1).511This is called when we do not know if we are in UTF-32 mode. */512513#define GETCHARLENTEST(c, eptr, len) \514GETCHARTEST(c, eptr)515516/* If the pointer is not at the start of a character, move it back until517it is. This is called only in UTF-32 mode - we don't put a test within the518macro because almost all calls are already within a block of UTF-32 only519code.520521These are all no-ops since all UTF-32 characters fit into one PCRE2_UCHAR. */522523#define BACKCHAR(eptr) do { } while (0)524525/* Same as above, just in the other direction. */526527#define FORWARDCHAR(eptr) do { } while (0)528#define FORWARDCHARTEST(eptr,end) do { } while (0)529530/* Same as above, but it allows a fully customizable form. */531532#define ACROSSCHAR(condition, eptr, action) do { } while (0)533534/* Deposit a character into memory, returning the number of code units. */535536#define PUTCHAR(c, p) (*p = c, 1)537538#endif /* UTF-32 character handling */539#endif /* SUPPORT_UNICODE */540541542/* Mode-dependent macros that have the same definition in all modes. */543544#define CU2BYTES(x) ((x)*((PCRE2_CODE_UNIT_WIDTH/8)))545#define BYTES2CU(x) ((x)/((PCRE2_CODE_UNIT_WIDTH/8)))546#define PUTINC(a,n,d) PUT(a,n,d), a += LINK_SIZE547#define PUT2INC(a,n,d) PUT2(a,n,d), a += IMM2_SIZE548549550/* ----------------------- HIDDEN STRUCTURES ----------------------------- */551552/* NOTE: All these structures *must* start with a pcre2_memctl structure. The553code that uses them is simpler because it assumes this. */554555/* The real general context structure. At present it holds only data for custom556memory control. */557558/* WARNING: if this is ever changed, code in pcre2_substitute.c will have to be559changed because it builds a general context "by hand" in order to avoid the560malloc() call in pcre2_general_context)_create(). There is also code in561pcre2_match.c that makes the same assumption. */562563typedef struct pcre2_real_general_context {564pcre2_memctl memctl;565} pcre2_real_general_context;566567/* The real compile context structure */568569typedef struct pcre2_real_compile_context {570pcre2_memctl memctl;571int (*stack_guard)(uint32_t, void *);572void *stack_guard_data;573const uint8_t *tables;574PCRE2_SIZE max_pattern_length;575PCRE2_SIZE max_pattern_compiled_length;576uint16_t bsr_convention;577uint16_t newline_convention;578uint32_t parens_nest_limit;579uint32_t extra_options;580uint32_t max_varlookbehind;581uint32_t optimization_flags;582} pcre2_real_compile_context;583584/* The real match context structure. */585586typedef struct pcre2_real_match_context {587pcre2_memctl memctl;588#ifdef SUPPORT_JIT589pcre2_jit_callback jit_callback;590void *jit_callback_data;591#endif592int (*callout)(pcre2_callout_block *, void *);593void *callout_data;594int (*substitute_callout)(pcre2_substitute_callout_block *, void *);595void *substitute_callout_data;596PCRE2_SIZE (*substitute_case_callout)(PCRE2_SPTR, PCRE2_SIZE, PCRE2_UCHAR *,597PCRE2_SIZE, int, void *);598void *substitute_case_callout_data;599PCRE2_SIZE offset_limit;600uint32_t heap_limit;601uint32_t match_limit;602uint32_t depth_limit;603} pcre2_real_match_context;604605/* The real convert context structure. */606607typedef struct pcre2_real_convert_context {608pcre2_memctl memctl;609uint32_t glob_separator;610uint32_t glob_escape;611} pcre2_real_convert_context;612613/* The real compiled code structure. The type for the blocksize field is614defined specially because it is required in pcre2_serialize_decode() when615copying the size from possibly unaligned memory into a variable of the same616type. Use a macro rather than a typedef to avoid compiler warnings when this617file is included multiple times by pcre2test. LOOKBEHIND_MAX specifies the618largest lookbehind that is supported. (OP_REVERSE and OP_VREVERSE in a pattern619have 16-bit arguments in 8-bit and 16-bit modes, so we need no more than a62016-bit field here.) */621622#undef CODE_BLOCKSIZE_TYPE623#define CODE_BLOCKSIZE_TYPE PCRE2_SIZE624625#undef LOOKBEHIND_MAX626#define LOOKBEHIND_MAX UINT16_MAX627628typedef struct pcre2_real_code {629pcre2_memctl memctl; /* Memory control fields */630const uint8_t *tables; /* The character tables */631void *executable_jit; /* Pointer to JIT code */632uint8_t start_bitmap[32]; /* Bitmap for starting code unit < 256 */633CODE_BLOCKSIZE_TYPE blocksize; /* Total (bytes) that was malloc-ed */634CODE_BLOCKSIZE_TYPE code_start; /* Byte code start offset */635uint32_t magic_number; /* Paranoid and endianness check */636uint32_t compile_options; /* Options passed to pcre2_compile() */637uint32_t overall_options; /* Options after processing the pattern */638uint32_t extra_options; /* Taken from compile_context */639uint32_t flags; /* Various state flags */640uint32_t limit_heap; /* Limit set in the pattern */641uint32_t limit_match; /* Limit set in the pattern */642uint32_t limit_depth; /* Limit set in the pattern */643uint32_t first_codeunit; /* Starting code unit */644uint32_t last_codeunit; /* This codeunit must be seen */645uint16_t bsr_convention; /* What \R matches */646uint16_t newline_convention; /* What is a newline? */647uint16_t max_lookbehind; /* Longest lookbehind (characters) */648uint16_t minlength; /* Minimum length of match */649uint16_t top_bracket; /* Highest numbered group */650uint16_t top_backref; /* Highest numbered back reference */651uint16_t name_entry_size; /* Size (code units) of table entries */652uint16_t name_count; /* Number of name entries in the table */653uint32_t optimization_flags; /* Optimizations enabled at compile time */654} pcre2_real_code;655656/* The real match data structure. Define ovector as large as it can ever657actually be so that array bound checkers don't grumble. Memory for this658structure is obtained by calling pcre2_match_data_create(), which sets the size659as the offset of ovector plus a pair of elements for each capturable string, so660the size varies from call to call. As the maximum number of capturing661subpatterns is 65535 we must allow for 65536 strings to include the overall662match. (See also the heapframe structure below.) */663664struct heapframe; /* Forward reference */665666typedef struct pcre2_real_match_data {667pcre2_memctl memctl; /* Memory control fields */668const pcre2_real_code *code; /* The pattern used for the match */669PCRE2_SPTR subject; /* The subject that was matched */670PCRE2_SPTR mark; /* Pointer to last mark */671struct heapframe *heapframes; /* Backtracking frames heap memory */672PCRE2_SIZE heapframes_size; /* Malloc-ed size */673PCRE2_SIZE subject_length; /* Subject length */674PCRE2_SIZE leftchar; /* Offset to leftmost code unit */675PCRE2_SIZE rightchar; /* Offset to rightmost code unit */676PCRE2_SIZE startchar; /* Offset to starting code unit */677uint8_t matchedby; /* Type of match (normal, JIT, DFA) */678uint8_t flags; /* Various flags */679uint16_t oveccount; /* Number of pairs */680int rc; /* The return code from the match */681PCRE2_SIZE ovector[131072]; /* Must be last in the structure */682} pcre2_real_match_data;683684685/* ----------------------- PRIVATE STRUCTURES ----------------------------- */686687/* These structures are not needed for pcre2test. */688689#ifndef PCRE2_PCRE2TEST690691/* Structures for checking for mutual function recursion when scanning compiled692or parsed code. */693694typedef struct recurse_check {695struct recurse_check *prev;696PCRE2_SPTR group;697} recurse_check;698699typedef struct parsed_recurse_check {700struct parsed_recurse_check *prev;701uint32_t *groupptr;702} parsed_recurse_check;703704/* Structure for building a cache when filling in pattern recursion offsets. */705706typedef struct recurse_cache {707PCRE2_SPTR group;708int groupnumber;709} recurse_cache;710711/* Structure for maintaining a chain of pointers to the currently incomplete712branches, for testing for left recursion while compiling. */713714typedef struct branch_chain {715struct branch_chain *outer;716PCRE2_UCHAR *current_branch;717} branch_chain;718719/* Structure for building a list of named groups during the first pass of720compiling. */721722typedef struct named_group {723PCRE2_SPTR name; /* Points to the name in the pattern */724uint32_t number; /* Group number */725uint16_t length; /* Length of the name */726uint16_t isdup; /* TRUE if a duplicate */727} named_group;728729/* Structure for caching sorted ranges. This improves the performance730of translating META code to byte code. */731732typedef struct class_ranges {733struct class_ranges *next; /* Next class ranges */734size_t char_lists_size; /* Total size of encoded char lists */735size_t char_lists_start; /* Start offset of encoded char lists */736uint16_t range_list_size; /* Size of ranges array */737uint16_t char_lists_types; /* The XCL_LIST header of char lists */738/* Followed by the list of ranges (start/end pairs) */739} class_ranges;740741typedef union class_bits_storage {742uint8_t classbits[32];743uint32_t classwords[8];744} class_bits_storage;745746/* Structure for passing "static" information around between the functions747doing the compiling, so that they are thread-safe. */748749typedef struct compile_block {750pcre2_real_compile_context *cx; /* Points to the compile context */751const uint8_t *lcc; /* Points to lower casing table */752const uint8_t *fcc; /* Points to case-flipping table */753const uint8_t *cbits; /* Points to character type table */754const uint8_t *ctypes; /* Points to table of type maps */755PCRE2_UCHAR *start_workspace; /* The start of working space */756PCRE2_UCHAR *start_code; /* The start of the compiled code */757PCRE2_SPTR start_pattern; /* The start of the pattern */758PCRE2_SPTR end_pattern; /* The end of the pattern */759PCRE2_UCHAR *name_table; /* The name/number table */760PCRE2_SIZE workspace_size; /* Size of workspace */761PCRE2_SIZE small_ref_offset[10]; /* Offsets for \1 to \9 */762PCRE2_SIZE erroroffset; /* Offset of error in pattern */763class_bits_storage classbits; /* Temporary store for classbits */764uint16_t names_found; /* Number of entries so far */765uint16_t name_entry_size; /* Size of each entry */766uint16_t parens_depth; /* Depth of nested parentheses */767uint16_t assert_depth; /* Depth of nested assertions */768named_group *named_groups; /* Points to vector in pre-compile */769uint32_t named_group_list_size; /* Number of entries in the list */770uint32_t external_options; /* External (initial) options */771uint32_t external_flags; /* External flag bits to be set */772uint32_t bracount; /* Count of capturing parentheses */773uint32_t lastcapture; /* Last capture encountered */774uint32_t *parsed_pattern; /* Parsed pattern buffer */775uint32_t *parsed_pattern_end; /* Parsed pattern should not get here */776uint32_t *groupinfo; /* Group info vector */777uint32_t top_backref; /* Maximum back reference */778uint32_t backref_map; /* Bitmap of low back refs */779uint32_t nltype; /* Newline type */780uint32_t nllen; /* Newline string length */781PCRE2_UCHAR nl[4]; /* Newline string when fixed length */782uint8_t class_op_used[ECLASS_NEST_LIMIT]; /* Operation used for783extended classes */784uint32_t req_varyopt; /* "After variable item" flag for reqbyte */785uint32_t max_varlookbehind; /* Limit for variable lookbehinds */786int max_lookbehind; /* Maximum lookbehind encountered (characters) */787BOOL had_accept; /* (*ACCEPT) encountered */788BOOL had_pruneorskip; /* (*PRUNE) or (*SKIP) encountered */789BOOL had_recurse; /* Had a pattern recursion or subroutine call */790BOOL dupnames; /* Duplicate names exist */791#ifdef SUPPORT_WIDE_CHARS792class_ranges *cranges; /* First class range. */793class_ranges *next_cranges; /* Next class range. */794size_t char_lists_size; /* Current size of character lists */795#endif796} compile_block;797798/* Structure for keeping the properties of the in-memory stack used799by the JIT matcher. */800801typedef struct pcre2_real_jit_stack {802pcre2_memctl memctl;803void* stack;804} pcre2_real_jit_stack;805806/* Structure for items in a linked list that represents an explicit recursive807call within the pattern when running pcre2_dfa_match(). */808809typedef struct dfa_recursion_info {810struct dfa_recursion_info *prevrec;811PCRE2_SPTR subject_position;812PCRE2_SPTR last_used_ptr;813uint32_t group_num;814} dfa_recursion_info;815816/* Structure for "stack" frames that are used for remembering backtracking817positions during matching. As these are used in a vector, with the ovector item818being extended, the size of the structure must be a multiple of PCRE2_SIZE. The819only way to check this at compile time is to force an error by generating an820array with a negative size. By putting this in a typedef (which is never used),821we don't generate any code when all is well. */822823typedef struct heapframe {824825/* The first set of fields are variables that have to be preserved over calls826to RRMATCH(), but which do not need to be copied to new frames. */827828PCRE2_SPTR ecode; /* The current position in the pattern */829PCRE2_SPTR temp_sptr[2]; /* Used for short-term PCRE2_SPTR values */830PCRE2_SIZE length; /* Used for character, string, or code lengths */831PCRE2_SIZE back_frame; /* Amount to subtract on RRETURN */832PCRE2_SIZE temp_size; /* Used for short-term PCRE2_SIZE values */833uint32_t rdepth; /* Function "recursion" depth within pcre2_match() */834uint32_t group_frame_type; /* Type information for group frames */835uint32_t temp_32[4]; /* Used for short-term 32-bit or BOOL values */836uint8_t return_id; /* Where to go on in internal "return" */837uint8_t op; /* Processing opcode */838839/* At this point, the structure is 16-bit aligned. On most architectures840the alignment requirement for a pointer will ensure that the eptr field below841is 32-bit or 64-bit aligned. However, on m68k it is fine to have a pointer842that is 16-bit aligned. We must therefore ensure that what comes between here843and eptr is an odd multiple of 16 bits so as to get back into 32-bit844alignment. This happens naturally when PCRE2_UCHAR is 8 bits wide, but needs845fudges in the other cases. In the 32-bit case the padding comes first so that846the occu field itself is 32-bit aligned. Without the padding, this structure847is no longer a multiple of PCRE2_SIZE on m68k, and the check below fails. */848849#if PCRE2_CODE_UNIT_WIDTH == 8850PCRE2_UCHAR occu[6]; /* Used for other case code units */851#elif PCRE2_CODE_UNIT_WIDTH == 16852PCRE2_UCHAR occu[2]; /* Used for other case code units */853uint8_t unused[2]; /* Ensure 32-bit alignment (see above) */854#else855uint8_t unused[2]; /* Ensure 32-bit alignment (see above) */856PCRE2_UCHAR occu[1]; /* Used for other case code units */857#endif858859/* The rest have to be copied from the previous frame whenever a new frame860becomes current. The final field is specified as a large vector so that861runtime array bound checks don't catch references to it. However, for any862specific call to pcre2_match() the memory allocated for each frame structure863allows for exactly the right size ovector for the number of capturing864parentheses. (See also the comment for pcre2_real_match_data above.) */865866PCRE2_SPTR eptr; /* MUST BE FIRST */867PCRE2_SPTR start_match; /* Can be adjusted by \K */868PCRE2_SPTR mark; /* Most recent mark on the success path */869PCRE2_SPTR recurse_last_used; /* Last character used at time of pattern recursion */870uint32_t current_recurse; /* Group number of current (deepest) pattern recursion */871uint32_t capture_last; /* Most recent capture */872PCRE2_SIZE last_group_offset; /* Saved offset to most recent group frame */873PCRE2_SIZE offset_top; /* Offset after highest capture */874PCRE2_SIZE ovector[131072]; /* Must be last in the structure */875} heapframe;876877/* Assert that the size of the heapframe structure is a multiple of PCRE2_SIZE.878See various comments above. */879880STATIC_ASSERT((sizeof(heapframe) % sizeof(PCRE2_SIZE)) == 0, heapframe_size);881882/* Structure for computing the alignment of heapframe. */883884typedef struct heapframe_align {885char unalign; /* Completely unalign the current offset */886heapframe frame; /* Offset is its alignment */887} heapframe_align;888889/* This define is the minimum alignment required for a heapframe, in bytes. */890891#define HEAPFRAME_ALIGNMENT offsetof(heapframe_align, frame)892893/* Structure for passing "static" information around between the functions894doing traditional NFA matching (pcre2_match() and friends). */895896typedef struct match_block {897pcre2_memctl memctl; /* For general use */898uint32_t heap_limit; /* As it says */899uint32_t match_limit; /* As it says */900uint32_t match_limit_depth; /* As it says */901uint32_t match_call_count; /* Number of times a new frame is created */902BOOL hitend; /* Hit the end of the subject at some point */903BOOL hasthen; /* Pattern contains (*THEN) */904BOOL allowemptypartial; /* Allow empty hard partial */905const uint8_t *lcc; /* Points to lower casing table */906const uint8_t *fcc; /* Points to case-flipping table */907const uint8_t *ctypes; /* Points to table of type maps */908PCRE2_SIZE start_offset; /* The start offset value */909PCRE2_SIZE end_offset_top; /* Highwater mark at end of match */910uint16_t partial; /* PARTIAL options */911uint16_t bsr_convention; /* \R interpretation */912uint16_t name_count; /* Number of names in name table */913uint16_t name_entry_size; /* Size of entry in names table */914PCRE2_SPTR name_table; /* Table of group names */915PCRE2_SPTR start_code; /* For use in pattern recursion */916PCRE2_SPTR start_subject; /* Start of the subject string */917PCRE2_SPTR check_subject; /* Where UTF-checked from */918PCRE2_SPTR end_subject; /* Usable end of the subject string */919PCRE2_SPTR true_end_subject; /* Actual end of the subject string */920PCRE2_SPTR end_match_ptr; /* Subject position at end match */921PCRE2_SPTR start_used_ptr; /* Earliest consulted character */922PCRE2_SPTR last_used_ptr; /* Latest consulted character */923PCRE2_SPTR mark; /* Mark pointer to pass back on success */924PCRE2_SPTR nomatch_mark; /* Mark pointer to pass back on failure */925PCRE2_SPTR verb_ecode_ptr; /* For passing back info */926PCRE2_SPTR verb_skip_ptr; /* For passing back a (*SKIP) name */927uint32_t verb_current_recurse; /* Current recursion group when (*VERB) happens */928uint32_t moptions; /* Match options */929uint32_t poptions; /* Pattern options */930uint32_t skip_arg_count; /* For counting SKIP_ARGs */931uint32_t ignore_skip_arg; /* For re-run when SKIP arg name not found */932uint32_t nltype; /* Newline type */933uint32_t nllen; /* Newline string length */934PCRE2_UCHAR nl[4]; /* Newline string when fixed */935pcre2_callout_block *cb; /* Points to a callout block */936void *callout_data; /* To pass back to callouts */937int (*callout)(pcre2_callout_block *,void *); /* Callout function or NULL */938} match_block;939940/* A similar structure is used for the same purpose by the DFA matching941functions. */942943typedef struct dfa_match_block {944pcre2_memctl memctl; /* For general use */945PCRE2_SPTR start_code; /* Start of the compiled pattern */946PCRE2_SPTR start_subject ; /* Start of the subject string */947PCRE2_SPTR end_subject; /* End of subject string */948PCRE2_SPTR start_used_ptr; /* Earliest consulted character */949PCRE2_SPTR last_used_ptr; /* Latest consulted character */950const uint8_t *tables; /* Character tables */951PCRE2_SIZE start_offset; /* The start offset value */952uint32_t heap_limit; /* As it says */953PCRE2_SIZE heap_used; /* As it says */954uint32_t match_limit; /* As it says */955uint32_t match_limit_depth; /* As it says */956uint32_t match_call_count; /* Number of calls of internal function */957uint32_t moptions; /* Match options */958uint32_t poptions; /* Pattern options */959uint32_t nltype; /* Newline type */960uint32_t nllen; /* Newline string length */961BOOL allowemptypartial; /* Allow empty hard partial */962PCRE2_UCHAR nl[4]; /* Newline string when fixed */963uint16_t bsr_convention; /* \R interpretation */964pcre2_callout_block *cb; /* Points to a callout block */965void *callout_data; /* To pass back to callouts */966int (*callout)(pcre2_callout_block *,void *); /* Callout function or NULL */967dfa_recursion_info *recursive; /* Linked list of pattern recursion data */968} dfa_match_block;969970#endif /* PCRE2_PCRE2TEST */971972/* End of pcre2_intmodedep.h */973974975