QLocal8Bit::convertFromUnicode[win]: Pre 2Gi changes

As we did for convertToUnicode, we do some smaller changes, like
increasing indentation, and switching to using pointers and calculating
the input-size in this commit, so that the real changes in the next
commit are (hopefully) easier to read.

Pick-to: 6.6 6.5
Task-number: QTBUG-105105
Change-Id: I3bf1a487f63a3e24efd7a945152647dd8fc0aca8
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
(cherry picked from commit cba0efc2705af1ef027603f7ee4cb60f184412d8)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Mårten Nordheim 2023-11-01 17:15:45 +01:00
parent 22f2fcd354
commit 5e882b5de9

View File

@ -1504,31 +1504,40 @@ QByteArray QLocal8Bit::convertFromUnicode_sys(QStringView in, quint32 codePage,
Q_ASSERT(uclen > 0);
int len = 0;
while (!(len = WideCharToMultiByte(codePage, 0, ch, int(uclen), out, int(outlen), nullptr,
nullptr))) {
int r = GetLastError();
if (r == ERROR_INSUFFICIENT_BUFFER) {
Q_ASSERT(mb.isEmpty());
int neededLength = WideCharToMultiByte(codePage, 0, ch, int(uclen), nullptr, 0, nullptr,
nullptr);
const qsizetype currentLength = out - buf.data();
mb.resize(currentLength + neededLength);
memcpy(mb.data(), out, currentLength * sizeof(*out));
out = mb.data() + currentLength;
outlen = neededLength;
// and try again...
while (uclen > 0) {
const int nextIn = qt_saturate<int>(uclen);
const int nextOut = qt_saturate<int>(outlen);
len = WideCharToMultiByte(codePage, 0, ch, nextIn, out, nextOut, nullptr, nullptr);
if (len > 0) {
ch += nextIn;
uclen -= nextIn;
out += len;
outlen -= len;
} else {
// Fail. Probably can't happen in fact (dwFlags is 0).
int r = GetLastError();
if (r == ERROR_INSUFFICIENT_BUFFER) {
Q_ASSERT(mb.isEmpty());
int neededLength = WideCharToMultiByte(codePage, 0, ch, nextIn, nullptr, 0,
nullptr, nullptr);
const qsizetype currentLength = out - buf.data();
mb.resize(currentLength + neededLength);
memcpy(mb.data(), out, currentLength * sizeof(*out));
out = mb.data() + currentLength;
outlen = neededLength;
// and try again...
} else {
// Fail. Probably can't happen in fact (dwFlags is 0).
#ifndef QT_NO_DEBUG
// Can't use qWarning(), as it'll recurse to handle %ls
fprintf(stderr, "WideCharToMultiByte: Cannot convert multibyte text (error %d): %ls\n",
r,
reinterpret_cast<const wchar_t *>(QStringView(ch, uclen).toString().utf16()));
// Can't use qWarning(), as it'll recurse to handle %ls
fprintf(stderr,
"WideCharToMultiByte: Cannot convert multibyte text (error %d): %ls\n", r,
reinterpret_cast<const wchar_t *>(
QStringView(ch, uclen).left(100).toString().utf16()));
#endif
break;
break;
}
}
}
out += len;
if (mb.isEmpty()) {
// We must have only used the stack buffer
if (out != buf.data()) // else: we return null-array