base64.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * base64.js
  3. *
  4. * Licensed under the BSD 3-Clause License.
  5. * http://opensource.org/licenses/BSD-3-Clause
  6. *
  7. * References:
  8. * http://en.wikipedia.org/wiki/Base64
  9. */
  10. ;(function (global, factory) {
  11. typeof exports === 'object' && typeof module !== 'undefined'
  12. ? module.exports = factory(global)
  13. : typeof define === 'function' && define.amd
  14. ? define(factory) : factory(global)
  15. }((
  16. typeof self !== 'undefined' ? self
  17. : typeof window !== 'undefined' ? window
  18. : typeof global !== 'undefined' ? global
  19. : this
  20. ), function(global) {
  21. 'use strict';
  22. // existing version for noConflict()
  23. global = global || {};
  24. var _Base64 = global.Base64;
  25. var version = "2.6.4";
  26. // constants
  27. var b64chars
  28. = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
  29. var b64tab = function(bin) {
  30. var t = {};
  31. for (var i = 0, l = bin.length; i < l; i++) t[bin.charAt(i)] = i;
  32. return t;
  33. }(b64chars);
  34. var fromCharCode = String.fromCharCode;
  35. // encoder stuff
  36. var cb_utob = function(c) {
  37. if (c.length < 2) {
  38. var cc = c.charCodeAt(0);
  39. return cc < 0x80 ? c
  40. : cc < 0x800 ? (fromCharCode(0xc0 | (cc >>> 6))
  41. + fromCharCode(0x80 | (cc & 0x3f)))
  42. : (fromCharCode(0xe0 | ((cc >>> 12) & 0x0f))
  43. + fromCharCode(0x80 | ((cc >>> 6) & 0x3f))
  44. + fromCharCode(0x80 | ( cc & 0x3f)));
  45. } else {
  46. var cc = 0x10000
  47. + (c.charCodeAt(0) - 0xD800) * 0x400
  48. + (c.charCodeAt(1) - 0xDC00);
  49. return (fromCharCode(0xf0 | ((cc >>> 18) & 0x07))
  50. + fromCharCode(0x80 | ((cc >>> 12) & 0x3f))
  51. + fromCharCode(0x80 | ((cc >>> 6) & 0x3f))
  52. + fromCharCode(0x80 | ( cc & 0x3f)));
  53. }
  54. };
  55. var re_utob = /[\uD800-\uDBFF][\uDC00-\uDFFFF]|[^\x00-\x7F]/g;
  56. var utob = function(u) {
  57. return u.replace(re_utob, cb_utob);
  58. };
  59. var cb_encode = function(ccc) {
  60. var padlen = [0, 2, 1][ccc.length % 3],
  61. ord = ccc.charCodeAt(0) << 16
  62. | ((ccc.length > 1 ? ccc.charCodeAt(1) : 0) << 8)
  63. | ((ccc.length > 2 ? ccc.charCodeAt(2) : 0)),
  64. chars = [
  65. b64chars.charAt( ord >>> 18),
  66. b64chars.charAt((ord >>> 12) & 63),
  67. padlen >= 2 ? '=' : b64chars.charAt((ord >>> 6) & 63),
  68. padlen >= 1 ? '=' : b64chars.charAt(ord & 63)
  69. ];
  70. return chars.join('');
  71. };
  72. var btoa = global.btoa && typeof global.btoa == 'function'
  73. ? function(b){ return global.btoa(b) } : function(b) {
  74. if (b.match(/[^\x00-\xFF]/)) throw new RangeError(
  75. 'The string contains invalid characters.'
  76. );
  77. return b.replace(/[\s\S]{1,3}/g, cb_encode);
  78. };
  79. var _encode = function(u) {
  80. return btoa(utob(String(u)));
  81. };
  82. var mkUriSafe = function (b64) {
  83. return b64.replace(/[+\/]/g, function(m0) {
  84. return m0 == '+' ? '-' : '_';
  85. }).replace(/=/g, '');
  86. };
  87. var encode = function(u, urisafe) {
  88. return urisafe ? mkUriSafe(_encode(u)) : _encode(u);
  89. };
  90. var encodeURI = function(u) { return encode(u, true) };
  91. var fromUint8Array;
  92. if (global.Uint8Array) fromUint8Array = function(a, urisafe) {
  93. // return btoa(fromCharCode.apply(null, a));
  94. var b64 = '';
  95. for (var i = 0, l = a.length; i < l; i += 3) {
  96. var a0 = a[i], a1 = a[i+1], a2 = a[i+2];
  97. var ord = a0 << 16 | a1 << 8 | a2;
  98. b64 += b64chars.charAt( ord >>> 18)
  99. + b64chars.charAt((ord >>> 12) & 63)
  100. + ( typeof a1 != 'undefined'
  101. ? b64chars.charAt((ord >>> 6) & 63) : '=')
  102. + ( typeof a2 != 'undefined'
  103. ? b64chars.charAt( ord & 63) : '=');
  104. }
  105. return urisafe ? mkUriSafe(b64) : b64;
  106. };
  107. // decoder stuff
  108. var re_btou = /[\xC0-\xDF][\x80-\xBF]|[\xE0-\xEF][\x80-\xBF]{2}|[\xF0-\xF7][\x80-\xBF]{3}/g;
  109. var cb_btou = function(cccc) {
  110. switch(cccc.length) {
  111. case 4:
  112. var cp = ((0x07 & cccc.charCodeAt(0)) << 18)
  113. | ((0x3f & cccc.charCodeAt(1)) << 12)
  114. | ((0x3f & cccc.charCodeAt(2)) << 6)
  115. | (0x3f & cccc.charCodeAt(3)),
  116. offset = cp - 0x10000;
  117. return (fromCharCode((offset >>> 10) + 0xD800)
  118. + fromCharCode((offset & 0x3FF) + 0xDC00));
  119. case 3:
  120. return fromCharCode(
  121. ((0x0f & cccc.charCodeAt(0)) << 12)
  122. | ((0x3f & cccc.charCodeAt(1)) << 6)
  123. | (0x3f & cccc.charCodeAt(2))
  124. );
  125. default:
  126. return fromCharCode(
  127. ((0x1f & cccc.charCodeAt(0)) << 6)
  128. | (0x3f & cccc.charCodeAt(1))
  129. );
  130. }
  131. };
  132. var btou = function(b) {
  133. return b.replace(re_btou, cb_btou);
  134. };
  135. var cb_decode = function(cccc) {
  136. var len = cccc.length,
  137. padlen = len % 4,
  138. n = (len > 0 ? b64tab[cccc.charAt(0)] << 18 : 0)
  139. | (len > 1 ? b64tab[cccc.charAt(1)] << 12 : 0)
  140. | (len > 2 ? b64tab[cccc.charAt(2)] << 6 : 0)
  141. | (len > 3 ? b64tab[cccc.charAt(3)] : 0),
  142. chars = [
  143. fromCharCode( n >>> 16),
  144. fromCharCode((n >>> 8) & 0xff),
  145. fromCharCode( n & 0xff)
  146. ];
  147. chars.length -= [0, 0, 2, 1][padlen];
  148. return chars.join('');
  149. };
  150. var _atob = global.atob && typeof global.atob == 'function'
  151. ? function(a){ return global.atob(a) } : function(a){
  152. return a.replace(/\S{1,4}/g, cb_decode);
  153. };
  154. var atob = function(a) {
  155. return _atob(String(a).replace(/[^A-Za-z0-9\+\/]/g, ''));
  156. };
  157. var _decode = function(a) { return btou(_atob(a)) };
  158. var _fromURI = function(a) {
  159. return String(a).replace(/[-_]/g, function(m0) {
  160. return m0 == '-' ? '+' : '/'
  161. }).replace(/[^A-Za-z0-9\+\/]/g, '');
  162. };
  163. var decode = function(a){
  164. return _decode(_fromURI(a));
  165. };
  166. var toUint8Array;
  167. if (global.Uint8Array) toUint8Array = function(a) {
  168. return Uint8Array.from(atob(_fromURI(a)), function(c) {
  169. return c.charCodeAt(0);
  170. });
  171. };
  172. var noConflict = function() {
  173. var Base64 = global.Base64;
  174. global.Base64 = _Base64;
  175. return Base64;
  176. };
  177. // export Base64
  178. global.Base64 = {
  179. VERSION: version,
  180. atob: atob,
  181. btoa: btoa,
  182. fromBase64: decode,
  183. toBase64: encode,
  184. utob: utob,
  185. encode: encode,
  186. encodeURI: encodeURI,
  187. btou: btou,
  188. decode: decode,
  189. noConflict: noConflict,
  190. fromUint8Array: fromUint8Array,
  191. toUint8Array: toUint8Array
  192. };
  193. // if ES5 is available, make Base64.extendString() available
  194. if (typeof Object.defineProperty === 'function') {
  195. var noEnum = function(v){
  196. return {value:v,enumerable:false,writable:true,configurable:true};
  197. };
  198. global.Base64.extendString = function () {
  199. Object.defineProperty(
  200. String.prototype, 'fromBase64', noEnum(function () {
  201. return decode(this)
  202. }));
  203. Object.defineProperty(
  204. String.prototype, 'toBase64', noEnum(function (urisafe) {
  205. return encode(this, urisafe)
  206. }));
  207. Object.defineProperty(
  208. String.prototype, 'toBase64URI', noEnum(function () {
  209. return encode(this, true)
  210. }));
  211. };
  212. }
  213. //
  214. // export Base64 to the namespace
  215. //
  216. if (global['Meteor']) { // Meteor.js
  217. Base64 = global.Base64;
  218. }
  219. // module.exports and AMD are mutually exclusive.
  220. // module.exports has precedence.
  221. if (typeof module !== 'undefined' && module.exports) {
  222. module.exports.Base64 = global.Base64;
  223. }
  224. else if (typeof define === 'function' && define.amd) {
  225. // AMD. Register as an anonymous module.
  226. define([], function(){ return global.Base64 });
  227. }
  228. // that's it!
  229. return {Base64: global.Base64}
  230. }));