mirror of
https://github.com/php/php-src.git
synced 2026-04-01 13:12:16 +02:00
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.
81 lines
3.1 KiB
PHP
81 lines
3.1 KiB
PHP
--TEST--
|
|
Exhaustive test of Shift-JIS encoding verification and conversion
|
|
--EXTENSIONS--
|
|
mbstring
|
|
--SKIPIF--
|
|
<?php
|
|
if (getenv("SKIP_SLOW_TESTS")) die("skip slow test");
|
|
?>
|
|
--FILE--
|
|
<?php
|
|
srand(999); /* Make results consistent */
|
|
include('encoding_tests.inc');
|
|
mb_substitute_character(0x25); // '%'
|
|
|
|
/* Read in the table of all characters in Shift-JIS */
|
|
readConversionTable(__DIR__ . '/data/SHIFTJIS.txt', $validChars, $fromUnicode);
|
|
|
|
for ($i = 0; $i < 0x20; $i++) {
|
|
$validChars[chr($i)] = "\x00" . chr($i);
|
|
$fromUnicode["\x00" . chr($i)] = chr($i);
|
|
}
|
|
|
|
/* U+007E is TILDE; convert to Shift-JIS 0x8160 (WAVE DASH) */
|
|
$fromUnicode["\x00\x7E"] = "\x81\x60";
|
|
/* DEL character */
|
|
$validChars["\x7F"] = "\x00\x7F";
|
|
$fromUnicode["\x00\x7F"] = "\x7F";
|
|
/* U+00AF is MACRON; Shift-JIS 0x7E is overline */
|
|
$fromUnicode["\x00\xAF"] = "\x7E";
|
|
/* Use fullwidth reverse solidus, not (halfwidth) backslash (0x5C) */
|
|
$validChars["\x81\x5F"] = "\xFF\x3C";
|
|
$fromUnicode["\xFF\x3C"] = "\x81\x5F";
|
|
/* Unicode has both halfwidth and fullwidth NOT SIGN; convert both of them
|
|
* to JIS X 0208 NOT SIGN */
|
|
$fromUnicode["\xFF\xE2"] = "\x81\xCA";
|
|
/* Likewise for fullwidth and halfwidth POUND SIGN */
|
|
$fromUnicode["\xFF\xE1"] = "\x81\x92";
|
|
/* Likewise for fullwidth and halfwidth CENT SIGN */
|
|
$fromUnicode["\xFF\xE0"] = "\x81\x91";
|
|
/* Convert Unicode FULLWIDTH TILDE to JIS X 0208 WAVE DASH */
|
|
$fromUnicode["\xFF\x5E"] = "\x81\x60";
|
|
/* Convert Unicode FULLWIDTH HYPHEN-MINUS to JIS X 0208 MINUS SIGN */
|
|
$fromUnicode["\xFF\x0D"] = "\x81\x7C";
|
|
/* Convert Unicode PARALLEL TO to JIS X 0208 DOUBLE VERTICAL LINE */
|
|
$fromUnicode["\x22\x25"] = "\x81\x61";
|
|
|
|
testAllValidChars($validChars, 'Shift-JIS', 'UTF-16BE');
|
|
echo "SJIS verification and conversion works on all valid characters\n";
|
|
|
|
findInvalidChars($validChars, $invalidChars, $truncated,
|
|
array_fill_keys(range(0x81, 0x9F), 2) + array_fill_keys(range(0xE0, 0xEF), 2));
|
|
testAllInvalidChars($invalidChars, $validChars, 'Shift-JIS', 'UTF-16BE', "\x00%");
|
|
testTruncatedChars($truncated, 'Shift-JIS', 'UTF-16BE', "\x00%");
|
|
echo "SJIS verification and conversion works on all invalid characters\n";
|
|
|
|
testAllValidChars($fromUnicode, 'UTF-16BE', 'Shift-JIS', false);
|
|
echo "Unicode -> SJIS conversion works on all valid characters\n";
|
|
|
|
findInvalidChars($fromUnicode, $invalidChars, $unused, array_fill_keys(range(0, 0xFF), 2));
|
|
convertAllInvalidChars($invalidChars, $fromUnicode, 'UTF-16BE', 'Shift-JIS', '%');
|
|
echo "Unicode -> SJIS conversion works on all invalid characters\n";
|
|
|
|
testValidString("\xFF\x5E", "\x81\x60", 'UTF-16BE', 'SJIS', false);
|
|
echo "Other mappings from Unicode -> SJIS are OK\n";
|
|
|
|
// Test "long" illegal character markers
|
|
mb_substitute_character("long");
|
|
convertInvalidString("\x80", "%", "Shift-JIS", "UTF-8");
|
|
convertInvalidString("\x81\x20", "%", "Shift-JIS", "UTF-8");
|
|
convertInvalidString("\xEA\xA9", "%", "Shift-JIS", "UTF-8");
|
|
|
|
echo "Done!\n";
|
|
?>
|
|
--EXPECT--
|
|
SJIS verification and conversion works on all valid characters
|
|
SJIS verification and conversion works on all invalid characters
|
|
Unicode -> SJIS conversion works on all valid characters
|
|
Unicode -> SJIS conversion works on all invalid characters
|
|
Other mappings from Unicode -> SJIS are OK
|
|
Done!
|