csharp-code-LZString

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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LZStringCSharp
{
/// <summary>
/// Converted from lz-string 1.4.4
/// https://github.com/pieroxy/lz-string/blob/c58a22021000ac2d99377cc0bf9ac193a12563c5/libs/lz-string.js
/// </summary>
public class LZString
{
private const string KeyStrBase64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
private const string KeyStrUriSafe = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-$";
private static readonly IDictionary<char, char> KeyStrBase64Dict = CreateBaseDict(KeyStrBase64);
private static readonly IDictionary<char, char> KeyStrUriSafeDict = CreateBaseDict(KeyStrUriSafe);

private static IDictionary<char, char> CreateBaseDict(string alphabet)
{
var dict = new Dictionary<char, char>();
for (var i = 0; i < alphabet.Length; i++)
{
dict[alphabet[i]] = (char)i;
}
return dict;
}

public static string CompressToBase64(string input)
{
if (input == null) throw new ArgumentNullException(nameof(input));

var res = Compress(input, 6, code => KeyStrBase64[code]);
switch (res.Length % 4)
{
default: throw new InvalidOperationException("When could this happen ?");
case 0: return res;
case 1: return res + "===";
case 2: return res + "==";
case 3: return res + "=";
}
}

public static string DecompressFromBase64(string input)
{
if (input == null) throw new ArgumentNullException(nameof(input));

return Decompress(input.Length, 32, index => KeyStrBase64Dict[input[index]]);
}

public static string CompressToUTF16(string input)
{
return Compress(input, 15, code => (char)(code + 32));
}

public static string DecompressFromUTF16(string input)
{
if (input == null) throw new ArgumentNullException(nameof(input));

return Decompress(input.Length, 16384, index => (char)(input[index] - 32));
}

public static string CompressToEncodedURIComponent(string input)
{
if (input == null) return "";

return Compress(input, 6, code => KeyStrUriSafe[code]);
}

public static string DecompressFromEncodedURIComponent(string input)
{
if (input == null) throw new ArgumentNullException(nameof(input));

input = input.Replace(" ", "+");
return Decompress(input.Length, 32, index => KeyStrUriSafeDict[input[index]]);
}

public static string Compress(string uncompressed)
{
return Compress(uncompressed, 16, code => (char)code);
}

private static string Compress(string uncompressed, int bitsPerChar, Func<int, char> getCharFromInt)
{
if (uncompressed == null) throw new ArgumentNullException(nameof(uncompressed));

int i, value;
var context_dictionary = new Dictionary<string, int>();
var context_dictionaryToCreate = new Dictionary<string, bool>();
var context_wc = "";
var context_w = "";
var context_enlargeIn = 2; // Compensate for the first entry which should not count
var context_dictSize = 3;
var context_numBits = 2;
var context_data = new StringBuilder();
var context_data_val = 0;
var context_data_position = 0;

foreach (var context_c in uncompressed)
{
if (!context_dictionary.ContainsKey(context_c.ToString()))
{
context_dictionary[context_c.ToString()] = context_dictSize++;
context_dictionaryToCreate[context_c.ToString()] = true;
}

context_wc = context_w + context_c;
if (context_dictionary.ContainsKey(context_wc))
{
context_w = context_wc;
}
else
{
if (context_dictionaryToCreate.ContainsKey(context_w))
{
if (context_w.FirstOrDefault() < 256)
{
for (i = 0; i < context_numBits; i++)
{
context_data_val = (context_data_val << 1);
if (context_data_position == bitsPerChar - 1)
{
context_data_position = 0;
context_data.Append(getCharFromInt(context_data_val));
context_data_val = 0;
}
else
{
context_data_position++;
}
}
value = context_w.FirstOrDefault();
for (i = 0; i < 8; i++)
{
context_data_val = (context_data_val << 1) | (value & 1);
if (context_data_position == bitsPerChar - 1)
{
context_data_position = 0;
context_data.Append(getCharFromInt(context_data_val));
context_data_val = 0;
}
else
{
context_data_position++;
}
value = value >> 1;
}
}
else
{
value = 1;
for (i = 0; i < context_numBits; i++)
{
context_data_val = (context_data_val << 1) | value;
if (context_data_position == bitsPerChar - 1)
{
context_data_position = 0;
context_data.Append(getCharFromInt(context_data_val));
context_data_val = 0;
}
else
{
context_data_position++;
}
value = 0;
}
value = context_w.FirstOrDefault();
for (i = 0; i < 16; i++)
{
context_data_val = (context_data_val << 1) | (value & 1);
if (context_data_position == bitsPerChar - 1)
{
context_data_position = 0;
context_data.Append(getCharFromInt(context_data_val));
context_data_val = 0;
}
else
{
context_data_position++;
}
value = value >> 1;
}
}
context_enlargeIn--;
if (context_enlargeIn == 0)
{
context_enlargeIn = (int)Math.Pow(2, context_numBits);
context_numBits++;
}
context_dictionaryToCreate.Remove(context_w);
}
else
{
value = context_dictionary[context_w];
for (i = 0; i < context_numBits; i++)
{
context_data_val = (context_data_val << 1) | (value & 1);
if (context_data_position == bitsPerChar - 1)
{
context_data_position = 0;
context_data.Append(getCharFromInt(context_data_val));
context_data_val = 0;
}
else
{
context_data_position++;
}
value = value >> 1;
}


}
context_enlargeIn--;
if (context_enlargeIn == 0)
{
context_enlargeIn = (int)Math.Pow(2, context_numBits);
context_numBits++;
}
// Add wc to the dictionary.
context_dictionary[context_wc] = context_dictSize++;
context_w = context_c.ToString();
}
}

// Output the code for w.
if (context_w != "")
{
if (context_dictionaryToCreate.ContainsKey(context_w))
{
if (context_w.FirstOrDefault() < 256)
{
for (i = 0; i < context_numBits; i++)
{
context_data_val = (context_data_val << 1);
if (context_data_position == bitsPerChar - 1)
{
context_data_position = 0;
context_data.Append(getCharFromInt(context_data_val));
context_data_val = 0;
}
else
{
context_data_position++;
}
}
value = context_w.FirstOrDefault();
for (i = 0; i < 8; i++)
{
context_data_val = (context_data_val << 1) | (value & 1);
if (context_data_position == bitsPerChar - 1)
{
context_data_position = 0;
context_data.Append(getCharFromInt(context_data_val));
context_data_val = 0;
}
else
{
context_data_position++;
}
value = value >> 1;
}
}
else
{
value = 1;
for (i = 0; i < context_numBits; i++)
{
context_data_val = (context_data_val << 1) | value;
if (context_data_position == bitsPerChar - 1)
{
context_data_position = 0;
context_data.Append(getCharFromInt(context_data_val));
context_data_val = 0;
}
else
{
context_data_position++;
}
value = 0;
}
value = context_w.FirstOrDefault();
for (i = 0; i < 16; i++)
{
context_data_val = (context_data_val << 1) | (value & 1);
if (context_data_position == bitsPerChar - 1)
{
context_data_position = 0;
context_data.Append(getCharFromInt(context_data_val));
context_data_val = 0;
}
else
{
context_data_position++;
}
value = value >> 1;
}
}
context_enlargeIn--;
if (context_enlargeIn == 0)
{
context_enlargeIn = (int)Math.Pow(2, context_numBits);
context_numBits++;
}
context_dictionaryToCreate.Remove(context_w);
}
else
{
value = context_dictionary[context_w];
for (i = 0; i < context_numBits; i++)
{
context_data_val = (context_data_val << 1) | (value & 1);
if (context_data_position == bitsPerChar - 1)
{
context_data_position = 0;
context_data.Append(getCharFromInt(context_data_val));
context_data_val = 0;
}
else
{
context_data_position++;
}
value = value >> 1;
}


}
context_enlargeIn--;
if (context_enlargeIn == 0)
{
context_enlargeIn = (int)Math.Pow(2, context_numBits);
context_numBits++;
}
}

// Mark the end of the stream
value = 2;
for (i = 0; i < context_numBits; i++)
{
context_data_val = (context_data_val << 1) | (value & 1);
if (context_data_position == bitsPerChar - 1)
{
context_data_position = 0;
context_data.Append(getCharFromInt(context_data_val));
context_data_val = 0;
}
else
{
context_data_position++;
}
value = value >> 1;
}

// Flush the last char
while (true)
{
context_data_val = (context_data_val << 1);
if (context_data_position == bitsPerChar - 1)
{
context_data.Append(getCharFromInt(context_data_val));
break;
}
else context_data_position++;
}
return context_data.ToString();
}

public static string Decompress(string compressed)
{
if (compressed == null) throw new ArgumentNullException(nameof(compressed));

//TODO: Use an enumerator
return Decompress(compressed.Length, 32768, index => compressed[index]);
}

private static string Decompress(int length, int resetValue, Func<int, char> getNextValue)
{
var dictionary = new List<string>();
var enlargeIn = 4;
var numBits = 3;
string entry;
var result = new StringBuilder();
int i;
string w;
int bits = 0, resb, maxpower, power;
var c = '\0';

var data_val = getNextValue(0);
var data_position = resetValue;
var data_index = 1;

for (i = 0; i < 3; i += 1)
{
dictionary.Add(((char)i).ToString());
}

maxpower = (int)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++);
}
bits |= (resb > 0 ? 1 : 0) * power;
power <<= 1;
}

switch (bits)
{
case 0:
bits = 0;
maxpower = (int)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++);
}
bits |= (resb > 0 ? 1 : 0) * power;
power <<= 1;
}
c = (char)bits;
break;
case 1:
bits = 0;
maxpower = (int)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++);
}
bits |= (resb > 0 ? 1 : 0) * power;
power <<= 1;
}
c = (char)bits;
break;
case 2:
return "";
}
w = c.ToString();
dictionary.Add(w);
result.Append(c);
while (true)
{
if (data_index > length)
{
return "";
}

bits = 0;
maxpower = (int)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++);
}
bits |= (resb > 0 ? 1 : 0) * power;
power <<= 1;
}

int c2;
switch (c2 = bits)
{
case (char)0:
bits = 0;
maxpower = (int)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++);
}
bits |= (resb > 0 ? 1 : 0) * power;
power <<= 1;
}

c2 = dictionary.Count;
dictionary.Add(((char)bits).ToString());
enlargeIn--;
break;
case (char)1:
bits = 0;
maxpower = (int)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++);
}
bits |= (resb > 0 ? 1 : 0) * power;
power <<= 1;
}
c2 = dictionary.Count;
dictionary.Add(((char)bits).ToString());
enlargeIn--;
break;
case (char)2:
return result.ToString();
}

if (enlargeIn == 0)
{
enlargeIn = (int)Math.Pow(2, numBits);
numBits++;
}

if (dictionary.Count - 1 >= c2)
{
entry = dictionary[c2];
}
else
{
if (c2 == dictionary.Count)
{
entry = w + w[0];
}
else
{
return null;
}
}
result.Append(entry);

// Add w+entry[0] to the dictionary.
dictionary.Add(w + entry[0]);
enlargeIn--;

w = entry;

if (enlargeIn == 0)
{
enlargeIn = (int)Math.Pow(2, numBits);
numBits++;
}
}
}
}
}