-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbase64.js
60 lines (54 loc) · 1.25 KB
/
base64.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/// Code can be found at: https://gist.github.com/1284012
(function() {
var a64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/', a256 = {
indexOf : function(c) {
return c.charCodeAt(0);
},
charAt : String.fromCharCode
};
function code(s, discard, alpha, beta, w1, w2) {
//console.log(s)
s = String(s);
//console.log(s)
var b = 0, x = '', i, c, bs = 1, sb = 1, length = s.length, tmp;
for (i = 0; i < length || (!discard && sb > 1); i += 1) {
b *= w1;
bs *= w1;
if (i < length) {
c = alpha.indexOf(s.charAt(i));
if (c <= -1 || c >= w1) {
throw new RangeError();
}
sb *= w1;
b += c;
}
while (bs >= w2) {
bs /= w2;
if (sb > 1) {
tmp = b;
b %= bs;
x += beta.charAt((tmp - b) / bs);
sb /= w2;
}
}
}
return x;
}
if (!("btoa" in window))
window.btoa = function(s) {
s = code(s, false, a256, a64, 256, 64);
return s + '===='.slice((s.length % 4) || 4);
};
if (!("atob" in window))
window.atob = function(s) {
var i;
s = String(s).split('=');
for (i = s.length - 1; i >= 0; i -= 1) {
if (s[i].length % 4 === 1) {
throw new RangeError();
}
s[i] = code(s[i], true, a64, a256, 64, 256);
}
return s.join('');
};
})();