/vendor/phputf8/utils/unicode.php

https://github.com/dianaprajescu/joomla-framework · PHP · 265 lines · 123 code · 45 blank · 97 comment · 52 complexity · f9d183bb29f725002349153d891c4d50 MD5 · raw file

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