Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

der: bytes feature #1156

Merged
merged 1 commit into from
Jul 17, 2023
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.

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