Skip to content

Instantly share code, notes, and snippets.

@anezih
Last active April 24, 2023 15:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anezih/b20e2fb4de33bcb89f861efeff3f6072 to your computer and use it in GitHub Desktop.
Save anezih/b20e2fb4de33bcb89f861efeff3f6072 to your computer and use it in GitHub Desktop.
import os
import re
import sys
import zlib
from struct import unpack
OFFSET = 0x6B4
"""
Zlib streams:
0: Alphabet?
1: Language, Keyboard
2: Wordlist
3+: Definitions
"""
def convert(fname: str) -> None:
defilist: list[tuple[str, str]] = []
info = ""
j = 0
i = 0
with open(fname, "rb") as f:
dic = f.read() # [OFFSET:]
while j < len(dic):
try:
decomp_obj = zlib.decompressobj()
decomp = decomp_obj.decompress(dic[j:])
try:
if i == 0:
decoded = decomp.decode("utf-16")
decoded = "\n".join(decoded.split("\x00"))
info += f"{decoded}\n\n"
elif i == 1:
decoded = decomp.decode()
info += f"{decoded}\n\n"
elif i == 2:
lst = decomp.split(b"\x00")
decoded = []
for word in lst:
try:
word_decoded = word[2:].decode()
if word_decoded:
decoded.append(word_decoded)
except:
pass
with_endline = '\n'.join(decoded)
info += f"Length of Wordlist: {len(decoded)}\n{with_endline}"
else:
pos = 0
while True:
_len = unpack("<H", decomp[pos:pos+2])[0]
if _len == 0:
break
idx = (pair := decomp[pos+2:pos+_len]).index(b"\x00")
hw = pair[0:idx].decode()
defi = pair[idx+1:].decode().replace("\n","\\n").removesuffix("\\n")
defi = re.sub("\s{2,}", " ", defi)
defi = re.sub("[\x00-\x1F]+", "", defi)
defilist.append((hw, defi))
pos += _len
i += 1
except Exception as e:
print(e)
j += len(dic[j:]) - len(decomp_obj.unused_data)
except zlib.error:
j += 1
outname = os.path.splitext(os.path.basename(fname))[0]
with open(f"{outname}.tsv", "w", encoding="utf-8") as tsv:
for i in defilist:
tsv.write(f"{i[0]}\t{i[1]}\n")
with open(f"{outname}_info.txt", "w", encoding="utf-8") as txt:
txt.write(info)
if __name__ == '__main__':
if len(sys.argv) < 2:
sys.exit("Missing filename.")
convert(sys.argv[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment