|
|
@ -15,7 +15,7 @@ |
|
|
|
// 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 = indexOf(b64, '='); |
|
|
|
placeHolders = placeHolders > 0 ? b64.length - placeHolders : 0; |
|
|
|
|
|
|
|
// base64 is 4/3 + up to two characters of the original data
|
|
|
@ -25,17 +25,17 @@ |
|
|
|
l = placeHolders > 0 ? b64.length - 4 : b64.length; |
|
|
|
|
|
|
|
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]); |
|
|
|
tmp = (indexOf(lookup, b64.charAt(i)) << 18) | (indexOf(lookup, b64.charAt(i + 1)) << 12) | (indexOf(lookup, b64.charAt(i + 2)) << 6) | indexOf(lookup, b64.charAt(i + 3)); |
|
|
|
arr.push((tmp & 0xFF0000) >> 16); |
|
|
|
arr.push((tmp & 0xFF00) >> 8); |
|
|
|
arr.push(tmp & 0xFF); |
|
|
|
} |
|
|
|
|
|
|
|
if (placeHolders === 2) { |
|
|
|
tmp = (lookup.indexOf(b64[i]) << 2) | (lookup.indexOf(b64[i + 1]) >> 4); |
|
|
|
tmp = (indexOf(lookup, b64.charAt(i)) << 2) | (indexOf(lookup, b64.charAt(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); |
|
|
|
tmp = (indexOf(lookup, b64.charAt(i)) << 10) | (indexOf(lookup, b64.charAt(i + 1)) << 4) | (indexOf(lookup, b64.charAt(i + 2)) >> 2); |
|
|
|
arr.push((tmp >> 8) & 0xFF); |
|
|
|
arr.push(tmp & 0xFF); |
|
|
|
} |
|
|
@ -50,7 +50,7 @@ |
|
|
|
temp, length; |
|
|
|
|
|
|
|
function tripletToBase64 (num) { |
|
|
|
return lookup[num >> 18 & 0x3F] + lookup[num >> 12 & 0x3F] + lookup[num >> 6 & 0x3F] + lookup[num & 0x3F]; |
|
|
|
return lookup.charAt(num >> 18 & 0x3F) + lookup.charAt(num >> 12 & 0x3F) + lookup.charAt(num >> 6 & 0x3F) + lookup.charAt(num & 0x3F); |
|
|
|
}; |
|
|
|
|
|
|
|
// go through the array every three bytes, we'll deal with trailing stuff later
|
|
|
@ -63,15 +63,15 @@ |
|
|
|
switch (extraBytes) { |
|
|
|
case 1: |
|
|
|
temp = uint8[uint8.length - 1]; |
|
|
|
output += lookup[temp >> 2]; |
|
|
|
output += lookup[(temp << 4) & 0x3F]; |
|
|
|
output += lookup.charAt(temp >> 2); |
|
|
|
output += lookup.charAt((temp << 4) & 0x3F); |
|
|
|
output += '=='; |
|
|
|
break; |
|
|
|
case 2: |
|
|
|
temp = (uint8[uint8.length - 2] << 8) + (uint8[uint8.length - 1]); |
|
|
|
output += lookup[temp >> 10]; |
|
|
|
output += lookup[(temp >> 4) & 0x3F]; |
|
|
|
output += lookup[(temp << 2) & 0x3F]; |
|
|
|
output += lookup.charAt(temp >> 10); |
|
|
|
output += lookup.charAt((temp >> 4) & 0x3F); |
|
|
|
output += lookup.charAt((temp << 2) & 0x3F); |
|
|
|
output += '='; |
|
|
|
break; |
|
|
|
} |
|
|
@ -82,3 +82,22 @@ |
|
|
|
module.exports.toByteArray = b64ToByteArray; |
|
|
|
module.exports.fromByteArray = uint8ToBase64; |
|
|
|
}()); |
|
|
|
|
|
|
|
function indexOf (arr, elt /*, from*/) { |
|
|
|
var len = arr.length; |
|
|
|
|
|
|
|
var from = Number(arguments[1]) || 0; |
|
|
|
from = (from < 0) |
|
|
|
? Math.ceil(from) |
|
|
|
: Math.floor(from); |
|
|
|
if (from < 0) |
|
|
|
from += len; |
|
|
|
|
|
|
|
for (; from < len; from++) { |
|
|
|
if ((typeof arr === 'string' && arr.charAt(from) === elt) || |
|
|
|
(typeof arr !== 'string' && arr[from] === elt)) { |
|
|
|
return from; |
|
|
|
} |
|
|
|
} |
|
|
|
return -1; |
|
|
|
} |
|
|
|