| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 9449242 commit f45d553
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -23,8 +23,10 @@ | |||
| 23 | 23 | ||
| 24 | 24 | const { | |
| 25 | 25 | Array, | |
| 26 | + ArrayFrom, | ||
| 26 | 27 | ArrayIsArray, | |
| 27 | 28 | ArrayPrototypeForEach, | |
| 29 | + ArrayPrototypeIncludes, | ||
| 28 | 30 | MathFloor, | |
| 29 | 31 | MathMin, | |
| 30 | 32 | MathTrunc, | |
@@ -1231,8 +1233,25 @@ function btoa(input) { | |||
| 1231 | 1233 | return buf.toString('base64'); | |
| 1232 | 1234 | } | |
| 1233 | 1235 | ||
| 1234 | - const kBase64Digits = | ||
| 1235 | - 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='; | ||
| 1236 | + // Refs: https://infra.spec.whatwg.org/#forgiving-base64-decode | ||
| 1237 | + const kForgivingBase64AllowedChars = [ | ||
| 1238 | + // ASCII whitespace | ||
| 1239 | + // Refs: https://infra.spec.whatwg.org/#ascii-whitespace | ||
| 1240 | + 0x09, 0x0A, 0x0C, 0x0D, 0x20, | ||
| 1241 | + | ||
| 1242 | + // Uppercase letters | ||
| 1243 | + ...ArrayFrom({ length: 26 }, (_, i) => StringPrototypeCharCodeAt('A') + i), | ||
| 1244 | + | ||
| 1245 | + // Lowercase letters | ||
| 1246 | + ...ArrayFrom({ length: 26 }, (_, i) => StringPrototypeCharCodeAt('a') + i), | ||
| 1247 | + | ||
| 1248 | + // Decimal digits | ||
| 1249 | + ...ArrayFrom({ length: 10 }, (_, i) => StringPrototypeCharCodeAt('0') + i), | ||
| 1250 | + | ||
| 1251 | + 0x2B, // + | ||
| 1252 | + 0x2F, // / | ||
| 1253 | + 0x3D, // = | ||
| 1254 | + ]; | ||
| 1236 | 1255 | ||
| 1237 | 1256 | function atob(input) { | |
| 1238 | 1257 | // The implementation here has not been performance optimized in any way and | |
@@ -1243,7 +1262,8 @@ function atob(input) { | |||
| 1243 | 1262 | } | |
| 1244 | 1263 | input = `${input}`; | |
| 1245 | 1264 | for (let n = 0; n < input.length; n++) { | |
| 1246 | - if (!kBase64Digits.includes(input[n])) | ||
| 1265 | + if (!ArrayPrototypeIncludes(kForgivingBase64AllowedChars, | ||
| 1266 | + StringPrototypeCharCodeAt(input, n))) | ||
| 1247 | 1267 | throw lazyDOMException('Invalid character', 'InvalidCharacterError'); | |
| 1248 | 1268 | } | |
| 1249 | 1269 | return Buffer.from(input, 'base64').toString('latin1'); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -12,3 +12,6 @@ strictEqual(globalThis.btoa, buffer.btoa); | |||
| 12 | 12 | // Throws type error on no argument passed | |
| 13 | 13 | throws(() => buffer.atob(), /TypeError/); | |
| 14 | 14 | throws(() => buffer.btoa(), /TypeError/); | |
| 15 | + | ||
| 16 | + strictEqual(atob(' '), ''); | ||
| 17 | + strictEqual(atob(' YW\tJ\njZA=\r= '), 'abcd'); | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments