Botan 3.13.0
Crypto and TLS for C&
Botan::ASN1_BitString Class Referencefinal

#include <asn1_obj.h>

Public Member Functions

 ASN1_BitString ()=default
 ASN1_BitString (std::span< const uint8_t > bytes, size_t unused_bits)
 ASN1_BitString (std::vector< uint8_t > bytes, size_t unused_bits)
bool bit_at (size_t bit) const
size_t bit_length () const
std::span< const uint8_t > bytes () const
size_t unused_bits () const

Detailed Description

ASN.1 BIT STRING with explicit unused-bit count.

Definition at line 174 of file asn1_obj.h.

Constructor & Destructor Documentation

◆ ASN1_BitString() [1/3]

Botan::ASN1_BitString::ASN1_BitString ( )
default

Create an empty BIT STRING

References ASN1_BitString(), bytes(), and unused_bits().

Referenced by ASN1_BitString(), and ASN1_BitString().

◆ ASN1_BitString() [2/3]

Botan::ASN1_BitString::ASN1_BitString ( std::vector< uint8_t > bytes,
size_t unused_bits )

Create a BIT STRING from the given bits

Parameters
bytesthe bits, without the leading unused-bit count octet
unused_bitsthe number of unused bits in the final byte

Throws Invalid_Argument if unused_bits is 8 or larger, if unused_bits is non-zero for an empty BIT STRING, or if any of the unused bits are set.

Definition at line 28 of file asn1_obj.cpp.

28 :
29 m_bytes(std::move(bytes)), m_unused_bits(unused_bits) {
30 if(m_unused_bits >= 8) {
31 throw Invalid_Argument("ASN1_BitString: Invalid unused bit count");
32 }
33
34 if(m_bytes.empty() && m_unused_bits != 0) {
35 throw Invalid_Argument("ASN1_BitString: Empty BIT STRING cannot have unused bits");
36 }
37
38 if(m_unused_bits > 0 && (m_bytes.back() & ((1U << m_unused_bits) - 1)) != 0) {
39 throw Invalid_Argument("ASN1_BitString: Unused bits must be zero");
40 }
41}
size_t unused_bits() const
Definition asn1_obj.h:211
std::span< const uint8_t > bytes() const
Definition asn1_obj.h:206

References bytes(), and unused_bits().

◆ ASN1_BitString() [3/3]

Botan::ASN1_BitString::ASN1_BitString ( std::span< const uint8_t > bytes,
size_t unused_bits )

Create a BIT STRING from the given bits

Parameters
bytesthe bits, without the leading unused-bit count octet
unused_bitsthe number of unused bits in the final byte

Throws Invalid_Argument if unused_bits is 8 or larger, if unused_bits is non-zero for an empty BIT STRING, or if any of the unused bits are set.

Definition at line 43 of file asn1_obj.cpp.

43 :
44 ASN1_BitString(std::vector<uint8_t>(bytes.begin(), bytes.end()), unused_bits) {}

References ASN1_BitString(), bytes(), and unused_bits().

Member Function Documentation

◆ bit_at()

bool Botan::ASN1_BitString::bit_at ( size_t bit) const

Return the value of a single bit, counting from the most significant bit of the first byte

Throws Invalid_Argument if bit is not less than bit_length()

Definition at line 50 of file asn1_obj.cpp.

50 {
51 if(bit >= bit_length()) {
52 throw Invalid_Argument("ASN1_BitString: Bit index out of range");
53 }
54
55 return (m_bytes[bit / 8] & (0x80 >> (bit % 8))) != 0;
56}
size_t bit_length() const
Definition asn1_obj.cpp:46

References bit_length().

Referenced by Botan::BER_Decoder::decode_named_bitstring().

◆ bit_length()

size_t Botan::ASN1_BitString::bit_length ( ) const

Return the number of significant bits in this BIT STRING

Definition at line 46 of file asn1_obj.cpp.

46 {
47 return 8 * m_bytes.size() - m_unused_bits;
48}

Referenced by bit_at(), and Botan::BER_Decoder::decode_named_bitstring().

◆ bytes()

std::span< const uint8_t > Botan::ASN1_BitString::bytes ( ) const
inline

Return the bits, without the leading unused-bit count octet

Definition at line 206 of file asn1_obj.h.

206{ return std::span{m_bytes}; }

Referenced by ASN1_BitString(), ASN1_BitString(), ASN1_BitString(), Botan::BER_Decoder::decode_octet_aligned_bitstring(), and Botan::DER_Encoder::encode_bitstring().

◆ unused_bits()

size_t Botan::ASN1_BitString::unused_bits ( ) const
inline

Return the number of unused bits in the final byte

Definition at line 211 of file asn1_obj.h.

211{ return m_unused_bits; }

Referenced by ASN1_BitString(), ASN1_BitString(), ASN1_BitString(), Botan::BER_Decoder::decode_octet_aligned_bitstring(), and Botan::DER_Encoder::encode_bitstring().


The documentation for this class was generated from the following files: