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>
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 }
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments