3 exports.byteLength = byteLength
4 exports.toByteArray = toByteArray
5 exports.fromByteArray = fromByteArray
9 var Arr = typeof Uint8Array !== 'undefined' ? Uint8Array : Array
11 var code = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
12 for (var i = 0, len = code.length; i < len; ++i) {
14 revLookup[code.charCodeAt(i)] = i
17 // Support decoding URL-safe base64 strings, as Node.js does.
18 // See: https://en.wikipedia.org/wiki/Base64#URL_applications
19 revLookup['-'.charCodeAt(0)] = 62
20 revLookup['_'.charCodeAt(0)] = 63
22 function getLens (b64) {
26 throw new Error('Invalid string. Length must be a multiple of 4')
29 // Trim off extra bytes after placeholder bytes are found
30 // See: https://github.com/beatgammit/base64-js/issues/42
31 var validLen = b64.indexOf('=')
32 if (validLen === -1) validLen = len
34 var placeHoldersLen = validLen === len
38 return [validLen, placeHoldersLen]
41 // base64 is 4/3 + up to two characters of the original data
42 function byteLength (b64) {
43 var lens = getLens(b64)
44 var validLen = lens[0]
45 var placeHoldersLen = lens[1]
46 return ((validLen + placeHoldersLen) * 3 / 4) - placeHoldersLen
49 function _byteLength (b64, validLen, placeHoldersLen) {
50 return ((validLen + placeHoldersLen) * 3 / 4) - placeHoldersLen
53 function toByteArray (b64) {
55 var lens = getLens(b64)
56 var validLen = lens[0]
57 var placeHoldersLen = lens[1]
59 var arr = new Arr(_byteLength(b64, validLen, placeHoldersLen))
63 // if there are placeholders, only get up to the last complete 4 chars
64 var len = placeHoldersLen > 0
69 for (i = 0; i < len; i += 4) {
71 (revLookup[b64.charCodeAt(i)] << 18) |
72 (revLookup[b64.charCodeAt(i + 1)] << 12) |
73 (revLookup[b64.charCodeAt(i + 2)] << 6) |
74 revLookup[b64.charCodeAt(i + 3)]
75 arr[curByte++] = (tmp >> 16) & 0xFF
76 arr[curByte++] = (tmp >> 8) & 0xFF
77 arr[curByte++] = tmp & 0xFF
80 if (placeHoldersLen === 2) {
82 (revLookup[b64.charCodeAt(i)] << 2) |
83 (revLookup[b64.charCodeAt(i + 1)] >> 4)
84 arr[curByte++] = tmp & 0xFF
87 if (placeHoldersLen === 1) {
89 (revLookup[b64.charCodeAt(i)] << 10) |
90 (revLookup[b64.charCodeAt(i + 1)] << 4) |
91 (revLookup[b64.charCodeAt(i + 2)] >> 2)
92 arr[curByte++] = (tmp >> 8) & 0xFF
93 arr[curByte++] = tmp & 0xFF
99 function tripletToBase64 (num) {
100 return lookup[num >> 18 & 0x3F] +
101 lookup[num >> 12 & 0x3F] +
102 lookup[num >> 6 & 0x3F] +
106 function encodeChunk (uint8, start, end) {
109 for (var i = start; i < end; i += 3) {
111 ((uint8[i] << 16) & 0xFF0000) +
112 ((uint8[i + 1] << 8) & 0xFF00) +
113 (uint8[i + 2] & 0xFF)
114 output.push(tripletToBase64(tmp))
116 return output.join('')
119 function fromByteArray (uint8) {
121 var len = uint8.length
122 var extraBytes = len % 3 // if we have 1 byte left, pad 2 bytes
124 var maxChunkLength = 16383 // must be multiple of 3
126 // go through the array every three bytes, we'll deal with trailing stuff later
127 for (var i = 0, len2 = len - extraBytes; i < len2; i += maxChunkLength) {
128 parts.push(encodeChunk(uint8, i, (i + maxChunkLength) > len2 ? len2 : (i + maxChunkLength)))
131 // pad the end with zeros, but make sure to not forget the extra bytes
132 if (extraBytes === 1) {
136 lookup[(tmp << 4) & 0x3F] +
139 } else if (extraBytes === 2) {
140 tmp = (uint8[len - 2] << 8) + uint8[len - 1]
143 lookup[(tmp >> 4) & 0x3F] +
144 lookup[(tmp << 2) & 0x3F] +
149 return parts.join('')