Path: blob/master/thirdparty/pcre2/src/pcre2_dfa_match.c
21797 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 the external function pcre2_dfa_match(), which is an42alternative matching function that uses a sort of DFA algorithm (not a true43FSM). This is NOT Perl-compatible, but it has advantages in certain44applications. */454647/* NOTE ABOUT PERFORMANCE: A user of this function sent some code that improved48the performance of his patterns greatly. I could not use it as it stood, as it49was not thread safe, and made assumptions about pattern sizes. Also, it caused50test 7 to loop, and test 9 to crash with a segfault.5152The issue is the check for duplicate states, which is done by a simple linear53search up the state list. (Grep for "duplicate" below to find the code.) For54many patterns, there will never be many states active at one time, so a simple55linear search is fine. In patterns that have many active states, it might be a56bottleneck. The suggested code used an indexing scheme to remember which states57had previously been used for each character, and avoided the linear search when58it knew there was no chance of a duplicate. This was implemented when adding59states to the state lists.6061I wrote some thread-safe, not-limited code to try something similar at the time62of checking for duplicates (instead of when adding states), using index vectors63on the stack. It did give a 13% improvement with one specially constructed64pattern for certain subject strings, but on other strings and on many of the65simpler patterns in the test suite it did worse. The major problem, I think,66was the extra time to initialize the index. This had to be done for each call67of internal_dfa_match(). (The supplied patch used a static vector, initialized68only once - I suspect this was the cause of the problems with the tests.)6970Overall, I concluded that the gains in some cases did not outweigh the losses71in others, so I abandoned this code. */727374#include "pcre2_internal.h"75767778#define NLBLOCK mb /* Block containing newline information */79#define PSSTART start_subject /* Field containing processed string start */80#define PSEND end_subject /* Field containing processed string end */8182#define PUBLIC_DFA_MATCH_OPTIONS \83(PCRE2_ANCHORED|PCRE2_ENDANCHORED|PCRE2_NOTBOL|PCRE2_NOTEOL|PCRE2_NOTEMPTY| \84PCRE2_NOTEMPTY_ATSTART|PCRE2_NO_UTF_CHECK|PCRE2_PARTIAL_HARD| \85PCRE2_PARTIAL_SOFT|PCRE2_DFA_SHORTEST|PCRE2_DFA_RESTART| \86PCRE2_COPY_MATCHED_SUBJECT)878889/*************************************************90* Code parameters and static tables *91*************************************************/9293/* These are offsets that are used to turn the OP_TYPESTAR and friends opcodes94into others, under special conditions. A gap of 20 between the blocks should be95enough. The resulting opcodes don't have to be less than 256 because they are96never stored, so we push them well clear of the normal opcodes. */9798#define OP_PROP_EXTRA 30099#define OP_EXTUNI_EXTRA 320100#define OP_ANYNL_EXTRA 340101#define OP_HSPACE_EXTRA 360102#define OP_VSPACE_EXTRA 380103104105/* This table identifies those opcodes that are followed immediately by a106character that is to be tested in some way. This makes it possible to107centralize the loading of these characters. In the case of Type * etc, the108"character" is the opcode for \D, \d, \S, \s, \W, or \w, which will always be a109small value. Non-zero values in the table are the offsets from the opcode where110the character is to be found. ***NOTE*** If the start of this table is111modified, the three tables that follow must also be modified. */112113static const uint8_t coptable[] = {1140, /* End */1150, 0, 0, 0, 0, /* \A, \G, \K, \B, \b */1160, 0, 0, 0, 0, 0, /* \D, \d, \S, \s, \W, \w */1170, 0, 0, /* Any, AllAny, Anybyte */1180, 0, /* \P, \p */1190, 0, 0, 0, 0, /* \R, \H, \h, \V, \v */1200, /* \X */1210, 0, 0, 0, 0, 0, /* \Z, \z, $, $M, ^, ^M */1221, /* Char */1231, /* Chari */1241, /* not */1251, /* noti */126/* Positive single-char repeats */1271, 1, 1, 1, 1, 1, /* *, *?, +, +?, ?, ?? */1281+IMM2_SIZE, 1+IMM2_SIZE, /* upto, minupto */1291+IMM2_SIZE, /* exact */1301, 1, 1, 1+IMM2_SIZE, /* *+, ++, ?+, upto+ */1311, 1, 1, 1, 1, 1, /* *I, *?I, +I, +?I, ?I, ??I */1321+IMM2_SIZE, 1+IMM2_SIZE, /* upto I, minupto I */1331+IMM2_SIZE, /* exact I */1341, 1, 1, 1+IMM2_SIZE, /* *+I, ++I, ?+I, upto+I */135/* Negative single-char repeats - only for chars < 256 */1361, 1, 1, 1, 1, 1, /* NOT *, *?, +, +?, ?, ?? */1371+IMM2_SIZE, 1+IMM2_SIZE, /* NOT upto, minupto */1381+IMM2_SIZE, /* NOT exact */1391, 1, 1, 1+IMM2_SIZE, /* NOT *+, ++, ?+, upto+ */1401, 1, 1, 1, 1, 1, /* NOT *I, *?I, +I, +?I, ?I, ??I */1411+IMM2_SIZE, 1+IMM2_SIZE, /* NOT upto I, minupto I */1421+IMM2_SIZE, /* NOT exact I */1431, 1, 1, 1+IMM2_SIZE, /* NOT *+I, ++I, ?+I, upto+I */144/* Positive type repeats */1451, 1, 1, 1, 1, 1, /* Type *, *?, +, +?, ?, ?? */1461+IMM2_SIZE, 1+IMM2_SIZE, /* Type upto, minupto */1471+IMM2_SIZE, /* Type exact */1481, 1, 1, 1+IMM2_SIZE, /* Type *+, ++, ?+, upto+ */149/* Character class & ref repeats */1500, 0, 0, 0, 0, 0, /* *, *?, +, +?, ?, ?? */1510, 0, /* CRRANGE, CRMINRANGE */1520, 0, 0, 0, /* Possessive *+, ++, ?+, CRPOSRANGE */1530, /* CLASS */1540, /* NCLASS */1550, /* XCLASS - variable length */1560, /* ECLASS - variable length */1570, /* REF */1580, /* REFI */1590, /* DNREF */1600, /* DNREFI */1610, /* RECURSE */1620, /* CALLOUT */1630, /* CALLOUT_STR */1640, /* Alt */1650, /* Ket */1660, /* KetRmax */1670, /* KetRmin */1680, /* KetRpos */1690, 0, /* Reverse, Vreverse */1700, /* Assert */1710, /* Assert not */1720, /* Assert behind */1730, /* Assert behind not */1740, /* NA assert */1750, /* NA assert behind */1760, /* Assert scan substring */1770, /* ONCE */1780, /* SCRIPT_RUN */1790, 0, 0, 0, 0, /* BRA, BRAPOS, CBRA, CBRAPOS, COND */1800, 0, 0, 0, 0, /* SBRA, SBRAPOS, SCBRA, SCBRAPOS, SCOND */1810, 0, /* CREF, DNCREF */1820, 0, /* RREF, DNRREF */1830, 0, /* FALSE, TRUE */1840, 0, 0, /* BRAZERO, BRAMINZERO, BRAPOSZERO */1850, 0, 0, /* MARK, PRUNE, PRUNE_ARG */1860, 0, 0, 0, /* SKIP, SKIP_ARG, THEN, THEN_ARG */1870, 0, /* COMMIT, COMMIT_ARG */1880, 0, 0, /* FAIL, ACCEPT, ASSERT_ACCEPT */1890, 0, 0, /* CLOSE, SKIPZERO, DEFINE */1900, 0, /* \B and \b in UCP mode */191};192193/* This table identifies those opcodes that inspect a character. It is used to194remember the fact that a character could have been inspected when the end of195the subject is reached. ***NOTE*** If the start of this table is modified, the196two tables that follow must also be modified. */197198static const uint8_t poptable[] = {1990, /* End */2000, 0, 0, 1, 1, /* \A, \G, \K, \B, \b */2011, 1, 1, 1, 1, 1, /* \D, \d, \S, \s, \W, \w */2021, 1, 1, /* Any, AllAny, Anybyte */2031, 1, /* \P, \p */2041, 1, 1, 1, 1, /* \R, \H, \h, \V, \v */2051, /* \X */2060, 0, 0, 0, 0, 0, /* \Z, \z, $, $M, ^, ^M */2071, /* Char */2081, /* Chari */2091, /* not */2101, /* noti */211/* Positive single-char repeats */2121, 1, 1, 1, 1, 1, /* *, *?, +, +?, ?, ?? */2131, 1, 1, /* upto, minupto, exact */2141, 1, 1, 1, /* *+, ++, ?+, upto+ */2151, 1, 1, 1, 1, 1, /* *I, *?I, +I, +?I, ?I, ??I */2161, 1, 1, /* upto I, minupto I, exact I */2171, 1, 1, 1, /* *+I, ++I, ?+I, upto+I */218/* Negative single-char repeats - only for chars < 256 */2191, 1, 1, 1, 1, 1, /* NOT *, *?, +, +?, ?, ?? */2201, 1, 1, /* NOT upto, minupto, exact */2211, 1, 1, 1, /* NOT *+, ++, ?+, upto+ */2221, 1, 1, 1, 1, 1, /* NOT *I, *?I, +I, +?I, ?I, ??I */2231, 1, 1, /* NOT upto I, minupto I, exact I */2241, 1, 1, 1, /* NOT *+I, ++I, ?+I, upto+I */225/* Positive type repeats */2261, 1, 1, 1, 1, 1, /* Type *, *?, +, +?, ?, ?? */2271, 1, 1, /* Type upto, minupto, exact */2281, 1, 1, 1, /* Type *+, ++, ?+, upto+ */229/* Character class & ref repeats */2301, 1, 1, 1, 1, 1, /* *, *?, +, +?, ?, ?? */2311, 1, /* CRRANGE, CRMINRANGE */2321, 1, 1, 1, /* Possessive *+, ++, ?+, CRPOSRANGE */2331, /* CLASS */2341, /* NCLASS */2351, /* XCLASS - variable length */2361, /* ECLASS - variable length */2370, /* REF */2380, /* REFI */2390, /* DNREF */2400, /* DNREFI */2410, /* RECURSE */2420, /* CALLOUT */2430, /* CALLOUT_STR */2440, /* Alt */2450, /* Ket */2460, /* KetRmax */2470, /* KetRmin */2480, /* KetRpos */2490, 0, /* Reverse, Vreverse */2500, /* Assert */2510, /* Assert not */2520, /* Assert behind */2530, /* Assert behind not */2540, /* NA assert */2550, /* NA assert behind */2560, /* Assert scan substring */2570, /* ONCE */2580, /* SCRIPT_RUN */2590, 0, 0, 0, 0, /* BRA, BRAPOS, CBRA, CBRAPOS, COND */2600, 0, 0, 0, 0, /* SBRA, SBRAPOS, SCBRA, SCBRAPOS, SCOND */2610, 0, /* CREF, DNCREF */2620, 0, /* RREF, DNRREF */2630, 0, /* FALSE, TRUE */2640, 0, 0, /* BRAZERO, BRAMINZERO, BRAPOSZERO */2650, 0, 0, /* MARK, PRUNE, PRUNE_ARG */2660, 0, 0, 0, /* SKIP, SKIP_ARG, THEN, THEN_ARG */2670, 0, /* COMMIT, COMMIT_ARG */2680, 0, 0, /* FAIL, ACCEPT, ASSERT_ACCEPT */2690, 0, 0, /* CLOSE, SKIPZERO, DEFINE */2701, 1, /* \B and \b in UCP mode */271};272273/* Compile-time check that these tables have the correct size. */274STATIC_ASSERT(sizeof(coptable) == OP_TABLE_LENGTH, coptable);275STATIC_ASSERT(sizeof(poptable) == OP_TABLE_LENGTH, poptable);276277/* These 2 tables allow for compact code for testing for \D, \d, \S, \s, \W,278and \w */279280static const uint8_t toptable1[] = {2810, 0, 0, 0, 0, 0,282ctype_digit, ctype_digit,283ctype_space, ctype_space,284ctype_word, ctype_word,2850, 0 /* OP_ANY, OP_ALLANY */286};287288static const uint8_t toptable2[] = {2890, 0, 0, 0, 0, 0,290ctype_digit, 0,291ctype_space, 0,292ctype_word, 0,2931, 1 /* OP_ANY, OP_ALLANY */294};295296297/* Structure for holding data about a particular state, which is in effect the298current data for an active path through the match tree. It must consist299entirely of ints because the working vector we are passed, and which we put300these structures in, is a vector of ints. */301302typedef struct stateblock {303int offset; /* Offset to opcode (-ve has meaning) */304int count; /* Count for repeats */305int data; /* Some use extra data */306} stateblock;307308#define INTS_PER_STATEBLOCK (int)(sizeof(stateblock)/sizeof(int))309310311/* Before version 10.32 the recursive calls of internal_dfa_match() were passed312local working space and output vectors that were created on the stack. This has313caused issues for some patterns, especially in small-stack environments such as314Windows. A new scheme is now in use which sets up a vector on the stack, but if315this is too small, heap memory is used, up to the heap_limit. The main316parameters are all numbers of ints because the workspace is a vector of ints.317318The size of the starting stack vector, DFA_START_RWS_SIZE, is in bytes, and is319defined in pcre2_internal.h so as to be available to pcre2test when it is320finding the minimum heap requirement for a match. */321322#define OVEC_UNIT (sizeof(PCRE2_SIZE)/sizeof(int))323324#define RWS_BASE_SIZE (DFA_START_RWS_SIZE/sizeof(int)) /* Stack vector */325#define RWS_RSIZE 1000 /* Work size for recursion */326#define RWS_OVEC_RSIZE (1000*OVEC_UNIT) /* Ovector for recursion */327#define RWS_OVEC_OSIZE (2*OVEC_UNIT) /* Ovector in other cases */328329/* This structure is at the start of each workspace block. */330331typedef struct RWS_anchor {332struct RWS_anchor *next;333uint32_t size; /* Number of ints */334uint32_t free; /* Number of ints */335} RWS_anchor;336337#define RWS_ANCHOR_SIZE (sizeof(RWS_anchor)/sizeof(int))338339340341/*************************************************342* Process a callout *343*************************************************/344345/* This function is called to perform a callout.346347Arguments:348code current code pointer349offsets points to current capture offsets350current_subject start of current subject match351ptr current position in subject352mb the match block353extracode extra code offset when called from condition354lengthptr where to return the callout length355356Returns: the return from the callout357*/358359static int360do_callout_dfa(PCRE2_SPTR code, PCRE2_SIZE *offsets, PCRE2_SPTR current_subject,361PCRE2_SPTR ptr, dfa_match_block *mb, PCRE2_SIZE extracode,362PCRE2_SIZE *lengthptr)363{364pcre2_callout_block *cb = mb->cb;365366*lengthptr = (code[extracode] == OP_CALLOUT)?367(PCRE2_SIZE)PRIV(OP_lengths)[OP_CALLOUT] :368(PCRE2_SIZE)GET(code, 1 + 2*LINK_SIZE + extracode);369370if (mb->callout == NULL) return 0; /* No callout provided */371372/* Fixed fields in the callout block are set once and for all at the start of373matching. */374375cb->offset_vector = offsets;376cb->start_match = (PCRE2_SIZE)(current_subject - mb->start_subject);377cb->current_position = (PCRE2_SIZE)(ptr - mb->start_subject);378cb->pattern_position = GET(code, 1 + extracode);379cb->next_item_length = GET(code, 1 + LINK_SIZE + extracode);380381if (code[extracode] == OP_CALLOUT)382{383cb->callout_number = code[1 + 2*LINK_SIZE + extracode];384cb->callout_string_offset = 0;385cb->callout_string = NULL;386cb->callout_string_length = 0;387}388else389{390cb->callout_number = 0;391cb->callout_string_offset = GET(code, 1 + 3*LINK_SIZE + extracode);392cb->callout_string = code + (1 + 4*LINK_SIZE + extracode) + 1;393cb->callout_string_length = *lengthptr - (1 + 4*LINK_SIZE) - 2;394}395396return (mb->callout)(cb, mb->callout_data);397}398399400401/*************************************************402* Expand local workspace memory *403*************************************************/404405/* This function is called when internal_dfa_match() is about to be called406recursively and there is insufficient working space left in the current407workspace block. If there's an existing next block, use it; otherwise get a new408block unless the heap limit is reached.409410Arguments:411rwsptr pointer to block pointer (updated)412ovecsize space needed for an ovector413mb the match block414415Returns: 0 rwsptr has been updated416!0 an error code417*/418419static int420more_workspace(RWS_anchor **rwsptr, unsigned int ovecsize, dfa_match_block *mb)421{422RWS_anchor *rws = *rwsptr;423RWS_anchor *new;424425if (rws->next != NULL)426{427new = rws->next;428}429430/* Sizes in the RWS_anchor blocks are in units of sizeof(int), but431mb->heap_limit and mb->heap_used are in kibibytes. Play carefully, to avoid432overflow. */433434else435{436uint32_t newsize = (rws->size >= UINT32_MAX/(sizeof(int)*2))? UINT32_MAX/sizeof(int) : rws->size * 2;437uint32_t newsizeK = newsize/(1024/sizeof(int));438439if (newsizeK + mb->heap_used > mb->heap_limit)440newsizeK = (uint32_t)(mb->heap_limit - mb->heap_used);441newsize = newsizeK*(1024/sizeof(int));442443if (newsize < RWS_RSIZE + ovecsize + RWS_ANCHOR_SIZE)444return PCRE2_ERROR_HEAPLIMIT;445new = mb->memctl.malloc(newsize*sizeof(int), mb->memctl.memory_data);446if (new == NULL) return PCRE2_ERROR_NOMEMORY;447mb->heap_used += newsizeK;448new->next = NULL;449new->size = newsize;450rws->next = new;451}452453new->free = new->size - RWS_ANCHOR_SIZE;454*rwsptr = new;455return 0;456}457458459460/*************************************************461* Match a Regular Expression - DFA engine *462*************************************************/463464/* This internal function applies a compiled pattern to a subject string,465starting at a given point, using a DFA engine. This function is called from the466external one, possibly multiple times if the pattern is not anchored. The467function calls itself recursively for some kinds of subpattern.468469Arguments:470mb the match_data block with fixed information471this_start_code the opening bracket of this subexpression's code472current_subject where we currently are in the subject string473start_offset start offset in the subject string474offsets vector to contain the matching string offsets475offsetcount size of same476workspace vector of workspace477wscount size of same478rlevel function call recursion level479480Returns: > 0 => number of match offset pairs placed in offsets481= 0 => offsets overflowed; longest matches are present482-1 => failed to match483< -1 => some kind of unexpected problem484485The following macros are used for adding states to the two state vectors (one486for the current character, one for the following character). */487488#define ADD_ACTIVE(x,y) \489if (active_count++ < wscount) \490{ \491next_active_state->offset = (x); \492next_active_state->count = (y); \493next_active_state++; \494} \495else return PCRE2_ERROR_DFA_WSSIZE496497#define ADD_ACTIVE_DATA(x,y,z) \498if (active_count++ < wscount) \499{ \500next_active_state->offset = (x); \501next_active_state->count = (y); \502next_active_state->data = (z); \503next_active_state++; \504} \505else return PCRE2_ERROR_DFA_WSSIZE506507#define ADD_NEW(x,y) \508if (new_count++ < wscount) \509{ \510next_new_state->offset = (x); \511next_new_state->count = (y); \512next_new_state++; \513} \514else return PCRE2_ERROR_DFA_WSSIZE515516#define ADD_NEW_DATA(x,y,z) \517if (new_count++ < wscount) \518{ \519next_new_state->offset = (x); \520next_new_state->count = (y); \521next_new_state->data = (z); \522next_new_state++; \523} \524else return PCRE2_ERROR_DFA_WSSIZE525526/* And now, here is the code */527528static int529internal_dfa_match(530dfa_match_block *mb,531PCRE2_SPTR this_start_code,532PCRE2_SPTR current_subject,533PCRE2_SIZE start_offset,534PCRE2_SIZE *offsets,535uint32_t offsetcount,536int *workspace,537int wscount,538uint32_t rlevel,539int *RWS)540{541stateblock *active_states, *new_states, *temp_states;542stateblock *next_active_state, *next_new_state;543const uint8_t *ctypes, *lcc, *fcc;544PCRE2_SPTR ptr;545PCRE2_SPTR end_code;546dfa_recursion_info new_recursive;547int active_count, new_count, match_count;548549/* Some fields in the mb block are frequently referenced, so we load them into550independent variables in the hope that this will perform better. */551552PCRE2_SPTR start_subject = mb->start_subject;553PCRE2_SPTR end_subject = mb->end_subject;554PCRE2_SPTR start_code = mb->start_code;555556#ifdef SUPPORT_UNICODE557BOOL utf = (mb->poptions & PCRE2_UTF) != 0;558BOOL utf_or_ucp = utf || (mb->poptions & PCRE2_UCP) != 0;559#else560BOOL utf = FALSE;561#endif562563BOOL reset_could_continue = FALSE;564565if (mb->match_call_count++ >= mb->match_limit) return PCRE2_ERROR_MATCHLIMIT;566if (rlevel++ > mb->match_limit_depth) return PCRE2_ERROR_DEPTHLIMIT;567offsetcount &= (uint32_t)(-2); /* Round down */568569wscount -= 2;570wscount = (wscount - (wscount % (INTS_PER_STATEBLOCK * 2))) /571(2 * INTS_PER_STATEBLOCK);572573ctypes = mb->tables + ctypes_offset;574lcc = mb->tables + lcc_offset;575fcc = mb->tables + fcc_offset;576577match_count = PCRE2_ERROR_NOMATCH; /* A negative number */578579active_states = (stateblock *)(workspace + 2);580next_new_state = new_states = active_states + wscount;581new_count = 0;582583/* The first thing in any (sub) pattern is a bracket of some sort. Push all584the alternative states onto the list, and find out where the end is. This585makes is possible to use this function recursively, when we want to stop at a586matching internal ket rather than at the end.587588If we are dealing with a backward assertion we have to find out the maximum589amount to move back, and set up each alternative appropriately. */590591if (*this_start_code == OP_ASSERTBACK || *this_start_code == OP_ASSERTBACK_NOT)592{593size_t max_back = 0;594size_t gone_back;595596end_code = this_start_code;597do598{599size_t back = (size_t)GET2(end_code, 2+LINK_SIZE);600if (back > max_back) max_back = back;601end_code += GET(end_code, 1);602}603while (*end_code == OP_ALT);604605/* If we can't go back the amount required for the longest lookbehind606pattern, go back as far as we can; some alternatives may still be viable. */607608#ifdef SUPPORT_UNICODE609/* In character mode we have to step back character by character */610611if (utf)612{613for (gone_back = 0; gone_back < max_back; gone_back++)614{615if (current_subject <= start_subject) break;616current_subject--;617ACROSSCHAR(current_subject > start_subject, current_subject,618current_subject--);619}620}621else622#endif623624/* In byte-mode we can do this quickly. */625626{627size_t current_offset = (size_t)(current_subject - start_subject);628gone_back = (current_offset < max_back)? current_offset : max_back;629current_subject -= gone_back;630}631632/* Save the earliest consulted character */633634if (current_subject < mb->start_used_ptr)635mb->start_used_ptr = current_subject;636637/* Now we can process the individual branches. There will be an OP_REVERSE at638the start of each branch, except when the length of the branch is zero. */639640end_code = this_start_code;641do642{643uint32_t revlen = (end_code[1+LINK_SIZE] == OP_REVERSE)? 1 + IMM2_SIZE : 0;644size_t back = (revlen == 0)? 0 : (size_t)GET2(end_code, 2+LINK_SIZE);645if (back <= gone_back)646{647int bstate = (int)(end_code - start_code + 1 + LINK_SIZE + revlen);648ADD_NEW_DATA(-bstate, 0, (int)(gone_back - back));649}650end_code += GET(end_code, 1);651}652while (*end_code == OP_ALT);653}654655/* This is the code for a "normal" subpattern (not a backward assertion). The656start of a whole pattern is always one of these. If we are at the top level,657we may be asked to restart matching from the same point that we reached for a658previous partial match. We still have to scan through the top-level branches to659find the end state. */660661else662{663end_code = this_start_code;664665/* Restarting */666667if (rlevel == 1 && (mb->moptions & PCRE2_DFA_RESTART) != 0)668{669do { end_code += GET(end_code, 1); } while (*end_code == OP_ALT);670new_count = workspace[1];671if (!workspace[0])672memcpy(new_states, active_states, (size_t)new_count * sizeof(stateblock));673}674675/* Not restarting */676677else678{679int length = 1 + LINK_SIZE +680((*this_start_code == OP_CBRA || *this_start_code == OP_SCBRA ||681*this_start_code == OP_CBRAPOS || *this_start_code == OP_SCBRAPOS)682? IMM2_SIZE:0);683do684{685ADD_NEW((int)(end_code - start_code + length), 0);686end_code += GET(end_code, 1);687length = 1 + LINK_SIZE;688}689while (*end_code == OP_ALT);690}691}692693workspace[0] = 0; /* Bit indicating which vector is current */694695/* Loop for scanning the subject */696697ptr = current_subject;698for (;;)699{700int i, j;701int clen, dlen;702uint32_t c, d;703BOOL partial_newline = FALSE;704BOOL could_continue = reset_could_continue;705reset_could_continue = FALSE;706707if (ptr > mb->last_used_ptr) mb->last_used_ptr = ptr;708709/* Make the new state list into the active state list and empty the710new state list. */711712temp_states = active_states;713active_states = new_states;714new_states = temp_states;715active_count = new_count;716new_count = 0;717718workspace[0] ^= 1; /* Remember for the restarting feature */719workspace[1] = active_count;720721/* Set the pointers for adding new states */722723next_active_state = active_states + active_count;724next_new_state = new_states;725726/* Load the current character from the subject outside the loop, as many727different states may want to look at it, and we assume that at least one728will. */729730if (ptr < end_subject)731{732clen = 1; /* Number of data items in the character */733#ifdef SUPPORT_UNICODE734GETCHARLENTEST(c, ptr, clen);735#else736c = *ptr;737#endif /* SUPPORT_UNICODE */738}739else740{741clen = 0; /* This indicates the end of the subject */742c = NOTACHAR; /* This value should never actually be used */743}744745/* Scan up the active states and act on each one. The result of an action746may be to add more states to the currently active list (e.g. on hitting a747parenthesis) or it may be to put states on the new list, for considering748when we move the character pointer on. */749750for (i = 0; i < active_count; i++)751{752stateblock *current_state = active_states + i;753BOOL caseless = FALSE;754PCRE2_SPTR code;755uint32_t codevalue;756int state_offset = current_state->offset;757int rrc;758int count;759760/* A negative offset is a special case meaning "hold off going to this761(negated) state until the number of characters in the data field have762been skipped". If the could_continue flag was passed over from a previous763state, arrange for it to passed on. */764765if (state_offset < 0)766{767if (current_state->data > 0)768{769ADD_NEW_DATA(state_offset, current_state->count,770current_state->data - 1);771if (could_continue) reset_could_continue = TRUE;772continue;773}774else775{776current_state->offset = state_offset = -state_offset;777}778}779780/* Check for a duplicate state with the same count, and skip if found.781See the note at the head of this module about the possibility of improving782performance here. */783784for (j = 0; j < i; j++)785{786if (active_states[j].offset == state_offset &&787active_states[j].count == current_state->count)788goto NEXT_ACTIVE_STATE;789}790791/* The state offset is the offset to the opcode */792793code = start_code + state_offset;794codevalue = *code;795796/* If this opcode inspects a character, but we are at the end of the797subject, remember the fact for use when testing for a partial match. */798799if (clen == 0 && poptable[codevalue] != 0)800could_continue = TRUE;801802/* If this opcode is followed by an inline character, load it. It is803tempting to test for the presence of a subject character here, but that804is wrong, because sometimes zero repetitions of the subject are805permitted.806807We also use this mechanism for opcodes such as OP_TYPEPLUS that take an808argument that is not a data character - but is always one byte long because809the values are small. We have to take special action to deal with \P, \p,810\H, \h, \V, \v and \X in this case. To keep the other cases fast, convert811these ones to new opcodes. */812813if (coptable[codevalue] > 0)814{815dlen = 1;816#ifdef SUPPORT_UNICODE817if (utf) { GETCHARLEN(d, (code + coptable[codevalue]), dlen); } else818#endif /* SUPPORT_UNICODE */819d = code[coptable[codevalue]];820if (codevalue >= OP_TYPESTAR)821{822switch(d)823{824case OP_ANYBYTE: return PCRE2_ERROR_DFA_UITEM;825case OP_NOTPROP:826case OP_PROP: codevalue += OP_PROP_EXTRA; break;827case OP_ANYNL: codevalue += OP_ANYNL_EXTRA; break;828case OP_EXTUNI: codevalue += OP_EXTUNI_EXTRA; break;829case OP_NOT_HSPACE:830case OP_HSPACE: codevalue += OP_HSPACE_EXTRA; break;831case OP_NOT_VSPACE:832case OP_VSPACE: codevalue += OP_VSPACE_EXTRA; break;833default: break;834}835}836}837else838{839dlen = 0; /* Not strictly necessary, but compilers moan */840d = NOTACHAR; /* if these variables are not set. */841}842843844/* Now process the individual opcodes */845846switch (codevalue)847{848/* ========================================================================== */849/* Reached a closing bracket. If not at the end of the pattern, carry850on with the next opcode. For repeating opcodes, also add the repeat851state. Note that KETRPOS will always be encountered at the end of the852subpattern, because the possessive subpattern repeats are always handled853using recursive calls. Thus, it never adds any new states.854855At the end of the (sub)pattern, unless we have an empty string and856PCRE2_NOTEMPTY is set, or PCRE2_NOTEMPTY_ATSTART is set and we are at the857start of the subject, save the match data, shifting up all previous858matches so we always have the longest first. */859860case OP_KET:861case OP_KETRMIN:862case OP_KETRMAX:863case OP_KETRPOS:864if (code != end_code)865{866ADD_ACTIVE(state_offset + 1 + LINK_SIZE, 0);867if (codevalue != OP_KET)868{869ADD_ACTIVE(state_offset - (int)GET(code, 1), 0);870}871}872else873{874if (ptr > current_subject ||875((mb->moptions & PCRE2_NOTEMPTY) == 0 &&876((mb->moptions & PCRE2_NOTEMPTY_ATSTART) == 0 ||877current_subject > start_subject + mb->start_offset)))878{879if (match_count < 0) match_count = (offsetcount >= 2)? 1 : 0;880else if (match_count > 0 && ++match_count * 2 > (int)offsetcount)881match_count = 0;882count = ((match_count == 0)? (int)offsetcount : match_count * 2) - 2;883if (count > 0) (void)memmove(offsets + 2, offsets,884(size_t)count * sizeof(PCRE2_SIZE));885if (offsetcount >= 2)886{887offsets[0] = (PCRE2_SIZE)(current_subject - start_subject);888offsets[1] = (PCRE2_SIZE)(ptr - start_subject);889}890if ((mb->moptions & PCRE2_DFA_SHORTEST) != 0) return match_count;891}892}893break;894895/* ========================================================================== */896/* These opcodes add to the current list of states without looking897at the current character. */898899/*-----------------------------------------------------------------*/900case OP_ALT:901do { code += GET(code, 1); } while (*code == OP_ALT);902ADD_ACTIVE((int)(code - start_code), 0);903break;904905/*-----------------------------------------------------------------*/906case OP_BRA:907case OP_SBRA:908do909{910ADD_ACTIVE((int)(code - start_code + 1 + LINK_SIZE), 0);911code += GET(code, 1);912}913while (*code == OP_ALT);914break;915916/*-----------------------------------------------------------------*/917case OP_CBRA:918case OP_SCBRA:919ADD_ACTIVE((int)(code - start_code + 1 + LINK_SIZE + IMM2_SIZE), 0);920code += GET(code, 1);921while (*code == OP_ALT)922{923ADD_ACTIVE((int)(code - start_code + 1 + LINK_SIZE), 0);924code += GET(code, 1);925}926break;927928/*-----------------------------------------------------------------*/929case OP_BRAZERO:930case OP_BRAMINZERO:931ADD_ACTIVE(state_offset + 1, 0);932code += 1 + GET(code, 2);933while (*code == OP_ALT) code += GET(code, 1);934ADD_ACTIVE((int)(code - start_code + 1 + LINK_SIZE), 0);935break;936937/*-----------------------------------------------------------------*/938case OP_SKIPZERO:939code += 1 + GET(code, 2);940while (*code == OP_ALT) code += GET(code, 1);941ADD_ACTIVE((int)(code - start_code + 1 + LINK_SIZE), 0);942break;943944/*-----------------------------------------------------------------*/945case OP_CIRC:946if (ptr == start_subject && (mb->moptions & PCRE2_NOTBOL) == 0)947{ ADD_ACTIVE(state_offset + 1, 0); }948break;949950/*-----------------------------------------------------------------*/951case OP_CIRCM:952if ((ptr == start_subject && (mb->moptions & PCRE2_NOTBOL) == 0) ||953((ptr != end_subject || (mb->poptions & PCRE2_ALT_CIRCUMFLEX) != 0 )954&& WAS_NEWLINE(ptr)))955{ ADD_ACTIVE(state_offset + 1, 0); }956break;957958/*-----------------------------------------------------------------*/959case OP_EOD:960if (ptr >= end_subject)961{962if ((mb->moptions & PCRE2_PARTIAL_HARD) != 0)963return PCRE2_ERROR_PARTIAL;964else { ADD_ACTIVE(state_offset + 1, 0); }965}966break;967968/*-----------------------------------------------------------------*/969case OP_SOD:970if (ptr == start_subject) { ADD_ACTIVE(state_offset + 1, 0); }971break;972973/*-----------------------------------------------------------------*/974case OP_SOM:975if (ptr == start_subject + start_offset) { ADD_ACTIVE(state_offset + 1, 0); }976break;977978979/* ========================================================================== */980/* These opcodes inspect the next subject character, and sometimes981the previous one as well, but do not have an argument. The variable982clen contains the length of the current character and is zero if we are983at the end of the subject. */984985/*-----------------------------------------------------------------*/986case OP_ANY:987if (clen > 0 && !IS_NEWLINE(ptr))988{989if (ptr + 1 >= mb->end_subject &&990(mb->moptions & (PCRE2_PARTIAL_HARD)) != 0 &&991NLBLOCK->nltype == NLTYPE_FIXED &&992NLBLOCK->nllen == 2 &&993c == NLBLOCK->nl[0])994{995could_continue = partial_newline = TRUE;996}997else998{999ADD_NEW(state_offset + 1, 0);1000}1001}1002break;10031004/*-----------------------------------------------------------------*/1005case OP_ALLANY:1006if (clen > 0)1007{ ADD_NEW(state_offset + 1, 0); }1008break;10091010/*-----------------------------------------------------------------*/1011case OP_EODN:1012if (clen == 0 || (IS_NEWLINE(ptr) && ptr == end_subject - mb->nllen))1013{1014if ((mb->moptions & PCRE2_PARTIAL_HARD) != 0)1015return PCRE2_ERROR_PARTIAL;1016ADD_ACTIVE(state_offset + 1, 0);1017}1018break;10191020/*-----------------------------------------------------------------*/1021case OP_DOLL:1022if ((mb->moptions & PCRE2_NOTEOL) == 0)1023{1024if (clen == 0 && (mb->moptions & PCRE2_PARTIAL_HARD) != 0)1025could_continue = TRUE;1026else if (clen == 0 ||1027((mb->poptions & PCRE2_DOLLAR_ENDONLY) == 0 && IS_NEWLINE(ptr) &&1028(ptr == end_subject - mb->nllen)1029))1030{ ADD_ACTIVE(state_offset + 1, 0); }1031else if (ptr + 1 >= mb->end_subject &&1032(mb->moptions & (PCRE2_PARTIAL_HARD|PCRE2_PARTIAL_SOFT)) != 0 &&1033NLBLOCK->nltype == NLTYPE_FIXED &&1034NLBLOCK->nllen == 2 &&1035c == NLBLOCK->nl[0])1036{1037if ((mb->moptions & PCRE2_PARTIAL_HARD) != 0)1038{1039reset_could_continue = TRUE;1040ADD_NEW_DATA(-(state_offset + 1), 0, 1);1041}1042else could_continue = partial_newline = TRUE;1043}1044}1045break;10461047/*-----------------------------------------------------------------*/1048case OP_DOLLM:1049if ((mb->moptions & PCRE2_NOTEOL) == 0)1050{1051if (clen == 0 && (mb->moptions & PCRE2_PARTIAL_HARD) != 0)1052could_continue = TRUE;1053else if (clen == 0 ||1054((mb->poptions & PCRE2_DOLLAR_ENDONLY) == 0 && IS_NEWLINE(ptr)))1055{ ADD_ACTIVE(state_offset + 1, 0); }1056else if (ptr + 1 >= mb->end_subject &&1057(mb->moptions & (PCRE2_PARTIAL_HARD|PCRE2_PARTIAL_SOFT)) != 0 &&1058NLBLOCK->nltype == NLTYPE_FIXED &&1059NLBLOCK->nllen == 2 &&1060c == NLBLOCK->nl[0])1061{1062if ((mb->moptions & PCRE2_PARTIAL_HARD) != 0)1063{1064reset_could_continue = TRUE;1065ADD_NEW_DATA(-(state_offset + 1), 0, 1);1066}1067else could_continue = partial_newline = TRUE;1068}1069}1070else if (IS_NEWLINE(ptr))1071{ ADD_ACTIVE(state_offset + 1, 0); }1072break;10731074/*-----------------------------------------------------------------*/10751076case OP_DIGIT:1077case OP_WHITESPACE:1078case OP_WORDCHAR:1079if (clen > 0 && c < 256 &&1080((ctypes[c] & toptable1[codevalue]) ^ toptable2[codevalue]) != 0)1081{ ADD_NEW(state_offset + 1, 0); }1082break;10831084/*-----------------------------------------------------------------*/1085case OP_NOT_DIGIT:1086case OP_NOT_WHITESPACE:1087case OP_NOT_WORDCHAR:1088if (clen > 0 && (c >= 256 ||1089((ctypes[c] & toptable1[codevalue]) ^ toptable2[codevalue]) != 0))1090{ ADD_NEW(state_offset + 1, 0); }1091break;10921093/*-----------------------------------------------------------------*/1094case OP_WORD_BOUNDARY:1095case OP_NOT_WORD_BOUNDARY:1096case OP_NOT_UCP_WORD_BOUNDARY:1097case OP_UCP_WORD_BOUNDARY:1098{1099int left_word, right_word;11001101if (ptr > start_subject)1102{1103PCRE2_SPTR temp = ptr - 1;1104if (temp < mb->start_used_ptr) mb->start_used_ptr = temp;1105#if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH != 321106if (utf) { BACKCHAR(temp); }1107#endif1108GETCHARTEST(d, temp);1109#ifdef SUPPORT_UNICODE1110if (codevalue == OP_UCP_WORD_BOUNDARY ||1111codevalue == OP_NOT_UCP_WORD_BOUNDARY)1112{1113int chartype = UCD_CHARTYPE(d);1114int category = PRIV(ucp_gentype)[chartype];1115left_word = (category == ucp_L || category == ucp_N ||1116chartype == ucp_Mn || chartype == ucp_Pc);1117}1118else1119#endif1120left_word = d < 256 && (ctypes[d] & ctype_word) != 0;1121}1122else left_word = FALSE;11231124if (clen > 0)1125{1126if (ptr >= mb->last_used_ptr)1127{1128PCRE2_SPTR temp = ptr + 1;1129#if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH != 321130if (utf) { FORWARDCHARTEST(temp, mb->end_subject); }1131#endif1132mb->last_used_ptr = temp;1133}1134#ifdef SUPPORT_UNICODE1135if (codevalue == OP_UCP_WORD_BOUNDARY ||1136codevalue == OP_NOT_UCP_WORD_BOUNDARY)1137{1138int chartype = UCD_CHARTYPE(c);1139int category = PRIV(ucp_gentype)[chartype];1140right_word = (category == ucp_L || category == ucp_N ||1141chartype == ucp_Mn || chartype == ucp_Pc);1142}1143else1144#endif1145right_word = c < 256 && (ctypes[c] & ctype_word) != 0;1146}1147else right_word = FALSE;11481149if ((left_word == right_word) ==1150(codevalue == OP_NOT_WORD_BOUNDARY ||1151codevalue == OP_NOT_UCP_WORD_BOUNDARY))1152{ ADD_ACTIVE(state_offset + 1, 0); }1153}1154break;115511561157/*-----------------------------------------------------------------*/1158/* Check the next character by Unicode property. We will get here only1159if the support is in the binary; otherwise a compile-time error occurs.1160*/11611162#ifdef SUPPORT_UNICODE1163case OP_PROP:1164case OP_NOTPROP:1165if (clen > 0)1166{1167BOOL OK;1168int chartype;1169const uint32_t *cp;1170const ucd_record * prop = GET_UCD(c);1171switch(code[1])1172{1173case PT_LAMP:1174chartype = prop->chartype;1175OK = chartype == ucp_Lu || chartype == ucp_Ll ||1176chartype == ucp_Lt;1177break;11781179case PT_GC:1180OK = PRIV(ucp_gentype)[prop->chartype] == code[2];1181break;11821183case PT_PC:1184OK = prop->chartype == code[2];1185break;11861187case PT_SC:1188OK = prop->script == code[2];1189break;11901191case PT_SCX:1192OK = (prop->script == code[2] ||1193MAPBIT(PRIV(ucd_script_sets) + UCD_SCRIPTX_PROP(prop), code[2]) != 0);1194break;11951196/* These are specials for combination cases. */11971198case PT_ALNUM:1199chartype = prop->chartype;1200OK = PRIV(ucp_gentype)[chartype] == ucp_L ||1201PRIV(ucp_gentype)[chartype] == ucp_N;1202break;12031204/* Perl space used to exclude VT, but from Perl 5.18 it is included,1205which means that Perl space and POSIX space are now identical. PCRE1206was changed at release 8.34. */12071208case PT_SPACE: /* Perl space */1209case PT_PXSPACE: /* POSIX space */1210switch(c)1211{1212HSPACE_CASES:1213VSPACE_CASES:1214OK = TRUE;1215break;12161217default:1218OK = PRIV(ucp_gentype)[prop->chartype] == ucp_Z;1219break;1220}1221break;12221223case PT_WORD:1224chartype = prop->chartype;1225OK = PRIV(ucp_gentype)[chartype] == ucp_L ||1226PRIV(ucp_gentype)[chartype] == ucp_N ||1227chartype == ucp_Mn || chartype == ucp_Pc;1228break;12291230case PT_CLIST:1231#if PCRE2_CODE_UNIT_WIDTH == 321232if (c > MAX_UTF_CODE_POINT)1233{1234OK = FALSE;1235break;1236}1237#endif1238cp = PRIV(ucd_caseless_sets) + code[2];1239for (;;)1240{1241if (c < *cp) { OK = FALSE; break; }1242if (c == *cp++) { OK = TRUE; break; }1243}1244break;12451246case PT_UCNC:1247OK = c == CHAR_DOLLAR_SIGN || c == CHAR_COMMERCIAL_AT ||1248c == CHAR_GRAVE_ACCENT || (c >= 0xa0 && c <= 0xd7ff) ||1249c >= 0xe000;1250break;12511252case PT_BIDICL:1253OK = UCD_BIDICLASS(c) == code[2];1254break;12551256case PT_BOOL:1257OK = MAPBIT(PRIV(ucd_boolprop_sets) +1258UCD_BPROPS_PROP(prop), code[2]) != 0;1259break;12601261/* Should never occur, but keep compilers from grumbling. */12621263default:1264OK = codevalue != OP_PROP;1265break;1266}12671268if (OK == (codevalue == OP_PROP)) { ADD_NEW(state_offset + 3, 0); }1269}1270break;1271#endif1272127312741275/* ========================================================================== */1276/* These opcodes likewise inspect the subject character, but have an1277argument that is not a data character. It is one of these opcodes:1278OP_ANY, OP_ALLANY, OP_DIGIT, OP_NOT_DIGIT, OP_WHITESPACE, OP_NOT_SPACE,1279OP_WORDCHAR, OP_NOT_WORDCHAR. The value is loaded into d. */12801281case OP_TYPEPLUS:1282case OP_TYPEMINPLUS:1283case OP_TYPEPOSPLUS:1284count = current_state->count; /* Already matched */1285if (count > 0) { ADD_ACTIVE(state_offset + 2, 0); }1286if (clen > 0)1287{1288if (d == OP_ANY && ptr + 1 >= mb->end_subject &&1289(mb->moptions & (PCRE2_PARTIAL_HARD)) != 0 &&1290NLBLOCK->nltype == NLTYPE_FIXED &&1291NLBLOCK->nllen == 2 &&1292c == NLBLOCK->nl[0])1293{1294could_continue = partial_newline = TRUE;1295}1296else if ((c >= 256 && d != OP_DIGIT && d != OP_WHITESPACE && d != OP_WORDCHAR) ||1297(c < 256 &&1298(d != OP_ANY || !IS_NEWLINE(ptr)) &&1299((ctypes[c] & toptable1[d]) ^ toptable2[d]) != 0))1300{1301if (count > 0 && codevalue == OP_TYPEPOSPLUS)1302{1303active_count--; /* Remove non-match possibility */1304next_active_state--;1305}1306count++;1307ADD_NEW(state_offset, count);1308}1309}1310break;13111312/*-----------------------------------------------------------------*/1313case OP_TYPEQUERY:1314case OP_TYPEMINQUERY:1315case OP_TYPEPOSQUERY:1316ADD_ACTIVE(state_offset + 2, 0);1317if (clen > 0)1318{1319if (d == OP_ANY && ptr + 1 >= mb->end_subject &&1320(mb->moptions & (PCRE2_PARTIAL_HARD)) != 0 &&1321NLBLOCK->nltype == NLTYPE_FIXED &&1322NLBLOCK->nllen == 2 &&1323c == NLBLOCK->nl[0])1324{1325could_continue = partial_newline = TRUE;1326}1327else if ((c >= 256 && d != OP_DIGIT && d != OP_WHITESPACE && d != OP_WORDCHAR) ||1328(c < 256 &&1329(d != OP_ANY || !IS_NEWLINE(ptr)) &&1330((ctypes[c] & toptable1[d]) ^ toptable2[d]) != 0))1331{1332if (codevalue == OP_TYPEPOSQUERY)1333{1334active_count--; /* Remove non-match possibility */1335next_active_state--;1336}1337ADD_NEW(state_offset + 2, 0);1338}1339}1340break;13411342/*-----------------------------------------------------------------*/1343case OP_TYPESTAR:1344case OP_TYPEMINSTAR:1345case OP_TYPEPOSSTAR:1346ADD_ACTIVE(state_offset + 2, 0);1347if (clen > 0)1348{1349if (d == OP_ANY && ptr + 1 >= mb->end_subject &&1350(mb->moptions & (PCRE2_PARTIAL_HARD)) != 0 &&1351NLBLOCK->nltype == NLTYPE_FIXED &&1352NLBLOCK->nllen == 2 &&1353c == NLBLOCK->nl[0])1354{1355could_continue = partial_newline = TRUE;1356}1357else if ((c >= 256 && d != OP_DIGIT && d != OP_WHITESPACE && d != OP_WORDCHAR) ||1358(c < 256 &&1359(d != OP_ANY || !IS_NEWLINE(ptr)) &&1360((ctypes[c] & toptable1[d]) ^ toptable2[d]) != 0))1361{1362if (codevalue == OP_TYPEPOSSTAR)1363{1364active_count--; /* Remove non-match possibility */1365next_active_state--;1366}1367ADD_NEW(state_offset, 0);1368}1369}1370break;13711372/*-----------------------------------------------------------------*/1373case OP_TYPEEXACT:1374count = current_state->count; /* Number already matched */1375if (clen > 0)1376{1377if (d == OP_ANY && ptr + 1 >= mb->end_subject &&1378(mb->moptions & (PCRE2_PARTIAL_HARD)) != 0 &&1379NLBLOCK->nltype == NLTYPE_FIXED &&1380NLBLOCK->nllen == 2 &&1381c == NLBLOCK->nl[0])1382{1383could_continue = partial_newline = TRUE;1384}1385else if ((c >= 256 && d != OP_DIGIT && d != OP_WHITESPACE && d != OP_WORDCHAR) ||1386(c < 256 &&1387(d != OP_ANY || !IS_NEWLINE(ptr)) &&1388((ctypes[c] & toptable1[d]) ^ toptable2[d]) != 0))1389{1390if (++count >= (int)GET2(code, 1))1391{ ADD_NEW(state_offset + 1 + IMM2_SIZE + 1, 0); }1392else1393{ ADD_NEW(state_offset, count); }1394}1395}1396break;13971398/*-----------------------------------------------------------------*/1399case OP_TYPEUPTO:1400case OP_TYPEMINUPTO:1401case OP_TYPEPOSUPTO:1402ADD_ACTIVE(state_offset + 2 + IMM2_SIZE, 0);1403count = current_state->count; /* Number already matched */1404if (clen > 0)1405{1406if (d == OP_ANY && ptr + 1 >= mb->end_subject &&1407(mb->moptions & (PCRE2_PARTIAL_HARD)) != 0 &&1408NLBLOCK->nltype == NLTYPE_FIXED &&1409NLBLOCK->nllen == 2 &&1410c == NLBLOCK->nl[0])1411{1412could_continue = partial_newline = TRUE;1413}1414else if ((c >= 256 && d != OP_DIGIT && d != OP_WHITESPACE && d != OP_WORDCHAR) ||1415(c < 256 &&1416(d != OP_ANY || !IS_NEWLINE(ptr)) &&1417((ctypes[c] & toptable1[d]) ^ toptable2[d]) != 0))1418{1419if (codevalue == OP_TYPEPOSUPTO)1420{1421active_count--; /* Remove non-match possibility */1422next_active_state--;1423}1424if (++count >= (int)GET2(code, 1))1425{ ADD_NEW(state_offset + 2 + IMM2_SIZE, 0); }1426else1427{ ADD_NEW(state_offset, count); }1428}1429}1430break;14311432/* ========================================================================== */1433/* These are virtual opcodes that are used when something like1434OP_TYPEPLUS has OP_PROP, OP_NOTPROP, OP_ANYNL, or OP_EXTUNI as its1435argument. It keeps the code above fast for the other cases. The argument1436is in the d variable. */14371438#ifdef SUPPORT_UNICODE1439case OP_PROP_EXTRA + OP_TYPEPLUS:1440case OP_PROP_EXTRA + OP_TYPEMINPLUS:1441case OP_PROP_EXTRA + OP_TYPEPOSPLUS:1442count = current_state->count; /* Already matched */1443if (count > 0) { ADD_ACTIVE(state_offset + 4, 0); }1444if (clen > 0)1445{1446BOOL OK;1447int chartype;1448const uint32_t *cp;1449const ucd_record * prop = GET_UCD(c);1450switch(code[2])1451{1452case PT_LAMP:1453chartype = prop->chartype;1454OK = chartype == ucp_Lu || chartype == ucp_Ll || chartype == ucp_Lt;1455break;14561457case PT_GC:1458OK = PRIV(ucp_gentype)[prop->chartype] == code[3];1459break;14601461case PT_PC:1462OK = prop->chartype == code[3];1463break;14641465case PT_SC:1466OK = prop->script == code[3];1467break;14681469case PT_SCX:1470OK = (prop->script == code[3] ||1471MAPBIT(PRIV(ucd_script_sets) + UCD_SCRIPTX_PROP(prop), code[3]) != 0);1472break;14731474/* These are specials for combination cases. */14751476case PT_ALNUM:1477chartype = prop->chartype;1478OK = PRIV(ucp_gentype)[chartype] == ucp_L ||1479PRIV(ucp_gentype)[chartype] == ucp_N;1480break;14811482/* Perl space used to exclude VT, but from Perl 5.18 it is included,1483which means that Perl space and POSIX space are now identical. PCRE1484was changed at release 8.34. */14851486case PT_SPACE: /* Perl space */1487case PT_PXSPACE: /* POSIX space */1488switch(c)1489{1490HSPACE_CASES:1491VSPACE_CASES:1492OK = TRUE;1493break;14941495default:1496OK = PRIV(ucp_gentype)[prop->chartype] == ucp_Z;1497break;1498}1499break;15001501case PT_WORD:1502chartype = prop->chartype;1503OK = PRIV(ucp_gentype)[chartype] == ucp_L ||1504PRIV(ucp_gentype)[chartype] == ucp_N ||1505chartype == ucp_Mn || chartype == ucp_Pc;1506break;15071508case PT_CLIST:1509#if PCRE2_CODE_UNIT_WIDTH == 321510if (c > MAX_UTF_CODE_POINT)1511{1512OK = FALSE;1513break;1514}1515#endif1516cp = PRIV(ucd_caseless_sets) + code[3];1517for (;;)1518{1519if (c < *cp) { OK = FALSE; break; }1520if (c == *cp++) { OK = TRUE; break; }1521}1522break;15231524case PT_UCNC:1525OK = c == CHAR_DOLLAR_SIGN || c == CHAR_COMMERCIAL_AT ||1526c == CHAR_GRAVE_ACCENT || (c >= 0xa0 && c <= 0xd7ff) ||1527c >= 0xe000;1528break;15291530case PT_BIDICL:1531OK = UCD_BIDICLASS(c) == code[3];1532break;15331534case PT_BOOL:1535OK = MAPBIT(PRIV(ucd_boolprop_sets) +1536UCD_BPROPS_PROP(prop), code[3]) != 0;1537break;15381539/* Should never occur, but keep compilers from grumbling. */15401541default:1542OK = codevalue != OP_PROP;1543break;1544}15451546if (OK == (d == OP_PROP))1547{1548if (count > 0 && codevalue == OP_PROP_EXTRA + OP_TYPEPOSPLUS)1549{1550active_count--; /* Remove non-match possibility */1551next_active_state--;1552}1553count++;1554ADD_NEW(state_offset, count);1555}1556}1557break;15581559/*-----------------------------------------------------------------*/1560case OP_EXTUNI_EXTRA + OP_TYPEPLUS:1561case OP_EXTUNI_EXTRA + OP_TYPEMINPLUS:1562case OP_EXTUNI_EXTRA + OP_TYPEPOSPLUS:1563count = current_state->count; /* Already matched */1564if (count > 0) { ADD_ACTIVE(state_offset + 2, 0); }1565if (clen > 0)1566{1567int ncount = 0;1568if (count > 0 && codevalue == OP_EXTUNI_EXTRA + OP_TYPEPOSPLUS)1569{1570active_count--; /* Remove non-match possibility */1571next_active_state--;1572}1573(void)PRIV(extuni)(c, ptr + clen, mb->start_subject, end_subject, utf,1574&ncount);1575count++;1576ADD_NEW_DATA(-state_offset, count, ncount);1577}1578break;1579#endif15801581/*-----------------------------------------------------------------*/1582case OP_ANYNL_EXTRA + OP_TYPEPLUS:1583case OP_ANYNL_EXTRA + OP_TYPEMINPLUS:1584case OP_ANYNL_EXTRA + OP_TYPEPOSPLUS:1585count = current_state->count; /* Already matched */1586if (count > 0) { ADD_ACTIVE(state_offset + 2, 0); }1587if (clen > 0)1588{1589int ncount = 0;1590switch (c)1591{1592case CHAR_VT:1593case CHAR_FF:1594case CHAR_NEL:1595#ifndef EBCDIC1596case 0x2028:1597case 0x2029:1598#endif /* Not EBCDIC */1599if (mb->bsr_convention == PCRE2_BSR_ANYCRLF) break;1600goto ANYNL01;16011602case CHAR_CR:1603if (ptr + 1 < end_subject && UCHAR21TEST(ptr + 1) == CHAR_LF) ncount = 1;1604/* Fall through */16051606ANYNL01:1607case CHAR_LF:1608if (count > 0 && codevalue == OP_ANYNL_EXTRA + OP_TYPEPOSPLUS)1609{1610active_count--; /* Remove non-match possibility */1611next_active_state--;1612}1613count++;1614ADD_NEW_DATA(-state_offset, count, ncount);1615break;16161617default:1618break;1619}1620}1621break;16221623/*-----------------------------------------------------------------*/1624case OP_VSPACE_EXTRA + OP_TYPEPLUS:1625case OP_VSPACE_EXTRA + OP_TYPEMINPLUS:1626case OP_VSPACE_EXTRA + OP_TYPEPOSPLUS:1627count = current_state->count; /* Already matched */1628if (count > 0) { ADD_ACTIVE(state_offset + 2, 0); }1629if (clen > 0)1630{1631BOOL OK;1632switch (c)1633{1634VSPACE_CASES:1635OK = TRUE;1636break;16371638default:1639OK = FALSE;1640break;1641}16421643if (OK == (d == OP_VSPACE))1644{1645if (count > 0 && codevalue == OP_VSPACE_EXTRA + OP_TYPEPOSPLUS)1646{1647active_count--; /* Remove non-match possibility */1648next_active_state--;1649}1650count++;1651ADD_NEW_DATA(-state_offset, count, 0);1652}1653}1654break;16551656/*-----------------------------------------------------------------*/1657case OP_HSPACE_EXTRA + OP_TYPEPLUS:1658case OP_HSPACE_EXTRA + OP_TYPEMINPLUS:1659case OP_HSPACE_EXTRA + OP_TYPEPOSPLUS:1660count = current_state->count; /* Already matched */1661if (count > 0) { ADD_ACTIVE(state_offset + 2, 0); }1662if (clen > 0)1663{1664BOOL OK;1665switch (c)1666{1667HSPACE_CASES:1668OK = TRUE;1669break;16701671default:1672OK = FALSE;1673break;1674}16751676if (OK == (d == OP_HSPACE))1677{1678if (count > 0 && codevalue == OP_HSPACE_EXTRA + OP_TYPEPOSPLUS)1679{1680active_count--; /* Remove non-match possibility */1681next_active_state--;1682}1683count++;1684ADD_NEW_DATA(-state_offset, count, 0);1685}1686}1687break;16881689/*-----------------------------------------------------------------*/1690#ifdef SUPPORT_UNICODE1691case OP_PROP_EXTRA + OP_TYPEQUERY:1692case OP_PROP_EXTRA + OP_TYPEMINQUERY:1693case OP_PROP_EXTRA + OP_TYPEPOSQUERY:1694count = 4;1695goto QS1;16961697case OP_PROP_EXTRA + OP_TYPESTAR:1698case OP_PROP_EXTRA + OP_TYPEMINSTAR:1699case OP_PROP_EXTRA + OP_TYPEPOSSTAR:1700count = 0;17011702QS1:17031704ADD_ACTIVE(state_offset + 4, 0);1705if (clen > 0)1706{1707BOOL OK;1708int chartype;1709const uint32_t *cp;1710const ucd_record * prop = GET_UCD(c);1711switch(code[2])1712{1713case PT_LAMP:1714chartype = prop->chartype;1715OK = chartype == ucp_Lu || chartype == ucp_Ll || chartype == ucp_Lt;1716break;17171718case PT_GC:1719OK = PRIV(ucp_gentype)[prop->chartype] == code[3];1720break;17211722case PT_PC:1723OK = prop->chartype == code[3];1724break;17251726case PT_SC:1727OK = prop->script == code[3];1728break;17291730case PT_SCX:1731OK = (prop->script == code[3] ||1732MAPBIT(PRIV(ucd_script_sets) + UCD_SCRIPTX_PROP(prop), code[3]) != 0);1733break;17341735/* These are specials for combination cases. */17361737case PT_ALNUM:1738chartype = prop->chartype;1739OK = PRIV(ucp_gentype)[chartype] == ucp_L ||1740PRIV(ucp_gentype)[chartype] == ucp_N;1741break;17421743/* Perl space used to exclude VT, but from Perl 5.18 it is included,1744which means that Perl space and POSIX space are now identical. PCRE1745was changed at release 8.34. */17461747case PT_SPACE: /* Perl space */1748case PT_PXSPACE: /* POSIX space */1749switch(c)1750{1751HSPACE_CASES:1752VSPACE_CASES:1753OK = TRUE;1754break;17551756default:1757OK = PRIV(ucp_gentype)[prop->chartype] == ucp_Z;1758break;1759}1760break;17611762case PT_WORD:1763chartype = prop->chartype;1764OK = PRIV(ucp_gentype)[chartype] == ucp_L ||1765PRIV(ucp_gentype)[chartype] == ucp_N ||1766chartype == ucp_Mn || chartype == ucp_Pc;1767break;17681769case PT_CLIST:1770#if PCRE2_CODE_UNIT_WIDTH == 321771if (c > MAX_UTF_CODE_POINT)1772{1773OK = FALSE;1774break;1775}1776#endif1777cp = PRIV(ucd_caseless_sets) + code[3];1778for (;;)1779{1780if (c < *cp) { OK = FALSE; break; }1781if (c == *cp++) { OK = TRUE; break; }1782}1783break;17841785case PT_UCNC:1786OK = c == CHAR_DOLLAR_SIGN || c == CHAR_COMMERCIAL_AT ||1787c == CHAR_GRAVE_ACCENT || (c >= 0xa0 && c <= 0xd7ff) ||1788c >= 0xe000;1789break;17901791case PT_BIDICL:1792OK = UCD_BIDICLASS(c) == code[3];1793break;17941795case PT_BOOL:1796OK = MAPBIT(PRIV(ucd_boolprop_sets) +1797UCD_BPROPS_PROP(prop), code[3]) != 0;1798break;17991800/* Should never occur, but keep compilers from grumbling. */18011802default:1803OK = codevalue != OP_PROP;1804break;1805}18061807if (OK == (d == OP_PROP))1808{1809if (codevalue == OP_PROP_EXTRA + OP_TYPEPOSSTAR ||1810codevalue == OP_PROP_EXTRA + OP_TYPEPOSQUERY)1811{1812active_count--; /* Remove non-match possibility */1813next_active_state--;1814}1815ADD_NEW(state_offset + count, 0);1816}1817}1818break;18191820/*-----------------------------------------------------------------*/1821case OP_EXTUNI_EXTRA + OP_TYPEQUERY:1822case OP_EXTUNI_EXTRA + OP_TYPEMINQUERY:1823case OP_EXTUNI_EXTRA + OP_TYPEPOSQUERY:1824count = 2;1825goto QS2;18261827case OP_EXTUNI_EXTRA + OP_TYPESTAR:1828case OP_EXTUNI_EXTRA + OP_TYPEMINSTAR:1829case OP_EXTUNI_EXTRA + OP_TYPEPOSSTAR:1830count = 0;18311832QS2:18331834ADD_ACTIVE(state_offset + 2, 0);1835if (clen > 0)1836{1837int ncount = 0;1838if (codevalue == OP_EXTUNI_EXTRA + OP_TYPEPOSSTAR ||1839codevalue == OP_EXTUNI_EXTRA + OP_TYPEPOSQUERY)1840{1841active_count--; /* Remove non-match possibility */1842next_active_state--;1843}1844(void)PRIV(extuni)(c, ptr + clen, mb->start_subject, end_subject, utf,1845&ncount);1846ADD_NEW_DATA(-(state_offset + count), 0, ncount);1847}1848break;1849#endif18501851/*-----------------------------------------------------------------*/1852case OP_ANYNL_EXTRA + OP_TYPEQUERY:1853case OP_ANYNL_EXTRA + OP_TYPEMINQUERY:1854case OP_ANYNL_EXTRA + OP_TYPEPOSQUERY:1855count = 2;1856goto QS3;18571858case OP_ANYNL_EXTRA + OP_TYPESTAR:1859case OP_ANYNL_EXTRA + OP_TYPEMINSTAR:1860case OP_ANYNL_EXTRA + OP_TYPEPOSSTAR:1861count = 0;18621863QS3:1864ADD_ACTIVE(state_offset + 2, 0);1865if (clen > 0)1866{1867int ncount = 0;1868switch (c)1869{1870case CHAR_VT:1871case CHAR_FF:1872case CHAR_NEL:1873#ifndef EBCDIC1874case 0x2028:1875case 0x2029:1876#endif /* Not EBCDIC */1877if (mb->bsr_convention == PCRE2_BSR_ANYCRLF) break;1878goto ANYNL02;18791880case CHAR_CR:1881if (ptr + 1 < end_subject && UCHAR21TEST(ptr + 1) == CHAR_LF) ncount = 1;1882/* Fall through */18831884ANYNL02:1885case CHAR_LF:1886if (codevalue == OP_ANYNL_EXTRA + OP_TYPEPOSSTAR ||1887codevalue == OP_ANYNL_EXTRA + OP_TYPEPOSQUERY)1888{1889active_count--; /* Remove non-match possibility */1890next_active_state--;1891}1892ADD_NEW_DATA(-(state_offset + (int)count), 0, ncount);1893break;18941895default:1896break;1897}1898}1899break;19001901/*-----------------------------------------------------------------*/1902case OP_VSPACE_EXTRA + OP_TYPEQUERY:1903case OP_VSPACE_EXTRA + OP_TYPEMINQUERY:1904case OP_VSPACE_EXTRA + OP_TYPEPOSQUERY:1905count = 2;1906goto QS4;19071908case OP_VSPACE_EXTRA + OP_TYPESTAR:1909case OP_VSPACE_EXTRA + OP_TYPEMINSTAR:1910case OP_VSPACE_EXTRA + OP_TYPEPOSSTAR:1911count = 0;19121913QS4:1914ADD_ACTIVE(state_offset + 2, 0);1915if (clen > 0)1916{1917BOOL OK;1918switch (c)1919{1920VSPACE_CASES:1921OK = TRUE;1922break;19231924default:1925OK = FALSE;1926break;1927}1928if (OK == (d == OP_VSPACE))1929{1930if (codevalue == OP_VSPACE_EXTRA + OP_TYPEPOSSTAR ||1931codevalue == OP_VSPACE_EXTRA + OP_TYPEPOSQUERY)1932{1933active_count--; /* Remove non-match possibility */1934next_active_state--;1935}1936ADD_NEW_DATA(-(state_offset + (int)count), 0, 0);1937}1938}1939break;19401941/*-----------------------------------------------------------------*/1942case OP_HSPACE_EXTRA + OP_TYPEQUERY:1943case OP_HSPACE_EXTRA + OP_TYPEMINQUERY:1944case OP_HSPACE_EXTRA + OP_TYPEPOSQUERY:1945count = 2;1946goto QS5;19471948case OP_HSPACE_EXTRA + OP_TYPESTAR:1949case OP_HSPACE_EXTRA + OP_TYPEMINSTAR:1950case OP_HSPACE_EXTRA + OP_TYPEPOSSTAR:1951count = 0;19521953QS5:1954ADD_ACTIVE(state_offset + 2, 0);1955if (clen > 0)1956{1957BOOL OK;1958switch (c)1959{1960HSPACE_CASES:1961OK = TRUE;1962break;19631964default:1965OK = FALSE;1966break;1967}19681969if (OK == (d == OP_HSPACE))1970{1971if (codevalue == OP_HSPACE_EXTRA + OP_TYPEPOSSTAR ||1972codevalue == OP_HSPACE_EXTRA + OP_TYPEPOSQUERY)1973{1974active_count--; /* Remove non-match possibility */1975next_active_state--;1976}1977ADD_NEW_DATA(-(state_offset + (int)count), 0, 0);1978}1979}1980break;19811982/*-----------------------------------------------------------------*/1983#ifdef SUPPORT_UNICODE1984case OP_PROP_EXTRA + OP_TYPEEXACT:1985case OP_PROP_EXTRA + OP_TYPEUPTO:1986case OP_PROP_EXTRA + OP_TYPEMINUPTO:1987case OP_PROP_EXTRA + OP_TYPEPOSUPTO:1988if (codevalue != OP_PROP_EXTRA + OP_TYPEEXACT)1989{ ADD_ACTIVE(state_offset + 1 + IMM2_SIZE + 3, 0); }1990count = current_state->count; /* Number already matched */1991if (clen > 0)1992{1993BOOL OK;1994int chartype;1995const uint32_t *cp;1996const ucd_record * prop = GET_UCD(c);1997switch(code[1 + IMM2_SIZE + 1])1998{1999case PT_LAMP:2000chartype = prop->chartype;2001OK = chartype == ucp_Lu || chartype == ucp_Ll || chartype == ucp_Lt;2002break;20032004case PT_GC:2005OK = PRIV(ucp_gentype)[prop->chartype] == code[1 + IMM2_SIZE + 2];2006break;20072008case PT_PC:2009OK = prop->chartype == code[1 + IMM2_SIZE + 2];2010break;20112012case PT_SC:2013OK = prop->script == code[1 + IMM2_SIZE + 2];2014break;20152016case PT_SCX:2017OK = (prop->script == code[1 + IMM2_SIZE + 2] ||2018MAPBIT(PRIV(ucd_script_sets) + UCD_SCRIPTX_PROP(prop),2019code[1 + IMM2_SIZE + 2]) != 0);2020break;20212022/* These are specials for combination cases. */20232024case PT_ALNUM:2025chartype = prop->chartype;2026OK = PRIV(ucp_gentype)[chartype] == ucp_L ||2027PRIV(ucp_gentype)[chartype] == ucp_N;2028break;20292030/* Perl space used to exclude VT, but from Perl 5.18 it is included,2031which means that Perl space and POSIX space are now identical. PCRE2032was changed at release 8.34. */20332034case PT_SPACE: /* Perl space */2035case PT_PXSPACE: /* POSIX space */2036switch(c)2037{2038HSPACE_CASES:2039VSPACE_CASES:2040OK = TRUE;2041break;20422043default:2044OK = PRIV(ucp_gentype)[prop->chartype] == ucp_Z;2045break;2046}2047break;20482049case PT_WORD:2050chartype = prop->chartype;2051OK = PRIV(ucp_gentype)[chartype] == ucp_L ||2052PRIV(ucp_gentype)[chartype] == ucp_N ||2053chartype == ucp_Mn || chartype == ucp_Pc;2054break;20552056case PT_CLIST:2057#if PCRE2_CODE_UNIT_WIDTH == 322058if (c > MAX_UTF_CODE_POINT)2059{2060OK = FALSE;2061break;2062}2063#endif2064cp = PRIV(ucd_caseless_sets) + code[1 + IMM2_SIZE + 2];2065for (;;)2066{2067if (c < *cp) { OK = FALSE; break; }2068if (c == *cp++) { OK = TRUE; break; }2069}2070break;20712072case PT_UCNC:2073OK = c == CHAR_DOLLAR_SIGN || c == CHAR_COMMERCIAL_AT ||2074c == CHAR_GRAVE_ACCENT || (c >= 0xa0 && c <= 0xd7ff) ||2075c >= 0xe000;2076break;20772078case PT_BIDICL:2079OK = UCD_BIDICLASS(c) == code[1 + IMM2_SIZE + 2];2080break;20812082case PT_BOOL:2083OK = MAPBIT(PRIV(ucd_boolprop_sets) +2084UCD_BPROPS_PROP(prop), code[1 + IMM2_SIZE + 2]) != 0;2085break;20862087/* Should never occur, but keep compilers from grumbling. */20882089default:2090OK = codevalue != OP_PROP;2091break;2092}20932094if (OK == (d == OP_PROP))2095{2096if (codevalue == OP_PROP_EXTRA + OP_TYPEPOSUPTO)2097{2098active_count--; /* Remove non-match possibility */2099next_active_state--;2100}2101if (++count >= (int)GET2(code, 1))2102{ ADD_NEW(state_offset + 1 + IMM2_SIZE + 3, 0); }2103else2104{ ADD_NEW(state_offset, count); }2105}2106}2107break;21082109/*-----------------------------------------------------------------*/2110case OP_EXTUNI_EXTRA + OP_TYPEEXACT:2111case OP_EXTUNI_EXTRA + OP_TYPEUPTO:2112case OP_EXTUNI_EXTRA + OP_TYPEMINUPTO:2113case OP_EXTUNI_EXTRA + OP_TYPEPOSUPTO:2114if (codevalue != OP_EXTUNI_EXTRA + OP_TYPEEXACT)2115{ ADD_ACTIVE(state_offset + 2 + IMM2_SIZE, 0); }2116count = current_state->count; /* Number already matched */2117if (clen > 0)2118{2119PCRE2_SPTR nptr;2120int ncount = 0;2121if (codevalue == OP_EXTUNI_EXTRA + OP_TYPEPOSUPTO)2122{2123active_count--; /* Remove non-match possibility */2124next_active_state--;2125}2126nptr = PRIV(extuni)(c, ptr + clen, mb->start_subject, end_subject, utf,2127&ncount);2128if (nptr >= end_subject && (mb->moptions & PCRE2_PARTIAL_HARD) != 0)2129reset_could_continue = TRUE;2130if (++count >= (int)GET2(code, 1))2131{ ADD_NEW_DATA(-(state_offset + 2 + IMM2_SIZE), 0, ncount); }2132else2133{ ADD_NEW_DATA(-state_offset, count, ncount); }2134}2135break;2136#endif21372138/*-----------------------------------------------------------------*/2139case OP_ANYNL_EXTRA + OP_TYPEEXACT:2140case OP_ANYNL_EXTRA + OP_TYPEUPTO:2141case OP_ANYNL_EXTRA + OP_TYPEMINUPTO:2142case OP_ANYNL_EXTRA + OP_TYPEPOSUPTO:2143if (codevalue != OP_ANYNL_EXTRA + OP_TYPEEXACT)2144{ ADD_ACTIVE(state_offset + 2 + IMM2_SIZE, 0); }2145count = current_state->count; /* Number already matched */2146if (clen > 0)2147{2148int ncount = 0;2149switch (c)2150{2151case CHAR_VT:2152case CHAR_FF:2153case CHAR_NEL:2154#ifndef EBCDIC2155case 0x2028:2156case 0x2029:2157#endif /* Not EBCDIC */2158if (mb->bsr_convention == PCRE2_BSR_ANYCRLF) break;2159goto ANYNL03;21602161case CHAR_CR:2162if (ptr + 1 < end_subject && UCHAR21TEST(ptr + 1) == CHAR_LF) ncount = 1;2163/* Fall through */21642165ANYNL03:2166case CHAR_LF:2167if (codevalue == OP_ANYNL_EXTRA + OP_TYPEPOSUPTO)2168{2169active_count--; /* Remove non-match possibility */2170next_active_state--;2171}2172if (++count >= (int)GET2(code, 1))2173{ ADD_NEW_DATA(-(state_offset + 2 + IMM2_SIZE), 0, ncount); }2174else2175{ ADD_NEW_DATA(-state_offset, count, ncount); }2176break;21772178default:2179break;2180}2181}2182break;21832184/*-----------------------------------------------------------------*/2185case OP_VSPACE_EXTRA + OP_TYPEEXACT:2186case OP_VSPACE_EXTRA + OP_TYPEUPTO:2187case OP_VSPACE_EXTRA + OP_TYPEMINUPTO:2188case OP_VSPACE_EXTRA + OP_TYPEPOSUPTO:2189if (codevalue != OP_VSPACE_EXTRA + OP_TYPEEXACT)2190{ ADD_ACTIVE(state_offset + 2 + IMM2_SIZE, 0); }2191count = current_state->count; /* Number already matched */2192if (clen > 0)2193{2194BOOL OK;2195switch (c)2196{2197VSPACE_CASES:2198OK = TRUE;2199break;22002201default:2202OK = FALSE;2203}22042205if (OK == (d == OP_VSPACE))2206{2207if (codevalue == OP_VSPACE_EXTRA + OP_TYPEPOSUPTO)2208{2209active_count--; /* Remove non-match possibility */2210next_active_state--;2211}2212if (++count >= (int)GET2(code, 1))2213{ ADD_NEW_DATA(-(state_offset + 2 + IMM2_SIZE), 0, 0); }2214else2215{ ADD_NEW_DATA(-state_offset, count, 0); }2216}2217}2218break;22192220/*-----------------------------------------------------------------*/2221case OP_HSPACE_EXTRA + OP_TYPEEXACT:2222case OP_HSPACE_EXTRA + OP_TYPEUPTO:2223case OP_HSPACE_EXTRA + OP_TYPEMINUPTO:2224case OP_HSPACE_EXTRA + OP_TYPEPOSUPTO:2225if (codevalue != OP_HSPACE_EXTRA + OP_TYPEEXACT)2226{ ADD_ACTIVE(state_offset + 2 + IMM2_SIZE, 0); }2227count = current_state->count; /* Number already matched */2228if (clen > 0)2229{2230BOOL OK;2231switch (c)2232{2233HSPACE_CASES:2234OK = TRUE;2235break;22362237default:2238OK = FALSE;2239break;2240}22412242if (OK == (d == OP_HSPACE))2243{2244if (codevalue == OP_HSPACE_EXTRA + OP_TYPEPOSUPTO)2245{2246active_count--; /* Remove non-match possibility */2247next_active_state--;2248}2249if (++count >= (int)GET2(code, 1))2250{ ADD_NEW_DATA(-(state_offset + 2 + IMM2_SIZE), 0, 0); }2251else2252{ ADD_NEW_DATA(-state_offset, count, 0); }2253}2254}2255break;22562257/* ========================================================================== */2258/* These opcodes are followed by a character that is usually compared2259to the current subject character; it is loaded into d. We still get2260here even if there is no subject character, because in some cases zero2261repetitions are permitted. */22622263/*-----------------------------------------------------------------*/2264case OP_CHAR:2265if (clen > 0 && c == d) { ADD_NEW(state_offset + dlen + 1, 0); }2266break;22672268/*-----------------------------------------------------------------*/2269case OP_CHARI:2270if (clen == 0) break;22712272#ifdef SUPPORT_UNICODE2273if (utf_or_ucp)2274{2275if (c == d) { ADD_NEW(state_offset + dlen + 1, 0); } else2276{2277unsigned int othercase;2278if (c < 128)2279othercase = fcc[c];2280else2281othercase = UCD_OTHERCASE(c);2282if (d == othercase) { ADD_NEW(state_offset + dlen + 1, 0); }2283}2284}2285else2286#endif /* SUPPORT_UNICODE */2287/* Not UTF or UCP mode */2288{2289if (TABLE_GET(c, lcc, c) == TABLE_GET(d, lcc, d))2290{ ADD_NEW(state_offset + 2, 0); }2291}2292break;229322942295#ifdef SUPPORT_UNICODE2296/*-----------------------------------------------------------------*/2297/* This is a tricky one because it can match more than one character.2298Find out how many characters to skip, and then set up a negative state2299to wait for them to pass before continuing. */23002301case OP_EXTUNI:2302if (clen > 0)2303{2304int ncount = 0;2305PCRE2_SPTR nptr = PRIV(extuni)(c, ptr + clen, mb->start_subject,2306end_subject, utf, &ncount);2307if (nptr >= end_subject && (mb->moptions & PCRE2_PARTIAL_HARD) != 0)2308reset_could_continue = TRUE;2309ADD_NEW_DATA(-(state_offset + 1), 0, ncount);2310}2311break;2312#endif23132314/*-----------------------------------------------------------------*/2315/* This is a tricky like EXTUNI because it too can match more than one2316character (when CR is followed by LF). In this case, set up a negative2317state to wait for one character to pass before continuing. */23182319case OP_ANYNL:2320if (clen > 0) switch(c)2321{2322case CHAR_VT:2323case CHAR_FF:2324case CHAR_NEL:2325#ifndef EBCDIC2326case 0x2028:2327case 0x2029:2328#endif /* Not EBCDIC */2329if (mb->bsr_convention == PCRE2_BSR_ANYCRLF) break;2330PCRE2_FALLTHROUGH /* Fall through */23312332case CHAR_LF:2333ADD_NEW(state_offset + 1, 0);2334break;23352336case CHAR_CR:2337if (ptr + 1 >= end_subject)2338{2339ADD_NEW(state_offset + 1, 0);2340if ((mb->moptions & PCRE2_PARTIAL_HARD) != 0)2341reset_could_continue = TRUE;2342}2343else if (UCHAR21TEST(ptr + 1) == CHAR_LF)2344{2345ADD_NEW_DATA(-(state_offset + 1), 0, 1);2346}2347else2348{2349ADD_NEW(state_offset + 1, 0);2350}2351break;2352}2353break;23542355/*-----------------------------------------------------------------*/2356case OP_NOT_VSPACE:2357if (clen > 0) switch(c)2358{2359VSPACE_CASES:2360break;23612362default:2363ADD_NEW(state_offset + 1, 0);2364break;2365}2366break;23672368/*-----------------------------------------------------------------*/2369case OP_VSPACE:2370if (clen > 0) switch(c)2371{2372VSPACE_CASES:2373ADD_NEW(state_offset + 1, 0);2374break;23752376default:2377break;2378}2379break;23802381/*-----------------------------------------------------------------*/2382case OP_NOT_HSPACE:2383if (clen > 0) switch(c)2384{2385HSPACE_CASES:2386break;23872388default:2389ADD_NEW(state_offset + 1, 0);2390break;2391}2392break;23932394/*-----------------------------------------------------------------*/2395case OP_HSPACE:2396if (clen > 0) switch(c)2397{2398HSPACE_CASES:2399ADD_NEW(state_offset + 1, 0);2400break;24012402default:2403break;2404}2405break;24062407/*-----------------------------------------------------------------*/2408/* Match a negated single character casefully. */24092410case OP_NOT:2411if (clen > 0 && c != d) { ADD_NEW(state_offset + dlen + 1, 0); }2412break;24132414/*-----------------------------------------------------------------*/2415/* Match a negated single character caselessly. */24162417case OP_NOTI:2418if (clen > 0)2419{2420uint32_t otherd;2421#ifdef SUPPORT_UNICODE2422if (utf_or_ucp && d >= 128)2423otherd = UCD_OTHERCASE(d);2424else2425#endif /* SUPPORT_UNICODE */2426otherd = TABLE_GET(d, fcc, d);2427if (c != d && c != otherd)2428{ ADD_NEW(state_offset + dlen + 1, 0); }2429}2430break;24312432/*-----------------------------------------------------------------*/2433case OP_PLUSI:2434case OP_MINPLUSI:2435case OP_POSPLUSI:2436case OP_NOTPLUSI:2437case OP_NOTMINPLUSI:2438case OP_NOTPOSPLUSI:2439caseless = TRUE;2440codevalue -= OP_STARI - OP_STAR;24412442PCRE2_FALLTHROUGH /* Fall through */2443case OP_PLUS:2444case OP_MINPLUS:2445case OP_POSPLUS:2446case OP_NOTPLUS:2447case OP_NOTMINPLUS:2448case OP_NOTPOSPLUS:2449count = current_state->count; /* Already matched */2450if (count > 0) { ADD_ACTIVE(state_offset + dlen + 1, 0); }2451if (clen > 0)2452{2453uint32_t otherd = NOTACHAR;2454if (caseless)2455{2456#ifdef SUPPORT_UNICODE2457if (utf_or_ucp && d >= 128)2458otherd = UCD_OTHERCASE(d);2459else2460#endif /* SUPPORT_UNICODE */2461otherd = TABLE_GET(d, fcc, d);2462}2463if ((c == d || c == otherd) == (codevalue < OP_NOTSTAR))2464{2465if (count > 0 &&2466(codevalue == OP_POSPLUS || codevalue == OP_NOTPOSPLUS))2467{2468active_count--; /* Remove non-match possibility */2469next_active_state--;2470}2471count++;2472ADD_NEW(state_offset, count);2473}2474}2475break;24762477/*-----------------------------------------------------------------*/2478case OP_QUERYI:2479case OP_MINQUERYI:2480case OP_POSQUERYI:2481case OP_NOTQUERYI:2482case OP_NOTMINQUERYI:2483case OP_NOTPOSQUERYI:2484caseless = TRUE;2485codevalue -= OP_STARI - OP_STAR;2486PCRE2_FALLTHROUGH /* Fall through */2487case OP_QUERY:2488case OP_MINQUERY:2489case OP_POSQUERY:2490case OP_NOTQUERY:2491case OP_NOTMINQUERY:2492case OP_NOTPOSQUERY:2493ADD_ACTIVE(state_offset + dlen + 1, 0);2494if (clen > 0)2495{2496uint32_t otherd = NOTACHAR;2497if (caseless)2498{2499#ifdef SUPPORT_UNICODE2500if (utf_or_ucp && d >= 128)2501otherd = UCD_OTHERCASE(d);2502else2503#endif /* SUPPORT_UNICODE */2504otherd = TABLE_GET(d, fcc, d);2505}2506if ((c == d || c == otherd) == (codevalue < OP_NOTSTAR))2507{2508if (codevalue == OP_POSQUERY || codevalue == OP_NOTPOSQUERY)2509{2510active_count--; /* Remove non-match possibility */2511next_active_state--;2512}2513ADD_NEW(state_offset + dlen + 1, 0);2514}2515}2516break;25172518/*-----------------------------------------------------------------*/2519case OP_STARI:2520case OP_MINSTARI:2521case OP_POSSTARI:2522case OP_NOTSTARI:2523case OP_NOTMINSTARI:2524case OP_NOTPOSSTARI:2525caseless = TRUE;2526codevalue -= OP_STARI - OP_STAR;2527PCRE2_FALLTHROUGH /* Fall through */2528case OP_STAR:2529case OP_MINSTAR:2530case OP_POSSTAR:2531case OP_NOTSTAR:2532case OP_NOTMINSTAR:2533case OP_NOTPOSSTAR:2534ADD_ACTIVE(state_offset + dlen + 1, 0);2535if (clen > 0)2536{2537uint32_t otherd = NOTACHAR;2538if (caseless)2539{2540#ifdef SUPPORT_UNICODE2541if (utf_or_ucp && d >= 128)2542otherd = UCD_OTHERCASE(d);2543else2544#endif /* SUPPORT_UNICODE */2545otherd = TABLE_GET(d, fcc, d);2546}2547if ((c == d || c == otherd) == (codevalue < OP_NOTSTAR))2548{2549if (codevalue == OP_POSSTAR || codevalue == OP_NOTPOSSTAR)2550{2551active_count--; /* Remove non-match possibility */2552next_active_state--;2553}2554ADD_NEW(state_offset, 0);2555}2556}2557break;25582559/*-----------------------------------------------------------------*/2560case OP_EXACTI:2561case OP_NOTEXACTI:2562caseless = TRUE;2563codevalue -= OP_STARI - OP_STAR;2564PCRE2_FALLTHROUGH /* Fall through */2565case OP_EXACT:2566case OP_NOTEXACT:2567count = current_state->count; /* Number already matched */2568if (clen > 0)2569{2570uint32_t otherd = NOTACHAR;2571if (caseless)2572{2573#ifdef SUPPORT_UNICODE2574if (utf_or_ucp && d >= 128)2575otherd = UCD_OTHERCASE(d);2576else2577#endif /* SUPPORT_UNICODE */2578otherd = TABLE_GET(d, fcc, d);2579}2580if ((c == d || c == otherd) == (codevalue < OP_NOTSTAR))2581{2582if (++count >= (int)GET2(code, 1))2583{ ADD_NEW(state_offset + dlen + 1 + IMM2_SIZE, 0); }2584else2585{ ADD_NEW(state_offset, count); }2586}2587}2588break;25892590/*-----------------------------------------------------------------*/2591case OP_UPTOI:2592case OP_MINUPTOI:2593case OP_POSUPTOI:2594case OP_NOTUPTOI:2595case OP_NOTMINUPTOI:2596case OP_NOTPOSUPTOI:2597caseless = TRUE;2598codevalue -= OP_STARI - OP_STAR;2599PCRE2_FALLTHROUGH /* Fall through */2600case OP_UPTO:2601case OP_MINUPTO:2602case OP_POSUPTO:2603case OP_NOTUPTO:2604case OP_NOTMINUPTO:2605case OP_NOTPOSUPTO:2606ADD_ACTIVE(state_offset + dlen + 1 + IMM2_SIZE, 0);2607count = current_state->count; /* Number already matched */2608if (clen > 0)2609{2610uint32_t otherd = NOTACHAR;2611if (caseless)2612{2613#ifdef SUPPORT_UNICODE2614if (utf_or_ucp && d >= 128)2615otherd = UCD_OTHERCASE(d);2616else2617#endif /* SUPPORT_UNICODE */2618otherd = TABLE_GET(d, fcc, d);2619}2620if ((c == d || c == otherd) == (codevalue < OP_NOTSTAR))2621{2622if (codevalue == OP_POSUPTO || codevalue == OP_NOTPOSUPTO)2623{2624active_count--; /* Remove non-match possibility */2625next_active_state--;2626}2627if (++count >= (int)GET2(code, 1))2628{ ADD_NEW(state_offset + dlen + 1 + IMM2_SIZE, 0); }2629else2630{ ADD_NEW(state_offset, count); }2631}2632}2633break;263426352636/* ========================================================================== */2637/* These are the class-handling opcodes */26382639case OP_CLASS:2640case OP_NCLASS:2641#ifdef SUPPORT_WIDE_CHARS2642case OP_XCLASS:2643case OP_ECLASS:2644#endif2645{2646BOOL isinclass = FALSE;2647int next_state_offset;2648PCRE2_SPTR ecode;26492650#ifdef SUPPORT_WIDE_CHARS2651/* An extended class may have a table or a list of single characters,2652ranges, or both, and it may be positive or negative. There's a2653function that sorts all this out. */26542655if (codevalue == OP_XCLASS)2656{2657ecode = code + GET(code, 1);2658if (clen > 0)2659isinclass = PRIV(xclass)(c, code + 1 + LINK_SIZE,2660(const uint8_t*)mb->start_code, utf);2661}26622663/* A nested set-based class has internal opcodes for performing2664set operations. */26652666else if (codevalue == OP_ECLASS)2667{2668ecode = code + GET(code, 1);2669if (clen > 0)2670isinclass = PRIV(eclass)(c, code + 1 + LINK_SIZE, ecode,2671(const uint8_t*)mb->start_code, utf);2672}26732674else2675#endif /* SUPPORT_WIDE_CHARS */26762677/* For a simple class, there is always just a 32-byte table, and we2678can set isinclass from it. */26792680{2681ecode = code + 1 + (32 / sizeof(PCRE2_UCHAR));2682if (clen > 0)2683{2684isinclass = (c > 255)? (codevalue == OP_NCLASS) :2685((((const uint8_t *)(code + 1))[c/8] & (1u << (c&7))) != 0);2686}2687}26882689/* At this point, isinclass is set for all kinds of class, and ecode2690points to the byte after the end of the class. If there is a2691quantifier, this is where it will be. */26922693next_state_offset = (int)(ecode - start_code);26942695switch (*ecode)2696{2697case OP_CRSTAR:2698case OP_CRMINSTAR:2699case OP_CRPOSSTAR:2700ADD_ACTIVE(next_state_offset + 1, 0);2701if (isinclass)2702{2703if (*ecode == OP_CRPOSSTAR)2704{2705active_count--; /* Remove non-match possibility */2706next_active_state--;2707}2708ADD_NEW(state_offset, 0);2709}2710break;27112712case OP_CRPLUS:2713case OP_CRMINPLUS:2714case OP_CRPOSPLUS:2715count = current_state->count; /* Already matched */2716if (count > 0) { ADD_ACTIVE(next_state_offset + 1, 0); }2717if (isinclass)2718{2719if (count > 0 && *ecode == OP_CRPOSPLUS)2720{2721active_count--; /* Remove non-match possibility */2722next_active_state--;2723}2724count++;2725ADD_NEW(state_offset, count);2726}2727break;27282729case OP_CRQUERY:2730case OP_CRMINQUERY:2731case OP_CRPOSQUERY:2732ADD_ACTIVE(next_state_offset + 1, 0);2733if (isinclass)2734{2735if (*ecode == OP_CRPOSQUERY)2736{2737active_count--; /* Remove non-match possibility */2738next_active_state--;2739}2740ADD_NEW(next_state_offset + 1, 0);2741}2742break;27432744case OP_CRRANGE:2745case OP_CRMINRANGE:2746case OP_CRPOSRANGE:2747count = current_state->count; /* Already matched */2748if (count >= (int)GET2(ecode, 1))2749{ ADD_ACTIVE(next_state_offset + 1 + 2 * IMM2_SIZE, 0); }2750if (isinclass)2751{2752int max = (int)GET2(ecode, 1 + IMM2_SIZE);27532754if (*ecode == OP_CRPOSRANGE && count >= (int)GET2(ecode, 1))2755{2756active_count--; /* Remove non-match possibility */2757next_active_state--;2758}27592760if (++count >= max && max != 0) /* Max 0 => no limit */2761{ ADD_NEW(next_state_offset + 1 + 2 * IMM2_SIZE, 0); }2762else2763{ ADD_NEW(state_offset, count); }2764}2765break;27662767default:2768if (isinclass) { ADD_NEW(next_state_offset, 0); }2769break;2770}2771}2772break;27732774/* ========================================================================== */2775/* These are the opcodes for fancy brackets of various kinds. We have2776to use recursion in order to handle them. The "always failing" assertion2777(?!) is optimised to OP_FAIL when compiling, so we have to support that,2778though the other "backtracking verbs" are not supported. */27792780case OP_FAIL:2781break;27822783case OP_ASSERT:2784case OP_ASSERT_NOT:2785case OP_ASSERTBACK:2786case OP_ASSERTBACK_NOT:2787{2788int rc;2789int *local_workspace;2790PCRE2_SIZE *local_offsets;2791PCRE2_SPTR endasscode = code + GET(code, 1);2792RWS_anchor *rws = (RWS_anchor *)RWS;27932794if (rws->free < RWS_RSIZE + RWS_OVEC_OSIZE)2795{2796rc = more_workspace(&rws, RWS_OVEC_OSIZE, mb);2797if (rc != 0) return rc;2798RWS = (int *)rws;2799}28002801local_offsets = (PCRE2_SIZE *)(RWS + rws->size - rws->free);2802local_workspace = ((int *)local_offsets) + RWS_OVEC_OSIZE;2803rws->free -= RWS_RSIZE + RWS_OVEC_OSIZE;28042805while (*endasscode == OP_ALT) endasscode += GET(endasscode, 1);28062807rc = internal_dfa_match(2808mb, /* static match data */2809code, /* this subexpression's code */2810ptr, /* where we currently are */2811(PCRE2_SIZE)(ptr - start_subject), /* start offset */2812local_offsets, /* offset vector */2813RWS_OVEC_OSIZE/OVEC_UNIT, /* size of same */2814local_workspace, /* workspace vector */2815RWS_RSIZE, /* size of same */2816rlevel, /* function recursion level */2817RWS); /* recursion workspace */28182819rws->free += RWS_RSIZE + RWS_OVEC_OSIZE;28202821if (rc < 0 && rc != PCRE2_ERROR_NOMATCH) return rc;2822if ((rc >= 0) == (codevalue == OP_ASSERT || codevalue == OP_ASSERTBACK))2823{ ADD_ACTIVE((int)(endasscode + LINK_SIZE + 1 - start_code), 0); }2824}2825break;28262827/*-----------------------------------------------------------------*/2828case OP_COND:2829case OP_SCOND:2830{2831int codelink = (int)GET(code, 1);2832PCRE2_UCHAR condcode;28332834/* Because of the way auto-callout works during compile, a callout item2835is inserted between OP_COND and an assertion condition. This does not2836happen for the other conditions. */28372838if (code[LINK_SIZE + 1] == OP_CALLOUT2839|| code[LINK_SIZE + 1] == OP_CALLOUT_STR)2840{2841PCRE2_SIZE callout_length;2842rrc = do_callout_dfa(code, offsets, current_subject, ptr, mb,28431 + LINK_SIZE, &callout_length);2844if (rrc < 0) return rrc; /* Abandon */2845if (rrc > 0) break; /* Fail this thread */2846code += callout_length; /* Skip callout data */2847}28482849condcode = code[LINK_SIZE+1];28502851/* Back reference conditions and duplicate named recursion conditions2852are not supported */28532854if (condcode == OP_CREF || condcode == OP_DNCREF ||2855condcode == OP_DNRREF)2856return PCRE2_ERROR_DFA_UCOND;28572858/* The DEFINE condition is always false, and the assertion (?!) is2859converted to OP_FAIL. */28602861if (condcode == OP_FALSE || condcode == OP_FAIL)2862{ ADD_ACTIVE(state_offset + codelink + LINK_SIZE + 1, 0); }28632864/* There is also an always-true condition */28652866else if (condcode == OP_TRUE)2867{ ADD_ACTIVE(state_offset + LINK_SIZE + 2, 0); }28682869/* The only supported version of OP_RREF is for the value RREF_ANY,2870which means "test if in any recursion". We can't test for specifically2871recursed groups. */28722873else if (condcode == OP_RREF)2874{2875unsigned int value = GET2(code, LINK_SIZE + 2);2876if (value != RREF_ANY) return PCRE2_ERROR_DFA_UCOND;2877if (mb->recursive != NULL)2878{ ADD_ACTIVE(state_offset + LINK_SIZE + 2 + IMM2_SIZE, 0); }2879else { ADD_ACTIVE(state_offset + codelink + LINK_SIZE + 1, 0); }2880}28812882/* Otherwise, the condition is an assertion */28832884else2885{2886int rc;2887int *local_workspace;2888PCRE2_SIZE *local_offsets;2889PCRE2_SPTR asscode = code + LINK_SIZE + 1;2890PCRE2_SPTR endasscode = asscode + GET(asscode, 1);2891RWS_anchor *rws = (RWS_anchor *)RWS;28922893if (rws->free < RWS_RSIZE + RWS_OVEC_OSIZE)2894{2895rc = more_workspace(&rws, RWS_OVEC_OSIZE, mb);2896if (rc != 0) return rc;2897RWS = (int *)rws;2898}28992900local_offsets = (PCRE2_SIZE *)(RWS + rws->size - rws->free);2901local_workspace = ((int *)local_offsets) + RWS_OVEC_OSIZE;2902rws->free -= RWS_RSIZE + RWS_OVEC_OSIZE;29032904while (*endasscode == OP_ALT) endasscode += GET(endasscode, 1);29052906rc = internal_dfa_match(2907mb, /* fixed match data */2908asscode, /* this subexpression's code */2909ptr, /* where we currently are */2910(PCRE2_SIZE)(ptr - start_subject), /* start offset */2911local_offsets, /* offset vector */2912RWS_OVEC_OSIZE/OVEC_UNIT, /* size of same */2913local_workspace, /* workspace vector */2914RWS_RSIZE, /* size of same */2915rlevel, /* function recursion level */2916RWS); /* recursion workspace */29172918rws->free += RWS_RSIZE + RWS_OVEC_OSIZE;29192920if (rc < 0 && rc != PCRE2_ERROR_NOMATCH) return rc;2921if ((rc >= 0) ==2922(condcode == OP_ASSERT || condcode == OP_ASSERTBACK))2923{ ADD_ACTIVE((int)(endasscode + LINK_SIZE + 1 - start_code), 0); }2924else2925{ ADD_ACTIVE(state_offset + codelink + LINK_SIZE + 1, 0); }2926}2927}2928break;29292930/*-----------------------------------------------------------------*/2931case OP_RECURSE:2932{2933int rc;2934int *local_workspace;2935PCRE2_SIZE *local_offsets;2936RWS_anchor *rws = (RWS_anchor *)RWS;2937PCRE2_SPTR callpat = start_code + GET(code, 1);2938uint32_t recno = (callpat == mb->start_code)? 0 :2939GET2(callpat, 1 + LINK_SIZE);29402941/* Argument list has not been supported yet. */2942if (code[1 + LINK_SIZE] == OP_CREF) return PCRE2_ERROR_DFA_UITEM;29432944if (rws->free < RWS_RSIZE + RWS_OVEC_RSIZE)2945{2946rc = more_workspace(&rws, RWS_OVEC_RSIZE, mb);2947if (rc != 0) return rc;2948RWS = (int *)rws;2949}29502951local_offsets = (PCRE2_SIZE *)(RWS + rws->size - rws->free);2952local_workspace = ((int *)local_offsets) + RWS_OVEC_RSIZE;2953rws->free -= RWS_RSIZE + RWS_OVEC_RSIZE;29542955/* Check for repeating a recursion without advancing the subject2956pointer or last used character. This should catch convoluted mutual2957recursions. (Some simple cases are caught at compile time.) */29582959for (dfa_recursion_info *ri = mb->recursive;2960ri != NULL;2961ri = ri->prevrec)2962{2963if (recno == ri->group_num && ptr == ri->subject_position &&2964mb->last_used_ptr == ri->last_used_ptr)2965return PCRE2_ERROR_RECURSELOOP;2966}29672968/* Remember this recursion and where we started it so as to2969catch infinite loops. */29702971new_recursive.group_num = recno;2972new_recursive.subject_position = ptr;2973new_recursive.last_used_ptr = mb->last_used_ptr;2974new_recursive.prevrec = mb->recursive;2975mb->recursive = &new_recursive;29762977rc = internal_dfa_match(2978mb, /* fixed match data */2979callpat, /* this subexpression's code */2980ptr, /* where we currently are */2981(PCRE2_SIZE)(ptr - start_subject), /* start offset */2982local_offsets, /* offset vector */2983RWS_OVEC_RSIZE/OVEC_UNIT, /* size of same */2984local_workspace, /* workspace vector */2985RWS_RSIZE, /* size of same */2986rlevel, /* function recursion level */2987RWS); /* recursion workspace */29882989rws->free += RWS_RSIZE + RWS_OVEC_RSIZE;2990mb->recursive = new_recursive.prevrec; /* Done this recursion */29912992/* Ran out of internal offsets */29932994if (rc == 0) return PCRE2_ERROR_DFA_RECURSE;29952996/* For each successful matched substring, set up the next state with a2997count of characters to skip before trying it. Note that the count is in2998characters, not bytes. */29993000if (rc > 0)3001{3002for (rc = rc*2 - 2; rc >= 0; rc -= 2)3003{3004PCRE2_SIZE charcount = local_offsets[rc+1] - local_offsets[rc];3005#if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH != 323006if (utf)3007{3008PCRE2_SPTR p = start_subject + local_offsets[rc];3009PCRE2_SPTR pp = start_subject + local_offsets[rc+1];3010while (p < pp) if (NOT_FIRSTCU(*p++)) charcount--;3011}3012#endif3013if (charcount > 0)3014{3015ADD_NEW_DATA(-(state_offset + LINK_SIZE + 1), 0,3016(int)(charcount - 1));3017}3018else3019{3020ADD_ACTIVE(state_offset + LINK_SIZE + 1, 0);3021}3022}3023}3024else if (rc != PCRE2_ERROR_NOMATCH) return rc;3025}3026break;30273028/*-----------------------------------------------------------------*/3029case OP_BRAPOS:3030case OP_SBRAPOS:3031case OP_CBRAPOS:3032case OP_SCBRAPOS:3033case OP_BRAPOSZERO:3034{3035int rc;3036int *local_workspace;3037PCRE2_SIZE *local_offsets;3038PCRE2_SIZE charcount, matched_count;3039PCRE2_SPTR local_ptr = ptr;3040RWS_anchor *rws = (RWS_anchor *)RWS;3041BOOL allow_zero;30423043if (rws->free < RWS_RSIZE + RWS_OVEC_OSIZE)3044{3045rc = more_workspace(&rws, RWS_OVEC_OSIZE, mb);3046if (rc != 0) return rc;3047RWS = (int *)rws;3048}30493050local_offsets = (PCRE2_SIZE *)(RWS + rws->size - rws->free);3051local_workspace = ((int *)local_offsets) + RWS_OVEC_OSIZE;3052rws->free -= RWS_RSIZE + RWS_OVEC_OSIZE;30533054if (codevalue == OP_BRAPOSZERO)3055{3056allow_zero = TRUE;3057++code; /* The following opcode will be one of the above BRAs */3058}3059else allow_zero = FALSE;30603061/* Loop to match the subpattern as many times as possible as if it were3062a complete pattern. */30633064for (matched_count = 0;; matched_count++)3065{3066rc = internal_dfa_match(3067mb, /* fixed match data */3068code, /* this subexpression's code */3069local_ptr, /* where we currently are */3070(PCRE2_SIZE)(ptr - start_subject), /* start offset */3071local_offsets, /* offset vector */3072RWS_OVEC_OSIZE/OVEC_UNIT, /* size of same */3073local_workspace, /* workspace vector */3074RWS_RSIZE, /* size of same */3075rlevel, /* function recursion level */3076RWS); /* recursion workspace */30773078/* Failed to match */30793080if (rc < 0)3081{3082if (rc != PCRE2_ERROR_NOMATCH) return rc;3083break;3084}30853086/* Matched: break the loop if zero characters matched. */30873088charcount = local_offsets[1] - local_offsets[0];3089if (charcount == 0) break;3090local_ptr += charcount; /* Advance temporary position ptr */3091}30923093rws->free += RWS_RSIZE + RWS_OVEC_OSIZE;30943095/* At this point we have matched the subpattern matched_count3096times, and local_ptr is pointing to the character after the end of the3097last match. */30983099if (matched_count > 0 || allow_zero)3100{3101PCRE2_SPTR end_subpattern = code;3102int next_state_offset;31033104do { end_subpattern += GET(end_subpattern, 1); }3105while (*end_subpattern == OP_ALT);3106next_state_offset =3107(int)(end_subpattern - start_code + LINK_SIZE + 1);31083109/* Optimization: if there are no more active states, and there3110are no new states yet set up, then skip over the subject string3111right here, to save looping. Otherwise, set up the new state to swing3112into action when the end of the matched substring is reached. */31133114if (i + 1 >= active_count && new_count == 0)3115{3116ptr = local_ptr;3117clen = 0;3118ADD_NEW(next_state_offset, 0);3119}3120else3121{3122PCRE2_SPTR p = ptr;3123PCRE2_SPTR pp = local_ptr;3124charcount = (PCRE2_SIZE)(pp - p);3125#if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH != 323126if (utf) while (p < pp) if (NOT_FIRSTCU(*p++)) charcount--;3127#endif3128ADD_NEW_DATA(-next_state_offset, 0, (int)(charcount - 1));3129}3130}3131}3132break;31333134/*-----------------------------------------------------------------*/3135case OP_ONCE:3136{3137int rc;3138int *local_workspace;3139PCRE2_SIZE *local_offsets;3140RWS_anchor *rws = (RWS_anchor *)RWS;31413142if (rws->free < RWS_RSIZE + RWS_OVEC_OSIZE)3143{3144rc = more_workspace(&rws, RWS_OVEC_OSIZE, mb);3145if (rc != 0) return rc;3146RWS = (int *)rws;3147}31483149local_offsets = (PCRE2_SIZE *)(RWS + rws->size - rws->free);3150local_workspace = ((int *)local_offsets) + RWS_OVEC_OSIZE;3151rws->free -= RWS_RSIZE + RWS_OVEC_OSIZE;31523153rc = internal_dfa_match(3154mb, /* fixed match data */3155code, /* this subexpression's code */3156ptr, /* where we currently are */3157(PCRE2_SIZE)(ptr - start_subject), /* start offset */3158local_offsets, /* offset vector */3159RWS_OVEC_OSIZE/OVEC_UNIT, /* size of same */3160local_workspace, /* workspace vector */3161RWS_RSIZE, /* size of same */3162rlevel, /* function recursion level */3163RWS); /* recursion workspace */31643165rws->free += RWS_RSIZE + RWS_OVEC_OSIZE;31663167if (rc >= 0)3168{3169PCRE2_SPTR end_subpattern = code;3170PCRE2_SIZE charcount = local_offsets[1] - local_offsets[0];3171int next_state_offset, repeat_state_offset;31723173do { end_subpattern += GET(end_subpattern, 1); }3174while (*end_subpattern == OP_ALT);3175next_state_offset =3176(int)(end_subpattern - start_code + LINK_SIZE + 1);31773178/* If the end of this subpattern is KETRMAX or KETRMIN, we must3179arrange for the repeat state also to be added to the relevant list.3180Calculate the offset, or set -1 for no repeat. */31813182repeat_state_offset = (*end_subpattern == OP_KETRMAX ||3183*end_subpattern == OP_KETRMIN)?3184(int)(end_subpattern - start_code - GET(end_subpattern, 1)) : -1;31853186/* If we have matched an empty string, add the next state at the3187current character pointer. This is important so that the duplicate3188checking kicks in, which is what breaks infinite loops that match an3189empty string. */31903191if (charcount == 0)3192{3193ADD_ACTIVE(next_state_offset, 0);3194}31953196/* Optimization: if there are no more active states, and there3197are no new states yet set up, then skip over the subject string3198right here, to save looping. Otherwise, set up the new state to swing3199into action when the end of the matched substring is reached. */32003201else if (i + 1 >= active_count && new_count == 0)3202{3203ptr += charcount;3204clen = 0;3205ADD_NEW(next_state_offset, 0);32063207/* If we are adding a repeat state at the new character position,3208we must fudge things so that it is the only current state.3209Otherwise, it might be a duplicate of one we processed before, and3210that would cause it to be skipped. */32113212if (repeat_state_offset >= 0)3213{3214next_active_state = active_states;3215active_count = 0;3216i = -1;3217ADD_ACTIVE(repeat_state_offset, 0);3218}3219}3220else3221{3222#if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH != 323223if (utf)3224{3225PCRE2_SPTR p = start_subject + local_offsets[0];3226PCRE2_SPTR pp = start_subject + local_offsets[1];3227while (p < pp) if (NOT_FIRSTCU(*p++)) charcount--;3228}3229#endif3230ADD_NEW_DATA(-next_state_offset, 0, (int)(charcount - 1));3231if (repeat_state_offset >= 0)3232{ ADD_NEW_DATA(-repeat_state_offset, 0, (int)(charcount - 1)); }3233}3234}3235else if (rc != PCRE2_ERROR_NOMATCH) return rc;3236}3237break;323832393240/* ========================================================================== */3241/* Handle callouts */32423243case OP_CALLOUT:3244case OP_CALLOUT_STR:3245{3246PCRE2_SIZE callout_length;3247rrc = do_callout_dfa(code, offsets, current_subject, ptr, mb, 0,3248&callout_length);3249if (rrc < 0) return rrc; /* Abandon */3250if (rrc == 0)3251{ ADD_ACTIVE(state_offset + (int)callout_length, 0); }3252}3253break;325432553256/* ========================================================================== */3257default: /* Unsupported opcode */3258return PCRE2_ERROR_DFA_UITEM;3259}32603261NEXT_ACTIVE_STATE: continue;32623263} /* End of loop scanning active states */32643265/* We have finished the processing at the current subject character. If no3266new states have been set for the next character, we have found all the3267matches that we are going to find. If partial matching has been requested,3268check for appropriate conditions.32693270The "could_continue" variable is true if a state could have continued but3271for the fact that the end of the subject was reached. */32723273if (new_count <= 0)3274{3275if (could_continue && /* Some could go on, and */3276( /* either... */3277(mb->moptions & PCRE2_PARTIAL_HARD) != 0 /* Hard partial */3278|| /* or... */3279((mb->moptions & PCRE2_PARTIAL_SOFT) != 0 && /* Soft partial and */3280match_count < 0) /* no matches */3281) && /* And... */3282(3283partial_newline || /* Either partial NL */3284( /* or ... */3285ptr >= end_subject && /* End of subject and */3286( /* either */3287ptr > mb->start_used_ptr || /* Inspected non-empty string */3288mb->allowemptypartial /* or pattern has lookbehind */3289) /* or could match empty */3290)3291))3292match_count = PCRE2_ERROR_PARTIAL;3293break; /* Exit from loop along the subject string */3294}32953296/* One or more states are active for the next character. */32973298ptr += clen; /* Advance to next subject character */3299} /* Loop to move along the subject string */33003301/* Control gets here from "break" a few lines above. If we have a match and3302PCRE2_ENDANCHORED is set, the match fails. */33033304if (match_count >= 0 &&3305((mb->moptions | mb->poptions) & PCRE2_ENDANCHORED) != 0 &&3306ptr < end_subject)3307match_count = PCRE2_ERROR_NOMATCH;33083309return match_count;3310}3311331233133314/*************************************************3315* Match a pattern using the DFA algorithm *3316*************************************************/33173318/* This function matches a compiled pattern to a subject string, using the3319alternate matching algorithm that finds all matches at once.33203321Arguments:3322code points to the compiled pattern3323subject subject string3324length length of subject string3325startoffset where to start matching in the subject3326options option bits3327match_data points to a match data structure3328gcontext points to a match context3329workspace pointer to workspace3330wscount size of workspace33313332Returns: > 0 => number of match offset pairs placed in offsets3333= 0 => offsets overflowed; longest matches are present3334-1 => failed to match3335< -1 => some kind of unexpected problem3336*/33373338PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION3339pcre2_dfa_match(const pcre2_code *code, PCRE2_SPTR subject, PCRE2_SIZE length,3340PCRE2_SIZE start_offset, uint32_t options, pcre2_match_data *match_data,3341pcre2_match_context *mcontext, int *workspace, PCRE2_SIZE wscount)3342{3343int rc;33443345const pcre2_real_code *re = (const pcre2_real_code *)code;3346uint32_t original_options = options;33473348PCRE2_UCHAR null_str[1] = { 0xcd };3349PCRE2_SPTR original_subject = subject;3350PCRE2_SPTR start_match;3351PCRE2_SPTR end_subject;3352PCRE2_SPTR bumpalong_limit;3353PCRE2_SPTR req_cu_ptr;33543355BOOL utf, anchored, startline, firstline;3356BOOL has_first_cu = FALSE;3357BOOL has_req_cu = FALSE;33583359#if PCRE2_CODE_UNIT_WIDTH == 83360PCRE2_SPTR memchr_found_first_cu = NULL;3361PCRE2_SPTR memchr_found_first_cu2 = NULL;3362#endif33633364PCRE2_UCHAR first_cu = 0;3365PCRE2_UCHAR first_cu2 = 0;3366PCRE2_UCHAR req_cu = 0;3367PCRE2_UCHAR req_cu2 = 0;33683369const uint8_t *start_bits = NULL;33703371/* We need to have mb pointing to a match block, because the IS_NEWLINE macro3372is used below, and it expects NLBLOCK to be defined as a pointer. */33733374pcre2_callout_block cb;3375dfa_match_block actual_match_block;3376dfa_match_block *mb = &actual_match_block;33773378/* Set up a starting block of memory for use during recursive calls to3379internal_dfa_match(). By putting this on the stack, it minimizes resource use3380in the case when it is not needed. If this is too small, more memory is3381obtained from the heap. At the start of each block is an anchor structure.*/33823383int base_recursion_workspace[RWS_BASE_SIZE];3384RWS_anchor *rws = (RWS_anchor *)base_recursion_workspace;3385rws->next = NULL;3386rws->size = RWS_BASE_SIZE;3387rws->free = RWS_BASE_SIZE - RWS_ANCHOR_SIZE;33883389/* Recognize NULL, length 0 as an empty string. */33903391if (subject == NULL && length == 0) subject = null_str;33923393/* Plausibility checks */33943395if (match_data == NULL) return PCRE2_ERROR_NULL;3396if (re == NULL || subject == NULL || workspace == NULL)3397{ rc = PCRE2_ERROR_NULL; goto EXIT; }3398if ((options & ~PUBLIC_DFA_MATCH_OPTIONS) != 0)3399{ rc = PCRE2_ERROR_BADOPTION; goto EXIT; }34003401if (length == PCRE2_ZERO_TERMINATED)3402{3403length = PRIV(strlen)(subject);3404}34053406if (wscount < 20) { rc = PCRE2_ERROR_DFA_WSSIZE; goto EXIT; }3407if (start_offset > length) { rc = PCRE2_ERROR_BADOFFSET; goto EXIT; }34083409/* Partial matching and PCRE2_ENDANCHORED are currently not allowed at the same3410time. */34113412if ((options & (PCRE2_PARTIAL_HARD|PCRE2_PARTIAL_SOFT)) != 0 &&3413((re->overall_options | options) & PCRE2_ENDANCHORED) != 0)3414{ rc = PCRE2_ERROR_BADOPTION; goto EXIT; }34153416/* Invalid UTF support is not available for DFA matching. */34173418if ((re->overall_options & PCRE2_MATCH_INVALID_UTF) != 0)3419{ rc = PCRE2_ERROR_DFA_UINVALID_UTF; goto EXIT; }34203421/* Check that the first field in the block is the magic number. If it is not,3422return with PCRE2_ERROR_BADMAGIC. */34233424if (re->magic_number != MAGIC_NUMBER)3425{ rc = PCRE2_ERROR_BADMAGIC; goto EXIT; }34263427/* Check the code unit width. */34283429if ((re->flags & PCRE2_MODE_MASK) != PCRE2_CODE_UNIT_WIDTH/8)3430{ rc = PCRE2_ERROR_BADMODE; goto EXIT; }34313432/* PCRE2_NOTEMPTY and PCRE2_NOTEMPTY_ATSTART are match-time flags in the3433options variable for this function. Users of PCRE2 who are not calling the3434function directly would like to have a way of setting these flags, in the same3435way that they can set pcre2_compile() flags like PCRE2_NO_AUTO_POSSESS with3436constructions like (*NO_AUTOPOSSESS). To enable this, (*NOTEMPTY) and3437(*NOTEMPTY_ATSTART) set bits in the pattern's "flag" function which can now be3438transferred to the options for this function. The bits are guaranteed to be3439adjacent, but do not have the same values. This bit of Boolean trickery assumes3440that the match-time bits are not more significant than the flag bits. If by3441accident this is not the case, a compile-time division by zero error will3442occur. */34433444#define FF (PCRE2_NOTEMPTY_SET|PCRE2_NE_ATST_SET)3445#define OO (PCRE2_NOTEMPTY|PCRE2_NOTEMPTY_ATSTART)3446options |= (re->flags & FF) / ((FF & (~FF+1)) / (OO & (~OO+1)));3447#undef FF3448#undef OO34493450/* If restarting after a partial match, do some sanity checks on the contents3451of the workspace. */34523453if ((options & PCRE2_DFA_RESTART) != 0)3454{3455if ((workspace[0] & (-2)) != 0 || workspace[1] < 1 ||3456workspace[1] > (int)((wscount - 2)/INTS_PER_STATEBLOCK))3457{ rc = PCRE2_ERROR_DFA_BADRESTART; goto EXIT; }3458}34593460/* Set some local values */34613462utf = (re->overall_options & PCRE2_UTF) != 0;3463start_match = subject + start_offset;3464end_subject = subject + length;3465req_cu_ptr = start_match - 1;3466anchored = (options & (PCRE2_ANCHORED|PCRE2_DFA_RESTART)) != 0 ||3467(re->overall_options & PCRE2_ANCHORED) != 0;34683469/* The "must be at the start of a line" flags are used in a loop when finding3470where to start. */34713472startline = (re->flags & PCRE2_STARTLINE) != 0;3473firstline = !anchored && (re->overall_options & PCRE2_FIRSTLINE) != 0;3474bumpalong_limit = end_subject;34753476/* Initialize and set up the fixed fields in the callout block, with a pointer3477in the match block. */34783479mb->cb = &cb;3480cb.version = 2;3481cb.subject = subject;3482cb.subject_length = (PCRE2_SIZE)(end_subject - subject);3483cb.callout_flags = 0;3484cb.capture_top = 1; /* No capture support */3485cb.capture_last = 0;3486cb.mark = NULL; /* No (*MARK) support */34873488/* Get data from the match context, if present, and fill in the remaining3489fields in the match block. It is an error to set an offset limit without3490setting the flag at compile time. */34913492if (mcontext == NULL)3493{3494mb->callout = NULL;3495mb->memctl = re->memctl;3496mb->match_limit = PRIV(default_match_context).match_limit;3497mb->match_limit_depth = PRIV(default_match_context).depth_limit;3498mb->heap_limit = PRIV(default_match_context).heap_limit;3499}3500else3501{3502if (mcontext->offset_limit != PCRE2_UNSET)3503{3504if ((re->overall_options & PCRE2_USE_OFFSET_LIMIT) == 0)3505{ rc = PCRE2_ERROR_BADOFFSETLIMIT; goto EXIT; }3506bumpalong_limit = subject + mcontext->offset_limit;3507}3508mb->callout = mcontext->callout;3509mb->callout_data = mcontext->callout_data;3510mb->memctl = mcontext->memctl;3511mb->match_limit = mcontext->match_limit;3512mb->match_limit_depth = mcontext->depth_limit;3513mb->heap_limit = mcontext->heap_limit;3514}35153516if (mb->match_limit > re->limit_match)3517mb->match_limit = re->limit_match;35183519if (mb->match_limit_depth > re->limit_depth)3520mb->match_limit_depth = re->limit_depth;35213522if (mb->heap_limit > re->limit_heap)3523mb->heap_limit = re->limit_heap;35243525mb->start_code = (PCRE2_SPTR)((const uint8_t *)re + re->code_start);3526mb->tables = re->tables;3527mb->start_subject = subject;3528mb->end_subject = end_subject;3529mb->start_offset = start_offset;3530mb->allowemptypartial = (re->max_lookbehind > 0) ||3531(re->flags & PCRE2_MATCH_EMPTY) != 0;3532mb->moptions = options;3533mb->poptions = re->overall_options;3534mb->match_call_count = 0;3535mb->heap_used = 0;35363537/* Process the \R and newline settings. */35383539mb->bsr_convention = re->bsr_convention;3540mb->nltype = NLTYPE_FIXED;3541switch(re->newline_convention)3542{3543case PCRE2_NEWLINE_CR:3544mb->nllen = 1;3545mb->nl[0] = CHAR_CR;3546break;35473548case PCRE2_NEWLINE_LF:3549mb->nllen = 1;3550mb->nl[0] = CHAR_NL;3551break;35523553case PCRE2_NEWLINE_NUL:3554mb->nllen = 1;3555mb->nl[0] = CHAR_NUL;3556break;35573558case PCRE2_NEWLINE_CRLF:3559mb->nllen = 2;3560mb->nl[0] = CHAR_CR;3561mb->nl[1] = CHAR_NL;3562break;35633564case PCRE2_NEWLINE_ANY:3565mb->nltype = NLTYPE_ANY;3566break;35673568case PCRE2_NEWLINE_ANYCRLF:3569mb->nltype = NLTYPE_ANYCRLF;3570break;35713572/* LCOV_EXCL_START */3573default:3574PCRE2_DEBUG_UNREACHABLE();3575rc = PCRE2_ERROR_INTERNAL;3576goto EXIT;3577/* LCOV_EXCL_STOP */3578}35793580/* Check a UTF string for validity if required. For 8-bit and 16-bit strings,3581we must also check that a starting offset does not point into the middle of a3582multiunit character. We check only the portion of the subject that is going to3583be inspected during matching - from the offset minus the maximum back reference3584to the given length. This saves time when a small part of a large subject is3585being matched by the use of a starting offset. Note that the maximum lookbehind3586is a number of characters, not code units. */35873588#ifdef SUPPORT_UNICODE3589if (utf && (options & PCRE2_NO_UTF_CHECK) == 0)3590{3591PCRE2_SPTR check_subject = start_match; /* start_match includes offset */35923593if (start_offset > 0)3594{3595#if PCRE2_CODE_UNIT_WIDTH != 323596unsigned int i;3597if (start_match < end_subject && NOT_FIRSTCU(*start_match))3598{ rc = PCRE2_ERROR_BADUTFOFFSET; goto EXIT; }3599for (i = re->max_lookbehind; i > 0 && check_subject > subject; i--)3600{3601check_subject--;3602while (check_subject > subject &&3603#if PCRE2_CODE_UNIT_WIDTH == 83604(*check_subject & 0xc0) == 0x80)3605#else /* 16-bit */3606(*check_subject & 0xfc00) == 0xdc00)3607#endif /* PCRE2_CODE_UNIT_WIDTH == 8 */3608check_subject--;3609}3610#else /* In the 32-bit library, one code unit equals one character. */3611check_subject -= re->max_lookbehind;3612if (check_subject < subject) check_subject = subject;3613#endif /* PCRE2_CODE_UNIT_WIDTH != 32 */3614}36153616/* Validate the relevant portion of the subject. After an error, adjust the3617offset to be an absolute offset in the whole string. */36183619rc = PRIV(valid_utf)(check_subject,3620length - (PCRE2_SIZE)(check_subject - subject), &(match_data->startchar));3621if (rc != 0)3622{3623match_data->startchar += (PCRE2_SIZE)(check_subject - subject);3624goto EXIT;3625}3626}3627#endif /* SUPPORT_UNICODE */36283629/* Set up the first code unit to match, if available. If there's no first code3630unit there may be a bitmap of possible first characters. */36313632if ((re->flags & PCRE2_FIRSTSET) != 0)3633{3634has_first_cu = TRUE;3635first_cu = first_cu2 = (PCRE2_UCHAR)(re->first_codeunit);3636if ((re->flags & PCRE2_FIRSTCASELESS) != 0)3637{3638first_cu2 = TABLE_GET(first_cu, mb->tables + fcc_offset, first_cu);3639#ifdef SUPPORT_UNICODE3640#if PCRE2_CODE_UNIT_WIDTH == 83641if (first_cu > 127 && !utf && (re->overall_options & PCRE2_UCP) != 0)3642first_cu2 = (PCRE2_UCHAR)UCD_OTHERCASE(first_cu);3643#else3644if (first_cu > 127 && (utf || (re->overall_options & PCRE2_UCP) != 0))3645first_cu2 = (PCRE2_UCHAR)UCD_OTHERCASE(first_cu);3646#endif3647#endif /* SUPPORT_UNICODE */3648}3649}3650else3651if (!startline && (re->flags & PCRE2_FIRSTMAPSET) != 0)3652start_bits = re->start_bitmap;36533654/* There may be a "last known required code unit" set. */36553656if ((re->flags & PCRE2_LASTSET) != 0)3657{3658has_req_cu = TRUE;3659req_cu = req_cu2 = (PCRE2_UCHAR)(re->last_codeunit);3660if ((re->flags & PCRE2_LASTCASELESS) != 0)3661{3662req_cu2 = TABLE_GET(req_cu, mb->tables + fcc_offset, req_cu);3663#ifdef SUPPORT_UNICODE3664#if PCRE2_CODE_UNIT_WIDTH == 83665if (req_cu > 127 && !utf && (re->overall_options & PCRE2_UCP) != 0)3666req_cu2 = (PCRE2_UCHAR)UCD_OTHERCASE(req_cu);3667#else3668if (req_cu > 127 && (utf || (re->overall_options & PCRE2_UCP) != 0))3669req_cu2 = (PCRE2_UCHAR)UCD_OTHERCASE(req_cu);3670#endif3671#endif /* SUPPORT_UNICODE */3672}3673}36743675/* If the match data block was previously used with PCRE2_COPY_MATCHED_SUBJECT,3676free the memory that was obtained. */36773678if ((match_data->flags & PCRE2_MD_COPIED_SUBJECT) != 0)3679{3680match_data->memctl.free((void *)match_data->subject,3681match_data->memctl.memory_data);3682match_data->flags &= ~PCRE2_MD_COPIED_SUBJECT;3683}36843685/* Fill in fields that are always returned in the match data. */36863687match_data->code = re;3688match_data->subject = NULL; /* Default for match error */3689match_data->mark = NULL;3690match_data->matchedby = PCRE2_MATCHEDBY_DFA_INTERPRETER;3691match_data->options = original_options;36923693/* Call the main matching function, looping for a non-anchored regex after a3694failed match. If not restarting, perform certain optimizations at the start of3695a match. */36963697for (;;)3698{3699/* ----------------- Start of match optimizations ---------------- */37003701/* There are some optimizations that avoid running the match if a known3702starting point is not found, or if a known later code unit is not present.3703However, there is an option (settable at compile time) that disables3704these, for testing and for ensuring that all callouts do actually occur.3705The optimizations must also be avoided when restarting a DFA match. */37063707if ((re->optimization_flags & PCRE2_OPTIM_START_OPTIMIZE) != 0 &&3708(options & PCRE2_DFA_RESTART) == 0)3709{3710/* If firstline is TRUE, the start of the match is constrained to the first3711line of a multiline string. That is, the match must be before or at the3712first newline following the start of matching. Temporarily adjust3713end_subject so that we stop the optimization scans for a first code unit3714immediately after the first character of a newline (the first code unit can3715legitimately be a newline). If the match fails at the newline, later code3716breaks this loop. */37173718if (firstline)3719{3720PCRE2_SPTR t = start_match;3721#ifdef SUPPORT_UNICODE3722if (utf)3723{3724while (t < end_subject && !IS_NEWLINE(t))3725{3726t++;3727ACROSSCHAR(t < end_subject, t, t++);3728}3729}3730else3731#endif3732while (t < end_subject && !IS_NEWLINE(t)) t++;3733end_subject = t;3734}37353736/* Anchored: check the first code unit if one is recorded. This may seem3737pointless but it can help in detecting a no match case without scanning for3738the required code unit. */37393740if (anchored)3741{3742if (has_first_cu || start_bits != NULL)3743{3744BOOL ok = start_match < end_subject;3745if (ok)3746{3747PCRE2_UCHAR c = UCHAR21TEST(start_match);3748ok = has_first_cu && (c == first_cu || c == first_cu2);3749if (!ok && start_bits != NULL)3750{3751#if PCRE2_CODE_UNIT_WIDTH != 83752if (c > 255) c = 255;3753#endif3754ok = (start_bits[c/8] & (1u << (c&7))) != 0;3755}3756}3757if (!ok) break;3758}3759}37603761/* Not anchored. Advance to a unique first code unit if there is one. */37623763else3764{3765if (has_first_cu)3766{3767if (first_cu != first_cu2) /* Caseless */3768{3769/* In 16-bit and 32_bit modes we have to do our own search, so can3770look for both cases at once. */37713772#if PCRE2_CODE_UNIT_WIDTH != 83773PCRE2_UCHAR smc;3774while (start_match < end_subject &&3775(smc = UCHAR21TEST(start_match)) != first_cu &&3776smc != first_cu2)3777start_match++;3778#else3779/* In 8-bit mode, the use of memchr() gives a big speed up, even3780though we have to call it twice in order to find the earliest3781occurrence of the code unit in either of its cases. Caching is used3782to remember the positions of previously found code units. This can3783make a huge difference when the strings are very long and only one3784case is actually present. */37853786PCRE2_SPTR pp1 = NULL;3787PCRE2_SPTR pp2 = NULL;3788PCRE2_SIZE searchlength = end_subject - start_match;37893790/* If we haven't got a previously found position for first_cu, or if3791the current starting position is later, we need to do a search. If3792the code unit is not found, set it to the end. */37933794if (memchr_found_first_cu == NULL ||3795start_match > memchr_found_first_cu)3796{3797pp1 = memchr(start_match, first_cu, searchlength);3798memchr_found_first_cu = (pp1 == NULL)? end_subject : pp1;3799}38003801/* If the start is before a previously found position, use the3802previous position, or NULL if a previous search failed. */38033804else pp1 = (memchr_found_first_cu == end_subject)? NULL :3805memchr_found_first_cu;38063807/* Do the same thing for the other case. */38083809if (memchr_found_first_cu2 == NULL ||3810start_match > memchr_found_first_cu2)3811{3812pp2 = memchr(start_match, first_cu2, searchlength);3813memchr_found_first_cu2 = (pp2 == NULL)? end_subject : pp2;3814}38153816else pp2 = (memchr_found_first_cu2 == end_subject)? NULL :3817memchr_found_first_cu2;38183819/* Set the start to the end of the subject if neither case was found.3820Otherwise, use the earlier found point. */38213822if (pp1 == NULL)3823start_match = (pp2 == NULL)? end_subject : pp2;3824else3825start_match = (pp2 == NULL || pp1 < pp2)? pp1 : pp2;38263827#endif /* 8-bit handling */3828}38293830/* The caseful case is much simpler. */38313832else3833{3834#if PCRE2_CODE_UNIT_WIDTH != 83835while (start_match < end_subject && UCHAR21TEST(start_match) !=3836first_cu)3837start_match++;3838#else /* 8-bit code units */3839start_match = memchr(start_match, first_cu, end_subject - start_match);3840if (start_match == NULL) start_match = end_subject;3841#endif3842}38433844/* If we can't find the required code unit, having reached the true end3845of the subject, break the bumpalong loop, to force a match failure,3846except when doing partial matching, when we let the next cycle run at3847the end of the subject. To see why, consider the pattern /(?<=abc)def/,3848which partially matches "abc", even though the string does not contain3849the starting character "d". If we have not reached the true end of the3850subject (PCRE2_FIRSTLINE caused end_subject to be temporarily modified)3851we also let the cycle run, because the matching string is legitimately3852allowed to start with the first code unit of a newline. */38533854if ((mb->moptions & (PCRE2_PARTIAL_HARD|PCRE2_PARTIAL_SOFT)) == 0 &&3855start_match >= mb->end_subject)3856break;3857}38583859/* If there's no first code unit, advance to just after a linebreak for a3860multiline match if required. */38613862else if (startline)3863{3864if (start_match > mb->start_subject + start_offset)3865{3866#ifdef SUPPORT_UNICODE3867if (utf)3868{3869while (start_match < end_subject && !WAS_NEWLINE(start_match))3870{3871start_match++;3872ACROSSCHAR(start_match < end_subject, start_match, start_match++);3873}3874}3875else3876#endif3877while (start_match < end_subject && !WAS_NEWLINE(start_match))3878start_match++;38793880/* If we have just passed a CR and the newline option is ANY or3881ANYCRLF, and we are now at a LF, advance the match position by one3882more code unit. */38833884if (start_match[-1] == CHAR_CR &&3885(mb->nltype == NLTYPE_ANY || mb->nltype == NLTYPE_ANYCRLF) &&3886start_match < end_subject &&3887UCHAR21TEST(start_match) == CHAR_NL)3888start_match++;3889}3890}38913892/* If there's no first code unit or a requirement for a multiline line3893start, advance to a non-unique first code unit if any have been3894identified. The bitmap contains only 256 bits. When code units are 16 or389532 bits wide, all code units greater than 254 set the 255 bit. */38963897else if (start_bits != NULL)3898{3899while (start_match < end_subject)3900{3901uint32_t c = UCHAR21TEST(start_match);3902#if PCRE2_CODE_UNIT_WIDTH != 83903if (c > 255) c = 255;3904#endif3905if ((start_bits[c/8] & (1u << (c&7))) != 0) break;3906start_match++;3907}39083909/* See comment above in first_cu checking about the next line. */39103911if ((mb->moptions & (PCRE2_PARTIAL_HARD|PCRE2_PARTIAL_SOFT)) == 0 &&3912start_match >= mb->end_subject)3913break;3914}3915} /* End of first code unit handling */39163917/* Restore fudged end_subject */39183919end_subject = mb->end_subject;39203921/* The following two optimizations are disabled for partial matching. */39223923if ((mb->moptions & (PCRE2_PARTIAL_HARD|PCRE2_PARTIAL_SOFT)) == 0)3924{3925PCRE2_SPTR p;39263927/* The minimum matching length is a lower bound; no actual string of that3928length may actually match the pattern. Although the value is, strictly,3929in characters, we treat it as code units to avoid spending too much time3930in this optimization. */39313932if (end_subject - start_match < re->minlength) goto NOMATCH_EXIT;39333934/* If req_cu is set, we know that that code unit must appear in the3935subject for the match to succeed. If the first code unit is set, req_cu3936must be later in the subject; otherwise the test starts at the match3937point. This optimization can save a huge amount of backtracking in3938patterns with nested unlimited repeats that aren't going to match.3939Writing separate code for cased/caseless versions makes it go faster, as3940does using an autoincrement and backing off on a match. As in the case of3941the first code unit, using memchr() in the 8-bit library gives a big3942speed up. Unlike the first_cu check above, we do not need to call3943memchr() twice in the caseless case because we only need to check for the3944presence of the character in either case, not find the first occurrence.39453946The search can be skipped if the code unit was found later than the3947current starting point in a previous iteration of the bumpalong loop.39483949HOWEVER: when the subject string is very, very long, searching to its end3950can take a long time, and give bad performance on quite ordinary3951patterns. This showed up when somebody was matching something like3952/^\d+C/ on a 32-megabyte string... so we don't do this when the string is3953sufficiently long, but it's worth searching a lot more for unanchored3954patterns. */39553956p = start_match + (has_first_cu? 1:0);3957if (has_req_cu && p > req_cu_ptr)3958{3959PCRE2_SIZE check_length = end_subject - start_match;39603961if (check_length < REQ_CU_MAX ||3962(!anchored && check_length < REQ_CU_MAX * 1000))3963{3964if (req_cu != req_cu2) /* Caseless */3965{3966#if PCRE2_CODE_UNIT_WIDTH != 83967while (p < end_subject)3968{3969uint32_t pp = UCHAR21INCTEST(p);3970if (pp == req_cu || pp == req_cu2) { p--; break; }3971}3972#else /* 8-bit code units */3973PCRE2_SPTR pp = p;3974p = memchr(pp, req_cu, end_subject - pp);3975if (p == NULL)3976{3977p = memchr(pp, req_cu2, end_subject - pp);3978if (p == NULL) p = end_subject;3979}3980#endif /* PCRE2_CODE_UNIT_WIDTH != 8 */3981}39823983/* The caseful case */39843985else3986{3987#if PCRE2_CODE_UNIT_WIDTH != 83988while (p < end_subject)3989{3990if (UCHAR21INCTEST(p) == req_cu) { p--; break; }3991}39923993#else /* 8-bit code units */3994p = memchr(p, req_cu, end_subject - p);3995if (p == NULL) p = end_subject;3996#endif3997}39983999/* If we can't find the required code unit, break the matching loop,4000forcing a match failure. */40014002if (p >= end_subject) break;40034004/* If we have found the required code unit, save the point where we4005found it, so that we don't search again next time round the loop if4006the start hasn't passed this code unit yet. */40074008req_cu_ptr = p;4009}4010}4011}4012}40134014/* ------------ End of start of match optimizations ------------ */40154016/* Give no match if we have passed the bumpalong limit. */40174018if (start_match > bumpalong_limit) break;40194020/* OK, now we can do the business */40214022mb->start_used_ptr = start_match;4023mb->last_used_ptr = start_match;4024mb->recursive = NULL;40254026rc = internal_dfa_match(4027mb, /* fixed match data */4028mb->start_code, /* this subexpression's code */4029start_match, /* where we currently are */4030start_offset, /* start offset in subject */4031match_data->ovector, /* offset vector */4032(uint32_t)match_data->oveccount * 2, /* actual size of same */4033workspace, /* workspace vector */4034(int)wscount, /* size of same */40350, /* function recurse level */4036base_recursion_workspace); /* initial workspace for recursion */40374038/* Anything other than "no match" means we are done, always; otherwise, carry4039on only if not anchored. */40404041if (rc != PCRE2_ERROR_NOMATCH || anchored)4042{4043if (rc == PCRE2_ERROR_NOMATCH) goto NOMATCH_EXIT;40444045if (rc == PCRE2_ERROR_PARTIAL && match_data->oveccount > 0)4046{4047match_data->ovector[0] = (PCRE2_SIZE)(start_match - subject);4048match_data->ovector[1] = (PCRE2_SIZE)(end_subject - subject);4049}40504051if (rc >= 0 || rc == PCRE2_ERROR_PARTIAL)4052{4053match_data->subject_length = length;4054match_data->start_offset = start_offset;4055match_data->leftchar = (PCRE2_SIZE)(mb->start_used_ptr - subject);4056match_data->rightchar = (PCRE2_SIZE)(mb->last_used_ptr - subject);4057match_data->startchar = (PCRE2_SIZE)(start_match - subject);4058}40594060if (rc >= 0 && (options & PCRE2_COPY_MATCHED_SUBJECT) != 0)4061{4062if (length != 0)4063{4064match_data->subject = match_data->memctl.malloc(CU2BYTES(length),4065match_data->memctl.memory_data);4066if (match_data->subject == NULL)4067{ rc = PCRE2_ERROR_NOMEMORY; goto EXIT; }4068memcpy((void *)match_data->subject, subject, CU2BYTES(length));4069}4070else4071match_data->subject = NULL;4072match_data->flags |= PCRE2_MD_COPIED_SUBJECT;4073}4074else if (rc >= 0 || rc == PCRE2_ERROR_PARTIAL)4075{4076match_data->subject = original_subject;4077}4078goto EXIT;4079}40804081/* Advance to the next subject character unless we are at the end of a line4082and firstline is set. */40834084if (firstline && IS_NEWLINE(start_match)) break;4085start_match++;4086#ifdef SUPPORT_UNICODE4087if (utf)4088{4089ACROSSCHAR(start_match < end_subject, start_match, start_match++);4090}4091#endif4092if (start_match > end_subject) break;40934094/* If we have just passed a CR and we are now at a LF, and the pattern does4095not contain any explicit matches for \r or \n, and the newline option is CRLF4096or ANY or ANYCRLF, advance the match position by one more character. */40974098if (UCHAR21TEST(start_match - 1) == CHAR_CR &&4099start_match < end_subject &&4100UCHAR21TEST(start_match) == CHAR_NL &&4101(re->flags & PCRE2_HASCRORLF) == 0 &&4102(mb->nltype == NLTYPE_ANY ||4103mb->nltype == NLTYPE_ANYCRLF ||4104mb->nllen == 2))4105start_match++;41064107} /* "Bumpalong" loop */41084109NOMATCH_EXIT:4110match_data->subject = original_subject;4111match_data->subject_length = length;4112match_data->start_offset = start_offset;4113rc = PCRE2_ERROR_NOMATCH;41144115EXIT:4116while (rws->next != NULL)4117{4118RWS_anchor *next = rws->next;4119rws->next = next->next;4120mb->memctl.free(next, mb->memctl.memory_data);4121}41224123match_data->rc = rc;4124return rc;4125}41264127/* These #undefs are here to enable unity builds with CMake. */41284129#undef NLBLOCK /* Block containing newline information */4130#undef PSSTART /* Field containing processed string start */4131#undef PSEND /* Field containing processed string end */41324133/* End of pcre2_dfa_match.c */413441354136