Fix base32 unicode alphabet (#2380)

This commit is contained in:
loki1205 2026-07-03 16:09:33 +05:30 committed by GitHub
parent 740d350306
commit eccaf723f5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 110 additions and 4 deletions

View File

@ -43,7 +43,14 @@ 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,
@ -74,10 +81,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;
}

View File

@ -65,6 +65,42 @@ TestRegister.addApiTests([
assert.strictEqual(result.toString(), "493e8136b759370a415ef2cf2f7a69690441ff86592aba082bc2e2e0");
}),
it("Composable Dish: toBase32 should support non-BMP Unicode alphabets", () => {
const alphabet = "🀇🀈🀉🀊🀋🀌🀍🀎🀏🀙🀚🀛🀜🀝🀞🀟🀠🀡🀐🀑🀒🀓🀔🀕🀖🀗🀘🀀🀁🀂🀃🀅";
const result = new Dish("hello")
.apply(toBase32, {alphabet})
.toString();
// Should not contain replacement characters
assert.equal(result.includes("<22>"), false);
// Should contain only symbols from the alphabet
for (const ch of Array.from(result)) {
assert.ok(Array.from(alphabet).includes(ch));
}
// "hello" => 8 Base32 symbols
assert.equal(Array.from(result).length, 8);
}),
it("Composable Dish: toBase32 should omit padding for 32-character Unicode alphabets", () => {
const alphabet = "🀇🀈🀉🀊🀋🀌🀍🀎🀏🀙🀚🀛🀜🀝🀞🀟🀠🀡🀐🀑🀒🀓🀔🀕🀖🀗🀘🀀🀁🀂🀃🀅";
const result = new Dish("hell")
.apply(toBase32, {alphabet})
.toString();
// Should not leak undefined from array indexing
assert.equal(result.includes("undefined"), false);
// Should not contain replacement characters
assert.equal(result.includes("<22>"), false);
// Unpadded Base32 output for 4-byte input should be 7 symbols
assert.equal(Array.from(result).length, 7);
}),
it("Dish translation: ArrayBuffer to ArrayBuffer", () => {
const dish = new Dish(new ArrayBuffer(10), 4);
dish.get("array buffer");

View File

@ -109,6 +109,38 @@ TestRegister.addApiTests([
assert.equal(3 + result, 35);
}),
it("toBase32: should support non-BMP Unicode alphabets", () => {
const alphabet = "🀇🀈🀉🀊🀋🀌🀍🀎🀏🀙🀚🀛🀜🀝🀞🀟🀠🀡🀐🀑🀒🀓🀔🀕🀖🀗🀘🀀🀁🀂🀃🀅";
const result = chef.toBase32("hello", {alphabet}).toString();
// Should not contain replacement characters
assert.equal(result.includes("<22>"), false);
// Should contain only symbols from the alphabet
for (const ch of Array.from(result)) {
assert.ok(Array.from(alphabet).includes(ch));
}
// "hello" => 8 Base32 symbols
assert.equal(Array.from(result).length, 8);
}),
it("toBase32: should omit padding for 32-character Unicode alphabets", () => {
const alphabet = "🀇🀈🀉🀊🀋🀌🀍🀎🀏🀙🀚🀛🀜🀝🀞🀟🀠🀡🀐🀑🀒🀓🀔🀕🀖🀗🀘🀀🀁🀂🀃🀅";
const result = chef.toBase32("hell", {alphabet}).toString();
// Should not leak undefined from array indexing
assert.equal(result.includes("undefined"), false);
// Should not contain replacement characters
assert.equal(result.includes("<22>"), false);
// Unpadded Base32 output for 4-byte input should be 7 symbols
assert.equal(Array.from(result).length, 7);
}),
it("chef.help: should exist", () => {
assert(chef.help);
}),

View File

@ -172,5 +172,27 @@ TestRegister.addTests([
},
],
},
{
name: "To Base32: should support non-BMP Unicode alphabets",
input: "hello",
expectedOutput: "🀝🀈🀐🀔🀖🀀🀊🀟",
recipeConfig: [
{
op: "To Base32",
args: ["🀇🀈🀉🀊🀋🀌🀍🀎🀏🀙🀚🀛🀜🀝🀞🀟🀠🀡🀐🀑🀒🀓🀔🀕🀖🀗🀘🀀🀁🀂🀃🀅"],
},
],
},
{
name: "To Base32: should omit padding for 32-character Unicode alphabets",
input: "hell",
expectedOutput: "🀝🀈🀐🀔🀖🀀🀇",
recipeConfig: [
{
op: "To Base32",
args: ["🀇🀈🀉🀊🀋🀌🀍🀎🀏🀙🀚🀛🀜🀝🀞🀟🀠🀡🀐🀑🀒🀓🀔🀕🀖🀗🀘🀀🀁🀂🀃🀅"],
},
],
},
]);