-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathunpacker.py
309 lines (259 loc) · 10.4 KB
/
unpacker.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
import logging
import os
import re
import shutil
import sys
import zlib
DEV_NULL = 'nul' if sys.platform == 'win32' else '/dev/null'
def unpack(f, dest_dir=None, do_decompile=False):
if not os.path.exists(f):
raise(OSError('File does not exist'))
os.environ['PATH'] = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'bin', sys.platform) + os.pathsep + os.environ['PATH']
fn = os.path.basename(f)
bn, ext = os.path.splitext(fn)
ext = ext.lower()
if os.path.isdir(f) and ext == '.app': # macOS projector.app bundle?
fn_bin = os.path.join(f, 'Contents', 'MacOS', bn)
bin_file = None
if os.path.isdir(fn_bin):
bin_file = fn_bin
else:
l = os.listdir(os.path.join(f, 'Contents', 'MacOS'))
if len(l):
bin_file = os.path.join(f, 'Contents', 'MacOS', l[0])
if bin_file is not None:
logging.info(f'Unpacking Mac OS X/macOS projector "{fn}"...')
output_dir = os.path.join(dest_dir if dest_dir else os.path.dirname(f), bn + '_contents')
if os.path.isdir(output_dir):
shutil.rmtree(output_dir)
os.mkdir(output_dir)
return unpack_projector(bin_file, output_dir, do_decompile)
elif ext == '.exe': # Windows projector.exe?
logging.info(f'Unpacking Windows projector "{fn}"...')
output_dir = os.path.join(dest_dir if dest_dir else os.path.dirname(f), bn + '_contents')
if os.path.isdir(output_dir):
shutil.rmtree(output_dir)
os.mkdir(output_dir)
return unpack_projector(f, output_dir, do_decompile)
# check if it's an old Mac OS 9- 68k/PPC/FAT binary
with open(f, 'rb') as fh:
magic = fh.read(4)
is_macos_bin = magic[:2] == b'PJ' or magic[2:] == b'JP'
if not is_macos_bin:
MAGIC_MACOS = [
b'Joy!', # D10- mac projector
b'\xCA\xFE\xBA\xBE', # D11+ Projector Resource
b'\xCE\xFA\xED\xFE', # D11+ Projector Intel Resource
b'RIFX' # data fork of classic mac app
]
for m in MAGIC_MACOS:
if magic == m:
is_macos_bin = True
break
if is_macos_bin:
logging.info(f'Unpacking Mac OS projector "{fn}"...')
output_dir = os.path.join(dest_dir if dest_dir else os.path.dirname(f), bn + '_contents')
if os.path.isdir(output_dir):
shutil.rmtree(output_dir)
os.mkdir(output_dir)
return unpack_projector(f, output_dir, do_decompile)
raise(TypeError('File not supported'))
def unpack_projector (exe_file, output_dir, do_decompile=False):
with open(exe_file, 'rb') as fh:
data_full = fh.read()
m1 = re.search(b'RIFX([\x00-\xFF]{4})APPL', data_full)
m2 = re.search(b'XFIR([\x00-\xFF]{4})LPPA', data_full)
if m1 and m2:
start_pos = m1.start() if m1.start() < m2.start() else m2.start()
byteorder = 'big' if m1.start() < m2.start() else 'little'
elif m1:
start_pos = m1.start()
byteorder = 'big'
elif m2:
start_pos = m2.start()
byteorder = 'little'
else:
raise(TypeError('Could not identify file as Director projector'))
offset = 32
data_full = data_full[offset:]
exe_size = len(data_full)
# find RIFX/XFIR and RIFF chunks
res = []
res2 = []
xres = []
start_pos += 12
data = data_full[start_pos:]
# find embedded movies/castlibs
if byteorder == 'big':
for m in re.finditer(b'RIFX([\x00-\xFF]{4})(MV93|MC95)', data):
res.append([m.start(), m.group()[8:]])
for m in re.finditer(b'RIFX([\x00-\xFF]{4})(FGDM|FGDC)', data):
res2.append([m.start(), m.group()[8:]])
else:
for m in re.finditer(b'XFIR([\x00-\xFF]{4})(39VM|59CM)', data):
res.append([m.start(), m.group()[8:]])
for m in re.finditer(b'XFIR([\x00-\xFF]{4})(MDGF|CDGF)', data):
res2.append([m.start(), m.group()[8:]])
if len(res) == 0:
if len(res2) == 0:
return print('Nothing found to extract!')
else:
compressed = True
res = res2
else:
compressed = False
# find embedded xtras
for m in re.finditer(b'RIFF([\x00-\xFF]{4})XtraFILE', data):
xres.append(m.start())
is_16bit = False
if len(xres) == 0:
for m in re.finditer(b'XFIR([\x00-\xFF]{4})artX', data):
xres.append(m.start())
is_16bit = len(xres) > 0
# header template
header = ((b'RIFX' if byteorder == 'big' else b'XFIR') +
int.to_bytes(exe_size - 8 + offset, 4, byteorder) +
(b'MV93imap' if byteorder == 'big' else b'39VMpami') +
int.to_bytes(24, 4, byteorder) +
int.to_bytes(1, 4, byteorder))
# extract file names from Dict chunk
dir_names = []
xtra_names = []
pos = res[0][0] # position of first XFIR/RIFX chunk
# find last 'Dict' (littleEndian: 'tciD') before this position
m = None
for m in re.finditer(b'Dict' if byteorder == 'big' else b'tciD', data[:pos-3]):
pass
if m:
dict = data[m.start():pos]
cnt = int.from_bytes(dict[24:28], byteorder)
byteorder_dict = byteorder
offset_dict = 0
if cnt > 0xFFFF: # 16-bit win projector
byteorder_dict = 'big'
cnt = int.from_bytes(dict[24:28], byteorder_dict)
offset_dict = 2
if cnt == 1:
# finding actual original filename would require parsing .dir file, so we use the projector name instead
bn, _ = os.path.splitext(os.path.basename(exe_file))
dir_names.append(bn + '.dxr')
else:
pt = cnt * 8 + 64 - offset_dict
for i in range(cnt):
flen = int.from_bytes(dict[pt:pt+4], byteorder_dict)
fn = dict[pt + 4:pt + 4 + flen]
if b'Xtras:' in fn or fn.endswith(b' Xtra') or fn.lower().endswith(b'.x32') or fn.lower().endswith(b'.x16') or fn.endswith(b'.cpio'):
xtra_names.append(get_filename(fn.decode()))
else:
dir_names.append(get_filename(fn.decode()))
if i < cnt - 1:
pt += 4 + flen + (4 - flen % 4 if flen % 4 else 0)
if compressed: # compressed files
logging.info('Files in projector are compressed.')
file_num = 0
for r in res:
pos = r[0]
fn = dir_names[file_num]
file_num += 1
_, ext = os.path.splitext(fn)
ext = ext.lower()
if ext == '':
fn += '.dcr' # just guessing
else:
fn = fn[:-4] + ('.cct' if ext == '.cst' else '.dcr')
chunk_size = int.from_bytes(data[pos + 4:pos + 8], byteorder)
fn = os.path.join(output_dir, sanitize_filename(fn))
with open(fn, 'wb') as fh:
fh.write(data[pos:pos + chunk_size + 8])
if do_decompile:
decompile(fn)
rebuild(fn)
else: # non-compressed files
logging.info('Files in projector are not compressed.')
file_num = 0
for r in res:
pos = r[0] + 32 + offset
fn = dir_names[file_num]
file_num += 1
_, ext = os.path.splitext(fn)
ext = ext.lower()
if ext == '': # just guessing
fn += '.dxr'
else:
fn = fn[:-4] + ('.cxt' if ext == '.cst' else '.dxr')
fn = os.path.join(output_dir, sanitize_filename(fn))
with open(fn, 'wb') as fh:
fh.write(header)
fh.write(int.to_bytes(start_pos + pos + 12, 4, byteorder))
fh.write(int.to_bytes(1923, 4, byteorder))
fh.write(data_full)
if do_decompile:
decompile(fn)
rebuild(fn)
# extract xtras
file_num = 0
if is_16bit:
for pos in xres:
pos += 49
xnam_len = int.from_bytes(data[pos:pos+4], byteorder)
pos += xnam_len + 8 + xnam_len % 2
data_len = int.from_bytes(data[pos:pos+4], byteorder)
pos += 4
xdata = data[pos:pos + data_len]
fn = xtra_names[file_num]
file_num += 1
xdata = zlib.decompress(xdata)
with open(os.path.join(output_dir, sanitize_filename(fn)), 'wb') as fh:
fh.write(xdata)
else:
for pos in xres:
chunk_size = int.from_bytes(data[pos:pos+4], 'big') # always big endian
xdata = data[pos + 48:pos + 48 + chunk_size]
fn = xtra_names[file_num]
file_num += 1
xdata = zlib.decompress(xdata)
with open(os.path.join(output_dir, sanitize_filename(fn)), 'wb') as fh:
fh.write(xdata)
return output_dir, len(res), len(xres)
def rebuild(fn):
dest_file = fn + '.tmp'
exit_code = os.system(f'ProjectorRays --rebuild-only "{fn}" "{dest_file}" >{DEV_NULL}')
if exit_code != 0:
raise(OSError('Rebuilding failed'))
if os.path.isfile(dest_file):
os.unlink(fn)
os.rename(dest_file, fn)
def decompile(fn):
dest_file, ext = os.path.splitext(fn)
ext = ext.lower()
dest_file += ('_decompiled.cst' if ext == '.cxt' or ext == '.cct' else '_decompiled.dir')
exit_code = os.system(f'ProjectorRays "{fn}" "{dest_file}" >{DEV_NULL}')
if exit_code != 0:
raise(OSError('Decompiling failed'))
def get_filename(fn):
''' cross-platform, extracts filename of Windows, POSIX or Mac OS path '''
if "/" in fn:
pd = "/" # POSIX (macOS/Mac OS X)
elif "\\" in fn:
pd = "\\" # Windows
else:
pd = ":" # classic Mac OS
return fn.split(pd)[-1]
def sanitize_filename(fn):
for c in r'\/:*?"<>|':
fn = fn.replace(c, '_')
return fn
if __name__ == '__main__':
args = sys.argv[1:]
verbose = '-verbose' in args
if verbose:
logging.basicConfig(level = logging.INFO, format = '[%(levelname)s] %(message)s')
args.remove('-verbose')
do_decompile = '-decompile' in args
if do_decompile:
args.remove('-decompile')
if len(args):
output_dir, num_dirs, num_xtras = unpack(args[0], do_decompile=do_decompile)
print(f'Done. {num_dirs+num_xtras} files were extracted to "{output_dir}".')
else:
print('Usage: python unpacker.py [-verbose] [-decompile] <projector-file>')