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 / apps / interplane-forge / browse / a_core.om
| 1 | note browse/a_core.om (ForgeBrowse): request-rest parsing, ref and path validation, ref |
| 2 | note resolution against the allow-listed storage, URL emission and small helpers. |
| 3 | note Names: functions br*, globals gb*, texts tb*. Contract: docs/CONTRACT.md v1. |
| 4 | note No user text ever reaches git as a ref: a ref is either a name listed by for-each-ref |
| 5 | note (longest match against the URL) or 7..40 lowercase hex naming a commit reachable from a |
| 6 | note published head or tag (brreach, shared with dl). Paths are checked by brokp |
| 7 | note (no NUL or control bytes, no empty, "." or ".." segment, no leading "-" or "/"). |
| 8 | global gbr uchar 4096 |
| 9 | global gbrl ulong 1 |
| 10 | global gbw uchar 64 |
| 11 | global gbwl ulong 1 |
| 12 | global gbref uchar 512 |
| 13 | global gbrefl ulong 1 |
| 14 | global gbfr uchar 1024 |
| 15 | global gbfrl ulong 1 |
| 16 | global gbsha uchar 40 |
| 17 | global gbshl ulong 1 |
| 18 | global gbsa uchar 40 |
| 19 | global gbp uchar 4096 |
| 20 | global gbpl ulong 1 |
| 21 | global gbn ulong 16 |
| 22 | text tbfer "for-each-ref" |
| 23 | text tbfmtr "--format=%(refname)" |
| 24 | text tbrh "refs/heads" |
| 25 | text tbrt "refs/tags" |
| 26 | text tbrhs "refs/heads/" |
| 27 | text tbrts "refs/tags/" |
| 28 | text tbns "refs/namespaces/" |
| 29 | text tbnsrh "/refs/heads" |
| 30 | text tbnsrt "/refs/tags" |
| 31 | text tbrevp "rev-parse" |
| 32 | text tbverif "--verify" |
| 33 | text tbq "-q" |
| 34 | text tbeoo "--end-of-options" |
| 35 | text tbpcom "^{commit}" |
| 36 | text tbmain "main" |
| 37 | text tbah "<a href=\"" |
| 38 | text tbaq "\">" |
| 39 | text tbae "</a>" |
| 40 | text tbhex "0123456789ABCDEF" |
| 41 | text tblhex "0123456789abcdef" |
| 42 | const BRTMO long 15000000000 |
| 43 | const BRBIG ulong 33554432 |
| 44 | note ---- byte classes |
| 45 | fn brdig c uchar to bool |
| 46 | ret and ge c 48 le c 57 |
| 47 | end |
| 48 | fn bralpha c uchar to bool |
| 49 | if and ge c 97 le c 122 |
| 50 | ret true |
| 51 | end |
| 52 | ret and ge c 65 le c 90 |
| 53 | end |
| 54 | fn bralnum c uchar to bool |
| 55 | if brdig c |
| 56 | ret true |
| 57 | end |
| 58 | ret bralpha c |
| 59 | end |
| 60 | fn brlhex c uchar to bool |
| 61 | if brdig c |
| 62 | ret true |
| 63 | end |
| 64 | ret and ge c 97 le c 102 |
| 65 | end |
| 66 | fn brspc c uchar to bool |
| 67 | ret or eq c 32 eq c 9 |
| 68 | end |
| 69 | fn brlow c uchar to uchar |
| 70 | if and ge c 65 le c 90 |
| 71 | ret uchar add c 32 |
| 72 | end |
| 73 | ret c |
| 74 | end |
| 75 | fn brhexv c uchar to ulong |
| 76 | if brdig c |
| 77 | ret ulong sub c 48 |
| 78 | end |
| 79 | if and ge c 97 le c 102 |
| 80 | ret ulong sub c 87 |
| 81 | end |
| 82 | if and ge c 65 le c 70 |
| 83 | ret ulong sub c 55 |
| 84 | end |
| 85 | ret 99 |
| 86 | end |
| 87 | note ---- rest parsing: frest = "" | "/" | "/<word>[/<rest>]"; rest percent-decoded into gbr |
| 88 | fn brisw t view uchar * to bool |
| 89 | n = get gbwl 0 |
| 90 | if ne n len t |
| 91 | ret false |
| 92 | end |
| 93 | for i ulong n |
| 94 | if ne get gbw i get t i |
| 95 | ret false |
| 96 | end |
| 97 | end |
| 98 | ret true |
| 99 | end |
| 100 | fn brparse to bool |
| 101 | n = get frestl 0 |
| 102 | put gbwl 0 0 |
| 103 | put gbrl 0 0 |
| 104 | if eq n 0 |
| 105 | ret true |
| 106 | end |
| 107 | i = ulong 1 |
| 108 | while lt i n bound BRBIG |
| 109 | c = get frest i |
| 110 | if eq c 47 |
| 111 | break |
| 112 | end |
| 113 | k = get gbwl 0 |
| 114 | if ge k 63 |
| 115 | ret false |
| 116 | end |
| 117 | put gbw k c |
| 118 | put gbwl 0 add k 1 |
| 119 | i = add i 1 |
| 120 | end |
| 121 | if lt i n |
| 122 | i = add i 1 |
| 123 | end |
| 124 | ret brdecr i n |
| 125 | end |
| 126 | note brdecr: percent-decode frest[a, e) into gbr; refuse NUL and control bytes (raw or decoded) |
| 127 | fn brdecr a ulong e ulong to bool |
| 128 | o = ulong 0 |
| 129 | i = a |
| 130 | while lt i e bound BRBIG |
| 131 | c = get frest i |
| 132 | if eq c 37 |
| 133 | if lt add i 2 e |
| 134 | h = brhexv get frest add i 1 |
| 135 | l = brhexv get frest add i 2 |
| 136 | if and lt h 16 lt l 16 |
| 137 | c = uchar add mul h 16 l |
| 138 | i = add i 2 |
| 139 | end |
| 140 | end |
| 141 | end |
| 142 | if or lt c 32 eq c 127 |
| 143 | ret false |
| 144 | end |
| 145 | if ge o 4096 |
| 146 | ret false |
| 147 | end |
| 148 | put gbr o c |
| 149 | o = add o 1 |
| 150 | i = add i 1 |
| 151 | end |
| 152 | put gbrl 0 o |
| 153 | ret true |
| 154 | end |
| 155 | note ---- path checks on gbp[0, gbpl) |
| 156 | fn brseg s ulong e ulong to bool |
| 157 | if ge s e |
| 158 | ret false |
| 159 | end |
| 160 | if eq get gbp s 46 |
| 161 | if eq e add s 1 |
| 162 | ret false |
| 163 | end |
| 164 | if eq e add s 2 |
| 165 | if eq get gbp add s 1 46 |
| 166 | ret false |
| 167 | end |
| 168 | end |
| 169 | end |
| 170 | ret true |
| 171 | end |
| 172 | fn brokp to bool |
| 173 | n = get gbpl 0 |
| 174 | if eq n 0 |
| 175 | ret true |
| 176 | end |
| 177 | if or eq get gbp 0 45 eq get gbp 0 47 |
| 178 | ret false |
| 179 | end |
| 180 | s = ulong 0 |
| 181 | for i ulong n bound BRBIG |
| 182 | c = get gbp i |
| 183 | if or lt c 32 eq c 127 |
| 184 | ret false |
| 185 | end |
| 186 | if eq c 47 |
| 187 | if not brseg s i |
| 188 | ret false |
| 189 | end |
| 190 | s = add i 1 |
| 191 | end |
| 192 | end |
| 193 | ret brseg s n |
| 194 | end |
| 195 | note ---- git argument helpers |
| 196 | fn brkns t view uchar * to void |
| 197 | fkopen |
| 198 | for i ulong len tbns |
| 199 | fkc get tbns i |
| 200 | end |
| 201 | for i ulong len tfnid |
| 202 | fkc get tfnid i |
| 203 | end |
| 204 | for i ulong len t |
| 205 | fkc get t i |
| 206 | end |
| 207 | end |
| 208 | fn brkfr to void |
| 209 | for i ulong get gbfrl 0 |
| 210 | fkc get gbfr i |
| 211 | end |
| 212 | end |
| 213 | fn brksha to void |
| 214 | for i ulong get gbshl 0 |
| 215 | fkc get gbsha i |
| 216 | end |
| 217 | end |
| 218 | fn brkp to void |
| 219 | for i ulong get gbpl 0 |
| 220 | fkc get gbp i |
| 221 | end |
| 222 | end |
| 223 | note brkshp: one argument "<sha>:<path>" |
| 224 | fn brkshp to void |
| 225 | fkopen |
| 226 | brksha |
| 227 | fkc 58 |
| 228 | brkp |
| 229 | end |
| 230 | note ---- fxo helpers |
| 231 | fn brxat a ulong e ulong t view uchar * to bool |
| 232 | k = len t |
| 233 | if gt add a k e |
| 234 | ret false |
| 235 | end |
| 236 | for i ulong k |
| 237 | if ne get fxo add a i get t i |
| 238 | ret false |
| 239 | end |
| 240 | end |
| 241 | ret true |
| 242 | end |
| 243 | fn brxfind a ulong e ulong c uchar to ulong |
| 244 | i = a |
| 245 | while lt i e bound BRBIG |
| 246 | if eq get fxo i c |
| 247 | ret i |
| 248 | end |
| 249 | i = add i 1 |
| 250 | end |
| 251 | ret e |
| 252 | end |
| 253 | fn brxdec a ulong e ulong to ulong |
| 254 | v = ulong 0 |
| 255 | i = a |
| 256 | while lt i e bound BRBIG |
| 257 | c = get fxo i |
| 258 | if not brdig c |
| 259 | ret v |
| 260 | end |
| 261 | v = add mul v 10 ulong sub c 48 |
| 262 | i = add i 1 |
| 263 | end |
| 264 | ret v |
| 265 | end |
| 266 | note brpfx: length of the ref prefix of fxo[s, e) (refs/heads/, refs/tags/, or the delegate |
| 267 | note namespace's), 0 when it is none of them |
| 268 | fn brpfx s ulong e ulong to ulong |
| 269 | if brxat s e tbrhs |
| 270 | ret len tbrhs |
| 271 | end |
| 272 | if brxat s e tbrts |
| 273 | ret len tbrts |
| 274 | end |
| 275 | if not brxat s e tbns |
| 276 | ret 0 |
| 277 | end |
| 278 | a = add s len tbns |
| 279 | if not brxat a e tfnid |
| 280 | ret 0 |
| 281 | end |
| 282 | a = add a len tfnid |
| 283 | if brxat a e tbnsrh |
| 284 | if lt add a len tbnsrh e |
| 285 | if eq get fxo add a len tbnsrh 47 |
| 286 | ret sub add a add len tbnsrh 1 s |
| 287 | end |
| 288 | end |
| 289 | end |
| 290 | if brxat a e tbnsrt |
| 291 | if lt add a len tbnsrt e |
| 292 | if eq get fxo add a len tbnsrt 47 |
| 293 | ret sub add a add len tbnsrt 1 s |
| 294 | end |
| 295 | end |
| 296 | end |
| 297 | ret 0 |
| 298 | end |
| 299 | note brmatch: short name fxo[a, e) is a prefix of gbr ending at a "/" or the end |
| 300 | fn brmatch a ulong e ulong to bool |
| 301 | k = sub e a |
| 302 | n = get gbrl 0 |
| 303 | if or eq k 0 gt k n |
| 304 | ret false |
| 305 | end |
| 306 | for i ulong k |
| 307 | if ne get fxo add a i get gbr i |
| 308 | ret false |
| 309 | end |
| 310 | end |
| 311 | if eq k n |
| 312 | ret true |
| 313 | end |
| 314 | ret eq get gbr k 47 |
| 315 | end |
| 316 | note brbest: longest listed ref matching the start of gbr; sets gbfr/gbref, returns its length. The |
| 317 | note listing (fsopen by brres) is read one line at a time through fxo, so no size of the ref list cuts |
| 318 | note it: a ref past the first 16 MiB of names is found like any other. 0 when no ref matches, or the |
| 319 | note listing could not be read to its end (brres looks at fxotr and the exit status). |
| 320 | fn brbest to ulong |
| 321 | best = ulong 0 |
| 322 | for it ulong 1000000000 |
| 323 | e = fsnext 10 |
| 324 | if eq e FSEND |
| 325 | break |
| 326 | end |
| 327 | if eq e FSFAIL |
| 328 | ret 0 |
| 329 | end |
| 330 | s = get fsrs 0 |
| 331 | pl = brpfx s e |
| 332 | if gt pl 0 |
| 333 | if brmatch add s pl e |
| 334 | k = sub e add s pl |
| 335 | note a ref name longer than gbref (512 bytes) is not a match: the copy below would write past gbref, |
| 336 | note which is trap 3 and ends the server (exit 103). The page is then a 404. |
| 337 | if and gt k best le k len gbref |
| 338 | best = k |
| 339 | brsetfr s e |
| 340 | end |
| 341 | end |
| 342 | end |
| 343 | end |
| 344 | if eq best 0 |
| 345 | ret 0 |
| 346 | end |
| 347 | put gbrefl 0 best |
| 348 | for i ulong best |
| 349 | put gbref i get gbr i |
| 350 | end |
| 351 | ret best |
| 352 | end |
| 353 | fn brsetfr a ulong e ulong to void |
| 354 | k = sub e a |
| 355 | if gt k 1024 |
| 356 | k = 1024 |
| 357 | end |
| 358 | for i ulong k |
| 359 | put gbfr i get fxo add a i |
| 360 | end |
| 361 | put gbfrl 0 k |
| 362 | end |
| 363 | note brhexseg: gbr starts with 7..40 lowercase hex then "/" or the end; returns that length or 0 |
| 364 | fn brhexseg to ulong |
| 365 | n = get gbrl 0 |
| 366 | k = ulong 0 |
| 367 | while lt k n bound BRBIG |
| 368 | c = get gbr k |
| 369 | if eq c 47 |
| 370 | break |
| 371 | end |
| 372 | if not brlhex c |
| 373 | ret 0 |
| 374 | end |
| 375 | k = add k 1 |
| 376 | end |
| 377 | if or lt k 7 gt k 40 |
| 378 | ret 0 |
| 379 | end |
| 380 | ret k |
| 381 | end |
| 382 | note brrevp: rev-parse gbfr^{commit} into gbsha; false when it does not name a commit |
| 383 | fn brrevp to bool |
| 384 | put gbshl 0 0 |
| 385 | fksource |
| 386 | fka tbrevp |
| 387 | fka tbverif |
| 388 | fka tbq |
| 389 | fka tbeoo |
| 390 | fkopen |
| 391 | brkfr |
| 392 | for i ulong len tbpcom |
| 393 | fkc get tbpcom i |
| 394 | end |
| 395 | st = fexec BRTMO |
| 396 | if ne st 0 |
| 397 | ret false |
| 398 | end |
| 399 | if lt get fxol 0 40 |
| 400 | ret false |
| 401 | end |
| 402 | for i ulong 40 |
| 403 | c = get fxo i |
| 404 | if not brlhex c |
| 405 | ret false |
| 406 | end |
| 407 | put gbsha i c |
| 408 | end |
| 409 | put gbshl 0 40 |
| 410 | ret true |
| 411 | end |
| 412 | note brres: resolve gbr = "<ref>[/<path>]" (decoded URL rest). Sets gbref (as in the URL), |
| 413 | note gbfr (full refname or hex), gbsha (40 hex) and gbp (path, trailing "/" dropped). False = 404. |
| 414 | fn brres to bool |
| 415 | put gbrefl 0 0 |
| 416 | put gbpl 0 0 |
| 417 | put gbshl 0 0 |
| 418 | put gbfrl 0 0 |
| 419 | if eq get gbrl 0 0 |
| 420 | ret false |
| 421 | end |
| 422 | fksource |
| 423 | fka tbfer |
| 424 | fka tbfmtr |
| 425 | fka tbrh |
| 426 | fka tbrt |
| 427 | brkns tbnsrh |
| 428 | brkns tbnsrt |
| 429 | if not fsopen BRTMO |
| 430 | ret false |
| 431 | end |
| 432 | k = brbest |
| 433 | st = fsclose |
| 434 | note one ref name longer than fxo cannot be read whole: the ref asked for may be that one |
| 435 | if ne get fxotr 0 0 |
| 436 | fincut |
| 437 | ret false |
| 438 | end |
| 439 | if ne st 0 |
| 440 | ret false |
| 441 | end |
| 442 | hx = false |
| 443 | if eq k 0 |
| 444 | k = brhexseg |
| 445 | if eq k 0 |
| 446 | ret false |
| 447 | end |
| 448 | hx = true |
| 449 | for i ulong k |
| 450 | put gbref i get gbr i |
| 451 | put gbfr i get gbr i |
| 452 | end |
| 453 | put gbrefl 0 k |
| 454 | put gbfrl 0 k |
| 455 | end |
| 456 | if not brsetp k |
| 457 | ret false |
| 458 | end |
| 459 | if not brrevp |
| 460 | ret false |
| 461 | end |
| 462 | if hx |
| 463 | ret brreach |
| 464 | end |
| 465 | ret true |
| 466 | end |
| 467 | note brreach: gbsha is reachable from a published head or tag (root or delegate namespace); |
| 468 | note the check is dl's (dlrch0 / dlrch1), so a commit id other peers, cobs or rewritten |
| 469 | note history hold is 404 in browse exactly as in downloads |
| 470 | fn brreach to bool |
| 471 | dlrch0 |
| 472 | fkopen |
| 473 | brksha |
| 474 | ret dlrch1 |
| 475 | end |
| 476 | note brsetp: gbp = gbr after the ref (k bytes) and its "/", without trailing slashes; checked |
| 477 | fn brsetp k ulong to bool |
| 478 | n = get gbrl 0 |
| 479 | a = k |
| 480 | if lt a n |
| 481 | a = add a 1 |
| 482 | end |
| 483 | e = n |
| 484 | while gt e a bound BRBIG |
| 485 | if ne get gbr sub e 1 47 |
| 486 | break |
| 487 | end |
| 488 | e = sub e 1 |
| 489 | end |
| 490 | o = ulong 0 |
| 491 | for i ulong sub e a |
| 492 | put gbp i get gbr add a i |
| 493 | o = add o 1 |
| 494 | end |
| 495 | put gbpl 0 o |
| 496 | ret brokp |
| 497 | end |
| 498 | note brdefref: put the default branch name into gbr (HEAD's branch, nodes/head.om; "main" when none) |
| 499 | fn brdefref to void |
| 500 | put gbrl 0 0 |
| 501 | if nshead |
| 502 | for i ulong get nshbl 0 bound 256 |
| 503 | put gbr i get nshb i |
| 504 | end |
| 505 | put gbrl 0 get nshbl 0 |
| 506 | end |
| 507 | if eq get gbrl 0 0 |
| 508 | for i ulong len tbmain |
| 509 | put gbr i get tbmain i |
| 510 | end |
| 511 | put gbrl 0 len tbmain |
| 512 | end |
| 513 | end |
| 514 | note ---- URL emission into fob (percent-encoding; every byte not unreserved or "/" is %XX) |
| 515 | fn bruc c uchar to void |
| 516 | ok = bralnum c |
| 517 | if or eq c 45 eq c 46 |
| 518 | ok = true |
| 519 | end |
| 520 | if or eq c 95 eq c 126 |
| 521 | ok = true |
| 522 | end |
| 523 | if eq c 47 |
| 524 | ok = true |
| 525 | end |
| 526 | if ok |
| 527 | fputc c |
| 528 | ret |
| 529 | end |
| 530 | fputc 37 |
| 531 | fputc get tbhex shr c 4 |
| 532 | fputc get tbhex and c 15 |
| 533 | end |
| 534 | fn brurepo to void |
| 535 | fputb |
| 536 | for i ulong get fownl 0 |
| 537 | bruc get fown i |
| 538 | end |
| 539 | fputc 47 |
| 540 | for i ulong get frepol 0 |
| 541 | bruc get frepo i |
| 542 | end |
| 543 | end |
| 544 | fn bruref to void |
| 545 | for i ulong get gbrefl 0 |
| 546 | bruc get gbref i |
| 547 | end |
| 548 | end |
| 549 | fn brup a ulong n ulong to void |
| 550 | for i ulong n bound BRBIG |
| 551 | bruc get gbp add a i |
| 552 | end |
| 553 | end |
| 554 | note bruact: <base><owner>/<repo>/<t>/<ref> |
| 555 | fn bruact t view uchar * to void |
| 556 | brurepo |
| 557 | fputc 47 |
| 558 | fput t |
| 559 | fputc 47 |
| 560 | bruref |
| 561 | end |
| 562 | note bruactp: bruact then "/" and gbp[0, n) when n > 0 |
| 563 | fn bruactp t view uchar * n ulong to void |
| 564 | bruact t |
| 565 | if gt n 0 |
| 566 | fputc 47 |
| 567 | brup 0 n |
| 568 | end |
| 569 | end |
| 570 | note brushx: url-encode fxo[a, a+n) (refs, paths and shas read from git output) |
| 571 | fn bruxo a ulong n ulong to void |
| 572 | for i ulong n bound BRBIG |
| 573 | bruc get fxo add a i |
| 574 | end |
| 575 | end |
| 576 | note ---- misc output |
| 577 | fn brgesc a ulong n ulong to void |
| 578 | for i ulong n bound BRBIG |
| 579 | fescc get gbp add a i |
| 580 | end |
| 581 | end |
| 582 | fn brrefesc to void |
| 583 | for i ulong get gbrefl 0 |
| 584 | fescc get gbref i |
| 585 | end |
| 586 | end |
| 587 | fn brshort to void |
| 588 | for i ulong 7 |
| 589 | fputc get gbsha i |
| 590 | end |
| 591 | end |
| 592 | fn brd2 v ulong to void |
| 593 | fputc uchar add div v 10 48 |
| 594 | fputc uchar add rem v 10 48 |
| 595 | end |
| 596 | note brdate: unix seconds as YYYY-MM-DD (UTC, proleptic Gregorian, days-from-civil inverse) |
| 597 | fn brdate t ulong to void |
| 598 | z = add div t 86400 719468 |
| 599 | era = div z 146097 |
| 600 | doe = sub z mul era 146097 |
| 601 | yoe = div sub add sub doe div doe 1460 div doe 36524 div doe 146096 365 |
| 602 | y = add yoe mul era 400 |
| 603 | doy = sub doe sub add mul 365 yoe div yoe 4 div yoe 100 |
| 604 | mp = div add mul 5 doy 2 153 |
| 605 | d = add sub doy div add mul 153 mp 2 5 1 |
| 606 | m = add mp 3 |
| 607 | if ge mp 10 |
| 608 | m = sub mp 9 |
| 609 | end |
| 610 | if le m 2 |
| 611 | y = add y 1 |
| 612 | end |
| 613 | fnum y |
| 614 | fputc 45 |
| 615 | brd2 m |
| 616 | fputc 45 |
| 617 | brd2 d |
| 618 | end |
| 619 | note brtime: the UTC time of day HH:MM |
| 620 | fn brtime t ulong to void |
| 621 | s = rem t 86400 |
| 622 | brd2 div s 3600 |
| 623 | fputc 58 |
| 624 | brd2 div rem s 3600 60 |
| 625 | end |