1 |
|
2 |
|
3 | let safeatob;
|
4 |
|
5 | const b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
|
6 |
|
7 | const b64re = /^(?:[A-Za-z\d+/]{4})*?(?:[A-Za-z\d+/]{2}(?:==)?|[A-Za-z\d+/]{3}=?)?$/;
|
8 | if ("function" !== typeof atob) {
|
9 |
|
10 | safeatob = (str) => {
|
11 |
|
12 |
|
13 | str = String(str).replace(/[\t\n\f\r ]+/g, "");
|
14 | if (!b64re.test(str)) {
|
15 | throw new TypeError("Failed to execute 'atob' on 'Window': The string to be decoded is not correctly encoded.");
|
16 | }
|
17 |
|
18 | str += "==".slice(2 - (str.length & 3));
|
19 | let bitmap;
|
20 | let result = "";
|
21 | let r1;
|
22 | let r2;
|
23 | let i = 0;
|
24 | for (; i < str.length;) {
|
25 | bitmap =
|
26 | (b64.indexOf(str.charAt(i++)) << 18) |
|
27 | (b64.indexOf(str.charAt(i++)) << 12) |
|
28 | ((r1 = b64.indexOf(str.charAt(i++))) << 6) |
|
29 | (r2 = b64.indexOf(str.charAt(i++)));
|
30 | result +=
|
31 | r1 === 64
|
32 | ? String.fromCharCode((bitmap >> 16) & 255)
|
33 | : r2 === 64
|
34 | ? String.fromCharCode((bitmap >> 16) & 255, (bitmap >> 8) & 255)
|
35 | : String.fromCharCode((bitmap >> 16) & 255, (bitmap >> 8) & 255, bitmap & 255);
|
36 | }
|
37 | return result;
|
38 | };
|
39 | }
|
40 | else {
|
41 | safeatob = atob;
|
42 | }
|
43 | export default safeatob;
|
44 |
|
\ | No newline at end of file |