Skip to content

Commit

Permalink
ssh-encoding: bytes feature
Browse files Browse the repository at this point in the history
Adds (optional) support for decoding/encoding `bytes::Bytes` as
`byte[n]`.
  • Loading branch information
tarcieri committed Jul 17, 2023
1 parent 501d7ba commit 15c5d36
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions ssh-encoding/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ rust-version = "1.60"

[dependencies]
base64 = { package = "base64ct", version = "1.4", optional = true }
bytes = { version = "1", optional = true, default-features = false }
pem = { package = "pem-rfc7468", version = "0.7", optional = true }
sha2 = { version = "0.10", optional = true, default-features = false }

Expand All @@ -26,6 +27,7 @@ hex-literal = "0.4.1"
alloc = ["base64?/alloc", "pem?/alloc"]
std = ["alloc", "base64?/std", "pem?/std", "sha2?/std"]

bytes = ["alloc", "dep:bytes"]
pem = ["base64", "dep:pem"]

[package.metadata.docs.rs]
Expand Down
19 changes: 19 additions & 0 deletions ssh-encoding/src/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ use crate::{reader::Reader, Error, Result};
#[cfg(feature = "alloc")]
use alloc::{string::String, vec::Vec};

#[cfg(feature = "bytes")]
use bytes::Bytes;

#[cfg(feature = "pem")]
use {crate::PEM_LINE_WIDTH, pem::PemLabel};

Expand Down Expand Up @@ -177,3 +180,19 @@ impl Decode for Vec<String> {
})
}
}

/// Decodes `Bytes` from `byte[n]` as described in [RFC4251 § 5]:
///
/// > A byte represents an arbitrary 8-bit value (octet). Fixed length
/// > data is sometimes represented as an array of bytes, written
/// > `byte[n]`, where n is the number of bytes in the array.
///
/// [RFC4251 § 5]: https://datatracker.ietf.org/doc/html/rfc4251#section-5
#[cfg(feature = "bytes")]
impl Decode for Bytes {
type Error = Error;

fn decode(reader: &mut impl Reader) -> Result<Self> {
Vec::<u8>::decode(reader).map(Into::into)
}
}
14 changes: 14 additions & 0 deletions ssh-encoding/src/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ use core::str;
#[cfg(feature = "alloc")]
use alloc::{string::String, vec::Vec};

#[cfg(feature = "bytes")]
use bytes::Bytes;

#[cfg(feature = "pem")]
use {
crate::PEM_LINE_WIDTH,
Expand Down Expand Up @@ -249,3 +252,14 @@ impl Encode for Vec<String> {
Ok(())
}
}

#[cfg(feature = "bytes")]
impl Encode for Bytes {
fn encoded_len(&self) -> Result<usize, Error> {
self.as_ref().encoded_len()
}

fn encode(&self, writer: &mut impl Writer) -> Result<(), Error> {
self.as_ref().encode(writer)
}
}

0 comments on commit 15c5d36

Please sign in to comment.