use core::fmt;
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DecodeError {
Truncated,
TrailingBytes,
InvalidUtf8,
InvalidValue,
Empty,
}
impl fmt::Display for DecodeError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let s = match self {
DecodeError::Truncated => "payload ended before a field was complete",
DecodeError::TrailingBytes => "bytes left over after the last field",
DecodeError::InvalidUtf8 => "text field is not valid UTF-8",
DecodeError::InvalidValue => "field holds a value the format does not allow",
DecodeError::Empty => "empty payload",
};
f.write_str(s)
}
}
impl std::error::Error for DecodeError {}
#[derive(Debug, Clone)]
pub struct Reader<'a> {
buf: &'a [u8],
}
impl<'a> Reader<'a> {
pub fn new(buf: &'a [u8]) -> Self {
Reader { buf }
}
pub fn remaining(&self) -> &'a [u8] {
self.buf
}
pub fn bytes(&mut self, n: usize) -> Result<&'a [u8], DecodeError> {
if self.buf.len() < n {
return Err(DecodeError::Truncated);
}
let (head, tail) = self.buf.split_at(n);
self.buf = tail;
Ok(head)
}
pub fn u8(&mut self) -> Result<u8, DecodeError> {
Ok(self.bytes(1)?[0])
}
pub fn bool(&mut self) -> Result<bool, DecodeError> {
Ok(self.u8()? != 0)
}
pub fn u32(&mut self) -> Result<u32, DecodeError> {
let b = self.bytes(4)?;
Ok(u32::from_be_bytes([b[0], b[1], b[2], b[3]]))
}
pub fn string(&mut self) -> Result<&'a [u8], DecodeError> {
let len = self.u32()? as usize;
self.bytes(len)
}
pub fn utf8(&mut self) -> Result<&'a str, DecodeError> {
core::str::from_utf8(self.string()?).map_err(|_| DecodeError::InvalidUtf8)
}
pub fn name_list(&mut self) -> Result<Vec<String>, DecodeError> {
let s = self.utf8()?;
if s.is_empty() {
return Ok(Vec::new());
}
Ok(s.split(',').map(str::to_owned).collect())
}
pub fn finish(&self) -> Result<(), DecodeError> {
if self.buf.is_empty() {
Ok(())
} else {
Err(DecodeError::TrailingBytes)
}
}
}
#[derive(Debug, Clone, Default)]
pub struct Writer {
buf: Vec<u8>,
}
impl Writer {
pub fn new() -> Self {
Writer::default()
}
pub(crate) fn appending(buf: Vec<u8>) -> Self {
Writer { buf }
}
pub fn into_bytes(self) -> Vec<u8> {
self.buf
}
pub fn bytes(&mut self, b: &[u8]) -> &mut Self {
self.buf.extend_from_slice(b);
self
}
pub fn u8(&mut self, v: u8) -> &mut Self {
self.buf.push(v);
self
}
pub fn bool(&mut self, v: bool) -> &mut Self {
self.u8(u8::from(v))
}
pub fn u32(&mut self, v: u32) -> &mut Self {
self.bytes(&v.to_be_bytes())
}
pub fn string(&mut self, s: &[u8]) -> &mut Self {
let len = u32::try_from(s.len()).expect("SSH string longer than u32::MAX");
self.u32(len).bytes(s)
}
pub fn name_list<S: AsRef<str>>(&mut self, names: &[S]) -> &mut Self {
let joined = names
.iter()
.map(AsRef::as_ref)
.collect::<Vec<_>>()
.join(",");
self.string(joined.as_bytes())
}
pub fn mpint_unsigned(&mut self, magnitude: &[u8]) -> &mut Self {
let start = magnitude
.iter()
.position(|&b| b != 0)
.unwrap_or(magnitude.len());
let m = &magnitude[start..];
if m.first().is_some_and(|&b| b & 0x80 != 0) {
let len = u32::try_from(m.len() + 1).expect("mpint longer than u32::MAX");
self.u32(len).u8(0).bytes(m)
} else {
self.string(m)
}
}
}