1
0
mirror of https://github.com/php/php-src.git synced 2026-03-26 01:02:25 +01:00
Files
archived-php-src/ext/mbstring/tests/sjis2004_encoding.phpt
Alex Dowad 776296e12f mbstring no longer provides 'long' substitutions for erroneous input bytes
Previously, mbstring had a special mode whereby it would convert
erroneous input byte sequences to output like "BAD+XXXX", where "XXXX"
would be the erroneous bytes expressed in hexadecimal. This mode could
be enabled by calling `mb_substitute_character("long")`.

However, accurately reproducing input byte sequences from the cached
state of a conversion filter is often tricky, and this significantly
complicates the implementation. Further, the means used for passing
the erroneous bytes through to where the "BAD+XXXX" text is generated
only allows for up to 3 bytes to be passed, meaning that some erroneous
byte sequences are truncated anyways.

More to the point, a search of publically available PHP code indicates
that nobody is really using this feature anyways.

Incidentally, this feature also provided error output like "JIS+XXXX"
if the input 'should have' represented a JISX 0208 codepoint, but it
decodes to a codepoint which does not exist in the JISX 0208 charset.
Similarly, specific error output was provided for non-existent
JISX 0212 codepoints, and likewise for JISX 0213, CP932, and a few
other charsets. All of that is now consigned to the flames.

However, "long" error markers also include a somewhat more useful
"U+XXXX" marker for Unicode codepoints which were successfully
decoded from the input text, but cannot be represented in the output
encoding. Those are still supported.

With this change, there is no need to use a variety of special values
in the high bits of a wchar to represent different types of error
values. We can (and will) just use a single error value. This will be
equal to -1.

One complicating factor: Text conversion functions return an integer to
indicate whether the conversion operation should be immediately
aborted, and the magic 'abort' marker is -1. Also, almost all of these
functions would return the received byte/codepoint to indicate success.
That doesn't work with the new error value; if an input filter detects
an error and passes -1 to the output filter, and the output filter
returns it back, that would be taken to mean 'abort'.

Therefore, amend all these functions to return 0 for success.
2021-08-31 13:41:34 +02:00

83 lines
3.2 KiB
PHP

--TEST--
Exhaustive test of SJIS-2004 encoding verification and conversion
--EXTENSIONS--
mbstring
--SKIPIF--
<?php
if (getenv("SKIP_SLOW_TESTS")) die("skip slow test");
?>
--FILE--
<?php
srand(101); /* Make results consistent */
include('encoding_tests.inc');
mb_substitute_character(0x25); // '%'
/* Read in the table of all characters in SJIS-2004 */
$validChars = array(); /* SJIS-2004 string -> UTF-32BE string */
$fromUnicode = array(); /* UTF-16BE -> SJIS-2004 */
$fp = fopen(__DIR__ . '/data/SJIS-2004.txt', 'r+');
while ($line = fgets($fp, 256)) {
if ($line[0] == '#')
continue;
$codepoint2 = null;
if (sscanf($line, "0x%x\tU+%x+%x", $bytes, $codepoint1, $codepoint2) >= 2) {
$sjis = ($bytes < 256) ? chr($bytes) : pack('n', $bytes);
if ($codepoint2) {
$validChars[$sjis] = pack('NN', $codepoint1, $codepoint2);
} else {
/* Two input byte sequences can translate to either a 'halfwidth' or a
* 'fullwidth' version of a character; our implementation of SJIS-2004
* translates them to the fullwidth versions */
if (preg_match('/Fullwidth: U\+([0-9A-F]+)/', $line, $match))
$codepoint1 = hexdec($match[1]);
$validChars[$sjis] = pack('N', $codepoint1);
if ($codepoint1 <= 0xFFFF)
$fromUnicode[pack('n', $codepoint1)] = $sjis;
}
}
}
/* U+007E is TILDE, Shift-JIS 0x8160 is WAVE DASH */
$fromUnicode["\x00\x7E"] = "\x81\x60";
/* U+005C is backslash, Shift-JIS 0x815F is REVERSE SOLIDUS
* (ie. a fancy way to say "backslash") */
$fromUnicode["\x00\x5C"] = "\x81\x5F";
testAllValidChars($validChars, 'SJIS-2004', 'UTF-32BE');
echo "SJIS-2004 verification and conversion works for all valid characters\n";
findInvalidChars($validChars, $invalidChars, $truncated,
array_fill_keys(range(0x81, 0x9F), 2) + array_fill_keys(range(0xE0, 0xFC), 2));
testAllInvalidChars($invalidChars, $validChars, 'SJIS-2004', 'UTF-32BE', "\x00\x00\x00%");
testTruncatedChars($truncated, 'SJIS-2004', 'UTF-32BE', "\x00\x00\x00%");
echo "SJIS-2004 verification and conversion rejects all invalid characters\n";
testAllValidChars($fromUnicode, 'UTF-16BE', 'SJIS-2004', false);
echo "Unicode -> SJIS-2004 conversion works on all valid characters\n";
findInvalidChars($fromUnicode, $invalidChars, $unused, array_fill_keys(range(0, 0xFF), 2));
convertAllInvalidChars($invalidChars, $fromUnicode, 'UTF-16BE', 'SJIS-2004', '%');
echo "Unicode -> SJIS-2004 conversion works on all invalid characters\n";
// Some pairs of Unicode codepoints are represented by a single character in SJIS-2004
// Test the case where the first codepoint looks like it might be one of these pairs...
// but the second one doesn't match
convertValidString("\x30\x4B\x00A", "\x82\xA9A", 'UTF-16BE', 'SJIS-2004', false);
// Test "long" illegal character markers
mb_substitute_character("long");
convertInvalidString("\x80", "%", "SJIS-2004", "UTF-8");
convertInvalidString("\x81\x20", "%", "SJIS-2004", "UTF-8");
convertInvalidString("\xFC\xF5", "%", "SJIS-2004", "UTF-8");
echo "Done!\n";
?>
--EXPECT--
SJIS-2004 verification and conversion works for all valid characters
SJIS-2004 verification and conversion rejects all invalid characters
Unicode -> SJIS-2004 conversion works on all valid characters
Unicode -> SJIS-2004 conversion works on all invalid characters
Done!