Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80542 views
1
;(function () {
2
3
var object = typeof exports != 'undefined' ? exports : this; // #8: web workers
4
var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
5
6
function InvalidCharacterError(message) {
7
this.message = message;
8
}
9
InvalidCharacterError.prototype = new Error;
10
InvalidCharacterError.prototype.name = 'InvalidCharacterError';
11
12
// encoder
13
// [https://gist.github.com/999166] by [https://github.com/nignag]
14
object.btoa || (
15
object.btoa = function (input) {
16
for (
17
// initialize result and counter
18
var block, charCode, idx = 0, map = chars, output = '';
19
// if the next input index does not exist:
20
// change the mapping table to "="
21
// check if d has no fractional digits
22
input.charAt(idx | 0) || (map = '=', idx % 1);
23
// "8 - idx % 1 * 8" generates the sequence 2, 4, 6, 8
24
output += map.charAt(63 & block >> 8 - idx % 1 * 8)
25
) {
26
charCode = input.charCodeAt(idx += 3/4);
27
if (charCode > 0xFF) {
28
throw new InvalidCharacterError("'btoa' failed: The string to be encoded contains characters outside of the Latin1 range.");
29
}
30
block = block << 8 | charCode;
31
}
32
return output;
33
});
34
35
// decoder
36
// [https://gist.github.com/1020396] by [https://github.com/atk]
37
object.atob || (
38
object.atob = function (input) {
39
input = input.replace(/=+$/, '');
40
if (input.length % 4 == 1) {
41
throw new InvalidCharacterError("'atob' failed: The string to be decoded is not correctly encoded.");
42
}
43
for (
44
// initialize result and counters
45
var bc = 0, bs, buffer, idx = 0, output = '';
46
// get next character
47
buffer = input.charAt(idx++);
48
// character found in table? initialize bit storage and add its ascii value;
49
~buffer && (bs = bc % 4 ? bs * 64 + buffer : buffer,
50
// and if not first of each 4 characters,
51
// convert the first 8 bits to one ascii character
52
bc++ % 4) ? output += String.fromCharCode(255 & bs >> (-2 * bc & 6)) : 0
53
) {
54
// try to find character in table (0-63, not found => -1)
55
buffer = chars.indexOf(buffer);
56
}
57
return output;
58
});
59
60
}());
61
62