mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-22 18:41:27 +01:00
* Port Java class source: Craft Anything Source: storytold/craft-anything@f64ae3bf3c * Port Java class source: Find class usage in multidimensional object arrays (R0.5) (#437) Source: storytold/craft-anything@ffe15c9165 * Port Java class source: More format parsers keep to their declared lengths and refuse impossible values (#722) Source: storytold/craft-anything@b9ca099903 * Port Java class source: Address the parser review findings from #722 (#766) Source: storytold/craft-anything@eab6a0f01b * Port Java class source: Read JVM method parameters containing parentheses in class names (R3.12) (#1342) Source: storytold/craft-anything@fb4c437fe6 * Port Java class source: Reject Java constants that exceed the constant-pool slots (R3.12) (#1373) Source: storytold/craft-anything@1c4f233f08 * Port Java class source: Validate JVM switch tables before allocating decoded pairs (R3.12) (#1503) Source: storytold/craft-anything@b40c56ef5c * Port Java class source: Reuse constant-pool names while reading Java attributes (R3.12) (#1549) Source: storytold/craft-anything@76fcdaadb7 * Port Java class source: Trace JVM bootstrap dependencies at their bytecode use sites (R3.12) (#1648) Source: storytold/craft-anything@c5e524dd08 * Port Java class source: Reject impossible JVM array allocation operands (R3.12) (#1675) Source: storytold/craft-anything@f04d7456e0 * Port Java class source: Report truncated Java class attribute tables (R3.12) Source: storytold/craft-anything@420552ff74 * Expose neutral Java class API and retain checked reader and synthetic fixtures * Retain Java regression: java_array_operands Source: storytold/craft-anything@f04d7456e0 * Retain Java regression: java_attribute_names Source: storytold/craft-anything@76fcdaadb7 * Retain Java regression: java_constant_pool_slots Source: storytold/craft-anything@1c4f233f08 * Retain Java regression: java_invoke_operands Source: storytold/craft-anything@eab6a0f01b * Retain Java regression: java_linkage_references Source: storytold/craft-anything@c5e524dd08 * Retain Java regression: java_truncated_attributes Source: storytold/craft-anything@420552ff74 * Retain Java regression: java_wide_opcodes Source: storytold/craft-anything@b9ca099903 * Register Java class reader and retain source regression coverage --------- Co-authored-by: Brandon Thomas <echelon@gmail.com> Co-authored-by: Afloat <132809511+Afloat16@users.noreply.github.com> Co-authored-by: BFlat <bombayapirat@gmail.com> Co-authored-by: Afloat <ShuffleMonkey@163.com> Co-authored-by: RRFZ <247457381+Plastic-time@users.noreply.github.com>
52 lines
1.6 KiB
Rust
52 lines
1.6 KiB
Rust
//! Checked little/big-endian reads over untrusted bytes. Every read returns `None` when out of range.
|
|
|
|
#[derive(Debug, Clone, Copy)]
|
|
pub struct Reader<'a> {
|
|
data: &'a [u8],
|
|
}
|
|
|
|
impl<'a> Reader<'a> {
|
|
pub fn new(data: &'a [u8]) -> Self {
|
|
Reader { data }
|
|
}
|
|
|
|
pub fn slice(&self, off: usize, len: usize) -> Option<&'a [u8]> {
|
|
let end = off.checked_add(len)?;
|
|
self.data.get(off..end)
|
|
}
|
|
|
|
pub fn u8(&self, off: usize) -> Option<u8> {
|
|
self.data.get(off).copied()
|
|
}
|
|
|
|
fn arr<const N: usize>(&self, off: usize) -> Option<[u8; N]> {
|
|
self.slice(off, N)?.try_into().ok()
|
|
}
|
|
|
|
pub fn u16_be(&self, off: usize) -> Option<u16> {
|
|
self.arr(off).map(u16::from_be_bytes)
|
|
}
|
|
|
|
pub fn u32_be(&self, off: usize) -> Option<u32> {
|
|
self.arr(off).map(u32::from_be_bytes)
|
|
}
|
|
|
|
pub fn u64_be(&self, off: usize) -> Option<u64> {
|
|
self.arr(off).map(u64::from_be_bytes)
|
|
}
|
|
}
|
|
|
|
/// Decode one modified UTF-8 code unit. Invalid bytes consume only themselves,
|
|
/// so a damaged sequence cannot hide valid text or borrow missing bytes.
|
|
pub(crate) fn mutf8_unit(bytes: &[u8]) -> (u16, usize) {
|
|
match bytes {
|
|
[a @ 0x00..=0x7f, ..] => (u16::from(*a), 1),
|
|
[0xc0, 0x80, ..] => (0, 2),
|
|
[a @ 0xc2..=0xdf, b @ 0x80..=0xbf, ..] => ((u16::from(*a & 0x1f) << 6) | u16::from(*b & 0x3f), 2),
|
|
[a @ 0xe0..=0xef, b @ 0x80..=0xbf, c @ 0x80..=0xbf, ..] if *a != 0xe0 || *b >= 0xa0 => {
|
|
((u16::from(*a & 0x0f) << 12) | (u16::from(*b & 0x3f) << 6) | u16::from(*c & 0x3f), 3)
|
|
}
|
|
_ => (0xfffd, 1),
|
|
}
|
|
}
|