interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / bench / inference_cost / handover.om

986 lines · 18343 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note bench/inference_cost/handover.om -- the handover rule: does a candidate model take over a job
3note (language, arm) from the incumbent? Decided from scoreboard run records only, in integers,
4note with no person involved.
5note
6note Usage: handover LANG ARM PERMFILE INCUMBENT_DIR CANDIDATE_DIR...
7note LANG om|c|py|rs, ARM plain|feedback. PERMFILE lists task ids that must never fail again
8note (whitespace separated; it may be empty). Each *_DIR is a driver.sh --run-model directory
9note holding run.txt and attempts.txt (wire version 2).
10note Output: line 1 is HANDOVER YES, HANDOVER NO or HANDOVER REFUSED; then one line per rule with
11note the counts that decided it; then "sha256 <hex> <path>" for every input file (PERMFILE, then
12note run.txt and attempts.txt of each run in argument order) so the decision can be rechecked.
13note Exit: 0 YES, 1 NO, 2 REFUSED, 3 usage.
14note
15note Rules (README section "Handover rule"):
16note 1 comparable: every run's task_set_sha256 and omc_sha256 equal the incumbent's, every run has
17note the job's group, and every candidate names one reported model and the same model_requested
18note and models_reported as the first candidate. Otherwise REFUSED.
19note 2 evidence: at least 3 candidate runs, no two in one directory (device and inode) or with one
20note started time. Otherwise REFUSED.
21note 3 every candidate run verifies at least as many tasks of the job as the incumbent run.
22note 4 every candidate run verifies every permanent case in the job.
23note 5 complete: a run with a NOT_RUN or interrupted line or a NOT_RUN file, without a finished
24note line, with rows.txt left over (an interrupted driver), without run.txt or attempts.txt, or
25note whose attempts.txt the scorer refuses (truncated or malformed) makes the answer REFUSED.
26note REFUSED wins over NO; NO when rule 3 or 4 fails; YES otherwise.
27note
28note Build (tests/handover/run.sh): { printf 'om2\n'; cat om/tls/sha2.om;
29note sed '/^fn main to int$/,$d' bench/inference_cost/scorer.om; cat bench/inference_cost/handover.om; } | omc
30note The attempt wire is read by scorer.om's own functions and tables (read_all, read_task, lang_idx,
31note arm_idx, parse_row, record_task, require_eof), unchanged. Only scorer.om's fn main is left out,
32note because a unit has one main, and its version-2 header loop is repeated in hv_wire. scorer.om
33note stops the process (fail, exit 2) on a malformed wire, so every wire is first read in a forked
34note child and the parent reads it again only after the child exited 0.
35note Files are read twice (decision, then digest); they must not change while the program runs.
36
37note two run.txt images: base 0 and base 983040 (the scorer's input byte limit)
38global hvrun uchar 1966080
39global hvrlen ulong 2
40global hvperm uchar 983040
41global hvpermn ulong 1
42note one argument (Linux PATH_MAX 4096) plus the longest file suffix
43global hvpath uchar 4112
44global hvpathn ulong 1
45global hvst uchar 128
46global hvwst uchar 4
47global hvvo ulong 1
48global hvvl ulong 1
49global hvjob ulong 2
50global hvid ulong 2
51global hvc ulong 32
52
53const HV_NOTRUN ulong 0
54const HV_INTR ulong 1
55const HV_MISS ulong 2
56const HV_WIRE ulong 3
57const HV_TSMIS ulong 4
58const HV_OMCMIS ulong 5
59const HV_MODMIS ulong 6
60const HV_FMISS ulong 7
61const HV_JOBABS ulong 8
62const HV_DUPDIR ulong 9
63const HV_DUPSTART ulong 10
64const HV_INCV ulong 11
65const HV_ATLEAST ulong 12
66const HV_PERMOK ulong 13
67const HV_CMIN ulong 14
68const HV_PCASES ulong 15
69const HV_CPARSED ulong 16
70
71text hv_usage "usage: handover LANG ARM PERMFILE INCUMBENT_DIR CANDIDATE_DIR...\n"
72text hv_frun "/run.txt"
73text hv_fatt "/attempts.txt"
74text hv_frows "/rows.txt"
75text hv_fnr "/NOT_RUN"
76text hv_tnr "NOT_RUN"
77text hv_tint "interrupted"
78text hv_kfin "finished"
79text hv_kts "task_set_sha256"
80text hv_komc "omc_sha256"
81text hv_kmreq "model_requested"
82text hv_kmrep "models_reported"
83text hv_kstart "started"
84
85fn hv_inc i ulong to void
86put hvc i add get hvc i 1
87end
88
89fn hv_die to void
90w = write 2 hv_usage 0 len hv_usage
91exit 3
92end
93
94note hv_arg: argument a into hvpath[0..n), trailing slashes dropped; usage when empty or too long
95fn hv_arg a ulong to ulong
96k = argread a hvpath 0 4096
97if le k 0
98hv_die
99end
100n = ulong k
101while gt n 1 bound 4096
102if ne get hvpath sub n 1 47
103break
104end
105n = sub n 1
106end
107put hvpathn 0 n
108ret n
109end
110
111fn hv_add t view uchar * to ulong
112n = get hvpathn 0
113for i ulong len t
114put hvpath add n i get t i
115end
116m = add n len t
117put hvpathn 0 m
118ret m
119end
120
121fn hv_exists to bool
122s = stat hvpath 0 get hvpathn 0 hvst 0 128
123ret ge s 0
124end
125
126fn hv_argeq n ulong t view uchar * to bool
127if ne n len t
128ret false
129end
130for i ulong n
131if ne get hvpath i get t i
132ret false
133end
134end
135ret true
136end
137
138fn hv_langarg n ulong to ulong
139if hv_argeq n l_om
140ret 0
141end
142if hv_argeq n l_c
143ret 1
144end
145if hv_argeq n l_py
146ret 2
147end
148if hv_argeq n l_rs
149ret 3
150end
151hv_die
152ret 0
153end
154
155fn hv_armarg n ulong to ulong
156if hv_argeq n a_plain
157ret 0
158end
159if hv_argeq n a_feed
160ret 1
161end
162hv_die
163ret 0
164end
165
166note hv_load: the file at hvpath into hvrun at base; its length, -1 cannot open or read, -2 too large
167fn hv_load base ulong to long
168fd = int fopen hvpath 0 get hvpathn 0 0 0
169if lt fd 0
170ret -1
171end
172p = ulong 0
173bad = false
174note every pass adds at least one byte or leaves the loop
175while lt p 983040 bound 999999
176k = read fd hvrun add base p sub 983040 p
177if lt k 0
178bad = true
179end
180if le k 0
181break
182end
183p = add p ulong k
184end
185c = fclose fd
186if bad
187ret -1
188end
189if eq p 983040
190ret -2
191end
192put hvrlen div base 983040 p
193ret long p
194end
195
196fn hv_loadrun r ulong base ulong to long
197n = hv_arg add 4 r
198m = hv_add hv_frun
199ret hv_load base
200end
201
202fn hv_nextline s ulong lim ulong to ulong
203p = s
204while lt p lim bound 999999
205if eq get hvrun p 10
206ret add p 1
207end
208p = add p 1
209end
210ret lim
211end
212
213fn hv_startswith s ulong lim ulong t view uchar * to bool
214if gt add s len t lim
215ret false
216end
217for i ulong len t
218if ne get hvrun add s i get t i
219ret false
220end
221end
222ret true
223end
224
225note hv_keyat: the line at s starts with key and a space
226fn hv_keyat s ulong lim ulong key view uchar * to bool
227e = add s len key
228if ge e lim
229ret false
230end
231if ne get hvrun e 32
232ret false
233end
234ret hv_startswith s lim key
235end
236
237note hv_value: the value after the key's space, to the end of the line, trailing blanks dropped
238fn hv_value v ulong lim ulong to void
239s = v
240while lt s lim bound 999999
241if ne get hvrun s 32
242break
243end
244s = add s 1
245end
246e = s
247while lt e lim bound 999999
248if eq get hvrun e 10
249break
250end
251e = add e 1
252end
253while gt e s bound 999999
254c = get hvrun sub e 1
255if and ne c 32 and ne c 9 ne c 13
256break
257end
258e = sub e 1
259end
260put hvvo 0 s
261put hvvl 0 sub e s
262end
263
264note hv_find: key appears on exactly one line of the run image at base; its value in hvvo, hvvl
265fn hv_find base ulong key view uchar * to bool
266lim = add base get hvrlen div base 983040
267s = base
268hits = ulong 0
269while lt s lim bound 999999
270if hv_keyat s lim key
271hits = add hits 1
272hv_value add s len key lim
273end
274s = hv_nextline s lim
275end
276ret eq hits 1
277end
278
279note hv_scan: bit 0 a line starts NOT_RUN, bit 1 a line starts interrupted (image at base 0)
280fn hv_scan to ulong
281lim = get hvrlen 0
282f = ulong 0
283s = ulong 0
284while lt s lim bound 999999
285if hv_startswith s lim hv_tnr
286f = or f 1
287end
288if hv_startswith s lim hv_tint
289f = or f 2
290end
291s = hv_nextline s lim
292end
293ret f
294end
295
296fn hv_eqv ao ulong al ulong bo ulong bl ulong to bool
297if ne al bl
298ret false
299end
300for i ulong al
301if ne get hvrun add ao i get hvrun add bo i
302ret false
303end
304end
305ret true
306end
307
308note hv_same_key: key once in both run images, nonempty and equal; a difference counts in c
309fn hv_same_key key view uchar * c ulong to void
310if not (hv_find 0 key)
311hv_inc HV_FMISS
312ret
313end
314ao = get hvvo 0
315al = get hvvl 0
316if not (hv_find 983040 key)
317hv_inc HV_FMISS
318ret
319end
320if or eq al 0 not (hv_eqv ao al get hvvo 0 get hvvl 0)
321hv_inc c
322end
323end
324
325note ---- the attempt wire, read by scorer.om's functions (version 2 only: the job needs lang, arm)
326fn hv_wire to int
327n = read_all
328if eq n 0
329ret 1
330end
331put inlen 0 n
332put pos 0 0
333skip_token
334v = read_uint
335if ne v 2
336ret 3
337end
338skip_token
339att_cnt = read_uint
340for i ulong att_cnt
341read_task
342lg = lang_idx
343am = arm_idx
344parse_row mul add mul lg 2 am 12
345end
346require_eof
347ret 0
348end
349
350fn hv_stdin to bool
351fd = int fopen hvpath 0 get hvpathn 0 0 0
352if lt fd 0
353ret false
354end
355if eq fd 0
356ret true
357end
358r = dupfd fd 0 0
359c = fclose fd
360ret eq r 0
361end
362
363fn hv_child to void
364hv_reset
365if not (hv_stdin)
366exit 5
367end
368rc = hv_wire
369exit rc
370end
371
372note hv_wirechk: the wire at hvpath read in a child; its exit status (0 accepted, 5 cannot open)
373fn hv_wirechk to int
374pid = fork
375if lt pid 0
376ret 7
377end
378if eq pid 0
379hv_child
380exit 9
381end
382r = waitpid int pid hvwst 0 4 0
383if ne r pid
384ret 8
385end
386s0 = ulong get hvwst 0
387if ne and s0 127 0
388ret 9
389end
390ret int get hvwst 1
391end
392
393fn hv_reset to void
394for i ulong 96
395put stats i 0
396end
397for i ulong 65536
398put taskbeg i 0
399put tasklen i 0
400put taskgrp i 0
401put taskatt i 0
402put taskver i 0
403end
404end
405
406fn hv_grp to ulong
407ret mul add mul get hvjob 0 2 get hvjob 1 12
408end
409
410fn hv_same h ulong s ulong n ulong to bool
411st = get taskbeg h
412for i ulong n
413if ne get inbuf add st i get hvperm add s i
414ret false
415end
416end
417ret true
418end
419
420note hv_pver: the permanent case hvperm[s .. s+n) is verified in group b of the parsed wire
421fn hv_pver b ulong s ulong n ulong to bool
422for h ulong 65536
423if and eq get tasklen h n eq get taskgrp h b
424if hv_same h s n
425ret eq get taskver h 1
426end
427end
428end
429ret false
430end
431
432fn hv_ptokend s ulong lim ulong to ulong
433e = s
434while lt e lim bound 999999
435if is_space (get hvperm e)
436ret e
437end
438e = add e 1
439end
440ret lim
441end
442
443note hv_perm: b < 65536 checks every permanent case in group b; b = 65536 only counts them
444fn hv_perm b ulong to ulong
445lim = get hvpermn 0
446s = ulong 0
447bad = ulong 0
448while lt s lim bound 999999
449if is_space (get hvperm s)
450s = add s 1
451else
452e = hv_ptokend s lim
453if eq b 65536
454bad = add bad 1
455else
456if not (hv_pver b s (sub e s))
457bad = add bad 1
458end
459end
460s = e
461end
462end
463ret bad
464end
465
466fn hv_loadperm to void
467fd = int fopen hvpath 0 get hvpathn 0 0 0
468if lt fd 0
469hv_die
470end
471p = ulong 0
472note every pass adds at least one byte or leaves the loop
473while lt p 983040 bound 999999
474k = read fd hvperm p sub 983040 p
475if lt k 0
476hv_die
477end
478if eq k 0
479break
480end
481p = add p ulong k
482end
483c = fclose fd
484if eq p 983040
485hv_die
486end
487put hvpermn 0 p
488put hvc HV_PCASES hv_perm 65536
489end
490
491fn hv_args to ulong
492c = ulong argcnt
493if lt c 6
494hv_die
495end
496n1 = hv_arg 1
497put hvjob 0 hv_langarg n1
498n2 = hv_arg 2
499put hvjob 1 hv_armarg n2
500n3 = hv_arg 3
501hv_loadperm
502for i ulong sub c 4
503n4 = hv_arg add i 4
504end
505ret sub c 5
506end
507
508note ---- rule 3 and 4 counts of one parsed run (r 0 is the incumbent)
509fn hv_atleast cv ulong to bool
510iv = get hvc HV_INCV
511note rule 3: at least the incumbent's verified count
512if ge cv iv
513ret true
514end
515ret false
516end
517
518fn hv_tally r ulong to void
519b = hv_grp
520if eq get stats add b 9 0
521hv_inc HV_JOBABS
522ret
523end
524v = get stats add b 2
525if eq r 0
526put hvc HV_INCV v
527ret
528end
529hv_inc HV_CPARSED
530if hv_atleast v
531hv_inc HV_ATLEAST
532end
533if or (eq get hvc HV_CPARSED 1) (lt v get hvc HV_CMIN)
534put hvc HV_CMIN v
535end
536if eq (hv_perm b) 0
537hv_inc HV_PERMOK
538end
539end
540
541fn hv_parse r ulong to void
542hv_reset
543if not (hv_stdin)
544hv_inc HV_WIRE
545ret
546end
547rc = hv_wire
548if ne rc 0
549hv_inc HV_WIRE
550ret
551end
552hv_tally r
553end
554
555note ---- rule 5 for run r, then its wire and counts
556fn hv_run r ulong to void
557n = hv_loadrun r 0
558if lt n 0
559hv_inc HV_MISS
560ret
561end
562f = hv_scan
563fin = hv_find 0 hv_kfin
564n1 = hv_arg add 4 r
565m1 = hv_add hv_frows
566rows = hv_exists
567n2 = hv_arg add 4 r
568m2 = hv_add hv_fnr
569nrf = hv_exists
570if or (ne (and f 1) 0) nrf
571hv_inc HV_NOTRUN
572end
573if or (or (ne (and f 2) 0) (not fin)) rows
574hv_inc HV_INTR
575end
576n3 = hv_arg add 4 r
577m3 = hv_add hv_fatt
578st = hv_wirechk
579if eq st 5
580hv_inc HV_MISS
581ret
582end
583if ne st 0
584hv_inc HV_WIRE
585ret
586end
587hv_parse r
588end
589
590note ---- rule 1
591fn hv_cmp1 r ulong to void
592n = hv_loadrun r 983040
593if lt n 0
594hv_inc HV_FMISS
595ret
596end
597hv_same_key hv_kts HV_TSMIS
598hv_same_key hv_komc HV_OMCMIS
599end
600
601fn hv_onetok to bool
602o = get hvvo 0
603for i ulong get hvvl 0
604if eq get hvrun add o i 32
605ret false
606end
607end
608ret true
609end
610
611fn hv_cmpm r ulong to void
612n = hv_loadrun r 983040
613if lt n 0
614hv_inc HV_FMISS
615ret
616end
617hv_same_key hv_kmreq HV_MODMIS
618hv_same_key hv_kmrep HV_MODMIS
619if hv_find 983040 hv_kmrep
620if not (hv_onetok)
621hv_inc HV_MODMIS
622end
623end
624end
625
626fn hv_rule1 nc ulong to void
627n0 = hv_loadrun 0 0
628if lt n0 0
629hv_inc HV_FMISS
630else
631for r ulong nc
632hv_cmp1 add r 1
633end
634end
635n1 = hv_loadrun 1 0
636if lt n1 0
637hv_inc HV_FMISS
638ret
639end
640for i ulong nc
641hv_cmpm add i 1
642end
643end
644
645fn hv_r1ok to bool
646a = and eq get hvc HV_TSMIS 0 eq get hvc HV_OMCMIS 0
647b = and eq get hvc HV_MODMIS 0 eq get hvc HV_FMISS 0
648ret and and a b eq get hvc HV_JOBABS 0
649end
650
651note ---- rule 2
652fn hv_le64 o ulong to ulong
653v = ulong 0
654for i ulong 8
655v = or shl v 8 ulong get hvst sub add o 7 i
656end
657ret v
658end
659
660fn hv_dirid r ulong to bool
661n = hv_arg add 4 r
662s = stat hvpath 0 n hvst 0 128
663if lt s 0
664ret false
665end
666put hvid 0 hv_le64 0
667put hvid 1 hv_le64 8
668ret true
669end
670
671fn hv_samedir j ulong dev ulong ino ulong to bool
672if not (hv_dirid j)
673ret false
674end
675ret and eq get hvid 0 dev eq get hvid 1 ino
676end
677
678note hv_samestart: run j (into base 983040) has the started value of the image at base 0
679fn hv_samestart j ulong ao ulong al ulong to bool
680n = hv_loadrun j 983040
681if lt n 0
682ret false
683end
684if not (hv_find 983040 hv_kstart)
685ret true
686end
687ret hv_eqv ao al get hvvo 0 get hvvl 0
688end
689
690note hv_dupof: candidate r shares its directory or its started time with an earlier candidate
691fn hv_dupof r ulong to void
692n = hv_loadrun r 0
693if lt n 0
694ret
695end
696ds = false
697if not (hv_find 0 hv_kstart)
698ds = true
699end
700ao = get hvvo 0
701al = get hvvl 0
702if eq al 0
703ds = true
704end
705ok = hv_dirid r
706dev = get hvid 0
707ino = get hvid 1
708dd = false
709for j ulong sub r 1
710if and ok (hv_samedir add j 1 dev ino)
711dd = true
712end
713if hv_samestart add j 1 ao al
714ds = true
715end
716end
717if dd
718hv_inc HV_DUPDIR
719end
720if ds
721hv_inc HV_DUPSTART
722end
723end
724
725fn hv_rule2 nc ulong to void
726for i ulong nc
727hv_dupof add i 1
728end
729end
730
731fn hv_r5ok to bool
732a = and eq get hvc HV_NOTRUN 0 eq get hvc HV_INTR 0
733b = and eq get hvc HV_MISS 0 eq get hvc HV_WIRE 0
734ret and a b
735end
736
737note ---- output
738fn hv_num v ulong to void
739p = ulong 32
740x = v
741for i ulong 20
742p = sub p 1
743put obuf p uchar add rem x 10 48
744x = div x 10
745if eq x 0
746break
747end
748end
749w = write 1 obuf p sub 32 p
750end
751
752fn hv_pair t view uchar * v ulong to void
753wtext t
754hv_num v
755end
756
757text hv_tpass "PASS"
758text hv_tfailw "FAIL"
759text hv_tnot "NOT_EVALUATED"
760fn hv_status ok bool to void
761if ok
762wtext hv_tpass
763else
764wtext hv_tfailw
765end
766end
767
768text hv_tr1 "rule1 comparable "
769text hv_tr1a " runs "
770text hv_tr1b " task_set_mismatch "
771text hv_tr1c " omc_mismatch "
772text hv_tr1d " model_mismatch "
773text hv_tr1e " fields_missing "
774text hv_tr1f " job_absent "
775fn hv_line1 nc ulong ok bool to void
776wtext hv_tr1
777hv_status ok
778hv_pair hv_tr1a add nc 1
779hv_pair hv_tr1b get hvc HV_TSMIS
780hv_pair hv_tr1c get hvc HV_OMCMIS
781hv_pair hv_tr1d get hvc HV_MODMIS
782hv_pair hv_tr1e get hvc HV_FMISS
783hv_pair hv_tr1f get hvc HV_JOBABS
784wtext t_nl
785end
786
787text hv_tr2 "rule2 evidence "
788text hv_tr2a " candidate_runs "
789text hv_tr2b " distinct_dirs "
790text hv_tr2c " distinct_starts "
791text hv_tr2d " need 3\n"
792fn hv_line2 nc ulong ok bool to void
793wtext hv_tr2
794hv_status ok
795hv_pair hv_tr2a nc
796hv_pair hv_tr2b sub nc get hvc HV_DUPDIR
797hv_pair hv_tr2c sub nc get hvc HV_DUPSTART
798wtext hv_tr2d
799end
800
801fn hv_label to void
802l = get hvjob 0
803if eq l 0
804wtext l_om
805end
806if eq l 1
807wtext l_c
808end
809if eq l 2
810wtext l_py
811end
812if eq l 3
813wtext l_rs
814end
815wtext t_sp
816if eq get hvjob 1 0
817wtext a_plain
818else
819wtext a_feed
820end
821end
822
823text hv_tr3 "rule3 verified "
824text hv_tr3a " job "
825text hv_tr3b " incumbent "
826text hv_tr3c " candidate_min "
827text hv_tr3d " at_least "
828text hv_tr3e " of "
829fn hv_line3 nc ulong ok bool refused bool to void
830wtext hv_tr3
831if refused
832wtext hv_tnot
833else
834hv_status ok
835end
836wtext hv_tr3a
837hv_label
838hv_pair hv_tr3b get hvc HV_INCV
839hv_pair hv_tr3c get hvc HV_CMIN
840hv_pair hv_tr3d get hvc HV_ATLEAST
841hv_pair hv_tr3e nc
842wtext t_nl
843end
844
845text hv_tr4 "rule4 permanent "
846text hv_tr4a " cases "
847text hv_tr4b " all_verified_in "
848fn hv_line4 nc ulong ok bool refused bool to void
849wtext hv_tr4
850if refused
851wtext hv_tnot
852else
853hv_status ok
854end
855hv_pair hv_tr4a get hvc HV_PCASES
856hv_pair hv_tr4b get hvc HV_PERMOK
857hv_pair hv_tr3e nc
858wtext t_nl
859end
860
861text hv_tr5 "rule5 complete "
862text hv_tr5a " not_run "
863text hv_tr5b " interrupted "
864text hv_tr5c " missing_files "
865text hv_tr5d " wire_refused "
866fn hv_line5 nc ulong ok bool to void
867wtext hv_tr5
868hv_status ok
869hv_pair hv_tr1a add nc 1
870hv_pair hv_tr5a get hvc HV_NOTRUN
871hv_pair hv_tr5b get hvc HV_INTR
872hv_pair hv_tr5c get hvc HV_MISS
873hv_pair hv_tr5d get hvc HV_WIRE
874wtext t_nl
875end
876
877text hv_hexd "0123456789abcdef"
878text hv_tsha "sha256 "
879text hv_tmiss "MISSING "
880text hv_tunr "UNREADABLE "
881fn hv_pathln to void
882w = write 1 hvpath 0 get hvpathn 0
883wtext t_nl
884end
885
886fn hv_hexout dig view uchar 32 to void
887hx = new uchar 65 32
888for i ulong 32
889d = get dig i
890put hx mul i 2 get hv_hexd ulong shr d 4
891put hx add mul i 2 1 get hv_hexd ulong and d 15
892end
893w = write 1 hx 0 65
894end
895
896note hv_sha: "sha256 <hex> <path>" for the file at hvpath
897fn hv_sha to void
898ctx = new ulong 40 0
899buf = new uchar 65536 0
900dig = new uchar 32 0
901wtext hv_tsha
902fd = int fopen hvpath 0 get hvpathn 0 0 0
903if lt fd 0
904wtext hv_tmiss
905hv_pathln
906ret
907end
908sha256_init ctx
909more = true
910bad = false
911note every pass reads at least one byte or leaves the loop (files below 999999 * 64 KiB)
912while more bound 999999
913k = read fd buf 0 65536
914if gt k 0
915sha256_update ctx part buf 0 ulong k
916else
917more = false
918if lt k 0
919bad = true
920end
921end
922end
923c = fclose fd
924if bad
925wtext hv_tunr
926else
927sha256_final ctx dig
928hv_hexout dig
929end
930hv_pathln
931end
932
933fn hv_digests nc ulong to void
934n = hv_arg 3
935hv_sha
936for r ulong add nc 1
937a1 = hv_arg add 4 r
938b1 = hv_add hv_frun
939hv_sha
940a2 = hv_arg add 4 r
941b2 = hv_add hv_fatt
942hv_sha
943end
944end
945
946text hv_tyes "HANDOVER YES\n"
947text hv_tno "HANDOVER NO\n"
948text hv_tref "HANDOVER REFUSED\n"
949fn hv_report nc ulong to int
950r5 = hv_r5ok
951r1 = hv_r1ok
952r2n = ge nc 3
953r2 = and r2n and eq get hvc HV_DUPDIR 0 eq get hvc HV_DUPSTART 0
954refused = not (and r1 and r2 r5)
955r3ok = eq get hvc HV_ATLEAST nc
956r4ok = eq get hvc HV_PERMOK nc
957rc = int 0
958if refused
959wtext hv_tref
960rc = 2
961else
962if and r3ok r4ok
963wtext hv_tyes
964else
965wtext hv_tno
966rc = 1
967end
968end
969hv_line1 nc r1
970hv_line2 nc r2
971hv_line3 nc r3ok refused
972hv_line4 nc r4ok refused
973hv_line5 nc r5
974hv_digests nc
975ret rc
976end
977
978fn main to int
979nc = hv_args
980for r ulong add nc 1
981hv_run r
982end
983hv_rule1 nc
984hv_rule2 nc
985ret hv_report nc
986end