Skip to content

Instantly share code, notes, and snippets.

@bigjason
Last active August 29, 2015 14:05
Show Gist options
  • Select an option

  • Save bigjason/6275b99155f4f462f410 to your computer and use it in GitHub Desktop.

Select an option

Save bigjason/6275b99155f4f462f410 to your computer and use it in GitHub Desktop.
Copy of couchbase decodeBase64 for debugging
var decodeBase64 = (function(b64) {
var i, j, l, tmp, scratch, arr = [];
var lookup = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
if (typeof b64 !== 'string') {
throw 'Input is not a string';
}
if (b64.length % 4 > 0) {
throw 'Invalid base64 source.';
}
scratch = b64.indexOf('=');
scratch = scratch > 0 ? b64.length - scratch : 0;
l = scratch > 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);
tmp |= (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 (scratch === 2) {
tmp = (lookup.indexOf(b64[i]) << 2) | (lookup.indexOf(b64[i + 1]) >> 4);
arr.push(tmp & 0xFF);
} else if (scratch === 1) {
tmp = (lookup.indexOf(b64[i]) << 10) | (lookup.indexOf(b64[i + 1]) << 4);
tmp |= (lookup.indexOf(b64[i + 2]) >> 2);
arr.push((tmp >> 8) & 0xFF);
arr.push(tmp & 0xFF);
}
return arr;
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment