-
Notifications
You must be signed in to change notification settings - Fork 11
/
decode_zsp.py
122 lines (93 loc) · 2.38 KB
/
decode_zsp.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
#!/usr/bin/python
#
# Script decode/encode 'ZSP' files, used by GCE-3
# (c) Simon Wood, 11 Nov 2021
#
from construct import *
#--------------------------------------------------
# Define ZSP file format using Construct (v2.9)
# requires:
# https://github.com/construct/construct
VAR = Struct(
"name" / PaddedString(4, "Ascii"),
"vlength" / Int32ul,
"length" / Computed(this.vlength - 8),
"data" / Bytes(this.length),
)
ModelEnum = Enum(Byte,
G5N = 0x00,
G3N = 0x02,
G3XN = 0x03,
B3N = 0x04,
G1FOUR = 0x0C,
G1XFOUR = 0x0D,
B1FOUR = 0x0E,
B1XFOUR = 0x0F,
A1FOUR = 0x15,
A1XFOUR = 0x16,
)
HDIF = Struct(
Const(b"HDIF"),
"length" / Bitwise(Struct(
"length" / BitsInteger(4), # Nyble swap?
Padding(4),
Padding(24),
)),
"hexdump" / HexDump(Peek(Bytes(32))),
"model" / ModelEnum,
"unknown" / Bytes(31),
"data" / Array(this.length.length, VAR),
)
APIF = Struct(
Const(b"APIF"),
"length" / Bitwise(Struct(
"length" / BitsInteger(4), # Nyble swap?
Padding(4),
Padding(24),
)),
"data" / Array(this.length.length, VAR),
)
ZTYP = Struct(
Const(b"ZTYP"),
"unknown" / Int32ul,
"name" / PaddedString(12, "Ascii"),
"unknown2" / Int32ul,
)
ZDLT = Struct(
Const(b"ZDLT"),
"blength" / Int32ul, # in bytes
"length" / Computed(this.blength // 24),
"data" / Array(this.length, ZTYP),
)
ZSP = Struct(
Const(b"ZSPF"),
"length" / Int32ul,
"HDIF" / HDIF,
"APIF" / APIF,
"ZDLT" / ZDLT,
)
#--------------------------------------------------
def main():
from argparse import ArgumentParser
parser = ArgumentParser(prog="decode_zsp")
parser.add_argument('files', metavar='FILE', nargs=1,
help='File to process')
parser.add_argument("-d", "--dump",
help="dump configuration to text",
action="store_true", dest="dump")
options = parser.parse_args()
if not len(options.files):
parser.error("FILE not specified")
# Read data from file
infile = open(options.files[0], "rb")
if not infile:
sys.exit("Unable to open FILE for reading")
else:
data = infile.read()
infile.close()
if data:
config = ZSP.parse(data)
if options.dump:
print(config)
if __name__ == "__main__":
main()