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 / si / packages / homebrew / formula.om

811 lines · 13884 bytes Raw Blame History
1om2
2note si/packages/homebrew/formula.om: Deterministic Homebrew Ruby formula generator and validator.
3note Faithful pure-Omega conversion of homebrew-omega (Formula/omc.rb).
4note Reads key=value manifest on stdin (version, url, sha256, omc_sha256, optional homepage, license),
5note validates semver/date version, allowed HTTPS archive URL prefix, 64-hex lowercase SHA-256 digests,
6note rejects Ruby/shell injection characters, emits canonical Formula/omc.rb on stdout, and self-validates.
7note Exit codes: 0 = emitted valid formula, 1 = validation refusal, 2 = missing fields or I/O error.
8
9global inbuf uchar 8192
10global rdbuf uchar 2048
11global outbuf uchar 8192
12global f_ver uchar 128
13global f_url uchar 384
14global f_sha uchar 128
15global f_omc uchar 128
16global f_home uchar 384
17global f_lic uchar 128
18global meta ulong 16
19
20const k_inlen ulong 0
21const k_outlen ulong 1
22const k_vlen ulong 2
23const k_ulen ulong 3
24const k_slen ulong 4
25const k_olen ulong 5
26const k_hlen ulong 6
27const k_llen ulong 7
28const k_err ulong 8
29
30text pfx_ver "version="
31text pfx_url "url="
32text pfx_sha "sha256="
33text pfx_omc "omc_sha256="
34text pfx_home "homepage="
35text pfx_lic "license="
36
37text def_home "https://github.com/aien-dev/omega-mv"
38text def_lic "Apache-2.0"
39
40text allow_p1 "https://interplane.io/"
41text allow_p2 "https://github.com/aien-dev/"
42text allow_p3 "https://github.com/drakestapleton/"
43text sfx_tgz ".tar.gz"
44text sfx_txz ".tar.xz"
45
46text rb_hdr "# omc, the Omega compiler, born from the 408-byte hand-written seed of aien-dev/omega-mv.\n# The build runs omega-mv's seed/omega-from-seed.sh (seed0 -> hex1 -> asm2 -> om4 -> omc -> omc -> omc)\n# using only sh, printf, cat, stat, sed, sha256sum and cmp, then refuses any omc whose sha256 is not\n# the pinned one. AArch64 (arm64) Linux only: omc runs on and emits code for that platform alone.\nclass Omc < Formula\n desc \"Omega compiler, born from a 408-byte hand-written seed\"\n homepage \""
47text rb_url "\"\n url \""
48text rb_ver "\"\n version \""
49text rb_sha "\"\n sha256 \""
50text rb_lic "\"\n license \""
51text rb_dep "\"\n\n depends_on arch: :arm64\n depends_on :linux\n\n OMC_SHA256 = \""
52text rb_inst "\".freeze\n\n def install\n system \"sh\", \"seed/omega-from-seed.sh\"\n got = Digest::SHA256.file(\"seed/out/omc\").hexdigest\n odie \"omc sha256 #{got} is not the pinned #{OMC_SHA256}\" if got != OMC_SHA256\n bin.install \"seed/out/omc\"\n end\n\n test do\n (testpath/\"hello.om\").write <<~OM\n note hello: sum 1..10 and print \"omega 55\" through write\n global out uchar 16\n fn main to int\n s = ulong 0\n for i ulong 11\n s = add s i\n end\n put out 0 111\n put out 1 109\n put out 2 101\n put out 3 103\n put out 4 97\n put out 5 32\n put out 6 uchar add div s 10 48\n put out 7 uchar add rem s 10 48\n put out 8 10\n w = write 1 out 0 9\n ret 0\n end\n OM\n system \"sh\", \"-c\", \"#{bin}/omc < hello.om > hello\"\n chmod 0755, testpath/\"hello\"\n assert_equal \"omega 55\\n\", shell_output(\"./hello\")\n assert_equal OMC_SHA256, Digest::SHA256.file(bin/\"omc\").hexdigest\n end\nend\n"
53
54text chk_cls "class Omc < Formula"
55text chk_arm "depends_on arch: :arm64"
56text chk_lin "depends_on :linux"
57text chk_odi "odie \"omc sha256 "
58
59text e_io "formula: failed to read stdin\n"
60text e_miss "formula: missing required field (version, url, sha256, omc_sha256)\n"
61text e_ver "formula: refused invalid or unsafe version\n"
62text e_url "formula: refused invalid or disallowed url\n"
63text e_sha "formula: refused invalid sha256 (need 64 lowercase hex)\n"
64text e_omc "formula: refused invalid omc_sha256 (need 64 lowercase hex)\n"
65text e_home "formula: refused invalid homepage\n"
66text e_lic "formula: refused invalid license\n"
67text e_self "formula: emitted formula failed self-validation\n"
68
69fn write_all fd int src view uchar * off ulong n ulong to void
70p = ulong 0
71while lt p n bound 16384
72k = write fd src add off p sub n p
73if le k 0
74exit 2
75end
76p = add p ulong k
77end
78end
79
80fn wstr fd int s view uchar * to void
81write_all fd s 0 len s
82end
83
84fn is_dig c uchar to bool
85ret and ge c 48 le c 57
86end
87
88fn is_hex_low c uchar to bool
89if is_dig c
90ret true
91end
92ret and ge c 97 le c 102
93end
94
95fn is_alnum c uchar to bool
96if is_dig c
97ret true
98end
99if and ge c 65 le c 90
100ret true
101end
102ret and ge c 97 le c 122
103end
104
105fn is_injection_ch c uchar to bool
106if or lt c 32 gt c 126
107ret true
108end
109if or eq c 34 eq c 39
110ret true
111end
112if or eq c 96 eq c 92
113ret true
114end
115if or eq c 36 eq c 35
116ret true
117end
118if or eq c 59 eq c 124
119ret true
120end
121if or eq c 38 eq c 40
122ret true
123end
124if or eq c 41 eq c 123
125ret true
126end
127if or eq c 125 or eq c 60 eq c 62
128ret true
129end
130ret false
131end
132
133fn is_url_ch c uchar to bool
134if is_alnum c
135ret true
136end
137if or eq c 45 eq c 46
138ret true
139end
140if or eq c 95 eq c 126
141ret true
142end
143if or eq c 58 eq c 47
144ret true
145end
146ret eq c 37
147end
148
149fn valid_sha_field which ulong to bool
150n = ulong 0
151if eq which 0
152n = get meta k_slen
153else
154n = get meta k_olen
155end
156if ne n 64
157ret false
158end
159i = ulong 0
160while lt i 64 bound 64
161c = uchar 0
162if eq which 0
163c = get f_sha i
164else
165c = get f_omc i
166end
167if not is_hex_low c
168ret false
169end
170i = add i 1
171end
172ret true
173end
174
175fn valid_ver_ext pos ulong vlen ulong to bool
176if ge pos vlen
177ret false
178end
179seg = ulong 0
180i = pos
181while lt i vlen bound 128
182c = get f_ver i
183if is_injection_ch c
184ret false
185end
186if eq c 46
187if eq seg 0
188ret false
189end
190seg = ulong 0
191else
192if or is_alnum c eq c 45
193seg = add seg 1
194else
195ret false
196end
197end
198i = add i 1
199end
200ret gt seg 0
201end
202
203fn valid_date_ver to bool
204j = ulong 0
205while lt j 8 bound 8
206if not is_dig get f_ver j
207ret false
208end
209j = add j 1
210end
211yr = mul ulong sub get f_ver 0 48 1000
212yr = add yr mul ulong sub get f_ver 1 48 100
213yr = add yr mul ulong sub get f_ver 2 48 10
214yr = add yr ulong sub get f_ver 3 48
215mo = mul ulong sub get f_ver 4 48 10
216mo = add mo ulong sub get f_ver 5 48
217dy = mul ulong sub get f_ver 6 48 10
218dy = add dy ulong sub get f_ver 7 48
219if lt yr 2020
220ret false
221end
222if or lt mo 1 gt mo 12
223ret false
224end
225ret and ge dy 1 le dy 31
226end
227
228note valid_version: accepts semver MAJOR.MINOR.PATCH[-pre][+build] or YYYYMMDD (8 digits)
229fn valid_version to bool
230n = get meta k_vlen
231if or eq n 0 gt n 64
232ret false
233end
234i = ulong 0
235while lt i n bound 128
236if is_injection_ch get f_ver i
237ret false
238end
239i = add i 1
240end
241if eq n 8
242if valid_date_ver
243ret true
244end
245end
246dots = ulong 0
247seg = ulong 0
248p = ulong 0
249while lt p n bound 128
250c = get f_ver p
251if is_dig c
252seg = add seg 1
253else
254if eq c 46
255if or eq seg 0 ge dots 2
256ret false
257end
258dots = add dots 1
259seg = ulong 0
260else
261if or eq c 45 eq c 43
262if or ne dots 2 eq seg 0
263ret false
264end
265ret valid_ver_ext add p 1 n
266else
267ret false
268end
269end
270end
271p = add p 1
272end
273ret and eq dots 2 gt seg 0
274end
275
276fn url_starts_with pfx view uchar * is_home bool to bool
277pl = len pfx
278ul = ulong 0
279if is_home
280ul = get meta k_hlen
281else
282ul = get meta k_ulen
283end
284if lt ul pl
285ret false
286end
287i = ulong 0
288while lt i pl bound 64
289c = uchar 0
290if is_home
291c = get f_home i
292else
293c = get f_url i
294end
295if ne c get pfx i
296ret false
297end
298i = add i 1
299end
300ret true
301end
302
303fn url_ends_with sfx view uchar * to bool
304sl = len sfx
305ul = get meta k_ulen
306if lt ul sl
307ret false
308end
309off = sub ul sl
310i = ulong 0
311while lt i sl bound 32
312if ne get f_url add off i get sfx i
313ret false
314end
315i = add i 1
316end
317ret true
318end
319
320fn url_has_dotdot is_home bool to bool
321ul = ulong 0
322if is_home
323ul = get meta k_hlen
324else
325ul = get meta k_ulen
326end
327if lt ul 2
328ret false
329end
330i = ulong 0
331lim = sub ul 1
332while lt i lim bound 384
333a = uchar 0
334b = uchar 0
335if is_home
336a = get f_home i
337b = get f_home add i 1
338else
339a = get f_url i
340b = get f_url add i 1
341end
342if and eq a 46 eq b 46
343ret true
344end
345i = add i 1
346end
347ret false
348end
349
350fn valid_url to bool
351ul = get meta k_ulen
352if or lt ul 16 gt ul 320
353ret false
354end
355ok_pfx = false
356if url_starts_with allow_p1 false
357ok_pfx = true
358end
359if url_starts_with allow_p2 false
360ok_pfx = true
361end
362if url_starts_with allow_p3 false
363ok_pfx = true
364end
365if not ok_pfx
366ret false
367end
368ok_sfx = false
369if url_ends_with sfx_tgz
370ok_sfx = true
371end
372if url_ends_with sfx_txz
373ok_sfx = true
374end
375if not ok_sfx
376ret false
377end
378if url_has_dotdot false
379ret false
380end
381i = ulong 0
382while lt i ul bound 384
383c = get f_url i
384if or is_injection_ch c not is_url_ch c
385ret false
386end
387i = add i 1
388end
389ret true
390end
391
392fn valid_homepage to bool
393hl = get meta k_hlen
394if or lt hl 16 gt hl 320
395ret false
396end
397ok_pfx = false
398if url_starts_with allow_p1 true
399ok_pfx = true
400end
401if url_starts_with allow_p2 true
402ok_pfx = true
403end
404if url_starts_with allow_p3 true
405ok_pfx = true
406end
407if not ok_pfx
408ret false
409end
410if url_has_dotdot true
411ret false
412end
413i = ulong 0
414while lt i hl bound 384
415c = get f_home i
416if or is_injection_ch c not is_url_ch c
417ret false
418end
419i = add i 1
420end
421ret true
422end
423
424fn valid_license to bool
425ll = get meta k_llen
426if or eq ll 0 gt ll 64
427ret false
428end
429i = ulong 0
430while lt i ll bound 64
431c = get f_lic i
432if is_injection_ch c
433ret false
434end
435ok = false
436if is_alnum c
437ok = true
438end
439if or eq c 45 or eq c 46 or eq c 43 eq c 32
440ok = true
441end
442if not ok
443ret false
444end
445i = add i 1
446end
447ret true
448end
449
450note Output builder helpers
451fn out_clear to void
452put meta k_outlen 0
453end
454
455fn out_push_lit s view uchar * to void
456sl = len s
457p = get meta k_outlen
458i = ulong 0
459while lt i sl bound 4096
460if lt p 8192
461put outbuf p get s i
462p = add p 1
463end
464i = add i 1
465end
466put meta k_outlen p
467end
468
469fn out_push_field which ulong to void
470p = get meta k_outlen
471n = ulong 0
472if eq which 0
473n = get meta k_hlen
474else
475if eq which 1
476n = get meta k_ulen
477else
478if eq which 2
479n = get meta k_vlen
480else
481if eq which 3
482n = get meta k_slen
483else
484if eq which 4
485n = get meta k_llen
486else
487n = get meta k_olen
488end
489end
490end
491end
492end
493i = ulong 0
494while lt i n bound 384
495c = uchar 0
496if eq which 0
497c = get f_home i
498else
499if eq which 1
500c = get f_url i
501else
502if eq which 2
503c = get f_ver i
504else
505if eq which 3
506c = get f_sha i
507else
508if eq which 4
509c = get f_lic i
510else
511c = get f_omc i
512end
513end
514end
515end
516end
517if lt p 8192
518put outbuf p c
519p = add p 1
520end
521i = add i 1
522end
523put meta k_outlen p
524end
525
526fn emit_formula to void
527out_clear
528out_push_lit rb_hdr
529out_push_field 0
530out_push_lit rb_url
531out_push_field 1
532out_push_lit rb_ver
533out_push_field 2
534out_push_lit rb_sha
535out_push_field 3
536out_push_lit rb_lic
537out_push_field 4
538out_push_lit rb_dep
539out_push_field 5
540out_push_lit rb_inst
541end
542
543fn out_starts_at pos ulong needle view uchar * to bool
544nl = len needle
545tot = get meta k_outlen
546if gt add pos nl tot
547ret false
548end
549i = ulong 0
550while lt i nl bound 128
551if ne get outbuf add pos i get needle i
552ret false
553end
554i = add i 1
555end
556ret true
557end
558
559fn out_contains needle view uchar * to bool
560nl = len needle
561tot = get meta k_outlen
562if gt nl tot
563ret false
564end
565lim = add sub tot nl 1
566i = ulong 0
567while lt i lim bound 8192
568if out_starts_at i needle
569ret true
570end
571i = add i 1
572end
573ret false
574end
575
576fn validate_emitted_formula to bool
577if not out_contains chk_cls
578ret false
579end
580if not out_contains chk_arm
581ret false
582end
583if not out_contains chk_lin
584ret false
585end
586if not out_contains chk_odi
587ret false
588end
589ret true
590end
591
592fn in_starts off ulong llen ulong pfx view uchar * to bool
593pl = len pfx
594if lt llen pl
595ret false
596end
597i = ulong 0
598while lt i pl bound 64
599if ne get inbuf add off i get pfx i
600ret false
601end
602i = add i 1
603end
604ret true
605end
606
607fn copy_in_to_field which ulong off ulong n ulong to void
608lim = ulong 128
609if or eq which 0 eq which 1
610lim = ulong 384
611end
612if gt n lim
613n = lim
614end
615i = ulong 0
616while lt i n bound 384
617c = get inbuf add off i
618if eq which 0
619put f_home i c
620else
621if eq which 1
622put f_url i c
623else
624if eq which 2
625put f_ver i c
626else
627if eq which 3
628put f_sha i c
629else
630if eq which 4
631put f_lic i c
632else
633put f_omc i c
634end
635end
636end
637end
638end
639i = add i 1
640end
641if eq which 0
642put meta k_hlen n
643else
644if eq which 1
645put meta k_ulen n
646else
647if eq which 2
648put meta k_vlen n
649else
650if eq which 3
651put meta k_slen n
652else
653if eq which 4
654put meta k_llen n
655else
656put meta k_olen n
657end
658end
659end
660end
661end
662end
663
664fn set_defaults to void
665for i ulong 16
666put meta i 0
667end
668hl = len def_home
669i = ulong 0
670while lt i hl bound 128
671put f_home i get def_home i
672i = add i 1
673end
674put meta k_hlen hl
675ll = len def_lic
676j = ulong 0
677while lt j ll bound 64
678put f_lic j get def_lic j
679j = add j 1
680end
681put meta k_llen ll
682end
683
684fn parse_manifest to void
685tot = get meta k_inlen
686pos = ulong 0
687while lt pos tot bound 8192
688ls = pos
689while lt pos tot bound 8192
690if eq get inbuf pos 10
691break
692end
693pos = add pos 1
694end
695ll = sub pos ls
696if gt ll 0
697if ne get inbuf ls 35
698if in_starts ls ll pfx_ver
699copy_in_to_field 2 add ls 8 sub ll 8
700else
701if in_starts ls ll pfx_url
702copy_in_to_field 1 add ls 4 sub ll 4
703else
704if in_starts ls ll pfx_sha
705copy_in_to_field 3 add ls 7 sub ll 7
706else
707if in_starts ls ll pfx_omc
708copy_in_to_field 5 add ls 11 sub ll 11
709else
710if in_starts ls ll pfx_home
711copy_in_to_field 0 add ls 9 sub ll 9
712else
713if in_starts ls ll pfx_lic
714copy_in_to_field 4 add ls 8 sub ll 8
715end
716end
717end
718end
719end
720end
721end
722end
723if lt pos tot
724pos = add pos 1
725end
726end
727end
728
729fn read_stdin to bool
730tot = ulong 0
731while true bound 64
732k = read 0 rdbuf 0 2048
733if lt k 0
734ret false
735end
736if eq k 0
737break
738end
739kn = ulong k
740i = ulong 0
741while lt i kn bound 2048
742c = get rdbuf i
743if ne c 13
744if ge tot 8192
745ret false
746end
747put inbuf tot c
748tot = add tot 1
749end
750i = add i 1
751end
752end
753put meta k_inlen tot
754ret true
755end
756
757fn flush_outbuf to void
758n = get meta k_outlen
759p = ulong 0
760while lt p n bound 8192
761k = write 1 outbuf p sub n p
762if le k 0
763exit 2
764end
765p = add p ulong k
766end
767end
768
769fn main to int
770set_defaults
771if not read_stdin
772wstr 2 e_io
773ret 2
774end
775parse_manifest
776if or eq get meta k_vlen 0 or eq get meta k_ulen 0 or eq get meta k_slen 0 eq get meta k_olen 0
777wstr 2 e_miss
778ret 2
779end
780if not valid_version
781wstr 2 e_ver
782ret 1
783end
784if not valid_url
785wstr 2 e_url
786ret 1
787end
788if not valid_sha_field 0
789wstr 2 e_sha
790ret 1
791end
792if not valid_sha_field 1
793wstr 2 e_omc
794ret 1
795end
796if not valid_homepage
797wstr 2 e_home
798ret 1
799end
800if not valid_license
801wstr 2 e_lic
802ret 1
803end
804emit_formula
805if not validate_emitted_formula
806wstr 2 e_self
807ret 1
808end
809flush_outbuf
810ret 0
811end