-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathlzjb.py
More file actions
executable file
·233 lines (207 loc) · 6.4 KB
/
lzjb.py
File metadata and controls
executable file
·233 lines (207 loc) · 6.4 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
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
#!/usr/bin/env python
#
# An attempt at re-implementing LZJB compression in native Python.
#
# Created in May 2014 by Emil Brink <emil@obsession.se>. See LICENSE.
#
# ---------------------------------------------------------------------
#
# Copyright (c) 2014-2020, Emil Brink
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification, are permitted provided
# that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this list of conditions and
# the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions
# and the following disclaimer in the documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
# PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
# TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
BYTE_BITS = 8
MATCH_BITS = 6
MATCH_MIN = 3
MATCH_MAX = (1 << MATCH_BITS) + (MATCH_MIN - 1)
MATCH_RANGE = range(MATCH_MIN, MATCH_MAX + 1) # Length 64, fine on 2.x.
OFFSET_MASK = (1 << (16 - MATCH_BITS)) - 1
LEMPEL_SIZE = 1024
def size_encode(size, dst = None):
"""
Encodes the given size in little-endian variable-length encoding.
The dst argument can be an existing bytearray to append the size. If it's
omitted (or None), a new bytearray is created and used.
Returns the destination bytearray.
"""
if dst is None: dst = bytearray()
done = False
while not done:
dst.append(size & 0x7f)
size >>= 7
done = size == 0
dst[-1] |= 0x80
return dst
def size_decode(src):
"""
Decodes a size (encoded with size_encode()) from the start of src.
Returns a tuple (size, len) where size is the size that was decoded,
and len is the number of bytes from src that were consumed.
"""
dstSize = 0
pos = 0
# Extract prefixed encoded size, if present.
val = 1
while True:
c = src[pos]
pos += 1
if c & 0x80:
dstSize += val * (c & 0x7f)
break
dstSize += val * c
val <<= 7
return (dstSize, pos)
def compress(src, dst = None):
"""
Compresses src, the source bytearray.
If dst is not None, it's assumed to be the output bytearray and bytes are appended to it using dst.append().
If it is None, a new bytearray is created.
The destination bytearray is returned.
"""
if dst is None: dst = bytearray()
lempel = [0] * LEMPEL_SIZE
copymask = 1 << (BYTE_BITS - 1)
pos = 0 # Current input offset.
while pos < len(src):
copymask <<= 1
if copymask == (1 << BYTE_BITS):
copymask = 1
copymap = len(dst)
dst.append(0)
if pos > len(src) - MATCH_MAX:
dst.append(src[pos])
pos += 1
continue
hsh = (src[pos] << 16) + (src[pos + 1] << 8) + src[pos + 2]
hsh += hsh >> 9
hsh += hsh >> 5
hsh &= LEMPEL_SIZE - 1
offset = (pos - lempel[hsh]) & OFFSET_MASK
lempel[hsh] = pos
cpy = pos - offset
if cpy >= 0 and cpy != pos and src[pos:pos + 3] == src[cpy:cpy + 3]:
dst[copymap] |= copymask
# Process the usual range if far from end of buffer, else a shorter one to avoid stepping out of bounds.
for mlen in MATCH_RANGE if pos + MATCH_RANGE[-1] < len(src) else range(MATCH_MIN, len(src) - pos):
if src[pos + mlen] != src[cpy + mlen]:
break
dst.append(((mlen - MATCH_MIN) << (BYTE_BITS - MATCH_BITS)) | (offset >> BYTE_BITS))
dst.append(offset & 255)
pos += mlen
else:
dst.append(src[pos])
pos += 1
return dst
def decompress(src, dst = None):
"""
Decompresses src, a bytearray of compressed data.
The dst argument can be an optional bytearray which will have the output appended.
If it's None, a new bytearray is created.
The output bytearray is returned.
"""
if dst is None: dst = bytearray()
pos = 0
copymask = 1 << (BYTE_BITS - 1)
while pos < len(src):
copymask <<= 1
if copymask == (1 << BYTE_BITS):
copymask = 1
copymap = src[pos]
pos += 1
if copymap & copymask:
mlen = (src[pos] >> (BYTE_BITS - MATCH_BITS)) + MATCH_MIN
offset = ((src[pos] << BYTE_BITS) | src[pos + 1]) & OFFSET_MASK
pos += 2
cpy = len(dst) - offset
if cpy < 0:
return None
while mlen > 0:
dst.append(dst[cpy])
cpy += 1
mlen -= 1
else:
dst.append(src[pos])
pos += 1
return dst
if __name__ == "__main__":
import cProfile
import os
import sys
import time
def loop(data):
t0 = time.clock()
if profile:
pr = cProfile.Profile()
pr.enable()
compr = compress(data)
if profile:
pr.disable()
pr.print_stats()
elapsed = time.clock() - t0
rate = len(data) / (1024 * 1024 * elapsed)
print(" Compressed to %u bytes, %.2f%% in %s s [%.2f MB/s]" % (len(compr), 100.0 * len(compr) / len(data), elapsed, rate))
decompr = decompress(compr)
if decompr != data:
print("**Decompression failed!")
def save(filename, data):
out = open(outname, "wb")
out.write(data)
out.close()
DECOMPRESS = 0
COMPRESS = 1
mode = None
outname = None
quiet = False
profile = False
for a in sys.argv[1:]:
if a.startswith("-"):
if a[1:] == "profile":
profile = True
elif a[1:2] == "o":
outname = a[2:]
elif a[1:] == "c":
mode = COMPRESS
if outname is None:
print("**Use -oNAME to set output name, before -c")
elif a[1:] == "x":
mode = DECOMPRESS
if outname is None:
print("**Use -oNAME to set output name, before -x")
elif a[1:] == "q":
quiet = True
else:
print("**Ignoring unknown option '%s'" % a)
else:
try:
inf = open(a, "rb")
data = inf.read()
inf.close()
data = bytearray(data)
except:
print("**Failed to open '%s'" % a)
continue
if not quiet: print("Loaded %u bytes from '%s'" % (len(data), a))
if mode == COMPRESS:
dst = size_encode(len(data))
save(outname, compress(data, dst))
elif mode == DECOMPRESS:
size, slen = size_decode(data)
save(outname, decompress(data[slen:]))
else:
loop(data)