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 / seed / build / common.om
| 1 | om2 |
| 2 | note SPDX-License-Identifier: Apache-2.0 |
| 3 | note seed/build/common.om: shared pure seeded OM-2 runtime for build, release, and automation coordinators. |
| 4 | note Dynamic run-time pools (global ... *) grow on demand with enlarge: zero artificial file/buffer caps. |
| 5 | note Streaming FIPS 180-4 SHA-256 (64 KiB chunks) hashes files of arbitrary size. |
| 6 | |
| 7 | text cb_hexd "0123456789abcdef" |
| 8 | text cb_nl "\n" |
| 9 | text cb_sp " " |
| 10 | text cb_slash "/" |
| 11 | text cb_dot "." |
| 12 | text cb_dotdot ".." |
| 13 | text cb_devnull "/dev/null" |
| 14 | text cb_sha_kiv "1116352408 1899447441 3049323471 3921009573 961987163 1508970993 2453635748 2870763221 3624381080 310598401 607225278 1426881987 1925078388 2162078206 2614888103 3248222580 3835390401 4022224774 264347078 604807628 770255983 1249150122 1555081692 1996064986 2554220882 2821834349 2952996808 3210313671 3336571891 3584528711 113926993 338241895 666307205 773529912 1294757372 1396182291 1695183700 1986661051 2177026350 2456956037 2730485921 2820302411 3259730800 3345764771 3516065817 3600352804 4094571909 275423344 430227734 506948616 659060556 883997877 958139571 1322822218 1537002063 1747873779 1955562222 2024104815 2227730452 2361852424 2428436474 2756734187 3204031479 3329325298 1779033703 3144134277 1013904242 2773480762 1359893119 2600822924 528734635 1541459225" |
| 15 | |
| 16 | note Dynamic byte pools (grow via enlarge, no fixed cap) |
| 17 | global cb_rb uchar * |
| 18 | global cb_wb uchar * |
| 19 | global cb_sb uchar * |
| 20 | global cb_n ulong 32 |
| 21 | note cb_n slots: |
| 22 | note 0 = cb_rb len, 1 = cb_wb len, 2 = cb_sb len |
| 23 | note 3 = cb_pa len, 4 = cb_pb len, 5 = cb_av bytes, 6 = cb_av argc |
| 24 | note 7 = cb_ev bytes, 8 = cb_ev envc, 9 = cb_sha initialized |
| 25 | note 10 = cb_sha total_bytes, 11 = cb_sha buf_len |
| 26 | note 12 = cb_tmp_len |
| 27 | |
| 28 | note Path & small scratch buffers |
| 29 | global cb_pa uchar 4096 |
| 30 | global cb_pb uchar 4096 |
| 31 | global cb_pc uchar 4096 |
| 32 | global cb_av uchar 65536 |
| 33 | global cb_ev uchar 4096 |
| 34 | global cb_pkt uchar 4096 |
| 35 | global cb_st uchar 4 |
| 36 | global cb_numbuf uchar 32 |
| 37 | global cb_chunk1 uchar 65536 |
| 38 | global cb_chunk2 uchar 65536 |
| 39 | global cb_dent uchar 32768 |
| 40 | global cb_envtmp uchar 4096 |
| 41 | |
| 42 | note Streaming SHA-256 state |
| 43 | global cb_sk uint 72 |
| 44 | global cb_sw uint 64 |
| 45 | global cb_sv uint 8 |
| 46 | global cb_sh uint 8 |
| 47 | global cb_sbuf uchar 128 |
| 48 | global cb_sdig uchar 32 |
| 49 | global cb_shex uchar 64 |
| 50 | |
| 51 | note ---- dynamic pool growth ---- |
| 52 | fn cb_rb_need need ulong to bool |
| 53 | if le need len cb_rb |
| 54 | ret true |
| 55 | end |
| 56 | cap = len cb_rb |
| 57 | if eq cap 0 |
| 58 | cap = 65536 |
| 59 | end |
| 60 | while lt cap need bound 64 |
| 61 | cap = mul cap 2 |
| 62 | end |
| 63 | z = enlarge cb_rb cap |
| 64 | if ne z 0 |
| 65 | ret false |
| 66 | end |
| 67 | ret true |
| 68 | end |
| 69 | |
| 70 | fn cb_wb_need need ulong to bool |
| 71 | if le need len cb_wb |
| 72 | ret true |
| 73 | end |
| 74 | cap = len cb_wb |
| 75 | if eq cap 0 |
| 76 | cap = 65536 |
| 77 | end |
| 78 | while lt cap need bound 64 |
| 79 | cap = mul cap 2 |
| 80 | end |
| 81 | z = enlarge cb_wb cap |
| 82 | if ne z 0 |
| 83 | ret false |
| 84 | end |
| 85 | ret true |
| 86 | end |
| 87 | |
| 88 | fn cb_sb_need need ulong to bool |
| 89 | if le need len cb_sb |
| 90 | ret true |
| 91 | end |
| 92 | cap = len cb_sb |
| 93 | if eq cap 0 |
| 94 | cap = 65536 |
| 95 | end |
| 96 | while lt cap need bound 64 |
| 97 | cap = mul cap 2 |
| 98 | end |
| 99 | z = enlarge cb_sb cap |
| 100 | if ne z 0 |
| 101 | ret false |
| 102 | end |
| 103 | ret true |
| 104 | end |
| 105 | |
| 106 | fn cb_wb_clr to void |
| 107 | put cb_n 1 0 |
| 108 | end |
| 109 | |
| 110 | fn cb_wb_ch c uchar to void |
| 111 | n = get cb_n 1 |
| 112 | if not cb_wb_need add n 1 |
| 113 | exit 70 |
| 114 | end |
| 115 | put cb_wb n c |
| 116 | put cb_n 1 add n 1 |
| 117 | end |
| 118 | |
| 119 | fn cb_wb_tx t view uchar * to void |
| 120 | m = len t |
| 121 | n = get cb_n 1 |
| 122 | if not cb_wb_need add n m |
| 123 | exit 70 |
| 124 | end |
| 125 | for i ulong m bound 9223372036854775807 |
| 126 | put cb_wb add n i get t i |
| 127 | end |
| 128 | put cb_n 1 add n m |
| 129 | end |
| 130 | |
| 131 | fn cb_wb_rb off ulong cnt ulong to void |
| 132 | n = get cb_n 1 |
| 133 | if not cb_wb_need add n cnt |
| 134 | exit 70 |
| 135 | end |
| 136 | for i ulong cnt bound 9223372036854775807 |
| 137 | put cb_wb add n i get cb_rb add off i |
| 138 | end |
| 139 | put cb_n 1 add n cnt |
| 140 | end |
| 141 | |
| 142 | fn cb_wb_num v ulong to void |
| 143 | x = v |
| 144 | i = ulong 32 |
| 145 | for j ulong 21 |
| 146 | i = sub i 1 |
| 147 | put cb_numbuf i uchar add 48 rem x 10 |
| 148 | x = div x 10 |
| 149 | if eq x 0 |
| 150 | break |
| 151 | end |
| 152 | end |
| 153 | for q ulong sub 32 i bound 32 |
| 154 | cb_wb_ch get cb_numbuf add i q |
| 155 | end |
| 156 | end |
| 157 | |
| 158 | note ---- fd write helpers ---- |
| 159 | fn cb_wtx fd int t view uchar * to void |
| 160 | n = len t |
| 161 | o = ulong 0 |
| 162 | while lt o n bound 9223372036854775807 |
| 163 | k = write fd t o sub n o |
| 164 | if le k 0 |
| 165 | ret |
| 166 | end |
| 167 | o = add o ulong k |
| 168 | end |
| 169 | end |
| 170 | |
| 171 | fn cb_out t view uchar * to void |
| 172 | cb_wtx 1 t |
| 173 | end |
| 174 | |
| 175 | fn cb_err t view uchar * to void |
| 176 | cb_wtx 2 t |
| 177 | end |
| 178 | |
| 179 | fn cb_wnum fd int v ulong to void |
| 180 | x = v |
| 181 | i = ulong 32 |
| 182 | for j ulong 21 |
| 183 | i = sub i 1 |
| 184 | put cb_numbuf i uchar add 48 rem x 10 |
| 185 | x = div x 10 |
| 186 | if eq x 0 |
| 187 | break |
| 188 | end |
| 189 | end |
| 190 | r = write fd cb_numbuf i sub 32 i |
| 191 | end |
| 192 | |
| 193 | fn cb_wbuf_pa fd int to void |
| 194 | n = get cb_n 3 |
| 195 | if gt n 0 |
| 196 | r = write fd cb_pa 0 n |
| 197 | end |
| 198 | end |
| 199 | |
| 200 | fn cb_wbuf_pb fd int to void |
| 201 | n = get cb_n 4 |
| 202 | if gt n 0 |
| 203 | r = write fd cb_pb 0 n |
| 204 | end |
| 205 | end |
| 206 | |
| 207 | fn cb_wbuf_hex fd int to void |
| 208 | r = write fd cb_shex 0 64 |
| 209 | end |
| 210 | |
| 211 | note ---- path buffer cb_pa[0..cb_n[3]) and cb_pb[0..cb_n[4]) ---- |
| 212 | fn cb_pa_clr to void |
| 213 | put cb_n 3 0 |
| 214 | end |
| 215 | |
| 216 | fn cb_pa_ch c uchar to void |
| 217 | n = get cb_n 3 |
| 218 | if ge n 4095 |
| 219 | exit 70 |
| 220 | end |
| 221 | put cb_pa n c |
| 222 | put cb_n 3 add n 1 |
| 223 | end |
| 224 | |
| 225 | fn cb_pa_tx t view uchar * to void |
| 226 | m = len t |
| 227 | n = get cb_n 3 |
| 228 | if gt add n m 4095 |
| 229 | exit 70 |
| 230 | end |
| 231 | for i ulong m bound 4096 |
| 232 | put cb_pa add n i get t i |
| 233 | end |
| 234 | put cb_n 3 add n m |
| 235 | end |
| 236 | |
| 237 | fn cb_pa_set t view uchar * to void |
| 238 | cb_pa_clr |
| 239 | cb_pa_tx t |
| 240 | end |
| 241 | |
| 242 | fn cb_pa_pb to void |
| 243 | n = get cb_n 4 |
| 244 | for i ulong n bound 4096 |
| 245 | put cb_pa i get cb_pb i |
| 246 | end |
| 247 | put cb_n 3 n |
| 248 | end |
| 249 | |
| 250 | fn cb_pa_add_pb to void |
| 251 | m = get cb_n 4 |
| 252 | n = get cb_n 3 |
| 253 | if gt add n m 4095 |
| 254 | exit 70 |
| 255 | end |
| 256 | for i ulong m bound 4096 |
| 257 | put cb_pa add n i get cb_pb i |
| 258 | end |
| 259 | put cb_n 3 add n m |
| 260 | end |
| 261 | |
| 262 | fn cb_pb_clr to void |
| 263 | put cb_n 4 0 |
| 264 | end |
| 265 | |
| 266 | fn cb_pb_ch c uchar to void |
| 267 | n = get cb_n 4 |
| 268 | if ge n 4095 |
| 269 | exit 70 |
| 270 | end |
| 271 | put cb_pb n c |
| 272 | put cb_n 4 add n 1 |
| 273 | end |
| 274 | |
| 275 | fn cb_pb_tx t view uchar * to void |
| 276 | m = len t |
| 277 | n = get cb_n 4 |
| 278 | if gt add n m 4095 |
| 279 | exit 70 |
| 280 | end |
| 281 | for i ulong m bound 4096 |
| 282 | put cb_pb add n i get t i |
| 283 | end |
| 284 | put cb_n 4 add n m |
| 285 | end |
| 286 | |
| 287 | fn cb_pb_set t view uchar * to void |
| 288 | cb_pb_clr |
| 289 | cb_pb_tx t |
| 290 | end |
| 291 | |
| 292 | fn cb_pb_from_pa to void |
| 293 | n = get cb_n 3 |
| 294 | for i ulong n bound 4096 |
| 295 | put cb_pb i get cb_pa i |
| 296 | end |
| 297 | put cb_n 4 n |
| 298 | end |
| 299 | |
| 300 | fn cb_pa_arg idx ulong to bool |
| 301 | k = argread idx cb_pa 0 4095 |
| 302 | if lt k 0 |
| 303 | ret false |
| 304 | end |
| 305 | put cb_n 3 ulong k |
| 306 | ret true |
| 307 | end |
| 308 | |
| 309 | fn cb_pb_arg idx ulong to bool |
| 310 | k = argread idx cb_pb 0 4095 |
| 311 | if lt k 0 |
| 312 | ret false |
| 313 | end |
| 314 | put cb_n 4 ulong k |
| 315 | ret true |
| 316 | end |
| 317 | |
| 318 | fn cb_pa_eq t view uchar * to bool |
| 319 | n = get cb_n 3 |
| 320 | if ne n len t |
| 321 | ret false |
| 322 | end |
| 323 | for i ulong n bound 4096 |
| 324 | if ne get cb_pa i get t i |
| 325 | ret false |
| 326 | end |
| 327 | end |
| 328 | ret true |
| 329 | end |
| 330 | |
| 331 | fn cb_pa_ends t view uchar * to bool |
| 332 | n = get cb_n 3 |
| 333 | m = len t |
| 334 | if lt n m |
| 335 | ret false |
| 336 | end |
| 337 | off = sub n m |
| 338 | for i ulong m bound 4096 |
| 339 | if ne get cb_pa add off i get t i |
| 340 | ret false |
| 341 | end |
| 342 | end |
| 343 | ret true |
| 344 | end |
| 345 | |
| 346 | note ---- environment variable lookup into cb_pb (returns true if set and non-empty) ---- |
| 347 | fn cb_env_match name view uchar * to bool |
| 348 | ml = len name |
| 349 | for j ulong ml bound 4096 |
| 350 | if ne get cb_envtmp j get name j |
| 351 | ret false |
| 352 | end |
| 353 | end |
| 354 | ret true |
| 355 | end |
| 356 | |
| 357 | fn cb_env_copy_pb start ulong vlen ulong to void |
| 358 | for j ulong vlen bound 4096 |
| 359 | put cb_pb j get cb_envtmp add start j |
| 360 | end |
| 361 | put cb_n 4 vlen |
| 362 | end |
| 363 | |
| 364 | fn cb_getenv name view uchar * to bool |
| 365 | ec = envcnt |
| 366 | ml = len name |
| 367 | for idx ulong ulong ec bound 65536 |
| 368 | k = envread idx cb_envtmp 0 4095 |
| 369 | if lt k 0 |
| 370 | continue |
| 371 | end |
| 372 | kl = ulong k |
| 373 | if lt kl add ml 1 |
| 374 | continue |
| 375 | end |
| 376 | if ne get cb_envtmp ml 61 |
| 377 | continue |
| 378 | end |
| 379 | if cb_env_match name |
| 380 | cb_env_copy_pb add ml 1 sub kl add ml 1 |
| 381 | ret true |
| 382 | end |
| 383 | end |
| 384 | put cb_n 4 0 |
| 385 | ret false |
| 386 | end |
| 387 | |
| 388 | note ---- streaming FIPS 180-4 SHA-256 ---- |
| 389 | fn cb_sha_init_k to void |
| 390 | if ne get cb_n 9 0 |
| 391 | ret |
| 392 | end |
| 393 | put cb_n 9 1 |
| 394 | p = ulong 0 |
| 395 | for i ulong 72 |
| 396 | v = ulong 0 |
| 397 | while lt get cb_sha_kiv p 48 bound 4 |
| 398 | p = add p 1 |
| 399 | end |
| 400 | while ge get cb_sha_kiv p 48 bound 11 |
| 401 | v = add mul v 10 ulong sub get cb_sha_kiv p 48 |
| 402 | p = add p 1 |
| 403 | if ge p len cb_sha_kiv |
| 404 | put cb_sk i uint v |
| 405 | ret |
| 406 | end |
| 407 | end |
| 408 | put cb_sk i uint v |
| 409 | end |
| 410 | end |
| 411 | |
| 412 | fn cb_sha_reset to void |
| 413 | cb_sha_init_k |
| 414 | for j ulong 8 |
| 415 | put cb_sh j get cb_sk add j 64 |
| 416 | end |
| 417 | put cb_n 10 0 |
| 418 | put cb_n 11 0 |
| 419 | end |
| 420 | |
| 421 | fn cb_sha_s0 a uint to uint |
| 422 | ret xor xor rotr a 2 rotr a 13 rotr a 22 |
| 423 | end |
| 424 | |
| 425 | fn cb_sha_s1 e uint to uint |
| 426 | ret xor xor rotr e 6 rotr e 11 rotr e 25 |
| 427 | end |
| 428 | |
| 429 | fn cb_sha_g0 x uint to uint |
| 430 | ret xor xor rotr x 7 rotr x 18 shr x 3 |
| 431 | end |
| 432 | |
| 433 | fn cb_sha_g1 x uint to uint |
| 434 | ret xor xor rotr x 17 rotr x 19 shr x 10 |
| 435 | end |
| 436 | |
| 437 | fn cb_sha_maj a uint b uint c uint to uint |
| 438 | ret xor xor and a b and a c and b c |
| 439 | end |
| 440 | |
| 441 | fn cb_sha_rnd i ulong to void |
| 442 | e = get cb_sv 4 |
| 443 | t1 = add get cb_sv 7 cb_sha_s1 e |
| 444 | t1 = add t1 xor and e get cb_sv 5 and not e get cb_sv 6 |
| 445 | t1 = add t1 add get cb_sk i get cb_sw i |
| 446 | a = get cb_sv 0 |
| 447 | t2 = add cb_sha_s0 a cb_sha_maj a get cb_sv 1 get cb_sv 2 |
| 448 | put cb_sv 7 get cb_sv 6 |
| 449 | put cb_sv 6 get cb_sv 5 |
| 450 | put cb_sv 5 e |
| 451 | put cb_sv 4 add get cb_sv 3 t1 |
| 452 | put cb_sv 3 get cb_sv 2 |
| 453 | put cb_sv 2 get cb_sv 1 |
| 454 | put cb_sv 1 a |
| 455 | put cb_sv 0 add t1 t2 |
| 456 | end |
| 457 | |
| 458 | fn cb_sha_blk_sbuf off ulong to void |
| 459 | for t ulong 16 |
| 460 | p = add off mul t 4 |
| 461 | v = uint get cb_sbuf p |
| 462 | v = or shl v 8 uint get cb_sbuf add p 1 |
| 463 | v = or shl v 8 uint get cb_sbuf add p 2 |
| 464 | v = or shl v 8 uint get cb_sbuf add p 3 |
| 465 | put cb_sw t v |
| 466 | end |
| 467 | for t ulong 48 |
| 468 | u = add t 16 |
| 469 | x = add cb_sha_g1 get cb_sw sub u 2 get cb_sw sub u 7 |
| 470 | put cb_sw u add x add cb_sha_g0 get cb_sw sub u 15 get cb_sw sub u 16 |
| 471 | end |
| 472 | for j ulong 8 |
| 473 | put cb_sv j get cb_sh j |
| 474 | end |
| 475 | for i ulong 64 |
| 476 | cb_sha_rnd i |
| 477 | end |
| 478 | for j ulong 8 |
| 479 | put cb_sh j add get cb_sh j get cb_sv j |
| 480 | end |
| 481 | end |
| 482 | |
| 483 | fn cb_sha_copy_chunk bl ulong src_off ulong take ulong to void |
| 484 | for j ulong take bound 64 |
| 485 | put cb_sbuf add bl j get cb_chunk1 add src_off j |
| 486 | end |
| 487 | end |
| 488 | |
| 489 | fn cb_sha_copy_rb bl ulong src_off ulong take ulong to void |
| 490 | for j ulong take bound 64 |
| 491 | put cb_sbuf add bl j get cb_rb add src_off j |
| 492 | end |
| 493 | end |
| 494 | |
| 495 | fn cb_sha_push_chunk off ulong cnt ulong to void |
| 496 | i = ulong 0 |
| 497 | while lt i cnt bound 9223372036854775807 |
| 498 | bl = get cb_n 11 |
| 499 | take = sub 64 bl |
| 500 | if lt sub cnt i take |
| 501 | take = sub cnt i |
| 502 | end |
| 503 | cb_sha_copy_chunk bl add off i take |
| 504 | bl = add bl take |
| 505 | i = add i take |
| 506 | put cb_n 10 add get cb_n 10 take |
| 507 | if eq bl 64 |
| 508 | cb_sha_blk_sbuf 0 |
| 509 | put cb_n 11 0 |
| 510 | else |
| 511 | put cb_n 11 bl |
| 512 | end |
| 513 | end |
| 514 | end |
| 515 | |
| 516 | fn cb_sha_push_rb off ulong cnt ulong to void |
| 517 | i = ulong 0 |
| 518 | while lt i cnt bound 9223372036854775807 |
| 519 | bl = get cb_n 11 |
| 520 | take = sub 64 bl |
| 521 | if lt sub cnt i take |
| 522 | take = sub cnt i |
| 523 | end |
| 524 | cb_sha_copy_rb bl add off i take |
| 525 | bl = add bl take |
| 526 | i = add i take |
| 527 | put cb_n 10 add get cb_n 10 take |
| 528 | if eq bl 64 |
| 529 | cb_sha_blk_sbuf 0 |
| 530 | put cb_n 11 0 |
| 531 | else |
| 532 | put cb_n 11 bl |
| 533 | end |
| 534 | end |
| 535 | end |
| 536 | |
| 537 | fn cb_sha_finish to void |
| 538 | r = get cb_n 11 |
| 539 | tot = get cb_n 10 |
| 540 | for j ulong sub 128 r bound 128 |
| 541 | put cb_sbuf add r j 0 |
| 542 | end |
| 543 | put cb_sbuf r 128 |
| 544 | e = ulong 64 |
| 545 | if ge r 56 |
| 546 | e = 128 |
| 547 | end |
| 548 | bits = mul tot 8 |
| 549 | for j ulong 8 |
| 550 | put cb_sbuf sub sub e 1 j uchar and shr bits mul j 8 255 |
| 551 | end |
| 552 | cb_sha_blk_sbuf 0 |
| 553 | if eq e 128 |
| 554 | cb_sha_blk_sbuf 64 |
| 555 | end |
| 556 | for j ulong 8 |
| 557 | v = get cb_sh j |
| 558 | for q ulong 4 |
| 559 | s = uint mul sub 3 q 8 |
| 560 | put cb_sdig add mul j 4 q uchar and shr v s 255 |
| 561 | end |
| 562 | end |
| 563 | for j ulong 32 |
| 564 | b = get cb_sdig j |
| 565 | put cb_shex mul j 2 get cb_hexd ulong shr b 4 |
| 566 | put cb_shex add mul j 2 1 get cb_hexd ulong and b 15 |
| 567 | end |
| 568 | end |
| 569 | |
| 570 | fn cb_sha_file_pa to bool |
| 571 | fd = fopen cb_pa 0 get cb_n 3 524288 0 |
| 572 | if lt fd 0 |
| 573 | ret false |
| 574 | end |
| 575 | cb_sha_reset |
| 576 | ok = true |
| 577 | while true bound 9223372036854775807 |
| 578 | k = read int fd cb_chunk1 0 65536 |
| 579 | if lt k 0 |
| 580 | ok = false |
| 581 | break |
| 582 | end |
| 583 | if eq k 0 |
| 584 | break |
| 585 | end |
| 586 | cb_sha_push_chunk 0 ulong k |
| 587 | end |
| 588 | z = fclose int fd |
| 589 | if not ok |
| 590 | ret false |
| 591 | end |
| 592 | cb_sha_finish |
| 593 | ret true |
| 594 | end |
| 595 | |
| 596 | fn cb_sha_rb off ulong cnt ulong to void |
| 597 | cb_sha_reset |
| 598 | cb_sha_push_rb off cnt |
| 599 | cb_sha_finish |
| 600 | end |
| 601 | |
| 602 | fn cb_is_hex c uchar to bool |
| 603 | if and ge c 48 le c 57 |
| 604 | ret true |
| 605 | end |
| 606 | ret and ge c 97 le c 102 |
| 607 | end |
| 608 | |
| 609 | fn cb_hex_val c uchar to ulong |
| 610 | if and ge c 48 le c 57 |
| 611 | ret ulong sub c 48 |
| 612 | end |
| 613 | if and ge c 97 le c 102 |
| 614 | ret add 10 ulong sub c 97 |
| 615 | end |
| 616 | if and ge c 65 le c 70 |
| 617 | ret add 10 ulong sub c 65 |
| 618 | end |
| 619 | ret 255 |
| 620 | end |
| 621 | |
| 622 | note ---- file I/O (zero caps via dynamic pools or 64 KiB streaming) ---- |
| 623 | fn cb_read_pa to bool |
| 624 | fd = fopen cb_pa 0 get cb_n 3 524288 0 |
| 625 | if lt fd 0 |
| 626 | put cb_n 0 0 |
| 627 | ret false |
| 628 | end |
| 629 | n = ulong 0 |
| 630 | ok = true |
| 631 | while true bound 9223372036854775807 |
| 632 | if not cb_rb_need add n 65536 |
| 633 | ok = false |
| 634 | break |
| 635 | end |
| 636 | k = read int fd cb_rb n 65536 |
| 637 | if lt k 0 |
| 638 | ok = false |
| 639 | break |
| 640 | end |
| 641 | if eq k 0 |
| 642 | break |
| 643 | end |
| 644 | n = add n ulong k |
| 645 | end |
| 646 | z = fclose int fd |
| 647 | put cb_n 0 n |
| 648 | ret ok |
| 649 | end |
| 650 | |
| 651 | fn cb_append_pa_to_wb to bool |
| 652 | fd = fopen cb_pa 0 get cb_n 3 524288 0 |
| 653 | if lt fd 0 |
| 654 | ret false |
| 655 | end |
| 656 | n = get cb_n 1 |
| 657 | ok = true |
| 658 | while true bound 9223372036854775807 |
| 659 | if not cb_wb_need add n 65536 |
| 660 | ok = false |
| 661 | break |
| 662 | end |
| 663 | k = read int fd cb_wb n 65536 |
| 664 | if lt k 0 |
| 665 | ok = false |
| 666 | break |
| 667 | end |
| 668 | if eq k 0 |
| 669 | break |
| 670 | end |
| 671 | n = add n ulong k |
| 672 | end |
| 673 | z = fclose int fd |
| 674 | put cb_n 1 n |
| 675 | ret ok |
| 676 | end |
| 677 | |
| 678 | fn cb_write_wb_pa mode int to bool |
| 679 | r = unlink cb_pa 0 get cb_n 3 |
| 680 | fd = fopen cb_pa 0 get cb_n 3 524865 mode |
| 681 | if lt fd 0 |
| 682 | ret false |
| 683 | end |
| 684 | n = get cb_n 1 |
| 685 | o = ulong 0 |
| 686 | ok = true |
| 687 | while lt o n bound 9223372036854775807 |
| 688 | k = write int fd cb_wb o sub n o |
| 689 | if le k 0 |
| 690 | ok = false |
| 691 | break |
| 692 | end |
| 693 | o = add o ulong k |
| 694 | end |
| 695 | z = fclose int fd |
| 696 | ret ok |
| 697 | end |
| 698 | |
| 699 | fn cb_write_rb_pa mode int to bool |
| 700 | r = unlink cb_pa 0 get cb_n 3 |
| 701 | fd = fopen cb_pa 0 get cb_n 3 524865 mode |
| 702 | if lt fd 0 |
| 703 | ret false |
| 704 | end |
| 705 | n = get cb_n 0 |
| 706 | o = ulong 0 |
| 707 | ok = true |
| 708 | while lt o n bound 9223372036854775807 |
| 709 | k = write int fd cb_rb o sub n o |
| 710 | if le k 0 |
| 711 | ok = false |
| 712 | break |
| 713 | end |
| 714 | o = add o ulong k |
| 715 | end |
| 716 | z = fclose int fd |
| 717 | ret ok |
| 718 | end |
| 719 | |
| 720 | fn cb_copy_pa_to_pb mode int to bool |
| 721 | fdi = fopen cb_pa 0 get cb_n 3 524288 0 |
| 722 | if lt fdi 0 |
| 723 | ret false |
| 724 | end |
| 725 | z0 = unlink cb_pb 0 get cb_n 4 |
| 726 | fdo = fopen cb_pb 0 get cb_n 4 524865 mode |
| 727 | if lt fdo 0 |
| 728 | z1 = fclose int fdi |
| 729 | ret false |
| 730 | end |
| 731 | ok = true |
| 732 | while true bound 9223372036854775807 |
| 733 | k = read int fdi cb_chunk1 0 65536 |
| 734 | if lt k 0 |
| 735 | ok = false |
| 736 | break |
| 737 | end |
| 738 | if eq k 0 |
| 739 | break |
| 740 | end |
| 741 | o = ulong 0 |
| 742 | kl = ulong k |
| 743 | while lt o kl bound 65537 |
| 744 | w = write int fdo cb_chunk1 o sub kl o |
| 745 | if le w 0 |
| 746 | ok = false |
| 747 | break |
| 748 | end |
| 749 | o = add o ulong w |
| 750 | end |
| 751 | if not ok |
| 752 | break |
| 753 | end |
| 754 | end |
| 755 | z2 = fclose int fdi |
| 756 | z3 = fclose int fdo |
| 757 | ret ok |
| 758 | end |
| 759 | |
| 760 | fn cb_chunks_eq n ulong to bool |
| 761 | for j ulong n bound 65536 |
| 762 | if ne get cb_chunk1 j get cb_chunk2 j |
| 763 | ret false |
| 764 | end |
| 765 | end |
| 766 | ret true |
| 767 | end |
| 768 | |
| 769 | fn cb_read_exact_c1 fd int have ulong want ulong to bool |
| 770 | u = have |
| 771 | while lt u want bound 65537 |
| 772 | r = read fd cb_chunk1 u sub want u |
| 773 | if le r 0 |
| 774 | ret false |
| 775 | end |
| 776 | u = add u ulong r |
| 777 | end |
| 778 | ret true |
| 779 | end |
| 780 | |
| 781 | fn cb_read_exact_c2 fd int have ulong want ulong to bool |
| 782 | u = have |
| 783 | while lt u want bound 65537 |
| 784 | r = read fd cb_chunk2 u sub want u |
| 785 | if le r 0 |
| 786 | ret false |
| 787 | end |
| 788 | u = add u ulong r |
| 789 | end |
| 790 | ret true |
| 791 | end |
| 792 | |
| 793 | fn cb_cmp_pa_pb to bool |
| 794 | fd1 = fopen cb_pa 0 get cb_n 3 524288 0 |
| 795 | if lt fd1 0 |
| 796 | ret false |
| 797 | end |
| 798 | fd2 = fopen cb_pb 0 get cb_n 4 524288 0 |
| 799 | if lt fd2 0 |
| 800 | z0 = fclose int fd1 |
| 801 | ret false |
| 802 | end |
| 803 | same = true |
| 804 | while true bound 9223372036854775807 |
| 805 | k1 = read int fd1 cb_chunk1 0 65536 |
| 806 | k2 = read int fd2 cb_chunk2 0 65536 |
| 807 | if or lt k1 0 lt k2 0 |
| 808 | same = false |
| 809 | break |
| 810 | end |
| 811 | if and eq k1 0 eq k2 0 |
| 812 | break |
| 813 | end |
| 814 | if or eq k1 0 eq k2 0 |
| 815 | same = false |
| 816 | break |
| 817 | end |
| 818 | u1 = ulong k1 |
| 819 | u2 = ulong k2 |
| 820 | if lt u1 u2 |
| 821 | if not cb_read_exact_c1 int fd1 u1 u2 |
| 822 | same = false |
| 823 | break |
| 824 | end |
| 825 | u1 = u2 |
| 826 | end |
| 827 | if lt u2 u1 |
| 828 | if not cb_read_exact_c2 int fd2 u2 u1 |
| 829 | same = false |
| 830 | break |
| 831 | end |
| 832 | end |
| 833 | if not cb_chunks_eq u1 |
| 834 | same = false |
| 835 | break |
| 836 | end |
| 837 | end |
| 838 | z1 = fclose int fd1 |
| 839 | z2 = fclose int fd2 |
| 840 | ret same |
| 841 | end |
| 842 | |
| 843 | fn cb_file_size_pa to long |
| 844 | fd = fopen cb_pa 0 get cb_n 3 524288 0 |
| 845 | if lt fd 0 |
| 846 | ret -1 |
| 847 | end |
| 848 | tot = long 0 |
| 849 | while true bound 9223372036854775807 |
| 850 | k = read int fd cb_chunk1 0 65536 |
| 851 | if lt k 0 |
| 852 | z0 = fclose int fd |
| 853 | ret -1 |
| 854 | end |
| 855 | if eq k 0 |
| 856 | break |
| 857 | end |
| 858 | tot = add tot k |
| 859 | end |
| 860 | z1 = fclose int fd |
| 861 | ret tot |
| 862 | end |
| 863 | |
| 864 | fn cb_mkdir_p_pa to bool |
| 865 | n = get cb_n 3 |
| 866 | if eq n 0 |
| 867 | ret false |
| 868 | end |
| 869 | for i ulong n bound 4096 |
| 870 | if and gt i 0 eq get cb_pa i 47 |
| 871 | r = makedir cb_pa 0 i 493 |
| 872 | if and lt r 0 ne r -17 |
| 873 | ret false |
| 874 | end |
| 875 | end |
| 876 | end |
| 877 | r = makedir cb_pa 0 n 493 |
| 878 | if and lt r 0 ne r -17 |
| 879 | ret false |
| 880 | end |
| 881 | ret true |
| 882 | end |
| 883 | |
| 884 | note atomic rename cb_pa -> cb_pb |
| 885 | fn cb_rename_pa_pb to bool |
| 886 | n1 = get cb_n 3 |
| 887 | n2 = get cb_n 4 |
| 888 | if or eq n1 0 eq n2 0 |
| 889 | ret false |
| 890 | end |
| 891 | if gt add add n1 n2 6 4096 |
| 892 | ret false |
| 893 | end |
| 894 | put cb_pkt 0 2 |
| 895 | put cb_pkt 1 0 |
| 896 | put cb_pkt 2 uchar and n1 255 |
| 897 | put cb_pkt 3 uchar shr n1 8 |
| 898 | for i ulong n1 bound 4096 |
| 899 | put cb_pkt add 4 i get cb_pa i |
| 900 | end |
| 901 | p = add 4 n1 |
| 902 | put cb_pkt p uchar and n2 255 |
| 903 | put cb_pkt add p 1 uchar shr n2 8 |
| 904 | for i ulong n2 bound 4096 |
| 905 | put cb_pkt add add p 2 i get cb_pb i |
| 906 | end |
| 907 | tot = add add p 2 n2 |
| 908 | r = renames cb_pkt 0 tot 0 |
| 909 | ret eq r 0 |
| 910 | end |
| 911 | |
| 912 | note ---- subprocess spawner (packed argv + optional envp) ---- |
| 913 | fn cb_av_clr to void |
| 914 | put cb_n 5 2 |
| 915 | put cb_n 6 0 |
| 916 | put cb_av 0 0 |
| 917 | put cb_av 1 0 |
| 918 | end |
| 919 | |
| 920 | fn cb_av_push_pa to void |
| 921 | p = get cb_n 5 |
| 922 | n = get cb_n 3 |
| 923 | if gt add add p 2 n 65536 |
| 924 | exit 70 |
| 925 | end |
| 926 | put cb_av p uchar and n 255 |
| 927 | put cb_av add p 1 uchar shr n 8 |
| 928 | for i ulong n bound 4096 |
| 929 | put cb_av add add p 2 i get cb_pa i |
| 930 | end |
| 931 | put cb_n 5 add add p 2 n |
| 932 | c = add get cb_n 6 1 |
| 933 | put cb_n 6 c |
| 934 | put cb_av 0 uchar and c 255 |
| 935 | put cb_av 1 uchar shr c 8 |
| 936 | end |
| 937 | |
| 938 | fn cb_av_push_pb to void |
| 939 | p = get cb_n 5 |
| 940 | n = get cb_n 4 |
| 941 | if gt add add p 2 n 65536 |
| 942 | exit 70 |
| 943 | end |
| 944 | put cb_av p uchar and n 255 |
| 945 | put cb_av add p 1 uchar shr n 8 |
| 946 | for i ulong n bound 4096 |
| 947 | put cb_av add add p 2 i get cb_pb i |
| 948 | end |
| 949 | put cb_n 5 add add p 2 n |
| 950 | c = add get cb_n 6 1 |
| 951 | put cb_n 6 c |
| 952 | put cb_av 0 uchar and c 255 |
| 953 | put cb_av 1 uchar shr c 8 |
| 954 | end |
| 955 | |
| 956 | fn cb_av_push_tx t view uchar * to void |
| 957 | p = get cb_n 5 |
| 958 | n = len t |
| 959 | if gt add add p 2 n 65536 |
| 960 | exit 70 |
| 961 | end |
| 962 | put cb_av p uchar and n 255 |
| 963 | put cb_av add p 1 uchar shr n 8 |
| 964 | for i ulong n bound 4096 |
| 965 | put cb_av add add p 2 i get t i |
| 966 | end |
| 967 | put cb_n 5 add add p 2 n |
| 968 | c = add get cb_n 6 1 |
| 969 | put cb_n 6 c |
| 970 | put cb_av 0 uchar and c 255 |
| 971 | put cb_av 1 uchar shr c 8 |
| 972 | end |
| 973 | |
| 974 | fn cb_ev_clr to void |
| 975 | put cb_n 7 2 |
| 976 | put cb_n 8 0 |
| 977 | put cb_ev 0 0 |
| 978 | put cb_ev 1 0 |
| 979 | end |
| 980 | |
| 981 | fn cb_ev_push_env_pa to void |
| 982 | p = get cb_n 7 |
| 983 | n = get cb_n 3 |
| 984 | if gt add add p 2 n 4096 |
| 985 | ret |
| 986 | end |
| 987 | if ge get cb_n 8 64 |
| 988 | ret |
| 989 | end |
| 990 | put cb_ev p uchar and n 255 |
| 991 | put cb_ev add p 1 uchar shr n 8 |
| 992 | for i ulong n bound 4096 |
| 993 | put cb_ev add add p 2 i get cb_pa i |
| 994 | end |
| 995 | put cb_n 7 add add p 2 n |
| 996 | c = add get cb_n 8 1 |
| 997 | put cb_n 8 c |
| 998 | put cb_ev 0 uchar and c 255 |
| 999 | put cb_ev 1 uchar shr c 8 |
| 1000 | end |
| 1001 | |
| 1002 | fn cb_redir s int d int to void |
| 1003 | if ge s 0 |
| 1004 | if ne dupfd s d 0 long d |
| 1005 | exit 127 |
| 1006 | end |
| 1007 | end |
| 1008 | end |
| 1009 | |
| 1010 | note spawn with fds fi/fo/fe (-1 = inherit parent fd); closes any >=0 fds in parent. |
| 1011 | note returns exit code 0..255, 256+sig on signal, 512 on spawn error. |
| 1012 | fn cb_spawn_fds fi int fo int fe int use_ev bool to ulong |
| 1013 | pid = fork |
| 1014 | if lt pid 0 |
| 1015 | if ge fi 0 |
| 1016 | z0 = fclose fi |
| 1017 | end |
| 1018 | if ge fo 0 |
| 1019 | z1 = fclose fo |
| 1020 | end |
| 1021 | if ge fe 0 |
| 1022 | z2 = fclose fe |
| 1023 | end |
| 1024 | ret 512 |
| 1025 | end |
| 1026 | if eq pid 0 |
| 1027 | cb_redir fi 0 |
| 1028 | cb_redir fo 1 |
| 1029 | cb_redir fe 2 |
| 1030 | if use_ev |
| 1031 | r = execenv cb_av 0 get cb_n 5 cb_ev 0 get cb_n 7 |
| 1032 | else |
| 1033 | r = execprog cb_av 0 get cb_n 5 |
| 1034 | end |
| 1035 | exit 127 |
| 1036 | end |
| 1037 | if ge fi 0 |
| 1038 | z0 = fclose fi |
| 1039 | end |
| 1040 | if ge fo 0 |
| 1041 | z1 = fclose fo |
| 1042 | end |
| 1043 | if ge fe 0 |
| 1044 | z2 = fclose fe |
| 1045 | end |
| 1046 | w = waitpid int pid cb_st 0 4 0 |
| 1047 | if ne w pid |
| 1048 | ret 512 |
| 1049 | end |
| 1050 | if ne get cb_st 0 0 |
| 1051 | ret add 256 ulong get cb_st 0 |
| 1052 | end |
| 1053 | ret ulong get cb_st 1 |
| 1054 | end |
| 1055 | |
| 1056 | note compile: OMC (in cb_pc[0..omclen)) < SRC (in cb_pa) > OUT (in cb_pb) 2> err (or inherit if fe == -1) |
| 1057 | fn cb_run_omc omclen ulong mode int fe int to ulong |
| 1058 | fi = fopen cb_pa 0 get cb_n 3 524288 0 |
| 1059 | if lt fi 0 |
| 1060 | if ge fe 0 |
| 1061 | z0 = fclose fe |
| 1062 | end |
| 1063 | ret 512 |
| 1064 | end |
| 1065 | z1 = unlink cb_pb 0 get cb_n 4 |
| 1066 | fo = fopen cb_pb 0 get cb_n 4 524865 mode |
| 1067 | if lt fo 0 |
| 1068 | z2 = fclose int fi |
| 1069 | if ge fe 0 |
| 1070 | z3 = fclose fe |
| 1071 | end |
| 1072 | ret 512 |
| 1073 | end |
| 1074 | cb_av_clr |
| 1075 | p = get cb_n 5 |
| 1076 | put cb_av p uchar and omclen 255 |
| 1077 | put cb_av add p 1 uchar shr omclen 8 |
| 1078 | for i ulong omclen bound 4096 |
| 1079 | put cb_av add add p 2 i get cb_pc i |
| 1080 | end |
| 1081 | put cb_n 5 add add p 2 omclen |
| 1082 | put cb_n 6 1 |
| 1083 | put cb_av 0 1 |
| 1084 | put cb_av 1 0 |
| 1085 | ret cb_spawn_fds int fi int fo fe false |
| 1086 | end |
| 1087 | |
| 1088 | fn cb_dent_nlen ns ulong lim ulong to ulong |
| 1089 | nl = ulong 0 |
| 1090 | while lt add ns nl lim bound 256 |
| 1091 | if eq get cb_dent add ns nl 0 |
| 1092 | break |
| 1093 | end |
| 1094 | nl = add nl 1 |
| 1095 | end |
| 1096 | ret nl |
| 1097 | end |
| 1098 | |
| 1099 | fn cb_rm_flat_one base_len ulong ns ulong nl ulong to void |
| 1100 | if and eq nl 1 eq get cb_dent ns 46 |
| 1101 | ret |
| 1102 | end |
| 1103 | if and eq nl 2 and eq get cb_dent ns 46 eq get cb_dent add ns 1 46 |
| 1104 | ret |
| 1105 | end |
| 1106 | put cb_n 3 base_len |
| 1107 | cb_pa_ch 47 |
| 1108 | for j ulong nl bound 256 |
| 1109 | cb_pa_ch get cb_dent add ns j |
| 1110 | end |
| 1111 | z = unlink cb_pa 0 get cb_n 3 |
| 1112 | end |
| 1113 | |
| 1114 | fn cb_rm_flat_buf base_len ulong nr ulong to void |
| 1115 | pos = ulong 0 |
| 1116 | while lt pos nr bound 32768 |
| 1117 | rlen = or ulong get cb_dent add pos 16 shl ulong get cb_dent add pos 17 8 |
| 1118 | if eq rlen 0 |
| 1119 | break |
| 1120 | end |
| 1121 | ns = add pos 19 |
| 1122 | nl = cb_dent_nlen ns add pos rlen |
| 1123 | cb_rm_flat_one base_len ns nl |
| 1124 | pos = add pos rlen |
| 1125 | end |
| 1126 | put cb_n 3 base_len |
| 1127 | end |
| 1128 | |
| 1129 | note remove a flat temporary directory (files inside + rmdir) at cb_pa |
| 1130 | fn cb_rm_flat_pa to void |
| 1131 | base_len = get cb_n 3 |
| 1132 | dfd = fopen cb_pa 0 base_len 540672 0 |
| 1133 | if lt dfd 0 |
| 1134 | ret |
| 1135 | end |
| 1136 | while true bound 65536 |
| 1137 | nr = getdnts int dfd cb_dent 0 32768 |
| 1138 | if le nr 0 |
| 1139 | break |
| 1140 | end |
| 1141 | cb_rm_flat_buf base_len ulong nr |
| 1142 | end |
| 1143 | z0 = fclose int dfd |
| 1144 | put cb_n 3 base_len |
| 1145 | z1 = rmdir cb_pa 0 base_len |
| 1146 | end |
| 1147 | |
| 1148 | fn cb_rb_match_at i ulong t view uchar * to bool |
| 1149 | for j ulong len t bound 4096 |
| 1150 | if ne get cb_rb add i j get t j |
| 1151 | ret false |
| 1152 | end |
| 1153 | end |
| 1154 | ret true |
| 1155 | end |
| 1156 | |
| 1157 | note check whether file in cb_pa contains text t |
| 1158 | fn cb_file_contains_pa t view uchar * to bool |
| 1159 | if not cb_read_pa |
| 1160 | ret false |
| 1161 | end |
| 1162 | n = get cb_n 0 |
| 1163 | m = len t |
| 1164 | if lt n m |
| 1165 | ret false |
| 1166 | end |
| 1167 | lim = add sub n m 1 |
| 1168 | for i ulong lim bound 9223372036854775807 |
| 1169 | if cb_rb_match_at i t |
| 1170 | ret true |
| 1171 | end |
| 1172 | end |
| 1173 | ret false |
| 1174 | end |