Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omtls/blob/main/src/b64.om # omtls omtls: a TLS 1.3 client (RFC 8446) written fresh in seeded Omega, for mailhand. Not audited by a cryptographer. Free (Apache-2.0). Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omtls / src / b64.om` 112 lines ยท 2573 bytes Raw Blame History 1 note SPDX-License-Identifier: Apache-2.0 2 note b64.om (ot_b64_): strict base64 decoding (RFC 4648 section 4) of bytes in the store ot_der_m. 3 note Whitespace (space, tab, CR, LF) between characters is skipped, as RFC 7468 section 3 lets a PEM 4 note body wrap lines. Everything else is refused: a character outside the alphabet, a total count 5 note that is not a multiple of four, "=" anywhere but the last one or two places of the last group, 6 note non-zero pad bits (RFC 4648 3.5: refused here), and any character after the padded group. 7 note Output may overlap the input as long as o <= p: each group of four input characters writes at 8 note most three bytes, so the write position never passes the read position. 9 note Unit order: src/der.om must come earlier in the unit (a global is declared before its first use). 10 11 note ot_b64_val: 0..63 for an alphabet character, -2 for "=", -3 for whitespace, -1 otherwise 12 fn ot_b64_val c uchar to long 13 if and ge c 65 le c 90 14 ret long sub c 65 15 end 16 if and ge c 97 le c 122 17 ret add long sub c 97 26 18 end 19 if and ge c 48 le c 57 20 ret add long sub c 48 52 21 end 22 if eq c 43 23 ret 62 24 end 25 if eq c 47 26 ret 63 27 end 28 if eq c 61 29 ret -2 30 end 31 if or or eq c 32 eq c 9 or eq c 10 eq c 13 32 ret -3 33 end 34 ret -1 35 end 36 37 note ot_b64_r: decoder state: 0 group bits, 1 characters in the group, 2 "=" seen, 3 write position 38 global ot_b64_r ulong 4 39 40 note ot_b64_grp: one complete group of four: write 3 - pad bytes; false on non-zero pad bits 41 fn ot_b64_grp to bool 42 a = get ot_b64_r 0 43 pd = get ot_b64_r 2 44 w = get ot_b64_r 3 45 if eq pd 1 46 if ne and a 255 0 47 ret false 48 end 49 end 50 if eq pd 2 51 if ne and a 65535 0 52 ret false 53 end 54 end 55 put ot_der_m w uchar shr a 16 56 if lt pd 2 57 put ot_der_m add w 1 uchar and shr a 8 255 58 end 59 if eq pd 0 60 put ot_der_m add w 2 uchar and a 255 61 end 62 put ot_b64_r 3 add w sub 3 pd 63 put ot_b64_r 0 0 64 put ot_b64_r 1 0 65 ret true 66 end 67 68 note ot_b64_dec: decode the characters in store [p, e) to store offset o; the byte count or -1 69 fn ot_b64_dec p ulong e ulong o ulong to long 70 if gt o p 71 ret -1 72 end 73 put ot_b64_r 0 0 74 put ot_b64_r 1 0 75 put ot_b64_r 2 0 76 put ot_b64_r 3 o 77 dn = false 78 i = p 79 while lt i e bound `16777216` 80 v = ot_b64_val get ot_der_m i 81 i = add i 1 82 if ne v -3 83 if or eq v -1 dn 84 ret -1 85 end 86 k = get ot_b64_r 1 87 if eq v -2 88 if lt k 2 89 ret -1 90 end 91 put ot_b64_r 2 add get ot_b64_r 2 1 92 put ot_b64_r 0 shl get ot_b64_r 0 6 93 else 94 if gt get ot_b64_r 2 0 95 ret -1 96 end 97 put ot_b64_r 0 or shl get ot_b64_r 0 6 ulong v 98 end 99 put ot_b64_r 1 add k 1 100 if eq k 3 101 dn = gt get ot_b64_r 2 0 102 if eq ot_b64_grp false 103 ret -1 104 end 105 end 106 end 107 end 108 if ne get ot_b64_r 1 0 109 ret -1 110 end 111 ret long sub get ot_b64_r 3 o 112 end