Initial revision
[gzip.git] / unzip.c
blobdb3e3ac3cb09e5041f2abc96e5ffb0797a0fbd39
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.
8 */
11 This version can extract files in gzip or pkzip format.
12 For the latter, only the first entry is extracted, and it has to be
13 either deflated or stored.
16 #ifdef RCSID
17 static char rcsid[] = "$Id$";
18 #endif
20 #include <config.h>
21 #include "tailor.h"
22 #include "gzip.h"
23 #include "crypt.h"
25 /* PKZIP header definitions */
26 #define LOCSIG 0x04034b50L /* four-byte lead-in (lsb first) */
27 #define LOCFLG 6 /* offset of bit flag */
28 #define CRPFLG 1 /* bit for encrypted entry */
29 #define EXTFLG 8 /* bit for extended local header */
30 #define LOCHOW 8 /* offset of compression method */
31 #define LOCTIM 10 /* file mod time (for decryption) */
32 #define LOCCRC 14 /* offset of crc */
33 #define LOCSIZ 18 /* offset of compressed size */
34 #define LOCLEN 22 /* offset of uncompressed length */
35 #define LOCFIL 26 /* offset of file name field length */
36 #define LOCEXT 28 /* offset of extra field length */
37 #define LOCHDR 30 /* size of local header, including sig */
38 #define EXTHDR 16 /* size of extended local header, inc sig */
39 #define RAND_HEAD_LEN 12 /* length of encryption random header */
42 /* Globals */
44 int decrypt; /* flag to turn on decryption */
45 char *key; /* not used--needed to link crypt.c */
46 int pkzip = 0; /* set for a pkzip file */
47 int ext_header = 0; /* set if extended local header */
49 /* ===========================================================================
50 * Check zip file and advance inptr to the start of the compressed data.
51 * Get ofname from the local header if necessary.
53 int check_zipfile(in)
54 int in; /* input file descriptors */
56 uch *h = inbuf + inptr; /* first local header */
58 ifd = in;
60 /* Check validity of local header, and skip name and extra fields */
61 inptr += LOCHDR + SH(h + LOCFIL) + SH(h + LOCEXT);
63 if (inptr > insize || LG(h) != LOCSIG) {
64 fprintf(stderr, "\n%s: %s: not a valid zip file\n",
65 progname, ifname);
66 exit_code = ERROR;
67 return ERROR;
69 method = h[LOCHOW];
70 if (method != STORED && method != DEFLATED) {
71 fprintf(stderr,
72 "\n%s: %s: first entry not deflated or stored -- use unzip\n",
73 progname, ifname);
74 exit_code = ERROR;
75 return ERROR;
78 /* If entry encrypted, decrypt and validate encryption header */
79 if ((decrypt = h[LOCFLG] & CRPFLG) != 0) {
80 fprintf(stderr, "\n%s: %s: encrypted file -- use unzip\n",
81 progname, ifname);
82 exit_code = ERROR;
83 return ERROR;
86 /* Save flags for unzip() */
87 ext_header = (h[LOCFLG] & EXTFLG) != 0;
88 pkzip = 1;
90 /* Get ofname and time stamp from local header (to be done) */
91 return OK;
94 /* ===========================================================================
95 * Unzip in to out. This routine works on both gzip and pkzip files.
97 * IN assertions: the buffer inbuf contains already the beginning of
98 * the compressed data, from offsets inptr to insize-1 included.
99 * The magic header has already been checked. The output buffer is cleared.
101 int unzip(in, out)
102 int in, out; /* input and output file descriptors */
104 ulg orig_crc = 0; /* original crc */
105 ulg orig_len = 0; /* original uncompressed length */
106 int n;
107 uch buf[EXTHDR]; /* extended local header */
108 int err = OK;
110 ifd = in;
111 ofd = out;
113 updcrc(NULL, 0); /* initialize crc */
115 if (pkzip && !ext_header) { /* crc and length at the end otherwise */
116 orig_crc = LG(inbuf + LOCCRC);
117 orig_len = LG(inbuf + LOCLEN);
120 /* Decompress */
121 if (method == DEFLATED) {
123 int res = inflate();
125 if (res == 3) {
126 error("out of memory");
127 } else if (res != 0) {
128 error("invalid compressed data--format violated");
131 } else if (pkzip && method == STORED) {
133 register ulg n = LG(inbuf + LOCLEN);
135 if (n != LG(inbuf + LOCSIZ) - (decrypt ? RAND_HEAD_LEN : 0)) {
137 fprintf(stderr, "len %ld, siz %ld\n", n, LG(inbuf + LOCSIZ));
138 error("invalid compressed data--length mismatch");
140 while (n--) {
141 uch c = (uch)get_byte();
142 put_ubyte(c);
144 flush_window();
145 } else {
146 error("internal error, invalid method");
149 /* Get the crc and original length */
150 if (!pkzip) {
151 /* crc32 (see algorithm.doc)
152 * uncompressed input size modulo 2^32
154 for (n = 0; n < 8; n++) {
155 buf[n] = (uch)get_byte(); /* may cause an error if EOF */
157 orig_crc = LG(buf);
158 orig_len = LG(buf+4);
160 } else if (ext_header) { /* If extended header, check it */
161 /* signature - 4bytes: 0x50 0x4b 0x07 0x08
162 * CRC-32 value
163 * compressed size 4-bytes
164 * uncompressed size 4-bytes
166 for (n = 0; n < EXTHDR; n++) {
167 buf[n] = (uch)get_byte(); /* may cause an error if EOF */
169 orig_crc = LG(buf+4);
170 orig_len = LG(buf+12);
173 /* Validate decompression */
174 if (orig_crc != updcrc(outbuf, 0)) {
175 fprintf(stderr, "\n%s: %s: invalid compressed data--crc error\n",
176 progname, ifname);
177 err = ERROR;
179 if (orig_len != (ulg)(bytes_out & 0xffffffff)) {
180 fprintf(stderr, "\n%s: %s: invalid compressed data--length error\n",
181 progname, ifname);
182 err = ERROR;
185 /* Check if there are more entries in a pkzip file */
186 if (pkzip && inptr + 4 < insize && LG(inbuf+inptr) == LOCSIG) {
187 if (to_stdout) {
188 WARN((stderr,
189 "%s: %s has more than one entry--rest ignored\n",
190 progname, ifname));
191 } else {
192 /* Don't destroy the input zip file */
193 fprintf(stderr,
194 "%s: %s has more than one entry -- unchanged\n",
195 progname, ifname);
196 err = ERROR;
199 ext_header = pkzip = 0; /* for next file */
200 if (err == OK) return OK;
201 exit_code = ERROR;
202 if (!test) abort_gzip();
203 return err;