c@117: 'use strict' c@117: c@117: if (typeof document === "undefined") { c@117: exports.byteLength = byteLength c@117: exports.toByteArray = toByteArray c@117: exports.fromByteArray = fromByteArray c@117: } c@117: c@117: var lookup = [] c@117: var revLookup = [] c@117: var Arr = typeof Uint8Array !== 'undefined' ? Uint8Array : Array c@117: c@117: var code = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/' c@117: for (var i = 0, len = code.length; i < len; ++i) { c@117: lookup[i] = code[i] c@117: revLookup[code.charCodeAt(i)] = i c@117: } c@117: c@117: revLookup['-'.charCodeAt(0)] = 62 c@117: revLookup['_'.charCodeAt(0)] = 63 c@117: c@117: function placeHoldersCount (b64) { c@117: var len = b64.length c@117: if (len % 4 > 0) { c@117: throw new Error('Invalid string. Length must be a multiple of 4') c@117: } c@117: c@117: // the number of equal signs (place holders) c@117: // if there are two placeholders, than the two characters before it c@117: // represent one byte c@117: // if there is only one, then the three characters before it represent 2 bytes c@117: // this is just a cheap hack to not do indexOf twice c@117: return b64[len - 2] === '=' ? 2 : b64[len - 1] === '=' ? 1 : 0 c@117: } c@117: c@117: function byteLength (b64) { c@117: // base64 is 4/3 + up to two characters of the original data c@117: return b64.length * 3 / 4 - placeHoldersCount(b64) c@117: } c@117: c@117: function toByteArray (b64) { c@117: var i, j, l, tmp, placeHolders, arr c@117: var len = b64.length c@117: placeHolders = placeHoldersCount(b64) c@117: c@117: arr = new Arr(len * 3 / 4 - placeHolders) c@117: c@117: // if there are placeholders, only get up to the last complete 4 chars c@117: l = placeHolders > 0 ? len - 4 : len c@117: c@117: var L = 0 c@117: c@117: for (i = 0, j = 0; i < l; i += 4, j += 3) { c@117: tmp = (revLookup[b64.charCodeAt(i)] << 18) | (revLookup[b64.charCodeAt(i + 1)] << 12) | (revLookup[b64.charCodeAt(i + 2)] << 6) | revLookup[b64.charCodeAt(i + 3)] c@117: arr[L++] = (tmp >> 16) & 0xFF c@117: arr[L++] = (tmp >> 8) & 0xFF c@117: arr[L++] = tmp & 0xFF c@117: } c@117: c@117: if (placeHolders === 2) { c@117: tmp = (revLookup[b64.charCodeAt(i)] << 2) | (revLookup[b64.charCodeAt(i + 1)] >> 4) c@117: arr[L++] = tmp & 0xFF c@117: } else if (placeHolders === 1) { c@117: tmp = (revLookup[b64.charCodeAt(i)] << 10) | (revLookup[b64.charCodeAt(i + 1)] << 4) | (revLookup[b64.charCodeAt(i + 2)] >> 2) c@117: arr[L++] = (tmp >> 8) & 0xFF c@117: arr[L++] = tmp & 0xFF c@117: } c@117: c@117: return arr c@117: } c@117: c@117: function tripletToBase64 (num) { c@117: return lookup[num >> 18 & 0x3F] + lookup[num >> 12 & 0x3F] + lookup[num >> 6 & 0x3F] + lookup[num & 0x3F] c@117: } c@117: c@117: function encodeChunk (uint8, start, end) { c@117: var tmp c@117: var output = [] c@117: for (var i = start; i < end; i += 3) { c@117: tmp = (uint8[i] << 16) + (uint8[i + 1] << 8) + (uint8[i + 2]) c@117: output.push(tripletToBase64(tmp)) c@117: } c@117: return output.join('') c@117: } c@117: c@117: function fromByteArray (uint8) { c@117: var tmp c@117: var len = uint8.length c@117: var extraBytes = len % 3 // if we have 1 byte left, pad 2 bytes c@117: var output = '' c@117: var parts = [] c@117: var maxChunkLength = 16383 // must be multiple of 3 c@117: c@117: // go through the array every three bytes, we'll deal with trailing stuff later c@117: for (var i = 0, len2 = len - extraBytes; i < len2; i += maxChunkLength) { c@117: parts.push(encodeChunk(uint8, i, (i + maxChunkLength) > len2 ? len2 : (i + maxChunkLength))) c@117: } c@117: c@117: // pad the end with zeros, but make sure to not forget the extra bytes c@117: if (extraBytes === 1) { c@117: tmp = uint8[len - 1] c@117: output += lookup[tmp >> 2] c@117: output += lookup[(tmp << 4) & 0x3F] c@117: output += '==' c@117: } else if (extraBytes === 2) { c@117: tmp = (uint8[len - 2] << 8) + (uint8[len - 1]) c@117: output += lookup[tmp >> 10] c@117: output += lookup[(tmp >> 4) & 0x3F] c@117: output += lookup[(tmp << 2) & 0x3F] c@117: output += '=' c@117: } c@117: c@117: parts.push(output) c@117: c@117: return parts.join('') c@117: }