-
Notifications
You must be signed in to change notification settings - Fork 1
/
manifest.py
477 lines (370 loc) · 13.5 KB
/
manifest.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
import struct
import zlib
import hashlib
from cStringIO import StringIO
import os
import requests
import json
from collections import defaultdict
# ToDo JSON manifest reader
# manifest_file = 'spellbreak.manifest'
MANIFEST_HEADER_MAGIC = 0x44BEC00C
CHUNK_HEADER_MAGIC = 0xB1FE3AA2
def read_fstring(sio, return_empty=False):
length = struct.unpack('<i', sio.read(4))[0]
is_utf16 = False
if length < 0: # unicode
length *= -2
is_utf16 = True
elif length == 0:
if return_empty:
return '<empty>'
else:
return ''
if is_utf16:
s = sio.read(length)
return s.decode('utf-16')
else:
s = sio.read(length - 1)
sio.seek(1, 1) # skip string terminator
return s
def read_fstring_tset(sio):
_set = []
entries = struct.unpack('<I', sio.read(4))[0]
for i in xrange(entries):
_set.append(read_fstring(sio))
return _set
def get_chunk_dir(version):
chunk_version = 'Chunks'
if version >= 3:
chunk_version = 'ChunksV2'
if version >= 6:
chunk_version = 'ChunksV3'
if version >= 15:
chunk_version = 'ChunksV4'
return chunk_version
class Manifest(object):
def __init__(self):
self.header_size = 0
self.size_compressed = 0
self.size_uncompressed = 0
self.sha_hash = ''
self.stored_as = 0
self.version = 0
self._data = ''
self.data = ''
# remainder
self.meta = None
self.chunk_data_list = None
self.file_manifest_list = None
self.custom_fields = None
@property
def compressed(self):
return self.stored_as & 0x1
@classmethod
def read_all(cls, data):
_m = cls.read(data)
_tmp = StringIO(_m.data)
_m.meta = ManifestMeta.read(_tmp)
_m.chunk_data_list = CDL.read(_tmp)
_m.file_manifest_list = FML.read(_tmp)
_m.custom_fields = CustomFields.read(_tmp)
unhandled_data = _tmp.read()
if unhandled_data:
print 'Did not read {} bytes!'.format(len(unhandled_data))
open('unhandled.bin', 'wb').write(unhandled_data)
exit(1)
return _m
@classmethod
def read(cls, data):
sio = StringIO(data)
if struct.unpack('<I', sio.read(4))[0] == MANIFEST_HEADER_MAGIC:
print 'Magic OK!'
else:
raise ValueError('No header magic!')
_manifest = cls()
_manifest.header_size = struct.unpack('<I', sio.read(4))[0]
_manifest.size_compressed = struct.unpack('<I', sio.read(4))[0]
_manifest.size_ucompressed = struct.unpack('<I', sio.read(4))[0]
_manifest.sha_hash = sio.read(20)
_manifest.stored_as = struct.unpack('B', sio.read(1))[0]
_manifest.version = struct.unpack('<I', sio.read(4))[0]
_manifest._data = sio.read()
if _manifest.compressed:
_manifest.data = zlib.decompress(_manifest._data)
dec_hash = hashlib.sha1(_manifest.data).hexdigest()
if dec_hash != _manifest.sha_hash.encode('hex'):
raise ValueError('Hash does not match!')
# this is hacky, but required
ChunkInfo._chunk_format = get_chunk_dir(_manifest.version)
return _manifest
class ManifestMeta(object):
def __init__(self):
self.meta_size = 0
self.data_version = 0
self.feature_level = 0
self.is_file_data = False
self.app_id = 0
self.app_name = ''
self.build_version = ''
self.launch_exe = ''
self.launch_command = ''
self.prereq_set = []
self.prereq_name = ''
self.prereq_path = ''
self.prereq_args = ''
@classmethod
def read(cls, sio):
_meta = cls()
_meta.meta_size = struct.unpack('<I', sio.read(4))[0]
_meta.data_version = struct.unpack('B', sio.read(1))[0]
_meta.feat_lvl = struct.unpack('<I', sio.read(4))[0]
_meta.is_file_data = struct.unpack('B', sio.read(1))[0] == 1
_meta.app_id = struct.unpack('<I', sio.read(4))[0]
_meta.app_name = read_fstring(sio)
_meta.build_version = read_fstring(sio)
_meta.launch_exe = read_fstring(sio)
_meta.launch_command = read_fstring(sio)
_meta.prereq_set = read_fstring_tset(sio)
_meta.prereq_name = read_fstring(sio)
_meta.prereq_path = read_fstring(sio)
_meta.prereq_args = read_fstring(sio)
if sio.tell() != _meta.meta_size:
raise ValueError('Did not read entire meta!')
# seek to end if not already
# sio.seek(0 + _meta.meta_size)
return _meta
class CDL(object):
def __init__(self):
self.version = 0
self.size = 0
self.count = 0
self.elements = []
@property
def number(self):
return len(self.elements)
@classmethod
def read(cls, sio):
cdl_start = sio.tell()
_cdl = cls()
_cdl.size = struct.unpack('<I', sio.read(4))[0]
_cdl.version = struct.unpack('B', sio.read(1))[0]
_cdl.count = struct.unpack('<I', sio.read(4))[0]
for i in xrange(_cdl.count):
_cdl.elements.append(ChunkInfo())
# read guid
for chunk in _cdl.elements:
chunk.guid.append(struct.unpack('<I', sio.read(4))[0])
chunk.guid.append(struct.unpack('<I', sio.read(4))[0])
chunk.guid.append(struct.unpack('<I', sio.read(4))[0])
chunk.guid.append(struct.unpack('<I', sio.read(4))[0])
# hash
for chunk in _cdl.elements:
chunk.hash = struct.unpack('<Q', sio.read(8))[0]
# sha hash
for chunk in _cdl.elements:
chunk.sha_hash = sio.read(20)
# group number
for chunk in _cdl.elements:
chunk.group_num = struct.unpack('B', sio.read(1))[0]
# window size
for chunk in _cdl.elements:
chunk.window_size = struct.unpack('<I', sio.read(4))[0]
# file size
for chunk in _cdl.elements:
chunk.file_size = struct.unpack('<q', sio.read(8))[0]
if sio.tell() - cdl_start != _cdl.size:
raise ValueError('Did not read entire chunk data list!')
return _cdl
class ChunkInfo(object):
_chunk_format = 'ChunksV4'
def __init__(self):
self.guid = []
self.hash = 0
self.sha_hash = ''
self.group_num = 0
self.window_size = 0
self.file_size = 0
def __repr__(self):
return '<ChunkInfo (guid={}, hash={}, sha_hash={}, group_num={}, window_size={}, file_size={})>'.format(
self.guid_str, self.hash, self.sha_hash.encode('hex'), self.group_num, self.window_size, self.file_size
)
@property
def guid_str(self):
return '-'.join('{:08x}'.format(g) for g in self.guid)
@property
def path(self):
return '{}/{:02d}/{:016X}_{}.chunk'.format(
self._chunk_format,
(zlib.crc32(struct.pack('<I', self.guid[0]) +
struct.pack('<I', self.guid[1]) +
struct.pack('<I', self.guid[2]) +
struct.pack('<I', self.guid[3])) & 0xffffffff) % 100,
self.hash, ''.join('{:08X}'.format(g) for g in self.guid)
)
class FML(object):
def __init__(self):
self.version = 0
self.size = 0
self.count = 0
self.elements = []
@property
def number(self):
return len(self.elements)
@classmethod
def read(cls, sio):
fml_start = sio.tell()
_fml = cls()
_fml.size = struct.unpack('<I', sio.read(4))[0]
_fml.version = struct.unpack('B', sio.read(1))[0]
_fml.count = struct.unpack('<I', sio.read(4))[0]
for i in xrange(_fml.count):
_fml.elements.append(FileManifest())
for fm in _fml.elements:
fm.filename = read_fstring(sio)
for fm in _fml.elements:
fm.symlink_target = read_fstring(sio)
for fm in _fml.elements:
fm.hash = sio.read(20)
for fm in _fml.elements:
fm.flags = struct.unpack('B', sio.read(1))[0]
for fm in _fml.elements:
_elem = struct.unpack('<I', sio.read(4))[0]
for i in xrange(_elem):
fm.install_tags.append(read_fstring(sio))
for fm in _fml.elements:
_elem = struct.unpack('<I', sio.read(4))[0]
for i in xrange(_elem):
chunkp = ChunkPart()
_size = struct.unpack('<I', sio.read(4))[0]
chunkp.guid.append(struct.unpack('<I', sio.read(4))[0])
chunkp.guid.append(struct.unpack('<I', sio.read(4))[0])
chunkp.guid.append(struct.unpack('<I', sio.read(4))[0])
chunkp.guid.append(struct.unpack('<I', sio.read(4))[0])
chunkp.offset = struct.unpack('<I', sio.read(4))[0]
chunkp.size = struct.unpack('<I', sio.read(4))[0]
fm.chunk_parts.append(chunkp)
# calc size
for fm in _fml.elements:
fm.file_size = sum(c.size for c in fm.chunk_parts)
if sio.tell() - fml_start != _fml.size:
raise ValueError('Did not read entire chunk data list!')
return _fml
class FileManifest(object):
def __init__(self):
self.filename = ''
self.symlink_target = ''
self.hash = ''
self.flags = 0
self.install_tags = []
self.chunk_parts = []
self.file_size = 0
@property
def sha_hash(self):
return self.hash
def __repr__(self):
return '<FileManifest (filename="{}", symlink_target="{}", hash={}, flags={}, ' \
'install_tags=[{}], chunk_parts=[{}], file_size={})>'.format(
self.filename, self.symlink_target, self.hash.encode('hex'), self.flags,
', '.join(self.install_tags), ', '.join(repr(c) for c in self.chunk_parts),
self.file_size)
class ChunkPart(object):
def __init__(self):
self.guid = []
self.offset = 0
self.size = 0
@property
def guid_str(self):
return '-'.join('{:08x}'.format(g) for g in self.guid)
def __repr__(self):
guid_readable = '-'.join('{:08x}'.format(g) for g in self.guid)
return '<ChunkPart (guid={}, offset={}, size={})>'.format(
guid_readable, self.offset, self.size)
class CustomFields(object):
def __init__(self):
self.size = 0
self.version = 0
self.count = 0
self._dict = dict()
def __getitem__(self, item):
return self._dict.get(item, None)
def __str__(self):
return str(self._dict)
def keys(self):
return self._dict.keys()
def values(self):
return self._dict.values()
@classmethod
def read(cls, sio):
_cf = cls()
cf_start = sio.tell()
_cf.size = struct.unpack('<I', sio.read(4))[0]
_cf.version = struct.unpack('B', sio.read(1))[0]
_cf.count = struct.unpack('<I', sio.read(4))[0]
_keys = []
_values = []
for i in xrange(_cf.count):
_keys.append(read_fstring(sio))
for i in xrange(_cf.count):
_values.append(read_fstring(sio))
_cf._dict = dict(zip(_keys, _values))
if sio.tell() - cf_start != _cf.size:
raise ValueError('Did not read entire custom fields list!')
return _cf
class Chunk(object):
def __init__(self):
self.header_version = 0
self.header_size = 0
self.compressed_size = 0
self.hash = 0
self.stored_as = 0
self.guid = []
self.hash_type = 0
self.sha_hash = None
self.uncompressed_size = 1024 * 1024
self._sio = None
self._data = None
@property
def data(self):
if self._data:
return self._data
if self.compressed:
print 'Decompressing'
self._data = zlib.decompress(self._sio.read())
else:
self._data = self._sio.read()
return self._data
@property
def guid_str(self):
return '-'.join('{:08x}'.format(g) for g in self.guid)
@property
def compressed(self):
return self.stored_as & 0x1
@classmethod
def read_buffer(cls, data):
_sio = StringIO(data)
return cls.read(_sio)
@classmethod
def read(cls, sio):
head_start = sio.tell()
if struct.unpack('<I', sio.read(4))[0] != CHUNK_HEADER_MAGIC:
raise ValueError('Chunk magic doesn\'t match!')
_chunk = cls()
_chunk._sio = sio
_chunk.header_version = struct.unpack('<I', sio.read(4))[0]
_chunk.header_size = struct.unpack('<I', sio.read(4))[0]
_chunk.compressed_size = struct.unpack('<I', sio.read(4))[0]
_chunk.guid = [struct.unpack('<I', sio.read(4))[0],
struct.unpack('<I', sio.read(4))[0],
struct.unpack('<I', sio.read(4))[0],
struct.unpack('<I', sio.read(4))[0]]
_chunk.hash = struct.unpack('<Q', sio.read(8))[0]
_chunk.stored_as = struct.unpack('B', sio.read(1))[0]
if _chunk.header_version >= 2:
_chunk.sha_hash = sio.read(20)
_chunk.hash_type = struct.unpack('B', sio.read(1))[0]
if _chunk.header_version >= 3:
_chunk.uncompressed_size = struct.unpack('<I', sio.read(4))[0]
if sio.tell() - head_start != _chunk.header_size:
raise ValueError('Did not read entire chunk header!')
return _chunk