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 / si / seed-challenge / challenge.om

1171 lines · 20051 bytes Raw Blame History
1om2
2note si/seed-challenge/challenge.om: Pure-Omega seed-ladder pin table and challenge log verifier.
3note Faithful OM-2 conversion of omega-seed-challenge (check-result.sh + seed0->hex1->asm2->om4->omc ladder).
4note Implements FIPS 180-4 SHA-256, strict 40-hex commit pin and 64-hex digest validation,
5note 5-rung chain completeness verification, challenge log auditing (including stage1..stage3 omc fixpoint),
6note and fail-closed refusal diagnostics.
7note Usage:
8note challenge <expected.txt> <challenge.log> (verify two files)
9note challenge < combined.txt (stdin with '---' line separating expected table and log)
10note Exit codes: 0 = RESULT ACCEPTED, 1 = RESULT REFUSED, 2 = usage / I/O / malformed expected table.
11
12global expbuf uchar 16384
13global logbuf uchar 32768
14global pinbuf uchar 64
15global omcsha uchar 64
16global patbuf uchar 512
17global decbuf uchar 32
18global argbuf uchar 512
19global rdbuf uchar 4096
20global rungseen ulong 8
21global gmeta ulong 12
22global shastate uint 8
23global shak uint 64
24global shaw uint 64
25global shablk uchar 64
26global shadig uchar 32
27global shahex uchar 64
28
29note gmeta indices
30const m_elen ulong 0
31const m_llen ulong 1
32const m_plen ulong 2
33const m_olen ulong 3
34const m_bad ulong 4
35const m_patlen ulong 5
36const m_expe ulong 6
37const m_lastch ulong 7
38
39note rungseen indices: 0..4 are seed0, hex1, asm2, om4, omc; 5=pin, 6=inputs, 7=rungs
40const r_seed0 ulong 0
41const r_hex1 ulong 1
42const r_asm2 ulong 2
43const r_om4 ulong 3
44const r_omc ulong 4
45const r_pin ulong 5
46const r_incnt ulong 6
47const r_rcnt ulong 7
48
49text khex "0123456789abcdef"
50text t_nl "\n"
51text t_sp " "
52text t_pin "pin "
53text t_input "input "
54text t_rung "rung "
55text t_seed0 "seed0"
56text t_hex1 "hex1"
57text t_asm2 "asm2"
58text t_om4 "om4"
59text t_omc "omc"
60text t_host "host "
61text t_a64 "aarch64"
62text t_pass_in "PASS input "
63text t_pass_rg "PASS rung "
64text t_st1 "stage1 "
65text t_st2 "stage2 "
66text t_st3 "stage3 "
67text t_fail "FAIL "
68text t_notrun "NOT_RUN "
69text t_ch_pfx "CHALLENGE "
70text t_ch_pass "CHALLENGE PASS pin="
71text t_ch_omc " omc="
72text t_sep "---"
73text t_ref_pfx "REFUSED "
74text t_ref_pin "log does not name expected pin\n"
75text t_ref_host "host line does not say aarch64\n"
76text t_ref_in "input line missing or mismatched in log\n"
77text t_ref_rg "rung line missing or mismatched in log\n"
78text t_ref_st "omc stage1..3 fixpoint line missing or mismatched in log\n"
79text t_ref_fn "log contains FAIL or NOT_RUN lines\n"
80text t_ref_last "final CHALLENGE line does not match CHALLENGE PASS pin=<pin> omc=<omc>\n"
81text t_ref_chain "expected table missing required seed ladder rung (seed0/hex1/asm2/om4/omc)\n"
82text t_ref_expe "expected table contains malformed pin, digest, or entry\n"
83text t_acc1 "RESULT ACCEPTED pin="
84text t_acc2 " omc="
85text t_rej1 "RESULT REFUSED reasons="
86text t_usage "usage: challenge <expected.txt> <challenge.log> (or stdin with --- separator)\n"
87text t_ioerr "challenge: failed to read input file\n"
88
89fn write_all fd int src view uchar * off ulong n ulong to void
90p = ulong 0
91while lt p n bound 65536
92k = write fd src add off p sub n p
93if le k 0
94exit 2
95end
96p = add p ulong k
97end
98end
99
100fn wstr fd int s view uchar * to void
101write_all fd s 0 len s
102end
103
104fn wdec fd int v ulong to void
105i = ulong 32
106n = v
107while true bound 24
108i = sub i 1
109put decbuf i uchar add rem n 10 48
110n = div n 10
111if eq n 0
112break
113end
114end
115p = i
116while lt p 32 bound 32
117k = write fd decbuf p sub 32 p
118if le k 0
119exit 2
120end
121p = add p ulong k
122end
123end
124
125fn wpin fd int to void
126n = get gmeta m_plen
127p = ulong 0
128while lt p n bound 64
129k = write fd pinbuf p sub n p
130if le k 0
131exit 2
132end
133p = add p ulong k
134end
135end
136
137fn womc fd int to void
138n = get gmeta m_olen
139p = ulong 0
140while lt p n bound 64
141k = write fd omcsha p sub n p
142if le k 0
143exit 2
144end
145p = add p ulong k
146end
147end
148
149fn is_hex_ch c uchar to bool
150if and ge c 48 le c 57
151ret true
152end
153if and ge c 97 le c 102
154ret true
155end
156ret false
157end
158
159fn is_dig_ch c uchar to bool
160ret and ge c 48 le c 57
161end
162
163fn is_hex_exp off ulong n ulong to bool
164if eq n 0
165ret false
166end
167if gt add off n 16384
168ret false
169end
170i = ulong 0
171while lt i n bound 128
172if not is_hex_ch get expbuf add off i
173ret false
174end
175i = add i 1
176end
177ret true
178end
179
180fn is_dec_exp off ulong n ulong to bool
181if eq n 0
182ret false
183end
184if gt n 20
185ret false
186end
187if gt add off n 16384
188ret false
189end
190nz = false
191i = ulong 0
192while lt i n bound 32
193c = get expbuf add off i
194if not is_dig_ch c
195ret false
196end
197if ne c 48
198nz = true
199end
200i = add i 1
201end
202ret nz
203end
204
205fn is_hex_view s view uchar * n ulong to bool
206if ne len s n
207ret false
208end
209i = ulong 0
210while lt i n bound 128
211if not is_hex_ch get s i
212ret false
213end
214i = add i 1
215end
216ret true
217end
218
219note FIPS 180-4 SHA-256 constants and block compression
220fn sha_kinit to void
221put shak 0 0x428a2f98
222put shak 1 0x71374491
223put shak 2 0xb5c0fbcf
224put shak 3 0xe9b5dba5
225put shak 4 0x3956c25b
226put shak 5 0x59f111f1
227put shak 6 0x923f82a4
228put shak 7 0xab1c5ed5
229put shak 8 0xd807aa98
230put shak 9 0x12835b01
231put shak 10 0x243185be
232put shak 11 0x550c7dc3
233put shak 12 0x72be5d74
234put shak 13 0x80deb1fe
235put shak 14 0x9bdc06a7
236put shak 15 0xc19bf174
237put shak 16 0xe49b69c1
238put shak 17 0xefbe4786
239put shak 18 0x0fc19dc6
240put shak 19 0x240ca1cc
241put shak 20 0x2de92c6f
242put shak 21 0x4a7484aa
243put shak 22 0x5cb0a9dc
244put shak 23 0x76f988da
245put shak 24 0x983e5152
246put shak 25 0xa831c66d
247put shak 26 0xb00327c8
248put shak 27 0xbf597fc7
249put shak 28 0xc6e00bf3
250put shak 29 0xd5a79147
251put shak 30 0x06ca6351
252put shak 31 0x14292967
253put shak 32 0x27b70a85
254put shak 33 0x2e1b2138
255put shak 34 0x4d2c6dfc
256put shak 35 0x53380d13
257put shak 36 0x650a7354
258put shak 37 0x766a0abb
259put shak 38 0x81c2c92e
260put shak 39 0x92722c85
261put shak 40 0xa2bfe8a1
262put shak 41 0xa81a664b
263put shak 42 0xc24b8b70
264put shak 43 0xc76c51a3
265put shak 44 0xd192e819
266put shak 45 0xd6990624
267put shak 46 0xf40e3585
268put shak 47 0x106aa070
269put shak 48 0x19a4c116
270put shak 49 0x1e376c08
271put shak 50 0x2748774c
272put shak 51 0x34b0bcb5
273put shak 52 0x391c0cb3
274put shak 53 0x4ed8aa4a
275put shak 54 0x5b9cca4f
276put shak 55 0x682e6ff3
277put shak 56 0x748f82ee
278put shak 57 0x78a5636f
279put shak 58 0x84c87814
280put shak 59 0x8cc70208
281put shak 60 0x90befffa
282put shak 61 0xa4506ceb
283put shak 62 0xbef9a3f7
284put shak 63 0xc67178f2
285end
286
287fn sha_iv to void
288put shastate 0 0x6a09e667
289put shastate 1 0xbb67ae85
290put shastate 2 0x3c6ef372
291put shastate 3 0xa54ff53a
292put shastate 4 0x510e527f
293put shastate 5 0x9b05688c
294put shastate 6 0x1f83d9ab
295put shastate 7 0x5be0cd19
296end
297
298fn sha_sched to void
299for i ulong 16
300q = mul i 4
301x = uint get shablk q
302x = or shl x 8 uint get shablk add q 1
303x = or shl x 8 uint get shablk add q 2
304put shaw i or shl x 8 uint get shablk add q 3
305end
306for i ulong 48
307x = get shaw add i 1
308y = get shaw add i 14
309s0 = xor xor rotr x 7 rotr x 18 shr x 3
310s1 = xor xor rotr y 17 rotr y 19 shr y 10
311put shaw add i 16 add add get shaw i get shaw add i 9 add s0 s1
312end
313end
314
315fn sha_compress to void
316sha_sched
317a = get shastate 0
318b = get shastate 1
319c = get shastate 2
320d = get shastate 3
321e = get shastate 4
322f = get shastate 5
323g = get shastate 6
324h = get shastate 7
325t = uint 0
326for i ulong 64
327h = add h get shak i
328h = add h get shaw i
329t = xor rotr e 6 rotr e 11
330t = xor t rotr e 25
331h = add h t
332h = add h xor g and e xor f g
333d = add d h
334t = xor rotr a 2 rotr a 13
335t = xor t rotr a 22
336h = add h t
337h = add h or and a b and c or a b
338t = h
339h = g
340g = f
341f = e
342e = d
343d = c
344c = b
345b = a
346a = t
347end
348put shastate 0 add get shastate 0 a
349put shastate 1 add get shastate 1 b
350put shastate 2 add get shastate 2 c
351put shastate 3 add get shastate 3 d
352put shastate 4 add get shastate 4 e
353put shastate 5 add get shastate 5 f
354put shastate 6 add get shastate 6 g
355put shastate 7 add get shastate 7 h
356end
357
358fn sha256_bytes src view uchar * dlen ulong to void
359sha_kinit
360sha_iv
361off = ulong 0
362while ge sub dlen off 64 bound 65536
363for j ulong 64
364put shablk j get src add off j
365end
366sha_compress
367off = add off 64
368end
369remn = sub dlen off
370for j ulong 64
371if lt j remn
372put shablk j get src add off j
373else
374put shablk j 0
375end
376end
377put shablk remn 0x80
378if gt remn 55
379sha_compress
380for j ulong 64
381put shablk j 0
382end
383end
384bits = mul dlen 8
385put shablk 56 uchar and shr bits 56 255
386put shablk 57 uchar and shr bits 48 255
387put shablk 58 uchar and shr bits 40 255
388put shablk 59 uchar and shr bits 32 255
389put shablk 60 uchar and shr bits 24 255
390put shablk 61 uchar and shr bits 16 255
391put shablk 62 uchar and shr bits 8 255
392put shablk 63 uchar and bits 255
393sha_compress
394for i ulong 8
395w = get shastate i
396b0 = uchar and shr w 24 255
397b1 = uchar and shr w 16 255
398b2 = uchar and shr w 8 255
399b3 = uchar and w 255
400q = mul i 4
401put shadig q b0
402put shadig add q 1 b1
403put shadig add q 2 b2
404put shadig add q 3 b3
405end
406for i ulong 32
407bv = ulong get shadig i
408put shahex mul i 2 get khex shr bv 4
409put shahex add mul i 2 1 get khex and bv 15
410end
411end
412
413fn verify_rung_bytes src view uchar * dlen ulong exphex view uchar * to bool
414if not is_hex_view exphex 64
415ret false
416end
417sha256_bytes src dlen
418for i ulong 64
419if ne get shahex i get exphex i
420ret false
421end
422end
423ret true
424end
425
426note Pattern buffer helpers
427fn pat_clear to void
428put gmeta m_patlen 0
429end
430
431fn pat_push_ch c uchar to void
432n = get gmeta m_patlen
433if lt n 512
434put patbuf n c
435put gmeta m_patlen add n 1
436end
437end
438
439fn pat_push_lit s view uchar * to void
440sl = len s
441i = ulong 0
442while lt i sl bound 512
443pat_push_ch get s i
444i = add i 1
445end
446end
447
448fn pat_push_exp off ulong n ulong to void
449i = ulong 0
450while lt i n bound 512
451if lt add off i 16384
452pat_push_ch get expbuf add off i
453end
454i = add i 1
455end
456end
457
458fn pat_push_pin to void
459n = get gmeta m_plen
460i = ulong 0
461while lt i n bound 64
462pat_push_ch get pinbuf i
463i = add i 1
464end
465end
466
467fn pat_push_omc to void
468n = get gmeta m_olen
469i = ulong 0
470while lt i n bound 64
471pat_push_ch get omcsha i
472i = add i 1
473end
474end
475
476note Compare expbuf slice against literal
477fn exp_eq off ulong n ulong lit view uchar * to bool
478if ne n len lit
479ret false
480end
481i = ulong 0
482while lt i n bound 512
483if ne get expbuf add off i get lit i
484ret false
485end
486i = add i 1
487end
488ret true
489end
490
491fn exp_starts off ulong elen ulong pfx view uchar * to bool
492pl = len pfx
493if lt elen pl
494ret false
495end
496i = ulong 0
497while lt i pl bound 512
498if ne get expbuf add off i get pfx i
499ret false
500end
501i = add i 1
502end
503ret true
504end
505
506fn log_starts off ulong llen ulong pfx view uchar * to bool
507pl = len pfx
508if lt llen pl
509ret false
510end
511i = ulong 0
512while lt i pl bound 512
513if ne get logbuf add off i get pfx i
514ret false
515end
516i = add i 1
517end
518ret true
519end
520
521fn log_contains off ulong llen ulong needle view uchar * to bool
522nl = len needle
523if gt nl llen
524ret false
525end
526lim = add sub llen nl 1
527i = ulong 0
528while lt i lim bound 1024
529if log_starts add off i sub llen i needle
530ret true
531end
532i = add i 1
533end
534ret false
535end
536
537note Check if logbuf[off..off+llen) equals patbuf[0..patlen) exactly, or with optional " <digits>ms" suffix
538fn log_line_matches_pat off ulong llen ulong allow_ms bool to bool
539pl = get gmeta m_patlen
540if lt llen pl
541ret false
542end
543i = ulong 0
544while lt i pl bound 512
545if ne get logbuf add off i get patbuf i
546ret false
547end
548i = add i 1
549end
550if eq llen pl
551ret true
552end
553if not allow_ms
554ret false
555end
556if lt sub llen pl 4
557ret false
558end
559if ne get logbuf add off pl 32
560ret false
561end
562if ne get logbuf add off sub llen 2 109
563ret false
564end
565if ne get logbuf add off sub llen 1 115
566ret false
567end
568pos = add pl 1
569endpos = sub llen 2
570if ge pos endpos
571ret false
572end
573while lt pos endpos bound 32
574if not is_dig_ch get logbuf add off pos
575ret false
576end
577pos = add pos 1
578end
579ret true
580end
581
582fn log_has_pat allow_ms bool to bool
583tot = get gmeta m_llen
584pos = ulong 0
585while lt pos tot bound 32768
586ls = pos
587while lt pos tot bound 32768
588if eq get logbuf pos 10
589break
590end
591pos = add pos 1
592end
593ll = sub pos ls
594if log_line_matches_pat ls ll allow_ms
595ret true
596end
597if lt pos tot
598pos = add pos 1
599end
600end
601ret false
602end
603
604fn log_has_aarch64_host to bool
605tot = get gmeta m_llen
606pos = ulong 0
607while lt pos tot bound 32768
608ls = pos
609while lt pos tot bound 32768
610if eq get logbuf pos 10
611break
612end
613pos = add pos 1
614end
615ll = sub pos ls
616if log_starts ls ll t_host
617if log_contains ls ll t_a64
618ret true
619end
620end
621if lt pos tot
622pos = add pos 1
623end
624end
625ret false
626end
627
628fn log_has_fail_or_notrun to bool
629tot = get gmeta m_llen
630pos = ulong 0
631while lt pos tot bound 32768
632ls = pos
633while lt pos tot bound 32768
634if eq get logbuf pos 10
635break
636end
637pos = add pos 1
638end
639ll = sub pos ls
640off = ls
641remn = ll
642if log_starts ls ll t_st1
643off = add ls 7
644remn = sub ll 7
645else
646if log_starts ls ll t_st2
647off = add ls 7
648remn = sub ll 7
649else
650if log_starts ls ll t_st3
651off = add ls 7
652remn = sub ll 7
653end
654end
655end
656if log_starts off remn t_fail
657ret true
658end
659if log_starts off remn t_notrun
660ret true
661end
662if lt pos tot
663pos = add pos 1
664end
665end
666ret false
667end
668
669fn log_check_last_challenge to bool
670pat_clear
671pat_push_lit t_ch_pass
672pat_push_pin
673pat_push_lit t_ch_omc
674pat_push_omc
675tot = get gmeta m_llen
676pos = ulong 0
677found = false
678matched = false
679while lt pos tot bound 32768
680ls = pos
681while lt pos tot bound 32768
682if eq get logbuf pos 10
683break
684end
685pos = add pos 1
686end
687ll = sub pos ls
688if log_starts ls ll t_ch_pfx
689found = true
690matched = log_line_matches_pat ls ll false
691end
692if lt pos tot
693pos = add pos 1
694end
695end
696if not found
697ret false
698end
699ret matched
700end
701
702fn record_refusal msg view uchar * quiet bool to void
703put gmeta m_bad add get gmeta m_bad 1
704if not quiet
705wstr 1 t_ref_pfx
706wstr 1 msg
707end
708end
709
710fn find_space_exp pos ulong lim ulong to ulong
711p = pos
712while lt p lim bound 512
713if eq get expbuf p 32
714ret p
715end
716p = add p 1
717end
718ret lim
719end
720
721fn check_input_line ls ulong le_pos ulong quiet bool to void
722p1 = add ls 6
723s1 = find_space_exp p1 le_pos
724if or eq s1 p1 ge s1 le_pos
725put gmeta m_expe add get gmeta m_expe 1
726ret
727end
728p2 = add s1 1
729s2 = find_space_exp p2 le_pos
730if or ne sub s2 p2 64 ge s2 le_pos
731put gmeta m_expe add get gmeta m_expe 1
732ret
733end
734if not is_hex_exp p2 64
735put gmeta m_expe add get gmeta m_expe 1
736ret
737end
738p3 = add s2 1
739b3 = sub le_pos p3
740if not is_dec_exp p3 b3
741put gmeta m_expe add get gmeta m_expe 1
742ret
743end
744put rungseen r_incnt add get rungseen r_incnt 1
745pat_clear
746pat_push_lit t_pass_in
747pat_push_exp p1 sub le_pos p1
748if not log_has_pat false
749record_refusal t_ref_in quiet
750end
751end
752
753fn identify_rung p1 ulong nlen ulong to ulong
754if exp_eq p1 nlen t_seed0
755ret r_seed0
756end
757if exp_eq p1 nlen t_hex1
758ret r_hex1
759end
760if exp_eq p1 nlen t_asm2
761ret r_asm2
762end
763if exp_eq p1 nlen t_om4
764ret r_om4
765end
766if exp_eq p1 nlen t_omc
767ret r_omc
768end
769ret 99
770end
771
772fn check_omc_stages p1 ulong remn ulong quiet bool to void
773pat_clear
774pat_push_lit t_st1
775pat_push_lit t_pass_rg
776pat_push_exp p1 remn
777ok = log_has_pat true
778pat_clear
779pat_push_lit t_st2
780pat_push_lit t_pass_rg
781pat_push_exp p1 remn
782if not log_has_pat true
783ok = false
784end
785pat_clear
786pat_push_lit t_st3
787pat_push_lit t_pass_rg
788pat_push_exp p1 remn
789if not log_has_pat true
790ok = false
791end
792if not ok
793record_refusal t_ref_st quiet
794end
795end
796
797fn check_rung_line ls ulong le_pos ulong quiet bool to void
798p1 = add ls 5
799s1 = find_space_exp p1 le_pos
800nlen = sub s1 p1
801if or eq nlen 0 ge s1 le_pos
802put gmeta m_expe add get gmeta m_expe 1
803ret
804end
805p2 = add s1 1
806s2 = find_space_exp p2 le_pos
807if or ne sub s2 p2 64 ge s2 le_pos
808put gmeta m_expe add get gmeta m_expe 1
809ret
810end
811if not is_hex_exp p2 64
812put gmeta m_expe add get gmeta m_expe 1
813ret
814end
815p3 = add s2 1
816if not is_dec_exp p3 sub le_pos p3
817put gmeta m_expe add get gmeta m_expe 1
818ret
819end
820ridx = identify_rung p1 nlen
821if eq ridx 99
822put gmeta m_expe add get gmeta m_expe 1
823ret
824end
825put rungseen ridx 1
826put rungseen r_rcnt add get rungseen r_rcnt 1
827if eq ridx r_omc
828for i ulong 64
829put omcsha i get expbuf add p2 i
830end
831put gmeta m_olen 64
832check_omc_stages p1 sub le_pos p1 quiet
833else
834pat_clear
835pat_push_lit t_pass_rg
836pat_push_exp p1 sub le_pos p1
837if not log_has_pat true
838record_refusal t_ref_rg quiet
839end
840end
841end
842
843fn verify_loaded quiet bool to int
844for i ulong 8
845put rungseen i 0
846end
847put gmeta m_plen 0
848put gmeta m_olen 0
849put gmeta m_bad 0
850put gmeta m_expe 0
851etot = get gmeta m_elen
852pos = ulong 0
853while lt pos etot bound 16384
854ls = pos
855while lt pos etot bound 16384
856if eq get expbuf pos 10
857break
858end
859pos = add pos 1
860end
861ll = sub pos ls
862if gt ll 0
863if ne get expbuf ls 35
864if exp_starts ls ll t_pin
865plen = sub ll 4
866if and eq plen 40 is_hex_exp add ls 4 40
867for j ulong 40
868put pinbuf j get expbuf add add ls 4 j
869end
870put gmeta m_plen 40
871put rungseen r_pin 1
872else
873put gmeta m_expe add get gmeta m_expe 1
874end
875else
876if exp_starts ls ll t_input
877check_input_line ls pos quiet
878else
879if exp_starts ls ll t_rung
880check_rung_line ls pos quiet
881else
882put gmeta m_expe add get gmeta m_expe 1
883end
884end
885end
886end
887end
888if lt pos etot
889pos = add pos 1
890end
891end
892if gt get gmeta m_expe 0
893record_refusal t_ref_expe quiet
894ret 2
895end
896if eq get rungseen r_pin 0
897record_refusal t_ref_expe quiet
898ret 2
899end
900chain_ok = true
901for r ulong 5
902if eq get rungseen r 0
903chain_ok = false
904end
905end
906if not chain_ok
907record_refusal t_ref_chain quiet
908end
909pat_clear
910pat_push_lit t_pin
911pat_push_pin
912if not log_has_pat false
913record_refusal t_ref_pin quiet
914end
915if not log_has_aarch64_host
916record_refusal t_ref_host quiet
917end
918if log_has_fail_or_notrun
919record_refusal t_ref_fn quiet
920end
921if not log_check_last_challenge
922record_refusal t_ref_last quiet
923end
924if eq get gmeta m_bad 0
925ret 0
926end
927ret 1
928end
929
930fn read_file_into_exp plen ulong to bool
931fd = fopen argbuf 0 plen 0 0
932if lt fd 0
933ret false
934end
935tot = ulong 0
936while true bound 256
937k = read int fd rdbuf 0 4096
938if lt k 0
939cl = fclose int fd
940if lt cl 0
941ret false
942end
943ret false
944end
945if eq k 0
946break
947end
948kn = ulong k
949i = ulong 0
950while lt i kn bound 4096
951c = get rdbuf i
952if ne c 13
953if ge tot 16384
954cl2 = fclose int fd
955if lt cl2 0
956ret false
957end
958ret false
959end
960put expbuf tot c
961tot = add tot 1
962end
963i = add i 1
964end
965end
966cl3 = fclose int fd
967if lt cl3 0
968ret false
969end
970put gmeta m_elen tot
971ret true
972end
973
974fn read_file_into_log plen ulong to bool
975fd = fopen argbuf 0 plen 0 0
976if lt fd 0
977ret false
978end
979tot = ulong 0
980while true bound 256
981k = read int fd rdbuf 0 4096
982if lt k 0
983cl = fclose int fd
984if lt cl 0
985ret false
986end
987ret false
988end
989if eq k 0
990break
991end
992kn = ulong k
993i = ulong 0
994while lt i kn bound 4096
995c = get rdbuf i
996if ne c 13
997if ge tot 32768
998cl2 = fclose int fd
999if lt cl2 0
1000ret false
1001end
1002ret false
1003end
1004put logbuf tot c
1005tot = add tot 1
1006end
1007i = add i 1
1008end
1009end
1010cl3 = fclose int fd
1011if lt cl3 0
1012ret false
1013end
1014put gmeta m_llen tot
1015ret true
1016end
1017
1018fn is_sep_line lpos ulong to bool
1019if ne lpos 3
1020ret false
1021end
1022if ne get patbuf 0 45
1023ret false
1024end
1025if ne get patbuf 1 45
1026ret false
1027end
1028ret eq get patbuf 2 45
1029end
1030
1031fn flush_pat_line in_log bool lpos ulong add_nl bool to bool
1032j = ulong 0
1033if not in_log
1034etot = get gmeta m_elen
1035while lt j lpos bound 512
1036if ge etot 16384
1037ret false
1038end
1039put expbuf etot get patbuf j
1040etot = add etot 1
1041j = add j 1
1042end
1043if add_nl
1044if ge etot 16384
1045ret false
1046end
1047put expbuf etot 10
1048etot = add etot 1
1049end
1050put gmeta m_elen etot
1051ret true
1052end
1053ltot = get gmeta m_llen
1054while lt j lpos bound 512
1055if ge ltot 32768
1056ret false
1057end
1058put logbuf ltot get patbuf j
1059ltot = add ltot 1
1060j = add j 1
1061end
1062if add_nl
1063if ge ltot 32768
1064ret false
1065end
1066put logbuf ltot 10
1067ltot = add ltot 1
1068end
1069put gmeta m_llen ltot
1070ret true
1071end
1072
1073fn read_stdin_split to bool
1074put gmeta m_elen 0
1075put gmeta m_llen 0
1076in_log = false
1077lpos = ulong 0
1078while true bound 256
1079k = read 0 rdbuf 0 4096
1080if lt k 0
1081ret false
1082end
1083if eq k 0
1084break
1085end
1086kn = ulong k
1087i = ulong 0
1088while lt i kn bound 4096
1089c = get rdbuf i
1090if ne c 13
1091if eq c 10
1092sep = false
1093if not in_log
1094sep = is_sep_line lpos
1095end
1096if sep
1097in_log = true
1098lpos = ulong 0
1099else
1100if not flush_pat_line in_log lpos true
1101ret false
1102end
1103lpos = ulong 0
1104end
1105else
1106if lt lpos 512
1107put patbuf lpos c
1108lpos = add lpos 1
1109end
1110end
1111end
1112i = add i 1
1113end
1114end
1115if gt lpos 0
1116if not flush_pat_line in_log lpos false
1117ret false
1118end
1119end
1120ret in_log
1121end
1122
1123fn main to int
1124ac = argcnt
1125if eq ac 3
1126n1 = argread 1 argbuf 0 512
1127if le n1 0
1128wstr 2 t_usage
1129ret 2
1130end
1131if not read_file_into_exp ulong n1
1132wstr 2 t_ioerr
1133ret 2
1134end
1135n2 = argread 2 argbuf 0 512
1136if le n2 0
1137wstr 2 t_usage
1138ret 2
1139end
1140if not read_file_into_log ulong n2
1141wstr 2 t_ioerr
1142ret 2
1143end
1144else
1145if eq ac 1
1146if not read_stdin_split
1147wstr 2 t_usage
1148ret 2
1149end
1150else
1151wstr 2 t_usage
1152ret 2
1153end
1154end
1155rc = verify_loaded false
1156if eq rc 0
1157wstr 1 t_acc1
1158wpin 1
1159wstr 1 t_acc2
1160womc 1
1161wstr 1 t_nl
1162ret 0
1163end
1164if eq rc 2
1165ret 2
1166end
1167wstr 1 t_rej1
1168wdec 1 get gmeta m_bad
1169wstr 1 t_nl
1170ret 1
1171end