Skip to content

Commit

Permalink
merges #5
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisumbel committed Jul 24, 2016
2 parents acf4048 + 9179df9 commit 1f9d240
Showing 1 changed file with 17 additions and 16 deletions.
33 changes: 17 additions & 16 deletions lib/thirty-two/thirty-two.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
Copyright (c) 2011, Chris Umbel
Permission is hereby granted, free of charge, to any person obtaining a copy
Expand All @@ -17,7 +17,7 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
THE SOFTWARE.
*/
'use strict';

Expand All @@ -37,7 +37,7 @@ var byteTable = [

function quintetCount(buff) {
var quintets = Math.floor(buff.length / 5);
return buff.length % 5 == 0 ? quintets: quintets + 1;
return buff.length % 5 === 0 ? quintets: quintets + 1;
}

exports.encode = function(plain) {
Expand All @@ -54,7 +54,7 @@ exports.encode = function(plain) {
faster. will have to revisit. */
while(i < plain.length) {
var current = plain[i];

if(shiftIndex > 3) {
digit = current & (0xff >> shiftIndex);
shiftIndex = (shiftIndex + 5) % 8;
Expand All @@ -63,18 +63,18 @@ exports.encode = function(plain) {
i++;
} else {
digit = (current >> (8 - (shiftIndex + 5))) & 0x1f;
shiftIndex = (shiftIndex + 5) % 8;
if(shiftIndex == 0) { i++; }
shiftIndex = (shiftIndex + 5) % 8;
if(shiftIndex === 0) i++;
}

encoded[j] = charTable.charCodeAt(digit);
j++;
}

for(i = j; i < encoded.length; i++) {
encoded[i] = 0x3d; //'='.charCodeAt(0)
}

return encoded;
};

Expand All @@ -87,23 +87,23 @@ exports.decode = function(encoded) {
encoded = new Buffer(encoded);
}
var decoded = new Buffer(Math.ceil(encoded.length * 5 / 8));

/* byte by byte isn't as pretty as octet by octet but tests a bit
faster. will have to revisit. */
faster. will have to revisit. */
for(var i = 0; i < encoded.length; i++) {
if(encoded[i] == 0x3d){ //'='
if(encoded[i] === 0x3d){ //'='
break;
}

var encodedByte = encoded[i] - 0x30;

if(encodedByte < byteTable.length) {
plainDigit = byteTable[encodedByte];

if(shiftIndex <= 3) {
shiftIndex = (shiftIndex + 5) % 8;
if(shiftIndex == 0) {

if(shiftIndex === 0) {
plainChar |= plainDigit;
decoded[plainPos] = plainChar;
plainPos++;
Expand All @@ -123,5 +123,6 @@ exports.decode = function(encoded) {
throw new Error('Invalid input - it is not base32 encoded string');
}
}

return decoded.slice(0, plainPos);
};

0 comments on commit 1f9d240

Please sign in to comment.