mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-18 08:15:29 +00:00
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>
40 lines
1.1 KiB
Rust
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)
|
|
}
|
|
}
|