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 / protocol / synapse / claims / src / 20_time_io.om

329 lines · 6025 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2note Time (UTC civil <-> Unix seconds, days algorithms of H. Hinnant "chrono-Compatible Low-Level Date
3note Algorithms"), the bounded line reader, and the child-process runner for synapse-claims-sig.
4text mpk "argument packet too long"
5text mmemfd "memfd failure"
6
7note ======== time ========
8
9fn t_days y ulong m ulong d ulong to ulong
10yy = y
11if le m 2
12yy = sub y 1
13end
14era = div yy 400
15yoe = sub yy mul era 400
16mm = add m 9
17if gt m 2
18mm = sub m 3
19end
20doy = add div add mul 153 mm 2 5 sub d 1
21doe = add mul yoe 365 div yoe 4
22doe = add sub doe div yoe 100 doy
23ret sub add mul era 146097 doe 719468
24end
25
26note t_civ: days since 1970-01-01 -> y*10000 + m*100 + d
27fn t_civ days ulong to ulong
28z = add days 719468
29era = div z 146097
30doe = sub z mul era 146097
31yoe = sub add sub doe div doe 1460 div doe 36524 div doe 146096
32yoe = div yoe 365
33y = add yoe mul era 400
34doy = sub add mul 365 yoe div yoe 4 div yoe 100
35doy = sub doe doy
36mp = div add mul 5 doy 2 153
37d = add sub doy div add mul 153 mp 2 5 1
38m = add mp 3
39if ge mp 10
40m = sub mp 9
41end
42if le m 2
43y = add y 1
44end
45ret add add mul y 10000 mul m 100 d
46end
47
48fn tb_dig p ulong v ulong w ulong to void
49x = v
50for i ulong w
51put gtb sub sub add p w 1 i uchar add 48 rem x 10
52x = div x 10
53end
54end
55
56note ts_fmt: gtb[0..20) = YYYY-MM-DDTHH:MM:SSZ
57fn ts_fmt s ulong to void
58c = t_civ div s 86400
59r = rem s 86400
60tb_dig 0 div c 10000 4
61put gtb 4 45
62tb_dig 5 rem div c 100 100 2
63put gtb 7 45
64tb_dig 8 rem c 100 2
65put gtb 10 84
66tb_dig 11 div r 3600 2
67put gtb 13 58
68tb_dig 14 rem div r 60 60 2
69put gtb 16 58
70tb_dig 17 rem r 60 2
71put gtb 19 90
72end
73
74fn o_ts s ulong to void
75ts_fmt s
76for i ulong 20
77o_b get gtb i
78end
79end
80
81fn t_num s edit uchar * off ulong n ulong to ulong
82v = ulong 0
83for i ulong n
84c = get s add off i
85if or lt c 48 gt c 57
86ret tbad
87end
88v = add mul v 10 ulong sub c 48
89end
90ret v
91end
92
93note ts_ymd: days since 1970-01-01 of s[off..off+10) YYYY-MM-DD, 1970..9999; else tbad
94fn ts_ymd s edit uchar * off ulong to ulong
95y = t_num s off 4
96m = t_num s add off 5 2
97d = t_num s add off 8 2
98if or or eq y tbad eq m tbad eq d tbad
99ret tbad
100end
101if or or lt y 1970 lt m 1 gt m 12
102ret tbad
103end
104if or lt d 1 gt d 31
105ret tbad
106end
107ret t_days y m d
108end
109
110note ts_hms: seconds of day of s[off..off+8) HH:MM:SS; else tbad
111fn ts_hms s edit uchar * off ulong to ulong
112hh = t_num s off 2
113mi = t_num s add off 3 2
114se = t_num s add off 6 2
115if or or eq hh tbad eq mi tbad eq se tbad
116ret tbad
117end
118if or or ge hh 24 ge mi 60 ge se 60
119ret tbad
120end
121ret add add mul hh 3600 mul mi 60 se
122end
123
124note ts_parse: s[off..off+20) strict YYYY-MM-DDTHH:MM:SSZ, 1970..9999, round-trip exact; else tbad
125fn ts_parse s edit uchar * off ulong to ulong
126dy = ts_ymd s off
127if eq dy tbad
128ret tbad
129end
130sd = ts_hms s add off 11
131if eq sd tbad
132ret tbad
133end
134v = add mul dy 86400 sd
135ts_fmt v
136for i ulong 20
137if ne get gtb i get s add off i
138ret tbad
139end
140end
141ret v
142end
143
144note ======== ledger line reader (grd chunks, fd in xrfd) ========
145
146fn r_open fd long to void
147put gx xrfd ulong fd
148put gx xrpos 0
149put gx xrlen 0
150put gx xreof 0
151end
152
153note r_line: copies the next line (without LF) into ln; returns its length, -1 at clean EOF, -2 line too
154note long, -3 read error, -4 last line not LF-terminated. Each pass consumes one byte or refills with
155note at least one byte, so 2 * 65536 + 4 passes cover the longest accepted line.
156fn r_line ln edit uchar * to long
157n = ulong 0
158while true bound 140000
159if ge get gx xrpos get gx xrlen
160if eq get gx xreof 1
161if eq n 0
162ret -1
163end
164ret -4
165end
166k = read int get gx xrfd grd 0 65536
167if lt k 0
168ret -3
169end
170if eq k 0
171put gx xreof 1
172continue
173end
174put gx xrpos 0
175put gx xrlen ulong k
176continue
177end
178c = get grd get gx xrpos
179put gx xrpos add get gx xrpos 1
180if eq c 10
181ret long n
182end
183if ge n 65535
184ret -2
185end
186put ln n c
187n = add n 1
188end
189ret -2
190end
191
192note ======== child process: packet (gpk), stdin fd, stdout captured into gso ========
193
194fn pk_reset to void
195put gpk 0 0
196put gpk 1 0
197put gx xpkn 2
198end
199
200fn pk_rec n ulong to ulong
201p = get gx xpkn
202if gt add add p n 2 4096
203die 70 mpk
204end
205put gpk p uchar and n 255
206put gpk add p 1 uchar shr n 8
207put gpk 0 add get gpk 0 1
208put gx xpkn add add p n 2
209ret add p 2
210end
211
212fn pk_ar off ulong n ulong to void
213p = pk_rec n
214for i ulong n
215put gpk add p i ar_g add off i
216end
217end
218
219fn pk_t t view uchar * to void
220p = pk_rec len t
221for i ulong len t
222put gpk add p i get t i
223end
224end
225
226fn fdword off ulong to int
227x = ulong 0
228for i ulong 4
229x = or x shl ulong get gfd add off i mul i 8
230end
231ret int x
232end
233
234note run_kid: fork; child gets stdin = infd, stdout = pipe, exec gpk; parent reads the pipe to EOF into
235note gso (xson bytes) and returns the child's exit status, or -1 (spawn failure, signal, overflow).
236fn run_kid infd long to long
237r = pipefds gfd 0 8 524288
238if ne r 0
239ret -1
240end
241rd = fdword 0
242wr = fdword 4
243pid = fork
244if lt pid 0
245ret -1
246end
247if eq pid 0
248a = dupfd int infd 0 0
249b = dupfd wr 1 0
250e = execprog gpk 0 get gx xpkn
251exit 70
252end
253c = fclose wr
254n = ulong 0
255ovf = false
256while true bound 999999
257if ge n socap
258ovf = true
259k = killpid int pid 9
260break
261end
262k = read rd gso n sub socap n
263if le k 0
264break
265end
266n = add n ulong k
267end
268put gx xson n
269c = fclose rd
270r = waitpid int pid gws 0 4 0
271if ne r pid
272ret -1
273end
274if ovf
275ret -1
276end
277if ne get gws 0 0
278ret -1
279end
280ret long get gws 1
281end
282
283note ======== staging buffer gsi -> memfd (xmfd) ========
284
285text mfdname "synapse-claims-sig-in"
286
287fn si_open to void
288m = memfd mfdname 0 len mfdname 1
289if lt m 0
290die 70 mmemfd
291end
292put gx xmfd ulong m
293put gx xsin 0
294end
295
296fn si_flush to void
297n = get gx xsin
298p = ulong 0
299while lt p n bound 999999
300w = write int get gx xmfd gsi p sub n p
301if le w 0
302die 70 mmemfd
303end
304p = add p ulong w
305end
306put gx xsin 0
307end
308
309fn si_b c uchar to void
310if ge get gx xsin sicap
311si_flush
312end
313n = get gx xsin
314put gsi n c
315put gx xsin add n 1
316end
317
318note si_run: flush, rewind and run gpk with the memfd as stdin; closes the memfd
319fn si_run to long
320si_flush
321s = fseek int get gx xmfd 0 0
322if ne s 0
323die 70 mmemfd
324end
325r = run_kid long get gx xmfd
326c = fclose int get gx xmfd
327ret r
328end
329