00001 /* unpack.c -- decompress files in pack format. 00002 00003 Copyright (C) 1997, 1999, 2006 Free Software Foundation, Inc. 00004 Copyright (C) 1992-1993 Jean-loup Gailly 00005 00006 This program is free software; you can redistribute it and/or modify 00007 it under the terms of the GNU General Public License as published by 00008 the Free Software Foundation; either version 2, or (at your option) 00009 any later version. 00010 00011 This program is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 GNU General Public License for more details. 00015 00016 You should have received a copy of the GNU General Public License 00017 along with this program; if not, write to the Free Software Foundation, 00018 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ 00019 00020 #ifdef RCSID 00021 static char rcsid[] = "$Id: unpack.c,v 1.4 2006/11/20 08:40:34 eggert Exp $"; 00022 #endif 00023 00024 #include <config.h> 00025 #include "tailor.h" 00026 #include "gzip.h" 00027 #include "crypt.h" 00028 00029 #define MIN(a,b) ((a) <= (b) ? (a) : (b)) 00030 /* The arguments must not have side effects. */ 00031 00032 #define MAX_BITLEN 25 00033 /* Maximum length of Huffman codes. (Minor modifications to the code 00034 * would be needed to support 32 bits codes, but pack never generates 00035 * more than 24 bits anyway.) 00036 */ 00037 00038 #define LITERALS 256 00039 /* Number of literals, excluding the End of Block (EOB) code */ 00040 00041 #define MAX_PEEK 12 00042 /* Maximum number of 'peek' bits used to optimize traversal of the 00043 * Huffman tree. 00044 */ 00045 00046 local ulg orig_len; /* original uncompressed length */ 00047 local int max_len; /* maximum bit length of Huffman codes */ 00048 00049 local uch literal[LITERALS]; 00050 /* The literal bytes present in the Huffman tree. The EOB code is not 00051 * represented. 00052 */ 00053 00054 local int lit_base[MAX_BITLEN+1]; 00055 /* All literals of a given bit length are contiguous in literal[] and 00056 * have contiguous codes. literal[code+lit_base[len]] is the literal 00057 * for a code of len bits. 00058 */ 00059 00060 local int leaves [MAX_BITLEN+1]; /* Number of leaves for each bit length */ 00061 local int parents[MAX_BITLEN+1]; /* Number of parents for each bit length */ 00062 00063 local int peek_bits; /* Number of peek bits currently used */ 00064 00065 /* local uch prefix_len[1 << MAX_PEEK]; */ 00066 #define prefix_len outbuf 00067 /* For each bit pattern b of peek_bits bits, prefix_len[b] is the length 00068 * of the Huffman code starting with a prefix of b (upper bits), or 0 00069 * if all codes of prefix b have more than peek_bits bits. It is not 00070 * necessary to have a huge table (large MAX_PEEK) because most of the 00071 * codes encountered in the input stream are short codes (by construction). 00072 * So for most codes a single lookup will be necessary. 00073 */ 00074 #if (1<<MAX_PEEK) > OUTBUFSIZ 00075 error cannot overlay prefix_len and outbuf 00076 #endif 00077 00078 local ulg bitbuf; 00079 /* Bits are added on the low part of bitbuf and read from the high part. */ 00080 00081 local int valid; /* number of valid bits in bitbuf */ 00082 /* all bits above the last valid bit are always zero */ 00083 00084 /* Set code to the next 'bits' input bits without skipping them. code 00085 * must be the name of a simple variable and bits must not have side effects. 00086 * IN assertions: bits <= 25 (so that we still have room for an extra byte 00087 * when valid is only 24), and mask = (1<<bits)-1. 00088 */ 00089 #define look_bits(code,bits,mask) \ 00090 { \ 00091 while (valid < (bits)) bitbuf = (bitbuf<<8) | (ulg)get_byte(), valid += 8; \ 00092 code = (bitbuf >> (valid-(bits))) & (mask); \ 00093 } 00094 00095 /* Skip the given number of bits (after having peeked at them): */ 00096 #define skip_bits(bits) (valid -= (bits)) 00097 00098 #define clear_bitbuf() (valid = 0, bitbuf = 0) 00099 00100 /* Local functions */ 00101 00102 local void read_tree OF((void)); 00103 local void build_tree OF((void)); 00104 00105 /* =========================================================================== 00106 * Read the Huffman tree. 00107 */ 00108 local void read_tree() 00109 { 00110 int len; /* bit length */ 00111 int base; /* base offset for a sequence of leaves */ 00112 int n; 00113 int max_leaves = 1; 00114 00115 /* Read the original input size, MSB first */ 00116 orig_len = 0; 00117 for (n = 1; n <= 4; n++) orig_len = (orig_len << 8) | (ulg)get_byte(); 00118 00119 max_len = (int)get_byte(); /* maximum bit length of Huffman codes */ 00120 if (max_len > MAX_BITLEN) { 00121 gzip_error ("invalid compressed data -- Huffman code > 32 bits"); 00122 } 00123 00124 /* Get the number of leaves at each bit length */ 00125 n = 0; 00126 for (len = 1; len <= max_len; len++) { 00127 leaves[len] = (int)get_byte(); 00128 if (max_leaves - (len == max_len) < leaves[len]) 00129 gzip_error ("too many leaves in Huffman tree"); 00130 max_leaves = (max_leaves - leaves[len] + 1) * 2 - 1; 00131 n += leaves[len]; 00132 } 00133 if (LITERALS <= n) { 00134 gzip_error ("too many leaves in Huffman tree"); 00135 } 00136 Trace((stderr, "orig_len %lu, max_len %d, leaves %d\n", 00137 orig_len, max_len, n)); 00138 /* There are at least 2 and at most 256 leaves of length max_len. 00139 * (Pack arbitrarily rejects empty files and files consisting of 00140 * a single byte even repeated.) To fit the last leaf count in a 00141 * byte, it is offset by 2. However, the last literal is the EOB 00142 * code, and is not transmitted explicitly in the tree, so we must 00143 * adjust here by one only. 00144 */ 00145 leaves[max_len]++; 00146 00147 /* Now read the leaves themselves */ 00148 base = 0; 00149 for (len = 1; len <= max_len; len++) { 00150 /* Remember where the literals of this length start in literal[] : */ 00151 lit_base[len] = base; 00152 /* And read the literals: */ 00153 for (n = leaves[len]; n > 0; n--) { 00154 literal[base++] = (uch)get_byte(); 00155 } 00156 } 00157 leaves[max_len]++; /* Now include the EOB code in the Huffman tree */ 00158 } 00159 00160 /* =========================================================================== 00161 * Build the Huffman tree and the prefix table. 00162 */ 00163 local void build_tree() 00164 { 00165 int nodes = 0; /* number of nodes (parents+leaves) at current bit length */ 00166 int len; /* current bit length */ 00167 uch *prefixp; /* pointer in prefix_len */ 00168 00169 for (len = max_len; len >= 1; len--) { 00170 /* The number of parent nodes at this level is half the total 00171 * number of nodes at parent level: 00172 */ 00173 nodes >>= 1; 00174 parents[len] = nodes; 00175 /* Update lit_base by the appropriate bias to skip the parent nodes 00176 * (which are not represented in the literal array): 00177 */ 00178 lit_base[len] -= nodes; 00179 /* Restore nodes to be parents+leaves: */ 00180 nodes += leaves[len]; 00181 } 00182 /* Construct the prefix table, from shortest leaves to longest ones. 00183 * The shortest code is all ones, so we start at the end of the table. 00184 */ 00185 peek_bits = MIN(max_len, MAX_PEEK); 00186 prefixp = &prefix_len[1<<peek_bits]; 00187 for (len = 1; len <= peek_bits; len++) { 00188 int prefixes = leaves[len] << (peek_bits-len); /* may be 0 */ 00189 while (prefixes--) *--prefixp = (uch)len; 00190 } 00191 /* The length of all other codes is unknown: */ 00192 while (prefixp > prefix_len) *--prefixp = 0; 00193 } 00194 00195 /* =========================================================================== 00196 * Unpack in to out. This routine does not support the old pack format 00197 * with magic header \037\037. 00198 * 00199 * IN assertions: the buffer inbuf contains already the beginning of 00200 * the compressed data, from offsets inptr to insize-1 included. 00201 * The magic header has already been checked. The output buffer is cleared. 00202 */ 00203 int unpack(in, out) 00204 int in, out; /* input and output file descriptors */ 00205 { 00206 int len; /* Bit length of current code */ 00207 unsigned eob; /* End Of Block code */ 00208 register unsigned peek; /* lookahead bits */ 00209 unsigned peek_mask; /* Mask for peek_bits bits */ 00210 00211 ifd = in; 00212 ofd = out; 00213 00214 read_tree(); /* Read the Huffman tree */ 00215 build_tree(); /* Build the prefix table */ 00216 clear_bitbuf(); /* Initialize bit input */ 00217 peek_mask = (1<<peek_bits)-1; 00218 00219 /* The eob code is the largest code among all leaves of maximal length: */ 00220 eob = leaves[max_len]-1; 00221 Trace((stderr, "eob %d %x\n", max_len, eob)); 00222 00223 /* Decode the input data: */ 00224 for (;;) { 00225 /* Since eob is the longest code and not shorter than max_len, 00226 * we can peek at max_len bits without having the risk of reading 00227 * beyond the end of file. 00228 */ 00229 look_bits(peek, peek_bits, peek_mask); 00230 len = prefix_len[peek]; 00231 if (len > 0) { 00232 peek >>= peek_bits - len; /* discard the extra bits */ 00233 } else { 00234 /* Code of more than peek_bits bits, we must traverse the tree */ 00235 ulg mask = peek_mask; 00236 len = peek_bits; 00237 do { 00238 len++, mask = (mask<<1)+1; 00239 look_bits(peek, len, mask); 00240 } while (peek < (unsigned)parents[len]); 00241 /* loop as long as peek is a parent node */ 00242 } 00243 /* At this point, peek is the next complete code, of len bits */ 00244 if (peek == eob && len == max_len) break; /* end of file? */ 00245 put_ubyte(literal[peek+lit_base[len]]); 00246 Tracev((stderr,"%02d %04x %c\n", len, peek, 00247 literal[peek+lit_base[len]])); 00248 skip_bits(len); 00249 } /* for (;;) */ 00250 00251 flush_window(); 00252 if (orig_len != (ulg)(bytes_out & 0xffffffff)) { 00253 gzip_error ("invalid compressed data--length error"); 00254 } 00255 return OK; 00256 }