diff --git a/src/protocol/encoding.ts b/src/protocol/encoding.ts index 72d571e..daa2a26 100644 --- a/src/protocol/encoding.ts +++ b/src/protocol/encoding.ts @@ -1,24 +1,46 @@ const qoderCustomAlphabet = "_doRTgHZBKcGVjlvpC,@aFSx#DPuNJme&i*MzLOEn)sUrthbf%Y^w.(kIQyXqWA!"; + const qoderStdAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; +const encodeTable = new Uint8Array(256); + +for (let i = 0; i < encodeTable.length; i++) { + encodeTable[i] = i; +} + +for (let i = 0; i < qoderStdAlphabet.length; i++) { + encodeTable[qoderStdAlphabet.charCodeAt(i)] = qoderCustomAlphabet.charCodeAt(i); +} + +// Qoder uses "$" instead of standard Base64 "=" padding. +encodeTable["=".charCodeAt(0)] = "$".charCodeAt(0); + export function qoderEncodeBody(plaintext: string | Buffer): string { const std = Buffer.isBuffer(plaintext) ? plaintext.toString("base64") : Buffer.from(plaintext).toString("base64"); - const n = std.length; + + const src = Buffer.from(std, "ascii"); + const n = src.length; const a = Math.floor(n / 3); - const rearranged = std.slice(n - a) + std.slice(a, n - a) + std.slice(0, a); - let out = ""; - for (let i = 0; i < n; i++) { - const c = rearranged[i]; - if (c === "=") { - out += "$"; - } else { - const idx = qoderStdAlphabet.indexOf(c); - if (idx >= 0) { - out += qoderCustomAlphabet[idx]; - } else { - out += c; - } - } + + const out = Buffer.allocUnsafe(n); + let dst = 0; + + // Equivalent to: + // std.slice(n - a) + std.slice(a, n - a) + std.slice(0, a) + // + // Translate directly into the output buffer to avoid building a large + // rearranged string and repeatedly concatenating one-character strings. + for (let i = n - a; i < n; i++) { + out[dst++] = encodeTable[src[i]]; } - return out; + + for (let i = a; i < n - a; i++) { + out[dst++] = encodeTable[src[i]]; + } + + for (let i = 0; i < a; i++) { + out[dst++] = encodeTable[src[i]]; + } + + return out.toString("ascii"); }