interplane.Get Omega
Read in your language

interplane-forge

INTERPLANE Forge

Brought by drakestapleton

branch main Branches Tags History

interplane-forge / tools / bring / src / 35enc.om

217 lines · 3973 bytes Raw Blame History
1note Encodings used by bring/get. SHA-256 is om/tls/sha2.om, composed before this unit.
2text thex "0123456789abcdef"
3text tb64 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
4text tb64u "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"
5text tb58 "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
6text tdidk "did:key:"
7text tproject "project"
8fn hexw d view uchar * o edit uchar * n ulong to ulong
9for i ulong len d bound 9223372036854775807
10put o add n mul i 2 get thex shr get d i 4
11put o add add n mul i 2 1 get thex and get d i 15
12end
13ret add n mul len d 2
14end
15note sha of an open descriptor from its current position to EOF.
16fn shafd fd int d edit uchar 32 to void
17c = new ulong 40 0
18b = new uchar 65536 0
19sha256_init c
20while true bound 9223372036854775807
21r = read fd b 0 len b
22if eq r -4
23continue
24end
25if lt r 0
26die terrio
27end
28if eq r 0
29break
30end
31sha256_update c part b 0 ulong r
32end
33sha256_final c d
34end
35note base64url, no padding. Returns output length.
36fn b64u s view uchar * o edit uchar * to ulong
37n = ulong 0
38i = ulong 0
39while lt i len s bound 9223372036854775807
40k = sub len s i
41w = shl ulong get s i 16
42if gt k 1
43w = or w shl ulong get s add i 1 8
44end
45if gt k 2
46w = or w ulong get s add i 2
47end
48put o n get tb64u and shr w 18 63
49put o add n 1 get tb64u and shr w 12 63
50n = add n 2
51if gt k 1
52put o n get tb64u and shr w 6 63
53n = add n 1
54end
55if gt k 2
56put o n get tb64u and w 63
57n = add n 1
58end
59i = add i 3
60end
61ret n
62end
63fn b64v c uchar to long
64for i ulong 64
65if eq get tb64 i c
66ret long i
67end
68end
69ret -1
70end
71note standard padded base64 decode; returns length or -1.
72fn b64d s view uchar * o edit uchar * to long
73if ne rem len s 4 0
74ret -1
75end
76n = ulong 0
77i = ulong 0
78while lt i len s bound 9223372036854775807
79g = b64grp s i
80if lt g 0
81ret -1
82end
83if and ge g 16777216 lt add i 4 len s
84ret -1
85end
86k = b64emit ulong g o n
87if lt k 0
88ret -1
89end
90n = ulong k
91i = add i 4
92end
93ret long n
94end
95fn b64emit w ulong o edit uchar * n ulong to long
96pad = shr w 24
97k = n
98for j ulong sub 3 pad
99if ge k len o
100ret -1
101end
102sh = sub 16 mul j 8
103put o k uchar and shr w sh 255
104k = add k 1
105end
106ret long k
107end
108note b64grp: one 4-char group at s[i]: the 24 bits, plus the pad count shifted 24; -1 when invalid
109fn b64grp s view uchar * i ulong to long
110w = ulong 0
111pad = ulong 0
112for j ulong 4
113c = get s add i j
114v = long 0
115if eq c 61
116if lt j 2
117ret -1
118end
119pad = add pad 1
120else
121if gt pad 0
122ret -1
123end
124v = b64v c
125if lt v 0
126ret -1
127end
128end
129w = or shl w 6 ulong v
130end
131ret long or and w 16777215 shl pad 24
132end
133note did:key:z + base58btc(0xed 0x01 + 32 bytes): exactly the Radicle ed25519 form.
134fn didkey pk view uchar 32 o edit uchar * to ulong
135x = new uchar 34 0
136put x 0 237
137put x 1 1
138for i ulong 32
139put x add i 2 get pk i
140end
141dg = new uchar 64 0
142dn = ulong 0
143for i ulong 34
144carry = ulong get x i
145for j ulong dn bound 64
146carry = add carry mul ulong get dg j 256
147put dg j uchar rem carry 58
148carry = div carry 58
149end
150while gt carry 0 bound 64
151put dg dn uchar rem carry 58
152dn = add dn 1
153carry = div carry 58
154end
155end
156n = cpy o 0 tdidk
157put o n 122
158n = add n 1
159for i ulong dn bound 64
160put o n get tb58 get dg sub sub dn i 1
161n = add n 1
162end
163ret n
164end
165note form encoding of every byte except unreserved ASCII.
166fn formw s view uchar * o edit uchar * n ulong to ulong
167k = n
168for i ulong len s bound 9223372036854775807
169c = get s i
170if unres c
171put o k c
172k = add k 1
173else
174put o k 37
175put o add k 1 get thex shr c 4
176put o add k 2 get thex and c 15
177k = add k 3
178end
179end
180ret k
181end
182fn unres c uchar to bool
183if isaln c
184ret true
185end
186if or eq c 45 eq c 46
187ret true
188end
189ret or eq c 95 eq c 126
190end
191fn projname folder view uchar * o edit uchar 64 to ulong
192n = ulong 0
193dash = false
194for i ulong len folder bound 9223372036854775807
195c = lcb get folder i
196ok = or and ge c 97 le c 122 isdig c
197if ok
198if and dash gt n 0
199if lt n 63
200put o n 45
201n = add n 1
202end
203end
204dash = false
205if lt n 64
206put o n c
207n = add n 1
208end
209else
210dash = true
211end
212end
213if eq n 0
214n = cpy o 0 tproject
215end
216ret n
217end