// encoding/base32 — RFC 4648 base32 encode/decode, buffer-based. // // Mirrors Hare's encoding::base32 surface, modulo Hare's stream-based // encoder/decoder. ww ships the in-memory subset only: `encode(dst, // src)` writes the encoded bytes into `dst`, returning the count; // `decode(dst, src)` writes the decoded bytes into `dst`, returning a // count or invalid. // // std uses 'A'-'Z' and '2'-'7' for indexes 0..31 (RFC 4648 §6); hex // uses '0'-'9' and 'A'-'V' (the base32hex alphabet, RFC 4648 §7). // Both pad encoded output with '=' to a multiple of 8 bytes. package base32; export type invalid = !i32; // encodedsize — bytes required to encode `n` source bytes (including // '=' padding). Hare names it the same. export fn encodedsize(n: i32) i32 = { if (n == 0) { return 0; }; return ((n - 1) / 5 + 1) * 8; }; // decodedsize — upper bound on the number of bytes decoded from `n` // encoded bytes. export fn decodedsize(n: i32) i32 = { return (n / 8) * 5; }; // encchar — map a 5-bit value to its alphabet character. `hex` picks // the base32hex alphabet instead of std. fn encchar(v: u8, hex: bool) u8 = { if (hex) { if (v < 10u8) { return v + 48u8; }; // '0' + v return v + 55u8; // 'A' + (v - 10) = v + 55 }; if (v < 26u8) { return v + 65u8; }; // 'A' + v return v + 24u8; // '2' + (v - 26) = v + 24 }; // decchar — inverse of encchar. Returns 0..31 or 255 on invalid char. // '=' is handled in the decode loop, not here. fn decchar(c: u8, hex: bool) u8 = { if (hex) { if (c >= 48u8) { if (c <= 57u8) { return c - 48u8; }; }; // '0'..'9' if (c >= 65u8) { if (c <= 86u8) { return c - 55u8; }; }; // 'A'..'V' return 255u8; }; if (c >= 65u8) { if (c <= 90u8) { return c - 65u8; }; }; // 'A'..'Z' if (c >= 50u8) { if (c <= 55u8) { return c - 24u8; }; }; // '2'..'7' return 255u8; }; fn encgroup(dst: []u8, j: i32, src: []u8, i: i32, n: i32, hex: bool) void = { let b0: u8 = 0u8; let b1: u8 = 0u8; let b2: u8 = 0u8; let b3: u8 = 0u8; let b4: u8 = 0u8; if (n > 0) { b0 = src[i]; }; if (n > 1) { b1 = src[i + 1]; }; if (n > 2) { b2 = src[i + 2]; }; if (n > 3) { b3 = src[i + 3]; }; if (n > 4) { b4 = src[i + 4]; }; dst[j] = encchar(b0 >> 3u8, hex); dst[j + 1] = encchar(((b0 & 7u8) << 2u8) | (b1 >> 6u8), hex); dst[j + 2] = encchar((b1 >> 1u8) & 31u8, hex); dst[j + 3] = encchar(((b1 & 1u8) << 4u8) | (b2 >> 4u8), hex); dst[j + 4] = encchar(((b2 & 15u8) << 1u8) | (b3 >> 7u8), hex); dst[j + 5] = encchar((b3 >> 2u8) & 31u8, hex); dst[j + 6] = encchar(((b3 & 3u8) << 3u8) | (b4 >> 5u8), hex); dst[j + 7] = encchar(b4 & 31u8, hex); // Pad the encoded slots that map past the source bytes. if (n < 5) { // n=1 → 2 chars then 6 '='. n=2 → 4 chars. n=3 → 5. n=4 → 7. let keep: i32 = 2; if (n == 2) { keep = 4; }; if (n == 3) { keep = 5; }; if (n == 4) { keep = 7; }; let k: i32 = keep; for (k < 8) { dst[j + k] = 61u8; k += 1; }; // '=' }; }; fn encodeinto(dst: []u8, src: []u8, hex: bool) i32 = { let i: i32 = 0; let j: i32 = 0; for (i + 5 <= src.len) { encgroup(dst, j, src, i, 5, hex); i += 5; j += 8; }; let rem: i32 = src.len - i; if (rem > 0) { encgroup(dst, j, src, i, rem, hex); j += 8; }; return j; }; // encode — encode `src` into `dst` using the std (RFC 4648 §6) // alphabet. Returns bytes written. `dst` must hold at least // encodedsize(src.len) bytes. export fn encode(dst: []u8, src: []u8) i32 = { return encodeinto(dst, src, false); }; // encodehex — same as encode but uses the base32hex alphabet // (RFC 4648 §7). export fn encodehex(dst: []u8, src: []u8) i32 = { return encodeinto(dst, src, true); }; // padcount — number of bytes encoded in the last group, given the // count `p` of trailing '=' chars. RFC 4648 lists the legal mapping: // 6=>1, 4=>2, 3=>3, 1=>4, 0=>5. Returns -1 if `p` isn't legal. fn padcount(p: i32) i32 = { if (p == 0) { return 5; }; if (p == 1) { return 4; }; if (p == 3) { return 3; }; if (p == 4) { return 2; }; if (p == 6) { return 1; }; return -1; }; fn decodeinto(dst: []u8, src: []u8, hex: bool) (i32 | invalid) = { if (src.len == 0) { return 0; }; if ((src.len & 7) != 0) { return src.len: invalid; }; let i: i32 = 0; let j: i32 = 0; let end: i32 = src.len; for (i < end) { let v: [8]u8; let last: bool = false; let p: i32 = 0; let k: i32 = 0; for (k < 8) { let c: u8 = src[i + k]; if (c == 61u8) { // '=' if (i + 8 != end) { return (i + k): invalid; }; last = true; v[k] = 0u8; p += 1; } else { if (last) { return (i + k): invalid; }; let d: u8 = decchar(c, hex); if (d == 255u8) { return (i + k): invalid; }; v[k] = d; }; k += 1; }; let nb: i32 = 5; if (last) { nb = padcount(p); if (nb < 0) { return (i + 8 - p): invalid; }; }; // First two chars cover byte[0] (5 + 3 bits). if (nb > 0) { dst[j] = (v[0] << 3u8) | (v[1] >> 2u8); }; if (nb > 1) { dst[j + 1] = (v[1] << 6u8) | (v[2] << 1u8) | (v[3] >> 4u8); }; if (nb > 2) { dst[j + 2] = (v[3] << 4u8) | (v[4] >> 1u8); }; if (nb > 3) { dst[j + 3] = (v[4] << 7u8) | (v[5] << 2u8) | (v[6] >> 3u8); }; if (nb > 4) { dst[j + 4] = (v[6] << 5u8) | v[7]; }; j += nb; i += 8; }; return j; }; // decode — decode std-alphabet base32 from `src` into `dst`. Returns // count of decoded bytes, or invalid on malformed input. export fn decode(dst: []u8, src: []u8) (i32 | invalid) = { return decodeinto(dst, src, false); }; // decodehex — same as decode but accepts base32hex. export fn decodehex(dst: []u8, src: []u8) (i32 | invalid) = { return decodeinto(dst, src, true); };