From 9ec94434bb5d1ebbbe7182c0400c80137435f220 Mon Sep 17 00:00:00 2001 From: n1474335 Date: Mon, 6 Jun 2022 14:54:06 +0100 Subject: [PATCH] Fixed 'From Base64' bug adding null bytes. Closes #1362 --- src/core/lib/Base64.mjs | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/core/lib/Base64.mjs b/src/core/lib/Base64.mjs index 06ec5ab9..c26630b9 100644 --- a/src/core/lib/Base64.mjs +++ b/src/core/lib/Base64.mjs @@ -130,10 +130,11 @@ export function fromBase64(data, alphabet="A-Za-z0-9+/=", returnType="string", r i = 0; while (i < data.length) { - enc1 = alphabet.indexOf(data.charAt(i++)); - enc2 = alphabet.indexOf(data.charAt(i++)); - enc3 = alphabet.indexOf(data.charAt(i++)); - enc4 = alphabet.indexOf(data.charAt(i++)); + // Including `|| null` forces empty strings to null so that indexOf returns -1 instead of 0 + enc1 = alphabet.indexOf(data.charAt(i++) || null); + enc2 = alphabet.indexOf(data.charAt(i++) || null); + enc3 = alphabet.indexOf(data.charAt(i++) || null); + enc4 = alphabet.indexOf(data.charAt(i++) || null); if (strictMode && (enc1 < 0 || enc2 < 0 || enc3 < 0 || enc4 < 0)) { throw new OperationError("Error: Base64 input contains non-alphabet char(s)"); @@ -143,13 +144,13 @@ export function fromBase64(data, alphabet="A-Za-z0-9+/=", returnType="string", r chr2 = ((enc2 & 15) << 4) | (enc3 >> 2); chr3 = ((enc3 & 3) << 6) | enc4; - if (chr1 < 256) { + if (chr1 >= 0 && chr1 < 256) { output.push(chr1); } - if (chr2 < 256 && enc3 !== 64) { + if (chr2 >= 0 && chr2 < 256 && enc3 !== 64) { output.push(chr2); } - if (chr3 < 256 && enc4 !== 64) { + if (chr3 >= 0 && chr3 < 256 && enc4 !== 64) { output.push(chr3); } }