|
@ -1,5 +1,6 @@ |
|
|
'use strict' |
|
|
'use strict' |
|
|
|
|
|
|
|
|
|
|
|
exports.byteLength = byteLength |
|
|
exports.toByteArray = toByteArray |
|
|
exports.toByteArray = toByteArray |
|
|
exports.fromByteArray = fromByteArray |
|
|
exports.fromByteArray = fromByteArray |
|
|
|
|
|
|
|
@ -20,10 +21,8 @@ function init () { |
|
|
|
|
|
|
|
|
init() |
|
|
init() |
|
|
|
|
|
|
|
|
function toByteArray (b64) { |
|
|
function placeHoldersCount (b64) { |
|
|
var i, j, l, tmp, placeHolders, arr |
|
|
|
|
|
var len = b64.length |
|
|
var len = b64.length |
|
|
|
|
|
|
|
|
if (len % 4 > 0) { |
|
|
if (len % 4 > 0) { |
|
|
throw new Error('Invalid string. Length must be a multiple of 4') |
|
|
throw new Error('Invalid string. Length must be a multiple of 4') |
|
|
} |
|
|
} |
|
@ -33,9 +32,19 @@ function toByteArray (b64) { |
|
|
// represent one byte
|
|
|
// represent one byte
|
|
|
// if there is only one, then the three characters before it represent 2 bytes
|
|
|
// if there is only one, then the three characters before it represent 2 bytes
|
|
|
// this is just a cheap hack to not do indexOf twice
|
|
|
// this is just a cheap hack to not do indexOf twice
|
|
|
placeHolders = b64[len - 2] === '=' ? 2 : b64[len - 1] === '=' ? 1 : 0 |
|
|
return b64[len - 2] === '=' ? 2 : b64[len - 1] === '=' ? 1 : 0 |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
function byteLength (b64) { |
|
|
// base64 is 4/3 + up to two characters of the original data
|
|
|
// base64 is 4/3 + up to two characters of the original data
|
|
|
|
|
|
return b64.length * 3 / 4 - placeHoldersCount(b64) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
function toByteArray (b64) { |
|
|
|
|
|
var i, j, l, tmp, placeHolders, arr |
|
|
|
|
|
var len = b64.length |
|
|
|
|
|
placeHolders = placeHoldersCount(b64) |
|
|
|
|
|
|
|
|
arr = new Arr(len * 3 / 4 - placeHolders) |
|
|
arr = new Arr(len * 3 / 4 - placeHolders) |
|
|
|
|
|
|
|
|
// if there are placeholders, only get up to the last complete 4 chars
|
|
|
// if there are placeholders, only get up to the last complete 4 chars
|
|
|