1 <?php
defined('SYSPATH') OR die('No direct script access.');
7 * @copyright (c) 2007-2012 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 // Cached expected number of octets after the current octet until the beginning of the next UTF8 character sequence
15 // Cached Unicode character
17 // Cached expected number of octets in the current sequence
24 for ($i = 0; $i < $len; $i++
)
30 // When m_state is zero we expect either a US-ASCII character or a multi-octet sequence.
31 if (0 == (0x80 & $in))
33 // US-ASCII, pass straight through.
37 elseif (0xC0 == (0xE0 & $in))
39 // First octet of 2 octet sequence
41 $m_ucs4 = ($m_ucs4 & 0x1F) << 6;
45 elseif (0xE0 == (0xF0 & $in))
47 // First octet of 3 octet sequence
49 $m_ucs4 = ($m_ucs4 & 0x0F) << 12;
53 elseif (0xF0 == (0xF8 & $in))
55 // First octet of 4 octet sequence
57 $m_ucs4 = ($m_ucs4 & 0x07) << 18;
61 elseif (0xF8 == (0xFC & $in))
63 /** First octet of 5 octet sequence.
65 * This is illegal because the encoded codepoint must be either
66 * (a) not the shortest form or
67 * (b) outside the Unicode range of 0-0x10FFFF.
68 * Rather than trying to resynchronize, we will carry on until the end
69 * of the sequence and let the later error handling code catch it.
72 $m_ucs4 = ($m_ucs4 & 0x03) << 24;
76 elseif (0xFC == (0xFE & $in))
78 // First octet of 6 octet sequence, see comments for 5 octet sequence.
80 $m_ucs4 = ($m_ucs4 & 1) << 30;
86 // Current octet is neither in the US-ASCII range nor a legal first octet of a multi-octet sequence.
87 trigger_error('UTF8::to_unicode: Illegal sequence identifier in UTF-8 at byte '.$i, E_USER_WARNING
);
93 // When m_state is non-zero, we expect a continuation of the multi-octet sequence
94 if (0x80 == (0xC0 & $in))
97 $shift = ($m_state - 1) * 6;
99 $tmp = ($tmp & 0x0000003F) << $shift;
102 // End of the multi-octet sequence. mUcs4 now contains the final Unicode codepoint to be output
105 // Check for illegal sequences and codepoints
107 // From Unicode 3.1, non-shortest form is illegal
108 if (((2 == $m_bytes) AND ($m_ucs4 < 0x0080)) OR
109 ((3 == $m_bytes) AND ($m_ucs4 < 0x0800)) OR
110 ((4 == $m_bytes) AND ($m_ucs4 < 0x10000)) OR
112 // From Unicode 3.2, surrogate characters are illegal
113 (($m_ucs4 & 0xFFFFF800) == 0xD800) OR
114 // Codepoints outside the Unicode range are illegal
115 ($m_ucs4 > 0x10FFFF))
117 trigger_error('UTF8::to_unicode: Illegal sequence or codepoint in UTF-8 at byte '.$i, E_USER_WARNING
);
121 if (0xFEFF != $m_ucs4)
123 // BOM is legal but we don't want to output it
127 // Initialize UTF-8 cache
135 // ((0xC0 & (*in) != 0x80) AND (m_state != 0))
136 // Incomplete multi-octet sequence
137 throw new UTF8_Exception("UTF8::to_unicode: Incomplete multi-octet sequence in UTF-8 at byte ':byte'", array(