* Decodes a Base64-encoded string into a binary string. * * @param {string} input - The Base64-encoded string * @returns {string} The decoded binary string * @throws {Error} If the input contains invalid Base64 characters
(input)
| 22 | * @throws {Error} If the input contains invalid Base64 characters |
| 23 | */ |
| 24 | function atob(input) { |
| 25 | input = input.replace(/[\s=]+$/, ''); |
| 26 | const base64Chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; |
| 27 | let output = ''; |
| 28 | let buffer = 0, bits = 0; |
| 29 | |
| 30 | for (let i = 0; i < input.length; i++) { |
| 31 | let value = base64Chars.indexOf(input[i]); |
| 32 | if (value === -1) |
| 33 | throw new Error('Invalid character in Base64 string'); |
| 34 | |
| 35 | buffer = (buffer << 6) | value; |
| 36 | bits += 6; |
| 37 | if (bits >= 8) { |
| 38 | bits -= 8; |
| 39 | output += String.fromCharCode((buffer >> bits) & 0xFF); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | return output; |
| 44 | } |
| 45 | |
| 46 | //------------------------------------------------------------------------------ |
| 47 | // Frame Parser Function |