-
-
Save the6p4c/ba52af73ece05829acf5335093b30609 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import sys | |
def decompress(f, offset, table_length): | |
decompressed = [] | |
f.seek(offset, os.SEEK_SET) | |
while True: | |
if f.tell() == offset + table_length: | |
return bytes(decompressed) | |
tag = f.read(1)[0] | |
tag_clen, tag_coffset, tag_len = tag >> 4, (tag >> 2) & 3, tag & 3 | |
print(f'{tag=:#04x}, {tag_clen=:#04x}, {tag_coffset=:#04x}, {tag_len=:#04x}') | |
length = tag_len | |
if tag_len == 0: | |
length = f.read(1)[0] + 3 | |
clen = tag_clen | |
if tag_clen == 0xf: | |
clen = f.read(1)[0] + 0xf | |
print(f' -> data {length=:#04x}') | |
data = f.read(length - 1) | |
decompressed += data | |
if clen != 0: | |
coffset_low = f.read(1)[0] | |
coffset_high = tag_coffset | |
if tag_coffset == 3: | |
coffset_high = f.read(1)[0] | |
coffset = (coffset_high << 8) | coffset_low | |
print(f' -> compressed {clen=:#04x} {coffset=:#06x}') | |
idx = len(decompressed) - coffset | |
for _ in range(clen + 2): | |
decompressed.append(decompressed[idx]) | |
idx += 1 | |
with open(sys.argv[1], 'rb') as f: | |
# offset into the file of the compressed data | |
TABLE = 0x14058b | |
# the length * 2 that comes from the first init table that told you where | |
# the compressed data was | |
LENGTH = 0x1cce >> 1 | |
data = decompress(f, TABLE, LENGTH) | |
with open(sys.argv[2], 'wb') as f: | |
f.write(data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment