"Fossies" - the Fresh Open Source Software Archive

Member "src/Boot/Windows/Platform.cpp" (10 Oct 2018, 4004 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 "Platform.cpp" 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  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 #include "Platform.h"
   14 #include "BootConsoleIo.h"
   15 
   16 
   17 uint64 operator+ (const uint64 &a, const uint64 &b)
   18 {
   19     int carry = 0;
   20     uint64 r;
   21 
   22     r.LowPart = a.LowPart + b.LowPart;
   23     __asm
   24     {
   25         jnc nocarry
   26         mov carry, 1
   27     nocarry:
   28     }
   29 
   30     r.HighPart = a.HighPart + b.HighPart + carry;
   31 
   32     return r;
   33 }
   34 
   35 uint64 operator+ (const uint64 &a, uint32 b)
   36 {
   37     uint64 b64;
   38     b64.HighPart = 0;
   39     b64.LowPart = b;
   40     return a + b64;
   41 }
   42 
   43 uint64 &operator+= (uint64 &a, const uint64 &b)
   44 {
   45     return a = a + b;
   46 }
   47 
   48 uint64 operator- (const uint64 &a, const uint64 &b)
   49 {
   50     int carry = 0;
   51     uint64 r;
   52 
   53     r.LowPart = a.LowPart - b.LowPart;
   54     __asm
   55     {
   56         jnc nocarry
   57         mov carry, 1
   58     nocarry:
   59     }
   60 
   61     r.HighPart = a.HighPart - b.HighPart - carry;
   62 
   63     return r;
   64 }
   65 
   66 uint64 operator- (const uint64 &a, uint32 b)
   67 {
   68     uint64 b64;
   69     b64.HighPart = 0;
   70     b64.LowPart = b;
   71     return a - b64;
   72 }
   73 
   74 uint64 &operator-= (uint64 &a, const uint64 &b)
   75 {
   76     return a = a - b;
   77 }
   78 
   79 uint64 operator>> (const uint64 &a, int shiftCount)
   80 {
   81     uint64 r = a;
   82 
   83     while (shiftCount--)
   84     {
   85         r.LowPart >>= 1;
   86 
   87         if ((byte) r.HighPart & 1)
   88             r.LowPart |= 0x80000000UL;
   89 
   90         r.HighPart >>= 1;
   91     }
   92 
   93     return r;
   94 }
   95 
   96 uint64 operator<< (const uint64 &a, int shiftCount)
   97 {
   98     uint64 r = a;
   99 
  100     while (shiftCount--)
  101         r += r;
  102 
  103     return r;
  104 }
  105 
  106 uint64 &operator++ (uint64 &a)
  107 {
  108     uint64 b;
  109     b.HighPart = 0;
  110     b.LowPart = 1;
  111 
  112     return a += b;
  113 }
  114 
  115 bool operator== (const uint64 &a, const uint64 &b)
  116 {
  117     return a.HighPart == b.HighPart && a.LowPart == b.LowPart;
  118 }
  119 
  120 bool operator> (const uint64 &a, const uint64 &b)
  121 {
  122     return (a.HighPart > b.HighPart) || (a.HighPart == b.HighPart && a.LowPart > b.LowPart);
  123 }
  124 
  125 bool operator< (const uint64 &a, const uint64 &b)
  126 {
  127     return (a.HighPart < b.HighPart) || (a.HighPart == b.HighPart && a.LowPart < b.LowPart);
  128 }
  129 
  130 bool operator>= (const uint64 &a, const uint64 &b)
  131 {
  132     return a > b || a == b;
  133 }
  134 
  135 bool operator<= (const uint64 &a, const uint64 &b)
  136 {
  137     return a < b || a == b;
  138 }
  139 
  140 #ifdef TC_BOOT_DEBUG_ENABLED
  141 
  142 bool TestInt64 ()
  143 {
  144     uint64 a, b, c;
  145     a.HighPart = 0x00112233UL;
  146     a.LowPart = 0xabcd1234UL;
  147 
  148     b.HighPart = 0x00ffeeddUL;
  149     b.LowPart = 0xffffFFFFUL;
  150 
  151     a += b;
  152     a -= b;
  153 
  154     ++a;
  155 
  156     b = b + (uint32) 1UL;
  157 
  158     c = (a - ((a + b) >> 32) - (uint32) 1UL);
  159     if (c.HighPart != 0x112233UL || c.LowPart != 0xAABC0123UL)
  160         return false;
  161 
  162     c = c << 9;
  163     return c.HighPart == 0x22446755UL && c.LowPart == 0x78024600UL;
  164 }
  165 
  166 #endif
  167 
  168 void CopyMemory (void *source, uint16 destSegment, uint16 destOffset, uint16 blockSize)
  169 {
  170     __asm
  171     {
  172         push es
  173         mov si, ss:source
  174         mov es, ss:destSegment
  175         mov di, ss:destOffset
  176         mov cx, ss:blockSize
  177         cld
  178         rep movsb
  179         pop es
  180     }
  181 }
  182 
  183 
  184 void CopyMemory (uint16 sourceSegment, uint16 sourceOffset, void *destination, uint16 blockSize)
  185 {
  186     __asm
  187     {
  188         push ds
  189         push es
  190         mov ax, ds
  191         mov es, ax
  192         mov di, ss:destination
  193         mov si, ss:sourceOffset
  194         mov cx, ss:blockSize
  195         mov ds, ss:sourceSegment
  196         cld
  197         rep movsb
  198         pop es
  199         pop ds
  200     }
  201 }
  202 
  203 
  204 void EraseMemory (void *memory, int size)
  205 {
  206     memset (memory, 0, size);
  207 }
  208 
  209 
  210 uint32 GetLinearAddress (uint16 segment, uint16 offset)
  211 {
  212     return (uint32 (segment) << 4) + offset;
  213 }
  214 
  215 
  216 bool RegionsIntersect (const uint64 &start1, uint32 length1, const uint64 &start2, const uint64 &end2)
  217 {
  218     uint64 end1 = start1 + length1 - 1UL;
  219     uint64 intersectEnd = (end1 <= end2) ? end1 : end2;
  220 
  221     uint64 intersectStart = (start1 >= start2) ? start1 : start2;
  222     if (intersectStart > intersectEnd)
  223         return false;
  224 
  225     return (intersectEnd + 1UL - intersectStart).LowPart != 0;
  226 }
  227 
  228 
  229 void ThrowFatalException (int line)
  230 {
  231     PrintChar ('#'); Print (line);
  232     while (1);
  233 }