Skip to content

Commit bec01d0

Browse files
authored
pkcs7: avoid code duplication (#926)
1 parent b0b4311 commit bec01d0

File tree

1 file changed

+26
-46
lines changed

1 file changed

+26
-46
lines changed

pkcs7/src/content_info.rs

+26-46
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ use crate::{
55

66
use der::{
77
asn1::{ContextSpecific, OctetStringRef},
8-
DecodeValue, Encode, EncodeValue, Header, Length, Reader, Sequence, TagMode, TagNumber, Writer,
8+
Decode, DecodeValue, Encode, EncodeValue, Header, Length, Reader, Sequence, TagMode, TagNumber,
9+
Writer,
910
};
1011

1112
const CONTENT_TAG: TagNumber = TagNumber::new(0);
@@ -67,56 +68,35 @@ impl<'a> ContentInfo<'a> {
6768

6869
impl<'a> DecodeValue<'a> for ContentInfo<'a> {
6970
fn decode_value<R: Reader<'a>>(reader: &mut R, header: Header) -> der::Result<ContentInfo<'a>> {
71+
#[inline]
72+
fn decode_context_specific<'a, R: Reader<'a>, T: Decode<'a>>(
73+
reader: &mut R,
74+
) -> der::Result<T> {
75+
Ok(ContextSpecific::<T>::decode_explicit(reader, CONTENT_TAG)?
76+
.ok_or_else(|| {
77+
der::Tag::ContextSpecific {
78+
number: CONTENT_TAG,
79+
constructed: false,
80+
}
81+
.value_error()
82+
})?
83+
.value)
84+
}
85+
7086
reader.read_nested(header.length, |reader| {
7187
let content_type = reader.decode()?;
7288
match content_type {
73-
ContentType::Data => Ok(ContentInfo::Data(
74-
ContextSpecific::<DataContent<'_>>::decode_explicit(reader, CONTENT_TAG)?
75-
.ok_or_else(|| {
76-
der::Tag::ContextSpecific {
77-
number: CONTENT_TAG,
78-
constructed: false,
79-
}
80-
.value_error()
81-
})?
82-
.value,
83-
)),
84-
ContentType::EncryptedData => Ok(ContentInfo::EncryptedData(
85-
ContextSpecific::<EncryptedDataContent<'_>>::decode_explicit(
86-
reader,
87-
CONTENT_TAG,
88-
)?
89-
.ok_or_else(|| {
90-
der::Tag::ContextSpecific {
91-
number: CONTENT_TAG,
92-
constructed: false,
93-
}
94-
.value_error()
95-
})?
96-
.value,
97-
)),
98-
ContentType::SignedData => Ok(ContentInfo::SignedData(
99-
ContextSpecific::<SignedDataContent<'_>>::decode_explicit(reader, CONTENT_TAG)?
100-
.ok_or_else(|| {
101-
der::Tag::ContextSpecific {
102-
number: CONTENT_TAG,
103-
constructed: false,
104-
}
105-
.value_error()
106-
})?
107-
.value,
108-
)),
89+
ContentType::Data => Ok(ContentInfo::Data(decode_context_specific(reader)?)),
90+
ContentType::EncryptedData => {
91+
Ok(ContentInfo::EncryptedData(decode_context_specific(reader)?))
92+
}
93+
ContentType::SignedData => {
94+
Ok(ContentInfo::SignedData(decode_context_specific(reader)?))
95+
}
96+
10997
_ => Ok(ContentInfo::Other((
11098
content_type,
111-
ContextSpecific::<OctetStringRef<'_>>::decode_explicit(reader, CONTENT_TAG)?
112-
.ok_or_else(|| {
113-
der::Tag::ContextSpecific {
114-
number: CONTENT_TAG,
115-
constructed: false,
116-
}
117-
.value_error()
118-
})?
119-
.value,
99+
decode_context_specific(reader)?,
120100
))),
121101
}
122102
})

0 commit comments

Comments
 (0)