-
Notifications
You must be signed in to change notification settings - Fork 0
/
disassemble-charaden.py
51 lines (39 loc) · 1.39 KB
/
disassemble-charaden.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
# -*- coding: utf-8 -*-
import os
import sys
def extract(magic: bytes, content: bytes):
ret = []
offset = 0
while True:
offset = content.find(magic, offset)
if (offset == -1):
break
if (int.from_bytes(content[offset-2:offset]) != 0):
offset += 1
continue
size = int.from_bytes(content[offset-4:offset-2], byteorder="little")
ret.append(content[offset:offset+size])
offset += 1
return ret
def main(file_path):
file_name = os.path.splitext(os.path.basename(file_path))[0]
dir_name = os.path.dirname(file_path)
with open(file_path, "rb") as f:
charaden_content = f.read()
mbacs = extract(b"MB", charaden_content)
mtras = extract(b"MT", charaden_content)
gifs = extract(b"GIF", charaden_content)
def output(contents, ext):
i = 0
for content in contents:
output_path = os.path.join(dir_name, f"{file_name}_{i}.{ext}")
with open(output_path, "wb") as f:
f.write(content)
print(f"{os.path.basename(output_path)}: done!")
i += 1
output(mbacs, ext="mbac")
output(mtras, ext="mtra")
output(gifs, ext="gif")
if __name__ == "__main__":
for file_path in sys.argv[1:]:
main(file_path)