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