base64.js 6.7 KB

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