|
| 1 | +// About base64: https://en.wikipedia.org/wiki/Base64 |
| 2 | + |
| 3 | +/** |
| 4 | + * Converts a base64 string to an array of bytes |
| 5 | + * @param {string} b64 A base64 string |
| 6 | + * @returns {ArrayBuffer} An ArrayBuffer representing the bytes encoded by the base64 string |
| 7 | + */ |
| 8 | +function base64ToBuffer (b64) { |
| 9 | + // The base64 encoding uses the following set of characters to encode any binary data as text |
| 10 | + const base64Table = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/' |
| 11 | + // Find the index of char '=' first occurrence |
| 12 | + const paddingIdx = b64.indexOf('=') |
| 13 | + // Remove padding chars from base64 string, if there are any |
| 14 | + const b64NoPadding = paddingIdx !== -1 ? b64.slice(0, paddingIdx) : b64 |
| 15 | + // Calculate the length of the result buffer |
| 16 | + const bufferLength = Math.floor((b64NoPadding.length * 6) / 8) |
| 17 | + // Create the result buffer |
| 18 | + const result = new ArrayBuffer(bufferLength) |
| 19 | + // Create an instance of Uint8Array, to write to the `result` buffer |
| 20 | + const byteView = new Uint8Array(result) |
| 21 | + |
| 22 | + // Loop through all chars in the base64 string, in increments of 4 chars, and in increments of 3 bytes |
| 23 | + for (let i = 0, j = 0; i < b64NoPadding.length; i += 4, j += 3) { |
| 24 | + // Get the index of the next 4 base64 chars |
| 25 | + const b64Char1 = base64Table.indexOf(b64NoPadding[i]) |
| 26 | + const b64Char2 = base64Table.indexOf(b64NoPadding[i + 1]) |
| 27 | + let b64Char3 = base64Table.indexOf(b64NoPadding[i + 2]) |
| 28 | + let b64Char4 = base64Table.indexOf(b64NoPadding[i + 3]) |
| 29 | + |
| 30 | + // If base64 chars 3 and 4 don't exit, then set them to 0 |
| 31 | + if (b64Char3 === -1) b64Char3 = 0 |
| 32 | + if (b64Char4 === -1) b64Char4 = 0 |
| 33 | + |
| 34 | + // Calculate the next 3 bytes |
| 35 | + const byte1 = (b64Char1 << 2) + ((b64Char2 & 48) >> 4) |
| 36 | + const byte2 = ((b64Char2 & 15) << 4) + ((b64Char3 & 60) >> 2) |
| 37 | + const byte3 = ((b64Char3 & 3) << 6) + b64Char4 |
| 38 | + |
| 39 | + byteView[j] = byte1 |
| 40 | + byteView[j + 1] = byte2 |
| 41 | + byteView[j + 2] = byte3 |
| 42 | + } |
| 43 | + |
| 44 | + return result |
| 45 | +} |
| 46 | + |
| 47 | +export { base64ToBuffer } |
0 commit comments