Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80744 views
1
var lookup = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
2
3
;(function (exports) {
4
'use strict';
5
6
var Arr = (typeof Uint8Array !== 'undefined')
7
? Uint8Array
8
: Array
9
10
var PLUS = '+'.charCodeAt(0)
11
var SLASH = '/'.charCodeAt(0)
12
var NUMBER = '0'.charCodeAt(0)
13
var LOWER = 'a'.charCodeAt(0)
14
var UPPER = 'A'.charCodeAt(0)
15
16
function decode (elt) {
17
var code = elt.charCodeAt(0)
18
if (code === PLUS)
19
return 62 // '+'
20
if (code === SLASH)
21
return 63 // '/'
22
if (code < NUMBER)
23
return -1 //no match
24
if (code < NUMBER + 10)
25
return code - NUMBER + 26 + 26
26
if (code < UPPER + 26)
27
return code - UPPER
28
if (code < LOWER + 26)
29
return code - LOWER + 26
30
}
31
32
function b64ToByteArray (b64) {
33
var i, j, l, tmp, placeHolders, arr
34
35
if (b64.length % 4 > 0) {
36
throw new Error('Invalid string. Length must be a multiple of 4')
37
}
38
39
// the number of equal signs (place holders)
40
// if there are two placeholders, than the two characters before it
41
// represent one byte
42
// if there is only one, then the three characters before it represent 2 bytes
43
// this is just a cheap hack to not do indexOf twice
44
var len = b64.length
45
placeHolders = '=' === b64.charAt(len - 2) ? 2 : '=' === b64.charAt(len - 1) ? 1 : 0
46
47
// base64 is 4/3 + up to two characters of the original data
48
arr = new Arr(b64.length * 3 / 4 - placeHolders)
49
50
// if there are placeholders, only get up to the last complete 4 chars
51
l = placeHolders > 0 ? b64.length - 4 : b64.length
52
53
var L = 0
54
55
function push (v) {
56
arr[L++] = v
57
}
58
59
for (i = 0, j = 0; i < l; i += 4, j += 3) {
60
tmp = (decode(b64.charAt(i)) << 18) | (decode(b64.charAt(i + 1)) << 12) | (decode(b64.charAt(i + 2)) << 6) | decode(b64.charAt(i + 3))
61
push((tmp & 0xFF0000) >> 16)
62
push((tmp & 0xFF00) >> 8)
63
push(tmp & 0xFF)
64
}
65
66
if (placeHolders === 2) {
67
tmp = (decode(b64.charAt(i)) << 2) | (decode(b64.charAt(i + 1)) >> 4)
68
push(tmp & 0xFF)
69
} else if (placeHolders === 1) {
70
tmp = (decode(b64.charAt(i)) << 10) | (decode(b64.charAt(i + 1)) << 4) | (decode(b64.charAt(i + 2)) >> 2)
71
push((tmp >> 8) & 0xFF)
72
push(tmp & 0xFF)
73
}
74
75
return arr
76
}
77
78
function uint8ToBase64 (uint8) {
79
var i,
80
extraBytes = uint8.length % 3, // if we have 1 byte left, pad 2 bytes
81
output = "",
82
temp, length
83
84
function encode (num) {
85
return lookup.charAt(num)
86
}
87
88
function tripletToBase64 (num) {
89
return encode(num >> 18 & 0x3F) + encode(num >> 12 & 0x3F) + encode(num >> 6 & 0x3F) + encode(num & 0x3F)
90
}
91
92
// go through the array every three bytes, we'll deal with trailing stuff later
93
for (i = 0, length = uint8.length - extraBytes; i < length; i += 3) {
94
temp = (uint8[i] << 16) + (uint8[i + 1] << 8) + (uint8[i + 2])
95
output += tripletToBase64(temp)
96
}
97
98
// pad the end with zeros, but make sure to not forget the extra bytes
99
switch (extraBytes) {
100
case 1:
101
temp = uint8[uint8.length - 1]
102
output += encode(temp >> 2)
103
output += encode((temp << 4) & 0x3F)
104
output += '=='
105
break
106
case 2:
107
temp = (uint8[uint8.length - 2] << 8) + (uint8[uint8.length - 1])
108
output += encode(temp >> 10)
109
output += encode((temp >> 4) & 0x3F)
110
output += encode((temp << 2) & 0x3F)
111
output += '='
112
break
113
}
114
115
return output
116
}
117
118
exports.toByteArray = b64ToByteArray
119
exports.fromByteArray = uint8ToBase64
120
}(typeof exports === 'undefined' ? (this.base64js = {}) : exports))
121
122