Blame src/pcre2_valid_utf.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-2017 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
Packit 504f36
/* This module contains an internal function for validating UTF character
Packit 504f36
strings. This file is also #included by the pcre2test program, which uses
Packit 504f36
macros to change names from _pcre2_xxx to xxxx, thereby avoiding name clashes
Packit 504f36
with the library. In this case, PCRE2_PCRE2TEST is defined. */
Packit 504f36
Packit 504f36
#ifndef PCRE2_PCRE2TEST           /* We're compiling the library */
Packit 504f36
#ifdef HAVE_CONFIG_H
Packit 504f36
#include "config.h"
Packit 504f36
#endif
Packit 504f36
#include "pcre2_internal.h"
Packit 504f36
#endif /* PCRE2_PCRE2TEST */
Packit 504f36
Packit 504f36
Packit 504f36
#ifndef SUPPORT_UNICODE
Packit 504f36
/*************************************************
Packit 504f36
*  Dummy function when Unicode is not supported  *
Packit 504f36
*************************************************/
Packit 504f36
Packit 504f36
/* This function should never be called when Unicode is not supported. */
Packit 504f36
Packit 504f36
int
Packit 504f36
PRIV(valid_utf)(PCRE2_SPTR string, PCRE2_SIZE length, PCRE2_SIZE *erroroffset)
Packit 504f36
{
Packit 504f36
(void)string;
Packit 504f36
(void)length;
Packit 504f36
(void)erroroffset;
Packit 504f36
return 0;
Packit 504f36
}
Packit 504f36
#else  /* UTF is supported */
Packit 504f36
Packit 504f36
Packit 504f36
Packit 504f36
/*************************************************
Packit 504f36
*           Validate a UTF string                *
Packit 504f36
*************************************************/
Packit 504f36
Packit 504f36
/* This function is called (optionally) at the start of compile or match, to
Packit 504f36
check that a supposed UTF string is actually valid. The early check means
Packit 504f36
that subsequent code can assume it is dealing with a valid string. The check
Packit 504f36
can be turned off for maximum performance, but the consequences of supplying an
Packit 504f36
invalid string are then undefined.
Packit 504f36
Packit 504f36
Arguments:
Packit 504f36
  string       points to the string
Packit 504f36
  length       length of string
Packit 504f36
  errp         pointer to an error position offset variable
Packit 504f36
Packit 504f36
Returns:       == 0    if the string is a valid UTF string
Packit 504f36
               != 0    otherwise, setting the offset of the bad character
Packit 504f36
*/
Packit 504f36
Packit 504f36
int
Packit 504f36
PRIV(valid_utf)(PCRE2_SPTR string, PCRE2_SIZE length, PCRE2_SIZE *erroroffset)
Packit 504f36
{
Packit 504f36
PCRE2_SPTR p;
Packit 504f36
uint32_t c;
Packit 504f36
Packit 504f36
/* ----------------- Check a UTF-8 string ----------------- */
Packit 504f36
Packit 504f36
#if PCRE2_CODE_UNIT_WIDTH == 8
Packit 504f36
Packit 504f36
/* Originally, this function checked according to RFC 2279, allowing for values
Packit 504f36
in the range 0 to 0x7fffffff, up to 6 bytes long, but ensuring that they were
Packit 504f36
in the canonical format. Once somebody had pointed out RFC 3629 to me (it
Packit 504f36
obsoletes 2279), additional restrictions were applied. The values are now
Packit 504f36
limited to be between 0 and 0x0010ffff, no more than 4 bytes long, and the
Packit 504f36
subrange 0xd000 to 0xdfff is excluded. However, the format of 5-byte and 6-byte
Packit 504f36
characters is still checked. Error returns are as follows:
Packit 504f36
Packit 504f36
PCRE2_ERROR_UTF8_ERR1   Missing 1 byte at the end of the string
Packit 504f36
PCRE2_ERROR_UTF8_ERR2   Missing 2 bytes at the end of the string
Packit 504f36
PCRE2_ERROR_UTF8_ERR3   Missing 3 bytes at the end of the string
Packit 504f36
PCRE2_ERROR_UTF8_ERR4   Missing 4 bytes at the end of the string
Packit 504f36
PCRE2_ERROR_UTF8_ERR5   Missing 5 bytes at the end of the string
Packit 504f36
PCRE2_ERROR_UTF8_ERR6   2nd-byte's two top bits are not 0x80
Packit 504f36
PCRE2_ERROR_UTF8_ERR7   3rd-byte's two top bits are not 0x80
Packit 504f36
PCRE2_ERROR_UTF8_ERR8   4th-byte's two top bits are not 0x80
Packit 504f36
PCRE2_ERROR_UTF8_ERR9   5th-byte's two top bits are not 0x80
Packit 504f36
PCRE2_ERROR_UTF8_ERR10  6th-byte's two top bits are not 0x80
Packit 504f36
PCRE2_ERROR_UTF8_ERR11  5-byte character is not permitted by RFC 3629
Packit 504f36
PCRE2_ERROR_UTF8_ERR12  6-byte character is not permitted by RFC 3629
Packit 504f36
PCRE2_ERROR_UTF8_ERR13  4-byte character with value > 0x10ffff is not permitted
Packit 504f36
PCRE2_ERROR_UTF8_ERR14  3-byte character with value 0xd800-0xdfff is not permitted
Packit 504f36
PCRE2_ERROR_UTF8_ERR15  Overlong 2-byte sequence
Packit 504f36
PCRE2_ERROR_UTF8_ERR16  Overlong 3-byte sequence
Packit 504f36
PCRE2_ERROR_UTF8_ERR17  Overlong 4-byte sequence
Packit 504f36
PCRE2_ERROR_UTF8_ERR18  Overlong 5-byte sequence (won't ever occur)
Packit 504f36
PCRE2_ERROR_UTF8_ERR19  Overlong 6-byte sequence (won't ever occur)
Packit 504f36
PCRE2_ERROR_UTF8_ERR20  Isolated 0x80 byte (not within UTF-8 character)
Packit 504f36
PCRE2_ERROR_UTF8_ERR21  Byte with the illegal value 0xfe or 0xff
Packit 504f36
*/
Packit 504f36
Packit 504f36
for (p = string; length > 0; p++)
Packit 504f36
  {
Packit 504f36
  uint32_t ab, d;
Packit 504f36
Packit 504f36
  c = *p;
Packit 504f36
  length--;
Packit 504f36
Packit 504f36
  if (c < 128) continue;                /* ASCII character */
Packit 504f36
Packit 504f36
  if (c < 0xc0)                         /* Isolated 10xx xxxx byte */
Packit 504f36
    {
Packit 504f36
    *erroroffset = (PCRE2_SIZE)(p - string);
Packit 504f36
    return PCRE2_ERROR_UTF8_ERR20;
Packit 504f36
    }
Packit 504f36
Packit 504f36
  if (c >= 0xfe)                        /* Invalid 0xfe or 0xff bytes */
Packit 504f36
    {
Packit 504f36
    *erroroffset = (PCRE2_SIZE)(p - string);
Packit 504f36
    return PCRE2_ERROR_UTF8_ERR21;
Packit 504f36
    }
Packit 504f36
Packit 504f36
  ab = PRIV(utf8_table4)[c & 0x3f];     /* Number of additional bytes (1-5) */
Packit 504f36
  if (length < ab)                      /* Missing bytes */
Packit 504f36
    {
Packit 504f36
    *erroroffset = (PCRE2_SIZE)(p - string);
Packit 504f36
    switch(ab - length)
Packit 504f36
      {
Packit 504f36
      case 1: return PCRE2_ERROR_UTF8_ERR1;
Packit 504f36
      case 2: return PCRE2_ERROR_UTF8_ERR2;
Packit 504f36
      case 3: return PCRE2_ERROR_UTF8_ERR3;
Packit 504f36
      case 4: return PCRE2_ERROR_UTF8_ERR4;
Packit 504f36
      case 5: return PCRE2_ERROR_UTF8_ERR5;
Packit 504f36
      }
Packit 504f36
    }
Packit 504f36
  length -= ab;                         /* Length remaining */
Packit 504f36
Packit 504f36
  /* Check top bits in the second byte */
Packit 504f36
Packit 504f36
  if (((d = *(++p)) & 0xc0) != 0x80)
Packit 504f36
    {
Packit 504f36
    *erroroffset = (int)(p - string) - 1;
Packit 504f36
    return PCRE2_ERROR_UTF8_ERR6;
Packit 504f36
    }
Packit 504f36
Packit 504f36
  /* For each length, check that the remaining bytes start with the 0x80 bit
Packit 504f36
  set and not the 0x40 bit. Then check for an overlong sequence, and for the
Packit 504f36
  excluded range 0xd800 to 0xdfff. */
Packit 504f36
Packit 504f36
  switch (ab)
Packit 504f36
    {
Packit 504f36
    /* 2-byte character. No further bytes to check for 0x80. Check first byte
Packit 504f36
    for for xx00 000x (overlong sequence). */
Packit 504f36
Packit 504f36
    case 1: if ((c & 0x3e) == 0)
Packit 504f36
      {
Packit 504f36
      *erroroffset = (int)(p - string) - 1;
Packit 504f36
      return PCRE2_ERROR_UTF8_ERR15;
Packit 504f36
      }
Packit 504f36
    break;
Packit 504f36
Packit 504f36
    /* 3-byte character. Check third byte for 0x80. Then check first 2 bytes
Packit 504f36
      for 1110 0000, xx0x xxxx (overlong sequence) or
Packit 504f36
          1110 1101, 1010 xxxx (0xd800 - 0xdfff) */
Packit 504f36
Packit 504f36
    case 2:
Packit 504f36
    if ((*(++p) & 0xc0) != 0x80)     /* Third byte */
Packit 504f36
      {
Packit 504f36
      *erroroffset = (int)(p - string) - 2;
Packit 504f36
      return PCRE2_ERROR_UTF8_ERR7;
Packit 504f36
      }
Packit 504f36
    if (c == 0xe0 && (d & 0x20) == 0)
Packit 504f36
      {
Packit 504f36
      *erroroffset = (int)(p - string) - 2;
Packit 504f36
      return PCRE2_ERROR_UTF8_ERR16;
Packit 504f36
      }
Packit 504f36
    if (c == 0xed && d >= 0xa0)
Packit 504f36
      {
Packit 504f36
      *erroroffset = (int)(p - string) - 2;
Packit 504f36
      return PCRE2_ERROR_UTF8_ERR14;
Packit 504f36
      }
Packit 504f36
    break;
Packit 504f36
Packit 504f36
    /* 4-byte character. Check 3rd and 4th bytes for 0x80. Then check first 2
Packit 504f36
       bytes for for 1111 0000, xx00 xxxx (overlong sequence), then check for a
Packit 504f36
       character greater than 0x0010ffff (f4 8f bf bf) */
Packit 504f36
Packit 504f36
    case 3:
Packit 504f36
    if ((*(++p) & 0xc0) != 0x80)     /* Third byte */
Packit 504f36
      {
Packit 504f36
      *erroroffset = (int)(p - string) - 2;
Packit 504f36
      return PCRE2_ERROR_UTF8_ERR7;
Packit 504f36
      }
Packit 504f36
    if ((*(++p) & 0xc0) != 0x80)     /* Fourth byte */
Packit 504f36
      {
Packit 504f36
      *erroroffset = (int)(p - string) - 3;
Packit 504f36
      return PCRE2_ERROR_UTF8_ERR8;
Packit 504f36
      }
Packit 504f36
    if (c == 0xf0 && (d & 0x30) == 0)
Packit 504f36
      {
Packit 504f36
      *erroroffset = (int)(p - string) - 3;
Packit 504f36
      return PCRE2_ERROR_UTF8_ERR17;
Packit 504f36
      }
Packit 504f36
    if (c > 0xf4 || (c == 0xf4 && d > 0x8f))
Packit 504f36
      {
Packit 504f36
      *erroroffset = (int)(p - string) - 3;
Packit 504f36
      return PCRE2_ERROR_UTF8_ERR13;
Packit 504f36
      }
Packit 504f36
    break;
Packit 504f36
Packit 504f36
    /* 5-byte and 6-byte characters are not allowed by RFC 3629, and will be
Packit 504f36
    rejected by the length test below. However, we do the appropriate tests
Packit 504f36
    here so that overlong sequences get diagnosed, and also in case there is
Packit 504f36
    ever an option for handling these larger code points. */
Packit 504f36
Packit 504f36
    /* 5-byte character. Check 3rd, 4th, and 5th bytes for 0x80. Then check for
Packit 504f36
    1111 1000, xx00 0xxx */
Packit 504f36
Packit 504f36
    case 4:
Packit 504f36
    if ((*(++p) & 0xc0) != 0x80)     /* Third byte */
Packit 504f36
      {
Packit 504f36
      *erroroffset = (int)(p - string) - 2;
Packit 504f36
      return PCRE2_ERROR_UTF8_ERR7;
Packit 504f36
      }
Packit 504f36
    if ((*(++p) & 0xc0) != 0x80)     /* Fourth byte */
Packit 504f36
      {
Packit 504f36
      *erroroffset = (int)(p - string) - 3;
Packit 504f36
      return PCRE2_ERROR_UTF8_ERR8;
Packit 504f36
      }
Packit 504f36
    if ((*(++p) & 0xc0) != 0x80)     /* Fifth byte */
Packit 504f36
      {
Packit 504f36
      *erroroffset = (int)(p - string) - 4;
Packit 504f36
      return PCRE2_ERROR_UTF8_ERR9;
Packit 504f36
      }
Packit 504f36
    if (c == 0xf8 && (d & 0x38) == 0)
Packit 504f36
      {
Packit 504f36
      *erroroffset = (int)(p - string) - 4;
Packit 504f36
      return PCRE2_ERROR_UTF8_ERR18;
Packit 504f36
      }
Packit 504f36
    break;
Packit 504f36
Packit 504f36
    /* 6-byte character. Check 3rd-6th bytes for 0x80. Then check for
Packit 504f36
    1111 1100, xx00 00xx. */
Packit 504f36
Packit 504f36
    case 5:
Packit 504f36
    if ((*(++p) & 0xc0) != 0x80)     /* Third byte */
Packit 504f36
      {
Packit 504f36
      *erroroffset = (int)(p - string) - 2;
Packit 504f36
      return PCRE2_ERROR_UTF8_ERR7;
Packit 504f36
      }
Packit 504f36
    if ((*(++p) & 0xc0) != 0x80)     /* Fourth byte */
Packit 504f36
      {
Packit 504f36
      *erroroffset = (int)(p - string) - 3;
Packit 504f36
      return PCRE2_ERROR_UTF8_ERR8;
Packit 504f36
      }
Packit 504f36
    if ((*(++p) & 0xc0) != 0x80)     /* Fifth byte */
Packit 504f36
      {
Packit 504f36
      *erroroffset = (int)(p - string) - 4;
Packit 504f36
      return PCRE2_ERROR_UTF8_ERR9;
Packit 504f36
      }
Packit 504f36
    if ((*(++p) & 0xc0) != 0x80)     /* Sixth byte */
Packit 504f36
      {
Packit 504f36
      *erroroffset = (int)(p - string) - 5;
Packit 504f36
      return PCRE2_ERROR_UTF8_ERR10;
Packit 504f36
      }
Packit 504f36
    if (c == 0xfc && (d & 0x3c) == 0)
Packit 504f36
      {
Packit 504f36
      *erroroffset = (int)(p - string) - 5;
Packit 504f36
      return PCRE2_ERROR_UTF8_ERR19;
Packit 504f36
      }
Packit 504f36
    break;
Packit 504f36
    }
Packit 504f36
Packit 504f36
  /* Character is valid under RFC 2279, but 4-byte and 5-byte characters are
Packit 504f36
  excluded by RFC 3629. The pointer p is currently at the last byte of the
Packit 504f36
  character. */
Packit 504f36
Packit 504f36
  if (ab > 3)
Packit 504f36
    {
Packit 504f36
    *erroroffset = (int)(p - string) - ab;
Packit 504f36
    return (ab == 4)? PCRE2_ERROR_UTF8_ERR11 : PCRE2_ERROR_UTF8_ERR12;
Packit 504f36
    }
Packit 504f36
  }
Packit 504f36
return 0;
Packit 504f36
Packit 504f36
Packit 504f36
/* ----------------- Check a UTF-16 string ----------------- */
Packit 504f36
Packit 504f36
#elif PCRE2_CODE_UNIT_WIDTH == 16
Packit 504f36
Packit 504f36
/* There's not so much work, nor so many errors, for UTF-16.
Packit 504f36
PCRE2_ERROR_UTF16_ERR1  Missing low surrogate at the end of the string
Packit 504f36
PCRE2_ERROR_UTF16_ERR2  Invalid low surrogate
Packit 504f36
PCRE2_ERROR_UTF16_ERR3  Isolated low surrogate
Packit 504f36
*/
Packit 504f36
Packit 504f36
for (p = string; length > 0; p++)
Packit 504f36
  {
Packit 504f36
  c = *p;
Packit 504f36
  length--;
Packit 504f36
Packit 504f36
  if ((c & 0xf800) != 0xd800)
Packit 504f36
    {
Packit 504f36
    /* Normal UTF-16 code point. Neither high nor low surrogate. */
Packit 504f36
    }
Packit 504f36
  else if ((c & 0x0400) == 0)
Packit 504f36
    {
Packit 504f36
    /* High surrogate. Must be a followed by a low surrogate. */
Packit 504f36
    if (length == 0)
Packit 504f36
      {
Packit 504f36
      *erroroffset = p - string;
Packit 504f36
      return PCRE2_ERROR_UTF16_ERR1;
Packit 504f36
      }
Packit 504f36
    p++;
Packit 504f36
    length--;
Packit 504f36
    if ((*p & 0xfc00) != 0xdc00)
Packit 504f36
      {
Packit 504f36
      *erroroffset = p - string;
Packit 504f36
      return PCRE2_ERROR_UTF16_ERR2;
Packit 504f36
      }
Packit 504f36
    }
Packit 504f36
  else
Packit 504f36
    {
Packit 504f36
    /* Isolated low surrogate. Always an error. */
Packit 504f36
    *erroroffset = p - string;
Packit 504f36
    return PCRE2_ERROR_UTF16_ERR3;
Packit 504f36
    }
Packit 504f36
  }
Packit 504f36
return 0;
Packit 504f36
Packit 504f36
Packit 504f36
Packit 504f36
/* ----------------- Check a UTF-32 string ----------------- */
Packit 504f36
Packit 504f36
#else
Packit 504f36
Packit 504f36
/* There is very little to do for a UTF-32 string.
Packit 504f36
PCRE2_ERROR_UTF32_ERR1  Surrogate character
Packit 504f36
PCRE2_ERROR_UTF32_ERR2  Character > 0x10ffff
Packit 504f36
*/
Packit 504f36
Packit 504f36
for (p = string; length > 0; length--, p++)
Packit 504f36
  {
Packit 504f36
  c = *p;
Packit 504f36
  if ((c & 0xfffff800u) != 0xd800u)
Packit 504f36
    {
Packit 504f36
    /* Normal UTF-32 code point. Neither high nor low surrogate. */
Packit 504f36
    if (c > 0x10ffffu)
Packit 504f36
      {
Packit 504f36
      *erroroffset = p - string;
Packit 504f36
      return PCRE2_ERROR_UTF32_ERR2;
Packit 504f36
      }
Packit 504f36
    }
Packit 504f36
  else
Packit 504f36
    {
Packit 504f36
    /* A surrogate */
Packit 504f36
    *erroroffset = p - string;
Packit 504f36
    return PCRE2_ERROR_UTF32_ERR1;
Packit 504f36
    }
Packit 504f36
  }
Packit 504f36
return 0;
Packit 504f36
#endif  /* CODE_UNIT_WIDTH */
Packit 504f36
}
Packit 504f36
#endif  /* SUPPORT_UNICODE */
Packit 504f36
Packit 504f36
/* End of pcre2_valid_utf.c */