Skip to content

Commit 2beab58

Browse files
committed
Only use WC_ERR_INVALID_CHARS on NT, fall back to 0 flags on incompatible versions of Windows
Fixes #18
1 parent 1388d9a commit 2beab58

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

library/std/src/sys/pal/windows/stdio.rs

+37
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,10 @@ fn utf16_to_utf8(utf16: &[u16], utf8: &mut [u8]) -> io::Result<usize> {
400400
return Ok(0);
401401
}
402402

403+
#[cfg(target_vendor = "rust9x")]
404+
let is_nt = crate::sys::compat::checks::is_windows_nt();
405+
406+
#[cfg(not(target_vendor = "rust9x"))]
403407
let result = unsafe {
404408
c::WideCharToMultiByte(
405409
c::CP_UTF8, // CodePage
@@ -412,6 +416,39 @@ fn utf16_to_utf8(utf16: &[u16], utf8: &mut [u8]) -> io::Result<usize> {
412416
ptr::null_mut(), // lpUsedDefaultChar
413417
)
414418
};
419+
420+
#[cfg(target_vendor = "rust9x")]
421+
let result = {
422+
let mut result = unsafe {
423+
c::WideCharToMultiByte(
424+
c::CP_UTF8, // CodePage
425+
if is_nt { c::WC_ERR_INVALID_CHARS } else { 0 }, // dwFlags
426+
utf16.as_ptr(), // lpWideCharStr
427+
utf16.len() as i32, // cchWideChar
428+
utf8.as_mut_ptr(), // lpMultiByteStr
429+
utf8.len() as i32, // cbMultiByte
430+
ptr::null(), // lpDefaultChar
431+
ptr::null_mut(), // lpUsedDefaultChar
432+
)
433+
};
434+
435+
if result == 0 && unsafe { c::GetLastError() } == c::ERROR_INVALID_FLAGS && is_nt {
436+
result = unsafe {
437+
c::WideCharToMultiByte(
438+
c::CP_UTF8, // CodePage
439+
0, // dwFlags (0 for pre-Vista compatibility)
440+
utf16.as_ptr(), // lpWideCharStr
441+
utf16.len() as i32, // cchWideChar
442+
utf8.as_mut_ptr(), // lpMultiByteStr
443+
utf8.len() as i32, // cbMultiByte
444+
ptr::null(), // lpDefaultChar
445+
ptr::null_mut(), // lpUsedDefaultChar
446+
)
447+
};
448+
}
449+
result
450+
};
451+
415452
if result == 0 {
416453
// We can't really do any better than forget all data and return an error.
417454
Err(io::const_error!(

0 commit comments

Comments
 (0)