interplane.Get Omega
Read in your language

omnet

aien-dev/omnet

Brought by drakestapleton

branch main Branches Tags History

omnet / om / cbor / cbor.om

626 lines · 15005 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note cbor.om: the CBOR (RFC 8949) subset that Tailscale Tailnet Lock (tka, tag v1.102.0) uses.
3note Decoder: accepts and rejects exactly what github.com/fxamacker/cbor/v2 v2.9.0 Wellformed
4note accepts and rejects under tka's cborDecOpts (tka/tka.go 23-32): no tags, no indefinite
5note lengths, at most 16 nested arrays/maps, at most 4096 array elements, at most 1024 map pairs,
6note no bytes after the item. Like fxamacker it is lenient about form: non-shortest heads and
7note unsorted map keys are accepted (the encoder writes the canonical form). Floats and simple
8note values are kept as opaque tokens. Duplicate keys and text UTF-8 are typed checks in
9note fxamacker; cbrdupk and cbrutf8 are the helpers the model layer applies where Go does.
10note Encoder: CTAP2 canonical as cbor.CTAP2EncOptions (encode.go 647-656): shortest heads,
11note definite lengths, map keys sorted bytewise-lexically by their canonical encoding.
12note Library unit: functions only, no main, no globals. Every name starts with cbr.
13note Never traps on input bytes; traps only on caller contract violations (see README).
14note NOT constant-time-audited.
15note
16note Token table t (ulong 131074): token i at t[2i] (argument) and t[2i+1] (packed):
17note bits 0..2 major type, bits 3..7 additional information (0..27),
18note bits 8..24 offset of the head in the input, bits 32..48 index of the token after the subtree.
19note t[131072] = token count, t[131073] = input length. Tokens are in input (pre-)order; the
20note items of an array or map follow its token; a map's items alternate key, value.
21note Error codes of cbrdec (the class of the fxamacker error, in its check order, valid.go):
22note 1 empty input 2 truncated 3 reserved additional information
23note 4 indefinite length 5 break code outside one 6 tag
24note 7 simple value < 32 in two bytes 8 more than 16 nested arrays/maps
25note 9 array over 4096 10 map over 1024 pairs 11 bytes after the item
26note 12 length or count >= 2^63
27
28note argument width in bytes for additional information a: 0 for a < 24, 1 2 4 8 for 24..27,
29note 9 for the reserved 28..30 and the indefinite 31
30fn cbrargw a ulong to ulong
31if lt a 24
32ret 0
33end
34if eq a 24
35ret 1
36end
37if eq a 25
38ret 2
39end
40if eq a 26
41ret 4
42end
43if eq a 27
44ret 8
45end
46ret 9
47end
48
49note big-endian unsigned value of d[p..p+w), w <= 8
50fn cbrbe d view uchar 65536 p ulong w ulong to ulong
51v = ulong 0
52for k ulong w
53v = or shl v 8 ulong get d add p k
54end
55ret v
56end
57
58note error for additional information 28..31 at depth dep (fxamacker valid.go 366-377, 110-114, 126-137)
59fn cbrbadai m ulong a ulong dep ulong to ulong
60if ne a 31
61ret 3
62end
63if eq m 7
64ret 5
65end
66if or eq m 2 eq m 3
67ret 4
68end
69if or eq m 4 eq m 5
70if ge dep 16
71ret 8
72end
73ret 4
74end
75ret 3
76end
77
78note checks after the head (valid.go 108-210): q = offset after the head, dep = open containers
79fn cbrargck n ulong q ulong m ulong v ulong dep ulong to ulong
80if eq m 6
81ret 6
82end
83if or eq m 2 eq m 3
84if gt v 9223372036854775807
85ret 12
86end
87if gt v sub n q
88ret 2
89end
90ret 0
91end
92if or eq m 4 eq m 5
93if ge dep 16
94ret 8
95end
96if gt v 9223372036854775807
97ret 12
98end
99if and eq m 4 gt v 4096
100ret 9
101end
102if and eq m 5 gt v 1024
103ret 10
104end
105end
106ret 0
107end
108
109note argument of a head with additional information a and argument width w at d[p]
110fn cbrhv d view uchar 65536 p ulong a ulong w ulong to ulong
111if eq w 0
112ret a
113end
114ret cbrbe d add p 1 w
115end
116
117fn cbrput t edit ulong 131074 i ulong v ulong x ulong to void
118put t mul i 2 v
119put t add mul i 2 1 x
120end
121
122note parse the head at d[p] into token i (dep containers open); 0 or an error code
123fn cbrhd d view uchar 65536 n ulong p ulong t edit ulong 131074 i ulong dep ulong to ulong
124if ge p n
125ret 2
126end
127m = ulong shr get d p 5
128a = ulong and get d p 31
129w = cbrargw a
130if gt w 8
131ret cbrbadai m a dep
132end
133if gt add add p 1 w n
134ret 2
135end
136v = cbrhv d p a w
137if and eq m 7 and eq a 24 lt v 32
138ret 7
139end
140cbrput t i v or or m shl a 3 shl p 8
141ret cbrargck n add add p 1 w m v dep
142end
143
144note accessors of token i
145fn cbrmaj t view ulong 131074 i ulong to ulong
146ret and get t add mul i 2 1 7
147end
148
149fn cbrai t view ulong 131074 i ulong to ulong
150ret and shr get t add mul i 2 1 3 31
151end
152
153fn cbrval t view ulong 131074 i ulong to ulong
154ret get t mul i 2
155end
156
157fn cbrhoff t view ulong 131074 i ulong to ulong
158ret and shr get t add mul i 2 1 8 131071
159end
160
161fn cbrnext t view ulong 131074 i ulong to ulong
162ret shr get t add mul i 2 1 32
163end
164
165fn cbrcount t view ulong 131074 to ulong
166ret get t 131072
167end
168
169fn cbrinlen t view ulong 131074 to ulong
170ret get t 131073
171end
172
173note offset of the payload of token i (the byte after its head)
174fn cbrpoff t view ulong 131074 i ulong to ulong
175ret add add cbrhoff t i 1 cbrargw cbrai t i
176end
177
178note input offset just after token i's own bytes (head, plus payload for byte and text strings)
179fn cbrtend t view ulong 131074 i ulong to ulong
180m = cbrmaj t i
181if or eq m 2 eq m 3
182ret add cbrpoff t i cbrval t i
183end
184ret cbrpoff t i
185end
186
187note number of items directly inside token i: array elements, or 2 per map pair
188fn cbritems t view ulong 131074 i ulong to ulong
189m = cbrmaj t i
190if eq m 4
191ret cbrval t i
192end
193if eq m 5
194ret mul cbrval t i 2
195end
196ret 0
197end
198
199note input bytes of the subtree of token i
200fn cbrspan t view ulong 131074 i ulong to ulong
201nx = cbrnext t i
202e = cbrinlen t
203if lt nx cbrcount t
204e = cbrhoff t nx
205end
206ret sub e cbrhoff t i
207end
208
209fn cbrsetn t edit ulong 131074 i ulong nx ulong to void
210k = add mul i 2 1
211put t k or get t k shl nx 32
212end
213
214note one item finished: count it off the open containers, closing every one that is now full;
215note returns the new stack depth
216fn cbrpop t edit ulong 131074 sk edit ulong 32 sr edit ulong 32 sp ulong nx ulong to ulong
217s = sp
218while gt s 0 bound 64
219k = sub s 1
220r = sub get sr k 1
221put sr k r
222if gt r 0
223ret s
224end
225cbrsetn t get sk k nx
226s = k
227end
228ret s
229end
230
231note decode d[0..n) into the token table t; 0 on success, else an error code (header notes).
232note n > 65536 is a caller error: trap 3.
233fn cbrdec d view uchar 65536 n ulong t edit ulong 131074 to ulong
234if gt n 65536
235trap 3
236end
237put t 131072 0
238put t 131073 n
239if eq n 0
240ret 1
241end
242sk = new ulong 32 0
243sr = new ulong 32 0
244sp = ulong 0
245i = ulong 0
246p = ulong 0
247while true bound 65537
248e = cbrhd d n p t i sp
249if ne e 0
250ret e
251end
252p = cbrtend t i
253c = cbritems t i
254if gt c 0
255put sk sp i
256put sr sp c
257sp = add sp 1
258else
259cbrsetn t i add i 1
260sp = cbrpop t sk sr sp add i 1
261end
262i = add i 1
263if eq sp 0
264break
265end
266end
267put t 131072 i
268if ne p n
269ret 11
270end
271ret 0
272end
273
274note value token of the first pair whose key is the unsigned integer key in map token m; 0 if none
275note (0 is never a value token). Keys of other types are skipped.
276fn cbrmfind t view ulong 131074 m ulong key ulong to ulong
277k = add m 1
278for j ulong cbrval t m
279v = cbrnext t k
280if and eq cbrmaj t k 0 eq cbrval t k key
281ret v
282end
283k = cbrnext t v
284end
285ret 0
286end
287
288note 1 when byte or text tokens a and b hold the same bytes
289fn cbrbeq d view uchar 65536 t view ulong 131074 a ulong b ulong to ulong
290if ne cbrval t a cbrval t b
291ret 0
292end
293pa = cbrpoff t a
294pb = cbrpoff t b
295for k ulong cbrval t a
296if ne get d add pa k get d add pb k
297ret 0
298end
299end
300ret 1
301end
302
303note integer key i as its int64 bit pattern (decode.go 2768-2784)
304fn cbrkiv t view ulong 131074 i ulong to ulong
305if eq cbrmaj t i 1
306ret not cbrval t i
307end
308ret cbrval t i
309end
310
311note a negative integer key below -2^63 (an error in Go, decode.go 2773-2782)
312fn cbrkbig t view ulong 131074 i ulong to bool
313ret and eq cbrmaj t i 1 gt cbrval t i 9223372036854775807
314end
315
316note 1 when map keys a and b are the same key under fxamacker's duplicate rule (decode.go 2765-2815):
317note integers compare as int64 (so 2^64-1 equals -1, as in Go); a negative integer below -2^63 is a
318note decode error in Go and never equal here; strings compare type and bytes; simple values and
319note floats compare head and argument; arrays and maps are never equal (Go rejects them as keys).
320fn cbrkeq d view uchar 65536 t view ulong 131074 a ulong b ulong to ulong
321ma = cbrmaj t a
322mb = cbrmaj t b
323if and le ma 1 le mb 1
324if or cbrkbig t a cbrkbig t b
325ret 0
326end
327if eq cbrkiv t a cbrkiv t b
328ret 1
329end
330ret 0
331end
332if ne ma mb
333ret 0
334end
335if or eq ma 2 eq ma 3
336ret cbrbeq d t a b
337end
338if and eq ma 7 and eq cbrval t a cbrval t b eq cbrai t a cbrai t b
339ret 1
340end
341ret 0
342end
343
344note 1 when map token m has two keys equal under cbrkeq, else 0
345fn cbrdupk d view uchar 65536 t view ulong 131074 m ulong to ulong
346np = cbrval t m
347k = add m 1
348for j ulong np
349l = cbrnext t cbrnext t k
350for x ulong sub sub np j 1
351if eq cbrkeq d t k l 1
352ret 1
353end
354l = cbrnext t cbrnext t l
355end
356k = cbrnext t cbrnext t k
357end
358ret 0
359end
360
361note second-byte range of a UTF-8 sequence led by b (Unicode 15 Table 3-7, as Go utf8.Valid)
362fn cbru8lo b ulong to ulong
363if eq b 224
364ret 160
365end
366if eq b 240
367ret 144
368end
369ret 128
370end
371
372fn cbru8hi b ulong to ulong
373if eq b 237
374ret 159
375end
376if eq b 244
377ret 143
378end
379ret 191
380end
381
382note sequence length for lead byte b >= 128: 2 3 4, or 0 when b cannot lead
383fn cbru8w b ulong to ulong
384if and ge b 194 le b 223
385ret 2
386end
387if and ge b 224 le b 239
388ret 3
389end
390if and ge b 240 le b 244
391ret 4
392end
393ret 0
394end
395
396note 1 when d[off..off+ln) is valid UTF-8 (Go utf8.Valid), else 0; off+ln > 65536 traps 3
397fn cbrutf8 d view uchar 65536 off ulong ln ulong to ulong
398i = ulong 0
399while lt i ln bound 65537
400b = ulong get d add off i
401if lt b 128
402i = add i 1
403continue
404end
405w = cbru8w b
406if or eq w 0 gt add i w ln
407ret 0
408end
409c = ulong get d add add off i 1
410if or lt c cbru8lo b gt c cbru8hi b
411ret 0
412end
413for k ulong sub w 2
414c = ulong get d add add off i add k 2
415if or lt c 128 gt c 191
416ret 0
417end
418end
419i = add i w
420end
421ret 1
422end
423
424note shortest argument width for v: 0 (v < 24), 1, 2, 4 or 8 bytes
425fn cbrhw v ulong to ulong
426if lt v 24
427ret 0
428end
429if le v 255
430ret 1
431end
432if le v 65535
433ret 2
434end
435if le v 4294967295
436ret 4
437end
438ret 8
439end
440
441note additional information for argument width w in 1 2 4 8
442fn cbrwai w ulong to ulong
443if eq w 1
444ret 24
445end
446if eq w 2
447ret 25
448end
449if eq w 4
450ret 26
451end
452ret 27
453end
454
455note v as w big-endian bytes at o[q..q+w); returns q + w
456fn cbrwbe o edit uchar 65536 q ulong v ulong w ulong to ulong
457for k ulong w
458put o add q k uchar and shr v mul sub sub w k 1 8 255
459end
460ret add q w
461end
462
463note write the shortest head for major m and argument v at o[q]; returns the offset after it.
464note Not for floats (cbrtok writes those). Traps 3 when the head does not fit in o.
465fn cbrhead o edit uchar 65536 q ulong m ulong v ulong to ulong
466w = cbrhw v
467if eq w 0
468put o q uchar or shl m 5 v
469ret add q 1
470end
471put o q uchar or shl m 5 cbrwai w
472ret cbrwbe o add q 1 v w
473end
474
475note copy d[p..p+n) to o[q..q+n); returns q + n
476fn cbrcopy d view uchar 65536 p ulong o edit uchar 65536 q ulong n ulong to ulong
477for k ulong n
478put o add q k get d add p k
479end
480ret add q n
481end
482
483note write token i of input d at o[q] in canonical form (strings with their payload); new offset.
484note Simple values are canonical already (fxamacker rejects simple < 32 in two bytes); floats keep
485note their width and bits.
486fn cbrtok d view uchar 65536 t view ulong 131074 i ulong o edit uchar 65536 q ulong to ulong
487m = cbrmaj t i
488v = cbrval t i
489a = cbrai t i
490if and eq m 7 ge a 25
491put o q uchar add 224 a
492ret cbrwbe o add q 1 v cbrargw a
493end
494r = cbrhead o q m v
495if or eq m 2 eq m 3
496ret cbrcopy d cbrpoff t i o r v
497end
498ret r
499end
500
501note bytes.Compare of o[a0..a1) and o[b0..b1) > 0
502fn cbrkgt o view uchar 65536 a0 ulong a1 ulong b0 ulong b1 ulong to bool
503la = sub a1 a0
504lb = sub b1 b0
505l = la
506if lt lb l
507l = lb
508end
509for k ulong l
510x = get o add a0 k
511y = get o add b0 k
512if ne x y
513ret gt x y
514end
515end
516ret gt la lb
517end
518
519note Pair workspace w (ulong 4096) for one map of np <= 1024 pairs, in output offsets:
520note w[j] key start, w[1024+j] key end (value start), w[2048+j] pair end, w[3072+j] order.
521
522note key of pair b sorts after key of pair c
523fn cbrpgt o view uchar 65536 w view ulong 4096 b ulong c ulong to bool
524ret cbrkgt o get w b get w add 1024 b get w c get w add 1024 c
525end
526
527note stable insertion sort of the order w[3072..3072+np) by key bytes o[w[x]..w[1024+x])
528fn cbrisort o view uchar 65536 w edit ulong 4096 np ulong to void
529for j ulong np
530c = get w add 3072 j
531s = j
532while gt s 0 bound 1025
533b = get w add 3071 s
534if not cbrpgt o w b c
535break
536end
537put w add 3072 s b
538s = sub s 1
539end
540put w add 3072 s c
541end
542end
543
544note rewrite o[w[0]..) with the pairs in sorted order; returns the end offset, which must be
545note w[2048+np-1] (the caller traps 10 otherwise: an internal invariant, never seen in any run)
546fn cbrperm o edit uchar 65536 w view ulong 4096 np ulong sc edit uchar 65536 to ulong
547s = ulong 0
548for j ulong np
549b = get w add 3072 j
550p = get w b
551s = cbrcopy o p sc s sub get w add 2048 b p
552end
553ret cbrcopy sc 0 o get w 0 s
554end
555
556note store pair j of a map: key token k, value token v, next pair token l (output offsets oo[token])
557fn cbrpset w edit ulong 4096 oo view ulong 65537 j ulong k ulong v ulong l ulong to void
558put w j get oo k
559put w add 1024 j get oo v
560put w add 2048 j get oo l
561put w add 3072 j j
562end
563
564note fill w for map token m
565fn cbrpairs t view ulong 131074 m ulong oo view ulong 65537 w edit ulong 4096 to void
566k = add m 1
567for j ulong cbrval t m
568v = cbrnext t k
569l = cbrnext t v
570cbrpset w oo j k v l
571k = l
572end
573end
574
575note sort the encoded pairs of map token m in place (w and sc are scratch, reused for every map)
576fn cbrsortm o edit uchar 65536 t view ulong 131074 m ulong oo view ulong 65537 w edit ulong 4096 sc edit uchar 65536 to void
577np = cbrval t m
578cbrpairs t m oo w
579cbrisort o w np
580if ne cbrperm o w np sc get w add 2047 np
581trap 10
582end
583end
584
585note pass 2 of cbrencs: sort every map with 2 or more pairs among tokens r..nx-1, last first
586fn cbrsorts o edit uchar 65536 t view ulong 131074 r ulong nx ulong oo view ulong 65537 to void
587w = new ulong 4096 0
588sc = new uchar 65536 0
589i = nx
590while gt i r bound 65537
591i = sub i 1
592if and eq cbrmaj t i 5 gt cbrval t i 1
593cbrsortm o t i oo w sc
594end
595end
596end
597
598note encode the subtree of token r of input d (decoded into t by cbrdec) at o[off..), CTAP2
599note canonical; returns the offset after it. The output is never longer than the input subtree
600note (cbrspan); off + cbrspan t r > 65536 is a caller error: trap 9 before any write.
601note Pass 1 writes every token in canonical form and records its output offset oo[token]; pass 2
602note sorts the pairs of every map with 2 or more pairs, last token first, so a map's keys are
603note final before the map that holds them is sorted. Sorting permutes bytes inside one map's
604note range only, so the offset of every token that heads a pair of an enclosing map is unchanged.
605fn cbrencs d view uchar 65536 t view ulong 131074 r ulong o edit uchar 65536 off ulong to ulong
606if gt add off cbrspan t r 65536
607trap 9
608end
609nx = cbrnext t r
610oo = new ulong 65537 0
611q = off
612i = r
613while lt i nx bound 65537
614put oo i q
615q = cbrtok d t i o q
616i = add i 1
617end
618put oo nx q
619cbrsorts o t r nx oo
620ret q
621end
622
623note canonical encoding of the whole decoded item into o; returns its length
624fn cbrenc d view uchar 65536 t view ulong 131074 o edit uchar 65536 to ulong
625ret cbrencs d t 0 o 0
626end