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 / apps / interplane-forge / browse / a_core.om

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