"Fossies" - the Fresh Open Source Software Archive

Member "src/Common/Endian.h" (10 Oct 2018, 4593 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 "Endian.h" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 1.21_Source_vs_1.22_Source.

    1 /*
    2  Legal Notice: Some portions of the source code contained in this file were
    3  derived from the source code of TrueCrypt 7.1a, which is 
    4  Copyright (c) 2003-2012 TrueCrypt Developers Association and which is 
    5  governed by the TrueCrypt License 3.0, also from the source code of
    6  Encryption for the Masses 2.02a, which is Copyright (c) 1998-2000 Paul Le Roux
    7  and which is governed by the 'License Agreement for Encryption for the Masses' 
    8  Modifications and additions to the original source code (contained in this file) 
    9  and all other portions of this file are Copyright (c) 2013-2017 IDRIX
   10  and are governed by the Apache License 2.0 the full text of which is
   11  contained in the file License.txt included in VeraCrypt binary and source
   12  code distribution packages. */
   13 
   14 #ifndef TC_ENDIAN_H
   15 #define TC_ENDIAN_H
   16 
   17 #include "Common/Tcdefs.h"
   18 
   19 #if defined(__cplusplus)
   20 extern "C"
   21 {
   22 #endif
   23 
   24 #if defined(_WIN32) || defined(_UEFI)
   25 
   26 #   ifndef LITTLE_ENDIAN
   27 #       define LITTLE_ENDIAN 1234
   28 #   endif
   29 #   ifndef BYTE_ORDER
   30 #       define BYTE_ORDER LITTLE_ENDIAN
   31 #   endif
   32 
   33 #elif !defined(BYTE_ORDER)
   34 
   35 #   ifdef TC_MACOSX
   36 #       include <machine/endian.h>
   37 #   elif defined (TC_BSD)
   38 #       include <sys/endian.h>
   39 #   elif defined (TC_SOLARIS)
   40 #       include <sys/types.h>
   41 #       define LITTLE_ENDIAN 1234
   42 #       define BIG_ENDIAN 4321
   43 #       ifdef _BIG_ENDIAN
   44 #           define BYTE_ORDER BIG_ENDIAN
   45 #       else
   46 #           define BYTE_ORDER LITTLE_ENDIAN
   47 #       endif
   48 #   else
   49 #       include <endian.h>
   50 #   endif
   51 
   52 #   ifndef BYTE_ORDER
   53 #       ifndef __BYTE_ORDER
   54 #           error Byte order cannot be determined (BYTE_ORDER undefined)
   55 #       endif
   56 
   57 #       define BYTE_ORDER __BYTE_ORDER
   58 #   endif
   59 
   60 #   ifndef LITTLE_ENDIAN
   61 #       define LITTLE_ENDIAN __LITTLE_ENDIAN
   62 #   endif
   63 
   64 #   ifndef BIG_ENDIAN
   65 #       define BIG_ENDIAN __BIG_ENDIAN
   66 #   endif
   67 
   68 #endif // !BYTE_ORDER
   69 
   70 /* Macros to read and write 16, 32, and 64-bit quantities in a portable manner.
   71    These functions are implemented as macros rather than true functions as
   72    the need to adjust the memory pointers makes them somewhat painful to call
   73    in user code */
   74 
   75 #define mputInt64(memPtr,data) \
   76     *memPtr++ = ( unsigned char ) ( ( ( data ) >> 56 ) & 0xFF ), \
   77     *memPtr++ = ( unsigned char ) ( ( ( data ) >> 48 ) & 0xFF ), \
   78     *memPtr++ = ( unsigned char ) ( ( ( data ) >> 40 ) & 0xFF ), \
   79     *memPtr++ = ( unsigned char ) ( ( ( data ) >> 32 ) & 0xFF ), \
   80     *memPtr++ = ( unsigned char ) ( ( ( data ) >> 24 ) & 0xFF ), \
   81     *memPtr++ = ( unsigned char ) ( ( ( data ) >> 16 ) & 0xFF ), \
   82     *memPtr++ = ( unsigned char ) ( ( ( data ) >> 8 ) & 0xFF ), \
   83     *memPtr++ = ( unsigned char ) ( ( data ) & 0xFF )
   84 
   85 #define mputLong(memPtr,data) \
   86     *memPtr++ = ( unsigned char ) ( ( ( data ) >> 24 ) & 0xFF ), \
   87     *memPtr++ = ( unsigned char ) ( ( ( data ) >> 16 ) & 0xFF ), \
   88     *memPtr++ = ( unsigned char ) ( ( ( data ) >> 8 ) & 0xFF ), \
   89     *memPtr++ = ( unsigned char ) ( ( data ) & 0xFF )
   90 
   91 #define mputWord(memPtr,data) \
   92     *memPtr++ = ( unsigned char ) ( ( ( data ) >> 8 ) & 0xFF ), \
   93     *memPtr++ = ( unsigned char ) ( ( data ) & 0xFF )
   94 
   95 #define mputByte(memPtr,data)   \
   96     *memPtr++ = ( unsigned char ) data
   97 
   98 #define mputBytes(memPtr,data,len)  \
   99     memcpy (memPtr,data,len); \
  100     memPtr += len;
  101 
  102 #define mgetInt64(memPtr)       \
  103     ( memPtr += 8, ( ( unsigned __int64 ) memPtr[ -8 ] << 56 ) | ( ( unsigned __int64 ) memPtr[ -7 ] << 48 ) | \
  104     ( ( unsigned __int64 ) memPtr[ -6 ] << 40 ) | ( ( unsigned __int64 ) memPtr[ -5 ] << 32 ) | \
  105     ( ( unsigned __int64 ) memPtr[ -4 ] << 24 ) | ( ( unsigned __int64 ) memPtr[ -3 ] << 16 ) | \
  106       ( ( unsigned __int64 ) memPtr[ -2 ] << 8 ) | ( unsigned __int64 ) memPtr[ -1 ] )
  107 
  108 #define mgetLong(memPtr)        \
  109     ( memPtr += 4, ( ( unsigned __int32 ) memPtr[ -4 ] << 24 ) | ( ( unsigned __int32 ) memPtr[ -3 ] << 16 ) | \
  110       ( ( unsigned __int32 ) memPtr[ -2 ] << 8 ) | ( unsigned __int32 ) memPtr[ -1 ] )
  111 
  112 #define mgetWord(memPtr)        \
  113     ( memPtr += 2, ((( unsigned short ) memPtr[ -2 ] << 8 ) | ( ( unsigned short ) memPtr[ -1 ] )) ) 
  114 
  115 #define mgetByte(memPtr)        \
  116     ( ( unsigned char ) *memPtr++ )
  117 
  118 #if BYTE_ORDER == BIG_ENDIAN
  119 #   define LE16(x) MirrorBytes16(x)
  120 #   define LE32(x) MirrorBytes32(x)
  121 #   define LE64(x) MirrorBytes64(x)
  122 #else
  123 #   define LE16(x) (x)
  124 #   define LE32(x) (x)
  125 #   define LE64(x) (x)
  126 #endif
  127 
  128 #if BYTE_ORDER == LITTLE_ENDIAN
  129 #   define BE16(x) MirrorBytes16(x)
  130 #   define BE32(x) MirrorBytes32(x)
  131 #   define BE64(x) MirrorBytes64(x)
  132 #else
  133 #   define BE16(x) (x)
  134 #   define BE32(x) (x)
  135 #   define BE64(x) (x)
  136 #endif
  137 
  138 unsigned __int16 MirrorBytes16 (unsigned __int16 x);
  139 unsigned __int32 MirrorBytes32 (unsigned __int32 x);
  140 #ifndef TC_NO_COMPILER_INT64
  141 uint64 MirrorBytes64 (uint64 x);
  142 #endif
  143 
  144 #if defined(__cplusplus)
  145 }
  146 #endif
  147 
  148 #endif /* TC_ENDIAN_H */