OOps bad flag input to WideCharToMultibyte()

This commit is contained in:
Jean-Francois Dockes 2020-05-11 16:41:56 +01:00
parent 2f45ceb1dc
commit 980b6c7b4a

View File

@ -163,17 +163,16 @@ using namespace std;
bool wchartoutf8(const wchar_t *in, std::string& out) bool wchartoutf8(const wchar_t *in, std::string& out)
{ {
// fprintf(stderr, "WCHARTOUTF8: in [%S]\n", in);
out.clear(); out.clear();
if (nullptr == in) { if (nullptr == in) {
return true; return true;
} }
size_t wlen = wcslen(in); size_t wlen = wcslen(in);
const UINT codePageUtf8 = CP_UTF8;
const DWORD flags = MB_ERR_INVALID_CHARS;
int bytes = ::WideCharToMultiByte( int bytes = ::WideCharToMultiByte(
codePageUtf8, flags, in, wlen, nullptr, 0, nullptr, nullptr); CP_UTF8, WC_ERR_INVALID_CHARS, in, wlen, nullptr, 0, nullptr, nullptr);
if (bytes <= 0) { if (bytes <= 0) {
std::cerr << "wchartoutf8: CONVERSION ERROR\n"; std::cerr << "wchartoutf8: CONVERSION ERROR1\n";
return false; return false;
} }
char *cp = (char *)malloc(bytes+1); char *cp = (char *)malloc(bytes+1);
@ -182,9 +181,9 @@ bool wchartoutf8(const wchar_t *in, std::string& out)
return false; return false;
} }
bytes = ::WideCharToMultiByte( bytes = ::WideCharToMultiByte(
codePageUtf8, flags, in, wlen, cp, bytes, nullptr, nullptr); CP_UTF8, WC_ERR_INVALID_CHARS, in, wlen, cp, bytes, nullptr, nullptr);
if (bytes <= 0) { if (bytes <= 0) {
std::cerr << "wchartoutf8: CONVERSION ERROR\n"; std::cerr << "wchartoutf8: CONVERSION ERROR2\n";
free(cp); free(cp);
return false; return false;
} }
@ -202,10 +201,8 @@ bool utf8towchar(const std::string& in, wchar_t *out, size_t obytescap)
} }
out[0] = 0; out[0] = 0;
const UINT codePageUtf8 = CP_UTF8;
const DWORD flags = MB_ERR_INVALID_CHARS;
int wcharcnt = MultiByteToWideChar( int wcharcnt = MultiByteToWideChar(
codePageUtf8, flags, in.c_str(), in.size(), nullptr, 0); CP_UTF8, MB_ERR_INVALID_CHARS, in.c_str(), in.size(), nullptr, 0);
if (wcharcnt <= 0) { if (wcharcnt <= 0) {
std::cerr << "utf8towchar: CONVERSION ERROR\n"; std::cerr << "utf8towchar: CONVERSION ERROR\n";
return false; return false;
@ -215,7 +212,7 @@ bool utf8towchar(const std::string& in, wchar_t *out, size_t obytescap)
return false; return false;
} }
wcharcnt = MultiByteToWideChar( wcharcnt = MultiByteToWideChar(
codePageUtf8, flags, in.c_str(), in.size(), out, wcharsavail); CP_UTF8, MB_ERR_INVALID_CHARS, in.c_str(), in.size(), out, wcharsavail);
if (wcharcnt <= 0) { if (wcharcnt <= 0) {
std::cerr << "utf8towchar: CONVERSION ERROR\n"; std::cerr << "utf8towchar: CONVERSION ERROR\n";
return false; return false;