aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / vendor / der / src / asn1 / null.rs
| 1 | //! ASN.1 `NULL` support. |
| 2 | |
| 3 | use crate::{ |
| 4 | asn1::AnyRef, ord::OrdIsValueOrd, BytesRef, DecodeValue, EncodeValue, Error, ErrorKind, |
| 5 | FixedTag, Header, Length, Reader, Result, Tag, Writer, |
| 6 | }; |
| 7 | |
| 8 | /// ASN.1 `NULL` type. |
| 9 | #[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)] |
| 10 | pub struct Null; |
| 11 | |
| 12 | impl_any_conversions!(Null); |
| 13 | |
| 14 | impl<'a> DecodeValue<'a> for Null { |
| 15 | fn decode_value<R: Reader<'a>>(reader: &mut R, header: Header) -> Result<Self> { |
| 16 | if header.length.is_zero() { |
| 17 | Ok(Null) |
| 18 | } else { |
| 19 | Err(reader.error(ErrorKind::Length { tag: Self::TAG })) |
| 20 | } |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | impl EncodeValue for Null { |
| 25 | fn value_len(&self) -> Result<Length> { |
| 26 | Ok(Length::ZERO) |
| 27 | } |
| 28 | |
| 29 | fn encode_value(&self, _writer: &mut impl Writer) -> Result<()> { |
| 30 | Ok(()) |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | impl FixedTag for Null { |
| 35 | const TAG: Tag = Tag::Null; |
| 36 | } |
| 37 | |
| 38 | impl OrdIsValueOrd for Null {} |
| 39 | |
| 40 | impl<'a> From<Null> for AnyRef<'a> { |
| 41 | fn from(_: Null) -> AnyRef<'a> { |
| 42 | AnyRef::from_tag_and_value(Tag::Null, BytesRef::default()) |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | impl TryFrom<AnyRef<'_>> for () { |
| 47 | type Error = Error; |
| 48 | |
| 49 | fn try_from(any: AnyRef<'_>) -> Result<()> { |
| 50 | Null::try_from(any).map(|_| ()) |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | impl<'a> From<()> for AnyRef<'a> { |
| 55 | fn from(_: ()) -> AnyRef<'a> { |
| 56 | Null.into() |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | impl<'a> DecodeValue<'a> for () { |
| 61 | fn decode_value<R: Reader<'a>>(reader: &mut R, header: Header) -> Result<Self> { |
| 62 | Null::decode_value(reader, header)?; |
| 63 | Ok(()) |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | impl EncodeValue for () { |
| 68 | fn value_len(&self) -> Result<Length> { |
| 69 | Ok(Length::ZERO) |
| 70 | } |
| 71 | |
| 72 | fn encode_value(&self, _writer: &mut impl Writer) -> Result<()> { |
| 73 | Ok(()) |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | impl FixedTag for () { |
| 78 | const TAG: Tag = Tag::Null; |
| 79 | } |
| 80 | |
| 81 | #[cfg(test)] |
| 82 | mod tests { |
| 83 | use super::Null; |
| 84 | use crate::{Decode, Encode}; |
| 85 | |
| 86 | #[test] |
| 87 | fn decode() { |
| 88 | Null::from_der(&[0x05, 0x00]).unwrap(); |
| 89 | } |
| 90 | |
| 91 | #[test] |
| 92 | fn encode() { |
| 93 | let mut buffer = [0u8; 2]; |
| 94 | assert_eq!(&[0x05, 0x00], Null.encode_to_slice(&mut buffer).unwrap()); |
| 95 | assert_eq!(&[0x05, 0x00], ().encode_to_slice(&mut buffer).unwrap()); |
| 96 | } |
| 97 | |
| 98 | #[test] |
| 99 | fn reject_non_canonical() { |
| 100 | assert!(Null::from_der(&[0x05, 0x81, 0x00]).is_err()); |
| 101 | } |
| 102 | } |