| | @@ -385,6 +385,96 @@ |
| 385 | 385 | return v, true; |
| 386 | 386 | end; |
| 387 | 387 | |
| 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 | + |
| 388 | 478 | (* n in decimal, as Out.int writes it: its bytes, the first of the 20, |
| 389 | 479 | and how many. *) |
| 390 | 480 | proc fromInt*(n: I64): ([20]U8, INT); |
| | @@ -627,6 +717,66 @@ |
| 627 | 717 | if d[0..n] # "9223372036854775807" then Rt.stop("Str.fromInt: the largest"); end; |
| 628 | 718 | end; |
| 629 | 719 | |
| 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 | + |
| 630 | 780 | (* The module's test: stops the program if something is wrong. *) |
| 631 | 781 | proc main*; |
| 632 | 782 | var buf: [3]U8; t: []U8; ok: BOOL; |
| | @@ -652,6 +802,7 @@ |
| 652 | 802 | testUtf8; |
| 653 | 803 | testToInt; |
| 654 | 804 | testNumbers; |
| 805 | + testBase64; |
| 655 | 806 | testOrder; |
| 656 | 807 | end; |
| 657 | 808 | |