iso2022jp_ms_encoding.phpt 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. --TEST--
  2. Exhaustive test of ISO-2022-JP-MS text encoding
  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(444); /* Make results consistent */
  12. include('encoding_tests.inc');
  13. mb_substitute_character(0x25); // '%'
  14. function shiftJISDecode($bytes) {
  15. /* Convert CP932's default Shift-JIS representation to kuten code */
  16. $first = ($bytes >> 8) & 0xFF;
  17. $second = $bytes & 0xFF;
  18. $hi_bits = $first - (($first > 0x9F) ? 0xE0 - 31 : 0x81);
  19. if ($second > 0x9E) {
  20. $kuten = ((($hi_bits << 1) + 0x22) << 8) + ($second - 0x9F + 0x21);
  21. } else if ($second > 0x7F) {
  22. $kuten = ((($hi_bits << 1) + 0x21) << 8) + ($second - 0x80 + 63 + 0x21);
  23. } else {
  24. $kuten = ((($hi_bits << 1) + 0x21) << 8) + ($second - 0x40 + 0x21);
  25. }
  26. return $kuten;
  27. }
  28. /* Read in the table of all characters in CP932 */
  29. $cp932Chars = array(); /* CP932 string -> UTF-32BE string */
  30. $fp = fopen(realpath(__DIR__ . '/data/CP932.txt'), 'r+');
  31. while ($line = fgets($fp, 256)) {
  32. if ($line[0] == '#')
  33. continue;
  34. if (sscanf($line, "0x%x\t0x%x", $bytes, $codepoint) == 2) {
  35. if ($bytes < 256)
  36. continue;
  37. /* ISO-2022-JP-MS only uses the first two ranges of MS vendor extensions */
  38. if ($bytes >= 0xFA00)
  39. continue;
  40. $cp932Chars[pack('n', shiftJISDecode($bytes))] = pack('N', $codepoint);
  41. }
  42. }
  43. /* Windows-932 has many cases where two different kuten codes map to the same
  44. * Unicode codepoints */
  45. foreach ([0x8790, 0x8791, 0x8792, 0x8795, 0x8796, 0x8797, 0x879A, 0x879B, 0x879C, 0xEEF9] as $i) {
  46. $bytes = pack('n', shiftJISDecode($i));
  47. $nonInvertible[$bytes] = $cp932Chars[$bytes];
  48. }
  49. /* Add User Defined codes (which use ESC $ ( ? escape sequence)) */
  50. $udcChars = array();
  51. for ($cp = 0xE000; $cp < (0xE000 + (20 * 94)); $cp++) {
  52. $i = $cp - 0xE000;
  53. $bytes = (((int)($i / 94) + 0x21) << 8) + (($i % 94) + 0x21);
  54. $udcChars[pack('n', $bytes)] = pack('N', $cp);
  55. }
  56. /* Read in table of all characters in JISX-0201 charset */
  57. $jisx0201Chars = array(); /* JISX0201 -> UTF-32BE */
  58. $fp = fopen(realpath(__DIR__ . '/data/JISX0201.txt'), 'r+');
  59. while ($line = fgets($fp, 256)) {
  60. if ($line[0] == '#')
  61. continue;
  62. if (sscanf($line, "0x%x\t0x%x", $byte, $codepoint) == 2)
  63. $jisx0201Chars[chr($byte)] = pack('N', $codepoint);
  64. }
  65. function testValid($from, $to, $encoding, $bothWays = true) {
  66. identifyValidString($from, $encoding);
  67. convertValidString($from, $to, $encoding, 'UTF-32BE', false);
  68. if ($bothWays) {
  69. /* ESC ( B at the beginning is redundant, since ASCII mode is the default */
  70. if (substr($from, 0, 3) == "\x1B(B")
  71. $from = substr($from, 3, strlen($from) - 3);
  72. /* If the string switches to a different charset, it should switch back to
  73. * ASCII at the end */
  74. if (strpos($from, "\x1B\$B") !== false || strpos($from, "\x1B(I") !== false || strpos($from, "\x1B\$@") !== false || strpos($from, "\x1B\$(B") !== false || strpos($from, "\x1B\$(@") !== false || strpos($from, "\x1B\$(?") !== false)
  75. $from .= "\x1B(B";
  76. convertValidString($to, $from, 'UTF-32BE', $encoding, false);
  77. }
  78. }
  79. function testInvalid($from, $to, $encoding) {
  80. testInvalidString($from, $to, $encoding, 'UTF-32BE');
  81. }
  82. for ($i = 0; $i < 0x80; $i++) {
  83. if ($i == 0x1B)
  84. continue;
  85. testValid(chr($i), "\x00\x00\x00" . chr($i), 'ISO-2022-JP-MS');
  86. testValid("\x1B(B" . chr($i), "\x00\x00\x00" . chr($i), 'ISO-2022-JP-MS', false);
  87. testValid("\x1B(J" . chr($i), "\x00\x00\x00" . chr($i), 'ISO-2022-JP-MS', false);
  88. }
  89. for ($i = 0x80; $i < 256; $i++) {
  90. if ($i >= 0xA1 && $i <= 0xDF) // We convert single bytes from 0xA1-0xDF as JIS X 0201 kana
  91. continue;
  92. testInvalid(chr($i), "\x00\x00\x00%", 'ISO-2022-JP-MS');
  93. testInvalid("\x1B(B" . chr($i), "\x00\x00\x00%", 'ISO-2022-JP-MS');
  94. testInvalid("\x1B(J" . chr($i), "\x00\x00\x00%", 'ISO-2022-JP-MS');
  95. }
  96. echo "ASCII support OK\n";
  97. /* All valid JIS X 0201 characters
  98. * Those with a 1 in the high bit are JIS X 0201 kana */
  99. foreach ($jisx0201Chars as $jisx0201 => $utf32BE) {
  100. if (ord($jisx0201) >= 128) {
  101. $kana = chr(ord($jisx0201) - 128);
  102. testValid("\x1B(I" . $kana, $utf32BE, 'ISO-2022-JP-MS', false);
  103. testValid($jisx0201, $utf32BE, 'ISO-2022-JP-MS', false);
  104. }
  105. }
  106. for ($i = 0x80; $i < 256; $i++) {
  107. if ($i >= 0xA1 && $i <= 0xDF)
  108. continue;
  109. testInvalid("\x1B(I" . chr($i), "\x00\x00\x00%", 'ISO-2022-JP-MS');
  110. testInvalid("\x1B(J" . chr($i), "\x00\x00\x00%", 'ISO-2022-JP-MS');
  111. }
  112. echo "JIS X 0201 support OK\n";
  113. function testAllValidCharsWithPrefix($validChars, $prefix, $bothWays) {
  114. $good = array_keys($validChars);
  115. shuffle($good);
  116. while (!empty($good)) {
  117. $length = min(rand(5,10), count($good));
  118. $from = $to = '';
  119. while ($length--) {
  120. $goodChar = array_pop($good);
  121. $from .= $goodChar;
  122. $to .= $validChars[$goodChar];
  123. }
  124. testValid($prefix . $from, $to, 'ISO-2022-JP-MS', $bothWays);
  125. }
  126. }
  127. $validChars = $cp932Chars;
  128. /* We allow ASCII/JIS X 0201 characters to appear even in JIS X 0208 mode */
  129. for ($i = 0; $i <= 0x7F; $i++)
  130. $validChars[chr($i)] = chr($i);
  131. for ($i = 0xA1; $i <= 0xDF; $i++)
  132. $validChars[chr($i)] = $jisx0201Chars[chr($i)];
  133. $lenTable = array_fill_keys(range(0xE0, 0xFC), 2) + array_fill_keys(range(0x81, 0x9F), 2);
  134. findInvalidChars($validChars, $invalidChars, $truncatedChars, $lenTable);
  135. foreach ($nonInvertible as $bytes => $char)
  136. unset($cp932Chars[$bytes]);
  137. testAllValidCharsWithPrefix($cp932Chars, "\x1B\$B", true);
  138. testAllValidCharsWithPrefix($nonInvertible, "\x1B\$B", false);
  139. foreach (array_keys($invalidChars) as $invalid) {
  140. $firstByte = ord($invalid[0]);
  141. if (($firstByte > 0x80 && $firstByte < 0xA0) || $firstByte >= 0xE0) {
  142. /* The first byte of this 2-byte character will be rejected and result in % being sent
  143. * to the output. Then the second byte will do something else. It is easier to write the
  144. * test if we only check with the 1st byte. */
  145. testInvalidString("\x1B\$B" . $invalid[0], "\x00\x00\x00%", 'ISO-2022-JP-MS', 'UTF-32BE');
  146. } else {
  147. testInvalidString("\x1B\$B" . $invalid, "\x00\x00\x00%", 'ISO-2022-JP-MS', 'UTF-32BE');
  148. }
  149. }
  150. foreach (array_keys($truncatedChars) as $truncated)
  151. testInvalidString("\x1B\$B" . $truncated, "\x00\x00\x00%", 'ISO-2022-JP-MS', 'UTF-32BE');
  152. echo "JIS X 0208 (with MS extensions) support OK\n";
  153. $validChars = $udcChars;
  154. for ($i = 0; $i <= 0x7F; $i++)
  155. $validChars[chr($i)] = chr($i);
  156. for ($i = 0xA1; $i <= 0xDF; $i++)
  157. $validChars[chr($i)] = $jisx0201Chars[chr($i)];
  158. findInvalidChars($validChars, $invalidChars, $truncatedChars, array_fill_keys(range(0x21, 0x7F), 2));
  159. testAllValidCharsWithPrefix($udcChars, "\x1B\$(?", true);
  160. foreach (array_keys($invalidChars) as $invalid) {
  161. $firstByte = ord(substr($invalid, 0, 1));
  162. if (($firstByte > 0x80 && $firstByte < 0xA0) || $firstByte >= 0xE0) {
  163. testInvalidString("\x1B\$(?" . $invalid[0], "\x00\x00\x00%", 'ISO-2022-JP-MS', 'UTF-32BE');
  164. } else {
  165. testInvalidString("\x1B\$(?" . $invalid, "\x00\x00\x00%", 'ISO-2022-JP-MS', 'UTF-32BE');
  166. }
  167. }
  168. foreach (array_keys($truncatedChars) as $truncated)
  169. testInvalidString("\x1B\$(?" . $truncated, "\x00\x00\x00%", 'ISO-2022-JP-MS', 'UTF-32BE');
  170. echo "UDC support OK\n";
  171. testValidString("\x00\xA5", "\x1B\$B!o\x1B(B", "UTF-16BE", "ISO-2022-JP-MS", false);
  172. testValidString("\x20\x3E", "\x1B\$B!1\x1B(B", "UTF-16BE", "ISO-2022-JP-MS", false);
  173. testValidString("\xFF\x5E", "\x1B\$B!A\x1B(B", "UTF-16BE", "ISO-2022-JP-MS", false);
  174. echo "Other mappings from Unicode -> ISO-2022-JP-MS OK\n";
  175. // Alternative escape sequences for 2-byte characters
  176. testValidString("\x1B\$(B\x21\x21", "\x30\x00", "ISO-2022-JP-MS", "UTF-16BE", false);
  177. testValidString("\x1B\$(@\x21\x21", "\x30\x00", "ISO-2022-JP-MS", "UTF-16BE", false);
  178. // Switching between different character types
  179. testValidString("\x00a\x00b\x00c\xFF\x61\x00a\x00b\x00c", "abc\x1B(I\x21\x1B(Babc", "UTF-16BE", "ISO-2022-JP-MS", false);
  180. // Test "long" illegal character markers
  181. mb_substitute_character("long");
  182. convertInvalidString("\xE0", "%", "ISO-2022-JP-MS", "UTF-8");
  183. // Invalid escapes:
  184. convertInvalidString("\x1B", "%", "ISO-2022-JP-MS", "UTF-8");
  185. convertInvalidString("\x1B.", "%", "ISO-2022-JP-MS", "UTF-8");
  186. convertInvalidString("\x1B\$", "%", "ISO-2022-JP-MS", "UTF-8");
  187. convertInvalidString("\x1B\$.", "%", "ISO-2022-JP-MS", "UTF-8");
  188. convertInvalidString("\x1B(", "%", "ISO-2022-JP-MS", "UTF-8");
  189. convertInvalidString("\x1B(.", "%", "ISO-2022-JP-MS", "UTF-8");
  190. convertInvalidString("\x1B\$(", "%", "ISO-2022-JP-MS", "UTF-8");
  191. convertInvalidString("\x1B\$(X", "%", "ISO-2022-JP-MS", "UTF-8");
  192. convertInvalidString("\x1B\$B\x9F", "%", "ISO-2022-JP-MS", "UTF-8"); // 0x9F does not start any 2-byte character
  193. echo "Done!\n";
  194. ?>
  195. --EXPECT--
  196. ASCII support OK
  197. JIS X 0201 support OK
  198. JIS X 0208 (with MS extensions) support OK
  199. UDC support OK
  200. Other mappings from Unicode -> ISO-2022-JP-MS OK
  201. Done!