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
20 changes: 20 additions & 0 deletions ssh-encoding/src/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,26 @@ impl Decode for u8 {
}
}

/// Decode a `boolean` as described in [RFC4251 § 5]:
///
/// > A boolean value is stored as a single byte. The value 0
/// > represents FALSE, and the value 1 represents TRUE. All non-zero
/// > values MUST be interpreted as TRUE; however, applications MUST NOT
/// > store values other than 0 and 1.
///
/// [RFC4251 § 5]: https://datatracker.ietf.org/doc/html/rfc4251#section-5
impl Decode for bool {
type Error = Error;

fn decode(reader: &mut impl Reader) -> Result<Self> {
let byte = u8::decode(reader)?;
match byte {
0 => Ok(false),
_ => Ok(true),
}
}
}

/// Decode a `uint32` as described in [RFC4251 § 5]:
///
/// > Represents a 32-bit unsigned integer. Stored as four bytes in the
Expand Down
22 changes: 22 additions & 0 deletions ssh-encoding/src/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,28 @@ impl Encode for u8 {
}
}

/// Encode a `boolean` as described in [RFC4251 § 5]:
///
/// > A boolean value is stored as a single byte. The value 0
/// > represents FALSE, and the value 1 represents TRUE. All non-zero
/// > values MUST be interpreted as TRUE; however, applications MUST NOT
/// > store values other than 0 and 1.
///
/// [RFC4251 § 5]: https://datatracker.ietf.org/doc/html/rfc4251#section-5
impl Encode for bool {
fn encoded_len(&self) -> Result<usize, Error> {
Ok(1)
}

fn encode(&self, writer: &mut impl Writer) -> Result<(), Error> {
if *self {
1u8.encode(writer)
} else {
0u8.encode(writer)
}
}
}

/// Encode a `uint32` as described in [RFC4251 § 5]:
///
/// > Represents a 32-bit unsigned integer. Stored as four bytes in the
Expand Down
12 changes: 12 additions & 0 deletions ssh-encoding/tests/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,18 @@ fn decode_u8() {
assert_eq!(ret, 0x42u8);
}

#[test]
fn decode_boolean() {
let mut bytes = hex!("01").as_slice();
let ret = bool::decode(&mut bytes).unwrap();
assert_eq!(ret, true);

// "All non-zero values MUST be interpreted as TRUE"
let mut bytes = hex!("FF").as_slice();
let ret = bool::decode(&mut bytes).unwrap();
assert_eq!(ret, true);
}

#[test]
fn decode_u32() {
let mut bytes = hex!("DEADBEEF").as_slice();
Expand Down
7 changes: 7 additions & 0 deletions ssh-encoding/tests/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ fn encode_u8() {
assert_eq!(out, hex!("42"));
}

#[test]
fn encode_boolean() {
let mut out = Vec::new();
true.encode(&mut out).unwrap();
assert_eq!(out, hex!("01"));
}

#[test]
fn encode_u32() {
let mut out = Vec::new();
Expand Down