Skip to content

Commit

Permalink
der: bytes feature
Browse files Browse the repository at this point in the history
Adds (optional) support for using `bytes::Bytes` as an `OctetString`.
  • Loading branch information
tarcieri committed Jul 17, 2023
1 parent bd793e6 commit 3699ed9
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 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.

4 changes: 3 additions & 1 deletion der/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ rust-version = "1.65"

[dependencies]
arbitrary = { version = "1.3", features = ["derive"], optional = true }
const-oid = { version = "0.9.2", optional = true } # TODO: path = "../const-oid"
bytes = { version = "1", optional = true, default-features = false }
const-oid = { version = "0.9.2", optional = true }
der_derive = { version = "0.7.1", optional = true }
flagset = { version = "0.4.3", optional = true }
pem-rfc7468 = { version = "0.7", optional = true, features = ["alloc"] }
Expand All @@ -33,6 +34,7 @@ alloc = ["zeroize?/alloc"]
std = ["alloc"]

arbitrary = ["dep:arbitrary", "const-oid?/arbitrary", "std"]
bytes = ["dep:bytes", "alloc"]
derive = ["dep:der_derive"]
oid = ["dep:const-oid"]
pem = ["dep:pem-rfc7468", "alloc", "zeroize"]
Expand Down
29 changes: 28 additions & 1 deletion der/src/asn1/octet_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ mod allocating {
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
pub struct OctetString {
/// Bitstring represented as a slice of bytes.
inner: Vec<u8>,
pub(super) inner: Vec<u8>,
}

impl OctetString {
Expand Down Expand Up @@ -214,6 +214,33 @@ mod allocating {
}
}

#[cfg(feature = "bytes")]
mod bytes {
use super::OctetString;
use crate::{DecodeValue, EncodeValue, FixedTag, Header, Length, Reader, Result, Tag, Writer};
use bytes::Bytes;

impl<'a> DecodeValue<'a> for Bytes {
fn decode_value<R: Reader<'a>>(reader: &mut R, header: Header) -> Result<Self> {
OctetString::decode_value(reader, header).map(|octet_string| octet_string.inner.into())
}
}

impl EncodeValue for Bytes {
fn value_len(&self) -> Result<Length> {
self.len().try_into()
}

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

impl FixedTag for Bytes {
const TAG: Tag = Tag::OctetString;
}
}

#[cfg(test)]
mod tests {
use crate::asn1::{OctetStringRef, PrintableStringRef};
Expand Down

0 comments on commit 3699ed9

Please sign in to comment.