MonkeyBusiness/utils/lz77.py

34 lines
1.2 KiB
Python
Raw Normal View History

2022-08-26 12:39:11 +02:00
class EamuseLZ77:
@staticmethod
def decode(data):
data_length = len(data)
offset = 0
output = []
while offset < data_length:
flag = data[offset]
offset += 1
for bit in range(8):
if flag & (1 << bit):
output.append(data[offset])
offset += 1
else:
if offset >= data_length:
break
2022-11-15 15:03:37 +01:00
lookback_flag = int.from_bytes(data[offset : offset + 2], "big")
lookback_length = (lookback_flag & 0x000F) + 3
2022-08-26 12:39:11 +02:00
lookback_offset = lookback_flag >> 4
offset += 2
if lookback_flag == 0:
break
for _ in range(lookback_length):
loffset = len(output) - lookback_offset
if loffset <= 0 or loffset >= len(output):
output.append(0)
else:
output.append(output[loffset])
return bytes(output)
# @staticmethod
# def encode(data):
# return bytes(output)