@@ -203,7 +203,7 @@ mod decl {
203203 if quad_pos >= 2 && quad_pos + pads >= 4 {
204204 if strict_mode && i + 1 < b. len ( ) {
205205 // Represents excess data after padding error
206- return Err ( base64:: DecodeError :: InvalidLastSymbol ( i, el ) ) ;
206+ return Err ( base64:: DecodeError :: InvalidLastSymbol ( i, PAD ) ) ;
207207 }
208208
209209 return Ok ( decoded) ;
@@ -246,18 +246,11 @@ mod decl {
246246 }
247247 }
248248
249- if quad_pos == 1 {
250- // Ensure that a PAD never gets passed, since that'd mistakenly cause an excess
251- // data after padding error
252- return Err ( base64:: DecodeError :: InvalidLastSymbol (
253- decoded. len ( ) / 3 * 4 + 1 ,
254- 0 ,
255- ) ) ;
256- } else if quad_pos > 1 {
257- return Err ( base64:: DecodeError :: InvalidLength ) ;
258- }
259-
260- Ok ( decoded)
249+ return match quad_pos {
250+ 0 => Ok ( decoded) ,
251+ 1 => Err ( base64:: DecodeError :: InvalidLastSymbol ( decoded. len ( ) / 3 * 4 + 1 , 0 ) ) ,
252+ _ => Err ( base64:: DecodeError :: InvalidLength )
253+ } ;
261254 } )
262255 . map_err ( |err| {
263256 let python_error = match err {
@@ -270,10 +263,13 @@ mod decl {
270263 base64:: DecodeError :: InvalidByte ( _, _) => {
271264 String :: from ( "Only base64 data is allowed" )
272265 }
273- base64:: DecodeError :: InvalidLastSymbol ( _, _ ) => {
266+ base64:: DecodeError :: InvalidLastSymbol ( _, PAD ) => {
274267 String :: from ( "Excess data after padding" )
275268 }
276- base64:: DecodeError :: InvalidLength => String :: from ( "Not implemented (yet)" ) ,
269+ base64:: DecodeError :: InvalidLastSymbol ( length, _) => {
270+ format ! ( "Invalid base64-encoded string: number of data characters {} cannot be 1 more than a multiple of 4" , length)
271+ }
272+ base64:: DecodeError :: InvalidLength => String :: from ( "Incorrect padding" ) ,
277273 } ;
278274
279275 new_binascii_error ( format ! ( "error decoding base64: {python_error}" ) , vm)
0 commit comments