raz / commit 66c926e6

commit
66c926e642efa0315cf706c30bf30cec1af48f41
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
31c6ebe1
Str: toU64, fromC and toC, base64With and unbase64With (Base64Url, unpadded); Prop.decimal is Str.toU64

patch · log

4 files changed, +282 -76

doc/raz-lib.md (history) modified +21 -2

@@ -667,6 +667,12 @@
667667 proc chars*(s: []U8): INT; (* the characters in s, as decode reads them *)
668668 proc toInt*(s: []U8): (I64, BOOL); (* s, in decimal; false if it isn't a number an I64 holds *)
669669 proc toHex*(s: []U8): (U64, BOOL); (* s, in hexadecimal; false if it isn't a number a U64 holds *)
670+proc toU64*(s: []U8): (U64, BOOL); (* s, decimal digits only; false if it isn't one, or a U64 can't hold it *)
671+proc fromC*(p: ^U8): []U8; (* the bytes at p up to its zero byte, not copied; "" for nil *)
672+proc toC*(s, out: []U8): ^U8; (* s copied into out with a zero byte after it, which holds LEN(s) + 1 bytes at least: PTR(out) *)
673+const Base64*, Base64Url*; (* the digits of RFC 4648, sections 4 and 5 *)
674+proc base64With*(s, out, alphabet: []U8; pad: BOOL): INT; (* s in base64 with alphabet's 64 digits into out, which holds base64Size(LEN(s)) bytes at least, padded with = if pad: the bytes written *)
675+proc unbase64With*(s, out, alphabet: []U8; pad: BOOL): (INT, BOOL); (* s, base64 as base64With wrote it, into out, which holds (LEN(s) + 3) div 4 * 3 bytes at least: the bytes written; false if s is not that *)
670676 proc base64Size*(n: INT): INT; (* the bytes base64 makes of n bytes: 4 for each 3, or part of 3 *)
671677 proc base64*(s, out: []U8): INT; (* s in base64 into out, which holds base64Size(LEN(s)) bytes at least: the bytes written *)
672678 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 *)
@@ -838,9 +844,22 @@
838844 and nothing else. `toHex` reads hexadecimal digits, capitals or not,
839845 and nothing else: no sign, `0x` or `H`. `fromInt` writes a number as
840846 `Out.int` does, with a `-` if it is negative, its bytes the first of
841-the 20 it gives.
847+the 20 it gives. `toU64` reads a decimal number as `toInt` does, but
848+digits only (no sign), up to the largest `U64`: what `Fmt.uint` writes.
849+
850+`fromC` and `toC` are the two ends of a call to C. `fromC` makes a
851+slice of the bytes at a pointer up to its zero byte, without copying
852+them; `nil` gives `""`. `toC` copies `s` into `out`, puts a zero after
853+it and gives `PTR(out)`, so a call reads `C.realpath(Str.toC(path,
854+room), ...)`. `out` holds `LEN(s) + 1` bytes at least; a shorter one
855+stops the program, as any slice does.
842856
843-`base64` writes `s` as RFC 4648 (section 4) does: the digits `A` to
857+`base64With` and `unbase64With` take the 64 digits as `alphabet` and
858+say with `pad` whether `=` pads a last group: `Base64Url` is section
859+5's, with `-` and `_`, and `Base64` section 4's. Without padding a last
860+group has 2 or 3 digits, and a length 1 more than a multiple of 4 is
861+no base64. `base64` and `unbase64` are `Base64` with padding. In
862+detail, `base64` writes `s` as RFC 4648 (section 4) does: the digits `A` to
844863 `Z`, `a` to `z`, `0` to `9`, `+` and `/`, and `=` to pad a last group
845864 of 1 or 2 bytes to four. `unbase64` reads that and nothing else: its
846865 length a multiple of 4, `=` only as the last one or two bytes, and the

lib/Prop.raz (history) modified +2 -22

@@ -288,20 +288,6 @@
288288 until not better or (runs >= Budget);
289289 end;
290290
291-(* s as a decimal U64, and true; false if it is not one. *)
292-proc decimal(s: []U8): (U64, BOOL);
293-var v, d: U64; i: INT;
294-begin
295- if LEN(s) = 0 then return 0, false; end;
296- for i := 0 to LEN(s) - 1 do
297- if not Str.isDigit(s[i]) then return 0, false; end;
298- d := U64(s[i] - '0');
299- if v > (0FFFFFFFFFFFFFFFFH - d) div 10 then return 0, false; end;
300- v := v * 10 + d;
301- end;
302- return v, true;
303-end;
304-
305291 (* Stops the program: the environment variable called key is not a
306292 number, as it must be. *)
307293 proc badEnv(key: []U8);
@@ -326,7 +312,7 @@
326312 var v: U64; ok: BOOL;
327313 begin
328314 if LEN(Sys.env("PROP_SEED")) = 0 then return Rand.seed(); end;
329- v, ok := decimal(Sys.env("PROP_SEED"));
315+ v, ok := Str.toU64(Sys.env("PROP_SEED"));
330316 if not ok then badEnv("PROP_SEED"); end;
331317 return v;
332318 end;
@@ -526,16 +512,10 @@
526512
527513 (* Small private procedures. *)
528514 proc testHelpers;
529-var v: U64; ok: BOOL; h: [16]U8;
515+var h: [16]U8;
530516 begin
531517 h := hex(0123456789ABCDEFH);
532518 Check.str("hex", h, "0123456789abcdef");
533- v, ok := decimal("18446744073709551615");
534- Check.ok(ok and (v = 0FFFFFFFFFFFFFFFFH), "decimal largest");
535- v, ok := decimal("18446744073709551616");
536- Check.ok(not ok, "decimal too big");
537- v, ok := decimal("12a");
538- Check.ok(not ok, "decimal not digits");
539519 Check.ok(simpler("ab", "abc") and simpler("ab", "ac") and not simpler("ab", "ab"), "simpler");
540520 end;
541521

lib/Str.raz (history) modified +216 -37

@@ -385,7 +385,11 @@
385385 return v, true;
386386 end;
387387
388-const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; (* RFC 4648 section 4's base64 digits *)
388+const
389+ (* The digits of RFC 4648: section 4's base64, and section 5's, which
390+ has - and _ for + and / and is safe in a URL or a file name. *)
391+ Base64* = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
392+ Base64Url* = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
389393
390394 (* The bytes base64 makes of n bytes: 4 for each 3, or part of 3. *)
391395 proc base64Size*(n: INT): INT;
@@ -393,11 +397,12 @@
393397 return (n + 2) div 3 * 4;
394398 end;
395399
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;
400+(* s in base64 into out, which holds base64Size(LEN(s)) bytes at least;
401+ a shorter out stops the program at the byte it lacks, as any slice
402+ does. alphabet holds the 64 digits (Base64 or Base64Url, or one's
403+ own). A last group of 1 or 2 bytes is padded with = to four if pad,
404+ and else has 2 or 3 digits. The bytes written. *)
405+proc base64With*(s, out, alphabet: []U8; pad: BOOL): INT;
401406 var i, k, v, left: INT;
402407 begin
403408 k := 0;
@@ -409,48 +414,93 @@
409414 if left > 2 then v := v | INT(s[i + 2]); end;
410415 out[k] := alphabet[v shr 18];
411416 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;
417+ INC(k, 2);
418+ if left > 1 then
419+ out[k] := alphabet[(v shr 6) & 63];
420+ INC(k);
421+ elsif pad then
422+ out[k] := '=';
423+ INC(k);
424+ end;
425+ if left > 2 then
426+ out[k] := alphabet[v & 63];
427+ INC(k);
428+ elsif pad then
429+ out[k] := '=';
430+ INC(k);
431+ end;
414432 INC(i, 3);
415- INC(k, 4);
416433 end;
417434 return k;
418435 end;
419436
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;
437+(* s in base64 (RFC 4648, section 4), padded, into out as base64With
438+ writes it. *)
439+proc base64*(s, out: []U8): INT;
422440 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;
441+ return base64With(s, out, Base64, true);
442+end;
443+
444+(* c's value as a digit of alphabet, 0 to 63, or -1 if it isn't one; = is -1. *)
445+proc base64Val(c: U8; alphabet: []U8): INT;
446+var i: INT;
447+begin
448+ for i := 0 to 63 do
449+ if alphabet[i] = c then return i; end;
450+ end;
428451 return -1;
429452 end;
430453
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;
454+(* The last 2 or 3 digits of an unpadded s, at s, decoded into out:
455+ the bytes written, 1 or 2; or -1 if a digit isn't one, or the bits
456+ that no byte uses aren't 0. *)
457+proc unbase64Tail(s, out, alphabet: []U8): INT;
458+var a, b, c: INT;
459+begin
460+ a := base64Val(s[0], alphabet);
461+ b := base64Val(s[1], alphabet);
462+ if (a < 0) or (b < 0) then return -1; end;
463+ out[0] := U8(((a shl 6) | b) shr 4);
464+ if LEN(s) = 2 then
465+ if b & 15 # 0 then return -1; end; (* b's low 4 bits belong to no byte *)
466+ return 1;
467+ end;
468+ c := base64Val(s[2], alphabet);
469+ if (c < 0) or (c & 3 # 0) then return -1; end; (* c's low 2 bits belong to no byte *)
470+ out[1] := U8((((a shl 12) | (b shl 6) | c) shr 2) & 255);
471+ return 2;
472+end;
473+
474+(* s, base64 with the digits of alphabet, decoded into out, which holds
475+ (LEN(s) + 3) div 4 * 3 bytes at least; a shorter out stops the
476+ program at the byte it lacks, as any slice does. The bytes written,
477+ and true; or 0 and false, with out left in no state to use, if s is
478+ not base64 as the RFC writes it and in no other way. With pad: its
479+ length a multiple of 4, every byte a digit but the last one or two,
480+ which may be = (and then both of the last two are, or the last
481+ alone). Without: no =, and a length that is not 1 more than a
482+ multiple of 4. Either way the bits of the last digit that no byte
483+ uses are 0, so a byte string has the one spelling that base64 gives
484+ it (RFC 4648, section 3.5 allows a decoder to take that). Line
485+ breaks, and padding or its lack, are the caller's to mend first. *)
486+proc unbase64With*(s, out, alphabet: []U8; pad: BOOL): (INT, BOOL);
487+var i, k, a, b, c, d, v, whole, n: INT; last: BOOL;
444488 begin
445- if LEN(s) mod 4 # 0 then return 0, false; end;
489+ whole := LEN(s); (* the bytes in groups of 4 *)
490+ if pad then
491+ if LEN(s) mod 4 # 0 then return 0, false; end;
492+ else
493+ whole := LEN(s) div 4 * 4;
494+ if LEN(s) mod 4 = 1 then return 0, false; end;
495+ end;
446496 k := 0;
447497 i := 0;
448- while i < LEN(s) do
498+ while i < whole do
449499 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]);
500+ a := base64Val(s[i], alphabet);
501+ b := base64Val(s[i + 1], alphabet);
502+ c := base64Val(s[i + 2], alphabet);
503+ d := base64Val(s[i + 3], alphabet);
454504 if (a < 0) or (b < 0) then return 0, false; end;
455505 v := (a shl 6) | b;
456506 out[k] := U8(v shr 4);
@@ -460,21 +510,68 @@
460510 if d >= 0 then
461511 out[k + 2] := U8(((v shl 6) | d) & 255);
462512 INC(k, 3);
463- elsif last and (s[i + 3] = '=') and (c & 3 = 0) then
513+ elsif pad and last and (s[i + 3] = '=') and (c & 3 = 0) then
464514 INC(k, 2); (* "xxx=": c's low 2 bits belong to no byte *)
465515 else
466516 return 0, false;
467517 end;
468- elsif last and (s[i + 2] = '=') and (s[i + 3] = '=') and (b & 15 = 0) then
518+ elsif pad and last and (s[i + 2] = '=') and (s[i + 3] = '=') and (b & 15 = 0) then
469519 INC(k); (* "xx==": b's low 4 bits belong to no byte *)
470520 else
471521 return 0, false;
472522 end;
473523 INC(i, 4);
474524 end;
525+ if whole < LEN(s) then
526+ n := unbase64Tail(s[whole..LEN(s)], out[k..LEN(out)], alphabet);
527+ if n < 0 then return 0, false; end;
528+ INC(k, n);
529+ end;
475530 return k, true;
476531 end;
477532
533+(* s, base64 (RFC 4648, section 4), padded, decoded into out as
534+ unbase64With reads it; out holds LEN(s) div 4 * 3 bytes at least. *)
535+proc unbase64*(s, out: []U8): (INT, BOOL);
536+begin
537+ return unbase64With(s, out, Base64, true);
538+end;
539+
540+(* s as a decimal number, digits only; false if it isn't one, or a U64
541+ can't hold it. *)
542+proc toU64*(s: []U8): (U64, BOOL);
543+var v, d: U64; i: INT;
544+begin
545+ if LEN(s) = 0 then return 0, false; end;
546+ for i := 0 to LEN(s) - 1 do
547+ if not isDigit(s[i]) then return 0, false; end;
548+ d := U64(s[i] - '0');
549+ if v > (0FFFFFFFFFFFFFFFFH - d) div 10 then return 0, false; end;
550+ v := v * 10 + d;
551+ end;
552+ return v, true;
553+end;
554+
555+(* The bytes at p up to its zero byte, not copied; "" for nil. *)
556+proc fromC*(p: ^U8): []U8;
557+var n: INT;
558+begin
559+ if p = nil then return ""; end;
560+ while p[n] # 0 do INC(n); end;
561+ return p[0..n];
562+end;
563+
564+(* s copied into out with a zero byte after it, for C: out holds
565+ LEN(s) + 1 bytes at least, or the program stops at the byte it
566+ lacks, as any slice does. The pointer C is given, PTR(out). *)
567+proc toC*(s, out: []U8): ^U8;
568+var i: INT;
569+begin
570+ for i := 0 to LEN(s) - 1 do out[i] := s[i]; end;
571+ out[LEN(s)] := 0;
572+ return PTR(out);
573+end;
574+
478575 (* n in decimal, as Out.int writes it: its bytes, the first of the 20,
479576 and how many. *)
480577 proc fromInt*(n: I64): ([20]U8, INT);
@@ -697,6 +794,20 @@
697794 proc testNumbers;
698795 var u: U64; ok: BOOL; d: [20]U8; n: INT;
699796 begin
797+ u, ok := toU64("18446744073709551615");
798+ if not ok or (u # 0FFFFFFFFFFFFFFFFH) then Rt.stop("Str.toU64: the largest"); end;
799+ u, ok := toU64("18446744073709551616");
800+ if ok then Rt.stop("Str.toU64: past the largest"); end;
801+ u, ok := toU64("0042");
802+ if not ok or (u # 42) then Rt.stop("Str.toU64: 0042"); end;
803+ u, ok := toU64("");
804+ if ok then Rt.stop("Str.toU64: empty"); end;
805+ u, ok := toU64("+1");
806+ if ok then Rt.stop("Str.toU64: +1"); end;
807+ u, ok := toU64("-1");
808+ if ok then Rt.stop("Str.toU64: -1"); end;
809+ u, ok := toU64("12a");
810+ if ok then Rt.stop("Str.toU64: 12a"); end;
700811 u, ok := toHex("ff");
701812 if not ok or (u # 255) then Rt.stop("Str.toHex: ff"); end;
702813 u, ok := toHex("FFFFFFFFFFFFFFFF");
@@ -717,6 +828,22 @@
717828 if d[0..n] # "9223372036854775807" then Rt.stop("Str.fromInt: the largest"); end;
718829 end;
719830
831+(* fromC and toC. *)
832+proc testC;
833+var room: [4]U8; c: [4]U8; p: ^U8;
834+begin
835+ if LEN(fromC(nil)) # 0 then Rt.stop("Str.fromC: nil"); end;
836+ c := "abc\x00";
837+ if fromC(ADR(c[0])) # "abc" then Rt.stop("Str.fromC: abc"); end;
838+ c[0] := 0;
839+ if LEN(fromC(ADR(c[0]))) # 0 then Rt.stop("Str.fromC: empty"); end;
840+ room := "xxxx";
841+ p := toC("abc", room);
842+ if (p # ADR(room[0])) or (room[0..3] # "abc") or (room[3] # 0) then Rt.stop("Str.toC: abc"); end;
843+ p := toC("", room);
844+ if room[0] # 0 then Rt.stop("Str.toC: empty"); end;
845+end;
846+
720847 (* One of the RFC 4648 section 10 vectors: plain codes to coded and back. *)
721848 proc testVector(plain, coded: []U8);
722849 var buf: [16]U8; n: INT; ok: BOOL;
@@ -749,6 +876,56 @@
749876 end;
750877 end;
751878
879+(* plain in the other alphabet and unpadded, and back; in the padded
880+ form too, with the - and _ of base64url. *)
881+proc testUrlVector(plain, bare, padded: []U8);
882+var buf: [16]U8; n: INT; ok: BOOL;
883+begin
884+ n := base64With(plain, buf[0..base64Size(LEN(plain))], Base64Url, false);
885+ if buf[0..n] # bare then Rt.stop("Str.base64With: unpadded"); end;
886+ n, ok := unbase64With(bare, buf[0..(LEN(bare) + 3) div 4 * 3], Base64Url, false);
887+ if not ok or (buf[0..n] # plain) then Rt.stop("Str.unbase64With: unpadded"); end;
888+ n := base64With(plain, buf[0..base64Size(LEN(plain))], Base64Url, true);
889+ if buf[0..n] # padded then Rt.stop("Str.base64With: padded"); end;
890+ n, ok := unbase64With(padded, buf[0..LEN(padded) div 4 * 3], Base64Url, true);
891+ if not ok or (buf[0..n] # plain) then Rt.stop("Str.unbase64With: padded"); end;
892+end;
893+
894+(* s must not decode, unpadded, in base64url's alphabet. *)
895+proc testUrlBad(s: []U8);
896+var buf: [16]U8; ok: BOOL;
897+begin
898+ _, ok := unbase64With(s, buf[0..(LEN(s) + 3) div 4 * 3], Base64Url, false);
899+ if ok then Rt.stop("Str.unbase64With: took a bad string"); end;
900+end;
901+
902+(* RFC 4648 section 5's alphabet, padded and not. *)
903+proc testBase64Url;
904+var buf: [8]U8; ok: BOOL;
905+begin
906+ testUrlVector("", "", "");
907+ testUrlVector("f", "Zg", "Zg==");
908+ testUrlVector("fo", "Zm8", "Zm8=");
909+ testUrlVector("foo", "Zm9v", "Zm9v");
910+ testUrlVector("foob", "Zm9vYg", "Zm9vYg==");
911+ testUrlVector("fooba", "Zm9vYmE", "Zm9vYmE=");
912+ testUrlVector("foobar", "Zm9vYmFy", "Zm9vYmFy");
913+ testUrlVector("\xfb\xff", "-_8", "-_8=");
914+ testUrlBad("Z"); (* one digit over *)
915+ testUrlBad("Zm9vY");
916+ testUrlBad("Zg=="); (* padding, unpadded *)
917+ testUrlBad("Zm8=");
918+ testUrlBad("Zm+v"); (* base64's digit *)
919+ testUrlBad("Zm/v");
920+ testUrlBad("Zh"); (* the low 4 bits of h are not 0 *)
921+ testUrlBad("Zm9"); (* the low 2 bits of 9 are not 0 *)
922+ testUrlBad("Zm9v\n");
923+ _, ok := unbase64With("Zg", buf[0..3], Base64, true); (* padding left off *)
924+ if ok then Rt.stop("Str.unbase64With: padding left off"); end;
925+ _, ok := unbase64With("Zm-v", buf[0..3], Base64, true);
926+ if ok then Rt.stop("Str.unbase64With: base64url's digit in base64"); end;
927+end;
928+
752929 proc testBase64;
753930 begin
754931 testVector("", "");
@@ -775,6 +952,7 @@
775952 testBad("Zm9="); (* the low 2 bits of 9 are not 0 *)
776953 testBad("Zm8=Zm9v"); (* padding, then another group *)
777954 testBase64Round;
955+ testBase64Url;
778956 end;
779957
780958 (* The module's test: stops the program if something is wrong. *)
@@ -802,6 +980,7 @@
802980 testUtf8;
803981 testToInt;
804982 testNumbers;
983+ testC;
805984 testBase64;
806985 testOrder;
807986 end;

test/doc/Str.expect (history) modified +43 -15

@@ -127,27 +127,55 @@
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+const Base64* = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
131+ The digits of RFC 4648: section 4's base64, and section 5's, which
132+ has - and _ for + and / and is safe in a URL or a file name.
133+
134+const Base64Url* = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
135+
130136 proc base64Size*(n: INT): INT;
131137 The bytes base64 makes of n bytes: 4 for each 3, or part of 3.
132138
139+proc base64With*(s, out, alphabet: []U8; pad: BOOL): INT;
140+ s in base64 into out, which holds base64Size(LEN(s)) bytes at least;
141+ a shorter out stops the program at the byte it lacks, as any slice
142+ does. alphabet holds the 64 digits (Base64 or Base64Url, or one's
143+ own). A last group of 1 or 2 bytes is padded with = to four if pad,
144+ and else has 2 or 3 digits. The bytes written.
145+
133146 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.
147+ s in base64 (RFC 4648, section 4), padded, into out as base64With
148+ writes it.
138149
150+proc unbase64With*(s, out, alphabet: []U8; pad: BOOL): (INT, BOOL);
151+ s, base64 with the digits of alphabet, decoded into out, which holds
152+ (LEN(s) + 3) div 4 * 3 bytes at least; a shorter out stops the
153+ program at the byte it lacks, as any slice does. The bytes written,
154+ and true; or 0 and false, with out left in no state to use, if s is
155+ not base64 as the RFC writes it and in no other way. With pad: its
156+ length a multiple of 4, every byte a digit but the last one or two,
157+ which may be = (and then both of the last two are, or the last
158+ alone). Without: no =, and a length that is not 1 more than a
159+ multiple of 4. Either way the bits of the last digit that no byte
160+ uses are 0, so a byte string has the one spelling that base64 gives
161+ it (RFC 4648, section 3.5 allows a decoder to take that). Line
162+ breaks, and padding or its lack, are the caller's to mend first.
163+
139164 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.
165+ s, base64 (RFC 4648, section 4), padded, decoded into out as
166+ unbase64With reads it; out holds LEN(s) div 4 * 3 bytes at least.
167+
168+proc toU64*(s: []U8): (U64, BOOL);
169+ s as a decimal number, digits only; false if it isn't one, or a U64
170+ can't hold it.
171+
172+proc fromC*(p: ^U8): []U8;
173+ The bytes at p up to its zero byte, not copied; "" for nil.
174+
175+proc toC*(s, out: []U8): ^U8;
176+ s copied into out with a zero byte after it, for C: out holds
177+ LEN(s) + 1 bytes at least, or the program stops at the byte it
178+ lacks, as any slice does. The pointer C is given, PTR(out).
151179
152180 proc fromInt*(n: I64): ([20]U8, INT);
153181 n in decimal, as Out.int writes it: its bytes, the first of the 20,