lz-string.js 16 KB

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