Skip to main content

Crate safe_read

Crate safe_read 

Source
Expand description

Panic-free bounded integer readers over an untrusted byte slice.

Every multi-byte read returns 0 when the requested window is out of range โ€” never a panic. This is the shared front door for every offset/length field parsed from an attacker-controllable forensic image, so each reader crate does not re-derive its own bounds-checked helpers.

use safe_read::{le_u32, be_u16};
assert_eq!(le_u32(&[0x78, 0x56, 0x34, 0x12], 0), 0x1234_5678);
assert_eq!(be_u16(&[0xaa, 0x12, 0x34], 1), 0x1234);
// Out of range is 0, never a panic:
assert_eq!(le_u32(&[1, 2, 3], 0), 0);

#![no_std] โ€” pure slice arithmetic, no allocation.

Functionsยง

be_u16
Read a u16 at off; 0 if out of range. Never panics.
be_u32
Read a u32 at off; 0 if out of range. Never panics.
be_u64
Read a u64 at off; 0 if out of range. Never panics.
le_u16
Read a u16 at off; 0 if out of range. Never panics.
le_u32
Read a u32 at off; 0 if out of range. Never panics.
le_u64
Read a u64 at off; 0 if out of range. Never panics.