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(__SUNPRO_CC) 77 CRYPTOPP_CONSTEXPR size_type
max_size(size_type n)
const {
return (~(size_type)0)/n;}
80 #if defined(CRYPTOPP_CXX11_VARIADIC_TEMPLATES) || defined(CRYPTOPP_DOXYGEN_PROCESSING) 89 template<
typename V,
typename... Args>
90 void construct(V* ptr, Args&&... args) {::new ((
void*)ptr) V(std::forward<Args>(args)...);}
97 void destroy(V* ptr) {
if (ptr) ptr->~V();}
118 static void CheckSize(
size_t size)
121 CRYPTOPP_UNUSED(size);
124 throw InvalidArgument(
"AllocatorBase: requested size would cause integer overflow");
128 #define CRYPTOPP_INHERIT_ALLOCATOR_TYPES \ 129 typedef typename AllocatorBase<T>::value_type value_type;\ 130 typedef typename AllocatorBase<T>::size_type size_type;\ 131 typedef typename AllocatorBase<T>::difference_type difference_type;\ 132 typedef typename AllocatorBase<T>::pointer pointer;\ 133 typedef typename AllocatorBase<T>::const_pointer const_pointer;\ 134 typedef typename AllocatorBase<T>::reference reference;\ 135 typedef typename AllocatorBase<T>::const_reference const_reference; 147 template <
class T,
class A>
148 typename A::pointer
StandardReallocate(A& alloc, T *oldPtr,
typename A::size_type oldSize,
typename A::size_type newSize,
bool preserve)
151 if (oldSize == newSize)
156 typename A::pointer newPointer = alloc.allocate(newSize, NULLPTR);
157 const size_t copySize =
STDMIN(oldSize, newSize) *
sizeof(T);
159 if (oldPtr && newPointer) {
memcpy_s(newPointer, copySize, oldPtr, copySize);}
160 alloc.deallocate(oldPtr, oldSize);
165 alloc.deallocate(oldPtr, oldSize);
166 return alloc.allocate(newSize, NULLPTR);
178 template <
class T,
bool T_Align16 = false>
182 CRYPTOPP_INHERIT_ALLOCATOR_TYPES
199 pointer
allocate(size_type size,
const void *ptr = NULLPTR)
202 this->CheckSize(size);
206 #if CRYPTOPP_BOOL_ALIGN16 208 if (T_Align16 && size)
230 #if CRYPTOPP_BOOL_ALIGN16 231 if (T_Align16 && size)
251 pointer
reallocate(T *oldPtr, size_type oldSize, size_type newSize,
bool preserve)
275 #if defined(CRYPTOPP_WORD128_AVAILABLE) 278 #if CRYPTOPP_BOOL_X86 294 CRYPTOPP_INHERIT_ALLOCATOR_TYPES
299 pointer allocate(size_type n,
const void* unused = NULLPTR)
301 CRYPTOPP_UNUSED(n); CRYPTOPP_UNUSED(unused);
305 void deallocate(
void *p, size_type n)
307 CRYPTOPP_UNUSED(p); CRYPTOPP_UNUSED(n);
311 CRYPTOPP_CONSTEXPR size_type max_size()
const {
return 0;}
327 template <
class T,
size_t S,
class A = NullAllocator<T>,
bool T_Align16 = false>
347 template <
class T,
size_t S,
class A>
351 CRYPTOPP_INHERIT_ALLOCATOR_TYPES
372 if (size <= S && !m_allocated)
375 return GetAlignedArray();
378 return m_fallbackAllocator.allocate(size);
396 if (size <= S && !m_allocated)
399 return GetAlignedArray();
402 return m_fallbackAllocator.allocate(size, hint);
415 if (ptr == GetAlignedArray())
427 m_fallbackAllocator.deallocate(ptr, size);
448 pointer
reallocate(pointer oldPtr, size_type oldSize, size_type newSize,
bool preserve)
450 if (oldPtr == GetAlignedArray() && newSize <= S)
453 if (oldSize > newSize)
458 pointer newPointer = allocate(newSize, NULLPTR);
459 if (preserve && newSize)
461 const size_t copySize =
STDMIN(oldSize, newSize);
462 memcpy_s(newPointer,
sizeof(T)*newSize, oldPtr,
sizeof(T)*copySize);
464 deallocate(oldPtr, oldSize);
468 CRYPTOPP_CONSTEXPR size_type
max_size()
const 470 return STDMAX(m_fallbackAllocator.max_size(), S);
475 #if defined(CRYPTOPP_BOOL_ALIGN16) && (defined(_M_X64) || defined(__x86_64__)) 480 T* GetAlignedArray() {
484 CRYPTOPP_ALIGN_DATA(16) T m_array[S];
486 #elif defined(CRYPTOPP_BOOL_ALIGN16) 505 T* GetAlignedArray() {
506 T* p_array =
reinterpret_cast<T*
>(
static_cast<void*
>((
reinterpret_cast<byte*
>(m_array)) + (0-
reinterpret_cast<size_t>(m_array))%16));
516 enum { Q =
sizeof(T), PAD = (Q >= 16) ? 1 : (Q >= 8) ? 2 : (Q >= 4) ? 4 : (Q >= 2) ? 8 : 16 };
517 CRYPTOPP_ALIGN_DATA(8) T m_array[S+PAD];
520 enum { Q =
sizeof(T), PAD = (Q >= 8) ? 1 : (Q >= 4) ? 2 : (Q >= 2) ? 4 : 8 };
521 CRYPTOPP_ALIGN_DATA(8) T m_array[S+PAD];
527 T* GetAlignedArray() {
return m_array;}
528 CRYPTOPP_ALIGN_DATA(4) T m_array[S];
532 A m_fallbackAllocator;
546 template <
class T,
size_t S,
class A>
550 CRYPTOPP_INHERIT_ALLOCATOR_TYPES
571 if (size <= S && !m_allocated)
574 return GetAlignedArray();
577 return m_fallbackAllocator.allocate(size);
595 if (size <= S && !m_allocated)
598 return GetAlignedArray();
601 return m_fallbackAllocator.allocate(size, hint);
614 if (ptr == GetAlignedArray())
624 m_fallbackAllocator.deallocate(ptr, size);
645 pointer
reallocate(pointer oldPtr, size_type oldSize, size_type newSize,
bool preserve)
647 if (oldPtr == GetAlignedArray() && newSize <= S)
650 if (oldSize > newSize)
655 pointer newPointer = allocate(newSize, NULLPTR);
656 if (preserve && newSize)
658 const size_t copySize =
STDMIN(oldSize, newSize);
659 memcpy_s(newPointer,
sizeof(T)*newSize, oldPtr,
sizeof(T)*copySize);
661 deallocate(oldPtr, oldSize);
665 CRYPTOPP_CONSTEXPR size_type
max_size()
const 667 return STDMAX(m_fallbackAllocator.max_size(), S);
677 T* GetAlignedArray() {
return m_array;}
678 CRYPTOPP_ALIGN_DATA(8) T m_array[S];
680 A m_fallbackAllocator;
691 typedef typename A::value_type value_type;
692 typedef typename A::pointer iterator;
693 typedef typename A::const_pointer const_iterator;
694 typedef typename A::size_type size_type;
708 #if defined(CRYPTOPP_DOXYGEN_PROCESSING) 710 #elif defined(_MSC_VER) && (_MSC_VER <= 1400) 711 static const size_type
ELEMS_MAX = (~(size_type)0)/
sizeof(T);
712 #elif defined(CRYPTOPP_CXX11_ENUM) 713 enum : size_type {
ELEMS_MAX = A::ELEMS_MAX};
724 : m_mark(
ELEMS_MAX), m_size(size), m_ptr(m_alloc.allocate(size, NULLPTR)) { }
730 : m_mark(t.m_mark), m_size(t.m_size), m_ptr(m_alloc.allocate(t.m_size, NULLPTR)) {
732 if (t.m_ptr) {
memcpy_s(m_ptr, m_size*
sizeof(T), t.m_ptr, t.m_size*
sizeof(T));}
744 : m_mark(
ELEMS_MAX), m_size(len), m_ptr(m_alloc.allocate(len, NULLPTR)) {
747 memcpy_s(m_ptr, m_size*
sizeof(T), ptr, len*
sizeof(T));
749 memset(m_ptr, 0, m_size*
sizeof(T));
753 {m_alloc.deallocate(m_ptr,
STDMIN(m_size, m_mark));}
759 operator const void *()
const 764 operator const T *()
const 781 {
return m_ptr+m_size;}
784 const_iterator
end()
const 785 {
return m_ptr+m_size;}
789 typename A::pointer
data() {
return m_ptr;}
792 typename A::const_pointer
data()
const {
return m_ptr;}
797 size_type
size()
const {
return m_size;}
800 bool empty()
const {
return m_size == 0;}
807 const byte *
BytePtr()
const {
return (
const byte *)m_ptr;}
845 {
memcpy_s(m_ptr, m_size*
sizeof(T), ptr, len*
sizeof(T));}
857 for (
size_t i=0; i<count; ++i)
874 if (m_ptr && t.m_ptr)
875 {
memcpy_s(m_ptr, m_size*
sizeof(T), t, t.m_size*
sizeof(T));}
901 const size_type oldSize = m_size;
904 Grow(m_size+t.m_size);
905 memcpy_s(m_ptr+oldSize, (m_size-oldSize)*
sizeof(T), t.m_ptr, t.m_size*
sizeof(T));
910 memcpy_s(m_ptr+oldSize, (m_size-oldSize)*
sizeof(T), m_ptr, oldSize*
sizeof(T));
925 if(!t.m_size)
return SecBlock(*
this);
928 if (m_size) {
memcpy_s(result.m_ptr, result.m_size*
sizeof(T), m_ptr, m_size*
sizeof(T));}
929 memcpy_s(result.m_ptr+m_size, (result.m_size-m_size)*
sizeof(T), t.m_ptr, t.m_size*
sizeof(T));
941 return m_size == t.m_size &&
942 VerifyBufsEqual(reinterpret_cast<const byte*>(m_ptr), reinterpret_cast<const byte*>(t.m_ptr), m_size*
sizeof(T));
965 void New(size_type newSize)
967 m_ptr = m_alloc.reallocate(m_ptr, m_size, newSize,
false);
983 if (m_ptr) {
memset_z(m_ptr, 0, m_size*
sizeof(T));}
997 if (newSize > m_size)
999 m_ptr = m_alloc.reallocate(m_ptr, m_size, newSize,
true);
1015 if (newSize > m_size)
1017 m_ptr = m_alloc.reallocate(m_ptr, m_size, newSize,
true);
1018 memset_z(m_ptr+m_size, 0, (newSize-m_size)*
sizeof(T));
1033 m_ptr = m_alloc.reallocate(m_ptr, m_size, newSize,
true);
1044 std::swap(m_alloc, b.m_alloc);
1045 std::swap(m_mark, b.m_mark);
1046 std::swap(m_size, b.m_size);
1047 std::swap(m_ptr, b.m_ptr);
1052 size_type m_mark, m_size;
1056 #ifdef CRYPTOPP_DOXYGEN_PROCESSING 1076 template <
class T,
unsigned int S,
class A = FixedSizeAllocatorWithCleanup<T, S> >
1088 template <
class T,
unsigned int S,
bool T_Align16 = true>
1097 template <
class T,
unsigned int S,
class A = FixedSizeAllocatorWithCleanup<T, S, AllocatorWithCleanup<T> > >
1105 template<
class T,
bool A,
class V,
bool B>
1106 inline bool operator==(
const CryptoPP::AllocatorWithCleanup<T, A>&,
const CryptoPP::AllocatorWithCleanup<V, B>&) {
return (
true);}
1107 template<
class T,
bool A,
class V,
bool B>
1108 inline bool operator!=(
const CryptoPP::AllocatorWithCleanup<T, A>&,
const CryptoPP::AllocatorWithCleanup<V, B>&) {
return (
false);}
1112 NAMESPACE_BEGIN(
std)
1113 template <
class T,
class A>
1114 inline void swap(CryptoPP::SecBlock<T, A> &a, CryptoPP::SecBlock<T, A> &b)
1119 #if defined(_STLP_DONT_SUPPORT_REBIND_MEMBER_TEMPLATE) || (defined(_STLPORT_VERSION) && !defined(_STLP_MEMBER_TEMPLATE_CLASSES)) 1121 template <
class _Tp1,
class _Tp2>
1122 inline CryptoPP::AllocatorWithCleanup<_Tp2>&
1123 __stl_alloc_rebind(CryptoPP::AllocatorWithCleanup<_Tp1>& __a,
const _Tp2*)
1125 return (CryptoPP::AllocatorWithCleanup<_Tp2>&)(__a);
1131 #if CRYPTOPP_MSC_VERSION 1132 # 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 member 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.