PageRenderTime 55ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/libraries/phputf8/utils/unicode.php

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