pub struct Decoder<R> { /* private fields */ }Expand description
Decoder for SSH protocol data types over a byte Reader.
Use Decoder::from_bytes for binary input, Decoder::from_base64 with the base64
feature, or Decoder::from_pem with the pem feature. For a custom reader, use
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.
A clone has an independent read position and retains the current scope, nesting depth, and failure state.
§Example
use ssh_encoding::{Decode, Decoder};
let mut decoder = Decoder::from_bytes(&[0, 0, 0, 42])?;
let value = u32::decode(&mut decoder)?;
assert_eq!(decoder.finish(value)?, 42);Implementations§
Source§impl<'i> Decoder<Base64Reader<'i>>
impl<'i> Decoder<Base64Reader<'i>>
Sourcepub fn from_base64(base64: &'i (impl AsRef<[u8]> + ?Sized)) -> Result<Self>
Available on crate feature base64 only.
pub fn from_base64(base64: &'i (impl AsRef<[u8]> + ?Sized)) -> Result<Self>
base64 only.Create a decoder over contiguous Base64 text.
§Errors
Propagates errors from Base64Reader::new or Decoder::new.
Source§impl<'i> Decoder<PemReader<'i>>
impl<'i> Decoder<PemReader<'i>>
Sourcepub fn from_pem(pem: &'i (impl AsRef<[u8]> + ?Sized)) -> Result<Self>
Available on crate feature pem only.
pub fn from_pem(pem: &'i (impl AsRef<[u8]> + ?Sized)) -> Result<Self>
pem only.Create a decoder over the body of a PEM document, detecting its line width.
Use Self::pem_label to check the document type before decoding its body.
§Errors
Propagates errors from PemReader::new or Decoder::new.
Source§impl<R: Reader> Decoder<R>
impl<R: Reader> Decoder<R>
Sourcepub fn new(reader: R) -> Result<Self>
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.
Sourcepub fn read<'o>(&mut self, out: &'o mut [u8]) -> Result<&'o [u8]>
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
- Returns
Error::Failedif the decoder has already failed. - Returns
Error::Lengthifoutexceeds the remaining length. - Propagates errors returned from
Reader::read.
Sourcepub fn remaining_len(&self) -> usize
pub fn remaining_len(&self) -> usize
Get the number of decoded bytes remaining in the active scope.
Sourcepub fn is_finished(&self) -> bool
pub fn is_finished(&self) -> bool
Have all bytes in the active scope been consumed?
Sourcepub fn read_nested<T, E, F>(&mut self, len: usize, f: F) -> Result<T, E>
pub fn read_nested<T, E, F>(&mut self, len: usize, f: F) -> 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
- Returns
Error::Failedif the decoder has already failed. - Returns
Error::Lengthiflenexceeds the remaining length. - Returns
Error::NestingDepthif the decoder already has 64 active nested scopes. - Returns
Error::TrailingDataif the callback leaves unread bytes. - Propagates errors returned from the callback.
Sourcepub fn read_prefixed<T, E, F>(&mut self, f: F) -> Result<T, E>
pub fn read_prefixed<T, E, F>(&mut self, f: F) -> 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.
Sourcepub fn read_string_bytes<'o>(&mut self, out: &'o mut [u8]) -> Result<&'o [u8]>
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
- Returns
Error::Lengthif the payload is larger thanout. - Propagates errors returned from
Decoder::read_prefixedorDecoder::read.
Sourcepub fn read_string<'o>(&mut self, buf: &'o mut [u8]) -> Result<&'o str>
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
- Propagates errors returned from
Decoder::read_string_bytes. - Returns
Error::CharacterEncodingif the data is not valid UTF-8.
Sourcepub fn read_mpint_bytes<'o>(&mut self, out: &'o mut [u8]) -> Result<&'o [u8]>
pub fn read_mpint_bytes<'o>(&mut self, out: &'o mut [u8]) -> Result<&'o [u8]>
Decode an SSH mpint, left-padding its unsigned big-endian magnitude with zeros to fill out.
Returns the significant bytes in out, or an empty slice for zero. Redundant leading
zeros are accepted up to the 2049-byte wire limit. Decoding is variable-time.
§Errors
Returns Error::MpintEncoding for a negative encoding or Error::Length if
the encoding exceeds the wire limit or the magnitude does not fit in out.
Propagates errors from the reader.
Sourcepub fn drain(&mut self, n_bytes: usize) -> Result<()>
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.
Sourcepub fn drain_prefixed(&mut self) -> Result<usize>
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
- Propagates errors returned from
Decoder::read_prefixed. - Propagates errors returned from
Decoder::drain.
Sourcepub fn ensure_finished(&self) -> Result<()>
pub fn ensure_finished(&self) -> Result<()>
Ensure that decoding is finished.
§Errors
- Returns
Error::Failedif the decoder has already failed. - Returns
Error::TrailingDataif there is data remaining in the decoder.
Sourcepub fn finish<T>(self, value: T) -> Result<T>
pub fn finish<T>(self, value: T) -> Result<T>
Finish decoding, returning the given value if there is no remaining data.
§Errors
- Returns
Error::Failedif the decoder has already failed. - Returns
Error::TrailingDataif there is data remaining in the decoder.