-
Notifications
You must be signed in to change notification settings - Fork 11
/
lz-string.go
368 lines (350 loc) · 9.03 KB
/
lz-string.go
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
package lzstring
import (
"errors"
"math"
"strings"
"sync"
"unicode/utf8"
)
// Compress
//goland:noinspection SpellCheckingInspection
const _defaultKeyStrBase64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="
func Compress(uncompressed string, keyStrBase64 string) string {
if len(uncompressed) == 0 {
return ""
}
if keyStrBase64 == "" {
keyStrBase64 = _defaultKeyStrBase64
}
charArr := []rune(keyStrBase64)
res := _compress([]rune(uncompressed), 6, charArr)
switch len(res) % 4 {
case 3:
return res + "="
case 2:
return res + "=="
case 1:
return res + "==="
}
return res
}
func _compress(uncompressed []rune, bitsPerChar int, charArr []rune) string {
if len(uncompressed) == 0 {
return ""
}
var value int
contextDictionary := make(map[string]int)
contextDictionaryToCreate := make(map[string]bool)
var contextC string
var contextW string
var contextWc string
contextEnlargeIn := int64(2)
contextDictSize := 3
contextNumBits := 2
//var contextDataString string
var contextDataString = strings.Builder{}
contextDataVal := 0
contextDataPosition := 0
for ii := 0; ii < len(uncompressed); ii++ {
contextC = string(uncompressed[ii])
_, in := contextDictionary[contextC]
if !in {
contextDictionary[contextC] = contextDictSize
contextDictSize++
contextDictionaryToCreate[contextC] = true
}
contextWc = contextW + contextC
_, in = contextDictionary[contextWc]
if in {
contextW = contextWc
} else {
_, in = contextDictionaryToCreate[contextW]
if in {
contextWRune := int([]rune(contextW)[0])
if contextWRune < 256 {
for i := 0; i < contextNumBits; i++ {
contextDataVal <<= 1
if contextDataPosition == bitsPerChar-1 {
contextDataPosition = 0
contextDataString.WriteRune(charArr[contextDataVal])
contextDataVal = 0
} else {
contextDataPosition++
}
}
value = contextWRune
for i := 0; i < 8; i++ {
contextDataVal = (contextDataVal << 1) | (value & 1)
if contextDataPosition == bitsPerChar-1 {
contextDataPosition = 0
contextDataString.WriteRune(charArr[contextDataVal])
contextDataVal = 0
} else {
contextDataPosition++
}
value >>= 1
}
} else {
value = 1
for i := 0; i < contextNumBits; i++ {
contextDataVal = (contextDataVal << 1) | value
if contextDataPosition == bitsPerChar-1 {
contextDataPosition = 0
contextDataString.WriteRune(charArr[contextDataVal])
contextDataVal = 0
} else {
contextDataPosition++
}
value = 0
}
value = contextWRune
for i := 0; i < 16; i++ {
contextDataVal = (contextDataVal << 1) | (value & 1)
if contextDataPosition == bitsPerChar-1 {
contextDataPosition = 0
contextDataString.WriteRune(charArr[contextDataVal])
contextDataVal = 0
} else {
contextDataPosition++
}
value >>= 1
}
}
contextEnlargeIn--
if contextEnlargeIn == 0 {
contextEnlargeIn = int64(math.Pow(2, float64(contextNumBits)))
contextNumBits++
}
delete(contextDictionaryToCreate, contextW)
} else {
value = contextDictionary[contextW]
for i := 0; i < contextNumBits; i++ {
contextDataVal = (contextDataVal << 1) | (value & 1)
if contextDataPosition == bitsPerChar-1 {
contextDataPosition = 0
contextDataString.WriteRune(charArr[contextDataVal])
contextDataVal = 0
} else {
contextDataPosition++
}
value >>= 1
}
}
contextEnlargeIn--
if contextEnlargeIn == 0 {
contextEnlargeIn = int64(math.Pow(2, float64(contextNumBits)))
contextNumBits++
}
contextDictionary[contextWc] = contextDictSize
contextDictSize++
contextW = contextC
}
}
if contextW != "" {
_, in := contextDictionaryToCreate[contextW]
if in {
contextWRune := int([]rune(contextW)[0])
if contextWRune < 256 {
for i := 0; i < contextNumBits; i++ {
contextDataVal <<= 1
if contextDataPosition == bitsPerChar-1 {
contextDataPosition = 0
contextDataString.WriteRune(charArr[contextDataVal])
contextDataVal = 0
} else {
contextDataPosition++
}
}
value = contextWRune
for i := 0; i < 8; i++ {
contextDataVal = (contextDataVal << 1) | (value & 1)
if contextDataPosition == bitsPerChar-1 {
contextDataPosition = 0
contextDataString.WriteRune(charArr[contextDataVal])
contextDataVal = 0
} else {
contextDataPosition++
}
value >>= 1
}
} else {
value = 1
for i := 0; i < contextNumBits; i++ {
contextDataVal = (contextDataVal << 1) | value
if contextDataPosition == bitsPerChar-1 {
contextDataPosition = 0
contextDataString.WriteRune(charArr[contextDataVal])
contextDataVal = 0
} else {
contextDataPosition++
}
value = 0
}
value = contextWRune
for i := 0; i < 16; i++ {
contextDataVal = (contextDataVal << 1) | (value & 1)
if contextDataPosition == bitsPerChar-1 {
contextDataPosition = 0
contextDataString.WriteRune(charArr[contextDataVal])
contextDataVal = 0
} else {
contextDataPosition++
}
value >>= 1
}
}
contextEnlargeIn--
if contextEnlargeIn == 0 {
contextEnlargeIn = int64(math.Pow(2, float64(contextNumBits)))
contextNumBits++
}
delete(contextDictionaryToCreate, contextW)
} else {
value = contextDictionary[contextW]
for i := 0; i < contextNumBits; i++ {
contextDataVal = (contextDataVal << 1) | (value & 1)
if contextDataPosition == bitsPerChar-1 {
contextDataPosition = 0
contextDataString.WriteRune(charArr[contextDataVal])
contextDataVal = 0
} else {
contextDataPosition++
}
value >>= 1
}
}
contextEnlargeIn--
if contextEnlargeIn == 0 {
contextEnlargeIn = int64(math.Pow(2, float64(contextNumBits)))
contextNumBits++
}
}
value = 2
for i := 0; i < contextNumBits; i++ {
contextDataVal = (contextDataVal << 1) | (value & 1)
if contextDataPosition == bitsPerChar-1 {
contextDataPosition = 0
contextDataString.WriteRune(charArr[contextDataVal])
contextDataVal = 0
} else {
contextDataPosition++
}
value >>= 1
}
for {
contextDataVal <<= 1
if contextDataPosition == bitsPerChar-1 {
contextDataString.WriteRune(charArr[contextDataVal])
break
} else {
contextDataPosition++
}
}
return contextDataString.String()
}
// Decompress
var baseReverseDic = sync.Map{}
type dataStruct struct {
input string
alphabet string
val int
position int
index int
dictionary []string
enlargeIn float64
numBits int
}
func covertToBaseReverseDic(alphabet string) map[byte]int {
var val = map[byte]int{}
charArr := []rune(alphabet)
for i := 0; i < len(charArr); i++ {
val[byte(charArr[i])] = i
}
return val
}
func getBaseValue(alphabet string, char byte) int {
vv, ok := baseReverseDic.Load(alphabet)
var arr map[byte]int
if ok {
arr = vv.(map[byte]int)
} else {
arr = covertToBaseReverseDic(alphabet)
baseReverseDic.Store(alphabet, arr)
}
return arr[char]
}
// Input is composed of ASCII characters, so accessing it by array has no UTF-8 pb.
func readBits(nb int, data *dataStruct) int {
result := 0
power := 1
for i := 0; i < nb; i++ {
respB := data.val & data.position
data.position /= 2
if data.position == 0 {
data.position = 32
data.val = getBaseValue(data.alphabet, data.input[data.index])
data.index++
}
if respB > 0 {
result |= power
}
power *= 2
}
return result
}
func appendValue(data *dataStruct, str string) {
data.dictionary = append(data.dictionary, str)
data.enlargeIn--
if data.enlargeIn == 0 {
data.enlargeIn = math.Pow(2, float64(data.numBits))
data.numBits++
}
}
func getString(last string, data *dataStruct) (string, bool, error) {
c := readBits(data.numBits, data)
switch c {
case 0:
str := string(rune(readBits(8, data)))
appendValue(data, str)
return str, false, nil
case 1:
str := string(rune(readBits(16, data)))
appendValue(data, str)
return str, false, nil
case 2:
return "", true, nil
}
if c < len(data.dictionary) {
return data.dictionary[c], false, nil
}
if c == len(data.dictionary) {
return concatWithFirstRune(last, last), false, nil
}
return "", false, errors.New("bad character encoding")
}
// Need to handle UTF-8, so we need to use rune to concatenate
func concatWithFirstRune(str string, getFirstRune string) string {
r, _ := utf8.DecodeRuneInString(getFirstRune)
return str + string(r)
}
func Decompress(input string, keyStrBase64 string) (string, error) {
if keyStrBase64 == "" {
keyStrBase64 = _defaultKeyStrBase64
}
data := dataStruct{input, keyStrBase64, getBaseValue(keyStrBase64, input[0]), 32, 1, []string{"0", "1", "2"}, 5, 2}
result, isEnd, err := getString("", &data)
if err != nil || isEnd {
return result, err
}
last := result
data.numBits++
for {
str, isEnd, err := getString(last, &data)
if err != nil || isEnd {
return result, err
}
result += str
appendValue(&data, concatWithFirstRune(last, str))
last = str
}
}