Encoding: Report bad UTF-16 surrogates and Shift-JIS lead bytes as invalid

UTF16_Type::next() assumed every surrogate came in a valid pair, so a lone
high surrogate swallowed the unit after it, and one right before the
terminator stepped over it and kept reading. It now returns INVALID and
consumes only the bad unit, which sceCccDecodeUTF16() already checks for.

ShiftJIS::next() returned 0xA0 and 0xFD-0xFF as single-byte characters.
Like 0x80, which it already rejected, they encode nothing (the PSP's JIS
to UCS table in pspautotests/tests/ccc/jis2ucs.bin has no entry for any of
them), so they're INVALID too.

Only sceCcc uses these decoders.
This commit is contained in:
mariobob committed 2026-10-16 22:05:12 +02:00
1 parent 62b2eaa3fd
commit a42ed5e312
3 files changed
+70 -7

No files matched your search

+8 -2
View File
@@ -33,11 +33,17 @@ struct ShiftJIS {
} else if (j < 0xFD) {
row = ((j & 0x7F) << 1) - 0x1B;
} else {
return j;
return INVALID;
}
break;
// Anything else (i.e. <= 0x7x, 0xAx, 0xBx, 0xCx, and 0xDx) is JIS X 0201, return directly.
case 0xA:
if (j == 0xA0) {
return INVALID;
}
return j;
// Anything else (i.e. <= 0x7x, 0xBx, 0xCx, and 0xDx) is JIS X 0201, return directly.
default:
return j;
}
+10 -5
View File
@@ -25,12 +25,17 @@ public:
char32_t next() {
const char32_t u = UTF16_Swap<is_little>(c_[index_++]);
// Surrogate pair. UTF-16 is so simple. We assume it's valid.
if ((u & 0xF800) == 0xD800) {
return 0x10000 + (((u & 0x3FF) << 10) | (UTF16_Swap<is_little>(c_[index_++]) & 0x3FF));
if ((u & 0xF800) != 0xD800) {
return u;
}
return u;
// A surrogate is only valid as a high one followed by a low one.
const char32_t low = UTF16_Swap<is_little>(c_[index_]);
if ((u & 0xFC00) != 0xD800 || (low & 0xFC00) != 0xDC00) {
return INVALID;
}
index_++;
return 0x10000 + (((u & 0x3FF) << 10) | (low & 0x3FF));
}
bool end() const {