/rust/registry/src/index.crates.io-6f17d22bba15001f/der-0.7.10/src/asn1/boolean.rs
Line | Count | Source (jump to first uncovered line) |
1 | | //! ASN.1 `BOOLEAN` support. |
2 | | |
3 | | use crate::{ |
4 | | asn1::AnyRef, ord::OrdIsValueOrd, DecodeValue, EncodeValue, Error, ErrorKind, FixedTag, Header, |
5 | | Length, Reader, Result, Tag, Writer, |
6 | | }; |
7 | | |
8 | | /// Byte used to encode `true` in ASN.1 DER. From X.690 Section 11.1: |
9 | | /// |
10 | | /// > If the encoding represents the boolean value TRUE, its single contents |
11 | | /// > octet shall have all eight bits set to one. |
12 | | const TRUE_OCTET: u8 = 0b11111111; |
13 | | |
14 | | /// Byte used to encode `false` in ASN.1 DER. |
15 | | const FALSE_OCTET: u8 = 0b00000000; |
16 | | |
17 | | impl<'a> DecodeValue<'a> for bool { |
18 | 0 | fn decode_value<R: Reader<'a>>(reader: &mut R, header: Header) -> Result<Self> { |
19 | 0 | if header.length != Length::ONE { |
20 | 0 | return Err(reader.error(ErrorKind::Length { tag: Self::TAG })); |
21 | 0 | } |
22 | 0 |
|
23 | 0 | match reader.read_byte()? { |
24 | 0 | FALSE_OCTET => Ok(false), |
25 | 0 | TRUE_OCTET => Ok(true), |
26 | 0 | _ => Err(Self::TAG.non_canonical_error()), |
27 | | } |
28 | 0 | } |
29 | | } |
30 | | |
31 | | impl EncodeValue for bool { |
32 | 0 | fn value_len(&self) -> Result<Length> { |
33 | 0 | Ok(Length::ONE) |
34 | 0 | } |
35 | | |
36 | 0 | fn encode_value(&self, writer: &mut impl Writer) -> Result<()> { |
37 | 0 | writer.write_byte(if *self { TRUE_OCTET } else { FALSE_OCTET }) |
38 | 0 | } |
39 | | } |
40 | | |
41 | | impl FixedTag for bool { |
42 | | const TAG: Tag = Tag::Boolean; |
43 | | } |
44 | | |
45 | | impl OrdIsValueOrd for bool {} |
46 | | |
47 | | impl TryFrom<AnyRef<'_>> for bool { |
48 | | type Error = Error; |
49 | | |
50 | 0 | fn try_from(any: AnyRef<'_>) -> Result<bool> { |
51 | 0 | any.try_into() |
52 | 0 | } |
53 | | } |
54 | | |
55 | | #[cfg(test)] |
56 | | mod tests { |
57 | | use crate::{Decode, Encode}; |
58 | | |
59 | | #[test] |
60 | | fn decode() { |
61 | | assert_eq!(true, bool::from_der(&[0x01, 0x01, 0xFF]).unwrap()); |
62 | | assert_eq!(false, bool::from_der(&[0x01, 0x01, 0x00]).unwrap()); |
63 | | } |
64 | | |
65 | | #[test] |
66 | | fn encode() { |
67 | | let mut buffer = [0u8; 3]; |
68 | | assert_eq!( |
69 | | &[0x01, 0x01, 0xFF], |
70 | | true.encode_to_slice(&mut buffer).unwrap() |
71 | | ); |
72 | | assert_eq!( |
73 | | &[0x01, 0x01, 0x00], |
74 | | false.encode_to_slice(&mut buffer).unwrap() |
75 | | ); |
76 | | } |
77 | | |
78 | | #[test] |
79 | | fn reject_non_canonical() { |
80 | | assert!(bool::from_der(&[0x01, 0x01, 0x01]).is_err()); |
81 | | } |
82 | | } |