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