iso2022jp_2004_encoding.phpt 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. --TEST--
  2. Exhaustive test of ISO-2022-JP-2004 encoding verification and conversion
  3. --EXTENSIONS--
  4. mbstring
  5. --SKIPIF--
  6. <?php
  7. if (getenv("SKIP_SLOW_TESTS")) die("skip slow test");
  8. ?>
  9. --FILE--
  10. <?php
  11. srand(111); /* Make results consistent */
  12. include('encoding_tests.inc');
  13. mb_substitute_character(0x25); // '%'
  14. /* Read in table of all characters in JISX-0208 charset */
  15. $jisx0208Chars = array(); /* JISX0208 -> UTF-16BE */
  16. $fp = fopen(__DIR__ . '/data/JISX0208.txt', 'r+');
  17. while ($line = fgets($fp, 256)) {
  18. if ($line[0] == '#')
  19. continue;
  20. if (sscanf($line, "0x%x\t0x%x\t0x%x", $shiftJIS, $jis0208Code, $unicodeCP) == 3) {
  21. $jisx0208Chars[pack('n', $jis0208Code)] = pack('n', $unicodeCP);
  22. }
  23. }
  24. /* The JIS X 0208 character set does not have a single, straightforward
  25. * mapping to the Unicode character set
  26. * mbstring converts one character differently from the mappings in
  27. * data/JISX0208.txt, which comes from the Unicode Consortium */
  28. /* 0x2140 is a backslash; this can be mapped to 0x005C for an ordinary
  29. * backslash, or 0xFF3C for a _fullwidth_ one */
  30. $jisx0208Chars["\x21\x40"] = "\xFF\x3C";
  31. /* Single bytes from 0x0-0x20 are allowed */
  32. for ($i = 0; $i <= 0x20; $i++) {
  33. if ($i != 0x1B)
  34. $jisx0208Chars[chr($i)] = "\x00" . chr($i);
  35. }
  36. /* As is 0x7F */
  37. $jisx0208Chars["\x7F"] = "\x00\x7F";
  38. /* Now read table of JISX-0213:2004 plane 1 and JISX-0213:2000 plane 2 chars */
  39. $jisx0213_2004_1Chars = array();
  40. $jisx0213_2000_2Chars = array();
  41. $fp = fopen(__DIR__ . '/data/ISO-2022-JP-2004-JISX0213.txt', 'r+');
  42. while ($line = fgets($fp, 256)) {
  43. if ($line[0] == '#')
  44. continue;
  45. $cp2 = null;
  46. if (sscanf($line, "%d-%x\tU+%x+%x", $type, $bytes, $cp1, $cp2) >= 3) {
  47. if ($cp1 <= 0xFFFF)
  48. $unicode = pack('n', $cp1);
  49. else
  50. $unicode = mb_convert_encoding(pack('N', $cp1), 'UTF-16BE', 'UTF-32BE');
  51. if ($cp2)
  52. $unicode .= pack('n', $cp2);
  53. if ($type == 3)
  54. $jisx0213_2004_1Chars[pack('n', $bytes)] = $unicode;
  55. else if ($type == 4)
  56. $jisx0213_2000_2Chars[pack('n', $bytes)] = $unicode;
  57. }
  58. }
  59. /* JISX 0213 plane 1 0x2131 is an overline; Unicode has a halfwidth overline
  60. * at 0x203E and a fullwidth overline at 0xFFE3
  61. * We'll use the fullwidth version when converting JISX 0213 to Unicode */
  62. $jisx0213_2004_1Chars["\x21\x31"] = "\xFF\xE3";
  63. /* Same deal with the Yen sign; use the fullwidth one */
  64. $jisx0213_2004_1Chars["\x21\x6F"] = "\xFF\xE5";
  65. /* Since JISX 0213 is an extension of JISX 0208, allow the same single-byte chars */
  66. for ($i = 0; $i <= 0x20; $i++) {
  67. if ($i != 0x1B)
  68. $jisx0213_2004_1Chars[chr($i)] = "\x00" . chr($i);
  69. }
  70. $jisx0213_2004_1Chars["\x7F"] = "\x00\x7F";
  71. for ($i = 0; $i <= 0x20; $i++) {
  72. if ($i != 0x1B)
  73. $jisx0213_2000_2Chars[chr($i)] = "\x00" . chr($i);
  74. }
  75. $jisx0213_2000_2Chars["\x7F"] = "\x00\x7F";
  76. function testValid($from, $to, $bothWays = true) {
  77. identifyValidString($from, 'ISO-2022-JP-2004');
  78. convertValidString($from, $to, 'ISO-2022-JP-2004', 'UTF-16BE', false);
  79. if ($bothWays) {
  80. /* Try going in the opposite direction too
  81. * ESC ( B at the beginning of ISO-2022-JP-2004 string is redundant,
  82. * since ASCII mode is the default */
  83. if (substr($from, 0, 3) == "\x1B(B")
  84. $from = substr($from, 3, strlen($from) - 3);
  85. /* If the ISO-2022-JP-2004 string switches to a different charset, it
  86. * should switch back to ASCII at the end */
  87. if (strpos($from, "\x1B\$B") !== false || strpos($from, "\x1B\$(Q") !== false || strpos($from, "\x1B\$(P") !== false)
  88. $from .= "\x1B(B";
  89. convertValidString($to, $from, 'UTF-16BE', 'ISO-2022-JP-2004', false);
  90. }
  91. }
  92. function testInvalid($from, $to) {
  93. testInvalidString($from, $to, 'ISO-2022-JP-2004', 'UTF-16BE');
  94. }
  95. /* Try all ASCII characters */
  96. for ($i = 0; $i <= 0x7F; $i++) {
  97. if ($i == 0x1B)
  98. continue;
  99. testValid(chr($i), "\x00" . chr($i));
  100. }
  101. /* Try all ASCII characters, with explicit ASCII escape */
  102. for ($i = 0; $i <= 0x7F; $i++) {
  103. if ($i == 0x1B)
  104. continue;
  105. testValid("\x1B(B" . chr($i), "\x00" . chr($i));
  106. }
  107. echo "Encoding verification and conversion works for all ASCII characters\n";
  108. /* Try a bare ESC */
  109. identifyInvalidString("\x1B", 'ISO-2022-JP-2004');
  110. /* Try all non-ASCII, non-ESC single bytes */
  111. for ($i = 0x80; $i <= 0xFF; $i++) {
  112. testInvalid(chr($i), "\x00%");
  113. }
  114. echo "Encoding verification and conversion rejects all invalid single bytes\n";
  115. /* All valid JISX0208 characters */
  116. foreach ($jisx0208Chars as $jisx0208 => $utf16BE) {
  117. /* Since JIS X 0213 charset is a superset of JIS X 0208, we don't bother
  118. * using JIS X 0208 when converting Unicode to ISO-2022-JP-2004
  119. * Therefore, don't test conversion in both directions here */
  120. testValid("\x1B\$B" . $jisx0208, $utf16BE, false);
  121. }
  122. /* All invalid 1-byte JISX0208 characters */
  123. for ($i = 0; $i < 256; $i++) {
  124. if ($i == 0x1B)
  125. continue;
  126. if ($i >= 0x21 && $i <= 0x7E)
  127. continue;
  128. $testString = chr($i);
  129. if (!isset($jisx0208Chars[$testString])) {
  130. testInvalid("\x1B\$B" . $testString, "\x00%");
  131. }
  132. }
  133. /* All invalid 2-byte JISX0208 characters */
  134. for ($i = 0x21; $i <= 0x7E; $i++) {
  135. for ($j = 0; $j < 256; $j++) {
  136. $testString = chr($i) . chr($j);
  137. if (!isset($jisx0208Chars[$testString])) {
  138. testInvalid("\x1B\$B" . $testString, "\x00%");
  139. }
  140. }
  141. }
  142. echo "Encoding verification and conversion work on JISX-0208 characters\n";
  143. /* All JISX0213 plane 1 characters */
  144. foreach ($jisx0213_2004_1Chars as $jisx0213_2004 => $utf16BE) {
  145. /* For single bytes, don't try conversion in both directions */
  146. testValid("\x1B$(Q" . $jisx0213_2004, $utf16BE, $utf16BE > "\x01\x00");
  147. }
  148. /* All invalid 2-byte JISX0213 plane 1 characters */
  149. for ($i = 0x21; $i <= 0x7E; $i++) {
  150. for ($j = 0; $j < 256; $j++) {
  151. $testString = chr($i) . chr($j);
  152. if (!isset($jisx0213_2004_1Chars[$testString])) {
  153. testInvalid("\x1B$(Q" . $testString, "\x00%");
  154. }
  155. }
  156. }
  157. echo "Encoding verification and conversion work on JISX-0213:2004 plane 1 characters\n";
  158. /* All JISX0213 plane 2 characters */
  159. foreach ($jisx0213_2000_2Chars as $jisx0213_2000 => $utf16BE) {
  160. /* For single bytes, don't try conversion in both directions */
  161. testValid("\x1B$(P" . $jisx0213_2000, $utf16BE, $utf16BE > "\x01\x00");
  162. }
  163. /* All invalid 2-byte JISX0213 plane 2 characters */
  164. for ($i = 0x21; $i <= 0x7E; $i++) {
  165. for ($j = 0; $j < 256; $j++) {
  166. $testString = chr($i) . chr($j);
  167. if (!isset($jisx0213_2000_2Chars[$testString])) {
  168. testInvalid("\x1B$(P" . $testString, "\x00%");
  169. }
  170. }
  171. }
  172. echo "Encoding verification and conversion work on JISX-0213:2000 plane 2 characters\n";
  173. /* All possible escape sequences */
  174. $validEscapes = ["\x1B\$B" => true, "\x1B(B" => true, "\x1B$(Q" => true, "\x1B$(P" => true];
  175. for ($i = 0; $i <= 0xFF; $i++) {
  176. for ($j = 0; $j <= 0xFF; $j++) {
  177. $escapeSequence = "\x1B" . chr($i) . chr($j);
  178. if (isset($validEscapes[$escapeSequence])) {
  179. testValid($escapeSequence, "", false);
  180. } else {
  181. identifyInvalidString($escapeSequence, 'ISO-2022-JP-2004');
  182. }
  183. }
  184. }
  185. echo "All escape sequences work as expected\n";
  186. identifyInvalidString("\x1B$", 'ISO-2022-JP-2004');
  187. identifyInvalidString("\x1B(", 'ISO-2022-JP-2004');
  188. identifyInvalidString("\x1B$(", 'ISO-2022-JP-2004');
  189. echo "All incomplete escape sequences are rejected\n";
  190. /* Try all combinations of 2 different charsets in the same string */
  191. $ascii = "\x1B(Ba";
  192. $jisx0208 = "\x1B\$B" . array_keys($jisx0208Chars)[rand(0,1000)];
  193. $jisx0213_1 = "\x1B$(Q" . array_keys($jisx0213_2004_1Chars)[rand(0,1000)];
  194. $jisx0213_2 = "\x1B$(P" . array_keys($jisx0213_2000_2Chars)[rand(0,1000)];
  195. $differentCharsets = [$ascii, $jisx0208, $jisx0213_1, $jisx0213_2];
  196. foreach ($differentCharsets as $a) {
  197. foreach ($differentCharsets as $b) {
  198. identifyValidString($a . $b, 'ISO-2022-JP-2004');
  199. }
  200. }
  201. /* Try redundant escape sequences (switching mode but including any characters
  202. * in the new mode) */
  203. $ascii_Esc = "\x1B(B";
  204. $jisx0208_Esc = "\x1B\$B";
  205. $jisx0213_1_Esc = "\x1B$(Q";
  206. $jisx0213_2_Esc = "\x1B$(P";
  207. $differentCharsets = [$ascii_Esc, $jisx0208_Esc, $jisx0213_1_Esc, $jisx0213_2_Esc];
  208. foreach ($differentCharsets as $a) {
  209. foreach ($differentCharsets as $b) {
  210. testValid($a . $b, "", false);
  211. }
  212. }
  213. echo "Combining multiple charsets in the same string works as expected\n";
  214. /* Try ending in the middle of a JISX0208 character */
  215. testInvalid(substr($jisx0208, 0, strlen($jisx0208) - 1), "\x00%");
  216. /* Try ending in the middle of a JISX0213 plane 1 character */
  217. testInvalid(substr($jisx0213_1, 0, strlen($jisx0213_1) - 1), "\x00%");
  218. /* Try ending in the middle of a JISX0213 plane 2 character */
  219. testInvalid(substr($jisx0213_2, 0, strlen($jisx0213_2) - 1), "\x00%");
  220. echo "Strings with truncated multi-byte characters are rejected\n";
  221. /* We have tried converting all kinds of strings with single characters;
  222. * now try some random examples of strings with multiple characters */
  223. $jisx0208 = array_keys($jisx0208Chars);
  224. shuffle($jisx0208);
  225. $jisx0213_1 = array_keys($jisx0213_2004_1Chars);
  226. shuffle($jisx0213_1);
  227. $jisx0213_2 = array_keys($jisx0213_2000_2Chars);
  228. shuffle($jisx0213_2);
  229. for ($i = 0; $i < 100; $i++) {
  230. $size = rand(5,20);
  231. $testString = '';
  232. $convertsTo = '';
  233. /* Build a string from a random combination of characters in the supported
  234. * character sets */
  235. while ($size--) {
  236. $type = rand(0,4);
  237. $chars = rand(0,10);
  238. if ($type == 0) { /* ASCII */
  239. $testString .= "\x1B(B";
  240. while ($chars--) {
  241. $ascii = chr(rand(0x20, 0x7E));
  242. $testString .= $ascii;
  243. $convertsTo .= "\x00" . $ascii;
  244. }
  245. } else if ($type == 1) { /* JIS X 0208 */
  246. $testString .= "\x1B\$B";
  247. while ($chars--) {
  248. $jis = array_pop($jisx0208);
  249. $testString .= $jis;
  250. $convertsTo .= $jisx0208Chars[$jis];
  251. }
  252. } else if ($type == 2) { /* JIS X 0213:2004 plane 1 */
  253. $testString .= "\x1B$(Q";
  254. while ($chars--) {
  255. $jis = array_pop($jisx0213_1);
  256. $testString .= $jis;
  257. $convertsTo .= $jisx0213_2004_1Chars[$jis];
  258. }
  259. } else { /* JIS X 0213:2000 plane 2 */
  260. $testString .= "\x1B$(P";
  261. while ($chars-- && !empty($jisx0213_2)) {
  262. $jis = array_pop($jisx0213_2);
  263. $testString .= $jis;
  264. $convertsTo .= $jisx0213_2000_2Chars[$jis];
  265. }
  266. }
  267. }
  268. testValid($testString, $convertsTo, false);
  269. }
  270. // Test "long" illegal character markers
  271. mb_substitute_character("long");
  272. convertInvalidString("\xE0", "%", "ISO-2022-JP-2004", "UTF-8");
  273. convertInvalidString("\x1B\$(X", "%", "ISO-2022-JP-2004", "UTF-8"); // Invalid escape
  274. convertInvalidString("\x1B\$B!", "%", "ISO-2022-JP-2004", "UTF-8"); // Truncated character
  275. echo "All done!\n";
  276. ?>
  277. --EXPECT--
  278. Encoding verification and conversion works for all ASCII characters
  279. Encoding verification and conversion rejects all invalid single bytes
  280. Encoding verification and conversion work on JISX-0208 characters
  281. Encoding verification and conversion work on JISX-0213:2004 plane 1 characters
  282. Encoding verification and conversion work on JISX-0213:2000 plane 2 characters
  283. All escape sequences work as expected
  284. All incomplete escape sequences are rejected
  285. Combining multiple charsets in the same string works as expected
  286. Strings with truncated multi-byte characters are rejected
  287. All done!