Path: blob/master/thirdparty/pcre2/src/pcre2_xclass.c
9898 views
/*************************************************1* Perl-Compatible Regular Expressions *2*************************************************/34/* PCRE is a library of functions to support regular expressions whose syntax5and semantics are as close as possible to those of the Perl 5 language.67Written by Philip Hazel8Original API code Copyright (c) 1997-2012 University of Cambridge9New API code Copyright (c) 2016-2024 University of Cambridge1011-----------------------------------------------------------------------------12Redistribution and use in source and binary forms, with or without13modification, are permitted provided that the following conditions are met:1415* Redistributions of source code must retain the above copyright notice,16this list of conditions and the following disclaimer.1718* Redistributions in binary form must reproduce the above copyright19notice, this list of conditions and the following disclaimer in the20documentation and/or other materials provided with the distribution.2122* Neither the name of the University of Cambridge nor the names of its23contributors may be used to endorse or promote products derived from24this software without specific prior written permission.2526THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"27AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE28IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE29ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE30LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR31CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF32SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS33INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN34CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)35ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE36POSSIBILITY OF SUCH DAMAGE.37-----------------------------------------------------------------------------38*/3940/* This module contains two internal functions that are used to match41OP_XCLASS and OP_ECLASS. It is used by pcre2_auto_possessify() and by both42pcre2_match() and pcre2_dfa_match(). */434445#ifdef HAVE_CONFIG_H46#include "config.h"47#endif484950#include "pcre2_internal.h"5152/*************************************************53* Match character against an XCLASS *54*************************************************/5556/* This function is called to match a character against an extended class that57might contain codepoints above 255 and/or Unicode properties.5859Arguments:60c the character61data points to the flag code unit of the XCLASS data62utf TRUE if in UTF mode6364Returns: TRUE if character matches, else FALSE65*/6667BOOL68PRIV(xclass)(uint32_t c, PCRE2_SPTR data, const uint8_t *char_lists_end, BOOL utf)69{70/* Update PRIV(update_classbits) when this function is changed. */71PCRE2_UCHAR t;72BOOL not_negated = (*data & XCL_NOT) == 0;73uint32_t type, max_index, min_index, value;74const uint8_t *next_char;7576#if PCRE2_CODE_UNIT_WIDTH == 877/* In 8 bit mode, this must always be TRUE. Help the compiler to know that. */78utf = TRUE;79#endif8081/* Code points < 256 are matched against a bitmap, if one is present. */8283if ((*data++ & XCL_MAP) != 0)84{85if (c < 256)86return (((const uint8_t *)data)[c/8] & (1u << (c&7))) != 0;87/* Skip bitmap. */88data += 32 / sizeof(PCRE2_UCHAR);89}9091/* Match against the list of Unicode properties. We won't ever92encounter XCL_PROP or XCL_NOTPROP when UTF support is not compiled. */93#ifdef SUPPORT_UNICODE94if (*data == XCL_PROP || *data == XCL_NOTPROP)95{96/* The UCD record is the same for all properties. */97const ucd_record *prop = GET_UCD(c);9899do100{101int chartype;102BOOL isprop = (*data++) == XCL_PROP;103BOOL ok;104105switch(*data)106{107case PT_LAMP:108chartype = prop->chartype;109if ((chartype == ucp_Lu || chartype == ucp_Ll ||110chartype == ucp_Lt) == isprop) return not_negated;111break;112113case PT_GC:114if ((data[1] == PRIV(ucp_gentype)[prop->chartype]) == isprop)115return not_negated;116break;117118case PT_PC:119if ((data[1] == prop->chartype) == isprop) return not_negated;120break;121122case PT_SC:123if ((data[1] == prop->script) == isprop) return not_negated;124break;125126case PT_SCX:127ok = (data[1] == prop->script ||128MAPBIT(PRIV(ucd_script_sets) + UCD_SCRIPTX_PROP(prop), data[1]) != 0);129if (ok == isprop) return not_negated;130break;131132case PT_ALNUM:133chartype = prop->chartype;134if ((PRIV(ucp_gentype)[chartype] == ucp_L ||135PRIV(ucp_gentype)[chartype] == ucp_N) == isprop)136return not_negated;137break;138139/* Perl space used to exclude VT, but from Perl 5.18 it is included,140which means that Perl space and POSIX space are now identical. PCRE141was changed at release 8.34. */142143case PT_SPACE: /* Perl space */144case PT_PXSPACE: /* POSIX space */145switch(c)146{147HSPACE_CASES:148VSPACE_CASES:149if (isprop) return not_negated;150break;151152default:153if ((PRIV(ucp_gentype)[prop->chartype] == ucp_Z) == isprop)154return not_negated;155break;156}157break;158159case PT_WORD:160chartype = prop->chartype;161if ((PRIV(ucp_gentype)[chartype] == ucp_L ||162PRIV(ucp_gentype)[chartype] == ucp_N ||163chartype == ucp_Mn || chartype == ucp_Pc) == isprop)164return not_negated;165break;166167case PT_UCNC:168if (c < 0xa0)169{170if ((c == CHAR_DOLLAR_SIGN || c == CHAR_COMMERCIAL_AT ||171c == CHAR_GRAVE_ACCENT) == isprop)172return not_negated;173}174else175{176if ((c < 0xd800 || c > 0xdfff) == isprop)177return not_negated;178}179break;180181case PT_BIDICL:182if ((UCD_BIDICLASS_PROP(prop) == data[1]) == isprop)183return not_negated;184break;185186case PT_BOOL:187ok = MAPBIT(PRIV(ucd_boolprop_sets) +188UCD_BPROPS_PROP(prop), data[1]) != 0;189if (ok == isprop) return not_negated;190break;191192/* The following three properties can occur only in an XCLASS, as there193is no \p or \P coding for them. */194195/* Graphic character. Implement this as not Z (space or separator) and196not C (other), except for Cf (format) with a few exceptions. This seems197to be what Perl does. The exceptional characters are:198199U+061C Arabic Letter Mark200U+180E Mongolian Vowel Separator201U+2066 - U+2069 Various "isolate"s202*/203204case PT_PXGRAPH:205chartype = prop->chartype;206if ((PRIV(ucp_gentype)[chartype] != ucp_Z &&207(PRIV(ucp_gentype)[chartype] != ucp_C ||208(chartype == ucp_Cf &&209c != 0x061c && c != 0x180e && (c < 0x2066 || c > 0x2069))210)) == isprop)211return not_negated;212break;213214/* Printable character: same as graphic, with the addition of Zs, i.e.215not Zl and not Zp, and U+180E. */216217case PT_PXPRINT:218chartype = prop->chartype;219if ((chartype != ucp_Zl &&220chartype != ucp_Zp &&221(PRIV(ucp_gentype)[chartype] != ucp_C ||222(chartype == ucp_Cf &&223c != 0x061c && (c < 0x2066 || c > 0x2069))224)) == isprop)225return not_negated;226break;227228/* Punctuation: all Unicode punctuation, plus ASCII characters that229Unicode treats as symbols rather than punctuation, for Perl230compatibility (these are $+<=>^`|~). */231232case PT_PXPUNCT:233chartype = prop->chartype;234if ((PRIV(ucp_gentype)[chartype] == ucp_P ||235(c < 128 && PRIV(ucp_gentype)[chartype] == ucp_S)) == isprop)236return not_negated;237break;238239/* Perl has two sets of hex digits */240241case PT_PXXDIGIT:242if (((c >= CHAR_0 && c <= CHAR_9) ||243(c >= CHAR_A && c <= CHAR_F) ||244(c >= CHAR_a && c <= CHAR_f) ||245(c >= 0xff10 && c <= 0xff19) || /* Fullwidth digits */246(c >= 0xff21 && c <= 0xff26) || /* Fullwidth letters */247(c >= 0xff41 && c <= 0xff46)) == isprop)248return not_negated;249break;250251/* This should never occur, but compilers may mutter if there is no252default. */253254default:255PCRE2_DEBUG_UNREACHABLE();256return FALSE;257}258259data += 2;260}261while (*data == XCL_PROP || *data == XCL_NOTPROP);262}263#else264(void)utf; /* Avoid compiler warning */265#endif /* SUPPORT_UNICODE */266267/* Match against large chars or ranges that end with a large char. */268if (*data < XCL_LIST)269{270while ((t = *data++) != XCL_END)271{272uint32_t x, y;273274#ifdef SUPPORT_UNICODE275if (utf)276{277GETCHARINC(x, data); /* macro generates multiple statements */278}279else280#endif281x = *data++;282283if (t == XCL_SINGLE)284{285/* Since character ranges follow the properties, and they are286sorted, early return is possible for all characters <= x. */287if (c <= x) return (c == x) ? not_negated : !not_negated;288continue;289}290291PCRE2_ASSERT(t == XCL_RANGE);292#ifdef SUPPORT_UNICODE293if (utf)294{295GETCHARINC(y, data); /* macro generates multiple statements */296}297else298#endif299y = *data++;300301/* Since character ranges follow the properties, and they are302sorted, early return is possible for all characters <= y. */303if (c <= y) return (c >= x) ? not_negated : !not_negated;304}305306return !not_negated; /* char did not match */307}308309#if PCRE2_CODE_UNIT_WIDTH == 8310type = (uint32_t)(data[0] << 8) | data[1];311data += 2;312#else313type = data[0];314data++;315#endif /* CODE_UNIT_WIDTH */316317/* Align characters. */318next_char = char_lists_end - (GET(data, 0) << 1);319type &= XCL_TYPE_MASK;320321/* Alignment check. */322PCRE2_ASSERT(((uintptr_t)next_char & 0x1) == 0);323324if (c >= XCL_CHAR_LIST_HIGH_16_START)325{326max_index = type & XCL_ITEM_COUNT_MASK;327if (max_index == XCL_ITEM_COUNT_MASK)328{329max_index = *(const uint16_t*)next_char;330PCRE2_ASSERT(max_index >= XCL_ITEM_COUNT_MASK);331next_char += 2;332}333334next_char += max_index << 1;335type >>= XCL_TYPE_BIT_LEN;336}337338if (c < XCL_CHAR_LIST_LOW_32_START)339{340max_index = type & XCL_ITEM_COUNT_MASK;341342c = (uint16_t)((c << XCL_CHAR_SHIFT) | XCL_CHAR_END);343344if (max_index == XCL_ITEM_COUNT_MASK)345{346max_index = *(const uint16_t*)next_char;347PCRE2_ASSERT(max_index >= XCL_ITEM_COUNT_MASK);348next_char += 2;349}350351if (max_index == 0 || c < *(const uint16_t*)next_char)352return ((type & XCL_BEGIN_WITH_RANGE) != 0) == not_negated;353354min_index = 0;355value = ((const uint16_t*)next_char)[--max_index];356if (c >= value)357return (value == c || (value & XCL_CHAR_END) == 0) == not_negated;358359max_index--;360361/* Binary search of a range. */362while (TRUE)363{364uint32_t mid_index = (min_index + max_index) >> 1;365value = ((const uint16_t*)next_char)[mid_index];366367if (c < value)368max_index = mid_index - 1;369else if (((const uint16_t*)next_char)[mid_index + 1] <= c)370min_index = mid_index + 1;371else372return (value == c || (value & XCL_CHAR_END) == 0) == not_negated;373}374}375376/* Skip the 16 bit ranges. */377max_index = type & XCL_ITEM_COUNT_MASK;378if (max_index == XCL_ITEM_COUNT_MASK)379{380max_index = *(const uint16_t*)next_char;381PCRE2_ASSERT(max_index >= XCL_ITEM_COUNT_MASK);382next_char += 2;383}384385next_char += (max_index << 1);386type >>= XCL_TYPE_BIT_LEN;387388/* Alignment check. */389PCRE2_ASSERT(((uintptr_t)next_char & 0x3) == 0);390391max_index = type & XCL_ITEM_COUNT_MASK;392393#if PCRE2_CODE_UNIT_WIDTH == 32394if (c >= XCL_CHAR_LIST_HIGH_32_START)395{396if (max_index == XCL_ITEM_COUNT_MASK)397{398max_index = *(const uint32_t*)next_char;399PCRE2_ASSERT(max_index >= XCL_ITEM_COUNT_MASK);400next_char += 4;401}402403next_char += max_index << 2;404type >>= XCL_TYPE_BIT_LEN;405max_index = type & XCL_ITEM_COUNT_MASK;406}407#endif408409c = (uint32_t)((c << XCL_CHAR_SHIFT) | XCL_CHAR_END);410411if (max_index == XCL_ITEM_COUNT_MASK)412{413max_index = *(const uint32_t*)next_char;414next_char += 4;415}416417if (max_index == 0 || c < *(const uint32_t*)next_char)418return ((type & XCL_BEGIN_WITH_RANGE) != 0) == not_negated;419420min_index = 0;421value = ((const uint32_t*)next_char)[--max_index];422if (c >= value)423return (value == c || (value & XCL_CHAR_END) == 0) == not_negated;424425max_index--;426427/* Binary search of a range. */428while (TRUE)429{430uint32_t mid_index = (min_index + max_index) >> 1;431value = ((const uint32_t*)next_char)[mid_index];432433if (c < value)434max_index = mid_index - 1;435else if (((const uint32_t*)next_char)[mid_index + 1] <= c)436min_index = mid_index + 1;437else438return (value == c || (value & XCL_CHAR_END) == 0) == not_negated;439}440}441442443444/*************************************************445* Match character against an ECLASS *446*************************************************/447448/* This function is called to match a character against an extended class449used for describing characters using boolean operations on sets.450451Arguments:452c the character453data_start points to the start of the ECLASS data454data_end points one-past-the-last of the ECLASS data455utf TRUE if in UTF mode456457Returns: TRUE if character matches, else FALSE458*/459460BOOL461PRIV(eclass)(uint32_t c, PCRE2_SPTR data_start, PCRE2_SPTR data_end,462const uint8_t *char_lists_end, BOOL utf)463{464PCRE2_SPTR ptr = data_start;465PCRE2_UCHAR flags;466uint32_t stack = 0;467int stack_depth = 0;468469PCRE2_ASSERT(data_start < data_end);470flags = *ptr++;471PCRE2_ASSERT((flags & ECL_MAP) == 0 ||472(data_end - ptr) >= 32 / (int)sizeof(PCRE2_UCHAR));473474/* Code points < 256 are matched against a bitmap, if one is present.475Otherwise all codepoints are checked later. */476477if ((flags & ECL_MAP) != 0)478{479if (c < 256)480return (((const uint8_t *)ptr)[c/8] & (1u << (c&7))) != 0;481482/* Skip the bitmap. */483ptr += 32 / sizeof(PCRE2_UCHAR);484}485486/* Do a little loop, until we reach the end of the ECLASS. */487while (ptr < data_end)488{489switch (*ptr)490{491case ECL_AND:492++ptr;493stack = (stack >> 1) & (stack | ~(uint32_t)1u);494PCRE2_ASSERT(stack_depth >= 2);495--stack_depth;496break;497498case ECL_OR:499++ptr;500stack = (stack >> 1) | (stack & (uint32_t)1u);501PCRE2_ASSERT(stack_depth >= 2);502--stack_depth;503break;504505case ECL_XOR:506++ptr;507stack = (stack >> 1) ^ (stack & (uint32_t)1u);508PCRE2_ASSERT(stack_depth >= 2);509--stack_depth;510break;511512case ECL_NOT:513++ptr;514stack ^= (uint32_t)1u;515PCRE2_ASSERT(stack_depth >= 1);516break;517518case ECL_XCLASS:519{520uint32_t matched = PRIV(xclass)(c, ptr + 1 + LINK_SIZE, char_lists_end, utf);521522ptr += GET(ptr, 1);523stack = (stack << 1) | matched;524++stack_depth;525break;526}527528/* This should never occur, but compilers may mutter if there is no529default. */530531default:532PCRE2_DEBUG_UNREACHABLE();533return FALSE;534}535}536537PCRE2_ASSERT(stack_depth == 1);538(void)stack_depth; /* Ignore unused variable, if assertions are disabled. */539540/* The final bit left on the stack now holds the match result. */541return (stack & 1u) != 0;542}543544/* End of pcre2_xclass.c */545546547