omnet
aien-dev/omnet
Brought by drakestapleton
| 1 | note SPDX-License-Identifier: BSD-3-Clause |
| 2 | note Derived behavior: Tailscale v1.102.0 tka/{aum,key,state,tka,sync}.go. |
| 3 | note Copyright (c) Tailscale Inc & contributors. See tailscale.LICENSE. |
| 4 | note Model schemas: 1 AUM, 2 Key, 3 State, 4 Signature; scalar schemas 5 uint, |
| 5 | note 6 uint8, 7 bytes, 8 metadata, 9 hash pointer; arrays 12 Keys, 14 signatures, 17 bytes. |
| 6 | note Result lengths <=65536; errors 65537 type, 65538 capacity, 65539 static, |
| 7 | note 65540 signature, 65541 parent/key/state, 65542 uninitialized/intersection. |
| 8 | fn tkabool b bool to ulong |
| 9 | if b |
| 10 | ret 1 |
| 11 | end |
| 12 | ret 0 |
| 13 | end |
| 14 | |
| 15 | fn tkanull t view ulong 131074 r ulong to ulong |
| 16 | if eq r 65536 |
| 17 | ret 1 |
| 18 | end |
| 19 | if ne cbrmaj t r 7 |
| 20 | ret 0 |
| 21 | end |
| 22 | ret tkabool or eq cbrval t r 22 eq cbrval t r 23 |
| 23 | end |
| 24 | |
| 25 | fn tkaheadw o edit uchar 65536 w edit ulong 4 m ulong v ulong to void |
| 26 | if ne get w 1 0 |
| 27 | ret |
| 28 | end |
| 29 | n = ulong 1 |
| 30 | if ge v 24 |
| 31 | n = 2 |
| 32 | end |
| 33 | if gt v 255 |
| 34 | n = 3 |
| 35 | end |
| 36 | if gt v 65535 |
| 37 | n = 5 |
| 38 | end |
| 39 | if gt v 4294967295 |
| 40 | n = 9 |
| 41 | end |
| 42 | if gt n sub 65536 get w 0 |
| 43 | put w 1 65538 |
| 44 | ret |
| 45 | end |
| 46 | put w 0 cbrhead o get w 0 m v |
| 47 | end |
| 48 | |
| 49 | fn tkacopy d view uchar 65536 a ulong n ulong o edit uchar 65536 w edit ulong 4 to void |
| 50 | if ne get w 1 0 |
| 51 | ret |
| 52 | end |
| 53 | if gt n sub 65536 get w 0 |
| 54 | put w 1 65538 |
| 55 | ret |
| 56 | end |
| 57 | for i ulong n |
| 58 | put o add get w 0 i get d add a i |
| 59 | end |
| 60 | put w 0 add get w 0 n |
| 61 | end |
| 62 | |
| 63 | fn tkafld s ulong f ulong to ulong |
| 64 | if eq s 1 |
| 65 | if eq f 1 |
| 66 | ret 6 |
| 67 | end |
| 68 | if or eq f 2 eq f 4 |
| 69 | ret 7 |
| 70 | end |
| 71 | if eq f 3 |
| 72 | ret 2 |
| 73 | end |
| 74 | if eq f 5 |
| 75 | ret 3 |
| 76 | end |
| 77 | if eq f 6 |
| 78 | ret 5 |
| 79 | end |
| 80 | if eq f 7 |
| 81 | ret 8 |
| 82 | end |
| 83 | if eq f 23 |
| 84 | ret 14 |
| 85 | end |
| 86 | end |
| 87 | if eq s 2 |
| 88 | if eq f 1 |
| 89 | ret 6 |
| 90 | end |
| 91 | if eq f 2 |
| 92 | ret 5 |
| 93 | end |
| 94 | if eq f 3 |
| 95 | ret 7 |
| 96 | end |
| 97 | if eq f 12 |
| 98 | ret 8 |
| 99 | end |
| 100 | end |
| 101 | if eq s 3 |
| 102 | if eq f 1 |
| 103 | ret 9 |
| 104 | end |
| 105 | if eq f 2 |
| 106 | ret 17 |
| 107 | end |
| 108 | if eq f 3 |
| 109 | ret 12 |
| 110 | end |
| 111 | if or eq f 4 eq f 5 |
| 112 | ret 5 |
| 113 | end |
| 114 | end |
| 115 | if eq s 4 |
| 116 | if or eq f 1 eq f 2 |
| 117 | ret 7 |
| 118 | end |
| 119 | end |
| 120 | ret 0 |
| 121 | end |
| 122 | |
| 123 | fn tkareq s ulong f ulong to ulong |
| 124 | if eq s 1 |
| 125 | ret tkabool le f 2 |
| 126 | end |
| 127 | if or eq s 2 eq s 3 |
| 128 | ret tkabool le f 3 |
| 129 | end |
| 130 | ret tkabool eq s 4 |
| 131 | end |
| 132 | |
| 133 | fn tkafnum d view uchar 65536 t view ulong 131074 r ulong to ulong |
| 134 | m = cbrmaj t r |
| 135 | if eq m 0 |
| 136 | if lt cbrval t r 24 |
| 137 | ret cbrval t r |
| 138 | end |
| 139 | ret 0 |
| 140 | end |
| 141 | if eq m 1 |
| 142 | if gt cbrval t r 9223372036854775807 |
| 143 | ret 65537 |
| 144 | end |
| 145 | ret 0 |
| 146 | end |
| 147 | if ne m 3 |
| 148 | ret 65537 |
| 149 | end |
| 150 | n = cbrval t r |
| 151 | p = cbrpoff t r |
| 152 | if eq cbrutf8 d p n 0 |
| 153 | ret 65537 |
| 154 | end |
| 155 | if eq n 1 |
| 156 | v = ulong get d p |
| 157 | if and ge v 49 le v 57 |
| 158 | ret sub v 48 |
| 159 | end |
| 160 | end |
| 161 | if eq n 2 |
| 162 | if and eq get d p 49 eq get d add p 1 50 |
| 163 | ret 12 |
| 164 | end |
| 165 | if and eq get d p 50 eq get d add p 1 51 |
| 166 | ret 23 |
| 167 | end |
| 168 | end |
| 169 | ret 0 |
| 170 | end |
| 171 | |
| 172 | fn tkaomit t view ulong 131074 r ulong s ulong f ulong keep ulong to ulong |
| 173 | if ne tkareq s f 0 |
| 174 | ret 0 |
| 175 | end |
| 176 | if ne tkanull t r 0 |
| 177 | ret 1 |
| 178 | end |
| 179 | if eq keep 1 |
| 180 | ret 0 |
| 181 | end |
| 182 | if and eq s 1 or eq f 3 or eq f 5 eq f 6 |
| 183 | ret 0 |
| 184 | end |
| 185 | ret tkabool eq cbrval t r 0 |
| 186 | end |
| 187 | |
| 188 | note fxamacker fillPositiveInt also accepts non-Boolean/non-null CBOR simple numbers. |
| 189 | fn tkauint t view ulong 131074 r ulong to bool |
| 190 | if eq cbrmaj t r 0 |
| 191 | ret true |
| 192 | end |
| 193 | if eq cbrmaj t r 7 |
| 194 | ret or lt cbrai t r 20 eq cbrai t r 24 |
| 195 | end |
| 196 | ret false |
| 197 | end |
| 198 | |
| 199 | fn tkabytes d view uchar 65536 t view ulong 131074 o edit uchar 65536 w edit ulong 4 r ulong to void |
| 200 | if ne tkanull t r 0 |
| 201 | tkaheadw o w 7 22 |
| 202 | ret |
| 203 | end |
| 204 | m = cbrmaj t r |
| 205 | n = cbrval t r |
| 206 | if eq m 2 |
| 207 | tkaheadw o w 2 n |
| 208 | tkacopy d cbrpoff t r n o w |
| 209 | ret |
| 210 | end |
| 211 | if ne m 4 |
| 212 | put w 1 65537 |
| 213 | ret |
| 214 | end |
| 215 | tkaheadw o w 2 n |
| 216 | j = add r 1 |
| 217 | for i ulong n |
| 218 | v = ulong 0 |
| 219 | if eq tkanull t j 0 |
| 220 | if or not tkauint t j gt cbrval t j 255 |
| 221 | put w 1 65537 |
| 222 | ret |
| 223 | end |
| 224 | v = cbrval t j |
| 225 | end |
| 226 | if ge get w 0 65536 |
| 227 | put w 1 65538 |
| 228 | ret |
| 229 | end |
| 230 | put o get w 0 uchar v |
| 231 | put w 0 add get w 0 1 |
| 232 | j = cbrnext t j |
| 233 | end |
| 234 | end |
| 235 | |
| 236 | fn tkameta d view uchar 65536 t view ulong 131074 o edit uchar 65536 w edit ulong 4 r ulong to void |
| 237 | if ne tkanull t r 0 |
| 238 | tkaheadw o w 7 22 |
| 239 | ret |
| 240 | end |
| 241 | if ne cbrmaj t r 5 |
| 242 | put w 1 65537 |
| 243 | ret |
| 244 | end |
| 245 | if ne cbrdupk d t r 0 |
| 246 | put w 1 65537 |
| 247 | ret |
| 248 | end |
| 249 | note Normalize null strings to empty, then canonical-sort the rebuilt map. |
| 250 | b = new uchar 65536 0 |
| 251 | z = new ulong 4 0 |
| 252 | tkaheadw b z 5 cbrval t r |
| 253 | j = add r 1 |
| 254 | for i ulong mul 2 cbrval t r |
| 255 | if ne tkanull t j 0 |
| 256 | tkaheadw b z 3 0 |
| 257 | else |
| 258 | if ne cbrmaj t j 3 |
| 259 | put w 1 65537 |
| 260 | ret |
| 261 | end |
| 262 | if eq cbrutf8 d cbrpoff t j cbrval t j 0 |
| 263 | put w 1 65537 |
| 264 | ret |
| 265 | end |
| 266 | tkaheadw b z 3 cbrval t j |
| 267 | tkacopy d cbrpoff t j cbrval t j b z |
| 268 | end |
| 269 | j = cbrnext t j |
| 270 | end |
| 271 | u = new ulong 131074 0 |
| 272 | if ne get z 1 0 |
| 273 | put w 1 get z 1 |
| 274 | ret |
| 275 | end |
| 276 | v = cbrdec b get z 0 u |
| 277 | if ne cbrdupk b u 0 0 |
| 278 | put w 1 65537 |
| 279 | ret |
| 280 | end |
| 281 | if gt get z 0 sub 65536 get w 0 |
| 282 | put w 1 65538 |
| 283 | ret |
| 284 | end |
| 285 | put w 0 cbrencs b u 0 o get w 0 |
| 286 | end |
| 287 | |
| 288 | fn tkafixed d view uchar 65536 t view ulong 131074 o edit uchar 65536 w edit ulong 4 r ulong to void |
| 289 | if ne tkanull t r 0 |
| 290 | tkaheadw o w 7 22 |
| 291 | ret |
| 292 | end |
| 293 | m = cbrmaj t r |
| 294 | if and ne m 2 ne m 4 |
| 295 | put w 1 65537 |
| 296 | ret |
| 297 | end |
| 298 | tkaheadw o w 2 32 |
| 299 | if or ne get w 1 0 gt get w 0 65504 |
| 300 | put w 1 65538 |
| 301 | ret |
| 302 | end |
| 303 | j = add r 1 |
| 304 | for i ulong 32 |
| 305 | v = ulong 0 |
| 306 | if lt i cbrval t r |
| 307 | if eq m 2 |
| 308 | v = ulong get d add cbrpoff t r i |
| 309 | else |
| 310 | if eq tkanull t j 0 |
| 311 | if or not tkauint t j gt cbrval t j 255 |
| 312 | put w 1 65537 |
| 313 | ret |
| 314 | end |
| 315 | v = cbrval t j |
| 316 | end |
| 317 | j = cbrnext t j |
| 318 | end |
| 319 | end |
| 320 | put o get w 0 uchar v |
| 321 | put w 0 add get w 0 1 |
| 322 | end |
| 323 | end |
| 324 | |
| 325 | fn tkaval d view uchar 65536 t view ulong 131074 o edit uchar 65536 w edit ulong 4 r ulong s ulong to void |
| 326 | if ge s 10 |
| 327 | if ne tkanull t r 0 |
| 328 | tkaheadw o w 7 22 |
| 329 | ret |
| 330 | end |
| 331 | if ne cbrmaj t r 4 |
| 332 | put w 1 65537 |
| 333 | ret |
| 334 | end |
| 335 | tkaheadw o w 4 cbrval t r |
| 336 | j = add r 1 |
| 337 | for i ulong cbrval t r |
| 338 | tkaval d t o w j sub s 10 |
| 339 | j = cbrnext t j |
| 340 | end |
| 341 | ret |
| 342 | end |
| 343 | if eq s 7 |
| 344 | tkabytes d t o w r |
| 345 | ret |
| 346 | end |
| 347 | if eq s 8 |
| 348 | tkameta d t o w r |
| 349 | ret |
| 350 | end |
| 351 | if or eq s 5 eq s 6 |
| 352 | v = ulong 0 |
| 353 | if eq tkanull t r 0 |
| 354 | if not tkauint t r |
| 355 | put w 1 65537 |
| 356 | ret |
| 357 | end |
| 358 | v = cbrval t r |
| 359 | end |
| 360 | if and eq s 6 gt v 255 |
| 361 | put w 1 65537 |
| 362 | ret |
| 363 | end |
| 364 | tkaheadw o w 0 v |
| 365 | ret |
| 366 | end |
| 367 | note AUMHash is a fixed Go array: short values pad, long values truncate. |
| 368 | if eq s 9 |
| 369 | tkafixed d t o w r |
| 370 | ret |
| 371 | end |
| 372 | note Null into a struct leaves its zero value, pointers are filtered by their owning field. |
| 373 | a = new ulong 24 65536 |
| 374 | if eq tkanull t r 0 |
| 375 | if ne cbrmaj t r 5 |
| 376 | put w 1 65537 |
| 377 | ret |
| 378 | end |
| 379 | if ne cbrdupk d t r 0 |
| 380 | put w 1 65537 |
| 381 | ret |
| 382 | end |
| 383 | j = add r 1 |
| 384 | for i ulong cbrval t r |
| 385 | f = tkafnum d t j |
| 386 | if ge f 65537 |
| 387 | put w 1 f |
| 388 | ret |
| 389 | end |
| 390 | v = cbrnext t j |
| 391 | if ne tkafld s f 0 |
| 392 | if ne get a f 65536 |
| 393 | put w 1 65537 |
| 394 | ret |
| 395 | end |
| 396 | put a f v |
| 397 | end |
| 398 | j = cbrnext t v |
| 399 | end |
| 400 | end |
| 401 | n = ulong 0 |
| 402 | for f ulong 24 |
| 403 | if ne tkafld s f 0 |
| 404 | if eq tkaomit t get a f s f get w 2 0 |
| 405 | n = add n 1 |
| 406 | end |
| 407 | end |
| 408 | end |
| 409 | tkaheadw o w 5 n |
| 410 | for f ulong 24 |
| 411 | if ne tkafld s f 0 |
| 412 | if eq tkaomit t get a f s f get w 2 0 |
| 413 | tkaheadw o w 0 f |
| 414 | tkaval d t o w get a f tkafld s f |
| 415 | end |
| 416 | end |
| 417 | end |
| 418 | end |
| 419 | |
| 420 | fn tkadecode d view uchar 65536 n ulong s ulong o edit uchar 65536 keep ulong to ulong |
| 421 | t = new ulong 131074 0 |
| 422 | if ne cbrdec d n t 0 |
| 423 | ret 65537 |
| 424 | end |
| 425 | w = new ulong 4 0 |
| 426 | put w 2 keep |
| 427 | tkaval d t o w 0 s |
| 428 | if ne get w 1 0 |
| 429 | ret get w 1 |
| 430 | end |
| 431 | ret get w 0 |
| 432 | end |
| 433 | |
| 434 | fn tkamodel d view uchar 65536 n ulong s ulong o edit uchar 65536 keep ulong to ulong |
| 435 | if or gt n 65536 or lt s 1 gt s 4 |
| 436 | ret 65538 |
| 437 | end |
| 438 | b = new uchar 65536 0 |
| 439 | c = new uchar 65536 0 |
| 440 | z = tkadecode d n s b 1 |
| 441 | if gt z 65536 |
| 442 | ret z |
| 443 | end |
| 444 | if eq keep 0 |
| 445 | z = tkadecode b z s c 0 |
| 446 | if gt z 65536 |
| 447 | ret z |
| 448 | end |
| 449 | for i ulong z |
| 450 | put o i get c i |
| 451 | end |
| 452 | else |
| 453 | for i ulong z |
| 454 | put o i get b i |
| 455 | end |
| 456 | end |
| 457 | ret z |
| 458 | end |
| 459 | |
| 460 | fn tkadec d view uchar 65536 n ulong o edit uchar 65536 to ulong |
| 461 | ret tkamodel d n 1 o 0 |
| 462 | end |
| 463 | |
| 464 | fn tkahash d view uchar 65536 n ulong sig ulong o edit uchar 32 to ulong |
| 465 | b = new uchar 65536 0 |
| 466 | z = tkadec d n b |
| 467 | if gt z 65536 |
| 468 | ret z |
| 469 | end |
| 470 | t = new ulong 131074 0 |
| 471 | if ne sig 0 |
| 472 | x = cbrdec b z t |
| 473 | r = cbrmfind t 0 23 |
| 474 | if ne r 0 |
| 475 | note Signature field 23 is last in the canonical map. |
| 476 | z = cbrhoff t sub r 1 |
| 477 | put b 0 uchar sub ulong get b 0 1 |
| 478 | end |
| 479 | end |
| 480 | k = new uchar 32 0 |
| 481 | b2shash b z k 0 32 o |
| 482 | ret 0 |
| 483 | end |
| 484 | |
| 485 | fn tkapres t view ulong 131074 r ulong f ulong to ulong |
| 486 | v = cbrmfind t r f |
| 487 | if eq v 0 |
| 488 | ret 0 |
| 489 | end |
| 490 | ret tkabool eq tkanull t v 0 |
| 491 | end |
| 492 | |
| 493 | fn tkalen t view ulong 131074 r ulong f ulong to ulong |
| 494 | v = cbrmfind t r f |
| 495 | if eq v 0 |
| 496 | ret 0 |
| 497 | end |
| 498 | if ne tkanull t v 0 |
| 499 | ret 0 |
| 500 | end |
| 501 | ret cbrval t v |
| 502 | end |
| 503 | |
| 504 | fn tkablen t view ulong 131074 r ulong to ulong |
| 505 | if ne tkanull t r 0 |
| 506 | ret 0 |
| 507 | end |
| 508 | ret cbrval t r |
| 509 | end |
| 510 | |
| 511 | fn tkaeq d view uchar 65536 t view ulong 131074 a ulong b ulong to ulong |
| 512 | if ne tkablen t a tkablen t b |
| 513 | ret 0 |
| 514 | end |
| 515 | for i ulong tkablen t a |
| 516 | if ne get d add cbrpoff t a i get d add cbrpoff t b i |
| 517 | ret 0 |
| 518 | end |
| 519 | end |
| 520 | ret 1 |
| 521 | end |
| 522 | |
| 523 | fn tkakvalid d view uchar 65536 t view ulong 131074 r ulong to ulong |
| 524 | if ne tkalen t r 1 1 |
| 525 | ret 65539 |
| 526 | end |
| 527 | v = tkalen t r 2 |
| 528 | if or eq v 0 gt v 4096 |
| 529 | ret 65539 |
| 530 | end |
| 531 | m = cbrmfind t r 12 |
| 532 | if ne m 0 |
| 533 | j = add m 1 |
| 534 | n = ulong 0 |
| 535 | for i ulong mul 2 cbrval t m |
| 536 | n = add n cbrval t j |
| 537 | j = cbrnext t j |
| 538 | end |
| 539 | if gt n 512 |
| 540 | ret 65539 |
| 541 | end |
| 542 | end |
| 543 | ret 0 |
| 544 | end |
| 545 | |
| 546 | fn tkasvalid d view uchar 65536 t view ulong 131074 r ulong to ulong |
| 547 | if ne tkapres t r 1 0 |
| 548 | ret 65539 |
| 549 | end |
| 550 | n = tkalen t r 2 |
| 551 | if or eq n 0 gt n 32 |
| 552 | ret 65539 |
| 553 | end |
| 554 | a = cbrmfind t r 2 |
| 555 | j = add a 1 |
| 556 | for i ulong n |
| 557 | if ne cbrval t j 32 |
| 558 | ret 65539 |
| 559 | end |
| 560 | k = add a 1 |
| 561 | for h ulong i |
| 562 | if ne tkaeq d t j k 0 |
| 563 | ret 65539 |
| 564 | end |
| 565 | k = cbrnext t k |
| 566 | end |
| 567 | j = cbrnext t j |
| 568 | end |
| 569 | n = tkalen t r 3 |
| 570 | if or eq n 0 gt n 512 |
| 571 | ret 65539 |
| 572 | end |
| 573 | a = cbrmfind t r 3 |
| 574 | j = add a 1 |
| 575 | for i ulong n |
| 576 | if ne tkakvalid d t j 0 |
| 577 | ret 65539 |
| 578 | end |
| 579 | k = add a 1 |
| 580 | for h ulong i |
| 581 | if ne tkaeq d t cbrmfind t j 3 cbrmfind t k 3 0 |
| 582 | ret 65539 |
| 583 | end |
| 584 | k = cbrnext t k |
| 585 | end |
| 586 | j = cbrnext t j |
| 587 | end |
| 588 | ret 0 |
| 589 | end |
| 590 | |
| 591 | fn tkavalid d view uchar 65536 n ulong to ulong |
| 592 | b = new uchar 65536 0 |
| 593 | z = tkamodel d n 1 b 1 |
| 594 | if gt z 65536 |
| 595 | ret z |
| 596 | end |
| 597 | t = new ulong 131074 0 |
| 598 | v = cbrdec b z t |
| 599 | if and ne tkapres t 0 2 0 eq tkalen t 0 2 0 |
| 600 | ret 65539 |
| 601 | end |
| 602 | r = cbrmfind t 0 3 |
| 603 | if ne r 0 |
| 604 | if ne tkakvalid b t r 0 |
| 605 | ret 65539 |
| 606 | end |
| 607 | end |
| 608 | r = cbrmfind t 0 5 |
| 609 | if ne r 0 |
| 610 | if ne tkasvalid b t r 0 |
| 611 | ret 65539 |
| 612 | end |
| 613 | end |
| 614 | r = cbrmfind t 0 23 |
| 615 | if ne r 0 |
| 616 | j = add r 1 |
| 617 | for i ulong cbrval t r |
| 618 | if or ne tkalen t j 1 32 ne tkalen t j 2 64 |
| 619 | ret 65539 |
| 620 | end |
| 621 | j = cbrnext t j |
| 622 | end |
| 623 | end |
| 624 | k = tkalen t 0 1 |
| 625 | mask = ulong 0 |
| 626 | for f ulong 5 |
| 627 | if ne tkapres t 0 add f 3 0 |
| 628 | mask = or mask shl ulong 1 f |
| 629 | end |
| 630 | end |
| 631 | if and eq k 1 ne mask 1 |
| 632 | ret 65539 |
| 633 | end |
| 634 | if eq k 2 |
| 635 | if or ne mask 2 eq tkalen t 0 4 0 |
| 636 | ret 65539 |
| 637 | end |
| 638 | end |
| 639 | if eq k 4 |
| 640 | if or ne and mask 7 2 eq and mask 24 0 |
| 641 | ret 65539 |
| 642 | end |
| 643 | if eq tkalen t 0 4 0 |
| 644 | ret 65539 |
| 645 | end |
| 646 | end |
| 647 | if and eq k 5 ne mask 4 |
| 648 | ret 65539 |
| 649 | end |
| 650 | ret 0 |
| 651 | end |