Struct DecoderBuffer

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

DecoderBuffer is a panic-free byte buffer for look-ahead decoding untrusted input

Implementations§

Source§

impl<'a> DecoderBuffer<'a>

Source

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

Create a new DecoderBuffer from a byte slice

Source

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

Move out the buffer’s slice. This should be used with caution, as it removes any panic protection this struct provides.

Source§

impl<'a> DecoderBuffer<'a>

Source

pub fn decode_slice( self, count: usize, ) -> DecoderBufferResult<'a, DecoderBuffer<'a>>

Decode a slice of bytes by count, removing the slice from the current buffer

let mut data = [0, 1, 2, 3, 4];
let buffer = DecoderBuffer::new(&mut data);

let (slice, buffer) = buffer.decode_slice(5).unwrap();
assert_eq!(slice, [0u8, 1, 2, 3, 4][..]);

assert!(buffer.is_empty());
Source

pub fn decode<T: DecoderValue<'a>>(self) -> DecoderBufferResult<'a, T>

Decode a value of type T, splitting the data from the current buffer

let mut data = [0, 1, 2, 3, 4, 5, 6];
let buffer = DecoderBuffer::new(&mut data);

let (value, buffer) = buffer.decode::<u8>().unwrap();
assert_eq!(value, 0);

let (value, buffer) = buffer.decode::<u16>().unwrap();
assert_eq!(value, 258);

let (value, buffer) = buffer.decode::<u32>().unwrap();
assert_eq!(value, 50_595_078);

assert!(buffer.is_empty());
Source

pub fn decode_slice_with_len_prefix<Length: DecoderValue<'a> + TryInto<usize>>( self, ) -> DecoderBufferResult<'a, Self>

Decode a slice prefixed by type Length, splitting the data from the current buffer. With a Length as encoded u8:

let mut data = [5, 0, 1, 2, 3, 4];
let buffer = DecoderBuffer::new(&mut data);
let (slice, buffer) = buffer.decode_slice_with_len_prefix::<u8>().unwrap();
assert_eq!(slice, [0u8, 1, 2, 3, 4][..]);
assert!(buffer.is_empty())

With a Length as encoded u16:

let mut data = [0, 5, 0, 1, 2, 3, 4];
let buffer = DecoderBuffer::new(&mut data);
let (slice, buffer) = buffer.decode_slice_with_len_prefix::<u16>().unwrap();
assert_eq!(slice, [0u8, 1, 2, 3, 4][..]);
assert!(buffer.is_empty())
Source

pub fn decode_with_len_prefix<Length: DecoderValue<'a> + TryInto<usize>, T: DecoderValue<'a>>( self, ) -> DecoderBufferResult<'a, T>

Decode a value of type T prefixed by type Length, splitting the data from the current buffer. With a Length as encoded u8 and T as u16:

let mut data = [2, 0, 1, 2, 3];
let buffer = DecoderBuffer::new(&mut data);
let (value, buffer) = buffer.decode_with_len_prefix::<u8, u16>().unwrap();
assert_eq!(value, 1);
assert_eq!(buffer, [2, 3][..])

The DecoderValue implementation of T must consume the entire subslice otherwise an error will be returned.

let mut data = [3, 0, 1, 2];
let buffer = DecoderBuffer::new(&mut data);
let result = buffer.decode_with_len_prefix::<u8, u16>();
assert!(result.is_err())
Source

pub fn decode_parameterized<T: DecoderParameterizedValue<'a>>( self, parameter: T::Parameter, ) -> DecoderBufferResult<'a, T>

Decode a parameterized value of type T implementing DecoderParameterizedValue

Source

pub fn skip(self, count: usize) -> Result<DecoderBuffer<'a>, DecoderError>

Skip a count of bytes, discarding the bytes

let mut data = [0, 1, 2, 3, 4];
let buffer = DecoderBuffer::new(&mut data);
let buffer = buffer.skip(3).unwrap();
assert_eq!(buffer, [3, 4][..]);
Source

pub fn skip_with_len_prefix<Length: DecoderValue<'a> + TryInto<usize>>( self, ) -> Result<DecoderBuffer<'a>, DecoderError>

Skip a number of bytes encoded as a length prefix of type Length With a Length encoded as u8:

let mut data = [5, 0, 1, 2, 3, 4];
let buffer = DecoderBuffer::new(&mut data);
let buffer = buffer.skip_with_len_prefix::<u8>().unwrap();
assert!(buffer.is_empty());
Source

pub fn skip_into_range( self, count: usize, original_buffer: &DecoderBufferMut<'_>, ) -> DecoderBufferResult<'a, CheckedRange>

Skip a count of bytes, returning a CheckedRange for later access

Source

pub fn skip_into_range_with_len_prefix<Length: DecoderValue<'a> + TryInto<usize>>( self, original_buffer: &DecoderBufferMut<'_>, ) -> DecoderBufferResult<'a, CheckedRange>

Skip a number of bytes encoded as a length prefix of type Length into a CheckedRange for later access

Source

pub fn get_checked_range(&self, range: &CheckedRange) -> DecoderBuffer<'_>

Reads data from a CheckedRange

Source

pub fn peek(&'a self) -> DecoderBuffer<'a>

Create a peeking DecoderBuffer from the current buffer view

let mut data = [0, 1];
let buffer = DecoderBuffer::new(&mut data);

let peek = buffer.peek();
let (value, peek) = peek.decode::<u16>().unwrap();
assert_eq!(value, 1);
assert!(peek.is_empty());

// `buffer` still contains the previous view
assert_eq!(buffer, [0, 1][..]);
Source

pub fn peek_byte(&self, index: usize) -> Result<u8, DecoderError>

Returns a single byte at index

let mut data = [0, 1, 2];
let buffer = DecoderBuffer::new(&mut data);

assert_eq!(buffer.peek_byte(0).unwrap(), 0);
assert_eq!(buffer.peek_byte(1).unwrap(), 1);
assert_eq!(buffer.peek_byte(2).unwrap(), 2);

// `buffer` has not changed
assert_eq!(buffer, [0, 1, 2][..]);
Source

pub fn peek_range( &self, range: Range<usize>, ) -> Result<DecoderBuffer<'_>, DecoderError>

Returns a PeekBuffer by range

Source

pub fn ensure_empty(&self) -> Result<(), DecoderError>

Returns an error if the buffer is not empty.

let mut data = [1];
let buffer = DecoderBuffer::new(&mut data);

assert!(buffer.ensure_empty().is_err());
let (_, buffer) = buffer.decode::<u8>().unwrap();
assert!(buffer.ensure_empty().is_ok());
Source

pub fn ensure_len(&self, len: usize) -> Result<(), DecoderError>

Returns an error if the buffer does not have at least len bytes.

let mut data = [0, 1, 2];
let buffer = DecoderBuffer::new(&mut data);

assert!(buffer.ensure_len(2).is_ok());
assert!(buffer.ensure_len(5).is_err());
Source

pub fn len(&self) -> usize

Returns the number of bytes in the buffer.

let mut data = [0, 1, 2];
let buffer = DecoderBuffer::new(&mut data);

assert_eq!(buffer.len(), 3);
let (_, buffer) = buffer.decode::<u8>().unwrap();
assert_eq!(buffer.len(), 2);
Source

pub fn is_empty(&self) -> bool

Returns true if the buffer has a length of 0.

let mut data = [1];
let buffer = DecoderBuffer::new(&mut data);

assert!(!buffer.is_empty());
let (_, buffer) = buffer.decode::<u8>().unwrap();
assert!(buffer.is_empty());
Source

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

Borrows the buffer’s slice. This should be used with caution, as it removes any panic protection this struct provides.

Trait Implementations§

Source§

impl<'a> Clone for DecoderBuffer<'a>

Source§

fn clone(&self) -> DecoderBuffer<'a>

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<'a> Debug for DecoderBuffer<'a>

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<'a> DecoderValue<'a> for DecoderBuffer<'a>

Source§

fn decode(buffer: DecoderBuffer<'a>) -> DecoderBufferResult<'a, Self>

Source§

impl<'a> DecoderValueMut<'a> for DecoderBuffer<'a>

Source§

impl EncoderValue for DecoderBuffer<'_>

Source§

fn encode<E: Encoder>(&self, encoder: &mut E)

Encodes the value into the encoder
Source§

fn encoding_size(&self) -> usize

Returns the encoding size with no buffer constrains
Source§

fn encoding_size_for_encoder<E: Encoder>(&self, _encoder: &E) -> usize

Returns the encoding size for the given encoder’s capacity
Source§

fn encode_mut<E: Encoder>(&mut self, encoder: &mut E)

Encodes the value into the encoder, while potentially mutating the value itself
Source§

fn encode_with_len_prefix<Len: TryFrom<usize> + EncoderValue, E: Encoder>( &self, encoder: &mut E, )
where Self: Sized, Len::Error: Debug,

Encodes the value into the encoder with a prefix of Len
Source§

fn encode_to_vec(&self) -> Vec<u8>

Source§

impl<'a> From<&'a [u8]> for DecoderBuffer<'a>

Source§

fn from(bytes: &'a [u8]) -> Self

Converts to this type from the input type.
Source§

impl<'a> From<&'a mut [u8]> for DecoderBuffer<'a>

Source§

fn from(bytes: &'a mut [u8]) -> Self

Converts to this type from the input type.
Source§

impl<'a> From<DecoderBufferMut<'a>> for DecoderBuffer<'a>

Source§

fn from(b: DecoderBufferMut<'a>) -> Self

Converts to this type from the input type.
Source§

impl<'a> Hash for DecoderBuffer<'a>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<'a> Ord for DecoderBuffer<'a>

Source§

fn cmp(&self, other: &DecoderBuffer<'a>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<'a> PartialEq<[u8]> for DecoderBuffer<'a>

Source§

fn eq(&self, rhs: &[u8]) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

const fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'a> PartialEq for DecoderBuffer<'a>

Source§

fn eq(&self, other: &DecoderBuffer<'a>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

const fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'a> PartialOrd for DecoderBuffer<'a>

Source§

fn partial_cmp(&self, other: &DecoderBuffer<'a>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<'a> Copy for DecoderBuffer<'a>

Source§

impl<'a> Eq for DecoderBuffer<'a>

Source§

impl<'a> StructuralPartialEq for DecoderBuffer<'a>

Auto Trait Implementations§

§

impl<'a> Freeze for DecoderBuffer<'a>

§

impl<'a> RefUnwindSafe for DecoderBuffer<'a>

§

impl<'a> Send for DecoderBuffer<'a>

§

impl<'a> Sync for DecoderBuffer<'a>

§

impl<'a> Unpin for DecoderBuffer<'a>

§

impl<'a> UnwindSafe for DecoderBuffer<'a>

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 = 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.