File tree Expand file tree Collapse file tree 2 files changed +13
-0
lines changed Expand file tree Collapse file tree 2 files changed +13
-0
lines changed Original file line number Diff line number Diff line change @@ -110,6 +110,12 @@ <h1>Base64 Encoding/Decoding Tests</h1>
110
110
assert . areEqual ( expected , result ) ;
111
111
} ,
112
112
113
+ testMissingPaddingIndicator : function ( ) {
114
+ var expected = "hatch" ;
115
+ var result = base64Decode ( "aGF0Y2g" ) ;
116
+ assert . areEqual ( expected , result ) ;
117
+ } ,
118
+
113
119
testInvalidChar : function ( ) {
114
120
base64Decode ( "TWF,JFU" ) ;
115
121
}
Original file line number Diff line number Diff line change @@ -160,10 +160,17 @@ function base64Decode(text){
160
160
161
161
//remove padding
162
162
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
+ }
163
169
164
170
//transform what remains back into characters
165
171
while ( bits . length ) {
166
172
part = bits . splice ( 0 , 8 ) . join ( "" ) ;
173
+ console . log ( part ) ;
167
174
result . push ( String . fromCharCode ( parseInt ( part , 2 ) ) ) ;
168
175
}
169
176
You can’t perform that action at this time.
0 commit comments