Path: blob/master/node_modules/base64-js/index.js
2591 views
'use strict'12exports.byteLength = byteLength3exports.toByteArray = toByteArray4exports.fromByteArray = fromByteArray56var lookup = []7var revLookup = []8var Arr = typeof Uint8Array !== 'undefined' ? Uint8Array : Array910var code = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'11for (var i = 0, len = code.length; i < len; ++i) {12lookup[i] = code[i]13revLookup[code.charCodeAt(i)] = i14}1516// Support decoding URL-safe base64 strings, as Node.js does.17// See: https://en.wikipedia.org/wiki/Base64#URL_applications18revLookup['-'.charCodeAt(0)] = 6219revLookup['_'.charCodeAt(0)] = 632021function getLens (b64) {22var len = b64.length2324if (len % 4 > 0) {25throw new Error('Invalid string. Length must be a multiple of 4')26}2728// Trim off extra bytes after placeholder bytes are found29// See: https://github.com/beatgammit/base64-js/issues/4230var validLen = b64.indexOf('=')31if (validLen === -1) validLen = len3233var placeHoldersLen = validLen === len34? 035: 4 - (validLen % 4)3637return [validLen, placeHoldersLen]38}3940// base64 is 4/3 + up to two characters of the original data41function byteLength (b64) {42var lens = getLens(b64)43var validLen = lens[0]44var placeHoldersLen = lens[1]45return ((validLen + placeHoldersLen) * 3 / 4) - placeHoldersLen46}4748function _byteLength (b64, validLen, placeHoldersLen) {49return ((validLen + placeHoldersLen) * 3 / 4) - placeHoldersLen50}5152function toByteArray (b64) {53var tmp54var lens = getLens(b64)55var validLen = lens[0]56var placeHoldersLen = lens[1]5758var arr = new Arr(_byteLength(b64, validLen, placeHoldersLen))5960var curByte = 06162// if there are placeholders, only get up to the last complete 4 chars63var len = placeHoldersLen > 064? validLen - 465: validLen6667var i68for (i = 0; i < len; i += 4) {69tmp =70(revLookup[b64.charCodeAt(i)] << 18) |71(revLookup[b64.charCodeAt(i + 1)] << 12) |72(revLookup[b64.charCodeAt(i + 2)] << 6) |73revLookup[b64.charCodeAt(i + 3)]74arr[curByte++] = (tmp >> 16) & 0xFF75arr[curByte++] = (tmp >> 8) & 0xFF76arr[curByte++] = tmp & 0xFF77}7879if (placeHoldersLen === 2) {80tmp =81(revLookup[b64.charCodeAt(i)] << 2) |82(revLookup[b64.charCodeAt(i + 1)] >> 4)83arr[curByte++] = tmp & 0xFF84}8586if (placeHoldersLen === 1) {87tmp =88(revLookup[b64.charCodeAt(i)] << 10) |89(revLookup[b64.charCodeAt(i + 1)] << 4) |90(revLookup[b64.charCodeAt(i + 2)] >> 2)91arr[curByte++] = (tmp >> 8) & 0xFF92arr[curByte++] = tmp & 0xFF93}9495return arr96}9798function tripletToBase64 (num) {99return lookup[num >> 18 & 0x3F] +100lookup[num >> 12 & 0x3F] +101lookup[num >> 6 & 0x3F] +102lookup[num & 0x3F]103}104105function encodeChunk (uint8, start, end) {106var tmp107var output = []108for (var i = start; i < end; i += 3) {109tmp =110((uint8[i] << 16) & 0xFF0000) +111((uint8[i + 1] << 8) & 0xFF00) +112(uint8[i + 2] & 0xFF)113output.push(tripletToBase64(tmp))114}115return output.join('')116}117118function fromByteArray (uint8) {119var tmp120var len = uint8.length121var extraBytes = len % 3 // if we have 1 byte left, pad 2 bytes122var parts = []123var maxChunkLength = 16383 // must be multiple of 3124125// go through the array every three bytes, we'll deal with trailing stuff later126for (var i = 0, len2 = len - extraBytes; i < len2; i += maxChunkLength) {127parts.push(encodeChunk(uint8, i, (i + maxChunkLength) > len2 ? len2 : (i + maxChunkLength)))128}129130// pad the end with zeros, but make sure to not forget the extra bytes131if (extraBytes === 1) {132tmp = uint8[len - 1]133parts.push(134lookup[tmp >> 2] +135lookup[(tmp << 4) & 0x3F] +136'=='137)138} else if (extraBytes === 2) {139tmp = (uint8[len - 2] << 8) + uint8[len - 1]140parts.push(141lookup[tmp >> 10] +142lookup[(tmp >> 4) & 0x3F] +143lookup[(tmp << 2) & 0x3F] +144'='145)146}147148return parts.join('')149}150151152