7 #ifndef CRYPTOPP_MISC_H 8 #define CRYPTOPP_MISC_H 12 #if !defined(CRYPTOPP_DOXYGEN_PROCESSING) 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) 23 #if CRYPTOPP_GCC_DIAGNOSTIC_AVAILABLE 24 # pragma GCC diagnostic push 25 # pragma GCC diagnostic ignored "-Wconversion" 26 # pragma GCC diagnostic ignored "-Wsign-conversion" 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 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) 49 #define CRYPTOPP_FAST_ROTATE(x) ((x) == 32) 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 57 #define CRYPTOPP_FAST_ROTATE(x) 0 65 #if defined(__GNUC__) && defined(__linux__) 66 #define CRYPTOPP_BYTESWAP_AVAILABLE 70 #if defined(__GNUC__) && defined(__BMI__) 71 # include <immintrin.h> 72 # if defined(__clang__) 74 # define _tzcnt_u32(x) __tzcnt_u32(x) 77 # define _blsr_u32(x) __blsr_u32(x) 81 # define _tzcnt_u64(x) __tzcnt_u64(x) 84 # define _blsr_u64(x) __blsr_u64(x) 88 #endif // GNUC and BMI 90 #endif // CRYPTOPP_DOXYGEN_PROCESSING 92 #if CRYPTOPP_DOXYGEN_PROCESSING 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 113 # define SIZE_MAX ((std::numeric_limits<size_t>::max)()) 117 #endif // CRYPTOPP_DOXYGEN_PROCESSING 126 #if CRYPTOPP_DOXYGEN_PROCESSING 127 #define CRYPTOPP_COMPILE_ASSERT(expr) { ... } 131 #else // CRYPTOPP_DOXYGEN_PROCESSING 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) 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)) 148 # define CRYPTOPP_COMPILE_ASSERT_INSTANCE(assertion, instance) \ 149 static CompileAssert<(assertion)> \ 150 CRYPTOPP_ASSERT_JOIN(cryptopp_CRYPTOPP_ASSERT_, instance) 153 #define CRYPTOPP_ASSERT_JOIN(X, Y) CRYPTOPP_DO_ASSERT_JOIN(X, Y) 154 #define CRYPTOPP_DO_ASSERT_JOIN(X, Y) X##Y 156 #endif // CRYPTOPP_DOXYGEN_PROCESSING 160 #if CRYPTOPP_DOXYGEN_PROCESSING 161 # define COUNTOF(arr) 172 # if defined(_MSC_VER) && (_MSC_VER >= 1400) 173 # define COUNTOF(x) _countof(x) 175 # define COUNTOF(x) (sizeof(x)/sizeof(x[0])) 178 #endif // CRYPTOPP_DOXYGEN_PROCESSING 188 #if !defined(CRYPTOPP_DOXYGEN_PROCESSING) 189 template <
class BASE1,
class BASE2>
194 template <
class BASE1,
class BASE2,
class BASE3>
198 #endif // CRYPTOPP_DOXYGEN_PROCESSING 235 #if CRYPTOPP_DOXYGEN_PROCESSING 236 #define MEMORY_BARRIER ... 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") 256 # define MEMORY_BARRIER() 258 #endif // CRYPTOPP_DOXYGEN_PROCESSING 273 template <
class T,
class F = NewObject<T>,
int instance=0>
277 Singleton(
F objectFactory =
F()) : m_objectFactory(objectFactory) {}
290 #if defined(CRYPTOPP_CXX11_ATOMICS) && defined(CRYPTOPP_CXX11_SYNCHRONIZATION) 291 template <
class T,
class F,
int instance>
294 static std::mutex s_mutex;
295 static std::atomic<T*> s_pObject;
297 T *p = s_pObject.load(std::memory_order_relaxed);
298 std::atomic_thread_fence(std::memory_order_acquire);
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);
310 T *newObject = m_objectFactory();
311 s_pObject.store(newObject, std::memory_order_relaxed);
312 std::atomic_thread_fence(std::memory_order_release);
317 template <
class T,
class F,
int instance>
321 T *p = s_pObject.
m_p;
327 T *newObject = m_objectFactory();
337 s_pObject.
m_p = newObject;
346 #if (!__STDC_WANT_SECURE_LIB__ && !defined(_MEMORY_S_DEFINED)) || defined(CRYPTOPP_WANT_SECURE_LIB) 366 inline void memcpy_s(
void *dest,
size_t sizeInBytes,
const void *src,
size_t count)
374 if (count > sizeInBytes)
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) 385 #if CRYPTOPP_MSC_VERSION 386 # pragma warning(pop) 408 inline void memmove_s(
void *dest,
size_t sizeInBytes,
const void *src,
size_t count)
416 if (count > sizeInBytes)
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) 427 #if CRYPTOPP_MSC_VERSION 428 # pragma warning(pop) 432 #if __BORLANDC__ >= 0x620 434 # define memcpy_s CryptoPP::memcpy_s 435 # define memmove_s CryptoPP::memmove_s 438 #endif // __STDC_WANT_SECURE_LIB__ 461 inline void *
memset_z(
void *ptr,
int value,
size_t num)
464 #if CRYPTOPP_GCC_VERSION >= 30001 465 if (__builtin_constant_p(num) && num==0)
468 volatile void*
x = memset(ptr, value, num);
469 return const_cast<void*
>(
x);
479 return b < a ? b :
a;
489 return a < b ? b :
a;
492 #if CRYPTOPP_MSC_VERSION 493 # pragma warning(push) 494 # pragma warning(disable: 4389) 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" 515 if (
sizeof(
T1)<=
sizeof(
T2))
516 return b < (
T2)a ? (
T1)b :
a;
518 return (
T1)b < a ? (
T1)b : a;
525 template <
class T1,
class T2>
529 if (from != to || (from > 0) != (to > 0))
542 static const unsigned int HIGH_BIT = (1U << 31);
543 const char CH = !!(base & HIGH_BIT) ?
'A' :
'a';
559 T digit = value % base;
560 result = char((digit < 10 ?
'0' : (CH - 10)) + digit) + result;
564 result =
"-" + result;
599 #if CRYPTOPP_MSC_VERSION 600 # pragma warning(pop) 603 #if CRYPTOPP_GCC_DIAGNOSTIC_AVAILABLE 604 # pragma GCC diagnostic pop 607 #define RETURN_IF_NONZERO(x) size_t returnedValue = x; if (returnedValue) return returnedValue 610 #define GETBYTE(x, y) (unsigned int)byte((x)>>(8*(y))) 615 #define CRYPTOPP_GET_BYTE_AS_BYTE(x, y) byte((x)>>(8*(y))) 623 for (
unsigned int i=8*
sizeof(value)/2; i>0; i/=2)
625 return (
unsigned int)value&1;
637 unsigned int l=0,
h=8*
sizeof(value);
640 unsigned int t = (l+
h)/2;
659 unsigned int l=0,
h=8*
sizeof(value);
663 unsigned int t = (l+
h)/2;
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;
695 static const int MultiplyDeBruijnBitPosition[32] =
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
700 return MultiplyDeBruijnBitPosition[((
word32)((v & -v) * 0x077CB531U)) >> 27];
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;
739 if (bits < 8*
sizeof(value))
740 return T(value & ((
T(1) << bits) - 1));
751 return ((bitCount+7)/(8));
820 return value > 0 && (value & (value-1)) == 0;
823 #if defined(__GNUC__) && defined(__BMI__) 825 inline bool IsPowerOf2<word32>(
const word32 &value)
827 return value > 0 && _blsr_u32(value) == 0;
830 # if defined(__x86_64__) 832 inline bool IsPowerOf2<word64>(
const word64 &value)
834 return value > 0 && _blsr_u64(value) == 0;
846 template <
class T1,
class T2>
850 return T1((a > b) ? (a - b) : 0);
861 template <
class T1,
class T2>
865 return T1((a > b) ? (a - b) : 1);
874 template <
class T1,
class T2>
888 template <
class T1,
class T2>
904 template <
class T1,
class T2>
925 #if defined(CRYPTOPP_ALLOW_UNALIGNED_DATA_ACCESS) 930 #if defined(CRYPTOPP_CXX11_ALIGNOF) 932 #elif (_MSC_VER >= 1300) 934 #elif defined(__GNUC__) 935 return __alignof__(
T);
936 #elif CRYPTOPP_BOOL_SLOW_WORD64 939 # if __BIGGEST_ALIGNMENT__ 940 if (__BIGGEST_ALIGNMENT__ <
sizeof(
T))
941 return __BIGGEST_ALIGNMENT__;
956 return alignment==1 || (
IsPowerOf2(alignment) ?
ModPowerOf2((
size_t)ptr, alignment) == 0 : (size_t)ptr % alignment == 0);
969 #if defined(IS_LITTLE_ENDIAN) 971 #elif defined(IS_BIG_ENDIAN) 974 # error "Unable to determine endian-ness" 1032 for (
int i=
int(size-1), carry=1; i>=0 && carry; i--)
1033 carry = !++inout[i];
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);
1072 ptrdiff_t t = size_t(c) * (a -
b);
1088 volatile T *p = buf+n;
1093 #if (_MSC_VER >= 1400 || defined(__GNUC__)) && (CRYPTOPP_BOOL_X64 || CRYPTOPP_BOOL_X86) 1101 volatile byte *p = buf;
1103 asm volatile(
"rep stosb" :
"+c"(n),
"+D"(p) :
"a"(0) :
"memory");
1105 __stosb((
byte *)(
size_t)p, 0, n);
1115 volatile word16 *p = buf;
1117 asm volatile(
"rep stosw" :
"+c"(n),
"+D"(p) :
"a"(0) :
"memory");
1119 __stosw((
word16 *)(
size_t)p, 0, n);
1129 volatile word32 *p = buf;
1131 asm volatile(
"rep stosl" :
"+c"(n),
"+D"(p) :
"a"(0) :
"memory");
1133 __stosd((
unsigned long *)(
size_t)p, 0, n);
1143 #if CRYPTOPP_BOOL_X64 1144 volatile word64 *p = buf;
1146 asm volatile(
"rep stosq" :
"+c"(n),
"+D"(p) :
"a"(0) :
"memory");
1148 __stosq((
word64 *)(
size_t)p, 0, n);
1155 #endif // #if (_MSC_VER >= 1400 || defined(__GNUC__)) && (CRYPTOPP_BOOL_X64 || CRYPTOPP_BOOL_X86) 1157 #if (_MSC_VER >= 1700) && defined(_M_ARM) 1160 char *p =
reinterpret_cast<char*
>(buf+n);
1162 __iso_volatile_store8(--p, 0);
1167 short *p =
reinterpret_cast<short*
>(buf+n);
1169 __iso_volatile_store16(--p, 0);
1174 int *p =
reinterpret_cast<int*
>(buf+n);
1176 __iso_volatile_store32(--p, 0);
1181 __int64 *p =
reinterpret_cast<__int64*
>(buf+n);
1183 __iso_volatile_store64(--p, 0);
1194 if (
sizeof(
T) % 8 == 0 && GetAlignmentOf<T>() % GetAlignmentOf<word64>() == 0)
1196 else if (
sizeof(
T) % 4 == 0 && GetAlignmentOf<T>() % GetAlignmentOf<word32>() == 0)
1198 else if (
sizeof(
T) % 2 == 0 && GetAlignmentOf<T>() % GetAlignmentOf<word16>() == 0)
1216 std::string
StringNarrow(
const wchar_t *str,
bool throwOnError =
true);
1218 #ifdef CRYPTOPP_DOXYGEN_PROCESSING 1236 #endif // CRYPTOPP_DOXYGEN_PROCESSING 1238 #if CRYPTOPP_BOOL_ALIGN16 1241 #endif // CRYPTOPP_BOOL_ALIGN16 1269 static const unsigned int THIS_SIZE =
sizeof(
T)*8;
1270 static const unsigned int MASK = THIS_SIZE-1;
1272 return T((x<<y)|(x>>(-y&MASK)));
1291 static const unsigned int THIS_SIZE =
sizeof(
T)*8;
1292 static const unsigned int MASK = THIS_SIZE-1;
1294 return T((x >> y)|(x<<(-y&MASK)));
1309 static const unsigned int THIS_SIZE =
sizeof(
T)*8;
1310 static const unsigned int MASK = THIS_SIZE-1;
1312 return T((x<<y)|(x>>(-y&MASK)));
1327 static const unsigned int THIS_SIZE =
sizeof(
T)*8;
1328 static const unsigned int MASK = THIS_SIZE-1;
1330 return T((x>>y)|(x<<(-y&MASK)));
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)));
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)));
1371 template<>
inline word32 rotlFixed<word32>(
word32 x,
unsigned int y)
1375 return y ? _lrotl(x, static_cast<byte>(y)) :
x;
1386 template<>
inline word32 rotrFixed<word32>(
word32 x,
unsigned int y)
1390 return y ? _lrotr(x, static_cast<byte>(y)) :
x;
1401 template<>
inline word32 rotlVariable<word32>(
word32 x,
unsigned int y)
1404 return _lrotl(x, static_cast<byte>(y));
1415 template<>
inline word32 rotrVariable<word32>(
word32 x,
unsigned int y)
1418 return _lrotr(x, static_cast<byte>(y));
1428 template<>
inline word32 rotlMod<word32>(
word32 x,
unsigned int y)
1431 return _lrotl(x, static_cast<byte>(y));
1441 template<>
inline word32 rotrMod<word32>(
word32 x,
unsigned int y)
1444 return _lrotr(x, static_cast<byte>(y));
1447 #endif // #ifdef _MSC_VER 1449 #if (_MSC_VER >= 1400) || (defined(_MSC_VER) && !defined(_DLL)) 1460 template<>
inline word64 rotlFixed<word64>(
word64 x,
unsigned int y)
1464 return y ? _rotl64(x, static_cast<byte>(y)) :
x;
1475 template<>
inline word64 rotrFixed<word64>(
word64 x,
unsigned int y)
1479 return y ? _rotr64(x, static_cast<byte>(y)) :
x;
1490 template<>
inline word64 rotlVariable<word64>(
word64 x,
unsigned int y)
1493 return _rotl64(x, static_cast<byte>(y));
1504 template<>
inline word64 rotrVariable<word64>(
word64 x,
unsigned int y)
1507 return y ? _rotr64(x, static_cast<byte>(y)) :
x;
1517 template<>
inline word64 rotlMod<word64>(
word64 x,
unsigned int y)
1520 return y ? _rotl64(x, static_cast<byte>(y)) :
x;
1530 template<>
inline word64 rotrMod<word64>(
word64 x,
unsigned int y)
1533 return y ? _rotr64(x, static_cast<byte>(y)) :
x;
1536 #endif // #if _MSC_VER >= 1310 1538 #if _MSC_VER >= 1400 && !defined(__INTEL_COMPILER) 1541 template<>
inline word16 rotlFixed<word16>(
word16 x,
unsigned int y)
1544 return _rotl16(x, static_cast<byte>(y));
1547 template<>
inline word16 rotrFixed<word16>(
word16 x,
unsigned int y)
1550 return _rotr16(x, static_cast<byte>(y));
1553 template<>
inline word16 rotlVariable<word16>(
word16 x,
unsigned int y)
1555 return _rotl16(x, static_cast<byte>(y));
1558 template<>
inline word16 rotrVariable<word16>(
word16 x,
unsigned int y)
1560 return _rotr16(x, static_cast<byte>(y));
1563 template<>
inline word16 rotlMod<word16>(
word16 x,
unsigned int y)
1565 return _rotl16(x, static_cast<byte>(y));
1568 template<>
inline word16 rotrMod<word16>(
word16 x,
unsigned int y)
1570 return _rotr16(x, static_cast<byte>(y));
1573 template<>
inline byte rotlFixed<byte>(
byte x,
unsigned int y)
1576 return _rotl8(x, static_cast<byte>(y));
1579 template<>
inline byte rotrFixed<byte>(
byte x,
unsigned int y)
1582 return _rotr8(x, static_cast<byte>(y));
1585 template<>
inline byte rotlVariable<byte>(
byte x,
unsigned int y)
1587 return _rotl8(x, static_cast<byte>(y));
1590 template<>
inline byte rotrVariable<byte>(
byte x,
unsigned int y)
1592 return _rotr8(x, static_cast<byte>(y));
1595 template<>
inline byte rotlMod<byte>(
byte x,
unsigned int y)
1597 return _rotl8(x, static_cast<byte>(y));
1600 template<>
inline byte rotrMod<byte>(
byte x,
unsigned int y)
1602 return _rotr8(x, static_cast<byte>(y));
1605 #endif // #if _MSC_VER >= 1400 1607 #if (defined(__MWERKS__) && TARGET_CPU_PPC) 1609 template<>
inline word32 rotlFixed<word32>(
word32 x,
unsigned int y)
1612 return y ? __rlwinm(x,y,0,31) :
x;
1615 template<>
inline word32 rotrFixed<word32>(
word32 x,
unsigned int y)
1618 return y ? __rlwinm(x,32-y,0,31) :
x;
1621 template<>
inline word32 rotlVariable<word32>(
word32 x,
unsigned int y)
1624 return (__rlwnm(x,y,0,31));
1627 template<>
inline word32 rotrVariable<word32>(
word32 x,
unsigned int y)
1630 return (__rlwnm(x,32-y,0,31));
1633 template<>
inline word32 rotlMod<word32>(
word32 x,
unsigned int y)
1635 return (__rlwnm(x,y,0,31));
1638 template<>
inline word32 rotrMod<word32>(
word32 x,
unsigned int y)
1640 return (__rlwnm(x,32-y,0,31));
1643 #endif // #if (defined(__MWERKS__) && TARGET_CPU_PPC) 1657 return GETBYTE(value,
sizeof(
T)-index-1);
1674 #ifdef CRYPTOPP_BYTESWAP_AVAILABLE 1676 #elif (_MSC_VER >= 1400) || (defined(_MSC_VER) && !defined(_DLL)) 1677 return _byteswap_ushort(value);
1689 #if defined(__GNUC__) && defined(CRYPTOPP_X86_ASM_AVAILABLE) 1690 __asm__ (
"bswap %0" :
"=r" (value) :
"0" (value));
1692 #elif defined(CRYPTOPP_BYTESWAP_AVAILABLE) 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) 1703 value = ((value & 0xFF00FF00) >> 8) | ((value & 0x00FF00FF) << 8);
1714 #if defined(__GNUC__) && defined(CRYPTOPP_X86_ASM_AVAILABLE) && defined(__x86_64__) 1715 __asm__ (
"bswap %0" :
"=r" (value) :
"0" (value));
1717 #elif defined(CRYPTOPP_BYTESWAP_AVAILABLE) 1719 #elif (_MSC_VER >= 1400) || (defined(_MSC_VER) && !defined(_DLL)) 1720 return _byteswap_uint64(value);
1721 #elif CRYPTOPP_BOOL_SLOW_WORD64 1724 value = ((value &
W64LIT(0xFF00FF00FF00FF00)) >> 8) | ((value &
W64LIT(0x00FF00FF00FF00FF)) << 8);
1725 value = ((value &
W64LIT(0xFFFF0000FFFF0000)) >> 16) | ((value &
W64LIT(0x0000FFFF0000FFFF)) << 16);
1735 value =
byte((value & 0xAA) >> 1) |
byte((value & 0x55) << 1);
1736 value =
byte((value & 0xCC) >> 2) |
byte((value & 0x33) << 2);
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);
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);
1767 #if CRYPTOPP_BOOL_SLOW_WORD64 1788 else if (
sizeof(
T) == 2)
1790 else if (
sizeof(
T) == 4)
1851 size_t count = byteCount/
sizeof(
T);
1852 for (
size_t i=0; i<
count; i++)
1875 memcpy_s(out, byteCount, in, byteCount);
1881 const size_t U =
sizeof(
T);
1883 memcpy_s(out, outlen*U, in, inlen);
1888 #ifndef CRYPTOPP_ALLOW_UNALIGNED_DATA_ACCESS 1898 ? block[1] | (block[0] << 8)
1899 : block[0] | (block[1] << 8);
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))
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));
1935 block[0] = (
byte)(xorBlock ? (value ^ xorBlock[0]) : value);
2059 #endif // #ifndef CRYPTOPP_ALLOW_UNALIGNED_DATA_ACCESS 2081 #ifdef CRYPTOPP_ALLOW_UNALIGNED_DATA_ACCESS 2085 memcpy(&temp, block,
sizeof(
T));
2109 result = GetWord<T>(assumeAligned, order, block);
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);
2131 if (xorBlock) {
memcpy(&t2, xorBlock,
sizeof(
T)); t1 ^=
t2;}
2132 memcpy(block, &t1,
sizeof(
T));
2152 template <
class T,
class B,
bool A=false>
2159 : m_block((const
byte *)block) {}
2169 x = GetWord<T>(A, B::ToEnum(), m_block);
2170 m_block +=
sizeof(
T);
2194 template <
class T,
class B,
bool A=false>
2202 : m_xorBlock((const
byte *)xorBlock), m_block((
byte *)block) {}
2211 PutWord(A, B::ToEnum(), m_block, (
T)x, m_xorBlock);
2212 m_block +=
sizeof(
T);
2214 m_xorBlock +=
sizeof(
T);
2232 template <
class T,
class B,
bool GA=false,
bool PA=false>
2246 return std::string((
char *)&value,
sizeof(value));
2313 return value >> bits;
2324 return value << bits;
2337 template <
unsigned int bits,
class T>
2352 template <
unsigned int bits,
class T>
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; 2372 #if (CRYPTOPP_MSC_VERSION) 2373 # pragma warning(pop) 2376 #if CRYPTOPP_GCC_DIAGNOSTIC_AVAILABLE 2377 # pragma GCC diagnostic pop void SecureWipeBuffer(T *buf, size_t n)
Sets each element of an array to 0.
the cipher is performing decryption
An invalid argument was detected.
CRYPTOPP_DLL std::string IntToString< Integer >(Integer value, unsigned int base)
Converts an Integer to a string.
void GetUserKey(ByteOrder order, T *out, size_t outlen, const byte *in, size_t inlen)
std::string StringNarrow(const wchar_t *str, bool throwOnError=true)
Converts a wide character C-string to a multibyte string.
bool NativeByteOrderIs(ByteOrder order)
Determines whether order follows native byte ordering.
void memmove_s(void *dest, size_t sizeInBytes, const void *src, size_t count)
Bounds checking replacement for memmove()
bool SafeConvert(T1 from, T2 &to)
Tests whether a conversion from -> to is safe to perform.
ByteOrder
Provides the byte ordering.
Restricts the instantiation of a class to one static object without locks.
void IncrementCounterByOne(byte *inout, unsigned int size)
Performs an addition with carry on a block of bytes.
T2 ModPowerOf2(const T1 &a, const T2 &b)
Reduces a value to a power of 2.
T SafeRightShift(T value)
size_t BitsToBytes(size_t bitCount)
Returns the number of 8-bit bytes or octets required for the specified number of bits.
T StringToWord(const std::string &str, ByteOrder order=BIG_ENDIAN_ORDER)
bool IsAligned(const void *ptr)
Determines whether ptr is minimally aligned.
T rotlFixed(T x, unsigned int y)
Performs a left rotate.
size_t BitsToWords(size_t bitCount)
Returns the number of words required for the specified number of bits.
void PutWord(bool assumeAligned, ByteOrder order, byte *block, T value, const byte *xorBlock=NULL)
Access a block of memory.
unsigned int BytePrecision(const T &value)
Returns the number of 8-bit bytes or octets required for a value.
#define NAMESPACE_BEGIN(x)
PutBlock(const void *xorBlock, void *block)
Construct a PutBlock.
Converts an enumeration to a type suitable for use as a template parameter.
CipherDir
Specifies a direction for a cipher to operate.
void UnalignedbyteNonTemplate(ByteOrder order, byte *block, byte value, const byte *xorBlock)
Abstract base classes that provide a uniform interface to this library.
static GetBlock< T, B, GA > Get(const void *block)
void memcpy_s(void *dest, size_t sizeInBytes, const void *src, size_t count)
Bounds checking replacement for memcpy()
unsigned int GetAlignmentOf()
Returns the minimum alignment requirements of a type.
static T LeftShift(T value, unsigned int bits)
Left shifts a value that does not overflow.
An object factory function.
Classes for automatic resource management.
Library configuration file.
size_t BytesToWords(size_t byteCount)
Returns the number of words required for the specified number of bytes.
std::string WordToString(T value, ByteOrder order=BIG_ENDIAN_ORDER)
T rotlVariable(T x, unsigned int y)
Performs a left rotate.
Access a block of memory.
byte BitReverse(byte value)
Reverses bits in a 8-bit value.
byte order is little-endian
the cipher is performing encryption
uint32_t bswap_32(uint32_t x)
T GetWord(bool assumeAligned, ByteOrder order, const byte *block)
Access a block of memory.
LittleEndian NativeByteOrder
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.
Uses encapsulation to hide an object in derived classes.
const unsigned int WORD_BITS
#define CRYPTOPP_COMPILE_ASSERT(assertion)
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.
CRYPTOPP_DLL void *CRYPTOPP_API UnalignedAllocate(size_t size)
Allocates a buffer.
Manages resources for a single object.
unsigned int TrailingZeros(word32 v)
Determines the number of trailing 0-bits in a value.
T Crop(T value, size_t bits)
Truncates the value to the specified number of bits.
T ConditionalByteReverse(ByteOrder order, T value)
Reverses bytes in a value depending upon endianness.
CRYPTOPP_DLL std::string IntToString< word64 >(word64 value, unsigned int base)
Converts an unsigned value to a string.
void ConditionalSwapPointers(bool c, T &a, T &b)
Performs a branchless swap of pointers a and b if condition c is true.
CRYPTOPP_DLL void CRYPTOPP_API UnalignedDeallocate(void *ptr)
Frees a buffer allocated with UnalignedAllocate.
Multiple precision integer with arithmetic operations.
uint16_t bswap_16(uint16_t x)
T1 SaturatingSubtract(const T1 &a, const T2 &b)
Performs a saturating subtract clamped at 0.
CipherDir GetCipherDir(const T &obj)
Returns the direction the cipher is being operated.
const T1 UnsignedMin(const T1 &a, const T2 &b)
Safe comparison of values that could be neagtive and incorrectly promoted.
#define CRYPTOPP_NOINLINE_DOTDOTDOT
unsigned long long word64
bool IsPowerOf2(const T &value)
Tests whether a value is a power of 2.
byte UnalignedGetWordNonTemplate(ByteOrder order, const byte *block, const byte *)
static T RightShift(T value, unsigned int bits)
Right shifts a value that does not overflow.
static char dummy[2 *b-1]
void * memset_z(void *ptr, int value, size_t num)
Memory block initializer and eraser that attempts to survive optimizations.
unsigned int Parity(T value)
Returns the parity of a value.
const unsigned int WORD_SIZE
const T & STDMIN(const T &a, const T &b)
Replacement function for std::min.
#define CRYPTOPP_ASSERT(exp)
CRYPTOPP_NOINLINE const T & Ref(CRYPTOPP_NOINLINE_DOTDOTDOT) const
Return a reference to the inner Singleton object.
#define CRYPTOPP_NO_VTABLE
CRYPTOPP_DLL void CRYPTOPP_API CallNewHandler()
Attempts to reclaim unused memory.
PutBlock< T, B, A > & operator()(U x)
Access a block of memory.
#define CRYPTOPP_NOINLINE
void ConditionalSwap(bool c, T &a, T &b)
Performs a branchless swap of values a and b if condition c is true.
T1 SaturatingSubtract1(const T1 &a, const T2 &b)
Performs a saturating subtract clamped at 1.
GetBlock(const void *block)
Construct a GetBlock.
uint64_t bswap_64(uint64_t x)
static T RightShift(T value, unsigned int bits)
Right shifts a value that overflows.
uint8_t const size_t const size
void * memcpy(void *a, const void *b, size_t c)
#define CRYPTOPP_UNUSED(x)
Access a block of memory.
std::string IntToString(T value, unsigned int base=10)
Converts a value to a string.
CRYPTOPP_DLL void CRYPTOPP_API xorbuf(byte *buf, const byte *mask, size_t count)
Performs an XOR of a buffer with a mask.
GetBlock< T, B, A > & operator()(U &x)
Access a block of memory.
ByteOrder GetNativeByteOrder()
Returns NativeByteOrder as an enumerated ByteOrder value.
T1 RoundUpToMultipleOf(const T1 &n, const T2 &m)
Rounds a value up to a multiple of a second value.
Safely shift values when undefined behavior could occur.
const T & STDMAX(const T &a, const T &b)
Replacement function for std::max.
Access a block of memory.
T rotrVariable(T x, unsigned int y)
Performs a right rotate.
#define CRYPTOPP_GET_BYTE_AS_BYTE(x, y)
T rotlMod(T x, unsigned int y)
Performs a left rotate.
unsigned int GetByte(ByteOrder order, T value, unsigned int index)
Gets a byte from a value.
T rotrMod(T x, unsigned int y)
Performs a right rotate.
T rotrFixed(T x, unsigned int y)
Performs a right rotate.
Ensures an object is not copyable.
void * memmove(void *a, const void *b, size_t c)
Singleton(F objectFactory=F())
byte ByteReverse(byte value)
Reverses bytes in a 8-bit value.
static ENUM_TYPE ToEnum()
unsigned int BitPrecision(const T &value)
Returns the number of bits required for a value.
size_t BitsToDwords(size_t bitCount)
Returns the number of double words required for the specified number of bits.
static T LeftShift(T value, unsigned int bits)
Left shifts a value that overflows.
T1 RoundDownToMultipleOf(const T1 &n, const T2 &m)
Rounds a value down to a multiple of a second value.
void vec_swap(T &a, T &b)
Swaps two variables which are arrays.