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