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(_MSC_VER) && (_MSC_VER <= 1400) 59 static const size_type
ELEMS_MAX = (~(size_type)0)/
sizeof(T);
60 #elif defined(CRYPTOPP_CXX11_ENUM) 74 #if defined(CRYPTOPP_CXX11_VARIADIC_TEMPLATES) || defined(CRYPTOPP_DOXYGEN_PROCESSING) 83 template<
typename V,
typename... Args>
84 void construct(V* ptr, Args&&... args) {::new ((
void*)ptr) V(std::forward<Args>(args)...);}
91 void destroy(V* ptr) {
if (ptr) ptr->~V();}
112 static void CheckSize(
size_t size)
115 CRYPTOPP_UNUSED(size);
118 throw InvalidArgument(
"AllocatorBase: requested size would cause integer overflow");
122 #define CRYPTOPP_INHERIT_ALLOCATOR_TYPES \ 123 typedef typename AllocatorBase<T>::value_type value_type;\ 124 typedef typename AllocatorBase<T>::size_type size_type;\ 125 typedef typename AllocatorBase<T>::difference_type difference_type;\ 126 typedef typename AllocatorBase<T>::pointer pointer;\ 127 typedef typename AllocatorBase<T>::const_pointer const_pointer;\ 128 typedef typename AllocatorBase<T>::reference reference;\ 129 typedef typename AllocatorBase<T>::const_reference const_reference; 141 template <
class T,
class A>
142 typename A::pointer
StandardReallocate(A& alloc, T *oldPtr,
typename A::size_type oldSize,
typename A::size_type newSize,
bool preserve)
145 if (oldSize == newSize)
150 typename A::pointer newPointer = alloc.allocate(newSize, NULLPTR);
151 const size_t copySize =
STDMIN(oldSize, newSize) *
sizeof(T);
153 if (oldPtr && newPointer) {
memcpy_s(newPointer, copySize, oldPtr, copySize);}
154 alloc.deallocate(oldPtr, oldSize);
159 alloc.deallocate(oldPtr, oldSize);
160 return alloc.allocate(newSize, NULLPTR);
172 template <
class T,
bool T_Align16 = false>
176 CRYPTOPP_INHERIT_ALLOCATOR_TYPES
193 pointer
allocate(size_type size,
const void *ptr = NULLPTR)
196 this->CheckSize(size);
200 #if CRYPTOPP_BOOL_ALIGN16 202 if (T_Align16 && size)
224 #if CRYPTOPP_BOOL_ALIGN16 225 if (T_Align16 && size)
245 pointer
reallocate(T *oldPtr, size_type oldSize, size_type newSize,
bool preserve)
269 #if defined(CRYPTOPP_WORD128_AVAILABLE) 272 #if CRYPTOPP_BOOL_X86 288 CRYPTOPP_INHERIT_ALLOCATOR_TYPES
293 pointer allocate(size_type n,
const void* unused = NULLPTR)
295 CRYPTOPP_UNUSED(n); CRYPTOPP_UNUSED(unused);
299 void deallocate(
void *p, size_type n)
301 CRYPTOPP_UNUSED(p); CRYPTOPP_UNUSED(n);
305 CRYPTOPP_CONSTEXPR size_type max_size()
const {
return 0;}
321 template <
class T,
size_t S,
class A = NullAllocator<T>,
bool T_Align16 = false>
341 template <
class T,
size_t S,
class A>
345 CRYPTOPP_INHERIT_ALLOCATOR_TYPES
366 if (size <= S && !m_allocated)
369 return GetAlignedArray();
372 return m_fallbackAllocator.allocate(size);
390 if (size <= S && !m_allocated)
393 return GetAlignedArray();
396 return m_fallbackAllocator.allocate(size, hint);
409 if (ptr == GetAlignedArray())
421 m_fallbackAllocator.deallocate(ptr, size);
442 pointer
reallocate(pointer oldPtr, size_type oldSize, size_type newSize,
bool preserve)
444 if (oldPtr == GetAlignedArray() && newSize <= S)
447 if (oldSize > newSize)
452 pointer newPointer = allocate(newSize, NULLPTR);
453 if (preserve && newSize)
455 const size_t copySize =
STDMIN(oldSize, newSize);
456 memcpy_s(newPointer,
sizeof(T)*newSize, oldPtr,
sizeof(T)*copySize);
458 deallocate(oldPtr, oldSize);
462 CRYPTOPP_CONSTEXPR size_type
max_size()
const 464 return STDMAX(m_fallbackAllocator.max_size(), S);
469 #if defined(CRYPTOPP_BOOL_ALIGN16) && (defined(_M_X64) || defined(__x86_64__)) 474 T* GetAlignedArray() {
478 CRYPTOPP_ALIGN_DATA(16) T m_array[S];
480 #elif defined(CRYPTOPP_BOOL_ALIGN16) 499 T* GetAlignedArray() {
500 T* p_array = (T*)(
void*)(((byte*)m_array) + (0-(size_t)m_array)%16);
510 enum { Q =
sizeof(T), PAD = (Q >= 16) ? 1 : (Q >= 8) ? 2 : (Q >= 4) ? 4 : (Q >= 2) ? 8 : 16 };
511 CRYPTOPP_ALIGN_DATA(8) T m_array[S+PAD];
514 enum { Q =
sizeof(T), PAD = (Q >= 8) ? 1 : (Q >= 4) ? 2 : (Q >= 2) ? 4 : 8 };
515 CRYPTOPP_ALIGN_DATA(8) T m_array[S+PAD];
521 T* GetAlignedArray() {
return m_array;}
522 CRYPTOPP_ALIGN_DATA(4) T m_array[S];
526 A m_fallbackAllocator;
540 template <
class T,
size_t S,
class A>
544 CRYPTOPP_INHERIT_ALLOCATOR_TYPES
565 if (size <= S && !m_allocated)
568 return GetAlignedArray();
571 return m_fallbackAllocator.allocate(size);
589 if (size <= S && !m_allocated)
592 return GetAlignedArray();
595 return m_fallbackAllocator.allocate(size, hint);
608 if (ptr == GetAlignedArray())
618 m_fallbackAllocator.deallocate(ptr, size);
639 pointer
reallocate(pointer oldPtr, size_type oldSize, size_type newSize,
bool preserve)
641 if (oldPtr == GetAlignedArray() && newSize <= S)
644 if (oldSize > newSize)
649 pointer newPointer = allocate(newSize, NULLPTR);
650 if (preserve && newSize)
652 const size_t copySize =
STDMIN(oldSize, newSize);
653 memcpy_s(newPointer,
sizeof(T)*newSize, oldPtr,
sizeof(T)*copySize);
655 deallocate(oldPtr, oldSize);
659 CRYPTOPP_CONSTEXPR size_type
max_size()
const 661 return STDMAX(m_fallbackAllocator.max_size(), S);
671 T* GetAlignedArray() {
return m_array;}
672 CRYPTOPP_ALIGN_DATA(8) T m_array[S];
674 A m_fallbackAllocator;
685 typedef typename A::value_type value_type;
686 typedef typename A::pointer iterator;
687 typedef typename A::const_pointer const_iterator;
688 typedef typename A::size_type size_type;
702 #if defined(CRYPTOPP_DOXYGEN_PROCESSING) 704 #elif defined(_MSC_VER) && (_MSC_VER <= 1400) 705 static const size_type
ELEMS_MAX = (~(size_type)0)/
sizeof(T);
706 #elif defined(CRYPTOPP_CXX11_ENUM) 707 enum : size_type {
ELEMS_MAX = A::ELEMS_MAX};
718 : m_mark(
ELEMS_MAX), m_size(size), m_ptr(m_alloc.allocate(size, NULLPTR)) { }
724 : m_mark(t.m_mark), m_size(t.m_size), m_ptr(m_alloc.allocate(t.m_size, NULLPTR)) {
726 if (t.m_ptr) {
memcpy_s(m_ptr, m_size*
sizeof(T), t.m_ptr, t.m_size*
sizeof(T));}
738 : m_mark(
ELEMS_MAX), m_size(len), m_ptr(m_alloc.allocate(len, NULLPTR)) {
741 memcpy_s(m_ptr, m_size*
sizeof(T), ptr, len*
sizeof(T));
743 memset(m_ptr, 0, m_size*
sizeof(T));
747 {m_alloc.deallocate(m_ptr,
STDMIN(m_size, m_mark));}
753 operator const void *()
const 758 operator const T *()
const 775 {
return m_ptr+m_size;}
778 const_iterator
end()
const 779 {
return m_ptr+m_size;}
783 typename A::pointer
data() {
return m_ptr;}
786 typename A::const_pointer
data()
const {
return m_ptr;}
791 size_type
size()
const {
return m_size;}
794 bool empty()
const {
return m_size == 0;}
801 const byte *
BytePtr()
const {
return (
const byte *)m_ptr;}
839 {
memcpy_s(m_ptr, m_size*
sizeof(T), ptr, len*
sizeof(T));}
851 for (
size_t i=0; i<count; ++i)
868 if (m_ptr && t.m_ptr)
869 {
memcpy_s(m_ptr, m_size*
sizeof(T), t, t.m_size*
sizeof(T));}
895 const size_type oldSize = m_size;
898 Grow(m_size+t.m_size);
899 memcpy_s(m_ptr+oldSize, (m_size-oldSize)*
sizeof(T), t.m_ptr, t.m_size*
sizeof(T));
904 memcpy_s(m_ptr+oldSize, (m_size-oldSize)*
sizeof(T), m_ptr, oldSize*
sizeof(T));
919 if(!t.m_size)
return SecBlock(*
this);
922 if (m_size) {
memcpy_s(result.m_ptr, result.m_size*
sizeof(T), m_ptr, m_size*
sizeof(T));}
923 memcpy_s(result.m_ptr+m_size, (result.m_size-m_size)*
sizeof(T), t.m_ptr, t.m_size*
sizeof(T));
935 return m_size == t.m_size &&
936 VerifyBufsEqual(reinterpret_cast<const byte*>(m_ptr), reinterpret_cast<const byte*>(t.m_ptr), m_size*
sizeof(T));
959 void New(size_type newSize)
961 m_ptr = m_alloc.reallocate(m_ptr, m_size, newSize,
false);
977 if (m_ptr) {
memset_z(m_ptr, 0, m_size*
sizeof(T));}
991 if (newSize > m_size)
993 m_ptr = m_alloc.reallocate(m_ptr, m_size, newSize,
true);
1009 if (newSize > m_size)
1011 m_ptr = m_alloc.reallocate(m_ptr, m_size, newSize,
true);
1012 memset_z(m_ptr+m_size, 0, (newSize-m_size)*
sizeof(T));
1027 m_ptr = m_alloc.reallocate(m_ptr, m_size, newSize,
true);
1038 std::swap(m_alloc, b.m_alloc);
1039 std::swap(m_mark, b.m_mark);
1040 std::swap(m_size, b.m_size);
1041 std::swap(m_ptr, b.m_ptr);
1046 size_type m_mark, m_size;
1050 #ifdef CRYPTOPP_DOXYGEN_PROCESSING 1070 template <
class T,
unsigned int S,
class A = FixedSizeAllocatorWithCleanup<T, S> >
1082 template <
class T,
unsigned int S,
bool T_Align16 = true>
1091 template <
class T,
unsigned int S,
class A = FixedSizeAllocatorWithCleanup<T, S, AllocatorWithCleanup<T> > >
1099 template<
class T,
bool A,
class V,
bool B>
1100 inline bool operator==(
const CryptoPP::AllocatorWithCleanup<T, A>&,
const CryptoPP::AllocatorWithCleanup<V, B>&) {
return (
true);}
1101 template<
class T,
bool A,
class V,
bool B>
1102 inline bool operator!=(
const CryptoPP::AllocatorWithCleanup<T, A>&,
const CryptoPP::AllocatorWithCleanup<V, B>&) {
return (
false);}
1106 NAMESPACE_BEGIN(
std)
1107 template <
class T,
class A>
1108 inline void swap(CryptoPP::SecBlock<T, A> &a, CryptoPP::SecBlock<T, A> &b)
1113 #if defined(_STLP_DONT_SUPPORT_REBIND_MEMBER_TEMPLATE) || (defined(_STLPORT_VERSION) && !defined(_STLP_MEMBER_TEMPLATE_CLASSES)) 1115 template <
class _Tp1,
class _Tp2>
1116 inline CryptoPP::AllocatorWithCleanup<_Tp2>&
1117 __stl_alloc_rebind(CryptoPP::AllocatorWithCleanup<_Tp1>& __a,
const _Tp2*)
1119 return (CryptoPP::AllocatorWithCleanup<_Tp2>&)(__a);
1125 #if CRYPTOPP_MSC_VERSION 1126 # 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.
pointer allocate(size_type size, const void *hint)
Allocates a block of memory.
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.
void New(size_type newSize)
Change size without preserving contents.
pointer allocate(size_type size, const void *hint)
Allocates a block of memory.
pointer reallocate(T *oldPtr, size_type oldSize, size_type newSize, bool preserve)
Reallocates a block of memory.
pointer reallocate(pointer 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.
pointer allocate(size_type size)
Allocates 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.
pointer allocate(size_type size)
Allocates a block of memory.
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.
FixedSizeAllocatorWithCleanup()
Constructs a FixedSizeAllocatorWithCleanup.
A::const_pointer data() const
Provides a pointer to the first element in the memory block.
Fixed size stack-based SecBlock with 16-byte alignment.
void deallocate(void *ptr, size_type size)
Deallocates a block of memory.
pointer reallocate(pointer oldPtr, size_type oldSize, size_type newSize, bool preserve)
Reallocates a block of memory.
FixedSizeAllocatorWithCleanup()
Constructs a FixedSizeAllocatorWithCleanup.
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.
const T & STDMAX(const T &a, const T &b)
Replacement function for std::max.
void deallocate(void *ptr, size_type size)
Deallocates a block of memory.
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.
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.