Struct bsn1::Ber

source · []
pub struct Ber { /* private fields */ }
Expand description

Ber owns BerRef and represents a BER.

The structure of Ber is similar to that of Vec<u8>.

User can access to the BerRef via the Deref implementation, and to the inner slice via the BerRef.

Implementations

Creates a new instance from id and contents with definite length.

Note that BER allows both definite and indefinite length, however, this function always returns definite length value. (Generally speaking, the performance of definite length is better than that of indefinite length. Indefinite length is seldom used these days.)

Examples
use bsn1::{Ber, ContentsRef, IdRef};

let id = IdRef::octet_string();
let contents = ContentsRef::from_bytes(&[]);
let ber = Ber::new(id, contents);

assert_eq!(ber.id(), id);
assert!(ber.contents().is_empty());

Parses bytes starting with BER octets and builds a new instance.

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

This function is same to TryFrom::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.

Builds a new instance holding bytes without any check.

bytes 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::{Ber, ContentsRef, IdRef};

let contents = ContentsRef::from_bytes(&[]);
let ber0 = Ber::new(IdRef::octet_string(), contents);
let ber1 = unsafe { Ber::from_bytes_unchecked(ber0.as_ref()) };
assert_eq!(ber0, ber1);

Creates a new instance from id and contents .

Examples
use bsn1::{Ber, ContentsRef, IdRef};

let id = IdRef::sequence();

// Build instance using function 'from_id_iterator()'.
let contents: &[Ber] = &[Ber::utf8_string("foo"), Ber::integer(29_i32)];
let ber = Ber::from_id_iterator(id, contents.iter());

// Build instance using function 'new()'.
let contents: Vec<u8> = contents.iter()
                        .map(|i| Vec::from(i.as_ref() as &[u8]))
                        .flatten().collect();
let contents = ContentsRef::from_bytes(&contents);
let expected = Ber::new(id, contents);

assert_eq!(expected, ber);

Returns a new instance representing a boolean.

Examples
use bsn1::{Ber, IdRef};

let val = true;
let ber = Ber::boolean(val);

assert_eq!(IdRef::boolean(), ber.id());
assert_eq!(val, ber.contents().to_bool_ber().unwrap());

Returns a new instance representing an integer.

Type T should be the builtin primitive integer types (e.g., u8, u32, isize, i128, …)

Examples
use bsn1::{Ber, IdRef};

let val = 39;
let ber = Ber::integer(val);

assert_eq!(IdRef::integer(), ber.id());
assert_eq!(val, ber.contents().to_integer().unwrap());

Returns a new instance representing a utf8_string.

Examples
use bsn1::{Ber, IdRef};

let val = &"foo";
let ber = Ber::utf8_string(val);

assert_eq!(IdRef::utf8_string(), ber.id());
assert_eq!(val.as_bytes(), ber.contents() as &[u8]);

Returns a new instance representing an octet_string.

Examples
use bsn1::{Ber, IdRef};

let val = &[1, 2, 3];
let ber = Ber::octet_string(val);

assert_eq!(IdRef::octet_string(), ber.id());
assert_eq!(val, ber.contents() as &[u8]);

Consumes self , returning Vec .

Examples
use bsn1::{Ber, ContentsRef, IdRef};

let id = IdRef::octet_string();
let contents = ContentsRef::from_bytes(&[0, 1, 2, 3, 4]);

let ber = Ber::new(id, contents);
let v = ber.clone().into_vec();

assert_eq!(ber.as_ref() as &[u8], v.as_ref() as &[u8]);

Methods from Deref<Target = BerRef>

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
Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
The resulting type after dereferencing.
Dereferences the value.
Converts to this type from the input type.
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
Feeds a slice of this type 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

Parses bytes starting with octets of ‘ASN.1 BER’ and returns a new instance.

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

This function is same to from_bytes .

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

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

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
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.