tests: reference: don't rely on od's -w option
[gzip.git] / unzip.c
blob8f6a3bb6ae6fe6662ef0d62ce318795e6e85785d
1 /* unzip.c -- decompress files in gzip or pkzip format.
3 Copyright (C) 1997-1999, 2009-2024 Free Software Foundation, Inc.
4 Copyright (C) 1992-1993 Jean-loup Gailly
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 3, or (at your option)
9 any later version.
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.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <https://www.gnu.org/licenses/>. */
20 * The code in this file is derived from the file funzip.c written
21 * and put in the public domain by Mark Adler.
25 This version can extract files in gzip or pkzip format.
26 For the latter, only the first entry is extracted, and it has to be
27 either deflated or stored.
30 #include <config.h>
31 #include "tailor.h"
32 #include "gzip.h"
34 /* PKZIP header definitions */
35 #define LOCSIG 0x04034b50L /* four-byte lead-in (lsb first) */
36 #define LOCFLG 6 /* offset of bit flag */
37 #define CRPFLG 1 /* bit for encrypted entry */
38 #define EXTFLG 8 /* bit for extended local header */
39 #define LOCHOW 8 /* offset of compression method */
40 /* #define LOCTIM 10 UNUSED file mod time (for decryption) */
41 #define LOCCRC 14 /* offset of crc */
42 #define LOCSIZ 18 /* offset of compressed size */
43 #define LOCLEN 22 /* offset of uncompressed length */
44 #define LOCFIL 26 /* offset of file name field length */
45 #define LOCEXT 28 /* offset of extra field length */
46 #define LOCHDR 30 /* size of local header, including sig */
47 #define EXTHDR 16 /* size of extended local header, inc sig */
48 #define RAND_HEAD_LEN 12 /* length of encryption random header */
51 /* Globals */
53 ulg unzip_crc; /* CRC found by 'unzip'. */
55 static int decrypt; /* flag to turn on decryption */
56 static int pkzip = 0; /* set for a pkzip file */
57 static int ext_header = 0; /* set if extended local header */
59 /* ===========================================================================
60 * Check zip file and advance inptr to the start of the compressed data.
61 * Get ofname from the local header if necessary.
62 * IN is the input file descriptor.
64 int
65 check_zipfile (int in)
67 uch *h = inbuf + inptr; /* first local header */
69 ifd = in;
71 /* Check validity of local header, and skip name and extra fields */
72 inptr += LOCHDR + SH(h + LOCFIL) + SH(h + LOCEXT);
74 if (inptr > insize || LG(h) != LOCSIG) {
75 fprintf(stderr, "\n%s: %s: not a valid zip file\n",
76 program_name, ifname);
77 exit_code = ERROR;
78 return ERROR;
80 method = h[LOCHOW];
81 if (method != STORED && method != DEFLATED) {
82 fprintf(stderr,
83 "\n%s: %s: first entry not deflated or stored -- use unzip\n",
84 program_name, ifname);
85 exit_code = ERROR;
86 return ERROR;
89 /* If entry encrypted, decrypt and validate encryption header */
90 if ((decrypt = h[LOCFLG] & CRPFLG) != 0) {
91 fprintf(stderr, "\n%s: %s: encrypted file -- use unzip\n",
92 program_name, ifname);
93 exit_code = ERROR;
94 return ERROR;
97 /* Save flags for unzip() */
98 ext_header = (h[LOCFLG] & EXTFLG) != 0;
99 pkzip = 1;
101 /* Get ofname and timestamp from local header (to be done) */
102 return OK;
105 /* ===========================================================================
106 * Unzip in to out. This routine works on both gzip and pkzip files.
108 * IN assertions: the buffer inbuf contains already the beginning of
109 * the compressed data, from offsets inptr to insize-1 included.
110 * The magic header has already been checked. The output buffer is cleared.
112 * 'in' and 'out' are the input and output file descriptors.
115 unzip (int in, int out)
117 ulg orig_crc = 0; /* original crc */
118 ulg orig_len = 0; /* original uncompressed length */
119 off_t orig_bytes_out = bytes_out;
120 int n;
121 uch buf[EXTHDR]; /* extended local header */
122 int err = OK;
124 ifd = in;
125 ofd = out;
127 updcrc(NULL, 0); /* initialize crc */
129 if (pkzip && !ext_header) { /* crc and length at the end otherwise */
130 orig_crc = LG(inbuf + LOCCRC);
131 orig_len = LG(inbuf + LOCLEN);
134 /* Decompress */
135 if (method == DEFLATED) {
137 #ifdef IBM_Z_DFLTCC
138 int res = dfltcc_inflate ();
139 #else
140 int res = gzip_inflate ();
141 #endif
143 if (res == 3) {
144 xalloc_die ();
145 } else if (res != 0) {
146 gzip_error ("invalid compressed data--format violated");
149 } else if (pkzip && method == STORED) {
151 register ulg n = LG(inbuf + LOCLEN);
153 if (n != LG(inbuf + LOCSIZ) - (decrypt ? RAND_HEAD_LEN : 0)) {
155 fprintf(stderr, "len %lu, siz %lu\n", n, LG(inbuf + LOCSIZ));
156 gzip_error ("invalid compressed data--length mismatch");
158 while (n--) {
159 uch c = (uch)get_byte();
160 put_ubyte(c);
162 flush_window();
163 } else {
164 gzip_error ("internal error, invalid method");
167 /* Get the crc and original length */
168 if (!pkzip) {
169 /* crc32 (see algorithm.doc)
170 * uncompressed input size modulo 2^32
172 for (n = 0; n < 8; n++) {
173 buf[n] = (uch)get_byte(); /* may cause an error if EOF */
175 orig_crc = LG(buf);
176 orig_len = LG(buf+4);
178 } else if (ext_header) { /* If extended header, check it */
179 /* signature - 4bytes: 0x50 0x4b 0x07 0x08
180 * CRC-32 value
181 * compressed size 4-bytes
182 * uncompressed size 4-bytes
184 for (n = 0; n < EXTHDR; n++) {
185 buf[n] = (uch)get_byte(); /* may cause an error if EOF */
187 orig_crc = LG(buf+4);
188 orig_len = LG(buf+12);
191 /* Validate decompression */
192 if (orig_crc != updcrc(outbuf, 0)) {
193 fprintf(stderr, "\n%s: %s: invalid compressed data--crc error\n",
194 program_name, ifname);
195 err = ERROR;
197 if (orig_len != (ulg)((bytes_out - orig_bytes_out) & 0xffffffff)) {
198 fprintf(stderr, "\n%s: %s: invalid compressed data--length error\n",
199 program_name, ifname);
200 err = ERROR;
203 /* Check if there are more entries in a pkzip file */
204 if (pkzip && inptr + 4 < insize && LG(inbuf+inptr) == LOCSIG) {
205 if (to_stdout) {
206 WARN((stderr,
207 "%s: %s has more than one entry--rest ignored\n",
208 program_name, ifname));
209 } else {
210 /* Don't destroy the input zip file */
211 fprintf(stderr,
212 "%s: %s has more than one entry -- unchanged\n",
213 program_name, ifname);
214 err = ERROR;
217 ext_header = pkzip = 0; /* for next file */
218 unzip_crc = orig_crc;
219 if (err == OK) return OK;
220 exit_code = ERROR;
221 if (!test) abort_gzip();
222 return err;