66 #include <sys/types.h> 94 struct ind *me = (
struct ind *)in_desc;
101 if ((
unsigned)ret >
SIZE - len)
102 ret = (
int)(
SIZE - len);
110 }
while (ret != 0 && len <
SIZE);
131 local int out(
void *out_desc,
unsigned char *buf,
unsigned len)
134 struct outd *me = (
struct outd *)out_desc;
143 if ((
unsigned)ret > len)
155 #define NEXT() (have ? 0 : (have = in(indp, &next)), \ 156 last = have ? (have--, (int)(*next++)) : -1) 171 #define FLUSHCODE() \ 175 if (chunk > have) { \ 181 if (chunk > have) { \ 230 strm->
msg = (
char *)
"unknown lzw flags set";
234 if (max < 9 || max > 16) {
235 strm->
msg = (
char *)
"lzw bits out of range";
245 end = flags ? 256 : 255;
251 final = prev = (unsigned)last;
255 strm->
msg = (
char *)
"invalid lzw code";
258 rem = (unsigned)last >> 1;
261 outbuf[0] = (
unsigned char)
final;
268 if (end >= mask && bits < max) {
281 if (outcnt &&
out(&outd,
outbuf, outcnt)) {
287 code += (unsigned)last << left;
293 code += (unsigned)last << left;
299 rem = (unsigned)last >> (8 - left);
302 if (code == 256 && flags) {
321 if (code != end + 1 || prev > end) {
322 strm->
msg = (
char *)
"invalid lzw code";
325 match[stack++] = (
unsigned char)
final;
331 while (code >= 256) {
336 match[stack++] = (
unsigned char)code;
350 while (stack >
SIZE - outcnt) {
351 while (outcnt <
SIZE)
352 outbuf[outcnt++] = match[--stack];
385 int ret, first, last;
386 unsigned have, flags, len;
387 z_const unsigned char *next = NULL;
388 struct ind ind, *indp;
406 if (last != 31 || (
NEXT() != 139 && last != 157)) {
407 strm->
msg = (
char *)
"incorrect header check";
415 ret =
lunpipe(have, next, indp, outfile, strm);
422 if (last == -1)
break;
423 strm->
msg = (
char *)
"unknown compression method";
434 if (last == -1)
break;
436 strm->
msg = (
char *)
"unknown header flags set";
442 len += (unsigned)(
NEXT()) << 8;
443 if (last == -1)
break;
447 if (
NEXT() == -1)
break;
450 if (last == -1)
break;
455 while (
NEXT() != 0 && last != -1)
458 while (
NEXT() != 0 && last != -1)
464 if (last == -1)
break;
483 if (
NEXT() != (
int)(outd.
crc & 0xff) ||
484 NEXT() != (
int)((outd.
crc >> 8) & 0xff) ||
485 NEXT() != (
int)((outd.
crc >> 16) & 0xff) ||
486 NEXT() != (
int)((outd.
crc >> 24) & 0xff)) {
489 strm->
msg = (
char *)
"incorrect data check";
494 if (
NEXT() != (int)(outd.
total & 0xff) ||
495 NEXT() != (int)((outd.
total >> 8) & 0xff) ||
496 NEXT() != (int)((outd.
total >> 16) & 0xff) ||
497 NEXT() != (int)((outd.
total >> 24) & 0xff)) {
500 strm->
msg = (
char *)
"incorrect length check";
523 if (stat(from, &was) != 0 || (was.st_mode & S_IFMT) != S_IFREG)
527 (void)chmod(to, was.st_mode & 07777);
530 (void)chown(to, was.st_uid, was.st_gid);
533 when.actime = was.st_atime;
534 when.modtime = was.st_mtime;
535 (void)utime(to, &when);
554 if (inname == NULL || *inname == 0) {
559 infile = open(inname, O_RDONLY, 0);
561 fprintf(stderr,
"gun cannot open %s\n", inname);
567 else if (outname == NULL || *outname == 0) {
572 outfile = open(outname, O_CREAT | O_TRUNC | O_WRONLY, 0666);
575 fprintf(stderr,
"gun cannot create %s\n", outname);
582 ret =
gunpipe(strm, infile, outfile);
583 if (outfile > 2) close(outfile);
584 if (infile > 2) close(infile);
590 if (infile > 2 && outfile > 2) {
595 fprintf(stderr,
"gun warning: trailing garbage ignored in %s\n",
599 if (outfile > 2) unlink(outname);
600 fprintf(stderr,
"gun data error on %s: %s\n", inname, strm->
msg);
603 if (outfile > 2) unlink(outname);
604 fprintf(stderr,
"gun out of memory error--aborting\n");
607 if (outfile > 2) unlink(outname);
609 fprintf(stderr,
"gun write error on %s: %s\n",
610 outname, strerror(errno));
613 fprintf(stderr,
"gun read error on %s: %s\n",
614 inname, strerror(errno));
617 fprintf(stderr,
"gun unexpected end of file on %s\n",
622 if (outfile > 2) unlink(outname);
623 fprintf(stderr,
"gun internal error--aborting\n");
631 int main(
int argc,
char **argv)
635 unsigned char *window;
645 fprintf(stderr,
"gun out of memory error--aborting\n");
653 if (argc && strcmp(*argv,
"-h") == 0) {
654 fprintf(stderr,
"gun 1.6 (17 Jan 2010)\n");
655 fprintf(stderr,
"Copyright (C) 2003-2010 Mark Adler\n");
656 fprintf(stderr,
"usage: gun [-t] [file1.gz [file2.Z ...]]\n");
659 if (argc && strcmp(*argv,
"-t") == 0) {
669 len = (int)strlen(*argv);
670 if (strcmp(*argv + len - 3,
".gz") == 0 ||
671 strcmp(*argv + len - 3,
"-gz") == 0)
673 else if (strcmp(*argv + len - 2,
".z") == 0 ||
674 strcmp(*argv + len - 2,
"-z") == 0 ||
675 strcmp(*argv + len - 2,
"_z") == 0 ||
676 strcmp(*argv + len - 2,
".Z") == 0)
679 fprintf(stderr,
"gun error: no gz type on %s--skipping\n",
683 outname =
malloc(len + 1);
684 if (outname == NULL) {
685 fprintf(stderr,
"gun out of memory error--aborting\n");
689 memcpy(outname, *argv, len);
692 ret =
gunzip(&strm, *argv, outname, test);
693 if (outname != NULL)
free(outname);
695 }
while (argv++, --argc);
697 ret =
gunzip(&strm, NULL, NULL, test);
unsigned char match[65280+2]
AdjacencyIterator end(const Adjacency &adj, const Vector< D > &vct, size_t pixel_index)
Returns an iterator to position after the end of elements.
static int lunpipe(unsigned have, z_const unsigned char *next, struct ind *indp, int outfile, z_stream *strm)
unsigned short prefix[65536]
unsigned long crc32(unsigned long crc, unsigned char *buf, uInt len)
int write(ozstream &zs, const T *x, Items items)
static int gunzip(z_stream *strm, char *inname, char *outname, int test)
static void copymeta(char *from, char *to)
static int out(void *out_desc, unsigned char *buf, unsigned len)
int inflateBack(z_streamp strm, in_func in, void *in_desc, out_func out, void *out_desc)
unsigned char suffix[65536]
int read(izstream &zs, T *x, Items items)
int inflateBackEnd(z_streamp strm)
unsigned char outbuf[32768U]
int main(int argc, char **argv)
static int gunpipe(z_stream *strm, int infile, int outfile)
static unsigned in(void *in_desc, z_const unsigned char **buf)
static int bits(struct state *s, int need)
#define inflateBackInit(strm, windowBits, window)