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 / src / turing / tc_pstream.om

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