base64-1.7.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * $Id: base64.js,v 1.7 2012/08/23 10:30:18 dankogai Exp dankogai $
  3. *
  4. * Licensed under the MIT license.
  5. * http://www.opensource.org/licenses/mit-license.php
  6. *
  7. * References:
  8. * http://en.wikipedia.org/wiki/Base64
  9. */
  10. (function(global){
  11. var b64chars
  12. = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
  13. var b64charcodes = function(){
  14. var a = [];
  15. var codeA = 'A'.charCodeAt(0);
  16. var codea = 'a'.charCodeAt(0);
  17. var code0 = '0'.charCodeAt(0);
  18. for (var i = 0; i < 26; i ++) a.push(codeA + i);
  19. for (var i = 0; i < 26; i ++) a.push(codea + i);
  20. for (var i = 0; i < 10; i ++) a.push(code0 + i);
  21. a.push('+'.charCodeAt(0));
  22. a.push('/'.charCodeAt(0));
  23. return a;
  24. }();
  25. var b64tab = function(bin){
  26. var t = {};
  27. for (var i = 0, l = bin.length; i < l; i++) t[bin.charAt(i)] = i;
  28. return t;
  29. }(b64chars);
  30. var stringToArray = function(s){
  31. var a = [];
  32. for (var i = 0, l = s.length; i < l; i ++) a[i] = s.charCodeAt(i);
  33. return a;
  34. };
  35. var convertUTF8ArrayToBase64 = function(bin){
  36. var padlen = 0;
  37. while (bin.length % 3){
  38. bin.push(0);
  39. padlen++;
  40. };
  41. var b64 = [];
  42. for (var i = 0, l = bin.length; i < l; i += 3){
  43. var c0 = bin[i], c1 = bin[i+1], c2 = bin[i+2];
  44. if (c0 >= 256 || c1 >= 256 || c2 >= 256)
  45. throw 'unsupported character found';
  46. var n = (c0 << 16) | (c1 << 8) | c2;
  47. b64.push(
  48. b64charcodes[ n >>> 18],
  49. b64charcodes[(n >>> 12) & 63],
  50. b64charcodes[(n >>> 6) & 63],
  51. b64charcodes[ n & 63]
  52. );
  53. }
  54. while (padlen--) b64[b64.length - padlen - 1] = '='.charCodeAt(0);
  55. return chunkStringFromCharCodeApply(b64);
  56. };
  57. var convertBase64ToUTF8Array = function(b64){
  58. b64 = b64.replace(/[^A-Za-z0-9+\/]+/g, '');
  59. var bin = [];
  60. var padlen = b64.length % 4;
  61. for (var i = 0, l = b64.length; i < l; i += 4){
  62. var n = ((b64tab[b64.charAt(i )] || 0) << 18)
  63. | ((b64tab[b64.charAt(i+1)] || 0) << 12)
  64. | ((b64tab[b64.charAt(i+2)] || 0) << 6)
  65. | ((b64tab[b64.charAt(i+3)] || 0));
  66. bin.push(
  67. ( n >> 16 ),
  68. ( (n >> 8) & 0xff ),
  69. ( n & 0xff )
  70. );
  71. }
  72. bin.length -= [0,0,2,1][padlen];
  73. return bin;
  74. };
  75. var convertUTF16ArrayToUTF8Array = function(uni){
  76. var bin = [];
  77. for (var i = 0, l = uni.length; i < l; i++){
  78. var n = uni[i];
  79. if (n < 0x80)
  80. bin.push(n);
  81. else if (n < 0x800)
  82. bin.push(
  83. 0xc0 | (n >>> 6),
  84. 0x80 | (n & 0x3f));
  85. else
  86. bin.push(
  87. 0xe0 | ((n >>> 12) & 0x0f),
  88. 0x80 | ((n >>> 6) & 0x3f),
  89. 0x80 | (n & 0x3f));
  90. }
  91. return bin;
  92. };
  93. var convertUTF8ArrayToUTF16Array = function(bin){
  94. var uni = [];
  95. for (var i = 0, l = bin.length; i < l; i++){
  96. var c0 = bin[i];
  97. if (c0 < 0x80){
  98. uni.push(c0);
  99. }else{
  100. var c1 = bin[++i];
  101. if (c0 < 0xe0){
  102. uni.push(((c0 & 0x1f) << 6) | (c1 & 0x3f));
  103. }else{
  104. var c2 = bin[++i];
  105. uni.push(
  106. ((c0 & 0x0f) << 12) | ((c1 & 0x3f) << 6) | (c2 & 0x3f)
  107. );
  108. }
  109. }
  110. }
  111. return uni;
  112. };
  113. var convertUTF8StringToBase64 = function(bin){
  114. return convertUTF8ArrayToBase64(stringToArray(bin));
  115. };
  116. var convertBase64ToUTF8String = function(b64){
  117. return chunkStringFromCharCodeApply(convertBase64ToUTF8Array(b64));
  118. };
  119. var convertUTF8StringToUTF16Array = function(bin){
  120. return convertUTF8ArrayToUTF16Array(stringToArray(bin));
  121. };
  122. var convertUTF8ArrayToUTF16String = function(bin){
  123. return chunkStringFromCharCodeApply(convertUTF8ArrayToUTF16Array(bin));
  124. };
  125. var convertUTF8StringToUTF16String = function(bin){
  126. return chunkStringFromCharCodeApply(
  127. convertUTF8ArrayToUTF16Array(stringToArray(bin))
  128. );
  129. };
  130. var convertUTF16StringToUTF8Array = function(uni){
  131. return convertUTF16ArrayToUTF8Array(stringToArray(uni));
  132. };
  133. var convertUTF16ArrayToUTF8String = function(uni){
  134. return chunkStringFromCharCodeApply(convertUTF16ArrayToUTF8Array(uni));
  135. };
  136. var convertUTF16StringToUTF8String = function(uni){
  137. return chunkStringFromCharCodeApply(
  138. convertUTF16ArrayToUTF8Array(stringToArray(uni))
  139. );
  140. };
  141. /*
  142. * String.fromCharCode.apply will only handle arrays as big as 65536,
  143. * after that it'll return a truncated string with no warning.
  144. */
  145. var chunkStringFromCharCodeApply = function(arr){
  146. var strs = [], i;
  147. for (i = 0; i < arr.length; i += 65536){
  148. strs.push(String.fromCharCode.apply(String, arr.slice(i, i+65536)));
  149. }
  150. return strs.join('');
  151. };
  152. if (global.btoa){
  153. var btoa = global.btoa;
  154. var convertUTF16StringToBase64 = function (uni){
  155. return btoa(convertUTF16StringToUTF8String(uni));
  156. };
  157. }
  158. else {
  159. var btoa = convertUTF8StringToBase64;
  160. var convertUTF16StringToBase64 = function (uni){
  161. return convertUTF8ArrayToBase64(convertUTF16StringToUTF8Array(uni));
  162. };
  163. }
  164. if (global.atob){
  165. var atob = global.atob;
  166. var convertBase64ToUTF16String = function (b64){
  167. return convertUTF8StringToUTF16String(atob(b64));
  168. };
  169. }
  170. else {
  171. var atob = convertBase64ToUTF8String;
  172. var convertBase64ToUTF16String = function (b64){
  173. return convertUTF8ArrayToUTF16String(convertBase64ToUTF8Array(b64));
  174. };
  175. }
  176. global.Base64 = {
  177. convertUTF8ArrayToBase64:convertUTF8ArrayToBase64,
  178. convertByteArrayToBase64:convertUTF8ArrayToBase64,
  179. convertBase64ToUTF8Array:convertBase64ToUTF8Array,
  180. convertBase64ToByteArray:convertBase64ToUTF8Array,
  181. convertUTF16ArrayToUTF8Array:convertUTF16ArrayToUTF8Array,
  182. convertUTF16ArrayToByteArray:convertUTF16ArrayToUTF8Array,
  183. convertUTF8ArrayToUTF16Array:convertUTF8ArrayToUTF16Array,
  184. convertByteArrayToUTF16Array:convertUTF8ArrayToUTF16Array,
  185. convertUTF8StringToBase64:convertUTF8StringToBase64,
  186. convertBase64ToUTF8String:convertBase64ToUTF8String,
  187. convertUTF8StringToUTF16Array:convertUTF8StringToUTF16Array,
  188. convertUTF8ArrayToUTF16String:convertUTF8ArrayToUTF16String,
  189. convertByteArrayToUTF16String:convertUTF8ArrayToUTF16String,
  190. convertUTF8StringToUTF16String:convertUTF8StringToUTF16String,
  191. convertUTF16StringToUTF8Array:convertUTF16StringToUTF8Array,
  192. convertUTF16StringToByteArray:convertUTF16StringToUTF8Array,
  193. convertUTF16ArrayToUTF8String:convertUTF16ArrayToUTF8String,
  194. convertUTF16StringToUTF8String:convertUTF16StringToUTF8String,
  195. convertUTF16StringToBase64:convertUTF16StringToBase64,
  196. convertBase64ToUTF16String:convertBase64ToUTF16String,
  197. fromBase64:convertBase64ToUTF8String,
  198. toBase64:convertUTF8StringToBase64,
  199. atob:atob,
  200. btoa:btoa,
  201. utob:convertUTF16StringToUTF8String,
  202. btou:convertUTF8StringToUTF16String,
  203. encode:convertUTF16StringToBase64,
  204. encodeURI:function(u){
  205. return convertUTF16StringToBase64(u).replace(/[+\/]/g, function(m0){
  206. return m0 == '+' ? '-' : '_';
  207. }).replace(/=+$/, '');
  208. },
  209. decode:function(a){
  210. return convertBase64ToUTF16String(a.replace(/[-_]/g, function(m0){
  211. return m0 == '-' ? '+' : '/';
  212. }));
  213. }
  214. };
  215. })(this);