-
Notifications
You must be signed in to change notification settings - Fork 1
/
mklzw.py
81 lines (71 loc) · 2.14 KB
/
mklzw.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
'''
Mortal Kombat GRA files parser
Copyright (c) 2018 ReWolf
http://blog.rewolf.pl/
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
'''
from bitreader import BitReader
class LzwState:
def __init__(self, c):
self.c = c
self.M_CLR = 1 << c
self.M_NEW = self.M_CLR + 2
self.M_EOD = self.M_CLR + 1
self.default_req_bits = c + 1
def clear(self):
self.next_code = self.M_NEW
self.req_bits = self.default_req_bits
self.next_shift = 1 << self.default_req_bits
self.finchar = -1
self.oldcode = -1
def decompress(compressed_data, c):
br = BitReader(compressed_data)
state = LzwState(c)
state.clear()
d = {}
htab = {}
output = []
output2 = []
while True:
if br.isEnd():
return []
cur_code = br.getBits(state.req_bits)
incode = cur_code
if cur_code == state.M_EOD:
break
if cur_code == state.M_CLR:
state.clear()
else:
if cur_code == state.next_code:
cur_code = state.oldcode
output.append(state.finchar)
while cur_code >= state.M_CLR:
if cur_code not in htab:
return []
output.append(htab[cur_code])
cur_code = d[cur_code + 1]
state.finchar = cur_code
while True:
output2.append(cur_code)
if not output:
break
cur_code = output.pop()
if state.next_code < 4096 and state.oldcode != -1:
d[state.next_code + 1] = state.oldcode
htab[state.next_code] = state.finchar
state.next_code += 1
if state.next_code >= state.next_shift:
if state.req_bits < 12:
state.req_bits += 1
state.next_shift = 1 << state.req_bits
state.oldcode = incode
return bytes(output2)