-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdecode2.py
executable file
·447 lines (371 loc) · 13.8 KB
/
decode2.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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
import sys
import uuid
import struct
import pyjson5
import re
import string
def decode_length(file):
"""
Decode the length encoded in two bytes according to the specified scheme.
Args:
- byte1: The first byte as an integer.
- byte2: The second byte as an integer.
Returns:
- The decoded length as an integer.
"""
byte1 = file.read(1)[0]
if byte1 & 0b10000000: # Check if the highest bit is set
# Combine the bits from byte1 (excluding the highest bit) and byte2
byte2 = file.read(1)[0]
actual_length = (byte1 & 0b01111111) | (byte2 << 7)
else:
actual_length = byte1 # Use byte1 directly if the highest bit isn't set
return actual_length
def decode_length_manual(byte1, byte2):
"""
Decode the length encoded in two bytes according to the specified scheme.
Args:
- byte1: The first byte as an integer.
- byte2: The second byte as an integer.
Returns:
- The decoded length as an integer.
"""
if byte1 & 0b10000000: # Check if the highest bit is set
# Combine the bits from byte1 (excluding the highest bit) and byte2
actual_length = (byte1 & 0b01111111) | (byte2 << 7)
else:
actual_length = byte1 # Use byte1 directly if the highest bit isn't set
return actual_length
def read_until_marker(file, marker):
"""Reads file content until a specific marker is found. Returns the content up to (but not including) the marker."""
content = b''
marker_length = len(marker)
read_bytes = 0
while True:
byte = file.read(1)
read_bytes = read_bytes + 1
if not byte:
# End of file reached without finding the marker
break
content += byte
# Check if the last part of content matches the marker
if content[-marker_length:] == marker:
# Remove the marker from the content before returning
content = content[:-marker_length]
break
return read_bytes, content
def debug_bytes_to_string(byte_data):
"""Convert bytes to two debug strings: colored hex representation and printable characters.
Non-printables and whitespace (except spaces) are replaced with ':', spaces remain unchanged."""
hex_output = [] # For hex representation
char_output = [] # For printable characters, with specific replacements for non-printables
# ANSI color codes
green = '\033[92m' # Printable
blue = '\033[94m' # Whitespace
red = '\033[91m' # Non-printable
reset = '\033[0m' # Reset color
for byte in byte_data:
color = red # Assume non-printable by default
if chr(byte) in string.printable:
if chr(byte) == ' ':
color = green # Keep spaces green (printable)
elif chr(byte) in string.whitespace:
color = blue # Color other whitespace blue
char_output.append(':')
hex_output.append(f"{color}{byte:02x}{reset}")
continue
else:
color = green # Other printable characters
char_output.append(chr(byte))
else:
char_output.append(':')
hex_output.append(f"{color}{byte:02x}{reset}")
# Join the parts into a single string for each representation
hex_string = " ".join(hex_output) + f" {reset}({len(byte_data)} bytes)"
char_string = "".join(char_output)
return hex_string, char_string
def parse_json_from_file(file):
prelude_bytes = b'' # To accumulate prelude data
json_str = '' # Initialize an empty string to accumulate JSON data
depth = 0 # Keep track of the depth of nested objects
in_json = False # Flag to indicate whether we've encountered the opening brace
prelude = '' # To store prelude as string or hex string
while True:
byte = file.read(1) # Read one byte at a time
if not byte:
# End of file reached
break
if not in_json and byte != b'{':
prelude_bytes += byte
continue
if byte == b'{':
if not in_json: # Capture the prelude data
try:
prelude = prelude_bytes.decode('utf-8')
except UnicodeDecodeError:
prelude = prelude_bytes.hex() # Use hex if not UTF-8 decodable
in_json = True
depth += 1
elif byte == b'}':
depth -= 1
if in_json:
# Accumulate the JSON string
json_str += byte.decode('utf-8') # Safely decode
#json_str += byte.decode('utf-8', errors='replace') # Safely decode
if in_json and depth == 0:
# All braces are matched; the JSON object is complete
break
prelude_hex, prelude_chars = debug_bytes_to_string(prelude_bytes)
return prelude_bytes, prelude_hex, prelude_chars, json_str
def read_json_blob(file):
json_length = decode_length(file)
json = file.read(json_length).decode('utf-8')
return json
def read_text(file):
length = decode_length(file)
return file.read(length).decode('utf-8')
def read_uuid(file):
# Read the next 32 ASCII characters for the UUID-like hexadecimal value
hex_uuid = file.read(32)
if len(hex_uuid) < 32:
print("File does not contain enough data for a hex UUID.")
return
# Convert ASCII bytes to string
hex_str = hex_uuid.decode('ascii')
# Inserting hyphens to match the UUID format
formatted_uuid_str = f"{hex_str[:8]}-{hex_str[8:12]}-{hex_str[12:16]}-{hex_str[16:20]}-{hex_str[20:]}"
try:
# Attempt to create a UUID object from the formatted string
extracted_uuid = uuid.UUID(formatted_uuid_str)
return extracted_uuid
except ValueError as e:
print(f"Error interpreting extracted data as UUID: {e}")
return ""
def peek(f, length=1):
pos = f.tell()
data = f.read(length) # Might try/except this line, and finally: f.seek(pos)
f.seek(pos)
return data
def parse_header(file):
# Read the first 4 bytes as an offset
magic_number_bytes = file.read(4)
if len(magic_number_bytes) == 4:
magic_number = struct.unpack('<I', magic_number_bytes)[0] # '<I' specifies little-endian unsigned int
if magic_number != 0xa18f40a:
print("Magic Number wrong, got ", hex(magic_number))
return
else:
print("file to short")
return
# Expecting a separator byte (0x20)
separator = file.read(1)
if separator != b'\x20':
print("Expected separator not found.")
return
print("Separator found.")
# Read the next 32 ASCII characters for the UUID-like hexadecimal value
hex_uuid = file.read(32)
if len(hex_uuid) < 32:
print("File does not contain enough data for a hex UUID.")
return
# Convert ASCII bytes to string
hex_str = hex_uuid.decode('ascii')
# Inserting hyphens to match the UUID format
formatted_uuid_str = f"{hex_str[:8]}-{hex_str[8:12]}-{hex_str[12:16]}-{hex_str[16:20]}-{hex_str[20:]}"
try:
# Attempt to create a UUID object from the formatted string
extracted_uuid = uuid.UUID(formatted_uuid_str)
print(f"Extracted UUID: {extracted_uuid}")
except ValueError as e:
print(f"Error interpreting extracted data as UUID: {e}")
# lets continue decoding the rest of the file
unknown_data = file.read(15)
#marker = b'\x31\x32' # Marker to look for after "Pilot"
#read_bytes, content = read_until_marker(file, marker)
#print(f"Read {read_bytes} bytes until marker.")
length = decode_length(file)
print("name length: ", length)
name = file.read(length).decode('utf-8')
print("name: " + name)
for i in range(0, 12):
marker = file.read(1)[0]
print("marker: ", hex(marker))
match marker:
case 0x0a:
subcmd = file.read(1)[0]
match subcmd:
case 0x20: # uuid
uuid = read_uuid(file)
print("uuid: ", uuid)
case 0x02: # unknown
file.read(6) #03 a8 02 01 ba 02
case 0x3a:
json = read_json_blob(file)
print("json: ", json)
case 0x40:
file.read(1)
file.read(4)
file.read(4)
file.read(1)
file.read(1)
case 0x5a:
json = read_json_blob(file)
print("json: ", json)
case 0x62:
json = read_json_blob(file)
print("json: ", json)
case 0x6a:
json = read_json_blob(file)
print("json: ", json)
case 0x72:
json = read_json_blob(file)
print("json: ", json)
case 0x78:
file.read(18)
json = read_json_blob(file)
print("json: ", json)
case 0xb5:
file.read(4)
file.read(1)
case 0xbd:
file.read(4)
file.read(1)
case 0xc2:
file.read(1)
length = decode_length(file)
unknown = file.read(length).decode('utf-8')
print("unknown: ", unknown)
case 0xd0:
unknown = file.read(5)
if peek(file, 1)[0] == 0x80:
unknown = file.read(5)
if peek(file, 1)[0] == 0x01:
file.read(1)
case 0xaa:
file.read(1)
length = decode_length(file)
unknown = file.read(length).decode('utf-8')
case 0x10:
file.read(6) # unknown
case 0x18:
file.read(4) # unknown
case 0xb7:
file.read(2) # 0x31 0x32
length = decode_length(file)
case _:
print("unknown marker: ", hex(marker))
print("")
def debug_content(file):
for i in range(0, 100):
# Parse the first JSON object
prelude_bytes, prelude_hex, prelude_chars, first_json = parse_json_from_file(file)
if first_json is not None:
print("prelude:", prelude_hex)
print("", prelude_chars)
print("JSON object length:", len(first_json))
if len(prelude_bytes) > 2:
print("decoded: ", decode_length_manual(prelude_bytes[1], prelude_bytes[2]))
print("")
def hex_format(data):
return " ".join([f"{byte:02x}" for byte in data])
def parse_note_tree(f):
start = f.read(5)
assert(start[0] == 0x0a)
assert(start[3] == 0x0a)
assert(start[4] == 0x20)
uuid = read_uuid(f)
block = f.read(15)
print("block: ", hex_format(block))
assert(block[13] == 0x31, hex_format(block))
if block[14] == 0x22:
assert(f.read(1)[0] == 0x20)
uuid = read_uuid(f)
print("unknown uuid: ", uuid)
assert(f.read(1)[0] == 0x32)
else:
assert(block[14] == 0x32, hex_format(block))
print("peek: ", hex_format(peek(f, 10)))
name = read_text(f)
print("uuid: ", uuid)
print("name: ", name)
assert(f.read(1)[0] == 0x3a)
active_scene = read_json_blob(f)
print("json: ", active_scene)
# 0x40
assert(f.read(1)[0] == 0x40)
f.read(1)
f.read(4)
f.read(4)
f.read(1)
f.read(1)
# 0x5a
assert(f.read(1)[0] == 0x5a)
json = read_json_blob(f)
print("json: ", json)
assert(f.read(1)[0] == 0x62)
json = read_json_blob(f)
print("json: ", json)
assert(f.read(1)[0] == 0x6a)
json = read_json_blob(f)
print("json: ", json)
assert(f.read(1)[0] == 0x72)
json = read_json_blob(f)
print("json: ", json)
assert(f.read(1)[0] == 0x78)
f.read(18)
json = read_json_blob(f)
print("json: ", json)
# differs
assert(f.read(1)[0] == 0xaa)
f.read(1)
json = read_json_blob(f)
print("json: ", json)
# 0xb5
assert(f.read(1)[0] == 0xb5)
f.read(4)
f.read(1)
# 0xbd
assert(f.read(1)[0] == 0xbd)
f.read(4)
f.read(1)
# 0xc2
assert(f.read(1)[0] == 0xc2)
f.read(1)
length = decode_length(f)
unknown = f.read(length).decode('utf-8')
print("unknown: ", unknown)
# 0xd0
assert(f.read(1)[0] == 0xd0)
unknown = f.read(5)
if peek(f, 1)[0] == 0x80:
unknown = unknown + f.read(5)
if peek(f, 1)[0] == 0x01:
unknown = unknown + f.read(1)
unknown = unknown + f.read(8)
expected_end = bytes([0xa0, 0x02, 0x03, 0xa8, 0x02, 0x01, 0xba, 0x02])
assert unknown[-len(expected_end):] == expected_end, f"unknown does not end with the expected byte sequence {unknown.hex()}"
#a0 73 68 61 72 65 5f 75 73 65 72
expected = bytes([0x0a, 0x73, 0x68, 0x61, 0x72, 0x65, 0x5f, 0x75, 0x73, 0x65, 0x72])
unknown = f.read(11)
if peek(f, 1)[0] != 0x0a:
f.read(3) # this is optional, could differ in size, we don't know
assert unknown == expected, f"unknown does not end with the expected byte sequence. \nExpected: {expected.hex()}\nActual: {unknown.hex()}"
next_start = peek(f, 5)
print("next start: ", next_start.hex())
print("")
def parse_file_for_offset_and_hex_uuid(file_path, debug):
try:
with open(file_path, 'rb') as file:
if debug:
debug_content(file)
else:
#parse_header(file)
while True:
parse_note_tree(file)
except IOError as e:
print(f"Error reading file {file_path}: {e}")
if __name__ == "__main__":
if len(sys.argv) < 2:
print("Usage: python script.py <path_to_binary_file>")
else:
parse_file_for_offset_and_hex_uuid(sys.argv[1], len(sys.argv) > 2 and sys.argv[2] == "-d")