xref: /dokuwiki/inc/Utf8/Unicode.php (revision d4f83172d9533c4d84f450fe22ef630816b21d75)
1f41bbe4cSAndreas Gohr<?php
2f41bbe4cSAndreas Gohr
3f41bbe4cSAndreas Gohrnamespace dokuwiki\Utf8;
4f41bbe4cSAndreas Gohr
5f41bbe4cSAndreas Gohr/**
6f41bbe4cSAndreas Gohr * Convert between UTF-8 and a list of Unicode Code Points
7f41bbe4cSAndreas Gohr */
8f41bbe4cSAndreas Gohrclass Unicode
9f41bbe4cSAndreas Gohr{
10f41bbe4cSAndreas Gohr    /**
11f41bbe4cSAndreas Gohr     * Takes an UTF-8 string and returns an array of ints representing the
12f41bbe4cSAndreas Gohr     * Unicode characters. Astral planes are supported ie. the ints in the
13f41bbe4cSAndreas Gohr     * output can be > 0xFFFF. Occurrances of the BOM are ignored. Surrogates
14f41bbe4cSAndreas Gohr     * are not allowed.
15f41bbe4cSAndreas Gohr     *
16f41bbe4cSAndreas Gohr     * If $strict is set to true the function returns false if the input
17f41bbe4cSAndreas Gohr     * string isn't a valid UTF-8 octet sequence and raises a PHP error at
18f41bbe4cSAndreas Gohr     * level E_USER_WARNING
19f41bbe4cSAndreas Gohr     *
20f41bbe4cSAndreas Gohr     * Note: this function has been modified slightly in this library to
21f41bbe4cSAndreas Gohr     * trigger errors on encountering bad bytes
22f41bbe4cSAndreas Gohr     *
23f41bbe4cSAndreas Gohr     * @author <hsivonen@iki.fi>
24f41bbe4cSAndreas Gohr     * @author Harry Fuecks <hfuecks@gmail.com>
25f41bbe4cSAndreas Gohr     * @see    unicode_to_utf8
26f41bbe4cSAndreas Gohr     * @link   http://hsivonen.iki.fi/php-utf8/
27f41bbe4cSAndreas Gohr     * @link   http://sourceforge.net/projects/phputf8/
28f41bbe4cSAndreas Gohr     * @todo break into less complex chunks
29f41bbe4cSAndreas Gohr     * @todo use exceptions instead of user errors
30f41bbe4cSAndreas Gohr     *
31f41bbe4cSAndreas Gohr     * @param  string $str UTF-8 encoded string
32f41bbe4cSAndreas Gohr     * @param  boolean $strict Check for invalid sequences?
33f41bbe4cSAndreas Gohr     * @return mixed array of unicode code points or false if UTF-8 invalid
34f41bbe4cSAndreas Gohr     */
35f41bbe4cSAndreas Gohr    public static function fromUtf8($str, $strict = false)
36f41bbe4cSAndreas Gohr    {
37f41bbe4cSAndreas Gohr        $mState = 0;     // cached expected number of octets after the current octet
38f41bbe4cSAndreas Gohr        // until the beginning of the next UTF8 character sequence
39f41bbe4cSAndreas Gohr        $mUcs4 = 0;     // cached Unicode character
40f41bbe4cSAndreas Gohr        $mBytes = 1;     // cached expected number of octets in the current sequence
41f41bbe4cSAndreas Gohr
42e025be72SAndreas Gohr        $out = [];
43f41bbe4cSAndreas Gohr
44f41bbe4cSAndreas Gohr        $len = strlen($str);
45f41bbe4cSAndreas Gohr
46f41bbe4cSAndreas Gohr        for ($i = 0; $i < $len; $i++) {
472401f18dSSyntaxseed            $in = ord($str[$i]);
48f41bbe4cSAndreas Gohr
49f41bbe4cSAndreas Gohr            if ($mState === 0) {
50f41bbe4cSAndreas Gohr                // When mState is zero we expect either a US-ASCII character or a
51f41bbe4cSAndreas Gohr                // multi-octet sequence.
52f41bbe4cSAndreas Gohr                if (0 === (0x80 & $in)) {
53f41bbe4cSAndreas Gohr                    // US-ASCII, pass straight through.
54f41bbe4cSAndreas Gohr                    $out[] = $in;
55f41bbe4cSAndreas Gohr                    $mBytes = 1;
56f41bbe4cSAndreas Gohr                } elseif (0xC0 === (0xE0 & $in)) {
57f41bbe4cSAndreas Gohr                    // First octet of 2 octet sequence
58f41bbe4cSAndreas Gohr                    $mUcs4 = $in;
59f41bbe4cSAndreas Gohr                    $mUcs4 = ($mUcs4 & 0x1F) << 6;
60f41bbe4cSAndreas Gohr                    $mState = 1;
61f41bbe4cSAndreas Gohr                    $mBytes = 2;
62f41bbe4cSAndreas Gohr                } elseif (0xE0 === (0xF0 & $in)) {
63f41bbe4cSAndreas Gohr                    // First octet of 3 octet sequence
64f41bbe4cSAndreas Gohr                    $mUcs4 = $in;
65f41bbe4cSAndreas Gohr                    $mUcs4 = ($mUcs4 & 0x0F) << 12;
66f41bbe4cSAndreas Gohr                    $mState = 2;
67f41bbe4cSAndreas Gohr                    $mBytes = 3;
68f41bbe4cSAndreas Gohr                } elseif (0xF0 === (0xF8 & $in)) {
69f41bbe4cSAndreas Gohr                    // First octet of 4 octet sequence
70f41bbe4cSAndreas Gohr                    $mUcs4 = $in;
71f41bbe4cSAndreas Gohr                    $mUcs4 = ($mUcs4 & 0x07) << 18;
72f41bbe4cSAndreas Gohr                    $mState = 3;
73f41bbe4cSAndreas Gohr                    $mBytes = 4;
74f41bbe4cSAndreas Gohr                } elseif (0xF8 === (0xFC & $in)) {
75f41bbe4cSAndreas Gohr                    /* First octet of 5 octet sequence.
76f41bbe4cSAndreas Gohr                     *
77f41bbe4cSAndreas Gohr                     * This is illegal because the encoded codepoint must be either
78f41bbe4cSAndreas Gohr                     * (a) not the shortest form or
79f41bbe4cSAndreas Gohr                     * (b) outside the Unicode range of 0-0x10FFFF.
80f41bbe4cSAndreas Gohr                     * Rather than trying to resynchronize, we will carry on until the end
81f41bbe4cSAndreas Gohr                     * of the sequence and let the later error handling code catch it.
82f41bbe4cSAndreas Gohr                     */
83f41bbe4cSAndreas Gohr                    $mUcs4 = $in;
84f41bbe4cSAndreas Gohr                    $mUcs4 = ($mUcs4 & 0x03) << 24;
85f41bbe4cSAndreas Gohr                    $mState = 4;
86f41bbe4cSAndreas Gohr                    $mBytes = 5;
87f41bbe4cSAndreas Gohr                } elseif (0xFC === (0xFE & $in)) {
88f41bbe4cSAndreas Gohr                    // First octet of 6 octet sequence, see comments for 5 octet sequence.
89f41bbe4cSAndreas Gohr                    $mUcs4 = $in;
90f41bbe4cSAndreas Gohr                    $mUcs4 = ($mUcs4 & 1) << 30;
91f41bbe4cSAndreas Gohr                    $mState = 5;
92f41bbe4cSAndreas Gohr                    $mBytes = 6;
93f41bbe4cSAndreas Gohr                } elseif ($strict) {
94f41bbe4cSAndreas Gohr                    /* Current octet is neither in the US-ASCII range nor a legal first
95f41bbe4cSAndreas Gohr                     * octet of a multi-octet sequence.
96f41bbe4cSAndreas Gohr                     */
97f41bbe4cSAndreas Gohr                    trigger_error(
98f41bbe4cSAndreas Gohr                        'utf8_to_unicode: Illegal sequence identifier ' .
99f41bbe4cSAndreas Gohr                        'in UTF-8 at byte ' . $i,
100f41bbe4cSAndreas Gohr                        E_USER_WARNING
101f41bbe4cSAndreas Gohr                    );
102f41bbe4cSAndreas Gohr                    return false;
103f41bbe4cSAndreas Gohr                }
104e025be72SAndreas Gohr            } elseif (0x80 === (0xC0 & $in)) {
105f41bbe4cSAndreas Gohr                // When mState is non-zero, we expect a continuation of the multi-octet
106f41bbe4cSAndreas Gohr                // sequence
107f41bbe4cSAndreas Gohr                // Legal continuation.
108f41bbe4cSAndreas Gohr                $shift = ($mState - 1) * 6;
109f41bbe4cSAndreas Gohr                $tmp = $in;
110f41bbe4cSAndreas Gohr                $tmp = ($tmp & 0x0000003F) << $shift;
111f41bbe4cSAndreas Gohr                $mUcs4 |= $tmp;
112f41bbe4cSAndreas Gohr                /**
113f41bbe4cSAndreas Gohr                 * End of the multi-octet sequence. mUcs4 now contains the final
114f41bbe4cSAndreas Gohr                 * Unicode codepoint to be output
115f41bbe4cSAndreas Gohr                 */
116f41bbe4cSAndreas Gohr                if (0 === --$mState) {
117f41bbe4cSAndreas Gohr                    /*
118f41bbe4cSAndreas Gohr                     * Check for illegal sequences and codepoints.
119f41bbe4cSAndreas Gohr                     */
120f41bbe4cSAndreas Gohr                    // From Unicode 3.1, non-shortest form is illegal
121*7d34963bSAndreas Gohr                    if (
122*7d34963bSAndreas Gohr                        ((2 === $mBytes) && ($mUcs4 < 0x0080)) ||
123f41bbe4cSAndreas Gohr                        ((3 === $mBytes) && ($mUcs4 < 0x0800)) ||
124f41bbe4cSAndreas Gohr                        ((4 === $mBytes) && ($mUcs4 < 0x10000)) ||
125f41bbe4cSAndreas Gohr                        (4 < $mBytes) ||
126f41bbe4cSAndreas Gohr                        // From Unicode 3.2, surrogate characters are illegal
127f41bbe4cSAndreas Gohr                        (($mUcs4 & 0xFFFFF800) === 0xD800) ||
128f41bbe4cSAndreas Gohr                        // Codepoints outside the Unicode range are illegal
129*7d34963bSAndreas Gohr                        ($mUcs4 > 0x10FFFF)
130*7d34963bSAndreas Gohr                    ) {
131f41bbe4cSAndreas Gohr                        if ($strict) {
132f41bbe4cSAndreas Gohr                            trigger_error(
133f41bbe4cSAndreas Gohr                                'utf8_to_unicode: Illegal sequence or codepoint ' .
134f41bbe4cSAndreas Gohr                                'in UTF-8 at byte ' . $i,
135f41bbe4cSAndreas Gohr                                E_USER_WARNING
136f41bbe4cSAndreas Gohr                            );
137f41bbe4cSAndreas Gohr
138f41bbe4cSAndreas Gohr                            return false;
139f41bbe4cSAndreas Gohr                        }
140f41bbe4cSAndreas Gohr                    }
141f41bbe4cSAndreas Gohr
142f41bbe4cSAndreas Gohr                    if (0xFEFF !== $mUcs4) {
143f41bbe4cSAndreas Gohr                        // BOM is legal but we don't want to output it
144f41bbe4cSAndreas Gohr                        $out[] = $mUcs4;
145f41bbe4cSAndreas Gohr                    }
146f41bbe4cSAndreas Gohr
147f41bbe4cSAndreas Gohr                    //initialize UTF8 cache
148f41bbe4cSAndreas Gohr                    $mState = 0;
149f41bbe4cSAndreas Gohr                    $mUcs4 = 0;
150f41bbe4cSAndreas Gohr                    $mBytes = 1;
151f41bbe4cSAndreas Gohr                }
152f41bbe4cSAndreas Gohr            } elseif ($strict) {
153f41bbe4cSAndreas Gohr                /**
154f41bbe4cSAndreas Gohr                 *((0xC0 & (*in) != 0x80) && (mState != 0))
155f41bbe4cSAndreas Gohr                 * Incomplete multi-octet sequence.
156f41bbe4cSAndreas Gohr                 */
157f41bbe4cSAndreas Gohr                trigger_error(
158f41bbe4cSAndreas Gohr                    'utf8_to_unicode: Incomplete multi-octet ' .
159f41bbe4cSAndreas Gohr                    '   sequence in UTF-8 at byte ' . $i,
160f41bbe4cSAndreas Gohr                    E_USER_WARNING
161f41bbe4cSAndreas Gohr                );
162f41bbe4cSAndreas Gohr
163f41bbe4cSAndreas Gohr                return false;
164f41bbe4cSAndreas Gohr            }
165f41bbe4cSAndreas Gohr        }
166f41bbe4cSAndreas Gohr        return $out;
167f41bbe4cSAndreas Gohr    }
168f41bbe4cSAndreas Gohr
169f41bbe4cSAndreas Gohr    /**
170f41bbe4cSAndreas Gohr     * Takes an array of ints representing the Unicode characters and returns
171f41bbe4cSAndreas Gohr     * a UTF-8 string. Astral planes are supported ie. the ints in the
172f41bbe4cSAndreas Gohr     * input can be > 0xFFFF. Occurrances of the BOM are ignored. Surrogates
173f41bbe4cSAndreas Gohr     * are not allowed.
174f41bbe4cSAndreas Gohr     *
175f41bbe4cSAndreas Gohr     * If $strict is set to true the function returns false if the input
176f41bbe4cSAndreas Gohr     * array contains ints that represent surrogates or are outside the
177f41bbe4cSAndreas Gohr     * Unicode range and raises a PHP error at level E_USER_WARNING
178f41bbe4cSAndreas Gohr     *
179f41bbe4cSAndreas Gohr     * Note: this function has been modified slightly in this library to use
180f41bbe4cSAndreas Gohr     * output buffering to concatenate the UTF-8 string (faster) as well as
181f41bbe4cSAndreas Gohr     * reference the array by it's keys
182f41bbe4cSAndreas Gohr     *
183f41bbe4cSAndreas Gohr     * @param  array $arr of unicode code points representing a string
184f41bbe4cSAndreas Gohr     * @param  boolean $strict Check for invalid sequences?
185f41bbe4cSAndreas Gohr     * @return string|false UTF-8 string or false if array contains invalid code points
186f41bbe4cSAndreas Gohr     *
187f41bbe4cSAndreas Gohr     * @author <hsivonen@iki.fi>
188f41bbe4cSAndreas Gohr     * @author Harry Fuecks <hfuecks@gmail.com>
189f41bbe4cSAndreas Gohr     * @see    utf8_to_unicode
190f41bbe4cSAndreas Gohr     * @link   http://hsivonen.iki.fi/php-utf8/
191f41bbe4cSAndreas Gohr     * @link   http://sourceforge.net/projects/phputf8/
192f41bbe4cSAndreas Gohr     * @todo use exceptions instead of user errors
193f41bbe4cSAndreas Gohr     */
194f41bbe4cSAndreas Gohr    public static function toUtf8($arr, $strict = false)
195f41bbe4cSAndreas Gohr    {
196f41bbe4cSAndreas Gohr        if (!is_array($arr)) return '';
197f41bbe4cSAndreas Gohr        ob_start();
198f41bbe4cSAndreas Gohr
199f41bbe4cSAndreas Gohr        foreach (array_keys($arr) as $k) {
200f41bbe4cSAndreas Gohr            if (($arr[$k] >= 0) && ($arr[$k] <= 0x007f)) {
201f41bbe4cSAndreas Gohr                # ASCII range (including control chars)
202f41bbe4cSAndreas Gohr                echo chr($arr[$k]);
203f41bbe4cSAndreas Gohr            } elseif ($arr[$k] <= 0x07ff) {
204f41bbe4cSAndreas Gohr                # 2 byte sequence
205f41bbe4cSAndreas Gohr                echo chr(0xc0 | ($arr[$k] >> 6));
206f41bbe4cSAndreas Gohr                echo chr(0x80 | ($arr[$k] & 0x003f));
207f41bbe4cSAndreas Gohr            } elseif ($arr[$k] == 0xFEFF) {
208f41bbe4cSAndreas Gohr                # Byte order mark (skip)
209f41bbe4cSAndreas Gohr                // nop -- zap the BOM
210f41bbe4cSAndreas Gohr            } elseif ($arr[$k] >= 0xD800 && $arr[$k] <= 0xDFFF) {
211f41bbe4cSAndreas Gohr                # Test for illegal surrogates
212f41bbe4cSAndreas Gohr                // found a surrogate
213f41bbe4cSAndreas Gohr                if ($strict) {
214f41bbe4cSAndreas Gohr                    trigger_error(
215f41bbe4cSAndreas Gohr                        'unicode_to_utf8: Illegal surrogate ' .
216f41bbe4cSAndreas Gohr                        'at index: ' . $k . ', value: ' . $arr[$k],
217f41bbe4cSAndreas Gohr                        E_USER_WARNING
218f41bbe4cSAndreas Gohr                    );
219f41bbe4cSAndreas Gohr                    return false;
220f41bbe4cSAndreas Gohr                }
221f41bbe4cSAndreas Gohr            } elseif ($arr[$k] <= 0xffff) {
222f41bbe4cSAndreas Gohr                # 3 byte sequence
223f41bbe4cSAndreas Gohr                echo chr(0xe0 | ($arr[$k] >> 12));
224f41bbe4cSAndreas Gohr                echo chr(0x80 | (($arr[$k] >> 6) & 0x003f));
225f41bbe4cSAndreas Gohr                echo chr(0x80 | ($arr[$k] & 0x003f));
226f41bbe4cSAndreas Gohr            } elseif ($arr[$k] <= 0x10ffff) {
227f41bbe4cSAndreas Gohr                # 4 byte sequence
228f41bbe4cSAndreas Gohr                echo chr(0xf0 | ($arr[$k] >> 18));
229f41bbe4cSAndreas Gohr                echo chr(0x80 | (($arr[$k] >> 12) & 0x3f));
230f41bbe4cSAndreas Gohr                echo chr(0x80 | (($arr[$k] >> 6) & 0x3f));
231f41bbe4cSAndreas Gohr                echo chr(0x80 | ($arr[$k] & 0x3f));
232f41bbe4cSAndreas Gohr            } elseif ($strict) {
233f41bbe4cSAndreas Gohr                trigger_error(
234f41bbe4cSAndreas Gohr                    'unicode_to_utf8: Codepoint out of Unicode range ' .
235f41bbe4cSAndreas Gohr                    'at index: ' . $k . ', value: ' . $arr[$k],
236f41bbe4cSAndreas Gohr                    E_USER_WARNING
237f41bbe4cSAndreas Gohr                );
238f41bbe4cSAndreas Gohr
239f41bbe4cSAndreas Gohr                // out of range
240f41bbe4cSAndreas Gohr                return false;
241f41bbe4cSAndreas Gohr            }
242f41bbe4cSAndreas Gohr        }
243f41bbe4cSAndreas Gohr
244f41bbe4cSAndreas Gohr        return ob_get_clean();
245f41bbe4cSAndreas Gohr    }
246f41bbe4cSAndreas Gohr}
247