use crate::ReadSeek;
use byteorder::ReadBytesExt;
use std::char::decode_utf16;
use std::fmt::Write;
use std::io;
pub fn to_hex_string(bytes: &[u8]) -> String {
let len = bytes.len();
let mut s = String::with_capacity(len * 2);
for byte in bytes {
write!(s, "{:02X}", byte).expect("Writing to an allocated string cannot fail");
}
s
}
pub fn read_utf16_string<T: ReadSeek>(stream: &mut T, len: Option<usize>) -> io::Result<String> {
let mut buffer = match len {
Some(len) => Vec::with_capacity(len),
None => Vec::new(),
};
match len {
Some(len) => {
for _ in 0..len {
let next_char = stream.read_u16::<byteorder::LittleEndian>()?;
buffer.push(next_char);
}
}
None => loop {
let next_char = stream.read_u16::<byteorder::LittleEndian>()?;
if next_char == 0 {
break;
}
buffer.push(next_char);
},
}
decode_utf16(buffer.into_iter().take_while(|&byte| byte != 0x00))
.map(|r| r.map_err(|_e| io::Error::from(io::ErrorKind::InvalidData)))
.collect()
}