|
|
@ -4,34 +4,40 @@ |
|
|
|
var lookup = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; |
|
|
|
|
|
|
|
function b64ToByteArray(b64) { |
|
|
|
var i, l, tmp, hasPadding, arr = []; |
|
|
|
|
|
|
|
if (i % 4 > 0) { |
|
|
|
var i, j, l, tmp, placeHolders, arr; |
|
|
|
|
|
|
|
if (b64.length % 4 > 0) { |
|
|
|
throw 'Invalid string. Length must be a multiple of 4'; |
|
|
|
} |
|
|
|
|
|
|
|
hasPadding = /=$/.test(b64); |
|
|
|
// the number of equal signs (place holders)
|
|
|
|
// if there are two placeholders, than the two characters before it
|
|
|
|
// represent one byte
|
|
|
|
// 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
|
|
|
|
placeHolders = b64.indexOf('='); |
|
|
|
placeHolders = placeHolders > 0 ? b64.length - placeHolders : 0; |
|
|
|
|
|
|
|
// base64 is 4/3 + up to two characters of the original data
|
|
|
|
arr = [];//new Uint8Array(b64.length * 3 / 4 - placeHolders);
|
|
|
|
|
|
|
|
l = hasPadding ? b64.length - 4: b64.length; |
|
|
|
// if there are placeholders, only get up to the last complete 4 chars
|
|
|
|
l = placeHolders > 0 ? b64.length - 4 : b64.length; |
|
|
|
|
|
|
|
for (i = 0; i < l; i += 4) { |
|
|
|
for (i = 0, j = 0; i < l; i += 4, j += 3) { |
|
|
|
tmp = (lookup.indexOf(b64[i]) << 18) | (lookup.indexOf(b64[i + 1]) << 12) | (lookup.indexOf(b64[i + 2]) << 6) | lookup.indexOf(b64[i + 3]); |
|
|
|
arr.push((tmp & 0xFF0000) >> 16); |
|
|
|
arr.push((tmp & 0xFF00) >> 8); |
|
|
|
arr.push(tmp & 0xFF); |
|
|
|
} |
|
|
|
|
|
|
|
if (hasPadding) { |
|
|
|
b64 = b64.substring(i, b64.indexOf('=')); |
|
|
|
|
|
|
|
if (b64.length === 2) { |
|
|
|
tmp = (lookup.indexOf(b64[0]) << 2) | (lookup.indexOf(b64[1]) >> 4); |
|
|
|
arr.push(tmp & 0xFF); |
|
|
|
} else { |
|
|
|
tmp = (lookup.indexOf(b64[0]) << 10) | (lookup.indexOf(b64[1]) << 4) | (lookup.indexOf(b64[2]) >> 2); |
|
|
|
arr.push((tmp >> 8) & 0xFF); |
|
|
|
arr.push(tmp & 0xFF); |
|
|
|
} |
|
|
|
if (placeHolders === 2) { |
|
|
|
tmp = (lookup.indexOf(b64[i]) << 2) | (lookup.indexOf(b64[i + 1]) >> 4); |
|
|
|
arr.push(tmp & 0xFF); |
|
|
|
} else if (placeHolders === 1) { |
|
|
|
tmp = (lookup.indexOf(b64[i]) << 10) | (lookup.indexOf(b64[i + 1]) << 4) | (lookup.indexOf(b64[i + 2]) >> 2); |
|
|
|
arr.push((tmp >> 8) & 0xFF); |
|
|
|
arr.push(tmp & 0xFF); |
|
|
|
} |
|
|
|
|
|
|
|
return arr; |
|
|
|