Repository navigation
Expand file tree
/
Copy pathangular-lz-string.js
More file actions
437 lines (366 loc) · 10.8 KB
/
Copy pathangular-lz-string.js
File metadata and controls
437 lines (366 loc) · 10.8 KB
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
// angular-lz-string.js
// Author: Carl Ansley <carl.ansley@gmail.com>
// Wraps lz-string library by Pieroxy
(function (angular, undefined) {
//noinspection SpellCheckingInspection
var base64Characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
function compressToBase64(input) {
if (input == null) return "";
var output = "";
var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
var i = 0;
input = compress(input);
while (i < input.length * 2) {
if (i % 2 == 0) {
chr1 = input.charCodeAt(i / 2) >> 8;
chr2 = input.charCodeAt(i / 2) & 255;
if (i / 2 + 1 < input.length)
chr3 = input.charCodeAt(i / 2 + 1) >> 8;
else
chr3 = NaN;
} else {
chr1 = input.charCodeAt((i - 1) / 2) & 255;
if ((i + 1) / 2 < input.length) {
chr2 = input.charCodeAt((i + 1) / 2) >> 8;
chr3 = input.charCodeAt((i + 1) / 2) & 255;
} else
chr2 = chr3 = NaN;
}
i += 3;
enc1 = chr1 >> 2;
enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
enc4 = chr3 & 63;
if (isNaN(chr2)) {
enc3 = enc4 = 64;
} else if (isNaN(chr3)) {
enc4 = 64;
}
output = output +
base64Characters[enc1] + base64Characters[enc2] +
base64Characters[enc3] + base64Characters[enc4];
}
return output;
}
function decompressFromBase64(input) {
if (input == null) return "";
var output = "",
ol = 0,
output_ = null,
chr1, chr2, chr3,
enc1, enc2, enc3, enc4,
i = 0;
input = input.replace(/[^A-Za-z0-9\+\/=]/g, "");
while (i < input.length) {
enc1 = base64Characters.indexOf(input[i++]);
enc2 = base64Characters.indexOf(input[i++]);
enc3 = base64Characters.indexOf(input[i++]);
enc4 = base64Characters.indexOf(input[i++]);
chr1 = (enc1 << 2) | (enc2 >> 4);
chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
chr3 = ((enc3 & 3) << 6) | enc4;
if (ol % 2 == 0) {
output_ = chr1 << 8;
if (enc3 != 64) {
output += String.fromCharCode(output_ | chr2);
}
if (enc4 != 64) {
output_ = chr3 << 8;
}
} else {
output = output + String.fromCharCode(output_ | chr1);
if (enc3 != 64) {
output_ = chr2 << 8;
}
if (enc4 != 64) {
output += String.fromCharCode(output_ | chr3);
}
}
ol += 3;
}
return decompress(output);
}
function compressToUTF16(input) {
if (input == null) return "";
var output = "",
i, c,
current = 0,
status = 0;
input = compress(input);
for (i = 0; i < input.length; i++) {
c = input.charCodeAt(i);
switch (status) {
case 0:
output += String.fromCharCode((c >> 1) + 32);
current = (c & 1) << 14;
status++;
break;
case 14:
output += String.fromCharCode((current + (c >> 15)) + 32, (c & 32767) + 32);
status = 0;
break;
default:
output += String.fromCharCode((current + (c >> (status + 1))) + 32);
current = (c & ((2 << status) - 1)) << (14 - status);
status++;
break;
}
}
return output + String.fromCharCode(current + 32);
}
function decompressFromUTF16(input) {
if (input == null) {
return "";
}
var output = "", current = 0, c;
for (var i=0 ; i< input.length ; i++) {
c = input.charCodeAt(i) - 32;
if ((i & 15) !== 0) {
output += String.fromCharCode(current | (c >> (15 - (i & 15))));
}
current = (c & ((1 << (15 - (i & 15))) - 1)) << ((i+1) & 15);
}
return decompress(output);
}
//compress into uint8array (UCS-2 big endian format)
function compressToUint8Array(uncompressed) {
var compressed = compress(uncompressed);
var buf = new Uint8Array(compressed.length * 2); // 2 bytes per character
for (var i = 0, TotalLen = compressed.length; i < TotalLen; i++) {
var current_value = compressed.charCodeAt(i);
buf[i * 2] = current_value >>> 8;
buf[i * 2 + 1] = current_value % 256;
}
return buf;
}
//decompress from uint8array (UCS-2 big endian format)
function decompressFromUint8Array(compressed) {
if (compressed === null || compressed === undefined) {
return decompress(compressed);
} else {
var buf = new Array(compressed.length / 2); // 2 bytes per character
for (var i = 0, TotalLen = buf.length; i < TotalLen; i++) {
buf[i] = compressed[i * 2] * 256 + compressed[i * 2 + 1];
}
var result = "";
buf.forEach(function (c) {
result += String.fromCharCode(c);
});
return decompress(result);
}
}
//compress into a string that is already URI encoded
function compressToEncodedURIComponent(uncompressed) {
return compressToBase64(uncompressed).replace(/=/g, "$").replace(/\//g, "-");
}
//decompress from an output of compressToEncodedURIComponent
function decompressFromEncodedURIComponent(compressed) {
if (compressed) compressed = compressed.replace(/$/g, "=").replace(/-/g, "/");
return decompressFromBase64(compressed);
}
function writeBit(value, data) {
data.val = (data.val << 1) | value;
if (data.position == 15) {
data.position = 0;
data.string += String.fromCharCode(data.val);
data.val = 0;
} else {
data.position++;
}
}
function writeBits(numBits, value, data) {
if (typeof(value) == "string")
value = value.charCodeAt(0);
for (var i = 0; i < numBits; i++) {
writeBit(value & 1, data);
value = value >> 1;
}
}
function produceW(context) {
if (context.dictionaryToCreate[context.w]) {
if (context.w.charCodeAt(0) < 256) {
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);
}
decrementEnlargeIn(context);
delete context.dictionaryToCreate[context.w];
} else {
writeBits(context.numBits, context.dictionary[context.w], context.data);
}
decrementEnlargeIn(context);
}
function decrementEnlargeIn(context) {
context.enlargeIn--;
if (context.enlargeIn == 0) {
context.enlargeIn = Math.pow(2, context.numBits);
context.numBits++;
}
}
function compress(uncompressed) {
if (uncompressed === null || uncompressed === undefined) {
return '';
}
var context = {
dictionary: {},
dictionaryToCreate: {},
c: "",
wc: "",
w: "",
enlargeIn: 2, // Compensate for the first entry which should not count
dictSize: 3,
numBits: 2,
result: "",
data: {string: "", val: 0, position: 0}
}, i;
for (i = 0; i < uncompressed.length; i += 1) {
context.c = uncompressed.charAt(i);
if (!context.dictionary[context.c]) {
context.dictionary[context.c] = context.dictSize++;
context.dictionaryToCreate[context.c] = true;
}
context.wc = context.w + context.c;
if (context.dictionary[context.wc]) {
context.w = context.wc;
} else {
produceW(context);
// Add wc to the dictionary.
context.dictionary[context.wc] = context.dictSize++;
context.w = String(context.c);
}
}
// Output the code for w.
if (context.w !== "") {
produceW(context);
}
// Mark the end of the stream
writeBits(context.numBits, 2, context.data);
// Flush the last char
while (true) {
context.data.val = (context.data.val << 1);
if (context.data.position == 15) {
context.data.string += String.fromCharCode(context.data.val);
break;
}
else context.data.position++;
}
return context.data.string;
}
function readBit(data) {
var res = data.val & data.position;
data.position >>= 1;
if (data.position == 0) {
data.position = 32768;
data.val = data.string.charCodeAt(data.index++);
}
return res > 0 ? 1 : 0;
}
function readBits(numBits, data) {
var res = 0;
var maxpower = Math.pow(2, numBits);
var power = 1;
while (power != maxpower) {
res |= readBit(data) * power;
power <<= 1;
}
return res;
}
function decompress(compressed) {
if (compressed === '') {
return null;
}
if (compressed === null || compressed === undefined) {
return '';
}
var dictionary = {},
next,
enlargeIn = 4,
dictSize = 4,
numBits = 3,
entry = "",
result,
i,
w,
c,
errorCount = 0,
data = {string: compressed, val: compressed.charCodeAt(0), position: 32768, index: 1};
for (i = 0; i < 3; i += 1) {
dictionary[i] = i;
}
next = readBits(2, data);
switch (next) {
case 0:
c = String.fromCharCode(readBits(8, data));
break;
case 1:
c = String.fromCharCode(readBits(16, data));
break;
case 2:
return "";
}
dictionary[3] = c;
w = result = c;
while (true) {
c = readBits(numBits, data);
switch (c) {
case 0:
if (errorCount++ > 10000) return "Error";
c = String.fromCharCode(readBits(8, data));
dictionary[dictSize++] = c;
c = dictSize - 1;
enlargeIn--;
break;
case 1:
c = String.fromCharCode(readBits(16, data));
dictionary[dictSize++] = c;
c = dictSize - 1;
enlargeIn--;
break;
case 2:
return result;
}
if (enlargeIn == 0) {
enlargeIn = Math.pow(2, numBits);
numBits++;
}
if (dictionary[c]) {
entry = dictionary[c];
} else {
if (c === dictSize) {
entry = w + w.charAt(0);
} else {
return null;
}
}
result += entry;
// Add w+entry[0] to the dictionary.
dictionary[dictSize++] = w + entry.charAt(0);
enlargeIn--;
w = entry;
if (enlargeIn == 0) {
enlargeIn = Math.pow(2, numBits);
numBits++;
}
}
}
angular.module('lz-string', [])
.factory('LZString', function () {
return {
compressToBase64: compressToBase64,
decompressFromBase64: decompressFromBase64,
compressToUTF16: compressToUTF16,
decompressFromUTF16: decompressFromUTF16,
compressToUint8Array: compressToUint8Array,
decompressFromUint8Array: decompressFromUint8Array,
compressToEncodedURIComponent: compressToEncodedURIComponent,
decompressFromEncodedURIComponent: decompressFromEncodedURIComponent,
compress: compress,
decompress: decompress
};
});
if (typeof module !== 'undefined') {
module.exports = 'lz-string';
}
}(angular));