base64.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. //
  2. // THIS FILE IS AUTOMATICALLY GENERATED! DO NOT EDIT BY HAND!
  3. //
  4. ;
  5. (function (global, factory) {
  6. typeof exports === 'object' && typeof module !== 'undefined'
  7. ? module.exports = factory()
  8. : typeof define === 'function' && define.amd
  9. ? define(factory) :
  10. // cf. https://github.com/dankogai/js-base64/issues/119
  11. (function () {
  12. // existing version for noConflict()
  13. var _Base64 = global.Base64;
  14. var gBase64 = factory();
  15. gBase64.noConflict = function () {
  16. global.Base64 = _Base64;
  17. return gBase64;
  18. };
  19. if (global.Meteor) { // Meteor.js
  20. Base64 = gBase64;
  21. }
  22. global.Base64 = gBase64;
  23. })();
  24. }((typeof self !== 'undefined' ? self
  25. : typeof window !== 'undefined' ? window
  26. : typeof global !== 'undefined' ? global
  27. : this), function () {
  28. 'use strict';
  29. /**
  30. * base64.ts
  31. *
  32. * Licensed under the BSD 3-Clause License.
  33. * http://opensource.org/licenses/BSD-3-Clause
  34. *
  35. * References:
  36. * http://en.wikipedia.org/wiki/Base64
  37. *
  38. * @author Dan Kogai (https://github.com/dankogai)
  39. */
  40. var version = '3.7.5';
  41. /**
  42. * @deprecated use lowercase `version`.
  43. */
  44. var VERSION = version;
  45. var _hasatob = typeof atob === 'function';
  46. var _hasbtoa = typeof btoa === 'function';
  47. var _hasBuffer = typeof Buffer === 'function';
  48. var _TD = typeof TextDecoder === 'function' ? new TextDecoder() : undefined;
  49. var _TE = typeof TextEncoder === 'function' ? new TextEncoder() : undefined;
  50. var b64ch = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
  51. var b64chs = Array.prototype.slice.call(b64ch);
  52. var b64tab = (function (a) {
  53. var tab = {};
  54. a.forEach(function (c, i) { return tab[c] = i; });
  55. return tab;
  56. })(b64chs);
  57. var b64re = /^(?:[A-Za-z\d+\/]{4})*?(?:[A-Za-z\d+\/]{2}(?:==)?|[A-Za-z\d+\/]{3}=?)?$/;
  58. var _fromCC = String.fromCharCode.bind(String);
  59. var _U8Afrom = typeof Uint8Array.from === 'function'
  60. ? Uint8Array.from.bind(Uint8Array)
  61. : function (it) { return new Uint8Array(Array.prototype.slice.call(it, 0)); };
  62. var _mkUriSafe = function (src) { return src
  63. .replace(/=/g, '').replace(/[+\/]/g, function (m0) { return m0 == '+' ? '-' : '_'; }); };
  64. var _tidyB64 = function (s) { return s.replace(/[^A-Za-z0-9\+\/]/g, ''); };
  65. /**
  66. * polyfill version of `btoa`
  67. */
  68. var btoaPolyfill = function (bin) {
  69. // console.log('polyfilled');
  70. var u32, c0, c1, c2, asc = '';
  71. var pad = bin.length % 3;
  72. for (var i = 0; i < bin.length;) {
  73. if ((c0 = bin.charCodeAt(i++)) > 255 ||
  74. (c1 = bin.charCodeAt(i++)) > 255 ||
  75. (c2 = bin.charCodeAt(i++)) > 255)
  76. throw new TypeError('invalid character found');
  77. u32 = (c0 << 16) | (c1 << 8) | c2;
  78. asc += b64chs[u32 >> 18 & 63]
  79. + b64chs[u32 >> 12 & 63]
  80. + b64chs[u32 >> 6 & 63]
  81. + b64chs[u32 & 63];
  82. }
  83. return pad ? asc.slice(0, pad - 3) + "===".substring(pad) : asc;
  84. };
  85. /**
  86. * does what `window.btoa` of web browsers do.
  87. * @param {String} bin binary string
  88. * @returns {string} Base64-encoded string
  89. */
  90. var _btoa = _hasbtoa ? function (bin) { return btoa(bin); }
  91. : _hasBuffer ? function (bin) { return Buffer.from(bin, 'binary').toString('base64'); }
  92. : btoaPolyfill;
  93. var _fromUint8Array = _hasBuffer
  94. ? function (u8a) { return Buffer.from(u8a).toString('base64'); }
  95. : function (u8a) {
  96. // cf. https://stackoverflow.com/questions/12710001/how-to-convert-uint8-array-to-base64-encoded-string/12713326#12713326
  97. var maxargs = 0x1000;
  98. var strs = [];
  99. for (var i = 0, l = u8a.length; i < l; i += maxargs) {
  100. strs.push(_fromCC.apply(null, u8a.subarray(i, i + maxargs)));
  101. }
  102. return _btoa(strs.join(''));
  103. };
  104. /**
  105. * converts a Uint8Array to a Base64 string.
  106. * @param {boolean} [urlsafe] URL-and-filename-safe a la RFC4648 §5
  107. * @returns {string} Base64 string
  108. */
  109. var fromUint8Array = function (u8a, urlsafe) {
  110. if (urlsafe === void 0) { urlsafe = false; }
  111. return urlsafe ? _mkUriSafe(_fromUint8Array(u8a)) : _fromUint8Array(u8a);
  112. };
  113. // This trick is found broken https://github.com/dankogai/js-base64/issues/130
  114. // const utob = (src: string) => unescape(encodeURIComponent(src));
  115. // reverting good old fationed regexp
  116. var cb_utob = function (c) {
  117. if (c.length < 2) {
  118. var cc = c.charCodeAt(0);
  119. return cc < 0x80 ? c
  120. : cc < 0x800 ? (_fromCC(0xc0 | (cc >>> 6))
  121. + _fromCC(0x80 | (cc & 0x3f)))
  122. : (_fromCC(0xe0 | ((cc >>> 12) & 0x0f))
  123. + _fromCC(0x80 | ((cc >>> 6) & 0x3f))
  124. + _fromCC(0x80 | (cc & 0x3f)));
  125. }
  126. else {
  127. var cc = 0x10000
  128. + (c.charCodeAt(0) - 0xD800) * 0x400
  129. + (c.charCodeAt(1) - 0xDC00);
  130. return (_fromCC(0xf0 | ((cc >>> 18) & 0x07))
  131. + _fromCC(0x80 | ((cc >>> 12) & 0x3f))
  132. + _fromCC(0x80 | ((cc >>> 6) & 0x3f))
  133. + _fromCC(0x80 | (cc & 0x3f)));
  134. }
  135. };
  136. var re_utob = /[\uD800-\uDBFF][\uDC00-\uDFFFF]|[^\x00-\x7F]/g;
  137. /**
  138. * @deprecated should have been internal use only.
  139. * @param {string} src UTF-8 string
  140. * @returns {string} UTF-16 string
  141. */
  142. var utob = function (u) { return u.replace(re_utob, cb_utob); };
  143. //
  144. var _encode = _hasBuffer
  145. ? function (s) { return Buffer.from(s, 'utf8').toString('base64'); }
  146. : _TE
  147. ? function (s) { return _fromUint8Array(_TE.encode(s)); }
  148. : function (s) { return _btoa(utob(s)); };
  149. /**
  150. * converts a UTF-8-encoded string to a Base64 string.
  151. * @param {boolean} [urlsafe] if `true` make the result URL-safe
  152. * @returns {string} Base64 string
  153. */
  154. var encode = function (src, urlsafe) {
  155. if (urlsafe === void 0) { urlsafe = false; }
  156. return urlsafe
  157. ? _mkUriSafe(_encode(src))
  158. : _encode(src);
  159. };
  160. /**
  161. * converts a UTF-8-encoded string to URL-safe Base64 RFC4648 §5.
  162. * @returns {string} Base64 string
  163. */
  164. var encodeURI = function (src) { return encode(src, true); };
  165. // This trick is found broken https://github.com/dankogai/js-base64/issues/130
  166. // const btou = (src: string) => decodeURIComponent(escape(src));
  167. // reverting good old fationed regexp
  168. var re_btou = /[\xC0-\xDF][\x80-\xBF]|[\xE0-\xEF][\x80-\xBF]{2}|[\xF0-\xF7][\x80-\xBF]{3}/g;
  169. var cb_btou = function (cccc) {
  170. switch (cccc.length) {
  171. case 4:
  172. var cp = ((0x07 & cccc.charCodeAt(0)) << 18)
  173. | ((0x3f & cccc.charCodeAt(1)) << 12)
  174. | ((0x3f & cccc.charCodeAt(2)) << 6)
  175. | (0x3f & cccc.charCodeAt(3)), offset = cp - 0x10000;
  176. return (_fromCC((offset >>> 10) + 0xD800)
  177. + _fromCC((offset & 0x3FF) + 0xDC00));
  178. case 3:
  179. return _fromCC(((0x0f & cccc.charCodeAt(0)) << 12)
  180. | ((0x3f & cccc.charCodeAt(1)) << 6)
  181. | (0x3f & cccc.charCodeAt(2)));
  182. default:
  183. return _fromCC(((0x1f & cccc.charCodeAt(0)) << 6)
  184. | (0x3f & cccc.charCodeAt(1)));
  185. }
  186. };
  187. /**
  188. * @deprecated should have been internal use only.
  189. * @param {string} src UTF-16 string
  190. * @returns {string} UTF-8 string
  191. */
  192. var btou = function (b) { return b.replace(re_btou, cb_btou); };
  193. /**
  194. * polyfill version of `atob`
  195. */
  196. var atobPolyfill = function (asc) {
  197. // console.log('polyfilled');
  198. asc = asc.replace(/\s+/g, '');
  199. if (!b64re.test(asc))
  200. throw new TypeError('malformed base64.');
  201. asc += '=='.slice(2 - (asc.length & 3));
  202. var u24, bin = '', r1, r2;
  203. for (var i = 0; i < asc.length;) {
  204. u24 = b64tab[asc.charAt(i++)] << 18
  205. | b64tab[asc.charAt(i++)] << 12
  206. | (r1 = b64tab[asc.charAt(i++)]) << 6
  207. | (r2 = b64tab[asc.charAt(i++)]);
  208. bin += r1 === 64 ? _fromCC(u24 >> 16 & 255)
  209. : r2 === 64 ? _fromCC(u24 >> 16 & 255, u24 >> 8 & 255)
  210. : _fromCC(u24 >> 16 & 255, u24 >> 8 & 255, u24 & 255);
  211. }
  212. return bin;
  213. };
  214. /**
  215. * does what `window.atob` of web browsers do.
  216. * @param {String} asc Base64-encoded string
  217. * @returns {string} binary string
  218. */
  219. var _atob = _hasatob ? function (asc) { return atob(_tidyB64(asc)); }
  220. : _hasBuffer ? function (asc) { return Buffer.from(asc, 'base64').toString('binary'); }
  221. : atobPolyfill;
  222. //
  223. var _toUint8Array = _hasBuffer
  224. ? function (a) { return _U8Afrom(Buffer.from(a, 'base64')); }
  225. : function (a) { return _U8Afrom(_atob(a).split('').map(function (c) { return c.charCodeAt(0); })); };
  226. /**
  227. * converts a Base64 string to a Uint8Array.
  228. */
  229. var toUint8Array = function (a) { return _toUint8Array(_unURI(a)); };
  230. //
  231. var _decode = _hasBuffer
  232. ? function (a) { return Buffer.from(a, 'base64').toString('utf8'); }
  233. : _TD
  234. ? function (a) { return _TD.decode(_toUint8Array(a)); }
  235. : function (a) { return btou(_atob(a)); };
  236. var _unURI = function (a) { return _tidyB64(a.replace(/[-_]/g, function (m0) { return m0 == '-' ? '+' : '/'; })); };
  237. /**
  238. * converts a Base64 string to a UTF-8 string.
  239. * @param {String} src Base64 string. Both normal and URL-safe are supported
  240. * @returns {string} UTF-8 string
  241. */
  242. var decode = function (src) { return _decode(_unURI(src)); };
  243. /**
  244. * check if a value is a valid Base64 string
  245. * @param {String} src a value to check
  246. */
  247. var isValid = function (src) {
  248. if (typeof src !== 'string')
  249. return false;
  250. var s = src.replace(/\s+/g, '').replace(/={0,2}$/, '');
  251. return !/[^\s0-9a-zA-Z\+/]/.test(s) || !/[^\s0-9a-zA-Z\-_]/.test(s);
  252. };
  253. //
  254. var _noEnum = function (v) {
  255. return {
  256. value: v, enumerable: false, writable: true, configurable: true
  257. };
  258. };
  259. /**
  260. * extend String.prototype with relevant methods
  261. */
  262. var extendString = function () {
  263. var _add = function (name, body) { return Object.defineProperty(String.prototype, name, _noEnum(body)); };
  264. _add('fromBase64', function () { return decode(this); });
  265. _add('toBase64', function (urlsafe) { return encode(this, urlsafe); });
  266. _add('toBase64URI', function () { return encode(this, true); });
  267. _add('toBase64URL', function () { return encode(this, true); });
  268. _add('toUint8Array', function () { return toUint8Array(this); });
  269. };
  270. /**
  271. * extend Uint8Array.prototype with relevant methods
  272. */
  273. var extendUint8Array = function () {
  274. var _add = function (name, body) { return Object.defineProperty(Uint8Array.prototype, name, _noEnum(body)); };
  275. _add('toBase64', function (urlsafe) { return fromUint8Array(this, urlsafe); });
  276. _add('toBase64URI', function () { return fromUint8Array(this, true); });
  277. _add('toBase64URL', function () { return fromUint8Array(this, true); });
  278. };
  279. /**
  280. * extend Builtin prototypes with relevant methods
  281. */
  282. var extendBuiltins = function () {
  283. extendString();
  284. extendUint8Array();
  285. };
  286. var gBase64 = {
  287. version: version,
  288. VERSION: VERSION,
  289. atob: _atob,
  290. atobPolyfill: atobPolyfill,
  291. btoa: _btoa,
  292. btoaPolyfill: btoaPolyfill,
  293. fromBase64: decode,
  294. toBase64: encode,
  295. encode: encode,
  296. encodeURI: encodeURI,
  297. encodeURL: encodeURI,
  298. utob: utob,
  299. btou: btou,
  300. decode: decode,
  301. isValid: isValid,
  302. fromUint8Array: fromUint8Array,
  303. toUint8Array: toUint8Array,
  304. extendString: extendString,
  305. extendUint8Array: extendUint8Array,
  306. extendBuiltins: extendBuiltins
  307. };
  308. //
  309. // export Base64 to the namespace
  310. //
  311. // ES5 is yet to have Object.assign() that may make transpilers unhappy.
  312. // gBase64.Base64 = Object.assign({}, gBase64);
  313. gBase64.Base64 = {};
  314. Object.keys(gBase64).forEach(function (k) { return gBase64.Base64[k] = gBase64[k]; });
  315. return gBase64;
  316. }));