Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
}