to_unicode.php 3.7 KB

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