1 <?php
defined('SYSPATH') OR die('No direct access allowed.');
7 * @copyright (c) 2007 Kohana Team
8 * @copyright (c) 2005 Harry Fuecks
9 * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
11 function _to_unicode($str)
13 $mState = 0; // cached expected number of octets after the current octet until the beginning of the next UTF8 character sequence
14 $mUcs4 = 0; // cached Unicode character
15 $mBytes = 1; // cached expected number of octets in the current sequence
21 for ($i = 0; $i < $len; $i++
)
27 // When mState is zero we expect either a US-ASCII character or a
28 // multi-octet sequence.
29 if (0 == (0x80 & $in))
31 // US-ASCII, pass straight through.
35 elseif (0xC0 == (0xE0 & $in))
37 // First octet of 2 octet sequence
39 $mUcs4 = ($mUcs4 & 0x1F) << 6;
43 elseif (0xE0 == (0xF0 & $in))
45 // First octet of 3 octet sequence
47 $mUcs4 = ($mUcs4 & 0x0F) << 12;
51 elseif (0xF0 == (0xF8 & $in))
53 // First octet of 4 octet sequence
55 $mUcs4 = ($mUcs4 & 0x07) << 18;
59 elseif (0xF8 == (0xFC & $in))
61 // First octet of 5 octet sequence.
63 // This is illegal because the encoded codepoint must be either
64 // (a) not the shortest form or
65 // (b) outside the Unicode range of 0-0x10FFFF.
66 // Rather than trying to resynchronize, we will carry on until the end
67 // of the sequence and let the later error handling code catch it.
69 $mUcs4 = ($mUcs4 & 0x03) << 24;
73 elseif (0xFC == (0xFE & $in))
75 // First octet of 6 octet sequence, see comments for 5 octet sequence.
77 $mUcs4 = ($mUcs4 & 1) << 30;
83 // Current octet is neither in the US-ASCII range nor a legal first octet of a multi-octet sequence.
84 trigger_error('utf8::to_unicode: Illegal sequence identifier in UTF-8 at byte '.$i, E_USER_WARNING
);
90 // When mState is non-zero, we expect a continuation of the multi-octet sequence
91 if (0x80 == (0xC0 & $in))
94 $shift = ($mState - 1) * 6;
96 $tmp = ($tmp & 0x0000003F) << $shift;
99 // End of the multi-octet sequence. mUcs4 now contains the final Unicode codepoint to be output
102 // Check for illegal sequences and codepoints
104 // From Unicode 3.1, non-shortest form is illegal
105 if (((2 == $mBytes) AND ($mUcs4 < 0x0080)) OR
106 ((3 == $mBytes) AND ($mUcs4 < 0x0800)) OR
107 ((4 == $mBytes) AND ($mUcs4 < 0x10000)) OR
109 // From Unicode 3.2, surrogate characters are illegal
110 (($mUcs4 & 0xFFFFF800) == 0xD800) OR
111 // Codepoints outside the Unicode range are illegal
114 trigger_error('utf8::to_unicode: Illegal sequence or codepoint in UTF-8 at byte '.$i, E_USER_WARNING
);
118 if (0xFEFF != $mUcs4)
120 // BOM is legal but we don't want to output it
124 // Initialize UTF-8 cache
132 // ((0xC0 & (*in) != 0x80) AND (mState != 0))
133 // Incomplete multi-octet sequence
134 trigger_error('utf8::to_unicode: Incomplete multi-octet sequence in UTF-8 at byte '.$i, E_USER_WARNING
);