id_pack 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,511 @@
1
+ // Copyright (c) 2013 Pieroxy <pieroxy@pieroxy.net>
2
+ // This work is free. You can redistribute it and/or modify it
3
+ // under the terms of the WTFPL, Version 2
4
+ // For more information see LICENSE.txt or http://www.wtfpl.net/
5
+ //
6
+ // For more information, the home page:
7
+ // http://pieroxy.net/blog/pages/lz-string/testing.html
8
+ //
9
+ // LZ-based compression algorithm, version 1.4.4
10
+ var LZString = (function() {
11
+
12
+ // private property
13
+ var f = String.fromCharCode;
14
+ var keyStrBase64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
15
+ var keyStrUriSafe = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-$";
16
+ var baseReverseDic = {};
17
+
18
+ function getBaseValue(alphabet, character) {
19
+ if (!baseReverseDic[alphabet]) {
20
+ baseReverseDic[alphabet] = {};
21
+ for (var i=0 ; i<alphabet.length ; i++) {
22
+ baseReverseDic[alphabet][alphabet.charAt(i)] = i;
23
+ }
24
+ }
25
+ return baseReverseDic[alphabet][character];
26
+ }
27
+
28
+ var LZString = {
29
+ compressToBase64 : function (input) {
30
+ if (input == null) return "";
31
+ var res = LZString._compress(input, 6, function(a){return keyStrBase64.charAt(a);});
32
+ switch (res.length % 4) { // To produce valid Base64
33
+ default: // When could this happen ?
34
+ case 0 : return res;
35
+ case 1 : return res+"===";
36
+ case 2 : return res+"==";
37
+ case 3 : return res+"=";
38
+ }
39
+ },
40
+
41
+ decompressFromBase64 : function (input) {
42
+ if (input == null) return "";
43
+ if (input == "") return null;
44
+ return LZString._decompress(input.length, 32, function(index) { return getBaseValue(keyStrBase64, input.charAt(index)); });
45
+ },
46
+
47
+ compressToUTF16 : function (input) {
48
+ if (input == null) return "";
49
+ return LZString._compress(input, 15, function(a){return f(a+32);}) + " ";
50
+ },
51
+
52
+ decompressFromUTF16: function (compressed) {
53
+ if (compressed == null) return "";
54
+ if (compressed == "") return null;
55
+ return LZString._decompress(compressed.length, 16384, function(index) { return compressed.charCodeAt(index) - 32; });
56
+ },
57
+
58
+ //compress into uint8array (UCS-2 big endian format)
59
+ compressToUint8Array: function (uncompressed) {
60
+ var compressed = LZString.compress(uncompressed);
61
+ var buf=new Uint8Array(compressed.length*2); // 2 bytes per character
62
+
63
+ for (var i=0, TotalLen=compressed.length; i<TotalLen; i++) {
64
+ var current_value = compressed.charCodeAt(i);
65
+ buf[i*2] = current_value >>> 8;
66
+ buf[i*2+1] = current_value % 256;
67
+ }
68
+ return buf;
69
+ },
70
+
71
+ //decompress from uint8array (UCS-2 big endian format)
72
+ decompressFromUint8Array:function (compressed) {
73
+ if (compressed===null || compressed===undefined){
74
+ return LZString.decompress(compressed);
75
+ } else {
76
+ var buf=new Array(compressed.length/2); // 2 bytes per character
77
+ for (var i=0, TotalLen=buf.length; i<TotalLen; i++) {
78
+ buf[i]=compressed[i*2]*256+compressed[i*2+1];
79
+ }
80
+
81
+ var result = [];
82
+ buf.forEach(function (c) {
83
+ result.push(f(c));
84
+ });
85
+ return LZString.decompress(result.join(''));
86
+
87
+ }
88
+
89
+ },
90
+
91
+
92
+ //compress into a string that is already URI encoded
93
+ compressToEncodedURIComponent: function (input) {
94
+ if (input == null) return "";
95
+ return LZString._compress(input, 6, function(a){return keyStrUriSafe.charAt(a);});
96
+ },
97
+
98
+ //decompress from an output of compressToEncodedURIComponent
99
+ decompressFromEncodedURIComponent:function (input) {
100
+ if (input == null) return "";
101
+ if (input == "") return null;
102
+ input = input.replace(/ /g, "+");
103
+ return LZString._decompress(input.length, 32, function(index) { return getBaseValue(keyStrUriSafe, input.charAt(index)); });
104
+ },
105
+
106
+ compress: function (uncompressed) {
107
+ return LZString._compress(uncompressed, 16, function(a){return f(a);});
108
+ },
109
+ _compress: function (uncompressed, bitsPerChar, getCharFromInt) {
110
+ if (uncompressed == null) return "";
111
+ var i, value,
112
+ context_dictionary= {},
113
+ context_dictionaryToCreate= {},
114
+ context_c="",
115
+ context_wc="",
116
+ context_w="",
117
+ context_enlargeIn= 2, // Compensate for the first entry which should not count
118
+ context_dictSize= 3,
119
+ context_numBits= 2,
120
+ context_data=[],
121
+ context_data_val=0,
122
+ context_data_position=0,
123
+ ii;
124
+
125
+ for (ii = 0; ii < uncompressed.length; ii += 1) {
126
+ context_c = uncompressed.charAt(ii);
127
+ if (!Object.prototype.hasOwnProperty.call(context_dictionary,context_c)) {
128
+ context_dictionary[context_c] = context_dictSize++;
129
+ context_dictionaryToCreate[context_c] = true;
130
+ }
131
+
132
+ context_wc = context_w + context_c;
133
+ if (Object.prototype.hasOwnProperty.call(context_dictionary,context_wc)) {
134
+ context_w = context_wc;
135
+ } else {
136
+ if (Object.prototype.hasOwnProperty.call(context_dictionaryToCreate,context_w)) {
137
+ if (context_w.charCodeAt(0)<256) {
138
+ for (i=0 ; i<context_numBits ; i++) {
139
+ context_data_val = (context_data_val << 1);
140
+ if (context_data_position == bitsPerChar-1) {
141
+ context_data_position = 0;
142
+ context_data.push(getCharFromInt(context_data_val));
143
+ context_data_val = 0;
144
+ } else {
145
+ context_data_position++;
146
+ }
147
+ }
148
+ value = context_w.charCodeAt(0);
149
+ for (i=0 ; i<8 ; i++) {
150
+ context_data_val = (context_data_val << 1) | (value&1);
151
+ if (context_data_position == bitsPerChar-1) {
152
+ context_data_position = 0;
153
+ context_data.push(getCharFromInt(context_data_val));
154
+ context_data_val = 0;
155
+ } else {
156
+ context_data_position++;
157
+ }
158
+ value = value >> 1;
159
+ }
160
+ } else {
161
+ value = 1;
162
+ for (i=0 ; i<context_numBits ; i++) {
163
+ context_data_val = (context_data_val << 1) | value;
164
+ if (context_data_position ==bitsPerChar-1) {
165
+ context_data_position = 0;
166
+ context_data.push(getCharFromInt(context_data_val));
167
+ context_data_val = 0;
168
+ } else {
169
+ context_data_position++;
170
+ }
171
+ value = 0;
172
+ }
173
+ value = context_w.charCodeAt(0);
174
+ for (i=0 ; i<16 ; i++) {
175
+ context_data_val = (context_data_val << 1) | (value&1);
176
+ if (context_data_position == bitsPerChar-1) {
177
+ context_data_position = 0;
178
+ context_data.push(getCharFromInt(context_data_val));
179
+ context_data_val = 0;
180
+ } else {
181
+ context_data_position++;
182
+ }
183
+ value = value >> 1;
184
+ }
185
+ }
186
+ context_enlargeIn--;
187
+ if (context_enlargeIn == 0) {
188
+ context_enlargeIn = Math.pow(2, context_numBits);
189
+ context_numBits++;
190
+ }
191
+ delete context_dictionaryToCreate[context_w];
192
+ } else {
193
+ value = context_dictionary[context_w];
194
+ for (i=0 ; i<context_numBits ; i++) {
195
+ context_data_val = (context_data_val << 1) | (value&1);
196
+ if (context_data_position == bitsPerChar-1) {
197
+ context_data_position = 0;
198
+ context_data.push(getCharFromInt(context_data_val));
199
+ context_data_val = 0;
200
+ } else {
201
+ context_data_position++;
202
+ }
203
+ value = value >> 1;
204
+ }
205
+
206
+
207
+ }
208
+ context_enlargeIn--;
209
+ if (context_enlargeIn == 0) {
210
+ context_enlargeIn = Math.pow(2, context_numBits);
211
+ context_numBits++;
212
+ }
213
+ // Add wc to the dictionary.
214
+ context_dictionary[context_wc] = context_dictSize++;
215
+ context_w = String(context_c);
216
+ }
217
+ }
218
+
219
+ // Output the code for w.
220
+ if (context_w !== "") {
221
+ if (Object.prototype.hasOwnProperty.call(context_dictionaryToCreate,context_w)) {
222
+ if (context_w.charCodeAt(0)<256) {
223
+ for (i=0 ; i<context_numBits ; i++) {
224
+ context_data_val = (context_data_val << 1);
225
+ if (context_data_position == bitsPerChar-1) {
226
+ context_data_position = 0;
227
+ context_data.push(getCharFromInt(context_data_val));
228
+ context_data_val = 0;
229
+ } else {
230
+ context_data_position++;
231
+ }
232
+ }
233
+ value = context_w.charCodeAt(0);
234
+ for (i=0 ; i<8 ; i++) {
235
+ context_data_val = (context_data_val << 1) | (value&1);
236
+ if (context_data_position == bitsPerChar-1) {
237
+ context_data_position = 0;
238
+ context_data.push(getCharFromInt(context_data_val));
239
+ context_data_val = 0;
240
+ } else {
241
+ context_data_position++;
242
+ }
243
+ value = value >> 1;
244
+ }
245
+ } else {
246
+ value = 1;
247
+ for (i=0 ; i<context_numBits ; i++) {
248
+ context_data_val = (context_data_val << 1) | value;
249
+ if (context_data_position == bitsPerChar-1) {
250
+ context_data_position = 0;
251
+ context_data.push(getCharFromInt(context_data_val));
252
+ context_data_val = 0;
253
+ } else {
254
+ context_data_position++;
255
+ }
256
+ value = 0;
257
+ }
258
+ value = context_w.charCodeAt(0);
259
+ for (i=0 ; i<16 ; i++) {
260
+ context_data_val = (context_data_val << 1) | (value&1);
261
+ if (context_data_position == bitsPerChar-1) {
262
+ context_data_position = 0;
263
+ context_data.push(getCharFromInt(context_data_val));
264
+ context_data_val = 0;
265
+ } else {
266
+ context_data_position++;
267
+ }
268
+ value = value >> 1;
269
+ }
270
+ }
271
+ context_enlargeIn--;
272
+ if (context_enlargeIn == 0) {
273
+ context_enlargeIn = Math.pow(2, context_numBits);
274
+ context_numBits++;
275
+ }
276
+ delete context_dictionaryToCreate[context_w];
277
+ } else {
278
+ value = context_dictionary[context_w];
279
+ for (i=0 ; i<context_numBits ; i++) {
280
+ context_data_val = (context_data_val << 1) | (value&1);
281
+ if (context_data_position == bitsPerChar-1) {
282
+ context_data_position = 0;
283
+ context_data.push(getCharFromInt(context_data_val));
284
+ context_data_val = 0;
285
+ } else {
286
+ context_data_position++;
287
+ }
288
+ value = value >> 1;
289
+ }
290
+
291
+
292
+ }
293
+ context_enlargeIn--;
294
+ if (context_enlargeIn == 0) {
295
+ context_enlargeIn = Math.pow(2, context_numBits);
296
+ context_numBits++;
297
+ }
298
+ }
299
+
300
+ // Mark the end of the stream
301
+ value = 2;
302
+ for (i=0 ; i<context_numBits ; i++) {
303
+ context_data_val = (context_data_val << 1) | (value&1);
304
+ if (context_data_position == bitsPerChar-1) {
305
+ context_data_position = 0;
306
+ context_data.push(getCharFromInt(context_data_val));
307
+ context_data_val = 0;
308
+ } else {
309
+ context_data_position++;
310
+ }
311
+ value = value >> 1;
312
+ }
313
+
314
+ // Flush the last char
315
+ while (true) {
316
+ context_data_val = (context_data_val << 1);
317
+ if (context_data_position == bitsPerChar-1) {
318
+ context_data.push(getCharFromInt(context_data_val));
319
+ break;
320
+ }
321
+ else context_data_position++;
322
+ }
323
+ return context_data.join('');
324
+ },
325
+
326
+ decompress: function (compressed) {
327
+ if (compressed == null) return "";
328
+ if (compressed == "") return null;
329
+ return LZString._decompress(compressed.length, 32768, function(index) { return compressed.charCodeAt(index); });
330
+ },
331
+
332
+ _decompress: function (length, resetValue, getNextValue) {
333
+ var dictionary = [],
334
+ next,
335
+ enlargeIn = 4,
336
+ dictSize = 4,
337
+ numBits = 3,
338
+ entry = "",
339
+ result = [],
340
+ i,
341
+ w,
342
+ bits, resb, maxpower, power,
343
+ c,
344
+ data = {val:getNextValue(0), position:resetValue, index:1};
345
+
346
+ for (i = 0; i < 3; i += 1) {
347
+ dictionary[i] = i;
348
+ }
349
+
350
+ bits = 0;
351
+ maxpower = Math.pow(2,2);
352
+ power=1;
353
+ while (power!=maxpower) {
354
+ console.warn("data.val", data.val)
355
+ console.warn("data.position", data.position)
356
+ resb = data.val & data.position;
357
+ data.position >>= 1;
358
+ if (data.position == 0) {
359
+ data.position = resetValue;
360
+ data.val = getNextValue(data.index++);
361
+ }
362
+ bits |= (resb>0 ? 1 : 0) * power;
363
+ power <<= 1;
364
+ }
365
+ console.warn("bits", bits)
366
+
367
+ switch (next = bits) {
368
+ case 0:
369
+ bits = 0;
370
+ maxpower = Math.pow(2,8);
371
+ power=1;
372
+ while (power!=maxpower) {
373
+ resb = data.val & data.position;
374
+ data.position >>= 1;
375
+ if (data.position == 0) {
376
+ data.position = resetValue;
377
+ data.val = getNextValue(data.index++);
378
+ }
379
+ bits |= (resb>0 ? 1 : 0) * power;
380
+ power <<= 1;
381
+ }
382
+ c = f(bits);
383
+ break;
384
+ case 1:
385
+ bits = 0;
386
+ maxpower = Math.pow(2,16);
387
+ power=1;
388
+ while (power!=maxpower) {
389
+ resb = data.val & data.position;
390
+ data.position >>= 1;
391
+ if (data.position == 0) {
392
+ data.position = resetValue;
393
+ data.val = getNextValue(data.index++);
394
+ }
395
+ bits |= (resb>0 ? 1 : 0) * power;
396
+ power <<= 1;
397
+ }
398
+ c = f(bits);
399
+ break;
400
+ case 2:
401
+ return "";
402
+ }
403
+ dictionary[3] = c;
404
+ w = c;
405
+ result.push(c);
406
+ while (true) {
407
+ if (data.index > length) {
408
+ return "";
409
+ }
410
+
411
+ bits = 0;
412
+ maxpower = Math.pow(2,numBits);
413
+ power=1;
414
+ while (power!=maxpower) {
415
+ resb = data.val & data.position;
416
+ data.position >>= 1;
417
+ if (data.position == 0) {
418
+ data.position = resetValue;
419
+ data.val = getNextValue(data.index++);
420
+ }
421
+ bits |= (resb>0 ? 1 : 0) * power;
422
+ power <<= 1;
423
+ }
424
+
425
+ switch (c = bits) {
426
+ case 0:
427
+ bits = 0;
428
+ maxpower = Math.pow(2,8);
429
+ power=1;
430
+ while (power!=maxpower) {
431
+ resb = data.val & data.position;
432
+ data.position >>= 1;
433
+ if (data.position == 0) {
434
+ data.position = resetValue;
435
+ data.val = getNextValue(data.index++);
436
+ }
437
+ bits |= (resb>0 ? 1 : 0) * power;
438
+ power <<= 1;
439
+ }
440
+
441
+ dictionary[dictSize++] = f(bits);
442
+ c = dictSize-1;
443
+ enlargeIn--;
444
+ break;
445
+ case 1:
446
+ bits = 0;
447
+ maxpower = Math.pow(2,16);
448
+ power=1;
449
+ while (power!=maxpower) {
450
+ resb = data.val & data.position;
451
+ data.position >>= 1;
452
+ if (data.position == 0) {
453
+ data.position = resetValue;
454
+ data.val = getNextValue(data.index++);
455
+ }
456
+ bits |= (resb>0 ? 1 : 0) * power;
457
+ power <<= 1;
458
+ }
459
+ dictionary[dictSize++] = f(bits);
460
+ c = dictSize-1;
461
+ enlargeIn--;
462
+ break;
463
+ case 2:
464
+ return result.join('');
465
+ }
466
+
467
+ if (enlargeIn == 0) {
468
+ enlargeIn = Math.pow(2, numBits);
469
+ numBits++;
470
+ }
471
+
472
+ if (dictionary[c]) {
473
+ entry = dictionary[c];
474
+ } else {
475
+ if (c === dictSize) {
476
+ entry = w + w.charAt(0);
477
+ } else {
478
+ return null;
479
+ }
480
+ }
481
+ result.push(entry);
482
+
483
+ // Add w+entry[0] to the dictionary.
484
+ dictionary[dictSize++] = w + entry.charAt(0);
485
+ enlargeIn--;
486
+
487
+ w = entry;
488
+
489
+ if (enlargeIn == 0) {
490
+ enlargeIn = Math.pow(2, numBits);
491
+ numBits++;
492
+ }
493
+
494
+ }
495
+ }
496
+ };
497
+ return LZString;
498
+ })();
499
+
500
+ if (typeof define === 'function' && define.amd) {
501
+ define(function () { return LZString; });
502
+ } else if( typeof module !== 'undefined' && module != null ) {
503
+ module.exports = LZString
504
+ } else if( typeof angular !== 'undefined' && angular != null ) {
505
+ angular.module('LZString', [])
506
+ .factory('LZString', function () {
507
+ return LZString;
508
+ });
509
+ }
510
+
511
+ window.LZString = LZString;