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