Botan 1.10.17
Botan::MemoryVector< T > Class Template Reference

#include <secmem.h>

Inheritance diagram for Botan::MemoryVector< T >:
Botan::MemoryRegion< T >

Public Member Functions

T * begin ()
const T * begin () const
void clear ()
void copy (const T in[], size_t n)
void copy (size_t off, const T in[], size_t n)
T * data ()
bool empty () const
T * end ()
const T * end () const
 MemoryVector (const MemoryRegion< T > &in)
 MemoryVector (const T in[], size_t n)
 MemoryVector (size_t n=0)
 operator const T * () const
 operator T* ()
bool operator!= (const MemoryRegion< T > &other) const
bool operator< (const MemoryRegion< T > &other) const
MemoryVector< T > & operator= (const MemoryRegion< T > &in)
bool operator== (const MemoryRegion< T > &other) const
void push_back (T x)
void resize (size_t n)
size_t size () const
void swap (MemoryRegion< T > &other)

Protected Member Functions

void init (bool locking, size_t length=0)

Detailed Description

template<typename T>
class Botan::MemoryVector< T >

This class represents variable length buffers that do not make use of memory locking.

Definition at line 273 of file secmem.h.

Constructor & Destructor Documentation

◆ MemoryVector() [1/3]

template<typename T>
Botan::MemoryVector< T >::MemoryVector ( size_t n = 0)
inline

Create a buffer of the specified length.

Parameters
nthe length of the buffer to create.

Definition at line 295 of file secmem.h.

295{ this->init(false, n); }
void init(bool locking, size_t length=0)
Definition secmem.h:195

References Botan::MemoryRegion< T >::init().

Referenced by operator=().

◆ MemoryVector() [2/3]

template<typename T>
Botan::MemoryVector< T >::MemoryVector ( const T in[],
size_t n )
inline

Create a buffer with the specified contents.

Parameters
inthe array containing the data to be initially copied into the newly created buffer
nthe size of the arry in

Definition at line 303 of file secmem.h.

304 {
305 this->init(false);
306 this->resize(n);
307 this->copy(in, n);
308 }
void resize(size_t n)
Definition secmem.h:217
void copy(const T in[], size_t n)
Definition secmem.h:126

References Botan::MemoryRegion< T >::copy(), Botan::MemoryRegion< T >::init(), and Botan::MemoryRegion< T >::resize().

◆ MemoryVector() [3/3]

template<typename T>
Botan::MemoryVector< T >::MemoryVector ( const MemoryRegion< T > & in)
inline

Copy constructor.

Definition at line 313 of file secmem.h.

314 {
315 this->init(false);
316 this->resize(in.size());
317 this->copy(&in[0], in.size());
318 }
size_t size() const
Definition secmem.h:29

References Botan::MemoryRegion< T >::copy(), Botan::MemoryRegion< T >::init(), Botan::MemoryRegion< T >::MemoryRegion(), Botan::MemoryRegion< T >::resize(), and Botan::MemoryRegion< T >::size().

Member Function Documentation

◆ begin() [1/2]

template<typename T>
T * Botan::MemoryRegion< T >::begin ( )
inlineinherited

Get a pointer to the first element in the buffer.

Returns
pointer to the first element in the buffer

Definition at line 59 of file secmem.h.

59{ return buf; }

Referenced by Botan::rfc3394_keyunwrap(), and Botan::rfc3394_keywrap().

◆ begin() [2/2]

template<typename T>
const T * Botan::MemoryRegion< T >::begin ( ) const
inlineinherited

Get a constant pointer to the first element in the buffer.

Returns
constant pointer to the first element in the buffer

Definition at line 65 of file secmem.h.

65{ return buf; }

◆ clear()

template<typename T>
void Botan::MemoryRegion< T >::clear ( )
inlineinherited

Reset this buffer to an empty buffer with size zero.

Definition at line 158 of file secmem.h.

158{ resize(0); }

References resize().

Referenced by Botan::BER_Decoder::decode_optional_string(), Botan::PGP_decode(), and Botan::BER_Decoder::raw_bytes().

◆ copy() [1/2]

template<typename T>
void Botan::MemoryRegion< T >::copy ( const T in[],
size_t n )
inlineinherited

Copy the contents of an array of objects of type T into this buffer. The former contents of *this are discarded. The length of *this must be at least n, otherwise memory errors occur.

Parameters
inthe array to copy the contents from
nthe length of in

Definition at line 126 of file secmem.h.

127 {
128 copy_mem(buf, in, std::min(n, size()));
129 }

References Botan::copy_mem(), and size().

Referenced by Botan::OpenPGP_S2K::derive_key(), MemoryRegion(), Botan::MemoryVector< T >::MemoryVector(), Botan::MemoryVector< T >::MemoryVector(), operator=(), Botan::MemoryVector< T >::operator=(), Botan::SecureVector< T >::operator=(), Botan::operator^(), Botan::SecureVector< T >::SecureVector(), and Botan::SecureVector< T >::SecureVector().

◆ copy() [2/2]

template<typename T>
void Botan::MemoryRegion< T >::copy ( size_t off,
const T in[],
size_t n )
inlineinherited

Copy the contents of an array of objects of type T into this buffer. The former contents of *this are discarded. The length of *this must be at least n, otherwise memory errors occur.

Parameters
offthe offset position inside this buffer to start inserting the copied bytes
inthe array to copy the contents from
nthe length of in

Definition at line 140 of file secmem.h.

141 {
142 copy_mem(buf + off, in, std::min(n, size() - off));
143 }

References Botan::copy_mem(), and size().

◆ data()

template<typename T>
T * Botan::MemoryRegion< T >::data ( )
inlineinherited

Get a pointer to the first element in the buffer.

Returns
pointer to the first element in the buffer

Definition at line 53 of file secmem.h.

53{ return buf; }

Referenced by Botan::BigInt::const_time_lookup(), and Botan::Montgomery_Exponentiator::execute().

◆ empty()

template<typename T>
bool Botan::MemoryRegion< T >::empty ( ) const
inlineinherited

Find out whether this buffer is empty.

Returns
true if the buffer is empty, false otherwise

Definition at line 35 of file secmem.h.

35{ return (used == 0); }

Referenced by Botan::BER_Decoder::decode(), Botan::BER_Decoder::decode(), and Botan::X509::load_key().

◆ end() [1/2]

template<typename T>
T * Botan::MemoryRegion< T >::end ( )
inlineinherited

Get a pointer to one past the last element in the buffer.

Returns
pointer to one past the last element in the buffer

Definition at line 71 of file secmem.h.

71{ return (buf + size()); }

References size().

◆ end() [2/2]

template<typename T>
const T * Botan::MemoryRegion< T >::end ( ) const
inlineinherited

Get a const pointer to one past the last element in the buffer.

Returns
const pointer to one past the last element in the buffer

Definition at line 77 of file secmem.h.

77{ return (buf + size()); }

References size().

◆ init()

template<typename T>
void Botan::MemoryRegion< T >::init ( bool locking,
size_t length = 0 )
inlineprotectedinherited
Parameters
lockingshould we use a locking allocator
lengththe initial length to use

Definition at line 195 of file secmem.h.

196 { alloc = Allocator::get(locking); resize(length); }
static Allocator * get(bool locking)
Definition defalloc.cpp:90

References Botan::Allocator::get(), and resize().

Referenced by Botan::MemoryVector< T >::MemoryVector(), Botan::MemoryVector< T >::MemoryVector(), Botan::MemoryVector< T >::MemoryVector(), Botan::SecureVector< T >::SecureVector(), Botan::SecureVector< T >::SecureVector(), and Botan::SecureVector< T >::SecureVector().

◆ operator const T *()

template<typename T>
Botan::MemoryRegion< T >::operator const T * ( ) const
inlineinherited

Get a constant pointer to the first element in the buffer.

Returns
constant pointer to the first element in the buffer

Definition at line 47 of file secmem.h.

47{ return buf; }

◆ operator T*()

template<typename T>
Botan::MemoryRegion< T >::operator T* ( )
inlineinherited

Get a pointer to the first element in the buffer.

Returns
pointer to the first element in the buffer

Definition at line 41 of file secmem.h.

41{ return buf; }

◆ operator!=()

template<typename T>
bool Botan::MemoryRegion< T >::operator!= ( const MemoryRegion< T > & other) const
inlineinherited

Check two buffers for inequality.

Returns
false if the content of both buffers is byte-wise equal, true otherwise.

Definition at line 100 of file secmem.h.

101 { return (!(*this == other)); }

References MemoryRegion().

◆ operator<()

template<typename T>
bool Botan::MemoryRegion< T >::operator< ( const MemoryRegion< T > & other) const
inherited

Compare two buffers

Returns
true iff this is ordered before other

Definition at line 239 of file secmem.h.

240 {
241 const size_t min_size = std::min(size(), other.size());
242
243 // This should probably be rewritten to run in constant time
244 for(size_t i = 0; i != min_size; ++i)
245 {
246 if(buf[i] < other[i])
247 return true;
248 if(buf[i] > other[i])
249 return false;
250 }
251
252 // First min_size bytes are equal, shorter is first
253 return (size() < other.size());
254 }

References MemoryRegion(), and size().

◆ operator=()

template<typename T>
MemoryVector< T > & Botan::MemoryVector< T >::operator= ( const MemoryRegion< T > & in)
inline

Copy the contents of another buffer into this buffer.

Parameters
inthe buffer to copy the contents from
Returns
reference to *this

Definition at line 281 of file secmem.h.

282 {
283 if(this != &in)
284 {
285 this->resize(in.size());
286 this->copy(&in[0], in.size());
287 }
288 return (*this);
289 }

References Botan::MemoryRegion< T >::copy(), Botan::MemoryRegion< T >::MemoryRegion(), MemoryVector(), Botan::MemoryRegion< T >::resize(), and Botan::MemoryRegion< T >::size().

◆ operator==()

template<typename T>
bool Botan::MemoryRegion< T >::operator== ( const MemoryRegion< T > & other) const
inlineinherited

Check two buffers for equality.

Returns
true iff the content of both buffers is byte-wise equal

Definition at line 83 of file secmem.h.

84 {
85 return (size() == other.size() &&
86 same_mem(buf, other.buf, size()));
87 }

References MemoryRegion(), Botan::same_mem(), and size().

◆ push_back()

template<typename T>
void Botan::MemoryRegion< T >::push_back ( T x)
inlineinherited

◆ resize()

template<typename T>
void Botan::MemoryRegion< T >::resize ( size_t n)
inherited

Inserts or erases elements at the end such that the size becomes n, leaving elements in the range 0...n unmodified if set or otherwise zero-initialized

Parameters
nlength of the new buffer

Definition at line 217 of file secmem.h.

218 {
219 if(n <= allocated)
220 {
221 size_t zap = std::min(used, n);
222 clear_mem(buf + zap, allocated - zap);
223 used = n;
224 }
225 else
226 {
227 T* new_buf = allocate(n);
228 copy_mem(new_buf, buf, used);
229 deallocate(buf, allocated);
230 buf = new_buf;
231 allocated = used = n;
232 }
233 }

References Botan::clear_mem(), and Botan::copy_mem().

Referenced by Botan::base64_decode(), Botan::OctetString::change(), clear(), Botan::BER_Decoder::decode(), Botan::divide(), Botan::Montgomery_Exponentiator::execute(), Botan::Entropy_Accumulator::get_io_buffer(), Botan::BER_Decoder::get_next_object(), Botan::Record_Reader::get_record(), Botan::hex_decode(), init(), MemoryRegion(), Botan::MemoryVector< T >::MemoryVector(), Botan::MemoryVector< T >::MemoryVector(), Botan::operator+=(), Botan::operator+=(), Botan::operator+=(), operator=(), Botan::MemoryVector< T >::operator=(), Botan::SecureVector< T >::operator=(), push_back(), Botan::Pipe::read_all(), Botan::SecureVector< T >::SecureVector(), and Botan::SecureVector< T >::SecureVector().

◆ size()

template<typename T>
size_t Botan::MemoryRegion< T >::size ( ) const
inlineinherited

Find out the size of the buffer, i.e. how many objects of type T it contains.

Returns
size of the buffer

Definition at line 29 of file secmem.h.

29{ return used; }

Referenced by Botan::Data_Store::add(), Botan::Randpool::add_entropy(), Botan::DER_Encoder::add_object(), Botan::Alert::Alert(), Botan::append_tls_length_value(), Botan::append_tls_length_value(), Botan::base64_encode(), Botan::BigInt::binary_decode(), Botan::check_passhash9(), Botan::PK_Verifier::check_signature(), Botan::PK_Verifier::check_signature(), Botan::MD4::compress_n(), Botan::MD5::compress_n(), Botan::BigInt::const_time_lookup(), copy(), copy(), Botan::BER::decode(), Botan::BER_Decoder::decode(), Botan::BER_Decoder::decode(), Botan::BER_Decoder::decode(), Botan::BigInt::decode(), Botan::BigInt::decode(), Botan::EME::decode(), Botan::decode_concatenation(), Botan::AlternativeName::decode_from(), Botan::EAC_Time::decode_from(), Botan::OID::decode_from(), Botan::BER_Decoder::decode_null(), Botan::BER_Decoder::decode_octet_string_bigint(), Botan::CryptoBox::decrypt(), Botan::PK_Decryptor::decrypt(), Botan::KDF2::derive(), Botan::X942_PRF::derive(), Botan::KDF::derive_key(), Botan::KDF::derive_key(), Botan::KDF::derive_key(), Botan::OpenPGP_S2K::derive_key(), Botan::PK_Key_Agreement::derive_key(), Botan::PK_Key_Agreement::derive_key(), Botan::PKCS5_PBKDF1::derive_key(), Botan::BigInt::encode(), Botan::BigInt::encode(), Botan::DER_Encoder::encode(), Botan::DER_Encoder::encode(), Botan::EME::encode(), Botan::PEM_Code::encode(), Botan::EMSA3::encoding_of(), Botan::CryptoBox::encrypt(), Botan::ElGamal_Encryption_Operation::encrypt(), Botan::PK_Encryptor::encrypt(), end(), end(), Botan::Hash_Filter::end_msg(), Botan::MAC_Filter::end_msg(), Botan::Montgomery_Exponentiator::execute(), Botan::Certificate_Store_Memory::find_cert_by_subject_and_key_id(), Botan::Certificate_Store_Memory::find_crl_by_subject_and_key_id(), Botan::generate_dsa_primes(), Botan::generate_passhash9(), Botan::Record_Reader::get_record(), Botan::hex_encode(), Botan::MGF1::mask(), Botan::PEM_Code::matches(), MemoryRegion(), Botan::MemoryVector< T >::MemoryVector(), Botan::BigInt::operator*=(), Botan::BigInt::operator+=(), Botan::operator+=(), Botan::operator+=(), Botan::operator+=(), operator<(), Botan::operator<<(), Botan::operator<<(), Botan::operator<<(), operator=(), Botan::MemoryVector< T >::operator=(), Botan::SecureVector< T >::operator=(), operator==(), Botan::operator>>(), Botan::operator>>(), Botan::OS2ECP(), Botan::OSSL_BN::OSSL_BN(), Botan::DataSource_Stream::peek(), Botan::Device_EntropySource::poll(), Botan::EGD_EntropySource::poll(), Botan::FTW_EntropySource::poll(), Botan::Unix_EntropySource::poll(), Botan::Win32_CAPI_EntropySource::poll(), Botan::CMAC::poly_double(), Botan::Buffered_Computation::process(), Botan::Pipe::process_msg(), push_back(), Botan::random_prime(), Botan::RandomNumberGenerator::random_vec(), Botan::BigInt::randomize(), Botan::DER_Encoder::raw_bytes(), Botan::Pipe::read_all(), Botan::Pipe::read_all_as_string(), Botan::RTSS_Share::reconstruct(), Botan::Randpool::reseed(), Botan::rfc3394_keyunwrap(), Botan::rfc3394_keywrap(), Botan::SecureVector< T >::SecureVector(), Botan::Filter::send(), Botan::HandshakeMessage::send(), Botan::Montgomery_Exponentiator::set_base(), Botan::EAX_Base::set_iv(), Botan::Turing::set_iv(), Botan::CMS_Encoder::sign(), Botan::DSA_Signature_Operation::sign(), Botan::ECDSA_Signature_Operation::sign(), Botan::NR_Signature_Operation::sign(), Botan::PK_Signer::sign_message(), Botan::PK_Signer::signature(), Botan::RTSS_Share::split(), Botan::BER_Decoder::start_cons(), Botan::ASN1::to_string(), Botan::Buffered_Computation::update(), Botan::PK_Signer::update(), Botan::PK_Verifier::update(), Botan::EMSA3::verify(), Botan::MessageAuthenticationCode::verify_mac(), Botan::PK_Verifier::verify_message(), Botan::Pipe::write(), Botan::Pipe::write(), and Botan::zeroise().

◆ swap()

template<typename T>
void Botan::MemoryRegion< T >::swap ( MemoryRegion< T > & other)
inherited

Swap this buffer with another object.

Definition at line 260 of file secmem.h.

261 {
262 std::swap(buf, x.buf);
263 std::swap(used, x.used);
264 std::swap(allocated, x.allocated);
265 std::swap(alloc, x.alloc);
266 }
void swap(Botan::MemoryRegion< T > &x, Botan::MemoryRegion< T > &y)
Definition secmem.h:438

References MemoryRegion(), and std::swap().

Referenced by std::swap().


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