-
Notifications
You must be signed in to change notification settings - Fork 85
/
legacysimos.py
38 lines (34 loc) · 1.11 KB
/
legacysimos.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
import struct
def fill_bits(count):
num = 0
for i in range(count):
num |= 1 << i
return num
def decompress(data: bytes):
signifier = data[0]
block_size = struct.unpack(">L", data[3:7])[0]
count = struct.unpack(">H", data[1:3])[0]
output_cursor = 0
offset_size = int(data[1])
dict_size = fill_bits(int(data[2]))
output = bytearray(block_size)
cursor = 11
while cursor < len(data):
if data[cursor] == signifier:
cursor += 1
offset_and_len = struct.unpack(">H", data[cursor : cursor + 2])[0]
cursor += 2
offset = offset_and_len >> (16 - offset_size)
length = offset_and_len & dict_size
block_offset = output_cursor
for i in range(0, length):
output[output_cursor] = output[block_offset - offset + i]
output_cursor += 1
output[output_cursor] = data[cursor]
cursor += 1
output_cursor += 1
else:
output[output_cursor] = data[cursor]
cursor += 1
output_cursor += 1
return output