home *** CD-ROM | disk | FTP | other *** search
- *** funzip.c.51d3 Tue Feb 2 13:48:00 1993
- --- funzip.c Tue May 11 13:47:12 1993
- ***************
- *** 1,6 ****
- ! /* funzip.c -- Not copyrighted 1992 by Mark Adler */
-
- ! #define VERSION "2.3 of 14 December 1992"
-
-
- /* You can do whatever you like with this source file, though I would
- --- 1,6 ----
- ! /* funzip.c -- Not copyrighted 1992,1993 by Mark Adler */
-
- ! #define VERSION "3.0 of 11 May 1993"
-
-
- /* You can do whatever you like with this source file, though I would
- ***************
- *** 39,44 ****
- --- 39,45 ----
- 2.3 14 Dec 92 M. Adler replaced fseek (fails on stdin for SCO
- Unix V.3.2.4). added quietflg for
- inflate.c.
- + 3.0 11 May 93 M. Adler added gzip support
- */
-
-
- ***************
- *** 72,91 ****
- #endif
-
- /* PKZIP header definitions */
- ! #define LOCSIG 0x04034b50L /* four-byte lead-in (lsb first) */
- ! #define LOCFLG 6 /* offset of bit flag */
- #define CRPFLG 1 /* bit for encrypted entry */
- #define EXTFLG 8 /* bit for extended local header */
- ! #define LOCHOW 8 /* offset of compression method */
- ! #define LOCTIM 10 /* file mod time (for decryption) */
- ! #define LOCCRC 14 /* offset of crc */
- ! #define LOCSIZ 18 /* offset of compressed size */
- ! #define LOCLEN 22 /* offset of uncompressed length */
- ! #define LOCFIL 26 /* offset of file name field length */
- ! #define LOCEXT 28 /* offset of extra field length */
- ! #define LOCHDR 30 /* size of local header, including sig */
- #define EXTHDR 16 /* size of extended local header, inc sig */
-
- /* Macros for getting two-byte and four-byte header values */
- #define SH(p) ((ush)(uch)((p)[0]) | ((ush)(uch)((p)[1]) << 8))
- #define LG(p) ((ulg)(SH(p)) | ((ulg)(SH((p)+2)) << 16))
- --- 73,108 ----
- #endif
-
- /* PKZIP header definitions */
- ! #define ZIPMAG 0x4b50 /* two-byte zip lead-in */
- ! #define LOCREM 0x0403 /* remaining two bytes in zip signature */
- ! #define LOCSIG 0x04034b50L /* full signature */
- ! #define LOCFLG 4 /* offset of bit flag */
- #define CRPFLG 1 /* bit for encrypted entry */
- #define EXTFLG 8 /* bit for extended local header */
- ! #define LOCHOW 6 /* offset of compression method */
- ! #define LOCTIM 8 /* file mod time (for decryption) */
- ! #define LOCCRC 12 /* offset of crc */
- ! #define LOCSIZ 16 /* offset of compressed size */
- ! #define LOCLEN 20 /* offset of uncompressed length */
- ! #define LOCFIL 24 /* offset of file name field length */
- ! #define LOCEXT 26 /* offset of extra field length */
- ! #define LOCHDR 28 /* size of local header, including LOCREM */
- #define EXTHDR 16 /* size of extended local header, inc sig */
-
- + /* GZIP header definitions */
- + #define GZPMAG 0x8b1f /* two-byte gzip lead-in */
- + #define GZPHOW 0 /* offset of method number */
- + #define GZPFLG 1 /* offset of gzip flags */
- + #define GZPMUL 2 /* bit for multiple-part gzip file */
- + #define GZPISX 4 /* bit for extra field present */
- + #define GZPISF 8 /* bit for filename present */
- + #define GZPISC 16 /* bit for comment present */
- + #define GZPISE 32 /* bit for encryption */
- + #define GZPTIM 2 /* offset of Unix file modification time */
- + #define GZPEXF 6 /* offset of extra flags */
- + #define GZPCOS 7 /* offset of operating system compressed on */
- + #define GZPHDR 8 /* length of minimal gzip header */
- +
- /* Macros for getting two-byte and four-byte header values */
- #define SH(p) ((ush)(uch)((p)[0]) | ((ush)(uch)((p)[1]) << 8))
- #define LG(p) ((ulg)(SH(p)) | ((ulg)(SH((p)+2)) << 16))
- ***************
- *** 244,250 ****
- /* Given a zip file on stdin, decompress the first entry to stdout. */
- {
- ush n;
- ! uch h[LOCHDR]; /* first local header */
- #ifdef CRYPT
- char *s = " [-password]";
- char *p; /* password */
- --- 261,268 ----
- /* Given a zip file on stdin, decompress the first entry to stdout. */
- {
- ush n;
- ! uch h[LOCHDR]; /* first local header (GZPHDR < LOCHDR) */
- ! int g; /* true if gzip format */
- #ifdef CRYPT
- char *s = " [-password]";
- char *p; /* password */
- ***************
- *** 273,280 ****
- fprintf(stderr, "usage: ... | funzip%s | ...\n", s);
- fprintf(stderr, " ... | funzip%s > outfile\n", s);
- fprintf(stderr, " funzip%s infile.zip > outfile\n", s);
- fprintf(stderr,
- ! " extracts to stdout the first zip entry of stdin.\n");
- exit(3);
- }
-
- --- 291,299 ----
- fprintf(stderr, "usage: ... | funzip%s | ...\n", s);
- fprintf(stderr, " ... | funzip%s > outfile\n", s);
- fprintf(stderr, " funzip%s infile.zip > outfile\n", s);
- + fprintf(stderr, " funzip%s infile.z > outfile\n", s);
- fprintf(stderr,
- ! " extracts to stdout the gzip file or first zip entry of stdin.\n");
- exit(3);
- }
-
- ***************
- *** 301,315 ****
- err(2, "cannot write to stdout");
-
- /* read local header, check validity, and skip name and extra fields */
- ! if (fread((char *)h, 1, LOCHDR, in) != LOCHDR || LG(h) != LOCSIG)
- ! err(3, "input not a zip file or empty");
- ! if (SH(h + LOCHOW) != STORED && SH(h + LOCHOW) != DEFLATED)
- ! err(3, "first entry not deflated or stored--can't funzip");
- ! for (n = SH(h + LOCFIL); n--; ) getc(in);
- ! for (n = SH(h + LOCEXT); n--; ) getc(in);
-
- /* if entry encrypted, decrypt and validate encryption header */
- ! if ((decrypt = h[LOCFLG] & CRPFLG) != 0)
- #ifdef CRYPT
- {
- ush i, e;
- --- 320,362 ----
- err(2, "cannot write to stdout");
-
- /* read local header, check validity, and skip name and extra fields */
- ! n = getc(in); n |= getc(in) << 8;
- ! if (n == ZIPMAG)
- ! {
- ! if (fread((char *)h, 1, LOCHDR, in) != LOCHDR || SH(h) != LOCREM)
- ! err(3, "invalid zip file");
- ! if (SH(h + LOCHOW) != STORED && SH(h + LOCHOW) != DEFLATED)
- ! err(3, "first entry not deflated or stored--can't funzip");
- ! for (n = SH(h + LOCFIL); n--; ) getc(in);
- ! for (n = SH(h + LOCEXT); n--; ) getc(in);
- ! g = 0;
- ! decrypt = h[LOCFLG] & CRPFLG;
- ! }
- ! else if (n == GZPMAG)
- ! {
- ! if (fread((char *)h, 1, GZPHDR, in) != GZPHDR)
- ! err(3, "invalid gzip file");
- ! if (h[GZPHOW] != DEFLATED)
- ! err(3, "gzip file not deflated");
- ! if (h[GZPFLG] & GZPMUL)
- ! err(3, "cannot handle multi-part gzip files");
- ! if (h[GZPFLG] & GZPISX)
- ! {
- ! n = getc(in); n |= getc(in) << 8;
- ! while (n--) getc(in);
- ! }
- ! if (h[GZPFLG] & GZPISF)
- ! while ((g = getc(in)) != 0 && g != EOF) ;
- ! if (h[GZPFLG] & GZPISC)
- ! while ((g = getc(in)) != 0 && g != EOF) ;
- ! g = 1;
- ! decrypt = h[GZPFLG] & GZPISE;
- ! }
- ! else
- ! err(3, "input not a zip or gzip file");
-
- /* if entry encrypted, decrypt and validate encryption header */
- ! if (decrypt)
- #ifdef CRYPT
- {
- ush i, e;
- ***************
- *** 337,343 ****
- updcrc(NULL, 0);
-
- /* decompress */
- ! if (h[LOCHOW])
- { /* deflated entry */
- int r;
-
- --- 384,390 ----
- updcrc(NULL, 0);
-
- /* decompress */
- ! if (g || h[LOCHOW])
- { /* deflated entry */
- int r;
-
- ***************
- *** 369,386 ****
- fflush(out);
-
- /* if extended header, get it */
- ! if ((h[LOCFLG] & EXTFLG) &&
- ! fread((char *)h + LOCCRC - 4, 1, EXTHDR, in) != EXTHDR)
- ! err(3, "zip file ended prematurely");
-
- /* validate decompression */
- if (LG(h + LOCCRC) != updcrc(outbuf, 0))
- err(4, "invalid compressed data--crc error");
- ! if (LG(h + LOCLEN) != outsiz)
- err(4, "invalid compressed data--length error");
-
- /* check if there are more entries */
- ! if (fread((char *)h, 1, 4, in) == 4 && LG(h) == LOCSIG)
- fprintf(stderr,
- "funzip warning: zip file has more than one entry--rest ignored\n");
-
- --- 416,439 ----
- fflush(out);
-
- /* if extended header, get it */
- ! if (g)
- ! {
- ! if (fread((char *)h + LOCCRC, 1, 8, in) != 8)
- ! err(3, "gzip file ended prematurely");
- ! }
- ! else
- ! if ((h[LOCFLG] & EXTFLG) &&
- ! fread((char *)h + LOCCRC - 4, 1, EXTHDR, in) != EXTHDR)
- ! err(3, "zip file ended prematurely");
-
- /* validate decompression */
- if (LG(h + LOCCRC) != updcrc(outbuf, 0))
- err(4, "invalid compressed data--crc error");
- ! if (LG(h + (g ? LOCSIZ : LOCLEN)) != outsiz)
- err(4, "invalid compressed data--length error");
-
- /* check if there are more entries */
- ! if (!g && fread((char *)h, 1, 4, in) == 4 && LG(h) == LOCSIG)
- fprintf(stderr,
- "funzip warning: zip file has more than one entry--rest ignored\n");
-
-