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 / site_builder / radicle_index.om

299 lines · 5514 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note apps/site_builder/radicle_index.om: pure-Omega Radicle repo index parser and validator
3note Validates rad:z<base58btc> RIDs, repository names, Git SHA-1/SHA-256 OIDs,
4note visibility tokens, tab-separated record structure, and canonical sorted index ordering.
5note Compose after si/directives/om2.om and apps/site_builder/html_feed.om.
6
7const RAD_OK ulong 0
8const RAD_ERR_RID_PREFIX ulong 1
9const RAD_ERR_RID_LEN ulong 2
10const RAD_ERR_RID_CHAR ulong 3
11const RAD_ERR_NAME ulong 4
12const RAD_ERR_OID ulong 5
13const RAD_ERR_VIS ulong 6
14const RAD_ERR_FIELDS ulong 7
15const RAD_ERR_DUPLICATE ulong 8
16const RAD_ERR_ORDER ulong 9
17
18text rad_pfx "rad:z"
19text rad_vis_pub "public"
20text rad_vis_priv "private"
21
22fn rad_is_b58 b uchar to bool
23if and ge b uchar 49 le b uchar 57
24ret true
25end
26if and ge b uchar 65 le b uchar 72
27ret true
28end
29if and ge b uchar 74 le b uchar 78
30ret true
31end
32if and ge b uchar 80 le b uchar 90
33ret true
34end
35if and ge b uchar 97 le b uchar 107
36ret true
37end
38if and ge b uchar 109 le b uchar 122
39ret true
40end
41ret false
42end
43
44fn rad_is_lhex b uchar to bool
45if and ge b uchar 48 le b uchar 57
46ret true
47end
48if and ge b uchar 97 le b uchar 102
49ret true
50end
51ret false
52end
53
54fn rad_is_name_ch b uchar to bool
55if and ge b uchar 97 le b uchar 122
56ret true
57end
58if and ge b uchar 48 le b uchar 57
59ret true
60end
61if or eq b uchar 45 or eq b uchar 46 eq b uchar 95
62ret true
63end
64ret false
65end
66
67fn rad_validate_rid rid view uchar * to ulong
68n = len rid
69if lt n ulong 5
70ret RAD_ERR_RID_PREFIX
71end
72if not sb_slice_eq part rid ulong 0 ulong 5 rad_pfx
73ret RAD_ERR_RID_PREFIX
74end
75body = sub n ulong 5
76if or lt body ulong 20 gt body ulong 48
77ret RAD_ERR_RID_LEN
78end
79i = ulong 5
80while lt i n bound 64
81if not rad_is_b58 get rid i
82ret RAD_ERR_RID_CHAR
83end
84i = add i ulong 1
85end
86ret RAD_OK
87end
88
89fn rad_validate_name name view uchar * to ulong
90n = len name
91if or eq n ulong 0 gt n ulong 64
92ret RAD_ERR_NAME
93end
94first = get name ulong 0
95if or eq first uchar 46 eq first uchar 45
96ret RAD_ERR_NAME
97end
98prev_dot = false
99i = ulong 0
100while lt i n bound 64
101b = get name i
102if not rad_is_name_ch b
103ret RAD_ERR_NAME
104end
105if eq b uchar 46
106if prev_dot
107ret RAD_ERR_NAME
108end
109prev_dot = true
110else
111prev_dot = false
112end
113i = add i ulong 1
114end
115ret RAD_OK
116end
117
118fn rad_validate_oid oid view uchar * to ulong
119n = len oid
120if and ne n ulong 40 ne n ulong 64
121ret RAD_ERR_OID
122end
123i = ulong 0
124while lt i n bound 64
125if not rad_is_lhex get oid i
126ret RAD_ERR_OID
127end
128i = add i ulong 1
129end
130ret RAD_OK
131end
132
133fn rad_validate_vis vis view uchar * to ulong
134if sb_slice_eq vis rad_vis_pub
135ret RAD_OK
136end
137if sb_slice_eq vis rad_vis_priv
138ret RAD_OK
139end
140ret RAD_ERR_VIS
141end
142
143fn rad_slice_cmp a view uchar * b view uchar * to int
144na = len a
145nb = len b
146ml = na
147if lt nb ml
148ml = nb
149end
150if gt ml ulong 128
151ret int 0
152end
153i = ulong 0
154while lt i ml bound 128
155ca = get a i
156cb = get b i
157if lt ca cb
158ret int -1
159end
160if gt ca cb
161ret int 1
162end
163i = add i ulong 1
164end
165if lt na nb
166ret int -1
167end
168if gt na nb
169ret int 1
170end
171ret int 0
172end
173
174fn rad_parse_record line view uchar * out_meta edit ulong 8 to ulong
175n = len line
176if or eq n ulong 0 gt n ulong 512
177ret RAD_ERR_FIELDS
178end
179tabs = new ulong 4 0
180tcnt = ulong 0
181i = ulong 0
182while lt i n bound 512
183if eq get line i uchar 9
184if ge tcnt ulong 3
185ret RAD_ERR_FIELDS
186end
187put tabs tcnt i
188tcnt = add tcnt ulong 1
189end
190i = add i ulong 1
191end
192if ne tcnt ulong 3
193ret RAD_ERR_FIELDS
194end
195t0 = get tabs ulong 0
196t1 = get tabs ulong 1
197t2 = get tabs ulong 2
198put out_meta ulong 0 ulong 0
199put out_meta ulong 1 t0
200put out_meta ulong 2 add t0 ulong 1
201put out_meta ulong 3 sub t1 add t0 ulong 1
202put out_meta ulong 4 add t1 ulong 1
203put out_meta ulong 5 sub t2 add t1 ulong 1
204put out_meta ulong 6 add t2 ulong 1
205put out_meta ulong 7 sub n add t2 ulong 1
206rc = rad_validate_rid part line ulong 0 t0
207if ne rc RAD_OK
208ret rc
209end
210rc = rad_validate_name part line get out_meta ulong 2 get out_meta ulong 3
211if ne rc RAD_OK
212ret rc
213end
214rc = rad_validate_oid part line get out_meta ulong 4 get out_meta ulong 5
215if ne rc RAD_OK
216ret rc
217end
218rc = rad_validate_vis part line get out_meta ulong 6 get out_meta ulong 7
219if ne rc RAD_OK
220ret rc
221end
222ret RAD_OK
223end
224
225fn rad_copy_rid dst edit uchar 64 src view uchar * to ulong
226ns = len src
227if gt ns ulong 64
228ret ulong 0
229end
230i = ulong 0
231while lt i ns bound 64
232put dst i get src i
233i = add i ulong 1
234end
235ret ns
236end
237
238fn rad_check_line line view uchar * prev_rid edit uchar 64 prev_len edit ulong 1 to ulong
239meta = new ulong 8 0
240rc = rad_parse_record line meta
241if ne rc RAD_OK
242ret rc
243end
244rlen = get meta ulong 1
245plen = get prev_len ulong 0
246if gt plen ulong 0
247c = rad_slice_cmp part prev_rid ulong 0 plen part line ulong 0 rlen
248if eq c int 0
249ret RAD_ERR_DUPLICATE
250end
251if gt c int 0
252ret RAD_ERR_ORDER
253end
254end
255nlen = rad_copy_rid prev_rid part line ulong 0 rlen
256put prev_len ulong 0 nlen
257ret RAD_OK
258end
259
260fn rad_parse_index buf view uchar * out_count edit ulong 1 to ulong
261n = len buf
262if gt n ulong 65536
263ret RAD_ERR_FIELDS
264end
265prev_rid = new uchar 64 0
266prev_len = new ulong 1 0
267cnt = ulong 0
268pos = ulong 0
269while lt pos n bound 65536
270start = pos
271while lt pos n bound 65536
272if eq get buf pos uchar 10
273break
274end
275pos = add pos ulong 1
276end
277endp = pos
278if gt endp start
279if eq get buf sub endp ulong 1 uchar 13
280endp = sub endp ulong 1
281end
282end
283llen = sub endp start
284if gt llen ulong 0
285if ne get buf start uchar 35
286rc = rad_check_line part buf start llen prev_rid prev_len
287if ne rc RAD_OK
288ret rc
289end
290cnt = add cnt ulong 1
291end
292end
293if lt pos n
294pos = add pos ulong 1
295end
296end
297put out_count ulong 0 cnt
298ret RAD_OK
299end