delete trailing whitespace from control files
[debian/gzip] / unzip.c
1 /* unzip.c -- decompress files in gzip or pkzip format.
2
3    Copyright (C) 1997-1999, 2009-2018 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 3, 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 #include <config.h>
32 #include "tailor.h"
33 #include "gzip.h"
34
35 /* PKZIP header definitions */
36 #define LOCSIG 0x04034b50L      /* four-byte lead-in (lsb first) */
37 #define LOCFLG 6                /* offset of bit flag */
38 #define  CRPFLG 1               /*  bit for encrypted entry */
39 #define  EXTFLG 8               /*  bit for extended local header */
40 #define LOCHOW 8                /* offset of compression method */
41 /* #define LOCTIM 10               UNUSED file mod time (for decryption) */
42 #define LOCCRC 14               /* offset of crc */
43 #define LOCSIZ 18               /* offset of compressed size */
44 #define LOCLEN 22               /* offset of uncompressed length */
45 #define LOCFIL 26               /* offset of file name field length */
46 #define LOCEXT 28               /* offset of extra field length */
47 #define LOCHDR 30               /* size of local header, including sig */
48 #define EXTHDR 16               /* size of extended local header, inc sig */
49 #define RAND_HEAD_LEN  12       /* length of encryption random header */
50
51
52 /* Globals */
53
54 static int decrypt;        /* flag to turn on decryption */
55 static int pkzip = 0;      /* set for a pkzip file */
56 static int ext_header = 0; /* set if extended local header */
57
58 /* ===========================================================================
59  * Check zip file and advance inptr to the start of the compressed data.
60  * Get ofname from the local header if necessary.
61  */
62 int check_zipfile(in)
63     int in;   /* input file descriptors */
64 {
65     uch *h = inbuf + inptr; /* first local header */
66
67     ifd = in;
68
69     /* Check validity of local header, and skip name and extra fields */
70     inptr += LOCHDR + SH(h + LOCFIL) + SH(h + LOCEXT);
71
72     if (inptr > insize || LG(h) != LOCSIG) {
73         fprintf(stderr, "\n%s: %s: not a valid zip file\n",
74                 program_name, ifname);
75         exit_code = ERROR;
76         return ERROR;
77     }
78     method = h[LOCHOW];
79     if (method != STORED && method != DEFLATED) {
80         fprintf(stderr,
81                 "\n%s: %s: first entry not deflated or stored -- use unzip\n",
82                 program_name, ifname);
83         exit_code = ERROR;
84         return ERROR;
85     }
86
87     /* If entry encrypted, decrypt and validate encryption header */
88     if ((decrypt = h[LOCFLG] & CRPFLG) != 0) {
89         fprintf(stderr, "\n%s: %s: encrypted file -- use unzip\n",
90                 program_name, ifname);
91         exit_code = ERROR;
92         return ERROR;
93     }
94
95     /* Save flags for unzip() */
96     ext_header = (h[LOCFLG] & EXTFLG) != 0;
97     pkzip = 1;
98
99     /* Get ofname and timestamp from local header (to be done) */
100     return OK;
101 }
102
103 /* ===========================================================================
104  * Unzip in to out.  This routine works on both gzip and pkzip files.
105  *
106  * IN assertions: the buffer inbuf contains already the beginning of
107  *   the compressed data, from offsets inptr to insize-1 included.
108  *   The magic header has already been checked. The output buffer is cleared.
109  */
110 int unzip(in, out)
111     int in, out;   /* input and output file descriptors */
112 {
113     ulg orig_crc = 0;       /* original crc */
114     ulg orig_len = 0;       /* original uncompressed length */
115     int n;
116     uch buf[EXTHDR];        /* extended local header */
117     int err = OK;
118
119     ifd = in;
120     ofd = out;
121
122     updcrc(NULL, 0);           /* initialize crc */
123
124     if (pkzip && !ext_header) {  /* crc and length at the end otherwise */
125         orig_crc = LG(inbuf + LOCCRC);
126         orig_len = LG(inbuf + LOCLEN);
127     }
128
129     /* Decompress */
130     if (method == DEFLATED)  {
131
132         int res = inflate();
133
134         if (res == 3) {
135             xalloc_die ();
136         } else if (res != 0) {
137             gzip_error ("invalid compressed data--format violated");
138         }
139
140     } else if (pkzip && method == STORED) {
141
142         register ulg n = LG(inbuf + LOCLEN);
143
144         if (n != LG(inbuf + LOCSIZ) - (decrypt ? RAND_HEAD_LEN : 0)) {
145
146             fprintf(stderr, "len %lu, siz %lu\n", n, LG(inbuf + LOCSIZ));
147             gzip_error ("invalid compressed data--length mismatch");
148         }
149         while (n--) {
150             uch c = (uch)get_byte();
151             put_ubyte(c);
152         }
153         flush_window();
154     } else {
155         gzip_error ("internal error, invalid method");
156     }
157
158     /* Get the crc and original length */
159     if (!pkzip) {
160         /* crc32  (see algorithm.doc)
161          * uncompressed input size modulo 2^32
162          */
163         for (n = 0; n < 8; n++) {
164             buf[n] = (uch)get_byte(); /* may cause an error if EOF */
165         }
166         orig_crc = LG(buf);
167         orig_len = LG(buf+4);
168
169     } else if (ext_header) {  /* If extended header, check it */
170         /* signature - 4bytes: 0x50 0x4b 0x07 0x08
171          * CRC-32 value
172          * compressed size 4-bytes
173          * uncompressed size 4-bytes
174          */
175         for (n = 0; n < EXTHDR; n++) {
176             buf[n] = (uch)get_byte(); /* may cause an error if EOF */
177         }
178         orig_crc = LG(buf+4);
179         orig_len = LG(buf+12);
180     }
181
182     /* Validate decompression */
183     if (orig_crc != updcrc(outbuf, 0)) {
184         fprintf(stderr, "\n%s: %s: invalid compressed data--crc error\n",
185                 program_name, ifname);
186         err = ERROR;
187     }
188     if (orig_len != (ulg)(bytes_out & 0xffffffff)) {
189         fprintf(stderr, "\n%s: %s: invalid compressed data--length error\n",
190                 program_name, ifname);
191         err = ERROR;
192     }
193
194     /* Check if there are more entries in a pkzip file */
195     if (pkzip && inptr + 4 < insize && LG(inbuf+inptr) == LOCSIG) {
196         if (to_stdout) {
197             WARN((stderr,
198                   "%s: %s has more than one entry--rest ignored\n",
199                   program_name, ifname));
200         } else {
201             /* Don't destroy the input zip file */
202             fprintf(stderr,
203                     "%s: %s has more than one entry -- unchanged\n",
204                     program_name, ifname);
205             err = ERROR;
206         }
207     }
208     ext_header = pkzip = 0; /* for next file */
209     if (err == OK) return OK;
210     exit_code = ERROR;
211     if (!test) abort_gzip();
212     return err;
213 }