refs-core 0.1.0

Pure-Rust from-scratch ReFS (Resilient File System) reader — boot VBR, superblock, version detection, over any byte source. Reverse-engineered format (no official Microsoft spec)
Documentation
//! Bounds-checked little-endian readers (the Paranoid Gatekeeper standard).
//!
//! ReFS is a **little-endian** on-disk format (Windows/NT platform), so these
//! mirror `xfs-core`'s big-endian helpers with LE decode. Every reader yields
//! `0` when the requested range lies outside the buffer, so a malformed or
//! truncated image can never panic a parser. Callers that need to distinguish
//! "field absent" from "field is zero" bounds-check the buffer length up front
//! and surface [`crate::RefsError::Truncated`].

/// Read a little-endian `u16` at `off`, or `0` if out of range.
#[must_use]
pub fn le_u16(data: &[u8], off: usize) -> u16 {
    let mut b = [0u8; 2];
    if let Some(s) = data.get(off..off + 2) {
        b.copy_from_slice(s);
    }
    u16::from_le_bytes(b)
}

/// Read a little-endian `u32` at `off`, or `0` if out of range.
#[must_use]
pub fn le_u32(data: &[u8], off: usize) -> u32 {
    let mut b = [0u8; 4];
    if let Some(s) = data.get(off..off + 4) {
        b.copy_from_slice(s);
    }
    u32::from_le_bytes(b)
}

/// Read a little-endian `u64` at `off`, or `0` if out of range.
#[must_use]
pub fn le_u64(data: &[u8], off: usize) -> u64 {
    let mut b = [0u8; 8];
    if let Some(s) = data.get(off..off + 8) {
        b.copy_from_slice(s);
    }
    u64::from_le_bytes(b)
}

/// Read a single byte at `off`, or `0` if out of range.
#[must_use]
pub fn u8_at(data: &[u8], off: usize) -> u8 {
    data.get(off).copied().unwrap_or(0)
}

/// Best-effort ASCII rendering of a byte slice — printable ASCII kept, every
/// other byte shown as `.`. Used to render the offending value legibly in
/// fail-loud "unrecognized signature" errors (show the unrecognized value).
#[must_use]
pub fn ascii(bytes: &[u8]) -> String {
    bytes
        .iter()
        .map(|&c| {
            if (0x20..0x7f).contains(&c) {
                c as char
            } else {
                '.'
            }
        })
        .collect()
}