forked from institution/mpskit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hag.py
78 lines (54 loc) · 1.57 KB
/
hag.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
import os
from common import *
verbose = 0
def read_madsconcat(f, fname):
output_path = '{}.dir'.format(fname)
lst_name = '{}.lst'.format(fname)
if not os.path.exists(output_path):
os.mkdir(output_path)
output(output_path)
output(lst_name)
with open(lst_name, 'w') as lst_file:
# idstring
read_idstring(f, b"MADSCONCAT 1.0\x1A\x00")
# number of files
num_files, = read_struct(f, "<H")
curr_header = f.tell()
for _ in range(num_files):
# file header
f.seek(curr_header)
offset,length,name14 = read_struct(f, "<II14s")
name = get_asciiz(name14)
curr_header = f.tell()
# file content
f.seek(offset)
oname = os.path.join(output_path,name)
with open(oname, 'wb') as out:
out.write(f.read(length))
# lst entry
lst_file.write(name+'\n')
def write_madsconcat(fname):
f = open(fname, 'wb')
dir_path = '{}.dir'.format(fname)
lst_name = '{}.lst'.format(fname)
lst = [fn.strip() for fn in open(lst_name, 'r').readlines()]
# idstring
f.write(b"MADSCONCAT 1.0\x1A\x00")
# number of files
numfiles = len(lst)
write_struct(f, "<H", (numfiles,))
curr_header = f.tell()
curr_offset = f.tell() + numfiles * struct.calcsize("<II14s")
for subfname in lst:
fpath = os.path.join(dir_path, subfname)
length = os.path.getsize(fpath)
# file header
f.seek(curr_header)
write_struct(f, "<II14s", (curr_offset,length,subfname.encode('ascii')))
curr_header = f.tell()
# file content
f.seek(curr_offset)
f.write(open(fpath,'rb').read())
curr_offset = f.tell()
f.close()
output(fname)