PageRenderTime 61ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/include/utf8/utils/bad.php

https://bitbucket.org/yoorick/fluxbb.pe
PHP | 435 lines | 222 code | 49 blank | 164 comment | 48 complexity | aff25bfe7fb82a231c6946ae7847c4e0 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /**
  3. * @version $Id: bad.php,v 1.2 2006/02/26 13:20:44 harryf Exp $
  4. * Tools for locating / replacing bad bytes in UTF-8 strings
  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 bad
  17. * @see utf8_is_valid
  18. */
  19. /**
  20. * Locates the first bad byte in a UTF-8 string returning it's
  21. * byte index in the string
  22. * PCRE Pattern to locate bad bytes in a UTF-8 string
  23. * Comes from W3 FAQ: Multilingual Forms
  24. * Note: modified to include full ASCII range including control chars
  25. * @see http://www.w3.org/International/questions/qa-forms-utf-8
  26. * @param string
  27. * @return mixed integer byte index or FALSE if no bad found
  28. * @package utf8
  29. * @subpackage bad
  30. */
  31. function utf8_bad_find($str)
  32. {
  33. $UTF8_BAD =
  34. '([\x00-\x7F]'. # ASCII (including control chars)
  35. '|[\xC2-\xDF][\x80-\xBF]'. # Non-overlong 2-byte
  36. '|\xE0[\xA0-\xBF][\x80-\xBF]'. # Excluding overlongs
  37. '|[\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}'. # Straight 3-byte
  38. '|\xED[\x80-\x9F][\x80-\xBF]'. # Excluding surrogates
  39. '|\xF0[\x90-\xBF][\x80-\xBF]{2}'. # Planes 1-3
  40. '|[\xF1-\xF3][\x80-\xBF]{3}'. # Planes 4-15
  41. '|\xF4[\x80-\x8F][\x80-\xBF]{2}'. # Plane 16
  42. '|(.{1}))'; # Invalid byte
  43. $pos = 0;
  44. $badList = array();
  45. while (preg_match('/'.$UTF8_BAD.'/S', $str, $matches))
  46. {
  47. $bytes = strlen($matches[0]);
  48. if (isset($matches[2]))
  49. return $pos;
  50. $pos += $bytes;
  51. $str = substr($str,$bytes);
  52. }
  53. return false;
  54. }
  55. /**
  56. * Locates all bad bytes in a UTF-8 string and returns a list of their
  57. * byte index in the string
  58. * PCRE Pattern to locate bad bytes in a UTF-8 string
  59. * Comes from W3 FAQ: Multilingual Forms
  60. * Note: modified to include full ASCII range including control chars
  61. * @see http://www.w3.org/International/questions/qa-forms-utf-8
  62. * @param string
  63. * @return mixed array of integers or FALSE if no bad found
  64. * @package utf8
  65. * @subpackage bad
  66. */
  67. function utf8_bad_findall($str)
  68. {
  69. $UTF8_BAD =
  70. '([\x00-\x7F]'. # ASCII (including control chars)
  71. '|[\xC2-\xDF][\x80-\xBF]'. # Non-overlong 2-byte
  72. '|\xE0[\xA0-\xBF][\x80-\xBF]'. # Excluding overlongs
  73. '|[\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}'. # Straight 3-byte
  74. '|\xED[\x80-\x9F][\x80-\xBF]'. # Excluding surrogates
  75. '|\xF0[\x90-\xBF][\x80-\xBF]{2}'. # Planes 1-3
  76. '|[\xF1-\xF3][\x80-\xBF]{3}'. # Planes 4-15
  77. '|\xF4[\x80-\x8F][\x80-\xBF]{2}'. # Plane 16
  78. '|(.{1}))'; # Invalid byte
  79. $pos = 0;
  80. $badList = array();
  81. while (preg_match('/'.$UTF8_BAD.'/S', $str, $matches))
  82. {
  83. $bytes = strlen($matches[0]);
  84. if (isset($matches[2]))
  85. $badList[] = $pos;
  86. $pos += $bytes;
  87. $str = substr($str,$bytes);
  88. }
  89. if (count($badList) > 0)
  90. return $badList;
  91. return false;
  92. }
  93. /**
  94. * Strips out any bad bytes from a UTF-8 string and returns the rest
  95. * PCRE Pattern to locate bad bytes in a UTF-8 string
  96. * Comes from W3 FAQ: Multilingual Forms
  97. * Note: modified to include full ASCII range including control chars
  98. * @see http://www.w3.org/International/questions/qa-forms-utf-8
  99. * @param string
  100. * @return string
  101. * @package utf8
  102. * @subpackage bad
  103. */
  104. function utf8_bad_strip($str)
  105. {
  106. $UTF8_BAD =
  107. '([\x00-\x7F]'. # ASCII (including control chars)
  108. '|[\xC2-\xDF][\x80-\xBF]'. # Non-overlong 2-byte
  109. '|\xE0[\xA0-\xBF][\x80-\xBF]'. # Excluding overlongs
  110. '|[\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}'. # Straight 3-byte
  111. '|\xED[\x80-\x9F][\x80-\xBF]'. # Excluding surrogates
  112. '|\xF0[\x90-\xBF][\x80-\xBF]{2}'. # Planes 1-3
  113. '|[\xF1-\xF3][\x80-\xBF]{3}'. # Planes 4-15
  114. '|\xF4[\x80-\x8F][\x80-\xBF]{2}'. # Plane 16
  115. '|(.{1}))'; # Invalid byte
  116. ob_start();
  117. while (preg_match('/'.$UTF8_BAD.'/S', $str, $matches))
  118. {
  119. if (!isset($matches[2]))
  120. echo $matches[0];
  121. $str = substr($str, strlen($matches[0]));
  122. }
  123. $result = ob_get_contents();
  124. ob_end_clean();
  125. return $result;
  126. }
  127. /**
  128. * Replace bad bytes with an alternative character - ASCII character
  129. * recommended is replacement char
  130. * PCRE Pattern to locate bad bytes in a UTF-8 string
  131. * Comes from W3 FAQ: Multilingual Forms
  132. * Note: modified to include full ASCII range including control chars
  133. * @see http://www.w3.org/International/questions/qa-forms-utf-8
  134. * @param string to search
  135. * @param string to replace bad bytes with (defaults to '?') - use ASCII
  136. * @return string
  137. * @package utf8
  138. * @subpackage bad
  139. */
  140. function utf8_bad_replace($str, $replace='?')
  141. {
  142. $UTF8_BAD =
  143. '([\x00-\x7F]'. # ASCII (including control chars)
  144. '|[\xC2-\xDF][\x80-\xBF]'. # Non-overlong 2-byte
  145. '|\xE0[\xA0-\xBF][\x80-\xBF]'. # Excluding overlongs
  146. '|[\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}'. # Straight 3-byte
  147. '|\xED[\x80-\x9F][\x80-\xBF]'. # Excluding surrogates
  148. '|\xF0[\x90-\xBF][\x80-\xBF]{2}'. # Planes 1-3
  149. '|[\xF1-\xF3][\x80-\xBF]{3}'. # Planes 4-15
  150. '|\xF4[\x80-\x8F][\x80-\xBF]{2}'. # Plane 16
  151. '|(.{1}))'; # Invalid byte
  152. ob_start();
  153. while (preg_match('/'.$UTF8_BAD.'/S', $str, $matches))
  154. {
  155. if (!isset($matches[2]))
  156. echo $matches[0];
  157. else
  158. echo $replace;
  159. $str = substr($str, strlen($matches[0]));
  160. }
  161. $result = ob_get_contents();
  162. ob_end_clean();
  163. return $result;
  164. }
  165. /**
  166. * Return code from utf8_bad_identify() when a five octet sequence is detected.
  167. * Note: 5 octets sequences are valid UTF-8 but are not supported by Unicode so
  168. * do not represent a useful character
  169. * @see utf8_bad_identify
  170. * @package utf8
  171. * @subpackage bad
  172. */
  173. define('UTF8_BAD_5OCTET', 1);
  174. /**
  175. * Return code from utf8_bad_identify() when a six octet sequence is detected.
  176. * Note: 6 octets sequences are valid UTF-8 but are not supported by Unicode so
  177. * do not represent a useful character
  178. * @see utf8_bad_identify
  179. * @package utf8
  180. * @subpackage bad
  181. */
  182. define('UTF8_BAD_6OCTET', 2);
  183. /**
  184. * Return code from utf8_bad_identify().
  185. * Invalid octet for use as start of multi-byte UTF-8 sequence
  186. * @see utf8_bad_identify
  187. * @package utf8
  188. * @subpackage bad
  189. */
  190. define('UTF8_BAD_SEQID', 3);
  191. /**
  192. * Return code from utf8_bad_identify().
  193. * From Unicode 3.1, non-shortest form is illegal
  194. * @see utf8_bad_identify
  195. * @package utf8
  196. * @subpackage bad
  197. */
  198. define('UTF8_BAD_NONSHORT', 4);
  199. /**
  200. * Return code from utf8_bad_identify().
  201. * From Unicode 3.2, surrogate characters are illegal
  202. * @see utf8_bad_identify
  203. * @package utf8
  204. * @subpackage bad
  205. */
  206. define('UTF8_BAD_SURROGATE', 5);
  207. /**
  208. * Return code from utf8_bad_identify().
  209. * Codepoints outside the Unicode range are illegal
  210. * @see utf8_bad_identify
  211. * @package utf8
  212. * @subpackage bad
  213. */
  214. define('UTF8_BAD_UNIOUTRANGE', 6);
  215. /**
  216. * Return code from utf8_bad_identify().
  217. * Incomplete multi-octet sequence
  218. * Note: this is kind of a "catch-all"
  219. * @see utf8_bad_identify
  220. * @package utf8
  221. * @subpackage bad
  222. */
  223. define('UTF8_BAD_SEQINCOMPLETE', 7);
  224. /**
  225. * Reports on the type of bad byte found in a UTF-8 string. Returns a
  226. * status code on the first bad byte found
  227. * @author <hsivonen@iki.fi>
  228. * @param string UTF-8 encoded string
  229. * @return mixed integer constant describing problem or FALSE if valid UTF-8
  230. * @see utf8_bad_explain
  231. * @see http://hsivonen.iki.fi/php-utf8/
  232. * @package utf8
  233. * @subpackage bad
  234. */
  235. function utf8_bad_identify($str, &$i)
  236. {
  237. $mState = 0; // Cached expected number of octets after the current octet
  238. // until the beginning of the next UTF8 character sequence
  239. $mUcs4 = 0; // Cached Unicode character
  240. $mBytes = 1; // Cached expected number of octets in the current sequence
  241. $len = strlen($str);
  242. for($i=0; $i < $len; $i++)
  243. {
  244. $in = ord($str{$i});
  245. if ( $mState == 0)
  246. {
  247. // When mState is zero we expect either a US-ASCII character or a multi-octet sequence.
  248. if (0 == (0x80 & ($in)))
  249. {
  250. // US-ASCII, pass straight through.
  251. $mBytes = 1;
  252. }
  253. else if (0xC0 == (0xE0 & ($in)))
  254. {
  255. // First octet of 2 octet sequence
  256. $mUcs4 = ($in);
  257. $mUcs4 = ($mUcs4 & 0x1F) << 6;
  258. $mState = 1;
  259. $mBytes = 2;
  260. }
  261. else if (0xE0 == (0xF0 & ($in)))
  262. {
  263. // First octet of 3 octet sequence
  264. $mUcs4 = ($in);
  265. $mUcs4 = ($mUcs4 & 0x0F) << 12;
  266. $mState = 2;
  267. $mBytes = 3;
  268. }
  269. else if (0xF0 == (0xF8 & ($in)))
  270. {
  271. // First octet of 4 octet sequence
  272. $mUcs4 = ($in);
  273. $mUcs4 = ($mUcs4 & 0x07) << 18;
  274. $mState = 3;
  275. $mBytes = 4;
  276. }
  277. else if (0xF8 == (0xFC & ($in)))
  278. {
  279. /* First octet of 5 octet sequence.
  280. *
  281. * This is illegal because the encoded codepoint must be either
  282. * (a) not the shortest form or
  283. * (b) outside the Unicode range of 0-0x10FFFF.
  284. */
  285. return UTF8_BAD_5OCTET;
  286. }
  287. else if (0xFC == (0xFE & ($in)))
  288. {
  289. // First octet of 6 octet sequence, see comments for 5 octet sequence.
  290. return UTF8_BAD_6OCTET;
  291. }
  292. else
  293. {
  294. // Current octet is neither in the US-ASCII range nor a legal first
  295. // octet of a multi-octet sequence.
  296. return UTF8_BAD_SEQID;
  297. }
  298. }
  299. else
  300. {
  301. // When mState is non-zero, we expect a continuation of the multi-octet sequence
  302. if (0x80 == (0xC0 & ($in)))
  303. {
  304. // Legal continuation.
  305. $shift = ($mState - 1) * 6;
  306. $tmp = $in;
  307. $tmp = ($tmp & 0x0000003F) << $shift;
  308. $mUcs4 |= $tmp;
  309. /**
  310. * End of the multi-octet sequence. mUcs4 now contains the final
  311. * Unicode codepoint to be output
  312. */
  313. if (0 == --$mState)
  314. {
  315. // From Unicode 3.1, non-shortest form is illegal
  316. if (((2 == $mBytes) && ($mUcs4 < 0x0080)) ||
  317. ((3 == $mBytes) && ($mUcs4 < 0x0800)) ||
  318. ((4 == $mBytes) && ($mUcs4 < 0x10000)) )
  319. return UTF8_BAD_NONSHORT;
  320. else if (($mUcs4 & 0xFFFFF800) == 0xD800) // From Unicode 3.2, surrogate characters are illegal
  321. return UTF8_BAD_SURROGATE;
  322. else if ($mUcs4 > 0x10FFFF) // Codepoints outside the Unicode range are illegal
  323. return UTF8_BAD_UNIOUTRANGE;
  324. // Initialize UTF8 cache
  325. $mState = 0;
  326. $mUcs4 = 0;
  327. $mBytes = 1;
  328. }
  329. }
  330. else
  331. {
  332. // ((0xC0 & (*in) != 0x80) && (mState != 0))
  333. // Incomplete multi-octet sequence.
  334. $i--;
  335. return UTF8_BAD_SEQINCOMPLETE;
  336. }
  337. }
  338. }
  339. // Incomplete multi-octet sequence
  340. if ($mState != 0)
  341. {
  342. $i--;
  343. return UTF8_BAD_SEQINCOMPLETE;
  344. }
  345. // No bad octets found
  346. $i = null;
  347. return false;
  348. }
  349. /**
  350. * Takes a return code from utf8_bad_identify() are returns a message
  351. * (in English) explaining what the problem is.
  352. * @param int return code from utf8_bad_identify
  353. * @return mixed string message or FALSE if return code unknown
  354. * @see utf8_bad_identify
  355. * @package utf8
  356. * @subpackage bad
  357. */
  358. function utf8_bad_explain($code)
  359. {
  360. switch ($code)
  361. {
  362. case UTF8_BAD_5OCTET:
  363. return 'Five octet sequences are valid UTF-8 but are not supported by Unicode';
  364. break;
  365. case UTF8_BAD_6OCTET:
  366. return 'Six octet sequences are valid UTF-8 but are not supported by Unicode';
  367. break;
  368. case UTF8_BAD_SEQID:
  369. return 'Invalid octet for use as start of multi-byte UTF-8 sequence';
  370. break;
  371. case UTF8_BAD_NONSHORT:
  372. return 'From Unicode 3.1, non-shortest form is illegal';
  373. break;
  374. case UTF8_BAD_SURROGATE:
  375. return 'From Unicode 3.2, surrogate characters are illegal';
  376. break;
  377. case UTF8_BAD_UNIOUTRANGE:
  378. return 'Codepoints outside the Unicode range are illegal';
  379. break;
  380. case UTF8_BAD_SEQINCOMPLETE:
  381. return 'Incomplete multi-octet sequence';
  382. break;
  383. }
  384. trigger_error('Unknown error code: '.$code, E_USER_WARNING);
  385. return false;
  386. }