6 #ifndef CRYPTOPP_SECBLOCK_H 7 #define CRYPTOPP_SECBLOCK_H 13 #if CRYPTOPP_MSC_VERSION 14 # pragma warning(push) 15 # pragma warning(disable: 4231 4275 4700) 16 # if (CRYPTOPP_MSC_VERSION >= 1400) 17 # pragma warning(disable: 6011 6386 28193) 32 typedef size_t size_type;
33 typedef std::ptrdiff_t difference_type;
35 typedef const T * const_pointer;
36 typedef T & reference;
37 typedef const T & const_reference;
39 pointer address(reference r)
const {
return (&r);}
40 const_pointer address(const_reference r)
const {
return (&r); }
41 void construct(pointer p,
const T& val) {
new (p) T(val);}
42 void destroy(pointer p) {CRYPTOPP_UNUSED(p); p->~T();}
56 #if defined(CRYPTOPP_DOXYGEN_PROCESSING) 58 #elif defined(CRYPTOPP_CXX11_ENUM) 59 enum : size_type {ELEMS_MAX =
SIZE_MAX/
sizeof(T)};
61 static const size_type ELEMS_MAX =
SIZE_MAX/
sizeof(T);
72 #if defined(CRYPTOPP_CXX11_VARIADIC_TEMPLATES) || defined(CRYPTOPP_DOXYGEN_PROCESSING) 81 template<
typename V,
typename... Args>
82 void construct(V* ptr, Args&&... args) {::new ((
void*)ptr) V(std::forward<Args>(args)...);}
89 void destroy(V* ptr) {
if (ptr) ptr->~V();}
110 static void CheckSize(
size_t size)
113 CRYPTOPP_UNUSED(size);
115 if (
sizeof(T) != 1 && size > ELEMS_MAX)
116 throw InvalidArgument(
"AllocatorBase: requested size would cause integer overflow");
120 #define CRYPTOPP_INHERIT_ALLOCATOR_TYPES \ 121 typedef typename AllocatorBase<T>::value_type value_type;\ 122 typedef typename AllocatorBase<T>::size_type size_type;\ 123 typedef typename AllocatorBase<T>::difference_type difference_type;\ 124 typedef typename AllocatorBase<T>::pointer pointer;\ 125 typedef typename AllocatorBase<T>::const_pointer const_pointer;\ 126 typedef typename AllocatorBase<T>::reference reference;\ 127 typedef typename AllocatorBase<T>::const_reference const_reference; 139 template <
class T,
class A>
140 typename A::pointer
StandardReallocate(A& alloc, T *oldPtr,
typename A::size_type oldSize,
typename A::size_type newSize,
bool preserve)
143 if (oldSize == newSize)
148 typename A::pointer newPointer = alloc.allocate(newSize, NULLPTR);
149 const size_t copySize =
STDMIN(oldSize, newSize) *
sizeof(T);
151 if (oldPtr && newPointer) {
memcpy_s(newPointer, copySize, oldPtr, copySize);}
152 alloc.deallocate(oldPtr, oldSize);
157 alloc.deallocate(oldPtr, oldSize);
158 return alloc.allocate(newSize, NULLPTR);
170 template <
class T,
bool T_Align16 = false>
174 CRYPTOPP_INHERIT_ALLOCATOR_TYPES
191 pointer
allocate(size_type size,
const void *ptr = NULLPTR)
194 this->CheckSize(size);
198 #if CRYPTOPP_BOOL_ALIGN16 200 if (T_Align16 && size)
222 #if CRYPTOPP_BOOL_ALIGN16 223 if (T_Align16 && size)
243 pointer
reallocate(T *oldPtr, size_type oldSize, size_type newSize,
bool preserve)
267 #if defined(CRYPTOPP_WORD128_AVAILABLE) 270 #if CRYPTOPP_BOOL_X86 286 CRYPTOPP_INHERIT_ALLOCATOR_TYPES
291 pointer allocate(size_type n,
const void* unused = NULLPTR)
293 CRYPTOPP_UNUSED(n); CRYPTOPP_UNUSED(unused);
297 void deallocate(
void *p, size_type n)
299 CRYPTOPP_UNUSED(p); CRYPTOPP_UNUSED(n);
303 CRYPTOPP_CONSTEXPR size_type
max_size()
const {
return 0;}
318 template <
class T,
size_t S,
class A = NullAllocator<T>,
bool T_Align16 = false>
322 CRYPTOPP_INHERIT_ALLOCATOR_TYPES
343 if (size <= S && !m_allocated)
346 return GetAlignedArray();
349 return m_fallbackAllocator.allocate(size);
367 if (size <= S && !m_allocated)
370 return GetAlignedArray();
373 return m_fallbackAllocator.allocate(size, hint);
386 if (ptr == GetAlignedArray())
394 m_fallbackAllocator.deallocate(ptr, size);
414 pointer
reallocate(pointer oldPtr, size_type oldSize, size_type newSize,
bool preserve)
416 if (oldPtr == GetAlignedArray() && newSize <= S)
419 if (oldSize > newSize)
424 pointer newPointer = allocate(newSize, NULLPTR);
425 if (preserve && newSize)
427 const size_t copySize =
STDMIN(oldSize, newSize);
428 memcpy_s(newPointer,
sizeof(T)*newSize, oldPtr,
sizeof(T)*copySize);
430 deallocate(oldPtr, oldSize);
434 CRYPTOPP_CONSTEXPR size_type
max_size()
const {
return STDMAX(m_fallbackAllocator.max_size(), S);}
439 T* GetAlignedArray() {
return m_array;}
442 T* GetAlignedArray() {
return (CRYPTOPP_BOOL_ALIGN16 && T_Align16) ? (T*)(
void *)(((byte *)m_array) + (0-(size_t)m_array)%16) : m_array;}
443 CRYPTOPP_ALIGN_DATA(8) T m_array[(CRYPTOPP_BOOL_ALIGN16 && T_Align16) ? S+8/
sizeof(T) : S];
446 A m_fallbackAllocator;
453 template <
class T,
class A = AllocatorWithCleanup<T> >
457 typedef typename A::value_type value_type;
458 typedef typename A::pointer iterator;
459 typedef typename A::const_pointer const_iterator;
460 typedef typename A::size_type size_type;
474 #if defined(CRYPTOPP_DOXYGEN_PROCESSING) 476 #elif defined(CRYPTOPP_CXX11_ENUM) 477 enum : size_type {ELEMS_MAX = A::ELEMS_MAX};
479 static const size_type ELEMS_MAX =
SIZE_MAX/
sizeof(T);
488 : m_mark(ELEMS_MAX), m_size(size), m_ptr(m_alloc.allocate(size, NULLPTR)) { }
494 : m_mark(t.m_mark), m_size(t.m_size), m_ptr(m_alloc.allocate(t.m_size, NULLPTR)) {
496 if (t.m_ptr) {
memcpy_s(m_ptr, m_size*
sizeof(T), t.m_ptr, t.m_size*
sizeof(T));}
508 : m_mark(ELEMS_MAX), m_size(len), m_ptr(m_alloc.allocate(len, NULLPTR)) {
511 memcpy_s(m_ptr, m_size*
sizeof(T), ptr, len*
sizeof(T));
513 memset(m_ptr, 0, m_size*
sizeof(T));
517 {m_alloc.deallocate(m_ptr,
STDMIN(m_size, m_mark));}
523 operator const void *()
const 528 operator const T *()
const 545 {
return m_ptr+m_size;}
548 const_iterator
end()
const 549 {
return m_ptr+m_size;}
553 typename A::pointer
data() {
return m_ptr;}
556 typename A::const_pointer
data()
const {
return m_ptr;}
561 size_type
size()
const {
return m_size;}
564 bool empty()
const {
return m_size == 0;}
571 const byte *
BytePtr()
const {
return (
const byte *)m_ptr;}
609 {
memcpy_s(m_ptr, m_size*
sizeof(T), ptr, len*
sizeof(T));}
621 for (
size_t i=0; i<count; ++i)
638 if (m_ptr && t.m_ptr)
639 {
memcpy_s(m_ptr, m_size*
sizeof(T), t, t.m_size*
sizeof(T));}
665 const size_type oldSize = m_size;
668 Grow(m_size+t.m_size);
669 memcpy_s(m_ptr+oldSize, (m_size-oldSize)*
sizeof(T), t.m_ptr, t.m_size*
sizeof(T));
674 memcpy_s(m_ptr+oldSize, (m_size-oldSize)*
sizeof(T), m_ptr, oldSize*
sizeof(T));
689 if(!t.m_size)
return SecBlock(*
this);
692 if (m_size) {
memcpy_s(result.m_ptr, result.m_size*
sizeof(T), m_ptr, m_size*
sizeof(T));}
693 memcpy_s(result.m_ptr+m_size, (result.m_size-m_size)*
sizeof(T), t.m_ptr, t.m_size*
sizeof(T));
705 return m_size == t.m_size &&
706 VerifyBufsEqual(reinterpret_cast<const byte*>(m_ptr), reinterpret_cast<const byte*>(t.m_ptr), m_size*
sizeof(T));
729 void New(size_type newSize)
731 m_ptr = m_alloc.reallocate(m_ptr, m_size, newSize,
false);
747 if (m_ptr) {
memset_z(m_ptr, 0, m_size*
sizeof(T));}
761 if (newSize > m_size)
763 m_ptr = m_alloc.reallocate(m_ptr, m_size, newSize,
true);
779 if (newSize > m_size)
781 m_ptr = m_alloc.reallocate(m_ptr, m_size, newSize,
true);
782 memset_z(m_ptr+m_size, 0, (newSize-m_size)*
sizeof(T));
797 m_ptr = m_alloc.reallocate(m_ptr, m_size, newSize,
true);
808 std::swap(m_alloc, b.m_alloc);
809 std::swap(m_mark, b.m_mark);
810 std::swap(m_size, b.m_size);
811 std::swap(m_ptr, b.m_ptr);
816 size_type m_mark, m_size;
820 #ifdef CRYPTOPP_DOXYGEN_PROCESSING 840 template <
class T,
unsigned int S,
class A = FixedSizeAllocatorWithCleanup<T, S> >
852 template <
class T,
unsigned int S,
bool T_Align16 = true>
861 template <
class T,
unsigned int S,
class A = FixedSizeAllocatorWithCleanup<T, S, AllocatorWithCleanup<T> > >
869 template<
class T,
bool A,
class V,
bool B>
870 inline bool operator==(
const CryptoPP::AllocatorWithCleanup<T, A>&,
const CryptoPP::AllocatorWithCleanup<V, B>&) {
return (
true);}
871 template<
class T,
bool A,
class V,
bool B>
872 inline bool operator!=(
const CryptoPP::AllocatorWithCleanup<T, A>&,
const CryptoPP::AllocatorWithCleanup<V, B>&) {
return (
false);}
877 template <
class T,
class A>
878 inline void swap(CryptoPP::SecBlock<T, A> &a, CryptoPP::SecBlock<T, A> &b)
883 #if defined(_STLP_DONT_SUPPORT_REBIND_MEMBER_TEMPLATE) || (defined(_STLPORT_VERSION) && !defined(_STLP_MEMBER_TEMPLATE_CLASSES)) 885 template <
class _Tp1,
class _Tp2>
886 inline CryptoPP::AllocatorWithCleanup<_Tp2>&
887 __stl_alloc_rebind(CryptoPP::AllocatorWithCleanup<_Tp1>& __a,
const _Tp2*)
889 return (CryptoPP::AllocatorWithCleanup<_Tp2>&)(__a);
895 #if CRYPTOPP_MSC_VERSION 896 # pragma warning(pop) iterator end()
Provides an iterator pointing beyond the last element in the memory block.
An invalid argument was detected.
void construct(V *ptr, Args &&... args)
Constructs a new V using variadic arguments.
Base class for all allocators used by SecBlock.
void swap(SecBlock< T, A > &b)
Swap contents with another SecBlock.
Stack-based SecBlock that grows into the heap.
Utility functions for the Crypto++ library.
void AlignedDeallocate(void *ptr)
Frees a buffer allocated with AlignedAllocate.
FixedSizeSecBlock()
Construct a FixedSizeSecBlock.
void CleanNew(size_type newSize)
Change size without preserving contents.
SecBlock< T, A > & operator=(const SecBlock< T, A > &t)
Assign contents from another SecBlock.
size_type SizeInBytes() const
Provides the number of bytes in the SecBlock.
void resize(size_type newSize)
Change size and preserve contents.
SecBlock< T, A > & operator+=(const SecBlock< T, A > &t)
Append contents from another SecBlock.
static const size_type ELEMS_MAX
Returns the maximum number of elements the allocator can provide.
void CleanGrow(size_type newSize)
Change size and preserve contents.
const_iterator end() const
Provides a constant iterator pointing beyond the last element in the memory block.
void Assign(const SecBlock< T, A > &t)
Copy contents from another SecBlock.
SecBlock< T, A > operator+(const SecBlock< T, A > &t)
Construct a SecBlock from this and another SecBlock.
SecBlock(size_type size=0)
Construct a SecBlock with space for size elements.
Secure memory block with allocator and cleanup.
void memcpy_s(void *dest, size_t sizeInBytes, const void *src, size_t count)
Bounds checking replacement for memcpy()
Library configuration file.
bool operator!=(const SecBlock< T, A > &t) const
Bitwise compare two SecBlocks.
pointer allocate(size_type size, const void *hint)
Allocates a block of memory.
void New(size_type newSize)
Change size without preserving contents.
pointer reallocate(T *oldPtr, size_type oldSize, size_type newSize, bool preserve)
Reallocates a block of memory.
bool operator==(const OID &lhs, const OID &rhs)
Compare two OIDs for equality.
Static secure memory block with cleanup.
Allocates a block of memory with cleanup.
size_type max_size() const
Returns the maximum number of elements the allocator can provide.
void deallocate(void *ptr, size_type size)
Deallocates a block of memory.
bool operator!=(const OID &lhs, const OID &rhs)
Compare two OIDs for inequality.
void SecureWipeArray(T *buf, size_t n)
Sets each element of an array to 0.
bool IsAlignedOn(const void *ptr, unsigned int alignment)
Determines whether ptr is aligned to a minimum value.
void * UnalignedAllocate(size_t size)
Allocates a buffer.
Template class memeber Rebind.
A::pointer data()
Provides a pointer to the first element in the memory block.
void Assign(const T *ptr, size_type len)
Set contents and size from an array.
A::const_pointer data() const
Provides a pointer to the first element in the memory block.
pointer reallocate(pointer oldPtr, size_type oldSize, size_type newSize, bool preserve)
Reallocates a block of memory.
Fixed size stack-based SecBlock with 16-byte alignment.
SecBlock using AllocatorWithCleanup<byte, true> typedef.
pointer allocate(size_type size, const void *ptr=NULL)
Allocates a block of memory.
Fixed size stack-based SecBlock.
SecBlock(const SecBlock< T, A > &t)
Copy construct a SecBlock from another SecBlock.
void * memset_z(void *ptr, int value, size_t num)
Memory block initializer and eraser that attempts to survive optimizations.
const T & STDMIN(const T &a, const T &b)
Replacement function for std::min.
#define CRYPTOPP_ASSERT(exp)
Debugging and diagnostic assertion.
void deallocate(void *ptr, size_type size)
Deallocates a block of memory.
iterator begin()
Provides an iterator pointing to the first element in the memory block.
bool operator==(const SecBlock< T, A > &t) const
Bitwise compare two SecBlocks.
SecBlockWithHint(size_t size)
construct a SecBlockWithHint with a count of elements
const byte * BytePtr() const
Return a byte pointer to the first element in the memory block.
A::pointer StandardReallocate(A &alloc, T *oldPtr, typename A::size_type oldSize, typename A::size_type newSize, bool preserve)
Reallocation function.
bool VerifyBufsEqual(const byte *buf1, const byte *buf2, size_t count)
Performs a near constant-time comparison of two equally sized buffers.
const_iterator begin() const
Provides a constant iterator pointing to the first element in the memory block.
SecBlock(const T *ptr, size_type len)
Construct a SecBlock from an array of elements.
pointer allocate(size_type size)
Allocates a block of memory.
const T & STDMAX(const T &a, const T &b)
Replacement function for std::max.
void Grow(size_type newSize)
Change size and preserve contents.
Crypto++ library namespace.
void destroy(V *ptr)
Destroys an V constructed with variadic arguments.
void Assign(size_type count, T value)
Set contents from a value.
void SetMark(size_t count)
Sets the number of elements to zeroize.
FixedSizeAllocatorWithCleanup()
Constructs a FixedSizeAllocatorWithCleanup.
bool empty() const
Determines if the SecBlock is empty.
void UnalignedDeallocate(void *ptr)
Frees a buffer allocated with UnalignedAllocate.
size_type size() const
Provides the count of elements in the SecBlock.
void * AlignedAllocate(size_t size)
Allocates a buffer on 16-byte boundary.
#define SIZE_MAX
The maximum value of a machine word.
byte * BytePtr()
Provides a byte pointer to the first element in the memory block.