raz / commit 6603c911

commit
6603c91125da28c5e48bcf95e7aabe6b920ddf24
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
abca7b2f
Str: base64Size, base64 and unbase64, RFC 4648 base64, decoded strictly

As PROPOSALS.md had it, approved by the owner: the caller gives the
room; decoding takes padding at the end only and pad bits of 0 alone
(RFC 4648 3.5), so a byte string has one spelling. Tested by the RFC's
vectors, every rejection and a round trip of all 256 bytes. Breaks no
program: report 2026-10.35, volume 2.

patch · log

5 files changed, +196 -0

PROPOSALS.md modified +5 -0

@@ -1263,6 +1263,11 @@
12631263
12641264 ## Base64 in `Str`
12651265
1266+**Status.** Approved by the owner on 2026-10-22 and implemented in
1267+`lib/Str.raz`, with the interface below; `out` too short stops the
1268+program at the byte it lacks, as any slice does. Decoding also rejects
1269+padding bits that are not 0 (RFC 4648, section 3.5 allows it).
1270+
12661271 Library growth that breaks no program: three procedures in `Str.raz`,
12671272 beside `hexVal` and `toHex`, of base64 as RFC 4648 (4) has it: its
12681273 alphabet, `A` to `Z`, `a` to `z`, `0` to `9`, `+` and `/`, and `=` to

doc/raz-lib.md (history) modified +15 -0

@@ -643,6 +643,9 @@
643643 proc chars*(s: []U8): INT; (* the characters in s, as decode reads them *)
644644 proc toInt*(s: []U8): (I64, BOOL); (* s, in decimal; false if it isn't a number an I64 holds *)
645645 proc toHex*(s: []U8): (U64, BOOL); (* s, in hexadecimal; false if it isn't a number a U64 holds *)
646+proc base64Size*(n: INT): INT; (* the bytes base64 makes of n bytes: 4 for each 3, or part of 3 *)
647+proc base64*(s, out: []U8): INT; (* s in base64 into out, which holds base64Size(LEN(s)) bytes at least: the bytes written *)
648+proc unbase64*(s, out: []U8): (INT, BOOL); (* s, base64, into out, which holds LEN(s) div 4 * 3 bytes at least: the bytes written; false if s is not base64 *)
646649 proc fromInt*(n: I64): ([20]U8, INT); (* n in decimal: its bytes, and how many *)
647650 proc le*(s: []U8): U64; (* s, at most 8 bytes, the first the lowest *)
648651 proc be*(s: []U8): U64; (* s, at most 8 bytes, the first the highest *)
@@ -809,6 +812,18 @@
809812 and nothing else: no sign, `0x` or `H`. `fromInt` writes a number as
810813 `Out.int` does, with a `-` if it is negative, its bytes the first of
811814 the 20 it gives.
815+
816+`base64` writes `s` as RFC 4648 (section 4) does: the digits `A` to
817+`Z`, `a` to `z`, `0` to `9`, `+` and `/`, and `=` to pad a last group
818+of 1 or 2 bytes to four. `unbase64` reads that and nothing else: its
819+length a multiple of 4, `=` only as the last one or two bytes, and the
820+bits of the digit before the padding that no byte uses all 0, so a
821+byte string has the one spelling `base64` gives it. Line breaks,
822+missing padding and the `-` and `_` of base64url are the caller's to
823+mend first; `unbase64` of such a string is false, and what it wrote
824+into `out` is not to be used. The caller gives the room, as `Fmt`
825+does: an `out` shorter than the comment says stops the program, as
826+indexing past a slice does.
812827
813828 `le` and `be` read an unsigned number of up to 8 bytes in a fixed
814829 order, little-endian or big-endian, whatever the machine's own, and

doc/report-history.md (history) modified +3 -0

@@ -227,3 +227,6 @@
227227 integer type left and right by `n` bits modulo its width, and of
228228 constants are a constant (sections 4, 6.1 and 6.8). A predeclared
229229 name may be hidden (section 2), so it breaks no program.
230+ Then `Str` gains `base64Size`, `base64` and `unbase64`, RFC 4648's
231+ base64 with padding, decoded strictly (volume 2). It breaks no
232+ program.

lib/Str.raz (history) modified +151 -0

@@ -385,6 +385,96 @@
385385 return v, true;
386386 end;
387387
388+const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; (* RFC 4648 section 4's base64 digits *)
389+
390+(* The bytes base64 makes of n bytes: 4 for each 3, or part of 3. *)
391+proc base64Size*(n: INT): INT;
392+begin
393+ return (n + 2) div 3 * 4;
394+end;
395+
396+(* s in base64 (RFC 4648, section 4) into out, which holds
397+ base64Size(LEN(s)) bytes at least; a shorter out stops the program
398+ at the byte it lacks, as any slice does. The bytes written. A last
399+ group of 1 or 2 bytes is padded with = to four. *)
400+proc base64*(s, out: []U8): INT;
401+var i, k, v, left: INT;
402+begin
403+ k := 0;
404+ i := 0;
405+ while i < LEN(s) do
406+ left := LEN(s) - i; (* the bytes not yet taken; 3 or more fill a group *)
407+ v := INT(s[i]) shl 16;
408+ if left > 1 then v := v | (INT(s[i + 1]) shl 8); end;
409+ if left > 2 then v := v | INT(s[i + 2]); end;
410+ out[k] := alphabet[v shr 18];
411+ out[k + 1] := alphabet[(v shr 12) & 63];
412+ if left > 1 then out[k + 2] := alphabet[(v shr 6) & 63]; else out[k + 2] := '='; end;
413+ if left > 2 then out[k + 3] := alphabet[v & 63]; else out[k + 3] := '='; end;
414+ INC(i, 3);
415+ INC(k, 4);
416+ end;
417+ return k;
418+end;
419+
420+(* c's value as a base64 digit, 0 to 63, or -1 if it isn't one; = is -1. *)
421+proc base64Val(c: U8): INT;
422+begin
423+ if (c >= 'A') and (c <= 'Z') then return INT(c - 'A'); end;
424+ if (c >= 'a') and (c <= 'z') then return INT(c - 'a') + 26; end;
425+ if (c >= '0') and (c <= '9') then return INT(c - '0') + 52; end;
426+ if c = '+' then return 62; end;
427+ if c = '/' then return 63; end;
428+ return -1;
429+end;
430+
431+(* s, base64 (RFC 4648, section 4), decoded into out, which holds
432+ LEN(s) div 4 * 3 bytes at least; a shorter out stops the program at
433+ the byte it lacks, as any slice does. The bytes written, and true;
434+ or 0 and false, with out left in no state to use, if s is not base64
435+ as the RFC writes it and in no other way: its length a multiple of
436+ 4, every byte a digit but the last one or two, which may be =
437+ (and then both of the last two are, or the last alone), and the bits
438+ of the digit before the padding that no byte uses are 0, so a byte
439+ string has the one spelling that base64 gives it (RFC 4648, section
440+ 3.5 allows a decoder to take that). Line breaks and padding left off
441+ are the caller's to mend first. *)
442+proc unbase64*(s, out: []U8): (INT, BOOL);
443+var i, k, a, b, c, d, v: INT; last: BOOL;
444+begin
445+ if LEN(s) mod 4 # 0 then return 0, false; end;
446+ k := 0;
447+ i := 0;
448+ while i < LEN(s) do
449+ last := i + 4 = LEN(s); (* padding is for the last group only *)
450+ a := base64Val(s[i]);
451+ b := base64Val(s[i + 1]);
452+ c := base64Val(s[i + 2]);
453+ d := base64Val(s[i + 3]);
454+ if (a < 0) or (b < 0) then return 0, false; end;
455+ v := (a shl 6) | b;
456+ out[k] := U8(v shr 4);
457+ if c >= 0 then
458+ v := (v shl 6) | c;
459+ out[k + 1] := U8((v shr 2) & 255);
460+ if d >= 0 then
461+ out[k + 2] := U8(((v shl 6) | d) & 255);
462+ INC(k, 3);
463+ elsif last and (s[i + 3] = '=') and (c & 3 = 0) then
464+ INC(k, 2); (* "xxx=": c's low 2 bits belong to no byte *)
465+ else
466+ return 0, false;
467+ end;
468+ elsif last and (s[i + 2] = '=') and (s[i + 3] = '=') and (b & 15 = 0) then
469+ INC(k); (* "xx==": b's low 4 bits belong to no byte *)
470+ else
471+ return 0, false;
472+ end;
473+ INC(i, 4);
474+ end;
475+ return k, true;
476+end;
477+
388478 (* n in decimal, as Out.int writes it: its bytes, the first of the 20,
389479 and how many. *)
390480 proc fromInt*(n: I64): ([20]U8, INT);
@@ -627,6 +717,66 @@
627717 if d[0..n] # "9223372036854775807" then Rt.stop("Str.fromInt: the largest"); end;
628718 end;
629719
720+(* One of the RFC 4648 section 10 vectors: plain codes to coded and back. *)
721+proc testVector(plain, coded: []U8);
722+var buf: [16]U8; n: INT; ok: BOOL;
723+begin
724+ if base64Size(LEN(plain)) # LEN(coded) then Rt.stop("Str.base64Size"); end;
725+ n := base64(plain, buf[0..base64Size(LEN(plain))]);
726+ if buf[0..n] # coded then Rt.stop("Str.base64: vector"); end;
727+ n, ok := unbase64(coded, buf[0..LEN(coded) div 4 * 3]);
728+ if not ok or (buf[0..n] # plain) then Rt.stop("Str.unbase64: vector"); end;
729+end;
730+
731+(* s must not decode. *)
732+proc testBad(s: []U8);
733+var buf: [16]U8; ok: BOOL;
734+begin
735+ _, ok := unbase64(s, buf[0..LEN(s) div 4 * 3]);
736+ if ok then Rt.stop("Str.unbase64: took a bad string"); end;
737+end;
738+
739+(* Every byte value, in 256 strings of 1 to 256 bytes, goes there and back. *)
740+proc testBase64Round;
741+var plain: [256]U8; back: [258]U8; coded: [344]U8; i, n: INT; ok: BOOL;
742+begin
743+ for i := 0 to 255 do plain[i] := U8(i); end;
744+ for i := 1 to 256 do
745+ n := base64(plain[0..i], coded[0..base64Size(i)]);
746+ if n # base64Size(i) then Rt.stop("Str.base64: size"); end;
747+ n, ok := unbase64(coded[0..base64Size(i)], back[0..base64Size(i) div 4 * 3]);
748+ if not ok or (back[0..n] # plain[0..i]) then Rt.stop("Str.base64: round trip"); end;
749+ end;
750+end;
751+
752+proc testBase64;
753+begin
754+ testVector("", "");
755+ testVector("f", "Zg==");
756+ testVector("fo", "Zm8=");
757+ testVector("foo", "Zm9v");
758+ testVector("foob", "Zm9vYg==");
759+ testVector("fooba", "Zm9vYmE=");
760+ testVector("foobar", "Zm9vYmFy");
761+ testVector("\xfb\xff", "+/8=");
762+ testBad("Zg="); (* length not a multiple of 4 *)
763+ testBad("Zg"); (* padding left off *)
764+ testBad("Zm9v\n"); (* a line break *)
765+ testBad("Zm9!"); (* a byte outside the alphabet *)
766+ testBad("Zm-v"); (* base64url's digit *)
767+ testBad("Zg==Zm9v"); (* padding in the middle *)
768+ testBad("Z=9v"); (* = in a group's second place *)
769+ testBad("=m9v"); (* = in a group's first place *)
770+ testBad("Zm=v"); (* = before a digit *)
771+ testBad("Z==="); (* three pads *)
772+ testBad("====");
773+ testBad("Zg=A"); (* one pad but a digit last *)
774+ testBad("Zh=="); (* the low 4 bits of h are not 0 *)
775+ testBad("Zm9="); (* the low 2 bits of 9 are not 0 *)
776+ testBad("Zm8=Zm9v"); (* padding, then another group *)
777+ testBase64Round;
778+end;
779+
630780 (* The module's test: stops the program if something is wrong. *)
631781 proc main*;
632782 var buf: [3]U8; t: []U8; ok: BOOL;
@@ -652,6 +802,7 @@
652802 testUtf8;
653803 testToInt;
654804 testNumbers;
805+ testBase64;
655806 testOrder;
656807 end;
657808

test/doc/Str.expect (history) modified +22 -0

@@ -127,6 +127,28 @@
127127 s in hexadecimal, digits only, capitals or not; false if it isn't
128128 that, or a U64 can't hold it.
129129
130+proc base64Size*(n: INT): INT;
131+ The bytes base64 makes of n bytes: 4 for each 3, or part of 3.
132+
133+proc base64*(s, out: []U8): INT;
134+ s in base64 (RFC 4648, section 4) into out, which holds
135+ base64Size(LEN(s)) bytes at least; a shorter out stops the program
136+ at the byte it lacks, as any slice does. The bytes written. A last
137+ group of 1 or 2 bytes is padded with = to four.
138+
139+proc unbase64*(s, out: []U8): (INT, BOOL);
140+ s, base64 (RFC 4648, section 4), decoded into out, which holds
141+ LEN(s) div 4 * 3 bytes at least; a shorter out stops the program at
142+ the byte it lacks, as any slice does. The bytes written, and true;
143+ or 0 and false, with out left in no state to use, if s is not base64
144+ as the RFC writes it and in no other way: its length a multiple of
145+ 4, every byte a digit but the last one or two, which may be =
146+ (and then both of the last two are, or the last alone), and the bits
147+ of the digit before the padding that no byte uses are 0, so a byte
148+ string has the one spelling that base64 gives it (RFC 4648, section
149+ 3.5 allows a decoder to take that). Line breaks and padding left off
150+ are the caller's to mend first.
151+
130152 proc fromInt*(n: I64): ([20]U8, INT);
131153 n in decimal, as Out.int writes it: its bytes, the first of the 20,
132154 and how many.