You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

61 lines
1.3 KiB

use std::io::{self, Read};
pub const fn i16le_16(b: &[u8], p: usize) -> i16 {
i16::from_le_bytes([b[p], b[p + 1]])
}
pub const fn u16le_16(b: &[u8], p: usize) -> u16 {
u16::from_le_bytes([b[p], b[p + 1]])
}
pub const fn u16le_sz(b: &[u8], p: usize) -> usize {
u16le_16(b, p) as usize
}
pub const fn i32le_32(b: &[u8], p: usize) -> i32 {
i32::from_le_bytes([b[p], b[p + 1], b[p + 2], b[p + 3]])
}
pub const fn u32le_32(b: &[u8], p: usize) -> u32 {
u32::from_le_bytes([b[p], b[p + 1], b[p + 2], b[p + 3]])
}
pub const fn u32le_64(b: &[u8], p: usize) -> u64 {
u32le_32(b, p) as u64
}
pub const fn u32le_sz(b: &[u8], p: usize) -> usize {
u32le_32(b, p) as usize
}
pub fn f32le_32(b: &[u8], p: usize) -> f32 {
f32::from_le_bytes([b[p], b[p + 1], b[p + 2], b[p + 3]])
}
pub fn f64le_64(b: &[u8], p: usize) -> f64 {
f64::from_le_bytes([b[p], b[p + 1], b[p + 2], b[p + 3], b[p + 4], b[p + 5], b[p + 6], b[p + 7]])
}
pub fn array<'a, 'b, T>(
f: impl Fn(&[u8], usize) -> T, b: &'a [u8], v: &'b mut [T], step: usize,
beg: usize,
) -> &'b [T] {
for (i, p) in (beg..beg + v.len() * step).step_by(step).enumerate() {
v[i] = f(b, p);
}
v
}
pub fn hunk(rd: &mut impl Read, size: usize) -> io::Result<Vec<u8>> {
let mut data = Vec::with_capacity(size);
unsafe {
data.set_len(size);
}
rd.read_exact(&mut data)?;
Ok(data)
}
// EOF