to_unicode.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. /**
  3. * UTF8::to_unicode
  4. *
  5. * @package KO7
  6. *
  7. * @copyright (c) 2007-2016 Kohana Team
  8. * @copyright (c) since 2016 Koseven Team
  9. * @copyright (c) 2005 Harry Fuecks
  10. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
  11. */
  12. function _to_unicode($str)
  13. {
  14. // Cached expected number of octets after the current octet until the beginning of the next UTF8 character sequence
  15. $m_state = 0;
  16. // Cached Unicode character
  17. $m_ucs4 = 0;
  18. // Cached expected number of octets in the current sequence
  19. $m_bytes = 1;
  20. $out = [];
  21. $len = strlen($str);
  22. for ($i = 0; $i < $len; $i++)
  23. {
  24. $in = ord($str[$i]);
  25. if ($m_state == 0)
  26. {
  27. // When m_state is zero we expect either a US-ASCII character or a multi-octet sequence.
  28. if (0 == (0x80 & $in))
  29. {
  30. // US-ASCII, pass straight through.
  31. $out[] = $in;
  32. $m_bytes = 1;
  33. }
  34. elseif (0xC0 == (0xE0 & $in))
  35. {
  36. // First octet of 2 octet sequence
  37. $m_ucs4 = $in;
  38. $m_ucs4 = ($m_ucs4 & 0x1F) << 6;
  39. $m_state = 1;
  40. $m_bytes = 2;
  41. }
  42. elseif (0xE0 == (0xF0 & $in))
  43. {
  44. // First octet of 3 octet sequence
  45. $m_ucs4 = $in;
  46. $m_ucs4 = ($m_ucs4 & 0x0F) << 12;
  47. $m_state = 2;
  48. $m_bytes = 3;
  49. }
  50. elseif (0xF0 == (0xF8 & $in))
  51. {
  52. // First octet of 4 octet sequence
  53. $m_ucs4 = $in;
  54. $m_ucs4 = ($m_ucs4 & 0x07) << 18;
  55. $m_state = 3;
  56. $m_bytes = 4;
  57. }
  58. elseif (0xF8 == (0xFC & $in))
  59. {
  60. /** First octet of 5 octet sequence.
  61. *
  62. * This is illegal because the encoded codepoint must be either
  63. * (a) not the shortest form or
  64. * (b) outside the Unicode range of 0-0x10FFFF.
  65. * Rather than trying to resynchronize, we will carry on until the end
  66. * of the sequence and let the later error handling code catch it.
  67. **/
  68. $m_ucs4 = $in;
  69. $m_ucs4 = ($m_ucs4 & 0x03) << 24;
  70. $m_state = 4;
  71. $m_bytes = 5;
  72. }
  73. elseif (0xFC == (0xFE & $in))
  74. {
  75. // First octet of 6 octet sequence, see comments for 5 octet sequence.
  76. $m_ucs4 = $in;
  77. $m_ucs4 = ($m_ucs4 & 1) << 30;
  78. $m_state = 5;
  79. $m_bytes = 6;
  80. }
  81. else
  82. {
  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);
  85. return FALSE;
  86. }
  87. }
  88. else
  89. {
  90. // When m_state is non-zero, we expect a continuation of the multi-octet sequence
  91. if (0x80 == (0xC0 & $in))
  92. {
  93. // Legal continuation
  94. $shift = ($m_state - 1) * 6;
  95. $tmp = $in;
  96. $tmp = ($tmp & 0x0000003F) << $shift;
  97. $m_ucs4 |= $tmp;
  98. // End of the multi-octet sequence. mUcs4 now contains the final Unicode codepoint to be output
  99. if (0 == --$m_state)
  100. {
  101. // Check for illegal sequences and codepoints
  102. // From Unicode 3.1, non-shortest form is illegal
  103. if (((2 == $m_bytes) AND ($m_ucs4 < 0x0080)) OR
  104. ((3 == $m_bytes) AND ($m_ucs4 < 0x0800)) OR
  105. ((4 == $m_bytes) AND ($m_ucs4 < 0x10000)) OR
  106. (4 < $m_bytes) OR
  107. // From Unicode 3.2, surrogate characters are illegal
  108. (($m_ucs4 & 0xFFFFF800) == 0xD800) OR
  109. // Codepoints outside the Unicode range are illegal
  110. ($m_ucs4 > 0x10FFFF))
  111. {
  112. trigger_error('UTF8::to_unicode: Illegal sequence or codepoint in UTF-8 at byte '.$i, E_USER_WARNING);
  113. return FALSE;
  114. }
  115. if (0xFEFF != $m_ucs4)
  116. {
  117. // BOM is legal but we don't want to output it
  118. $out[] = $m_ucs4;
  119. }
  120. // Initialize UTF-8 cache
  121. $m_state = 0;
  122. $m_ucs4 = 0;
  123. $m_bytes = 1;
  124. }
  125. }
  126. else
  127. {
  128. // ((0xC0 & (*in) != 0x80) AND (m_state != 0))
  129. // Incomplete multi-octet sequence
  130. throw new UTF8_Exception("UTF8::to_unicode: Incomplete multi-octet sequence in UTF-8 at byte ':byte'", [
  131. ':byte' => $i,
  132. ]);
  133. }
  134. }
  135. }
  136. return $out;
  137. }