1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-07-01 11:32:48 -05:00

Make decode_utf8() read from a BinarySource.

This enables it to handle data that isn't presented as a
NUL-terminated string.

In particular, the NUL byte can appear _within_ the string and be
correctly translated to the NUL wide character. So I've been able to
remove the awkwardness in the test rig of having to include the
terminating NUL in every test to ensure NUL has been tested, and
instead, insert a single explicit test for it.

Similarly to the previous commit, the simplification at the (one) call
site gives me a strong feeling of 'this is what the API should have
been all along'!
This commit is contained in:
Simon Tatham
2022-11-09 19:01:04 +00:00
parent d89f2bfc55
commit 69e217d23a
4 changed files with 41 additions and 35 deletions

View File

@ -1357,18 +1357,15 @@ int mb_to_wc(int codepage, int flags, const char *mbstr, int mblen,
* codepage is UTF-8, we can do the translation ourselves.
*/
if (codepage == CP_UTF8 && mblen > 0 && wclen > 0) {
BinarySource src[1];
BinarySource_BARE_INIT(src, mbstr, mblen);
size_t remaining = wclen;
wchar_t *p = wcstr;
while (mblen > 0) {
char utfbuf[7];
int thissize = mblen < 6 ? mblen : 6;
memcpy(utfbuf, mbstr, thissize);
utfbuf[thissize] = '\0';
const char *utfptr = utfbuf;
while (get_avail(src)) {
wchar_t wcbuf[2];
size_t nwc = decode_utf8_to_wchar(&utfptr, wcbuf);
size_t nwc = decode_utf8_to_wchar(src, wcbuf);
for (size_t i = 0; i < nwc; i++) {
if (remaining > 0) {
@ -1378,9 +1375,6 @@ int mb_to_wc(int codepage, int flags, const char *mbstr, int mblen,
return p - wcstr;
}
}
mbstr += (utfptr - utfbuf);
mblen -= (utfptr - utfbuf);
}
return p - wcstr;