Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/blob/main/src/turing/tc_pstream.om # omega-mv Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance. Brought by drakestapleton Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / src / turing / tc_pstream.om` 1474 lines ยท 26572 bytes Raw Blame History 1 note SPDX-License-Identifier: Apache-2.0 2 note Copyright 2026 Drake Stapleton (AIEN / aien-dev) 3 note 4 note src/turing/tc_pstream.om: Canonical probability stream (TPS1), observed-symbol 5 note file (TSY1), coded-file header (56 bytes), and ideal code length (G2 ub rule) 6 note in pure seeded OM-2. Composed after seed/ng/sha256.om. 7 note Normative layout: calibration/docs/CODER_SPEC.md. 8 9 const TC_OK int 0 10 const TC_E_ARG int -101 11 const TC_E_IO int -102 12 const TC_E_FORMAT int -103 13 const TC_E_DIGEST int -104 14 const TC_E_NORM int -105 15 const TC_E_ZERO int -106 16 const TC_E_PROFILE int -107 17 const TC_E_MODEL int -108 18 const TC_E_DATASET int -109 19 const TC_E_COUNT int -110 20 const TC_E_HEADER int -111 21 const TC_E_BINDING int -112 22 const TC_E_TRUNC int -113 23 const TC_E_TRAIL int -114 24 const TC_E_CORRUPT int -115 25 const TC_E_SYMBOL int -116 26 const TC_E_INDEX int -117 27 28 const TC_QONE ulong 65536 29 const TC_KMIN ulong 2 30 const TC_KMAX ulong 16 31 const TC_PS_VERSION ulong 1 32 const TC_PS_HEADER ulong 128 33 const TC_PS_REC_FIXED ulong 24 34 const TC_SY_VERSION ulong 1 35 const TC_SY_HEADER ulong 56 36 const TC_DIGEST ulong 32 37 const TC_CODED_HEADER ulong 56 38 const TC_CODED_VERSION ulong 1 39 const TC_PB_HALF ulong `16777216` 40 const TC_PB_CAP ulong `33554432` 41 const TC_SB_CAP ulong 524288 42 const TC_CB_CAP ulong `4194304` 43 const TC_MAX_N ulong 500000 44 const TC_MAX_Q ulong `5000000` 45 46 note Domain separators and error names 47 text TC_DOM_PS "turing.tc.pstream.v1" 48 text TC_DOM_SY "turing.tc.symbols.v1" 49 text TC_DOM_YM "turing.ymodel.v0" 50 text TC_HEX_DIG "`0123456789abcdef`" 51 text TC_ERR_OK "OK" 52 text TC_ERR_ARG "TC_E_ARG" 53 text TC_ERR_IO "TC_E_IO" 54 text TC_ERR_FORMAT "TC_E_FORMAT" 55 text TC_ERR_DIGEST "TC_E_DIGEST" 56 text TC_ERR_NORM "TC_E_NORM" 57 text TC_ERR_ZERO "TC_E_ZERO" 58 text TC_ERR_PROFILE "TC_E_PROFILE" 59 text TC_ERR_MODEL "TC_E_MODEL" 60 text TC_ERR_DATASET "TC_E_DATASET" 61 text TC_ERR_COUNT "TC_E_COUNT" 62 text TC_ERR_HEADER "TC_E_HEADER" 63 text TC_ERR_BINDING "TC_E_BINDING" 64 text TC_ERR_TRUNC "TC_E_TRUNC" 65 text TC_ERR_TRAIL "TC_E_TRAIL" 66 text TC_ERR_CORRUPT "TC_E_CORRUPT" 67 text TC_ERR_SYMBOL "TC_E_SYMBOL" 68 text TC_ERR_INDEX "TC_E_INDEX" 69 text TC_ERR_UNK "TC_E_UNKNOWN" 70 71 text TC_W_FMT_PS "not a TPS1 probability stream (magic, header or size)" 72 text TC_W_DIG_PS "TPS1 trailer digest does not match the stream bytes" 73 text TC_W_IDX_PS "record observation_index is not its position" 74 text TC_W_ZERO "probability entry is 0 (zero-probability policy: refuse)" 75 text TC_W_NORM "record entries do not sum to 65536 or norm_sum != 65536" 76 text TC_W_FMT_SY "not a TSY1 symbol file (magic, header or size)" 77 text TC_W_DIG_SY "TSY1 trailer digest does not match the symbol bytes" 78 text TC_W_CNT_SY "TSY1 count disagrees with size" 79 text TC_W_SYM "symbol outside 0..K-1" 80 text TC_W_PROF "profile digest mismatch" 81 text TC_W_MOD "model digest mismatch" 82 text TC_W_DAT "dataset digest mismatch" 83 text TC_W_PAIR_CNT "TPS1 and TSY1 K or count mismatch" 84 text TC_W_PAIR_DAT "TPS1 and TSY1 name different dataset digests" 85 text TC_W_CHDR "coded file header missing or altered" 86 text TC_W_CBIND "coded file is bound to a different TPS1 digest" 87 text TC_W_CCNT "coded file symbol count disagrees with TPS1" 88 text TC_W_CTRUNC "payload declared exceeds file bytes (truncated bitstream)" 89 text TC_W_CTRAIL "file carries trailing bytes past declared payload" 90 text TC_W_IO "I/O failure" 91 text TC_W_ARG "invalid argument" 92 93 note Streaming SHA-256 state (wraps seed/ng/sha256.om sh_blk/sh_out) 94 global tc_sh_h uint 8 95 global tc_sh_buf uchar 64 96 global tc_sh_n ulong 1 97 global tc_sh_out uchar 32 98 99 note Primary TPS1 state 100 global tc_ps_k ulong 1 101 global tc_ps_n ulong 1 102 global tc_ps_prof uchar 32 103 global tc_ps_mod uchar 32 104 global tc_ps_dat uchar 32 105 global tc_ps_dig uchar 32 106 global tc_ps_key ulong 500000 107 global tc_ps_crumb uint 500000 108 global tc_ps_q ushort `5000000` 109 110 note Primary TSY1 state 111 global tc_sy_k ulong 1 112 global tc_sy_n ulong 1 113 global tc_sy_dat uchar 32 114 global tc_sy_dig uchar 32 115 global tc_sy_sym uchar 500000 116 117 note Decoded TSY1 state 118 global tc_dsy_k ulong 1 119 global tc_dsy_n ulong 1 120 global tc_dsy_dat uchar 32 121 global tc_dsy_dig uchar 32 122 global tc_dsy_sym uchar 500000 123 124 note Expected digests for tc_ps_expect 125 global tc_exp_prof uchar 32 126 global tc_exp_mod uchar 32 127 global tc_exp_dat uchar 32 128 129 note Serialized TPS1 (32 MiB split across two 16 MiB globals), TSY1, coded buffers 130 global tc_pb0 uchar `16777216` 131 global tc_pb1 uchar `16777216` 132 global tc_pb_len ulong 1 133 global tc_sb uchar 524288 134 global tc_sb_len ulong 1 135 global tc_db uchar 524288 136 global tc_db_len ulong 1 137 global tc_cb uchar `4194304` 138 global tc_cb_len ulong 1 139 global tc_cb_plen ulong 1 140 141 note Shared I/O, output formatting, reason buffer, ub(q) memo table 142 global tc_iobuf uchar 65536 143 global tc_why uchar 512 144 global tc_whylen ulong 1 145 global tc_outbuf uchar 16384 146 global tc_outlen ulong 1 147 global tc_ubt ulong 65537 148 149 note ---------------------------------------------------------------- reason string 150 151 fn tc_why_clear to void 152 put tc_whylen 0 0 153 put tc_why 0 0 154 end 155 156 fn tc_why_set s view uchar * to void 157 n = len s 158 if gt n 511 159 n = ulong 511 160 end 161 for i ulong n bound 512 162 put tc_why i get s i 163 end 164 put tc_whylen 0 n 165 put tc_why n 0 166 end 167 168 note ---------------------------------------------------------------- output formatting 169 170 fn tc_out_reset to void 171 put tc_outlen 0 0 172 end 173 174 fn tc_out_byte b uchar to void 175 n = get tc_outlen 0 176 if lt n 16384 177 put tc_outbuf n b 178 put tc_outlen 0 add n 1 179 end 180 end 181 182 fn tc_out_str s view uchar * to void 183 for i ulong len s bound 4096 184 tc_out_byte get s i 185 end 186 end 187 188 fn tc_out_why to void 189 n = get tc_whylen 0 190 for i ulong n bound 512 191 tc_out_byte get tc_why i 192 end 193 end 194 195 fn tc_out_u64 v ulong to void 196 d = new uchar 24 0 197 k = ulong 0 198 x = v 199 while true bound 24 200 put d k uchar add rem x 10 48 201 k = add k 1 202 x = div x 10 203 if eq x 0 204 break 205 end 206 end 207 while gt k 0 bound 24 208 k = sub k 1 209 tc_out_byte get d k 210 end 211 end 212 213 fn tc_out_i64 v long to void 214 if lt v 0 215 tc_out_byte 45 216 if eq v -`9223372036854775808` 217 tc_out_u64 `9223372036854775808` 218 ret 219 end 220 tc_out_u64 ulong neg v 221 ret 222 end 223 tc_out_u64 ulong v 224 end 225 226 fn tc_out_hex_byte b uchar to void 227 v = ulong b 228 tc_out_byte get TC_HEX_DIG shr v 4 229 tc_out_byte get TC_HEX_DIG and v 15 230 end 231 232 fn tc_out_hex_sh nbytes ulong to void 233 for i ulong nbytes bound 32 234 tc_out_hex_byte get tc_sh_out i 235 end 236 end 237 238 fn tc_out_hex_ps_dig nbytes ulong to void 239 for i ulong nbytes bound 32 240 tc_out_hex_byte get tc_ps_dig i 241 end 242 end 243 244 fn tc_out_hex_sy_dig nbytes ulong to void 245 for i ulong nbytes bound 32 246 tc_out_hex_byte get tc_sy_dig i 247 end 248 end 249 250 fn tc_out_errname e int to void 251 if eq e TC_OK 252 tc_out_str TC_ERR_OK 253 ret 254 end 255 if eq e TC_E_ARG 256 tc_out_str TC_ERR_ARG 257 ret 258 end 259 if eq e TC_E_IO 260 tc_out_str TC_ERR_IO 261 ret 262 end 263 if eq e TC_E_FORMAT 264 tc_out_str TC_ERR_FORMAT 265 ret 266 end 267 if eq e TC_E_DIGEST 268 tc_out_str TC_ERR_DIGEST 269 ret 270 end 271 if eq e TC_E_NORM 272 tc_out_str TC_ERR_NORM 273 ret 274 end 275 if eq e TC_E_ZERO 276 tc_out_str TC_ERR_ZERO 277 ret 278 end 279 if eq e TC_E_PROFILE 280 tc_out_str TC_ERR_PROFILE 281 ret 282 end 283 if eq e TC_E_MODEL 284 tc_out_str TC_ERR_MODEL 285 ret 286 end 287 if eq e TC_E_DATASET 288 tc_out_str TC_ERR_DATASET 289 ret 290 end 291 if eq e TC_E_COUNT 292 tc_out_str TC_ERR_COUNT 293 ret 294 end 295 if eq e TC_E_HEADER 296 tc_out_str TC_ERR_HEADER 297 ret 298 end 299 if eq e TC_E_BINDING 300 tc_out_str TC_ERR_BINDING 301 ret 302 end 303 if eq e TC_E_TRUNC 304 tc_out_str TC_ERR_TRUNC 305 ret 306 end 307 if eq e TC_E_TRAIL 308 tc_out_str TC_ERR_TRAIL 309 ret 310 end 311 if eq e TC_E_CORRUPT 312 tc_out_str TC_ERR_CORRUPT 313 ret 314 end 315 if eq e TC_E_SYMBOL 316 tc_out_str TC_ERR_SYMBOL 317 ret 318 end 319 if eq e TC_E_INDEX 320 tc_out_str TC_ERR_INDEX 321 ret 322 end 323 tc_out_str TC_ERR_UNK 324 end 325 326 fn tc_out_flush fd int to int 327 n = get tc_outlen 0 328 o = ulong 0 329 while lt o n bound 16384 330 k = write fd tc_outbuf o sub n o 331 if le k 0 332 put tc_outlen 0 0 333 ret TC_E_IO 334 end 335 o = add o ulong k 336 end 337 put tc_outlen 0 0 338 ret TC_OK 339 end 340 341 note ---------------------------------------------------------------- hex decode 342 343 fn tc_hex_nib c uchar to int 344 v = int c 345 if ge v 48 346 if le v 57 347 ret sub v 48 348 end 349 end 350 if ge v 97 351 if le v 102 352 ret add sub v 97 10 353 end 354 end 355 ret -1 356 end 357 358 fn tc_unhex_to_exp s view uchar * off ulong slen ulong which ulong to int 359 note which: 0=tc_exp_prof, 1=tc_exp_mod, 2=tc_exp_dat 360 if ne slen 64 361 ret TC_E_ARG 362 end 363 for i ulong 32 364 hi = tc_hex_nib get s add off mul i 2 365 lo = tc_hex_nib get s add off add mul i 2 1 366 if lt hi 0 367 ret TC_E_ARG 368 end 369 if lt lo 0 370 ret TC_E_ARG 371 end 372 bv = uchar or shl ulong hi 4 ulong lo 373 if eq which 0 374 put tc_exp_prof i bv 375 else 376 if eq which 1 377 put tc_exp_mod i bv 378 else 379 put tc_exp_dat i bv 380 end 381 end 382 end 383 ret TC_OK 384 end 385 386 note ---------------------------------------------------------------- streaming SHA-256 387 388 fn tc_sha_blk to void 389 for j ulong 8 390 put sh_h j get tc_sh_h j 391 end 392 for j ulong 64 393 put sh_pad j get tc_sh_buf j 394 end 395 sh_blk 1 0 396 for j ulong 8 397 put tc_sh_h j get sh_h j 398 end 399 end 400 401 fn tc_sha_init to void 402 sh_init 403 for j ulong 8 404 put tc_sh_h j get sh_k add j 64 405 end 406 put tc_sh_n 0 0 407 end 408 409 fn tc_sha_byte b uchar to void 410 n = get tc_sh_n 0 411 put tc_sh_buf rem n 64 b 412 n = add n 1 413 put tc_sh_n 0 n 414 if eq rem n 64 0 415 tc_sha_blk 416 end 417 end 418 419 fn tc_sha_dom s view uchar * to void 420 for i ulong len s bound 64 421 tc_sha_byte get s i 422 end 423 tc_sha_byte 0 424 end 425 426 fn tc_sha_final to void 427 bits = mul get tc_sh_n 0 8 428 tc_sha_byte 128 429 while ne rem get tc_sh_n 0 64 56 bound 64 430 tc_sha_byte 0 431 end 432 for j ulong 8 433 tc_sha_byte uchar and shr bits mul sub 7 j 8 255 434 end 435 for j ulong 8 436 put sh_h j get tc_sh_h j 437 end 438 sh_out 439 for i ulong 32 440 put tc_sh_out i get sh_dig i 441 end 442 end 443 444 fn tc_sha_cb off ulong n ulong to void 445 tc_sha_init 446 for i ulong n bound `4194304` 447 tc_sha_byte get tc_cb add off i 448 end 449 tc_sha_final 450 end 451 452 fn tc_file_sha256 path view uchar * off ulong plen ulong to int 453 fd = fopen path off plen 0 0 454 if lt fd 0 455 ret TC_E_IO 456 end 457 tc_sha_init 458 while true bound `1000000` 459 k = read int fd tc_iobuf 0 65536 460 if lt k 0 461 z = fclose int fd 462 ret TC_E_IO 463 end 464 if eq k 0 465 break 466 end 467 kn = ulong k 468 for i ulong kn bound 65536 469 tc_sha_byte get tc_iobuf i 470 end 471 end 472 z = fclose int fd 473 tc_sha_final 474 ret TC_OK 475 end 476 477 note ---------------------------------------------------------------- 32 MiB TPS1 buffer accessors 478 479 fn tc_pb_get i ulong to uchar 480 if lt i TC_PB_HALF 481 ret get tc_pb0 i 482 end 483 ret get tc_pb1 sub i TC_PB_HALF 484 end 485 486 fn tc_pb_put i ulong v uchar to void 487 if lt i TC_PB_HALF 488 put tc_pb0 i v 489 ret 490 end 491 put tc_pb1 sub i TC_PB_HALF v 492 end 493 494 fn tc_pb_put_u16 o ulong v ulong to void 495 tc_pb_put o uchar and v 255 496 tc_pb_put add o 1 uchar and shr v 8 255 497 end 498 499 fn tc_pb_put_u32 o ulong v ulong to void 500 for i ulong 4 501 tc_pb_put add o i uchar and shr v mul i 8 255 502 end 503 end 504 505 fn tc_pb_put_u64 o ulong v ulong to void 506 for i ulong 8 507 tc_pb_put add o i uchar and shr v mul i 8 255 508 end 509 end 510 511 fn tc_pb_get_u16 o ulong to ulong 512 b0 = ulong tc_pb_get o 513 b1 = ulong tc_pb_get add o 1 514 ret or b0 shl b1 8 515 end 516 517 fn tc_pb_get_u32 o ulong to ulong 518 v = ulong 0 519 for i ulong 4 520 b = ulong tc_pb_get add o sub 3 i 521 v = or shl v 8 b 522 end 523 ret v 524 end 525 526 fn tc_pb_get_u64 o ulong to ulong 527 v = ulong 0 528 for i ulong 8 529 b = ulong tc_pb_get add o sub 7 i 530 v = or shl v 8 b 531 end 532 ret v 533 end 534 535 note Little-endian helpers on tc_sb, tc_db, tc_cb 536 537 fn tc_sb_put_u16 o ulong v ulong to void 538 put tc_sb o uchar and v 255 539 put tc_sb add o 1 uchar and shr v 8 255 540 end 541 542 fn tc_sb_put_u64 o ulong v ulong to void 543 for i ulong 8 544 put tc_sb add o i uchar and shr v mul i 8 255 545 end 546 end 547 548 fn tc_sb_get_u16 o ulong to ulong 549 b0 = ulong get tc_sb o 550 b1 = ulong get tc_sb add o 1 551 ret or b0 shl b1 8 552 end 553 554 fn tc_sb_get_u64 o ulong to ulong 555 v = ulong 0 556 for i ulong 8 557 b = ulong get tc_sb add o sub 7 i 558 v = or shl v 8 b 559 end 560 ret v 561 end 562 563 fn tc_db_put_u16 o ulong v ulong to void 564 put tc_db o uchar and v 255 565 put tc_db add o 1 uchar and shr v 8 255 566 end 567 568 fn tc_db_put_u64 o ulong v ulong to void 569 for i ulong 8 570 put tc_db add o i uchar and shr v mul i 8 255 571 end 572 end 573 574 fn tc_cb_put_u64 o ulong v ulong to void 575 for i ulong 8 576 put tc_cb add o i uchar and shr v mul i 8 255 577 end 578 end 579 580 fn tc_cb_get_u64 o ulong to ulong 581 v = ulong 0 582 for i ulong 8 583 b = ulong get tc_cb add o sub 7 i 584 v = or shl v 8 b 585 end 586 ret v 587 end 588 589 note ---------------------------------------------------------------- ideal length (G2 ub rule) 590 591 fn tc_sq62 m ulong to ulong 592 a = shr m 32 593 b = and m `4294967295` 594 p0 = mul b b 595 p1 = mul a b 596 p2 = mul a a 597 c = shl p1 1 598 mid = add shr p0 32 and c `4294967295` 599 lo = or and p0 `4294967295` shl and mid `4294967295` 32 600 hi = add add p2 shr c 32 shr mid 32 601 ret or shl hi 2 shr lo 62 602 end 603 604 fn tc_hibit q ulong to ulong 605 i = ulong 63 606 while eq and shr q i 1 0 bound 64 607 i = sub i 1 608 end 609 ret i 610 end 611 612 fn tc_ubcalc q ulong to ulong 613 ip = tc_hibit q 614 m = shl q sub 62 ip 615 f = ulong 0 616 for i ulong 32 617 m = tc_sq62 m 618 if ge m `9223372036854775808` 619 f = or f shl 1 sub 31 i 620 m = shr m 1 621 end 622 end 623 l = or shl ip 32 f 624 x = sub `68719476736` l 625 ret shr add mul x `1000000` `2147483648` 32 626 end 627 628 fn tc_ubits_q16 q ulong to long 629 if eq q 0 630 ret -1 631 end 632 if gt q TC_QONE 633 ret -1 634 end 635 v = get tc_ubt q 636 if eq v 0 637 v = add tc_ubcalc q 1 638 put tc_ubt q v 639 end 640 ret long sub v 1 641 end 642 643 fn tc_ideal_ub lo ulong hi ulong to long 644 n = get tc_ps_n 0 645 if gt hi n 646 ret -1 647 end 648 if gt hi get tc_sy_n 0 649 ret -1 650 end 651 if gt lo hi 652 ret -1 653 end 654 k = get tc_ps_k 0 655 tot = long 0 656 t = lo 657 while lt t hi bound 500000 658 s = ulong get tc_sy_sym t 659 if ge s k 660 ret -1 661 end 662 qv = ulong get tc_ps_q add mul t k s 663 b = tc_ubits_q16 qv 664 if lt b 0 665 ret -1 666 end 667 tot = add tot b 668 t = add t 1 669 end 670 ret tot 671 end 672 673 note ---------------------------------------------------------------- TPS1 check, serialize, parse 674 675 fn tc_ps_check_row t ulong norm_sum ulong to int 676 k = get tc_ps_k 0 677 base = mul t k 678 s = ulong 0 679 for x ulong k bound 16 680 v = ulong get tc_ps_q add base x 681 if eq v 0 682 tc_why_set TC_W_ZERO 683 ret TC_E_ZERO 684 end 685 s = add s v 686 end 687 if ne s TC_QONE 688 tc_why_set TC_W_NORM 689 ret TC_E_NORM 690 end 691 if ne norm_sum TC_QONE 692 tc_why_set TC_W_NORM 693 ret TC_E_NORM 694 end 695 ret TC_OK 696 end 697 698 fn tc_ps_check_rows to int 699 k = get tc_ps_k 0 700 if lt k TC_KMIN 701 tc_why_set TC_W_ARG 702 ret TC_E_ARG 703 end 704 if gt k TC_KMAX 705 tc_why_set TC_W_ARG 706 ret TC_E_ARG 707 end 708 n = get tc_ps_n 0 709 for t ulong n bound 500000 710 rc = tc_ps_check_row t TC_QONE 711 if ne rc TC_OK 712 ret rc 713 end 714 end 715 ret TC_OK 716 end 717 718 fn tc_ps_hash_pb body ulong to void 719 tc_sha_init 720 tc_sha_dom TC_DOM_PS 721 i = ulong 0 722 while lt i body bound `33554432` 723 tc_sha_byte tc_pb_get i 724 i = add i 1 725 end 726 tc_sha_final 727 end 728 729 fn tc_ps_ser_rec t ulong off ulong k ulong to void 730 tc_pb_put_u64 off t 731 tc_pb_put_u64 add off 8 get tc_ps_key t 732 tc_pb_put_u32 add off 16 ulong get tc_ps_crumb t 733 tc_pb_put_u32 add off 20 TC_QONE 734 qbase = mul t k 735 for x ulong k bound 16 736 qo = add off add 24 mul 2 x 737 qv = ulong get tc_ps_q add qbase x 738 tc_pb_put_u16 qo qv 739 end 740 end 741 742 fn tc_ps_serialize to int 743 k = get tc_ps_k 0 744 if lt k TC_KMIN 745 tc_why_set TC_W_ARG 746 ret TC_E_ARG 747 end 748 if gt k TC_KMAX 749 tc_why_set TC_W_ARG 750 ret TC_E_ARG 751 end 752 rc = tc_ps_check_rows 753 if ne rc TC_OK 754 ret rc 755 end 756 n = get tc_ps_n 0 757 rb = add TC_PS_REC_FIXED mul 2 k 758 body = add TC_PS_HEADER mul n rb 759 if gt add body TC_DIGEST TC_PB_CAP 760 tc_why_set TC_W_IO 761 ret TC_E_IO 762 end 763 for i ulong 128 764 tc_pb_put i 0 765 end 766 tc_pb_put 0 84 767 tc_pb_put 1 80 768 tc_pb_put 2 83 769 tc_pb_put 3 49 770 tc_pb_put_u16 4 TC_PS_VERSION 771 tc_pb_put 6 uchar k 772 tc_pb_put 7 16 773 tc_pb_put_u32 8 0 774 tc_pb_put_u64 12 n 775 tc_pb_put_u32 20 rb 776 tc_pb_put_u32 24 0 777 for i ulong 32 778 tc_pb_put add 28 i get tc_ps_prof i 779 tc_pb_put add 60 i get tc_ps_mod i 780 tc_pb_put add 92 i get tc_ps_dat i 781 end 782 off = TC_PS_HEADER 783 for t ulong n bound 500000 784 tc_ps_ser_rec t off k 785 off = add off rb 786 end 787 tc_ps_hash_pb body 788 for i ulong 32 789 bv = get tc_sh_out i 790 tc_pb_put add body i bv 791 put tc_ps_dig i bv 792 end 793 put tc_pb_len 0 add body TC_DIGEST 794 ret TC_OK 795 end 796 797 fn tc_ps_parse_hdr plen ulong to int 798 if lt plen add TC_PS_HEADER TC_DIGEST 799 tc_why_set TC_W_FMT_PS 800 ret TC_E_FORMAT 801 end 802 if ne tc_pb_get 0 84 803 tc_why_set TC_W_FMT_PS 804 ret TC_E_FORMAT 805 end 806 if ne tc_pb_get 1 80 807 tc_why_set TC_W_FMT_PS 808 ret TC_E_FORMAT 809 end 810 if ne tc_pb_get 2 83 811 tc_why_set TC_W_FMT_PS 812 ret TC_E_FORMAT 813 end 814 if ne tc_pb_get 3 49 815 tc_why_set TC_W_FMT_PS 816 ret TC_E_FORMAT 817 end 818 body = sub plen TC_DIGEST 819 tc_ps_hash_pb body 820 for i ulong 32 821 if ne get tc_sh_out i tc_pb_get add body i 822 tc_why_set TC_W_DIG_PS 823 ret TC_E_DIGEST 824 end 825 end 826 ver = tc_pb_get_u16 4 827 k = ulong tc_pb_get 6 828 qb = ulong tc_pb_get 7 829 flags = tc_pb_get_u32 8 830 res = tc_pb_get_u32 24 831 res2 = tc_pb_get_u32 124 832 if ne ver TC_PS_VERSION 833 tc_why_set TC_W_FMT_PS 834 ret TC_E_FORMAT 835 end 836 if ne qb 16 837 tc_why_set TC_W_FMT_PS 838 ret TC_E_FORMAT 839 end 840 if ne flags 0 841 tc_why_set TC_W_FMT_PS 842 ret TC_E_FORMAT 843 end 844 if ne res 0 845 tc_why_set TC_W_FMT_PS 846 ret TC_E_FORMAT 847 end 848 if ne res2 0 849 tc_why_set TC_W_FMT_PS 850 ret TC_E_FORMAT 851 end 852 if lt k TC_KMIN 853 tc_why_set TC_W_FMT_PS 854 ret TC_E_FORMAT 855 end 856 if gt k TC_KMAX 857 tc_why_set TC_W_FMT_PS 858 ret TC_E_FORMAT 859 end 860 ret TC_OK 861 end 862 863 fn tc_ps_parse_rec t ulong off ulong k ulong to int 864 if ne tc_pb_get_u64 off t 865 tc_why_set TC_W_IDX_PS 866 ret TC_E_INDEX 867 end 868 put tc_ps_key t tc_pb_get_u64 add off 8 869 put tc_ps_crumb t uint tc_pb_get_u32 add off 16 870 nsum = tc_pb_get_u32 add off 20 871 qbase = mul t k 872 for x ulong k bound 16 873 qo = add off add 24 mul 2 x 874 qv = tc_pb_get_u16 qo 875 put tc_ps_q add qbase x ushort qv 876 end 877 ret tc_ps_check_row t nsum 878 end 879 880 fn tc_ps_parse plen ulong to int 881 rc = tc_ps_parse_hdr plen 882 if ne rc TC_OK 883 ret rc 884 end 885 k = ulong tc_pb_get 6 886 n = tc_pb_get_u64 12 887 rbw = tc_pb_get_u32 20 888 rb = add TC_PS_REC_FIXED mul 2 k 889 if ne rbw rb 890 tc_why_set TC_W_FMT_PS 891 ret TC_E_FORMAT 892 end 893 avail = sub sub plen TC_PS_HEADER TC_DIGEST 894 if gt n div avail rb 895 tc_why_set TC_W_FMT_PS 896 ret TC_E_FORMAT 897 end 898 if ne add add TC_PS_HEADER mul n rb TC_DIGEST plen 899 tc_why_set TC_W_FMT_PS 900 ret TC_E_FORMAT 901 end 902 if gt n TC_MAX_N 903 tc_why_set TC_W_IO 904 ret TC_E_IO 905 end 906 if gt mul n k TC_MAX_Q 907 tc_why_set TC_W_IO 908 ret TC_E_IO 909 end 910 put tc_ps_k 0 k 911 put tc_ps_n 0 n 912 for i ulong 32 913 put tc_ps_prof i tc_pb_get add 28 i 914 put tc_ps_mod i tc_pb_get add 60 i 915 put tc_ps_dat i tc_pb_get add 92 i 916 put tc_ps_dig i get tc_sh_out i 917 end 918 off = TC_PS_HEADER 919 for t ulong n bound 500000 920 rc2 = tc_ps_parse_rec t off k 921 if ne rc2 TC_OK 922 ret rc2 923 end 924 off = add off rb 925 end 926 ret TC_OK 927 end 928 929 note ---------------------------------------------------------------- TSY1 serialize and parse 930 931 fn tc_sy_serialize use_dsy ulong to int 932 k = ulong 0 933 n = ulong 0 934 if eq use_dsy 0 935 k = get tc_sy_k 0 936 n = get tc_sy_n 0 937 else 938 k = get tc_dsy_k 0 939 n = get tc_dsy_n 0 940 end 941 if lt k TC_KMIN 942 tc_why_set TC_W_ARG 943 ret TC_E_ARG 944 end 945 if gt k TC_KMAX 946 tc_why_set TC_W_ARG 947 ret TC_E_ARG 948 end 949 body = add TC_SY_HEADER n 950 if gt add body TC_DIGEST TC_SB_CAP 951 tc_why_set TC_W_IO 952 ret TC_E_IO 953 end 954 for t ulong n bound 500000 955 sv = ulong 0 956 if eq use_dsy 0 957 sv = ulong get tc_sy_sym t 958 else 959 sv = ulong get tc_dsy_sym t 960 end 961 if ge sv k 962 tc_why_set TC_W_SYM 963 ret TC_E_SYMBOL 964 end 965 end 966 if eq use_dsy 0 967 for i ulong 56 968 put tc_sb i 0 969 end 970 put tc_sb 0 84 971 put tc_sb 1 83 972 put tc_sb 2 89 973 put tc_sb 3 49 974 tc_sb_put_u16 4 TC_SY_VERSION 975 put tc_sb 6 uchar k 976 tc_sb_put_u64 8 n 977 for i ulong 32 978 put tc_sb add 24 i get tc_sy_dat i 979 end 980 for t ulong n bound 500000 981 put tc_sb add TC_SY_HEADER t get tc_sy_sym t 982 end 983 tc_sha_init 984 tc_sha_dom TC_DOM_SY 985 for i ulong body bound 524288 986 tc_sha_byte get tc_sb i 987 end 988 tc_sha_final 989 for i ulong 32 990 bv = get tc_sh_out i 991 put tc_sb add body i bv 992 put tc_sy_dig i bv 993 end 994 put tc_sb_len 0 add body TC_DIGEST 995 else 996 for i ulong 56 997 put tc_db i 0 998 end 999 put tc_db 0 84 1000 put tc_db 1 83 1001 put tc_db 2 89 1002 put tc_db 3 49 1003 tc_db_put_u16 4 TC_SY_VERSION 1004 put tc_db 6 uchar k 1005 tc_db_put_u64 8 n 1006 for i ulong 32 1007 put tc_db add 24 i get tc_dsy_dat i 1008 end 1009 for t ulong n bound 500000 1010 put tc_db add TC_SY_HEADER t get tc_dsy_sym t 1011 end 1012 tc_sha_init 1013 tc_sha_dom TC_DOM_SY 1014 for i ulong body bound 524288 1015 tc_sha_byte get tc_db i 1016 end 1017 tc_sha_final 1018 for i ulong 32 1019 bv = get tc_sh_out i 1020 put tc_db add body i bv 1021 put tc_dsy_dig i bv 1022 end 1023 put tc_db_len 0 add body TC_DIGEST 1024 end 1025 ret TC_OK 1026 end 1027 1028 fn tc_sy_parse slen ulong to int 1029 if lt slen add TC_SY_HEADER TC_DIGEST 1030 tc_why_set TC_W_FMT_SY 1031 ret TC_E_FORMAT 1032 end 1033 if ne get tc_sb 0 84 1034 tc_why_set TC_W_FMT_SY 1035 ret TC_E_FORMAT 1036 end 1037 if ne get tc_sb 1 83 1038 tc_why_set TC_W_FMT_SY 1039 ret TC_E_FORMAT 1040 end 1041 if ne get tc_sb 2 89 1042 tc_why_set TC_W_FMT_SY 1043 ret TC_E_FORMAT 1044 end 1045 if ne get tc_sb 3 49 1046 tc_why_set TC_W_FMT_SY 1047 ret TC_E_FORMAT 1048 end 1049 body = sub slen TC_DIGEST 1050 tc_sha_init 1051 tc_sha_dom TC_DOM_SY 1052 for i ulong body bound 524288 1053 tc_sha_byte get tc_sb i 1054 end 1055 tc_sha_final 1056 for i ulong 32 1057 if ne get tc_sh_out i get tc_sb add body i 1058 tc_why_set TC_W_DIG_SY 1059 ret TC_E_DIGEST 1060 end 1061 end 1062 ver = tc_sb_get_u16 4 1063 k = ulong get tc_sb 6 1064 r7 = ulong get tc_sb 7 1065 r16 = tc_sb_get_u64 16 1066 n = tc_sb_get_u64 8 1067 if ne ver TC_SY_VERSION 1068 tc_why_set TC_W_FMT_SY 1069 ret TC_E_FORMAT 1070 end 1071 if ne r7 0 1072 tc_why_set TC_W_FMT_SY 1073 ret TC_E_FORMAT 1074 end 1075 if ne r16 0 1076 tc_why_set TC_W_FMT_SY 1077 ret TC_E_FORMAT 1078 end 1079 if lt k TC_KMIN 1080 tc_why_set TC_W_FMT_SY 1081 ret TC_E_FORMAT 1082 end 1083 if gt k TC_KMAX 1084 tc_why_set TC_W_FMT_SY 1085 ret TC_E_FORMAT 1086 end 1087 if ne n sub body TC_SY_HEADER 1088 tc_why_set TC_W_CNT_SY 1089 ret TC_E_COUNT 1090 end 1091 if gt n TC_MAX_N 1092 tc_why_set TC_W_IO 1093 ret TC_E_IO 1094 end 1095 for t ulong n bound 500000 1096 sv = ulong get tc_sb add TC_SY_HEADER t 1097 if ge sv k 1098 tc_why_set TC_W_SYM 1099 ret TC_E_SYMBOL 1100 end 1101 put tc_sy_sym t uchar sv 1102 end 1103 put tc_sy_k 0 k 1104 put tc_sy_n 0 n 1105 for i ulong 32 1106 put tc_sy_dat i get tc_sb add 24 i 1107 put tc_sy_dig i get tc_sh_out i 1108 end 1109 ret TC_OK 1110 end 1111 1112 note ---------------------------------------------------------------- expect, pair, coded header 1113 1114 fn tc_ps_expect chk_p ulong chk_m ulong chk_d ulong to int 1115 if ne chk_p 0 1116 for i ulong 32 1117 if ne get tc_exp_prof i get tc_ps_prof i 1118 tc_why_set TC_W_PROF 1119 ret TC_E_PROFILE 1120 end 1121 end 1122 end 1123 if ne chk_m 0 1124 for i ulong 32 1125 if ne get tc_exp_mod i get tc_ps_mod i 1126 tc_why_set TC_W_MOD 1127 ret TC_E_MODEL 1128 end 1129 end 1130 end 1131 if ne chk_d 0 1132 for i ulong 32 1133 if ne get tc_exp_dat i get tc_ps_dat i 1134 tc_why_set TC_W_DAT 1135 ret TC_E_DATASET 1136 end 1137 end 1138 end 1139 ret TC_OK 1140 end 1141 1142 fn tc_pair_check to int 1143 pk = get tc_ps_k 0 1144 pn = get tc_ps_n 0 1145 sk = get tc_sy_k 0 1146 sn = get tc_sy_n 0 1147 if ne pk sk 1148 tc_why_set TC_W_PAIR_CNT 1149 ret TC_E_COUNT 1150 end 1151 if ne pn sn 1152 tc_why_set TC_W_PAIR_CNT 1153 ret TC_E_COUNT 1154 end 1155 for i ulong 32 1156 if ne get tc_ps_dat i get tc_sy_dat i 1157 tc_why_set TC_W_PAIR_DAT 1158 ret TC_E_DATASET 1159 end 1160 end 1161 for t ulong sn bound 500000 1162 if ge ulong get tc_sy_sym t sk 1163 tc_why_set TC_W_SYM 1164 ret TC_E_SYMBOL 1165 end 1166 end 1167 ret TC_OK 1168 end 1169 1170 fn tc_coded_hdr_write cid ulong payload ulong to void 1171 for i ulong 56 1172 put tc_cb i 0 1173 end 1174 put tc_cb 0 84 1175 put tc_cb 1 67 1176 if eq cid 1 1177 put tc_cb 2 82 1178 else 1179 put tc_cb 2 65 1180 end 1181 put tc_cb 3 49 1182 put tc_cb 4 uchar TC_CODED_VERSION 1183 put tc_cb 5 uchar cid 1184 tc_cb_put_u64 8 get tc_ps_n 0 1185 for i ulong 32 1186 put tc_cb add 16 i get tc_ps_dig i 1187 end 1188 tc_cb_put_u64 48 payload 1189 end 1190 1191 fn tc_coded_hdr_check cid ulong clen ulong to int 1192 if lt clen TC_CODED_HEADER 1193 tc_why_set TC_W_CHDR 1194 ret TC_E_HEADER 1195 end 1196 m2 = uchar 65 1197 if eq cid 1 1198 m2 = uchar 82 1199 end 1200 if ne get tc_cb 0 84 1201 tc_why_set TC_W_CHDR 1202 ret TC_E_HEADER 1203 end 1204 if ne get tc_cb 1 67 1205 tc_why_set TC_W_CHDR 1206 ret TC_E_HEADER 1207 end 1208 if ne get tc_cb 2 m2 1209 tc_why_set TC_W_CHDR 1210 ret TC_E_HEADER 1211 end 1212 if ne get tc_cb 3 49 1213 tc_why_set TC_W_CHDR 1214 ret TC_E_HEADER 1215 end 1216 if ne ulong get tc_cb 4 TC_CODED_VERSION 1217 tc_why_set TC_W_CHDR 1218 ret TC_E_HEADER 1219 end 1220 if ne ulong get tc_cb 5 cid 1221 tc_why_set TC_W_CHDR 1222 ret TC_E_HEADER 1223 end 1224 if ne get tc_cb 6 0 1225 tc_why_set TC_W_CHDR 1226 ret TC_E_HEADER 1227 end 1228 if ne get tc_cb 7 0 1229 tc_why_set TC_W_CHDR 1230 ret TC_E_HEADER 1231 end 1232 for i ulong 32 1233 if ne get tc_cb add 16 i get tc_ps_dig i 1234 tc_why_set TC_W_CBIND 1235 ret TC_E_BINDING 1236 end 1237 end 1238 n = tc_cb_get_u64 8 1239 pl = tc_cb_get_u64 48 1240 if ne n get tc_ps_n 0 1241 tc_why_set TC_W_CCNT 1242 ret TC_E_COUNT 1243 end 1244 avail = sub clen TC_CODED_HEADER 1245 if gt pl avail 1246 tc_why_set TC_W_CTRUNC 1247 ret TC_E_TRUNC 1248 end 1249 if lt pl avail 1250 tc_why_set TC_W_CTRAIL 1251 ret TC_E_TRAIL 1252 end 1253 put tc_cb_plen 0 pl 1254 ret TC_OK 1255 end 1256 1257 note ---------------------------------------------------------------- whole-file I/O 1258 1259 fn tc_copy_io_pb tot ulong kn ulong to void 1260 for i ulong kn bound 65536 1261 tc_pb_put add tot i get tc_iobuf i 1262 end 1263 end 1264 1265 fn tc_copy_io_sb tot ulong kn ulong to void 1266 for i ulong kn bound 65536 1267 put tc_sb add tot i get tc_iobuf i 1268 end 1269 end 1270 1271 fn tc_copy_io_cb tot ulong kn ulong to void 1272 for i ulong kn bound 65536 1273 put tc_cb add tot i get tc_iobuf i 1274 end 1275 end 1276 1277 fn tc_copy_pb_io pos ulong chk ulong to void 1278 for i ulong chk bound 65536 1279 put tc_iobuf i tc_pb_get add pos i 1280 end 1281 end 1282 1283 fn tc_write_iobuf fd int chk ulong to int 1284 wo = ulong 0 1285 while lt wo chk bound 65536 1286 k = write fd tc_iobuf wo sub chk wo 1287 if le k 0 1288 ret TC_E_IO 1289 end 1290 wo = add wo ulong k 1291 end 1292 ret TC_OK 1293 end 1294 1295 fn tc_read_pb path view uchar * off ulong plen ulong to int 1296 fd = fopen path off plen 0 0 1297 if lt fd 0 1298 tc_why_set TC_W_IO 1299 ret TC_E_IO 1300 end 1301 tot = ulong 0 1302 while true bound `1000000` 1303 k = read int fd tc_iobuf 0 65536 1304 if lt k 0 1305 z = fclose int fd 1306 tc_why_set TC_W_IO 1307 ret TC_E_IO 1308 end 1309 if eq k 0 1310 break 1311 end 1312 kn = ulong k 1313 if gt add tot kn TC_PB_CAP 1314 z = fclose int fd 1315 tc_why_set TC_W_IO 1316 ret TC_E_IO 1317 end 1318 tc_copy_io_pb tot kn 1319 tot = add tot kn 1320 end 1321 z = fclose int fd 1322 put tc_pb_len 0 tot 1323 ret TC_OK 1324 end 1325 1326 fn tc_write_pb path view uchar * off ulong plen ulong to int 1327 fd = fopen path off plen 577 420 1328 if lt fd 0 1329 tc_why_set TC_W_IO 1330 ret TC_E_IO 1331 end 1332 tot = get tc_pb_len 0 1333 pos = ulong 0 1334 while lt pos tot bound 1000 1335 remn = sub tot pos 1336 chk = ulong 65536 1337 if lt remn chk 1338 chk = remn 1339 end 1340 tc_copy_pb_io pos chk 1341 if ne tc_write_iobuf int fd chk TC_OK 1342 z = fclose int fd 1343 tc_why_set TC_W_IO 1344 ret TC_E_IO 1345 end 1346 pos = add pos chk 1347 end 1348 z = fclose int fd 1349 if ne z 0 1350 tc_why_set TC_W_IO 1351 ret TC_E_IO 1352 end 1353 ret TC_OK 1354 end 1355 1356 fn tc_read_sb path view uchar * off ulong plen ulong to int 1357 fd = fopen path off plen 0 0 1358 if lt fd 0 1359 tc_why_set TC_W_IO 1360 ret TC_E_IO 1361 end 1362 tot = ulong 0 1363 while true bound 1000 1364 k = read int fd tc_iobuf 0 65536 1365 if lt k 0 1366 z = fclose int fd 1367 tc_why_set TC_W_IO 1368 ret TC_E_IO 1369 end 1370 if eq k 0 1371 break 1372 end 1373 kn = ulong k 1374 if gt add tot kn TC_SB_CAP 1375 z = fclose int fd 1376 tc_why_set TC_W_IO 1377 ret TC_E_IO 1378 end 1379 tc_copy_io_sb tot kn 1380 tot = add tot kn 1381 end 1382 z = fclose int fd 1383 put tc_sb_len 0 tot 1384 ret TC_OK 1385 end 1386 1387 fn tc_write_sb path view uchar * off ulong plen ulong use_db ulong to int 1388 fd = fopen path off plen 577 420 1389 if lt fd 0 1390 tc_why_set TC_W_IO 1391 ret TC_E_IO 1392 end 1393 tot = get tc_sb_len 0 1394 if ne use_db 0 1395 tot = get tc_db_len 0 1396 end 1397 pos = ulong 0 1398 while lt pos tot bound 1000 1399 k = long 0 1400 if eq use_db 0 1401 k = write int fd tc_sb pos sub tot pos 1402 else 1403 k = write int fd tc_db pos sub tot pos 1404 end 1405 if le k 0 1406 z = fclose int fd 1407 tc_why_set TC_W_IO 1408 ret TC_E_IO 1409 end 1410 pos = add pos ulong k 1411 end 1412 z = fclose int fd 1413 if ne z 0 1414 tc_why_set TC_W_IO 1415 ret TC_E_IO 1416 end 1417 ret TC_OK 1418 end 1419 1420 fn tc_read_cb path view uchar * off ulong plen ulong to int 1421 fd = fopen path off plen 0 0 1422 if lt fd 0 1423 tc_why_set TC_W_IO 1424 ret TC_E_IO 1425 end 1426 tot = ulong 0 1427 while true bound 1000 1428 k = read int fd tc_iobuf 0 65536 1429 if lt k 0 1430 z = fclose int fd 1431 tc_why_set TC_W_IO 1432 ret TC_E_IO 1433 end 1434 if eq k 0 1435 break 1436 end 1437 kn = ulong k 1438 if gt add tot kn TC_CB_CAP 1439 z = fclose int fd 1440 tc_why_set TC_W_IO 1441 ret TC_E_IO 1442 end 1443 tc_copy_io_cb tot kn 1444 tot = add tot kn 1445 end 1446 z = fclose int fd 1447 put tc_cb_len 0 tot 1448 ret TC_OK 1449 end 1450 1451 fn tc_write_cb path view uchar * off ulong plen ulong to int 1452 fd = fopen path off plen 577 420 1453 if lt fd 0 1454 tc_why_set TC_W_IO 1455 ret TC_E_IO 1456 end 1457 tot = get tc_cb_len 0 1458 pos = ulong 0 1459 while lt pos tot bound 1000 1460 k = write int fd tc_cb pos sub tot pos 1461 if le k 0 1462 z = fclose int fd 1463 tc_why_set TC_W_IO 1464 ret TC_E_IO 1465 end 1466 pos = add pos ulong k 1467 end 1468 z = fclose int fd 1469 if ne z 0 1470 tc_why_set TC_W_IO 1471 ret TC_E_IO 1472 end 1473 ret TC_OK 1474 end