-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLZString.vb
511 lines (308 loc) · 12.5 KB
/
LZString.vb
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
Public NotInheritable Class LZString
' Public members
Public Shared Function CompressToBase64(str As String) As String
If String.IsNullOrEmpty(str) Then Return String.Empty
Dim res As String = Compress(str, 6, Function(a) KeyStrBase64(a))
Select Case res.Length Mod 4
Case 1
Return res & "==="
Case 2
Return res & "=="
Case 3
Return res & "="
Case Else
Return res
End Select
End Function
Public Shared Function DecompressFromBase64(compressed As String) As String
If String.IsNullOrEmpty(compressed) Then Return String.Empty
Return Decompress(compressed.Length, 32, Function(index) GetBaseValue(KeyStrBase64, compressed(index)))
End Function
Public Shared Function CompressToUtf16(str As String) As String
If String.IsNullOrEmpty(str) Then Return String.Empty
Return Compress(str, 15, Function(a) ChrW(a + 32))
End Function
Public Shared Function DecompressFromUtf16(compressed As String) As String
If String.IsNullOrEmpty(compressed) Then Return String.Empty
Return Decompress(compressed.Length, 16384, Function(index) ChrW(AscW(compressed(index)) - 32))
End Function
''' <summary>compress into uint8array (UCS-2 big endian format)</summary>
Public Shared Function CompressToUInt8Array(str As String) As Byte()
Dim compressed As String = Compress(str)
Dim buffer = New Byte(compressed.Length * 2 - 1) {} ' 2 bytes per character
Dim totalLength As Integer = compressed.Length
For i As Integer = 0 To totalLength - 1
Dim currentValue As UInteger = AscW(compressed(i))
buffer(i * 2) = currentValue >> 8
buffer(i * 2 + 1) = currentValue Mod 256
Next
Return buffer
End Function
''' <summary>decompress from uint8array (UCS-2 big endian format)</summary>
Public Shared Function DecompressFromUInt8Array(compressed As Byte()) As String
If compressed Is Nothing Then Return String.Empty
Dim result As New Text.StringBuilder
For i As Integer = 0 To compressed.Length / 2 - 1
Dim currentValue As Char = ChrW(compressed(i * 2) * 256 + compressed(i * 2 + 1))
result.Append(currentValue)
Next
Return Decompress(result.ToString())
End Function
Public Shared Function CompressToEncodedUriComponent(str As String) As String
If str Is Nothing Then Return String.Empty
Return Compress(str, 6, Function(a) KeyStrUriSafe(a))
End Function
Public Shared Function DecompressFromEncodedUriComponent(compressed As String) As String
If compressed Is Nothing Then Return String.Empty
If String.IsNullOrEmpty(compressed) Then Return Nothing
compressed = compressed.Replace(" ", "+")
Return Decompress(compressed.Length, 32, Function(index) GetBaseValue(KeyStrUriSafe, compressed(index)))
End Function
Public Shared Function Compress(str As String) As String
If str Is Nothing Then Return String.Empty
Return Compress(str, 16, Function(a) ChrW(a))
End Function
Public Shared Function Decompress(compressed As String) As String
If String.IsNullOrEmpty(compressed) Then Return String.Empty
Return Decompress(compressed.Length, 32768, Function(index) compressed(index))
End Function
' Private members
Private Structure CompressData
Dim [String] As Text.StringBuilder
Dim Val As Integer
Dim Position As Integer
Dim Index As Integer
Dim BitsPerChar As Integer
Dim GetCharFromInt As Func(Of Integer, Char)
End Structure
Private Structure DecompressData
Dim Val As Integer
Dim Position As Integer
Dim Index As Integer
Dim ResetValue As Integer
Dim GetNextValue As Func(Of Integer, Char)
End Structure
Private Structure Context
Dim Dictionary As Dictionary(Of String, Integer)
Dim DictionaryToCreate As Dictionary(Of String, Boolean)
Dim C As String
Dim WC As String
Dim W As String
Dim EnlargeIn As Integer
Dim DictSize As Integer
Dim NumBits As Integer
Dim Result As String
Dim Data As CompressData
End Structure
Private Const KeyStrBase64 As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="
Private Const KeyStrUriSafe As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-$"
Private Shared ReadOnly BaseReverseDic As New Dictionary(Of String, Dictionary(Of Integer, Char))
Private Sub New()
End Sub
Private Shared Function ReadBit(ByRef data As DecompressData) As Integer
Dim res As Integer = data.Val And data.Position
data.Position >>= 1
If data.Position = 0 Then
data.Position = data.ResetValue
data.Val = AscW(data.GetNextValue(data.Index))
data.Index += 1
End If
Return If(res > 0, 1, 0)
End Function
Private Shared Function ReadBits(numBits As Integer, ByRef data As DecompressData) As Integer
Dim res As Integer = 0
Dim maxpower As Integer = Math.Pow(2, numBits)
Dim power As Integer = 1
While power <> maxpower
res = res Or ReadBit(data) * power
power <<= 1
End While
Return res
End Function
Private Shared Sub WriteBit(value As Integer, ByRef data As CompressData)
data.Val = (data.Val << 1) Or value
If data.Position = data.BitsPerChar - 1 Then
data.Position = 0
data.[String].Append(data.GetCharFromInt(data.Val))
data.Val = 0
Else
data.Position += 1
End If
End Sub
Private Shared Sub WriteBits(numBits As Integer, value As Integer, ByRef data As CompressData)
For i As Integer = 0 To numBits - 1
WriteBit(value And 1, data)
value >>= 1
Next
End Sub
Private Shared Sub WriteBits(numBits As Integer, value As String, ByRef data As CompressData)
WriteBits(numBits, AscW(value(0)), data)
End Sub
Private Shared Sub ProduceW(ByRef context As Context)
If context.DictionaryToCreate.ContainsKey(context.W) Then
If AscW(context.W(0)) < 256 Then
WriteBits(context.NumBits, 0, context.Data)
WriteBits(8, context.W, context.Data)
Else
WriteBits(context.NumBits, 1, context.Data)
WriteBits(16, context.W, context.Data)
End If
DecrementEnlargeIn(context)
context.DictionaryToCreate.Remove(context.W)
Else
WriteBits(context.NumBits, context.Dictionary(context.W), context.Data)
End If
DecrementEnlargeIn(context)
End Sub
Private Shared Sub DecrementEnlargeIn(ByRef context As Context)
context.EnlargeIn -= 1
If context.EnlargeIn = 0 Then
context.EnlargeIn = Math.Pow(2, context.NumBits)
context.NumBits += 1
End If
End Sub
Private Shared Function GetBaseValue(alphabet As String, character As Char) As Char
If Not BaseReverseDic.ContainsKey(alphabet) Then
Dim newBaseReverseDict = New Dictionary(Of Integer, Char)
For i As Integer = 0 To alphabet.Length - 1
newBaseReverseDict(AscW(alphabet(i))) = ChrW(i)
Next
BaseReverseDic(alphabet) = newBaseReverseDict
End If
Return BaseReverseDic(alphabet)(AscW(character))
End Function
Private Shared Function Compress(uncompressed As String, bitsPerChar As Integer, getCharFromInt As Func(Of Integer, Char)) As String
Dim context As New Context With {
.Dictionary = New Dictionary(Of String, Integer),
.DictionaryToCreate = New Dictionary(Of String, Boolean),
.C = "",
.WC = "",
.W = "",
.EnlargeIn = 2,
.DictSize = 3,
.NumBits = 2,
.Result = "",
.Data = New CompressData With {
.[String] = New Text.StringBuilder,
.Val = 0,
.Position = 0,
.BitsPerChar = bitsPerChar,
.GetCharFromInt = getCharFromInt
}
}
For i As Integer = 0 To uncompressed.Length - 1
context.C = uncompressed(i)
If Not context.Dictionary.ContainsKey(context.C) Then
context.Dictionary(context.C) = context.DictSize
context.DictSize += 1
context.DictionaryToCreate(context.C) = True
End If
context.WC = context.W & context.C
If context.Dictionary.ContainsKey(context.WC) Then
context.W = context.WC
Else
ProduceW(context)
' Add WC to the dictionary.
context.Dictionary(context.WC) = context.DictSize
context.DictSize += 1
context.W = context.C
End If
Next
' Output the code for W.
If Not String.IsNullOrEmpty(context.W) Then
ProduceW(context)
End If
' Mark the end of the stream.
WriteBits(context.NumBits, 2, context.Data)
' Flush the last character.
While True
context.Data.Val <<= 1
If context.Data.Position = bitsPerChar - 1 Then
context.Data.String.Append(getCharFromInt(context.Data.Val))
Exit While
Else
context.Data.Position += 1
End If
End While
Return context.Data.String.ToString
End Function
Private Shared Function Decompress(length As Integer, resetValue As Integer, getNextValue As Func(Of Integer, Char)) As String
Dim dictionary As New Dictionary(Of Integer, String)
Dim enlargeIn As Integer = 4
Dim dictSize As Integer = 4
Dim numBits As Integer = 3
Dim entry As String
Dim result As New Text.StringBuilder
Dim c As Integer
Dim w As String
Dim errorCount As Integer = 0
Dim data As New DecompressData With {
.Val = AscW(getNextValue(0)),
.Position = resetValue,
.Index = 1,
.ResetValue = resetValue,
.GetNextValue = getNextValue
}
For i As Integer = 0 To 3 - 1
dictionary(i) = ChrW(i)
Next
Dim [next] As Integer = ReadBits(2, data)
Select Case [next]
Case 0
c = ReadBits(8, data)
Case 1
c = ReadBits(16, data)
Case 2
Return String.Empty
End Select
dictionary(3) = ChrW(c)
w = ChrW(c)
result.Append(ChrW(c))
While True
If data.Index > length Then
Return String.Empty
End If
c = ReadBits(numBits, data)
Select Case c
Case 0
If errorCount > 10000 Then Return "Error"
errorCount += 1
c = ReadBits(8, data)
dictionary(dictSize) = ChrW(c)
dictSize += 1
c = dictSize - 1
enlargeIn -= 1
Case 1
c = ReadBits(16, data)
dictionary(dictSize) = ChrW(c)
dictSize += 1
c = dictSize - 1
enlargeIn -= 1
Case 2
Return result.ToString()
End Select
If enlargeIn = 0 Then
enlargeIn = Math.Pow(2, numBits)
numBits += 1
End If
If dictionary.ContainsKey(c) Then
entry = dictionary(c)
ElseIf c = dictSize Then
entry = w & w(0)
Else
Return String.Empty
End If
result.Append(entry)
' Add w+entry[0] to the dictionary.
dictionary(dictSize) = w & entry(0)
dictSize += 1
enlargeIn -= 1
w = entry
If enlargeIn = 0 Then
enlargeIn = Math.Pow(2, numBits)
numBits += 1
End If
End While
Return result.ToString()
End Function
End Class