Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/interplane-forge/blob/main/browse/a_core.om # interplane-forge INTERPLANE Forge Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `interplane-forge / browse / a_core.om` 629 lines ยท 11111 bytes Raw Blame History 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 "" 39 text tbae "" 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 = "" | "/" | "/[/]"; 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 ":" 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 = "[/]" (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 note A full commit id wins over a same-named ref, keeping pinned page links pinned. 444 if eq brhexseg 40 445 k = 0 446 end 447 if eq k 0 448 k = brhexseg 449 if eq k 0 450 ret false 451 end 452 hx = true 453 for i ulong k 454 put gbref i get gbr i 455 put gbfr i get gbr i 456 end 457 put gbrefl 0 k 458 put gbfrl 0 k 459 end 460 if not brsetp k 461 ret false 462 end 463 if not brrevp 464 ret false 465 end 466 if hx 467 ret brreach 468 end 469 ret true 470 end 471 note brreach: gbsha is reachable from a published head or tag (root or delegate namespace); 472 note the check is dl's (dlrch0 / dlrch1), so a commit id other peers, cobs or rewritten 473 note history hold is 404 in browse exactly as in downloads 474 fn brreach to bool 475 dlrch0 476 fkopen 477 brksha 478 ret dlrch1 479 end 480 note brsetp: gbp = gbr after the ref (k bytes) and its "/", without trailing slashes; checked 481 fn brsetp k ulong to bool 482 n = get gbrl 0 483 a = k 484 if lt a n 485 a = add a 1 486 end 487 e = n 488 while gt e a bound BRBIG 489 if ne get gbr sub e 1 47 490 break 491 end 492 e = sub e 1 493 end 494 o = ulong 0 495 for i ulong sub e a 496 put gbp i get gbr add a i 497 o = add o 1 498 end 499 put gbpl 0 o 500 ret brokp 501 end 502 note brdefref: put the default branch name into gbr (HEAD's branch, nodes/head.om; "main" when none) 503 fn brdefref to void 504 put gbrl 0 0 505 if nshead 506 for i ulong get nshbl 0 bound 256 507 put gbr i get nshb i 508 end 509 put gbrl 0 get nshbl 0 510 end 511 if eq get gbrl 0 0 512 for i ulong len tbmain 513 put gbr i get tbmain i 514 end 515 put gbrl 0 len tbmain 516 end 517 end 518 note ---- URL emission into fob (percent-encoding; every byte not unreserved or "/" is %XX) 519 fn bruc c uchar to void 520 ok = bralnum c 521 if or eq c 45 eq c 46 522 ok = true 523 end 524 if or eq c 95 eq c 126 525 ok = true 526 end 527 if eq c 47 528 ok = true 529 end 530 if ok 531 fputc c 532 ret 533 end 534 fputc 37 535 fputc get tbhex shr c 4 536 fputc get tbhex and c 15 537 end 538 fn brurepo to void 539 fputb 540 for i ulong get fownl 0 541 bruc get fown i 542 end 543 fputc 47 544 for i ulong get frepol 0 545 bruc get frepo i 546 end 547 end 548 fn bruref to void 549 for i ulong get gbrefl 0 550 bruc get gbref i 551 end 552 end 553 fn brup a ulong n ulong to void 554 for i ulong n bound BRBIG 555 bruc get gbp add a i 556 end 557 end 558 note bruact: /// 559 fn bruact t view uchar * to void 560 brurepo 561 fputc 47 562 fput t 563 fputc 47 564 bruref 565 end 566 note bruactp: bruact then "/" and gbp[0, n) when n > 0 567 fn bruactp t view uchar * n ulong to void 568 bruact t 569 if gt n 0 570 fputc 47 571 brup 0 n 572 end 573 end 574 note brushx: url-encode fxo[a, a+n) (refs, paths and shas read from git output) 575 fn bruxo a ulong n ulong to void 576 for i ulong n bound BRBIG 577 bruc get fxo add a i 578 end 579 end 580 note ---- misc output 581 fn brgesc a ulong n ulong to void 582 for i ulong n bound BRBIG 583 fescc get gbp add a i 584 end 585 end 586 fn brrefesc to void 587 for i ulong get gbrefl 0 588 fescc get gbref i 589 end 590 end 591 fn brshort to void 592 for i ulong 7 593 fputc get gbsha i 594 end 595 end 596 fn brd2 v ulong to void 597 fputc uchar add div v 10 48 598 fputc uchar add rem v 10 48 599 end 600 note brdate: unix seconds as YYYY-MM-DD (UTC, proleptic Gregorian, days-from-civil inverse) 601 fn brdate t ulong to void 602 z = add div t 86400 719468 603 era = div z 146097 604 doe = sub z mul era 146097 605 yoe = div sub add sub doe div doe 1460 div doe 36524 div doe 146096 365 606 y = add yoe mul era 400 607 doy = sub doe sub add mul 365 yoe div yoe 4 div yoe 100 608 mp = div add mul 5 doy 2 153 609 d = add sub doy div add mul 153 mp 2 5 1 610 m = add mp 3 611 if ge mp 10 612 m = sub mp 9 613 end 614 if le m 2 615 y = add y 1 616 end 617 fnum y 618 fputc 45 619 brd2 m 620 fputc 45 621 brd2 d 622 end 623 note brtime: the UTC time of day HH:MM 624 fn brtime t ulong to void 625 s = rem t 86400 626 brd2 div s 3600 627 fputc 58 628 brd2 div rem s 3600 60 629 end