Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-zip/blob/main/tests/mkcases.om # omega-zip omega-zip: ZIP archive reader in seeded Omega (store and deflate, CRC-32, fail-closed refusals) Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-zip / tests / mkcases.om` 544 lines ยท 12437 bytes Raw Blame History 1 om2 2 note tests/mkcases.om: writes the hand-built DEFLATE and zlib streams and the patched zip files under 3 note corpus/refuse/ (refusal fixtures, and a few valid hand-built streams with their expected output). 4 note Build and run from the repository root with the pinned omc (README "Gate"): 5 note omc < tests/mkcases.om > build/mkcases && chmod +x build/mkcases && build/mkcases 6 note Deterministic: the same inputs give the same bytes. Each stream in the text `cases` is one line 7 note NAME KIND DATA. KIND b: DATA is a bit string in stream order ('0'/'1', spaces ignored), header 8 note fields least significant bit first, Huffman codes most significant bit first (RFC 1951 3.1.1), 9 note padded with zero bits to a whole byte. KIND h: DATA is hex bytes. 10 note The zip fixtures are patched copies of corpus/zip/base.zip, st.zip and dd.zip (made by Info-ZIP 11 note zip 3.0, corpus/SOURCES.txt), with the offsets of APPNOTE 4.3.7, 4.3.9, 4.3.12 and 4.3.16. 12 13 global bo uchar 65536 14 global ex uchar 65536 15 global fb uchar 65536 16 global pth uchar 512 17 note st: 0 bytes in bo, 1 bit buffer, 2 bits in it, 3 bytes in ex, 4 bytes in fb, 5 files written 18 global st ulong 8 19 global lb ushort 29 20 global lx uchar 29 21 global db ushort 30 22 global dx uchar 30 23 24 text tdir "corpus/refuse/" 25 text tzdir "corpus/zip/" 26 text tldir "corpus/zlib/" 27 text tfail "mkcases: cannot write or read " 28 text tnl "\n" 29 text tdone "mkcases: files written " 30 31 fn die k ulong to void 32 r = write 2 tfail 0 len tfail 33 r = write 2 pth 0 k 34 r = write 2 tnl 0 1 35 exit 1 36 end 37 38 note ---- paths: pth = DIR + one word of a word list ---- 39 fn pfx d view uchar * to ulong 40 for i ulong len d bound 256 41 put pth i get d i 42 end 43 ret len d 44 end 45 46 note word: append word k (0-based, space separated) of t at pth[o ..]; the new length. 47 fn word t view uchar * k ulong o ulong to ulong 48 w = ulong 0 49 p = o 50 inw = false 51 for i ulong len t bound 4096 52 c = get t i 53 if eq c 32 54 if inw 55 w = add w 1 56 end 57 inw = false 58 continue 59 end 60 inw = true 61 if eq w k 62 put pth p c 63 p = add p 1 64 end 65 end 66 ret p 67 end 68 69 note wbuf: buffer b (0 bo, 1 ex, 2 fb) to the file pth[0 .. k). 70 fn wbuf b ulong k ulong to void 71 fd = fopen pth 0 k 577 420 72 if lt fd 0 73 die k 74 end 75 n = get st 0 76 if eq b 1 77 n = get st 3 78 end 79 if eq b 2 80 n = get st 4 81 end 82 r = long 0 83 if eq b 0 84 r = write int fd bo 0 n 85 end 86 if eq b 1 87 r = write int fd ex 0 n 88 end 89 if eq b 2 90 r = write int fd fb 0 n 91 end 92 if ne r long n 93 die k 94 end 95 r = fclose int fd 96 put st 5 add get st 5 1 97 end 98 99 note rfb: fb = the file DIR + word k of t. 100 fn rfb d view uchar * t view uchar * k ulong to void 101 o = pfx d 102 n = word t k o 103 fd = fopen pth 0 n 0 0 104 if lt fd 0 105 die n 106 end 107 r = read int fd fb 0 65536 108 if lt r 0 109 die n 110 end 111 put st 4 ulong r 112 r = fclose int fd 113 end 114 115 note ---- bit writer (RFC 1951 3.1.1: bytes filled from the least significant bit) ---- 116 fn breset to void 117 put st 0 0 118 put st 1 0 119 put st 2 0 120 end 121 122 fn bput v ulong n ulong to void 123 b = get st 1 124 c = get st 2 125 for i ulong n bound 64 126 b = or b shl and shr v i 1 c 127 c = add c 1 128 if eq c 8 129 put bo get st 0 uchar b 130 put st 0 add get st 0 1 131 b = 0 132 c = 0 133 end 134 end 135 put st 1 b 136 put st 2 c 137 end 138 139 fn bcode v ulong n ulong to void 140 for i ulong n bound 32 141 bput and shr v sub sub n 1 i 1 1 142 end 143 end 144 145 fn bflush to void 146 if gt get st 2 0 147 put bo get st 0 uchar get st 1 148 put st 0 add get st 0 1 149 put st 1 0 150 put st 2 0 151 end 152 end 153 154 fn hexv c uchar to ulong 155 if le c 57 156 ret ulong sub c 48 157 end 158 ret ulong sub c 87 159 end 160 161 note The stream table. The common dynamic header (lines d-*, except d-repfirst, d-manylen, d-manydist, 162 note d-clinc, d-clover): HLIT 0 (257 codes), HCLEN 14, so 18 code-length code lengths in the order 163 note 16 17 18 0 8 7 9 6 10 5 11 4 12 3 13 2 14 1; symbols 0, 1, 2 and 18 get length 2, so their codes 164 note are 00, 01, 10 and 11. HDIST sits between (00000 one code, 10000 two, 01000 three). 165 note "11 `1111111`" is 138 zeros, "11 `0101011`" 117, "11 `0001011`" 115, "11 `0011011`" 119. 166 note d-repfirst: HCLEN 0, lengths 1 2 2 0 for 16 17 18 0 (codes 0, 10, 11), and the first symbol is 16. 167 note Fixed blocks: "1 10" is BFINAL 1, BTYPE 01; 'A' is `01110001`, length 3 is `0000001`, end of block 168 note `0000000`, distance codes are 5 bits. 169 text cases "d-litinc.raw b 1 01 00000 00000 0111 000 000 010 010 000 000 000 000 000 000 000 000 000 000 000 010 000 010 10 11 `1111111` 11 `0101011` 01 01\nd-litover.raw b 1 01 00000 00000 0111 000 000 010 010 000 000 000 000 000 000 000 000 000 000 000 010 000 010 01 01 01 11 `1111111` 11 `0001011` 01 01\nd-distover.raw b 1 01 00000 01000 0111 000 000 010 010 000 000 000 000 000 000 000 000 000 000 000 010 000 010 01 11 `1111111` 11 `0101011` 01 01 01 01\nd-distinc.raw b 1 01 00000 10000 0111 000 000 010 010 000 000 000 000 000 000 000 000 000 000 000 010 000 010 01 11 `1111111` 11 `0101011` 01 10 00\nd-noeob.raw b 1 01 00000 00000 0111 000 000 010 010 000 000 000 000 000 000 000 000 000 000 000 010 000 010 11 `1111111` 11 `0011011` 01\nd-reppast.raw b 1 01 00000 00000 0111 000 000 010 010 000 000 000 000 000 000 000 000 000 000 000 010 000 010 11 `1111111` 11 `1111111`\nd-repfirst.raw b 1 01 00000 00000 0000 100 010 010 000 0 00\nd-manylen.raw b 1 01 01111 00000 0000 `0000000000`\nd-manydist.raw b 1 01 00000 01111 0000 `0000000000`\nd-clinc.raw b 1 01 00000 00000 0000 100 000 000 000\nd-clover.raw b 1 01 00000 00000 0000 100 100 100 000\nf-lit286.raw b 1 10 `11000110`\nf-dist30.raw b 1 10 `01110001` `0000001` 11110\nf-far.raw b 1 10 `01110001` `0000001` 00001 `0000000`\nf-trunc.raw b 1 10 `01110001`\nok-fixed.raw b 1 10 `01110001` `0000001` 00000 `0000000`\nb-type3.raw b 1 11\ns-nlen.raw h 01 05 00 fa fe 68 65 6c 6c 6f\ns-trunc.raw h 01 05 00 fa ff 68 65\nok-stored.raw h 00 05 00 fa ff 68 65 6c 6c 6f 01 00 00 ff ff\ne-empty.raw h\nzl-fcheck.zlib h 78 9d 03 00 00 00 00 01\nzl-cinfo.zlib h 88 1c 03 00 00 00 00 01\nzl-method.zlib h 77 09 03 00 00 00 00 01\nzl-dict.zlib h 78 3f 00 00 00 01 03 00 00 00 00 01\nok-empty.zlib h 78 01 01 00 00 ff ff 00 00 00 01\n" 170 171 note streams: every line of `cases` into its file. 172 fn streams to void 173 o = pfx tdir 174 p = o 175 i = ulong 0 176 n = len cases 177 while lt i n bound 65536 178 c = get cases i 179 if eq c 32 180 i = add i 1 181 k = get cases i 182 i = add i 1 183 breset 184 v = ulong 0 185 h = ulong 0 186 while lt i n bound 4096 187 d = get cases i 188 if eq d 10 189 break 190 end 191 if eq k 98 192 if eq d 48 193 bput 0 1 194 end 195 if eq d 49 196 bput 1 1 197 end 198 else 199 if ne d 32 200 v = or shl v 4 hexv d 201 h = add h 1 202 if eq h 2 203 bput v 8 204 v = 0 205 h = 0 206 end 207 end 208 end 209 i = add i 1 210 end 211 bflush 212 wbuf 0 p 213 i = add i 1 214 p = o 215 continue 216 end 217 put pth p c 218 p = add p 1 219 i = add i 1 220 end 221 end 222 223 note ---- the all-codes stream ---- 224 fn flit s ulong to void 225 if lt s 144 226 bcode add s 48 8 227 ret 228 end 229 if lt s 256 230 bcode add s 256 9 231 ret 232 end 233 if lt s 280 234 bcode sub s 256 7 235 ret 236 end 237 bcode sub s 88 8 238 end 239 240 fn tables to void 241 b = ushort 3 242 for i ulong 28 243 e = ulong 0 244 if ge i 8 245 e = sub div i 4 1 246 end 247 put lb i b 248 put lx i uchar e 249 b = add b ushort shl ulong 1 e 250 end 251 put lb 28 258 252 put lx 28 0 253 d = ushort 1 254 for i ulong 30 255 e = ulong 0 256 if ge i 4 257 e = sub div i 2 1 258 end 259 put db i d 260 put dx i uchar e 261 d = add d ushort shl ulong 1 e 262 end 263 end 264 265 note allcodes: one fixed block: 33000 pseudo-random literals (8- and 9-bit codes), then 59 matches 266 note that use every length code 257..285 and every distance code 0..29, first with zero extra bits, 267 note then with the largest extra value (code 284 with 30, so the length stays inside 227..257), so the 268 note longest distance 32768 is used. ex gets the expected output, computed byte by byte. 269 note plen, pdist: the k-th match's length and distance codes with their extra bits (zero for k < 29, 270 note the largest value from k = 29 on); each returns its length or distance. 271 fn plen k ulong to ulong 272 lc = rem k 29 273 el = ulong get lx lc 274 a = ulong 0 275 if ge k 29 276 a = sub shl ulong 1 el 1 277 if eq lc 27 278 a = 30 279 end 280 end 281 flit add 257 lc 282 bput a el 283 ret add ulong get lb lc a 284 end 285 286 fn pdist k ulong to ulong 287 dc = rem k 30 288 ed = ulong get dx dc 289 b = ulong 0 290 if ge k 29 291 b = sub shl ulong 1 ed 1 292 end 293 bcode dc 5 294 bput b ed 295 ret add ulong get db dc b 296 end 297 298 fn allcodes to void 299 breset 300 bput 1 1 301 bput 1 2 302 x = ulong 12345 303 for i ulong 33000 304 x = and add mul x `1103515245` 12345 `2147483647` 305 v = and shr x 16 255 306 flit v 307 put ex i uchar v 308 end 309 o = ulong 33000 310 for k ulong 59 311 n = plen k 312 d = pdist k 313 for j ulong n bound 258 314 put ex o get ex sub o d 315 o = add o 1 316 end 317 end 318 flit 256 319 bflush 320 put st 3 o 321 end 322 323 note ---- zip patching (fb holds one zip without a comment; offsets per APPNOTE) ---- 324 fn le32f o ulong to ulong 325 a = or ulong get fb o shl ulong get fb add o 1 8 326 b = or shl ulong get fb add o 2 16 shl ulong get fb add o 3 24 327 ret or a b 328 end 329 330 fn put32f o ulong v ulong to void 331 put fb o uchar and v 255 332 put fb add o 1 uchar and shr v 8 255 333 put fb add o 2 uchar and shr v 16 255 334 put fb add o 3 uchar and shr v 24 255 335 end 336 337 fn eocd to ulong 338 ret sub get st 4 22 339 end 340 341 fn cdat to ulong 342 ret le32f add eocd 16 343 end 344 345 note setnm: word k of nnames as the first entry's name (local at 30, central at cd + 46). 346 text nnames "../evil.txt /a/evil.txt aa\\evil.txt ./aevil.txt" 347 fn setnm k ulong to void 348 n = word nnames k 0 349 c = cdat 350 for i ulong n bound 64 351 put fb add 30 i get pth i 352 put fb add add c 46 i get pth i 353 end 354 end 355 356 note both: a 32-bit field at local offset lo and central offset co set to v. 357 fn both lo ulong co ulong v ulong to void 358 put32f lo v 359 put32f add cdat co v 360 end 361 362 text zsrc "base.zip st.zip dd.zip" 363 text znames "z-dotdot.zip z-abs.zip z-bslash.zip z-dotpart.zip z-crypt.zip z-method.zip z-crc.zip z-local.zip z-big.zip z-short.zip z-long.zip z-disk.zip z-cdoff.zip z-lsig.zip z-count.zip z-comment.zip z-ssize.zip z-desc.zip z-lname.zip z-lmeth.zip z-lflag.zip z-ldata.zip z-desccs.zip z-count0.zip z-cdend.zip z-cs64.zip z-overlap.zip" 364 365 fn wz k ulong to void 366 o = pfx tdir 367 n = word znames k o 368 wbuf 2 n 369 end 370 371 note overlap: base.zip with its one central entry written twice; both point at the same local 372 note header and data, the overlap of D. Fifield's zip bomb. The end record moves up by the entry. 373 fn overlap to void 374 c = cdat 375 e = eocd 376 l = sub e c 377 for i ulong 22 378 put fb add add e l sub 21 i get fb add e sub 21 i 379 end 380 for i ulong l bound 4096 381 put fb add e i get fb add c i 382 end 383 put st 4 add get st 4 l 384 e = eocd 385 put fb add e 8 2 386 put fb add e 10 2 387 put32f add e 12 mul l 2 388 end 389 390 fn zips to void 391 for k ulong 4 392 rfb tzdir zsrc 0 393 setnm k 394 wz k 395 end 396 rfb tzdir zsrc 0 397 c = cdat 398 put fb 6 or get fb 6 1 399 put fb add c 8 or get fb add c 8 1 400 wz 4 401 rfb tzdir zsrc 0 402 c = cdat 403 put fb 8 12 404 put fb add c 10 12 405 wz 5 406 rfb tzdir zsrc 0 407 both 14 16 xor le32f 14 1 408 wz 6 409 rfb tzdir zsrc 0 410 put32f 22 add le32f 22 1 411 wz 7 412 rfb tzdir zsrc 0 413 both 22 24 `2147483647` 414 wz 8 415 rfb tzdir zsrc 0 416 both 22 24 sub le32f 22 1 417 wz 9 418 rfb tzdir zsrc 0 419 both 22 24 add le32f 22 1 420 wz 10 421 rfb tzdir zsrc 0 422 put fb add eocd 4 1 423 wz 11 424 rfb tzdir zsrc 0 425 e = eocd 426 put32f add e 16 add le32f add e 16 1 427 wz 12 428 rfb tzdir zsrc 0 429 put fb 0 81 430 wz 13 431 rfb tzdir zsrc 0 432 e = eocd 433 put fb add e 8 2 434 put fb add e 10 2 435 wz 14 436 rfb tzdir zsrc 0 437 e = eocd 438 put fb add e 20 1 439 wz 15 440 rfb tzdir zsrc 1 441 both 18 20 add le32f 18 1 442 wz 16 443 rfb tzdir zsrc 2 444 c = cdat 445 nl = or ulong get fb 26 shl ulong get fb 27 8 446 xl = or ulong get fb 28 shl ulong get fb 29 8 447 p = add add 30 add nl xl le32f add c 20 448 if eq le32f p `134695760` 449 p = add p 4 450 end 451 put32f p xor le32f p 1 452 wz 17 453 rfb tzdir zsrc 0 454 put fb 30 98 455 wz 18 456 rfb tzdir zsrc 0 457 put fb 8 0 458 wz 19 459 rfb tzdir zsrc 0 460 put fb 6 or get fb 6 8 461 wz 20 462 rfb tzdir zsrc 0 463 put fb 28 add get fb 28 1 464 wz 21 465 rfb tzdir zsrc 2 466 c = cdat 467 nl = or ulong get fb 26 shl ulong get fb 27 8 468 xl = or ulong get fb 28 shl ulong get fb 29 8 469 p = add add 30 add nl xl le32f add c 20 470 if eq le32f p `134695760` 471 p = add p 4 472 end 473 put32f add p 4 add le32f add p 4 1 474 wz 22 475 rfb tzdir zsrc 0 476 e = eocd 477 put fb add e 8 0 478 put fb add e 10 0 479 wz 23 480 rfb tzdir zsrc 0 481 c = cdat 482 put fb add c 32 22 483 e = eocd 484 put32f add e 12 add le32f add e 12 22 485 wz 24 486 rfb tzdir zsrc 0 487 c = cdat 488 put32f add c 20 `4294967295` 489 wz 25 490 rfb tzdir zsrc 0 491 overlap 492 wz 26 493 end 494 495 note ---- zlib fixtures from corpus/zlib/text.om.l6 ---- 496 text zlsrc "text.om.l6" 497 text zlnames "zl-adler.zlib zl-trunc.zlib" 498 499 fn zlibs to void 500 rfb tldir zlsrc 0 501 n = get st 4 502 put fb sub n 1 xor get fb sub n 1 1 503 o = pfx tdir 504 k = word zlnames 0 o 505 wbuf 2 k 506 rfb tldir zlsrc 0 507 put st 4 sub get st 4 2 508 o = pfx tdir 509 k = word zlnames 1 o 510 wbuf 2 k 511 end 512 513 text acnames "ok-allcodes.raw ok-allcodes.out" 514 515 fn main to int 516 tables 517 streams 518 allcodes 519 o = pfx tdir 520 k = word acnames 0 o 521 wbuf 0 k 522 o = pfx tdir 523 k = word acnames 1 o 524 wbuf 1 k 525 zlibs 526 zips 527 r = write 1 tdone 0 len tdone 528 b = new uchar 24 0 529 v = get st 5 530 for i ulong 20 531 put b sub 19 i uchar add rem v 10 48 532 v = div v 10 533 end 534 k = ulong 0 535 while lt k 19 bound 20 536 if ne get b k 48 537 break 538 end 539 k = add k 1 540 end 541 r = write 1 b k sub 20 k 542 r = write 1 tnl 0 1 543 ret 0 544 end