Skip to main content

Decoder

Struct Decoder 

Source
pub struct Decoder<R> { /* private fields */ }
Expand description

Decoder for SSH protocol data types over a byte Reader.

Construct a decoder with Decoder::new or Reader::into_decoder.

All nested decoding uses the same decoder type, with enclosing bounds stored on the call stack. A decoder permits up to 64 active nested scopes. Neither nesting nor decoding from Base64 or PEM requires a heap allocation.

A failed read or nested decoding operation invalidates the decoder. Subsequent reads and completion checks return Error::Failed. The underlying reader is not rewound on errors.

§Example

use ssh_encoding::{Decode, Reader};

let mut decoder = [0, 0, 0, 42].as_slice().into_decoder()?;
let value = u32::decode(&mut decoder)?;
assert_eq!(decoder.finish(value)?, 42);

Implementations§

Source§

impl<R: Reader> Decoder<R>

Source

pub fn new(reader: R) -> Result<Self>

Create a decoder which owns the given reader.

§Errors

Returns Error::Length if the remaining decoded input exceeds MAX_MSG_SIZE.

Source

pub fn read<'o>(&mut self, out: &'o mut [u8]) -> Result<&'o [u8]>

Read enough bytes to exactly fill out, without crossing the active scope’s boundary.

§Errors
Source

pub fn remaining_len(&self) -> usize

Get the number of decoded bytes remaining in the active scope.

Source

pub fn is_finished(&self) -> bool

Have all bytes in the active scope been consumed?

Source

pub fn read_nested<T, E, F>(&mut self, len: usize, f: F) -> Result<T, E>
where E: From<Error>, F: FnOnce(&mut Self) -> Result<T, E>,

Decode exactly len bytes within a nested scope.

The callback receives the same decoder type with its bounds temporarily narrowed. It must consume the entire scope before returning successfully. Enclosing bounds are restored on success, error, or unwinding, without rewinding the underlying reader.

§Errors
Source

pub fn read_prefixed<T, E, F>(&mut self, f: F) -> Result<T, E>
where E: From<Error>, F: FnOnce(&mut Self) -> Result<T, E>,

Decode a uint32 length prefix, then decode its body in a nested scope.

§Errors

Propagates errors decoding the prefix or returned by Decoder::read_nested.

Source

pub fn read_string_bytes<'o>(&mut self, out: &'o mut [u8]) -> Result<&'o [u8]>

Decode an SSH binary string into out and return the filled portion.

The uint32 length prefix gives the number of payload bytes to read (RFC4251 §5). To decode to a str, use Decoder::read_string.

The Decode impl on Vec<u8> can be used to allocate a buffer for the result.

§Errors
Source

pub fn read_string<'o>(&mut self, buf: &'o mut [u8]) -> Result<&'o str>

Decode an SSH string into buf and return the filled portion as a &str (RFC4251 § 5).

Arbitrary length binary string. Strings are allowed to contain arbitrary binary data, including null characters and 8-bit characters. They are stored as a uint32 containing its length (number of bytes that follow) and zero (= empty string) or more bytes that are the value of the string. Terminating null characters are not used.

Strings are also used to store text. In that case, US-ASCII is used for internal names, and ISO-10646 UTF-8 for text that might be displayed to the user. The terminating null character SHOULD NOT normally be stored in the string. For example: the US-ASCII string “testing” is represented as 00 00 00 07 t e s t i n g. The UTF-8 mapping does not alter the encoding of US-ASCII characters.

Storage for the string data must be provided as mutable byte slice in order to accommodate no_alloc use cases.

The Decode impl on String can be used to allocate a buffer for the result.

§Errors
Source

pub fn drain(&mut self, n_bytes: usize) -> Result<()>

Drain the given number of bytes from the reader, discarding them.

§Errors

Propagates errors returned from Decoder::read.

Source

pub fn drain_prefixed(&mut self) -> Result<usize>

Decode a u32 length prefix, and then drain the length of the body.

Upon success, returns the number of bytes drained sans the length of the u32 length prefix (4-bytes).

§Errors
Source

pub fn ensure_finished(&self) -> Result<()>

Ensure that decoding is finished.

§Errors
Source

pub fn finish<T>(self, value: T) -> Result<T>

Finish decoding, returning the given value if there is no remaining data.

§Errors

Trait Implementations§

Source§

impl<R: Reader> Debug for Decoder<R>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<R> Forget for Decoder<R>

§

impl<R> Freeze for Decoder<R>
where R: Freeze,

§

impl<R> Move for Decoder<R>

§

impl<R> RefUnwindSafe for Decoder<R>
where R: RefUnwindSafe,

§

impl<R> Send for Decoder<R>
where R: Send,

§

impl<R> Sync for Decoder<R>
where R: Sync,

§

impl<R> Unpin for Decoder<R>
where R: Unpin,

§

impl<R> UnsafeUnpin for Decoder<R>
where R: UnsafeUnpin,

§

impl<R> UnwindSafe for Decoder<R>
where R: UnwindSafe,

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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.