Files
craft-libs/crates/wasm/src/bytes.rs
T
5039e49ab6 Add shared WebAssembly inspection and validation reports (#2044)
* wasm: Craft Anything

Port Craft Anything f64ae3bf3c8863a982af1304e06de20653803968.

* wasm: Compare WebAssembly exported tag parameter types (R0.5) (#441)

Port Craft Anything fa4aae2e79198ece843a876512172bfb10d902e3.

* wasm: Count all local declaration groups in a WebAssembly function (R3.12) (#1125)

Port Craft Anything ad4d57d4ae5993e157a280b4237a0aa8a7a7fdb7.

* wasm: Read WebAssembly debug URLs as length-prefixed UTF-8 names (R3.12) (#1126)

Port Craft Anything 07a5f8686f63bff6d0efe19af56bf129e0fee9f7.

* wasm: Compare WebAssembly body counts with all declared functions (R3.12) (#1129)

Port Craft Anything 58402ab226ce1a417d250e789dce9a0613158567.

* wasm: Bound WebAssembly metadata across repeated custom sections (R3.12) (#1390)

Port Craft Anything 37ec5557f07eff39050b0bd448a4a52d155d130e.

* wasm: Bound WebAssembly strings expanded through type and name references (R3.12) (#1540)

Port Craft Anything ada52c6cce7979c386460e80e69c95cf4b82dbda.

* wasm: Report top-level WebAssembly component sections (R3.12)

Port Craft Anything 7d60ac9f896fc91cb623bd1666c4424e6ce944f9.

* wasm: retain component_layout regression tests

Source crates/formats/tests/wasm_component_layout.rs at 7d60ac9f896fc91cb623bd1666c4424e6ce944f9; imports adapted only.

* wasm: retain custom_inventory_budget regression tests

Source crates/formats/tests/wasm_custom_inventory_budget.rs at 37ec5557f07eff39050b0bd448a4a52d155d130e; imports adapted only.

* wasm: retain reference_bytes regression tests

Source crates/formats/tests/wasm_reference_bytes.rs at ada52c6cce7979c386460e80e69c95cf4b82dbda; imports adapted only.

* Expose shared WebAssembly inspection and retained hostile-input tests

* Split WASM section inspection and reuse regression encoders

---------

Co-authored-by: Brandon Thomas <echelon@gmail.com>
Co-authored-by: Afloat <132809511+Afloat16@users.noreply.github.com>
Co-authored-by: Afloat <ShuffleMonkey@163.com>
Co-authored-by: RRFZ <247457381+Plastic-time@users.noreply.github.com>
2026-11-17 14:13:51 -05:00

33 lines
1.0 KiB
Rust

// Checked reader subset retained from Craft Anything; MIT OR Apache-2.0.
pub(crate) struct Reader<'a> {
data: &'a [u8],
}
impl<'a> Reader<'a> {
pub fn new(data: &'a [u8]) -> Self {
Self { data }
}
pub fn u8(&self, off: usize) -> Option<u8> {
self.data.get(off).copied()
}
pub fn slice(&self, off: usize, len: usize) -> Option<&'a [u8]> {
self.data.get(off..off.checked_add(len)?)
}
/// Unsigned LEB128 at `off`: (value, bytes consumed). At most 10 bytes.
pub fn uleb128(&self, off: usize) -> Option<(u64, usize)> {
let mut result = 0u64;
for i in 0..10usize {
let b = self.u8(off.checked_add(i)?)?;
let payload = u64::from(b & 0x7f);
let shift = 7 * i as u32;
if shift > 0 && payload.checked_shl(shift)? >> shift != payload {
return None;
}
result |= payload << shift;
if b & 0x80 == 0 {
return Some((result, i + 1));
}
}
None
}
}