1 /* unzip.c -- decompress files in gzip or pkzip format.
2 * Copyright (C) 1992-1993 Jean-loup Gailly
3 * This is free software; you can redistribute it and/or modify it under the
4 * terms of the GNU General Public License, see the file COPYING.
6 * The code in this file is derived from the file funzip.c written
7 * and put in the public domain by Mark Adler.
9 * $FreeBSD: src/gnu/usr.bin/gzip/unzip.c,v 1.6 1999/08/27 23:35:54 peter Exp $
10 * $DragonFly: src/gnu/usr.bin/gzip/Attic/unzip.c,v 1.2 2003/06/17 04:25:46 dillon Exp $
14 This version can extract files in gzip or pkzip format.
15 For the latter, only the first entry is extracted, and it has to be
16 either deflated or stored.
23 /* PKZIP header definitions */
24 #define LOCSIG 0x04034b50L /* four-byte lead-in (lsb first) */
25 #define LOCFLG 6 /* offset of bit flag */
26 #define CRPFLG 1 /* bit for encrypted entry */
27 #define EXTFLG 8 /* bit for extended local header */
28 #define LOCHOW 8 /* offset of compression method */
29 #define LOCTIM 10 /* file mod time (for decryption) */
30 #define LOCCRC 14 /* offset of crc */
31 #define LOCSIZ 18 /* offset of compressed size */
32 #define LOCLEN 22 /* offset of uncompressed length */
33 #define LOCFIL 26 /* offset of file name field length */
34 #define LOCEXT 28 /* offset of extra field length */
35 #define LOCHDR 30 /* size of local header, including sig */
36 #define EXTHDR 16 /* size of extended local header, inc sig */
41 int decrypt; /* flag to turn on decryption */
42 char *key; /* not used--needed to link crypt.c */
43 int pkzip = 0; /* set for a pkzip file */
44 int ext_header = 0; /* set if extended local header */
46 /* ===========================================================================
47 * Check zip file and advance inptr to the start of the compressed data.
48 * Get ofname from the local header if necessary.
51 int in; /* input file descriptors */
53 uch *h = inbuf + inptr; /* first local header */
57 /* Check validity of local header, and skip name and extra fields */
58 inptr += LOCHDR + SH(h + LOCFIL) + SH(h + LOCEXT);
60 if (inptr > insize || LG(h) != LOCSIG) {
61 fprintf(stderr, "\n%s: %s: not a valid zip file\n",
67 if (method != STORED && method != DEFLATED) {
69 "\n%s: %s: first entry not deflated or stored -- use unzip\n",
75 /* If entry encrypted, decrypt and validate encryption header */
76 if ((decrypt = h[LOCFLG] & CRPFLG) != 0) {
77 fprintf(stderr, "\n%s: %s: encrypted file -- use unzip\n",
83 /* Save flags for unzip() */
84 ext_header = (h[LOCFLG] & EXTFLG) != 0;
87 /* Get ofname and time stamp from local header (to be done) */
91 /* ===========================================================================
92 * Unzip in to out. This routine works on both gzip and pkzip files.
94 * IN assertions: the buffer inbuf contains already the beginning of
95 * the compressed data, from offsets inptr to insize-1 included.
96 * The magic header has already been checked. The output buffer is cleared.
99 int in, out; /* input and output file descriptors */
101 ulg orig_crc = 0; /* original crc */
102 ulg orig_len = 0; /* original uncompressed length */
104 uch buf[EXTHDR]; /* extended local header */
109 updcrc(NULL, 0); /* initialize crc */
111 if (pkzip && !ext_header) { /* crc and length at the end otherwise */
112 orig_crc = LG(inbuf + LOCCRC);
113 orig_len = LG(inbuf + LOCLEN);
117 if (method == DEFLATED) {
122 error("out of memory");
123 } else if (res != 0) {
124 error("invalid compressed data--format violated");
127 } else if (pkzip && method == STORED) {
129 register ulg n = LG(inbuf + LOCLEN);
131 if (n != LG(inbuf + LOCSIZ) - (decrypt ? RAND_HEAD_LEN : 0)) {
133 fprintf(stderr, "len %ld, siz %ld\n", n, LG(inbuf + LOCSIZ));
134 error("invalid compressed data--length mismatch");
137 uch c = (uch)get_byte();
139 if (decrypt) zdecode(c);
145 error("internal error, invalid method");
148 /* Get the crc and original length */
150 /* crc32 (see algorithm.doc)
151 * uncompressed input size modulo 2^32
153 for (n = 0; n < 8; n++) {
154 buf[n] = (uch)get_byte(); /* may cause an error if EOF */
157 orig_len = LG(buf+4);
159 } else if (ext_header) { /* If extended header, check it */
160 /* signature - 4bytes: 0x50 0x4b 0x07 0x08
162 * compressed size 4-bytes
163 * uncompressed size 4-bytes
165 for (n = 0; n < EXTHDR; n++) {
166 buf[n] = (uch)get_byte(); /* may cause an error if EOF */
168 orig_crc = LG(buf+4);
169 orig_len = LG(buf+12);
172 /* Validate decompression */
173 if (orig_crc != updcrc(outbuf, 0)) {
174 error("invalid compressed data--crc error");
176 if (orig_len != (ulg)bytes_out) {
177 error("invalid compressed data--length error");
180 /* Check if there are more entries in a pkzip file */
181 if (pkzip && inptr + 4 < insize && LG(inbuf+inptr) == LOCSIG) {
184 "%s: %s has more than one entry--rest ignored\n",
187 /* Don't destroy the input zip file */
189 "%s: %s has more than one entry -- unchanged\n",
192 ext_header = pkzip = 0;
196 ext_header = pkzip = 0; /* for next file */