summaryrefslogtreecommitdiffstats
path: root/vendor/der/src/header.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-04 12:41:41 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-04 12:41:41 +0000
commit10ee2acdd26a7f1298c6f6d6b7af9b469fe29b87 (patch)
treebdffd5d80c26cf4a7a518281a204be1ace85b4c1 /vendor/der/src/header.rs
parentReleasing progress-linux version 1.70.0+dfsg1-9~progress7.99u1. (diff)
downloadrustc-10ee2acdd26a7f1298c6f6d6b7af9b469fe29b87.tar.xz
rustc-10ee2acdd26a7f1298c6f6d6b7af9b469fe29b87.zip
Merging upstream version 1.70.0+dfsg2.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/der/src/header.rs')
-rw-r--r--vendor/der/src/header.rs60
1 files changed, 60 insertions, 0 deletions
diff --git a/vendor/der/src/header.rs b/vendor/der/src/header.rs
new file mode 100644
index 000000000..ddb484ee7
--- /dev/null
+++ b/vendor/der/src/header.rs
@@ -0,0 +1,60 @@
+//! ASN.1 DER headers.
+
+use crate::{Decode, DerOrd, Encode, ErrorKind, Length, Reader, Result, Tag, Writer};
+use core::cmp::Ordering;
+
+/// ASN.1 DER headers: tag + length component of TLV-encoded values
+#[derive(Copy, Clone, Debug, Eq, PartialEq)]
+pub struct Header {
+ /// Tag representing the type of the encoded value
+ pub tag: Tag,
+
+ /// Length of the encoded value
+ pub length: Length,
+}
+
+impl Header {
+ /// Create a new [`Header`] from a [`Tag`] and a specified length.
+ ///
+ /// Returns an error if the length exceeds the limits of [`Length`].
+ pub fn new(tag: Tag, length: impl TryInto<Length>) -> Result<Self> {
+ let length = length.try_into().map_err(|_| ErrorKind::Overflow)?;
+ Ok(Self { tag, length })
+ }
+}
+
+impl<'a> Decode<'a> for Header {
+ fn decode<R: Reader<'a>>(reader: &mut R) -> Result<Header> {
+ let tag = Tag::decode(reader)?;
+
+ let length = Length::decode(reader).map_err(|e| {
+ if e.kind() == ErrorKind::Overlength {
+ ErrorKind::Length { tag }.into()
+ } else {
+ e
+ }
+ })?;
+
+ Ok(Self { tag, length })
+ }
+}
+
+impl Encode for Header {
+ fn encoded_len(&self) -> Result<Length> {
+ self.tag.encoded_len()? + self.length.encoded_len()?
+ }
+
+ fn encode(&self, writer: &mut dyn Writer) -> Result<()> {
+ self.tag.encode(writer)?;
+ self.length.encode(writer)
+ }
+}
+
+impl DerOrd for Header {
+ fn der_cmp(&self, other: &Self) -> Result<Ordering> {
+ match self.tag.der_cmp(&other.tag)? {
+ Ordering::Equal => self.length.der_cmp(&other.length),
+ ordering => Ok(ordering),
+ }
+ }
+}