Path: blob/master/thirdparty/pcre2/src/pcre2_valid_utf.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-2020 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 an internal function for validating UTF character42strings. This file is also #included by the pcre2test program, which uses43macros to change names from _pcre2_xxx to xxxx, thereby avoiding name clashes44with the library. In this case, PCRE2_PCRE2TEST is defined. */4546#ifndef PCRE2_PCRE2TEST /* We're compiling the library */47#ifdef HAVE_CONFIG_H48#include "config.h"49#endif50#include "pcre2_internal.h"51#endif /* PCRE2_PCRE2TEST */525354#ifndef SUPPORT_UNICODE55/*************************************************56* Dummy function when Unicode is not supported *57*************************************************/5859/* This function should never be called when Unicode is not supported. */6061int62PRIV(valid_utf)(PCRE2_SPTR string, PCRE2_SIZE length, PCRE2_SIZE *erroroffset)63{64(void)string;65(void)length;66(void)erroroffset;67return 0;68}69#else /* UTF is supported */70717273/*************************************************74* Validate a UTF string *75*************************************************/7677/* This function is called (optionally) at the start of compile or match, to78check that a supposed UTF string is actually valid. The early check means79that subsequent code can assume it is dealing with a valid string. The check80can be turned off for maximum performance, but the consequences of supplying an81invalid string are then undefined.8283Arguments:84string points to the string85length length of string86errp pointer to an error position offset variable8788Returns: == 0 if the string is a valid UTF string89!= 0 otherwise, setting the offset of the bad character90*/9192int93PRIV(valid_utf)(PCRE2_SPTR string, PCRE2_SIZE length, PCRE2_SIZE *erroroffset)94{95PCRE2_SPTR p;96uint32_t c;9798/* ----------------- Check a UTF-8 string ----------------- */99100#if PCRE2_CODE_UNIT_WIDTH == 8101102/* Originally, this function checked according to RFC 2279, allowing for values103in the range 0 to 0x7fffffff, up to 6 bytes long, but ensuring that they were104in the canonical format. Once somebody had pointed out RFC 3629 to me (it105obsoletes 2279), additional restrictions were applied. The values are now106limited to be between 0 and 0x0010ffff, no more than 4 bytes long, and the107subrange 0xd000 to 0xdfff is excluded. However, the format of 5-byte and 6-byte108characters is still checked. Error returns are as follows:109110PCRE2_ERROR_UTF8_ERR1 Missing 1 byte at the end of the string111PCRE2_ERROR_UTF8_ERR2 Missing 2 bytes at the end of the string112PCRE2_ERROR_UTF8_ERR3 Missing 3 bytes at the end of the string113PCRE2_ERROR_UTF8_ERR4 Missing 4 bytes at the end of the string114PCRE2_ERROR_UTF8_ERR5 Missing 5 bytes at the end of the string115PCRE2_ERROR_UTF8_ERR6 2nd-byte's two top bits are not 0x80116PCRE2_ERROR_UTF8_ERR7 3rd-byte's two top bits are not 0x80117PCRE2_ERROR_UTF8_ERR8 4th-byte's two top bits are not 0x80118PCRE2_ERROR_UTF8_ERR9 5th-byte's two top bits are not 0x80119PCRE2_ERROR_UTF8_ERR10 6th-byte's two top bits are not 0x80120PCRE2_ERROR_UTF8_ERR11 5-byte character is not permitted by RFC 3629121PCRE2_ERROR_UTF8_ERR12 6-byte character is not permitted by RFC 3629122PCRE2_ERROR_UTF8_ERR13 4-byte character with value > 0x10ffff is not permitted123PCRE2_ERROR_UTF8_ERR14 3-byte character with value 0xd800-0xdfff is not permitted124PCRE2_ERROR_UTF8_ERR15 Overlong 2-byte sequence125PCRE2_ERROR_UTF8_ERR16 Overlong 3-byte sequence126PCRE2_ERROR_UTF8_ERR17 Overlong 4-byte sequence127PCRE2_ERROR_UTF8_ERR18 Overlong 5-byte sequence (won't ever occur)128PCRE2_ERROR_UTF8_ERR19 Overlong 6-byte sequence (won't ever occur)129PCRE2_ERROR_UTF8_ERR20 Isolated 0x80 byte (not within UTF-8 character)130PCRE2_ERROR_UTF8_ERR21 Byte with the illegal value 0xfe or 0xff131*/132133for (p = string; length > 0; p++)134{135uint32_t ab, d;136137c = *p;138length--;139140if (c < 128) continue; /* ASCII character */141142if (c < 0xc0) /* Isolated 10xx xxxx byte */143{144*erroroffset = (PCRE2_SIZE)(p - string);145return PCRE2_ERROR_UTF8_ERR20;146}147148if (c >= 0xfe) /* Invalid 0xfe or 0xff bytes */149{150*erroroffset = (PCRE2_SIZE)(p - string);151return PCRE2_ERROR_UTF8_ERR21;152}153154ab = PRIV(utf8_table4)[c & 0x3f]; /* Number of additional bytes (1-5) */155if (length < ab) /* Missing bytes */156{157*erroroffset = (PCRE2_SIZE)(p - string);158switch(ab - length)159{160case 1: return PCRE2_ERROR_UTF8_ERR1;161case 2: return PCRE2_ERROR_UTF8_ERR2;162case 3: return PCRE2_ERROR_UTF8_ERR3;163case 4: return PCRE2_ERROR_UTF8_ERR4;164case 5: return PCRE2_ERROR_UTF8_ERR5;165}166}167length -= ab; /* Length remaining */168169/* Check top bits in the second byte */170171if (((d = *(++p)) & 0xc0) != 0x80)172{173*erroroffset = (PCRE2_SIZE)(p - string) - 1;174return PCRE2_ERROR_UTF8_ERR6;175}176177/* For each length, check that the remaining bytes start with the 0x80 bit178set and not the 0x40 bit. Then check for an overlong sequence, and for the179excluded range 0xd800 to 0xdfff. */180181switch (ab)182{183/* 2-byte character. No further bytes to check for 0x80. Check first byte184for for xx00 000x (overlong sequence). */185186case 1: if ((c & 0x3e) == 0)187{188*erroroffset = (PCRE2_SIZE)(p - string) - 1;189return PCRE2_ERROR_UTF8_ERR15;190}191break;192193/* 3-byte character. Check third byte for 0x80. Then check first 2 bytes194for 1110 0000, xx0x xxxx (overlong sequence) or1951110 1101, 1010 xxxx (0xd800 - 0xdfff) */196197case 2:198if ((*(++p) & 0xc0) != 0x80) /* Third byte */199{200*erroroffset = (PCRE2_SIZE)(p - string) - 2;201return PCRE2_ERROR_UTF8_ERR7;202}203if (c == 0xe0 && (d & 0x20) == 0)204{205*erroroffset = (PCRE2_SIZE)(p - string) - 2;206return PCRE2_ERROR_UTF8_ERR16;207}208if (c == 0xed && d >= 0xa0)209{210*erroroffset = (PCRE2_SIZE)(p - string) - 2;211return PCRE2_ERROR_UTF8_ERR14;212}213break;214215/* 4-byte character. Check 3rd and 4th bytes for 0x80. Then check first 2216bytes for for 1111 0000, xx00 xxxx (overlong sequence), then check for a217character greater than 0x0010ffff (f4 8f bf bf) */218219case 3:220if ((*(++p) & 0xc0) != 0x80) /* Third byte */221{222*erroroffset = (PCRE2_SIZE)(p - string) - 2;223return PCRE2_ERROR_UTF8_ERR7;224}225if ((*(++p) & 0xc0) != 0x80) /* Fourth byte */226{227*erroroffset = (PCRE2_SIZE)(p - string) - 3;228return PCRE2_ERROR_UTF8_ERR8;229}230if (c == 0xf0 && (d & 0x30) == 0)231{232*erroroffset = (PCRE2_SIZE)(p - string) - 3;233return PCRE2_ERROR_UTF8_ERR17;234}235if (c > 0xf4 || (c == 0xf4 && d > 0x8f))236{237*erroroffset = (PCRE2_SIZE)(p - string) - 3;238return PCRE2_ERROR_UTF8_ERR13;239}240break;241242/* 5-byte and 6-byte characters are not allowed by RFC 3629, and will be243rejected by the length test below. However, we do the appropriate tests244here so that overlong sequences get diagnosed, and also in case there is245ever an option for handling these larger code points. */246247/* 5-byte character. Check 3rd, 4th, and 5th bytes for 0x80. Then check for2481111 1000, xx00 0xxx */249250case 4:251if ((*(++p) & 0xc0) != 0x80) /* Third byte */252{253*erroroffset = (PCRE2_SIZE)(p - string) - 2;254return PCRE2_ERROR_UTF8_ERR7;255}256if ((*(++p) & 0xc0) != 0x80) /* Fourth byte */257{258*erroroffset = (PCRE2_SIZE)(p - string) - 3;259return PCRE2_ERROR_UTF8_ERR8;260}261if ((*(++p) & 0xc0) != 0x80) /* Fifth byte */262{263*erroroffset = (PCRE2_SIZE)(p - string) - 4;264return PCRE2_ERROR_UTF8_ERR9;265}266if (c == 0xf8 && (d & 0x38) == 0)267{268*erroroffset = (PCRE2_SIZE)(p - string) - 4;269return PCRE2_ERROR_UTF8_ERR18;270}271break;272273/* 6-byte character. Check 3rd-6th bytes for 0x80. Then check for2741111 1100, xx00 00xx. */275276case 5:277if ((*(++p) & 0xc0) != 0x80) /* Third byte */278{279*erroroffset = (PCRE2_SIZE)(p - string) - 2;280return PCRE2_ERROR_UTF8_ERR7;281}282if ((*(++p) & 0xc0) != 0x80) /* Fourth byte */283{284*erroroffset = (PCRE2_SIZE)(p - string) - 3;285return PCRE2_ERROR_UTF8_ERR8;286}287if ((*(++p) & 0xc0) != 0x80) /* Fifth byte */288{289*erroroffset = (PCRE2_SIZE)(p - string) - 4;290return PCRE2_ERROR_UTF8_ERR9;291}292if ((*(++p) & 0xc0) != 0x80) /* Sixth byte */293{294*erroroffset = (PCRE2_SIZE)(p - string) - 5;295return PCRE2_ERROR_UTF8_ERR10;296}297if (c == 0xfc && (d & 0x3c) == 0)298{299*erroroffset = (PCRE2_SIZE)(p - string) - 5;300return PCRE2_ERROR_UTF8_ERR19;301}302break;303}304305/* Character is valid under RFC 2279, but 4-byte and 5-byte characters are306excluded by RFC 3629. The pointer p is currently at the last byte of the307character. */308309if (ab > 3)310{311*erroroffset = (PCRE2_SIZE)(p - string) - ab;312return (ab == 4)? PCRE2_ERROR_UTF8_ERR11 : PCRE2_ERROR_UTF8_ERR12;313}314}315return 0;316317318/* ----------------- Check a UTF-16 string ----------------- */319320#elif PCRE2_CODE_UNIT_WIDTH == 16321322/* There's not so much work, nor so many errors, for UTF-16.323PCRE2_ERROR_UTF16_ERR1 Missing low surrogate at the end of the string324PCRE2_ERROR_UTF16_ERR2 Invalid low surrogate325PCRE2_ERROR_UTF16_ERR3 Isolated low surrogate326*/327328for (p = string; length > 0; p++)329{330c = *p;331length--;332333if ((c & 0xf800) != 0xd800)334{335/* Normal UTF-16 code point. Neither high nor low surrogate. */336}337else if ((c & 0x0400) == 0)338{339/* High surrogate. Must be a followed by a low surrogate. */340if (length == 0)341{342*erroroffset = (PCRE2_SIZE)(p - string);343return PCRE2_ERROR_UTF16_ERR1;344}345p++;346length--;347if ((*p & 0xfc00) != 0xdc00)348{349*erroroffset = (PCRE2_SIZE)(p - string) - 1;350return PCRE2_ERROR_UTF16_ERR2;351}352}353else354{355/* Isolated low surrogate. Always an error. */356*erroroffset = (PCRE2_SIZE)(p - string);357return PCRE2_ERROR_UTF16_ERR3;358}359}360return 0;361362363364/* ----------------- Check a UTF-32 string ----------------- */365366#else367368/* There is very little to do for a UTF-32 string.369PCRE2_ERROR_UTF32_ERR1 Surrogate character370PCRE2_ERROR_UTF32_ERR2 Character > 0x10ffff371*/372373for (p = string; length > 0; length--, p++)374{375c = *p;376if ((c & 0xfffff800u) != 0xd800u)377{378/* Normal UTF-32 code point. Neither high nor low surrogate. */379if (c > 0x10ffffu)380{381*erroroffset = (PCRE2_SIZE)(p - string);382return PCRE2_ERROR_UTF32_ERR2;383}384}385else386{387/* A surrogate */388*erroroffset = (PCRE2_SIZE)(p - string);389return PCRE2_ERROR_UTF32_ERR1;390}391}392return 0;393#endif /* CODE_UNIT_WIDTH */394}395#endif /* SUPPORT_UNICODE */396397/* End of pcre2_valid_utf.c */398399400