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