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