#[cfg(feature = "ber")]
pub(crate) mod indefinite;
pub(super) const INDEFINITE_LENGTH_OCTET: u8 = 0b10000000;
use crate::{Decode, DerOrd, Encode, EncodingRules, Error, ErrorKind, Reader, Result, Tag, Writer};
use core::{
cmp::Ordering,
fmt,
ops::{Add, Sub},
};
#[derive(Copy, Clone, Default, Eq, Hash, PartialEq, PartialOrd, Ord)]
pub struct Length {
inner: u32,
#[cfg(feature = "ber")]
indefinite: bool,
}
impl Length {
pub const ZERO: Self = Self::new(0);
pub const ONE: Self = Self::new(1);
pub const MAX: Self = Self::new(u32::MAX);
#[cfg(feature = "ber")]
pub(crate) const EOC_LEN: Self = Self::new(2);
#[must_use]
pub const fn new(value: u32) -> Self {
Self {
inner: value,
#[cfg(feature = "ber")]
indefinite: false,
}
}
#[allow(clippy::cast_possible_truncation)]
pub(crate) const fn new_usize(len: usize) -> Result<Self> {
if len > (u32::MAX as usize) {
Err(Error::from_kind(ErrorKind::Overflow))
} else {
Ok(Self::new(len as u32))
}
}
#[must_use]
pub const fn is_zero(self) -> bool {
self.inner == 0
}
#[cfg(feature = "ber")]
pub(crate) const fn is_indefinite(self) -> bool {
self.indefinite
}
pub fn for_tlv(self, tag: Tag) -> Result<Self> {
tag.encoded_len()? + self.encoded_len()? + self
}
#[must_use]
pub fn saturating_add(self, rhs: Self) -> Self {
Self::new(self.inner.saturating_add(rhs.inner))
}
#[must_use]
pub fn saturating_sub(self, rhs: Self) -> Self {
Self::new(self.inner.saturating_sub(rhs.inner))
}
#[cfg(feature = "ber")]
pub(crate) fn sans_eoc(self) -> Self {
if self.indefinite {
debug_assert!(self >= Self::EOC_LEN);
Self {
inner: self.saturating_sub(Self::EOC_LEN).inner,
indefinite: true,
}
} else {
self
}
}
fn initial_octet(self) -> Option<u8> {
match self.inner {
0x80..=0xFF => Some(0x81),
0x100..=0xFFFF => Some(0x82),
0x10000..=0xFFFFFF => Some(0x83),
0x1000000..=0xFFFFFFFF => Some(0x84),
_ => None,
}
}
}
impl Add for Length {
type Output = Result<Self>;
fn add(self, other: Self) -> Result<Self> {
self.inner
.checked_add(other.inner)
.ok_or_else(|| ErrorKind::Overflow.into())
.map(Self::new)
}
}
impl Add<u8> for Length {
type Output = Result<Self>;
fn add(self, other: u8) -> Result<Self> {
self + Length::from(other)
}
}
impl Add<u16> for Length {
type Output = Result<Self>;
fn add(self, other: u16) -> Result<Self> {
self + Length::from(other)
}
}
impl Add<u32> for Length {
type Output = Result<Self>;
fn add(self, other: u32) -> Result<Self> {
self + Length::from(other)
}
}
impl Add<usize> for Length {
type Output = Result<Self>;
fn add(self, other: usize) -> Result<Self> {
self + Length::try_from(other)?
}
}
impl Add<Length> for Result<Length> {
type Output = Self;
fn add(self, other: Length) -> Self {
self? + other
}
}
impl Sub for Length {
type Output = Result<Self>;
fn sub(self, other: Length) -> Result<Self> {
self.inner
.checked_sub(other.inner)
.ok_or_else(|| ErrorKind::Overflow.into())
.map(Self::new)
}
}
impl Sub<Length> for Result<Length> {
type Output = Self;
fn sub(self, other: Length) -> Self {
self? - other
}
}
impl From<u8> for Length {
fn from(len: u8) -> Length {
Length::new(len.into())
}
}
impl From<u16> for Length {
fn from(len: u16) -> Length {
Length::new(len.into())
}
}
impl From<u32> for Length {
fn from(len: u32) -> Length {
Length::new(len)
}
}
impl From<Length> for u32 {
fn from(length: Length) -> u32 {
length.inner
}
}
impl TryFrom<usize> for Length {
type Error = Error;
fn try_from(len: usize) -> Result<Length> {
Length::new_usize(len)
}
}
impl TryFrom<Length> for usize {
type Error = Error;
fn try_from(len: Length) -> Result<usize> {
len.inner.try_into().map_err(|_| ErrorKind::Overflow.into())
}
}
impl<'a> Decode<'a> for Length {
type Error = Error;
fn decode<R: Reader<'a>>(reader: &mut R) -> Result<Length> {
match reader.read_byte()? {
len if len < INDEFINITE_LENGTH_OCTET => Ok(len.into()),
INDEFINITE_LENGTH_OCTET => match reader.encoding_rules() {
#[cfg(feature = "ber")]
EncodingRules::Ber => indefinite::decode_indefinite_length(&mut reader.clone()),
EncodingRules::Der => Err(reader.error(ErrorKind::IndefiniteLength)),
},
tag @ 0x81..=0x84 => {
let nbytes = tag
.checked_sub(0x80)
.ok_or_else(|| reader.error(ErrorKind::Overlength))?
as usize;
debug_assert!(nbytes <= 4);
let mut decoded_len = 0u32;
for _ in 0..nbytes {
decoded_len = decoded_len
.checked_shl(8)
.ok_or_else(|| reader.error(ErrorKind::Overflow))?
| u32::from(reader.read_byte()?);
}
let length = Length::from(decoded_len);
if length.initial_octet() == Some(tag) {
Ok(length)
} else {
Err(reader.error(ErrorKind::Overlength))
}
}
_ => {
Err(reader.error(ErrorKind::Overlength))
}
}
}
}
impl Encode for Length {
fn encoded_len(&self) -> Result<Length> {
match self.inner {
0..=0x7F => Ok(Length::new(1)),
0x80..=0xFF => Ok(Length::new(2)),
0x100..=0xFFFF => Ok(Length::new(3)),
0x10000..=0xFFFFFF => Ok(Length::new(4)),
0x1000000..=0xFFFFFFFF => Ok(Length::new(5)),
}
}
fn encode(&self, writer: &mut impl Writer) -> Result<()> {
match self.initial_octet() {
Some(tag_byte) => {
writer.write_byte(tag_byte)?;
match self.inner.to_be_bytes() {
[0, 0, 0, byte] => writer.write_byte(byte),
[0, 0, bytes @ ..] => writer.write(&bytes),
[0, bytes @ ..] => writer.write(&bytes),
bytes => writer.write(&bytes),
}
}
#[allow(clippy::cast_possible_truncation)]
None => writer.write_byte(self.inner as u8),
}
}
}
impl DerOrd for Length {
fn der_cmp(&self, other: &Self) -> Result<Ordering> {
Ok(self.inner.cmp(&other.inner))
}
}
impl fmt::Debug for Length {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
#[cfg(feature = "ber")]
if self.indefinite {
return write!(f, "Length([indefinite])");
}
f.debug_tuple("Length").field(&self.inner).finish()
}
}
impl fmt::Display for Length {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.inner.fmt(f)
}
}
#[cfg(feature = "arbitrary")]
impl<'a> arbitrary::Arbitrary<'a> for Length {
fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
Ok(Self::new(u.arbitrary()?))
}
fn size_hint(depth: usize) -> (usize, Option<usize>) {
u32::size_hint(depth)
}
}
#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests {
use super::Length;
use crate::{Decode, DerOrd, Encode, ErrorKind};
use core::cmp::Ordering;
#[test]
fn decode() {
assert_eq!(Length::ZERO, Length::from_der(&[0x00]).unwrap());
assert_eq!(Length::from(0x7Fu8), Length::from_der(&[0x7F]).unwrap());
assert_eq!(
Length::from(0x80u8),
Length::from_der(&[0x81, 0x80]).unwrap()
);
assert_eq!(
Length::from(0xFFu8),
Length::from_der(&[0x81, 0xFF]).unwrap()
);
assert_eq!(
Length::from(0x100u16),
Length::from_der(&[0x82, 0x01, 0x00]).unwrap()
);
assert_eq!(
Length::from(0x10000u32),
Length::from_der(&[0x83, 0x01, 0x00, 0x00]).unwrap()
);
assert_eq!(
Length::from(0xFFFFFFFFu32),
Length::from_der(&[0x84, 0xFF, 0xFF, 0xFF, 0xFF]).unwrap()
);
}
#[test]
fn encode() {
let mut buffer = [0u8; 5];
assert_eq!(&[0x00], Length::ZERO.encode_to_slice(&mut buffer).unwrap());
assert_eq!(
&[0x7F],
Length::from(0x7Fu8).encode_to_slice(&mut buffer).unwrap()
);
assert_eq!(
&[0x81, 0x80],
Length::from(0x80u8).encode_to_slice(&mut buffer).unwrap()
);
assert_eq!(
&[0x81, 0xFF],
Length::from(0xFFu8).encode_to_slice(&mut buffer).unwrap()
);
assert_eq!(
&[0x82, 0x01, 0x00],
Length::from(0x100u16).encode_to_slice(&mut buffer).unwrap()
);
assert_eq!(
&[0x83, 0x01, 0x00, 0x00],
Length::from(0x10000u32)
.encode_to_slice(&mut buffer)
.unwrap()
);
assert_eq!(
&[0x84, 0xFF, 0xFF, 0xFF, 0xFF],
Length::from(0xFFFFFFFFu32)
.encode_to_slice(&mut buffer)
.unwrap()
);
}
#[test]
fn add_overflows_when_max_length_exceeded() {
let result = Length::MAX + Length::ONE;
assert_eq!(
result.err().map(super::super::error::Error::kind),
Some(ErrorKind::Overflow)
);
}
#[test]
fn der_ord() {
assert_eq!(Length::ONE.der_cmp(&Length::MAX).unwrap(), Ordering::Less);
assert_eq!(Length::ONE.der_cmp(&Length::ONE).unwrap(), Ordering::Equal);
assert_eq!(
Length::ONE.der_cmp(&Length::ZERO).unwrap(),
Ordering::Greater
);
}
}