@@ -1256,6 +1256,8 @@ const kForgivingBase64AllowedChars = [
12561256  0x2F ,  // / 
12571257  0x3D ,  // = 
12581258] ; 
1259+ const  kEqualSignIndex  =  ArrayPrototypeIndexOf ( kForgivingBase64AllowedChars , 
1260+                                               0x3D ) ; 
12591261
12601262function  atob ( input )  { 
12611263  // The implementation here has not been performance optimized in any way and 
@@ -1266,7 +1268,6 @@ function atob(input) {
12661268  } 
12671269
12681270  input  =  `${ input }  ; 
1269-   const  initLength  =  input . length ; 
12701271  let  nonAsciiWhitespaceCharCount  =  0 ; 
12711272  let  equalCharCount  =  0 ; 
12721273
@@ -1280,10 +1281,14 @@ function atob(input) {
12801281      // ASCII whitespace char codes. 
12811282      nonAsciiWhitespaceCharCount ++ ; 
12821283
1283-       if  ( index  ===  kForgivingBase64AllowedChars . length  -  1  &&  equalCharCount  !==  2 )  { 
1284-         // The last element of `kForgivingBase64AllowedChars` is the `=` 
1284+       if  ( index  ===  kEqualSignIndex )  { 
12851285        equalCharCount ++ ; 
12861286      }  else  if  ( equalCharCount )  { 
1287+         // The `=` char is only allowed at the end. 
1288+         throw  lazyDOMException ( 'Invalid character' ,  'InvalidCharacterError' ) ; 
1289+       } 
1290+ 
1291+       if  ( equalCharCount  >  2 )  { 
12871292        // Only one more `=` is permitted after the first equal sign. 
12881293        throw  lazyDOMException ( 'Invalid character' ,  'InvalidCharacterError' ) ; 
12891294      } 
@@ -1292,28 +1297,14 @@ function atob(input) {
12921297    } 
12931298  } 
12941299
1295-   // See #2 and #4 - https://infra.spec.whatwg.org/#forgiving-base64 
1296-   if  ( equalCharCount  >  2 )  { 
1297-     throw  lazyDOMException ( 'Invalid character' ,  'InvalidCharacterError' ) ; 
1298-   } 
1299- 
13001300  let  reminder  =  nonAsciiWhitespaceCharCount  %  4 ; 
13011301
1302-   // See #2 - https://infra.spec.whatwg.org/#forgiving-base64 
1302+   // See #2, #3, #4  - https://infra.spec.whatwg.org/#forgiving-base64 
13031303  if  ( ! reminder )  { 
1304-     // Remove all trailing `=` characters (whitespace characters will be 
1305-     // ignored). 
1306-     input  =  input . replace ( / ( = \s * ) { 1 , 2 } $ / ,  ( s )  =>  { 
1307-       return  s . replace ( / = / gi,  '' ) ; 
1308-     } ) ; 
1309-     const  deltaLength  =  initLength  -  input . length ; 
1310-     nonAsciiWhitespaceCharCount  -=  deltaLength ; 
1311-     equalCharCount  -=  deltaLength ; 
1312-     reminder  =  nonAsciiWhitespaceCharCount  %  4 ; 
1313-   } 
1314- 
1315-   // See #4 - https://infra.spec.whatwg.org/#forgiving-base64 
1316-   if  ( equalCharCount )  { 
1304+     // Remove all trailing `=` characters and get the new reminder. 
1305+     reminder  =  ( nonAsciiWhitespaceCharCount  -  equalCharCount )  %  4 ; 
1306+   }  else  if  ( equalCharCount )  { 
1307+     // `=` should not in the input if there's a reminder. 
13171308    throw  lazyDOMException ( 'Invalid character' ,  'InvalidCharacterError' ) ; 
13181309  } 
13191310
0 commit comments