Debian Codegraph @

doc/reveng/retevis/rt84/extract.py

raw
1 #!/usr/bin/env python3
2
3 import struct
4 import sys
5
6
7 def hexDump(s: bytes, prefix="", addr=0, compact=False) -> str:
8 """ Utility function to hex-dump binary data. """
9 N = len(s)
10 Nb = N//16
11 if (N%16): Nb += 1
12 res = ""
13 last_line = None
14 compressing = False
15 for j in range(Nb):
16 a,b = j*16, min((j+1)*16,N)
17 line = s[a:b]
18 if (not compact) or (last_line != line):
19 h = " ".join(map("{:02x}".format, line))
20 h += " "*(16-(b-a))
21 t = ""
22 for i in range(a,b):
23 c = s[i]
24 if c>=0x20 and c<0x7f:
25 t += chr(c)
26 else:
27 t += "."
28 res += (prefix + "{:08X} ".format(addr+16*j) + h + " | " + t + "\n")
29 last_line = line
30 compressing = False
31 elif not compressing:
32 res += (prefix + "*\n")
33 compressing = True
34 return res[:-1]
35
36
37 if 2 > len(sys.argv):
38 print("USAGE: extract.py CODEPLUG.rdt")
39 rdt_filename = sys.argv[1]
40
41 # list of sections: (file offset, size, destination address)
42 sections = [
43 (0x002225, 0x03e000, 0x002000),
44 (0x040230, 0x090000, 0x110000),
45 ]
46
47 # Read all
48 data = open(rdt_filename, "br").read()
49 for section in sections:
50 offset, length, addr = section
51 print(hexDump(data[offset:(offset+length)], "", addr, True))
52
53