-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.py
288 lines (210 loc) · 8.33 KB
/
parse.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#!/usr/bin/env python3
# seag-romtools
# Copyright (C) 2024 wilszdev
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import struct
import sys
ERR_OK = 0x00
ERR_USAGE = 0x01
def main() -> int:
if len(sys.argv) < 2:
sys.stderr.write(f'Usage: {sys.argv[0]} FILE0 [FILE...]\n')
return ERR_USAGE
for filePath in sys.argv[1:]:
try:
inputFile = sys.stdin.buffer if filePath == '-' else open(filePath, 'rb')
except OSError:
sys.stderr.write(f'Error: Failed to open file {filePath}\n')
continue
with inputFile:
data = inputFile.read()
root = build_root_container(data)
root.print()
return ERR_OK
EXTRA_SPACE_ID = 0x00
ROOT_CONTAINER_ID = 0x1d
# so that type hints work
class Element: pass
class Element:
def __init__(self):
self.id = 0xff
self.elements: list[Element] = []
def header_blob(self) -> bytes:
return bytes()
def footer_blob(self) -> bytes:
return bytes()
def to_blob(self) -> bytes:
blob = self.header_blob()
for element in self.elements:
blob += element.to_blob()
blob += self.footer_blob()
return blob
def print(self, level: int = 0) -> None:
print(f'{" " * level}{self.__class__.__name__} 0x{self.id:02x}: 0x{len(self.elements):02x} children. size=0x{len(self.to_blob()):06x}')
for element in self.elements:
element.print(level + 1)
class Blob(Element):
def __init__(self, id: int, data: bytes):
super().__init__()
self.id = id
self.data = data
def header_blob(self) -> bytes:
return self.data
class File(Element):
CHUNK_SIZE = 0x40
def __init__(self, data: bytes):
super().__init__()
self.packed, self.id, self.type, self.unknown, self.size, self.loadAddress = \
self.parse_header(data[:8])
self.blob = data[8:8+self.size]
def header_blob(self) -> bytes:
fileInfo = 0
fileInfo |= (1 if self.packed else 0)
fileInfo |= self.id << 1
fileInfo |= self.type << 5
fileInfo &= 0xff
sizeBytes = len(self.blob) % File.CHUNK_SIZE
sizeBytesAndUnknown = 0
sizeBytesAndUnknown |= sizeBytes << 2
sizeBytesAndUnknown |= self.unknown
sizeChunks = len(self.blob) // File.CHUNK_SIZE
return struct.pack('<BBHL',
fileInfo, sizeBytesAndUnknown, sizeChunks, self.loadAddress)
def footer_blob(self) -> bytes:
return self.blob
@staticmethod
def parse_header(data: bytes):
assert len(data) == 8
# data is packed together in slightly annoying bit fields
fileInfo, sizeBytesAndUnknown, sizeChunks, loadAddress = \
struct.unpack('<BBHL', data)
packed = (fileInfo & 1) == 1 # least significant bit is packed flag
fileId = (fileInfo >> 1) & 0x0f # followed by 4-bit file id
fileType = (fileInfo >> 5) # followed by 3-bit file type
sizeBytes = (sizeBytesAndUnknown & 0xf0) >> 2
size = sizeChunks * File.CHUNK_SIZE + sizeBytes
unknown = sizeBytesAndUnknown & 0x0f
return packed, fileId, fileType, unknown, size, loadAddress
class Directory(Element):
def __init__(self, id: int, data: bytes):
super().__init__()
self.id = id
offset = 0
while offset + 8 <= len(data):
file = File(data[offset:])
offset += len(file.to_blob())
self.elements.append(file)
if file.id == 0:
break
# remaining space is spare
if offset != len(data):
self.elements.append(Blob(0, data[offset:]))
class Container(Element):
PRE_TABLE_HEADER_LENGTH = 32
def __init__(self, id: int, data: bytes):
super().__init__()
self.id = id
self.preSegmentTableHeader = data[:self.PRE_TABLE_HEADER_LENGTH]
self.signature_assert(data)
# parse the table, construct contents
_, firstElementOffset = self.parse_entry(data[self.PRE_TABLE_HEADER_LENGTH:self.PRE_TABLE_HEADER_LENGTH+4])
if firstElementOffset:
elements = self.get_elements_old(data)
else:
elements = self.get_elements_new(data)
for i in range(len(elements) - 1):
currentId, currentOffset = elements[i]
_, nextOffset = elements[i + 1]
# carve out the data and decide the type
elementData = data[currentOffset:nextOffset]
if currentId == ROOT_CONTAINER_ID or currentOffset == 0:
element = Blob(currentId, elementData)
else:
element = self.create_element(currentId, elementData)
if element:
self.elements.append(element)
lastIndex = -1
lastId, lastOffset = elements[lastIndex]
while lastOffset == 0:
lastIndex -= 1
_, lastOffset = elements[lastIndex]
lastData = data[lastOffset:]
if element := self.create_element(lastId, lastData):
self.elements.append(element)
def get_elements_old(self, data: bytes):
_, firstElementOffset = self.parse_entry(data[self.PRE_TABLE_HEADER_LENGTH:self.PRE_TABLE_HEADER_LENGTH+4])
table = data[self.PRE_TABLE_HEADER_LENGTH:firstElementOffset]
return [self.parse_entry(table[i:i+4]) for i in range(0, len(table), 4)]
def get_elements_new(self, data: bytes):
elements = []
table = data[self.PRE_TABLE_HEADER_LENGTH:]
tableOffset = 0
while 1:
id, offset = self.parse_entry(table[tableOffset:tableOffset+4])
elements.append((id, offset))
if id == EXTRA_SPACE_ID:
return elements
tableOffset += 4
def signature_assert(self, data: bytes):
pass
@staticmethod
def create_element(id: int, data: bytes):
if len(data) > 36 and data[16:20] == b'csiD':
return DiscContainer(id, data)
if len(data) < 8:
return Blob(id, data)
*_, size, loadAddress = File.parse_header(data[:8])
if 0 <= size <= len(data) and loadAddress != 0xffffffff:
return Directory(id, data)
return Blob(id, data)
@staticmethod
def parse_entry(data: bytes) -> tuple[int, int]:
assert len(data) == 4
data += b'\0'
return struct.unpack('<BI', data)
def header_blob(self) -> bytes:
# if the first element is the root container ID, we don't
# need to build the header blob.
if self.elements and self.elements[0].id == ROOT_CONTAINER_ID:
return bytes()
# construct the segment table.
# would help to figure out what some of the other parameters are
# in the first 32 bytes, but not a big deal
header = self.preSegmentTableHeader
offset = len(header) + 4 * len(self.elements)
for element in self.elements:
header += struct.pack('<BI', element.id & 0xff, offset & 0xffffffff)[:4]
size = len(element.to_blob())
if size:
offset += size
else:
offset = 0
return header
class DiscContainer(Container):
PRE_TABLE_HEADER_LENGTH = 32
def signature_assert(self, data: bytes):
assert data[16:20] == b'csiD'
class OldContainer(Container):
PRE_TABLE_HEADER_LENGTH = 16
def signature_assert(self, data: bytes):
assert data[16:20] != b'csiD'
def build_root_container(data: bytes):
# check for "Disc" signature to determine root container type
if data[16:20] == b'csiD':
return DiscContainer(ROOT_CONTAINER_ID, data)
else:
return OldContainer(ROOT_CONTAINER_ID, data)
if __name__ == '__main__':
exit(main())