"Fossies" - the Fresh Open Source Software Archive

Member "src/Platform/Memory.h" (10 Oct 2018, 3683 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 "Memory.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 /*
    2  Derived from source code of TrueCrypt 7.1a, which is
    3  Copyright (c) 2008-2012 TrueCrypt Developers Association and which is governed
    4  by the TrueCrypt License 3.0.
    5 
    6  Modifications and additions to the original source code (contained in this file)
    7  and all other portions of this file are Copyright (c) 2013-2017 IDRIX
    8  and are governed by the Apache License 2.0 the full text of which is
    9  contained in the file License.txt included in VeraCrypt binary and source
   10  code distribution packages.
   11 */
   12 
   13 #ifndef TC_HEADER_Platform_Memory
   14 #define TC_HEADER_Platform_Memory
   15 
   16 #include <new>
   17 #include <memory.h>
   18 #include "PlatformBase.h"
   19 
   20 #ifdef TC_WINDOWS
   21 
   22 #   ifndef LITTLE_ENDIAN
   23 #       define LITTLE_ENDIAN 1234
   24 #   endif
   25 #   ifndef BYTE_ORDER
   26 #       define BYTE_ORDER LITTLE_ENDIAN
   27 #   endif
   28 
   29 #elif !defined(BYTE_ORDER)
   30 
   31 #   ifdef TC_MACOSX
   32 #       include <machine/endian.h>
   33 #   elif defined (TC_BSD)
   34 #       include <sys/endian.h>
   35 #   elif defined (TC_SOLARIS)
   36 #       include <sys/types.h>
   37 #       define LITTLE_ENDIAN 1234
   38 #       define BIG_ENDIAN 4321
   39 #       ifdef _BIG_ENDIAN
   40 #           define BYTE_ORDER BIG_ENDIAN
   41 #       else
   42 #           define BYTE_ORDER LITTLE_ENDIAN
   43 #       endif
   44 #   else
   45 #       include <endian.h>
   46 #   endif
   47 
   48 #   ifndef BYTE_ORDER
   49 #       ifndef __BYTE_ORDER
   50 #           error Byte ordering cannot be determined (BYTE_ORDER undefined).
   51 #       endif
   52 
   53 #       define BYTE_ORDER __BYTE_ORDER
   54 #   endif
   55 
   56 #   ifndef LITTLE_ENDIAN
   57 #       define LITTLE_ENDIAN __LITTLE_ENDIAN
   58 #   endif
   59 
   60 #   ifndef BIG_ENDIAN
   61 #       define BIG_ENDIAN __BIG_ENDIAN
   62 #   endif
   63 
   64 #endif // !BYTE_ORDER
   65 
   66 #if BYTE_ORDER != BIG_ENDIAN && BYTE_ORDER != LITTLE_ENDIAN
   67 #   error Unsupported byte ordering detected.
   68 #endif
   69 
   70 namespace VeraCrypt
   71 {
   72     class Memory
   73     {
   74     public:
   75         static void *Allocate (size_t size);
   76         static void *AllocateAligned (size_t size, size_t alignment);
   77         static int Compare (const void *memory1, size_t size1, const void *memory2, size_t size2);
   78         static void Copy (void *memoryDestination, const void *memorySource, size_t size);
   79         static void Erase (void *memory, size_t size);
   80         static void Free (void *memory);
   81         static void FreeAligned (void *memory);
   82         static void Zero (void *memory, size_t size);
   83     };
   84 
   85     class Endian
   86     {
   87     public:
   88         static byte Big (const byte &x)
   89         {
   90             return x;
   91         }
   92 
   93         static uint16 Big (const uint16 &x)
   94         {
   95 #if BYTE_ORDER == BIG_ENDIAN
   96             return x;
   97 #else
   98             return MirrorBytes (x);
   99 #endif
  100         }
  101 
  102         static uint32 Big (const uint32 &x)
  103         {
  104 #if BYTE_ORDER == BIG_ENDIAN
  105             return x;
  106 #else
  107             return MirrorBytes (x);
  108 #endif
  109         }
  110 
  111         static uint64 Big (const uint64 &x)
  112         {
  113 #if BYTE_ORDER == BIG_ENDIAN
  114             return x;
  115 #else
  116             return MirrorBytes (x);
  117 #endif
  118         }
  119 
  120         static byte Little (const byte &x)
  121         {
  122             return x;
  123         }
  124 
  125         static uint16 Little (const uint16 &x)
  126         {
  127 #if BYTE_ORDER == LITTLE_ENDIAN
  128             return x;
  129 #else
  130             return MirrorBytes (x);
  131 #endif
  132         }
  133 
  134         static uint32 Little (const uint32 &x)
  135         {
  136 #if BYTE_ORDER == LITTLE_ENDIAN
  137             return x;
  138 #else
  139             return MirrorBytes (x);
  140 #endif
  141         }
  142 
  143         static uint64 Little (const uint64 &x)
  144         {
  145 #if BYTE_ORDER == LITTLE_ENDIAN
  146             return x;
  147 #else
  148             return MirrorBytes (x);
  149 #endif
  150         }
  151 
  152     protected:
  153         static uint16 MirrorBytes (const uint16 &x)
  154         {
  155             return (x << 8) | (x >> 8);
  156         }
  157 
  158         static uint32 MirrorBytes (const uint32 &x)
  159         {
  160             uint32 n = (byte) x;
  161             n <<= 8; n |= (byte) (x >> 8);
  162             n <<= 8; n |= (byte) (x >> 16);
  163             return (n << 8) | (byte) (x >> 24);
  164         }
  165 
  166         static uint64 MirrorBytes (const uint64 &x)
  167         {
  168             uint64 n = (byte) x;
  169             n <<= 8; n |= (byte) (x >> 8);
  170             n <<= 8; n |= (byte) (x >> 16);
  171             n <<= 8; n |= (byte) (x >> 24);
  172             n <<= 8; n |= (byte) (x >> 32);
  173             n <<= 8; n |= (byte) (x >> 40);
  174             n <<= 8; n |= (byte) (x >> 48);
  175             return (n << 8) | (byte) (x >> 56);
  176         }
  177     };
  178 }
  179 
  180 #endif // TC_HEADER_Platform_Memory