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 / aienux / store / store_engine.om

1575 lines · 32711 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note aienux/store/store_engine.om: Pure-Omega Store v1 mount, transaction, and disk geometry engine.
3note Faithful OM-2 library (no om2 line, no fn main) converted from upstream aienux
4note native/store/store_engine.{h,c} and native/store/store_disk.c.
5note Compose after: si/directives/om2.om aienux/store/store_v1.om aienux/store/store_engine.om ... om/tls/sha2.om
6
7note MountState
8const ST_VALID int 0
9const ST_DEGRADED_RECOVERY int 1
10
11note PeerCondition
12const ST_PEER_ZERO int 0
13const ST_PEER_VALID int 1
14const ST_PEER_MALFORMED int 2
15const ST_PEER_GRAPH_BAD_NEWER int 3
16const ST_PEER_GRAPH_BAD_OLDER int 4
17
18note MountError
19const ST_M_IO int -101
20const ST_M_UNFORMATTED int -102
21const ST_M_FOREIGN int -103
22const ST_M_UNSUPPORTED_VERSION int -104
23const ST_M_CORRUPT_RECOVERY_REQUIRED int -105
24const ST_M_CONFLICTING_ROOTS int -106
25const ST_M_INCONSISTENT_HISTORY int -107
26
27note StoreError
28const ST_E_IO int -201
29const ST_E_CORRUPT int -202
30const ST_E_READ_ONLY_DEGRADED int -203
31const ST_E_DUPLICATE_OBJECT_CONFLICT int -204
32const ST_E_NO_SPACE int -205
33const ST_E_CATALOG_FULL int -206
34const ST_E_GENERATION_EXHAUSTED int -207
35const ST_E_TRANSACTION_LIMIT int -208
36const ST_E_INVALID_OBJECT int -209
37const ST_E_NEEDS_REOPEN int -210
38const ST_E_ARG int -211
39const ST_E_GEOMETRY int -212
40
41note Internal root-validation results
42const ST_RE_OK int 0
43const ST_RE_IO int 1
44const ST_RE_GRAPH int 2
45
46note Media and workspace capacities
47const ST_MEDIA_UNITS ulong 256
48const ST_MEDIA_BYTES ulong 1048576
49const ST_CAT_CAP ulong 512
50const ST_CAT_BYTES ulong 32768
51const ST_CATBUF_BYTES ulong 36864
52const ST_TX_MAX_OBJS ulong 16
53const ST_TX_BUF_BYTES ulong 262144
54
55note Engine metadata indices (global st_meta ulong 16)
56const ST_M_BLKSIZE ulong 0
57const ST_M_BLKCNT ulong 1
58const ST_M_BASE_LBA ulong 2
59const ST_M_BPU ulong 3
60const ST_M_REG_UNITS ulong 4
61const ST_M_STATE ulong 5
62const ST_M_PEER ulong 6
63const ST_M_POISONED ulong 7
64const ST_M_CAT_IDX ulong 8
65const ST_M_CAT_N ulong 9
66const ST_M_ACTIVE_SLOT ulong 10
67const ST_M_GEN ulong 11
68const ST_M_HW ulong 12
69const ST_M_FAULT_WRITE ulong 13
70const ST_M_FAULT_FLUSH ulong 14
71const ST_M_WRITE_CNT ulong 15
72
73note Global media and engine state
74global st_meta ulong 16
75global st_media uchar 1048576
76global st_root_sb uchar 4096
77global st_root_cmt uchar 200
78global st_root_cid uchar 32
79global st_root_uuid uchar 16
80
81note Two catalog tables (active + spare, up to 512 entries * 64 bytes each)
82global st_cat0 uchar 32768
83global st_cat1 uchar 32768
84global st_catbuf uchar 36864
85
86note Scratch units and root-open temporaries
87global st_raw0 uchar 4096
88global st_raw1 uchar 4096
89global st_unit uchar 4096
90global st_plan_cmt uchar 4096
91global st_plan_sb uchar 4096
92global st_open_cmt0 uchar 200
93global st_open_cmt1 uchar 200
94global st_open_cid0 uchar 32
95global st_open_cid1 uchar 32
96global st_open_n ulong 2
97
98note Extent scratch for overlap & append-transition checks
99global st_ext_f ulong 512
100global st_ext_e ulong 512
101
102note Transaction staging buffers (up to 16 objects per transaction)
103global st_tx_n ulong 1
104global st_tx_pos ulong 1
105global st_tx_buf uchar 262144
106global st_tx_off ulong 16
107global st_tx_len ulong 16
108global st_tx_kind ushort 16
109global st_tx_ver ushort 16
110global st_tx_id uchar 512
111global st_tx_units uint 16
112global st_tx_first ulong 16
113global st_tx_append uchar 16
114
115note ---- Disk geometry adapter (store_disk.c) ----
116
117fn st_media_zero to void
118for i ulong ST_MEDIA_BYTES
119put st_media i 0
120end
121for i ulong 16
122put st_meta i 0
123end
124put st_meta ST_M_POISONED 1
125end
126
127fn st_set_faults fail_write ulong fail_flush ulong to void
128put st_meta ST_M_FAULT_WRITE fail_write
129put st_meta ST_M_FAULT_FLUSH fail_flush
130put st_meta ST_M_WRITE_CNT 0
131end
132
133fn st_disk_bind blk_size ulong blk_cnt ulong base_lba ulong req_units ulong to int
134bpu = ulong 0
135if eq blk_size 512
136bpu = 8
137else
138if eq blk_size 4096
139bpu = 1
140else
141ret ST_E_GEOMETRY
142end
143end
144if ne rem base_lba bpu 0
145ret ST_E_GEOMETRY
146end
147if ge base_lba blk_cnt
148ret ST_E_GEOMETRY
149end
150max_u = div sub blk_cnt base_lba bpu
151ru = req_units
152if eq ru 0
153ru = max_u
154end
155if or gt ru max_u gt mul add div base_lba bpu ru SV1_UNIT ST_MEDIA_BYTES
156ret ST_E_GEOMETRY
157end
158put st_meta ST_M_BLKSIZE blk_size
159put st_meta ST_M_BLKCNT blk_cnt
160put st_meta ST_M_BASE_LBA base_lba
161put st_meta ST_M_BPU bpu
162put st_meta ST_M_REG_UNITS ru
163ret SV1_OK
164end
165
166fn st_dev_read unit ulong dst edit uchar 4096 to int
167if ge unit get st_meta ST_M_REG_UNITS
168ret ST_E_IO
169end
170bpu = get st_meta ST_M_BPU
171bsize = get st_meta ST_M_BLKSIZE
172lba = add get st_meta ST_M_BASE_LBA mul unit bpu
173boff = mul lba bsize
174if gt add boff SV1_UNIT ST_MEDIA_BYTES
175ret ST_E_IO
176end
177for i ulong 4096
178put dst i get st_media add boff i
179end
180ret SV1_OK
181end
182
183fn st_dev_write unit ulong src view uchar 4096 to int
184if ge unit get st_meta ST_M_REG_UNITS
185ret ST_E_IO
186end
187fw = get st_meta ST_M_FAULT_WRITE
188wcnt = add get st_meta ST_M_WRITE_CNT 1
189put st_meta ST_M_WRITE_CNT wcnt
190if and ne fw 0 eq wcnt fw
191ret ST_E_IO
192end
193bpu = get st_meta ST_M_BPU
194bsize = get st_meta ST_M_BLKSIZE
195lba = add get st_meta ST_M_BASE_LBA mul unit bpu
196boff = mul lba bsize
197if gt add boff SV1_UNIT ST_MEDIA_BYTES
198ret ST_E_IO
199end
200for i ulong 4096
201put st_media add boff i get src i
202end
203ret SV1_OK
204end
205
206fn st_dev_flush to int
207if ne get st_meta ST_M_FAULT_FLUSH 0
208ret ST_E_IO
209end
210ret SV1_OK
211end
212
213fn st_media_poke unit ulong off ulong val uchar to void
214bpu = get st_meta ST_M_BPU
215bsize = get st_meta ST_M_BLKSIZE
216lba = add get st_meta ST_M_BASE_LBA mul unit bpu
217boff = add mul lba bsize off
218if lt boff ST_MEDIA_BYTES
219put st_media boff val
220end
221end
222
223note ---- Catalog table accessors (cat_idx 0 -> st_cat0, 1 -> st_cat1) ----
224
225fn st_cat_get_entry cidx ulong idx ulong dst edit uchar 64 to void
226base = mul idx SV1_CATALOG_ENTRY
227if eq cidx 0
228for i ulong 64
229put dst i get st_cat0 add base i
230end
231else
232for i ulong 64
233put dst i get st_cat1 add base i
234end
235end
236end
237
238fn st_cat_put_entry cidx ulong idx ulong src view uchar 64 to void
239base = mul idx SV1_CATALOG_ENTRY
240if eq cidx 0
241for i ulong 64
242put st_cat0 add base i get src i
243end
244else
245for i ulong 64
246put st_cat1 add base i get src i
247end
248end
249end
250
251fn st_cat_cmp_idx cidx ulong a ulong b ulong to int
252ea = new uchar 64 0
253eb = new uchar 64 0
254st_cat_get_entry cidx a ea
255st_cat_get_entry cidx b eb
256ret sv1_cmp_id ea 0 eb 0
257end
258
259fn st_cat_swap cidx ulong a ulong b ulong to void
260ea = new uchar 64 0
261eb = new uchar 64 0
262st_cat_get_entry cidx a ea
263st_cat_get_entry cidx b eb
264st_cat_put_entry cidx a eb
265st_cat_put_entry cidx b ea
266end
267
268fn st_cat_check_order cidx ulong n ulong to int
269if lt n 2
270ret SV1_OK
271end
272ea = new uchar 64 0
273eb = new uchar 64 0
274i = ulong 0
275lim = sub n 1
276while lt i lim bound 512
277st_cat_get_entry cidx i ea
278st_cat_get_entry cidx add i 1 eb
279c = sv1_cmp_id ea 0 eb 0
280if eq c 0
281ret SV1_E_DUPLICATE_OBJECT
282end
283if gt c 0
284ret SV1_E_CATALOG_ORDER
285end
286i = add i 1
287end
288ret SV1_OK
289end
290
291fn st_cat_find cidx ulong n ulong id view uchar * to int
292lo = ulong 0
293hi = n
294ent = new uchar 64 0
295while lt lo hi bound 32
296mid = add lo div sub hi lo 2
297st_cat_get_entry cidx mid ent
298c = sv1_cmp_id ent 0 id 0
299if eq c 0
300ret int mid
301end
302if lt c 0
303lo = add mid 1
304else
305hi = mid
306end
307end
308ret -1
309end
310
311fn st_cat_sift cidx ulong start ulong n ulong to void
312root = start
313while true bound 32
314ch = add mul 2 root 1
315if ge ch n
316ret
317end
318if lt add ch 1 n
319if lt st_cat_cmp_idx cidx ch add ch 1 0
320ch = add ch 1
321end
322end
323if ge st_cat_cmp_idx cidx root ch 0
324ret
325end
326st_cat_swap cidx root ch
327root = ch
328end
329end
330
331fn st_cat_sort cidx ulong n ulong to void
332if lt n 2
333ret
334end
335i = div n 2
336while gt i 0 bound 512
337i = sub i 1
338st_cat_sift cidx i n
339end
340en = sub n 1
341while gt en 0 bound 512
342st_cat_swap cidx 0 en
343st_cat_sift cidx 0 en
344en = sub en 1
345end
346end
347
348note ---- Extent heapsort (st_ext_f / st_ext_e) ----
349
350fn st_ext_less a ulong b ulong to bool
351fa = get st_ext_f a
352fb = get st_ext_f b
353if lt fa fb
354ret true
355end
356if eq fa fb
357ret lt get st_ext_e a get st_ext_e b
358end
359ret false
360end
361
362fn st_ext_swap a ulong b ulong to void
363tf = get st_ext_f a
364te = get st_ext_e a
365put st_ext_f a get st_ext_f b
366put st_ext_e a get st_ext_e b
367put st_ext_f b tf
368put st_ext_e b te
369end
370
371fn st_ext_sift start ulong n ulong to void
372root = start
373while true bound 32
374ch = add mul 2 root 1
375if ge ch n
376ret
377end
378if lt add ch 1 n
379if st_ext_less ch add ch 1
380ch = add ch 1
381end
382end
383if not st_ext_less root ch
384ret
385end
386st_ext_swap root ch
387root = ch
388end
389end
390
391fn st_ext_sort n ulong to void
392if lt n 2
393ret
394end
395i = div n 2
396while gt i 0 bound 512
397i = sub i 1
398st_ext_sift i n
399end
400en = sub n 1
401while gt en 0 bound 512
402st_ext_swap 0 en
403st_ext_sift 0 en
404en = sub en 1
405end
406end
407
408note ---- Catalog encode / decode / validate ----
409
410fn st_cat_encode_to_buf cidx ulong n ulong out_len edit ulong 1 to int
411if gt n ST_CAT_CAP
412ret SV1_E_CATALOG_TOO_LARGE
413end
414r = st_cat_check_order cidx n
415if ne r SV1_OK
416ret r
417end
418clen = sv1_catalog_len n
419if gt clen ST_CATBUF_BYTES
420ret SV1_E_ARG
421end
422hdr = new uchar 16 0
423for i ulong 8
424put hdr i get sv1_cat_magic i
425end
426sv1_put16 hdr 8 1
427sv1_put16 hdr 10 ushort SV1_CATALOG_ENTRY
428sv1_put32 hdr 12 uint n
429for i ulong 16
430put st_catbuf i get hdr i
431end
432ent = new uchar 64 0
433i = ulong 0
434while lt i n bound 512
435st_cat_get_entry cidx i ent
436r = sv1_entry_validate_shape ent 0
437if ne r SV1_OK
438ret r
439end
440off = add SV1_CATALOG_HEADER mul i SV1_CATALOG_ENTRY
441for j ulong 64
442put st_catbuf add off j get ent j
443end
444i = add i 1
445end
446put out_len 0 clen
447ret SV1_OK
448end
449
450fn st_cat_decode_from_buf blen ulong cidx ulong n_out edit ulong 1 to int
451if lt blen SV1_CATALOG_HEADER
452ret SV1_E_INVALID_LENGTH
453end
454hdr = new uchar 16 0
455for i ulong 16
456put hdr i get st_catbuf i
457end
458if not sv1_equal part hdr 0 8 sv1_cat_magic
459ret SV1_E_BAD_CATALOG_MAGIC
460end
461if or ne sv1_get16 hdr 8 1 ne sv1_get16 hdr 10 ushort SV1_CATALOG_ENTRY
462ret SV1_E_UNSUPPORTED_VERSION
463end
464cnt = ulong sv1_get32 hdr 12
465if gt cnt SV1_MAX_CATALOG_ENTRIES
466ret SV1_E_CATALOG_TOO_LARGE
467end
468if ne blen sv1_catalog_len cnt
469ret SV1_E_INVALID_LENGTH
470end
471if gt cnt ST_CAT_CAP
472ret SV1_E_ARG
473end
474ent = new uchar 64 0
475i = ulong 0
476while lt i cnt bound 512
477off = add SV1_CATALOG_HEADER mul i SV1_CATALOG_ENTRY
478for j ulong 64
479put ent j get st_catbuf add off j
480end
481r = sv1_entry_validate_shape ent 0
482if ne r SV1_OK
483ret r
484end
485st_cat_put_entry cidx i ent
486i = add i 1
487end
488r = st_cat_check_order cidx cnt
489if ne r SV1_OK
490ret r
491end
492put n_out 0 cnt
493ret SV1_OK
494end
495
496fn st_cat_validate_extents cidx ulong n ulong reg_u ulong cat_fu ulong hw ulong to int
497if or lt reg_u 4 gt reg_u SV1_MAX_REGION_UNITS
498ret SV1_E_OUT_OF_BOUNDS
499end
500if or lt hw 4 gt hw reg_u
501ret SV1_E_OUT_OF_BOUNDS
502end
503if or lt cat_fu 2 ge cat_fu reg_u
504ret SV1_E_OUT_OF_BOUNDS
505end
506ent = new uchar 64 0
507i = ulong 0
508while lt i n bound 512
509st_cat_get_entry cidx i ent
510r = sv1_entry_validate_bounds ent 0 reg_u cat_fu hw
511if ne r SV1_OK
512ret r
513end
514fu = sv1_get64 ent 36
515uc = ulong sv1_get32 ent 52
516put st_ext_f i fu
517put st_ext_e i add fu uc
518i = add i 1
519end
520st_ext_sort n
521if ge n 2
522lim = sub n 1
523i = ulong 0
524while lt i lim bound 512
525if gt get st_ext_e i get st_ext_f add i 1
526ret SV1_E_OVERLAP
527end
528i = add i 1
529end
530end
531ret SV1_OK
532end
533
534note ---- Streamed object verification & reading ----
535
536fn st_hash_unit_step ctx edit ulong 40 ubuf view uchar 4096 start ulong blen ulong to bool
537take = ulong 0
538if gt blen start
539take = sub blen start
540end
541if gt take SV1_UNIT
542take = SV1_UNIT
543end
544sha256_update ctx part ubuf 0 take
545ret sv1_all_zero ubuf take sub SV1_UNIT take
546end
547
548fn st_verify_digest ctx edit ulong 40 ent view uchar 64 to int
549got = new uchar 32 0
550sha256_final ctx got
551if not sv1_equal got part ent 0 32
552ret ST_RE_GRAPH
553end
554ret ST_RE_OK
555end
556
557fn st_verify_app_object ent view uchar 64 to int
558kind = sv1_get16 ent 32
559ver = sv1_get16 ent 34
560fu = sv1_get64 ent 36
561blen = sv1_get64 ent 44
562ucnt = sv1_get32 ent 52
563ctx = new ulong 40 0
564graph = false
565u = new uint 1 0
566if or ne sv1_unit_count blen u SV1_OK ne get u 0 ucnt
567graph = true
568end
569if not graph
570if ne sv1_object_id_begin ctx kind ver blen SV1_OK
571graph = true
572end
573end
574ubuf = new uchar 4096 0
575i = ulong 0
576ulim = ulong ucnt
577while lt i ulim bound 256
578if ne st_dev_read add fu i ubuf SV1_OK
579ret ST_RE_IO
580end
581if not graph
582if not st_hash_unit_step ctx ubuf mul i SV1_UNIT blen
583graph = true
584end
585end
586i = add i 1
587end
588if graph
589ret ST_RE_GRAPH
590end
591ret st_verify_digest ctx ent
592end
593
594fn st_load_cat_units cat_fu ulong cat_uc ulong cat_bl ulong to int
595if gt mul cat_uc SV1_UNIT ST_CATBUF_BYTES
596ret ST_RE_GRAPH
597end
598ubuf = new uchar 4096 0
599i = ulong 0
600while lt i cat_uc bound 65
601if ne st_dev_read add cat_fu i ubuf SV1_OK
602ret ST_RE_IO
603end
604off = mul i SV1_UNIT
605for j ulong 4096
606put st_catbuf add off j get ubuf j
607end
608i = add i 1
609end
610p_start = cat_bl
611p_end = mul cat_uc SV1_UNIT
612while lt p_start p_end bound 36864
613if ne get st_catbuf p_start 0
614ret ST_RE_GRAPH
615end
616p_start = add p_start 1
617end
618ret ST_RE_OK
619end
620
621fn st_hash_catbuf cat_bl ulong digest_out edit uchar 32 to int
622ctx = new ulong 40 0
623if ne sv1_object_id_begin ctx SV1_KIND_CATALOG SV1_VERSION cat_bl SV1_OK
624ret ST_RE_GRAPH
625end
626ubuf = new uchar 4096 0
627pos = ulong 0
628while lt pos cat_bl bound 65
629take = sub cat_bl pos
630if gt take SV1_UNIT
631take = SV1_UNIT
632end
633for j ulong 4096
634if lt j take
635put ubuf j get st_catbuf add pos j
636end
637end
638sha256_update ctx part ubuf 0 take
639pos = add pos take
640end
641sha256_final ctx digest_out
642ret ST_RE_OK
643end
644
645fn st_validate_root_cat cmt_out view uchar 200 cidx ulong n_out edit ulong 1 to int
646cat_fu = sv1_get64 cmt_out 168
647cat_bl = sv1_get64 cmt_out 176
648cat_uc = ulong sv1_get32 cmt_out 184
649lr = st_load_cat_units cat_fu cat_uc cat_bl
650if ne lr ST_RE_OK
651ret lr
652end
653got_cat_id = new uchar 32 0
654hr = st_hash_catbuf cat_bl got_cat_id
655if ne hr ST_RE_OK
656ret hr
657end
658if not sv1_equal got_cat_id part cmt_out 136 32
659ret ST_RE_GRAPH
660end
661if ne st_cat_decode_from_buf cat_bl cidx n_out SV1_OK
662ret ST_RE_GRAPH
663end
664cnt = get n_out 0
665if ne cnt ulong sv1_get32 cmt_out 188
666ret ST_RE_GRAPH
667end
668if ne st_cat_validate_extents cidx cnt sv1_get64 cmt_out 48 cat_fu sv1_get64 cmt_out 192 SV1_OK
669ret ST_RE_GRAPH
670end
671ent = new uchar 64 0
672i = ulong 0
673while lt i cnt bound 512
674st_cat_get_entry cidx i ent
675r = st_verify_app_object ent
676if ne r ST_RE_OK
677ret r
678end
679i = add i 1
680end
681ret ST_RE_OK
682end
683
684fn st_validate_root sb view uchar 4096 cidx ulong cmt_out edit uchar 200 cid_out edit uchar 32 n_out edit ulong 1 to int
685if ne sv1_get64 sb 48 get st_meta ST_M_REG_UNITS
686ret ST_RE_GRAPH
687end
688ubuf = new uchar 4096 0
689if ne st_dev_read sv1_get64 sb 96 ubuf SV1_OK
690ret ST_RE_IO
691end
692if not sv1_all_zero ubuf SV1_COMMIT_BYTES sub SV1_UNIT SV1_COMMIT_BYTES
693ret ST_RE_GRAPH
694end
695if ne sv1_object_id SV1_KIND_COMMIT SV1_VERSION part ubuf 0 SV1_COMMIT_BYTES cid_out SV1_OK
696ret ST_RE_GRAPH
697end
698if not sv1_equal cid_out part sb 64 32
699ret ST_RE_GRAPH
700end
701for i ulong 200
702put cmt_out i get ubuf i
703end
704if ne sv1_commit_validate cmt_out 0 SV1_OK
705ret ST_RE_GRAPH
706end
707if ne sv1_superblock_validate_commit sb cid_out cmt_out SV1_OK
708ret ST_RE_GRAPH
709end
710ret st_validate_root_cat cmt_out cidx n_out
711end
712
713fn st_verify_append_trans old_idx ulong old_n ulong new_idx ulong new_n ulong old_hw ulong new_cat_fu ulong to int
714oe = new uchar 64 0
715ne_ent = new uchar 64 0
716i = ulong 0
717while lt i old_n bound 512
718st_cat_get_entry old_idx i oe
719j = st_cat_find new_idx new_n part oe 0 32
720if lt j 0
721ret ST_M_INCONSISTENT_HISTORY
722end
723st_cat_get_entry new_idx ulong j ne_ent
724if not sv1_equal ne_ent oe
725ret ST_M_INCONSISTENT_HISTORY
726end
727i = add i 1
728end
729k = ulong 0
730i = ulong 0
731while lt i new_n bound 512
732st_cat_get_entry new_idx i ne_ent
733j = st_cat_find old_idx old_n part ne_ent 0 32
734if ge j 0
735st_cat_get_entry old_idx ulong j oe
736if not sv1_equal oe ne_ent
737ret ST_M_INCONSISTENT_HISTORY
738end
739else
740put st_ext_f k sv1_get64 ne_ent 36
741put st_ext_e k ulong sv1_get32 ne_ent 52
742k = add k 1
743end
744i = add i 1
745end
746st_ext_sort k
747nxt = old_hw
748i = ulong 0
749while lt i k bound 512
750if ne get st_ext_f i nxt
751ret ST_M_INCONSISTENT_HISTORY
752end
753uc = get st_ext_e i
754if gt nxt sub SV1_U64_MAX uc
755ret ST_M_INCONSISTENT_HISTORY
756end
757nxt = add nxt uc
758i = add i 1
759end
760if ne nxt new_cat_fu
761ret ST_M_INCONSISTENT_HISTORY
762end
763ret SV1_OK
764end
765
766fn st_install_root slot ulong sb view uchar 4096 cmt view uchar 200 cid view uchar 32 n ulong to void
767for i ulong 4096
768put st_root_sb i get sb i
769end
770for i ulong 200
771put st_root_cmt i get cmt i
772end
773for i ulong 32
774put st_root_cid i get cid i
775end
776for i ulong 16
777put st_root_uuid i get sb add 28 i
778end
779put st_meta ST_M_CAT_IDX slot
780put st_meta ST_M_CAT_N n
781put st_meta ST_M_ACTIVE_SLOT ulong sv1_get32 sb 44
782put st_meta ST_M_GEN sv1_get64 cmt 56
783put st_meta ST_M_HW sv1_get64 cmt 192
784end
785
786fn st_is_unsup_sb sb view uchar 4096 magic bool crc_ok bool to bool
787if or not magic not crc_ok
788ret false
789end
790if or ne sv1_get16 sb 8 1 ne sv1_get16 sb 10 0
791ret true
792end
793ret not sv1_all_zero sb 12 16
794end
795
796note ---- Public Store Engine API ----
797
798fn st_format uuid view uchar 16 to int
799ru = get st_meta ST_M_REG_UNITS
800if or lt ru 4 gt ru SV1_MAX_REGION_UNITS
801ret SV1_E_MALFORMED_COMMIT
802end
803g = new uchar 16384 0
804r = sv1_genesis_units uuid ru g
805if ne r SV1_OK
806ret r
807end
808zu = new uchar 4096 0
809if or ne st_dev_write 0 zu SV1_OK or ne st_dev_write 1 zu SV1_OK ne st_dev_flush SV1_OK
810ret ST_E_IO
811end
812u2 = new uchar 4096 0
813u3 = new uchar 4096 0
814u0 = new uchar 4096 0
815for i ulong 4096
816put u0 i get g i
817put u2 i get g add 8192 i
818put u3 i get g add 12288 i
819end
820if or ne st_dev_write 2 u2 SV1_OK or ne st_dev_write 3 u3 SV1_OK ne st_dev_flush SV1_OK
821ret ST_E_IO
822end
823if or ne st_dev_write 0 u0 SV1_OK ne st_dev_flush SV1_OK
824ret ST_E_IO
825end
826ret SV1_OK
827end
828
829note Slot flags: 1=zero 2=magic 4=crc 8=unsup 16=dec 32=mal 64=cs 128=have 256=gbad
830fn st_classify_slot r view uchar 4096 slot uint ru ulong to uint
831fl = uint 0
832z = sv1_all_zero r 0 SV1_UNIT
833m = sv1_equal part r 0 8 sv1_sb_magic
834c = sv1_superblock_crc_ok r 0
835u = st_is_unsup_sb r m c
836if z
837fl = or fl 1
838end
839if m
840fl = or fl 2
841end
842if c
843fl = or fl 4
844end
845if u
846fl = or fl 8
847end
848if not z
849if or not m or not c u
850fl = or fl 32
851else
852fl = or fl 64
853if or ne sv1_superblock_decode r 0 slot SV1_OK ne sv1_get64 r 48 ru
854fl = or fl 32
855else
856fl = or fl 16
857end
858end
859end
860ret fl
861end
862
863fn st_open_both_valid r0 view uchar 4096 cmt0 view uchar 200 cid0 view uchar 32 r1 view uchar 4096 cmt1 view uchar 200 cid1 view uchar 32 to int
864n0 = ulong sv1_get32 cmt0 188
865n1 = ulong sv1_get32 cmt1 188
866g0 = sv1_get64 cmt0 56
867g1 = sv1_get64 cmt1 56
868if eq g0 g1
869if not sv1_superblock_equivalent r0 r1
870ret ST_M_CONFLICTING_ROOTS
871end
872st_install_root 0 r0 cmt0 cid0 n0
873put st_meta ST_M_STATE ulong long ST_VALID
874put st_meta ST_M_PEER ulong long ST_PEER_VALID
875ret SV1_OK
876end
877if lt g0 g1
878if or eq g0 SV1_U64_MAX ne add g0 1 g1
879ret ST_M_INCONSISTENT_HISTORY
880end
881if not sv1_commit_identifies_pred cmt1 cmt0 cid0
882ret ST_M_INCONSISTENT_HISTORY
883end
884ar = st_verify_append_trans 0 n0 1 n1 sv1_get64 cmt0 192 sv1_get64 cmt1 168
885if ne ar SV1_OK
886ret ar
887end
888st_install_root 1 r1 cmt1 cid1 n1
889else
890if or eq g1 SV1_U64_MAX ne add g1 1 g0
891ret ST_M_INCONSISTENT_HISTORY
892end
893if not sv1_commit_identifies_pred cmt0 cmt1 cid1
894ret ST_M_INCONSISTENT_HISTORY
895end
896ar = st_verify_append_trans 1 n1 0 n0 sv1_get64 cmt1 192 sv1_get64 cmt0 168
897if ne ar SV1_OK
898ret ar
899end
900st_install_root 0 r0 cmt0 cid0 n0
901end
902put st_meta ST_M_STATE ulong long ST_VALID
903put st_meta ST_M_PEER ulong long ST_PEER_VALID
904ret SV1_OK
905end
906
907fn st_open_one_peer r_good view uchar 4096 r_other view uchar 4096 fl_other uint to int
908if ne and fl_other 1 0
909put st_meta ST_M_STATE ulong long ST_VALID
910put st_meta ST_M_PEER ulong long ST_PEER_ZERO
911ret SV1_OK
912end
913if ne and fl_other 32 0
914put st_meta ST_M_STATE ulong long ST_DEGRADED_RECOVERY
915put st_meta ST_M_PEER ulong long ST_PEER_MALFORMED
916ret SV1_OK
917end
918if and ne and fl_other 256 0 ne and fl_other 64 0
919og = sv1_get64 r_other 56
920gg = sv1_get64 r_good 56
921if gt og gg
922put st_meta ST_M_STATE ulong long ST_DEGRADED_RECOVERY
923put st_meta ST_M_PEER ulong long ST_PEER_GRAPH_BAD_NEWER
924ret SV1_OK
925end
926if lt og gg
927put st_meta ST_M_STATE ulong long ST_VALID
928put st_meta ST_M_PEER ulong long ST_PEER_GRAPH_BAD_OLDER
929ret SV1_OK
930end
931end
932ret ST_M_CORRUPT_RECOVERY_REQUIRED
933end
934
935fn st_open_impl to int
936ru = get st_meta ST_M_REG_UNITS
937if or lt ru 4 gt ru SV1_MAX_REGION_UNITS
938ret ST_M_CORRUPT_RECOVERY_REQUIRED
939end
940r0 = new uchar 4096 0
941r1 = new uchar 4096 0
942if ne st_dev_read 0 r0 SV1_OK
943ret ST_M_IO
944end
945if ne st_dev_read 1 r1 SV1_OK
946ret ST_M_IO
947end
948f0 = st_classify_slot r0 0 ru
949f1 = st_classify_slot r1 1 ru
950if and ne and f0 1 0 ne and f1 1 0
951ret ST_M_UNFORMATTED
952end
953if and eq and f0 2 0 eq and f1 2 0
954ret ST_M_FOREIGN
955end
956if or eq and f0 12 12 eq and f1 12 12
957ret ST_M_UNSUPPORTED_VERSION
958end
959cmt0 = new uchar 200 0
960cid0 = new uchar 32 0
961ntmp = new ulong 1 0
962if ne and f0 16 0
963vr0 = st_validate_root r0 0 cmt0 cid0 ntmp
964if eq vr0 ST_RE_IO
965ret ST_M_IO
966end
967if eq vr0 ST_RE_GRAPH
968f0 = or f0 256
969else
970f0 = or f0 128
971end
972end
973cmt1 = new uchar 200 0
974cid1 = new uchar 32 0
975if ne and f1 16 0
976vr1 = st_validate_root r1 1 cmt1 cid1 ntmp
977if eq vr1 ST_RE_IO
978ret ST_M_IO
979end
980if eq vr1 ST_RE_GRAPH
981f1 = or f1 256
982else
983f1 = or f1 128
984end
985end
986put st_meta ST_M_POISONED 0
987if ne and and f0 f1 128 0
988ret st_open_both_valid r0 cmt0 cid0 r1 cmt1 cid1
989end
990if ne and f0 128 0
991st_install_root 0 r0 cmt0 cid0 ulong sv1_get32 cmt0 188
992ret st_open_one_peer r0 r1 f1
993end
994if ne and f1 128 0
995st_install_root 1 r1 cmt1 cid1 ulong sv1_get32 cmt1 188
996ret st_open_one_peer r1 r0 f0
997end
998ret ST_M_CORRUPT_RECOVERY_REQUIRED
999end
1000
1001fn st_open to int
1002put st_meta ST_M_POISONED 1
1003ret st_open_impl
1004end
1005
1006fn st_generation to ulong
1007ret get st_meta ST_M_GEN
1008end
1009
1010fn st_high_water to ulong
1011ret get st_meta ST_M_HW
1012end
1013
1014fn st_active_slot to ulong
1015ret get st_meta ST_M_ACTIVE_SLOT
1016end
1017
1018fn st_state to int
1019ret int long get st_meta ST_M_STATE
1020end
1021
1022fn st_peer to int
1023ret int long get st_meta ST_M_PEER
1024end
1025
1026fn st_catalog_count to ulong
1027ret get st_meta ST_M_CAT_N
1028end
1029
1030fn st_catalog_entry idx ulong dst edit uchar 64 to bool
1031if ge idx get st_meta ST_M_CAT_N
1032ret false
1033end
1034st_cat_get_entry get st_meta ST_M_CAT_IDX idx dst
1035ret true
1036end
1037
1038fn st_get_uuid dst edit uchar 16 to void
1039for i ulong 16
1040put dst i get st_root_uuid i
1041end
1042end
1043
1044note ---- Transaction builder & commit ----
1045
1046fn st_tx_begin to void
1047put st_tx_n 0 0
1048put st_tx_pos 0 0
1049end
1050
1051fn st_tx_add kind ushort ver ushort data view uchar * to int
1052n = get st_tx_n 0
1053if ge n ST_TX_MAX_OBJS
1054ret ST_E_TRANSACTION_LIMIT
1055end
1056dlen = len data
1057pos = get st_tx_pos 0
1058if gt add pos dlen ST_TX_BUF_BYTES
1059ret ST_E_ARG
1060end
1061put st_tx_kind n kind
1062put st_tx_ver n ver
1063put st_tx_off n pos
1064put st_tx_len n dlen
1065i = ulong 0
1066while lt i dlen bound 65536
1067put st_tx_buf add pos i get data i
1068i = add i 1
1069end
1070put st_tx_pos 0 add pos dlen
1071put st_tx_n 0 add n 1
1072ret SV1_OK
1073end
1074
1075fn st_write_staged_buf first ulong units ulong boff ulong blen ulong to int
1076ubuf = new uchar 4096 0
1077i = ulong 0
1078while lt i units bound 256
1079start = mul i SV1_UNIT
1080take = ulong 0
1081if gt blen start
1082take = sub blen start
1083end
1084if gt take SV1_UNIT
1085take = SV1_UNIT
1086end
1087for j ulong 4096
1088if lt j take
1089put ubuf j get st_tx_buf add boff add start j
1090else
1091put ubuf j 0
1092end
1093end
1094if ne st_dev_write add first i ubuf SV1_OK
1095ret ST_E_IO
1096end
1097i = add i 1
1098end
1099ret SV1_OK
1100end
1101
1102fn st_write_catbuf first ulong units ulong blen ulong to int
1103ubuf = new uchar 4096 0
1104i = ulong 0
1105while lt i units bound 65
1106start = mul i SV1_UNIT
1107take = ulong 0
1108if gt blen start
1109take = sub blen start
1110end
1111if gt take SV1_UNIT
1112take = SV1_UNIT
1113end
1114for j ulong 4096
1115if lt j take
1116put ubuf j get st_catbuf add start j
1117else
1118put ubuf j 0
1119end
1120end
1121if ne st_dev_write add first i ubuf SV1_OK
1122ret ST_E_IO
1123end
1124i = add i 1
1125end
1126ret SV1_OK
1127end
1128
1129fn st_copy_tx_chunk ubuf edit uchar 4096 src ulong take ulong to void
1130for j ulong 4096
1131if lt j take
1132put ubuf j get st_tx_buf add src j
1133end
1134end
1135end
1136
1137fn st_tx_hash_staged idx ulong id_out edit uchar 32 to int
1138boff = get st_tx_off idx
1139blen = get st_tx_len idx
1140ctx = new ulong 40 0
1141r = sv1_object_id_begin ctx get st_tx_kind idx get st_tx_ver idx blen
1142if ne r SV1_OK
1143ret r
1144end
1145ubuf = new uchar 4096 0
1146pos = ulong 0
1147while lt pos blen bound 65
1148take = sub blen pos
1149if gt take SV1_UNIT
1150take = SV1_UNIT
1151end
1152st_copy_tx_chunk ubuf add boff pos take
1153sha256_update ctx part ubuf 0 take
1154pos = add pos take
1155end
1156sha256_final ctx id_out
1157ret SV1_OK
1158end
1159
1160fn st_tx_id_eq j ulong id view uchar 32 to bool
1161base = mul j 32
1162for b ulong 32
1163if ne get st_tx_id add base b get id b
1164ret false
1165end
1166end
1167ret true
1168end
1169
1170fn st_tx_find_dup k ulong i ulong id view uchar 32 to int
1171j = ulong 0
1172while lt j k bound 16
1173if st_tx_id_eq j id
1174if ne get st_tx_kind j get st_tx_kind i
1175ret -1
1176end
1177if ne get st_tx_ver j get st_tx_ver i
1178ret -1
1179end
1180if ne get st_tx_len j get st_tx_len i
1181ret -1
1182end
1183ret 1
1184end
1185j = add j 1
1186end
1187ret 0
1188end
1189
1190fn st_tx_step1_dedup n ulong k_out edit ulong 1 to int
1191k = ulong 0
1192id = new uchar 32 0
1193u = new uint 1 0
1194i = ulong 0
1195while lt i n bound 16
1196r = st_tx_hash_staged i id
1197if ne r SV1_OK
1198ret r
1199end
1200r = sv1_unit_count get st_tx_len i u
1201if ne r SV1_OK
1202ret r
1203end
1204d = st_tx_find_dup k i id
1205if lt d 0
1206ret ST_E_DUPLICATE_OBJECT_CONFLICT
1207end
1208if eq d 0
1209base = mul k 32
1210for b ulong 32
1211put st_tx_id add base b get id b
1212end
1213put st_tx_kind k get st_tx_kind i
1214put st_tx_ver k get st_tx_ver i
1215put st_tx_off k get st_tx_off i
1216put st_tx_len k get st_tx_len i
1217put st_tx_units k get u 0
1218put st_tx_first k 0
1219put st_tx_append k 0
1220k = add k 1
1221end
1222i = add i 1
1223end
1224put k_out 0 k
1225ret SV1_OK
1226end
1227
1228fn st_tx_check_existing old_idx ulong found ulong i ulong ent edit uchar 64 to int
1229st_cat_get_entry old_idx found ent
1230if or ne sv1_get16 ent 32 get st_tx_kind i ne sv1_get16 ent 34 get st_tx_ver i
1231ret ST_E_DUPLICATE_OBJECT_CONFLICT
1232end
1233if or ne sv1_get64 ent 44 get st_tx_len i ne sv1_get32 ent 52 get st_tx_units i
1234ret ST_E_DUPLICATE_OBJECT_CONFLICT
1235end
1236vr = st_verify_app_object ent
1237if eq vr ST_RE_IO
1238ret ST_E_IO
1239end
1240if eq vr ST_RE_GRAPH
1241ret ST_E_CORRUPT
1242end
1243ret SV1_OK
1244end
1245
1246fn st_tx_fill_new_ent ent edit uchar 64 id view uchar 32 i ulong nxt ulong to void
1247for b ulong 64
1248put ent b 0
1249end
1250for b ulong 32
1251put ent b get id b
1252end
1253sv1_put16 ent 32 get st_tx_kind i
1254sv1_put16 ent 34 get st_tx_ver i
1255sv1_put64 ent 36 nxt
1256sv1_put64 ent 44 get st_tx_len i
1257sv1_put32 ent 52 get st_tx_units i
1258sv1_put16 ent 56 0
1259end
1260
1261fn st_tx_load_id i ulong id edit uchar 32 to void
1262base = mul i 32
1263for b ulong 32
1264put id b get st_tx_id add base b
1265end
1266end
1267
1268fn st_tx_step2_build_cat k ulong spare ulong out_mn edit ulong 2 to int
1269old_idx = get st_meta ST_M_CAT_IDX
1270old_n = get st_meta ST_M_CAT_N
1271ent = new uchar 64 0
1272id = new uchar 32 0
1273i = ulong 0
1274while lt i old_n bound 512
1275st_cat_get_entry old_idx i ent
1276st_cat_put_entry spare i ent
1277i = add i 1
1278end
1279m = old_n
1280app_u = ulong 0
1281nxt = get st_meta ST_M_HW
1282i = ulong 0
1283while lt i k bound 16
1284st_tx_load_id i id
1285found = st_cat_find old_idx old_n id
1286if ge found 0
1287r = st_tx_check_existing old_idx ulong found i ent
1288if ne r SV1_OK
1289ret r
1290end
1291else
1292if ge m ST_CAT_CAP
1293ret ST_E_CATALOG_FULL
1294end
1295uc = ulong get st_tx_units i
1296if gt app_u sub SV1_U64_MAX uc
1297ret ST_E_NO_SPACE
1298end
1299app_u = add app_u uc
1300st_tx_fill_new_ent ent id i nxt
1301st_cat_put_entry spare m ent
1302m = add m 1
1303put st_tx_first i nxt
1304put st_tx_append i 1
1305if gt nxt sub SV1_U64_MAX uc
1306ret ST_E_NO_SPACE
1307end
1308nxt = add nxt uc
1309end
1310i = add i 1
1311end
1312st_cat_sort spare m
1313if gt app_u SV1_MAX_TX_UNITS
1314ret ST_E_TRANSACTION_LIMIT
1315end
1316put out_mn 0 m
1317put out_mn 1 nxt
1318ret SV1_OK
1319end
1320
1321note txp: 0=ru 1=ngen 2=cur_gen 3=cat_fu 4=cat_len 5=cat_u 6=m 7=hw 8=cmt_u 9=nslot
1322fn st_tx_build_commit txp view ulong 10 cat_id view uchar 32 pc edit uchar 4096 cmt_id edit uchar 32 to int
1323for j ulong 8
1324put pc j get sv1_cmt_magic j
1325end
1326sv1_put16 pc 8 1
1327sv1_put16 pc 10 ushort SV1_COMMIT_BYTES
1328sv1_put16 pc 12 1
1329sv1_put16 pc 14 0
1330for j ulong 16
1331put pc add 32 j get st_root_uuid j
1332end
1333sv1_put64 pc 48 get txp 0
1334sv1_put64 pc 56 get txp 1
1335sv1_put64 pc 64 get txp 2
1336for j ulong 32
1337put pc add 72 j get st_root_cid j
1338put pc add 104 j get st_root_cmt add 136 j
1339put pc add 136 j get cat_id j
1340end
1341sv1_put64 pc 168 get txp 3
1342sv1_put64 pc 176 get txp 4
1343sv1_put32 pc 184 uint get txp 5
1344sv1_put32 pc 188 uint get txp 6
1345sv1_put64 pc 192 get txp 7
1346r = sv1_commit_validate pc 0
1347if ne r SV1_OK
1348ret r
1349end
1350ret sv1_object_id SV1_KIND_COMMIT SV1_VERSION part pc 0 SV1_COMMIT_BYTES cmt_id
1351end
1352
1353fn st_tx_build_sb txp view ulong 10 cat_id view uchar 32 cmt_id view uchar 32 psb edit uchar 4096 to void
1354for j ulong 8
1355put psb j get sv1_sb_magic j
1356end
1357sv1_put16 psb 8 1
1358sv1_put16 psb 10 0
1359for j ulong 16
1360put psb add 28 j get st_root_uuid j
1361end
1362sv1_put32 psb 44 uint get txp 9
1363sv1_put64 psb 48 get txp 0
1364sv1_put64 psb 56 get txp 1
1365for j ulong 32
1366put psb add 64 j get cmt_id j
1367put psb add 104 j get cat_id j
1368end
1369sv1_put64 psb 96 get txp 8
1370sv1_put64 psb 136 get txp 3
1371sv1_put64 psb 144 get txp 4
1372sv1_put32 psb 152 uint get txp 5
1373sv1_put32 psb 156 uint get txp 6
1374sv1_put64 psb 160 get txp 7
1375sv1_put32 psb SV1_SB_CRC_OFFSET sv1_sb_crc psb 0
1376end
1377
1378fn st_tx_write_all k ulong txp view ulong 10 pc view uchar 4096 psb view uchar 4096 to int
1379i = ulong 0
1380while lt i k bound 16
1381if ne get st_tx_append i 0
1382if ne st_write_staged_buf get st_tx_first i ulong get st_tx_units i get st_tx_off i get st_tx_len i SV1_OK
1383put st_meta ST_M_POISONED 1
1384ret ST_E_IO
1385end
1386end
1387i = add i 1
1388end
1389if ne st_write_catbuf get txp 3 get txp 5 get txp 4 SV1_OK
1390put st_meta ST_M_POISONED 1
1391ret ST_E_IO
1392end
1393if ne st_dev_write get txp 8 pc SV1_OK
1394put st_meta ST_M_POISONED 1
1395ret ST_E_IO
1396end
1397if ne st_dev_flush SV1_OK
1398put st_meta ST_M_POISONED 1
1399ret ST_E_IO
1400end
1401if ne st_dev_write get txp 9 psb SV1_OK
1402put st_meta ST_M_POISONED 1
1403ret ST_E_IO
1404end
1405if ne st_dev_flush SV1_OK
1406put st_meta ST_M_POISONED 1
1407ret ST_E_IO
1408end
1409ret SV1_OK
1410end
1411
1412fn st_tx_finish spare ulong m ulong pc view uchar 4096 psb view uchar 4096 cmt_id view uchar 32 to void
1413cmt200 = new uchar 200 0
1414for j ulong 200
1415put cmt200 j get pc j
1416end
1417st_install_root spare psb cmt200 cmt_id m
1418put st_meta ST_M_PEER ulong long ST_PEER_VALID
1419end
1420
1421fn st_tx_commit to int
1422if ne get st_meta ST_M_POISONED 0
1423ret ST_E_NEEDS_REOPEN
1424end
1425if eq st_state ST_DEGRADED_RECOVERY
1426ret ST_E_READ_ONLY_DEGRADED
1427end
1428n = get st_tx_n 0
1429if gt n ST_TX_MAX_OBJS
1430ret ST_E_TRANSACTION_LIMIT
1431end
1432k_arr = new ulong 1 0
1433r = st_tx_step1_dedup n k_arr
1434if ne r SV1_OK
1435ret r
1436end
1437k = get k_arr 0
1438spare = sub 1 get st_meta ST_M_CAT_IDX
1439mn = new ulong 2 0
1440r = st_tx_step2_build_cat k spare mn
1441if ne r SV1_OK
1442ret r
1443end
1444cat_len_arr = new ulong 1 0
1445r = st_cat_encode_to_buf spare get mn 0 cat_len_arr
1446if ne r SV1_OK
1447ret r
1448end
1449u = new uint 1 0
1450r = sv1_unit_count get cat_len_arr 0 u
1451if ne r SV1_OK
1452ret r
1453end
1454txp = new ulong 10 0
1455put txp 0 get st_meta ST_M_REG_UNITS
1456if eq get st_meta ST_M_GEN SV1_U64_MAX
1457ret ST_E_GENERATION_EXHAUSTED
1458end
1459put txp 2 get st_meta ST_M_GEN
1460put txp 1 add get txp 2 1
1461put txp 3 get mn 1
1462put txp 4 get cat_len_arr 0
1463put txp 5 ulong get u 0
1464put txp 6 get mn 0
1465if gt get txp 3 sub SV1_U64_MAX get txp 5
1466ret ST_E_NO_SPACE
1467end
1468put txp 8 add get txp 3 get txp 5
1469if eq get txp 8 SV1_U64_MAX
1470ret ST_E_NO_SPACE
1471end
1472put txp 7 add get txp 8 1
1473if or gt get txp 7 get txp 0 gt get txp 7 SV1_MAX_REGION_UNITS
1474ret ST_E_NO_SPACE
1475end
1476put txp 9 sub 1 get st_meta ST_M_ACTIVE_SLOT
1477cat_id = new uchar 32 0
1478if ne st_hash_catbuf get txp 4 cat_id ST_RE_OK
1479ret ST_E_CORRUPT
1480end
1481pc = new uchar 4096 0
1482cmt_id = new uchar 32 0
1483r = st_tx_build_commit txp cat_id pc cmt_id
1484if ne r SV1_OK
1485ret r
1486end
1487psb = new uchar 4096 0
1488st_tx_build_sb txp cat_id cmt_id psb
1489r = st_tx_write_all k txp pc psb
1490if ne r SV1_OK
1491ret r
1492end
1493st_tx_finish spare get txp 6 pc psb cmt_id
1494ret SV1_OK
1495end
1496
1497fn st_zero_dst dst edit uchar * to void
1498for z ulong len dst bound 65536
1499put dst z 0
1500end
1501end
1502
1503fn st_read_unit_step ctx edit ulong 40 ubuf view uchar 4096 dst edit uchar * start ulong blen ulong to bool
1504take = ulong 0
1505if gt blen start
1506take = sub blen start
1507end
1508if gt take SV1_UNIT
1509take = SV1_UNIT
1510end
1511sha256_update ctx part ubuf 0 take
1512ok = sv1_all_zero ubuf take sub SV1_UNIT take
1513k = ulong 0
1514while lt k take bound 4096
1515put dst add start k get ubuf k
1516k = add k 1
1517end
1518ret ok
1519end
1520
1521fn st_read_loop ctx edit ulong 40 ent view uchar 64 dst edit uchar * to int
1522blen = sv1_get64 ent 44
1523fu = sv1_get64 ent 36
1524ucnt = ulong sv1_get32 ent 52
1525ubuf = new uchar 4096 0
1526bad = false
1527i = ulong 0
1528while lt i ucnt bound 256
1529if ne st_dev_read add fu i ubuf SV1_OK
1530st_zero_dst dst
1531ret ST_E_IO
1532end
1533if not st_read_unit_step ctx ubuf dst mul i SV1_UNIT blen
1534bad = true
1535end
1536i = add i 1
1537end
1538if bad
1539ret ST_E_CORRUPT
1540end
1541ret SV1_OK
1542end
1543
1544fn st_read_object id view uchar 32 dst edit uchar * out_len edit ulong 1 to int
1545put out_len 0 0
1546cidx = get st_meta ST_M_CAT_IDX
1547j = st_cat_find cidx get st_meta ST_M_CAT_N id
1548if lt j 0
1549ret ST_E_INVALID_OBJECT
1550end
1551ent = new uchar 64 0
1552st_cat_get_entry cidx ulong j ent
1553blen = sv1_get64 ent 44
1554if gt blen len dst
1555ret ST_E_ARG
1556end
1557ctx = new ulong 40 0
1558r = sv1_object_id_begin ctx sv1_get16 ent 32 sv1_get16 ent 34 blen
1559if ne r SV1_OK
1560st_zero_dst dst
1561ret ST_E_CORRUPT
1562end
1563lr = st_read_loop ctx ent dst
1564if eq lr ST_E_IO
1565ret ST_E_IO
1566end
1567got = new uchar 32 0
1568sha256_final ctx got
1569if or ne lr SV1_OK not sv1_equal got id
1570st_zero_dst dst
1571ret ST_E_CORRUPT
1572end
1573put out_len 0 blen
1574ret SV1_OK
1575end