Browse Source

base64-js uses tabs, sorry!

master
Dominic Tarr 11 years ago
parent
commit
96c7a98bfb
  1. 234
      lib/b64.js
  2. 4
      test/bench.js

234
lib/b64.js

@ -2,122 +2,122 @@ var lookup = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
;(function (exports) { ;(function (exports) {
'use strict'; 'use strict';
var ZERO = '0'.charCodeAt(0) var ZERO = '0'.charCodeAt(0)
var PLUS = '+'.charCodeAt(0) var PLUS = '+'.charCodeAt(0)
var SLASH = '/'.charCodeAt(0) var SLASH = '/'.charCodeAt(0)
var NUMBER = '0'.charCodeAt(0) var NUMBER = '0'.charCodeAt(0)
var LOWER = 'a'.charCodeAt(0) var LOWER = 'a'.charCodeAt(0)
var UPPER = 'A'.charCodeAt(0) var UPPER = 'A'.charCodeAt(0)
function decode (elt) { function decode (elt) {
var code = elt.charCodeAt(0) var code = elt.charCodeAt(0)
if(code === PLUS) if(code === PLUS)
return 62 // '+' return 62 // '+'
if(code === SLASH) if(code === SLASH)
return 63 // '/' return 63 // '/'
if(code < NUMBER) if(code < NUMBER)
return -1 //no match return -1 //no match
if(code < NUMBER + 10) if(code < NUMBER + 10)
return code - NUMBER + 26 + 26 return code - NUMBER + 26 + 26
if(code < UPPER + 26) if(code < UPPER + 26)
return code - UPPER return code - UPPER
if(code < LOWER + 26) if(code < LOWER + 26)
return code - LOWER + 26 return code - LOWER + 26
} }
function b64ToByteArray(b64) { function b64ToByteArray(b64) {
var i, j, l, tmp, placeHolders, arr; var i, j, l, tmp, placeHolders, arr;
if (b64.length % 4 > 0) { if (b64.length % 4 > 0) {
throw 'Invalid string. Length must be a multiple of 4'; throw 'Invalid string. Length must be a multiple of 4';
} }
// the number of equal signs (place holders) // the number of equal signs (place holders)
// if there are two placeholders, than the two characters before it // if there are two placeholders, than the two characters before it
// 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
var len = b64.length var len = b64.length
placeHolders = '=' === b64[len - 2] ? 2 : '=' === b64[len - 1] ? 1 : 0 placeHolders = '=' === b64[len - 2] ? 2 : '=' === b64[len - 1] ? 1 : 0
// base64 is 4/3 + up to two characters of the original data // base64 is 4/3 + up to two characters of the original data
arr = new Uint8Array(b64.length * 3 / 4 - placeHolders); arr = new Uint8Array(b64.length * 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
l = placeHolders > 0 ? b64.length - 4 : b64.length; l = placeHolders > 0 ? b64.length - 4 : b64.length;
var L = 0 var L = 0
function push (v) { function push (v) {
arr[L++] = v arr[L++] = v
} }
for (i = 0, j = 0; i < l; i += 4, j += 3) { for (i = 0, j = 0; i < l; i += 4, j += 3) {
tmp = (decode(b64.charAt(i)) << 18) | (decode(b64.charAt(i + 1)) << 12) | (decode(b64.charAt(i + 2)) << 6) | decode(b64.charAt(i + 3)); tmp = (decode(b64.charAt(i)) << 18) | (decode(b64.charAt(i + 1)) << 12) | (decode(b64.charAt(i + 2)) << 6) | decode(b64.charAt(i + 3));
push((tmp & 0xFF0000) >> 16); push((tmp & 0xFF0000) >> 16);
push((tmp & 0xFF00) >> 8); push((tmp & 0xFF00) >> 8);
push(tmp & 0xFF); push(tmp & 0xFF);
} }
if (placeHolders === 2) { if (placeHolders === 2) {
tmp = (decode(b64.charAt(i)) << 2) | (decode(b64.charAt(i + 1)) >> 4); tmp = (decode(b64.charAt(i)) << 2) | (decode(b64.charAt(i + 1)) >> 4);
push(tmp & 0xFF); push(tmp & 0xFF);
} else if (placeHolders === 1) { } else if (placeHolders === 1) {
tmp = (decode(b64.charAt(i)) << 10) | (decode(b64.charAt(i + 1)) << 4) | (decode(b64.charAt(i + 2)) >> 2); tmp = (decode(b64.charAt(i)) << 10) | (decode(b64.charAt(i + 1)) << 4) | (decode(b64.charAt(i + 2)) >> 2);
push((tmp >> 8) & 0xFF); push((tmp >> 8) & 0xFF);
push(tmp & 0xFF); push(tmp & 0xFF);
} }
return arr; return arr;
} }
function uint8ToBase64(uint8) { function uint8ToBase64(uint8) {
var i, var i,
extraBytes = uint8.length % 3, // if we have 1 byte left, pad 2 bytes extraBytes = uint8.length % 3, // if we have 1 byte left, pad 2 bytes
output = "", output = "",
temp, length; temp, length;
function encode (num) { function encode (num) {
return lookup.charAt(num) return lookup.charAt(num)
} }
function tripletToBase64 (num) { function tripletToBase64 (num) {
return encode(num >> 18 & 0x3F) + encode(num >> 12 & 0x3F) + encode(num >> 6 & 0x3F) + encode(num & 0x3F); return encode(num >> 18 & 0x3F) + encode(num >> 12 & 0x3F) + encode(num >> 6 & 0x3F) + encode(num & 0x3F);
}; };
// go through the array every three bytes, we'll deal with trailing stuff later // go through the array every three bytes, we'll deal with trailing stuff later
for (i = 0, length = uint8.length - extraBytes; i < length; i += 3) { for (i = 0, length = uint8.length - extraBytes; i < length; i += 3) {
temp = (uint8[i] << 16) + (uint8[i + 1] << 8) + (uint8[i + 2]); temp = (uint8[i] << 16) + (uint8[i + 1] << 8) + (uint8[i + 2]);
output += tripletToBase64(temp); output += tripletToBase64(temp);
} }
// pad the end with zeros, but make sure to not forget the extra bytes // pad the end with zeros, but make sure to not forget the extra bytes
switch (extraBytes) { switch (extraBytes) {
case 1: case 1:
temp = uint8[uint8.length - 1]; temp = uint8[uint8.length - 1];
output += encode(temp >> 2); output += encode(temp >> 2);
output += encode((temp << 4) & 0x3F); output += encode((temp << 4) & 0x3F);
output += '=='; output += '==';
break; break;
case 2: case 2:
temp = (uint8[uint8.length - 2] << 8) + (uint8[uint8.length - 1]); temp = (uint8[uint8.length - 2] << 8) + (uint8[uint8.length - 1]);
output += encode(temp >> 10); output += encode(temp >> 10);
output += encode((temp >> 4) & 0x3F); output += encode((temp >> 4) & 0x3F);
output += encode((temp << 2) & 0x3F); output += encode((temp << 2) & 0x3F);
output += '='; output += '=';
break; break;
} }
return output; return output;
} }
module.exports.toByteArray = b64ToByteArray; module.exports.toByteArray = b64ToByteArray;
module.exports.fromByteArray = uint8ToBase64; module.exports.fromByteArray = uint8ToBase64;
module.exports.lookup = lookup module.exports.lookup = lookup
}()); }());

4
test/bench.js

@ -13,7 +13,7 @@ var end = Date.now()
console.log('decode ms, decode ops/ms, encode ms, encode ops/ms') console.log('decode ms, decode ops/ms, encode ms, encode ops/ms')
console.log( console.log(
middle - start, data.length / (middle - start), middle - start, data.length / (middle - start),
end - middle, data.length / (end - middle)) end - middle, data.length / (end - middle))
//console.log(data) //console.log(data)

Loading…
Cancel
Save