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