-
Notifications
You must be signed in to change notification settings - Fork 9
/
d77dec.py
61 lines (50 loc) · 1.75 KB
/
d77dec.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
#d77dec.py
# Decode D77 Disk image file into JSON file
import os
import sys
import argparse
import json
from d77lib import *
def byteEncoder(object):
if isinstance(object, bytes):
res = ''
for i, dt in enumerate(object):
if i != 0:
res += ','
res += '0x{:02x}'.format(dt)
return res
def main(args):
with open(args.input, 'rb') as f:
img = f.read()
if args.output is None:
base, ext = os.path.splitext(args.input)
out_name = base + '.json'
else:
out_name = args.input
base, ext = os.path.splitext(out_name)
# Divide multiple images from a file
img_array = []
while True:
size = get_dword(img, 0x1c) # D77 image size
img_array.append(img[:size])
if len(img) <= size:
break
img = img[size:]
print(len(img_array), 'image data found')
for idx, img in enumerate(img_array):
d77dic = decode_d77(img)
del d77dic['sector_offsets']
if 'track_offsets' in d77dic.keys():
del d77dic['track_offsets']
out_name = '{}_{}{}'.format(base, idx, ext)
print('generating', out_name)
with open(out_name, 'w') as f:
json.dump(d77dic, f, indent=2, default=byteEncoder)
if __name__ == "__main__":
print('** D77 image to JSON decoder')
parser = argparse.ArgumentParser()
parser.add_argument('-i', '--input', type=str, required=True, help='input D77 image file path')
parser.add_argument('-o', '--output', type=str, required=False, default=None, help='output txt file path')
parser.add_argument('--extension', action='store_true', default=False, help='Extract D77mod extension information')
args = parser.parse_args()
main(args)