base64Util.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. const C = "3.9.4", z = C, p = typeof TextDecoder == "function" ? new TextDecoder("utf-8", { ignoreBOM: !0 }) : void 0, b = typeof TextEncoder == "function" ? new TextEncoder() : void 0, P = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=", f = Array.prototype.slice.call(P), c = ((t) => {
  2. let e = {};
  3. return t.forEach((r, n) => e[r] = n), e;
  4. })(f), Z = /^(?:[A-Za-z\d+\/]{4})*?(?:[A-Za-z\d+\/]{2}(?:==)?|[A-Za-z\d+\/]{3}=?)?$/, o = String.fromCharCode.bind(String), O = typeof Uint8Array.from == "function" ? Uint8Array.from.bind(Uint8Array) : (t) => new Uint8Array(Array.prototype.slice.call(t, 0)), B = (t) => t.replace(/=/g, "").replace(/[+\/]/g, (e) => e == "+" ? "-" : "_"), _ = (t) => t.replace(/[^A-Za-z0-9\+\/]/g, ""), m = (t) => {
  5. let e, r, n, a, s = "";
  6. const l = t.length % 3;
  7. for (let u = 0; u < t.length; ) {
  8. if ((r = t.charCodeAt(u++)) > 255 || (n = t.charCodeAt(u++)) > 255 || (a = t.charCodeAt(u++)) > 255)
  9. throw new TypeError("invalid character found");
  10. e = r << 16 | n << 8 | a, s += f[e >> 18 & 63] + f[e >> 12 & 63] + f[e >> 6 & 63] + f[e & 63];
  11. }
  12. return l ? s.slice(0, l - 3) + "===".substring(l) : s;
  13. }, A = typeof btoa == "function" ? (t) => btoa(t) : m, x = typeof Uint8Array.prototype.toBase64 == "function" ? (t) => t.toBase64() : (t) => {
  14. let r = [];
  15. for (let n = 0, a = t.length; n < a; n += 4096)
  16. r.push(o.apply(null, t.subarray(n, n + 4096)));
  17. return A(r.join(""));
  18. }, d = (t, e = !1) => e ? B(x(t)) : x(t), j = (t) => {
  19. if (t.length < 2) {
  20. var e = t.charCodeAt(0);
  21. return e < 128 ? t : e < 2048 ? o(192 | e >>> 6) + o(128 | e & 63) : o(224 | e >>> 12 & 15) + o(128 | e >>> 6 & 63) + o(128 | e & 63);
  22. } else {
  23. var e = 65536 + (t.charCodeAt(0) - 55296) * 1024 + (t.charCodeAt(1) - 56320);
  24. return o(240 | e >>> 18 & 7) + o(128 | e >>> 12 & 63) + o(128 | e >>> 6 & 63) + o(128 | e & 63);
  25. }
  26. }, V = /[\uD800-\uDBFF][\uDC00-\uDFFF]|[^\x00-\x7F]/g, F = (t) => t.replace(V, j), g = b ? (t) => x(b.encode(t)) : (t) => A(F(t)), i = (t, e = !1) => e ? B(g(t)) : g(t), U = (t) => i(t, !0), L = /[\xC0-\xDF][\x80-\xBF]|[\xE0-\xEF][\x80-\xBF]{2}|[\xF0-\xF7][\x80-\xBF]{3}/g, N = (t) => {
  27. switch (t.length) {
  28. case 4:
  29. var e = (7 & t.charCodeAt(0)) << 18 | (63 & t.charCodeAt(1)) << 12 | (63 & t.charCodeAt(2)) << 6 | 63 & t.charCodeAt(3), r = e - 65536;
  30. return o((r >>> 10) + 55296) + o((r & 1023) + 56320);
  31. case 3:
  32. return o((15 & t.charCodeAt(0)) << 12 | (63 & t.charCodeAt(1)) << 6 | 63 & t.charCodeAt(2));
  33. default:
  34. return o((31 & t.charCodeAt(0)) << 6 | 63 & t.charCodeAt(1));
  35. }
  36. }, D = (t) => t.replace(L, N), E = (t) => {
  37. if (t = t.replace(/\s+/g, ""), !Z.test(t))
  38. throw new TypeError("malformed base64.");
  39. t += "==".slice(2 - (t.length & 3));
  40. let e, r, n, a = [];
  41. for (let s = 0; s < t.length; )
  42. e = c[t.charAt(s++)] << 18 | c[t.charAt(s++)] << 12 | (r = c[t.charAt(s++)]) << 6 | (n = c[t.charAt(s++)]), r === 64 ? a.push(o(e >> 16 & 255)) : n === 64 ? a.push(o(e >> 16 & 255, e >> 8 & 255)) : a.push(o(e >> 16 & 255, e >> 8 & 255, e & 255));
  43. return a.join("");
  44. }, y = typeof atob == "function" ? (t) => atob(_(t)) : E, R = typeof Uint8Array.fromBase64 == "function" ? (t) => Uint8Array.fromBase64(t) : (t) => O(y(t).split("").map((e) => e.charCodeAt(0))), w = (t) => R(S(t)), $ = p ? (t) => p.decode(R(t)) : (t) => D(y(t)), S = (t) => _(t.replace(/[-_]/g, (e) => e == "-" ? "+" : "/")), h = (t) => $(S(t)), k = (t) => {
  45. if (typeof t != "string")
  46. return !1;
  47. const e = t.replace(/\s+/g, "").replace(/={0,2}$/, "");
  48. return !/[^\s0-9a-zA-Z\+/]/.test(e) || !/[^\s0-9a-zA-Z\-_]/.test(e);
  49. }, T = (t) => ({
  50. value: t,
  51. enumerable: !1,
  52. writable: !0,
  53. configurable: !0
  54. }), v = function() {
  55. const t = (e, r) => Object.defineProperty(String.prototype, e, T(r));
  56. t("fromBase64", function() {
  57. return h(this);
  58. }), t("toBase64", function(e) {
  59. return i(this, e);
  60. }), t("toBase64URI", function() {
  61. return i(this, !0);
  62. }), t("toBase64URL", function() {
  63. return i(this, !0);
  64. }), t("toUint8Array", function() {
  65. return w(this);
  66. });
  67. }, I = function() {
  68. const t = (e, r) => Object.defineProperty(Uint8Array.prototype, e, T(r));
  69. t("toBase64", function(e) {
  70. return d(this, e);
  71. }), t("toBase64URI", function() {
  72. return d(this, !0);
  73. }), t("toBase64URL", function() {
  74. return d(this, !0);
  75. });
  76. }, M = () => {
  77. v(), I();
  78. }, q = {
  79. version: C,
  80. VERSION: z,
  81. atob: y,
  82. atobPolyfill: E,
  83. btoa: A,
  84. btoaPolyfill: m,
  85. fromBase64: h,
  86. toBase64: i,
  87. encode: i,
  88. encodeURI: U,
  89. encodeURL: U,
  90. utob: F,
  91. btou: D,
  92. decode: h,
  93. isValid: k,
  94. fromUint8Array: d,
  95. toUint8Array: w,
  96. extendString: v,
  97. extendUint8Array: I,
  98. extendBuiltins: M
  99. };
  100. function G(t = "") {
  101. return t == null ? "" : q.encode(String(t));
  102. }
  103. export {
  104. G as e
  105. };