Files
craft-libs/crates/macho/src/bytes.rs
T
65848bfe29 Add craft-macho: Mach-O slice tables and header names from Craft Anything
Moves the header pieces of Craft Anything's crates/formats/src/macho.rs
into a pure-Rust crate: fat_slices (fat and fat64), FatSlice, cpu_name,
load_command_name, load_command_min_size, nibble_version and
unixthread_pc. The load command walker, symbols, stubs and Objective-C
metadata stay in the app.

Part of #778.

Co-authored-by: Afloat <ShuffleMonkey@163.com>
Co-authored-by: BFlat <bombayapirat@gmail.com>
Co-authored-by: Brandon Thomas <echelon@gmail.com>
Co-authored-by: Cr <60964294+Chr0mium24@users.noreply.github.com>
Co-authored-by: GGWA <65288322+rech4210@users.noreply.github.com>
2026-11-16 11:47:05 +01:00

40 lines
1.1 KiB
Rust

//! Bounds-checked fixed-width reads in either byte order: out of range is `None`.
#[derive(Debug, Clone, Copy)]
pub(crate) struct Reader<'a> {
bytes: &'a [u8],
}
impl<'a> Reader<'a> {
pub(crate) fn new(bytes: &'a [u8]) -> Self {
Self { bytes }
}
/// `len` bytes starting at `at`.
pub(crate) fn slice(&self, at: usize, len: usize) -> Option<&'a [u8]> {
let end = at.checked_add(len)?;
self.bytes.get(at..end)
}
fn word<const N: usize>(&self, at: usize) -> Option<[u8; N]> {
let (word, _) = self.bytes.get(at..)?.split_first_chunk::<N>()?;
Some(*word)
}
pub(crate) fn u32_le(&self, at: usize) -> Option<u32> {
self.word(at).map(u32::from_le_bytes)
}
pub(crate) fn u64_le(&self, at: usize) -> Option<u64> {
self.word(at).map(u64::from_le_bytes)
}
pub(crate) fn u32_be(&self, at: usize) -> Option<u32> {
self.word(at).map(u32::from_be_bytes)
}
pub(crate) fn u64_be(&self, at: usize) -> Option<u64> {
self.word(at).map(u64::from_be_bytes)
}
}