diff --git a/lib/encoding/base64/base64.ww b/lib/encoding/base64/base64.ww index 4ead8f89..441ec8fb 100644 --- a/lib/encoding/base64/base64.ww +++ b/lib/encoding/base64/base64.ww @@ -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; }; -// decode — decode 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; }; diff --git a/lib/encoding/base64/base64_test.ww b/lib/encoding/base64/base64_test.ww index 83058468..cd0d9bce 100644 --- a/lib/encoding/base64/base64_test.ww +++ b/lib/encoding/base64/base64_test.ww @@ -1,174 +1,187 @@ +// base64_test — exercises lib/encoding/base64's io-streaming surface. +// Run with `out/bin/ww run lib/encoding/base64/base64_test.ww`. Mirrors +// Hare's base64 @test fns (ref/hare/encoding/base64/base64.ha:315,514, +// 601) over the RFC 4648 §10 vectors, table-driven (parallel arrays; +// tuple-row arrays are blocked by #111). Same signalled-then-fail()- +// with-+10 pattern as the rest of the 9xx stdlib tests; non-zero exit +// pinpoints the failing scenario. +// +// The streaming decoder (newdecoder) is deferred (#247-sibling), so the +// decode side is exercised through decodestr only. + package base64; import base64; +import bytes; +import errors; +import io; +import memio; +import os; +import strings; -fn putstr(s: str, into: []u8, off: i32) i32 = { - let i: i32 = 0; - for (i < s.len) { - into[off + i] = s[i]; - i += 1; - }; - return off + s.len; -}; +let signalled: i32 = 0; +fn fail() void = { os.exit(signalled + 10); }; -fn streq(buf: []u8, expect: str) bool = { - if (buf.len != expect.len) { return false; }; +fn streq(a: str, b: str) bool = { + if (a.len != b.len) { return false; }; let i: i32 = 0; - for (i < buf.len) { - if (buf[i] != expect[i]) { return false; }; + for (i < a.len) { + if (a[i] != b[i]) { return false; }; i += 1; }; return true; }; -fn encodevec(input: str, expect: str) void = { - let inbuf: [128]u8; - let outbuf: [128]u8; - let n: i32 = putstr(input, inbuf[0:128], 0); - let m: i32 = base64.encode(outbuf[0:128], inbuf[0:n]); - if (m != expect.len) { let _: i32 = 1/0; }; - if (!streq(outbuf[0:m], expect)) { let _: i32 = 1/0; }; -}; - -@test fn rfc4648_vectors() void = { - encodevec("", ""); - encodevec("f", "Zg=="); - encodevec("fo", "Zm8="); - encodevec("foo", "Zm9v"); - encodevec("foob", "Zm9vYg=="); - encodevec("fooba", "Zm9vYmE="); - encodevec("foobar", "Zm9vYmFy"); -}; - -fn decodevec(input: str, expect: str) void = { - let inbuf: [128]u8; - let outbuf: [128]u8; - let n: i32 = putstr(input, inbuf[0:128], 0); - let r: (i32 | base64.invalid) = base64.decode(outbuf[0:128], inbuf[0:n]); - match (r) { - case let m: i32 => { - if (m != expect.len) { let _: i32 = 1/0; }; - if (!streq(outbuf[0:m], expect)) { let _: i32 = 1/0; }; +// enc_check — encode `raw` two ways (the io.handle sink via base64.encode +// and the string form via base64.encodestr) and assert both equal +// `expect`. ref/hare/encoding/base64/base64.ha:315. +fn enc_check(enc: *base64.encoding, raw: []u8, expect: str) void = { + let out: memio.stream = memio.dynamic(); + match (base64.encode(&out.vt, enc, raw)) { + case let n: size => { if (n: i32 != raw.len) { fail(); }; }; + case let e: io.error => fail(); }; - case let e: base64.invalid => { let _: i32 = 1/0; }; + if (!streq(memio.string(&out), expect)) { fail(); }; + if (!streq(base64.encodestr(enc, raw), expect)) { fail(); }; +}; + +// dec_check — decodestr(`encoded`) must round-trip back to `raw`. +// ref/hare/encoding/base64/base64.ha:514. +fn dec_check(enc: *base64.encoding, encoded: str, raw: []u8) void = { + match (base64.decodestr(enc, encoded)) { + case let b: []u8 => { if (!bytes.equal(b, raw)) { fail(); }; }; + case let e: errors.invalid => fail(); }; }; -@test fn rfc4648_decode() void = { - decodevec("", ""); - decodevec("Zg==", "f"); - decodevec("Zm8=", "fo"); - decodevec("Zm9v", "foo"); - decodevec("Zm9vYg==", "foob"); - decodevec("Zm9vYmE=", "fooba"); - decodevec("Zm9vYmFy", "foobar"); +// inval_check — decodestr(`encoded`) must report errors.invalid. +// ref/hare/encoding/base64/base64.ha:525. +fn inval_check(enc: *base64.encoding, encoded: str) void = { + match (base64.decodestr(enc, encoded)) { + case let b: []u8 => fail(); + case let e: errors.invalid => void; + }; }; -@test fn alphabet_full() void = { - // Round-trip every 6-bit value (0..63) by encoding three bytes that - // expose b0=0x00, b1=AA, b2=FF — the encoded chars depend on all - // four positions including the >>2 path. +// ---- RFC 4648 §10 vectors, encode + decodestr round-trip ---- +// +// Inputs are the prefixes of "foobar". The §10 expected encodings +// contain no '+' / '/', so std and base64url agree on these vectors — +// both alphabets are driven over the same table here; the std-vs-url +// distinctness chars are covered separately by urlsafe_distinct(). + +@test fn rfc4648_std() void = { + let foobar: [6]u8 = ['f', 'o', 'o', 'b', 'a', 'r']; + let exp: [7]str = ["", "Zg==", "Zm8=", "Zm9v", "Zm9vYg==", + "Zm9vYmE=", "Zm9vYmFy"]; let i: i32 = 0; - for (i < 64) { - let bits: u8 = i: u8; - // Construct a triple [bits<<2, 0, 0] so the first encoded - // char encodes `bits`. The other three chars are derivable - // from the remaining bytes; we only check the first here. - let inbuf: [3]u8; - inbuf[0] = bits << 2u8; - inbuf[1] = 0u8; - inbuf[2] = 0u8; - let outbuf: [4]u8; - let m: i32 = base64.encode(outbuf[0:4], inbuf[0:3]); - if (m != 4) { let _: i32 = 1/0; }; - // Decoding back must give us `bits` in the high 6 bits of [0]. - let r: (i32 | base64.invalid) = base64.decode(inbuf[0:3], outbuf[0:4]); - match (r) { - case let n: i32 => { - if (n != 3) { let _: i32 = 1/0; }; - if ((inbuf[0] >> 2u8) != bits) { let _: i32 = 1/0; }; - }; - case let e: base64.invalid => { let _: i32 = 1/0; }; - }; + for (i <= 6) { + enc_check(&base64.std_encoding, foobar[0:i], exp[i]); + dec_check(&base64.std_encoding, exp[i], foobar[0:i]); i += 1; }; }; -@test fn invalid_inputs() void = { - let inbuf: [16]u8; - let outbuf: [16]u8; - // Length not a multiple of 4. - let n: i32 = putstr("abc", inbuf[0:16], 0); - let r1: (i32 | base64.invalid) = base64.decode(outbuf[0:16], inbuf[0:n]); - match (r1) { - case let m: i32 => { let _: i32 = 1/0; }; - case let e: base64.invalid => void; - }; - // Bad char ('@' is not in the std alphabet). - let n2: i32 = putstr("Z@==", inbuf[0:16], 0); - let r2: (i32 | base64.invalid) = base64.decode(outbuf[0:16], inbuf[0:n2]); - match (r2) { - case let m: i32 => { let _: i32 = 1/0; }; - case let e: base64.invalid => void; +@test fn rfc4648_url() void = { + let foobar: [6]u8 = ['f', 'o', 'o', 'b', 'a', 'r']; + let exp: [7]str = ["", "Zg==", "Zm8=", "Zm9v", "Zm9vYg==", + "Zm9vYmE=", "Zm9vYmFy"]; + let i: i32 = 0; + for (i <= 6) { + enc_check(&base64.url_encoding, foobar[0:i], exp[i]); + dec_check(&base64.url_encoding, exp[i], foobar[0:i]); + i += 1; }; }; -@test fn urlsafe_roundtrip() void = { - // Byte sequence chosen so the std alphabet would use '+' and '/', - // while url-safe replaces them with '-' and '_'. 0xFB = 11111011 - // hits index 62 in some quad, and 0xFF hits 63. - let raw: [3]u8; - raw[0] = 0xFBu8; - raw[1] = 0xFFu8; - raw[2] = 0xBFu8; - let std: [8]u8; - let url: [8]u8; - let dec: [3]u8; - let m1: i32 = base64.encode(std[0:8], raw[0:3]); - let m2: i32 = base64.encodeurl(url[0:8], raw[0:3]); - if (m1 != 4) { let _: i32 = 1/0; }; - if (m2 != 4) { let _: i32 = 1/0; }; - // Round-trip both ways. - let r1: (i32 | base64.invalid) = base64.decode(dec[0:3], std[0:m1]); - match (r1) { - case let n: i32 => { - if (n != 3) { let _: i32 = 1/0; }; - if (dec[0] != raw[0]) { let _: i32 = 1/0; }; - if (dec[1] != raw[1]) { let _: i32 = 1/0; }; - if (dec[2] != raw[2]) { let _: i32 = 1/0; }; - }; - case let e: base64.invalid => { let _: i32 = 1/0; }; - }; - let r2: (i32 | base64.invalid) = base64.decodeurl(dec[0:3], url[0:m2]); - match (r2) { - case let n: i32 => { - if (n != 3) { let _: i32 = 1/0; }; - if (dec[0] != raw[0]) { let _: i32 = 1/0; }; - if (dec[1] != raw[1]) { let _: i32 = 1/0; }; - if (dec[2] != raw[2]) { let _: i32 = 1/0; }; - }; - case let e: base64.invalid => { let _: i32 = 1/0; }; - }; +// ---- std vs base64url alphabet distinctness ---- +// +// [0xFB, 0xFF, 0xBF] hits the 62/63 alphabet slots: std emits '+'/'/', +// url emits '-'/'_'. The two encodings must differ, each round-trips +// under its own alphabet, and each is INVALID under the other (std +// decmap marks '-'/'_' 0xff and url marks '+'/'/' 0xff). + +@test fn urlsafe_distinct() void = { + let raw: [3]u8 = [0xFBu8, 0xFFu8, 0xBFu8]; + let s_std: str = base64.encodestr(&base64.std_encoding, raw[0:3]); + let s_url: str = base64.encodestr(&base64.url_encoding, raw[0:3]); + if (streq(s_std, s_url)) { fail(); }; + + dec_check(&base64.std_encoding, s_std, raw[0:3]); + dec_check(&base64.url_encoding, s_url, raw[0:3]); + + // cross-alphabet decode must reject the foreign chars. + inval_check(&base64.std_encoding, s_url); + inval_check(&base64.url_encoding, s_std); }; +// ---- decodestr error cases ---- ref/hare/encoding/base64/base64.ha:525 +// +// Wrong length, bad char, embedded / excess padding all → invalid. + +@test fn decode_invalid() void = { + inval_check(&base64.std_encoding, "Zg"); // not a multiple of 4 + inval_check(&base64.std_encoding, "Z@=="); // '@' not in alphabet + inval_check(&base64.std_encoding, "===="); // all padding + inval_check(&base64.std_encoding, "Zg==Zg=="); // data after padding + inval_check(&base64.std_encoding, "Zm8=Zm8="); // data after padding + inval_check(&base64.std_encoding, "@Zg="); // bad leading char +}; + +// ---- size calc ---- ref/hare/encoding/base64/base64.ha:601 + @test fn sizes() void = { - if (base64.encodedsize(0) != 0) { let _: i32 = 1/0; }; - if (base64.encodedsize(1) != 4) { let _: i32 = 1/0; }; - if (base64.encodedsize(2) != 4) { let _: i32 = 1/0; }; - if (base64.encodedsize(3) != 4) { let _: i32 = 1/0; }; - if (base64.encodedsize(4) != 8) { let _: i32 = 1/0; }; - if (base64.encodedsize(6) != 8) { let _: i32 = 1/0; }; - if (base64.encodedsize(7) != 12) { let _: i32 = 1/0; }; - if (base64.decodedsize(4) != 3) { let _: i32 = 1/0; }; - if (base64.decodedsize(8) != 6) { let _: i32 = 1/0; }; + if (base64.encodedsize(0) != 0) { fail(); }; + if (base64.encodedsize(1) != 4) { fail(); }; + if (base64.encodedsize(2) != 4) { fail(); }; + if (base64.encodedsize(3) != 4) { fail(); }; + if (base64.encodedsize(4) != 8) { fail(); }; + if (base64.encodedsize(10) != 16) { fail(); }; + if (base64.encodedsize(119) != 160) { fail(); }; + if (base64.encodedsize(120) != 160) { fail(); }; + if (base64.encodedsize(121) != 164) { fail(); }; + if (base64.encodedsize(122) != 164) { fail(); }; + if (base64.encodedsize(123) != 164) { fail(); }; + if (base64.decodedsize(0) != 0) { fail(); }; + if (base64.decodedsize(4) != 3) { fail(); }; + if (base64.decodedsize(8) != 6) { fail(); }; + if (base64.decodedsize(160) != 120) { fail(); }; + if (base64.decodedsize(164) != 123) { fail(); }; +}; + +// ---- round-trip every byte value 0..255 (std + url) ---- + +@test fn roundtrip_all_bytes() void = { + let src: [256]u8; + let i: i32 = 0; + for (i < 256) { + src[i] = i: u8; + i += 1; + }; + let s: str = base64.encodestr(&base64.std_encoding, src[0:256]); + match (base64.decodestr(&base64.std_encoding, s)) { + case let b: []u8 => { + if (b.len != 256) { fail(); }; + if (!bytes.equal(b, src[0:256])) { fail(); }; + }; + case let e: errors.invalid => fail(); + }; + let u: str = base64.encodestr(&base64.url_encoding, src[0:256]); + match (base64.decodestr(&base64.url_encoding, u)) { + case let b: []u8 => { + if (b.len != 256) { fail(); }; + if (!bytes.equal(b, src[0:256])) { fail(); }; + }; + case let e: errors.invalid => fail(); + }; }; export fn main() i32 = { - rfc4648_vectors(); - rfc4648_decode(); - alphabet_full(); - invalid_inputs(); - urlsafe_roundtrip(); - sizes(); + signalled = 1; rfc4648_std(); + signalled = 2; rfc4648_url(); + signalled = 3; urlsafe_distinct(); + signalled = 4; decode_invalid(); + signalled = 5; sizes(); + signalled = 6; roundtrip_all_bytes(); return 0; }; diff --git a/test/wcc/900_stdlib.c b/test/wcc/900_stdlib.c index 4c69ac10..2617328c 100644 --- a/test/wcc/900_stdlib.c +++ b/test/wcc/900_stdlib.c @@ -18,7 +18,6 @@ static const char *modules[] = { "lib/path/path.ww", "lib/encoding/utf8/utf8.ww", "lib/encoding/base32/base32.ww", - "lib/encoding/base64/base64.ww", "lib/hash/fnv/fnv.ww", "lib/hash/adler32/adler32.ww", "lib/hash/crc16/crc16.ww", @@ -38,14 +37,17 @@ static const char *modules[] = { * os.strerror (ww folds Hare's sys role into os); lib/strings.iterator * + lib/strings.next reference utf8.decoder / utf8.done; * lib/bytes.tokenize references os.assert + types.I64_MAX/MIN per - * ref/hare/bytes/tokenize.ha:23-24,42-43; lib/encoding/hex graduated - * to Hare's io-streaming surface (references io.handle / fmt.fprint / - * memio.dynamic / strconv / errors.invalid). Coverage lives at + * ref/hare/bytes/tokenize.ha:23-24,42-43; lib/encoding/hex and + * lib/encoding/base64 graduated to Hare's io-streaming surface + * (hex references io.handle / fmt.fprint / memio.dynamic / strconv / + * errors.invalid; base64 references io.handle / memio.dynamic / + * bytes.zero / strings.frombytes / errors.invalid). Coverage lives at * lib/bufio/bufiotest.ww + lib/bytes/bytestest.ww + * lib/errors/errnotest.ww + lib/fmt/fmttest.ww + lib/os/stattest.ww - * + lib/strings/stringstest.ww + lib/encoding/hex/hextest.ww (wired - * at 998_bufio_run.c, 967_bytes_run.c, 902_errno_run.c, 970_fmt_run.c, - * 976_stat_run.c, 966_strings_run.c, 979_hex_run.c), plus the + * + lib/strings/stringstest.ww + lib/encoding/hex/hextest.ww + + * lib/encoding/base64/base64_test.ww (wired at 998_bufio_run.c, + * 967_bytes_run.c, 902_errno_run.c, 970_fmt_run.c, 976_stat_run.c, + * 966_strings_run.c, 979_hex_run.c, 984_base64_run.c), plus the * bufio.scanline / fmt.println e2e rows in test/wcc/700_e2e.c. */ "lib/net/net.ww", NULL