forked from dhondta/python-codext
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlz77.py
More file actions
74 lines (58 loc) · 2.24 KB
/
Copy pathlz77.py
File metadata and controls
74 lines (58 loc) · 2.24 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# -*- coding: UTF-8 -*-
"""LZ77 Codec - Lempel-Ziv 1977 compression algorithm.
NB: Not an encoding properly speaking.
This codec:
- en/decodes strings from str to str
- en/decodes strings from bytes to bytes
- decodes file content to str (read)
- encodes file content from str to bytes (write)
Inspired from: https://github.com/manassra/LZ77-Compressor
"""
from ..__common__ import *
__examples__ = {'enc-dec(lz77)': ["test", "This is a test", "@random{512,1024,2048}"]}
_B2b = lambda B: bin(B if isinstance(B, int) else ord(B))[2:].zfill(8)
_b2B = lambda bt: "".join(chr(int(bt[i:i+8], 2)) for i in range(0, len(bt), 8))
WINDOW_SIZE = 20
def _find_longest_match(data, pos):
""" Finds the longest match to a substring starting at the current position (pos) in the lookahead buffer from
the history window. """
eob, bmd, bml = min(pos + 15, len(data) + 1), -1, -1
for j in range(pos + 2, eob):
start = max(0, pos - WINDOW_SIZE)
substr = data[pos:j]
l = len(substr)
for i in range(start, pos):
n, r = l // (pos - i), l % (pos - i)
if data[i:pos] * n + data[i:i+r] == substr and l > bml:
bmd, bml = pos - i, l
if bmd > 0 and bml > 0:
return bmd, bml
def lz77_compress(input, errors="strict"):
""" Compresses the given data by applying LZ77 compression algorithm. """
i, l, bits = 0, len(input), ""
while i < l:
try:
bmd, bml = _find_longest_match(input, i)
bits += "1" + _B2b(bmd >> 4) + _B2b(((bmd & 0xf) << 4) | bml)
i += bml
except TypeError:
bits += "0" + _B2b(input[i])
i += 1
bits += "0" * ((8 - (len(bits) % 8)) % 8)
return _b2B(bits), l
def lz77_decompress(input, errors="strict"):
""" Decompresses the given data. """
out, d = "", "".join(_B2b(c) for c in input)
while len(d) >= 9:
flag, d = d[0], d[1:]
if flag == "0":
out += _b2B(d[:8])
d = d[8:]
else:
B1, B2 = int(d[:8], 2), int(d[8:16], 2)
d = d[16:]
dist = (B1 << 4) | (B2 >> 4)
for i in range(B2 & 0xf):
out += out[-dist]
return out, len(out)
add("lz77", lz77_compress, lz77_decompress)