#2355 - fix ToBase32 operation breaking UTF-16 surrogate pairs in custom alphabets

This commit is contained in:
Loknath Mishra 2026-05-13 22:59:08 +05:30
parent d8aec9c43f
commit ab7e5006af

View File

@ -43,11 +43,19 @@ class ToBase32 extends Operation {
if (!input) return "";
input = new Uint8Array(input);
const alphabet = args[0] ? Utils.expandAlphRange(args[0]).join("") : "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=";
const alphabet = args[0]
? Utils.expandAlphRange(args[0]).join("")
: "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=";
// Unicode-safe alphabet handling
// Supports BMP + non-BMP characters (emoji, Mahjong tiles, etc.)
const alphabetChars = Array.from(alphabet);
let output = "",
chr1, chr2, chr3, chr4, chr5,
enc1, enc2, enc3, enc4, enc5, enc6, enc7, enc8,
i = 0;
while (i < input.length) {
chr1 = input[i++];
chr2 = input[i++];
@ -74,10 +82,19 @@ class ToBase32 extends Operation {
enc8 = 32;
}
output += alphabet.charAt(enc1) + alphabet.charAt(enc2) + alphabet.charAt(enc3) +
alphabet.charAt(enc4) + alphabet.charAt(enc5) + alphabet.charAt(enc6) +
alphabet.charAt(enc7) + alphabet.charAt(enc8);
// Preserve original charAt() behavior:
// out-of-range indexes return ""
output +=
(alphabetChars[enc1] || "") +
(alphabetChars[enc2] || "") +
(alphabetChars[enc3] || "") +
(alphabetChars[enc4] || "") +
(alphabetChars[enc5] || "") +
(alphabetChars[enc6] || "") +
(alphabetChars[enc7] || "") +
(alphabetChars[enc8] || "");
}
return output;
}