Fabcoin Core  0.16.2
P2P Digital Currency
misc.h
Go to the documentation of this file.
1 
2 // misc.h - written and placed in the public domain by Wei Dai
3 
6 
7 #ifndef CRYPTOPP_MISC_H
8 #define CRYPTOPP_MISC_H
9 
10 #include "config.h"
11 
12 #if !defined(CRYPTOPP_DOXYGEN_PROCESSING)
13 
14 #if (CRYPTOPP_MSC_VERSION)
15 # pragma warning(push)
16 # pragma warning(disable: 4146 4514)
17 # if (CRYPTOPP_MSC_VERSION >= 1400)
18 # pragma warning(disable: 6326)
19 # endif
20 #endif
21 
22 // Issue 340
23 #if CRYPTOPP_GCC_DIAGNOSTIC_AVAILABLE
24 # pragma GCC diagnostic push
25 # pragma GCC diagnostic ignored "-Wconversion"
26 # pragma GCC diagnostic ignored "-Wsign-conversion"
27 #endif
28 
29 #include "cryptlib.h"
30 #include "stdcpp.h"
31 #include "smartptr.h"
32 
33 #ifdef _MSC_VER
34  #if _MSC_VER >= 1400
35  // VC2005 workaround: disable declarations that conflict with winnt.h
36  #define _interlockedbittestandset CRYPTOPP_DISABLED_INTRINSIC_1
37  #define _interlockedbittestandreset CRYPTOPP_DISABLED_INTRINSIC_2
38  #define _interlockedbittestandset64 CRYPTOPP_DISABLED_INTRINSIC_3
39  #define _interlockedbittestandreset64 CRYPTOPP_DISABLED_INTRINSIC_4
40  #include <intrin.h>
41  #undef _interlockedbittestandset
42  #undef _interlockedbittestandreset
43  #undef _interlockedbittestandset64
44  #undef _interlockedbittestandreset64
45  #define CRYPTOPP_FAST_ROTATE(x) 1
46  #elif _MSC_VER >= 1300
47  #define CRYPTOPP_FAST_ROTATE(x) ((x) == 32 | (x) == 64)
48  #else
49  #define CRYPTOPP_FAST_ROTATE(x) ((x) == 32)
50  #endif
51 #elif (defined(__MWERKS__) && TARGET_CPU_PPC) || \
52  (defined(__GNUC__) && (defined(_ARCH_PWR2) || defined(_ARCH_PWR) || defined(_ARCH_PPC) || defined(_ARCH_PPC64) || defined(_ARCH_COM)))
53  #define CRYPTOPP_FAST_ROTATE(x) ((x) == 32)
54 #elif defined(__GNUC__) && (CRYPTOPP_BOOL_X64 || CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X86) // depend on GCC's peephole optimization to generate rotate instructions
55  #define CRYPTOPP_FAST_ROTATE(x) 1
56 #else
57  #define CRYPTOPP_FAST_ROTATE(x) 0
58 #endif
59 
60 #ifdef __BORLANDC__
61 #include <mem.h>
62 #include <stdlib.h>
63 #endif
64 
65 #if defined(__GNUC__) && defined(__linux__)
66 #define CRYPTOPP_BYTESWAP_AVAILABLE
67 #include <byteswap.h>
68 #endif
69 
70 #if defined(__GNUC__) && defined(__BMI__)
71 # include <immintrin.h>
72 # if defined(__clang__)
73 # ifndef _tzcnt_u32
74 # define _tzcnt_u32(x) __tzcnt_u32(x)
75 # endif
76 # ifndef _blsr_u32
77 # define _blsr_u32(x) __blsr_u32(x)
78 # endif
79 # ifdef __x86_64__
80 # ifndef _tzcnt_u64
81 # define _tzcnt_u64(x) __tzcnt_u64(x)
82 # endif
83 # ifndef _blsr_u64
84 # define _blsr_u64(x) __blsr_u64(x)
85 # endif
86 # endif // x86_64
87 # endif // Clang
88 #endif // GNUC and BMI
89 
90 #endif // CRYPTOPP_DOXYGEN_PROCESSING
91 
92 #if CRYPTOPP_DOXYGEN_PROCESSING
93 # define SIZE_MAX ...
103 #else
104 // Its amazing portability problems still plague this simple concept in 2015.
105 // http://stackoverflow.com/questions/30472731/which-c-standard-header-defines-size-max
106 // Avoid NOMINMAX macro on Windows. http://support.microsoft.com/en-us/kb/143208
107 #ifndef SIZE_MAX
108 # if defined(__SIZE_MAX__) && (__SIZE_MAX__ > 0)
109 # define SIZE_MAX __SIZE_MAX__
110 # elif defined(SIZE_T_MAX) && (SIZE_T_MAX > 0)
111 # define SIZE_MAX SIZE_T_MAX
112 # else
113 # define SIZE_MAX ((std::numeric_limits<size_t>::max)())
114 # endif
115 #endif
116 
117 #endif // CRYPTOPP_DOXYGEN_PROCESSING
118 
120 
121 // Forward declaration for IntToString specialization
122 class Integer;
123 
124 // ************** compile-time assertion ***************
125 
126 #if CRYPTOPP_DOXYGEN_PROCESSING
127 #define CRYPTOPP_COMPILE_ASSERT(expr) { ... }
131 #else // CRYPTOPP_DOXYGEN_PROCESSING
132 template <bool b>
134 {
135  static char dummy[2*b-1];
136 };
138 
139 #define CRYPTOPP_COMPILE_ASSERT(assertion) CRYPTOPP_COMPILE_ASSERT_INSTANCE(assertion, __LINE__)
140 #if defined(CRYPTOPP_EXPORTS) || defined(CRYPTOPP_IMPORTS)
141 #define CRYPTOPP_COMPILE_ASSERT_INSTANCE(assertion, instance)
142 #else
143 # if defined(__GNUC__)
144 # define CRYPTOPP_COMPILE_ASSERT_INSTANCE(assertion, instance) \
145  static CompileAssert<(assertion)> \
146  CRYPTOPP_ASSERT_JOIN(cryptopp_CRYPTOPP_ASSERT_, instance) __attribute__ ((unused))
147 # else
148 # define CRYPTOPP_COMPILE_ASSERT_INSTANCE(assertion, instance) \
149  static CompileAssert<(assertion)> \
150  CRYPTOPP_ASSERT_JOIN(cryptopp_CRYPTOPP_ASSERT_, instance)
151 # endif // __GNUC__
152 #endif
153 #define CRYPTOPP_ASSERT_JOIN(X, Y) CRYPTOPP_DO_ASSERT_JOIN(X, Y)
154 #define CRYPTOPP_DO_ASSERT_JOIN(X, Y) X##Y
155 
156 #endif // CRYPTOPP_DOXYGEN_PROCESSING
157 
158 // ************** count elements in an array ***************
159 
160 #if CRYPTOPP_DOXYGEN_PROCESSING
161 # define COUNTOF(arr)
169 #else
170 // VS2005 added _countof
171 #ifndef COUNTOF
172 # if defined(_MSC_VER) && (_MSC_VER >= 1400)
173 # define COUNTOF(x) _countof(x)
174 # else
175 # define COUNTOF(x) (sizeof(x)/sizeof(x[0]))
176 # endif
177 #endif // COUNTOF
178 #endif // CRYPTOPP_DOXYGEN_PROCESSING
179 
180 // ************** misc classes ***************
181 
185 {
186 };
187 
188 #if !defined(CRYPTOPP_DOXYGEN_PROCESSING)
189 template <class BASE1, class BASE2>
190 class CRYPTOPP_NO_VTABLE TwoBases : public BASE1, public BASE2
191 {
192 };
193 
194 template <class BASE1, class BASE2, class BASE3>
195 class CRYPTOPP_NO_VTABLE ThreeBases : public BASE1, public BASE2, public BASE3
196 {
197 };
198 #endif // CRYPTOPP_DOXYGEN_PROCESSING
199 
204 template <class T>
206 {
207 protected:
209 };
210 
218 {
219 public:
221 private:
222  NotCopyable(const NotCopyable &);
223  void operator=(const NotCopyable &);
224 };
225 
229 template <class T>
230 struct NewObject
231 {
232  T* operator()() const {return new T;}
233 };
234 
235 #if CRYPTOPP_DOXYGEN_PROCESSING
236 #define MEMORY_BARRIER ...
245 #else
246 #if defined(CRYPTOPP_CXX11_ATOMICS)
247 # define MEMORY_BARRIER() std::atomic_thread_fence(std::memory_order_acq_rel)
248 #elif (_MSC_VER >= 1400)
249 # pragma intrinsic(_ReadWriteBarrier)
250 # define MEMORY_BARRIER() _ReadWriteBarrier()
251 #elif defined(__INTEL_COMPILER)
252 # define MEMORY_BARRIER() __memory_barrier()
253 #elif defined(__GNUC__) || defined(__clang__)
254 # define MEMORY_BARRIER() __asm__ __volatile__ ("" ::: "memory")
255 #else
256 # define MEMORY_BARRIER()
257 #endif
258 #endif // CRYPTOPP_DOXYGEN_PROCESSING
259 
273 template <class T, class F = NewObject<T>, int instance=0>
275 {
276 public:
277  Singleton(F objectFactory = F()) : m_objectFactory(objectFactory) {}
278 
279  // prevent this function from being inlined
281 
282 private:
284 };
285 
290 #if defined(CRYPTOPP_CXX11_ATOMICS) && defined(CRYPTOPP_CXX11_SYNCHRONIZATION)
291 template <class T, class F, int instance>
293 {
294  static std::mutex s_mutex;
295  static std::atomic<T*> s_pObject;
296 
297  T *p = s_pObject.load(std::memory_order_relaxed);
298  std::atomic_thread_fence(std::memory_order_acquire);
299 
300  if (p)
301  return *p;
302 
303  std::lock_guard<std::mutex> lock(s_mutex);
304  p = s_pObject.load(std::memory_order_relaxed);
305  std::atomic_thread_fence(std::memory_order_acquire);
306 
307  if (p)
308  return *p;
309 
310  T *newObject = m_objectFactory();
311  s_pObject.store(newObject, std::memory_order_relaxed);
312  std::atomic_thread_fence(std::memory_order_release);
313 
314  return *newObject;
315 }
316 #else
317 template <class T, class F, int instance>
319 {
320  static volatile simple_ptr<T> s_pObject;
321  T *p = s_pObject.m_p;
322  MEMORY_BARRIER();
323 
324  if (p)
325  return *p;
326 
327  T *newObject = m_objectFactory();
328  p = s_pObject.m_p;
329  MEMORY_BARRIER();
330 
331  if (p)
332  {
333  delete newObject;
334  return *p;
335  }
336 
337  s_pObject.m_p = newObject;
338  MEMORY_BARRIER();
339 
340  return *newObject;
341 }
342 #endif
343 
344 // ************** misc functions ***************
345 
346 #if (!__STDC_WANT_SECURE_LIB__ && !defined(_MEMORY_S_DEFINED)) || defined(CRYPTOPP_WANT_SECURE_LIB)
347 
366 inline void memcpy_s(void *dest, size_t sizeInBytes, const void *src, size_t count)
367 {
368  // Safer functions on Windows for C&A, http://github.com/weidai11/cryptopp/issues/55
369 
370  // Pointers must be valid; otherwise undefined behavior
371  CRYPTOPP_ASSERT(dest != NULL); CRYPTOPP_ASSERT(src != NULL);
372  // Destination buffer must be large enough to satsify request
373  CRYPTOPP_ASSERT(sizeInBytes >= count);
374  if (count > sizeInBytes)
375  throw InvalidArgument("memcpy_s: buffer overflow");
376 
377 #if CRYPTOPP_MSC_VERSION
378 # pragma warning(push)
379 # pragma warning(disable: 4996)
380 # if (CRYPTOPP_MSC_VERSION >= 1400)
381 # pragma warning(disable: 6386)
382 # endif
383 #endif
384  memcpy(dest, src, count);
385 #if CRYPTOPP_MSC_VERSION
386 # pragma warning(pop)
387 #endif
388 }
389 
408 inline void memmove_s(void *dest, size_t sizeInBytes, const void *src, size_t count)
409 {
410  // Safer functions on Windows for C&A, http://github.com/weidai11/cryptopp/issues/55
411 
412  // Pointers must be valid; otherwise undefined behavior
413  CRYPTOPP_ASSERT(dest != NULL); CRYPTOPP_ASSERT(src != NULL);
414  // Destination buffer must be large enough to satsify request
415  CRYPTOPP_ASSERT(sizeInBytes >= count);
416  if (count > sizeInBytes)
417  throw InvalidArgument("memmove_s: buffer overflow");
418 
419 #if CRYPTOPP_MSC_VERSION
420 # pragma warning(push)
421 # pragma warning(disable: 4996)
422 # if (CRYPTOPP_MSC_VERSION >= 1400)
423 # pragma warning(disable: 6386)
424 # endif
425 #endif
426  memmove(dest, src, count);
427 #if CRYPTOPP_MSC_VERSION
428 # pragma warning(pop)
429 #endif
430 }
431 
432 #if __BORLANDC__ >= 0x620
433 // C++Builder 2010 workaround: can't use std::memcpy_s because it doesn't allow 0 lengths
434 # define memcpy_s CryptoPP::memcpy_s
435 # define memmove_s CryptoPP::memmove_s
436 #endif
437 
438 #endif // __STDC_WANT_SECURE_LIB__
439 
448 template <class T>
449 inline void vec_swap(T& a, T& b)
450 {
451  T t;
452  t=a, a=b, b=t;
453 }
454 
461 inline void * memset_z(void *ptr, int value, size_t num)
462 {
463 // avoid extranous warning on GCC 4.3.2 Ubuntu 8.10
464 #if CRYPTOPP_GCC_VERSION >= 30001
465  if (__builtin_constant_p(num) && num==0)
466  return ptr;
467 #endif
468  volatile void* x = memset(ptr, value, num);
469  return const_cast<void*>(x);
470 }
471 
477 template <class T> inline const T& STDMIN(const T& a, const T& b)
478 {
479  return b < a ? b : a;
480 }
481 
487 template <class T> inline const T& STDMAX(const T& a, const T& b)
488 {
489  return a < b ? b : a;
490 }
491 
492 #if CRYPTOPP_MSC_VERSION
493 # pragma warning(push)
494 # pragma warning(disable: 4389)
495 #endif
496 
497 #if CRYPTOPP_GCC_DIAGNOSTIC_AVAILABLE
498 # pragma GCC diagnostic push
499 # pragma GCC diagnostic ignored "-Wsign-compare"
500 # if (CRYPTOPP_LLVM_CLANG_VERSION >= 20800) || (CRYPTOPP_APPLE_CLANG_VERSION >= 30000)
501 # pragma GCC diagnostic ignored "-Wtautological-compare"
502 # elif (CRYPTOPP_GCC_VERSION >= 40300)
503 # pragma GCC diagnostic ignored "-Wtype-limits"
504 # endif
505 #endif
506 
512 template <class T1, class T2> inline const T1 UnsignedMin(const T1& a, const T2& b)
513 {
514  CRYPTOPP_COMPILE_ASSERT((sizeof(T1)<=sizeof(T2) && T2(-1)>0) || (sizeof(T1)>sizeof(T2) && T1(-1)>0));
515  if (sizeof(T1)<=sizeof(T2))
516  return b < (T2)a ? (T1)b : a;
517  else
518  return (T1)b < a ? (T1)b : a;
519 }
520 
525 template <class T1, class T2>
526 inline bool SafeConvert(T1 from, T2 &to)
527 {
528  to = (T2)from;
529  if (from != to || (from > 0) != (to > 0))
530  return false;
531  return true;
532 }
533 
538 template <class T>
539 std::string IntToString(T value, unsigned int base = 10)
540 {
541  // Hack... set the high bit for uppercase.
542  static const unsigned int HIGH_BIT = (1U << 31);
543  const char CH = !!(base & HIGH_BIT) ? 'A' : 'a';
544  base &= ~HIGH_BIT;
545 
546  CRYPTOPP_ASSERT(base >= 2);
547  if (value == 0)
548  return "0";
549 
550  bool negate = false;
551  if (value < 0)
552  {
553  negate = true;
554  value = 0-value; // VC .NET does not like -a
555  }
556  std::string result;
557  while (value > 0)
558  {
559  T digit = value % base;
560  result = char((digit < 10 ? '0' : (CH - 10)) + digit) + result;
561  value /= base;
562  }
563  if (negate)
564  result = "-" + result;
565  return result;
566 }
567 
574 template <> CRYPTOPP_DLL
575 std::string IntToString<word64>(word64 value, unsigned int base);
576 
596 template <> CRYPTOPP_DLL
597 std::string IntToString<Integer>(Integer value, unsigned int base);
598 
599 #if CRYPTOPP_MSC_VERSION
600 # pragma warning(pop)
601 #endif
602 
603 #if CRYPTOPP_GCC_DIAGNOSTIC_AVAILABLE
604 # pragma GCC diagnostic pop
605 #endif
606 
607 #define RETURN_IF_NONZERO(x) size_t returnedValue = x; if (returnedValue) return returnedValue
608 
609 // this version of the macro is fastest on Pentium 3 and Pentium 4 with MSVC 6 SP5 w/ Processor Pack
610 #define GETBYTE(x, y) (unsigned int)byte((x)>>(8*(y)))
611 // these may be faster on other CPUs/compilers
612 // #define GETBYTE(x, y) (unsigned int)(((x)>>(8*(y)))&255)
613 // #define GETBYTE(x, y) (((byte *)&(x))[y])
614 
615 #define CRYPTOPP_GET_BYTE_AS_BYTE(x, y) byte((x)>>(8*(y)))
616 
620 template <class T>
621 unsigned int Parity(T value)
622 {
623  for (unsigned int i=8*sizeof(value)/2; i>0; i/=2)
624  value ^= value >> i;
625  return (unsigned int)value&1;
626 }
627 
631 template <class T>
632 unsigned int BytePrecision(const T &value)
633 {
634  if (!value)
635  return 0;
636 
637  unsigned int l=0, h=8*sizeof(value);
638  while (h-l > 8)
639  {
640  unsigned int t = (l+h)/2;
641  if (value >> t)
642  l = t;
643  else
644  h = t;
645  }
646 
647  return h/8;
648 }
649 
653 template <class T>
654 unsigned int BitPrecision(const T &value)
655 {
656  if (!value)
657  return 0;
658 
659  unsigned int l=0, h=8*sizeof(value);
660 
661  while (h-l > 1)
662  {
663  unsigned int t = (l+h)/2;
664  if (value >> t)
665  l = t;
666  else
667  h = t;
668  }
669 
670  return h;
671 }
672 
679 inline unsigned int TrailingZeros(word32 v)
680 {
681  // GCC 4.7 and VS2012 provides tzcnt on AVX2/BMI enabled processors
682  // We don't enable for Microsoft because it requires a runtime check.
683  // http://msdn.microsoft.com/en-us/library/hh977023%28v=vs.110%29.aspx
684  CRYPTOPP_ASSERT(v != 0);
685 #if defined(__GNUC__) && defined(__BMI__)
686  return (unsigned int)_tzcnt_u32(v);
687 #elif defined(__GNUC__) && (CRYPTOPP_GCC_VERSION >= 30400)
688  return (unsigned int)__builtin_ctz(v);
689 #elif defined(_MSC_VER) && (_MSC_VER >= 1400)
690  unsigned long result;
691  _BitScanForward(&result, v);
692  return (unsigned int)result;
693 #else
694  // from http://graphics.stanford.edu/~seander/bithacks.html#ZerosOnRightMultLookup
695  static const int MultiplyDeBruijnBitPosition[32] =
696  {
697  0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,
698  31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9
699  };
700  return MultiplyDeBruijnBitPosition[((word32)((v & -v) * 0x077CB531U)) >> 27];
701 #endif
702 }
703 
710 inline unsigned int TrailingZeros(word64 v)
711 {
712  // GCC 4.7 and VS2012 provides tzcnt on AVX2/BMI enabled processors
713  // We don't enable for Microsoft because it requires a runtime check.
714  // http://msdn.microsoft.com/en-us/library/hh977023%28v=vs.110%29.aspx
715  CRYPTOPP_ASSERT(v != 0);
716 #if defined(__GNUC__) && defined(__BMI__) && defined(__x86_64__)
717  return (unsigned int)_tzcnt_u64(v);
718 #elif defined(__GNUC__) && (CRYPTOPP_GCC_VERSION >= 30400)
719  return (unsigned int)__builtin_ctzll(v);
720 #elif defined(_MSC_VER) && (_MSC_VER >= 1400) && (defined(_M_X64) || defined(_M_IA64))
721  unsigned long result;
722  _BitScanForward64(&result, v);
723  return (unsigned int)result;
724 #else
725  return word32(v) ? TrailingZeros(word32(v)) : 32 + TrailingZeros(word32(v>>32));
726 #endif
727 }
728 
736 template <class T>
737 inline T Crop(T value, size_t bits)
738 {
739  if (bits < 8*sizeof(value))
740  return T(value & ((T(1) << bits) - 1));
741  else
742  return value;
743 }
744 
749 inline size_t BitsToBytes(size_t bitCount)
750 {
751  return ((bitCount+7)/(8));
752 }
753 
759 inline size_t BytesToWords(size_t byteCount)
760 {
761  return ((byteCount+WORD_SIZE-1)/WORD_SIZE);
762 }
763 
769 inline size_t BitsToWords(size_t bitCount)
770 {
771  return ((bitCount+WORD_BITS-1)/(WORD_BITS));
772 }
773 
779 inline size_t BitsToDwords(size_t bitCount)
780 {
781  return ((bitCount+2*WORD_BITS-1)/(2*WORD_BITS));
782 }
783 
790 CRYPTOPP_DLL void CRYPTOPP_API xorbuf(byte *buf, const byte *mask, size_t count);
791 
799 CRYPTOPP_DLL void CRYPTOPP_API xorbuf(byte *output, const byte *input, const byte *mask, size_t count);
800 
810 CRYPTOPP_DLL bool CRYPTOPP_API VerifyBufsEqual(const byte *buf1, const byte *buf2, size_t count);
811 
817 template <class T>
818 inline bool IsPowerOf2(const T &value)
819 {
820  return value > 0 && (value & (value-1)) == 0;
821 }
822 
823 #if defined(__GNUC__) && defined(__BMI__)
824 template <>
825 inline bool IsPowerOf2<word32>(const word32 &value)
826 {
827  return value > 0 && _blsr_u32(value) == 0;
828 }
829 
830 # if defined(__x86_64__)
831 template <>
832 inline bool IsPowerOf2<word64>(const word64 &value)
833 {
834  return value > 0 && _blsr_u64(value) == 0;
835 }
836 # endif
837 #endif
838 
846 template <class T1, class T2>
847 inline T1 SaturatingSubtract(const T1 &a, const T2 &b)
848 {
849  // Generated ASM of a typical clamp, http://gcc.gnu.org/ml/gcc-help/2014-10/msg00112.html
850  return T1((a > b) ? (a - b) : 0);
851 }
852 
861 template <class T1, class T2>
862 inline T1 SaturatingSubtract1(const T1 &a, const T2 &b)
863 {
864  // Generated ASM of a typical clamp, http://gcc.gnu.org/ml/gcc-help/2014-10/msg00112.html
865  return T1((a > b) ? (a - b) : 1);
866 }
867 
874 template <class T1, class T2>
875 inline T2 ModPowerOf2(const T1 &a, const T2 &b)
876 {
878  // Coverity finding CID 170383 Overflowed return value (INTEGER_OVERFLOW)
879  return T2(a) & SaturatingSubtract(b,1U);
880 }
881 
888 template <class T1, class T2>
889 inline T1 RoundDownToMultipleOf(const T1 &n, const T2 &m)
890 {
891  if (IsPowerOf2(m))
892  return n - ModPowerOf2(n, m);
893  else
894  return n - n%m;
895 }
896 
904 template <class T1, class T2>
905 inline T1 RoundUpToMultipleOf(const T1 &n, const T2 &m)
906 {
907  if (n > (SIZE_MAX/sizeof(T1))-m-1)
908  throw InvalidArgument("RoundUpToMultipleOf: integer overflow");
909  return RoundDownToMultipleOf(T1(n+m-1), m);
910 }
911 
921 template <class T>
922 inline unsigned int GetAlignmentOf()
923 {
924 // GCC 4.6 (circa 2008) and above aggressively uses vectorization.
925 #if defined(CRYPTOPP_ALLOW_UNALIGNED_DATA_ACCESS)
926  if (sizeof(T) < 16)
927  return 1;
928 #endif
929 
930 #if defined(CRYPTOPP_CXX11_ALIGNOF)
931  return alignof(T);
932 #elif (_MSC_VER >= 1300)
933  return __alignof(T);
934 #elif defined(__GNUC__)
935  return __alignof__(T);
936 #elif CRYPTOPP_BOOL_SLOW_WORD64
937  return UnsignedMin(4U, sizeof(T));
938 #else
939 # if __BIGGEST_ALIGNMENT__
940  if (__BIGGEST_ALIGNMENT__ < sizeof(T))
941  return __BIGGEST_ALIGNMENT__;
942  else
943 # endif
944  return sizeof(T);
945 #endif
946 }
947 
954 inline bool IsAlignedOn(const void *ptr, unsigned int alignment)
955 {
956  return alignment==1 || (IsPowerOf2(alignment) ? ModPowerOf2((size_t)ptr, alignment) == 0 : (size_t)ptr % alignment == 0);
957 }
958 
963 template <class T>
964 inline bool IsAligned(const void *ptr)
965 {
966  return IsAlignedOn(ptr, GetAlignmentOf<T>());
967 }
968 
969 #if defined(IS_LITTLE_ENDIAN)
971 #elif defined(IS_BIG_ENDIAN)
972  typedef BigEndian NativeByteOrder;
973 #else
974 # error "Unable to determine endian-ness"
975 #endif
976 
986 {
987  return NativeByteOrder::ToEnum();
988 }
989 
993 inline bool NativeByteOrderIs(ByteOrder order)
994 {
995  return order == GetNativeByteOrder();
996 }
997 
1007 template <class T>
1008 inline CipherDir GetCipherDir(const T &obj)
1009 {
1010  return obj.IsForwardTransformation() ? ENCRYPTION : DECRYPTION;
1011 }
1012 
1022 
1029 inline void IncrementCounterByOne(byte *inout, unsigned int size)
1030 {
1031  CRYPTOPP_ASSERT(inout != NULL); CRYPTOPP_ASSERT(size < INT_MAX);
1032  for (int i=int(size-1), carry=1; i>=0 && carry; i--)
1033  carry = !++inout[i];
1034 }
1035 
1043 inline void IncrementCounterByOne(byte *output, const byte *input, unsigned int size)
1044 {
1045  CRYPTOPP_ASSERT(output != NULL); CRYPTOPP_ASSERT(input != NULL); CRYPTOPP_ASSERT(size < INT_MAX);
1046 
1047  int i, carry;
1048  for (i=int(size-1), carry=1; i>=0 && carry; i--)
1049  carry = ((output[i] = input[i]+1) == 0);
1050  memcpy_s(output, size, input, size_t(i)+1);
1051 }
1052 
1057 template <class T>
1058 inline void ConditionalSwap(bool c, T &a, T &b)
1059 {
1060  T t = c * (a ^ b);
1061  a ^= t;
1062  b ^= t;
1063 }
1064 
1069 template <class T>
1070 inline void ConditionalSwapPointers(bool c, T &a, T &b)
1071 {
1072  ptrdiff_t t = size_t(c) * (a - b);
1073  a -= t;
1074  b += t;
1075 }
1076 
1077 // see http://www.dwheeler.com/secure-programs/Secure-Programs-HOWTO/protect-secrets.html
1078 // and https://www.securecoding.cert.org/confluence/display/cplusplus/MSC06-CPP.+Be+aware+of+compiler+optimization+when+dealing+with+sensitive+data
1079 
1084 template <class T>
1085 void SecureWipeBuffer(T *buf, size_t n)
1086 {
1087  // GCC 4.3.2 on Cygwin optimizes away the first store if this loop is done in the forward direction
1088  volatile T *p = buf+n;
1089  while (n--)
1090  *(--p) = 0;
1091 }
1092 
1093 #if (_MSC_VER >= 1400 || defined(__GNUC__)) && (CRYPTOPP_BOOL_X64 || CRYPTOPP_BOOL_X86)
1094 
1099 template<> inline void SecureWipeBuffer(byte *buf, size_t n)
1100 {
1101  volatile byte *p = buf;
1102 #ifdef __GNUC__
1103  asm volatile("rep stosb" : "+c"(n), "+D"(p) : "a"(0) : "memory");
1104 #else
1105  __stosb((byte *)(size_t)p, 0, n);
1106 #endif
1107 }
1108 
1113 template<> inline void SecureWipeBuffer(word16 *buf, size_t n)
1114 {
1115  volatile word16 *p = buf;
1116 #ifdef __GNUC__
1117  asm volatile("rep stosw" : "+c"(n), "+D"(p) : "a"(0) : "memory");
1118 #else
1119  __stosw((word16 *)(size_t)p, 0, n);
1120 #endif
1121 }
1122 
1127 template<> inline void SecureWipeBuffer(word32 *buf, size_t n)
1128 {
1129  volatile word32 *p = buf;
1130 #ifdef __GNUC__
1131  asm volatile("rep stosl" : "+c"(n), "+D"(p) : "a"(0) : "memory");
1132 #else
1133  __stosd((unsigned long *)(size_t)p, 0, n);
1134 #endif
1135 }
1136 
1141 template<> inline void SecureWipeBuffer(word64 *buf, size_t n)
1142 {
1143 #if CRYPTOPP_BOOL_X64
1144  volatile word64 *p = buf;
1145 #ifdef __GNUC__
1146  asm volatile("rep stosq" : "+c"(n), "+D"(p) : "a"(0) : "memory");
1147 #else
1148  __stosq((word64 *)(size_t)p, 0, n);
1149 #endif
1150 #else
1151  SecureWipeBuffer((word32 *)buf, 2*n);
1152 #endif
1153 }
1154 
1155 #endif // #if (_MSC_VER >= 1400 || defined(__GNUC__)) && (CRYPTOPP_BOOL_X64 || CRYPTOPP_BOOL_X86)
1156 
1157 #if (_MSC_VER >= 1700) && defined(_M_ARM)
1158 template<> inline void SecureWipeBuffer(byte *buf, size_t n)
1159 {
1160  char *p = reinterpret_cast<char*>(buf+n);
1161  while (n--)
1162  __iso_volatile_store8(--p, 0);
1163 }
1164 
1165 template<> inline void SecureWipeBuffer(word16 *buf, size_t n)
1166 {
1167  short *p = reinterpret_cast<short*>(buf+n);
1168  while (n--)
1169  __iso_volatile_store16(--p, 0);
1170 }
1171 
1172 template<> inline void SecureWipeBuffer(word32 *buf, size_t n)
1173 {
1174  int *p = reinterpret_cast<int*>(buf+n);
1175  while (n--)
1176  __iso_volatile_store32(--p, 0);
1177 }
1178 
1179 template<> inline void SecureWipeBuffer(word64 *buf, size_t n)
1180 {
1181  __int64 *p = reinterpret_cast<__int64*>(buf+n);
1182  while (n--)
1183  __iso_volatile_store64(--p, 0);
1184 }
1185 #endif
1186 
1191 template <class T>
1192 inline void SecureWipeArray(T *buf, size_t n)
1193 {
1194  if (sizeof(T) % 8 == 0 && GetAlignmentOf<T>() % GetAlignmentOf<word64>() == 0)
1195  SecureWipeBuffer((word64 *)(void *)buf, n * (sizeof(T)/8));
1196  else if (sizeof(T) % 4 == 0 && GetAlignmentOf<T>() % GetAlignmentOf<word32>() == 0)
1197  SecureWipeBuffer((word32 *)(void *)buf, n * (sizeof(T)/4));
1198  else if (sizeof(T) % 2 == 0 && GetAlignmentOf<T>() % GetAlignmentOf<word16>() == 0)
1199  SecureWipeBuffer((word16 *)(void *)buf, n * (sizeof(T)/2));
1200  else
1201  SecureWipeBuffer((byte *)(void *)buf, n * sizeof(T));
1202 }
1203 
1216 std::string StringNarrow(const wchar_t *str, bool throwOnError = true);
1217 
1218 #ifdef CRYPTOPP_DOXYGEN_PROCESSING
1219 
1228 CRYPTOPP_DLL void* CRYPTOPP_API AlignedAllocate(size_t size);
1229 
1234 CRYPTOPP_DLL void CRYPTOPP_API AlignedDeallocate(void *ptr);
1235 
1236 #endif // CRYPTOPP_DOXYGEN_PROCESSING
1237 
1238 #if CRYPTOPP_BOOL_ALIGN16
1239 CRYPTOPP_DLL void* CRYPTOPP_API AlignedAllocate(size_t size);
1240 CRYPTOPP_DLL void CRYPTOPP_API AlignedDeallocate(void *ptr);
1241 #endif // CRYPTOPP_BOOL_ALIGN16
1242 
1245 CRYPTOPP_DLL void * CRYPTOPP_API UnalignedAllocate(size_t size);
1246 
1250 
1251 // ************** rotate functions ***************
1252 
1263 template <class T> inline T rotlFixed(T x, unsigned int y)
1264 {
1265  // Portable rotate that reduces to single instruction...
1266  // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=57157,
1267  // https://software.intel.com/en-us/forums/topic/580884
1268  // and https://llvm.org/bugs/show_bug.cgi?id=24226
1269  static const unsigned int THIS_SIZE = sizeof(T)*8;
1270  static const unsigned int MASK = THIS_SIZE-1;
1271  CRYPTOPP_ASSERT(y < THIS_SIZE);
1272  return T((x<<y)|(x>>(-y&MASK)));
1273 }
1274 
1285 template <class T> inline T rotrFixed(T x, unsigned int y)
1286 {
1287  // Portable rotate that reduces to single instruction...
1288  // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=57157,
1289  // https://software.intel.com/en-us/forums/topic/580884
1290  // and https://llvm.org/bugs/show_bug.cgi?id=24226
1291  static const unsigned int THIS_SIZE = sizeof(T)*8;
1292  static const unsigned int MASK = THIS_SIZE-1;
1293  CRYPTOPP_ASSERT(y < THIS_SIZE);
1294  return T((x >> y)|(x<<(-y&MASK)));
1295 }
1296 
1307 template <class T> inline T rotlVariable(T x, unsigned int y)
1308 {
1309  static const unsigned int THIS_SIZE = sizeof(T)*8;
1310  static const unsigned int MASK = THIS_SIZE-1;
1311  CRYPTOPP_ASSERT(y < THIS_SIZE);
1312  return T((x<<y)|(x>>(-y&MASK)));
1313 }
1314 
1325 template <class T> inline T rotrVariable(T x, unsigned int y)
1326 {
1327  static const unsigned int THIS_SIZE = sizeof(T)*8;
1328  static const unsigned int MASK = THIS_SIZE-1;
1329  CRYPTOPP_ASSERT(y < THIS_SIZE);
1330  return T((x>>y)|(x<<(-y&MASK)));
1331 }
1332 
1340 template <class T> inline T rotlMod(T x, unsigned int y)
1341 {
1342  static const unsigned int THIS_SIZE = sizeof(T)*8;
1343  static const unsigned int MASK = THIS_SIZE-1;
1344  return T((x<<(y&MASK))|(x>>(-y&MASK)));
1345 }
1346 
1354 template <class T> inline T rotrMod(T x, unsigned int y)
1355 {
1356  static const unsigned int THIS_SIZE = sizeof(T)*8;
1357  static const unsigned int MASK = THIS_SIZE-1;
1358  return T((x>>(y&MASK))|(x<<(-y&MASK)));
1359 }
1360 
1361 #ifdef _MSC_VER
1362 
1371 template<> inline word32 rotlFixed<word32>(word32 x, unsigned int y)
1372 {
1373  // Uses Microsoft <stdlib.h> call, bound to C/C++ language rules.
1374  CRYPTOPP_ASSERT(y < 8*sizeof(x));
1375  return y ? _lrotl(x, static_cast<byte>(y)) : x;
1376 }
1377 
1386 template<> inline word32 rotrFixed<word32>(word32 x, unsigned int y)
1387 {
1388  // Uses Microsoft <stdlib.h> call, bound to C/C++ language rules.
1389  CRYPTOPP_ASSERT(y < 8*sizeof(x));
1390  return y ? _lrotr(x, static_cast<byte>(y)) : x;
1391 }
1392 
1401 template<> inline word32 rotlVariable<word32>(word32 x, unsigned int y)
1402 {
1403  CRYPTOPP_ASSERT(y < 8*sizeof(x));
1404  return _lrotl(x, static_cast<byte>(y));
1405 }
1406 
1415 template<> inline word32 rotrVariable<word32>(word32 x, unsigned int y)
1416 {
1417  CRYPTOPP_ASSERT(y < 8*sizeof(x));
1418  return _lrotr(x, static_cast<byte>(y));
1419 }
1420 
1428 template<> inline word32 rotlMod<word32>(word32 x, unsigned int y)
1429 {
1430  y %= 8*sizeof(x);
1431  return _lrotl(x, static_cast<byte>(y));
1432 }
1433 
1441 template<> inline word32 rotrMod<word32>(word32 x, unsigned int y)
1442 {
1443  y %= 8*sizeof(x);
1444  return _lrotr(x, static_cast<byte>(y));
1445 }
1446 
1447 #endif // #ifdef _MSC_VER
1448 
1449 #if (_MSC_VER >= 1400) || (defined(_MSC_VER) && !defined(_DLL))
1450 // Intel C++ Compiler 10.0 calls a function instead of using the rotate instruction when using these instructions
1451 
1460 template<> inline word64 rotlFixed<word64>(word64 x, unsigned int y)
1461 {
1462  // Uses Microsoft <stdlib.h> call, bound to C/C++ language rules.
1463  CRYPTOPP_ASSERT(y < 8*sizeof(x));
1464  return y ? _rotl64(x, static_cast<byte>(y)) : x;
1465 }
1466 
1475 template<> inline word64 rotrFixed<word64>(word64 x, unsigned int y)
1476 {
1477  // Uses Microsoft <stdlib.h> call, bound to C/C++ language rules.
1478  CRYPTOPP_ASSERT(y < 8*sizeof(x));
1479  return y ? _rotr64(x, static_cast<byte>(y)) : x;
1480 }
1481 
1490 template<> inline word64 rotlVariable<word64>(word64 x, unsigned int y)
1491 {
1492  CRYPTOPP_ASSERT(y < 8*sizeof(x));
1493  return _rotl64(x, static_cast<byte>(y));
1494 }
1495 
1504 template<> inline word64 rotrVariable<word64>(word64 x, unsigned int y)
1505 {
1506  CRYPTOPP_ASSERT(y < 8*sizeof(x));
1507  return y ? _rotr64(x, static_cast<byte>(y)) : x;
1508 }
1509 
1517 template<> inline word64 rotlMod<word64>(word64 x, unsigned int y)
1518 {
1519  CRYPTOPP_ASSERT(y < 8*sizeof(x));
1520  return y ? _rotl64(x, static_cast<byte>(y)) : x;
1521 }
1522 
1530 template<> inline word64 rotrMod<word64>(word64 x, unsigned int y)
1531 {
1532  CRYPTOPP_ASSERT(y < 8*sizeof(x));
1533  return y ? _rotr64(x, static_cast<byte>(y)) : x;
1534 }
1535 
1536 #endif // #if _MSC_VER >= 1310
1537 
1538 #if _MSC_VER >= 1400 && !defined(__INTEL_COMPILER)
1539 // Intel C++ Compiler 10.0 gives undefined externals with these
1540 
1541 template<> inline word16 rotlFixed<word16>(word16 x, unsigned int y)
1542 {
1543  // Intrinsic, not bound to C/C++ language rules.
1544  return _rotl16(x, static_cast<byte>(y));
1545 }
1546 
1547 template<> inline word16 rotrFixed<word16>(word16 x, unsigned int y)
1548 {
1549  // Intrinsic, not bound to C/C++ language rules.
1550  return _rotr16(x, static_cast<byte>(y));
1551 }
1552 
1553 template<> inline word16 rotlVariable<word16>(word16 x, unsigned int y)
1554 {
1555  return _rotl16(x, static_cast<byte>(y));
1556 }
1557 
1558 template<> inline word16 rotrVariable<word16>(word16 x, unsigned int y)
1559 {
1560  return _rotr16(x, static_cast<byte>(y));
1561 }
1562 
1563 template<> inline word16 rotlMod<word16>(word16 x, unsigned int y)
1564 {
1565  return _rotl16(x, static_cast<byte>(y));
1566 }
1567 
1568 template<> inline word16 rotrMod<word16>(word16 x, unsigned int y)
1569 {
1570  return _rotr16(x, static_cast<byte>(y));
1571 }
1572 
1573 template<> inline byte rotlFixed<byte>(byte x, unsigned int y)
1574 {
1575  // Intrinsic, not bound to C/C++ language rules.
1576  return _rotl8(x, static_cast<byte>(y));
1577 }
1578 
1579 template<> inline byte rotrFixed<byte>(byte x, unsigned int y)
1580 {
1581  // Intrinsic, not bound to C/C++ language rules.
1582  return _rotr8(x, static_cast<byte>(y));
1583 }
1584 
1585 template<> inline byte rotlVariable<byte>(byte x, unsigned int y)
1586 {
1587  return _rotl8(x, static_cast<byte>(y));
1588 }
1589 
1590 template<> inline byte rotrVariable<byte>(byte x, unsigned int y)
1591 {
1592  return _rotr8(x, static_cast<byte>(y));
1593 }
1594 
1595 template<> inline byte rotlMod<byte>(byte x, unsigned int y)
1596 {
1597  return _rotl8(x, static_cast<byte>(y));
1598 }
1599 
1600 template<> inline byte rotrMod<byte>(byte x, unsigned int y)
1601 {
1602  return _rotr8(x, static_cast<byte>(y));
1603 }
1604 
1605 #endif // #if _MSC_VER >= 1400
1606 
1607 #if (defined(__MWERKS__) && TARGET_CPU_PPC)
1608 
1609 template<> inline word32 rotlFixed<word32>(word32 x, unsigned int y)
1610 {
1611  CRYPTOPP_ASSERT(y < 32);
1612  return y ? __rlwinm(x,y,0,31) : x;
1613 }
1614 
1615 template<> inline word32 rotrFixed<word32>(word32 x, unsigned int y)
1616 {
1617  CRYPTOPP_ASSERT(y < 32);
1618  return y ? __rlwinm(x,32-y,0,31) : x;
1619 }
1620 
1621 template<> inline word32 rotlVariable<word32>(word32 x, unsigned int y)
1622 {
1623  CRYPTOPP_ASSERT(y < 32);
1624  return (__rlwnm(x,y,0,31));
1625 }
1626 
1627 template<> inline word32 rotrVariable<word32>(word32 x, unsigned int y)
1628 {
1629  CRYPTOPP_ASSERT(y < 32);
1630  return (__rlwnm(x,32-y,0,31));
1631 }
1632 
1633 template<> inline word32 rotlMod<word32>(word32 x, unsigned int y)
1634 {
1635  return (__rlwnm(x,y,0,31));
1636 }
1637 
1638 template<> inline word32 rotrMod<word32>(word32 x, unsigned int y)
1639 {
1640  return (__rlwnm(x,32-y,0,31));
1641 }
1642 
1643 #endif // #if (defined(__MWERKS__) && TARGET_CPU_PPC)
1644 
1645 // ************** endian reversal ***************
1646 
1651 template <class T>
1652 inline unsigned int GetByte(ByteOrder order, T value, unsigned int index)
1653 {
1654  if (order == LITTLE_ENDIAN_ORDER)
1655  return GETBYTE(value, index);
1656  else
1657  return GETBYTE(value, sizeof(T)-index-1);
1658 }
1659 
1663 inline byte ByteReverse(byte value)
1664 {
1665  return value;
1666 }
1667 
1673 {
1674 #ifdef CRYPTOPP_BYTESWAP_AVAILABLE
1675  return bswap_16(value);
1676 #elif (_MSC_VER >= 1400) || (defined(_MSC_VER) && !defined(_DLL))
1677  return _byteswap_ushort(value);
1678 #else
1679  return rotlFixed(value, 8U);
1680 #endif
1681 }
1682 
1688 {
1689 #if defined(__GNUC__) && defined(CRYPTOPP_X86_ASM_AVAILABLE)
1690  __asm__ ("bswap %0" : "=r" (value) : "0" (value));
1691  return value;
1692 #elif defined(CRYPTOPP_BYTESWAP_AVAILABLE)
1693  return bswap_32(value);
1694 #elif defined(__MWERKS__) && TARGET_CPU_PPC
1695  return (word32)__lwbrx(&value,0);
1696 #elif (_MSC_VER >= 1400) || (defined(_MSC_VER) && !defined(_DLL))
1697  return _byteswap_ulong(value);
1698 #elif CRYPTOPP_FAST_ROTATE(32)
1699  // 5 instructions with rotate instruction, 9 without
1700  return (rotrFixed(value, 8U) & 0xff00ff00) | (rotlFixed(value, 8U) & 0x00ff00ff);
1701 #else
1702  // 6 instructions with rotate instruction, 8 without
1703  value = ((value & 0xFF00FF00) >> 8) | ((value & 0x00FF00FF) << 8);
1704  return rotlFixed(value, 16U);
1705 #endif
1706 }
1707 
1713 {
1714 #if defined(__GNUC__) && defined(CRYPTOPP_X86_ASM_AVAILABLE) && defined(__x86_64__)
1715  __asm__ ("bswap %0" : "=r" (value) : "0" (value));
1716  return value;
1717 #elif defined(CRYPTOPP_BYTESWAP_AVAILABLE)
1718  return bswap_64(value);
1719 #elif (_MSC_VER >= 1400) || (defined(_MSC_VER) && !defined(_DLL))
1720  return _byteswap_uint64(value);
1721 #elif CRYPTOPP_BOOL_SLOW_WORD64
1722  return (word64(ByteReverse(word32(value))) << 32) | ByteReverse(word32(value>>32));
1723 #else
1724  value = ((value & W64LIT(0xFF00FF00FF00FF00)) >> 8) | ((value & W64LIT(0x00FF00FF00FF00FF)) << 8);
1725  value = ((value & W64LIT(0xFFFF0000FFFF0000)) >> 16) | ((value & W64LIT(0x0000FFFF0000FFFF)) << 16);
1726  return rotlFixed(value, 32U);
1727 #endif
1728 }
1729 
1733 inline byte BitReverse(byte value)
1734 {
1735  value = byte((value & 0xAA) >> 1) | byte((value & 0x55) << 1);
1736  value = byte((value & 0xCC) >> 2) | byte((value & 0x33) << 2);
1737  return rotlFixed(value, 4U);
1738 }
1739 
1743 inline word16 BitReverse(word16 value)
1744 {
1745  value = word16((value & 0xAAAA) >> 1) | word16((value & 0x5555) << 1);
1746  value = word16((value & 0xCCCC) >> 2) | word16((value & 0x3333) << 2);
1747  value = word16((value & 0xF0F0) >> 4) | word16((value & 0x0F0F) << 4);
1748  return ByteReverse(value);
1749 }
1750 
1754 inline word32 BitReverse(word32 value)
1755 {
1756  value = word32((value & 0xAAAAAAAA) >> 1) | word32((value & 0x55555555) << 1);
1757  value = word32((value & 0xCCCCCCCC) >> 2) | word32((value & 0x33333333) << 2);
1758  value = word32((value & 0xF0F0F0F0) >> 4) | word32((value & 0x0F0F0F0F) << 4);
1759  return ByteReverse(value);
1760 }
1761 
1765 inline word64 BitReverse(word64 value)
1766 {
1767 #if CRYPTOPP_BOOL_SLOW_WORD64
1768  return (word64(BitReverse(word32(value))) << 32) | BitReverse(word32(value>>32));
1769 #else
1770  value = word64((value & W64LIT(0xAAAAAAAAAAAAAAAA)) >> 1) | word64((value & W64LIT(0x5555555555555555)) << 1);
1771  value = word64((value & W64LIT(0xCCCCCCCCCCCCCCCC)) >> 2) | word64((value & W64LIT(0x3333333333333333)) << 2);
1772  value = word64((value & W64LIT(0xF0F0F0F0F0F0F0F0)) >> 4) | word64((value & W64LIT(0x0F0F0F0F0F0F0F0F)) << 4);
1773  return ByteReverse(value);
1774 #endif
1775 }
1776 
1783 template <class T>
1784 inline T BitReverse(T value)
1785 {
1786  if (sizeof(T) == 1)
1787  return (T)BitReverse((byte)value);
1788  else if (sizeof(T) == 2)
1789  return (T)BitReverse((word16)value);
1790  else if (sizeof(T) == 4)
1791  return (T)BitReverse((word32)value);
1792  else
1793  {
1794  CRYPTOPP_ASSERT(sizeof(T) == 8);
1795  return (T)BitReverse((word64)value);
1796  }
1797 }
1798 
1806 template <class T>
1807 inline T ConditionalByteReverse(ByteOrder order, T value)
1808 {
1809  return NativeByteOrderIs(order) ? value : ByteReverse(value);
1810 }
1811 
1847 template <class T>
1848 void ByteReverse(T *out, const T *in, size_t byteCount)
1849 {
1850  CRYPTOPP_ASSERT(byteCount % sizeof(T) == 0);
1851  size_t count = byteCount/sizeof(T);
1852  for (size_t i=0; i<count; i++)
1853  out[i] = ByteReverse(in[i]);
1854 }
1855 
1869 template <class T>
1870 inline void ConditionalByteReverse(ByteOrder order, T *out, const T *in, size_t byteCount)
1871 {
1872  if (!NativeByteOrderIs(order))
1873  ByteReverse(out, in, byteCount);
1874  else if (in != out)
1875  memcpy_s(out, byteCount, in, byteCount);
1876 }
1877 
1878 template <class T>
1879 inline void GetUserKey(ByteOrder order, T *out, size_t outlen, const byte *in, size_t inlen)
1880 {
1881  const size_t U = sizeof(T);
1882  CRYPTOPP_ASSERT(inlen <= outlen*U);
1883  memcpy_s(out, outlen*U, in, inlen);
1884  memset_z((byte *)out+inlen, 0, outlen*U-inlen);
1885  ConditionalByteReverse(order, out, out, RoundUpToMultipleOf(inlen, U));
1886 }
1887 
1888 #ifndef CRYPTOPP_ALLOW_UNALIGNED_DATA_ACCESS
1889 inline byte UnalignedGetWordNonTemplate(ByteOrder order, const byte *block, const byte *)
1890 {
1891  CRYPTOPP_UNUSED(order);
1892  return block[0];
1893 }
1894 
1895 inline word16 UnalignedGetWordNonTemplate(ByteOrder order, const byte *block, const word16 *)
1896 {
1897  return (order == BIG_ENDIAN_ORDER)
1898  ? block[1] | (block[0] << 8)
1899  : block[0] | (block[1] << 8);
1900 }
1901 
1902 inline word32 UnalignedGetWordNonTemplate(ByteOrder order, const byte *block, const word32 *)
1903 {
1904  return (order == BIG_ENDIAN_ORDER)
1905  ? word32(block[3]) | (word32(block[2]) << 8) | (word32(block[1]) << 16) | (word32(block[0]) << 24)
1906  : word32(block[0]) | (word32(block[1]) << 8) | (word32(block[2]) << 16) | (word32(block[3]) << 24);
1907 }
1908 
1909 inline word64 UnalignedGetWordNonTemplate(ByteOrder order, const byte *block, const word64 *)
1910 {
1911  return (order == BIG_ENDIAN_ORDER)
1912  ?
1913  (word64(block[7]) |
1914  (word64(block[6]) << 8) |
1915  (word64(block[5]) << 16) |
1916  (word64(block[4]) << 24) |
1917  (word64(block[3]) << 32) |
1918  (word64(block[2]) << 40) |
1919  (word64(block[1]) << 48) |
1920  (word64(block[0]) << 56))
1921  :
1922  (word64(block[0]) |
1923  (word64(block[1]) << 8) |
1924  (word64(block[2]) << 16) |
1925  (word64(block[3]) << 24) |
1926  (word64(block[4]) << 32) |
1927  (word64(block[5]) << 40) |
1928  (word64(block[6]) << 48) |
1929  (word64(block[7]) << 56));
1930 }
1931 
1932 inline void UnalignedbyteNonTemplate(ByteOrder order, byte *block, byte value, const byte *xorBlock)
1933 {
1934  CRYPTOPP_UNUSED(order);
1935  block[0] = (byte)(xorBlock ? (value ^ xorBlock[0]) : value);
1936 }
1937 
1938 inline void UnalignedbyteNonTemplate(ByteOrder order, byte *block, word16 value, const byte *xorBlock)
1939 {
1940  if (order == BIG_ENDIAN_ORDER)
1941  {
1942  if (xorBlock)
1943  {
1944  block[0] = xorBlock[0] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 1);
1945  block[1] = xorBlock[1] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 0);
1946  }
1947  else
1948  {
1949  block[0] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 1);
1950  block[1] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 0);
1951  }
1952  }
1953  else
1954  {
1955  if (xorBlock)
1956  {
1957  block[0] = xorBlock[0] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 0);
1958  block[1] = xorBlock[1] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 1);
1959  }
1960  else
1961  {
1962  block[0] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 0);
1963  block[1] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 1);
1964  }
1965  }
1966 }
1967 
1968 inline void UnalignedbyteNonTemplate(ByteOrder order, byte *block, word32 value, const byte *xorBlock)
1969 {
1970  if (order == BIG_ENDIAN_ORDER)
1971  {
1972  if (xorBlock)
1973  {
1974  block[0] = xorBlock[0] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 3);
1975  block[1] = xorBlock[1] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 2);
1976  block[2] = xorBlock[2] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 1);
1977  block[3] = xorBlock[3] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 0);
1978  }
1979  else
1980  {
1981  block[0] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 3);
1982  block[1] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 2);
1983  block[2] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 1);
1984  block[3] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 0);
1985  }
1986  }
1987  else
1988  {
1989  if (xorBlock)
1990  {
1991  block[0] = xorBlock[0] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 0);
1992  block[1] = xorBlock[1] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 1);
1993  block[2] = xorBlock[2] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 2);
1994  block[3] = xorBlock[3] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 3);
1995  }
1996  else
1997  {
1998  block[0] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 0);
1999  block[1] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 1);
2000  block[2] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 2);
2001  block[3] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 3);
2002  }
2003  }
2004 }
2005 
2006 inline void UnalignedbyteNonTemplate(ByteOrder order, byte *block, word64 value, const byte *xorBlock)
2007 {
2008  if (order == BIG_ENDIAN_ORDER)
2009  {
2010  if (xorBlock)
2011  {
2012  block[0] = xorBlock[0] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 7);
2013  block[1] = xorBlock[1] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 6);
2014  block[2] = xorBlock[2] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 5);
2015  block[3] = xorBlock[3] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 4);
2016  block[4] = xorBlock[4] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 3);
2017  block[5] = xorBlock[5] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 2);
2018  block[6] = xorBlock[6] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 1);
2019  block[7] = xorBlock[7] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 0);
2020  }
2021  else
2022  {
2023  block[0] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 7);
2024  block[1] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 6);
2025  block[2] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 5);
2026  block[3] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 4);
2027  block[4] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 3);
2028  block[5] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 2);
2029  block[6] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 1);
2030  block[7] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 0);
2031  }
2032  }
2033  else
2034  {
2035  if (xorBlock)
2036  {
2037  block[0] = xorBlock[0] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 0);
2038  block[1] = xorBlock[1] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 1);
2039  block[2] = xorBlock[2] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 2);
2040  block[3] = xorBlock[3] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 3);
2041  block[4] = xorBlock[4] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 4);
2042  block[5] = xorBlock[5] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 5);
2043  block[6] = xorBlock[6] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 6);
2044  block[7] = xorBlock[7] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 7);
2045  }
2046  else
2047  {
2048  block[0] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 0);
2049  block[1] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 1);
2050  block[2] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 2);
2051  block[3] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 3);
2052  block[4] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 4);
2053  block[5] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 5);
2054  block[6] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 6);
2055  block[7] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 7);
2056  }
2057  }
2058 }
2059 #endif // #ifndef CRYPTOPP_ALLOW_UNALIGNED_DATA_ACCESS
2060 
2077 template <class T>
2078 inline T GetWord(bool assumeAligned, ByteOrder order, const byte *block)
2079 {
2080  CRYPTOPP_UNUSED(assumeAligned);
2081 #ifdef CRYPTOPP_ALLOW_UNALIGNED_DATA_ACCESS
2082  return ConditionalByteReverse(order, *reinterpret_cast<const T *>((const void *)block));
2083 #else
2084  T temp;
2085  memcpy(&temp, block, sizeof(T));
2086  return ConditionalByteReverse(order, temp);
2087 #endif
2088 }
2089 
2106 template <class T>
2107 inline void GetWord(bool assumeAligned, ByteOrder order, T &result, const byte *block)
2108 {
2109  result = GetWord<T>(assumeAligned, order, block);
2110 }
2111 
2122 template <class T>
2123 inline void PutWord(bool assumeAligned, ByteOrder order, byte *block, T value, const byte *xorBlock = NULL)
2124 {
2125  CRYPTOPP_UNUSED(assumeAligned);
2126 #ifdef CRYPTOPP_ALLOW_UNALIGNED_DATA_ACCESS
2127  *reinterpret_cast<T *>((void *)block) = ConditionalByteReverse(order, value) ^ (xorBlock ? *reinterpret_cast<const T *>((const void *)xorBlock) : 0);
2128 #else
2129  T t1, t2;
2130  t1 = ConditionalByteReverse(order, value);
2131  if (xorBlock) {memcpy(&t2, xorBlock, sizeof(T)); t1 ^= t2;}
2132  memcpy(block, &t1, sizeof(T));
2133 #endif
2134 }
2135 
2152 template <class T, class B, bool A=false>
2154 {
2155 public:
2158  GetBlock(const void *block)
2159  : m_block((const byte *)block) {}
2160 
2165  template <class U>
2167  {
2168  CRYPTOPP_COMPILE_ASSERT(sizeof(U) >= sizeof(T));
2169  x = GetWord<T>(A, B::ToEnum(), m_block);
2170  m_block += sizeof(T);
2171  return *this;
2172  }
2173 
2174 private:
2175  const byte *m_block;
2176 };
2177 
2194 template <class T, class B, bool A=false>
2196 {
2197 public:
2201  PutBlock(const void *xorBlock, void *block)
2202  : m_xorBlock((const byte *)xorBlock), m_block((byte *)block) {}
2203 
2208  template <class U>
2210  {
2211  PutWord(A, B::ToEnum(), m_block, (T)x, m_xorBlock);
2212  m_block += sizeof(T);
2213  if (m_xorBlock)
2214  m_xorBlock += sizeof(T);
2215  return *this;
2216  }
2217 
2218 private:
2221 };
2222 
2232 template <class T, class B, bool GA=false, bool PA=false>
2234 {
2235  // function needed because of C++ grammatical ambiguity between expression-statements and declarations
2236  static inline GetBlock<T, B, GA> Get(const void *block) {return GetBlock<T, B, GA>(block);}
2238 };
2239 
2240 template <class T>
2241 std::string WordToString(T value, ByteOrder order = BIG_ENDIAN_ORDER)
2242 {
2243  if (!NativeByteOrderIs(order))
2244  value = ByteReverse(value);
2245 
2246  return std::string((char *)&value, sizeof(value));
2247 }
2248 
2249 template <class T>
2250 T StringToWord(const std::string &str, ByteOrder order = BIG_ENDIAN_ORDER)
2251 {
2252  T value = 0;
2253  memcpy_s(&value, sizeof(value), str.data(), UnsignedMin(str.size(), sizeof(value)));
2254  return NativeByteOrderIs(order) ? value : ByteReverse(value);
2255 }
2256 
2257 // ************** help remove warning on g++ ***************
2258 
2266 template <bool overflow> struct SafeShifter;
2267 
2272 template<> struct SafeShifter<true>
2273 {
2279  template <class T>
2280  static inline T RightShift(T value, unsigned int bits)
2281  {
2282  CRYPTOPP_UNUSED(value); CRYPTOPP_UNUSED(bits);
2283  return 0;
2284  }
2285 
2291  template <class T>
2292  static inline T LeftShift(T value, unsigned int bits)
2293  {
2294  CRYPTOPP_UNUSED(value); CRYPTOPP_UNUSED(bits);
2295  return 0;
2296  }
2297 };
2298 
2303 template<> struct SafeShifter<false>
2304 {
2310  template <class T>
2311  static inline T RightShift(T value, unsigned int bits)
2312  {
2313  return value >> bits;
2314  }
2315 
2321  template <class T>
2322  static inline T LeftShift(T value, unsigned int bits)
2323  {
2324  return value << bits;
2325  }
2326 };
2327 
2337 template <unsigned int bits, class T>
2338 inline T SafeRightShift(T value)
2339 {
2340  return SafeShifter<(bits>=(8*sizeof(T)))>::RightShift(value, bits);
2341 }
2342 
2352 template <unsigned int bits, class T>
2353 inline T SafeLeftShift(T value)
2354 {
2355  return SafeShifter<(bits>=(8*sizeof(T)))>::LeftShift(value, bits);
2356 }
2357 
2358 // ************** use one buffer for multiple data members ***************
2359 
2360 #define CRYPTOPP_BLOCK_1(n, t, s) t* m_##n() {return (t *)(void *)(m_aggregate+0);} size_t SS1() {return sizeof(t)*(s);} size_t m_##n##Size() {return (s);}
2361 #define CRYPTOPP_BLOCK_2(n, t, s) t* m_##n() {return (t *)(void *)(m_aggregate+SS1());} size_t SS2() {return SS1()+sizeof(t)*(s);} size_t m_##n##Size() {return (s);}
2362 #define CRYPTOPP_BLOCK_3(n, t, s) t* m_##n() {return (t *)(void *)(m_aggregate+SS2());} size_t SS3() {return SS2()+sizeof(t)*(s);} size_t m_##n##Size() {return (s);}
2363 #define CRYPTOPP_BLOCK_4(n, t, s) t* m_##n() {return (t *)(void *)(m_aggregate+SS3());} size_t SS4() {return SS3()+sizeof(t)*(s);} size_t m_##n##Size() {return (s);}
2364 #define CRYPTOPP_BLOCK_5(n, t, s) t* m_##n() {return (t *)(void *)(m_aggregate+SS4());} size_t SS5() {return SS4()+sizeof(t)*(s);} size_t m_##n##Size() {return (s);}
2365 #define CRYPTOPP_BLOCK_6(n, t, s) t* m_##n() {return (t *)(void *)(m_aggregate+SS5());} size_t SS6() {return SS5()+sizeof(t)*(s);} size_t m_##n##Size() {return (s);}
2366 #define CRYPTOPP_BLOCK_7(n, t, s) t* m_##n() {return (t *)(void *)(m_aggregate+SS6());} size_t SS7() {return SS6()+sizeof(t)*(s);} size_t m_##n##Size() {return (s);}
2367 #define CRYPTOPP_BLOCK_8(n, t, s) t* m_##n() {return (t *)(void *)(m_aggregate+SS7());} size_t SS8() {return SS7()+sizeof(t)*(s);} size_t m_##n##Size() {return (s);}
2368 #define CRYPTOPP_BLOCKS_END(i) size_t SST() {return SS##i();} void AllocateBlocks() {m_aggregate.New(SST());} AlignedSecByteBlock m_aggregate;
2369 
2371 
2372 #if (CRYPTOPP_MSC_VERSION)
2373 # pragma warning(pop)
2374 #endif
2375 
2376 #if CRYPTOPP_GCC_DIAGNOSTIC_AVAILABLE
2377 # pragma GCC diagnostic pop
2378 #endif
2379 
2380 #endif
void SecureWipeBuffer(T *buf, size_t n)
Sets each element of an array to 0.
Definition: misc.h:1085
#define MEMORY_BARRIER()
Definition: misc.h:256
#define T1
Definition: integer.cpp:2170
the cipher is performing decryption
Definition: cryptlib.h:108
An invalid argument was detected.
Definition: cryptlib.h:184
CRYPTOPP_DLL std::string IntToString< Integer >(Integer value, unsigned int base)
Converts an Integer to a string.
Definition: integer.cpp:4648
void GetUserKey(ByteOrder order, T *out, size_t outlen, const byte *in, size_t inlen)
Definition: misc.h:1879
std::string StringNarrow(const wchar_t *str, bool throwOnError=true)
Converts a wide character C-string to a multibyte string.
Definition: misc.cpp:136
bool NativeByteOrderIs(ByteOrder order)
Determines whether order follows native byte ordering.
Definition: misc.h:993
void memmove_s(void *dest, size_t sizeInBytes, const void *src, size_t count)
Bounds checking replacement for memmove()
Definition: misc.h:408
uint8_t byte
Definition: Common.h:57
bool SafeConvert(T1 from, T2 &to)
Tests whether a conversion from -> to is safe to perform.
Definition: misc.h:526
#define T2
Definition: integer.cpp:2171
ByteOrder
Provides the byte ordering.
Definition: cryptlib.h:124
Restricts the instantiation of a class to one static object without locks.
Definition: misc.h:274
PutBlock< T, B, PA > Put
Definition: misc.h:2237
void IncrementCounterByOne(byte *inout, unsigned int size)
Performs an addition with carry on a block of bytes.
Definition: misc.h:1029
T2 ModPowerOf2(const T1 &a, const T2 &b)
Reduces a value to a power of 2.
Definition: misc.h:875
T SafeRightShift(T value)
Definition: misc.h:2338
unsigned short word16
Definition: config.h:230
size_t BitsToBytes(size_t bitCount)
Returns the number of 8-bit bytes or octets required for the specified number of bits.
Definition: misc.h:749
T StringToWord(const std::string &str, ByteOrder order=BIG_ENDIAN_ORDER)
Definition: misc.h:2250
bool IsAligned(const void *ptr)
Determines whether ptr is minimally aligned.
Definition: misc.h:964
T rotlFixed(T x, unsigned int y)
Performs a left rotate.
Definition: misc.h:1263
size_t BitsToWords(size_t bitCount)
Returns the number of words required for the specified number of bits.
Definition: misc.h:769
#define T(i, x)
void PutWord(bool assumeAligned, ByteOrder order, byte *block, T value, const byte *xorBlock=NULL)
Access a block of memory.
Definition: misc.h:2123
unsigned int BytePrecision(const T &value)
Returns the number of 8-bit bytes or octets required for a value.
Definition: misc.h:632
#define NAMESPACE_BEGIN(x)
Definition: config.h:200
#define h(i)
Definition: sha.cpp:736
PutBlock(const void *xorBlock, void *block)
Construct a PutBlock.
Definition: misc.h:2201
Converts an enumeration to a type suitable for use as a template parameter.
Definition: cryptlib.h:116
CipherDir
Specifies a direction for a cipher to operate.
Definition: cryptlib.h:104
size_t count
Definition: ExecStats.cpp:37
void UnalignedbyteNonTemplate(ByteOrder order, byte *block, byte value, const byte *xorBlock)
Definition: misc.h:1932
Abstract base classes that provide a uniform interface to this library.
static GetBlock< T, B, GA > Get(const void *block)
Definition: misc.h:2236
void memcpy_s(void *dest, size_t sizeInBytes, const void *src, size_t count)
Bounds checking replacement for memcpy()
Definition: misc.h:366
unsigned int GetAlignmentOf()
Returns the minimum alignment requirements of a type.
Definition: misc.h:922
static T LeftShift(T value, unsigned int bits)
Left shifts a value that does not overflow.
Definition: misc.h:2322
An object factory function.
Definition: misc.h:230
Classes for automatic resource management.
#define c(i)
Library configuration file.
size_t BytesToWords(size_t byteCount)
Returns the number of words required for the specified number of bytes.
Definition: misc.h:759
std::string WordToString(T value, ByteOrder order=BIG_ENDIAN_ORDER)
Definition: misc.h:2241
T rotlVariable(T x, unsigned int y)
Performs a left rotate.
Definition: misc.h:1307
Access a block of memory.
Definition: misc.h:2233
const byte * m_block
Definition: misc.h:2175
NotCopyable()
Definition: misc.h:220
byte BitReverse(byte value)
Reverses bits in a 8-bit value.
Definition: misc.h:1733
byte order is little-endian
Definition: cryptlib.h:126
the cipher is performing encryption
Definition: cryptlib.h:106
uint32_t bswap_32(uint32_t x)
Definition: byteswap.h:43
T GetWord(bool assumeAligned, ByteOrder order, const byte *block)
Access a block of memory.
Definition: misc.h:2078
LittleEndian NativeByteOrder
Definition: misc.h:970
void SecureWipeArray(T *buf, size_t n)
Sets each element of an array to 0.
Definition: misc.h:1192
bool IsAlignedOn(const void *ptr, unsigned int alignment)
Determines whether ptr is aligned to a minimum value.
Definition: misc.h:954
#define a(i)
Uses encapsulation to hide an object in derived classes.
Definition: misc.h:205
#define x(i)
const unsigned int WORD_BITS
Definition: config.h:317
#define CRYPTOPP_COMPILE_ASSERT(assertion)
Definition: misc.h:139
CRYPTOPP_DLL bool CRYPTOPP_API VerifyBufsEqual(const byte *buf1, const byte *buf2, size_t count)
Performs a near constant-time comparison of two equally sized buffers.
Definition: misc.cpp:96
CRYPTOPP_DLL void *CRYPTOPP_API UnalignedAllocate(size_t size)
Allocates a buffer.
Definition: misc.cpp:257
Manages resources for a single object.
Definition: smartptr.h:20
unsigned int TrailingZeros(word32 v)
Determines the number of trailing 0-bits in a value.
Definition: misc.h:679
T Crop(T value, size_t bits)
Truncates the value to the specified number of bits.
Definition: misc.h:737
T ConditionalByteReverse(ByteOrder order, T value)
Reverses bytes in a value depending upon endianness.
Definition: misc.h:1807
#define W64LIT(x)
Definition: config.h:241
CRYPTOPP_DLL std::string IntToString< word64 >(word64 value, unsigned int base)
Converts an unsigned value to a string.
Definition: integer.cpp:4714
T * m_p
Definition: smartptr.h:30
void ConditionalSwapPointers(bool c, T &a, T &b)
Performs a branchless swap of pointers a and b if condition c is true.
Definition: misc.h:1070
#define GETBYTE(x, y)
Definition: misc.h:610
CRYPTOPP_DLL void CRYPTOPP_API UnalignedDeallocate(void *ptr)
Frees a buffer allocated with UnalignedAllocate.
Definition: misc.cpp:265
Multiple precision integer with arithmetic operations.
Definition: integer.h:43
uint16_t bswap_16(uint16_t x)
Definition: byteswap.h:36
T1 SaturatingSubtract(const T1 &a, const T2 &b)
Performs a saturating subtract clamped at 0.
Definition: misc.h:847
CipherDir GetCipherDir(const T &obj)
Returns the direction the cipher is being operated.
Definition: misc.h:1008
#define CRYPTOPP_API
Definition: config.h:705
const T1 UnsignedMin(const T1 &a, const T2 &b)
Safe comparison of values that could be neagtive and incorrectly promoted.
Definition: misc.h:512
#define t1
#define CRYPTOPP_NOINLINE_DOTDOTDOT
Definition: config.h:532
unsigned long long word64
Definition: config.h:240
bool IsPowerOf2(const T &value)
Tests whether a value is a power of 2.
Definition: misc.h:818
byte UnalignedGetWordNonTemplate(ByteOrder order, const byte *block, const byte *)
Definition: misc.h:1889
static T RightShift(T value, unsigned int bits)
Right shifts a value that does not overflow.
Definition: misc.h:2311
#define F(x, y, z)
Definition: Hash.cpp:79
Definition: misc.h:190
static char dummy[2 *b-1]
Definition: misc.h:135
T m_object
Definition: misc.h:208
void * memset_z(void *ptr, int value, size_t num)
Memory block initializer and eraser that attempts to survive optimizations.
Definition: misc.h:461
unsigned int Parity(T value)
Returns the parity of a value.
Definition: misc.h:621
byte order is big-endian
Definition: cryptlib.h:128
const unsigned int WORD_SIZE
Definition: config.h:316
T * operator()() const
Definition: misc.h:232
#define b(i, j)
const T & STDMIN(const T &a, const T &b)
Replacement function for std::min.
Definition: misc.h:477
#define CRYPTOPP_ASSERT(exp)
Definition: trap.h:92
#define t2
CRYPTOPP_NOINLINE const T & Ref(CRYPTOPP_NOINLINE_DOTDOTDOT) const
Return a reference to the inner Singleton object.
Definition: misc.h:318
#define CRYPTOPP_NO_VTABLE
Definition: config.h:369
CRYPTOPP_DLL void CRYPTOPP_API CallNewHandler()
Attempts to reclaim unused memory.
Definition: misc.cpp:200
PutBlock< T, B, A > & operator()(U x)
Access a block of memory.
Definition: misc.h:2209
#define CRYPTOPP_NOINLINE
Definition: config.h:533
void ConditionalSwap(bool c, T &a, T &b)
Performs a branchless swap of values a and b if condition c is true.
Definition: misc.h:1058
T1 SaturatingSubtract1(const T1 &a, const T2 &b)
Performs a saturating subtract clamped at 1.
Definition: misc.h:862
GetBlock(const void *block)
Construct a GetBlock.
Definition: misc.h:2158
T SafeLeftShift(T value)
Definition: misc.h:2353
F m_objectFactory
Definition: misc.h:283
uint64_t bswap_64(uint64_t x)
Definition: byteswap.h:51
static T RightShift(T value, unsigned int bits)
Right shifts a value that overflows.
Definition: misc.h:2280
uint8_t const size_t const size
Definition: sha3.h:20
void * memcpy(void *a, const void *b, size_t c)
#define CRYPTOPP_UNUSED(x)
Definition: config.h:741
uint8_t byte
Definition: Common.h:10
Access a block of memory.
Definition: misc.h:2153
std::string IntToString(T value, unsigned int base=10)
Converts a value to a string.
Definition: misc.h:539
CRYPTOPP_DLL void CRYPTOPP_API xorbuf(byte *buf, const byte *mask, size_t count)
Performs an XOR of a buffer with a mask.
Definition: misc.cpp:28
GetBlock< T, B, A > & operator()(U &x)
Access a block of memory.
Definition: misc.h:2166
An Empty class.
Definition: misc.h:184
ByteOrder GetNativeByteOrder()
Returns NativeByteOrder as an enumerated ByteOrder value.
Definition: misc.h:985
T1 RoundUpToMultipleOf(const T1 &n, const T2 &m)
Rounds a value up to a multiple of a second value.
Definition: misc.h:905
Safely shift values when undefined behavior could occur.
Definition: misc.h:2266
const T & STDMAX(const T &a, const T &b)
Replacement function for std::max.
Definition: misc.h:487
#define NAMESPACE_END
Definition: config.h:201
Access a block of memory.
Definition: misc.h:2195
T rotrVariable(T x, unsigned int y)
Performs a right rotate.
Definition: misc.h:1325
const byte * m_xorBlock
Definition: misc.h:2219
#define CRYPTOPP_GET_BYTE_AS_BYTE(x, y)
Definition: misc.h:615
#define CRYPTOPP_DLL
Definition: config.h:704
unsigned int word32
Definition: config.h:231
T rotlMod(T x, unsigned int y)
Performs a left rotate.
Definition: misc.h:1340
byte * m_block
Definition: misc.h:2220
unsigned int GetByte(ByteOrder order, T value, unsigned int index)
Gets a byte from a value.
Definition: misc.h:1652
T rotrMod(T x, unsigned int y)
Performs a right rotate.
Definition: misc.h:1354
T rotrFixed(T x, unsigned int y)
Performs a right rotate.
Definition: misc.h:1285
Ensures an object is not copyable.
Definition: misc.h:217
void * memmove(void *a, const void *b, size_t c)
Singleton(F objectFactory=F())
Definition: misc.h:277
byte ByteReverse(byte value)
Reverses bytes in a 8-bit value.
Definition: misc.h:1663
static ENUM_TYPE ToEnum()
Definition: cryptlib.h:118
unsigned int BitPrecision(const T &value)
Returns the number of bits required for a value.
Definition: misc.h:654
size_t BitsToDwords(size_t bitCount)
Returns the number of double words required for the specified number of bits.
Definition: misc.h:779
static T LeftShift(T value, unsigned int bits)
Left shifts a value that overflows.
Definition: misc.h:2292
T1 RoundDownToMultipleOf(const T1 &n, const T2 &m)
Rounds a value down to a multiple of a second value.
Definition: misc.h:889
#define SIZE_MAX
Definition: misc.h:113
void vec_swap(T &a, T &b)
Swaps two variables which are arrays.
Definition: misc.h:449