From e16029776fa3ab4f3485099443039278eb275ed9 Mon Sep 17 00:00:00 2001 From: Jaco Date: Wed, 6 Dec 2023 10:44:07 +0200 Subject: [PATCH] Fix base64 last decoding offset (#551) --- packages/wasm-util/src/base64.ts | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/packages/wasm-util/src/base64.ts b/packages/wasm-util/src/base64.ts index d08fe280..f4198a95 100644 --- a/packages/wasm-util/src/base64.ts +++ b/packages/wasm-util/src/base64.ts @@ -3,33 +3,33 @@ // Use an array for our indexer - this is faster than using map access. In // this case we assume ASCII-only inputs, so we cannot overflow the array -const chr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; +const CHR = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/' as const; const map = new Array(256); // We use charCodeAt for access here and in the decoder loop - this is faster // on lookups (array + numbers) and also faster than accessing the specific // character via data[i] -for (let i = 0, count = chr.length; i < count; i++) { - map[chr.charCodeAt(i)] = i; +for (let i = 0, count = CHR.length; i < count; i++) { + map[CHR.charCodeAt(i)] = i; } /** * @name base64Decode * @description - * A base64Decoding function that operates in all environments. Unlike decoding + * A base64 decoding function that operates in all environments. Unlike decoding * from Buffer (Node.js only) or atob (browser-only) this implementation is * slightly slower, but it is platform independent. * - * For our usage, since we have access to the static final size (where used), we - * decode to a specified output buffer. This also means we have applied a number - * of optimizations based on this - checking output position instead of chars. + * For our usage, since we have access to the static final size, so we decode + * to a specified output buffer. This also means we have applied a number of + * optimizations based on this - checking output position instead of chars. */ export function base64Decode (data: string, out: Uint8Array): Uint8Array { let byte = 0; let bits = 0; let pos = -1; - for (let i = 0, count = out.length; pos < count; i++) { + for (let i = 0, last = out.length - 1; pos !== last; i++) { // each character represents 6 bits byte = (byte << 6) | map[data.charCodeAt(i)];