use crate::engine::{TChar, handles::path_from_pointer};
use std::ffi::c_int;
#[cfg(unix)]
use std::ffi::c_void;
use std::io::Read;
use wim_format::ParseError;
#[cfg(unix)]
unsafe extern "C" {
static mut stdin: *mut c_void;
fn fread(buffer: *mut c_void, size: usize, count: usize, stream: *mut c_void) -> usize;
fn feof(stream: *mut c_void) -> c_int;
}
#[cfg(unix)]
fn stdin_nomem() -> c_int {
crate::engine::diagnostics::message(false, b"Too much data sent on stdin!", false);
24
}
fn file_message(prefix: &[u8], path: &std::path::Path, with_errno: bool) {
let mut message = prefix.to_vec();
message.extend_from_slice(path.as_os_str().as_encoded_bytes());
message.push(b'"');
crate::engine::diagnostics::message(false, &message, with_errno);
}
fn read_stdin() -> Result<Vec<u8>, c_int> {
#[cfg(unix)]
{
let mut output = Vec::new();
let mut capacity = 0usize;
let mut filled = 0usize;
loop {
let next = capacity
.checked_mul(2)
.and_then(|n| n.checked_add(256))
.ok_or_else(stdin_nomem)?;
output
.try_reserve_exact(next - output.len())
.map_err(|_| stdin_nomem())?;
output.resize(next, 0);
capacity = next;
let count = unsafe {
fread(
output.as_mut_ptr().add(filled).cast(),
1,
capacity - filled,
stdin,
)
};
filled += count;
if filled != capacity {
if unsafe { feof(stdin) } == 0 {
crate::engine::diagnostics::message(false, b"Error reading stdin", true);
return Err(50);
}
output.truncate(filled);
return Ok(output);
}
}
}
#[cfg(not(unix))]
{
let mut output = Vec::new();
std::io::stdin().read_to_end(&mut output).map_err(|_| 50)?;
Ok(output)
}
}
unsafe fn read_input(path: *const TChar, dash_is_stdin: bool) -> Result<Vec<u8>, c_int> {
let standard_input =
path.is_null() || dash_is_stdin && unsafe { *path == b'-' as TChar && *path.add(1) == 0 };
if standard_input {
return read_stdin();
}
let path = if unsafe { *path } == 0 {
std::path::PathBuf::new()
} else {
unsafe { path_from_pointer(path) }?
};
let mut file = std::fs::File::open(&path).map_err(|_| {
file_message(b"Can't open \"", &path, true);
47
})?;
let size = match file.metadata() {
Ok(metadata) => metadata.len(),
Err(_error) => {
drop(file);
#[cfg(target_os = "linux")]
if let Some(errno) = _error.raw_os_error() {
unsafe {
*libc::__errno_location() = errno;
}
}
file_message(b"Can't stat \"", &path, true);
return Err(63);
}
};
let size = usize::try_from(size).map_err(|_| {
file_message(b"Not enough memory to read \"", &path, false);
39
})?;
let mut bytes = Vec::new();
bytes.try_reserve_exact(size).map_err(|_| {
file_message(b"Not enough memory to read \"", &path, false);
39
})?;
bytes.resize(size, 0);
let result = file.read_exact(bytes.as_mut_slice());
drop(file);
if let Err(error) = result {
let eof = error.kind() == std::io::ErrorKind::UnexpectedEof;
#[cfg(target_os = "linux")]
if let Some(errno) = if eof {
Some(libc::EINVAL)
} else {
error.raw_os_error()
} {
unsafe {
*libc::__errno_location() = errno;
}
}
file_message(b"Error reading \"", &path, true);
return Err(if eof { 65 } else { 50 });
}
Ok(bytes)
}
fn translate(raw: &[u8]) -> Result<Vec<TChar>, ParseError> {
let (utf16, offset) = if raw.starts_with(&[0xff, 0xfe]) {
(true, 2)
} else if raw.len() >= 2 && raw[0] <= 0x7f && raw[1] == 0 {
(true, 0)
} else if raw.starts_with(&[0xef, 0xbb, 0xbf]) {
(false, 3)
} else {
(false, 0)
};
let bytes = &raw[offset..];
#[cfg(not(windows))]
{
let bytes = if utf16 {
if !bytes.len().is_multiple_of(2) {
return Err(ParseError::InvalidUtf16String);
}
let units = bytes
.chunks_exact(2)
.map(|b| u16::from_le_bytes([b[0], b[1]]))
.collect::<Vec<_>>();
wim_format::platform_text::utf16_to_wtf8(&units)?
} else {
bytes.to_vec()
};
Ok(bytes.into_iter().map(|b| b as TChar).collect())
}
#[cfg(windows)]
{
if utf16 {
Ok(bytes
.chunks_exact(2)
.map(|b| u16::from_le_bytes([b[0], b[1]]))
.collect())
} else {
wim_format::platform_text::wtf8_to_utf16(bytes)
}
}
}
pub(crate) unsafe fn load_pathlist_text(path: *const TChar) -> Result<Vec<TChar>, c_int> {
let bytes = unsafe { read_input(path, false) }?;
translate(bytes.as_slice()).map_err(|error| {
#[cfg(target_os = "linux")]
if matches!(
error,
ParseError::InvalidUtf16String | ParseError::InvalidUtf8String
) {
unsafe {
*libc::__errno_location() = libc::EILSEQ;
}
}
error as c_int
})
}
#[cfg(any(unix, windows))]
pub(crate) unsafe fn load_capture_text(path: *const TChar) -> Result<Vec<TChar>, c_int> {
let bytes = unsafe { read_input(path, true) }?;
translate(bytes.as_slice()).map_err(|error| {
#[cfg(target_os = "linux")]
if matches!(
error,
ParseError::InvalidUtf16String | ParseError::InvalidUtf8String
) {
unsafe {
*libc::__errno_location() = libc::EILSEQ;
}
}
error as c_int
})
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn wimlib_load_text_file(
path: *const TChar,
output: *mut *mut TChar,
length: *mut usize,
) -> c_int {
if output.is_null() || length.is_null() {
return 24;
}
let bytes = match unsafe { read_input(path, true) } {
Ok(bytes) => bytes,
Err(error) => return error,
};
let text = match translate(bytes.as_slice()) {
Ok(text) => text,
Err(error) => {
#[cfg(target_os = "linux")]
if matches!(
error,
ParseError::InvalidUtf16String | ParseError::InvalidUtf8String
) {
unsafe {
*libc::__errno_location() = libc::EILSEQ;
}
}
return error as c_int;
}
};
let Some(size) = text
.len()
.checked_add(1)
.and_then(|n| n.checked_mul(std::mem::size_of::<TChar>()))
else {
return 39;
};
let pointer = unsafe { crate::engine::allocation::malloc(size) }.cast::<TChar>();
if pointer.is_null() {
return 39;
}
unsafe {
std::ptr::copy_nonoverlapping(text.as_ptr(), pointer, text.len());
pointer.add(text.len()).write(0);
output.write(pointer);
length.write(text.len());
}
0
}
#[cfg(all(test, not(windows)))]
mod tests {
use super::*;
fn bytes(raw: &[u8]) -> Result<Vec<u8>, ParseError> {
translate(raw).map(|v| v.into_iter().map(|c| c as u8).collect())
}
#[test]
fn bom_and_initial_ascii_nul_detection_match_original() {
assert_eq!(bytes(b"\xef\xbb\xbfhello\r\n"), Ok(b"hello\r\n".to_vec()));
assert_eq!(bytes(b"A\0B\0"), Ok(b"AB".to_vec()));
assert_eq!(bytes(b"\xff\xfeA\0\0\0B\0"), Ok(b"A\0B".to_vec()));
assert_eq!(bytes(b"\xfe\xffA"), Ok(b"\xfe\xffA".to_vec()));
}
#[test]
fn unix_utf8_copy_keeps_invalid_bytes_and_embedded_nuls() {
assert_eq!(
bytes(b"\xff\xc0\x80\0\xed\xa0\x80"),
Ok(b"\xff\xc0\x80\0\xed\xa0\x80".to_vec())
);
assert_eq!(bytes(b"\xff\xfe\0\xd8"), Ok(b"\xed\xa0\x80".to_vec()));
assert_eq!(bytes(b"\xff\xfeA"), Err(ParseError::InvalidUtf16String));
}
}