interplane.Get Omega
Read in your language

omnet

aien-dev/omnet

Brought by drakestapleton

branch main Branches Tags History

omnet / om / codec / codec.om

494 lines · 11571 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note codec.om: the text encodings of the Tailnet Lock JSON wire format, matching Go go1.26.0
3note encoding/base64, encoding/base32 and encoding/hex and tailscale.com v1.102.0 byte for byte.
4note Library unit: functions only, no main, no globals. Every name starts with cd.
5note
6note Byte strings are fixed-capacity arrays (view/edit uchar 65536) plus an explicit length.
7note Decoders read untrusted text: a malformed input returns cdbad (65537) or 0, never traps.
8note A length beyond the stated capacity is a caller contract violation: trap 3 before any write.
9note
10note fn cdb64enc s view uchar 65536 n ulong o edit uchar 65536 to ulong
11note base64.StdEncoding.Encode (RFC 4648 4, padded) of s[0..n), n 0..49152; returns the length.
12note fn cdb64dec s view uchar 65536 n ulong o edit uchar 65536 to ulong
13note base64.StdEncoding.Decode of s[0..n), n 0..65536, the decoder encoding/json uses for []byte.
14note Returns the byte count (o[0..count) holds the bytes) or cdbad.
15note fn cdb32enc s view uchar 65536 n ulong o edit uchar 65536 to ulong
16note base32.StdEncoding.WithPadding(NoPadding) encode (RFC 4648 6, no padding), n 0..40960.
17note fn cdb32dec s view uchar 65536 n ulong o edit uchar 65536 to ulong
18note base32.StdEncoding.WithPadding(NoPadding).DecodeString of s[0..n), n 0..65536.
19note fn cdhexenc s view uchar 65536 n ulong o edit uchar 65536 to ulong
20note hex.Encode (lowercase, RFC 4648 8), n 0..32768.
21note fn cdhexdec s view uchar 65536 n ulong o edit uchar 65536 to ulong
22note hex.DecodeString of s[0..n), n 0..65536; returns n/2 or cdbad.
23note fn cdaumenc h view uchar 32 o edit uchar 65536 to ulong
24note tka.AUMHash.MarshalText: 52 base32 characters; returns 52.
25note fn cdaumdec s view uchar 65536 n ulong h edit uchar 32 to ulong
26note tka.AUMHash.UnmarshalText into a zero AUMHash: 1 accepted, 0 rejected (h all zero).
27note fn cdnkenc k view uchar 32 o edit uchar 65536 to ulong
28note key.NodePublic.MarshalText: "nodekey:" and 64 lowercase hex digits; returns 72.
29note fn cdnkdec s view uchar 65536 n ulong k edit uchar 32 to ulong
30note key.NodePublic.UnmarshalText: 1 accepted, 0 rejected (k all zero).
31note
32note On cdbad the output bytes written so far are not cleared; callers discard o.
33note NOT constant-time-audited: Omega gives no timing guarantee and none was measured.
34
35const cdbad ulong 65537
36note "nodekey:" (types/key/node.go nodePublicHexPrefix) as 8 big-endian bytes 6e 6f 64 65 6b 65 79 3a
37const cdnkpfx ulong 7957689453343242554
38
39note base64 value of one character (RFC 4648 table 1), 255 when outside the alphabet
40fn cdb64v c ulong to ulong
41if and ge c 65 le c 90
42ret sub c 65
43end
44if and ge c 97 le c 122
45ret sub c 71
46end
47if and ge c 48 le c 57
48ret add c 4
49end
50if eq c 43
51ret 62
52end
53if eq c 47
54ret 63
55end
56ret 255
57end
58
59note base64 character of a value 0..63 (RFC 4648 table 1)
60fn cdb64c v ulong to uchar
61if lt v 26
62ret uchar add v 65
63end
64if lt v 52
65ret uchar add v 71
66end
67if lt v 62
68ret uchar sub v 4
69end
70if eq v 62
71ret 43
72end
73ret 47
74end
75
76note base32 value of one character (RFC 4648 table 3), 255 outside A-Z 2-7 (lowercase too)
77fn cdb32v c ulong to ulong
78if and ge c 65 le c 90
79ret sub c 65
80end
81if and ge c 50 le c 55
82ret sub c 24
83end
84ret 255
85end
86
87note base32 character of a value 0..31 (RFC 4648 table 3)
88fn cdb32c v ulong to uchar
89if lt v 26
90ret uchar add v 65
91end
92ret uchar add v 24
93end
94
95note hex digit value, either case (hex.go reverseHexTable), 255 otherwise
96fn cdhexv c ulong to ulong
97if and ge c 48 le c 57
98ret sub c 48
99end
100if and ge c 97 le c 102
101ret sub c 87
102end
103if and ge c 65 le c 70
104ret sub c 55
105end
106ret 255
107end
108
109note lowercase hex digit of a value 0..15 (hex.go hextable "0123456789abcdef")
110fn cdhexc v ulong to uchar
111if lt v 10
112ret uchar add v 48
113end
114ret uchar add v 87
115end
116
117note the byte of the two hex digits at s[i], s[i+1], or 256 when either is not a hex digit
118fn cdhexb s view uchar 65536 i ulong to ulong
119a = cdhexv ulong get s i
120b = cdhexv ulong get s add i 1
121if or gt a 15 gt b 15
122ret 256
123end
124ret or shl a 4 b
125end
126
127fn cdzero32 k edit uchar 32 to void
128for i ulong 32
129put k i 0
130end
131end
132
133note m bytes of s from i (m 1..w) as a big-endian w-byte number, missing bytes 0
134fn cdgetbe s view uchar 65536 i ulong m ulong w ulong to ulong
135x = ulong 0
136for t ulong w
137x = shl x 8
138if lt t m
139x = or x ulong get s add i t
140end
141end
142ret x
143end
144
145note the first m bytes of the w-byte big-endian number x at o[q..q+m)
146fn cdputbe o edit uchar 65536 q ulong x ulong m ulong w ulong to void
147for t ulong m
148sh = mul sub sub w 1 t 8
149v = and shr x sh 255
150put o add q t uchar v
151end
152end
153
154note four base64 characters at o[p..p+4) of the 24-bit group x: m sextets, then pad '='
155fn cdb64put o edit uchar 65536 p ulong x ulong m ulong to void
156for t ulong 4
157if lt t m
158put o add p t cdb64c and shr x sub 18 mul t 6 63
159else
160put o add p t 61
161end
162end
163end
164
165note RFC 4648 4: 3 bytes -> 4 characters; a final 1 or 2 bytes -> 2 or 3 characters and "==" or "="
166fn cdb64enc s view uchar 65536 n ulong o edit uchar 65536 to ulong
167if gt n 49152
168trap 3
169end
170f = div n 3
171for k ulong f
172cdb64put o mul k 4 cdgetbe s mul k 3 3 3 4
173end
174r = sub n mul f 3
175p = mul f 4
176if eq r 0
177ret p
178end
179cdb64put o p cdgetbe s mul f 3 r 3 add r 1
180ret add p 4
181end
182
183note base64.StdEncoding.Decode (base64.go 312-407, 518-584). CR and LF are skipped everywhere
184note (base64.go 340, 356-359, 372-375), so the rest must be whole quanta of 4; only the last may end in
185note "xx==" or "xxx=", and after the padding only CR/LF may follow. Non-zero trailing bits are
186note accepted (no Strict, base64.go 394, 401). pd: 0 no '=', 1 one '=' after 2 symbols, 2 done.
187fn cdb64dec s view uchar 65536 n ulong o edit uchar 65536 to ulong
188if gt n 65536
189trap 3
190end
191acc = ulong 0
192j = ulong 0
193q = ulong 0
194pd = ulong 0
195for i ulong n
196c = ulong get s i
197if or eq c 10 eq c 13
198continue
199end
200if eq pd 2
201ret cdbad
202end
203if eq pd 1
204if ne c 61
205ret cdbad
206end
207pd = 2
208continue
209end
210if eq c 61
211if eq j 2
212pd = 1
213continue
214end
215if eq j 3
216pd = 2
217continue
218end
219ret cdbad
220end
221v = cdb64v c
222if gt v 63
223ret cdbad
224end
225acc = or shl acc 6 v
226j = add j 1
227if eq j 4
228cdputbe o q acc 3 3
229q = add q 3
230acc = 0
231j = 0
232end
233end
234if eq pd 1
235ret cdbad
236end
237if eq pd 0
238if ne j 0
239ret cdbad
240end
241ret q
242end
243note pd 2: j is 2 or 3; the j symbols, left-aligned in 24 bits, give j - 1 bytes
244cdputbe o q shl acc mul sub 4 j 6 sub j 1 3
245ret add q sub j 1
246end
247
248note m base32 characters at o[p..) of the 40-bit group x
249fn cdb32put o edit uchar 65536 p ulong x ulong m ulong to void
250for t ulong m
251put o add p t cdb32c and shr x sub 35 mul t 5 31
252end
253end
254
255note RFC 4648 6 without padding (base32.go 121-187, EncodedLen 284-289): 5 bytes -> 8 characters,
256note a final 1, 2, 3 or 4 bytes -> 2, 4, 5 or 7 characters
257fn cdb32enc s view uchar 65536 n ulong o edit uchar 65536 to ulong
258if gt n 40960
259trap 3
260end
261f = div n 5
262for k ulong f
263cdb32put o mul k 8 cdgetbe s mul k 5 5 5 8
264end
265r = sub n mul f 5
266p = mul f 8
267if eq r 0
268ret p
269end
270c = div add mul r 8 4 5
271cdb32put o p cdgetbe s mul f 5 r 5 c
272ret add p c
273end
274
275note base32.go stripNewlines 545-555: copy s[0..n) without CR and LF into t, return the count
276fn cdstrip s view uchar 65536 n ulong t edit uchar 65536 to ulong
277l = ulong 0
278for i ulong n
279c = get s i
280if and ne c 10 ne c 13
281put t l c
282l = add l 1
283end
284end
285ret l
286end
287
288note base32.go 330-349, the padding branch, entered when the byte just read (t[p-1]) is the pad
289note byte, j >= 2 symbols are held and fewer than 8 bytes remain. With NoPadding the pad byte is
290note byte(rune(-1)) = 255. Returns 1 when Go accepts the padding, 0 when it reports an error.
291note Bytes past the 7 - j checked ones are not looked at, as in Go.
292fn cdb32pad t view uchar 65536 p ulong l ulong j ulong to ulong
293rm = sub l p
294if lt add rm j 7
295ret 0
296end
297for k ulong sub 7 j
298if gt rm k
299if ne get t add p k 255
300ret 0
301end
302end
303end
304if or eq j 3 eq j 6
305ret 0
306end
307ret 1
308end
309
310note output bytes of a quantum of dl symbols (base32.go 362-382): 8 7 5 4 2 -> 5 4 3 2 1;
311note 1, 3 and 6 symbols at the end of the input give no byte and no error
312fn cdb32nb dl ulong to ulong
313if eq dl 8
314ret 5
315end
316if eq dl 7
317ret 4
318end
319if eq dl 5
320ret 3
321end
322if eq dl 4
323ret 2
324end
325if eq dl 2
326ret 1
327end
328ret 0
329end
330
331note write the bytes of the quantum d[0..dl) at o[q..); returns the count
332fn cdb32out d view ulong 8 dl ulong o edit uchar 65536 q ulong to ulong
333x = ulong 0
334for i ulong 8
335x = or shl x 5 get d i
336end
337nb = cdb32nb dl
338cdputbe o q x nb 5
339ret nb
340end
341
342note one quantum of base32.go decode (305-358) from t[p..l), p < l, into d[0..dl). Returns
343note p' * 16 + dl, with p' the next read position, or 0 when Go reports an error. Go ends the
344note input (end = true) exactly when dl < 8: at the end of t, or in the padding branch.
345fn cdb32q t view uchar 65536 p ulong l ulong d edit ulong 8 to ulong
346for i ulong 8
347put d i 0
348end
349j = ulong 0
350while lt j 8 bound 9
351if eq p l
352ret add mul p 16 j
353end
354c = ulong get t p
355p = add p 1
356if and eq c 255 and ge j 2 lt sub l p 8
357if eq cdb32pad t p l j 0
358ret 0
359end
360ret add mul p 16 j
361end
362v = cdb32v c
363if gt v 31
364ret 0
365end
366put d j v
367j = add j 1
368end
369ret add mul p 16 8
370end
371
372note base32.go decode 305-386 on the newline-free t[0..l) with padChar = NoPadding
373fn cdb32raw t view uchar 65536 l ulong o edit uchar 65536 to ulong
374d = new ulong 8 0
375p = ulong 0
376q = ulong 0
377while lt p l bound 999999
378r = cdb32q t p l d
379if eq r 0
380ret cdbad
381end
382p = shr r 4
383dl = and r 15
384q = add q cdb32out d dl o q
385if lt dl 8
386ret q
387end
388end
389ret q
390end
391
392note base32.go DecodeString 421-426: strip CR and LF, then decode
393fn cdb32dec s view uchar 65536 n ulong o edit uchar 65536 to ulong
394if gt n 65536
395trap 3
396end
397t = new uchar 65536 0
398l = cdstrip s n t
399ret cdb32raw t l o
400end
401
402note hex.go Encode 45-53, lowercase
403fn cdhexenc s view uchar 65536 n ulong o edit uchar 65536 to ulong
404if gt n 32768
405trap 3
406end
407for i ulong n
408v = ulong get s i
409put o mul i 2 cdhexc shr v 4
410put o add mul i 2 1 cdhexc and v 15
411end
412ret mul n 2
413end
414
415note hex.go DecodeString 138-142 and Decode 87-113: odd length or a non-hex byte is an error
416fn cdhexdec s view uchar 65536 n ulong o edit uchar 65536 to ulong
417if gt n 65536
418trap 3
419end
420if ne and n 1 0
421ret cdbad
422end
423for i ulong div n 2
424v = cdhexb s mul i 2
425if gt v 255
426ret cdbad
427end
428put o i uchar v
429end
430ret div n 2
431end
432
433note tka/aum.go AppendText 44-46: base32StdNoPad of the 32 hash bytes
434fn cdaumenc h view uchar 32 o edit uchar 65536 to ulong
435t = new uchar 65536 0
436for i ulong 32
437put t i get h i
438end
439ret cdb32enc t 32 o
440end
441
442note tka/aum.go UnmarshalText 33-41: DecodedLen(len) must be 32, which holds for len 52 only
443note (52/8*5 + 52%8*5/8 = 32; 51 gives 31, 53 gives 33), counted before CR/LF are stripped.
444note Then Decode into the 32 bytes. Stripping leaves at most 52 symbols, at most 32 bytes.
445fn cdaumdec s view uchar 65536 n ulong h edit uchar 32 to ulong
446cdzero32 h
447if ne n 52
448ret 0
449end
450o = new uchar 65536 0
451r = cdb32dec s n o
452if eq r cdbad
453ret 0
454end
455for i ulong r
456put h i get o i
457end
458ret 1
459end
460
461note types/key/node.go AppendText 324-326 and util.go appendHexKey 56-61
462fn cdnkenc k view uchar 32 o edit uchar 65536 to ulong
463for i ulong 8
464put o i uchar and shr cdnkpfx sub 56 mul i 8 255
465end
466for i ulong 32
467v = ulong get k i
468put o add 8 mul i 2 cdhexc shr v 4
469put o add 9 mul i 2 cdhexc and v 15
470end
471ret 72
472end
473
474note types/key/node.go UnmarshalText 336-338 and util.go parseHex 70-87: exact prefix "nodekey:",
475note then exactly 64 hex digits of either case (fromHexChar 90-101)
476fn cdnkdec s view uchar 65536 n ulong k edit uchar 32 to ulong
477cdzero32 k
478if ne n 72
479ret 0
480end
481x = cdgetbe s 0 8 8
482if ne x cdnkpfx
483ret 0
484end
485for i ulong 32
486v = cdhexb s add 8 mul i 2
487if gt v 255
488cdzero32 k
489ret 0
490end
491put k i uchar v
492end
493ret 1
494end