UNPKG

1.83 kBJavaScriptView Raw
1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT license.
3let safeatob;
4// base64 character set, plus padding character (=)
5const b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
6// Regular expression to check formal correctness of base64 encoded strings
7const b64re = /^(?:[A-Za-z\d+/]{4})*?(?:[A-Za-z\d+/]{2}(?:==)?|[A-Za-z\d+/]{3}=?)?$/;
8if ("function" !== typeof atob) {
9 // atob implementation for React Native
10 safeatob = (str) => {
11 // atob can work with strings with whitespaces, even inside the encoded part,
12 // but only \t, \n, \f, \r and ' ', which can be stripped.
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 // Adding the padding if missing, for simplicity
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}
40else {
41 safeatob = atob;
42}
43export default safeatob;
44//# sourceMappingURL=atob.browser.js.map
\No newline at end of file