Path: blob/master/thirdparty/pcre2/src/pcre2_script_run.c
9903 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-2021 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 the function for checking a script run. */4142#ifdef HAVE_CONFIG_H43#include "config.h"44#endif4546#include "pcre2_internal.h"474849/*************************************************50* Check script run *51*************************************************/5253/* A script run is conceptually a sequence of characters all in the same54Unicode script. However, it isn't quite that simple. There are special rules55for scripts that are commonly used together, and also special rules for digits.56This function implements the appropriate checks, which is possible only when57PCRE2 is compiled with Unicode support. The function returns TRUE if there is58no Unicode support; however, it should never be called in that circumstance59because an error is given by pcre2_compile() if a script run is called for in a60version of PCRE2 compiled without Unicode support.6162Arguments:63pgr point to the first character64endptr point after the last character65utf TRUE if in UTF mode6667Returns: TRUE if this is a valid script run68*/6970/* These are states in the checking process. */7172enum { SCRIPT_UNSET, /* Requirement as yet unknown */73SCRIPT_MAP, /* Bitmap contains acceptable scripts */74SCRIPT_HANPENDING, /* Have had only Han characters */75SCRIPT_HANHIRAKATA, /* Expect Han or Hirikata */76SCRIPT_HANBOPOMOFO, /* Expect Han or Bopomofo */77SCRIPT_HANHANGUL /* Expect Han or Hangul */78};7980#define UCD_MAPSIZE (ucp_Unknown/32 + 1)81#define FULL_MAPSIZE (ucp_Script_Count/32 + 1)8283BOOL84PRIV(script_run)(PCRE2_SPTR ptr, PCRE2_SPTR endptr, BOOL utf)85{86#ifdef SUPPORT_UNICODE87uint32_t require_state = SCRIPT_UNSET;88uint32_t require_map[FULL_MAPSIZE];89uint32_t map[FULL_MAPSIZE];90uint32_t require_digitset = 0;91uint32_t c;9293#if PCRE2_CODE_UNIT_WIDTH == 3294(void)utf; /* Avoid compiler warning */95#endif9697/* Any string containing fewer than 2 characters is a valid script run. */9899if (ptr >= endptr) return TRUE;100GETCHARINCTEST(c, ptr);101if (ptr >= endptr) return TRUE;102103/* Initialize the require map. This is a full-size bitmap that has a bit for104every script, as opposed to the maps in ucd_script_sets, which only have bits105for scripts less than ucp_Unknown - those that appear in script extension106lists. */107108for (int i = 0; i < FULL_MAPSIZE; i++) require_map[i] = 0;109110/* Scan strings of two or more characters, checking the Unicode characteristics111of each code point. There is special code for scripts that can be combined with112characters from the Han Chinese script. This may be used in conjunction with113four other scripts in these combinations:114115. Han with Hiragana and Katakana is allowed (for Japanese).116. Han with Bopomofo is allowed (for Taiwanese Mandarin).117. Han with Hangul is allowed (for Korean).118119If the first significant character's script is one of the four, the required120script type is immediately known. However, if the first significant121character's script is Han, we have to keep checking for a non-Han character.122Hence the SCRIPT_HANPENDING state. */123124for (;;)125{126const ucd_record *ucd = GET_UCD(c);127uint32_t script = ucd->script;128129/* If the script is Unknown, the string is not a valid script run. Such130characters can only form script runs of length one (see test above). */131132if (script == ucp_Unknown) return FALSE;133134/* A character without any script extensions whose script is Inherited or135Common is always accepted with any script. If there are extensions, the136following processing happens for all scripts. */137138if (UCD_SCRIPTX_PROP(ucd) != 0 || (script != ucp_Inherited && script != ucp_Common))139{140BOOL OK;141142/* Set up a full-sized map for this character that can include bits for all143scripts. Copy the scriptx map for this character (which covers those144scripts that appear in script extension lists), set the remaining values to145zero, and then, except for Common or Inherited, add this script's bit to146the map. */147148memcpy(map, PRIV(ucd_script_sets) + UCD_SCRIPTX_PROP(ucd), UCD_MAPSIZE * sizeof(uint32_t));149memset(map + UCD_MAPSIZE, 0, (FULL_MAPSIZE - UCD_MAPSIZE) * sizeof(uint32_t));150if (script != ucp_Common && script != ucp_Inherited) MAPSET(map, script);151152/* Handle the different checking states */153154switch(require_state)155{156/* First significant character - it might follow Common or Inherited157characters that do not have any script extensions. */158159case SCRIPT_UNSET:160switch(script)161{162case ucp_Han:163require_state = SCRIPT_HANPENDING;164break;165166case ucp_Hiragana:167case ucp_Katakana:168require_state = SCRIPT_HANHIRAKATA;169break;170171case ucp_Bopomofo:172require_state = SCRIPT_HANBOPOMOFO;173break;174175case ucp_Hangul:176require_state = SCRIPT_HANHANGUL;177break;178179default:180memcpy(require_map, map, FULL_MAPSIZE * sizeof(uint32_t));181require_state = SCRIPT_MAP;182break;183}184break;185186/* The first significant character was Han. An inspection of the Unicode18711.0.0 files shows that there are the following types of Script Extension188list that involve the Han, Bopomofo, Hiragana, Katakana, and Hangul189scripts:190191. Bopomofo + Han192. Han + Hiragana + Katakana193. Hiragana + Katakana194. Bopopmofo + Hangul + Han + Hiragana + Katakana195196The following code tries to make sense of this. */197198#define FOUND_BOPOMOFO 1199#define FOUND_HIRAGANA 2200#define FOUND_KATAKANA 4201#define FOUND_HANGUL 8202203case SCRIPT_HANPENDING:204if (script != ucp_Han) /* Another Han does nothing */205{206uint32_t chspecial = 0;207208if (MAPBIT(map, ucp_Bopomofo) != 0) chspecial |= FOUND_BOPOMOFO;209if (MAPBIT(map, ucp_Hiragana) != 0) chspecial |= FOUND_HIRAGANA;210if (MAPBIT(map, ucp_Katakana) != 0) chspecial |= FOUND_KATAKANA;211if (MAPBIT(map, ucp_Hangul) != 0) chspecial |= FOUND_HANGUL;212213if (chspecial == 0) return FALSE; /* Not allowed with Han */214215if (chspecial == FOUND_BOPOMOFO)216require_state = SCRIPT_HANBOPOMOFO;217else if (chspecial == (FOUND_HIRAGANA|FOUND_KATAKANA))218require_state = SCRIPT_HANHIRAKATA;219220/* Otherwise this character must be allowed with all of them, so remain221in the pending state. */222}223break;224225/* Previously encountered one of the "with Han" scripts. Check that226this character is appropriate. */227228case SCRIPT_HANHIRAKATA:229if (MAPBIT(map, ucp_Han) + MAPBIT(map, ucp_Hiragana) +230MAPBIT(map, ucp_Katakana) == 0) return FALSE;231break;232233case SCRIPT_HANBOPOMOFO:234if (MAPBIT(map, ucp_Han) + MAPBIT(map, ucp_Bopomofo) == 0) return FALSE;235break;236237case SCRIPT_HANHANGUL:238if (MAPBIT(map, ucp_Han) + MAPBIT(map, ucp_Hangul) == 0) return FALSE;239break;240241/* Previously encountered one or more characters that are allowed with a242list of scripts. */243244case SCRIPT_MAP:245OK = FALSE;246247for (int i = 0; i < FULL_MAPSIZE; i++)248{249if ((require_map[i] & map[i]) != 0)250{251OK = TRUE;252break;253}254}255256if (!OK) return FALSE;257258/* The rest of the string must be in this script, but we have to259allow for the Han complications. */260261switch(script)262{263case ucp_Han:264require_state = SCRIPT_HANPENDING;265break;266267case ucp_Hiragana:268case ucp_Katakana:269require_state = SCRIPT_HANHIRAKATA;270break;271272case ucp_Bopomofo:273require_state = SCRIPT_HANBOPOMOFO;274break;275276case ucp_Hangul:277require_state = SCRIPT_HANHANGUL;278break;279280/* Compute the intersection of the required list of scripts and the281allowed scripts for this character. */282283default:284for (int i = 0; i < FULL_MAPSIZE; i++) require_map[i] &= map[i];285break;286}287288break;289}290} /* End checking character's script and extensions. */291292/* The character is in an acceptable script. We must now ensure that all293decimal digits in the string come from the same set. Some scripts (e.g.294Common, Arabic) have more than one set of decimal digits. This code does295not allow mixing sets, even within the same script. The vector called296PRIV(ucd_digit_sets)[] contains, in its first element, the number of297following elements, and then, in ascending order, the code points of the298'9' characters in every set of 10 digits. Each set is identified by the299offset in the vector of its '9' character. An initial check of the first300value picks up ASCII digits quickly. Otherwise, a binary chop is used. */301302if (ucd->chartype == ucp_Nd)303{304uint32_t digitset;305306if (c <= PRIV(ucd_digit_sets)[1]) digitset = 1; else307{308int mid;309int bot = 1;310int top = PRIV(ucd_digit_sets)[0];311for (;;)312{313if (top <= bot + 1) /* <= rather than == is paranoia */314{315digitset = top;316break;317}318mid = (top + bot) / 2;319if (c <= PRIV(ucd_digit_sets)[mid]) top = mid; else bot = mid;320}321}322323/* A required value of 0 means "unset". */324325if (require_digitset == 0) require_digitset = digitset;326else if (digitset != require_digitset) return FALSE;327} /* End digit handling */328329/* If we haven't yet got to the end, pick up the next character. */330331if (ptr >= endptr) return TRUE;332GETCHARINCTEST(c, ptr);333} /* End checking loop */334335#else /* NOT SUPPORT_UNICODE */336(void)ptr;337(void)endptr;338(void)utf;339return TRUE;340#endif /* SUPPORT_UNICODE */341}342343/* End of pcre2_script_run.c */344345346