-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecoder.py
384 lines (320 loc) · 13.8 KB
/
decoder.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
def readRom(romFile, startAddress, tableSize):
"""
Reads a segment of the ROM file from startOffset to endOffset.
Parameters:
romFile (str): The path to the ROM file.
startOffset (int): The starting position in the file to read from.
tableSize (int): Table pointer Size.
Returns:
bytes: The data read from the ROM file.
"""
with open(romFile, "rb") as f:
f.seek(startAddress)
data = f.read(tableSize)
return data
def readTbl(tblFile):
"""
Reads a .tbl file to create a character mapping table (supports DTE/MTE).
Parameters:
tblFile (str): The path to the .tbl file.
Returns:
dict: A dictionary where the keys are byte values (int) and the values are strings (characters or sequences).
"""
charTable = {}
with open(tblFile, "r", encoding="UTF-8") as f:
for line in f:
if line.startswith(";") or line.startswith("/"):
continue
if "=" in line:
hexValue, chars = line.split("=",1)
if "~" in chars:
continue
try:
hexValue = int(hexValue, 16)
chars = chars.rstrip("\n")
charTable[hexValue] = chars
except ValueError:
continue
return charTable
def processPointers2Bytes(data, header):
"""
Processes the pointer data by converting it to pairs and transforming it to big-endian,
then adding the header offset.
Parameters:
data (bytes): The raw pointer data read from the ROM.
header (int): The offset to add to each pointer.
Returns:
list: A list of processed pointers as integers.
"""
result = []
for i in range(0, len(data), 2):
# Read two bytes as a pair and convert to big-endian
pair = data[i:i + 2][::-1]
# Convert the pair to an integer
value = int.from_bytes(pair, byteorder='big') + header
result.append(value)
return result
def processPointers2BytesBigEndian(data, header):
"""
Processes the pointer data by converting it to pairs,
then adding the header offset.
Parameters:
data (bytes): The raw pointer data read from the ROM.
header (int): The offset to add to each pointer.
Returns:
list: A list of processed pointers as integers.
"""
result = []
for i in range(0, len(data), 2):
# Read two bytes as a pair and convert to big-endian
pair = data[i:i + 2]
# Convert the pair to an integer
value = int.from_bytes(pair, byteorder='big') + header
result.append(value)
return result
def processPointers2BytesSeparated(data, header):
"""
Processes the pointer data by converting it to pairs and transforming it to big-endian,
then adding the header offset.
Parameters:
data (bytes): The raw pointer data read from the ROM.
header (int): The offset to add to each pointer.
Returns:
list: A list of processed pointers as integers.
"""
result = []
half = len(data) // 2
for i in range(half):
# reorder list
firstByte = data[i]
secondByte = data[i + half]
# Read two bytes as a pair and convert to big-endian
pair = [firstByte, secondByte][::-1]
# Convert the pair to an integer
value = int.from_bytes(pair, byteorder='big') + header
result.append(value)
return result
def processPointers3Bytes(data, header):
"""
Processes the pointer data by converting it to triplets of 3 bytes,
then reversing the last two bytes, and transforming to big-endian,
and adding the header offset.
Parameters:
data (bytes): The raw pointer data read from the ROM.
header (int): The offset to add to each pointer.
Returns:
list: A list of processed pointers as integers.
"""
result = []
for i in range(0, len(data), 3):
# Read three bytes as a triplet
triplet = data[i:i + 3]
# Get last two byte of the triplet and convert to big-endian
get2Bytes = triplet[1:][::-1]
# Convert the final triplet to an integer
value = int.from_bytes(get2Bytes, byteorder='big') + header
result.append(value)
return result
def processPointers4Bytes(data, header):
"""
Processes the pointer data by converting it to pairs and transforming it to big-endian,
then adding the header offset.
Parameters:
data (bytes): The raw pointer data read from the ROM.
header (int): The offset to add to each pointer.
Returns:
list: A list of processed pointers as integers.
"""
result = []
for i in range(0, len(data), 4):
# Read four bytes as a quartet
quartet = data[i:i + 4]
# Convert the final triplet to an integer
value = int.from_bytes(quartet, byteorder='big') + header
result.append(value)
return result
def extractTexts(romData, addressesList, lineBreakers, charTable):
"""
Extracts texts from the ROM data at specified addresses until a line breaker is encountered.
Parameters:
romData (bytes): The complete ROM data.
addressesList (list): A list of addresses to read the texts from.
lineBreakers (set): A set of byte values used as line breakers.
charTable (dict): A dictionary mapping byte values to characters or sequences.
Returns:
tuple: Containing:
- texts (list): Script text.
- totalBytesRead (int): Total text block size.
- linesLength (int): Lenght of each line.
tuple: A list of extracted texts, the total bytes read, and the lengths of each extracted line in bytes.
"""
texts = []
linesLength = []
bytesLineCounter = 0
total= 0
# Loop over each starting address in the list
for addr in addressesList:
text = bytearray()
decodedValidCharacter = False
while True:
byte = romData[addr]
bytesLineCounter += 1
# If the byte is a line-breaker, stop extracting
if byte in lineBreakers and decodedValidCharacter:
breakerByte = byte
break
# Map the byte using charTable to get the character
char = charTable.get(byte, None)
if char:
# If single character
if len(char) == 1:
text.append(ord(char))
decodedValidCharacter = True
# If multiple characters (DTE/MTE)
else:
for c in char:
text.append(ord(c))
decodedValidCharacter = True
# If byte is not in charTable, print in format ~hex~
else:
hexValue = format(byte, '02X')
text.extend(f"~{hexValue}~".encode('UTF-8'))
addr += 1
# Add the breaker byte to the text
if breakerByte is not None:
char = charTable.get(breakerByte, None)
if char:
# if assigned to a single character
if len(char) == 1:
text.append(ord(char))
# if assigned to a chain characters
else:
for c in char:
text.append(ord(c))
else:
# If the breaker byte doesn't have a mapping, print in format ~hex~
hexValue = format(breakerByte, '02X')
text.extend(f"~{hexValue}~".encode('UTF-8'))
# Convert byte array to string
decodeText = text.decode('iso-8859-1', errors='replace')
# Append the decoded text to the list
texts.append(decodeText)
linesLength.append(bytesLineCounter)
total = total + bytesLineCounter
bytesLineCounter = 0
# Calculate total bytes read
totalBytesRead = abs((addressesList[-1] + linesLength[-1]) - addressesList[0])
return texts, totalBytesRead, linesLength
def extractTextsNoLineBreakers(romData, addressesList, endOffset, charTable):
"""
Extracts texts from the ROM data at specified addresses based on the lengths in linesLength.
Parameters:
romData (bytes): The complete ROM data.
addressesList (list): A list of addresses to read the texts from.
endOffset (set): The final offset after the last address.
charTable (dict): A dictionary mapping byte values to characters or sequences.
Returns:
tuple: Containing:
- texts (list): Extracted script text.
- totalBytesRead (int): Total text block size.
- linesLength (list): Length of each line in bytes.
"""
texts = []
linesLength = []
total = 0
# Add final offset to the addressesList
addressesList.append(int(endOffset.pop()))
# Calculate lines lenght of each segment is the difference between consecutive addresses
for i in range(len(addressesList) - 1):
length = int(addressesList[i + 1]) - int(addressesList[i])
linesLength.append(length)
# Loop over each starting address in the list and use linesLength for determining byte ranges
for i in range(len(addressesList) - 1):
startAddr = addressesList[i]
length = linesLength[i] # Get the length for this segment
endAddr = startAddr + length
text = bytearray()
decodedValidCharacter = False
# Read bytes from the starting address to the end address (using the specified length)
for addr in range(startAddr, endAddr):
byte = romData[addr]
# Map the byte using charTable to get the character
char = charTable.get(byte, None)
if char:
# If single character
if len(char) == 1:
text.append(ord(char))
decodedValidCharacter = True
# If multiple characters (DTE/MTE)
else:
for c in char:
text.append(ord(c))
decodedValidCharacter = True
# If byte is not in charTable, print in format ~hex~
else:
hexValue = format(byte, '02X')
text.extend(f"~{hexValue}~".encode('UTF-8'))
# Convert byte array to string
decodedText = text.decode('iso-8859-1', errors='replace')
# Append the decoded text to the list
texts.append(decodedText)
total += length
# Calculate total bytes read (this will be the sum of all lengths in linesLength)
totalBytesRead = total
return texts, totalBytesRead, linesLength
def parseLineBreakers(string):
"""
Parse a string of comma-separated hexadecimal values into a set of integers.
Parameters:
string (str): A string containing hexadecimal values separated by commas.
Returns:
lineBreakers: A set of integer values representing the line breakers.
"""
lineBreakers = set()
for byte in string.split(','):
byte = byte.strip()
lineBreakers.add(int(byte, 16))
# If the lineBreaker are the final text offset
is_offset = any(value > 255 for value in lineBreakers)
return lineBreakers, is_offset
def formatHexString(hexString):
"""
Takes a string of hex values separated by commas
and returns a string separated by ~.
Parameters:
hexString (str): A comma-separated string of hex values.
Returns:
str: The formatted string.
"""
# Eliminar los prefijos '0x' y los espacios
hexValues = hexString.split(',')
# Formatear cada valor eliminando '0x' y envolviéndolos en '~'
formattedValues = [f"-{val.strip()[2:].zfill(2).upper()}" for val in hexValues]
# Unir los valores con el delimitador vacío
formattedString = ''.join(formattedValues)
return formattedString
def writeOutFile(file, scriptText, pointersStartAddress, pointerTableSize, addressList, linesLenght, lineBreaker):
"""
Writes data to a file, formatting each line with a semicolon and newline.
Parameters:
file (str): The path to the output file.
scriptText (list): A list of strings representing the script content to write to the file.
pointersStartAddress (int): The starting address of the pointer table.
pointerTableSize (int): The size of the pointer table).
addressList (list): A list of addresses corresponding to each line in the script.
linesLenght (list): A list of the length of each line in the script.
lineBreaker (int): A value used to split lines.
"""
with open(file, "w", encoding='UTF-8') as f:
formattedString = formatHexString(lineBreaker)
f.write(f";{{{pointersStartAddress:08X}-{(pointersStartAddress + pointerTableSize - 1):08X}-{pointerTableSize:08X}}}{formattedString}\n")
i = 0
for line in scriptText:
# Format the address as uppercase hex with leading zeros (8 digits wide)
addressStr = f"{addressList[i]:08X}"
# Write the formatted address followed by the line content and length
f.write(f"@{i+1}\n")
f.write(f";{addressStr}{{{line}}}#{len(line)}#{linesLenght[i]}\n")
f.write(f"{line}\n")
f.write("|\n")
i += 1