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
sourceimpl BerRef
impl BerRef
sourcepub fn from_bytes(bytes: &[u8]) -> Result<&Self, Error>
pub fn from_bytes(bytes: &[u8]) -> Result<&Self, Error>
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);sourcepub unsafe fn from_bytes_unchecked(bytes: &[u8]) -> &Self
pub unsafe fn from_bytes_unchecked(bytes: &[u8]) -> &Self
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);sourceimpl BerRef
impl BerRef
sourcepub fn length(&self) -> Length
pub fn length(&self) -> Length
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));sourcepub fn contents(&self) -> &ContentsRef
pub fn contents(&self) -> &ContentsRef
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);sourcepub fn as_bytes(&self) -> &[u8]ⓘNotable traits for &mut [u8]impl Write for &mut [u8]impl Read for &[u8]
pub fn as_bytes(&self) -> &[u8]ⓘNotable traits for &mut [u8]impl Write for &mut [u8]impl Read for &[u8]
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
sourceimpl ToOwned for BerRef
impl ToOwned for BerRef
sourceimpl<'a> TryFrom<&'a [u8]> for &'a BerRef
impl<'a> TryFrom<&'a [u8]> for &'a BerRef
sourcefn try_from(bytes: &'a [u8]) -> Result<Self, Self::Error>
fn try_from(bytes: &'a [u8]) -> Result<Self, Self::Error>
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.