interplane.Get Omega
Read in your language

interplane-forge

INTERPLANE Forge

Brought by drakestapleton

branch main Branches Tags History

interplane-forge / chain / chain.om

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