lib/encoding/base64: Hare base64/base64url on io-streaming surface

Rewrite the buffer-based base64 placeholder as a faithful port of
ref/hare/encoding/base64/base64.ha over the just-landed io-streaming
surface (mirrors lib/encoding/hex).

Ships: std_encoding/url_encoding (module-level `def` consts; decmap
trailing 0xff run spelled out, no '...', to stay on #251 and avoid the
#250 repeat-fill sugar); the streaming encoder newencoder/encode/
encodeslice/encodestr with a padding closer wired into the inline
vtable; encodedsize/decodedsize; and decodestr as a direct in-memory
decode via decmap (the same divergence hex took for its direct path —
its return union carries errors.invalid, unconstrained by io.error).

Deferred (at-site notes): the streaming decoder newdecoder/decode_reader
(#247-sibling, blocked on #199b — io.error lacks errors.invalid).

clear() wipes the work buffers with explicit full-length slices
(`[0:len(...)]`) rather than Hare's bare-array decay (pending #258
[N]T->[]T coercion) to preserve the whole-array hygiene wipe.

base64 graduates off 900_stdlib (cross-module refs resolve only via
driver concatenation, as hex did); coverage at 984_base64_run over the
RFC 4648 §10 vectors for std and url.
This commit is contained in:
2026-06-02 04:55:08 +09:00
parent ca8c78e97d
commit 4d0d3b58e6
3 changed files with 566 additions and 312 deletions

View File

@@ -1,184 +1,423 @@
// encoding/base64 — RFC 4648 base64 encode/decode, buffer-based.
// encoding/base64 — RFC 4648 base64 / base64url over the io-streaming
// surface. Port of ref/hare/encoding/base64/base64.ha.
//
// Mirrors Hare's encoding::base64 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 '+' and '/' for indexes 62 and 63 (the RFC 4648 §4
// alphabet); url uses '-' and '_' (the §5 url-safe alphabet). Both
// pad encoded output with '=' to a multiple of 4 bytes.
// invalid — input was not well-formed base64 (bad char, wrong length,
// padding error). Payload is the byte index of the first offending
// position. Matches Hare's errors::invalid pairing with strconv.
// The streaming DECODER (newdecoder/decode_reader, ref base64.ha:363,
// 375) is DEFERRED — project #247-sibling, blocked on #199b. Hare's
// decode_reader returns errors::invalid on malformed input, which fits
// Hare's io::error (it spreads ...errors::error). ww's io.error
// (lib/io/types.ww:55) does NOT carry errors.invalid (the #199b
// deferral) and io.read's (size | eof | error) can't propagate it
// either, so a base64 decoder *stream* cannot faithfully report invalid
// through io.read until #199b lands. decodestr ships as a direct
// transform meanwhile (its own return union carries errors.invalid; it
// is not io.error-constrained) — the same divergence
// lib/encoding/hex/hex.ww:105-141 took for its direct path.
package base64;
export type invalid = !i32;
import bytes;
import errors;
import io;
import memio;
import strings;
// 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) / 3 + 1) * 4;
// ref/hare/encoding/base64/base64.ha:12.
def PADDING: u8 = '=';
// ref/hare/encoding/base64/base64.ha:14-17.
export type encoding = struct {
encmap: [64]u8,
decmap: [128]u8,
};
// decodedsize — upper bound on the number of bytes decoded from `n`
// encoded bytes. The exact count depends on padding; callers consult
// the i32 returned by `decode`.
export fn decodedsize(n: i32) i32 = {
return (n / 4) * 3;
// std_encoding — the standard RFC 4648 §4 alphabet.
// ref/hare/encoding/base64/base64.ha:20-44. Module-level `def` (ww has
// no `const`; same shape as math.f64info), addressed as
// &base64.std_encoding by callers (cross-module address-of-def, #149).
// encmap char-lits and decmap int-lits narrow to u8 via #251. The
// decmap trailing 0xff run (Hare's `0xff...` at :42) is SPELLED OUT (no
// `...`): Hare's explicit entries run to index 122 (0x33), then 0xff
// fills 123-127 — written here verbatim to stay byte-identical to
// base64.ha:42 while avoiding the un-implemented #250 repeat-fill sugar.
export def std_encoding: encoding = encoding {
encmap = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',
'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y',
'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y',
'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'],
decmap = [
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0x3e, 0xff, 0xff, 0xff, 0x3f,
0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b,
0x3c, 0x3d, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16,
0x17, 0x18, 0x19, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30,
0x31, 0x32, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff,
],
};
// encchar — map a 6-bit value to its alphabet character. `urlsafe`
// chooses '-'/'_' instead of '+'/'/' for 62/63.
fn encchar(v: u8, urlsafe: bool) u8 = {
if (v < 26u8) { return v + 65u8; }; // 'A' + v
if (v < 52u8) { return v + 71u8; }; // 'a' + (v - 26) = v + 71
if (v < 62u8) { return v - 4u8; }; // '0' + (v - 52) = v - 4
if (v == 62u8) {
if (urlsafe) { return 45u8; }; // '-'
return 43u8; // '+'
};
if (urlsafe) { return 95u8; }; // '_'
return 47u8; // '/'
// url_encoding — the RFC 4648 §5 "base64url" alphabet ('-'/'_' for
// 62/63), suitable for URLs and file paths.
// ref/hare/encoding/base64/base64.ha:48-72. decmap trailing 0xff run
// spelled out (no '...', #250) — byte-identical to base64.ha:70.
export def url_encoding: encoding = encoding {
encmap = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',
'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y',
'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y',
'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', '_'],
decmap = [
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0x3e, 0xff, 0xff,
0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b,
0x3c, 0x3d, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16,
0x17, 0x18, 0x19, 0xff, 0xff, 0xff, 0xff, 0x3f,
0xff, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30,
0x31, 0x32, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff,
],
};
// decchar — inverse of encchar. Returns 0..63 on success or 255 on
// invalid char. '=' is handled in the decode loop, not here.
fn decchar(c: u8, urlsafe: bool) u8 = {
if (c >= 65u8) { if (c <= 90u8) { return c - 65u8; }; }; // 'A'..'Z'
if (c >= 97u8) { if (c <= 122u8) { return c - 71u8; }; }; // 'a'..'z'
if (c >= 48u8) { if (c <= 57u8) { return c + 4u8; }; }; // '0'..'9'
if (urlsafe) {
if (c == 45u8) { return 62u8; }; // '-'
if (c == 95u8) { return 63u8; }; // '_'
} else {
if (c == 43u8) { return 62u8; }; // '+'
if (c == 47u8) { return 63u8; }; // '/'
};
return 255u8;
// encoder — a write-only io stream that base64-encodes writes before
// forwarding them to `out`. ref/hare/encoding/base64/base64.ha:110-118.
// `vt` at offset 0 for the intrusive stream→io.stream cast (&e.vt) and
// the callbacks' reverse `s: *encoder` cast — same shape as memio.stream
// / hex.encoder. Hare keeps a shared const encoder_vtable (base64.ha:120)
// + a `stream` field; ww embeds the vtable INLINE and wires the
// writer/closer slots post-construction (memio/hex convention). ibuf
// buffers the in-progress 3-byte group across writes; obuf holds the
// 4-char encoded group still to be flushed.
export type encoder = struct {
vt: io.vtable,
out: io.handle,
enc: *encoding,
ibuf: [3]u8,
obuf: [4]u8,
iavail: u8,
oavail: u8,
};
// encodeinto — encode `src` into `dst` using the std (`urlsafe=false`)
// or url-safe (`urlsafe=true`) alphabet. `dst` must hold at least
// encodedsize(src.len) bytes. Returns the number of bytes written.
fn encodeinto(dst: []u8, src: []u8, urlsafe: bool) i32 = {
// newencoder — wire an encoder over `out`. After writing, [[encode]] /
// [[io.close]] must run to flush the final partial group with '='
// padding. ref/hare/encoding/base64/base64.ha:131-141. Returns BY VALUE
// (memio/hex constructor convention); the caller passes &enc.vt to the
// io dispatchers.
export fn newencoder(enc: *encoding, out: io.handle) encoder = {
let r: encoder;
r.vt.writer = (&encode_writer): *io.writer;
r.vt.closer = (&encode_closer): *io.closer;
r.out = out;
r.enc = enc;
r.iavail = 0u8;
r.oavail = 0u8;
return r;
};
// encode_writer — the encoder's io.writer slot.
// ref/hare/encoding/base64/base64.ha:143-175. Fills ibuf to a full
// 3-byte group, encodes it into obuf, and drains obuf to `out`; a
// trailing partial group stays buffered in ibuf for the next write (or
// the closer). Returns the count of *input* bytes consumed (= len(in)
// on success, since the partial tail is buffered, mirroring Hare).
fn encode_writer(s: io.stream, in: []u8) (size | io.error) = {
let e: *encoder = s: *encoder;
let i: i32 = 0;
let j: i32 = 0;
for (i + 2 < src.len) {
let b0: u8 = src[i];
let b1: u8 = src[i + 1];
let b2: u8 = src[i + 2];
dst[j] = encchar(b0 >> 2u8, urlsafe);
dst[j + 1] = encchar(((b0 & 3u8) << 4u8) | (b1 >> 4u8), urlsafe);
dst[j + 2] = encchar(((b1 & 15u8) << 2u8) | (b2 >> 6u8), urlsafe);
dst[j + 3] = encchar(b2 & 63u8, urlsafe);
i += 3;
j += 4;
};
let rem: i32 = src.len - i;
if (rem == 1) {
let b0: u8 = src[i];
dst[j] = encchar(b0 >> 2u8, urlsafe);
dst[j + 1] = encchar((b0 & 3u8) << 4u8, urlsafe);
dst[j + 2] = 61u8; // '='
dst[j + 3] = 61u8; // '='
j += 4;
};
if (rem == 2) {
let b0: u8 = src[i];
let b1: u8 = src[i + 1];
dst[j] = encchar(b0 >> 2u8, urlsafe);
dst[j + 1] = encchar(((b0 & 3u8) << 4u8) | (b1 >> 4u8), urlsafe);
dst[j + 2] = encchar((b1 & 15u8) << 2u8, urlsafe);
dst[j + 3] = 61u8; // '='
j += 4;
};
return j;
};
// encode — encode `src` into `dst` using the std alphabet. Returns
// the number of bytes written. `dst` must hold at least
// encodedsize(src.len) bytes.
export fn encode(dst: []u8, src: []u8) i32 = {
return encodeinto(dst, src, false);
};
// encodeurl — same as encode but uses the url-safe alphabet ('-'/'_'
// for 62/63).
export fn encodeurl(dst: []u8, src: []u8) i32 = {
return encodeinto(dst, src, true);
};
// decodeinto — decode base64 `src` into `dst`. `dst` must hold at
// least decodedsize(src.len) bytes. Returns the number of bytes
// written, or invalid with the offending source index.
fn decodeinto(dst: []u8, src: []u8, urlsafe: bool) (i32 | invalid) = {
if (src.len == 0) { return 0; };
if ((src.len & 3) != 0) { return src.len: invalid; };
let i: i32 = 0;
let j: i32 = 0;
let end: i32 = src.len;
for (i < end) {
let c0: u8 = src[i];
let c1: u8 = src[i + 1];
let c2: u8 = src[i + 2];
let c3: u8 = src[i + 3];
let v0: u8 = decchar(c0, urlsafe);
let v1: u8 = decchar(c1, urlsafe);
if (v0 == 255u8) { return i: invalid; };
if (v1 == 255u8) { return (i + 1): invalid; };
// Last quad may carry '=' padding.
if (i + 4 == end) {
if (c2 == 61u8) {
// "XX=="
if (c3 != 61u8) { return (i + 3): invalid; };
dst[j] = (v0 << 2u8) | (v1 >> 4u8);
j += 1;
i += 4;
return j;
};
let v2: u8 = decchar(c2, urlsafe);
if (v2 == 255u8) { return (i + 2): invalid; };
if (c3 == 61u8) {
// "XXX="
dst[j] = (v0 << 2u8) | (v1 >> 4u8);
dst[j + 1] = (v1 << 4u8) | (v2 >> 2u8);
j += 2;
i += 4;
return j;
};
let v3: u8 = decchar(c3, urlsafe);
if (v3 == 255u8) { return (i + 3): invalid; };
dst[j] = (v0 << 2u8) | (v1 >> 4u8);
dst[j + 1] = (v1 << 4u8) | (v2 >> 2u8);
dst[j + 2] = (v2 << 6u8) | v3;
j += 3;
i += 4;
return j;
for (i < in.len) {
for (e.iavail < 3u8 && i < in.len) {
e.ibuf[e.iavail] = in[i];
i += 1;
e.iavail += 1u8;
};
if (e.iavail != 3u8) {
return i: size;
};
fillobuf(e);
match (writeavail(e)) {
case let er: io.error => {
if (i == 0) { return er; };
return i: size;
};
case void => void;
};
let v2: u8 = decchar(c2, urlsafe);
let v3: u8 = decchar(c3, urlsafe);
if (v2 == 255u8) { return (i + 2): invalid; };
if (v3 == 255u8) { return (i + 3): invalid; };
dst[j] = (v0 << 2u8) | (v1 >> 4u8);
dst[j + 1] = (v1 << 4u8) | (v2 >> 2u8);
dst[j + 2] = (v2 << 6u8) | v3;
i += 4;
j += 3;
};
return j;
return i: size;
};
// decodedecode std-alphabet base64 from `src` into `dst`. Returns
// the count of decoded bytes, or invalid on a malformed input.
export fn decode(dst: []u8, src: []u8) (i32 | invalid) = {
return decodeinto(dst, src, false);
// fillobuf — encode the full 3-byte ibuf group into the 4-char obuf.
// ref/hare/encoding/base64/base64.ha:177-187.
fn fillobuf(e: *encoder) void = {
let b0: u8 = e.ibuf[0];
let b1: u8 = e.ibuf[1];
let b2: u8 = e.ibuf[2];
e.obuf[0] = e.enc.encmap[b0 >> 2u8];
e.obuf[1] = e.enc.encmap[((b0 & 0x3u8) << 4u8) | (b1 >> 4u8)];
e.obuf[2] = e.enc.encmap[((b1 & 0xfu8) << 2u8) | (b2 >> 6u8)];
e.obuf[3] = e.enc.encmap[b2 & 0x3fu8];
e.oavail = 4u8;
};
// decodeurl — same as decode but accepts the url-safe alphabet.
export fn decodeurl(dst: []u8, src: []u8) (i32 | invalid) = {
return decodeinto(dst, src, true);
// writeavail — drain the encoded obuf tail to `out`.
// ref/hare/encoding/base64/base64.ha:189-202. Loops over io.write to
// absorb partial writes; clears iavail once obuf is fully drained.
fn writeavail(e: *encoder) (void | io.error) = {
if (e.oavail == 0u8) {
return;
};
let olen: i32 = len(e.obuf): i32;
for (e.oavail > 0u8) {
let start: i32 = olen - (e.oavail: i32);
match (io.write(e.out, e.obuf[start : olen])) {
case let n: size => e.oavail -= n: u8;
case let er: io.error => return er;
};
};
if (e.oavail == 0u8) {
e.iavail = 0u8;
};
};
// encode_closer — flush pending writes, padding the final partial group
// with '='. ref/hare/encoding/base64/base64.ha:205-242. Hare guards the
// final clear() behind a `defer if (finished)`; ww has no defer, so
// clear() is called explicitly on each success path and SKIPPED on the
// error returns (matching Hare's finished-only semantics).
fn encode_closer(s: io.stream) (void | io.error) = {
let e: *encoder = s: *encoder;
if (e.oavail > 0u8) {
for (e.oavail > 0u8) {
match (writeavail(e)) {
case let er: io.error => return er;
case void => void;
};
};
clear(e);
return;
};
if (e.iavail == 0u8) {
clear(e);
return;
};
// input length was not a multiple of 3 — pad the group.
// 0 1 2
let npa: [3]u8 = [0u8, 2u8, 1u8];
let np: u8 = npa[e.iavail];
for (e.iavail < 3u8) {
e.ibuf[e.iavail] = 0u8;
e.iavail += 1u8;
};
fillobuf(e);
let olast: i32 = (len(e.obuf): i32) - 1;
let npi: i32 = np: i32;
let k: i32 = 0;
for (k < npi) {
e.obuf[olast - k] = PADDING;
k += 1;
};
for (e.oavail > 0u8) {
match (writeavail(e)) {
case let er: io.error => return er;
case void => void;
};
};
clear(e);
return;
};
// clear — zero the work buffers after a flush.
// ref/hare/encoding/base64/base64.ha:244-247. Hare passes the bare
// arrays (`bytes::zero(e.ibuf)`) which array-decay to a WHOLE-array
// slice — a hygiene wipe of every byte. ww has no implicit [N]T->[]T
// coercion yet (#258), so the slices are spelled EXPLICIT and
// FULL-LENGTH (`[0:len(...)]`, length via len() per rule-13) to
// preserve Hare's whole-array wipe; a windowed slice would leave stale
// tail bytes (a behavioral divergence).
fn clear(e: *encoder) void = {
bytes.zero(e.ibuf[0 : len(e.ibuf)]);
bytes.zero(e.obuf[0 : len(e.obuf)]);
};
// encodeslice — encode `in` and return a fresh byte slice of base64
// ASCII. ref/hare/encoding/base64/base64.ha:271-289. Hare returns
// ([]u8 | nomem) and the caller frees; ww drops nomem per the memio
// rule-9 carve-out (memio.dynamic has no nomem path) and leaks the
// backing buffer (no-GC, process-exit reclaims) — same carve-out as
// hex.encodestr.
export fn encodeslice(enc: *encoding, in: []u8) []u8 = {
let out: memio.stream = memio.dynamic();
let e: encoder = newencoder(enc, &out.vt);
match (io.write(&e.vt, in)) {
case let n: size => void;
case let er: io.error => abort("base64.encodeslice: dynamic memio write failed");
};
match (io.close(&e.vt)) {
case void => void;
case let er: io.error => abort("base64.encodeslice: encoder close failed");
};
return memio.buffer(&out);
};
// encode — encode `buf` and write it to `out`, returning the number of
// input bytes encoded (i.e. len(buf)).
// ref/hare/encoding/base64/base64.ha:293-307. encode_writer consumes the
// whole slice in one call (the partial tail is buffered), so a single
// io.write replaces Hare's io::writeall (ww has no io.writeall —
// cf hex.ww:80-88); io.close then flushes the padded tail.
export fn encode(out: io.handle, enc: *encoding, buf: []u8) (size | io.error) = {
let e: encoder = newencoder(enc, out);
match (io.write(&e.vt, buf)) {
case let z: size => {
match (io.close(&e.vt)) {
case void => return z;
case let er: io.error => return er;
};
};
case let er: io.error => {
clear(&e);
return er;
};
};
};
// encodestr — encode `in` and return it as a base64 string.
// ref/hare/encoding/base64/base64.ha:311-313. Hare uses the validating
// strings::fromutf8; ww uses strings.frombytes (pure reinterpret) per
// the CLAUDE.md rule-9 carve-out — base64 output is ASCII by
// construction. Leaks the backing buffer as encodeslice does.
export fn encodestr(enc: *encoding, in: []u8) str = {
return strings.frombytes(encodeslice(enc, in));
};
// decodestr — decode a string of ASCII base64 into a byte slice.
// ref/hare/encoding/base64/base64.ha:499-501. Hare's decodestr defers to
// decodeslice (base64.ha:470), which decodes by copying through a
// newdecoder stream + io::copy; ww decodes DIRECTLY via enc.decmap — the
// streaming decoder is deferred to #247 (see file header), so there is
// no newdecoder to route through, and decodestr's own return union
// carries errors.invalid (it is not io.error-constrained). Same
// documented divergence as hex.ww:105-141. Hare also returns nomem;
// ww drops it (the alloc `!` aborts on OOM, as hex.decodestr).
//
// The decode mirrors Hare's decode_reader validation
// (base64.ha:424-441): length must be a multiple of 4; '=' padding is
// permitted only as the final 1-2 chars of the last quad (np>2 or
// embedded '=' → invalid); every data char must be ASCII with a non-0xff
// decmap entry.
export fn decodestr(enc: *encoding, in: str) ([]u8 | errors.invalid) = {
let b: []u8 = strings.toutf8(in);
let n: i32 = b.len;
if (n == 0) {
let empty: []u8;
empty.ptr = nil;
empty.len = 0;
return empty;
};
if ((n & 3) != 0) {
let er: errors.invalid;
return er;
};
// trailing '=' padding: at most 2, only in the final quad.
let np: i32 = 0;
if (b[n - 1] == PADDING) {
np = 1;
if (b[n - 2] == PADDING) {
np = 2;
};
};
// validate the data region (everything before the trailing pad):
// ascii alphabet only, no embedded '='.
let datalen: i32 = n - np;
let vi: i32 = 0;
for (vi < datalen) {
let c: u8 = b[vi];
if (c >= 128u8) {
let er: errors.invalid;
return er;
};
if (c == PADDING) {
let er: errors.invalid;
return er;
};
if (enc.decmap[c] == 0xffu8) {
let er: errors.invalid;
return er;
};
vi += 1;
};
let outlen: i32 = (n / 4) * 3 - np;
if (outlen == 0) {
let empty: []u8;
empty.ptr = nil;
empty.len = 0;
return empty;
};
let out: []u8 = alloc([], outlen: u64)!;
let nquads: i32 = n / 4;
let q: i32 = 0;
let di: i32 = 0;
for (q < nquads) {
let base: i32 = q * 4;
let v0: u8 = enc.decmap[b[base]];
let v1: u8 = enc.decmap[b[base + 1]];
if (q == nquads - 1 && np > 0) {
out[di] = (v0 << 2u8) | (v1 >> 4u8);
di += 1;
if (np == 1) {
let v2: u8 = enc.decmap[b[base + 2]];
out[di] = (v1 << 4u8) | (v2 >> 2u8);
di += 1;
};
} else {
let v2: u8 = enc.decmap[b[base + 2]];
let v3: u8 = enc.decmap[b[base + 3]];
out[di] = (v0 << 2u8) | (v1 >> 4u8);
out[di + 1] = (v1 << 4u8) | (v2 >> 2u8);
out[di + 2] = (v2 << 6u8) | v3;
di += 3;
};
q += 1;
};
out.len = di;
return out;
};
// encodedsize — bytes required to base64-encode `sz` source bytes,
// including '=' padding. ref/hare/encoding/base64/base64.ha:591.
export fn encodedsize(sz: i32) i32 = {
if (sz == 0) {
return 0;
};
return ((sz - 1) / 3 + 1) * 4;
};
// decodedsize — maximal decoded length for `sz` encoded bytes (the true
// length is up to 2 bytes shorter, depending on padding). `sz` must be a
// multiple of 4. ref/hare/encoding/base64/base64.ha:596-599.
export fn decodedsize(sz: i32) i32 = {
return sz / 4 * 3;
};