| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
#include <stdio.h> |
| 57 |
#include <stdlib.h> |
| 58 |
#include <string.h> |
| 59 |
#include <errno.h> |
| 60 |
#include <fcntl.h> |
| 61 |
#include <unistd.h> |
| 62 |
#include <sys/types.h> |
| 63 |
#include <sys/stat.h> |
| 64 |
#include <utime.h> |
| 65 |
#include "zlib.h" |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
#define local static |
| 70 |
|
| 71 |
|
| 72 |
#define SIZE 32768U |
| 73 |
#define PIECE 16384 |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
struct ind { |
| 78 |
int infile; |
| 79 |
unsigned char *inbuf; |
| 80 |
}; |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
local unsigned in(void *in_desc, unsigned char **buf) |
| 86 |
{ |
| 87 |
int ret; |
| 88 |
unsigned len; |
| 89 |
unsigned char *next; |
| 90 |
struct ind *me = (struct ind *)in_desc; |
| 91 |
|
| 92 |
next = me->inbuf; |
| 93 |
*buf = next; |
| 94 |
len = 0; |
| 95 |
do { |
| 96 |
ret = PIECE; |
| 97 |
if ((unsigned)ret > SIZE - len) |
| 98 |
ret = (int)(SIZE - len); |
| 99 |
ret = (int)read(me->infile, next, ret); |
| 100 |
if (ret == -1) { |
| 101 |
len = 0; |
| 102 |
break; |
| 103 |
} |
| 104 |
next += ret; |
| 105 |
len += ret; |
| 106 |
} while (ret != 0 && len < SIZE); |
| 107 |
return len; |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
struct outd { |
| 116 |
int outfile; |
| 117 |
int check; |
| 118 |
unsigned long crc; |
| 119 |
unsigned long total; |
| 120 |
}; |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
local int out(void *out_desc, unsigned char *buf, unsigned len) |
| 128 |
{ |
| 129 |
int ret; |
| 130 |
struct outd *me = (struct outd *)out_desc; |
| 131 |
|
| 132 |
if (me->check) { |
| 133 |
me->crc = crc32(me->crc, buf, len); |
| 134 |
me->total += len; |
| 135 |
} |
| 136 |
if (me->outfile != -1) |
| 137 |
do { |
| 138 |
ret = PIECE; |
| 139 |
if ((unsigned)ret > len) |
| 140 |
ret = (int)len; |
| 141 |
ret = (int)write(me->outfile, buf, ret); |
| 142 |
if (ret == -1) |
| 143 |
return 1; |
| 144 |
buf += ret; |
| 145 |
len -= ret; |
| 146 |
} while (len != 0); |
| 147 |
return 0; |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
#define NEXT() (have ? 0 : (have = in(indp, &next)), \ |
| 152 |
last = have ? (have--, (int)(*next++)) : -1) |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
unsigned char inbuf[SIZE]; |
| 158 |
unsigned char outbuf[SIZE]; |
| 159 |
unsigned short prefix[65536]; |
| 160 |
unsigned char suffix[65536]; |
| 161 |
unsigned char match[65280 + 2]; |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
#define FLUSHCODE() \ |
| 168 |
do { \ |
| 169 |
left = 0; \ |
| 170 |
rem = 0; \ |
| 171 |
if (chunk > have) { \ |
| 172 |
chunk -= have; \ |
| 173 |
have = 0; \ |
| 174 |
if (NEXT() == -1) \ |
| 175 |
break; \ |
| 176 |
chunk--; \ |
| 177 |
if (chunk > have) { \ |
| 178 |
chunk = have = 0; \ |
| 179 |
break; \ |
| 180 |
} \ |
| 181 |
} \ |
| 182 |
have -= chunk; \ |
| 183 |
next += chunk; \ |
| 184 |
chunk = 0; \ |
| 185 |
} while (0) |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
local int lunpipe(unsigned have, unsigned char *next, struct ind *indp, |
| 197 |
int outfile, z_stream *strm) |
| 198 |
{ |
| 199 |
int last; |
| 200 |
int chunk; |
| 201 |
int left; |
| 202 |
unsigned rem; |
| 203 |
int bits; |
| 204 |
unsigned code; |
| 205 |
unsigned mask; |
| 206 |
int max; |
| 207 |
int flags; |
| 208 |
unsigned end; |
| 209 |
unsigned temp; |
| 210 |
unsigned prev; |
| 211 |
unsigned final; |
| 212 |
unsigned stack; |
| 213 |
unsigned outcnt; |
| 214 |
struct outd outd; |
| 215 |
|
| 216 |
|
| 217 |
outd.outfile = outfile; |
| 218 |
outd.check = 0; |
| 219 |
|
| 220 |
|
| 221 |
flags = NEXT(); |
| 222 |
if (last == -1) |
| 223 |
return Z_BUF_ERROR; |
| 224 |
if (flags & 0x60) { |
| 225 |
strm->msg = (char *)"unknown lzw flags set"; |
| 226 |
return Z_DATA_ERROR; |
| 227 |
} |
| 228 |
max = flags & 0x1f; |
| 229 |
if (max < 9 || max > 16) { |
| 230 |
strm->msg = (char *)"lzw bits out of range"; |
| 231 |
return Z_DATA_ERROR; |
| 232 |
} |
| 233 |
if (max == 9) |
| 234 |
max = 10; |
| 235 |
flags &= 0x80; |
| 236 |
|
| 237 |
|
| 238 |
bits = 9; |
| 239 |
mask = 0x1ff; |
| 240 |
end = flags ? 256 : 255; |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
if (NEXT() == -1) |
| 245 |
return Z_OK; |
| 246 |
final = prev = (unsigned)last; |
| 247 |
if (NEXT() == -1) |
| 248 |
return Z_BUF_ERROR; |
| 249 |
if (last & 1) { |
| 250 |
strm->msg = (char *)"invalid lzw code"; |
| 251 |
return Z_DATA_ERROR; |
| 252 |
} |
| 253 |
rem = (unsigned)last >> 1; |
| 254 |
left = 7; |
| 255 |
chunk = bits - 2; |
| 256 |
outbuf[0] = (unsigned char)final; |
| 257 |
outcnt = 1; |
| 258 |
|
| 259 |
|
| 260 |
stack = 0; |
| 261 |
for (;;) { |
| 262 |
|
| 263 |
if (end >= mask && bits < max) { |
| 264 |
FLUSHCODE(); |
| 265 |
bits++; |
| 266 |
mask <<= 1; |
| 267 |
mask++; |
| 268 |
} |
| 269 |
|
| 270 |
|
| 271 |
if (chunk == 0) |
| 272 |
chunk = bits; |
| 273 |
code = rem; |
| 274 |
if (NEXT() == -1) { |
| 275 |
|
| 276 |
if (outcnt && out(&outd, outbuf, outcnt)) { |
| 277 |
strm->next_in = outbuf; |
| 278 |
return Z_BUF_ERROR; |
| 279 |
} |
| 280 |
return Z_OK; |
| 281 |
} |
| 282 |
code += (unsigned)last << left; |
| 283 |
left += 8; |
| 284 |
chunk--; |
| 285 |
if (bits > left) { |
| 286 |
if (NEXT() == -1) |
| 287 |
return Z_BUF_ERROR; |
| 288 |
code += (unsigned)last << left; |
| 289 |
left += 8; |
| 290 |
chunk--; |
| 291 |
} |
| 292 |
code &= mask; |
| 293 |
left -= bits; |
| 294 |
rem = (unsigned)last >> (8 - left); |
| 295 |
|
| 296 |
|
| 297 |
if (code == 256 && flags) { |
| 298 |
FLUSHCODE(); |
| 299 |
bits = 9; |
| 300 |
mask = 0x1ff; |
| 301 |
end = 255; |
| 302 |
continue; |
| 303 |
} |
| 304 |
|
| 305 |
|
| 306 |
temp = code; |
| 307 |
if (code > end) { |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
if (code != end + 1 || prev > end) { |
| 317 |
strm->msg = (char *)"invalid lzw code"; |
| 318 |
return Z_DATA_ERROR; |
| 319 |
} |
| 320 |
match[stack++] = (unsigned char)final; |
| 321 |
code = prev; |
| 322 |
} |
| 323 |
|
| 324 |
|
| 325 |
while (code >= 256) { |
| 326 |
match[stack++] = suffix[code]; |
| 327 |
code = prefix[code]; |
| 328 |
} |
| 329 |
match[stack++] = (unsigned char)code; |
| 330 |
final = code; |
| 331 |
|
| 332 |
|
| 333 |
if (end < mask) { |
| 334 |
end++; |
| 335 |
prefix[end] = (unsigned short)prev; |
| 336 |
suffix[end] = (unsigned char)final; |
| 337 |
} |
| 338 |
|
| 339 |
|
| 340 |
prev = temp; |
| 341 |
|
| 342 |
|
| 343 |
while (stack > SIZE - outcnt) { |
| 344 |
while (outcnt < SIZE) |
| 345 |
outbuf[outcnt++] = match[--stack]; |
| 346 |
if (out(&outd, outbuf, outcnt)) { |
| 347 |
strm->next_in = outbuf; |
| 348 |
return Z_BUF_ERROR; |
| 349 |
} |
| 350 |
outcnt = 0; |
| 351 |
} |
| 352 |
do { |
| 353 |
outbuf[outcnt++] = match[--stack]; |
| 354 |
} while (stack); |
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
} |
| 360 |
} |
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
local int gunpipe(z_stream *strm, int infile, int outfile) |
| 375 |
{ |
| 376 |
int ret, first, last; |
| 377 |
unsigned have, flags, len; |
| 378 |
unsigned char *next; |
| 379 |
struct ind ind, *indp; |
| 380 |
struct outd outd; |
| 381 |
|
| 382 |
|
| 383 |
ind.infile = infile; |
| 384 |
ind.inbuf = inbuf; |
| 385 |
indp = &ind; |
| 386 |
|
| 387 |
|
| 388 |
have = 0; |
| 389 |
first = 1; |
| 390 |
strm->next_in = Z_NULL; |
| 391 |
for (;;) { |
| 392 |
|
| 393 |
if (NEXT() == -1) { |
| 394 |
ret = Z_OK; |
| 395 |
break; |
| 396 |
} |
| 397 |
if (last != 31 || (NEXT() != 139 && last != 157)) { |
| 398 |
strm->msg = (char *)"incorrect header check"; |
| 399 |
ret = first ? Z_DATA_ERROR : Z_ERRNO; |
| 400 |
break; |
| 401 |
} |
| 402 |
first = 0; |
| 403 |
|
| 404 |
|
| 405 |
if (last == 157) { |
| 406 |
ret = lunpipe(have, next, indp, outfile, strm); |
| 407 |
break; |
| 408 |
} |
| 409 |
|
| 410 |
|
| 411 |
ret = Z_BUF_ERROR; |
| 412 |
if (NEXT() != 8) { |
| 413 |
if (last == -1) break; |
| 414 |
strm->msg = (char *)"unknown compression method"; |
| 415 |
ret = Z_DATA_ERROR; |
| 416 |
break; |
| 417 |
} |
| 418 |
flags = NEXT(); |
| 419 |
NEXT(); |
| 420 |
NEXT(); |
| 421 |
NEXT(); |
| 422 |
NEXT(); |
| 423 |
NEXT(); |
| 424 |
NEXT(); |
| 425 |
if (last == -1) break; |
| 426 |
if (flags & 0xe0) { |
| 427 |
strm->msg = (char *)"unknown header flags set"; |
| 428 |
ret = Z_DATA_ERROR; |
| 429 |
break; |
| 430 |
} |
| 431 |
if (flags & 4) { |
| 432 |
len = NEXT(); |
| 433 |
len += (unsigned)(NEXT()) << 8; |
| 434 |
if (last == -1) break; |
| 435 |
while (len > have) { |
| 436 |
len -= have; |
| 437 |
have = 0; |
| 438 |
if (NEXT() == -1) break; |
| 439 |
len--; |
| 440 |
} |
| 441 |
if (last == -1) break; |
| 442 |
have -= len; |
| 443 |
next += len; |
| 444 |
} |
| 445 |
if (flags & 8) |
| 446 |
while (NEXT() != 0 && last != -1) |
| 447 |
; |
| 448 |
if (flags & 16) |
| 449 |
while (NEXT() != 0 && last != -1) |
| 450 |
; |
| 451 |
if (flags & 2) { |
| 452 |
NEXT(); |
| 453 |
NEXT(); |
| 454 |
} |
| 455 |
if (last == -1) break; |
| 456 |
|
| 457 |
|
| 458 |
outd.outfile = outfile; |
| 459 |
outd.check = 1; |
| 460 |
outd.crc = crc32(0L, Z_NULL, 0); |
| 461 |
outd.total = 0; |
| 462 |
|
| 463 |
|
| 464 |
strm->next_in = next; |
| 465 |
strm->avail_in = have; |
| 466 |
ret = inflateBack(strm, in, indp, out, &outd); |
| 467 |
if (ret != Z_STREAM_END) break; |
| 468 |
next = strm->next_in; |
| 469 |
have = strm->avail_in; |
| 470 |
strm->next_in = Z_NULL; |
| 471 |
|
| 472 |
|
| 473 |
ret = Z_BUF_ERROR; |
| 474 |
if (NEXT() != (outd.crc & 0xff) || |
| 475 |
NEXT() != ((outd.crc >> 8) & 0xff) || |
| 476 |
NEXT() != ((outd.crc >> 16) & 0xff) || |
| 477 |
NEXT() != ((outd.crc >> 24) & 0xff)) { |
| 478 |
|
| 479 |
if (last != -1) { |
| 480 |
strm->msg = (char *)"incorrect data check"; |
| 481 |
ret = Z_DATA_ERROR; |
| 482 |
} |
| 483 |
break; |
| 484 |
} |
| 485 |
if (NEXT() != (outd.total & 0xff) || |
| 486 |
NEXT() != ((outd.total >> 8) & 0xff) || |
| 487 |
NEXT() != ((outd.total >> 16) & 0xff) || |
| 488 |
NEXT() != ((outd.total >> 24) & 0xff)) { |
| 489 |
|
| 490 |
if (last != -1) { |
| 491 |
strm->msg = (char *)"incorrect length check"; |
| 492 |
ret = Z_DATA_ERROR; |
| 493 |
} |
| 494 |
break; |
| 495 |
} |
| 496 |
|
| 497 |
|
| 498 |
} |
| 499 |
|
| 500 |
|
| 501 |
return ret; |
| 502 |
} |
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
local void copymeta(char *from, char *to) |
| 509 |
{ |
| 510 |
struct stat was; |
| 511 |
struct utimbuf when; |
| 512 |
|
| 513 |
|
| 514 |
if (stat(from, &was) != 0 || (was.st_mode & S_IFMT) != S_IFREG) |
| 515 |
return; |
| 516 |
|
| 517 |
|
| 518 |
(void)chmod(to, was.st_mode & 07777); |
| 519 |
|
| 520 |
|
| 521 |
(void)chown(to, was.st_uid, was.st_gid); |
| 522 |
|
| 523 |
|
| 524 |
when.actime = was.st_atime; |
| 525 |
when.modtime = was.st_mtime; |
| 526 |
(void)utime(to, &when); |
| 527 |
} |
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
local int gunzip(z_stream *strm, char *inname, char *outname, int test) |
| 540 |
{ |
| 541 |
int ret; |
| 542 |
int infile, outfile; |
| 543 |
|
| 544 |
|
| 545 |
if (inname == NULL || *inname == 0) { |
| 546 |
inname = "-"; |
| 547 |
infile = 0; |
| 548 |
} |
| 549 |
else { |
| 550 |
infile = open(inname, O_RDONLY, 0); |
| 551 |
if (infile == -1) { |
| 552 |
fprintf(stderr, "gun cannot open %s\n", inname); |
| 553 |
return 0; |
| 554 |
} |
| 555 |
} |
| 556 |
if (test) |
| 557 |
outfile = -1; |
| 558 |
else if (outname == NULL || *outname == 0) { |
| 559 |
outname = "-"; |
| 560 |
outfile = 1; |
| 561 |
} |
| 562 |
else { |
| 563 |
outfile = open(outname, O_CREAT | O_TRUNC | O_WRONLY, 0666); |
| 564 |
if (outfile == -1) { |
| 565 |
close(infile); |
| 566 |
fprintf(stderr, "gun cannot create %s\n", outname); |
| 567 |
return 0; |
| 568 |
} |
| 569 |
} |
| 570 |
errno = 0; |
| 571 |
|
| 572 |
|
| 573 |
ret = gunpipe(strm, infile, outfile); |
| 574 |
if (outfile > 2) close(outfile); |
| 575 |
if (infile > 2) close(infile); |
| 576 |
|
| 577 |
|
| 578 |
switch (ret) { |
| 579 |
case Z_OK: |
| 580 |
case Z_ERRNO: |
| 581 |
if (infile > 2 && outfile > 2) { |
| 582 |
copymeta(inname, outname); |
| 583 |
unlink(inname); |
| 584 |
} |
| 585 |
if (ret == Z_ERRNO) |
| 586 |
fprintf(stderr, "gun warning: trailing garbage ignored in %s\n", |
| 587 |
inname); |
| 588 |
break; |
| 589 |
case Z_DATA_ERROR: |
| 590 |
if (outfile > 2) unlink(outname); |
| 591 |
fprintf(stderr, "gun data error on %s: %s\n", inname, strm->msg); |
| 592 |
break; |
| 593 |
case Z_MEM_ERROR: |
| 594 |
if (outfile > 2) unlink(outname); |
| 595 |
fprintf(stderr, "gun out of memory error--aborting\n"); |
| 596 |
return 1; |
| 597 |
case Z_BUF_ERROR: |
| 598 |
if (outfile > 2) unlink(outname); |
| 599 |
if (strm->next_in != Z_NULL) { |
| 600 |
fprintf(stderr, "gun write error on %s: %s\n", |
| 601 |
outname, strerror(errno)); |
| 602 |
} |
| 603 |
else if (errno) { |
| 604 |
fprintf(stderr, "gun read error on %s: %s\n", |
| 605 |
inname, strerror(errno)); |
| 606 |
} |
| 607 |
else { |
| 608 |
fprintf(stderr, "gun unexpected end of file on %s\n", |
| 609 |
inname); |
| 610 |
} |
| 611 |
break; |
| 612 |
default: |
| 613 |
if (outfile > 2) unlink(outname); |
| 614 |
fprintf(stderr, "gun internal error--aborting\n"); |
| 615 |
return 1; |
| 616 |
} |
| 617 |
return 0; |
| 618 |
} |
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
int main(int argc, char **argv) |
| 623 |
{ |
| 624 |
int ret, len, test; |
| 625 |
char *outname; |
| 626 |
unsigned char *window; |
| 627 |
z_stream strm; |
| 628 |
|
| 629 |
|
| 630 |
window = match; |
| 631 |
strm.zalloc = Z_NULL; |
| 632 |
strm.zfree = Z_NULL; |
| 633 |
strm.opaque = Z_NULL; |
| 634 |
ret = inflateBackInit(&strm, 15, window); |
| 635 |
if (ret != Z_OK) { |
| 636 |
fprintf(stderr, "gun out of memory error--aborting\n"); |
| 637 |
return 1; |
| 638 |
} |
| 639 |
|
| 640 |
|
| 641 |
argc--; |
| 642 |
argv++; |
| 643 |
test = 0; |
| 644 |
if (argc && strcmp(*argv, "-h") == 0) { |
| 645 |
fprintf(stderr, "gun 1.3 (12 Jun 2005)\n"); |
| 646 |
fprintf(stderr, "Copyright (c) 2005 Mark Adler\n"); |
| 647 |
fprintf(stderr, "usage: gun [-t] [file1.gz [file2.Z ...]]\n"); |
| 648 |
return 0; |
| 649 |
} |
| 650 |
if (argc && strcmp(*argv, "-t") == 0) { |
| 651 |
test = 1; |
| 652 |
argc--; |
| 653 |
argv++; |
| 654 |
} |
| 655 |
if (argc) |
| 656 |
do { |
| 657 |
if (test) |
| 658 |
outname = NULL; |
| 659 |
else { |
| 660 |
len = (int)strlen(*argv); |
| 661 |
if (strcmp(*argv + len - 3, ".gz") == 0 || |
| 662 |
strcmp(*argv + len - 3, "-gz") == 0) |
| 663 |
len -= 3; |
| 664 |
else if (strcmp(*argv + len - 2, ".z") == 0 || |
| 665 |
strcmp(*argv + len - 2, "-z") == 0 || |
| 666 |
strcmp(*argv + len - 2, "_z") == 0 || |
| 667 |
strcmp(*argv + len - 2, ".Z") == 0) |
| 668 |
len -= 2; |
| 669 |
else { |
| 670 |
fprintf(stderr, "gun error: no gz type on %s--skipping\n", |
| 671 |
*argv); |
| 672 |
continue; |
| 673 |
} |
| 674 |
outname = malloc(len + 1); |
| 675 |
if (outname == NULL) { |
| 676 |
fprintf(stderr, "gun out of memory error--aborting\n"); |
| 677 |
ret = 1; |
| 678 |
break; |
| 679 |
} |
| 680 |
memcpy(outname, *argv, len); |
| 681 |
outname[len] = 0; |
| 682 |
} |
| 683 |
ret = gunzip(&strm, *argv, outname, test); |
| 684 |
if (outname != NULL) free(outname); |
| 685 |
if (ret) break; |
| 686 |
} while (argv++, --argc); |
| 687 |
else |
| 688 |
ret = gunzip(&strm, NULL, NULL, test); |
| 689 |
|
| 690 |
|
| 691 |
inflateBackEnd(&strm); |
| 692 |
return ret; |
| 693 |
} |