Blame Identity/Models/Html/phpBB/3.0.4/includes/utf/utf_normalizer.php

d6e8d8
d6e8d8
/**
d6e8d8
*
d6e8d8
* @package utf
d6e8d8
* @version $Id: utf_normalizer.php 8479 2008-03-29 00:22:48Z naderman $
d6e8d8
* @copyright (c) 2005 phpBB Group
d6e8d8
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
d6e8d8
*
d6e8d8
*/
d6e8d8
d6e8d8
/**
d6e8d8
*/
d6e8d8
if (!defined('IN_PHPBB'))
d6e8d8
{
d6e8d8
	exit;
d6e8d8
}
d6e8d8
d6e8d8
/**
d6e8d8
* Some Unicode characters encoded in UTF-8
d6e8d8
*
d6e8d8
* Preserved for compatibility
d6e8d8
*/
d6e8d8
define('UTF8_REPLACEMENT', "\xEF\xBF\xBD");
d6e8d8
define('UTF8_MAX', "\xF4\x8F\xBF\xBF");
d6e8d8
define('UTF8_FFFE', "\xEF\xBF\xBE");
d6e8d8
define('UTF8_FFFF', "\xEF\xBF\xBF");
d6e8d8
define('UTF8_SURROGATE_FIRST', "\xED\xA0\x80");
d6e8d8
define('UTF8_SURROGATE_LAST', "\xED\xBF\xBF");
d6e8d8
define('UTF8_HANGUL_FIRST', "\xEA\xB0\x80");
d6e8d8
define('UTF8_HANGUL_LAST', "\xED\x9E\xA3");
d6e8d8
d6e8d8
define('UTF8_CJK_FIRST', "\xE4\xB8\x80");
d6e8d8
define('UTF8_CJK_LAST', "\xE9\xBE\xBB");
d6e8d8
define('UTF8_CJK_B_FIRST', "\xF0\xA0\x80\x80");
d6e8d8
define('UTF8_CJK_B_LAST', "\xF0\xAA\x9B\x96");
d6e8d8
d6e8d8
// Unset global variables
d6e8d8
unset($GLOBALS['utf_jamo_index'], $GLOBALS['utf_jamo_type'], $GLOBALS['utf_nfc_qc'], $GLOBALS['utf_combining_class'], $GLOBALS['utf_canonical_comp'], $GLOBALS['utf_canonical_decomp'], $GLOBALS['utf_nfkc_qc'], $GLOBALS['utf_compatibility_decomp']);
d6e8d8
d6e8d8
// NFC_QC and NFKC_QC values
d6e8d8
define('UNICODE_QC_MAYBE', 0);
d6e8d8
define('UNICODE_QC_NO', 1);
d6e8d8
d6e8d8
// Contains all the ASCII characters appearing in UTF-8, sorted by frequency
d6e8d8
define('UTF8_ASCII_RANGE', "\x20\x65\x69\x61\x73\x6E\x74\x72\x6F\x6C\x75\x64\x5D\x5B\x63\x6D\x70\x27\x0A\x67\x7C\x68\x76\x2E\x66\x62\x2C\x3A\x3D\x2D\x71\x31\x30\x43\x32\x2A\x79\x78\x29\x28\x4C\x39\x41\x53\x2F\x50\x22\x45\x6A\x4D\x49\x6B\x33\x3E\x35\x54\x3C\x44\x34\x7D\x42\x7B\x38\x46\x77\x52\x36\x37\x55\x47\x4E\x3B\x4A\x7A\x56\x23\x48\x4F\x57\x5F\x26\x21\x4B\x3F\x58\x51\x25\x59\x5C\x09\x5A\x2B\x7E\x5E\x24\x40\x60\x7F\x00\x01\x02\x03\x04\x05\x06\x07\x08\x0B\x0C\x0D\x0E\x0F\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F");
d6e8d8
d6e8d8
// Contains all the tail bytes that can appear in the composition of a UTF-8 char
d6e8d8
define('UTF8_TRAILING_BYTES', "\xA9\xA0\xA8\x80\xAA\x99\xA7\xBB\xAB\x89\x94\x82\xB4\xA2\xAE\x83\xB0\xB9\xB8\x93\xAF\xBC\xB3\x81\xA4\xB2\x9C\xA1\xB5\xBE\xBD\xBA\x98\xAD\xB1\x84\x95\xA6\xB6\x88\x8D\x90\xB7\xBF\x92\x85\xA5\x97\x8C\x86\xA3\x8E\x9F\x8F\x87\x91\x9D\xAC\x9E\x8B\x96\x9B\x8A\x9A");
d6e8d8
d6e8d8
// Constants used by the Hangul [de]composition algorithms
d6e8d8
define('UNICODE_HANGUL_SBASE', 0xAC00);
d6e8d8
define('UNICODE_HANGUL_LBASE', 0x1100);
d6e8d8
define('UNICODE_HANGUL_VBASE', 0x1161);
d6e8d8
define('UNICODE_HANGUL_TBASE', 0x11A7);
d6e8d8
define('UNICODE_HANGUL_SCOUNT', 11172);
d6e8d8
define('UNICODE_HANGUL_LCOUNT', 19);
d6e8d8
define('UNICODE_HANGUL_VCOUNT', 21);
d6e8d8
define('UNICODE_HANGUL_TCOUNT', 28);
d6e8d8
define('UNICODE_HANGUL_NCOUNT', 588);
d6e8d8
define('UNICODE_JAMO_L', 0);
d6e8d8
define('UNICODE_JAMO_V', 1);
d6e8d8
define('UNICODE_JAMO_T', 2);
d6e8d8
d6e8d8
/**
d6e8d8
* Unicode normalization routines
d6e8d8
*
d6e8d8
* @package utf
d6e8d8
*/
d6e8d8
class utf_normalizer
d6e8d8
{
d6e8d8
	/**
d6e8d8
	* Validate, cleanup and normalize a string
d6e8d8
	*
d6e8d8
	* The ultimate convenience function! Clean up invalid UTF-8 sequences,
d6e8d8
	* and convert to Normal Form C, canonical composition.
d6e8d8
	*
d6e8d8
	* @param	string	&$str	The dirty string
d6e8d8
	* @return	string			The same string, all shiny and cleaned-up
d6e8d8
	*/
d6e8d8
	function cleanup(&$str)
d6e8d8
	{
d6e8d8
		// The string below is the list of all autorized characters, sorted by frequency in latin text
d6e8d8
		$pos = strspn($str, "\x20\x65\x69\x61\x73\x6E\x74\x72\x6F\x6C\x75\x64\x5D\x5B\x63\x6D\x70\x27\x0A\x67\x7C\x68\x76\x2E\x66\x62\x2C\x3A\x3D\x2D\x71\x31\x30\x43\x32\x2A\x79\x78\x29\x28\x4C\x39\x41\x53\x2F\x50\x22\x45\x6A\x4D\x49\x6B\x33\x3E\x35\x54\x3C\x44\x34\x7D\x42\x7B\x38\x46\x77\x52\x36\x37\x55\x47\x4E\x3B\x4A\x7A\x56\x23\x48\x4F\x57\x5F\x26\x21\x4B\x3F\x58\x51\x25\x59\x5C\x09\x5A\x2B\x7E\x5E\x24\x40\x60\x7F\x0D");
d6e8d8
		$len = strlen($str);
d6e8d8
d6e8d8
		if ($pos == $len)
d6e8d8
		{
d6e8d8
			// ASCII strings with no special chars return immediately
d6e8d8
			return;
d6e8d8
		}
d6e8d8
d6e8d8
		// Note: we do not check for $GLOBALS['utf_canonical_decomp']. It is assumed they are always loaded together
d6e8d8
		if (!isset($GLOBALS['utf_nfc_qc']))
d6e8d8
		{
d6e8d8
			global $phpbb_root_path, $phpEx;
d6e8d8
			include($phpbb_root_path . 'includes/utf/data/utf_nfc_qc.' . $phpEx);
d6e8d8
		}
d6e8d8
d6e8d8
		if (!isset($GLOBALS['utf_canonical_decomp']))
d6e8d8
		{
d6e8d8
			global $phpbb_root_path, $phpEx;
d6e8d8
			include($phpbb_root_path . 'includes/utf/data/utf_canonical_decomp.' . $phpEx);
d6e8d8
		}
d6e8d8
d6e8d8
		// Replace any byte in the range 0x00..0x1F, except for \r, \n and \t
d6e8d8
		// We replace those characters with a 0xFF byte, which is illegal in UTF-8 and will in turn be replaced with a UTF replacement char
d6e8d8
		$str = strtr(
d6e8d8
			$str,
d6e8d8
			"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x0B\x0C\x0E\x0F\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F",
d6e8d8
			"\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"
d6e8d8
		);
d6e8d8
d6e8d8
		$str = utf_normalizer::recompose($str, $pos, $len, $GLOBALS['utf_nfc_qc'], $GLOBALS['utf_canonical_decomp']);
d6e8d8
	}
d6e8d8
d6e8d8
	/**
d6e8d8
	* Validate and normalize a UTF string to NFC
d6e8d8
	*
d6e8d8
	* @param	string	&$str	Unchecked UTF string
d6e8d8
	* @return	string			The string, validated and in normal form
d6e8d8
	*/
d6e8d8
	function nfc(&$str)
d6e8d8
	{
d6e8d8
		$pos = strspn($str, UTF8_ASCII_RANGE);
d6e8d8
		$len = strlen($str);
d6e8d8
d6e8d8
		if ($pos == $len)
d6e8d8
		{
d6e8d8
			// ASCII strings return immediately
d6e8d8
			return;
d6e8d8
		}
d6e8d8
d6e8d8
		if (!isset($GLOBALS['utf_nfc_qc']))
d6e8d8
		{
d6e8d8
			global $phpbb_root_path, $phpEx;
d6e8d8
			include($phpbb_root_path . 'includes/utf/data/utf_nfc_qc.' . $phpEx);
d6e8d8
		}
d6e8d8
d6e8d8
		if (!isset($GLOBALS['utf_canonical_decomp']))
d6e8d8
		{
d6e8d8
			global $phpbb_root_path, $phpEx;
d6e8d8
			include($phpbb_root_path . 'includes/utf/data/utf_canonical_decomp.' . $phpEx);
d6e8d8
		}
d6e8d8
d6e8d8
		$str = utf_normalizer::recompose($str, $pos, $len, $GLOBALS['utf_nfc_qc'], $GLOBALS['utf_canonical_decomp']);
d6e8d8
	}
d6e8d8
d6e8d8
	/**
d6e8d8
	* Validate and normalize a UTF string to NFKC
d6e8d8
	*
d6e8d8
	* @param	string	&$str	Unchecked UTF string
d6e8d8
	* @return	string			The string, validated and in normal form
d6e8d8
	*/
d6e8d8
	function nfkc(&$str)
d6e8d8
	{
d6e8d8
		$pos = strspn($str, UTF8_ASCII_RANGE);
d6e8d8
		$len = strlen($str);
d6e8d8
d6e8d8
		if ($pos == $len)
d6e8d8
		{
d6e8d8
			// ASCII strings return immediately
d6e8d8
			return;
d6e8d8
		}
d6e8d8
d6e8d8
		if (!isset($GLOBALS['utf_nfkc_qc']))
d6e8d8
		{
d6e8d8
			global $phpbb_root_path, $phpEx;
d6e8d8
			include($phpbb_root_path . 'includes/utf/data/utf_nfkc_qc.' . $phpEx);
d6e8d8
		}
d6e8d8
d6e8d8
		if (!isset($GLOBALS['utf_compatibility_decomp']))
d6e8d8
		{
d6e8d8
			global $phpbb_root_path, $phpEx;
d6e8d8
			include($phpbb_root_path . 'includes/utf/data/utf_compatibility_decomp.' . $phpEx);
d6e8d8
		}
d6e8d8
d6e8d8
		$str = utf_normalizer::recompose($str, $pos, $len, $GLOBALS['utf_nfkc_qc'], $GLOBALS['utf_compatibility_decomp']);
d6e8d8
	}
d6e8d8
d6e8d8
	/**
d6e8d8
	* Validate and normalize a UTF string to NFD
d6e8d8
	*
d6e8d8
	* @param	string	&$str	Unchecked UTF string
d6e8d8
	* @return	string			The string, validated and in normal form
d6e8d8
	*/
d6e8d8
	function nfd(&$str)
d6e8d8
	{
d6e8d8
		$pos = strspn($str, UTF8_ASCII_RANGE);
d6e8d8
		$len = strlen($str);
d6e8d8
d6e8d8
		if ($pos == $len)
d6e8d8
		{
d6e8d8
			// ASCII strings return immediately
d6e8d8
			return;
d6e8d8
		}
d6e8d8
d6e8d8
		if (!isset($GLOBALS['utf_canonical_decomp']))
d6e8d8
		{
d6e8d8
			global $phpbb_root_path, $phpEx;
d6e8d8
			include($phpbb_root_path . 'includes/utf/data/utf_canonical_decomp.' . $phpEx);
d6e8d8
		}
d6e8d8
d6e8d8
		$str = utf_normalizer::decompose($str, $pos, $len, $GLOBALS['utf_canonical_decomp']);
d6e8d8
	}
d6e8d8
d6e8d8
	/**
d6e8d8
	* Validate and normalize a UTF string to NFKD
d6e8d8
	*
d6e8d8
	* @param	string	&$str	Unchecked UTF string
d6e8d8
	* @return	string			The string, validated and in normal form
d6e8d8
	*/
d6e8d8
	function nfkd(&$str)
d6e8d8
	{
d6e8d8
		$pos = strspn($str, UTF8_ASCII_RANGE);
d6e8d8
		$len = strlen($str);
d6e8d8
d6e8d8
		if ($pos == $len)
d6e8d8
		{
d6e8d8
			// ASCII strings return immediately
d6e8d8
			return;
d6e8d8
		}
d6e8d8
d6e8d8
		if (!isset($GLOBALS['utf_compatibility_decomp']))
d6e8d8
		{
d6e8d8
			global $phpbb_root_path, $phpEx;
d6e8d8
			include($phpbb_root_path . 'includes/utf/data/utf_compatibility_decomp.' . $phpEx);
d6e8d8
		}
d6e8d8
d6e8d8
		$str = utf_normalizer::decompose($str, $pos, $len, $GLOBALS['utf_compatibility_decomp']);
d6e8d8
	}
d6e8d8
d6e8d8
d6e8d8
	/**
d6e8d8
	* Recompose a UTF string
d6e8d8
	*
d6e8d8
	* @param	string	$str			Unchecked UTF string
d6e8d8
	* @param	integer	$pos			Position of the first UTF char (in bytes)
d6e8d8
	* @param	integer	$len			Length of the string (in bytes)
d6e8d8
	* @param	array	&$qc			Quick-check array, passed by reference but never modified
d6e8d8
	* @param	array	&$decomp_map	Decomposition mapping, passed by reference but never modified
d6e8d8
	* @return	string					The string, validated and recomposed
d6e8d8
	*
d6e8d8
	* @access	private
d6e8d8
	*/
d6e8d8
	function recompose($str, $pos, $len, &$qc, &$decomp_map)
d6e8d8
	{
d6e8d8
		global $utf_combining_class, $utf_canonical_comp, $utf_jamo_type, $utf_jamo_index;
d6e8d8
d6e8d8
		// Load some commonly-used tables
d6e8d8
		if (!isset($utf_jamo_index, $utf_jamo_type, $utf_combining_class))
d6e8d8
		{
d6e8d8
			global $phpbb_root_path, $phpEx;
d6e8d8
			include($phpbb_root_path . 'includes/utf/data/utf_normalizer_common.' . $phpEx);
d6e8d8
		}
d6e8d8
d6e8d8
		// Load the canonical composition table
d6e8d8
		if (!isset($utf_canonical_comp))
d6e8d8
		{
d6e8d8
			global $phpbb_root_path, $phpEx;
d6e8d8
			include($phpbb_root_path . 'includes/utf/data/utf_canonical_comp.' . $phpEx);
d6e8d8
		}
d6e8d8
d6e8d8
		// Buffer the last ASCII char before the UTF-8 stuff if applicable
d6e8d8
		$tmp = '';
d6e8d8
		$i = $tmp_pos = $last_cc = 0;
d6e8d8
d6e8d8
		$buffer = ($pos) ? array(++$i => $str[$pos - 1]) : array();
d6e8d8
d6e8d8
		// UTF char length array
d6e8d8
		// This array is used to determine the length of a UTF character.
d6e8d8
		// Be $c the result of ($str[$pos] & "\xF0") --where $str is the string we're operating on and $pos
d6e8d8
		// the position of the cursor--, if $utf_len_mask[$c] does not exist, the byte is an ASCII char.
d6e8d8
		// Otherwise, if $utf_len_mask[$c] is greater than 0, we have a the leading byte of a multibyte character
d6e8d8
		// whose length is $utf_len_mask[$c] and if it is equal to 0, the byte is a trailing byte.
d6e8d8
		$utf_len_mask = array(
d6e8d8
			// Leading bytes masks
d6e8d8
			"\xC0" => 2, "\xD0" => 2, "\xE0" => 3, "\xF0" => 4,
d6e8d8
			// Trailing bytes masks
d6e8d8
			"\x80" => 0, "\x90" => 0, "\xA0" => 0, "\xB0" => 0
d6e8d8
		);
d6e8d8
d6e8d8
		$extra_check = array(
d6e8d8
			"\xED" => 1, "\xEF" => 1, "\xC0" => 1, "\xC1" => 1, "\xE0" => 1, "\xF0" => 1,
d6e8d8
			"\xF4" => 1, "\xF5" => 1, "\xF6" => 1, "\xF7" => 1, "\xF8" => 1, "\xF9" => 1,
d6e8d8
			"\xFA" => 1, "\xFB" => 1, "\xFC" => 1, "\xFD" => 1, "\xFE" => 1, "\xFF" => 1
d6e8d8
		);
d6e8d8
d6e8d8
		$utf_validation_mask = array(
d6e8d8
			2	=> "\xE0\xC0",
d6e8d8
			3	=> "\xF0\xC0\xC0",
d6e8d8
			4	=> "\xF8\xC0\xC0\xC0"
d6e8d8
		);
d6e8d8
d6e8d8
		$utf_validation_check = array(
d6e8d8
			2	=> "\xC0\x80",
d6e8d8
			3	=> "\xE0\x80\x80",
d6e8d8
			4	=> "\xF0\x80\x80\x80"
d6e8d8
		);
d6e8d8
d6e8d8
		// Main loop
d6e8d8
		do
d6e8d8
		{
d6e8d8
			// STEP 0: Capture the current char and buffer it
d6e8d8
			$c = $str[$pos];
d6e8d8
			$c_mask = $c & "\xF0";
d6e8d8
d6e8d8
			if (isset($utf_len_mask[$c_mask]))
d6e8d8
			{
d6e8d8
				// Byte at $pos is either a leading byte or a missplaced trailing byte
d6e8d8
				if ($utf_len = $utf_len_mask[$c_mask])
d6e8d8
				{
d6e8d8
					// Capture the char
d6e8d8
					$buffer[++$i & 7] = $utf_char = substr($str, $pos, $utf_len);
d6e8d8
d6e8d8
					// Let's find out if a thorough check is needed
d6e8d8
					if (isset($qc[$utf_char]))
d6e8d8
					{
d6e8d8
						// If the UTF char is in the qc array then it may not be in normal form. We do nothing here, the actual processing is below this "if" block
d6e8d8
					}
d6e8d8
					else if (isset($utf_combining_class[$utf_char]))
d6e8d8
					{
d6e8d8
						if ($utf_combining_class[$utf_char] < $last_cc)
d6e8d8
						{
d6e8d8
							// A combining character that is NOT canonically ordered
d6e8d8
						}
d6e8d8
						else
d6e8d8
						{
d6e8d8
							// A combining character that IS canonically ordered, skip to the next char
d6e8d8
							$last_cc = $utf_combining_class[$utf_char];
d6e8d8
d6e8d8
							$pos += $utf_len;
d6e8d8
							continue;
d6e8d8
						}
d6e8d8
					}
d6e8d8
					else
d6e8d8
					{
d6e8d8
						// At this point, $utf_char holds a UTF char that we know is not a NF[K]C_QC and is not a combining character.
d6e8d8
						// It can be a singleton, a canonical composite, a replacement char or an even an ill-formed bunch of bytes. Let's find out
d6e8d8
						$last_cc = 0;
d6e8d8
d6e8d8
						// Check that we have the correct number of trailing bytes
d6e8d8
						if (($utf_char & $utf_validation_mask[$utf_len]) != $utf_validation_check[$utf_len])
d6e8d8
						{
d6e8d8
							// Current char isn't well-formed or legal: either one or several trailing bytes are missing, or the Unicode char
d6e8d8
							// has been encoded in a five- or six- byte sequence
d6e8d8
							if ($utf_char[0] >= "\xF8")
d6e8d8
							{
d6e8d8
								if ($utf_char[0] < "\xFC")
d6e8d8
								{
d6e8d8
									$trailing_bytes = 4;
d6e8d8
								}
d6e8d8
								else if ($utf_char[0] > "\xFD")
d6e8d8
								{
d6e8d8
									$trailing_bytes = 0;
d6e8d8
								}
d6e8d8
								else
d6e8d8
								{
d6e8d8
									$trailing_bytes = 5;
d6e8d8
								}
d6e8d8
							}
d6e8d8
							else
d6e8d8
							{
d6e8d8
								$trailing_bytes = $utf_len - 1;
d6e8d8
							}
d6e8d8
d6e8d8
							$tmp .= substr($str, $tmp_pos, $pos - $tmp_pos) . UTF8_REPLACEMENT;
d6e8d8
							$pos += strspn($str, UTF8_TRAILING_BYTES, ++$pos, $trailing_bytes);
d6e8d8
							$tmp_pos = $pos;
d6e8d8
d6e8d8
							continue;
d6e8d8
						}
d6e8d8
d6e8d8
						if (isset($extra_check[$c]))
d6e8d8
						{
d6e8d8
							switch ($c)
d6e8d8
							{
d6e8d8
								// Note: 0xED is quite common in Korean
d6e8d8
								case "\xED":
d6e8d8
									if ($utf_char >= "\xED\xA0\x80")
d6e8d8
									{
d6e8d8
										// Surrogates (U+D800..U+DFFF) are not allowed in UTF-8 (UTF sequence 0xEDA080..0xEDBFBF)
d6e8d8
										$tmp .= substr($str, $tmp_pos, $pos - $tmp_pos) . UTF8_REPLACEMENT;
d6e8d8
										$pos += $utf_len;
d6e8d8
										$tmp_pos = $pos;
d6e8d8
										continue 2;
d6e8d8
									}
d6e8d8
								break;
d6e8d8
d6e8d8
								// Note: 0xEF is quite common in Japanese
d6e8d8
								case "\xEF":
d6e8d8
									if ($utf_char == "\xEF\xBF\xBE" || $utf_char == "\xEF\xBF\xBF")
d6e8d8
									{
d6e8d8
										// U+FFFE and U+FFFF are explicitly disallowed (UTF sequence 0xEFBFBE..0xEFBFBF)
d6e8d8
										$tmp .= substr($str, $tmp_pos, $pos - $tmp_pos) . UTF8_REPLACEMENT;
d6e8d8
										$pos += $utf_len;
d6e8d8
										$tmp_pos = $pos;
d6e8d8
										continue 2;
d6e8d8
									}
d6e8d8
								break;
d6e8d8
d6e8d8
								case "\xC0":
d6e8d8
								case "\xC1":
d6e8d8
									if ($utf_char <= "\xC1\xBF")
d6e8d8
									{
d6e8d8
										// Overlong sequence: Unicode char U+0000..U+007F encoded as a double-byte UTF char
d6e8d8
										$tmp .= substr($str, $tmp_pos, $pos - $tmp_pos) . UTF8_REPLACEMENT;
d6e8d8
										$pos += $utf_len;
d6e8d8
										$tmp_pos = $pos;
d6e8d8
										continue 2;
d6e8d8
									}
d6e8d8
								break;
d6e8d8
d6e8d8
								case "\xE0":
d6e8d8
									if ($utf_char <= "\xE0\x9F\xBF")
d6e8d8
									{
d6e8d8
										// Unicode char U+0000..U+07FF encoded in 3 bytes
d6e8d8
										$tmp .= substr($str, $tmp_pos, $pos - $tmp_pos) . UTF8_REPLACEMENT;
d6e8d8
										$pos += $utf_len;
d6e8d8
										$tmp_pos = $pos;
d6e8d8
										continue 2;
d6e8d8
									}
d6e8d8
								break;
d6e8d8
d6e8d8
								case "\xF0":
d6e8d8
									if ($utf_char <= "\xF0\x8F\xBF\xBF")
d6e8d8
									{
d6e8d8
										// Unicode char U+0000..U+FFFF encoded in 4 bytes
d6e8d8
										$tmp .= substr($str, $tmp_pos, $pos - $tmp_pos) . UTF8_REPLACEMENT;
d6e8d8
										$pos += $utf_len;
d6e8d8
										$tmp_pos = $pos;
d6e8d8
										continue 2;
d6e8d8
									}
d6e8d8
								break;
d6e8d8
d6e8d8
								default:
d6e8d8
									// Five- and six- byte sequences do not need being checked for here anymore
d6e8d8
									if ($utf_char > UTF8_MAX)
d6e8d8
									{
d6e8d8
										// Out of the Unicode range
d6e8d8
										if ($utf_char[0] < "\xF8")
d6e8d8
										{
d6e8d8
											$trailing_bytes = 3;
d6e8d8
										}
d6e8d8
										else if ($utf_char[0] < "\xFC")
d6e8d8
										{
d6e8d8
											$trailing_bytes = 4;
d6e8d8
										}
d6e8d8
										else if ($utf_char[0] > "\xFD")
d6e8d8
										{
d6e8d8
											$trailing_bytes = 0;
d6e8d8
										}
d6e8d8
										else
d6e8d8
										{
d6e8d8
											$trailing_bytes = 5;
d6e8d8
										}
d6e8d8
d6e8d8
										$tmp .= substr($str, $tmp_pos, $pos - $tmp_pos) . UTF8_REPLACEMENT;
d6e8d8
										$pos += strspn($str, UTF8_TRAILING_BYTES, ++$pos, $trailing_bytes);
d6e8d8
										$tmp_pos = $pos;
d6e8d8
										continue 2;
d6e8d8
									}
d6e8d8
								break;
d6e8d8
							}
d6e8d8
						}
d6e8d8
d6e8d8
						// The char is a valid starter, move the cursor and go on
d6e8d8
						$pos += $utf_len;
d6e8d8
						continue;
d6e8d8
					}
d6e8d8
				}
d6e8d8
				else
d6e8d8
				{
d6e8d8
					// A trailing byte came out of nowhere, we will advance the cursor and treat the this byte and all following trailing bytes as if
d6e8d8
					// each of them was a Unicode replacement char
d6e8d8
					$spn = strspn($str, UTF8_TRAILING_BYTES, $pos);
d6e8d8
					$tmp .= substr($str, $tmp_pos, $pos - $tmp_pos) . str_repeat(UTF8_REPLACEMENT, $spn);
d6e8d8
d6e8d8
					$pos += $spn;
d6e8d8
					$tmp_pos = $pos;
d6e8d8
					continue;
d6e8d8
				}
d6e8d8
d6e8d8
d6e8d8
				// STEP 1: Decompose current char
d6e8d8
d6e8d8
				// We have found a character that is either:
d6e8d8
				//  - in the NFC_QC/NFKC_QC list
d6e8d8
				//  - a non-starter char that is not canonically ordered
d6e8d8
				//
d6e8d8
				// We are going to capture the shortest UTF sequence that satisfies these two conditions:
d6e8d8
				//
d6e8d8
				//  1 - If the sequence does not start at the begginning of the string, it must begin with a starter,
d6e8d8
				// and that starter must not have the NF[K]C_QC property equal to "MAYBE"
d6e8d8
				//
d6e8d8
				//  2 - If the sequence does not end at the end of the string, it must end with a non-starter and be
d6e8d8
				// immediately followed by a starter that is not on the QC list
d6e8d8
				//
d6e8d8
				$utf_seq = array();
d6e8d8
				$last_cc = 0;
d6e8d8
				$lpos = $pos;
d6e8d8
				$pos += $utf_len;
d6e8d8
d6e8d8
				if (isset($decomp_map[$utf_char]))
d6e8d8
				{
d6e8d8
					$_pos = 0;
d6e8d8
					$_len = strlen($decomp_map[$utf_char]);
d6e8d8
d6e8d8
					do
d6e8d8
					{
d6e8d8
						$_utf_len =& $utf_len_mask[$decomp_map[$utf_char][$_pos] & "\xF0"];
d6e8d8
d6e8d8
						if (isset($_utf_len))
d6e8d8
						{
d6e8d8
							$utf_seq[] = substr($decomp_map[$utf_char], $_pos, $_utf_len);
d6e8d8
							$_pos += $_utf_len;
d6e8d8
						}
d6e8d8
						else
d6e8d8
						{
d6e8d8
							$utf_seq[] = $decomp_map[$utf_char][$_pos];
d6e8d8
							++$_pos;
d6e8d8
						}
d6e8d8
					}
d6e8d8
					while ($_pos < $_len);
d6e8d8
				}
d6e8d8
				else
d6e8d8
				{
d6e8d8
					// The char is not decomposable
d6e8d8
					$utf_seq = array($utf_char);
d6e8d8
				}
d6e8d8
d6e8d8
d6e8d8
				// STEP 2: Capture the starter
d6e8d8
d6e8d8
				// Check out the combining class of the first character of the UTF sequence
d6e8d8
				$k = 0;
d6e8d8
				if (isset($utf_combining_class[$utf_seq[0]]) || $qc[$utf_char] == UNICODE_QC_MAYBE)
d6e8d8
				{
d6e8d8
					// Not a starter, inspect previous characters
d6e8d8
					// The last 8 characters are kept in a buffer so that we don't have to capture them everytime.
d6e8d8
					// This is enough for all real-life strings but even if it wasn't, we can capture characters in backward mode,
d6e8d8
					// although it is slower than this method.
d6e8d8
					//
d6e8d8
					// In the following loop, $j starts at the previous buffered character ($i - 1, because current character is
d6e8d8
					// at offset $i) and process them in backward mode until we find a starter.
d6e8d8
					//
d6e8d8
					// $k is the index on each UTF character inside of our UTF sequence. At this time, $utf_seq contains one or more
d6e8d8
					// characters numbered 0 to n. $k starts at 0 and for each char we prepend we pre-decrement it and for numbering
d6e8d8
					$starter_found = 0;
d6e8d8
					$j_min = max(1, $i - 7);
d6e8d8
d6e8d8
					for ($j = $i - 1; $j >= $j_min && $lpos > $tmp_pos; --$j)
d6e8d8
					{
d6e8d8
						$utf_char = $buffer[$j & 7];
d6e8d8
						$lpos -= strlen($utf_char);
d6e8d8
d6e8d8
						if (isset($decomp_map[$utf_char]))
d6e8d8
						{
d6e8d8
							// The char is a composite, decompose for storage
d6e8d8
							$decomp_seq = array();
d6e8d8
							$_pos = 0;
d6e8d8
							$_len = strlen($decomp_map[$utf_char]);
d6e8d8
d6e8d8
							do
d6e8d8
							{
d6e8d8
								$c = $decomp_map[$utf_char][$_pos];
d6e8d8
								$_utf_len =& $utf_len_mask[$c & "\xF0"];
d6e8d8
d6e8d8
								if (isset($_utf_len))
d6e8d8
								{
d6e8d8
									$decomp_seq[] = substr($decomp_map[$utf_char], $_pos, $_utf_len);
d6e8d8
									$_pos += $_utf_len;
d6e8d8
								}
d6e8d8
								else
d6e8d8
								{
d6e8d8
									$decomp_seq[] = $c;
d6e8d8
									++$_pos;
d6e8d8
								}
d6e8d8
							}
d6e8d8
							while ($_pos < $_len);
d6e8d8
d6e8d8
							// Prepend the UTF sequence with our decomposed sequence
d6e8d8
							if (isset($decomp_seq[1]))
d6e8d8
							{
d6e8d8
								// The char expanded into several chars
d6e8d8
								$decomp_cnt = sizeof($decomp_seq);
d6e8d8
d6e8d8
								foreach ($decomp_seq as $decomp_i => $decomp_char)
d6e8d8
								{
d6e8d8
									$utf_seq[$k + $decomp_i - $decomp_cnt] = $decomp_char;
d6e8d8
								}
d6e8d8
								$k -= $decomp_cnt;
d6e8d8
							}
d6e8d8
							else
d6e8d8
							{
d6e8d8
								// Decomposed to a single char, easier to prepend
d6e8d8
								$utf_seq[--$k] = $decomp_seq[0];
d6e8d8
							}
d6e8d8
						}
d6e8d8
						else
d6e8d8
						{
d6e8d8
							$utf_seq[--$k] = $utf_char;
d6e8d8
						}
d6e8d8
d6e8d8
						if (!isset($utf_combining_class[$utf_seq[$k]]))
d6e8d8
						{
d6e8d8
							// We have found our starter
d6e8d8
							$starter_found = 1;
d6e8d8
							break;
d6e8d8
						}
d6e8d8
					}
d6e8d8
d6e8d8
					if (!$starter_found && $lpos > $tmp_pos)
d6e8d8
					{
d6e8d8
						// The starter was not found in the buffer, let's rewind some more
d6e8d8
						do
d6e8d8
						{
d6e8d8
							// $utf_len_mask contains the masks of both leading bytes and trailing bytes. If $utf_en > 0 then it's a leading byte, otherwise it's a trailing byte.
d6e8d8
							$c = $str[--$lpos];
d6e8d8
							$c_mask = $c & "\xF0";
d6e8d8
d6e8d8
							if (isset($utf_len_mask[$c_mask]))
d6e8d8
							{
d6e8d8
								// UTF byte
d6e8d8
								if ($utf_len = $utf_len_mask[$c_mask])
d6e8d8
								{
d6e8d8
									// UTF *leading* byte
d6e8d8
									$utf_char = substr($str, $lpos, $utf_len);
d6e8d8
d6e8d8
									if (isset($decomp_map[$utf_char]))
d6e8d8
									{
d6e8d8
										// Decompose the character
d6e8d8
										$decomp_seq = array();
d6e8d8
										$_pos = 0;
d6e8d8
										$_len = strlen($decomp_map[$utf_char]);
d6e8d8
d6e8d8
										do
d6e8d8
										{
d6e8d8
											$c = $decomp_map[$utf_char][$_pos];
d6e8d8
											$_utf_len =& $utf_len_mask[$c & "\xF0"];
d6e8d8
d6e8d8
											if (isset($_utf_len))
d6e8d8
											{
d6e8d8
												$decomp_seq[] = substr($decomp_map[$utf_char], $_pos, $_utf_len);
d6e8d8
												$_pos += $_utf_len;
d6e8d8
											}
d6e8d8
											else
d6e8d8
											{
d6e8d8
												$decomp_seq[] = $c;
d6e8d8
												++$_pos;
d6e8d8
											}
d6e8d8
										}
d6e8d8
										while ($_pos < $_len);
d6e8d8
d6e8d8
										// Prepend the UTF sequence with our decomposed sequence
d6e8d8
										if (isset($decomp_seq[1]))
d6e8d8
										{
d6e8d8
											// The char expanded into several chars
d6e8d8
											$decomp_cnt = sizeof($decomp_seq);
d6e8d8
											foreach ($decomp_seq as $decomp_i => $utf_char)
d6e8d8
											{
d6e8d8
												$utf_seq[$k + $decomp_i - $decomp_cnt] = $utf_char;
d6e8d8
											}
d6e8d8
											$k -= $decomp_cnt;
d6e8d8
										}
d6e8d8
										else
d6e8d8
										{
d6e8d8
											// Decomposed to a single char, easier to prepend
d6e8d8
											$utf_seq[--$k] = $decomp_seq[0];
d6e8d8
										}
d6e8d8
									}
d6e8d8
									else
d6e8d8
									{
d6e8d8
										$utf_seq[--$k] = $utf_char;
d6e8d8
									}
d6e8d8
								}
d6e8d8
							}
d6e8d8
							else
d6e8d8
							{
d6e8d8
								// ASCII char
d6e8d8
								$utf_seq[--$k] = $c;
d6e8d8
							}
d6e8d8
						}
d6e8d8
						while ($lpos > $tmp_pos);
d6e8d8
					}
d6e8d8
				}
d6e8d8
d6e8d8
d6e8d8
				// STEP 3: Capture following combining modifiers
d6e8d8
d6e8d8
				while ($pos < $len)
d6e8d8
				{
d6e8d8
					$c_mask = $str[$pos] & "\xF0";
d6e8d8
d6e8d8
					if (isset($utf_len_mask[$c_mask]))
d6e8d8
					{
d6e8d8
						if ($utf_len = $utf_len_mask[$c_mask])
d6e8d8
						{
d6e8d8
							$utf_char = substr($str, $pos, $utf_len);
d6e8d8
						}
d6e8d8
						else
d6e8d8
						{
d6e8d8
							// A trailing byte came out of nowhere
d6e8d8
							// Trailing bytes are replaced with Unicode replacement chars, we will just ignore it for now, break out of the loop
d6e8d8
							// as if it was a starter (replacement chars ARE starters) and let the next loop replace it
d6e8d8
							break;
d6e8d8
						}
d6e8d8
d6e8d8
						if (isset($utf_combining_class[$utf_char]) || isset($qc[$utf_char]))
d6e8d8
						{
d6e8d8
							// Combining character, add it to the sequence and move the cursor
d6e8d8
							if (isset($decomp_map[$utf_char]))
d6e8d8
							{
d6e8d8
								// Decompose the character
d6e8d8
								$_pos = 0;
d6e8d8
								$_len = strlen($decomp_map[$utf_char]);
d6e8d8
d6e8d8
								do
d6e8d8
								{
d6e8d8
									$c = $decomp_map[$utf_char][$_pos];
d6e8d8
									$_utf_len =& $utf_len_mask[$c & "\xF0"];
d6e8d8
d6e8d8
									if (isset($_utf_len))
d6e8d8
									{
d6e8d8
										$utf_seq[] = substr($decomp_map[$utf_char], $_pos, $_utf_len);
d6e8d8
										$_pos += $_utf_len;
d6e8d8
									}
d6e8d8
									else
d6e8d8
									{
d6e8d8
										$utf_seq[] = $c;
d6e8d8
										++$_pos;
d6e8d8
									}
d6e8d8
								}
d6e8d8
								while ($_pos < $_len);
d6e8d8
							}
d6e8d8
							else
d6e8d8
							{
d6e8d8
								$utf_seq[] = $utf_char;
d6e8d8
							}
d6e8d8
d6e8d8
							$pos += $utf_len;
d6e8d8
						}
d6e8d8
						else
d6e8d8
						{
d6e8d8
							// Combining class 0 and no QC, break out of the loop
d6e8d8
							// Note: we do not know if that character is valid. If it's not, the next iteration will replace it
d6e8d8
							break;
d6e8d8
						}
d6e8d8
					}
d6e8d8
					else
d6e8d8
					{
d6e8d8
						// ASCII chars are starters
d6e8d8
						break;
d6e8d8
					}
d6e8d8
				}
d6e8d8
d6e8d8
d6e8d8
				// STEP 4: Sort and combine
d6e8d8
d6e8d8
				// Here we sort...
d6e8d8
				$k_max = $k + sizeof($utf_seq);
d6e8d8
d6e8d8
				if (!$k && $k_max == 1)
d6e8d8
				{
d6e8d8
					// There is only one char in the UTF sequence, add it then jump to the next iteration of main loop
d6e8d8
						// Note: the two commented lines below can be enabled under PHP5 for a very small performance gain in most cases
d6e8d8
//						if (substr_compare($str, $utf_seq[0], $lpos, $pos - $lpos))
d6e8d8
//						{
d6e8d8
						$tmp .= substr($str, $tmp_pos, $lpos - $tmp_pos) . $utf_seq[0];
d6e8d8
						$tmp_pos = $pos;
d6e8d8
//						}
d6e8d8
d6e8d8
					continue;
d6e8d8
				}
d6e8d8
d6e8d8
				// ...there we combine
d6e8d8
				if (isset($utf_combining_class[$utf_seq[$k]]))
d6e8d8
				{
d6e8d8
					$starter = $nf_seq = '';
d6e8d8
				}
d6e8d8
				else
d6e8d8
				{
d6e8d8
					$starter = $utf_seq[$k++];
d6e8d8
					$nf_seq = '';
d6e8d8
				}
d6e8d8
				$utf_sort = array();
d6e8d8
d6e8d8
				// We add an empty char at the end of the UTF char sequence. It will act as a starter and trigger the sort/combine routine
d6e8d8
				// at the end of the string without altering it
d6e8d8
				$utf_seq[] = '';
d6e8d8
d6e8d8
				do
d6e8d8
				{
d6e8d8
					$utf_char = $utf_seq[$k++];
d6e8d8
d6e8d8
					if (isset($utf_combining_class[$utf_char]))
d6e8d8
					{
d6e8d8
						$utf_sort[$utf_combining_class[$utf_char]][] = $utf_char;
d6e8d8
					}
d6e8d8
					else
d6e8d8
					{
d6e8d8
						if (empty($utf_sort))
d6e8d8
						{
d6e8d8
							// No combining characters... check for a composite of the two starters
d6e8d8
							if (isset($utf_canonical_comp[$starter . $utf_char]))
d6e8d8
							{
d6e8d8
								// Good ol' composite character
d6e8d8
								$starter = $utf_canonical_comp[$starter . $utf_char];
d6e8d8
							}
d6e8d8
							else if (isset($utf_jamo_type[$utf_char]))
d6e8d8
							{
d6e8d8
								// Current char is a composable jamo
d6e8d8
								if (isset($utf_jamo_type[$starter]) && $utf_jamo_type[$starter] == UNICODE_JAMO_L && $utf_jamo_type[$utf_char] == UNICODE_JAMO_V)
d6e8d8
								{
d6e8d8
									// We have a L jamo followed by a V jamo, we are going to prefetch the next char to see if it's a T jamo
d6e8d8
									if (isset($utf_jamo_type[$utf_seq[$k]]) && $utf_jamo_type[$utf_seq[$k]] == UNICODE_JAMO_T)
d6e8d8
									{
d6e8d8
										// L+V+T jamos, combine to a LVT Hangul syllable ($k is incremented)
d6e8d8
										$cp = $utf_jamo_index[$starter] + $utf_jamo_index[$utf_char] + $utf_jamo_index[$utf_seq[$k]];
d6e8d8
										++$k;
d6e8d8
									}
d6e8d8
									else
d6e8d8
									{
d6e8d8
										// L+V jamos, combine to a LV Hangul syllable
d6e8d8
										$cp = $utf_jamo_index[$starter] + $utf_jamo_index[$utf_char];
d6e8d8
									}
d6e8d8
d6e8d8
									$starter = chr(0xE0 | ($cp >> 12)) . chr(0x80 | (($cp >> 6) & 0x3F)) . chr(0x80 | ($cp & 0x3F));
d6e8d8
								}
d6e8d8
								else
d6e8d8
								{
d6e8d8
									// Non-composable jamo, just add it to the sequence
d6e8d8
									$nf_seq .= $starter;
d6e8d8
									$starter = $utf_char;
d6e8d8
								}
d6e8d8
							}
d6e8d8
							else
d6e8d8
							{
d6e8d8
								// No composite, just add the first starter to the sequence then continue with the other one
d6e8d8
								$nf_seq .= $starter;
d6e8d8
								$starter = $utf_char;
d6e8d8
							}
d6e8d8
						}
d6e8d8
						else
d6e8d8
						{
d6e8d8
							ksort($utf_sort);
d6e8d8
d6e8d8
							// For each class of combining characters
d6e8d8
							foreach ($utf_sort as $cc => $utf_chars)
d6e8d8
							{
d6e8d8
								$j = 0;
d6e8d8
d6e8d8
								do
d6e8d8
								{
d6e8d8
									// Look for a composite
d6e8d8
									if (isset($utf_canonical_comp[$starter . $utf_chars[$j]]))
d6e8d8
									{
d6e8d8
										// Found a composite, replace the starter
d6e8d8
										$starter = $utf_canonical_comp[$starter . $utf_chars[$j]];
d6e8d8
										unset($utf_sort[$cc][$j]);
d6e8d8
									}
d6e8d8
									else
d6e8d8
									{
d6e8d8
										// No composite, all following characters in that class are blocked
d6e8d8
										break;
d6e8d8
									}
d6e8d8
								}
d6e8d8
								while (isset($utf_sort[$cc][++$j]));
d6e8d8
							}
d6e8d8
d6e8d8
							// Add the starter to the normalized sequence, followed by non-starters in canonical order
d6e8d8
							$nf_seq .= $starter;
d6e8d8
d6e8d8
							foreach ($utf_sort as $utf_chars)
d6e8d8
							{
d6e8d8
								if (!empty($utf_chars))
d6e8d8
								{
d6e8d8
									$nf_seq .= implode('', $utf_chars);
d6e8d8
								}
d6e8d8
							}
d6e8d8
d6e8d8
							// Reset the array and go on
d6e8d8
							$utf_sort = array();
d6e8d8
							$starter = $utf_char;
d6e8d8
						}
d6e8d8
					}
d6e8d8
				}
d6e8d8
				while ($k <= $k_max);
d6e8d8
d6e8d8
				$tmp .= substr($str, $tmp_pos, $lpos - $tmp_pos) . $nf_seq;
d6e8d8
				$tmp_pos = $pos;
d6e8d8
			}
d6e8d8
			else
d6e8d8
			{
d6e8d8
				// Only a ASCII char can make the program get here
d6e8d8
				//
d6e8d8
				// First we skip the current byte with ++$pos, then we quickly skip following ASCII chars with strspn().
d6e8d8
				//
d6e8d8
				// The first two "if"'s here can be removed, with the consequences of being faster on latin text (lots of ASCII) and slower on
d6e8d8
				// multi-byte text (where the only ASCII chars are spaces and punctuation)
d6e8d8
				if (++$pos != $len)
d6e8d8
				{
d6e8d8
					if ($str[$pos] < "\x80")
d6e8d8
					{
d6e8d8
						$pos += strspn($str, UTF8_ASCII_RANGE, ++$pos);
d6e8d8
						$buffer[++$i & 7] = $str[$pos - 1];
d6e8d8
					}
d6e8d8
					else
d6e8d8
					{
d6e8d8
						$buffer[++$i & 7] = $c;
d6e8d8
					}
d6e8d8
				}
d6e8d8
			}
d6e8d8
		}
d6e8d8
		while ($pos < $len);
d6e8d8
d6e8d8
		// Now is time to return the string
d6e8d8
		if ($tmp_pos)
d6e8d8
		{
d6e8d8
			// If the $tmp_pos cursor is not at the beggining of the string then at least one character was not in normal form. Replace $str with the fixed version
d6e8d8
			if ($tmp_pos == $len)
d6e8d8
			{
d6e8d8
				// The $tmp_pos cursor is at the end of $str, therefore $tmp holds the whole $str
d6e8d8
				return $tmp;
d6e8d8
			}
d6e8d8
			else
d6e8d8
			{
d6e8d8
				// The rightmost chunk of $str has not been appended to $tmp yet
d6e8d8
				return $tmp . substr($str, $tmp_pos);
d6e8d8
			}
d6e8d8
		}
d6e8d8
d6e8d8
		// The string was already in normal form
d6e8d8
		return $str;
d6e8d8
	}
d6e8d8
d6e8d8
	/**
d6e8d8
	* Decompose a UTF string
d6e8d8
	*
d6e8d8
	* @param	string	$str			UTF string
d6e8d8
	* @param	integer	$pos			Position of the first UTF char (in bytes)
d6e8d8
	* @param	integer	$len			Length of the string (in bytes)
d6e8d8
	* @param	array	&$decomp_map	Decomposition mapping, passed by reference but never modified
d6e8d8
	* @return	string					The string, decomposed and sorted canonically
d6e8d8
	*
d6e8d8
	* @access	private
d6e8d8
	*/
d6e8d8
	function decompose($str, $pos, $len, &$decomp_map)
d6e8d8
	{
d6e8d8
		global $utf_combining_class;
d6e8d8
d6e8d8
		// Load some commonly-used tables
d6e8d8
		if (!isset($utf_combining_class))
d6e8d8
		{
d6e8d8
			global $phpbb_root_path, $phpEx;
d6e8d8
			include($phpbb_root_path . 'includes/utf/data/utf_normalizer_common.' . $phpEx);
d6e8d8
		}
d6e8d8
d6e8d8
		// UTF char length array
d6e8d8
		$utf_len_mask = array(
d6e8d8
			// Leading bytes masks
d6e8d8
			"\xC0" => 2, "\xD0" => 2, "\xE0" => 3, "\xF0" => 4,
d6e8d8
			// Trailing bytes masks
d6e8d8
			"\x80" => 0, "\x90" => 0, "\xA0" => 0, "\xB0" => 0
d6e8d8
		);
d6e8d8
d6e8d8
		// Some extra checks are triggered on the first byte of a UTF sequence
d6e8d8
		$extra_check = array(
d6e8d8
			"\xED" => 1, "\xEF" => 1, "\xC0" => 1, "\xC1" => 1, "\xE0" => 1, "\xF0" => 1,
d6e8d8
			"\xF4" => 1, "\xF5" => 1, "\xF6" => 1, "\xF7" => 1, "\xF8" => 1, "\xF9" => 1,
d6e8d8
			"\xFA" => 1, "\xFB" => 1, "\xFC" => 1, "\xFD" => 1, "\xFE" => 1, "\xFF" => 1
d6e8d8
		);
d6e8d8
d6e8d8
		// These masks are used to check if a UTF sequence is well formed. Here are the only 3 lengths we acknowledge:
d6e8d8
		//   - 2-byte: 110? ???? 10?? ????
d6e8d8
		//   - 3-byte: 1110 ???? 10?? ???? 10?? ????
d6e8d8
		//   - 4-byte: 1111 0??? 10?? ???? 10?? ???? 10?? ????
d6e8d8
		// Note that 5- and 6- byte sequences are automatically discarded
d6e8d8
		$utf_validation_mask = array(
d6e8d8
			2	=> "\xE0\xC0",
d6e8d8
			3	=> "\xF0\xC0\xC0",
d6e8d8
			4	=> "\xF8\xC0\xC0\xC0"
d6e8d8
		);
d6e8d8
d6e8d8
		$utf_validation_check = array(
d6e8d8
			2	=> "\xC0\x80",
d6e8d8
			3	=> "\xE0\x80\x80",
d6e8d8
			4	=> "\xF0\x80\x80\x80"
d6e8d8
		);
d6e8d8
d6e8d8
		$tmp = '';
d6e8d8
		$starter_pos = $pos;
d6e8d8
		$tmp_pos = $last_cc = $sort = $dump = 0;
d6e8d8
		$utf_sort = array();
d6e8d8
d6e8d8
d6e8d8
		// Main loop
d6e8d8
		do
d6e8d8
		{
d6e8d8
			// STEP 0: Capture the current char
d6e8d8
d6e8d8
			$cur_mask = $str[$pos] & "\xF0";
d6e8d8
			if (isset($utf_len_mask[$cur_mask]))
d6e8d8
			{
d6e8d8
				if ($utf_len = $utf_len_mask[$cur_mask])
d6e8d8
				{
d6e8d8
					// Multibyte char
d6e8d8
					$utf_char = substr($str, $pos, $utf_len);
d6e8d8
					$pos += $utf_len;
d6e8d8
				}
d6e8d8
				else
d6e8d8
				{
d6e8d8
					// A trailing byte came out of nowhere, we will treat it and all following trailing bytes as if each of them was a Unicode
d6e8d8
					// replacement char and we will advance the cursor
d6e8d8
					$spn = strspn($str, UTF8_TRAILING_BYTES, $pos);
d6e8d8
d6e8d8
					if ($dump)
d6e8d8
					{
d6e8d8
						$tmp .= substr($str, $tmp_pos, $starter_pos - $tmp_pos);
d6e8d8
d6e8d8
						// Dump combiners
d6e8d8
						if (!empty($utf_sort))
d6e8d8
						{
d6e8d8
							if ($sort)
d6e8d8
							{
d6e8d8
								ksort($utf_sort);
d6e8d8
							}
d6e8d8
d6e8d8
							foreach ($utf_sort as $utf_chars)
d6e8d8
							{
d6e8d8
								$tmp .= implode('', $utf_chars);
d6e8d8
							}
d6e8d8
						}
d6e8d8
d6e8d8
						$tmp .= str_repeat(UTF8_REPLACEMENT, $spn);
d6e8d8
						$dump = $sort = 0;
d6e8d8
					}
d6e8d8
					else
d6e8d8
					{
d6e8d8
						$tmp .= substr($str, $tmp_pos, $pos - $tmp_pos) . str_repeat(UTF8_REPLACEMENT, $spn);
d6e8d8
					}
d6e8d8
d6e8d8
					$pos += $spn;
d6e8d8
					$tmp_pos = $starter_pos = $pos;
d6e8d8
d6e8d8
					$utf_sort = array();
d6e8d8
					$last_cc = 0;
d6e8d8
d6e8d8
					continue;
d6e8d8
				}
d6e8d8
d6e8d8
d6e8d8
				// STEP 1: Decide what to do with current char
d6e8d8
d6e8d8
				// Now, in that order:
d6e8d8
				//  - check if that character is decomposable
d6e8d8
				//  - check if that character is a non-starter
d6e8d8
				//  - check if that character requires extra checks to be performed
d6e8d8
				if (isset($decomp_map[$utf_char]))
d6e8d8
				{
d6e8d8
					// Decompose the char
d6e8d8
					$_pos = 0;
d6e8d8
					$_len = strlen($decomp_map[$utf_char]);
d6e8d8
d6e8d8
					do
d6e8d8
					{
d6e8d8
						$c = $decomp_map[$utf_char][$_pos];
d6e8d8
						$_utf_len =& $utf_len_mask[$c & "\xF0"];
d6e8d8
d6e8d8
						if (isset($_utf_len))
d6e8d8
						{
d6e8d8
							$_utf_char = substr($decomp_map[$utf_char], $_pos, $_utf_len);
d6e8d8
							$_pos += $_utf_len;
d6e8d8
d6e8d8
							if (isset($utf_combining_class[$_utf_char]))
d6e8d8
							{
d6e8d8
								// The character decomposed to a non-starter, buffer it for sorting
d6e8d8
								$utf_sort[$utf_combining_class[$_utf_char]][] = $_utf_char;
d6e8d8
d6e8d8
								if ($utf_combining_class[$_utf_char] < $last_cc)
d6e8d8
								{
d6e8d8
									// Not canonically ordered, will require sorting
d6e8d8
									$sort = $dump = 1;
d6e8d8
								}
d6e8d8
								else
d6e8d8
								{
d6e8d8
									$dump = 1;
d6e8d8
									$last_cc = $utf_combining_class[$_utf_char];
d6e8d8
								}
d6e8d8
							}
d6e8d8
							else
d6e8d8
							{
d6e8d8
								// This character decomposition contains a starter, dump the buffer and continue
d6e8d8
								if ($dump)
d6e8d8
								{
d6e8d8
									$tmp .= substr($str, $tmp_pos, $starter_pos - $tmp_pos);
d6e8d8
d6e8d8
									// Dump combiners
d6e8d8
									if (!empty($utf_sort))
d6e8d8
									{
d6e8d8
										if ($sort)
d6e8d8
										{
d6e8d8
											ksort($utf_sort);
d6e8d8
										}
d6e8d8
d6e8d8
										foreach ($utf_sort as $utf_chars)
d6e8d8
										{
d6e8d8
											$tmp .= implode('', $utf_chars);
d6e8d8
										}
d6e8d8
									}
d6e8d8
d6e8d8
									$tmp .= $_utf_char;
d6e8d8
									$dump = $sort = 0;
d6e8d8
								}
d6e8d8
								else
d6e8d8
								{
d6e8d8
									$tmp .= substr($str, $tmp_pos, $starter_pos - $tmp_pos) . $_utf_char;
d6e8d8
								}
d6e8d8
d6e8d8
								$tmp_pos = $starter_pos = $pos;
d6e8d8
								$utf_sort = array();
d6e8d8
								$last_cc = 0;
d6e8d8
							}
d6e8d8
						}
d6e8d8
						else
d6e8d8
						{
d6e8d8
							// This character decomposition contains an ASCII char, which is a starter. Dump the buffer and continue
d6e8d8
							++$_pos;
d6e8d8
d6e8d8
							if ($dump)
d6e8d8
							{
d6e8d8
								$tmp .= substr($str, $tmp_pos, $starter_pos - $tmp_pos);
d6e8d8
d6e8d8
								// Dump combiners
d6e8d8
								if (!empty($utf_sort))
d6e8d8
								{
d6e8d8
									if ($sort)
d6e8d8
									{
d6e8d8
										ksort($utf_sort);
d6e8d8
									}
d6e8d8
d6e8d8
									foreach ($utf_sort as $utf_chars)
d6e8d8
									{
d6e8d8
										$tmp .= implode('', $utf_chars);
d6e8d8
									}
d6e8d8
								}
d6e8d8
d6e8d8
								$tmp .= $c;
d6e8d8
								$dump = $sort = 0;
d6e8d8
							}
d6e8d8
							else
d6e8d8
							{
d6e8d8
								$tmp .= substr($str, $tmp_pos, $pos - $utf_len - $tmp_pos) . $c;
d6e8d8
							}
d6e8d8
d6e8d8
							$tmp_pos = $starter_pos = $pos;
d6e8d8
							$utf_sort = array();
d6e8d8
							$last_cc = 0;
d6e8d8
						}
d6e8d8
					}
d6e8d8
					while ($_pos < $_len);
d6e8d8
				}
d6e8d8
				else if (isset($utf_combining_class[$utf_char]))
d6e8d8
				{
d6e8d8
					// Combining character
d6e8d8
					if ($utf_combining_class[$utf_char] < $last_cc)
d6e8d8
					{
d6e8d8
						// Not in canonical order
d6e8d8
						$sort = $dump = 1;
d6e8d8
					}
d6e8d8
					else
d6e8d8
					{
d6e8d8
						$last_cc = $utf_combining_class[$utf_char];
d6e8d8
					}
d6e8d8
d6e8d8
					$utf_sort[$utf_combining_class[$utf_char]][] = $utf_char;
d6e8d8
				}
d6e8d8
				else
d6e8d8
				{
d6e8d8
					// Non-decomposable starter, check out if it's a Hangul syllable
d6e8d8
					if ($utf_char < UTF8_HANGUL_FIRST || $utf_char > UTF8_HANGUL_LAST)
d6e8d8
					{
d6e8d8
						// Nope, regular UTF char, check that we have the correct number of trailing bytes
d6e8d8
						if (($utf_char & $utf_validation_mask[$utf_len]) != $utf_validation_check[$utf_len])
d6e8d8
						{
d6e8d8
							// Current char isn't well-formed or legal: either one or several trailing bytes are missing, or the Unicode char
d6e8d8
							// has been encoded in a five- or six- byte sequence.
d6e8d8
							// Move the cursor back to its original position then advance it to the position it should really be at
d6e8d8
							$pos -= $utf_len;
d6e8d8
							$tmp .= substr($str, $tmp_pos, $starter_pos - $tmp_pos);
d6e8d8
d6e8d8
							if (!empty($utf_sort))
d6e8d8
							{
d6e8d8
								ksort($utf_sort);
d6e8d8
d6e8d8
								foreach ($utf_sort as $utf_chars)
d6e8d8
								{
d6e8d8
									$tmp .= implode('', $utf_chars);
d6e8d8
								}
d6e8d8
								$utf_sort = array();
d6e8d8
							}
d6e8d8
d6e8d8
							// Add a replacement char then another replacement char for every trailing byte.
d6e8d8
							//
d6e8d8
							// @todo I'm not entirely sure that's how we're supposed to mark invalidated byte sequences, check this
d6e8d8
							$spn = strspn($str, UTF8_TRAILING_BYTES, ++$pos);
d6e8d8
							$tmp .= str_repeat(UTF8_REPLACEMENT, $spn + 1);
d6e8d8
d6e8d8
							$dump = $sort = 0;
d6e8d8
d6e8d8
							$pos += $spn;
d6e8d8
							$tmp_pos = $pos;
d6e8d8
							continue;
d6e8d8
						}
d6e8d8
d6e8d8
						if (isset($extra_check[$utf_char[0]]))
d6e8d8
						{
d6e8d8
							switch ($utf_char[0])
d6e8d8
							{
d6e8d8
								// Note: 0xED is quite common in Korean
d6e8d8
								case "\xED":
d6e8d8
									if ($utf_char >= "\xED\xA0\x80")
d6e8d8
									{
d6e8d8
										// Surrogates (U+D800..U+DFFF) are not allowed in UTF-8 (UTF sequence 0xEDA080..0xEDBFBF)
d6e8d8
										$tmp .= substr($str, $tmp_pos, $starter_pos - $tmp_pos);
d6e8d8
d6e8d8
										if (!empty($utf_sort))
d6e8d8
										{
d6e8d8
											ksort($utf_sort);
d6e8d8
d6e8d8
											foreach ($utf_sort as $utf_chars)
d6e8d8
											{
d6e8d8
												$tmp .= implode('', $utf_chars);
d6e8d8
											}
d6e8d8
											$utf_sort = array();
d6e8d8
										}
d6e8d8
d6e8d8
										$tmp .= UTF8_REPLACEMENT;
d6e8d8
										$dump = $sort = 0;
d6e8d8
d6e8d8
										$tmp_pos = $starter_pos = $pos;
d6e8d8
										continue 2;
d6e8d8
									}
d6e8d8
								break;
d6e8d8
d6e8d8
								// Note: 0xEF is quite common in Japanese
d6e8d8
								case "\xEF":
d6e8d8
									if ($utf_char == "\xEF\xBF\xBE" || $utf_char == "\xEF\xBF\xBF")
d6e8d8
									{
d6e8d8
										// U+FFFE and U+FFFF are explicitly disallowed (UTF sequence 0xEFBFBE..0xEFBFBF)
d6e8d8
										$tmp .= substr($str, $tmp_pos, $starter_pos - $tmp_pos);
d6e8d8
d6e8d8
										if (!empty($utf_sort))
d6e8d8
										{
d6e8d8
											ksort($utf_sort);
d6e8d8
d6e8d8
											foreach ($utf_sort as $utf_chars)
d6e8d8
											{
d6e8d8
												$tmp .= implode('', $utf_chars);
d6e8d8
											}
d6e8d8
											$utf_sort = array();
d6e8d8
										}
d6e8d8
d6e8d8
										$tmp .= UTF8_REPLACEMENT;
d6e8d8
										$dump = $sort = 0;
d6e8d8
d6e8d8
										$tmp_pos = $starter_pos = $pos;
d6e8d8
										continue 2;
d6e8d8
									}
d6e8d8
								break;
d6e8d8
d6e8d8
								case "\xC0":
d6e8d8
								case "\xC1":
d6e8d8
									if ($utf_char <= "\xC1\xBF")
d6e8d8
									{
d6e8d8
										// Overlong sequence: Unicode char U+0000..U+007F encoded as a double-byte UTF char
d6e8d8
										$tmp .= substr($str, $tmp_pos, $starter_pos - $tmp_pos);
d6e8d8
d6e8d8
										if (!empty($utf_sort))
d6e8d8
										{
d6e8d8
											ksort($utf_sort);
d6e8d8
d6e8d8
											foreach ($utf_sort as $utf_chars)
d6e8d8
											{
d6e8d8
												$tmp .= implode('', $utf_chars);
d6e8d8
											}
d6e8d8
											$utf_sort = array();
d6e8d8
										}
d6e8d8
d6e8d8
										$tmp .= UTF8_REPLACEMENT;
d6e8d8
										$dump = $sort = 0;
d6e8d8
d6e8d8
										$tmp_pos = $starter_pos = $pos;
d6e8d8
										continue 2;
d6e8d8
									}
d6e8d8
								break;
d6e8d8
d6e8d8
								case "\xE0":
d6e8d8
									if ($utf_char <= "\xE0\x9F\xBF")
d6e8d8
									{
d6e8d8
										// Unicode char U+0000..U+07FF encoded in 3 bytes
d6e8d8
										$tmp .= substr($str, $tmp_pos, $starter_pos - $tmp_pos);
d6e8d8
d6e8d8
										if (!empty($utf_sort))
d6e8d8
										{
d6e8d8
											ksort($utf_sort);
d6e8d8
d6e8d8
											foreach ($utf_sort as $utf_chars)
d6e8d8
											{
d6e8d8
												$tmp .= implode('', $utf_chars);
d6e8d8
											}
d6e8d8
											$utf_sort = array();
d6e8d8
										}
d6e8d8
d6e8d8
										$tmp .= UTF8_REPLACEMENT;
d6e8d8
										$dump = $sort = 0;
d6e8d8
d6e8d8
										$tmp_pos = $starter_pos = $pos;
d6e8d8
										continue 2;
d6e8d8
									}
d6e8d8
								break;
d6e8d8
d6e8d8
								case "\xF0":
d6e8d8
									if ($utf_char <= "\xF0\x8F\xBF\xBF")
d6e8d8
									{
d6e8d8
										// Unicode char U+0000..U+FFFF encoded in 4 bytes
d6e8d8
										$tmp .= substr($str, $tmp_pos, $starter_pos - $tmp_pos);
d6e8d8
d6e8d8
										if (!empty($utf_sort))
d6e8d8
										{
d6e8d8
											ksort($utf_sort);
d6e8d8
d6e8d8
											foreach ($utf_sort as $utf_chars)
d6e8d8
											{
d6e8d8
												$tmp .= implode('', $utf_chars);
d6e8d8
											}
d6e8d8
											$utf_sort = array();
d6e8d8
										}
d6e8d8
d6e8d8
										$tmp .= UTF8_REPLACEMENT;
d6e8d8
										$dump = $sort = 0;
d6e8d8
d6e8d8
										$tmp_pos = $starter_pos = $pos;
d6e8d8
										continue 2;
d6e8d8
									}
d6e8d8
								break;
d6e8d8
d6e8d8
								default:
d6e8d8
									if ($utf_char > UTF8_MAX)
d6e8d8
									{
d6e8d8
										// Out of the Unicode range
d6e8d8
										$tmp .= substr($str, $tmp_pos, $starter_pos - $tmp_pos);
d6e8d8
d6e8d8
										if (!empty($utf_sort))
d6e8d8
										{
d6e8d8
											ksort($utf_sort);
d6e8d8
d6e8d8
											foreach ($utf_sort as $utf_chars)
d6e8d8
											{
d6e8d8
												$tmp .= implode('', $utf_chars);
d6e8d8
											}
d6e8d8
											$utf_sort = array();
d6e8d8
										}
d6e8d8
d6e8d8
										$tmp .= UTF8_REPLACEMENT;
d6e8d8
										$dump = $sort = 0;
d6e8d8
d6e8d8
										$tmp_pos = $starter_pos = $pos;
d6e8d8
										continue 2;
d6e8d8
									}
d6e8d8
								break;
d6e8d8
							}
d6e8d8
						}
d6e8d8
					}
d6e8d8
					else
d6e8d8
					{
d6e8d8
						// Hangul syllable
d6e8d8
						$idx = (((ord($utf_char[0]) & 0x0F) << 12) | ((ord($utf_char[1]) & 0x3F) << 6) | (ord($utf_char[2]) & 0x3F)) - UNICODE_HANGUL_SBASE;
d6e8d8
d6e8d8
						// LIndex can only range from 0 to 18, therefore it cannot influence the first two bytes of the L Jamo, which allows us to hardcode them (based on LBase).
d6e8d8
						//
d6e8d8
						// The same goes for VIndex, but for TIndex there's a catch: the value of the third byte could exceed 0xBF and we would have to increment the second byte
d6e8d8
						if ($t_index = $idx % UNICODE_HANGUL_TCOUNT)
d6e8d8
						{
d6e8d8
							if ($t_index < 25)
d6e8d8
							{
d6e8d8
								$utf_char = "\xE1\x84\x00\xE1\x85\x00\xE1\x86\x00";
d6e8d8
								$utf_char[8] = chr(0xA7 + $t_index);
d6e8d8
							}
d6e8d8
							else
d6e8d8
							{
d6e8d8
								$utf_char = "\xE1\x84\x00\xE1\x85\x00\xE1\x87\x00";
d6e8d8
								$utf_char[8] = chr(0x67 + $t_index);
d6e8d8
							}
d6e8d8
						}
d6e8d8
						else
d6e8d8
						{
d6e8d8
							$utf_char = "\xE1\x84\x00\xE1\x85\x00";
d6e8d8
						}
d6e8d8
d6e8d8
						$utf_char[2] = chr(0x80 + (int) ($idx / UNICODE_HANGUL_NCOUNT));
d6e8d8
						$utf_char[5] = chr(0xA1 + (int) (($idx % UNICODE_HANGUL_NCOUNT) / UNICODE_HANGUL_TCOUNT));
d6e8d8
d6e8d8
						// Just like other decompositions, the resulting Jamos must be dumped to the tmp string
d6e8d8
						$dump = 1;
d6e8d8
					}
d6e8d8
d6e8d8
					// Do we need to dump stuff to the tmp string?
d6e8d8
					if ($dump)
d6e8d8
					{
d6e8d8
						$tmp .= substr($str, $tmp_pos, $starter_pos - $tmp_pos);
d6e8d8
d6e8d8
						// Dump combiners
d6e8d8
						if (!empty($utf_sort))
d6e8d8
						{
d6e8d8
							if ($sort)
d6e8d8
							{
d6e8d8
								ksort($utf_sort);
d6e8d8
							}
d6e8d8
d6e8d8
							foreach ($utf_sort as $utf_chars)
d6e8d8
							{
d6e8d8
								$tmp .= implode('', $utf_chars);
d6e8d8
							}
d6e8d8
						}
d6e8d8
d6e8d8
						$tmp .= $utf_char;
d6e8d8
						$dump = $sort = 0;
d6e8d8
						$tmp_pos = $pos;
d6e8d8
					}
d6e8d8
d6e8d8
					$last_cc = 0;
d6e8d8
					$utf_sort = array();
d6e8d8
					$starter_pos = $pos;
d6e8d8
				}
d6e8d8
			}
d6e8d8
			else
d6e8d8
			{
d6e8d8
				// ASCII char, which happens to be a starter (as any other ASCII char)
d6e8d8
				if ($dump)
d6e8d8
				{
d6e8d8
					$tmp .= substr($str, $tmp_pos, $starter_pos - $tmp_pos);
d6e8d8
d6e8d8
					// Dump combiners
d6e8d8
					if (!empty($utf_sort))
d6e8d8
					{
d6e8d8
						if ($sort)
d6e8d8
						{
d6e8d8
							ksort($utf_sort);
d6e8d8
						}
d6e8d8
d6e8d8
						foreach ($utf_sort as $utf_chars)
d6e8d8
						{
d6e8d8
							$tmp .= implode('', $utf_chars);
d6e8d8
						}
d6e8d8
					}
d6e8d8
d6e8d8
					$tmp .= $str[$pos];
d6e8d8
					$dump = $sort = 0;
d6e8d8
					$tmp_pos = ++$pos;
d6e8d8
d6e8d8
					$pos += strspn($str, UTF8_ASCII_RANGE, $pos);
d6e8d8
				}
d6e8d8
				else
d6e8d8
				{
d6e8d8
					$pos += strspn($str, UTF8_ASCII_RANGE, ++$pos);
d6e8d8
				}
d6e8d8
d6e8d8
				$last_cc = 0;
d6e8d8
				$utf_sort = array();
d6e8d8
				$starter_pos = $pos;
d6e8d8
			}
d6e8d8
		}
d6e8d8
		while ($pos < $len);
d6e8d8
d6e8d8
		// Now is time to return the string
d6e8d8
		if ($dump)
d6e8d8
		{
d6e8d8
			$tmp .= substr($str, $tmp_pos, $starter_pos - $tmp_pos);
d6e8d8
d6e8d8
			// Dump combiners
d6e8d8
			if (!empty($utf_sort))
d6e8d8
			{
d6e8d8
				if ($sort)
d6e8d8
				{
d6e8d8
					ksort($utf_sort);
d6e8d8
				}
d6e8d8
d6e8d8
				foreach ($utf_sort as $utf_chars)
d6e8d8
				{
d6e8d8
					$tmp .= implode('', $utf_chars);
d6e8d8
				}
d6e8d8
			}
d6e8d8
d6e8d8
			return $tmp;
d6e8d8
		}
d6e8d8
		else if ($tmp_pos)
d6e8d8
		{
d6e8d8
			// If the $tmp_pos cursor was moved then at least one character was not in normal form. Replace $str with the fixed version
d6e8d8
			if ($tmp_pos == $len)
d6e8d8
			{
d6e8d8
				// The $tmp_pos cursor is at the end of $str, therefore $tmp holds the whole $str
d6e8d8
				return $tmp;
d6e8d8
			}
d6e8d8
			else
d6e8d8
			{
d6e8d8
				// The rightmost chunk of $str has not been appended to $tmp yet
d6e8d8
				return $tmp . substr($str, $tmp_pos);
d6e8d8
			}
d6e8d8
		}
d6e8d8
d6e8d8
		// The string was already in normal form
d6e8d8
		return $str;
d6e8d8
	}
d6e8d8
}
d6e8d8
d6e8d8
?>