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