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