base64-string.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. // This lib is part of the lz-string project.
  7. // For more information, the home page:
  8. // http://pieroxy.net/blog/pages/lz-string/index.html
  9. //
  10. // Base64 compression / decompression for already compressed content (gif, png, jpg, mp3, ...)
  11. // version 1.4.1
  12. var Base64String = {
  13. compressToUTF16 : function (input) {
  14. var output = [],
  15. i,c,
  16. current,
  17. status = 0;
  18. input = this.compress(input);
  19. for (i=0 ; i<input.length ; i++) {
  20. c = input.charCodeAt(i);
  21. switch (status++) {
  22. case 0:
  23. output.push(String.fromCharCode((c >> 1)+32));
  24. current = (c & 1) << 14;
  25. break;
  26. case 1:
  27. output.push(String.fromCharCode((current + (c >> 2))+32));
  28. current = (c & 3) << 13;
  29. break;
  30. case 2:
  31. output.push(String.fromCharCode((current + (c >> 3))+32));
  32. current = (c & 7) << 12;
  33. break;
  34. case 3:
  35. output.push(String.fromCharCode((current + (c >> 4))+32));
  36. current = (c & 15) << 11;
  37. break;
  38. case 4:
  39. output.push(String.fromCharCode((current + (c >> 5))+32));
  40. current = (c & 31) << 10;
  41. break;
  42. case 5:
  43. output.push(String.fromCharCode((current + (c >> 6))+32));
  44. current = (c & 63) << 9;
  45. break;
  46. case 6:
  47. output.push(String.fromCharCode((current + (c >> 7))+32));
  48. current = (c & 127) << 8;
  49. break;
  50. case 7:
  51. output.push(String.fromCharCode((current + (c >> 8))+32));
  52. current = (c & 255) << 7;
  53. break;
  54. case 8:
  55. output.push(String.fromCharCode((current + (c >> 9))+32));
  56. current = (c & 511) << 6;
  57. break;
  58. case 9:
  59. output.push(String.fromCharCode((current + (c >> 10))+32));
  60. current = (c & 1023) << 5;
  61. break;
  62. case 10:
  63. output.push(String.fromCharCode((current + (c >> 11))+32));
  64. current = (c & 2047) << 4;
  65. break;
  66. case 11:
  67. output.push(String.fromCharCode((current + (c >> 12))+32));
  68. current = (c & 4095) << 3;
  69. break;
  70. case 12:
  71. output.push(String.fromCharCode((current + (c >> 13))+32));
  72. current = (c & 8191) << 2;
  73. break;
  74. case 13:
  75. output.push(String.fromCharCode((current + (c >> 14))+32));
  76. current = (c & 16383) << 1;
  77. break;
  78. case 14:
  79. output.push(String.fromCharCode((current + (c >> 15))+32, (c & 32767)+32));
  80. status = 0;
  81. break;
  82. }
  83. }
  84. output.push(String.fromCharCode(current + 32));
  85. return output.join('');
  86. },
  87. decompressFromUTF16 : function (input) {
  88. var output = [],
  89. current,c,
  90. status=0,
  91. i = 0;
  92. while (i < input.length) {
  93. c = input.charCodeAt(i) - 32;
  94. switch (status++) {
  95. case 0:
  96. current = c << 1;
  97. break;
  98. case 1:
  99. output.push(String.fromCharCode(current | (c >> 14)));
  100. current = (c&16383) << 2;
  101. break;
  102. case 2:
  103. output.push(String.fromCharCode(current | (c >> 13)));
  104. current = (c&8191) << 3;
  105. break;
  106. case 3:
  107. output.push(String.fromCharCode(current | (c >> 12)));
  108. current = (c&4095) << 4;
  109. break;
  110. case 4:
  111. output.push(String.fromCharCode(current | (c >> 11)));
  112. current = (c&2047) << 5;
  113. break;
  114. case 5:
  115. output.push(String.fromCharCode(current | (c >> 10)));
  116. current = (c&1023) << 6;
  117. break;
  118. case 6:
  119. output.push(String.fromCharCode(current | (c >> 9)));
  120. current = (c&511) << 7;
  121. break;
  122. case 7:
  123. output.push(String.fromCharCode(current | (c >> 8)));
  124. current = (c&255) << 8;
  125. break;
  126. case 8:
  127. output.push(String.fromCharCode(current | (c >> 7)));
  128. current = (c&127) << 9;
  129. break;
  130. case 9:
  131. output.push(String.fromCharCode(current | (c >> 6)));
  132. current = (c&63) << 10;
  133. break;
  134. case 10:
  135. output.push(String.fromCharCode(current | (c >> 5)));
  136. current = (c&31) << 11;
  137. break;
  138. case 11:
  139. output.push(String.fromCharCode(current | (c >> 4)));
  140. current = (c&15) << 12;
  141. break;
  142. case 12:
  143. output.push(String.fromCharCode(current | (c >> 3)));
  144. current = (c&7) << 13;
  145. break;
  146. case 13:
  147. output.push(String.fromCharCode(current | (c >> 2)));
  148. current = (c&3) << 14;
  149. break;
  150. case 14:
  151. output.push(String.fromCharCode(current | (c >> 1)));
  152. current = (c&1) << 15;
  153. break;
  154. case 15:
  155. output.push(String.fromCharCode(current | c));
  156. status=0;
  157. break;
  158. }
  159. i++;
  160. }
  161. return this.decompress(output.join(''));
  162. //return output;
  163. },
  164. // private property
  165. _keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
  166. decompress : function (input) {
  167. var output = [];
  168. var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
  169. var i = 1;
  170. var odd = input.charCodeAt(0) >> 8;
  171. while (i < input.length*2 && (i < input.length*2-1 || odd==0)) {
  172. if (i%2==0) {
  173. chr1 = input.charCodeAt(i/2) >> 8;
  174. chr2 = input.charCodeAt(i/2) & 255;
  175. if (i/2+1 < input.length)
  176. chr3 = input.charCodeAt(i/2+1) >> 8;
  177. else
  178. chr3 = NaN;
  179. } else {
  180. chr1 = input.charCodeAt((i-1)/2) & 255;
  181. if ((i+1)/2 < input.length) {
  182. chr2 = input.charCodeAt((i+1)/2) >> 8;
  183. chr3 = input.charCodeAt((i+1)/2) & 255;
  184. } else
  185. chr2=chr3=NaN;
  186. }
  187. i+=3;
  188. enc1 = chr1 >> 2;
  189. enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
  190. enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
  191. enc4 = chr3 & 63;
  192. if (isNaN(chr2) || (i==input.length*2+1 && odd)) {
  193. enc3 = enc4 = 64;
  194. } else if (isNaN(chr3) || (i==input.length*2 && odd)) {
  195. enc4 = 64;
  196. }
  197. output.push(this._keyStr.charAt(enc1));
  198. output.push(this._keyStr.charAt(enc2));
  199. output.push(this._keyStr.charAt(enc3));
  200. output.push(this._keyStr.charAt(enc4));
  201. }
  202. return output.join('');
  203. },
  204. compress : function (input) {
  205. var output = [],
  206. ol = 1,
  207. output_,
  208. chr1, chr2, chr3,
  209. enc1, enc2, enc3, enc4,
  210. i = 0, flush=false;
  211. input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
  212. while (i < input.length) {
  213. enc1 = this._keyStr.indexOf(input.charAt(i++));
  214. enc2 = this._keyStr.indexOf(input.charAt(i++));
  215. enc3 = this._keyStr.indexOf(input.charAt(i++));
  216. enc4 = this._keyStr.indexOf(input.charAt(i++));
  217. chr1 = (enc1 << 2) | (enc2 >> 4);
  218. chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
  219. chr3 = ((enc3 & 3) << 6) | enc4;
  220. if (ol%2==0) {
  221. output_ = chr1 << 8;
  222. flush = true;
  223. if (enc3 != 64) {
  224. output.push(String.fromCharCode(output_ | chr2));
  225. flush = false;
  226. }
  227. if (enc4 != 64) {
  228. output_ = chr3 << 8;
  229. flush = true;
  230. }
  231. } else {
  232. output.push(String.fromCharCode(output_ | chr1));
  233. flush = false;
  234. if (enc3 != 64) {
  235. output_ = chr2 << 8;
  236. flush = true;
  237. }
  238. if (enc4 != 64) {
  239. output.push(String.fromCharCode(output_ | chr3));
  240. flush = false;
  241. }
  242. }
  243. ol+=3;
  244. }
  245. if (flush) {
  246. output.push(String.fromCharCode(output_));
  247. output = output.join('');
  248. output = String.fromCharCode(output.charCodeAt(0)|256) + output.substring(1);
  249. } else {
  250. output = output.join('');
  251. }
  252. return output;
  253. }
  254. }