Skip to content

Commit b67bb9b

Browse files
author
Nicholas C. Zakas
committed
Fixed flaw in decoding, now better handles missing equals sign
1 parent 5e1dbf5 commit b67bb9b

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

encodings/base64/base64.htm

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,12 @@ <h1>Base64 Encoding/Decoding Tests</h1>
110110
assert.areEqual(expected, result);
111111
},
112112

113+
testMissingPaddingIndicator: function(){
114+
var expected = "hatch";
115+
var result = base64Decode("aGF0Y2g");
116+
assert.areEqual(expected, result);
117+
},
118+
113119
testInvalidChar: function(){
114120
base64Decode("TWF,JFU");
115121
}

encodings/base64/base64.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,17 @@ function base64Decode(text){
160160

161161
//remove padding
162162
bits = bits.slice(0, bits.length - padCount);
163+
164+
//if there's not enough bits, probably means an equals sign is missing
165+
//remove the extra bits
166+
if (bits.length % 8 != 0){
167+
bits = bits.slice(0, bits.length - bits.length % 8);
168+
}
163169

164170
//transform what remains back into characters
165171
while(bits.length){
166172
part = bits.splice(0, 8).join("");
173+
console.log(part);
167174
result.push(String.fromCharCode(parseInt(part, 2)));
168175
}
169176

0 commit comments

Comments
 (0)