From 9835e2ae736235810b4ea1c162ca5e65c547e770 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sat, 18 May 2024 04:49:50 +0200 Subject: Merging upstream version 1.71.1+dfsg1. Signed-off-by: Daniel Baumann --- vendor/der/src/asn1/integer.rs | 121 +---------------------------------------- 1 file changed, 3 insertions(+), 118 deletions(-) (limited to 'vendor/der/src/asn1/integer.rs') diff --git a/vendor/der/src/asn1/integer.rs b/vendor/der/src/asn1/integer.rs index 20e2f018f..a6e913d66 100644 --- a/vendor/der/src/asn1/integer.rs +++ b/vendor/der/src/asn1/integer.rs @@ -1,132 +1,17 @@ //! ASN.1 `INTEGER` support. -pub(super) mod bigint; pub(super) mod int; pub(super) mod uint; -use crate::{ - asn1::AnyRef, ByteSlice, DecodeValue, EncodeValue, Error, FixedTag, Header, Length, Reader, - Result, SliceWriter, Tag, ValueOrd, Writer, -}; use core::{cmp::Ordering, mem}; -macro_rules! impl_int_encoding { - ($($int:ty => $uint:ty),+) => { - $( - impl<'a> DecodeValue<'a> for $int { - fn decode_value>(reader: &mut R, header: Header) -> Result { - let bytes = ByteSlice::decode_value(reader, header)?.as_slice(); +use crate::{EncodeValue, Result, SliceWriter}; - let result = if is_highest_bit_set(bytes) { - <$uint>::from_be_bytes(int::decode_to_array(bytes)?) as $int - } else { - Self::from_be_bytes(uint::decode_to_array(bytes)?) - }; - - // Ensure we compute the same encoded length as the original any value - if header.length != result.value_len()? { - return Err(Self::TAG.non_canonical_error()); - } - - Ok(result) - } - } - - impl EncodeValue for $int { - fn value_len(&self) -> Result { - if *self < 0 { - int::encoded_len(&(*self as $uint).to_be_bytes()) - } else { - uint::encoded_len(&self.to_be_bytes()) - } - } - - fn encode_value(&self, writer: &mut dyn Writer) -> Result<()> { - if *self < 0 { - int::encode_bytes(writer, &(*self as $uint).to_be_bytes()) - } else { - uint::encode_bytes(writer, &self.to_be_bytes()) - } - } - } - - impl FixedTag for $int { - const TAG: Tag = Tag::Integer; - } - - impl ValueOrd for $int { - fn value_cmp(&self, other: &Self) -> Result { - value_cmp(*self, *other) - } - } - - impl TryFrom> for $int { - type Error = Error; - - fn try_from(any: AnyRef<'_>) -> Result { - any.decode_into() - } - } - )+ - }; -} - -macro_rules! impl_uint_encoding { - ($($uint:ty),+) => { - $( - impl<'a> DecodeValue<'a> for $uint { - fn decode_value>(reader: &mut R, header: Header) -> Result { - let bytes = ByteSlice::decode_value(reader, header)?.as_slice(); - let result = Self::from_be_bytes(uint::decode_to_array(bytes)?); - - // Ensure we compute the same encoded length as the original any value - if header.length != result.value_len()? { - return Err(Self::TAG.non_canonical_error()); - } - - Ok(result) - } - } - - impl EncodeValue for $uint { - fn value_len(&self) -> Result { - uint::encoded_len(&self.to_be_bytes()) - } - - fn encode_value(&self, writer: &mut dyn Writer) -> Result<()> { - uint::encode_bytes(writer, &self.to_be_bytes()) - } - } - - impl FixedTag for $uint { - const TAG: Tag = Tag::Integer; - } - - impl ValueOrd for $uint { - fn value_cmp(&self, other: &Self) -> Result { - value_cmp(*self, *other) - } - } - - impl TryFrom> for $uint { - type Error = Error; - - fn try_from(any: AnyRef<'_>) -> Result { - any.decode_into() - } - } - )+ - }; -} - -impl_int_encoding!(i8 => u8, i16 => u16, i32 => u32, i64 => u64, i128 => u128); -impl_uint_encoding!(u8, u16, u32, u64, u128); - -/// Is the highest bit of the first byte in the slice 1? (if present) +/// Is the highest bit of the first byte in the slice set to `1`? (if present) #[inline] fn is_highest_bit_set(bytes: &[u8]) -> bool { bytes - .get(0) + .first() .map(|byte| byte & 0b10000000 != 0) .unwrap_or(false) } -- cgit v1.2.3