декомпрессор для cpr проде работает, правда как упаковать обратно не ясно, ну не селен я в програмировании
Код:
import struct
import sys
def lzss_decompress(data: bytes) -> bytes:
print(" [LZSS] Checking input length...")
if len(data) < 5:
raise ValueError("LZSS input is too short")
target_size = struct.unpack_from("<I", data, 0)[0]
print(f" [LZSS] Target size from header: {target_size} bytes")
if target_size <= 0:
raise ValueError(f"Invalid LZSS decompressed size: {target_size}")
ring = bytearray(4096)
ring_pos = 0xFEE
src_pos = 4
out = bytearray()
print(" [LZSS] Starting decompression loop...")
while src_pos < len(data) and len(out) < target_size:
flags = data[src_pos]
src_pos += 1
for bit in range(8):
if flags & (1 << bit):
# Literal byte
if src_pos >= len(data):
raise ValueError("Truncated LZSS literal")
value = data[src_pos]
src_pos += 1
out.append(value)
ring[ring_pos] = value
ring_pos = (ring_pos + 1) & 0xFFF
else:
# Two-byte back-reference
if src_pos + 1 >= len(data):
raise ValueError("Truncated LZSS back-reference")
b0 = data[src_pos]
b1 = data[src_pos + 1]
src_pos += 2
offset = b0 | ((b1 & 0xF0) << 4)
length = (b1 & 0x0F) + 3
for i in range(length):
value = ring[(offset + i) & 0xFFF]
out.append(value)
ring[ring_pos] = value
ring_pos = (ring_pos + 1) & 0xFFF
if len(out) >= target_size:
break
if len(out) >= target_size:
break
if len(out) != target_size:
raise ValueError(
f"LZSS stream ended at {len(out):,} bytes; expected {target_size:,}"
)
return bytes(out)
def main():
print("=== LZSS Decompressor started ===")
if len(sys.argv) < 3:
print("Usage: python lzss_decompress.py <input.cpr> <output.bin>")
sys.exit(1)
input_path = sys.argv[1]
output_path = sys.argv[2]
print(f"[MAIN] Input: {input_path}")
print(f"[MAIN] Output: {output_path}")
try:
with open(input_path, "rb") as f:
data = f.read()
print(f"[MAIN] Read {len(data)} bytes from input file")
except FileNotFoundError:
print(f"ERROR: Input file not found: {input_path}", file=sys.stderr)
sys.exit(1)
except Exception as e:
print(f"ERROR reading file: {e}", file=sys.stderr)
sys.exit(1)
try:
decompressed = lzss_decompress(data)
print(f"[MAIN] Decompressed to {len(decompressed)} bytes")
except ValueError as e:
print(f"LZSS decompression error: {e}", file=sys.stderr)
sys.exit(1)
except Exception as e:
print(f"Unexpected error during decompression: {e}", file=sys.stderr)
sys.exit(1)
try:
with open(output_path, "wb") as f:
f.write(decompressed)
print(f"[MAIN] Wrote {len(decompressed)} bytes to output file")
print("=== Done ===")
except Exception as e:
print(f"Error writing output file: {e}", file=sys.stderr)
sys.exit(1)
if __name__ == "__main__":
main()