1 | /* unzip.c -- decompress files in gzip or pkzip format.
|
---|
2 |
|
---|
3 | Copyright (C) 1997, 1998, 1999 Free Software Foundation, Inc.
|
---|
4 | Copyright (C) 1992-1993 Jean-loup Gailly
|
---|
5 |
|
---|
6 | This program is free software; you can redistribute it and/or modify
|
---|
7 | it under the terms of the GNU General Public License as published by
|
---|
8 | the Free Software Foundation; either version 2, or (at your option)
|
---|
9 | any later version.
|
---|
10 |
|
---|
11 | This program is distributed in the hope that it will be useful,
|
---|
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
14 | GNU General Public License for more details.
|
---|
15 |
|
---|
16 | You should have received a copy of the GNU General Public License
|
---|
17 | along with this program; if not, write to the Free Software Foundation,
|
---|
18 | Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
|
---|
19 |
|
---|
20 | /*
|
---|
21 | * The code in this file is derived from the file funzip.c written
|
---|
22 | * and put in the public domain by Mark Adler.
|
---|
23 | */
|
---|
24 |
|
---|
25 | /*
|
---|
26 | This version can extract files in gzip or pkzip format.
|
---|
27 | For the latter, only the first entry is extracted, and it has to be
|
---|
28 | either deflated or stored.
|
---|
29 | */
|
---|
30 |
|
---|
31 | #ifdef RCSID
|
---|
32 | static char rcsid[] = "$Id: unzip.c,v 1.4 2006/11/20 08:40:34 eggert Exp $";
|
---|
33 | #endif
|
---|
34 |
|
---|
35 | #include <config.h>
|
---|
36 | #include "tailor.h"
|
---|
37 | #include "gzip.h"
|
---|
38 | #include "crypt.h"
|
---|
39 |
|
---|
40 | /* PKZIP header definitions */
|
---|
41 | #define LOCSIG 0x04034b50L /* four-byte lead-in (lsb first) */
|
---|
42 | #define LOCFLG 6 /* offset of bit flag */
|
---|
43 | #define CRPFLG 1 /* bit for encrypted entry */
|
---|
44 | #define EXTFLG 8 /* bit for extended local header */
|
---|
45 | #define LOCHOW 8 /* offset of compression method */
|
---|
46 | #define LOCTIM 10 /* file mod time (for decryption) */
|
---|
47 | #define LOCCRC 14 /* offset of crc */
|
---|
48 | #define LOCSIZ 18 /* offset of compressed size */
|
---|
49 | #define LOCLEN 22 /* offset of uncompressed length */
|
---|
50 | #define LOCFIL 26 /* offset of file name field length */
|
---|
51 | #define LOCEXT 28 /* offset of extra field length */
|
---|
52 | #define LOCHDR 30 /* size of local header, including sig */
|
---|
53 | #define EXTHDR 16 /* size of extended local header, inc sig */
|
---|
54 | #define RAND_HEAD_LEN 12 /* length of encryption random header */
|
---|
55 |
|
---|
56 |
|
---|
57 | /* Globals */
|
---|
58 |
|
---|
59 | int decrypt; /* flag to turn on decryption */
|
---|
60 | char *key; /* not used--needed to link crypt.c */
|
---|
61 | int pkzip = 0; /* set for a pkzip file */
|
---|
62 | int ext_header = 0; /* set if extended local header */
|
---|
63 |
|
---|
64 | /* ===========================================================================
|
---|
65 | * Check zip file and advance inptr to the start of the compressed data.
|
---|
66 | * Get ofname from the local header if necessary.
|
---|
67 | */
|
---|
68 | int check_zipfile(in)
|
---|
69 | int in; /* input file descriptors */
|
---|
70 | {
|
---|
71 | uch *h = inbuf + inptr; /* first local header */
|
---|
72 |
|
---|
73 | ifd = in;
|
---|
74 |
|
---|
75 | /* Check validity of local header, and skip name and extra fields */
|
---|
76 | inptr += LOCHDR + SH(h + LOCFIL) + SH(h + LOCEXT);
|
---|
77 |
|
---|
78 | if (inptr > insize || LG(h) != LOCSIG) {
|
---|
79 | fprintf(stderr, "\n%s: %s: not a valid zip file\n",
|
---|
80 | program_name, ifname);
|
---|
81 | exit_code = ERROR;
|
---|
82 | return ERROR;
|
---|
83 | }
|
---|
84 | method = h[LOCHOW];
|
---|
85 | if (method != STORED && method != DEFLATED) {
|
---|
86 | fprintf(stderr,
|
---|
87 | "\n%s: %s: first entry not deflated or stored -- use unzip\n",
|
---|
88 | program_name, ifname);
|
---|
89 | exit_code = ERROR;
|
---|
90 | return ERROR;
|
---|
91 | }
|
---|
92 |
|
---|
93 | /* If entry encrypted, decrypt and validate encryption header */
|
---|
94 | if ((decrypt = h[LOCFLG] & CRPFLG) != 0) {
|
---|
95 | fprintf(stderr, "\n%s: %s: encrypted file -- use unzip\n",
|
---|
96 | program_name, ifname);
|
---|
97 | exit_code = ERROR;
|
---|
98 | return ERROR;
|
---|
99 | }
|
---|
100 |
|
---|
101 | /* Save flags for unzip() */
|
---|
102 | ext_header = (h[LOCFLG] & EXTFLG) != 0;
|
---|
103 | pkzip = 1;
|
---|
104 |
|
---|
105 | /* Get ofname and time stamp from local header (to be done) */
|
---|
106 | return OK;
|
---|
107 | }
|
---|
108 |
|
---|
109 | /* ===========================================================================
|
---|
110 | * Unzip in to out. This routine works on both gzip and pkzip files.
|
---|
111 | *
|
---|
112 | * IN assertions: the buffer inbuf contains already the beginning of
|
---|
113 | * the compressed data, from offsets inptr to insize-1 included.
|
---|
114 | * The magic header has already been checked. The output buffer is cleared.
|
---|
115 | */
|
---|
116 | int unzip(in, out)
|
---|
117 | int in, out; /* input and output file descriptors */
|
---|
118 | {
|
---|
119 | ulg orig_crc = 0; /* original crc */
|
---|
120 | ulg orig_len = 0; /* original uncompressed length */
|
---|
121 | int n;
|
---|
122 | uch buf[EXTHDR]; /* extended local header */
|
---|
123 | int err = OK;
|
---|
124 |
|
---|
125 | ifd = in;
|
---|
126 | ofd = out;
|
---|
127 |
|
---|
128 | updcrc(NULL, 0); /* initialize crc */
|
---|
129 |
|
---|
130 | if (pkzip && !ext_header) { /* crc and length at the end otherwise */
|
---|
131 | orig_crc = LG(inbuf + LOCCRC);
|
---|
132 | orig_len = LG(inbuf + LOCLEN);
|
---|
133 | }
|
---|
134 |
|
---|
135 | /* Decompress */
|
---|
136 | if (method == DEFLATED) {
|
---|
137 |
|
---|
138 | int res = inflate();
|
---|
139 |
|
---|
140 | if (res == 3) {
|
---|
141 | xalloc_die ();
|
---|
142 | } else if (res != 0) {
|
---|
143 | gzip_error ("invalid compressed data--format violated");
|
---|
144 | }
|
---|
145 |
|
---|
146 | } else if (pkzip && method == STORED) {
|
---|
147 |
|
---|
148 | register ulg n = LG(inbuf + LOCLEN);
|
---|
149 |
|
---|
150 | if (n != LG(inbuf + LOCSIZ) - (decrypt ? RAND_HEAD_LEN : 0)) {
|
---|
151 |
|
---|
152 | fprintf(stderr, "len %ld, siz %ld\n", n, LG(inbuf + LOCSIZ));
|
---|
153 | gzip_error ("invalid compressed data--length mismatch");
|
---|
154 | }
|
---|
155 | while (n--) {
|
---|
156 | uch c = (uch)get_byte();
|
---|
157 | put_ubyte(c);
|
---|
158 | }
|
---|
159 | flush_window();
|
---|
160 | } else {
|
---|
161 | gzip_error ("internal error, invalid method");
|
---|
162 | }
|
---|
163 |
|
---|
164 | /* Get the crc and original length */
|
---|
165 | if (!pkzip) {
|
---|
166 | /* crc32 (see algorithm.doc)
|
---|
167 | * uncompressed input size modulo 2^32
|
---|
168 | */
|
---|
169 | for (n = 0; n < 8; n++) {
|
---|
170 | buf[n] = (uch)get_byte(); /* may cause an error if EOF */
|
---|
171 | }
|
---|
172 | orig_crc = LG(buf);
|
---|
173 | orig_len = LG(buf+4);
|
---|
174 |
|
---|
175 | } else if (ext_header) { /* If extended header, check it */
|
---|
176 | /* signature - 4bytes: 0x50 0x4b 0x07 0x08
|
---|
177 | * CRC-32 value
|
---|
178 | * compressed size 4-bytes
|
---|
179 | * uncompressed size 4-bytes
|
---|
180 | */
|
---|
181 | for (n = 0; n < EXTHDR; n++) {
|
---|
182 | buf[n] = (uch)get_byte(); /* may cause an error if EOF */
|
---|
183 | }
|
---|
184 | orig_crc = LG(buf+4);
|
---|
185 | orig_len = LG(buf+12);
|
---|
186 | }
|
---|
187 |
|
---|
188 | /* Validate decompression */
|
---|
189 | if (orig_crc != updcrc(outbuf, 0)) {
|
---|
190 | fprintf(stderr, "\n%s: %s: invalid compressed data--crc error\n",
|
---|
191 | program_name, ifname);
|
---|
192 | err = ERROR;
|
---|
193 | }
|
---|
194 | if (orig_len != (ulg)(bytes_out & 0xffffffff)) {
|
---|
195 | fprintf(stderr, "\n%s: %s: invalid compressed data--length error\n",
|
---|
196 | program_name, ifname);
|
---|
197 | err = ERROR;
|
---|
198 | }
|
---|
199 |
|
---|
200 | /* Check if there are more entries in a pkzip file */
|
---|
201 | if (pkzip && inptr + 4 < insize && LG(inbuf+inptr) == LOCSIG) {
|
---|
202 | if (to_stdout) {
|
---|
203 | WARN((stderr,
|
---|
204 | "%s: %s has more than one entry--rest ignored\n",
|
---|
205 | program_name, ifname));
|
---|
206 | } else {
|
---|
207 | /* Don't destroy the input zip file */
|
---|
208 | fprintf(stderr,
|
---|
209 | "%s: %s has more than one entry -- unchanged\n",
|
---|
210 | program_name, ifname);
|
---|
211 | err = ERROR;
|
---|
212 | }
|
---|
213 | }
|
---|
214 | ext_header = pkzip = 0; /* for next file */
|
---|
215 | if (err == OK) return OK;
|
---|
216 | exit_code = ERROR;
|
---|
217 | if (!test) abort_gzip();
|
---|
218 | return err;
|
---|
219 | }
|
---|