pub enum Length {
Indefinite,
Definite(usize),
}
Expand description
Length
represents ASN.1 length.
Note that Length
represents the count of the contents octets in ASN.1.
The total byte size of BER, DER, and CER is greater than that.
(BER, DER, and CER are composed of identifier, length, and contents.)
§Caution
Length
implements PartialEq
and PartialOrd
, but Eq
nor Ord
,
because ‘Indefinite length’ is not comparable.
Variants§
Indefinite
Represents ‘Indefinite’ length.
‘Indefinite’ is only for ‘BER’, and the contents must end with ‘EOC’ octets.
Definite(usize)
‘Definite’ is for ‘BER’, ‘DER’, and ‘CER’, and represents the byte count of the contents.
Implementations§
Source§impl Length
impl Length
Sourcepub fn parse<R>(readable: &mut R) -> Result<Self, Error>
pub fn parse<R>(readable: &mut R) -> Result<Self, Error>
Parses readable
starting with length octets and tries to creates a new instance.
This function ignores extra octet(s) at the end of readable
if any.
§Examples
use bsn1::Length;
// Serializes Definite(5).
let length = Length::Definite(5);
let mut serialized = Vec::from(&length.to_bytes()[..]);
// Parses the serialized bytes.
let deserialized = Length::parse(&mut &serialized[..]).unwrap();
assert_eq!(length, deserialized);
// The extra octets at the end does not affect the result.
serialized.push(0x03);
serialized.push(0x04);
let deserialized = Length::parse(&mut &serialized[..]).unwrap();
assert_eq!(length, deserialized);
Sourcepub fn to_bytes(self) -> impl Deref<Target = [u8]>
pub fn to_bytes(self) -> impl Deref<Target = [u8]>
Serializes self
.
§Examples
use bsn1::Length;
// Serializes Definite(3), and deserializes it.
// The result is Definite(3).
let bytes = Length::Definite(3).to_bytes();
let deserialized = Length::parse(&mut &*bytes).unwrap();
assert_eq!(Length::Definite(3), deserialized);
Sourcepub const fn len(self) -> usize
pub const fn len(self) -> usize
Returns the byte count of the octets that self
is serialized.
§Examples
use bsn1::Length;
// The length of INDEFINITE is always 1.
assert_eq!(Length::Indefinite.len(), 1);
// The length is 1 if the value is less than or equals to 127.
assert_eq!(Length::Definite(0).len(), 1);
assert_eq!(Length::Definite(127).len(), 1);
// The length is 2 if the value is 128.
assert_eq!(Length::Definite(128).len(), 2);
Sourcepub const fn definite(self) -> Option<usize>
pub const fn definite(self) -> Option<usize>
Converts self
to Option<usize>
.
Returns None
if self is Indefinite
; otherwise, wraps the inner number in Some
and returns it.
§Examples
use bsn1::Length;
let length = Length::Indefinite;
assert_eq!(length.definite(), None);
let length = Length::Definite(45);
assert_eq!(length.definite(), Some(45));
Sourcepub const fn is_indefinite(self) -> bool
pub const fn is_indefinite(self) -> bool
Returns true
if self
is Indefinite
; otherwise false
.
§Examples
use bsn1::Length;
let length = Length::Indefinite;
assert_eq!(length.is_indefinite(), true);
let length = Length::Definite(12);
assert_eq!(length.is_indefinite(), false);
Sourcepub const fn is_definite(self) -> bool
pub const fn is_definite(self) -> bool
Returns false
if self
is Indefinite
; otherwise true
.
§Examples
use bsn1::Length;
let length = Length::Indefinite;
assert_eq!(length.is_definite(), false);
let length = Length::Definite(12);
assert_eq!(length.is_definite(), true);