Skip to main content

LimitedReader

Struct LimitedReader 

Source
pub struct LimitedReader<'a, R: Reads> { /* private fields */ }
Expand description

A Reads wrapper that enforces byte and depth limits during decoding, protecting against malicious or malformed input.

The blanket ReadsDecodable implementation automatically wraps each top-level decode in a limited reader with default limits. Construct a LimitedReader explicitly to override the defaults:

use codas::codec::LimitedReader;
use codas::codec::ReadsDecodable;

// Custom limits:
let mut slice = encoded;
let data: u32 = LimitedReader::new(&mut slice)
    .max_bytes(1024)
    .max_depth(8)
    .read_data()?;

// No effective limits (trusted data):
let mut slice = encoded;
let data: u32 = LimitedReader::unlimited(&mut slice)
    .read_data()?;

Limits are cumulative within the LimitedReader’s lifetime: every sub-field’s bytes and nesting depth count against the same instance.

Implementations§

Source§

impl<'a, R: Reads> LimitedReader<'a, R>

Source

pub fn new(reader: &'a mut R) -> Self

Creates a new LimitedReader with default limits (DEFAULT_MAX_BYTES and DEFAULT_MAX_DEPTH).

Source

pub fn unlimited(reader: &'a mut R) -> Self

Creates a new LimitedReader with no effective limits.

Source

pub fn max_bytes(self, max: u64) -> Self

Sets the maximum number of bytes this reader will read.

Source

pub fn max_depth(self, max: u32) -> Self

Sets the maximum nesting depth this reader will allow.

Source

pub fn bytes_read(&self) -> u64

Returns the total number of bytes read so far.

Trait Implementations§

Source§

impl<R: Reads> ReadsDecodable for LimitedReader<'_, R>

Source§

fn read(&mut self, buf: &mut [u8]) -> Result<usize, CodecError>

Reads bytes into buf, returning the number of bytes read.
Source§

fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), CodecError>

Reads exactly buf.len() bytes into buf.
Source§

fn enter_scope(&mut self) -> Result<(), CodecError>

Called when entering a nested data scope during decoding.
Source§

fn exit_scope(&mut self)

Called when exiting a nested data scope during decoding.
Source§

fn read_data<T: Decodable + Default>(&mut self) -> Result<T, CodecError>

Reads and decodes a sequence of data into a new, default instance of T. Read more
Source§

fn read_data_into<T: Decodable>( &mut self, data: &mut T, ) -> Result<(), CodecError>

Reads and decodes a sequence of data into data. Read more
Source§

fn skip_blob(&mut self, length: usize) -> Result<(), CodecError>

Skips to the end of the next length bytes of data.
Source§

fn skip_data(&mut self) -> Result<usize, CodecError>

Skips to the end of the next encoded sequence of data, returning the total number of bytes skipped.
Source§

fn skip_data_with_format( &mut self, format: DataFormat, ) -> Result<usize, CodecError>

Skips to the end of the next encoded instance of data with format, returning the total number of bytes skipped.

Auto Trait Implementations§

§

impl<'a, R> Freeze for LimitedReader<'a, R>

§

impl<'a, R> RefUnwindSafe for LimitedReader<'a, R>
where R: RefUnwindSafe,

§

impl<'a, R> Send for LimitedReader<'a, R>
where R: Send,

§

impl<'a, R> Sync for LimitedReader<'a, R>
where R: Sync,

§

impl<'a, R> Unpin for LimitedReader<'a, R>

§

impl<'a, R> UnsafeUnpin for LimitedReader<'a, R>

§

impl<'a, R> !UnwindSafe for LimitedReader<'a, R>

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> 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> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> TryAsFormat<T> for T

Source§

type Error = Infallible

Type of error returned when self doesn’t contain data of format D. Read more
Source§

fn try_as_format(&self) -> Result<&T, <T as TryAsFormat<T>>::Error>

Returns a D-formatted reference to the data.
Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.