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