forked from Roman971/OoT-Randomizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crc.py
36 lines (26 loc) · 928 Bytes
/
crc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import itertools
from ntype import BigStream, uint32
def calculate_crc(self):
t1 = t2 = t3 = t4 = t5 = t6 = 0xDF26F436
u32 = 0xFFFFFFFF
m1 = self.read_bytes(0x1000, 0x100000)
words = map(uint32.value, zip(m1[0::4], m1[1::4], m1[2::4], m1[3::4]))
m2 = self.read_bytes(0x750, 0x100)
words2 = map(uint32.value, zip(m2[0::4], m2[1::4], m2[2::4], m2[3::4]))
for d, d2 in zip(words, itertools.cycle(words2)):
# keep t2 and t6 in u32 for comparisons; others can wait to be truncated
if ((t6 + d) & u32) < t6:
t4 += 1
t6 = (t6+d) & u32
t3 ^= d
shift = d & 0x1F
r = ((d << shift) | (d >> (32 - shift)))
t5 += r
if t2 > d:
t2 ^= r & u32
else:
t2 ^= t6 ^ d
t1 += d2 ^ d
crc0 = (t6 ^ t4 ^ t3) & u32
crc1 = (t5 ^ t2 ^ t1) & u32
return uint32.bytes(crc0) + uint32.bytes(crc1)