Struct s2n_codec::decoder::buffer::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 copy of the value. Read more
1.0.0 · source§

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 + PartialOrd,

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

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

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method 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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method 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

This method 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

This method 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

This method 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

This method 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> 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,

§

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>,

§

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>,

§

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.