includes/utf/utf_normalizer.php
changeset 0 ceef6d64c21d
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/includes/utf/utf_normalizer.php	Wed Mar 07 16:41:10 2012 +0400
     1.3 @@ -0,0 +1,1516 @@
     1.4 +<?php
     1.5 +/**
     1.6 +*
     1.7 +* @package utf
     1.8 +* @version $Id$
     1.9 +* @copyright (c) 2005 phpBB Group
    1.10 +* @license http://opensource.org/licenses/gpl-license.php GNU Public License
    1.11 +*
    1.12 +*/
    1.13 +
    1.14 +/**
    1.15 +*/
    1.16 +if (!defined('IN_PHPBB'))
    1.17 +{
    1.18 +	exit;
    1.19 +}
    1.20 +
    1.21 +/**
    1.22 +* Some Unicode characters encoded in UTF-8
    1.23 +*
    1.24 +* Preserved for compatibility
    1.25 +*/
    1.26 +define('UTF8_REPLACEMENT', "\xEF\xBF\xBD");
    1.27 +define('UTF8_MAX', "\xF4\x8F\xBF\xBF");
    1.28 +define('UTF8_FFFE', "\xEF\xBF\xBE");
    1.29 +define('UTF8_FFFF', "\xEF\xBF\xBF");
    1.30 +define('UTF8_SURROGATE_FIRST', "\xED\xA0\x80");
    1.31 +define('UTF8_SURROGATE_LAST', "\xED\xBF\xBF");
    1.32 +define('UTF8_HANGUL_FIRST', "\xEA\xB0\x80");
    1.33 +define('UTF8_HANGUL_LAST', "\xED\x9E\xA3");
    1.34 +
    1.35 +define('UTF8_CJK_FIRST', "\xE4\xB8\x80");
    1.36 +define('UTF8_CJK_LAST', "\xE9\xBE\xBB");
    1.37 +define('UTF8_CJK_B_FIRST', "\xF0\xA0\x80\x80");
    1.38 +define('UTF8_CJK_B_LAST', "\xF0\xAA\x9B\x96");
    1.39 +
    1.40 +// Unset global variables
    1.41 +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']);
    1.42 +
    1.43 +// NFC_QC and NFKC_QC values
    1.44 +define('UNICODE_QC_MAYBE', 0);
    1.45 +define('UNICODE_QC_NO', 1);
    1.46 +
    1.47 +// Contains all the ASCII characters appearing in UTF-8, sorted by frequency
    1.48 +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");
    1.49 +
    1.50 +// Contains all the tail bytes that can appear in the composition of a UTF-8 char
    1.51 +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");
    1.52 +
    1.53 +// Constants used by the Hangul [de]composition algorithms
    1.54 +define('UNICODE_HANGUL_SBASE', 0xAC00);
    1.55 +define('UNICODE_HANGUL_LBASE', 0x1100);
    1.56 +define('UNICODE_HANGUL_VBASE', 0x1161);
    1.57 +define('UNICODE_HANGUL_TBASE', 0x11A7);
    1.58 +define('UNICODE_HANGUL_SCOUNT', 11172);
    1.59 +define('UNICODE_HANGUL_LCOUNT', 19);
    1.60 +define('UNICODE_HANGUL_VCOUNT', 21);
    1.61 +define('UNICODE_HANGUL_TCOUNT', 28);
    1.62 +define('UNICODE_HANGUL_NCOUNT', 588);
    1.63 +define('UNICODE_JAMO_L', 0);
    1.64 +define('UNICODE_JAMO_V', 1);
    1.65 +define('UNICODE_JAMO_T', 2);
    1.66 +
    1.67 +/**
    1.68 +* Unicode normalization routines
    1.69 +*
    1.70 +* @package utf
    1.71 +*/
    1.72 +class utf_normalizer
    1.73 +{
    1.74 +	/**
    1.75 +	* Validate, cleanup and normalize a string
    1.76 +	*
    1.77 +	* The ultimate convenience function! Clean up invalid UTF-8 sequences,
    1.78 +	* and convert to Normal Form C, canonical composition.
    1.79 +	*
    1.80 +	* @param	string	&$str	The dirty string
    1.81 +	* @return	string			The same string, all shiny and cleaned-up
    1.82 +	*/
    1.83 +	function cleanup(&$str)
    1.84 +	{
    1.85 +		// The string below is the list of all autorized characters, sorted by frequency in latin text
    1.86 +		$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");
    1.87 +		$len = strlen($str);
    1.88 +
    1.89 +		if ($pos == $len)
    1.90 +		{
    1.91 +			// ASCII strings with no special chars return immediately
    1.92 +			return;
    1.93 +		}
    1.94 +
    1.95 +		// Note: we do not check for $GLOBALS['utf_canonical_decomp']. It is assumed they are always loaded together
    1.96 +		if (!isset($GLOBALS['utf_nfc_qc']))
    1.97 +		{
    1.98 +			global $phpbb_root_path, $phpEx;
    1.99 +			include($phpbb_root_path . 'includes/utf/data/utf_nfc_qc.' . $phpEx);
   1.100 +		}
   1.101 +
   1.102 +		if (!isset($GLOBALS['utf_canonical_decomp']))
   1.103 +		{
   1.104 +			global $phpbb_root_path, $phpEx;
   1.105 +			include($phpbb_root_path . 'includes/utf/data/utf_canonical_decomp.' . $phpEx);
   1.106 +		}
   1.107 +
   1.108 +		// Replace any byte in the range 0x00..0x1F, except for \r, \n and \t
   1.109 +		// 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
   1.110 +		$str = strtr(
   1.111 +			$str,
   1.112 +			"\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",
   1.113 +			"\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"
   1.114 +		);
   1.115 +
   1.116 +		$str = utf_normalizer::recompose($str, $pos, $len, $GLOBALS['utf_nfc_qc'], $GLOBALS['utf_canonical_decomp']);
   1.117 +	}
   1.118 +
   1.119 +	/**
   1.120 +	* Validate and normalize a UTF string to NFC
   1.121 +	*
   1.122 +	* @param	string	&$str	Unchecked UTF string
   1.123 +	* @return	string			The string, validated and in normal form
   1.124 +	*/
   1.125 +	function nfc(&$str)
   1.126 +	{
   1.127 +		$pos = strspn($str, UTF8_ASCII_RANGE);
   1.128 +		$len = strlen($str);
   1.129 +
   1.130 +		if ($pos == $len)
   1.131 +		{
   1.132 +			// ASCII strings return immediately
   1.133 +			return;
   1.134 +		}
   1.135 +
   1.136 +		if (!isset($GLOBALS['utf_nfc_qc']))
   1.137 +		{
   1.138 +			global $phpbb_root_path, $phpEx;
   1.139 +			include($phpbb_root_path . 'includes/utf/data/utf_nfc_qc.' . $phpEx);
   1.140 +		}
   1.141 +
   1.142 +		if (!isset($GLOBALS['utf_canonical_decomp']))
   1.143 +		{
   1.144 +			global $phpbb_root_path, $phpEx;
   1.145 +			include($phpbb_root_path . 'includes/utf/data/utf_canonical_decomp.' . $phpEx);
   1.146 +		}
   1.147 +
   1.148 +		$str = utf_normalizer::recompose($str, $pos, $len, $GLOBALS['utf_nfc_qc'], $GLOBALS['utf_canonical_decomp']);
   1.149 +	}
   1.150 +
   1.151 +	/**
   1.152 +	* Validate and normalize a UTF string to NFKC
   1.153 +	*
   1.154 +	* @param	string	&$str	Unchecked UTF string
   1.155 +	* @return	string			The string, validated and in normal form
   1.156 +	*/
   1.157 +	function nfkc(&$str)
   1.158 +	{
   1.159 +		$pos = strspn($str, UTF8_ASCII_RANGE);
   1.160 +		$len = strlen($str);
   1.161 +
   1.162 +		if ($pos == $len)
   1.163 +		{
   1.164 +			// ASCII strings return immediately
   1.165 +			return;
   1.166 +		}
   1.167 +
   1.168 +		if (!isset($GLOBALS['utf_nfkc_qc']))
   1.169 +		{
   1.170 +			global $phpbb_root_path, $phpEx;
   1.171 +			include($phpbb_root_path . 'includes/utf/data/utf_nfkc_qc.' . $phpEx);
   1.172 +		}
   1.173 +
   1.174 +		if (!isset($GLOBALS['utf_compatibility_decomp']))
   1.175 +		{
   1.176 +			global $phpbb_root_path, $phpEx;
   1.177 +			include($phpbb_root_path . 'includes/utf/data/utf_compatibility_decomp.' . $phpEx);
   1.178 +		}
   1.179 +
   1.180 +		$str = utf_normalizer::recompose($str, $pos, $len, $GLOBALS['utf_nfkc_qc'], $GLOBALS['utf_compatibility_decomp']);
   1.181 +	}
   1.182 +
   1.183 +	/**
   1.184 +	* Validate and normalize a UTF string to NFD
   1.185 +	*
   1.186 +	* @param	string	&$str	Unchecked UTF string
   1.187 +	* @return	string			The string, validated and in normal form
   1.188 +	*/
   1.189 +	function nfd(&$str)
   1.190 +	{
   1.191 +		$pos = strspn($str, UTF8_ASCII_RANGE);
   1.192 +		$len = strlen($str);
   1.193 +
   1.194 +		if ($pos == $len)
   1.195 +		{
   1.196 +			// ASCII strings return immediately
   1.197 +			return;
   1.198 +		}
   1.199 +
   1.200 +		if (!isset($GLOBALS['utf_canonical_decomp']))
   1.201 +		{
   1.202 +			global $phpbb_root_path, $phpEx;
   1.203 +			include($phpbb_root_path . 'includes/utf/data/utf_canonical_decomp.' . $phpEx);
   1.204 +		}
   1.205 +
   1.206 +		$str = utf_normalizer::decompose($str, $pos, $len, $GLOBALS['utf_canonical_decomp']);
   1.207 +	}
   1.208 +
   1.209 +	/**
   1.210 +	* Validate and normalize a UTF string to NFKD
   1.211 +	*
   1.212 +	* @param	string	&$str	Unchecked UTF string
   1.213 +	* @return	string			The string, validated and in normal form
   1.214 +	*/
   1.215 +	function nfkd(&$str)
   1.216 +	{
   1.217 +		$pos = strspn($str, UTF8_ASCII_RANGE);
   1.218 +		$len = strlen($str);
   1.219 +
   1.220 +		if ($pos == $len)
   1.221 +		{
   1.222 +			// ASCII strings return immediately
   1.223 +			return;
   1.224 +		}
   1.225 +
   1.226 +		if (!isset($GLOBALS['utf_compatibility_decomp']))
   1.227 +		{
   1.228 +			global $phpbb_root_path, $phpEx;
   1.229 +			include($phpbb_root_path . 'includes/utf/data/utf_compatibility_decomp.' . $phpEx);
   1.230 +		}
   1.231 +
   1.232 +		$str = utf_normalizer::decompose($str, $pos, $len, $GLOBALS['utf_compatibility_decomp']);
   1.233 +	}
   1.234 +
   1.235 +
   1.236 +	/**
   1.237 +	* Recompose a UTF string
   1.238 +	*
   1.239 +	* @param	string	$str			Unchecked UTF string
   1.240 +	* @param	integer	$pos			Position of the first UTF char (in bytes)
   1.241 +	* @param	integer	$len			Length of the string (in bytes)
   1.242 +	* @param	array	&$qc			Quick-check array, passed by reference but never modified
   1.243 +	* @param	array	&$decomp_map	Decomposition mapping, passed by reference but never modified
   1.244 +	* @return	string					The string, validated and recomposed
   1.245 +	*
   1.246 +	* @access	private
   1.247 +	*/
   1.248 +	function recompose($str, $pos, $len, &$qc, &$decomp_map)
   1.249 +	{
   1.250 +		global $utf_combining_class, $utf_canonical_comp, $utf_jamo_type, $utf_jamo_index;
   1.251 +
   1.252 +		// Load some commonly-used tables
   1.253 +		if (!isset($utf_jamo_index, $utf_jamo_type, $utf_combining_class))
   1.254 +		{
   1.255 +			global $phpbb_root_path, $phpEx;
   1.256 +			include($phpbb_root_path . 'includes/utf/data/utf_normalizer_common.' . $phpEx);
   1.257 +		}
   1.258 +
   1.259 +		// Load the canonical composition table
   1.260 +		if (!isset($utf_canonical_comp))
   1.261 +		{
   1.262 +			global $phpbb_root_path, $phpEx;
   1.263 +			include($phpbb_root_path . 'includes/utf/data/utf_canonical_comp.' . $phpEx);
   1.264 +		}
   1.265 +
   1.266 +		// Buffer the last ASCII char before the UTF-8 stuff if applicable
   1.267 +		$tmp = '';
   1.268 +		$i = $tmp_pos = $last_cc = 0;
   1.269 +
   1.270 +		$buffer = ($pos) ? array(++$i => $str[$pos - 1]) : array();
   1.271 +
   1.272 +		// UTF char length array
   1.273 +		// This array is used to determine the length of a UTF character.
   1.274 +		// Be $c the result of ($str[$pos] & "\xF0") --where $str is the string we're operating on and $pos
   1.275 +		// the position of the cursor--, if $utf_len_mask[$c] does not exist, the byte is an ASCII char.
   1.276 +		// Otherwise, if $utf_len_mask[$c] is greater than 0, we have a the leading byte of a multibyte character
   1.277 +		// whose length is $utf_len_mask[$c] and if it is equal to 0, the byte is a trailing byte.
   1.278 +		$utf_len_mask = array(
   1.279 +			// Leading bytes masks
   1.280 +			"\xC0" => 2, "\xD0" => 2, "\xE0" => 3, "\xF0" => 4,
   1.281 +			// Trailing bytes masks
   1.282 +			"\x80" => 0, "\x90" => 0, "\xA0" => 0, "\xB0" => 0
   1.283 +		);
   1.284 +
   1.285 +		$extra_check = array(
   1.286 +			"\xED" => 1, "\xEF" => 1, "\xC0" => 1, "\xC1" => 1, "\xE0" => 1, "\xF0" => 1,
   1.287 +			"\xF4" => 1, "\xF5" => 1, "\xF6" => 1, "\xF7" => 1, "\xF8" => 1, "\xF9" => 1,
   1.288 +			"\xFA" => 1, "\xFB" => 1, "\xFC" => 1, "\xFD" => 1, "\xFE" => 1, "\xFF" => 1
   1.289 +		);
   1.290 +
   1.291 +		$utf_validation_mask = array(
   1.292 +			2	=> "\xE0\xC0",
   1.293 +			3	=> "\xF0\xC0\xC0",
   1.294 +			4	=> "\xF8\xC0\xC0\xC0"
   1.295 +		);
   1.296 +
   1.297 +		$utf_validation_check = array(
   1.298 +			2	=> "\xC0\x80",
   1.299 +			3	=> "\xE0\x80\x80",
   1.300 +			4	=> "\xF0\x80\x80\x80"
   1.301 +		);
   1.302 +
   1.303 +		// Main loop
   1.304 +		do
   1.305 +		{
   1.306 +			// STEP 0: Capture the current char and buffer it
   1.307 +			$c = $str[$pos];
   1.308 +			$c_mask = $c & "\xF0";
   1.309 +
   1.310 +			if (isset($utf_len_mask[$c_mask]))
   1.311 +			{
   1.312 +				// Byte at $pos is either a leading byte or a missplaced trailing byte
   1.313 +				if ($utf_len = $utf_len_mask[$c_mask])
   1.314 +				{
   1.315 +					// Capture the char
   1.316 +					$buffer[++$i & 7] = $utf_char = substr($str, $pos, $utf_len);
   1.317 +
   1.318 +					// Let's find out if a thorough check is needed
   1.319 +					if (isset($qc[$utf_char]))
   1.320 +					{
   1.321 +						// 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
   1.322 +					}
   1.323 +					else if (isset($utf_combining_class[$utf_char]))
   1.324 +					{
   1.325 +						if ($utf_combining_class[$utf_char] < $last_cc)
   1.326 +						{
   1.327 +							// A combining character that is NOT canonically ordered
   1.328 +						}
   1.329 +						else
   1.330 +						{
   1.331 +							// A combining character that IS canonically ordered, skip to the next char
   1.332 +							$last_cc = $utf_combining_class[$utf_char];
   1.333 +
   1.334 +							$pos += $utf_len;
   1.335 +							continue;
   1.336 +						}
   1.337 +					}
   1.338 +					else
   1.339 +					{
   1.340 +						// 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.
   1.341 +						// It can be a singleton, a canonical composite, a replacement char or an even an ill-formed bunch of bytes. Let's find out
   1.342 +						$last_cc = 0;
   1.343 +
   1.344 +						// Check that we have the correct number of trailing bytes
   1.345 +						if (($utf_char & $utf_validation_mask[$utf_len]) != $utf_validation_check[$utf_len])
   1.346 +						{
   1.347 +							// Current char isn't well-formed or legal: either one or several trailing bytes are missing, or the Unicode char
   1.348 +							// has been encoded in a five- or six- byte sequence
   1.349 +							if ($utf_char[0] >= "\xF8")
   1.350 +							{
   1.351 +								if ($utf_char[0] < "\xFC")
   1.352 +								{
   1.353 +									$trailing_bytes = 4;
   1.354 +								}
   1.355 +								else if ($utf_char[0] > "\xFD")
   1.356 +								{
   1.357 +									$trailing_bytes = 0;
   1.358 +								}
   1.359 +								else
   1.360 +								{
   1.361 +									$trailing_bytes = 5;
   1.362 +								}
   1.363 +							}
   1.364 +							else
   1.365 +							{
   1.366 +								$trailing_bytes = $utf_len - 1;
   1.367 +							}
   1.368 +
   1.369 +							$tmp .= substr($str, $tmp_pos, $pos - $tmp_pos) . UTF8_REPLACEMENT;
   1.370 +							$pos += strspn($str, UTF8_TRAILING_BYTES, ++$pos, $trailing_bytes);
   1.371 +							$tmp_pos = $pos;
   1.372 +
   1.373 +							continue;
   1.374 +						}
   1.375 +
   1.376 +						if (isset($extra_check[$c]))
   1.377 +						{
   1.378 +							switch ($c)
   1.379 +							{
   1.380 +								// Note: 0xED is quite common in Korean
   1.381 +								case "\xED":
   1.382 +									if ($utf_char >= "\xED\xA0\x80")
   1.383 +									{
   1.384 +										// Surrogates (U+D800..U+DFFF) are not allowed in UTF-8 (UTF sequence 0xEDA080..0xEDBFBF)
   1.385 +										$tmp .= substr($str, $tmp_pos, $pos - $tmp_pos) . UTF8_REPLACEMENT;
   1.386 +										$pos += $utf_len;
   1.387 +										$tmp_pos = $pos;
   1.388 +										continue 2;
   1.389 +									}
   1.390 +								break;
   1.391 +
   1.392 +								// Note: 0xEF is quite common in Japanese
   1.393 +								case "\xEF":
   1.394 +									if ($utf_char == "\xEF\xBF\xBE" || $utf_char == "\xEF\xBF\xBF")
   1.395 +									{
   1.396 +										// U+FFFE and U+FFFF are explicitly disallowed (UTF sequence 0xEFBFBE..0xEFBFBF)
   1.397 +										$tmp .= substr($str, $tmp_pos, $pos - $tmp_pos) . UTF8_REPLACEMENT;
   1.398 +										$pos += $utf_len;
   1.399 +										$tmp_pos = $pos;
   1.400 +										continue 2;
   1.401 +									}
   1.402 +								break;
   1.403 +
   1.404 +								case "\xC0":
   1.405 +								case "\xC1":
   1.406 +									if ($utf_char <= "\xC1\xBF")
   1.407 +									{
   1.408 +										// Overlong sequence: Unicode char U+0000..U+007F encoded as a double-byte UTF char
   1.409 +										$tmp .= substr($str, $tmp_pos, $pos - $tmp_pos) . UTF8_REPLACEMENT;
   1.410 +										$pos += $utf_len;
   1.411 +										$tmp_pos = $pos;
   1.412 +										continue 2;
   1.413 +									}
   1.414 +								break;
   1.415 +
   1.416 +								case "\xE0":
   1.417 +									if ($utf_char <= "\xE0\x9F\xBF")
   1.418 +									{
   1.419 +										// Unicode char U+0000..U+07FF encoded in 3 bytes
   1.420 +										$tmp .= substr($str, $tmp_pos, $pos - $tmp_pos) . UTF8_REPLACEMENT;
   1.421 +										$pos += $utf_len;
   1.422 +										$tmp_pos = $pos;
   1.423 +										continue 2;
   1.424 +									}
   1.425 +								break;
   1.426 +
   1.427 +								case "\xF0":
   1.428 +									if ($utf_char <= "\xF0\x8F\xBF\xBF")
   1.429 +									{
   1.430 +										// Unicode char U+0000..U+FFFF encoded in 4 bytes
   1.431 +										$tmp .= substr($str, $tmp_pos, $pos - $tmp_pos) . UTF8_REPLACEMENT;
   1.432 +										$pos += $utf_len;
   1.433 +										$tmp_pos = $pos;
   1.434 +										continue 2;
   1.435 +									}
   1.436 +								break;
   1.437 +
   1.438 +								default:
   1.439 +									// Five- and six- byte sequences do not need being checked for here anymore
   1.440 +									if ($utf_char > UTF8_MAX)
   1.441 +									{
   1.442 +										// Out of the Unicode range
   1.443 +										if ($utf_char[0] < "\xF8")
   1.444 +										{
   1.445 +											$trailing_bytes = 3;
   1.446 +										}
   1.447 +										else if ($utf_char[0] < "\xFC")
   1.448 +										{
   1.449 +											$trailing_bytes = 4;
   1.450 +										}
   1.451 +										else if ($utf_char[0] > "\xFD")
   1.452 +										{
   1.453 +											$trailing_bytes = 0;
   1.454 +										}
   1.455 +										else
   1.456 +										{
   1.457 +											$trailing_bytes = 5;
   1.458 +										}
   1.459 +
   1.460 +										$tmp .= substr($str, $tmp_pos, $pos - $tmp_pos) . UTF8_REPLACEMENT;
   1.461 +										$pos += strspn($str, UTF8_TRAILING_BYTES, ++$pos, $trailing_bytes);
   1.462 +										$tmp_pos = $pos;
   1.463 +										continue 2;
   1.464 +									}
   1.465 +								break;
   1.466 +							}
   1.467 +						}
   1.468 +
   1.469 +						// The char is a valid starter, move the cursor and go on
   1.470 +						$pos += $utf_len;
   1.471 +						continue;
   1.472 +					}
   1.473 +				}
   1.474 +				else
   1.475 +				{
   1.476 +					// A trailing byte came out of nowhere, we will advance the cursor and treat the this byte and all following trailing bytes as if
   1.477 +					// each of them was a Unicode replacement char
   1.478 +					$spn = strspn($str, UTF8_TRAILING_BYTES, $pos);
   1.479 +					$tmp .= substr($str, $tmp_pos, $pos - $tmp_pos) . str_repeat(UTF8_REPLACEMENT, $spn);
   1.480 +
   1.481 +					$pos += $spn;
   1.482 +					$tmp_pos = $pos;
   1.483 +					continue;
   1.484 +				}
   1.485 +
   1.486 +
   1.487 +				// STEP 1: Decompose current char
   1.488 +
   1.489 +				// We have found a character that is either:
   1.490 +				//  - in the NFC_QC/NFKC_QC list
   1.491 +				//  - a non-starter char that is not canonically ordered
   1.492 +				//
   1.493 +				// We are going to capture the shortest UTF sequence that satisfies these two conditions:
   1.494 +				//
   1.495 +				//  1 - If the sequence does not start at the begginning of the string, it must begin with a starter,
   1.496 +				// and that starter must not have the NF[K]C_QC property equal to "MAYBE"
   1.497 +				//
   1.498 +				//  2 - If the sequence does not end at the end of the string, it must end with a non-starter and be
   1.499 +				// immediately followed by a starter that is not on the QC list
   1.500 +				//
   1.501 +				$utf_seq = array();
   1.502 +				$last_cc = 0;
   1.503 +				$lpos = $pos;
   1.504 +				$pos += $utf_len;
   1.505 +
   1.506 +				if (isset($decomp_map[$utf_char]))
   1.507 +				{
   1.508 +					$_pos = 0;
   1.509 +					$_len = strlen($decomp_map[$utf_char]);
   1.510 +
   1.511 +					do
   1.512 +					{
   1.513 +						$_utf_len =& $utf_len_mask[$decomp_map[$utf_char][$_pos] & "\xF0"];
   1.514 +
   1.515 +						if (isset($_utf_len))
   1.516 +						{
   1.517 +							$utf_seq[] = substr($decomp_map[$utf_char], $_pos, $_utf_len);
   1.518 +							$_pos += $_utf_len;
   1.519 +						}
   1.520 +						else
   1.521 +						{
   1.522 +							$utf_seq[] = $decomp_map[$utf_char][$_pos];
   1.523 +							++$_pos;
   1.524 +						}
   1.525 +					}
   1.526 +					while ($_pos < $_len);
   1.527 +				}
   1.528 +				else
   1.529 +				{
   1.530 +					// The char is not decomposable
   1.531 +					$utf_seq = array($utf_char);
   1.532 +				}
   1.533 +
   1.534 +
   1.535 +				// STEP 2: Capture the starter
   1.536 +
   1.537 +				// Check out the combining class of the first character of the UTF sequence
   1.538 +				$k = 0;
   1.539 +				if (isset($utf_combining_class[$utf_seq[0]]) || $qc[$utf_char] == UNICODE_QC_MAYBE)
   1.540 +				{
   1.541 +					// Not a starter, inspect previous characters
   1.542 +					// The last 8 characters are kept in a buffer so that we don't have to capture them everytime.
   1.543 +					// This is enough for all real-life strings but even if it wasn't, we can capture characters in backward mode,
   1.544 +					// although it is slower than this method.
   1.545 +					//
   1.546 +					// In the following loop, $j starts at the previous buffered character ($i - 1, because current character is
   1.547 +					// at offset $i) and process them in backward mode until we find a starter.
   1.548 +					//
   1.549 +					// $k is the index on each UTF character inside of our UTF sequence. At this time, $utf_seq contains one or more
   1.550 +					// characters numbered 0 to n. $k starts at 0 and for each char we prepend we pre-decrement it and for numbering
   1.551 +					$starter_found = 0;
   1.552 +					$j_min = max(1, $i - 7);
   1.553 +
   1.554 +					for ($j = $i - 1; $j >= $j_min && $lpos > $tmp_pos; --$j)
   1.555 +					{
   1.556 +						$utf_char = $buffer[$j & 7];
   1.557 +						$lpos -= strlen($utf_char);
   1.558 +
   1.559 +						if (isset($decomp_map[$utf_char]))
   1.560 +						{
   1.561 +							// The char is a composite, decompose for storage
   1.562 +							$decomp_seq = array();
   1.563 +							$_pos = 0;
   1.564 +							$_len = strlen($decomp_map[$utf_char]);
   1.565 +
   1.566 +							do
   1.567 +							{
   1.568 +								$c = $decomp_map[$utf_char][$_pos];
   1.569 +								$_utf_len =& $utf_len_mask[$c & "\xF0"];
   1.570 +
   1.571 +								if (isset($_utf_len))
   1.572 +								{
   1.573 +									$decomp_seq[] = substr($decomp_map[$utf_char], $_pos, $_utf_len);
   1.574 +									$_pos += $_utf_len;
   1.575 +								}
   1.576 +								else
   1.577 +								{
   1.578 +									$decomp_seq[] = $c;
   1.579 +									++$_pos;
   1.580 +								}
   1.581 +							}
   1.582 +							while ($_pos < $_len);
   1.583 +
   1.584 +							// Prepend the UTF sequence with our decomposed sequence
   1.585 +							if (isset($decomp_seq[1]))
   1.586 +							{
   1.587 +								// The char expanded into several chars
   1.588 +								$decomp_cnt = sizeof($decomp_seq);
   1.589 +
   1.590 +								foreach ($decomp_seq as $decomp_i => $decomp_char)
   1.591 +								{
   1.592 +									$utf_seq[$k + $decomp_i - $decomp_cnt] = $decomp_char;
   1.593 +								}
   1.594 +								$k -= $decomp_cnt;
   1.595 +							}
   1.596 +							else
   1.597 +							{
   1.598 +								// Decomposed to a single char, easier to prepend
   1.599 +								$utf_seq[--$k] = $decomp_seq[0];
   1.600 +							}
   1.601 +						}
   1.602 +						else
   1.603 +						{
   1.604 +							$utf_seq[--$k] = $utf_char;
   1.605 +						}
   1.606 +
   1.607 +						if (!isset($utf_combining_class[$utf_seq[$k]]))
   1.608 +						{
   1.609 +							// We have found our starter
   1.610 +							$starter_found = 1;
   1.611 +							break;
   1.612 +						}
   1.613 +					}
   1.614 +
   1.615 +					if (!$starter_found && $lpos > $tmp_pos)
   1.616 +					{
   1.617 +						// The starter was not found in the buffer, let's rewind some more
   1.618 +						do
   1.619 +						{
   1.620 +							// $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.
   1.621 +							$c = $str[--$lpos];
   1.622 +							$c_mask = $c & "\xF0";
   1.623 +
   1.624 +							if (isset($utf_len_mask[$c_mask]))
   1.625 +							{
   1.626 +								// UTF byte
   1.627 +								if ($utf_len = $utf_len_mask[$c_mask])
   1.628 +								{
   1.629 +									// UTF *leading* byte
   1.630 +									$utf_char = substr($str, $lpos, $utf_len);
   1.631 +
   1.632 +									if (isset($decomp_map[$utf_char]))
   1.633 +									{
   1.634 +										// Decompose the character
   1.635 +										$decomp_seq = array();
   1.636 +										$_pos = 0;
   1.637 +										$_len = strlen($decomp_map[$utf_char]);
   1.638 +
   1.639 +										do
   1.640 +										{
   1.641 +											$c = $decomp_map[$utf_char][$_pos];
   1.642 +											$_utf_len =& $utf_len_mask[$c & "\xF0"];
   1.643 +
   1.644 +											if (isset($_utf_len))
   1.645 +											{
   1.646 +												$decomp_seq[] = substr($decomp_map[$utf_char], $_pos, $_utf_len);
   1.647 +												$_pos += $_utf_len;
   1.648 +											}
   1.649 +											else
   1.650 +											{
   1.651 +												$decomp_seq[] = $c;
   1.652 +												++$_pos;
   1.653 +											}
   1.654 +										}
   1.655 +										while ($_pos < $_len);
   1.656 +
   1.657 +										// Prepend the UTF sequence with our decomposed sequence
   1.658 +										if (isset($decomp_seq[1]))
   1.659 +										{
   1.660 +											// The char expanded into several chars
   1.661 +											$decomp_cnt = sizeof($decomp_seq);
   1.662 +											foreach ($decomp_seq as $decomp_i => $utf_char)
   1.663 +											{
   1.664 +												$utf_seq[$k + $decomp_i - $decomp_cnt] = $utf_char;
   1.665 +											}
   1.666 +											$k -= $decomp_cnt;
   1.667 +										}
   1.668 +										else
   1.669 +										{
   1.670 +											// Decomposed to a single char, easier to prepend
   1.671 +											$utf_seq[--$k] = $decomp_seq[0];
   1.672 +										}
   1.673 +									}
   1.674 +									else
   1.675 +									{
   1.676 +										$utf_seq[--$k] = $utf_char;
   1.677 +									}
   1.678 +								}
   1.679 +							}
   1.680 +							else
   1.681 +							{
   1.682 +								// ASCII char
   1.683 +								$utf_seq[--$k] = $c;
   1.684 +							}
   1.685 +						}
   1.686 +						while ($lpos > $tmp_pos);
   1.687 +					}
   1.688 +				}
   1.689 +
   1.690 +
   1.691 +				// STEP 3: Capture following combining modifiers
   1.692 +
   1.693 +				while ($pos < $len)
   1.694 +				{
   1.695 +					$c_mask = $str[$pos] & "\xF0";
   1.696 +
   1.697 +					if (isset($utf_len_mask[$c_mask]))
   1.698 +					{
   1.699 +						if ($utf_len = $utf_len_mask[$c_mask])
   1.700 +						{
   1.701 +							$utf_char = substr($str, $pos, $utf_len);
   1.702 +						}
   1.703 +						else
   1.704 +						{
   1.705 +							// A trailing byte came out of nowhere
   1.706 +							// Trailing bytes are replaced with Unicode replacement chars, we will just ignore it for now, break out of the loop
   1.707 +							// as if it was a starter (replacement chars ARE starters) and let the next loop replace it
   1.708 +							break;
   1.709 +						}
   1.710 +
   1.711 +						if (isset($utf_combining_class[$utf_char]) || isset($qc[$utf_char]))
   1.712 +						{
   1.713 +							// Combining character, add it to the sequence and move the cursor
   1.714 +							if (isset($decomp_map[$utf_char]))
   1.715 +							{
   1.716 +								// Decompose the character
   1.717 +								$_pos = 0;
   1.718 +								$_len = strlen($decomp_map[$utf_char]);
   1.719 +
   1.720 +								do
   1.721 +								{
   1.722 +									$c = $decomp_map[$utf_char][$_pos];
   1.723 +									$_utf_len =& $utf_len_mask[$c & "\xF0"];
   1.724 +
   1.725 +									if (isset($_utf_len))
   1.726 +									{
   1.727 +										$utf_seq[] = substr($decomp_map[$utf_char], $_pos, $_utf_len);
   1.728 +										$_pos += $_utf_len;
   1.729 +									}
   1.730 +									else
   1.731 +									{
   1.732 +										$utf_seq[] = $c;
   1.733 +										++$_pos;
   1.734 +									}
   1.735 +								}
   1.736 +								while ($_pos < $_len);
   1.737 +							}
   1.738 +							else
   1.739 +							{
   1.740 +								$utf_seq[] = $utf_char;
   1.741 +							}
   1.742 +
   1.743 +							$pos += $utf_len;
   1.744 +						}
   1.745 +						else
   1.746 +						{
   1.747 +							// Combining class 0 and no QC, break out of the loop
   1.748 +							// Note: we do not know if that character is valid. If it's not, the next iteration will replace it
   1.749 +							break;
   1.750 +						}
   1.751 +					}
   1.752 +					else
   1.753 +					{
   1.754 +						// ASCII chars are starters
   1.755 +						break;
   1.756 +					}
   1.757 +				}
   1.758 +
   1.759 +
   1.760 +				// STEP 4: Sort and combine
   1.761 +
   1.762 +				// Here we sort...
   1.763 +				$k_max = $k + sizeof($utf_seq);
   1.764 +
   1.765 +				if (!$k && $k_max == 1)
   1.766 +				{
   1.767 +					// There is only one char in the UTF sequence, add it then jump to the next iteration of main loop
   1.768 +						// Note: the two commented lines below can be enabled under PHP5 for a very small performance gain in most cases
   1.769 +//						if (substr_compare($str, $utf_seq[0], $lpos, $pos - $lpos))
   1.770 +//						{
   1.771 +						$tmp .= substr($str, $tmp_pos, $lpos - $tmp_pos) . $utf_seq[0];
   1.772 +						$tmp_pos = $pos;
   1.773 +//						}
   1.774 +
   1.775 +					continue;
   1.776 +				}
   1.777 +
   1.778 +				// ...there we combine
   1.779 +				if (isset($utf_combining_class[$utf_seq[$k]]))
   1.780 +				{
   1.781 +					$starter = $nf_seq = '';
   1.782 +				}
   1.783 +				else
   1.784 +				{
   1.785 +					$starter = $utf_seq[$k++];
   1.786 +					$nf_seq = '';
   1.787 +				}
   1.788 +				$utf_sort = array();
   1.789 +
   1.790 +				// 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
   1.791 +				// at the end of the string without altering it
   1.792 +				$utf_seq[] = '';
   1.793 +
   1.794 +				do
   1.795 +				{
   1.796 +					$utf_char = $utf_seq[$k++];
   1.797 +
   1.798 +					if (isset($utf_combining_class[$utf_char]))
   1.799 +					{
   1.800 +						$utf_sort[$utf_combining_class[$utf_char]][] = $utf_char;
   1.801 +					}
   1.802 +					else
   1.803 +					{
   1.804 +						if (empty($utf_sort))
   1.805 +						{
   1.806 +							// No combining characters... check for a composite of the two starters
   1.807 +							if (isset($utf_canonical_comp[$starter . $utf_char]))
   1.808 +							{
   1.809 +								// Good ol' composite character
   1.810 +								$starter = $utf_canonical_comp[$starter . $utf_char];
   1.811 +							}
   1.812 +							else if (isset($utf_jamo_type[$utf_char]))
   1.813 +							{
   1.814 +								// Current char is a composable jamo
   1.815 +								if (isset($utf_jamo_type[$starter]) && $utf_jamo_type[$starter] == UNICODE_JAMO_L && $utf_jamo_type[$utf_char] == UNICODE_JAMO_V)
   1.816 +								{
   1.817 +									// 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
   1.818 +									if (isset($utf_jamo_type[$utf_seq[$k]]) && $utf_jamo_type[$utf_seq[$k]] == UNICODE_JAMO_T)
   1.819 +									{
   1.820 +										// L+V+T jamos, combine to a LVT Hangul syllable ($k is incremented)
   1.821 +										$cp = $utf_jamo_index[$starter] + $utf_jamo_index[$utf_char] + $utf_jamo_index[$utf_seq[$k]];
   1.822 +										++$k;
   1.823 +									}
   1.824 +									else
   1.825 +									{
   1.826 +										// L+V jamos, combine to a LV Hangul syllable
   1.827 +										$cp = $utf_jamo_index[$starter] + $utf_jamo_index[$utf_char];
   1.828 +									}
   1.829 +
   1.830 +									$starter = chr(0xE0 | ($cp >> 12)) . chr(0x80 | (($cp >> 6) & 0x3F)) . chr(0x80 | ($cp & 0x3F));
   1.831 +								}
   1.832 +								else
   1.833 +								{
   1.834 +									// Non-composable jamo, just add it to the sequence
   1.835 +									$nf_seq .= $starter;
   1.836 +									$starter = $utf_char;
   1.837 +								}
   1.838 +							}
   1.839 +							else
   1.840 +							{
   1.841 +								// No composite, just add the first starter to the sequence then continue with the other one
   1.842 +								$nf_seq .= $starter;
   1.843 +								$starter = $utf_char;
   1.844 +							}
   1.845 +						}
   1.846 +						else
   1.847 +						{
   1.848 +							ksort($utf_sort);
   1.849 +
   1.850 +							// For each class of combining characters
   1.851 +							foreach ($utf_sort as $cc => $utf_chars)
   1.852 +							{
   1.853 +								$j = 0;
   1.854 +
   1.855 +								do
   1.856 +								{
   1.857 +									// Look for a composite
   1.858 +									if (isset($utf_canonical_comp[$starter . $utf_chars[$j]]))
   1.859 +									{
   1.860 +										// Found a composite, replace the starter
   1.861 +										$starter = $utf_canonical_comp[$starter . $utf_chars[$j]];
   1.862 +										unset($utf_sort[$cc][$j]);
   1.863 +									}
   1.864 +									else
   1.865 +									{
   1.866 +										// No composite, all following characters in that class are blocked
   1.867 +										break;
   1.868 +									}
   1.869 +								}
   1.870 +								while (isset($utf_sort[$cc][++$j]));
   1.871 +							}
   1.872 +
   1.873 +							// Add the starter to the normalized sequence, followed by non-starters in canonical order
   1.874 +							$nf_seq .= $starter;
   1.875 +
   1.876 +							foreach ($utf_sort as $utf_chars)
   1.877 +							{
   1.878 +								if (!empty($utf_chars))
   1.879 +								{
   1.880 +									$nf_seq .= implode('', $utf_chars);
   1.881 +								}
   1.882 +							}
   1.883 +
   1.884 +							// Reset the array and go on
   1.885 +							$utf_sort = array();
   1.886 +							$starter = $utf_char;
   1.887 +						}
   1.888 +					}
   1.889 +				}
   1.890 +				while ($k <= $k_max);
   1.891 +
   1.892 +				$tmp .= substr($str, $tmp_pos, $lpos - $tmp_pos) . $nf_seq;
   1.893 +				$tmp_pos = $pos;
   1.894 +			}
   1.895 +			else
   1.896 +			{
   1.897 +				// Only a ASCII char can make the program get here
   1.898 +				//
   1.899 +				// First we skip the current byte with ++$pos, then we quickly skip following ASCII chars with strspn().
   1.900 +				//
   1.901 +				// The first two "if"'s here can be removed, with the consequences of being faster on latin text (lots of ASCII) and slower on
   1.902 +				// multi-byte text (where the only ASCII chars are spaces and punctuation)
   1.903 +				if (++$pos != $len)
   1.904 +				{
   1.905 +					if ($str[$pos] < "\x80")
   1.906 +					{
   1.907 +						$pos += strspn($str, UTF8_ASCII_RANGE, ++$pos);
   1.908 +						$buffer[++$i & 7] = $str[$pos - 1];
   1.909 +					}
   1.910 +					else
   1.911 +					{
   1.912 +						$buffer[++$i & 7] = $c;
   1.913 +					}
   1.914 +				}
   1.915 +			}
   1.916 +		}
   1.917 +		while ($pos < $len);
   1.918 +
   1.919 +		// Now is time to return the string
   1.920 +		if ($tmp_pos)
   1.921 +		{
   1.922 +			// 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
   1.923 +			if ($tmp_pos == $len)
   1.924 +			{
   1.925 +				// The $tmp_pos cursor is at the end of $str, therefore $tmp holds the whole $str
   1.926 +				return $tmp;
   1.927 +			}
   1.928 +			else
   1.929 +			{
   1.930 +				// The rightmost chunk of $str has not been appended to $tmp yet
   1.931 +				return $tmp . substr($str, $tmp_pos);
   1.932 +			}
   1.933 +		}
   1.934 +
   1.935 +		// The string was already in normal form
   1.936 +		return $str;
   1.937 +	}
   1.938 +
   1.939 +	/**
   1.940 +	* Decompose a UTF string
   1.941 +	*
   1.942 +	* @param	string	$str			UTF string
   1.943 +	* @param	integer	$pos			Position of the first UTF char (in bytes)
   1.944 +	* @param	integer	$len			Length of the string (in bytes)
   1.945 +	* @param	array	&$decomp_map	Decomposition mapping, passed by reference but never modified
   1.946 +	* @return	string					The string, decomposed and sorted canonically
   1.947 +	*
   1.948 +	* @access	private
   1.949 +	*/
   1.950 +	function decompose($str, $pos, $len, &$decomp_map)
   1.951 +	{
   1.952 +		global $utf_combining_class;
   1.953 +
   1.954 +		// Load some commonly-used tables
   1.955 +		if (!isset($utf_combining_class))
   1.956 +		{
   1.957 +			global $phpbb_root_path, $phpEx;
   1.958 +			include($phpbb_root_path . 'includes/utf/data/utf_normalizer_common.' . $phpEx);
   1.959 +		}
   1.960 +
   1.961 +		// UTF char length array
   1.962 +		$utf_len_mask = array(
   1.963 +			// Leading bytes masks
   1.964 +			"\xC0" => 2, "\xD0" => 2, "\xE0" => 3, "\xF0" => 4,
   1.965 +			// Trailing bytes masks
   1.966 +			"\x80" => 0, "\x90" => 0, "\xA0" => 0, "\xB0" => 0
   1.967 +		);
   1.968 +
   1.969 +		// Some extra checks are triggered on the first byte of a UTF sequence
   1.970 +		$extra_check = array(
   1.971 +			"\xED" => 1, "\xEF" => 1, "\xC0" => 1, "\xC1" => 1, "\xE0" => 1, "\xF0" => 1,
   1.972 +			"\xF4" => 1, "\xF5" => 1, "\xF6" => 1, "\xF7" => 1, "\xF8" => 1, "\xF9" => 1,
   1.973 +			"\xFA" => 1, "\xFB" => 1, "\xFC" => 1, "\xFD" => 1, "\xFE" => 1, "\xFF" => 1
   1.974 +		);
   1.975 +
   1.976 +		// These masks are used to check if a UTF sequence is well formed. Here are the only 3 lengths we acknowledge:
   1.977 +		//   - 2-byte: 110? ???? 10?? ????
   1.978 +		//   - 3-byte: 1110 ???? 10?? ???? 10?? ????
   1.979 +		//   - 4-byte: 1111 0??? 10?? ???? 10?? ???? 10?? ????
   1.980 +		// Note that 5- and 6- byte sequences are automatically discarded
   1.981 +		$utf_validation_mask = array(
   1.982 +			2	=> "\xE0\xC0",
   1.983 +			3	=> "\xF0\xC0\xC0",
   1.984 +			4	=> "\xF8\xC0\xC0\xC0"
   1.985 +		);
   1.986 +
   1.987 +		$utf_validation_check = array(
   1.988 +			2	=> "\xC0\x80",
   1.989 +			3	=> "\xE0\x80\x80",
   1.990 +			4	=> "\xF0\x80\x80\x80"
   1.991 +		);
   1.992 +
   1.993 +		$tmp = '';
   1.994 +		$starter_pos = $pos;
   1.995 +		$tmp_pos = $last_cc = $sort = $dump = 0;
   1.996 +		$utf_sort = array();
   1.997 +
   1.998 +
   1.999 +		// Main loop
  1.1000 +		do
  1.1001 +		{
  1.1002 +			// STEP 0: Capture the current char
  1.1003 +
  1.1004 +			$cur_mask = $str[$pos] & "\xF0";
  1.1005 +			if (isset($utf_len_mask[$cur_mask]))
  1.1006 +			{
  1.1007 +				if ($utf_len = $utf_len_mask[$cur_mask])
  1.1008 +				{
  1.1009 +					// Multibyte char
  1.1010 +					$utf_char = substr($str, $pos, $utf_len);
  1.1011 +					$pos += $utf_len;
  1.1012 +				}
  1.1013 +				else
  1.1014 +				{
  1.1015 +					// A trailing byte came out of nowhere, we will treat it and all following trailing bytes as if each of them was a Unicode
  1.1016 +					// replacement char and we will advance the cursor
  1.1017 +					$spn = strspn($str, UTF8_TRAILING_BYTES, $pos);
  1.1018 +
  1.1019 +					if ($dump)
  1.1020 +					{
  1.1021 +						$tmp .= substr($str, $tmp_pos, $starter_pos - $tmp_pos);
  1.1022 +
  1.1023 +						// Dump combiners
  1.1024 +						if (!empty($utf_sort))
  1.1025 +						{
  1.1026 +							if ($sort)
  1.1027 +							{
  1.1028 +								ksort($utf_sort);
  1.1029 +							}
  1.1030 +
  1.1031 +							foreach ($utf_sort as $utf_chars)
  1.1032 +							{
  1.1033 +								$tmp .= implode('', $utf_chars);
  1.1034 +							}
  1.1035 +						}
  1.1036 +
  1.1037 +						$tmp .= str_repeat(UTF8_REPLACEMENT, $spn);
  1.1038 +						$dump = $sort = 0;
  1.1039 +					}
  1.1040 +					else
  1.1041 +					{
  1.1042 +						$tmp .= substr($str, $tmp_pos, $pos - $tmp_pos) . str_repeat(UTF8_REPLACEMENT, $spn);
  1.1043 +					}
  1.1044 +
  1.1045 +					$pos += $spn;
  1.1046 +					$tmp_pos = $starter_pos = $pos;
  1.1047 +
  1.1048 +					$utf_sort = array();
  1.1049 +					$last_cc = 0;
  1.1050 +
  1.1051 +					continue;
  1.1052 +				}
  1.1053 +
  1.1054 +
  1.1055 +				// STEP 1: Decide what to do with current char
  1.1056 +
  1.1057 +				// Now, in that order:
  1.1058 +				//  - check if that character is decomposable
  1.1059 +				//  - check if that character is a non-starter
  1.1060 +				//  - check if that character requires extra checks to be performed
  1.1061 +				if (isset($decomp_map[$utf_char]))
  1.1062 +				{
  1.1063 +					// Decompose the char
  1.1064 +					$_pos = 0;
  1.1065 +					$_len = strlen($decomp_map[$utf_char]);
  1.1066 +
  1.1067 +					do
  1.1068 +					{
  1.1069 +						$c = $decomp_map[$utf_char][$_pos];
  1.1070 +						$_utf_len =& $utf_len_mask[$c & "\xF0"];
  1.1071 +
  1.1072 +						if (isset($_utf_len))
  1.1073 +						{
  1.1074 +							$_utf_char = substr($decomp_map[$utf_char], $_pos, $_utf_len);
  1.1075 +							$_pos += $_utf_len;
  1.1076 +
  1.1077 +							if (isset($utf_combining_class[$_utf_char]))
  1.1078 +							{
  1.1079 +								// The character decomposed to a non-starter, buffer it for sorting
  1.1080 +								$utf_sort[$utf_combining_class[$_utf_char]][] = $_utf_char;
  1.1081 +
  1.1082 +								if ($utf_combining_class[$_utf_char] < $last_cc)
  1.1083 +								{
  1.1084 +									// Not canonically ordered, will require sorting
  1.1085 +									$sort = $dump = 1;
  1.1086 +								}
  1.1087 +								else
  1.1088 +								{
  1.1089 +									$dump = 1;
  1.1090 +									$last_cc = $utf_combining_class[$_utf_char];
  1.1091 +								}
  1.1092 +							}
  1.1093 +							else
  1.1094 +							{
  1.1095 +								// This character decomposition contains a starter, dump the buffer and continue
  1.1096 +								if ($dump)
  1.1097 +								{
  1.1098 +									$tmp .= substr($str, $tmp_pos, $starter_pos - $tmp_pos);
  1.1099 +
  1.1100 +									// Dump combiners
  1.1101 +									if (!empty($utf_sort))
  1.1102 +									{
  1.1103 +										if ($sort)
  1.1104 +										{
  1.1105 +											ksort($utf_sort);
  1.1106 +										}
  1.1107 +
  1.1108 +										foreach ($utf_sort as $utf_chars)
  1.1109 +										{
  1.1110 +											$tmp .= implode('', $utf_chars);
  1.1111 +										}
  1.1112 +									}
  1.1113 +
  1.1114 +									$tmp .= $_utf_char;
  1.1115 +									$dump = $sort = 0;
  1.1116 +								}
  1.1117 +								else
  1.1118 +								{
  1.1119 +									$tmp .= substr($str, $tmp_pos, $starter_pos - $tmp_pos) . $_utf_char;
  1.1120 +								}
  1.1121 +
  1.1122 +								$tmp_pos = $starter_pos = $pos;
  1.1123 +								$utf_sort = array();
  1.1124 +								$last_cc = 0;
  1.1125 +							}
  1.1126 +						}
  1.1127 +						else
  1.1128 +						{
  1.1129 +							// This character decomposition contains an ASCII char, which is a starter. Dump the buffer and continue
  1.1130 +							++$_pos;
  1.1131 +
  1.1132 +							if ($dump)
  1.1133 +							{
  1.1134 +								$tmp .= substr($str, $tmp_pos, $starter_pos - $tmp_pos);
  1.1135 +
  1.1136 +								// Dump combiners
  1.1137 +								if (!empty($utf_sort))
  1.1138 +								{
  1.1139 +									if ($sort)
  1.1140 +									{
  1.1141 +										ksort($utf_sort);
  1.1142 +									}
  1.1143 +
  1.1144 +									foreach ($utf_sort as $utf_chars)
  1.1145 +									{
  1.1146 +										$tmp .= implode('', $utf_chars);
  1.1147 +									}
  1.1148 +								}
  1.1149 +
  1.1150 +								$tmp .= $c;
  1.1151 +								$dump = $sort = 0;
  1.1152 +							}
  1.1153 +							else
  1.1154 +							{
  1.1155 +								$tmp .= substr($str, $tmp_pos, $pos - $utf_len - $tmp_pos) . $c;
  1.1156 +							}
  1.1157 +
  1.1158 +							$tmp_pos = $starter_pos = $pos;
  1.1159 +							$utf_sort = array();
  1.1160 +							$last_cc = 0;
  1.1161 +						}
  1.1162 +					}
  1.1163 +					while ($_pos < $_len);
  1.1164 +				}
  1.1165 +				else if (isset($utf_combining_class[$utf_char]))
  1.1166 +				{
  1.1167 +					// Combining character
  1.1168 +					if ($utf_combining_class[$utf_char] < $last_cc)
  1.1169 +					{
  1.1170 +						// Not in canonical order
  1.1171 +						$sort = $dump = 1;
  1.1172 +					}
  1.1173 +					else
  1.1174 +					{
  1.1175 +						$last_cc = $utf_combining_class[$utf_char];
  1.1176 +					}
  1.1177 +
  1.1178 +					$utf_sort[$utf_combining_class[$utf_char]][] = $utf_char;
  1.1179 +				}
  1.1180 +				else
  1.1181 +				{
  1.1182 +					// Non-decomposable starter, check out if it's a Hangul syllable
  1.1183 +					if ($utf_char < UTF8_HANGUL_FIRST || $utf_char > UTF8_HANGUL_LAST)
  1.1184 +					{
  1.1185 +						// Nope, regular UTF char, check that we have the correct number of trailing bytes
  1.1186 +						if (($utf_char & $utf_validation_mask[$utf_len]) != $utf_validation_check[$utf_len])
  1.1187 +						{
  1.1188 +							// Current char isn't well-formed or legal: either one or several trailing bytes are missing, or the Unicode char
  1.1189 +							// has been encoded in a five- or six- byte sequence.
  1.1190 +							// Move the cursor back to its original position then advance it to the position it should really be at
  1.1191 +							$pos -= $utf_len;
  1.1192 +							$tmp .= substr($str, $tmp_pos, $starter_pos - $tmp_pos);
  1.1193 +
  1.1194 +							if (!empty($utf_sort))
  1.1195 +							{
  1.1196 +								ksort($utf_sort);
  1.1197 +
  1.1198 +								foreach ($utf_sort as $utf_chars)
  1.1199 +								{
  1.1200 +									$tmp .= implode('', $utf_chars);
  1.1201 +								}
  1.1202 +								$utf_sort = array();
  1.1203 +							}
  1.1204 +
  1.1205 +							// Add a replacement char then another replacement char for every trailing byte.
  1.1206 +							//
  1.1207 +							// @todo I'm not entirely sure that's how we're supposed to mark invalidated byte sequences, check this
  1.1208 +							$spn = strspn($str, UTF8_TRAILING_BYTES, ++$pos);
  1.1209 +							$tmp .= str_repeat(UTF8_REPLACEMENT, $spn + 1);
  1.1210 +
  1.1211 +							$dump = $sort = 0;
  1.1212 +
  1.1213 +							$pos += $spn;
  1.1214 +							$tmp_pos = $pos;
  1.1215 +							continue;
  1.1216 +						}
  1.1217 +
  1.1218 +						if (isset($extra_check[$utf_char[0]]))
  1.1219 +						{
  1.1220 +							switch ($utf_char[0])
  1.1221 +							{
  1.1222 +								// Note: 0xED is quite common in Korean
  1.1223 +								case "\xED":
  1.1224 +									if ($utf_char >= "\xED\xA0\x80")
  1.1225 +									{
  1.1226 +										// Surrogates (U+D800..U+DFFF) are not allowed in UTF-8 (UTF sequence 0xEDA080..0xEDBFBF)
  1.1227 +										$tmp .= substr($str, $tmp_pos, $starter_pos - $tmp_pos);
  1.1228 +
  1.1229 +										if (!empty($utf_sort))
  1.1230 +										{
  1.1231 +											ksort($utf_sort);
  1.1232 +
  1.1233 +											foreach ($utf_sort as $utf_chars)
  1.1234 +											{
  1.1235 +												$tmp .= implode('', $utf_chars);
  1.1236 +											}
  1.1237 +											$utf_sort = array();
  1.1238 +										}
  1.1239 +
  1.1240 +										$tmp .= UTF8_REPLACEMENT;
  1.1241 +										$dump = $sort = 0;
  1.1242 +
  1.1243 +										$tmp_pos = $starter_pos = $pos;
  1.1244 +										continue 2;
  1.1245 +									}
  1.1246 +								break;
  1.1247 +
  1.1248 +								// Note: 0xEF is quite common in Japanese
  1.1249 +								case "\xEF":
  1.1250 +									if ($utf_char == "\xEF\xBF\xBE" || $utf_char == "\xEF\xBF\xBF")
  1.1251 +									{
  1.1252 +										// U+FFFE and U+FFFF are explicitly disallowed (UTF sequence 0xEFBFBE..0xEFBFBF)
  1.1253 +										$tmp .= substr($str, $tmp_pos, $starter_pos - $tmp_pos);
  1.1254 +
  1.1255 +										if (!empty($utf_sort))
  1.1256 +										{
  1.1257 +											ksort($utf_sort);
  1.1258 +
  1.1259 +											foreach ($utf_sort as $utf_chars)
  1.1260 +											{
  1.1261 +												$tmp .= implode('', $utf_chars);
  1.1262 +											}
  1.1263 +											$utf_sort = array();
  1.1264 +										}
  1.1265 +
  1.1266 +										$tmp .= UTF8_REPLACEMENT;
  1.1267 +										$dump = $sort = 0;
  1.1268 +
  1.1269 +										$tmp_pos = $starter_pos = $pos;
  1.1270 +										continue 2;
  1.1271 +									}
  1.1272 +								break;
  1.1273 +
  1.1274 +								case "\xC0":
  1.1275 +								case "\xC1":
  1.1276 +									if ($utf_char <= "\xC1\xBF")
  1.1277 +									{
  1.1278 +										// Overlong sequence: Unicode char U+0000..U+007F encoded as a double-byte UTF char
  1.1279 +										$tmp .= substr($str, $tmp_pos, $starter_pos - $tmp_pos);
  1.1280 +
  1.1281 +										if (!empty($utf_sort))
  1.1282 +										{
  1.1283 +											ksort($utf_sort);
  1.1284 +
  1.1285 +											foreach ($utf_sort as $utf_chars)
  1.1286 +											{
  1.1287 +												$tmp .= implode('', $utf_chars);
  1.1288 +											}
  1.1289 +											$utf_sort = array();
  1.1290 +										}
  1.1291 +
  1.1292 +										$tmp .= UTF8_REPLACEMENT;
  1.1293 +										$dump = $sort = 0;
  1.1294 +
  1.1295 +										$tmp_pos = $starter_pos = $pos;
  1.1296 +										continue 2;
  1.1297 +									}
  1.1298 +								break;
  1.1299 +
  1.1300 +								case "\xE0":
  1.1301 +									if ($utf_char <= "\xE0\x9F\xBF")
  1.1302 +									{
  1.1303 +										// Unicode char U+0000..U+07FF encoded in 3 bytes
  1.1304 +										$tmp .= substr($str, $tmp_pos, $starter_pos - $tmp_pos);
  1.1305 +
  1.1306 +										if (!empty($utf_sort))
  1.1307 +										{
  1.1308 +											ksort($utf_sort);
  1.1309 +
  1.1310 +											foreach ($utf_sort as $utf_chars)
  1.1311 +											{
  1.1312 +												$tmp .= implode('', $utf_chars);
  1.1313 +											}
  1.1314 +											$utf_sort = array();
  1.1315 +										}
  1.1316 +
  1.1317 +										$tmp .= UTF8_REPLACEMENT;
  1.1318 +										$dump = $sort = 0;
  1.1319 +
  1.1320 +										$tmp_pos = $starter_pos = $pos;
  1.1321 +										continue 2;
  1.1322 +									}
  1.1323 +								break;
  1.1324 +
  1.1325 +								case "\xF0":
  1.1326 +									if ($utf_char <= "\xF0\x8F\xBF\xBF")
  1.1327 +									{
  1.1328 +										// Unicode char U+0000..U+FFFF encoded in 4 bytes
  1.1329 +										$tmp .= substr($str, $tmp_pos, $starter_pos - $tmp_pos);
  1.1330 +
  1.1331 +										if (!empty($utf_sort))
  1.1332 +										{
  1.1333 +											ksort($utf_sort);
  1.1334 +
  1.1335 +											foreach ($utf_sort as $utf_chars)
  1.1336 +											{
  1.1337 +												$tmp .= implode('', $utf_chars);
  1.1338 +											}
  1.1339 +											$utf_sort = array();
  1.1340 +										}
  1.1341 +
  1.1342 +										$tmp .= UTF8_REPLACEMENT;
  1.1343 +										$dump = $sort = 0;
  1.1344 +
  1.1345 +										$tmp_pos = $starter_pos = $pos;
  1.1346 +										continue 2;
  1.1347 +									}
  1.1348 +								break;
  1.1349 +
  1.1350 +								default:
  1.1351 +									if ($utf_char > UTF8_MAX)
  1.1352 +									{
  1.1353 +										// Out of the Unicode range
  1.1354 +										$tmp .= substr($str, $tmp_pos, $starter_pos - $tmp_pos);
  1.1355 +
  1.1356 +										if (!empty($utf_sort))
  1.1357 +										{
  1.1358 +											ksort($utf_sort);
  1.1359 +
  1.1360 +											foreach ($utf_sort as $utf_chars)
  1.1361 +											{
  1.1362 +												$tmp .= implode('', $utf_chars);
  1.1363 +											}
  1.1364 +											$utf_sort = array();
  1.1365 +										}
  1.1366 +
  1.1367 +										$tmp .= UTF8_REPLACEMENT;
  1.1368 +										$dump = $sort = 0;
  1.1369 +
  1.1370 +										$tmp_pos = $starter_pos = $pos;
  1.1371 +										continue 2;
  1.1372 +									}
  1.1373 +								break;
  1.1374 +							}
  1.1375 +						}
  1.1376 +					}
  1.1377 +					else
  1.1378 +					{
  1.1379 +						// Hangul syllable
  1.1380 +						$idx = (((ord($utf_char[0]) & 0x0F) << 12) | ((ord($utf_char[1]) & 0x3F) << 6) | (ord($utf_char[2]) & 0x3F)) - UNICODE_HANGUL_SBASE;
  1.1381 +
  1.1382 +						// 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).
  1.1383 +						//
  1.1384 +						// 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
  1.1385 +						if ($t_index = $idx % UNICODE_HANGUL_TCOUNT)
  1.1386 +						{
  1.1387 +							if ($t_index < 25)
  1.1388 +							{
  1.1389 +								$utf_char = "\xE1\x84\x00\xE1\x85\x00\xE1\x86\x00";
  1.1390 +								$utf_char[8] = chr(0xA7 + $t_index);
  1.1391 +							}
  1.1392 +							else
  1.1393 +							{
  1.1394 +								$utf_char = "\xE1\x84\x00\xE1\x85\x00\xE1\x87\x00";
  1.1395 +								$utf_char[8] = chr(0x67 + $t_index);
  1.1396 +							}
  1.1397 +						}
  1.1398 +						else
  1.1399 +						{
  1.1400 +							$utf_char = "\xE1\x84\x00\xE1\x85\x00";
  1.1401 +						}
  1.1402 +
  1.1403 +						$utf_char[2] = chr(0x80 + (int) ($idx / UNICODE_HANGUL_NCOUNT));
  1.1404 +						$utf_char[5] = chr(0xA1 + (int) (($idx % UNICODE_HANGUL_NCOUNT) / UNICODE_HANGUL_TCOUNT));
  1.1405 +
  1.1406 +						// Just like other decompositions, the resulting Jamos must be dumped to the tmp string
  1.1407 +						$dump = 1;
  1.1408 +					}
  1.1409 +
  1.1410 +					// Do we need to dump stuff to the tmp string?
  1.1411 +					if ($dump)
  1.1412 +					{
  1.1413 +						$tmp .= substr($str, $tmp_pos, $starter_pos - $tmp_pos);
  1.1414 +
  1.1415 +						// Dump combiners
  1.1416 +						if (!empty($utf_sort))
  1.1417 +						{
  1.1418 +							if ($sort)
  1.1419 +							{
  1.1420 +								ksort($utf_sort);
  1.1421 +							}
  1.1422 +
  1.1423 +							foreach ($utf_sort as $utf_chars)
  1.1424 +							{
  1.1425 +								$tmp .= implode('', $utf_chars);
  1.1426 +							}
  1.1427 +						}
  1.1428 +
  1.1429 +						$tmp .= $utf_char;
  1.1430 +						$dump = $sort = 0;
  1.1431 +						$tmp_pos = $pos;
  1.1432 +					}
  1.1433 +
  1.1434 +					$last_cc = 0;
  1.1435 +					$utf_sort = array();
  1.1436 +					$starter_pos = $pos;
  1.1437 +				}
  1.1438 +			}
  1.1439 +			else
  1.1440 +			{
  1.1441 +				// ASCII char, which happens to be a starter (as any other ASCII char)
  1.1442 +				if ($dump)
  1.1443 +				{
  1.1444 +					$tmp .= substr($str, $tmp_pos, $starter_pos - $tmp_pos);
  1.1445 +
  1.1446 +					// Dump combiners
  1.1447 +					if (!empty($utf_sort))
  1.1448 +					{
  1.1449 +						if ($sort)
  1.1450 +						{
  1.1451 +							ksort($utf_sort);
  1.1452 +						}
  1.1453 +
  1.1454 +						foreach ($utf_sort as $utf_chars)
  1.1455 +						{
  1.1456 +							$tmp .= implode('', $utf_chars);
  1.1457 +						}
  1.1458 +					}
  1.1459 +
  1.1460 +					$tmp .= $str[$pos];
  1.1461 +					$dump = $sort = 0;
  1.1462 +					$tmp_pos = ++$pos;
  1.1463 +
  1.1464 +					$pos += strspn($str, UTF8_ASCII_RANGE, $pos);
  1.1465 +				}
  1.1466 +				else
  1.1467 +				{
  1.1468 +					$pos += strspn($str, UTF8_ASCII_RANGE, ++$pos);
  1.1469 +				}
  1.1470 +
  1.1471 +				$last_cc = 0;
  1.1472 +				$utf_sort = array();
  1.1473 +				$starter_pos = $pos;
  1.1474 +			}
  1.1475 +		}
  1.1476 +		while ($pos < $len);
  1.1477 +
  1.1478 +		// Now is time to return the string
  1.1479 +		if ($dump)
  1.1480 +		{
  1.1481 +			$tmp .= substr($str, $tmp_pos, $starter_pos - $tmp_pos);
  1.1482 +
  1.1483 +			// Dump combiners
  1.1484 +			if (!empty($utf_sort))
  1.1485 +			{
  1.1486 +				if ($sort)
  1.1487 +				{
  1.1488 +					ksort($utf_sort);
  1.1489 +				}
  1.1490 +
  1.1491 +				foreach ($utf_sort as $utf_chars)
  1.1492 +				{
  1.1493 +					$tmp .= implode('', $utf_chars);
  1.1494 +				}
  1.1495 +			}
  1.1496 +
  1.1497 +			return $tmp;
  1.1498 +		}
  1.1499 +		else if ($tmp_pos)
  1.1500 +		{
  1.1501 +			// If the $tmp_pos cursor was moved then at least one character was not in normal form. Replace $str with the fixed version
  1.1502 +			if ($tmp_pos == $len)
  1.1503 +			{
  1.1504 +				// The $tmp_pos cursor is at the end of $str, therefore $tmp holds the whole $str
  1.1505 +				return $tmp;
  1.1506 +			}
  1.1507 +			else
  1.1508 +			{
  1.1509 +				// The rightmost chunk of $str has not been appended to $tmp yet
  1.1510 +				return $tmp . substr($str, $tmp_pos);
  1.1511 +			}
  1.1512 +		}
  1.1513 +
  1.1514 +		// The string was already in normal form
  1.1515 +		return $str;
  1.1516 +	}
  1.1517 +}
  1.1518 +
  1.1519 +?>
  1.1520 \ No newline at end of file