6 #ifndef CRYPTOPP_SECBLOCK_H 7 #define CRYPTOPP_SECBLOCK_H 13 #if CRYPTOPP_MSC_VERSION 14 # pragma warning(push) 15 # pragma warning(disable: 4700 6386) 30 typedef size_t size_type;
31 #ifdef CRYPTOPP_MSVCRT6 32 typedef ptrdiff_t difference_type;
34 typedef std::ptrdiff_t difference_type;
37 typedef const T * const_pointer;
38 typedef T & reference;
39 typedef const T & const_reference;
41 pointer address(reference r)
const {
return (&r);}
42 const_pointer address(const_reference r)
const {
return (&r); }
43 void construct(pointer p,
const T& val) {
new (p) T(val);}
44 void destroy(pointer p) {CRYPTOPP_UNUSED(p); p->~T();}
54 #if defined(CRYPTOPP_CXX11_VARIADIC_TEMPLATES) || defined(CRYPTOPP_DOXYGEN_PROCESSING) 63 template<
typename U,
typename... Args>
64 void construct(U* ptr, Args&&... args) {::new ((
void*)ptr) U(std::forward<Args>(args)...);}
88 static void CheckSize(
size_t size)
92 throw InvalidArgument(
"AllocatorBase: requested size would cause integer overflow");
96 #define CRYPTOPP_INHERIT_ALLOCATOR_TYPES \ 97 typedef typename AllocatorBase<T>::value_type value_type;\ 98 typedef typename AllocatorBase<T>::size_type size_type;\ 99 typedef typename AllocatorBase<T>::difference_type difference_type;\ 100 typedef typename AllocatorBase<T>::pointer pointer;\ 101 typedef typename AllocatorBase<T>::const_pointer const_pointer;\ 102 typedef typename AllocatorBase<T>::reference reference;\ 103 typedef typename AllocatorBase<T>::const_reference const_reference; 115 template <
class T,
class A>
116 typename A::pointer
StandardReallocate(A& alloc, T *oldPtr,
typename A::size_type oldSize,
typename A::size_type newSize,
bool preserve)
118 assert((oldPtr && oldSize) || !(oldPtr || oldSize));
119 if (oldSize == newSize)
124 typename A::pointer newPointer = alloc.allocate(newSize, NULL);
125 const size_t copySize =
STDMIN(oldSize, newSize) *
sizeof(T);
127 if (oldPtr && newPointer) {
memcpy_s(newPointer, copySize, oldPtr, copySize);}
128 alloc.deallocate(oldPtr, oldSize);
133 alloc.deallocate(oldPtr, oldSize);
134 return alloc.allocate(newSize, NULL);
147 template <
class T,
bool T_Align16 = false>
151 CRYPTOPP_INHERIT_ALLOCATOR_TYPES
168 pointer
allocate(size_type size,
const void *ptr = NULL)
170 CRYPTOPP_UNUSED(ptr); assert(ptr == NULL);
171 this->CheckSize(size);
175 #if CRYPTOPP_BOOL_ALIGN16 177 if (T_Align16 && size*
sizeof(T) >= 16)
194 assert((ptr && size) || !(ptr || size));
197 #if CRYPTOPP_BOOL_ALIGN16 198 if (T_Align16 && size*
sizeof(T) >= 16)
218 pointer
reallocate(T *oldPtr, size_type oldSize, size_type newSize,
bool preserve)
220 assert((oldPtr && oldSize) || !(oldPtr || oldSize));
237 #if CRYPTOPP_BOOL_X86 253 CRYPTOPP_INHERIT_ALLOCATOR_TYPES
258 pointer
allocate(size_type n,
const void* unused = NULL)
260 CRYPTOPP_UNUSED(n); CRYPTOPP_UNUSED(unused);
261 assert(
false);
return NULL;
266 CRYPTOPP_UNUSED(p); CRYPTOPP_UNUSED(n);
270 size_type
max_size()
const {
return 0;}
285 template <
class T,
size_t S,
class A = NullAllocator<T>,
bool T_Align16 = false>
289 CRYPTOPP_INHERIT_ALLOCATOR_TYPES
311 if (size <= S && !m_allocated)
314 return GetAlignedArray();
317 return m_fallbackAllocator.allocate(size);
336 if (size <= S && !m_allocated)
339 return GetAlignedArray();
342 return m_fallbackAllocator.allocate(size, hint);
355 if (ptr == GetAlignedArray())
363 m_fallbackAllocator.deallocate(ptr, size);
383 pointer
reallocate(pointer oldPtr, size_type oldSize, size_type newSize,
bool preserve)
385 if (oldPtr == GetAlignedArray() && newSize <= S)
387 assert(oldSize <= S);
388 if (oldSize > newSize)
393 pointer newPointer =
allocate(newSize, NULL);
394 if (preserve && newSize)
396 const size_t copySize =
STDMIN(oldSize, newSize);
397 memcpy_s(newPointer, copySize, oldPtr, copySize);
403 size_type
max_size()
const {
return STDMAX(m_fallbackAllocator.max_size(), S);}
407 T* GetAlignedArray() {
return m_array;}
410 T* GetAlignedArray() {
return (CRYPTOPP_BOOL_ALIGN16 && T_Align16) ? (T*)(((byte *)m_array) + (0-(size_t)m_array)%16) : m_array;}
411 CRYPTOPP_ALIGN_DATA(8) T m_array[(CRYPTOPP_BOOL_ALIGN16 && T_Align16) ? S+8/
sizeof(T) : S];
413 A m_fallbackAllocator;
421 template <
class T,
class A = AllocatorWithCleanup<T> >
425 typedef typename A::value_type value_type;
426 typedef typename A::pointer iterator;
427 typedef typename A::const_pointer const_iterator;
428 typedef typename A::size_type size_type;
436 : m_size(size), m_ptr(m_alloc.
allocate(size, NULL)) { }
442 : m_size(t.m_size), m_ptr(m_alloc.
allocate(t.m_size, NULL)) {
443 assert((!t.m_ptr && !m_size) || (t.m_ptr && m_size));
444 if (t.m_ptr) {
memcpy_s(m_ptr, m_size*
sizeof(T), t.m_ptr, t.m_size*
sizeof(T));}
456 : m_size(len), m_ptr(m_alloc.
allocate(len, NULL)) {
457 assert((!m_ptr && !m_size) || (m_ptr && m_size));
459 memcpy_s(m_ptr, m_size*
sizeof(T), ptr, len*
sizeof(T));
461 memset(m_ptr, 0, m_size*
sizeof(T));
465 {m_alloc.deallocate(m_ptr, m_size);}
471 operator const void *()
const 476 operator const T *()
const 493 {
return m_ptr+m_size;}
496 const_iterator
end()
const 497 {
return m_ptr+m_size;}
501 typename A::pointer
data() {
return m_ptr;}
504 typename A::const_pointer
data()
const {
return m_ptr;}
509 size_type
size()
const {
return m_size;}
512 bool empty()
const {
return m_size == 0;}
519 const byte *
BytePtr()
const {
return (
const byte *)m_ptr;}
532 if (m_ptr && ptr && len)
533 {
memcpy_s(m_ptr, m_size*
sizeof(T), ptr, len*
sizeof(T));}
545 if (m_ptr && t.m_ptr && t.m_size)
546 {
memcpy_s(m_ptr, m_size*
sizeof(T), t, t.m_size*
sizeof(T));}
567 assert((!t.m_ptr && !t.m_size) || (t.m_ptr && t.m_ptr.m_size));
571 size_type oldSize = m_size;
572 Grow(m_size+t.m_size);
574 if (m_ptr && t.m_ptr)
575 {
memcpy_s(m_ptr+oldSize, (m_size-oldSize)*
sizeof(T), t.m_ptr, t.m_size*
sizeof(T));}
588 assert((!m_ptr && !m_size) || (m_ptr && m_size));
589 assert((!t.m_ptr && !t.m_size) || (t.m_ptr && t.m_ptr.m_size));
593 memcpy_s(result.m_ptr, result.m_size*
sizeof(T), m_ptr, m_size*
sizeof(T));
594 memcpy_s(result.m_ptr+m_size, (t.m_size-m_size)*
sizeof(T), t.m_ptr, t.m_size*
sizeof(T));
606 return m_size == t.m_size &&
VerifyBufsEqual(m_ptr, t.m_ptr, m_size*
sizeof(T));
618 return !operator==(t);
628 void New(size_type newSize)
630 m_ptr = m_alloc.reallocate(m_ptr, m_size, newSize,
false);
643 if (m_ptr) {
memset_z(m_ptr, 0, m_size*
sizeof(T));}
657 if (newSize > m_size)
659 m_ptr = m_alloc.reallocate(m_ptr, m_size, newSize,
true);
676 if (newSize > m_size)
678 m_ptr = m_alloc.reallocate(m_ptr, m_size, newSize,
true);
679 memset_z(m_ptr+m_size, 0, (newSize-m_size)*
sizeof(T));
695 m_ptr = m_alloc.reallocate(m_ptr, m_size, newSize,
true);
705 std::swap(m_alloc, b.m_alloc);
706 std::swap(m_size, b.m_size);
707 std::swap(m_ptr, b.m_ptr);
716 #ifdef CRYPTOPP_DOXYGEN_PROCESSING 740 template <
class T,
unsigned int S,
class A = FixedSizeAllocatorWithCleanup<T, S> >
753 template <
class T,
unsigned int S,
bool T_Align16 = true>
763 template <
class T,
unsigned int S,
class A = FixedSizeAllocatorWithCleanup<T, S, AllocatorWithCleanup<T> > >
771 template<
class T,
bool A,
class U,
bool B>
772 inline bool operator==(
const CryptoPP::AllocatorWithCleanup<T, A>&,
const CryptoPP::AllocatorWithCleanup<U, B>&) {
return (
true);}
773 template<
class T,
bool A,
class U,
bool B>
774 inline bool operator!=(
const CryptoPP::AllocatorWithCleanup<T, A>&,
const CryptoPP::AllocatorWithCleanup<U, B>&) {
return (
false);}
779 template <
class T,
class A>
780 inline void swap(CryptoPP::SecBlock<T, A> &a, CryptoPP::SecBlock<T, A> &b)
785 #if defined(_STLP_DONT_SUPPORT_REBIND_MEMBER_TEMPLATE) || (defined(_STLPORT_VERSION) && !defined(_STLP_MEMBER_TEMPLATE_CLASSES)) 787 template <
class _Tp1,
class _Tp2>
788 inline CryptoPP::AllocatorWithCleanup<_Tp2>&
789 __stl_alloc_rebind(CryptoPP::AllocatorWithCleanup<_Tp1>& __a,
const _Tp2*)
791 return (CryptoPP::AllocatorWithCleanup<_Tp2>&)(__a);
797 #if CRYPTOPP_MSC_VERSION 798 # pragma warning(pop) iterator end()
Provides an iterator pointing beyond the last element in the memory block.
An invalid argument was detected.
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.
void destroy(U *ptr)
Destroys an U constructed with variadic arguments.
Utility functions for the Crypto++ library.
void construct(U *ptr, Args &&...args)
Constructs a new U using variadic arguments.
void AlignedDeallocate(void *ptr)
Frees a buffer allocated with AlignedAllocate.
FixedSizeSecBlock()
Construct a FixedSizeSecBlock.
void CleanNew(size_type newSize)
Change size without preserving contents.
bool operator!=(const SecBlock< T, A > &t) const
Bitwise compare two SecBlocks.
A::const_pointer data() const
Provides a pointer to the first element in the memory block.
bool empty() const
Determines if the SecBlock is empty.
SecBlock< T, A > & operator=(const SecBlock< T, A > &t)
Assign contents from another 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.
void CleanGrow(size_type newSize)
Change size and preserve contents.
void Assign(const SecBlock< T, A > &t)
Copy contents from another SecBlock.
SecBlock< T, A > operator+(const SecBlock< T, A > &t)
Concatenate contents from another SecBlock.
SecBlock(size_type size=0)
Construct a SecBlock with space for size elements.
const_iterator begin() const
Provides a constant iterator pointing to the first element in the memory block.
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()
size_type size() const
Provides the count of elements in the SecBlock.
Library configuration file.
pointer allocate(size_type size, const void *hint)
Allocates a block of memory.
void New(size_type newSize)
Change size without preserving contents.
SecByteBlock is a SecBlock<byte> typedef.
pointer reallocate(T *oldPtr, size_type oldSize, size_type newSize, bool preserve)
Reallocates a block of memory.
size_type max_size() const
Returns the maximum number of elements the allocator can provide.
Static secure memory block with cleanup.
Allocates a block of memory with cleanup.
void deallocate(void *ptr, size_type size)
Deallocates a block of memory.
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.
const_iterator end() const
Provides a constant iterator pointing beyond the last element in the memory block.
void * UnalignedAllocate(size_t size)
Allocates a buffer.
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.
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.
AlignedSecByteBlock is a SecBlock<byte, 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.
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.
const byte * BytePtr() const
Return a byte pointer to the first element in the memory block.
SecBlockWithHint(size_t size)
construct a SecBlockWithHint with a count of elements
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.
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.
bool operator==(const SecBlock< T, A > &t) const
Bitwise compare two SecBlocks.
size_type SizeInBytes() const
Provides the number of bytes in the SecBlock.
FixedSizeAllocatorWithCleanup()
Constructs a FixedSizeAllocatorWithCleanup.
void UnalignedDeallocate(void *ptr)
Frees a buffer allocated with UnalignedAllocate.
SecWordBlock is a SecBlock<word> typedef.
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.