| | @@ -385,7 +385,11 @@ |
| 385 | 385 | return v, true; |
| 386 | 386 | end; |
| 387 | 387 | |
| 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-_"; |
| 389 | 393 | |
| 390 | 394 | (* The bytes base64 makes of n bytes: 4 for each 3, or part of 3. *) |
| 391 | 395 | proc base64Size*(n: INT): INT; |
| | @@ -393,11 +397,12 @@ |
| 393 | 397 | return (n + 2) div 3 * 4; |
| 394 | 398 | end; |
| 395 | 399 | |
| 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; |
| 401 | 406 | var i, k, v, left: INT; |
| 402 | 407 | begin |
| 403 | 408 | k := 0; |
| | @@ -409,48 +414,93 @@ |
| 409 | 414 | if left > 2 then v := v | INT(s[i + 2]); end; |
| 410 | 415 | out[k] := alphabet[v shr 18]; |
| 411 | 416 | 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; |
| 414 | 432 | INC(i, 3); |
| 415 | | - INC(k, 4); |
| 416 | 433 | end; |
| 417 | 434 | return k; |
| 418 | 435 | end; |
| 419 | 436 | |
| 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; |
| 422 | 440 | 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; |
| 428 | 451 | return -1; |
| 429 | 452 | end; |
| 430 | 453 | |
| 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; |
| 444 | 488 | 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; |
| 446 | 496 | k := 0; |
| 447 | 497 | i := 0; |
| 448 | | - while i < LEN(s) do |
| 498 | + while i < whole do |
| 449 | 499 | 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); |
| 454 | 504 | if (a < 0) or (b < 0) then return 0, false; end; |
| 455 | 505 | v := (a shl 6) | b; |
| 456 | 506 | out[k] := U8(v shr 4); |
| | @@ -460,21 +510,68 @@ |
| 460 | 510 | if d >= 0 then |
| 461 | 511 | out[k + 2] := U8(((v shl 6) | d) & 255); |
| 462 | 512 | 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 |
| 464 | 514 | INC(k, 2); (* "xxx=": c's low 2 bits belong to no byte *) |
| 465 | 515 | else |
| 466 | 516 | return 0, false; |
| 467 | 517 | 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 |
| 469 | 519 | INC(k); (* "xx==": b's low 4 bits belong to no byte *) |
| 470 | 520 | else |
| 471 | 521 | return 0, false; |
| 472 | 522 | end; |
| 473 | 523 | INC(i, 4); |
| 474 | 524 | 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; |
| 475 | 530 | return k, true; |
| 476 | 531 | end; |
| 477 | 532 | |
| 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 | + |
| 478 | 575 | (* n in decimal, as Out.int writes it: its bytes, the first of the 20, |
| 479 | 576 | and how many. *) |
| 480 | 577 | proc fromInt*(n: I64): ([20]U8, INT); |
| | @@ -697,6 +794,20 @@ |
| 697 | 794 | proc testNumbers; |
| 698 | 795 | var u: U64; ok: BOOL; d: [20]U8; n: INT; |
| 699 | 796 | 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; |
| 700 | 811 | u, ok := toHex("ff"); |
| 701 | 812 | if not ok or (u # 255) then Rt.stop("Str.toHex: ff"); end; |
| 702 | 813 | u, ok := toHex("FFFFFFFFFFFFFFFF"); |
| | @@ -717,6 +828,22 @@ |
| 717 | 828 | if d[0..n] # "9223372036854775807" then Rt.stop("Str.fromInt: the largest"); end; |
| 718 | 829 | end; |
| 719 | 830 | |
| 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 | + |
| 720 | 847 | (* One of the RFC 4648 section 10 vectors: plain codes to coded and back. *) |
| 721 | 848 | proc testVector(plain, coded: []U8); |
| 722 | 849 | var buf: [16]U8; n: INT; ok: BOOL; |
| | @@ -749,6 +876,56 @@ |
| 749 | 876 | end; |
| 750 | 877 | end; |
| 751 | 878 | |
| 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 | + |
| 752 | 929 | proc testBase64; |
| 753 | 930 | begin |
| 754 | 931 | testVector("", ""); |
| | @@ -775,6 +952,7 @@ |
| 775 | 952 | testBad("Zm9="); (* the low 2 bits of 9 are not 0 *) |
| 776 | 953 | testBad("Zm8=Zm9v"); (* padding, then another group *) |
| 777 | 954 | testBase64Round; |
| 955 | + testBase64Url; |
| 778 | 956 | end; |
| 779 | 957 | |
| 780 | 958 | (* The module's test: stops the program if something is wrong. *) |
| | @@ -802,6 +980,7 @@ |
| 802 | 980 | testUtf8; |
| 803 | 981 | testToInt; |
| 804 | 982 | testNumbers; |
| 983 | + testC; |
| 805 | 984 | testBase64; |
| 806 | 985 | testOrder; |
| 807 | 986 | end; |