1*5a645f22SBen Gras /* $FreeBSD: head/usr.bin/gzip/unpack.c 194579 2009-06-21 09:39:43Z delphij $ */
2*5a645f22SBen Gras /* $NetBSD: unpack.c,v 1.2 2010/11/06 21:42:32 mrg Exp $ */
3*5a645f22SBen Gras
4*5a645f22SBen Gras /*-
5*5a645f22SBen Gras * Copyright (c) 2009 Xin LI <delphij@FreeBSD.org>
6*5a645f22SBen Gras * All rights reserved.
7*5a645f22SBen Gras *
8*5a645f22SBen Gras * Redistribution and use in source and binary forms, with or without
9*5a645f22SBen Gras * modification, are permitted provided that the following conditions
10*5a645f22SBen Gras * are met:
11*5a645f22SBen Gras * 1. Redistributions of source code must retain the above copyright
12*5a645f22SBen Gras * notice, this list of conditions and the following disclaimer.
13*5a645f22SBen Gras * 2. Redistributions in binary form must reproduce the above copyright
14*5a645f22SBen Gras * notice, this list of conditions and the following disclaimer in the
15*5a645f22SBen Gras * documentation and/or other materials provided with the distribution.
16*5a645f22SBen Gras *
17*5a645f22SBen Gras * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18*5a645f22SBen Gras * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19*5a645f22SBen Gras * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20*5a645f22SBen Gras * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21*5a645f22SBen Gras * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22*5a645f22SBen Gras * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23*5a645f22SBen Gras * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24*5a645f22SBen Gras * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25*5a645f22SBen Gras * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26*5a645f22SBen Gras * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27*5a645f22SBen Gras * SUCH DAMAGE.
28*5a645f22SBen Gras */
29*5a645f22SBen Gras
30*5a645f22SBen Gras /* This file is #included by gzip.c */
31*5a645f22SBen Gras
32*5a645f22SBen Gras /*
33*5a645f22SBen Gras * pack(1) file format:
34*5a645f22SBen Gras *
35*5a645f22SBen Gras * The first 7 bytes is the header:
36*5a645f22SBen Gras * 00, 01 - Signature (US, RS), we already validated it earlier.
37*5a645f22SBen Gras * 02..05 - Uncompressed size
38*5a645f22SBen Gras * 06 - Level for the huffman tree (<=24)
39*5a645f22SBen Gras *
40*5a645f22SBen Gras * pack(1) will then store symbols (leaf) nodes count in each huffman
41*5a645f22SBen Gras * tree levels, each level would consume 1 byte (See [1]).
42*5a645f22SBen Gras *
43*5a645f22SBen Gras * After the symbol count table, there is the symbol table, storing
44*5a645f22SBen Gras * symbols represented by corresponding leaf node. EOB is not being
45*5a645f22SBen Gras * explicitly transmitted (not necessary anyway) in the symbol table.
46*5a645f22SBen Gras *
47*5a645f22SBen Gras * Compressed data goes after the symbol table.
48*5a645f22SBen Gras *
49*5a645f22SBen Gras * NOTES
50*5a645f22SBen Gras *
51*5a645f22SBen Gras * [1] If we count EOB into the symbols, that would mean that we will
52*5a645f22SBen Gras * have at most 256 symbols in the huffman tree. pack(1) rejects empty
53*5a645f22SBen Gras * file and files that just repeats one character, which means that we
54*5a645f22SBen Gras * will have at least 2 symbols. Therefore, pack(1) would reduce the
55*5a645f22SBen Gras * last level symbol count by 2 which makes it a number in
56*5a645f22SBen Gras * range [0..254], so all levels' symbol count would fit into 1 byte.
57*5a645f22SBen Gras */
58*5a645f22SBen Gras
59*5a645f22SBen Gras #define PACK_HEADER_LENGTH 7
60*5a645f22SBen Gras #define HTREE_MAXLEVEL 24
61*5a645f22SBen Gras
62*5a645f22SBen Gras /*
63*5a645f22SBen Gras * unpack descriptor
64*5a645f22SBen Gras *
65*5a645f22SBen Gras * Represent the huffman tree in a similar way that pack(1) would
66*5a645f22SBen Gras * store in a packed file. We store all symbols in a linear table,
67*5a645f22SBen Gras * and store pointers to each level's first symbol. In addition to
68*5a645f22SBen Gras * that, maintain two counts for each level: inner nodes count and
69*5a645f22SBen Gras * leaf nodes count.
70*5a645f22SBen Gras */
71*5a645f22SBen Gras typedef struct {
72*5a645f22SBen Gras int symbol_size; /* Size of the symbol table */
73*5a645f22SBen Gras int treelevels; /* Levels for the huffman tree */
74*5a645f22SBen Gras
75*5a645f22SBen Gras int *symbolsin; /* Table of leaf symbols count in
76*5a645f22SBen Gras each level */
77*5a645f22SBen Gras int *inodesin; /* Table of internal nodes count in
78*5a645f22SBen Gras each level */
79*5a645f22SBen Gras
80*5a645f22SBen Gras char *symbol; /* The symbol table */
81*5a645f22SBen Gras char *symbol_eob; /* Pointer to the EOB symbol */
82*5a645f22SBen Gras char **tree; /* Decoding huffman tree (pointers to
83*5a645f22SBen Gras first symbol of each tree level */
84*5a645f22SBen Gras
85*5a645f22SBen Gras off_t uncompressed_size; /* Uncompressed size */
86*5a645f22SBen Gras FILE *fpIn; /* Input stream */
87*5a645f22SBen Gras FILE *fpOut; /* Output stream */
88*5a645f22SBen Gras } unpack_descriptor_t;
89*5a645f22SBen Gras
90*5a645f22SBen Gras /*
91*5a645f22SBen Gras * Release resource allocated to an unpack descriptor.
92*5a645f22SBen Gras *
93*5a645f22SBen Gras * Caller is responsible to make sure that all of these pointers are
94*5a645f22SBen Gras * initialized (in our case, they all point to valid memory block).
95*5a645f22SBen Gras * We don't zero out pointers here because nobody else would ever
96*5a645f22SBen Gras * reference the memory block without scrubbing them.
97*5a645f22SBen Gras */
98*5a645f22SBen Gras static void
unpack_descriptor_fini(unpack_descriptor_t * unpackd)99*5a645f22SBen Gras unpack_descriptor_fini(unpack_descriptor_t *unpackd)
100*5a645f22SBen Gras {
101*5a645f22SBen Gras
102*5a645f22SBen Gras free(unpackd->symbolsin);
103*5a645f22SBen Gras free(unpackd->inodesin);
104*5a645f22SBen Gras free(unpackd->symbol);
105*5a645f22SBen Gras free(unpackd->tree);
106*5a645f22SBen Gras
107*5a645f22SBen Gras fclose(unpackd->fpIn);
108*5a645f22SBen Gras fclose(unpackd->fpOut);
109*5a645f22SBen Gras }
110*5a645f22SBen Gras
111*5a645f22SBen Gras /*
112*5a645f22SBen Gras * Recursively fill the internal node count table
113*5a645f22SBen Gras */
114*5a645f22SBen Gras static void
unpackd_fill_inodesin(const unpack_descriptor_t * unpackd,int level)115*5a645f22SBen Gras unpackd_fill_inodesin(const unpack_descriptor_t *unpackd, int level)
116*5a645f22SBen Gras {
117*5a645f22SBen Gras
118*5a645f22SBen Gras /*
119*5a645f22SBen Gras * The internal nodes would be 1/2 of total internal nodes and
120*5a645f22SBen Gras * leaf nodes in the next level. For the last level there
121*5a645f22SBen Gras * would be no internal node by definition.
122*5a645f22SBen Gras */
123*5a645f22SBen Gras if (level < unpackd->treelevels) {
124*5a645f22SBen Gras unpackd_fill_inodesin(unpackd, level + 1);
125*5a645f22SBen Gras unpackd->inodesin[level] = (unpackd->inodesin[level + 1] +
126*5a645f22SBen Gras unpackd->symbolsin[level + 1]) / 2;
127*5a645f22SBen Gras } else
128*5a645f22SBen Gras unpackd->inodesin[level] = 0;
129*5a645f22SBen Gras }
130*5a645f22SBen Gras
131*5a645f22SBen Gras /*
132*5a645f22SBen Gras * Update counter for accepted bytes
133*5a645f22SBen Gras */
134*5a645f22SBen Gras static void
accepted_bytes(off_t * bytes_in,off_t newbytes)135*5a645f22SBen Gras accepted_bytes(off_t *bytes_in, off_t newbytes)
136*5a645f22SBen Gras {
137*5a645f22SBen Gras
138*5a645f22SBen Gras if (bytes_in != NULL)
139*5a645f22SBen Gras (*bytes_in) += newbytes;
140*5a645f22SBen Gras }
141*5a645f22SBen Gras
142*5a645f22SBen Gras /*
143*5a645f22SBen Gras * Read file header and construct the tree. Also, prepare the buffered I/O
144*5a645f22SBen Gras * for decode routine.
145*5a645f22SBen Gras *
146*5a645f22SBen Gras * Return value is uncompressed size.
147*5a645f22SBen Gras */
148*5a645f22SBen Gras static void
unpack_parse_header(int in,int out,char * pre,size_t prelen,off_t * bytes_in,unpack_descriptor_t * unpackd)149*5a645f22SBen Gras unpack_parse_header(int in, int out, char *pre, size_t prelen, off_t *bytes_in,
150*5a645f22SBen Gras unpack_descriptor_t *unpackd)
151*5a645f22SBen Gras {
152*5a645f22SBen Gras unsigned char hdr[PACK_HEADER_LENGTH]; /* buffer for header */
153*5a645f22SBen Gras ssize_t bytesread; /* Bytes read from the file */
154*5a645f22SBen Gras int i, j, thisbyte;
155*5a645f22SBen Gras
156*5a645f22SBen Gras /* Prepend the header buffer if we already read some data */
157*5a645f22SBen Gras if (prelen != 0)
158*5a645f22SBen Gras memcpy(hdr, pre, prelen);
159*5a645f22SBen Gras
160*5a645f22SBen Gras /* Read in and fill the rest bytes of header */
161*5a645f22SBen Gras bytesread = read(in, hdr + prelen, PACK_HEADER_LENGTH - prelen);
162*5a645f22SBen Gras if (bytesread < 0)
163*5a645f22SBen Gras maybe_err("Error reading pack header");
164*5a645f22SBen Gras
165*5a645f22SBen Gras accepted_bytes(bytes_in, PACK_HEADER_LENGTH);
166*5a645f22SBen Gras
167*5a645f22SBen Gras /* Obtain uncompressed length (bytes 2,3,4,5)*/
168*5a645f22SBen Gras unpackd->uncompressed_size = 0;
169*5a645f22SBen Gras for (i = 2; i <= 5; i++) {
170*5a645f22SBen Gras unpackd->uncompressed_size <<= 8;
171*5a645f22SBen Gras unpackd->uncompressed_size |= hdr[i];
172*5a645f22SBen Gras }
173*5a645f22SBen Gras
174*5a645f22SBen Gras /* Get the levels of the tree */
175*5a645f22SBen Gras unpackd->treelevels = hdr[6];
176*5a645f22SBen Gras if (unpackd->treelevels > HTREE_MAXLEVEL || unpackd->treelevels < 1)
177*5a645f22SBen Gras maybe_errx("Huffman tree has insane levels");
178*5a645f22SBen Gras
179*5a645f22SBen Gras /* Let libc take care for buffering from now on */
180*5a645f22SBen Gras if ((unpackd->fpIn = fdopen(in, "r")) == NULL)
181*5a645f22SBen Gras maybe_err("Can not fdopen() input stream");
182*5a645f22SBen Gras if ((unpackd->fpOut = fdopen(out, "w")) == NULL)
183*5a645f22SBen Gras maybe_err("Can not fdopen() output stream");
184*5a645f22SBen Gras
185*5a645f22SBen Gras /* Allocate for the tables of bounds and the tree itself */
186*5a645f22SBen Gras unpackd->inodesin =
187*5a645f22SBen Gras calloc(unpackd->treelevels, sizeof(*(unpackd->inodesin)));
188*5a645f22SBen Gras unpackd->symbolsin =
189*5a645f22SBen Gras calloc(unpackd->treelevels, sizeof(*(unpackd->symbolsin)));
190*5a645f22SBen Gras unpackd->tree =
191*5a645f22SBen Gras calloc(unpackd->treelevels, (sizeof (*(unpackd->tree))));
192*5a645f22SBen Gras if (unpackd->inodesin == NULL || unpackd->symbolsin == NULL ||
193*5a645f22SBen Gras unpackd->tree == NULL)
194*5a645f22SBen Gras maybe_err("calloc");
195*5a645f22SBen Gras
196*5a645f22SBen Gras /* We count from 0 so adjust to match array upper bound */
197*5a645f22SBen Gras unpackd->treelevels--;
198*5a645f22SBen Gras
199*5a645f22SBen Gras /* Read the levels symbol count table and calculate total */
200*5a645f22SBen Gras unpackd->symbol_size = 1; /* EOB */
201*5a645f22SBen Gras for (i = 0; i <= unpackd->treelevels; i++) {
202*5a645f22SBen Gras if ((thisbyte = fgetc(unpackd->fpIn)) == EOF)
203*5a645f22SBen Gras maybe_err("File appears to be truncated");
204*5a645f22SBen Gras unpackd->symbolsin[i] = (unsigned char)thisbyte;
205*5a645f22SBen Gras unpackd->symbol_size += unpackd->symbolsin[i];
206*5a645f22SBen Gras }
207*5a645f22SBen Gras accepted_bytes(bytes_in, unpackd->treelevels);
208*5a645f22SBen Gras if (unpackd->symbol_size > 256)
209*5a645f22SBen Gras maybe_errx("Bad symbol table");
210*5a645f22SBen Gras
211*5a645f22SBen Gras /* Allocate for the symbol table, point symbol_eob at the beginning */
212*5a645f22SBen Gras unpackd->symbol_eob = unpackd->symbol = calloc(1, unpackd->symbol_size);
213*5a645f22SBen Gras if (unpackd->symbol == NULL)
214*5a645f22SBen Gras maybe_err("calloc");
215*5a645f22SBen Gras
216*5a645f22SBen Gras /*
217*5a645f22SBen Gras * Read in the symbol table, which contain [2, 256] symbols.
218*5a645f22SBen Gras * In order to fit the count in one byte, pack(1) would offset
219*5a645f22SBen Gras * it by reducing 2 from the actual number from the last level.
220*5a645f22SBen Gras *
221*5a645f22SBen Gras * We adjust the last level's symbol count by 1 here, because
222*5a645f22SBen Gras * the EOB symbol is not being transmitted explicitly. Another
223*5a645f22SBen Gras * adjustment would be done later afterward.
224*5a645f22SBen Gras */
225*5a645f22SBen Gras unpackd->symbolsin[unpackd->treelevels]++;
226*5a645f22SBen Gras for (i = 0; i <= unpackd->treelevels; i++) {
227*5a645f22SBen Gras unpackd->tree[i] = unpackd->symbol_eob;
228*5a645f22SBen Gras for (j = 0; j < unpackd->symbolsin[i]; j++) {
229*5a645f22SBen Gras if ((thisbyte = fgetc(unpackd->fpIn)) == EOF)
230*5a645f22SBen Gras maybe_errx("Symbol table truncated");
231*5a645f22SBen Gras *unpackd->symbol_eob++ = (char)thisbyte;
232*5a645f22SBen Gras }
233*5a645f22SBen Gras accepted_bytes(bytes_in, unpackd->symbolsin[i]);
234*5a645f22SBen Gras }
235*5a645f22SBen Gras
236*5a645f22SBen Gras /* Now, take account for the EOB symbol as well */
237*5a645f22SBen Gras unpackd->symbolsin[unpackd->treelevels]++;
238*5a645f22SBen Gras
239*5a645f22SBen Gras /*
240*5a645f22SBen Gras * The symbolsin table has been constructed now.
241*5a645f22SBen Gras * Calculate the internal nodes count table based on it.
242*5a645f22SBen Gras */
243*5a645f22SBen Gras unpackd_fill_inodesin(unpackd, 0);
244*5a645f22SBen Gras }
245*5a645f22SBen Gras
246*5a645f22SBen Gras /*
247*5a645f22SBen Gras * Decode huffman stream, based on the huffman tree.
248*5a645f22SBen Gras */
249*5a645f22SBen Gras static void
unpack_decode(const unpack_descriptor_t * unpackd,off_t * bytes_in)250*5a645f22SBen Gras unpack_decode(const unpack_descriptor_t *unpackd, off_t *bytes_in)
251*5a645f22SBen Gras {
252*5a645f22SBen Gras int thislevel, thiscode, thisbyte, inlevelindex;
253*5a645f22SBen Gras int i;
254*5a645f22SBen Gras off_t bytes_out = 0;
255*5a645f22SBen Gras const char *thissymbol; /* The symbol pointer decoded from stream */
256*5a645f22SBen Gras
257*5a645f22SBen Gras /*
258*5a645f22SBen Gras * Decode huffman. Fetch every bytes from the file, get it
259*5a645f22SBen Gras * into 'thiscode' bit-by-bit, then output the symbol we got
260*5a645f22SBen Gras * when one has been found.
261*5a645f22SBen Gras *
262*5a645f22SBen Gras * Assumption: sizeof(int) > ((max tree levels + 1) / 8).
263*5a645f22SBen Gras * bad things could happen if not.
264*5a645f22SBen Gras */
265*5a645f22SBen Gras thislevel = 0;
266*5a645f22SBen Gras thiscode = thisbyte = 0;
267*5a645f22SBen Gras
268*5a645f22SBen Gras while ((thisbyte = fgetc(unpackd->fpIn)) != EOF) {
269*5a645f22SBen Gras accepted_bytes(bytes_in, 1);
270*5a645f22SBen Gras
271*5a645f22SBen Gras /*
272*5a645f22SBen Gras * Split one bit from thisbyte, from highest to lowest,
273*5a645f22SBen Gras * feed the bit into thiscode, until we got a symbol from
274*5a645f22SBen Gras * the tree.
275*5a645f22SBen Gras */
276*5a645f22SBen Gras for (i = 7; i >= 0; i--) {
277*5a645f22SBen Gras thiscode = (thiscode << 1) | ((thisbyte >> i) & 1);
278*5a645f22SBen Gras
279*5a645f22SBen Gras /* Did we got a symbol? (referencing leaf node) */
280*5a645f22SBen Gras if (thiscode >= unpackd->inodesin[thislevel]) {
281*5a645f22SBen Gras inlevelindex =
282*5a645f22SBen Gras thiscode - unpackd->inodesin[thislevel];
283*5a645f22SBen Gras if (inlevelindex > unpackd->symbolsin[thislevel])
284*5a645f22SBen Gras maybe_errx("File corrupt");
285*5a645f22SBen Gras
286*5a645f22SBen Gras thissymbol =
287*5a645f22SBen Gras &(unpackd->tree[thislevel][inlevelindex]);
288*5a645f22SBen Gras if ((thissymbol == unpackd->symbol_eob) &&
289*5a645f22SBen Gras (bytes_out == unpackd->uncompressed_size))
290*5a645f22SBen Gras goto finished;
291*5a645f22SBen Gras
292*5a645f22SBen Gras fputc((*thissymbol), unpackd->fpOut);
293*5a645f22SBen Gras bytes_out++;
294*5a645f22SBen Gras
295*5a645f22SBen Gras /* Prepare for next input */
296*5a645f22SBen Gras thislevel = 0; thiscode = 0;
297*5a645f22SBen Gras } else {
298*5a645f22SBen Gras thislevel++;
299*5a645f22SBen Gras if (thislevel > unpackd->treelevels)
300*5a645f22SBen Gras maybe_errx("File corrupt");
301*5a645f22SBen Gras }
302*5a645f22SBen Gras }
303*5a645f22SBen Gras }
304*5a645f22SBen Gras
305*5a645f22SBen Gras finished:
306*5a645f22SBen Gras if (bytes_out != unpackd->uncompressed_size)
307*5a645f22SBen Gras maybe_errx("Premature EOF");
308*5a645f22SBen Gras }
309*5a645f22SBen Gras
310*5a645f22SBen Gras /* Handler for pack(1)'ed file */
311*5a645f22SBen Gras static off_t
unpack(int in,int out,char * pre,size_t prelen,off_t * bytes_in)312*5a645f22SBen Gras unpack(int in, int out, char *pre, size_t prelen, off_t *bytes_in)
313*5a645f22SBen Gras {
314*5a645f22SBen Gras unpack_descriptor_t unpackd;
315*5a645f22SBen Gras
316*5a645f22SBen Gras unpack_parse_header(dup(in), dup(out), pre, prelen, bytes_in, &unpackd);
317*5a645f22SBen Gras unpack_decode(&unpackd, bytes_in);
318*5a645f22SBen Gras unpack_descriptor_fini(&unpackd);
319*5a645f22SBen Gras
320*5a645f22SBen Gras /* If we reached here, the unpack was successful */
321*5a645f22SBen Gras return (unpackd.uncompressed_size);
322*5a645f22SBen Gras }
323*5a645f22SBen Gras
324