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>
impl<'a, R: Reads> LimitedReader<'a, R>
Sourcepub fn new(reader: &'a mut R) -> Self
pub fn new(reader: &'a mut R) -> Self
Creates a new LimitedReader with default limits
(DEFAULT_MAX_BYTES and DEFAULT_MAX_DEPTH).
Sourcepub fn unlimited(reader: &'a mut R) -> Self
pub fn unlimited(reader: &'a mut R) -> Self
Creates a new LimitedReader with no effective limits.
Sourcepub fn max_bytes(self, max: u64) -> Self
pub fn max_bytes(self, max: u64) -> Self
Sets the maximum number of bytes this reader will read.
Sourcepub fn max_depth(self, max: u32) -> Self
pub fn max_depth(self, max: u32) -> Self
Sets the maximum nesting depth this reader will allow.
Sourcepub fn bytes_read(&self) -> u64
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>
impl<R: Reads> ReadsDecodable for LimitedReader<'_, R>
Source§fn read(&mut self, buf: &mut [u8]) -> Result<usize, CodecError>
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>
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>
fn enter_scope(&mut self) -> Result<(), CodecError>
Called when entering a nested data scope during decoding.
Source§fn exit_scope(&mut self)
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>
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 moreSource§fn read_data_into<T: Decodable>(
&mut self,
data: &mut T,
) -> Result<(), CodecError>
fn read_data_into<T: Decodable>( &mut self, data: &mut T, ) -> Result<(), CodecError>
Reads and decodes a sequence of data into
data. Read moreSource§fn skip_blob(&mut self, length: usize) -> Result<(), CodecError>
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>
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>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> TryAsFormat<T> for T
impl<T> TryAsFormat<T> for T
Source§type Error = Infallible
type Error = Infallible
Source§fn try_as_format(&self) -> Result<&T, <T as TryAsFormat<T>>::Error>
fn try_as_format(&self) -> Result<&T, <T as TryAsFormat<T>>::Error>
Returns a
D-formatted reference to the data.