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

DecoderBufferMut is a panic-free, mutable byte buffer for decoding untrusted input

Implementations§

source§

impl<'a> DecoderBufferMut<'a>

source

pub fn new(bytes: &'a mut [u8]) -> DecoderBufferMut<'a>

Create a new DecoderBufferMut from a byte slice

source

pub fn freeze(self) -> DecoderBuffer<'a>

Freeze the mutable buffer into a DecoderBuffer

source

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

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

source

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

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

source§

impl<'a> DecoderBufferMut<'a>

source

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

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

let mut data = [0, 1, 2, 3, 4];
let buffer = DecoderBufferMut::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>(self) -> Result<(T, DecoderBufferMut<'a>), DecoderError>
where T: DecoderValueMut<'a>,

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 = DecoderBufferMut::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>( self ) -> Result<(DecoderBufferMut<'a>, DecoderBufferMut<'a>), DecoderError>
where Length: DecoderValueMut<'a> + TryInto<usize>,

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 = DecoderBufferMut::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 = DecoderBufferMut::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, T>( self ) -> Result<(T, DecoderBufferMut<'a>), DecoderError>
where Length: DecoderValueMut<'a> + TryInto<usize>, T: DecoderValueMut<'a>,

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 = DecoderBufferMut::new(&mut data);
let (value, buffer) = buffer.decode_with_len_prefix::<u8, u16>().unwrap();
assert_eq!(value, 1);
assert_eq!(buffer, [2, 3][..])

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

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

pub fn decode_parameterized<T>( self, parameter: <T as DecoderParameterizedValueMut<'a>>::Parameter ) -> Result<(T, DecoderBufferMut<'a>), DecoderError>

Decode a parameterized value of type T implementing DecoderParameterizedValueMut

source

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

Skip a count of bytes, discarding the bytes

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

pub fn skip_with_len_prefix<Length>( self ) -> Result<DecoderBufferMut<'a>, DecoderError>
where Length: DecoderValueMut<'a> + TryInto<usize>,

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 = DecoderBufferMut::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<'_> ) -> Result<(CheckedRange, DecoderBufferMut<'a>), DecoderError>

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

source

pub fn skip_into_range_with_len_prefix<Length>( self, original_buffer: &DecoderBufferMut<'_> ) -> Result<(CheckedRange, DecoderBufferMut<'a>), DecoderError>
where Length: DecoderValueMut<'a> + TryInto<usize>,

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 = DecoderBufferMut::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 = DecoderBufferMut::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 = DecoderBufferMut::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 = DecoderBufferMut::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 = DecoderBufferMut::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 = DecoderBufferMut::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> Debug for DecoderBufferMut<'a>

source§

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

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

impl<'a> Decoder<'a> for DecoderBufferMut<'a>

source§

impl<'a> DecoderValueMut<'a> for DecoderBufferMut<'a>

source§

impl EncoderValue for DecoderBufferMut<'_>

source§

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

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>(&self, _encoder: &E) -> usize
where E: Encoder,

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

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

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

fn encode_with_len_prefix<Len, E>(&self, encoder: &mut E)
where Len: TryFrom<usize> + EncoderValue, E: Encoder, Self: Sized, <Len as TryFrom<usize>>::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 mut [u8]> for DecoderBufferMut<'a>

source§

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

Converts to this type from the input type.
source§

impl<'a> Hash for DecoderBufferMut<'a>

source§

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

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 DecoderBufferMut<'a>

source§

fn cmp(&self, other: &DecoderBufferMut<'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 DecoderBufferMut<'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 DecoderBufferMut<'a>

source§

fn eq(&self, other: &DecoderBufferMut<'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 DecoderBufferMut<'a>

source§

fn partial_cmp(&self, other: &DecoderBufferMut<'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> Eq for DecoderBufferMut<'a>

source§

impl<'a> StructuralPartialEq for DecoderBufferMut<'a>

Auto Trait Implementations§

§

impl<'a> Freeze for DecoderBufferMut<'a>

§

impl<'a> RefUnwindSafe for DecoderBufferMut<'a>

§

impl<'a> Send for DecoderBufferMut<'a>

§

impl<'a> Sync for DecoderBufferMut<'a>

§

impl<'a> Unpin for DecoderBufferMut<'a>

§

impl<'a> !UnwindSafe for DecoderBufferMut<'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> PacketPayloadEncoder for T
where T: EncoderValue,

source§

fn encoding_size_hint<E>(&mut self, encoder: &E, minimum_len: usize) -> usize
where E: Encoder,

Returns an estimate of the encoding size of the payload. This may be inaccurate from what actually is encoded. Estimates should be less than or equal to what is actually written. Implementations can return 0 to skip encoding.
source§

fn encode( &mut self, buffer: &mut Buffer<'_>, _minimum_len: usize, _header_len: usize, _tag_len: usize )

Encodes the payload into the buffer. Implementations should ensure the encoding len is at least the minimum_len, otherwise the packet writing will panic.
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.
source§

impl<T, U> Upcast<T> for U
where T: UpcastFrom<U>,

source§

fn upcast(self) -> T

source§

impl<T, B> UpcastFrom<Counter<T, B>> for T

source§

fn upcast_from(value: Counter<T, B>) -> T