pub struct BerRef { /* private fields */ }
Expand description

BerRef is a wrapper of [u8] and represents a BER.

This struct is ‘Unsized’, and user usually uses a reference to the instance.

Implementations

Parses bytes starting with octets of ‘ASN.1 BER’ and returns a reference to BerRef .

This function ignores extra octet(s) at the end of bytes if any.

This function is same to <&BerRef>::try_from .

Warnings

ASN.1 reserves some universal identifier numbers and they should not be used, however, this function ignores that. For example, number 15 (0x0f) is reserved for now, but this functions returns Ok.

Examples
use bsn1::BerRef;

// Represents 'True' as Boolean.
let bytes: &[u8] = &[0x01, 0x01, 0xff];
let ber0 = BerRef::from_bytes(bytes).unwrap();
assert!(ber0.contents().to_bool_ber().unwrap());

// The extra octets at the end does not affect to the result.
let bytes: &[u8] = &[0x01, 0x01, 0xff, 0x00];
let ber1 = BerRef::from_bytes(bytes).unwrap();
assert_eq!(ber0, ber1);

Provides a reference from bytes without any check.

bytes must be BER octets and must not include any extra octet.

If it is not sure whether bytes are valid octets as an ‘BER’ or not, use TryFrom implementation or from_bytes.

Safety

The behavior is undefined if bytes is not formatted as a BER.

Examples
use bsn1::{BerRef, IdRef};

// Represents '0x34' as an Integer.
let bytes: &[u8] = &[0x02, 0x01, 0x34];
let ber = unsafe { BerRef::from_bytes_unchecked(bytes) };

assert_eq!(ber.id(), IdRef::integer());
assert_eq!(ber.contents().to_integer::<i32>().unwrap(), 0x34);

Provides a reference to IdRef of self .

Examples
use bsn1::{BerRef, IdRef};

// Represents '3' as an Integer.
let bytes: &[u8] = &[0x02, 0x01, 0x03];
let ber = BerRef::from_bytes(bytes).unwrap();

assert_eq!(ber.id(), IdRef::integer());

Returns Length of self.

Warnings

Length stands for ‘the length octets of the contents’ in BER. The total byte count is greater than the value.

Examples
use bsn1::{BerRef, Length};

// Represents 'False' as a Boolean.
let bytes: &[u8] = &[0x01, 0x01, 0x00];
let ber = BerRef::from_bytes(bytes).unwrap();

assert_eq!(ber.length(), Length::Definite(1));

Provides a reference to the ‘contents’ octets of self .

Examples
use bsn1::BerRef;

// Represents 'False' as a Boolean.
let bytes: &[u8] = &[0x01, 0x01, 0x00];
let ber = BerRef::from_bytes(bytes).unwrap();

assert_eq!(ber.contents().to_bool_ber().unwrap(), false);

Provides a reference to the inner slice.

Examples
use bsn1::BerRef;

// This octets represents '3' as an integer.
let bytes = vec![0x02, 0x01, 0x03];

let ber = unsafe { BerRef::from_bytes_unchecked(&bytes) };
assert_eq!(&bytes, ber.as_bytes());

Trait Implementations

Converts this type into a shared reference of the (usually inferred) input type.
Immutably borrows from an owned value. Read more
Immutably borrows from an owned value. Read more
Formats the value using the given formatter. Read more
Converts to this type from the input type.
Converts to this type from the input type.
Feeds this value into the given Hasher. Read more
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more
The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more

Parses bytes starting with octets of ‘ASN.1 BER’ and returns a reference to BerRef.

This function ignores extra octet(s) at the end of bytes if any.

This function is same to BerRef::from_bytes.

Warnings

ASN.1 reserves some universal identifier numbers and they should not be used, however, this function ignores that. For example, number 15 (0x0f) is reserved for now, but this functions returns Ok.

The type returned in the event of a conversion error.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more