SecurityRonin/safe-read
GitHub: SecurityRonin/safe-read
一个无 panic、零依赖的 Rust 库,用于从不可信字节切片中安全读取固定宽度的有界整数。
Stars: 0 | Forks: 0
# 安全读取
[](https://crates.io/crates/safe-read)
[](https://docs.rs/safe-read)
[](LICENSE)
**对不受信任的字节切片进行无 panic 的有界整数读取** —— 这是为从攻击者可控的取证镜像中解析的每个 offset/length 字段提供的统一入口,从而让每个 reader crate 不再重复派生各自的边界检查辅助函数。
```
use safe_read::{le_u32, be_u16};
// In range → the value; out of range → 0, never a panic.
assert_eq!(le_u32(&[0x78, 0x56, 0x34, 0x12], 0), 0x1234_5678);
assert_eq!(le_u32(&[1, 2, 3], 0), 0); // too short
assert_eq!(be_u16(&[1, 2, 3, 4], usize::MAX), 0); // offset overflow
```
`be_u16`/`be_u32`/`be_u64` 和 `le_u16`/`le_u32`/`le_u64` 分别在指定的字节偏移量处读取固定宽度的整数,当读取窗口超出范围时(太短、偏移量超过 EOF,或者 `off + width` 溢出 `usize`),将返回 `0`。`#![no_std]`,无依赖,不使用 `unsafe`。
## 安装
```
[dependencies]
safe-read = "0.1"
```
[隐私政策](https://securityronin.github.io/safe-read/privacy/) · [服务条款](https://securityronin.github.io/safe-read/terms/) · © 2026 Security Ronin Ltd
标签:Golang, no_std, Rust库, 可视化界面, 字节解析, 安全编程, 数据解析, 无依赖, 边界检查, 通知系统