omega-zip
omega-zip: ZIP archive reader in seeded Omega (store and deflate, CRC-32, fail-closed refusals)
Brought by drakestapleton
| 1 | note ======== src/unzip.om: zip reader (PKWARE APPNOTE.TXT 6.3.10, sections 4.3 and 4.4) ======== |
| 2 | note Reads the end of central directory record (4.3.16), walks the central directory (4.3.12), |
| 3 | note checks each local header (4.3.7) and data descriptor (4.3.9) against it, and extracts methods |
| 4 | note 0 (stored) and 8 (deflated) with the CRC-32 checked. ZIP64 (4.3.14, 4.3.15), encryption |
| 5 | note (4.4.4 bits 0 and 6), split or spanned archives (4.3.16 disk numbers) are refused. |
| 6 | note Listing modes print exactly what Info-ZIP UnZip 6.0 prints for `unzip -l` and `unzip -v`. |
| 7 | |
| 8 | text tnozip "no end of central directory record (not a zip file)" |
| 9 | text tzip64 "ZIP64 archive: not yet supported" |
| 10 | text tdisk "split or spanned archive: not supported" |
| 11 | text tcrypt "encrypted entry: not supported" |
| 12 | text tmeth "compression method not supported (only 0 stored and 8 deflated)" |
| 13 | text tname "unsafe entry name (absolute, '..', '.', empty part, backslash or NUL)" |
| 14 | text tlong "name or output path longer than 4095 bytes" |
| 15 | text tlocal "local header disagrees with the central directory" |
| 16 | text tcrc "CRC-32 mismatch" |
| 17 | text tbound "entry larger than the size bound (1 GiB)" |
| 18 | text tnone "no entry with that name" |
| 19 | text tcdir "central directory is malformed" |
| 20 | text tssize "stored entry has different compressed and uncompressed sizes" |
| 21 | text tcreate "cannot create the output file (it may exist already)" |
| 22 | text tmkdir "cannot create a directory" |
| 23 | text tover "entries overlap (a possible zip bomb)" |
| 24 | |
| 25 | note hb: the header being read (central 46, local 30, end record 22, descriptor 16 bytes). |
| 26 | global hb uchar 64 |
| 27 | note nm: the current central directory name; want: the name asked for on the command line. |
| 28 | global nm uchar 4096 |
| 29 | global want uchar 4096 |
| 30 | global wantn ulong 1 |
| 31 | global wantf ulong 1 |
| 32 | global zfd ulong 1 |
| 33 | global zsize ulong 1 |
| 34 | global zeocd ulong 1 |
| 35 | global zclen ulong 1 |
| 36 | global cdoff ulong 1 |
| 37 | global cdsize ulong 1 |
| 38 | global cdn ulong 1 |
| 39 | global cdpos ulong 1 |
| 40 | global dirn ulong 1 |
| 41 | note the current central directory entry |
| 42 | global eflag ulong 1 |
| 43 | global emeth ulong 1 |
| 44 | global etime ulong 1 |
| 45 | global edate ulong 1 |
| 46 | global ecrc ulong 1 |
| 47 | global ecs ulong 1 |
| 48 | global eus ulong 1 |
| 49 | global enl ulong 1 |
| 50 | global eoff ulong 1 |
| 51 | note zspan: the bytes the entries need at least (local header, name, data), summed over the archive. |
| 52 | global zspan ulong 1 |
| 53 | |
| 54 | fn le16 o ulong to ulong |
| 55 | ret or ulong get hb o shl ulong get hb add o 1 8 |
| 56 | end |
| 57 | |
| 58 | fn le32 o ulong to ulong |
| 59 | ret or le16 o shl le16 add o 2 16 |
| 60 | end |
| 61 | |
| 62 | note zread: hb[0 .. n) from file offset off; false when the file ends first. |
| 63 | fn zread off ulong n ulong to bool |
| 64 | if gt add off n get zsize 0 |
| 65 | ret false |
| 66 | end |
| 67 | r = pread int get zfd 0 hb 0 n long off |
| 68 | if lt r 0 |
| 69 | fail 3 tread |
| 70 | ret false |
| 71 | end |
| 72 | ret eq ulong r n |
| 73 | end |
| 74 | |
| 75 | note ---- end of central directory ---- |
| 76 | note eocdat: true when a valid end record starts at p: signature, and the comment ends the file. |
| 77 | fn eocdat p ulong to bool |
| 78 | if not zread p 22 |
| 79 | ret false |
| 80 | end |
| 81 | if ne le32 0 101010256 |
| 82 | ret false |
| 83 | end |
| 84 | ret eq add add p 22 le16 20 get zsize 0 |
| 85 | end |
| 86 | |
| 87 | note findeocd: the last valid end record within the last 65557 bytes (22 + the longest comment). |
| 88 | note The tail is scanned backwards in 16 KiB reads that overlap by 3 bytes. |
| 89 | fn findeocd to ulong |
| 90 | size = get zsize 0 |
| 91 | if lt size 22 |
| 92 | fail 40 tnozip |
| 93 | ret 0 |
| 94 | end |
| 95 | lo = ulong 0 |
| 96 | if gt size 65557 |
| 97 | lo = sub size 65557 |
| 98 | end |
| 99 | ce = sub size 18 |
| 100 | while true bound 8 |
| 101 | cs = lo |
| 102 | if gt sub ce lo ISIZE |
| 103 | cs = sub ce ISIZE |
| 104 | end |
| 105 | r = pread int get zfd 0 ibuf 0 sub ce cs long cs |
| 106 | if ne r long sub ce cs |
| 107 | fail 3 tread |
| 108 | ret 0 |
| 109 | end |
| 110 | p = sub ce 4 |
| 111 | while ge p cs bound 16384 |
| 112 | q = sub p cs |
| 113 | if eq get ibuf q 80 |
| 114 | if eq get ibuf add q 1 75 |
| 115 | if eq get ibuf add q 2 5 |
| 116 | if eq get ibuf add q 3 6 |
| 117 | if eocdat p |
| 118 | ret p |
| 119 | end |
| 120 | end |
| 121 | end |
| 122 | end |
| 123 | end |
| 124 | if eq p 0 |
| 125 | break |
| 126 | end |
| 127 | p = sub p 1 |
| 128 | end |
| 129 | if eq cs lo |
| 130 | fail 40 tnozip |
| 131 | ret 0 |
| 132 | end |
| 133 | ce = add cs 3 |
| 134 | end |
| 135 | fail 40 tnozip |
| 136 | ret 0 |
| 137 | end |
| 138 | |
| 139 | fn zopen to void |
| 140 | s = fseek int get zfd 0 0 2 |
| 141 | if lt s 0 |
| 142 | fail 3 tread |
| 143 | end |
| 144 | put zsize 0 ulong s |
| 145 | p = findeocd |
| 146 | if ge p 20 |
| 147 | if zread sub p 20 4 |
| 148 | if eq le32 0 117853008 |
| 149 | fail 41 tzip64 |
| 150 | end |
| 151 | end |
| 152 | end |
| 153 | b = eocdat p |
| 154 | put zeocd 0 p |
| 155 | put zclen 0 le16 20 |
| 156 | if eq le16 10 65535 |
| 157 | fail 41 tzip64 |
| 158 | end |
| 159 | if eq le16 8 65535 |
| 160 | fail 41 tzip64 |
| 161 | end |
| 162 | if eq le32 12 4294967295 |
| 163 | fail 41 tzip64 |
| 164 | end |
| 165 | if eq le32 16 4294967295 |
| 166 | fail 41 tzip64 |
| 167 | end |
| 168 | if ne le16 4 0 |
| 169 | fail 42 tdisk |
| 170 | end |
| 171 | if ne le16 6 0 |
| 172 | fail 42 tdisk |
| 173 | end |
| 174 | if ne le16 8 le16 10 |
| 175 | fail 42 tdisk |
| 176 | end |
| 177 | put cdn 0 le16 10 |
| 178 | put cdsize 0 le32 12 |
| 179 | put cdoff 0 le32 16 |
| 180 | if gt add get cdoff 0 get cdsize 0 p |
| 181 | fail 50 tcdir |
| 182 | end |
| 183 | end |
| 184 | |
| 185 | note ---- central directory ---- |
| 186 | note cdnext: the entry at cdpos into eflag .. eoff and nm; cdpos moves past it. |
| 187 | fn cdnext to void |
| 188 | pos = get cdpos 0 |
| 189 | e = add get cdoff 0 get cdsize 0 |
| 190 | if gt add pos 46 e |
| 191 | fail 50 tcdir |
| 192 | end |
| 193 | if not zread pos 46 |
| 194 | fail 50 tcdir |
| 195 | end |
| 196 | if ne le32 0 33639248 |
| 197 | fail 50 tcdir |
| 198 | end |
| 199 | put eflag 0 le16 8 |
| 200 | put emeth 0 le16 10 |
| 201 | put etime 0 le16 12 |
| 202 | put edate 0 le16 14 |
| 203 | put ecrc 0 le32 16 |
| 204 | put ecs 0 le32 20 |
| 205 | put eus 0 le32 24 |
| 206 | n = le16 28 |
| 207 | put enl 0 n |
| 208 | put eoff 0 le32 42 |
| 209 | t = add add add pos 46 n add le16 30 le16 32 |
| 210 | if gt t e |
| 211 | fail 50 tcdir |
| 212 | end |
| 213 | if eq get ecs 0 4294967295 |
| 214 | fail 41 tzip64 |
| 215 | end |
| 216 | if eq get eus 0 4294967295 |
| 217 | fail 41 tzip64 |
| 218 | end |
| 219 | if eq get eoff 0 4294967295 |
| 220 | fail 41 tzip64 |
| 221 | end |
| 222 | if eq le16 34 65535 |
| 223 | fail 41 tzip64 |
| 224 | end |
| 225 | if ne le16 34 0 |
| 226 | fail 42 tdisk |
| 227 | end |
| 228 | if gt n NAMEMAX |
| 229 | fail 45 tlong |
| 230 | end |
| 231 | r = pread int get zfd 0 nm 0 n long add pos 46 |
| 232 | if ne r long n |
| 233 | fail 50 tcdir |
| 234 | end |
| 235 | put cdpos 0 t |
| 236 | end |
| 237 | |
| 238 | note cdcheck: walk the whole directory once: every record well formed, the count and size agree. |
| 239 | fn cdcheck to void |
| 240 | put cdpos 0 get cdoff 0 |
| 241 | for i ulong get cdn 0 bound 65535 |
| 242 | cdnext |
| 243 | end |
| 244 | if ne get cdpos 0 add get cdoff 0 get cdsize 0 |
| 245 | fail 50 tcdir |
| 246 | end |
| 247 | put cdpos 0 get cdoff 0 |
| 248 | end |
| 249 | |
| 250 | fn wanted to bool |
| 251 | if eq get wantf 0 0 |
| 252 | ret true |
| 253 | end |
| 254 | n = get enl 0 |
| 255 | if ne n get wantn 0 |
| 256 | ret false |
| 257 | end |
| 258 | for i ulong n bound 4096 |
| 259 | if ne get nm i get want i |
| 260 | ret false |
| 261 | end |
| 262 | end |
| 263 | ret true |
| 264 | end |
| 265 | |
| 266 | note nameok: a relative name of nonempty parts, none "." or "..", no backslash and no NUL byte; |
| 267 | note one trailing "/" marks a directory. |
| 268 | fn partok s ulong e ulong to bool |
| 269 | k = sub e s |
| 270 | if eq k 0 |
| 271 | ret false |
| 272 | end |
| 273 | if eq k 1 |
| 274 | if eq get nm s 46 |
| 275 | ret false |
| 276 | end |
| 277 | end |
| 278 | if eq k 2 |
| 279 | if eq get nm s 46 |
| 280 | if eq get nm add s 1 46 |
| 281 | ret false |
| 282 | end |
| 283 | end |
| 284 | end |
| 285 | ret true |
| 286 | end |
| 287 | |
| 288 | fn nameok to bool |
| 289 | n = get enl 0 |
| 290 | if eq n 0 |
| 291 | ret false |
| 292 | end |
| 293 | s = ulong 0 |
| 294 | for i ulong n bound 4096 |
| 295 | c = get nm i |
| 296 | if eq c 0 |
| 297 | ret false |
| 298 | end |
| 299 | if eq c 92 |
| 300 | ret false |
| 301 | end |
| 302 | if eq c 47 |
| 303 | if not partok s i |
| 304 | ret false |
| 305 | end |
| 306 | s = add i 1 |
| 307 | end |
| 308 | end |
| 309 | if lt s n |
| 310 | ret partok s n |
| 311 | end |
| 312 | ret true |
| 313 | end |
| 314 | |
| 315 | fn isdir to bool |
| 316 | n = get enl 0 |
| 317 | if eq n 0 |
| 318 | ret false |
| 319 | end |
| 320 | ret eq get nm sub n 1 47 |
| 321 | end |
| 322 | |
| 323 | note ---- listing (unzip -l and unzip -v) ---- |
| 324 | text tarch "Archive: " |
| 325 | text tlh1 " Length Date Time Name\n--------- ---------- ----- ----\n" |
| 326 | text tlf1 "--------- -------\n" |
| 327 | text tvh1 " Length Method Size Cmpr Date Time CRC-32 Name\n" |
| 328 | text tvh2 "-------- ------ ------- ---- ---------- ----- -------- ----\n" |
| 329 | text tvf1 "-------- ------- --- -------\n" |
| 330 | text tsp1 " " |
| 331 | text tsp2 " " |
| 332 | text tsp3 " " |
| 333 | text t10 " " |
| 334 | text t21 " " |
| 335 | text t28 " " |
| 336 | text tdash "-" |
| 337 | text tcolon ":" |
| 338 | text tfile " file\n" |
| 339 | text tfiles " files\n" |
| 340 | text tstored "Stored " |
| 341 | text tdefl "Defl:" |
| 342 | text tunk "Unk:" |
| 343 | text tmode "NXFS" |
| 344 | text tpct "%" |
| 345 | text t100 "100%" |
| 346 | |
| 347 | fn sayz v ulong k ulong to void |
| 348 | b = new uchar 24 0 |
| 349 | e = tx_fput v 10 k b 0 |
| 350 | r = write 1 b 0 ulong e |
| 351 | end |
| 352 | |
| 353 | fn saydate to void |
| 354 | d = get edate 0 |
| 355 | t = get etime 0 |
| 356 | sayz add 1980 shr d 9 4 |
| 357 | say tdash |
| 358 | sayz and shr d 5 15 2 |
| 359 | say tdash |
| 360 | sayz and d 31 2 |
| 361 | say tsp1 |
| 362 | sayz shr t 11 2 |
| 363 | say tcolon |
| 364 | sayz and shr t 5 63 2 |
| 365 | end |
| 366 | |
| 367 | note sayname: control bytes are shown as ^ and the byte plus 64, as UnZip does. |
| 368 | fn sayname to void |
| 369 | b = new uchar 8192 0 |
| 370 | o = ulong 0 |
| 371 | for i ulong get enl 0 bound 4096 |
| 372 | c = get nm i |
| 373 | if lt c 32 |
| 374 | put b o 94 |
| 375 | put b add o 1 add c 64 |
| 376 | o = add o 2 |
| 377 | else |
| 378 | put b o c |
| 379 | o = add o 1 |
| 380 | end |
| 381 | end |
| 382 | r = write 1 b 0 o |
| 383 | end |
| 384 | |
| 385 | note ratio: UnZip's compression factor in tenths of a percent, rounded half up. |
| 386 | fn ratio uc ulong c ulong to long |
| 387 | if eq uc 0 |
| 388 | ret 0 |
| 389 | end |
| 390 | dn = uc |
| 391 | mu = ulong 1000 |
| 392 | if gt uc 2000000 |
| 393 | dn = div uc 1000 |
| 394 | mu = 1 |
| 395 | end |
| 396 | h = shr dn 1 |
| 397 | if ge uc c |
| 398 | ret long div add mul mu sub uc c h dn |
| 399 | end |
| 400 | ret neg long div add mul mu sub c uc h dn |
| 401 | end |
| 402 | |
| 403 | note saypct: the 4-column percentage: "100%", else a sign or space, the percent and "%". |
| 404 | fn saypct uc ulong c ulong to void |
| 405 | r = ratio uc c |
| 406 | sg = false |
| 407 | if lt r 0 |
| 408 | sg = true |
| 409 | r = sub 0 r |
| 410 | end |
| 411 | f = ulong div add r 5 10 |
| 412 | if eq f 100 |
| 413 | say t100 |
| 414 | ret |
| 415 | end |
| 416 | k = add tx_ndig f 10 2 |
| 417 | while lt k 4 bound 4 |
| 418 | say tsp1 |
| 419 | k = add k 1 |
| 420 | end |
| 421 | if sg |
| 422 | say tdash |
| 423 | else |
| 424 | say tsp1 |
| 425 | end |
| 426 | saydec f 1 |
| 427 | say tpct |
| 428 | end |
| 429 | |
| 430 | fn saymeth to void |
| 431 | m = get emeth 0 |
| 432 | if eq m 0 |
| 433 | say tstored |
| 434 | ret |
| 435 | end |
| 436 | if eq m 8 |
| 437 | say tdefl |
| 438 | r = write 1 tmode and shr get eflag 0 1 3 1 |
| 439 | say tsp1 |
| 440 | ret |
| 441 | end |
| 442 | say tunk |
| 443 | sayz m 3 |
| 444 | end |
| 445 | |
| 446 | note saycomm: the archive comment and a newline, as UnZip prints it: a CR before a LF is dropped. |
| 447 | fn saycomm to void |
| 448 | n = get zclen 0 |
| 449 | if eq n 0 |
| 450 | ret |
| 451 | end |
| 452 | insrc get zfd 0 1 add get zeocd 0 22 n |
| 453 | cr = false |
| 454 | for i ulong n bound 65535 |
| 455 | c = inbyte |
| 456 | if cr |
| 457 | if ne c 10 |
| 458 | put win 0 13 |
| 459 | r = write 1 win 0 1 |
| 460 | end |
| 461 | end |
| 462 | cr = eq c 13 |
| 463 | if not cr |
| 464 | put win 0 uchar c |
| 465 | r = write 1 win 0 1 |
| 466 | end |
| 467 | end |
| 468 | if cr |
| 469 | put win 0 13 |
| 470 | r = write 1 win 0 1 |
| 471 | end |
| 472 | say tnl |
| 473 | end |
| 474 | |
| 475 | fn listing v bool ap view uchar * to int |
| 476 | cdcheck |
| 477 | say tarch |
| 478 | say ap |
| 479 | say tnl |
| 480 | saycomm |
| 481 | if v |
| 482 | say tvh1 |
| 483 | say tvh2 |
| 484 | else |
| 485 | say tlh1 |
| 486 | end |
| 487 | tu = ulong 0 |
| 488 | tc = ulong 0 |
| 489 | for i ulong get cdn 0 bound 65535 |
| 490 | cdnext |
| 491 | u = get eus 0 |
| 492 | c = get ecs 0 |
| 493 | if eq and get eflag 0 1 1 |
| 494 | if ge c 12 |
| 495 | c = sub c 12 |
| 496 | end |
| 497 | end |
| 498 | tu = add tu u |
| 499 | tc = add tc c |
| 500 | if v |
| 501 | saydec u 8 |
| 502 | say tsp2 |
| 503 | saymeth |
| 504 | saydec c 8 |
| 505 | say tsp1 |
| 506 | saypct u c |
| 507 | say tsp1 |
| 508 | saydate |
| 509 | say tsp1 |
| 510 | sayhex get ecrc 0 8 |
| 511 | say tsp2 |
| 512 | else |
| 513 | saydec u 9 |
| 514 | say tsp2 |
| 515 | saydate |
| 516 | say tsp3 |
| 517 | end |
| 518 | sayname |
| 519 | say tnl |
| 520 | end |
| 521 | if v |
| 522 | say tvf1 |
| 523 | saydec tu 8 |
| 524 | say t10 |
| 525 | saydec tc 7 |
| 526 | say tsp1 |
| 527 | saypct tu tc |
| 528 | say t28 |
| 529 | saydec get cdn 0 1 |
| 530 | else |
| 531 | say tlf1 |
| 532 | saydec tu 9 |
| 533 | say t21 |
| 534 | saydec get cdn 0 1 |
| 535 | end |
| 536 | if eq get cdn 0 1 |
| 537 | say tfile |
| 538 | else |
| 539 | say tfiles |
| 540 | end |
| 541 | ret 0 |
| 542 | end |
| 543 | |
| 544 | note ---- extraction ---- |
| 545 | note precheck: the central directory checks, for every selected entry, before any output. |
| 546 | fn precheck mode ulong to void |
| 547 | f = get eflag 0 |
| 548 | if ne and f 65 0 |
| 549 | fail 43 tcrypt |
| 550 | end |
| 551 | m = get emeth 0 |
| 552 | if ne m 0 |
| 553 | if ne m 8 |
| 554 | fail 44 tmeth |
| 555 | end |
| 556 | end |
| 557 | if gt get eus 0 ENTMAX |
| 558 | fail 48 tbound |
| 559 | end |
| 560 | if eq m 0 |
| 561 | if ne get ecs 0 get eus 0 |
| 562 | fail 52 tssize |
| 563 | end |
| 564 | end |
| 565 | if eq mode 1 |
| 566 | if not nameok |
| 567 | fail 45 tname |
| 568 | end |
| 569 | if gt add add get dirn 0 1 get enl 0 NAMEMAX |
| 570 | fail 45 tlong |
| 571 | end |
| 572 | end |
| 573 | end |
| 574 | |
| 575 | note localchk: the local header at eoff agrees with the central entry; the data offset. |
| 576 | fn localchk to ulong |
| 577 | o = get eoff 0 |
| 578 | if not zread o 30 |
| 579 | fail 46 tlocal |
| 580 | end |
| 581 | if ne le32 0 67324752 |
| 582 | fail 46 tlocal |
| 583 | end |
| 584 | if ne le16 8 get emeth 0 |
| 585 | fail 46 tlocal |
| 586 | end |
| 587 | if ne and le16 6 9 and get eflag 0 9 |
| 588 | fail 46 tlocal |
| 589 | end |
| 590 | n = le16 26 |
| 591 | if ne n get enl 0 |
| 592 | fail 46 tlocal |
| 593 | end |
| 594 | if eq and get eflag 0 8 0 |
| 595 | if ne le32 14 get ecrc 0 |
| 596 | fail 46 tlocal |
| 597 | end |
| 598 | if ne le32 18 get ecs 0 |
| 599 | fail 46 tlocal |
| 600 | end |
| 601 | if ne le32 22 get eus 0 |
| 602 | fail 46 tlocal |
| 603 | end |
| 604 | end |
| 605 | d = add add o 30 add n le16 28 |
| 606 | if gt add d get ecs 0 get cdoff 0 |
| 607 | fail 50 tcdir |
| 608 | end |
| 609 | r = pread int get zfd 0 ibuf 0 n long add o 30 |
| 610 | if ne r long n |
| 611 | fail 46 tlocal |
| 612 | end |
| 613 | for i ulong n bound 4096 |
| 614 | if ne get ibuf i get nm i |
| 615 | fail 46 tlocal |
| 616 | end |
| 617 | end |
| 618 | ret d |
| 619 | end |
| 620 | |
| 621 | note desc: with flag bit 3 the sizes and CRC follow the data (4.3.9), with or without the |
| 622 | note optional signature 0x08074b50; they must equal the central directory's. |
| 623 | fn desc d ulong to void |
| 624 | if eq and get eflag 0 8 0 |
| 625 | ret |
| 626 | end |
| 627 | p = add d get ecs 0 |
| 628 | k = ulong 0 |
| 629 | if zread p 4 |
| 630 | if eq le32 0 134695760 |
| 631 | k = 4 |
| 632 | end |
| 633 | end |
| 634 | if not zread add p k 12 |
| 635 | fail 46 tlocal |
| 636 | end |
| 637 | if ne le32 0 get ecrc 0 |
| 638 | fail 46 tlocal |
| 639 | end |
| 640 | if ne le32 4 get ecs 0 |
| 641 | fail 46 tlocal |
| 642 | end |
| 643 | if ne le32 8 get eus 0 |
| 644 | fail 46 tlocal |
| 645 | end |
| 646 | end |
| 647 | |
| 648 | note entout: one entry's data into the sink set by the caller; size and CRC checked. |
| 649 | fn entout to void |
| 650 | d = localchk |
| 651 | insrc get zfd 0 1 d get ecs 0 |
| 652 | if eq get emeth 0 0 |
| 653 | for i ulong get eus 0 bound ENTMAX |
| 654 | outb inbyte |
| 655 | end |
| 656 | wflush get wpos 0 |
| 657 | else |
| 658 | inflate |
| 659 | end |
| 660 | if ne get otot 0 get eus 0 |
| 661 | fail 53 tesize |
| 662 | end |
| 663 | if ne xor get crcv 0 4294967295 uint get ecrc 0 |
| 664 | fail 47 tcrc |
| 665 | end |
| 666 | desc d |
| 667 | end |
| 668 | |
| 669 | note outpath: opath = DIR "/" name (the DIR part is already in opath[0 .. dirn)). |
| 670 | fn outpath to void |
| 671 | b = get dirn 0 |
| 672 | put opath b 47 |
| 673 | for i ulong get enl 0 bound 4096 |
| 674 | put opath add add b 1 i get nm i |
| 675 | end |
| 676 | put opl 0 add add b 1 get enl 0 |
| 677 | end |
| 678 | |
| 679 | note mkparents: every directory on the way to the entry (each part ending in /), existing ones |
| 680 | note accepted. A directory entry (trailing /) creates the entry itself the same way. |
| 681 | fn mkparents to void |
| 682 | b = add get dirn 0 1 |
| 683 | n = get enl 0 |
| 684 | for i ulong n bound 4096 |
| 685 | if eq get nm i 47 |
| 686 | r = makedir opath 0 add b i 493 |
| 687 | if lt r 0 |
| 688 | if ne r -17 |
| 689 | fail 5 tmkdir |
| 690 | end |
| 691 | end |
| 692 | end |
| 693 | end |
| 694 | end |
| 695 | |
| 696 | fn extone mode ulong to void |
| 697 | if eq mode 0 |
| 698 | if isdir |
| 699 | ret |
| 700 | end |
| 701 | osink 1 get eus 0 1 1 |
| 702 | entout |
| 703 | ret |
| 704 | end |
| 705 | if eq mode 2 |
| 706 | osink -1 get eus 0 1 1 |
| 707 | entout |
| 708 | ret |
| 709 | end |
| 710 | outpath |
| 711 | if isdir |
| 712 | mkparents |
| 713 | osink -1 get eus 0 1 1 |
| 714 | entout |
| 715 | ret |
| 716 | end |
| 717 | mkparents |
| 718 | fd = fopen opath 0 get opl 0 32961 420 |
| 719 | if lt fd 0 |
| 720 | fail 5 tcreate |
| 721 | end |
| 722 | put ocl 0 1 |
| 723 | osink fd get eus 0 1 1 |
| 724 | entout |
| 725 | r = fclose int fd |
| 726 | put ocl 0 0 |
| 727 | if lt r 0 |
| 728 | fail 4 twrite |
| 729 | end |
| 730 | end |
| 731 | |
| 732 | note spanadd: the current entry's local header, name and data added to zspan. |
| 733 | fn spanadd to void |
| 734 | s = add get zspan 0 add 30 get enl 0 |
| 735 | put zspan 0 add s get ecs 0 |
| 736 | end |
| 737 | |
| 738 | note extract: mode 0 = to stdout (unzip -p), 1 = into the directory opath[0 .. dirn), 2 = test. |
| 739 | note Entries that do not overlap need at least zspan bytes before the central directory; more means |
| 740 | note two entries share data (D. Fifield's overlapping zip bomb), which UnZip also refuses: code 51. |
| 741 | fn extract mode ulong to int |
| 742 | cdcheck |
| 743 | k = ulong 0 |
| 744 | put zspan 0 0 |
| 745 | for i ulong get cdn 0 bound 65535 |
| 746 | cdnext |
| 747 | spanadd |
| 748 | if wanted |
| 749 | precheck mode |
| 750 | k = add k 1 |
| 751 | end |
| 752 | end |
| 753 | if eq k 0 |
| 754 | if eq get wantf 0 1 |
| 755 | fail 49 tnone |
| 756 | end |
| 757 | end |
| 758 | if gt get zspan 0 get cdoff 0 |
| 759 | fail 51 tover |
| 760 | end |
| 761 | put cdpos 0 get cdoff 0 |
| 762 | for i ulong get cdn 0 bound 65535 |
| 763 | cdnext |
| 764 | if wanted |
| 765 | p = get cdpos 0 |
| 766 | extone mode |
| 767 | put cdpos 0 p |
| 768 | end |
| 769 | end |
| 770 | ret 0 |
| 771 | end |