osd: Added multibyte check to osd_uchar_from_osdchar to fix decoding ASCII text. (#9536)

This allows ASCII INI files to be parsed on Windows systems set to use a double-byte code page.  It should also work with correctly-encoded Shift-JIS, GB2312, Big5 and EUC-KR.  It won’t work for more complex variable-length encodings, or when the input is not correctly encoded.
This commit is contained in:
987123879113 2022-04-10 01:48:27 +09:00 committed by GitHub
parent ec3cd170e7
commit c161f61973
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -254,6 +254,11 @@ std::string from_wstring(const WCHAR *s)
int osd_uchar_from_osdchar(char32_t *uchar, const char *osdchar, size_t count)
{
// FIXME: Does not handle charsets that use variable lengths encodings such
// as example GB18030 or UTF-8.
// FIXME: Assumes all characters can be converted into a single wchar_t
// which may not always be the case such as with surrogate pairs.
WCHAR wch;
CPINFO cp;
@ -261,7 +266,7 @@ int osd_uchar_from_osdchar(char32_t *uchar, const char *osdchar, size_t count)
goto error;
// The multibyte char can't be bigger than the max character size
count = std::min(count, size_t(cp.MaxCharSize));
count = std::min(count, size_t(IsDBCSLeadByte(*osdchar) ? cp.MaxCharSize : 1));
if (MultiByteToWideChar(CP_ACP, 0, osdchar, static_cast<DWORD>(count), &wch, 1) == 0)
goto error;