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 / tools / bring / src / 30tar.om

338 lines · 5865 bytes Raw Blame History
1note Deterministic POSIX ustar: regular files only; uid/gid/mtime zero; no extension headers.
2text tustar "ustar"
3text ttarbad "invalid or unsafe POSIX ustar archive"
4text tlinksk "Skipped a link (links are never created): "
5text texists "destination already exists or cannot be created"
6fn octput b edit uchar * off ulong width ulong v ulong to void
7x = v
8for i ulong sub width 1 bound 9223372036854775807
9p = sub add off width add i 2
10d = uchar add 48 and x 7
11put b p d
12x = shr x 3
13end
14if ne x 0
15die ttarbad
16end
17put b sub add off width 1 0
18end
19fn tarhead rec view uchar * h edit uchar * to void
20for i ulong 512
21put h i 0
22end
23n = rd16 rec 0
24split = ulong 0
25if gt n 100
26for i ulong n bound 255
27if eq get rec add 32 i 47
28if and le i 155 le sub sub n i 1 100
29split = i
30end
31end
32end
33if eq split 0
34die ttarbad
35end
36for i ulong split bound 155
37put h add 345 i get rec add 32 i
38end
39split = add split 1
40end
41for i ulong sub n split bound 100
42put h i get rec add 32 add split i
43end
44octput h 100 8 rd64 rec 8
45octput h 108 8 0
46octput h 116 8 0
47octput h 124 12 rd64 rec 16
48octput h 136 12 0
49for i ulong 8
50put h add 148 i 32
51end
52put h 156 48
53for i ulong 5
54put h add 257 i get tustar i
55end
56put h 263 48
57put h 264 48
58sum = ulong 0
59for i ulong 512
60sum = add sum ulong get h i
61end
62octput h 148 7 sum
63put h 155 32
64end
65fn copybytes src int dst int n ulong to void
66b = new uchar 65536 0
67left = n
68while gt left 0 bound 9223372036854775807
69k = left
70if gt k len b
71k = len b
72end
73if not exactread src part b 0 k
74die terrio
75end
76mustwrite dst part b 0 k
77left = sub left k
78end
79end
80fn archive dst int to void
81fd = opensorted
82src = topen tkeep 0
83for i ulong get gcount 0 bound 9223372036854775807
84archone fd src dst
85end
86z = new uchar 512 0
87mustwrite dst z
88mustwrite dst z
89pl_close src
90pl_close fd
91end
92fn archone fd int src int dst int to void
93rec = new uchar 288 0
94h = new uchar 512 0
95if not exactread fd rec
96die terrio
97end
98tarhead rec h
99mustwrite dst h
100r = pl_seek src rd64 rec 24
101if lt r 0
102die terrio
103end
104sz = rd64 rec 16
105copybytes src dst sz
106pad = rem sub 512 rem sz 512 512
107for i ulong 512
108put h i 0
109end
110mustwrite dst part h 0 pad
111end
112fn octread b view uchar * off ulong width ulong to ulong
113v = ulong 0
114ended = false
115for i ulong width bound 12
116c = get b add off i
117if or eq c 0 eq c 32
118ended = true
119else
120if or ended or lt c 48 gt c 55
121die ttarbad
122end
123v = add mul v 8 ulong sub c 48
124end
125end
126ret v
127end
128fn allzero b view uchar * to bool
129for i ulong len b bound 9223372036854775807
130if ne get b i 0
131ret false
132end
133end
134ret true
135end
136fn pathok p view uchar * to bool
137if or eq len p 0 gt len p 255
138ret false
139end
140start = ulong 0
141for i ulong add len p 1 bound 256
142sep = eq i len p
143if not sep
144c = get p i
145if eq c 0
146ret false
147end
148sep = eq c 47
149end
150if sep
151n = sub i start
152if eq n 0
153ret false
154end
155if or beq part p start n tdot beq part p start n tdotdot
156ret false
157end
158if isgit part p start n
159ret false
160end
161start = add i 1
162end
163end
164ret true
165end
166fn fieldlen b view uchar * off ulong n ulong to ulong
167for i ulong n bound 9223372036854775807
168if eq get b add off i 0
169ret i
170end
171end
172ret n
173end
174fn tarpath h view uchar * p edit uchar * isdir bool to ulong
175n = fieldlen h 345 155
176k = ulong 0
177if gt n 0
178k = cpy p 0 part h 345 n
179put p k 47
180k = add k 1
181end
182n = fieldlen h 0 100
183k = cpy p k part h 0 n
184if and isdir gt k 1
185if eq get p sub k 1 47
186k = sub k 1
187end
188end
189if not pathok part p 0 k
190die ttarbad
191end
192ret k
193end
194fn checkhead h view uchar * to ulong
195if not bat h 257 tustar
196die ttarbad
197end
198if or ne get h 262 0 or ne get h 263 48 ne get h 264 48
199die ttarbad
200end
201v = octread h 148 8
202sum = ulong 0
203for i ulong 512
204if and ge i 148 lt i 156
205sum = add sum 32
206else
207sum = add sum ulong get h i
208end
209end
210if ne sum v
211die ttarbad
212end
213ret octread h 124 12
214end
215note Validate first, then unpack into a newly created private directory. Never follow archive links.
216note Parent components are created by us; relative path validation runs before any creation.
217fn tarwalk fd int unpack bool to void
218h = new uchar 512 0
219p = new uchar 256 0
220z = new uchar 512 0
221while true bound 9223372036854775807
222if not exactread fd h
223die ttarbad
224end
225if allzero h
226if not exactread fd z
227die ttarbad
228end
229if not allzero z
230die ttarbad
231end
232while true bound 9223372036854775807
233r = read fd z 0 512
234if lt r 0
235die terrio
236end
237if eq r 0
238ret
239end
240if not allzero part z 0 ulong r
241die ttarbad
242end
243end
244end
245sz = checkhead h
246tarent fd h sz unpack
247pad = rem sub 512 rem sz 512 512
248if not exactread fd part z 0 pad
249die ttarbad
250end
251if not allzero part z 0 pad
252die ttarbad
253end
254end
255end
256note tarent: one entry. '0'/NUL regular file (0644, or 0755 when the owner exec bit is set), '5'
257note directory, 'g' a pax global header (git archive's commit comment; skipped, never applied),
258note '1'/'2' links (never created; listed as skipped). Anything else refuses the archive.
259fn tarent fd int h view uchar * sz ulong unpack bool to void
260t = get h 156
261if eq t 103
262skipdata fd sz
263ret
264end
265p = new uchar 256 0
266if eq t 53
267if not allzero part h 157 100
268die ttarbad
269end
270n = tarpath h p true
271if ne sz 0
272die ttarbad
273end
274if unpack
275put p n 47
276makeparents part p 0 add n 1
277end
278ret
279end
280if or eq t 49 eq t 50
281n = tarpath h p false
282if unpack
283say tlinksk
284sayp part p 0 n
285say tnl
286end
287skipdata fd sz
288ret
289end
290if and ne t 48 ne t 0
291die ttarbad
292end
293if not allzero part h 157 100
294die ttarbad
295end
296n = tarpath h p false
297if not unpack
298skipdata fd sz
299ret
300end
301makeparents part p 0 n
302md = int 420
303if ne and octread h 100 8 64 0
304md = int 493
305end
306of = pl_open part p 0 n 3 md
307if lt of 0
308die texists
309end
310copybytes fd int of sz
311pl_close int of
312end
313fn skipdata fd int sz ulong to void
314r = pl_skip fd sz
315if lt r 0
316die terrio
317end
318end
319fn makeparents p view uchar * to void
320st = new uchar 256 0
321for i ulong len p bound 255
322if eq get p i 47
323r = pl_mkdir part p 0 i 448
324if ne r 0
325if ne r -17
326die texists
327end
328r = pl_stat part p 0 i st
329if lt r 0
330die texists
331end
332if ne and pl_mode st 61440 16384
333die texists
334end
335end
336end
337end
338end