Skip to content
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
16 changes: 13 additions & 3 deletions der/src/asn1/real.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
//! ASN.1 `NULL` support.
//! ASN.1 `REAL` support.

use crate::{
str_slice::StrSlice, ByteSlice, DecodeValue, Decoder, EncodeValue, Encoder, FixedTag, Header,
Length, Result, Tag,
};

use super::integer::uint::strip_leading_zeroes;

// TODO: panic-free implementation
#[allow(clippy::panic_in_result_fn)]
impl DecodeValue<'_> for f64 {
fn decode_value(decoder: &mut Decoder<'_>, header: Header) -> Result<Self> {
let bytes = ByteSlice::decode_value(decoder, header)?.as_bytes();
Expand All @@ -15,8 +18,10 @@ impl DecodeValue<'_> for f64 {
} else if is_nth_bit_one::<7>(bytes) {
// Binary encoding from section 8.5.7 applies
let sign: u64 = if is_nth_bit_one::<6>(bytes) { 1 } else { 0 };

// Section 8.5.7.2: Check the base -- the DER specs say that only base 2 should be supported in DER
let base = mnth_bits_to_u8::<5, 4>(bytes);

if base != 0 {
// Real related error: base is not DER compliant (base encoded in enum)
return Err(Tag::Real.value_error());
Expand Down Expand Up @@ -60,7 +65,9 @@ impl DecodeValue<'_> for f64 {
1 => Ok(f64::NEG_INFINITY),
2 => Ok(f64::NAN),
3 => Ok(-0.0_f64),
_ => unreachable!(),
_ => {
unreachable!()
}
}
} else {
let astr = StrSlice::from_bytes(&bytes[1..])?;
Expand Down Expand Up @@ -158,7 +165,10 @@ impl EncodeValue for f64 {
0 | 1 => {}
2 => first_byte |= 0b0000_0001,
3 => first_byte |= 0b0000_0010,
_ => todo!("support multi octet exponent encoding"),
_ => {
// TODO: support multi octet exponent encoding?
return Err(Tag::Real.value_error());
}
}

encoder.bytes(&[first_byte])?;
Expand Down
3 changes: 3 additions & 0 deletions der/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
)]
#![forbid(unsafe_code, clippy::unwrap_used)]
#![warn(
clippy::panic,
clippy::panic_in_result_fn,
missing_docs,
rust_2018_idioms,
unused_lifetimes,
Expand All @@ -25,6 +27,7 @@
//! - [`bool`]: ASN.1 `BOOLEAN`.
//! - [`i8`], [`i16`], [`i32`], [`i64`], [`i128`]: ASN.1 `INTEGER`.
//! - [`u8`], [`u16`], [`u32`], [`u64`], [`u128`]: ASN.1 `INTEGER`.
//! - [`f64`]: ASN.1 `REAL`
//! - [`str`], [`String`][`alloc::string::String`]: ASN.1 `UTF8String`.
//! `String` requires `alloc` feature. See also [`Utf8String`].
//! Requires `alloc` feature. See also [`SetOf`].
Expand Down