Skip to main content

ByteReader

Struct ByteReader 

Source
pub struct ByteReader<'a> { /* private fields */ }
Expand description

Strictly bounds-checked little-endian binary reader.

Implementations§

Source§

impl<'a> ByteReader<'a>

Source

pub const fn new(data: &'a [u8]) -> Self

Source

pub const fn with_max_depth(self, max_depth: usize) -> Self

Overrides the maximum nested container depth (default DEFAULT_MAX_DEPTH = 64).

Source

pub const fn pos(&self) -> usize

Source

pub const fn remaining(&self) -> usize

Source

pub fn remaining_slice(&self) -> &'a [u8] ⓘ

Source

pub fn ensure_remaining(&self, needed: usize) -> Result<(), CodecError>

Source

pub fn read_exact(&mut self, len: usize) -> Result<&'a [u8], CodecError>

Source

pub fn read_array<const N: usize>(&mut self) -> Result<[u8; N], CodecError>

Source

pub fn skip(&mut self, len: usize) -> Result<(), CodecError>

Advances past len bytes without inspecting them (Truncated if fewer remain).

Source

pub fn expect_magic<const N: usize>( &mut self, magic: &[u8; N], ) -> Result<(), CodecError>

Source

pub fn expect_zero(&mut self, len: usize) -> Result<(), CodecError>

Source

pub fn read_u8(&mut self) -> Result<u8, CodecError>

Source

pub fn read_i8(&mut self) -> Result<i8, CodecError>

Source

pub fn read_bool(&mut self) -> Result<bool, CodecError>

Reads a strict boolean (0 => false, 1 => true; any other byte returns BadBool).

Source

pub fn read_u16(&mut self) -> Result<u16, CodecError>

Source

pub fn read_i16(&mut self) -> Result<i16, CodecError>

Source

pub fn read_u24(&mut self) -> Result<u32, CodecError>

Reads a little-endian 24-bit unsigned integer (e.g. WebP VP8X canvas dimensions).

Source

pub fn read_u32(&mut self) -> Result<u32, CodecError>

Source

pub fn read_i32(&mut self) -> Result<i32, CodecError>

Source

pub fn read_u64(&mut self) -> Result<u64, CodecError>

Source

pub fn read_i64(&mut self) -> Result<i64, CodecError>

Source

pub fn read_f32(&mut self) -> Result<f32, CodecError>

Reads a canonical little-endian f32 (-0.0 -> +0.0, NaN -> canonical quiet NaN).

Source

pub fn read_f32_raw(&mut self) -> Result<f32, CodecError>

Reads a raw little-endian f32 preserving exact IEEE-754 bit patterns.

Source

pub fn read_f64(&mut self) -> Result<f64, CodecError>

Reads a canonical little-endian f64 (-0.0 -> +0.0, NaN -> canonical quiet NaN).

Source

pub fn read_f64_raw(&mut self) -> Result<f64, CodecError>

Reads a raw little-endian f64 preserving exact IEEE-754 bit patterns.

Source

pub fn read_u32_words_into( &mut self, count: usize, out: &mut Vec<u32>, ) -> Result<(), CodecError>

Appends count raw little-endian u32 words to out with one bounds check and one reservation (Truncated before any allocation if fewer than count * 4 bytes remain).

Source

pub fn read_len( &mut self, max: usize, min_elem_bytes: usize, ) -> Result<usize, CodecError>

Reads a u32 sequence length bounded by max AND guaranteed to fit min_elem_bytes within the remaining slice (prevents OOM pre-allocation on malicious buffers).

Source

pub fn read_bytes(&mut self, max: usize) -> Result<&'a [u8], CodecError>

Reads a u32-length-prefixed byte slice bounded by max.

Source

pub fn read_bytes_u16(&mut self, max: usize) -> Result<&'a [u8], CodecError>

Reads a u16-length-prefixed byte slice bounded by max.

Source

pub fn read_bytes_u8(&mut self, max: usize) -> Result<&'a [u8], CodecError>

Reads a u8-length-prefixed byte slice bounded by max.

Source

pub fn read_str(&mut self, max: usize) -> Result<&'a str, CodecError>

Reads a u32-length-prefixed UTF-8 string slice bounded by max.

Source

pub fn read_str_u16(&mut self, max: usize) -> Result<&'a str, CodecError>

Reads a u16-length-prefixed UTF-8 string slice bounded by max.

Source

pub fn read_str_u8(&mut self, max: usize) -> Result<&'a str, CodecError>

Reads a u8-length-prefixed UTF-8 string slice bounded by max.

Source

pub fn read_string(&mut self, max: usize) -> Result<String, CodecError>

Reads a u32-length-prefixed owned String bounded by max.

Source

pub fn read_vec<T>( &mut self, max: usize, min_elem_bytes: usize, decode_item: impl FnMut(&mut Self) -> Result<T, CodecError>, ) -> Result<Vec<T>, CodecError>

Reads a u32-length-prefixed vector bounded by max elements and min_elem_bytes per item.

When min_elem_bytes == 0, no upfront capacity is allocated (so lying lengths fail with 0 heap allocations) and zero-byte-progress items are bounded to O(input_len) across the reader’s lifetime to prevent Vec<Vec<()>> CPU-amplification DoS attacks.

Source

pub fn read_opt<T>( &mut self, decode_some: impl FnOnce(&mut Self) -> Result<T, CodecError>, ) -> Result<Option<T>, CodecError>

Reads an optional value tagged by a strict boolean byte (0 = None, 1 = Some(T)).

Source

pub fn read_value<T: NvDecode>(&mut self) -> Result<T, CodecError>

Decodes a value implementing NvDecode.

Source

pub fn finish(self) -> Result<(), CodecError>

Asserts that no trailing unread bytes remain.

Trait Implementations§

Source§

impl<'a> Clone for ByteReader<'a>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a> Debug for ByteReader<'a>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a> Forget for ByteReader<'a>

§

impl<'a> Freeze for ByteReader<'a>
where &'a [u8]: Freeze,

§

impl<'a> Move for ByteReader<'a>

§

impl<'a> RefUnwindSafe for ByteReader<'a>

§

impl<'a> Send for ByteReader<'a>
where &'a [u8]: Send,

§

impl<'a> Sync for ByteReader<'a>

§

impl<'a> Unpin for ByteReader<'a>
where &'a [u8]: Unpin,

§

impl<'a> UnsafeUnpin for ByteReader<'a>
where &'a [u8]: UnsafeUnpin,

§

impl<'a> UnwindSafe for ByteReader<'a>
where &'a [u8]: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.