-
Notifications
You must be signed in to change notification settings - Fork 47
/
elite-checksum.py
174 lines (120 loc) · 3.77 KB
/
elite-checksum.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/usr/bin/env python
#
# S.BCFS
# Prepare the Big Code File
# ELTcode
#
import sys
from os.path import basename
if (sys.version_info > (3, 0)):
from io import BytesIO as ByteBuffer
else:
from StringIO import StringIO as ByteBuffer
argv = sys.argv
argc = len(argv)
Encrypt = True
if argc > 1 and argv[1] == '-u':
Encrypt = False
print "Elite Big Code File"
print "Encryption = ", Encrypt
data_block = bytearray()
eliteb_offset = 0
# Append all assembled code files
elite_names = ('ELThead','ELTA','ELTB','ELTC','ELTD','ELTE','ELTF','ELTG')
for file_name in elite_names:
print str(len(data_block)), file_name
if file_name == 'ELTB':
eliteb_offset = len(data_block)
elite_file = open('output/' + file_name + '.bin', 'rb')
data_block.extend(elite_file.read())
elite_file.close()
# Commander data checksum
commander_offset = 0x52
CH=0x4B - 2
CY=0
for i in range(CH,0,-1):
CH = CH + CY + data_block[eliteb_offset + i + 7]
CY = (CH > 255) & 1
CH = CH % 256
CH = CH ^ data_block[eliteb_offset + i + 8]
print "Commander checksum = ", CH
# Must have Commander checksum otherwise game will lock:
data_block[eliteb_offset + commander_offset] = CH ^ 0xA9
data_block[eliteb_offset + commander_offset + 1] = CH
# Skip one byte for checksum0
checksum0_offset = len(data_block)
data_block.append(0)
# Append SHIPS file
ships_file = open('data/SHIPS.bin', 'rb')
data_block.extend(ships_file.read())
ships_file.close()
# Calculate checksum0
checksum0 = 0
for n in range(0x0, 0x4600):
checksum0 += data_block[n + 0x28]
print "checksum 0 = ", checksum0
if Encrypt == True:
data_block[checksum0_offset] = checksum0 % 256
# Encrypt data block
if Encrypt == True:
for n in range(0x0, len(data_block) - 0x28):
data_block[n + 0x28] ^= (n % 256)
# Calculate checksum1
checksum1 = 0
for n in range(0x0, 0x28):
checksum1 += data_block[n]
print "checksum 1 = ", checksum1
# Write output file for 'ELTcode'
output_file = open('output/ELTcode.bin', 'wb')
output_file.write(data_block)
output_file.close()
output_file = None
data_block = None
# Start again but for loader
print "Elite Loader Encyption"
loader_block = bytearray()
loader_file = open('output/ELITE.unprot.bin', 'rb')
loader_block.extend(loader_file.read())
loader_file.close()
# Reverse bytes between BLOCK and ENDBLOCK
BLOCK_offset = 0x14B0
ENDBLOCK_offset = 0x1530
for i in range(0,(ENDBLOCK_offset - BLOCK_offset)/2):
temp = loader_block[BLOCK_offset + i]
loader_block[BLOCK_offset + i] = loader_block[ENDBLOCK_offset - i - 1]
loader_block[ENDBLOCK_offset - i - 1] = temp
# Compute MAINSUM
MAINSUM_offset = 0x1335
MAINSUM = 0
for i in range(0,0x400):
MAINSUM += loader_block[i]
print "MAINSUM = ", MAINSUM
if Encrypt == True:
loader_block[MAINSUM_offset + 1] = MAINSUM % 256
# Compute CHECKbyt
CHECKbyt_offset = 0x1334
CHECKbyt = 0
for i in range(1,384):
CHECKbyt += loader_block[CHECKbyt_offset + i]
print "CHECKbyt = ", CHECKbyt
if Encrypt == True:
loader_block[CHECKbyt_offset] = CHECKbyt % 256
if Encrypt == True:
print "Encypting..."
# EOR TUT BLOCK (offset = 0x13e1)
for i in range(0,ENDBLOCK_offset - BLOCK_offset):
loader_block[0x13e1 + i] ^= loader_block[BLOCK_offset + i]
# EOR CODE words (offset = 0xf86)
for i in range(0,2):
for j in range(0,256):
if (j + i * 256 + CHECKbyt_offset) < len(loader_block):
loader_block[j + i * 256 + CHECKbyt_offset] ^= loader_block[j + 0xf86]
# EOR DATA block at beginning of loader
for i in range(0,0xf):
for j in range(0,256):
loader_block[j + i * 256] ^= loader_block[j + 0xf86]
# Write output file for 'ELITE'
output_file = open('output/ELITE.bin', 'wb')
output_file.write(loader_block)
output_file.close()
output_file = None