-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlzstring.py
170 lines (154 loc) · 4.94 KB
/
lzstring.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
from collections import namedtuple
import math
_keyStrUriSafe = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789~-$"
_baseReverseDic = {}
def _getBaseValue(alphabet, character):
try:
_baseReverseDic[alphabet]
except KeyError:
_baseReverseDic[alphabet] = {}
for i in xrange(len(alphabet)):
_baseReverseDic[alphabet][alphabet[i]] = i
return _baseReverseDic[alphabet][character]
def decompressFromEncodedURIComponent(input):
if input is None:
return ""
if input == "":
return ""
return _decompress(len(input), 32, lambda index: _getBaseValue(_keyStrUriSafe, input[index]))
def decompress(compressed):
if compressed is None:
return ""
if compressed == "":
return ""
return _decompress(compressed.length, 32768, lambda index: compressed[index])
def _decompress(length, resetValue, getNextValue):
dictionary = [False]*256*256
enlargeIn = 4
dictSize = 4
numBits = 3
entry = ""
result = ""
class Data:
pass
data = Data()
data.val = getNextValue(0)
data.position = resetValue
data.index = 1
for i in range(3):
dictionary[i] = i
bits = 0
maxpower = math.pow(2, 2)
power = 1
while power != maxpower:
resb = data.val & data.position
data.position >>= 1
if data.position == 0:
data.position = resetValue
data.val = getNextValue(data.index)
data.index += 1
bits |= (1 if resb > 0 else 0) * power
power <<= 1
next = bits
if next == 0:
bits = 0
maxpower = math.pow(2, 8)
power = 1
while power != maxpower:
resb = data.val & data.position
data.position >>= 1
if data.position == 0:
data.position = resetValue
data.val = getNextValue(data.index)
data.index += 1
bits |= (1 if resb > 0 else 0) * power
power <<= 1
c = unichr(bits)
elif next == 1:
bits = 0
maxpower = math.pow(2, 16)
power = 1
while power != maxpower:
resb = data.val & data.position
data.position >>= 1
if data.position == 0:
data.position = resetValue
data.val = getNextValue(data.index)
data.index += 1
bits |= (1 if resb > 0 else 0) * power
power <<= 1
c = unichr(bits)
elif next == 2:
return ""
dictionary[3] = c
w = result = c
while True:
if data.index > length:
return ""
bits = 0
maxpower = math.pow(2, numBits)
power = 1
while power != maxpower:
resb = data.val & data.position
data.position >>= 1
if data.position == 0:
data.position = resetValue
data.val = getNextValue(data.index)
data.index += 1
bits |= (1 if resb > 0 else 0) * power
power <<= 1
c = bits
if c == 0:
bits = 0
maxpower = math.pow(2, 8)
power = 1
while power != maxpower:
resb = data.val & data.position
data.position >>= 1
if data.position == 0:
data.position = resetValue
data.val = getNextValue(data.index)
data.index += 1
bits |= (1 if resb > 0 else 0) * power
power <<= 1
dictionary[dictSize] = unichr(bits)
dictSize += 1
c = dictSize-1
enlargeIn -= 1
elif c == 1:
bits = 0
maxpower = math.pow(2, 16)
power = 1
while power != maxpower:
resb = data.val & data.position
data.position >>= 1
if data.position == 0:
data.position = resetValue
data.val = getNextValue(data.index)
data.index += 1
bits |= (1 if resb > 0 else 0) * power
power <<= 1
dictionary[dictSize] = unichr(bits)
dictSize += 1
c = dictSize-1
enlargeIn -= 1
elif c == 2:
return result
if enlargeIn == 0:
enlargeIn = math.pow(2, numBits)
numBits += 1
if dictionary[c]:
entry = dictionary[c]
elif c == dictSize:
entry = w + w[0]
else:
return None
result += entry
# Add w+entry[0] to the dictionary.
dictionary[dictSize] = w + entry[0]
dictSize += 1
enlargeIn -= 1
w = entry
if enlargeIn == 0:
enlargeIn = math.pow(2, numBits)
numBits += 1