Blame src/pcre2_xclass.c

Packit 504f36
/*************************************************
Packit 504f36
*      Perl-Compatible Regular Expressions       *
Packit 504f36
*************************************************/
Packit 504f36
Packit 504f36
/* PCRE is a library of functions to support regular expressions whose syntax
Packit 504f36
and semantics are as close as possible to those of the Perl 5 language.
Packit 504f36
Packit 504f36
                       Written by Philip Hazel
Packit 504f36
     Original API code Copyright (c) 1997-2012 University of Cambridge
Packit 504f36
         New API code Copyright (c) 2016 University of Cambridge
Packit 504f36
Packit 504f36
-----------------------------------------------------------------------------
Packit 504f36
Redistribution and use in source and binary forms, with or without
Packit 504f36
modification, are permitted provided that the following conditions are met:
Packit 504f36
Packit 504f36
    * Redistributions of source code must retain the above copyright notice,
Packit 504f36
      this list of conditions and the following disclaimer.
Packit 504f36
Packit 504f36
    * Redistributions in binary form must reproduce the above copyright
Packit 504f36
      notice, this list of conditions and the following disclaimer in the
Packit 504f36
      documentation and/or other materials provided with the distribution.
Packit 504f36
Packit 504f36
    * Neither the name of the University of Cambridge nor the names of its
Packit 504f36
      contributors may be used to endorse or promote products derived from
Packit 504f36
      this software without specific prior written permission.
Packit 504f36
Packit 504f36
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
Packit 504f36
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Packit 504f36
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Packit 504f36
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
Packit 504f36
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
Packit 504f36
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
Packit 504f36
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
Packit 504f36
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
Packit 504f36
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
Packit 504f36
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
Packit 504f36
POSSIBILITY OF SUCH DAMAGE.
Packit 504f36
-----------------------------------------------------------------------------
Packit 504f36
*/
Packit 504f36
Packit 504f36
/* This module contains an internal function that is used to match an extended
Packit 504f36
class. It is used by pcre2_auto_possessify() and by both pcre2_match() and
Packit 504f36
pcre2_def_match(). */
Packit 504f36
Packit 504f36
Packit 504f36
#ifdef HAVE_CONFIG_H
Packit 504f36
#include "config.h"
Packit 504f36
#endif
Packit 504f36
Packit 504f36
Packit 504f36
#include "pcre2_internal.h"
Packit 504f36
Packit 504f36
/*************************************************
Packit 504f36
*       Match character against an XCLASS        *
Packit 504f36
*************************************************/
Packit 504f36
Packit 504f36
/* This function is called to match a character against an extended class that
Packit 504f36
might contain codepoints above 255 and/or Unicode properties.
Packit 504f36
Packit 504f36
Arguments:
Packit 504f36
  c           the character
Packit 504f36
  data        points to the flag code unit of the XCLASS data
Packit 504f36
  utf         TRUE if in UTF mode
Packit 504f36
Packit 504f36
Returns:      TRUE if character matches, else FALSE
Packit 504f36
*/
Packit 504f36
Packit 504f36
BOOL
Packit 504f36
PRIV(xclass)(uint32_t c, PCRE2_SPTR data, BOOL utf)
Packit 504f36
{
Packit 504f36
PCRE2_UCHAR t;
Packit 504f36
BOOL negated = (*data & XCL_NOT) != 0;
Packit 504f36
Packit 504f36
#if PCRE2_CODE_UNIT_WIDTH == 8
Packit 504f36
/* In 8 bit mode, this must always be TRUE. Help the compiler to know that. */
Packit 504f36
utf = TRUE;
Packit 504f36
#endif
Packit 504f36
Packit 504f36
/* Code points < 256 are matched against a bitmap, if one is present. If not,
Packit 504f36
we still carry on, because there may be ranges that start below 256 in the
Packit 504f36
additional data. */
Packit 504f36
Packit 504f36
if (c < 256)
Packit 504f36
  {
Packit 504f36
  if ((*data & XCL_HASPROP) == 0)
Packit 504f36
    {
Packit 504f36
    if ((*data & XCL_MAP) == 0) return negated;
Packit 504f36
    return (((uint8_t *)(data + 1))[c/8] & (1 << (c&7))) != 0;
Packit 504f36
    }
Packit 504f36
  if ((*data & XCL_MAP) != 0 &&
Packit 504f36
    (((uint8_t *)(data + 1))[c/8] & (1 << (c&7))) != 0)
Packit 504f36
    return !negated; /* char found */
Packit 504f36
  }
Packit 504f36
Packit 504f36
/* First skip the bit map if present. Then match against the list of Unicode
Packit 504f36
properties or large chars or ranges that end with a large char. We won't ever
Packit 504f36
encounter XCL_PROP or XCL_NOTPROP when UTF support is not compiled. */
Packit 504f36
Packit 504f36
if ((*data++ & XCL_MAP) != 0) data += 32 / sizeof(PCRE2_UCHAR);
Packit 504f36
Packit 504f36
while ((t = *data++) != XCL_END)
Packit 504f36
  {
Packit 504f36
  uint32_t x, y;
Packit 504f36
  if (t == XCL_SINGLE)
Packit 504f36
    {
Packit 504f36
#ifdef SUPPORT_UNICODE
Packit 504f36
    if (utf)
Packit 504f36
      {
Packit 504f36
      GETCHARINC(x, data); /* macro generates multiple statements */
Packit 504f36
      }
Packit 504f36
    else
Packit 504f36
#endif
Packit 504f36
    x = *data++;
Packit 504f36
    if (c == x) return !negated;
Packit 504f36
    }
Packit 504f36
  else if (t == XCL_RANGE)
Packit 504f36
    {
Packit 504f36
#ifdef SUPPORT_UNICODE
Packit 504f36
    if (utf)
Packit 504f36
      {
Packit 504f36
      GETCHARINC(x, data); /* macro generates multiple statements */
Packit 504f36
      GETCHARINC(y, data); /* macro generates multiple statements */
Packit 504f36
      }
Packit 504f36
    else
Packit 504f36
#endif
Packit 504f36
      {
Packit 504f36
      x = *data++;
Packit 504f36
      y = *data++;
Packit 504f36
      }
Packit 504f36
    if (c >= x && c <= y) return !negated;
Packit 504f36
    }
Packit 504f36
Packit 504f36
#ifdef SUPPORT_UNICODE
Packit 504f36
  else  /* XCL_PROP & XCL_NOTPROP */
Packit 504f36
    {
Packit 504f36
    const ucd_record *prop = GET_UCD(c);
Packit 504f36
    BOOL isprop = t == XCL_PROP;
Packit 504f36
Packit 504f36
    switch(*data)
Packit 504f36
      {
Packit 504f36
      case PT_ANY:
Packit 504f36
      if (isprop) return !negated;
Packit 504f36
      break;
Packit 504f36
Packit 504f36
      case PT_LAMP:
Packit 504f36
      if ((prop->chartype == ucp_Lu || prop->chartype == ucp_Ll ||
Packit 504f36
           prop->chartype == ucp_Lt) == isprop) return !negated;
Packit 504f36
      break;
Packit 504f36
Packit 504f36
      case PT_GC:
Packit 504f36
      if ((data[1] == PRIV(ucp_gentype)[prop->chartype]) == isprop)
Packit 504f36
        return !negated;
Packit 504f36
      break;
Packit 504f36
Packit 504f36
      case PT_PC:
Packit 504f36
      if ((data[1] == prop->chartype) == isprop) return !negated;
Packit 504f36
      break;
Packit 504f36
Packit 504f36
      case PT_SC:
Packit 504f36
      if ((data[1] == prop->script) == isprop) return !negated;
Packit 504f36
      break;
Packit 504f36
Packit 504f36
      case PT_ALNUM:
Packit 504f36
      if ((PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
Packit 504f36
           PRIV(ucp_gentype)[prop->chartype] == ucp_N) == isprop)
Packit 504f36
        return !negated;
Packit 504f36
      break;
Packit 504f36
Packit 504f36
      /* Perl space used to exclude VT, but from Perl 5.18 it is included,
Packit 504f36
      which means that Perl space and POSIX space are now identical. PCRE
Packit 504f36
      was changed at release 8.34. */
Packit 504f36
Packit 504f36
      case PT_SPACE:    /* Perl space */
Packit 504f36
      case PT_PXSPACE:  /* POSIX space */
Packit 504f36
      switch(c)
Packit 504f36
        {
Packit 504f36
        HSPACE_CASES:
Packit 504f36
        VSPACE_CASES:
Packit 504f36
        if (isprop) return !negated;
Packit 504f36
        break;
Packit 504f36
Packit 504f36
        default:
Packit 504f36
        if ((PRIV(ucp_gentype)[prop->chartype] == ucp_Z) == isprop)
Packit 504f36
          return !negated;
Packit 504f36
        break;
Packit 504f36
        }
Packit 504f36
      break;
Packit 504f36
Packit 504f36
      case PT_WORD:
Packit 504f36
      if ((PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
Packit 504f36
           PRIV(ucp_gentype)[prop->chartype] == ucp_N || c == CHAR_UNDERSCORE)
Packit 504f36
             == isprop)
Packit 504f36
        return !negated;
Packit 504f36
      break;
Packit 504f36
Packit 504f36
      case PT_UCNC:
Packit 504f36
      if (c < 0xa0)
Packit 504f36
        {
Packit 504f36
        if ((c == CHAR_DOLLAR_SIGN || c == CHAR_COMMERCIAL_AT ||
Packit 504f36
             c == CHAR_GRAVE_ACCENT) == isprop)
Packit 504f36
          return !negated;
Packit 504f36
        }
Packit 504f36
      else
Packit 504f36
        {
Packit 504f36
        if ((c < 0xd800 || c > 0xdfff) == isprop)
Packit 504f36
          return !negated;
Packit 504f36
        }
Packit 504f36
      break;
Packit 504f36
Packit 504f36
      /* The following three properties can occur only in an XCLASS, as there
Packit 504f36
      is no \p or \P coding for them. */
Packit 504f36
Packit 504f36
      /* Graphic character. Implement this as not Z (space or separator) and
Packit 504f36
      not C (other), except for Cf (format) with a few exceptions. This seems
Packit 504f36
      to be what Perl does. The exceptional characters are:
Packit 504f36
Packit 504f36
      U+061C           Arabic Letter Mark
Packit 504f36
      U+180E           Mongolian Vowel Separator
Packit 504f36
      U+2066 - U+2069  Various "isolate"s
Packit 504f36
      */
Packit 504f36
Packit 504f36
      case PT_PXGRAPH:
Packit 504f36
      if ((PRIV(ucp_gentype)[prop->chartype] != ucp_Z &&
Packit 504f36
            (PRIV(ucp_gentype)[prop->chartype] != ucp_C ||
Packit 504f36
              (prop->chartype == ucp_Cf &&
Packit 504f36
                c != 0x061c && c != 0x180e && (c < 0x2066 || c > 0x2069))
Packit 504f36
         )) == isprop)
Packit 504f36
        return !negated;
Packit 504f36
      break;
Packit 504f36
Packit 504f36
      /* Printable character: same as graphic, with the addition of Zs, i.e.
Packit 504f36
      not Zl and not Zp, and U+180E. */
Packit 504f36
Packit 504f36
      case PT_PXPRINT:
Packit 504f36
      if ((prop->chartype != ucp_Zl &&
Packit 504f36
           prop->chartype != ucp_Zp &&
Packit 504f36
            (PRIV(ucp_gentype)[prop->chartype] != ucp_C ||
Packit 504f36
              (prop->chartype == ucp_Cf &&
Packit 504f36
                c != 0x061c && (c < 0x2066 || c > 0x2069))
Packit 504f36
         )) == isprop)
Packit 504f36
        return !negated;
Packit 504f36
      break;
Packit 504f36
Packit 504f36
      /* Punctuation: all Unicode punctuation, plus ASCII characters that
Packit 504f36
      Unicode treats as symbols rather than punctuation, for Perl
Packit 504f36
      compatibility (these are $+<=>^`|~). */
Packit 504f36
Packit 504f36
      case PT_PXPUNCT:
Packit 504f36
      if ((PRIV(ucp_gentype)[prop->chartype] == ucp_P ||
Packit 504f36
            (c < 128 && PRIV(ucp_gentype)[prop->chartype] == ucp_S)) == isprop)
Packit 504f36
        return !negated;
Packit 504f36
      break;
Packit 504f36
Packit 504f36
      /* This should never occur, but compilers may mutter if there is no
Packit 504f36
      default. */
Packit 504f36
Packit 504f36
      default:
Packit 504f36
      return FALSE;
Packit 504f36
      }
Packit 504f36
Packit 504f36
    data += 2;
Packit 504f36
    }
Packit 504f36
#else
Packit 504f36
  (void)utf;  /* Avoid compiler warning */
Packit 504f36
#endif  /* SUPPORT_UNICODE */
Packit 504f36
  }
Packit 504f36
Packit 504f36
return negated;   /* char did not match */
Packit 504f36
}
Packit 504f36
Packit 504f36
/* End of pcre2_xclass.c */