3 #ifndef CRYPTOPP_SECBLOCK_H
4 #define CRYPTOPP_SECBLOCK_H
10 NAMESPACE_BEGIN(CryptoPP)
19 typedef size_t size_type;
20 #ifdef CRYPTOPP_MSVCRT6
21 typedef ptrdiff_t difference_type;
23 typedef std::ptrdiff_t difference_type;
26 typedef const T * const_pointer;
27 typedef T & reference;
28 typedef const T & const_reference;
30 pointer address(reference r)
const {
return (&r);}
31 const_pointer address(const_reference r)
const {
return (&r); }
32 void construct(pointer p,
const T& val) {
new (p) T(val);}
33 void destroy(pointer p) {p->~T();}
34 size_type max_size()
const {
return ~size_type(0)/
sizeof(T);}
37 static void CheckSize(
size_t n)
39 if (n > ~
size_t(0) /
sizeof(T))
40 throw InvalidArgument(
"AllocatorBase: requested size would cause integer overflow");
44 #define CRYPTOPP_INHERIT_ALLOCATOR_TYPES \
45 typedef typename AllocatorBase<T>::value_type value_type;\
46 typedef typename AllocatorBase<T>::size_type size_type;\
47 typedef typename AllocatorBase<T>::difference_type difference_type;\
48 typedef typename AllocatorBase<T>::pointer pointer;\
49 typedef typename AllocatorBase<T>::const_pointer const_pointer;\
50 typedef typename AllocatorBase<T>::reference reference;\
51 typedef typename AllocatorBase<T>::const_reference const_reference;
53 #if defined(_MSC_VER) && (_MSC_VER < 1300)
56 #pragma warning(disable: 4700) // VC60 workaround: don't know how to get rid of this warning
59 template <
class T,
class A>
60 typename A::pointer StandardReallocate(A& a, T *p,
typename A::size_type oldSize,
typename A::size_type newSize,
bool preserve)
62 if (oldSize == newSize)
67 typename A::pointer newPointer = a.allocate(newSize, NULL);
68 memcpy_s(newPointer,
sizeof(T)*newSize, p,
sizeof(T)*STDMIN(oldSize, newSize));
69 a.deallocate(p, oldSize);
74 a.deallocate(p, oldSize);
75 return a.allocate(newSize, NULL);
79 #if defined(_MSC_VER) && (_MSC_VER < 1300)
83 template <
class T,
bool T_Align16 = false>
87 CRYPTOPP_INHERIT_ALLOCATOR_TYPES
89 pointer allocate(size_type n,
const void * = NULL)
95 #if CRYPTOPP_BOOL_ALIGN16_ENABLED
96 if (T_Align16 && n*
sizeof(T) >= 16)
97 return (pointer)AlignedAllocate(n*
sizeof(T));
100 return (pointer)UnalignedAllocate(n*
sizeof(T));
103 void deallocate(
void *p, size_type n)
105 SecureWipeArray((pointer)p, n);
107 #if CRYPTOPP_BOOL_ALIGN16_ENABLED
108 if (T_Align16 && n*
sizeof(T) >= 16)
109 return AlignedDeallocate(p);
112 UnalignedDeallocate(p);
115 pointer reallocate(T *p, size_type oldSize, size_type newSize,
bool preserve)
117 return StandardReallocate(*
this, p, oldSize, newSize, preserve);
133 #if CRYPTOPP_BOOL_X86
141 CRYPTOPP_INHERIT_ALLOCATOR_TYPES
143 pointer allocate(size_type n,
const void * = NULL)
149 void deallocate(
void *p, size_type n)
154 size_type max_size()
const {
return 0;}
160 template <
class T,
size_t S,
class A = NullAllocator<T>,
bool T_Align16 = false>
164 CRYPTOPP_INHERIT_ALLOCATOR_TYPES
168 pointer allocate(size_type n)
170 assert(IsAlignedOn(m_array, 8));
172 if (n <= S && !m_allocated)
175 return GetAlignedArray();
178 return m_fallbackAllocator.allocate(n);
181 pointer allocate(size_type n,
const void *hint)
183 if (n <= S && !m_allocated)
186 return GetAlignedArray();
189 return m_fallbackAllocator.allocate(n, hint);
192 void deallocate(
void *p, size_type n)
194 if (p == GetAlignedArray())
199 SecureWipeArray((pointer)p, n);
202 m_fallbackAllocator.deallocate(p, n);
205 pointer reallocate(pointer p, size_type oldSize, size_type newSize,
bool preserve)
207 if (p == GetAlignedArray() && newSize <= S)
209 assert(oldSize <= S);
210 if (oldSize > newSize)
211 SecureWipeArray(p+newSize, oldSize-newSize);
215 pointer newPointer = allocate(newSize, NULL);
217 memcpy(newPointer, p,
sizeof(T)*STDMIN(oldSize, newSize));
218 deallocate(p, oldSize);
222 size_type max_size()
const {
return STDMAX(m_fallbackAllocator.max_size(), S);}
226 T* GetAlignedArray() {
return m_array;}
229 T* GetAlignedArray() {
return (CRYPTOPP_BOOL_ALIGN16_ENABLED && T_Align16) ? (T*)(((byte *)m_array) + (0-(size_t)m_array)%16) : m_array;}
230 CRYPTOPP_ALIGN_DATA(8) T m_array[(CRYPTOPP_BOOL_ALIGN16_ENABLED && T_Align16) ? S+8/
sizeof(T) : S];
232 A m_fallbackAllocator;
237 template <
class T,
class A = AllocatorWithCleanup<T> >
241 typedef typename A::value_type value_type;
242 typedef typename A::pointer iterator;
243 typedef typename A::const_pointer const_iterator;
244 typedef typename A::size_type size_type;
247 : m_size(size) {m_ptr = m_alloc.allocate(size, NULL);}
249 : m_size(t.m_size) {m_ptr = m_alloc.allocate(m_size, NULL); memcpy_s(m_ptr, m_size*
sizeof(T), t.m_ptr, m_size*
sizeof(T));}
253 m_ptr = m_alloc.allocate(len, NULL);
255 memset_z(m_ptr, 0, len*
sizeof(T));
257 memcpy(m_ptr, t, len*
sizeof(T));
261 {m_alloc.deallocate(m_ptr, m_size);}
267 operator const void *()
const
272 operator const T *()
const
292 const_iterator begin()
const
295 {
return m_ptr+m_size;}
296 const_iterator end()
const
297 {
return m_ptr+m_size;}
299 typename A::pointer data() {
return m_ptr;}
300 typename A::const_pointer data()
const {
return m_ptr;}
302 size_type size()
const {
return m_size;}
303 bool empty()
const {
return m_size == 0;}
305 byte * BytePtr() {
return (byte *)m_ptr;}
306 const byte * BytePtr()
const {
return (
const byte *)m_ptr;}
307 size_type SizeInBytes()
const {
return m_size*
sizeof(T);}
313 memcpy_s(m_ptr, m_size*
sizeof(T), t, len*
sizeof(T));
322 memcpy_s(m_ptr, m_size*
sizeof(T), t.m_ptr, m_size*
sizeof(T));
335 size_type oldSize = m_size;
336 Grow(m_size+t.m_size);
337 memcpy_s(m_ptr+oldSize, m_size*
sizeof(T), t.m_ptr, t.m_size*
sizeof(T));
345 memcpy_s(result.m_ptr, result.m_size*
sizeof(T), m_ptr, m_size*
sizeof(T));
346 memcpy_s(result.m_ptr+m_size, t.m_size*
sizeof(T), t.m_ptr, t.m_size*
sizeof(T));
352 return m_size == t.m_size && VerifyBufsEqual(m_ptr, t.m_ptr, m_size*
sizeof(T));
357 return !operator==(t);
361 void New(size_type newSize)
363 m_ptr = m_alloc.reallocate(m_ptr, m_size, newSize,
false);
371 memset_z(m_ptr, 0, m_size*
sizeof(T));
377 if (newSize > m_size)
379 m_ptr = m_alloc.reallocate(m_ptr, m_size, newSize,
true);
387 if (newSize > m_size)
389 m_ptr = m_alloc.reallocate(m_ptr, m_size, newSize,
true);
390 memset(m_ptr+m_size, 0, (newSize-m_size)*
sizeof(T));
398 m_ptr = m_alloc.reallocate(m_ptr, m_size, newSize,
true);
405 std::swap(m_alloc, b.m_alloc);
406 std::swap(m_size, b.m_size);
407 std::swap(m_ptr, b.m_ptr);
421 template <
class T,
unsigned int S,
class A = FixedSizeAllocatorWithCleanup<T, S> >
428 template <
class T,
unsigned int S,
bool T_Align16 = true>
434 template <
class T,
unsigned int S,
class A = FixedSizeAllocatorWithCleanup<T, S, AllocatorWithCleanup<T> > >
441 template<
class T,
bool A,
class U,
bool B>
442 inline bool operator==(
const CryptoPP::AllocatorWithCleanup<T, A>&,
const CryptoPP::AllocatorWithCleanup<U, B>&) {
return (
true);}
443 template<
class T,
bool A,
class U,
bool B>
444 inline bool operator!=(
const CryptoPP::AllocatorWithCleanup<T, A>&,
const CryptoPP::AllocatorWithCleanup<U, B>&) {
return (
false);}
449 template <class T, class A>
455 #if defined(_STLP_DONT_SUPPORT_REBIND_MEMBER_TEMPLATE) || (defined(_STLPORT_VERSION) && !defined(_STLP_MEMBER_TEMPLATE_CLASSES))
457 template <
class _Tp1,
class _Tp2>
458 inline CryptoPP::AllocatorWithCleanup<_Tp2>&
459 __stl_alloc_rebind(CryptoPP::AllocatorWithCleanup<_Tp1>& __a,
const _Tp2*)
461 return (CryptoPP::AllocatorWithCleanup<_Tp2>&)(__a);
exception thrown when an invalid argument is detected
void swap(SecBlock< T, A > &b)
swap contents and size with another SecBlock
a SecBlock that preallocates size S statically, and uses the heap when this size is exceeded ...
void CleanNew(size_type newSize)
change size and set contents to 0
void resize(size_type newSize)
change size and preserve contents
void CleanGrow(size_type newSize)
change size only if newSize > current size. contents are preserved and additional area is set to 0 ...
void Assign(const SecBlock< T, A > &t)
copy contents and size from another SecBlock
a block of memory allocated using A
void New(size_type newSize)
change size, without preserving contents
void Assign(const T *t, size_type len)
set contents and size
a SecBlock with fixed size, allocated statically
void Grow(size_type newSize)
change size only if newSize > current size. contents are preserved