Struct bcder::OctetString
source · [−]pub struct OctetString(_);
Expand description
An octet string value.
An octet string is a sequence of octets, i.e., a glorified [u8]
. Basic
Encoding Rules, however, allow this sequence to be broken up into chunks
that are encoded separatedly to allow for very large octet strings and
cases where one doesn’t yet know the length of the string.
In order to avoid unnecessary allocations, this type wraps the raw content
octets of a BER encoded octet string. As a consequence, assembling the
complete string may actually be costly and should only be done if really
necessary. As an alternative, there is an iterator over the parts via the
iter
method or the IntoIterator
trait as well as an iterator over the
individual octets via the octets
method.
Octet strings are sometimes used to store BER encoded data. The
OctetStringSource
type, accessible via the to_source
method, provides
an implementation of the Source
trait to run a decoder on.
BER Encoding
Octet strings are either encoded as a primitive or a constructed value. In the primitive form, the content octets are the string’s octets. In a constructed form, the content is a sequence of encoded octets strings which in turn may be primitive or constructed. In this case, the string’s octets are the concatenation of all the content octets of the primitive forms in the order as encountered.
In CER, the string must use the primitive form if it is less than 1000 octets long and the constructed form otherwise. The constructed form must consists of a sequence of primitive values each exactly with a 1000 octets of content except for the last one.
In DER, only the primitive form is allowed.
Implementations
sourceimpl OctetString
impl OctetString
sourcepub fn iter(&self) -> OctetStringIter<'_>ⓘNotable traits for OctetStringIter<'a>impl<'a> Iterator for OctetStringIter<'a> type Item = &'a [u8];
pub fn iter(&self) -> OctetStringIter<'_>ⓘNotable traits for OctetStringIter<'a>impl<'a> Iterator for OctetStringIter<'a> type Item = &'a [u8];
Returns an iterator over the parts of the octet string.
The iterator will produce &[u8]
which, when appended produce the
complete content of the octet string.
sourcepub fn octets(&self) -> OctetStringOctets<'_>ⓘNotable traits for OctetStringOctets<'a>impl<'a> Iterator for OctetStringOctets<'a> type Item = u8;
pub fn octets(&self) -> OctetStringOctets<'_>ⓘNotable traits for OctetStringOctets<'a>impl<'a> Iterator for OctetStringOctets<'a> type Item = u8;
Returns an iterator over the individual octets of the string.
sourcepub fn as_slice(&self) -> Option<&[u8]>
pub fn as_slice(&self) -> Option<&[u8]>
Returns a reference to the complete content if possible.
The method will return a bytes slice of the content if the octet
string was encoded as a single primitive value or None
otherwise.
This is guaranteed to return some slice if the value was produced by decoding in DER mode.
sourcepub fn to_bytes(&self) -> Bytes
pub fn to_bytes(&self) -> Bytes
Produces a bytes value with the string’s content.
If the octet string was encoded as a single primitive value, the method will simply clone the content. Otherwise it will produce an entirely new bytes value from the concatenated content of all the primitive values.
sourcepub fn into_bytes(self) -> Bytes
pub fn into_bytes(self) -> Bytes
Converts the octet string into bytes value.
If the octet string was encoded as a single primitive value, the method will simply return the content. Otherwise it will produce an entirely new bytes value from the concatenated content of all the primitive values.
sourceimpl OctetString
impl OctetString
sourcepub fn take_from<S: Source>(
cons: &mut Constructed<'_, S>
) -> Result<Self, DecodeError<S::Error>>
pub fn take_from<S: Source>(
cons: &mut Constructed<'_, S>
) -> Result<Self, DecodeError<S::Error>>
Takes a single octet string value from constructed value content.
If there is no next value, if the next value does not have the tag
Tag::OCTET_STRING
, or if it doesn’t contain a correctly encoded
octet string, a malformed error is returned.
sourcepub fn take_opt_from<S: Source>(
cons: &mut Constructed<'_, S>
) -> Result<Option<Self>, DecodeError<S::Error>>
pub fn take_opt_from<S: Source>(
cons: &mut Constructed<'_, S>
) -> Result<Option<Self>, DecodeError<S::Error>>
Takes an optional octet string value from constructed value content.
If there is no next value, or if the next value does not have the
tag Tag::OCTET_STRING
, then Ok(None)
is returned.
If there is an octet string, but it is not correctly encoded, a malformed error is returned.
sourcepub fn from_content<S: Source>(
content: &mut Content<'_, S>
) -> Result<Self, DecodeError<S::Error>>
pub fn from_content<S: Source>(
content: &mut Content<'_, S>
) -> Result<Self, DecodeError<S::Error>>
Takes an octet string value from content.
sourcepub fn encode(self) -> impl Values
pub fn encode(self) -> impl Values
Returns a value encoder for the octet string using the natural tag.
sourcepub fn encode_as(self, tag: Tag) -> impl Values
pub fn encode_as(self, tag: Tag) -> impl Values
Returns a value encoder for the octet string using the given tag.
sourcepub fn encode_ref(&self) -> impl Values + '_
pub fn encode_ref(&self) -> impl Values + '_
Returns a value encoder for the octet string using the natural tag.
sourcepub fn encode_ref_as(&self, tag: Tag) -> impl Values + '_
pub fn encode_ref_as(&self, tag: Tag) -> impl Values + '_
Returns a value encoder for the octet string using the given tag.
sourcepub fn encode_wrapped<V: Values>(mode: Mode, values: V) -> impl Values
pub fn encode_wrapped<V: Values>(mode: Mode, values: V) -> impl Values
Returns a value encoder that wraps values into an octet string.
This function allows an octet string wrapping some values to be created without having to first create the octet string.
sourcepub fn encode_slice<T>(value: T) -> OctetSliceEncoder<T>
pub fn encode_slice<T>(value: T) -> OctetSliceEncoder<T>
Returns a value encoder that encodes a bytes slice as an octet string.
sourcepub fn encode_slice_as<T>(value: T, tag: Tag) -> OctetSliceEncoder<T>
pub fn encode_slice_as<T>(value: T, tag: Tag) -> OctetSliceEncoder<T>
Returns a value encoder that encodes a bytes slice as an octet string.
Trait Implementations
sourceimpl AsRef<OctetString> for OctetString
impl AsRef<OctetString> for OctetString
sourceimpl<L: CharSet> AsRef<OctetString> for RestrictedString<L>
impl<L: CharSet> AsRef<OctetString> for RestrictedString<L>
sourcefn as_ref(&self) -> &OctetString
fn as_ref(&self) -> &OctetString
Converts this type into a shared reference of the (usually inferred) input type.
sourceimpl Clone for OctetString
impl Clone for OctetString
sourcefn clone(&self) -> OctetString
fn clone(&self) -> OctetString
Returns a copy of the value. Read more
1.0.0 · sourcefn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from source
. Read more
sourceimpl Debug for OctetString
impl Debug for OctetString
sourceimpl Hash for OctetString
impl Hash for OctetString
sourceimpl<'a> IntoIterator for &'a OctetString
impl<'a> IntoIterator for &'a OctetString
sourceimpl IntoSource for OctetString
impl IntoSource for OctetString
type Source = OctetStringSource
fn into_source(self) -> Self::Source
sourceimpl Ord for OctetString
impl Ord for OctetString
sourceimpl PartialEq<OctetString> for OctetString
impl PartialEq<OctetString> for OctetString
sourceimpl<T: AsRef<[u8]>> PartialEq<T> for OctetString
impl<T: AsRef<[u8]>> PartialEq<T> for OctetString
sourceimpl PartialOrd<OctetString> for OctetString
impl PartialOrd<OctetString> for OctetString
sourcefn partial_cmp(&self, other: &Self) -> Option<Ordering>
fn partial_cmp(&self, other: &Self) -> Option<Ordering>
This method returns an ordering between self
and other
values if one exists. Read more
1.0.0 · sourcefn lt(&self, other: &Rhs) -> bool
fn lt(&self, other: &Rhs) -> bool
This method tests less than (for self
and other
) and is used by the <
operator. Read more
1.0.0 · sourcefn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
sourceimpl<T: AsRef<[u8]>> PartialOrd<T> for OctetString
impl<T: AsRef<[u8]>> PartialOrd<T> for OctetString
sourcefn partial_cmp(&self, other: &T) -> Option<Ordering>
fn partial_cmp(&self, other: &T) -> Option<Ordering>
This method returns an ordering between self
and other
values if one exists. Read more
1.0.0 · sourcefn lt(&self, other: &Rhs) -> bool
fn lt(&self, other: &Rhs) -> bool
This method tests less than (for self
and other
) and is used by the <
operator. Read more
1.0.0 · sourcefn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
impl Eq for OctetString
Auto Trait Implementations
impl RefUnwindSafe for OctetString
impl Send for OctetString
impl Sync for OctetString
impl Unpin for OctetString
impl UnwindSafe for OctetString
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more