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/interplane-forge/blob/main/chain/chain.om # interplane-forge INTERPLANE Forge Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `interplane-forge / chain / chain.om` 1289 lines ยท 25205 bytes Raw Blame History 1 note chain.om: the INTERPLANE ledger, a hash chain anyone can check. Composed after om/tls/sha2.om. 2 note chain add LEDGER KIND ACTOR SUBJECT < payload append one record; prints "OK seq head" 3 note chain verify LEDGER prints "OK count head" or "BAD line N code" 4 note chain check LEDGER SEQ < payload the payload is the one record SEQ committed to 5 note chain feed LEDGER FILE KIND ACTOR [PAGE] one record per new complete line of FILE; then 6 note the page, when PAGE is given 7 note chain page LEDGER OUT write OUT: the head as a page with the check by hand 8 note feed records line N of FILE with the subject " line N" and the SHA-256 of the line's 9 note bytes (without its line feed) as payload. It resumes after the highest N already recorded for 10 note that KIND and file name, so running it again adds only new lines. A last line without a line 11 note feed is still being written and is left for the next run. Only the digest enters the ledger. 12 note FILE may not be the ledger itself, by any name (exit 1, nothing written). 13 note A record is one line of seven tab-separated fields, ended by a line feed: 14 note seq utc kind actor subject payload prev 15 note seq counts from 1. utc is YYYY-MM-DDTHH:MM:SSZ and never goes backwards. payload is the SHA-256 16 note of the payload bytes, 64 lowercase hex digits. prev is the SHA-256 of the previous record's line 17 note (its bytes without the line feed), or the word GENESIS in record 1. The head of a ledger is the 18 note SHA-256 of its last line. Changing, removing, inserting or reordering any record changes the head 19 note unless a SHA-256 collision is found (Ledger.lean, theorem tamper_evident). 20 note Without this program: printf %s "LINE" | sha256sum gives the next record's prev. 21 note add locks the ledger, verifies all of it, and refuses to append to a ledger that does not verify. 22 note Exit 0 done; 1 usage or a bad argument; 2 the ledger does not verify; 3 read or write failed; 23 note 4 check: no such record, or the payload is not the one committed; 5 feed: FILE has fewer 24 note complete lines than were already recorded from it (nothing is written). 25 note BAD codes: 1 not seven fields, or a byte that is not a tab or printable ASCII; 2 seq (no leading 26 note zero); 3 utc (a real date and time, never earlier than the record before); 4 payload digest; 27 note 5 prev; 6 line longer than 1,023 bytes; 7 last line has no line feed; 8 kind, actor or subject 28 note breaks its rule (kind 1..24 bytes of A-Z 0-9 _, actor 1..64 bytes, subject 1..128 bytes). 29 note page verifies the ledger first and writes nothing when it does not verify (exit 2). OUT is 30 note written as OUT.tmp (created exclusively) and renamed, so a reader never sees half a page. A 31 note page name, or its .tmp, that is the ledger or the fed file is refused (exit 1). 32 note Commands are matched as whole words: add, verify, check, feed, page. 33 global gl uchar 1024 34 global gll ulong 1 35 global gh uchar 64 36 global ghl ulong 1 37 global gph uchar 64 38 global gq uchar 64 39 global gu uchar 20 40 global gs ulong 8 41 global ga uchar 4096 42 global gw uchar 2048 43 global gwl ulong 1 44 global gb uchar 65536 45 global gx uchar 512 46 global gxl ulong 4 47 text tgen "GENESIS" 48 text tok "OK\t" 49 text tbad "BAD\tline\t" 50 text tadd "add" 51 text tver "verify" 52 text tchk "check" 53 text tfeed "feed" 54 text tline " line " 55 text tpage "page" 56 text ttmp ".tmp" 57 text tnone "no records yet" 58 text q01 "Check the record yourself - INTERPLANE
\n" 59 text q02 "

Proof

Check the record yourself

\n

The agents that build INTERPLANE talk on one message board. Each line of that board is recorded here by its SHA-256 digest, and each record names the digest of the record before it. You do not need to trust this page, this site or its author. Download the ledger and check it with tools already on your machine.

\n
Records
" 60 text q03 "
Last record (UTC)
" 61 text q04 "
\n

The head

\n

The head is the SHA-256 of the ledger's last line. Change, remove, add or move any earlier record and the head changes.

\n
"

62 text q05 "
\n

Check it by hand

\n

Get the ledger and compute its head. It must be the line above. Where there is no sha256sum (macOS), shasum -a 256 does the same.

\n
curl -s https://interplane.io/proof/board.tsv -o board.tsv\nprintf %s "$(tail -n 1 board.tsv)" | sha256sum
\n" 63 text q06 "

Then check every link. Field 7 of each record must be the SHA-256 of the line above it.

\n
awk 'NR>1' board.tsv | cut -f7 > claimed\nsed '$d' board.tsv | while IFS= read -r line; do printf %s "$line" | sha256sum | cut -d' ' -f1; done > computed\ncmp claimed computed && echo chain intact
\n" 64 text q07 "

Prove one message

\n

Only digests are public; the board itself is not. Whoever holds line N of the board, as it was when the line was recorded, can prove it is the line that was recorded: its SHA-256 is field 6 of the record whose subject is BOARD.md line N. Write the line number in place of N; the command reads the line from the file, so nothing in it needs quoting.

\n
printf %s "$(sed -n 'Np' BOARD.md)" | sha256sum
\n" 65 text q08 "

What this proves, and what it does not

\n

Proved in Lean 4 with no axioms: two valid ledgers with the same head are the same ledger, unless SHA-256 gives one digest for two different lines. Nobody is known to be able to do that; it is assumed, not proved.

\n

Not proved by the chain alone: when a record was written, and who or what wrote a message. A ledger kept on one machine can be rebuilt by whoever holds it. Time comes from outside, from publishing each head where it cannot be taken back.

\n
\n" 66 global gp uchar 16384 67 global gpl ulong 1 68 global go uchar 2100 69 note gs: 0 records accepted, 1 first BAD code, 2 its line number, 3 wanted seq, 4 wanted seq found, 70 note 5 feed mode, 6 highest source line already recorded for the fed kind and file name 71 note gx: the fed kind at 0 (length gxl 0), actor at 64 (gxl 1), file name at 192 (gxl 2) 72 fn hexc v ulong to uchar 73 if lt v 10 74 ret uchar add 48 v 75 end 76 ret uchar add 87 v 77 end 78 note hline: gh = hex SHA-256 of the current line gl 79 fn hline to void 80 t = new uchar 1024 0 81 d = new uchar 32 0 82 n = get gll 0 83 i = ulong 0 84 while lt i n bound 1024 85 put t i get gl i 86 i = add i 1 87 end 88 k = sha2_digest 256 part t 0 n d 89 j = ulong 0 90 while lt j 32 bound 32 91 b = ulong get d j 92 put gh mul j 2 hexc shr b 4 93 put gh add mul j 2 1 hexc and b 15 94 j = add j 1 95 end 96 put ghl 0 64 97 end 98 note phash: gph = hex SHA-256 of everything on stdin; false when a read fails 99 fn phash to bool 100 c = new ulong 40 0 101 b = new uchar 65536 0 102 d = new uchar 32 0 103 sha256_init c 104 k = long 1 105 while gt k 0 bound `9223372036854775807` 106 k = read 0 b 0 65536 107 if gt k 0 108 sha256_update c part b 0 ulong k 109 end 110 end 111 sha256_final c d 112 j = ulong 0 113 while lt j 32 bound 32 114 x = ulong get d j 115 put gph mul j 2 hexc shr x 4 116 put gph add mul j 2 1 hexc and x 15 117 j = add j 1 118 end 119 ret eq k 0 120 end 121 note ---- the current line gl: field k starts at lfs k and ends at lfe of that start 122 fn lfe a ulong to ulong 123 n = get gll 0 124 i = a 125 while lt i n bound 1024 126 if eq get gl i 9 127 ret i 128 end 129 i = add i 1 130 end 131 ret n 132 end 133 fn lfs k ulong to ulong 134 p = ulong 0 135 j = ulong 0 136 while lt j k bound 8 137 p = add lfe p 1 138 j = add j 1 139 end 140 ret p 141 end 142 note lshape: exactly six tabs and every other byte printable ASCII (32..126) 143 fn lshape to bool 144 n = get gll 0 145 t = ulong 0 146 i = ulong 0 147 while lt i n bound 1024 148 c = get gl i 149 if eq c 9 150 t = add t 1 151 else 152 if or lt c 32 gt c 126 153 ret false 154 end 155 end 156 i = add i 1 157 end 158 ret eq t 6 159 end 160 note lnum: the decimal number in gl a..b; 0 when it is not 1 to 18 digits or has a leading zero 161 fn lnum a ulong b ulong to ulong 162 if ge a b 163 ret 0 164 end 165 if gt sub b a 18 166 ret 0 167 end 168 if eq get gl a 48 169 ret 0 170 end 171 v = ulong 0 172 i = a 173 while lt i b bound 18 174 c = get gl i 175 if or lt c 48 gt c 57 176 ret 0 177 end 178 v = add mul v 10 ulong sub c 48 179 i = add i 1 180 end 181 ret v 182 end 183 fn ldig i ulong to bool 184 c = get gl i 185 ret and ge c 48 le c 57 186 end 187 note l2: the two decimal digits at gl a as a number (the caller checked they are digits) 188 fn l2 a ulong to ulong 189 x = ulong get gl a 190 y = ulong get gl add a 1 191 ret add mul sub x 48 10 sub y 48 192 end 193 note ldim: days in month m of year y (proleptic Gregorian) 194 fn ldim y ulong m ulong to ulong 195 if eq m 2 196 if and eq rem y 4 0 or ne rem y 100 0 eq rem y 400 0 197 ret 29 198 end 199 ret 28 200 end 201 if or or eq m 4 eq m 6 or eq m 9 eq m 11 202 ret 30 203 end 204 ret 31 205 end 206 note lreal: the utc at gl a (digits already checked) names a real date and time 207 fn lreal a ulong to bool 208 m = l2 add a 5 209 if or lt m 1 gt m 12 210 ret false 211 end 212 d = l2 add a 8 213 y = add mul l2 a 100 l2 add a 2 214 if or lt d 1 gt d ldim y m 215 ret false 216 end 217 if gt l2 add a 11 23 218 ret false 219 end 220 if gt l2 add a 14 59 221 ret false 222 end 223 if gt l2 add a 17 59 224 ret false 225 end 226 ret true 227 end 228 note lword: field a..b is 1..cap bytes; with up set, each byte is A-Z, 0-9 or _ 229 fn lword a ulong b ulong cap ulong up bool to bool 230 if or eq a b gt sub b a cap 231 ret false 232 end 233 if not up 234 ret true 235 end 236 i = a 237 while lt i b bound 1024 238 c = get gl i 239 ok = false 240 if and ge c 65 le c 90 241 ok = true 242 end 243 if and ge c 48 le c 57 244 ok = true 245 end 246 if eq c 95 247 ok = true 248 end 249 if not ok 250 ret false 251 end 252 i = add i 1 253 end 254 ret true 255 end 256 note lutc: field a..b is YYYY-MM-DDTHH:MM:SSZ and not before the last accepted record's 257 fn lutc a ulong b ulong to bool 258 if ne sub b a 20 259 ret false 260 end 261 i = ulong 0 262 while lt i 20 bound 20 263 c = get gl add a i 264 w = uchar 0 265 if eq i 4 266 w = uchar 45 267 end 268 if eq i 7 269 w = uchar 45 270 end 271 if eq i 10 272 w = uchar 84 273 end 274 if eq i 13 275 w = uchar 58 276 end 277 if eq i 16 278 w = uchar 58 279 end 280 if eq i 19 281 w = uchar 90 282 end 283 if eq w 0 284 if not ldig add a i 285 ret false 286 end 287 else 288 if ne c w 289 ret false 290 end 291 end 292 i = add i 1 293 end 294 if not lreal a 295 ret false 296 end 297 if gt get gs 0 0 298 j = ulong 0 299 while lt j 20 bound 20 300 x = get gl add a j 301 y = get gu j 302 if gt x y 303 ret true 304 end 305 if lt x y 306 ret false 307 end 308 j = add j 1 309 end 310 end 311 ret true 312 end 313 fn lhex a ulong b ulong to bool 314 if ne sub b a 64 315 ret false 316 end 317 i = a 318 while lt i b bound 64 319 c = get gl i 320 ok = false 321 if and ge c 48 le c 57 322 ok = true 323 end 324 if and ge c 97 le c 102 325 ok = true 326 end 327 if not ok 328 ret false 329 end 330 i = add i 1 331 end 332 ret true 333 end 334 note lprev: field a..b equals the expected prev (gh, ghl bytes) 335 fn lprev a ulong b ulong to bool 336 n = get ghl 0 337 if ne sub b a n 338 ret false 339 end 340 i = ulong 0 341 while lt i n bound 64 342 if ne get gl add a i get gh i 343 ret false 344 end 345 i = add i 1 346 end 347 ret true 348 end 349 note lcheck: 0 when the current line is the next valid record, else its BAD code 350 fn lcheck to ulong 351 if not lshape 352 ret 1 353 end 354 if ne lnum 0 lfe 0 add get gs 0 1 355 ret 2 356 end 357 u = lfs 1 358 if not lutc u lfe u 359 ret 3 360 end 361 k = lfs 2 362 if not lword k lfe k 24 true 363 ret 8 364 end 365 k = lfs 3 366 if not lword k lfe k 64 false 367 ret 8 368 end 369 k = lfs 4 370 if not lword k lfe k 128 false 371 ret 8 372 end 373 p = lfs 5 374 if not lhex p lfe p 375 ret 4 376 end 377 q = lfs 6 378 if not lprev q lfe q 379 ret 5 380 end 381 ret 0 382 end 383 note laccept: the current line passed lcheck; remember its time, its digest, and the wanted record 384 fn laccept to void 385 u = lfs 1 386 i = ulong 0 387 while lt i 20 bound 20 388 put gu i get gl add u i 389 i = add i 1 390 end 391 n = add get gs 0 1 392 if eq n get gs 3 393 p = lfs 5 394 j = ulong 0 395 while lt j 64 bound 64 396 put gq j get gl add p j 397 j = add j 1 398 end 399 put gs 4 1 400 end 401 ltrack 402 hline 403 put gs 0 n 404 end 405 fn lbad code ulong to void 406 if eq get gs 1 0 407 put gs 1 code 408 put gs 2 add get gs 0 1 409 end 410 end 411 fn lend to void 412 if eq get gs 1 0 413 c = lcheck 414 if eq c 0 415 laccept 416 else 417 lbad c 418 end 419 end 420 put gll 0 0 421 end 422 note vfy: read the whole ledger from fd. false when a read fails; gs 1 holds the first BAD code. 423 fn vfy fd int to bool 424 put gs 0 0 425 put gs 1 0 426 put gs 4 0 427 put gll 0 0 428 put ghl 0 7 429 for g ulong 7 430 put gh g get tgen g 431 end 432 k = long 1 433 while gt k 0 bound `9223372036854775807` 434 k = read fd gb 0 65536 435 i = ulong 0 436 while lt long i k bound 65536 437 c = get gb i 438 if eq c 10 439 lend 440 else 441 m = get gll 0 442 if lt m 1023 443 put gl m c 444 put gll 0 add m 1 445 else 446 lbad 6 447 end 448 end 449 i = add i 1 450 end 451 end 452 if gt get gll 0 0 453 lbad 7 454 end 455 ret eq k 0 456 end 457 note ---- output line gw 458 fn wab c uchar to void 459 o = get gwl 0 460 if lt o 2047 461 put gw o c 462 put gwl 0 add o 1 463 end 464 end 465 fn wa t view uchar * to void 466 for i ulong len t bound 64 467 wab get t i 468 end 469 end 470 fn wan v ulong to void 471 d = new uchar 20 0 472 n = ulong 0 473 x = v 474 if eq x 0 475 wab 48 476 end 477 while gt x 0 bound 20 478 put d n uchar add 48 rem x 10 479 x = div x 10 480 n = add n 1 481 end 482 while gt n 0 bound 20 483 n = sub n 1 484 wab get d n 485 end 486 end 487 fn wa2 v ulong to void 488 wab uchar add 48 rem div v 10 10 489 wab uchar add 48 rem v 10 490 end 491 fn whead to void 492 n = get ghl 0 493 i = ulong 0 494 while lt i n bound 64 495 wab get gh i 496 i = add i 1 497 end 498 end 499 note wymd: days since 1970-01-01 as YYYY-MM-DD (proleptic Gregorian; the gate checks it against date -u) 500 fn wymd days ulong to void 501 z = add days 719468 502 era = div z 146097 503 doe = sub z mul era 146097 504 t = sub doe div doe 1460 505 t = add t div doe 36524 506 t = sub t div doe 146096 507 yoe = div t 365 508 y = add yoe mul era 400 509 doy = sub doe sub add mul 365 yoe div yoe 4 div yoe 100 510 mp = div add mul 5 doy 2 153 511 d = add sub doy div add mul 153 mp 2 5 1 512 m = add mp 3 513 if ge mp 10 514 m = sub mp 9 515 end 516 if le m 2 517 y = add y 1 518 end 519 wan y 520 wab 45 521 wa2 m 522 wab 45 523 wa2 d 524 end 525 fn wutc s ulong to void 526 r = rem s 86400 527 wymd div s 86400 528 wab 84 529 wa2 div r 3600 530 wab 58 531 wa2 div rem r 3600 60 532 wab 58 533 wa2 rem r 60 534 wab 90 535 end 536 note warg: append argument idx; false unless it is 1..cap bytes, each printable ASCII. With up 537 note set, only A-Z, 0-9 and _ pass (the kind field). 538 fn warg idx ulong cap ulong up bool to bool 539 n = argread idx ga 0 4096 540 if lt n 1 541 ret false 542 end 543 if gt ulong n cap 544 ret false 545 end 546 i = ulong 0 547 while lt i ulong n bound 4096 548 c = get ga i 549 if or lt c 32 gt c 126 550 ret false 551 end 552 if up 553 ok = false 554 if and ge c 65 le c 90 555 ok = true 556 end 557 if and ge c 48 le c 57 558 ok = true 559 end 560 if eq c 95 561 ok = true 562 end 563 if not ok 564 ret false 565 end 566 end 567 wab c 568 i = add i 1 569 end 570 ret true 571 end 572 fn wout fd int to bool 573 n = get gwl 0 574 o = ulong 0 575 while lt o n bound 2048 576 k = write fd gw o sub n o 577 if le k 0 578 ret false 579 end 580 o = add o ulong k 581 end 582 ret true 583 end 584 note ropen: open the ledger named by argument 2 with flags; the descriptor or a negative errno 585 fn ropen flags int to int 586 n = argread 2 ga 0 4095 587 if lt n 1 588 ret -1 589 end 590 ret int fopen ga 0 ulong n flags 420 591 end 592 note argis: argument 1 is exactly the word t 593 fn argis t view uchar * to bool 594 n = argread 1 ga 0 16 595 if ne n long len t 596 ret false 597 end 598 for i ulong len t bound 16 599 if ne get ga i get t i 600 ret false 601 end 602 end 603 ret true 604 end 605 note say: print OK count head, or BAD line N code; the exit status that goes with it 606 fn say to int 607 put gwl 0 0 608 if ne get gs 1 0 609 wa tbad 610 wan get gs 2 611 wab 9 612 wan get gs 1 613 wab 10 614 r = wout 1 615 ret 2 616 end 617 wa tok 618 wan get gs 0 619 wab 9 620 whead 621 wab 10 622 if wout 1 623 ret 0 624 end 625 ret 3 626 end 627 fn cverify to int 628 fd = ropen 0 629 if lt fd 0 630 ret 3 631 end 632 ok = vfy fd 633 r = fclose fd 634 if not ok 635 ret 3 636 end 637 ret say 638 end 639 fn ccheck to int 640 n = argread 3 ga 0 32 641 if lt n 1 642 ret 1 643 end 644 i = ulong 0 645 while lt i ulong n bound 32 646 put gl i get ga i 647 i = add i 1 648 end 649 put gll 0 ulong n 650 want = lnum 0 ulong n 651 if eq want 0 652 ret 1 653 end 654 fd = ropen 0 655 if lt fd 0 656 ret 3 657 end 658 put gs 3 want 659 ok = vfy fd 660 r = fclose fd 661 if not ok 662 ret 3 663 end 664 if ne get gs 1 0 665 ret say 666 end 667 if eq get gs 4 0 668 ret 4 669 end 670 if not phash 671 ret 3 672 end 673 j = ulong 0 674 while lt j 64 bound 64 675 if ne get gph j get gq j 676 ret 4 677 end 678 j = add j 1 679 end 680 ret say 681 end 682 fn cadd to int 683 fd = ropen 1090 684 if lt fd 0 685 ret 3 686 end 687 if ne flock fd 2 0 688 ret 3 689 end 690 if not vfy fd 691 ret 3 692 end 693 if ne get gs 1 0 694 r = fclose fd 695 ret say 696 end 697 if not phash 698 ret 3 699 end 700 put gwl 0 0 701 wan add get gs 0 1 702 wab 9 703 wutc div ulong realns `1000000000` 704 wab 9 705 if not warg 3 24 true 706 ret 1 707 end 708 wab 9 709 if not warg 4 64 false 710 ret 1 711 end 712 wab 9 713 if not warg 5 128 false 714 ret 1 715 end 716 wab 9 717 for i ulong 64 718 wab get gph i 719 end 720 wab 9 721 whead 722 r = wcommit fd 723 if ne r 0 724 ret r 725 end 726 x = fclose fd 727 ret say 728 end 729 note wcommit: the line built in gw becomes the next record: it must itself be the next valid 730 note record, then it is accepted and written. add syncs it here; feed (gs 5) syncs once, after its 731 note last record, in cfeed. 0, or the exit code. 732 fn wcommit fd int to int 733 n = get gwl 0 734 if gt n 1023 735 ret 1 736 end 737 for k ulong n bound 1024 738 put gl k get gw k 739 end 740 put gll 0 n 741 if ne lcheck 0 742 ret 1 743 end 744 laccept 745 wab 10 746 if not wout fd 747 ret 3 748 end 749 if eq get gs 5 0 750 if ne fsync fd 0 751 ret 3 752 end 753 end 754 ret 0 755 end 756 note ---- feed 757 fn kch c uchar to bool 758 if and ge c 65 le c 90 759 ret true 760 end 761 if and ge c 48 le c 57 762 ret true 763 end 764 ret eq c 95 765 end 766 note sarg: argument idx, 1..cap bytes of printable ASCII (with up only A-Z 0-9 _), kept at gx off with 767 note its length in gxl slot. For slot 2 only the part after the last "/" is kept (the file name). 768 fn sarg idx ulong off ulong slot ulong cap ulong up bool to bool 769 n = argread idx ga 0 4096 770 if lt n 1 771 ret false 772 end 773 a = ulong 0 774 if eq slot 2 775 i = ulong 0 776 while lt i ulong n bound 4096 777 if eq get ga i 47 778 a = add i 1 779 end 780 i = add i 1 781 end 782 end 783 m = sub ulong n a 784 if or lt m 1 gt m cap 785 ret false 786 end 787 j = ulong 0 788 while lt j m bound 128 789 c = get ga add a j 790 if or lt c 32 gt c 126 791 ret false 792 end 793 if up 794 if not kch c 795 ret false 796 end 797 end 798 put gx add off j c 799 j = add j 1 800 end 801 put gxl slot m 802 ret true 803 end 804 note ltrack: in feed mode, when the accepted line has the fed kind and the subject 805 note " line N", remember the largest N 806 fn ltrack to void 807 if eq get gs 5 0 808 ret 809 end 810 k = lfs 2 811 if ne sub lfe k k get gxl 0 812 ret 813 end 814 i = ulong 0 815 while lt i get gxl 0 bound 24 816 if ne get gl add k i get gx i 817 ret 818 end 819 i = add i 1 820 end 821 s = lfs 4 822 e = lfe s 823 nl = get gxl 2 824 if lt sub e s add nl 7 825 ret 826 end 827 j = ulong 0 828 while lt j nl bound 128 829 if ne get gl add s j get gx add 192 j 830 ret 831 end 832 j = add j 1 833 end 834 p = add s nl 835 t = ulong 0 836 while lt t 6 bound 6 837 if ne get gl add p t get tline t 838 ret 839 end 840 t = add t 1 841 end 842 v = lnum add p 6 e 843 if gt v get gs 6 844 put gs 6 v 845 end 846 end 847 fn fhex d view uchar 32 to void 848 j = ulong 0 849 while lt j 32 bound 32 850 x = ulong get d j 851 put gph mul j 2 hexc shr x 4 852 put gph add mul j 2 1 hexc and x 15 853 j = add j 1 854 end 855 end 856 note frec: one record for source line n, whose digest is in gph. 0, or the exit code. 857 fn frec fd int n ulong to int 858 put gwl 0 0 859 wan add get gs 0 1 860 wab 9 861 wutc div ulong realns `1000000000` 862 wab 9 863 i = ulong 0 864 while lt i get gxl 0 bound 24 865 wab get gx i 866 i = add i 1 867 end 868 wab 9 869 j = ulong 0 870 while lt j get gxl 1 bound 64 871 wab get gx add 64 j 872 j = add j 1 873 end 874 wab 9 875 k = ulong 0 876 while lt k get gxl 2 bound 128 877 wab get gx add 192 k 878 k = add k 1 879 end 880 wa tline 881 wan n 882 wab 9 883 for h ulong 64 884 wab get gph h 885 end 886 wab 9 887 whead 888 ret wcommit fd 889 end 890 note floop: record every complete line of sf after line gs 6. 0, or the exit code. 891 fn floop fd int sf int to int 892 c = new ulong 40 0 893 b = new uchar 65536 0 894 d = new uchar 32 0 895 start = get gs 6 896 n = ulong 0 897 sha256_init c 898 k = long 1 899 while gt k 0 bound `9223372036854775807` 900 k = read sf b 0 65536 901 if gt k 0 902 s = ulong 0 903 i = ulong 0 904 while lt i ulong k bound 65536 905 if eq get b i 10 906 n = add n 1 907 if gt n start 908 if gt i s 909 sha256_update c part b s sub i s 910 end 911 sha256_final c d 912 fhex d 913 r = frec fd n 914 if ne r 0 915 ret r 916 end 917 sha256_init c 918 end 919 s = add i 1 920 end 921 i = add i 1 922 end 923 if ge n start 924 if lt s ulong k 925 sha256_update c part b s sub ulong k s 926 end 927 end 928 end 929 end 930 if lt k 0 931 ret 3 932 end 933 if lt n start 934 ret 5 935 end 936 ret 0 937 end 938 note fsame: 1 when descriptors a and b name one file (equal st_dev and st_ino, bytes 0..15 of the 939 note 128-byte stat), 0 when they do not, 3 when fstat fails. Feeding the ledger into itself would 940 note record every record it writes, and the file would never end. 941 fn fsame a int b int to int 942 s = new uchar 128 0 943 t = new uchar 128 0 944 if lt fstat a s 0 128 0 945 ret 3 946 end 947 if lt fstat b t 0 128 0 948 ret 3 949 end 950 for i ulong 16 951 if ne get s i get t i 952 ret 0 953 end 954 end 955 ret 1 956 end 957 fn cfeed to int 958 if not sarg 4 0 0 24 true 959 ret 1 960 end 961 if not sarg 5 64 1 64 false 962 ret 1 963 end 964 if not sarg 3 192 2 100 false 965 ret 1 966 end 967 if eq argcnt 7 968 pl = argread 6 ga 0 4095 969 if or lt pl 1 gt pl 1000 970 ret 1 971 end 972 end 973 m = argread 3 ga 0 4095 974 sf = int fopen ga 0 ulong m 0 0 975 if lt sf 0 976 ret 3 977 end 978 fd = ropen 1090 979 if lt fd 0 980 ret 3 981 end 982 if ne flock fd 2 0 983 ret 3 984 end 985 r = fsame sf fd 986 if ne r 0 987 ret r 988 end 989 if eq argcnt 7 990 r = pprep 6 991 if ne r 0 992 ret r 993 end 994 end 995 put gs 5 1 996 put gs 6 0 997 if not vfy fd 998 ret 3 999 end 1000 if ne get gs 1 0 1001 ret say 1002 end 1003 r = floop fd sf 1004 x = fclose sf 1005 if ne fsync fd 0 1006 ret 3 1007 end 1008 if ne r 0 1009 ret r 1010 end 1011 if eq argcnt 7 1012 pbuild 1013 w = pfile 1014 if ne w 0 1015 ret w 1016 end 1017 end 1018 y = fclose fd 1019 ret say 1020 end 1021 note ---- page 1022 note pb: one byte into the page; a page that would pass 16,383 bytes is marked as over (gpl 16384) 1023 note and pput refuses to write it, so it is never cut short in silence 1024 fn pb c uchar to void 1025 o = get gpl 0 1026 if lt o 16383 1027 put gp o c 1028 put gpl 0 add o 1 1029 else 1030 put gpl 0 16384 1031 end 1032 end 1033 note pt: a text of at most 4,096 bytes into the page 1034 fn pt t view uchar * to void 1035 n = len t 1036 i = ulong 0 1037 while lt i n bound 4096 1038 pb get t i 1039 i = add i 1 1040 end 1041 end 1042 note pn: a count with its digits grouped in threes 1043 fn pn v ulong to void 1044 d = new uchar 20 0 1045 n = ulong 0 1046 x = v 1047 if eq x 0 1048 pb 48 1049 end 1050 while gt x 0 bound 20 1051 put d n uchar add 48 rem x 10 1052 x = div x 10 1053 n = add n 1 1054 end 1055 while gt n 0 bound 20 1056 n = sub n 1 1057 pb get d n 1058 if gt n 0 1059 if eq rem n 3 0 1060 pb 44 1061 end 1062 end 1063 end 1064 end 1065 note pbuild: the page for the ledger just verified (gs 0 records, head in gh, last time in gu) 1066 fn pbuild to void 1067 put gpl 0 0 1068 pt q01 1069 pt q02 1070 pn get gs 0 1071 pt q03 1072 if eq get gs 0 0 1073 pt tnone 1074 else 1075 for i ulong 10 1076 pb get gu i 1077 end 1078 pb 32 1079 for j ulong 8 1080 pb get gu add 11 j 1081 end 1082 end 1083 pt q04 1084 if eq get gs 0 0 1085 pt tnone 1086 else 1087 for k ulong 64 1088 pb get gh k 1089 end 1090 end 1091 pt q05 1092 pt q06 1093 pt q07 1094 pt q08 1095 end 1096 note pput: the page into the new file named go[0, n), mode 0644, created exclusively (O_WRONLY, 1097 note O_CREAT, O_EXCL: never through a link, never into a file that is already there); true when all 1098 note of it is written and synced 1099 fn pput n ulong to bool 1100 if gt get gpl 0 16383 1101 ret false 1102 end 1103 fd = int fopen go 0 n 193 420 1104 if lt fd 0 1105 ret false 1106 end 1107 m = get gpl 0 1108 o = ulong 0 1109 good = true 1110 while lt o m bound 16384 1111 k = write fd gp o sub m o 1112 if le k 0 1113 good = false 1114 o = m 1115 else 1116 o = add o ulong k 1117 end 1118 end 1119 if ne fsync fd 0 1120 good = false 1121 end 1122 r = fclose fd 1123 ret good 1124 end 1125 note peq: the first 16 bytes of two stat records (st_dev, st_ino) are equal 1126 fn peq a view uchar 128 b view uchar 128 to bool 1127 for i ulong 16 1128 if ne get a i get b i 1129 ret false 1130 end 1131 end 1132 ret true 1133 end 1134 note pid: s holds the lstat of a name; 1 when it is the entry or the file argument idx names (by 1135 note lstat or by stat), 0 when not, 3 when argument idx cannot be read or does not exist 1136 fn pid s view uchar 128 idx ulong to int 1137 m = argread idx ga 0 4095 1138 if lt m 1 1139 ret 3 1140 end 1141 t = new uchar 128 0 1142 if lt stat ga 0 ulong m t 0 128 0 1143 ret 3 1144 end 1145 if peq s t 1146 ret 1 1147 end 1148 if lt lstat ga 0 ulong m t 0 128 0 1149 ret 3 1150 end 1151 if peq s t 1152 ret 1 1153 end 1154 ret 0 1155 end 1156 note pguard: 0 when the name go[0, n) does not exist or is neither the ledger (argument 2) nor, in 1157 note feed, the fed file (argument 3); otherwise the exit code. Renaming the page over the ledger, or 1158 note removing a stale .tmp that is the ledger, would destroy it. 1159 fn pguard n ulong to int 1160 s = new uchar 128 0 1161 if lt lstat go 0 n s 0 128 0 1162 ret 0 1163 end 1164 r = pid s 2 1165 if ne r 0 1166 ret r 1167 end 1168 if eq argcnt 7 1169 ret pid s 3 1170 end 1171 ret 0 1172 end 1173 note pprep: argument idx (1..1,000 bytes) into go as .tmp, its length in gs 7, and both names 1174 note through pguard. Runs before anything is written. 0, or the exit code. 1175 fn pprep idx ulong to int 1176 n = argread idx ga 0 4095 1177 if or lt n 1 gt n 1000 1178 ret 1 1179 end 1180 l = ulong n 1181 for i ulong l bound 1000 1182 put go i get ga i 1183 end 1184 for j ulong 4 1185 put go add l j get ttmp j 1186 end 1187 put gs 7 l 1188 r = pguard l 1189 if ne r 0 1190 ret r 1191 end 1192 ret pguard add l 4 1193 end 1194 note pfile: the page built by pbuild into the name pprep prepared, written as .tmp and 1195 note renamed. A stale .tmp is removed first (the name only, never what it points to); on any 1196 note failure the .tmp is removed again. w holds the two names as the list renames takes: count 2, 1197 note then each name as a 2-byte length and its bytes. 0, or the exit code. 1198 fn pfile to int 1199 l = get gs 7 1200 t = add l 4 1201 u = unlink go 0 t 1202 if not pput t 1203 u = unlink go 0 t 1204 ret 3 1205 end 1206 w = new uchar 2100 0 1207 put w 0 2 1208 put w 1 0 1209 put w 2 uchar rem t 256 1210 put w 3 uchar div t 256 1211 for a ulong t bound 1004 1212 put w add 4 a get go a 1213 end 1214 b = add 4 t 1215 put w b uchar rem l 256 1216 put w add b 1 uchar div l 256 1217 for c ulong l bound 1000 1218 put w add add b 2 c get go c 1219 end 1220 if ne renames w 0 add add b 2 l 0 0 1221 u = unlink go 0 t 1222 ret 3 1223 end 1224 ret 0 1225 end 1226 fn cpage to int 1227 fd = ropen 0 1228 if lt fd 0 1229 ret 3 1230 end 1231 note the same exclusive lock add and feed take: one page writer per ledger, and never mid-append 1232 if ne flock fd 2 0 1233 ret 3 1234 end 1235 p = pprep 3 1236 if ne p 0 1237 ret p 1238 end 1239 good = vfy fd 1240 r = fclose fd 1241 if not good 1242 ret 3 1243 end 1244 if ne get gs 1 0 1245 ret say 1246 end 1247 pbuild 1248 w = pfile 1249 if ne w 0 1250 ret w 1251 end 1252 ret say 1253 end 1254 fn main to int 1255 if lt argcnt 3 1256 ret 1 1257 end 1258 if argis tadd 1259 if eq argcnt 6 1260 ret cadd 1261 end 1262 ret 1 1263 end 1264 if argis tver 1265 if eq argcnt 3 1266 ret cverify 1267 end 1268 ret 1 1269 end 1270 if argis tchk 1271 if eq argcnt 4 1272 ret ccheck 1273 end 1274 ret 1 1275 end 1276 if argis tfeed 1277 if or eq argcnt 6 eq argcnt 7 1278 ret cfeed 1279 end 1280 ret 1 1281 end 1282 if argis tpage 1283 if eq argcnt 4 1284 ret cpage 1285 end 1286 ret 1 1287 end 1288 ret 1 1289 end