-
Notifications
You must be signed in to change notification settings - Fork 0
/
silky_lzss.py
276 lines (243 loc) · 9.85 KB
/
silky_lzss.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
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# Class for working with Silky LZSS compression.
# So tiresome...
# Made by Tester Testerov.
# Partially based on original 4/6/1989 Haruhiko Okumura implementation.
# Has many differences, through, and not just cosmetic.
# Because Silky Engine's implementation is different itself. It is more closer to...
# Oh, probably, Saxman compression?
class SilkyLZSS:
def __init__(self, buffer: bytes, N: int = 4096, F: int = 18, threshold: int = 2, null: int = None,
debug: bool = False, progress_print: int = 2**15, padding_byte=b'\x00'):
self.debug = debug
self.input_buffer = buffer
if null is None: # No, thou cannot just take None there. Or program will [REDACTED]!!
self.null = N
else:
self.null = null
if isinstance(padding_byte, int):
self.padding_byte = self.unsigned_char(padding_byte)
else:
self.padding_byte = padding_byte[0]
self.progress_print = progress_print # Duration of progress print.
self.N = N # Buffer size. In classical implementation it is 2^14. More buffer size is, the lesser file becomes.
self.F = F # Match length limit. In original implementation it is 18. More limit is, the lesser file becomes.
self.threshold = threshold # Minimum limit of match length to encode as position and length.
# I strongly recommend thou use default threshold value.
self.text_buffer = [0] * (self.N + self.F - 1)
self.match_position = 0
self.match_length = 0
self.lson = [0] * (self.N + 1)
self.rson = [0] * (self.N + 257)
self.dad = [0] * (self.N + 1)
self.length_crutch = 0
def init_tree(self) -> None:
for i in range(self.N + 1, self.N + 257):
self.rson[i] = self.null
for i in range(self.N):
self.dad[i] = self.null
def insert_node(self, r: int) -> None:
"""Insert string (len(s) = F) from buffer into one of the trees and returns the longest match."""
i = 0
cmp = 1
p = self.N + 1 + self.text_buffer[r]
self.rson[r] = self.null
self.lson[r] = self.null
self.match_length = 0
while True:
if cmp >= 0:
if self.rson[p] != self.null:
p = self.rson[p]
else:
self.rson[p] = r
self.dad[r] = p
return
else:
if self.lson[p] != self.null:
p = self.lson[p]
else:
self.lson[p] = r
self.dad[r] = p
return
for i in range(1, self.F):
cmp = self.text_buffer[r + i] - self.text_buffer[p + i]
if cmp != 0:
i -= 1
break
i += 1
if i > self.match_length:
self.match_position = p
self.match_length = i
if self.match_length >= self.F:
break
self.dad[r] = self.dad[p]
self.lson[r] = self.lson[p]
self.rson[r] = self.rson[p]
self.dad[self.lson[p]] = r
self.dad[self.rson[p]] = r
if self.rson[self.dad[p]] == p:
self.rson[self.dad[p]] = r
else:
self.lson[self.dad[p]] = r
self.dad[p] = self.null
def delete_node(self, p: int) -> None:
"""Delete node p."""
if self.dad[p] == self.null:
return
if self.rson[p] == self.null:
q = self.lson[p]
elif self.lson[p] == self.null:
q = self.rson[p]
else:
q = self.lson[p]
if self.rson[q] != self.null:
q = self.rson[q]
while self.rson[q] != self.null:
q = self.rson[q]
self.rson[self.dad[q]] = self.lson[q]
self.dad[self.lson[q]] = self.dad[q]
self.lson[q] = self.lson[p]
self.dad[self.lson[p]] = q
self.rson[q] = self.rson[p]
self.dad[self.rson[p]] = q
self.dad[q] = self.dad[p]
if self.rson[self.dad[p]] == p:
self.rson[self.dad[p]] = q
else:
self.lson[self.dad[p]] = q
self.dad[p] = self.null
def encode(self) -> bytes:
i = -1
length = -1
r = self.N - self.F
s = 0
code_buf = [0]*17
mask = 1
code_buf_ptr = mask
output_buffer = b''
self.init_tree()
code_buf[0] = 0 # Delete?
for i in range(s, r):
self.text_buffer[i] = self.padding_byte
for length in range(0, self.F):
if length >= len(self.input_buffer):
length -= 1
break
self.text_buffer[r + length] = self.input_buffer[length]
length += 1 # Necessary crutch.
if length == 0:
return b''
for i in range(1, self.F+1):
self.insert_node(r - i)
self.insert_node(r)
pos = i # Crutch variable to fetch correct entries from input_buffer.
print_count = 0
while True:
if self.match_length > length: # Probably correct.
self.match_length = length
if self.match_length <= self.threshold: # Correct.
self.match_length = 1
code_buf[0] |= mask
code_buf[code_buf_ptr] = self.text_buffer[r]
code_buf_ptr += 1
else: # Correct.
code_buf[code_buf_ptr] = self.unsigned_char(self.match_position)
code_buf_ptr += 1
code_buf[code_buf_ptr] = self.unsigned_char((((self.match_position >> 4) & 0xf0) |
(self.match_length - (self.threshold + 1))))
code_buf_ptr += 1
mask <<= 1
mask %= 256 # In that implementation was used just an unsigned char! #ERR!!!
if mask == 0: # Probably right.
for i in range(0, code_buf_ptr):
output_buffer += code_buf[i].to_bytes(1, byteorder="big") # Matches.
i += 1
code_buf[0] = 0
code_buf_ptr = 1
mask = 1
last_match_length = self.match_length # Matches.
for i in range(0, last_match_length):
if pos >= len(self.input_buffer):
i -= 1
break
self.delete_node(s)
c = self.input_buffer[pos]
pos += 1
self.text_buffer[s] = c
if s < (self.F - 1):
self.text_buffer[s + self.N] = c
s = (s + 1) & (self.N - 1)
r = (r + 1) & (self.N - 1)
self.insert_node(r)
i += 1 # Alas, this crutch is necessary.
if pos > print_count: # Frankly, not very important, but...
if self.debug:
print("{}\r".format(pos))
print_count += self.progress_print
while i < last_match_length:
i += 1
self.delete_node(s)
s = (s + 1) & (self.N - 1)
r = (r + 1) & (self.N - 1)
length -= 1
if length:
self.insert_node(r)
i += 1
if length <= 0:
break
if code_buf_ptr > 1:
for i in range(0, code_buf_ptr):
output_buffer += code_buf[i].to_bytes(1, byteorder="big")
if self.debug:
print("In: {} bytes.".format(len(self.input_buffer)))
print("Out: {} bytes.".format(len(output_buffer)))
print("Out/In: {}.".format(round(len(self.input_buffer)/len(output_buffer), 4)))
return output_buffer
def decode(self) -> bytes:
"""Decode bytes from lzss."""
output_buffer = b''
r = self.N - self.F
flags = 0
text_buffer = [0]*r
self.init_tree()
for i in range(0, r):
self.text_buffer[i] = self.padding_byte
current_pos = 0
while True:
flags >>= 1
if (flags & 256) == 0:
if current_pos >= len(self.input_buffer):
break
c = self.input_buffer[current_pos]
current_pos += 1
flags = c | 0xff00
if flags & 1:
if current_pos >= len(self.input_buffer):
break
c = self.input_buffer[current_pos]
current_pos += 1
output_buffer += c.to_bytes(1, byteorder="big")
self.text_buffer[r] = c
r += 1
r &= self.N - 1
else:
if current_pos >= len(self.input_buffer):
break
i = self.input_buffer[current_pos]
current_pos += 1
if current_pos >= len(self.input_buffer):
break
j = self.input_buffer[current_pos]
current_pos += 1
i |= (j & 0xf0) << 4
j = (j & 0x0f) + self.threshold
for k in range(0, j+1):
c = self.text_buffer[(i + k) & (self.N - 1)]
output_buffer += c.to_bytes(1, byteorder="big")
self.text_buffer[r] = c
r += 1
r &= self.N - 1
return output_buffer
@staticmethod
def unsigned_char(char: int):
"""Convert into unsigned char."""
return char % 256