"Fossies" - the Fresh Open Source Software Archive

Member "src/Crypto/misc.h" (10 Oct 2018, 6394 Bytes) of package /windows/misc/VeraCrypt_1.23-Hotfix-2_Source.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "misc.h" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 1.18_Source_vs_1.19_Source.

    1 #ifndef CRYPTOPP_MISC_H
    2 #define CRYPTOPP_MISC_H
    3 
    4 #include "config.h"
    5 #if !defined(_UEFI)
    6 #include <string.h>     // for memcpy and memmove
    7 #ifndef _WIN32
    8 #include <strings.h> // for strcasecmp
    9 #define _stricmp strcasecmp
   10 #endif
   11 #else
   12 #include "Tcdefs.h"
   13 #endif // !defined(_UEFI)
   14 
   15 #ifdef  __cplusplus
   16 extern "C" {
   17 #endif
   18 
   19 #if defined(_MSC_VER) && !defined(_UEFI)
   20     #if _MSC_VER >= 1400
   21         #if !defined(TC_WINDOWS_DRIVER) && !defined(_UEFI)
   22             // VC2005 workaround: disable declarations that conflict with winnt.h
   23             #define _interlockedbittestandset CRYPTOPP_DISABLED_INTRINSIC_1
   24             #define _interlockedbittestandreset CRYPTOPP_DISABLED_INTRINSIC_2
   25             #define _interlockedbittestandset64 CRYPTOPP_DISABLED_INTRINSIC_3
   26             #define _interlockedbittestandreset64 CRYPTOPP_DISABLED_INTRINSIC_4
   27             #include <intrin.h>
   28             #undef _interlockedbittestandset
   29             #undef _interlockedbittestandreset
   30             #undef _interlockedbittestandset64
   31             #undef _interlockedbittestandreset64
   32         #endif
   33         #define CRYPTOPP_FAST_ROTATE(x) 1
   34     #elif !defined(_UEFI) &&  _MSC_VER >= 1300
   35         #define CRYPTOPP_FAST_ROTATE(x) ((x) == 32 | (x) == 64)
   36     #else
   37         #define CRYPTOPP_FAST_ROTATE(x) ((x) == 32)
   38     #endif
   39 #elif (defined(__MWERKS__) && TARGET_CPU_PPC) || \
   40     (defined(__GNUC__) && (defined(_ARCH_PWR2) || defined(_ARCH_PWR) || defined(_ARCH_PPC) || defined(_ARCH_PPC64) || defined(_ARCH_COM)))
   41     #define CRYPTOPP_FAST_ROTATE(x) ((x) == 32)
   42 #elif defined(__GNUC__) && (CRYPTOPP_BOOL_X64 || CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X86)    // depend on GCC's peephole optimization to generate rotate instructions
   43     #define CRYPTOPP_FAST_ROTATE(x) 1
   44 #else
   45     #define CRYPTOPP_FAST_ROTATE(x) 0
   46 #endif
   47 
   48 #if defined( _MSC_VER ) && ( _MSC_VER > 800 ) && !defined(_UEFI)
   49 #pragma intrinsic(memcpy,memset)
   50 #endif
   51 
   52 #if _MSC_VER >= 1300 && !defined(__INTEL_COMPILER)
   53 // Intel C++ Compiler 10.0 calls a function instead of using the rotate instruction when using these instructions
   54 #pragma intrinsic(_rotr,_rotl,_rotr64,_rotl64)
   55 
   56 #define rotr32(x,n) _rotr(x, n)
   57 #define rotl32(x,n) _rotl(x, n)
   58 #define rotr64(x,n) _rotr64(x, n)
   59 #define rotl64(x,n) _rotl64(x, n)
   60 
   61 #else
   62 
   63 #define rotr32(x,n) (((x) >> n) | ((x) << (32 - n)))
   64 #define rotl32(x,n) (((x) << n) | ((x) >> (32 - n)))
   65 #define rotr64(x,n) (((x) >> n) | ((x) << (64 - n)))
   66 #define rotl64(x,n) (((x) << n) | ((x) >> (64 - n)))
   67 
   68 #endif
   69 
   70 #if _MSC_VER >= 1400 && !defined(__INTEL_COMPILER)
   71 // Intel C++ Compiler 10.0 calls a function instead of using the rotate instruction when using these instructions
   72 #pragma intrinsic(_rotr8,_rotl8,_rotr16,_rotl16)
   73 
   74 #define rotr8(x,n)  _rotr8(x, n)
   75 #define rotl8(x,n)  _rotl8(x, n)
   76 #define rotr16(x,n) _rotr16(x, n)
   77 #define rotl16(x,n) _rotl16(x, n)
   78 
   79 #else
   80 
   81 #define rotr8(x,n)  (((x) >> n) | ((x) << (8 - n)))
   82 #define rotl8(x,n)  (((x) << n) | ((x) >> (8 - n)))
   83 #define rotr16(x,n) (((x) >> n) | ((x) << (16 - n)))
   84 #define rotl16(x,n) (((x) << n) | ((x) >> (16 - n)))
   85 
   86 #endif
   87 
   88 #if defined(__GNUC__) && defined(__linux__)
   89 #define CRYPTOPP_BYTESWAP_AVAILABLE
   90 #include <byteswap.h>
   91 #elif defined(_MSC_VER) && _MSC_VER >= 1300 && !defined(_UEFI)
   92 #pragma intrinsic(_byteswap_ulong,_byteswap_uint64)
   93 #define CRYPTOPP_BYTESWAP_AVAILABLE
   94 #define bswap_32(x) _byteswap_ulong(x)
   95 #define bswap_64(x) _byteswap_uint64(x)
   96 #elif defined(__APPLE__)
   97 #include <libkern/OSByteOrder.h>
   98 #define CRYPTOPP_BYTESWAP_AVAILABLE
   99 #define bswap_16 OSSwapInt16
  100 #define bswap_32 OSSwapInt32
  101 #define bswap_64 OSSwapInt64
  102 #else
  103 #if CRYPTOPP_FAST_ROTATE(32)
  104 #define bswap_32(x) (rotr32((x), 8U) & 0xff00ff00) | (rotl32((x), 8U) & 0x00ff00ff)
  105 #else
  106 #define CRYPTOPP_BYTESWAP_AVAILABLE
  107 #define bswap_32(x) (rotl32((((x) & 0xFF00FF00) >> 8) | (((x) & 0x00FF00FF) << 8), 16U))
  108 #define bswap_64(x) rotl64(((((((x & LL(0xFF00FF00FF00FF00)) >> 8) | ((x & LL(0x00FF00FF00FF00FF)) << 8)) & LL(0xFFFF0000FFFF0000)) >> 16) | (((((x & LL(0xFF00FF00FF00FF00)) >> 8) | ((x & LL(0x00FF00FF00FF00FF)) << 8)) & LL(0x0000FFFF0000FFFF)) << 16)), 32U)
  109 #endif
  110 #ifndef TC_NO_COMPILER_INT64
  111 #define bswap_64(x) rotl64(((((((x & LL(0xFF00FF00FF00FF00)) >> 8) | ((x & LL(0x00FF00FF00FF00FF)) << 8)) & LL(0xFFFF0000FFFF0000)) >> 16) | (((((x & LL(0xFF00FF00FF00FF00)) >> 8) | ((x & LL(0x00FF00FF00FF00FF)) << 8)) & LL(0x0000FFFF0000FFFF)) << 16)), 32U)
  112 #endif
  113 #endif
  114 
  115 VC_INLINE uint32 ByteReverseWord32 (uint32 value)
  116 {
  117 #if defined(__GNUC__) && defined(CRYPTOPP_X86_ASM_AVAILABLE)
  118     __asm__ ("bswap %0" : "=r" (value) : "0" (value));
  119     return value;
  120 #elif defined(CRYPTOPP_BYTESWAP_AVAILABLE)
  121     return bswap_32(value);
  122 #elif defined(__MWERKS__) && TARGET_CPU_PPC
  123     return (uint32)__lwbrx(&value,0);
  124 #elif _MSC_VER >= 1400 || (_MSC_VER >= 1300 && !defined(_DLL))
  125     return _byteswap_ulong(value);
  126 #elif CRYPTOPP_FAST_ROTATE(32)
  127     // 5 instructions with rotate instruction, 9 without
  128     return (rotr32(value, 8U) & 0xff00ff00) | (rotl32(value, 8U) & 0x00ff00ff);
  129 #else
  130     // 6 instructions with rotate instruction, 8 without
  131     value = ((value & 0xFF00FF00) >> 8) | ((value & 0x00FF00FF) << 8);
  132     return rotl32(value, 16U);
  133 #endif
  134 }
  135 
  136 #ifndef TC_NO_COMPILER_INT64
  137 
  138 VC_INLINE uint64 ByteReverseWord64(uint64 value)
  139 {
  140 #if defined(__GNUC__) && defined(CRYPTOPP_X86_ASM_AVAILABLE) && defined(__x86_64__)
  141     __asm__ ("bswap %0" : "=r" (value) : "0" (value));
  142     return value;
  143 #elif defined(CRYPTOPP_BYTESWAP_AVAILABLE)
  144     return bswap_64(value);
  145 #elif defined(_MSC_VER) && _MSC_VER >= 1300
  146     return _byteswap_uint64(value);
  147 #else
  148     value = ((value & LL(0xFF00FF00FF00FF00)) >> 8) | ((value & LL(0x00FF00FF00FF00FF)) << 8);
  149     value = ((value & LL(0xFFFF0000FFFF0000)) >> 16) | ((value & LL(0x0000FFFF0000FFFF)) << 16);
  150     return rotl64(value, 32U);
  151 #endif
  152 }
  153 
  154 VC_INLINE void CorrectEndianess(uint64 *out, const uint64 *in, size_t byteCount)
  155 
  156 {
  157     size_t i, count = byteCount/sizeof(uint64);
  158     for (i=0; i<count; i++)
  159         out[i] = ByteReverseWord64(in[i]);
  160 }
  161 
  162 #endif
  163 
  164 #ifdef CRYPTOPP_ALLOW_UNALIGNED_DATA_ACCESS
  165     #define GetAlignmentOf(T) 1
  166 #elif (_MSC_VER >= 1300)
  167     #define GetAlignmentOf(T) __alignof(T)
  168 #elif defined(__GNUC__)
  169     #define GetAlignmentOf(T) __alignof__(T)
  170 #else
  171     #define GetAlignmentOf(T) sizeof(T)
  172 #endif
  173 
  174 #define IsPowerOf2(n)   (((n) > 0) && (((n) & ((n)-1)) == 0))
  175 
  176 #define ModPowerOf2(a,b)    ((a) & ((b)-1))
  177 
  178 #define IsAlignedOn(p,alignment) ((alignment==1) || (IsPowerOf2(alignment) ? ModPowerOf2((size_t)p, alignment) == 0 : (size_t)p % alignment == 0))
  179 
  180 #define IsAligned16(p)  IsAlignedOn(p, GetAlignmentOf(uint64))
  181 
  182 #ifdef  __cplusplus
  183 }
  184 #endif
  185 
  186 #endif