1 /* $NetBSD: zuncompress.c,v 1.11 2011/08/16 13:55:02 joerg Exp $ */
4 * SPDX-License-Identifier: BSD-3-Clause
6 * Copyright (c) 1985, 1986, 1992, 1993
7 * The Regents of the University of California. All rights reserved.
9 * This code is derived from software contributed to Berkeley by
10 * Diomidis Spinellis and James A. Woods, derived from original
11 * work by Spencer Thomas and Joseph Orost.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * from: NetBSD: zopen.c,v 1.8 2003/08/07 11:13:29 agc Exp
41 /* This file is #included by gzip.c */
43 static int zread(void *, char *, int);
45 #define tab_prefixof(i) (zs->zs_codetab[i])
46 #define tab_suffixof(i) ((char_type *)(zs->zs_htab))[i]
47 #define de_stack ((char_type *)&tab_suffixof(1 << BITS))
49 #define BITS 16 /* Default bits. */
50 #define HSIZE 69001 /* 95% occupancy */ /* XXX may not need HSIZE */
51 #define BIT_MASK 0x1f /* Defines for third byte of header. */
52 #define BLOCK_MASK 0x80
53 #define CHECK_GAP 10000 /* Ratio check interval. */
54 #define BUFSIZE (64 * 1024)
57 * Masks 0x40 and 0x20 are free. I think 0x20 should mean that there is
58 * a fourth header byte (for expansion).
60 #define INIT_BITS 9 /* Initial number of bits/code. */
63 * the next two codes should not be changed lightly, as they must not
64 * lie within the contiguous general code space.
66 #define FIRST 257 /* First free entry. */
67 #define CLEAR 256 /* Table clear output code. */
70 #define MAXCODE(n_bits) ((1 << (n_bits)) - 1)
72 typedef long code_int;
73 typedef long count_int;
74 typedef u_char char_type;
76 static char_type magic_header[] =
77 {'\037', '\235'}; /* 1F 9D */
79 static char_type rmask[9] =
80 {0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff};
82 static off_t total_compressed_bytes;
83 static size_t compressed_prelen;
84 static char *compressed_pre;
87 FILE *zs_fp; /* File stream for I/O */
88 char zs_mode; /* r or w */
90 S_START, S_MIDDLE, S_EOF
91 } zs_state; /* State of computation */
92 int zs_n_bits; /* Number of bits/code. */
93 int zs_maxbits; /* User settable max # bits/code. */
94 code_int zs_maxcode; /* Maximum code, given n_bits. */
95 code_int zs_maxmaxcode; /* Should NEVER generate this code. */
96 count_int zs_htab [HSIZE];
97 u_short zs_codetab [HSIZE];
98 code_int zs_hsize; /* For dynamic table sizing. */
99 code_int zs_free_ent; /* First unused entry. */
101 * Block compression parameters -- after all codes are used up,
102 * and compression rate changes, start over.
104 int zs_block_compress;
107 count_int zs_checkpoint;
109 long zs_in_count; /* Length of input. */
110 long zs_bytes_out; /* Length of compressed output. */
111 long zs_out_count; /* # of codes output (for debugging). */
112 char_type zs_buf[BITS];
117 code_int zs_hsize_reg;
119 } w; /* Write parameters */
121 char_type *zs_stackp;
123 code_int zs_code, zs_oldcode, zs_incode;
124 int zs_roffset, zs_size;
125 char_type zs_gbuf[BITS];
126 } r; /* Read parameters */
130 static code_int getcode(struct s_zstate *zs);
133 zuncompress(FILE *in, FILE *out, char *pre, size_t prelen,
134 off_t *compressed_bytes)
139 buf = malloc(BUFSIZE);
144 compressed_prelen = prelen;
146 compressed_pre = pre;
148 compressed_pre = NULL;
150 while ((bin = fread(buf, 1, BUFSIZE, in)) != 0) {
151 if (tflag == 0 && (off_t)fwrite(buf, 1, bin, out) != bin) {
158 if (compressed_bytes)
159 *compressed_bytes = total_compressed_bytes;
169 /* We leave the caller to close the fd passed to zdopen() */
178 if ((zs = calloc(1, sizeof(struct s_zstate))) == NULL)
181 zs->zs_state = S_START;
183 /* XXX we can get rid of some of these */
184 zs->zs_hsize = HSIZE; /* For dynamic table sizing. */
185 zs->zs_free_ent = 0; /* First unused entry. */
186 zs->zs_block_compress = BLOCK_MASK;
187 zs->zs_clear_flg = 0; /* XXX we calloc()'d this structure why = 0? */
189 zs->zs_checkpoint = CHECK_GAP;
190 zs->zs_in_count = 1; /* Length of input. */
191 zs->zs_out_count = 0; /* # of codes output (for debugging). */
192 zs->u.r.zs_roffset = 0;
196 * Layering compress on top of stdio in order to provide buffering,
197 * and ensure that reads and write work with the data specified.
199 if ((zs->zs_fp = fdopen(fd, "r")) == NULL) {
204 return funopen(zs, zread, NULL, NULL, zclose);
208 * Decompress read. This routine adapts to the codes in the file building
209 * the "string" table on-the-fly; requiring no table to be stored in the
210 * compressed file. The tables used herein are shared with those of the
211 * compress() routine. See the definitions above.
214 zread(void *cookie, char *rbp, int num)
218 u_char *bp, header[3];
226 switch (zs->zs_state) {
228 zs->zs_state = S_MIDDLE;
236 /* Check the magic number */
237 for (i = 0; i < 3 && compressed_prelen; i++, compressed_prelen--)
238 header[i] = *compressed_pre++;
240 if (fread(header + i, 1, sizeof(header) - i, zs->zs_fp) !=
241 sizeof(header) - i ||
242 memcmp(header, magic_header, sizeof(magic_header)) != 0) {
246 total_compressed_bytes = 0;
247 zs->zs_maxbits = header[2]; /* Set -b from file. */
248 zs->zs_block_compress = zs->zs_maxbits & BLOCK_MASK;
249 zs->zs_maxbits &= BIT_MASK;
250 zs->zs_maxmaxcode = 1L << zs->zs_maxbits;
251 if (zs->zs_maxbits > BITS || zs->zs_maxbits < 12) {
255 /* As above, initialize the first 256 entries in the table. */
256 zs->zs_maxcode = MAXCODE(zs->zs_n_bits = INIT_BITS);
257 for (zs->u.r.zs_code = 255; zs->u.r.zs_code >= 0; zs->u.r.zs_code--) {
258 tab_prefixof(zs->u.r.zs_code) = 0;
259 tab_suffixof(zs->u.r.zs_code) = (char_type) zs->u.r.zs_code;
261 zs->zs_free_ent = zs->zs_block_compress ? FIRST : 256;
263 zs->u.r.zs_oldcode = -1;
264 zs->u.r.zs_stackp = de_stack;
266 while ((zs->u.r.zs_code = getcode(zs)) > -1) {
268 if ((zs->u.r.zs_code == CLEAR) && zs->zs_block_compress) {
269 for (zs->u.r.zs_code = 255; zs->u.r.zs_code >= 0;
271 tab_prefixof(zs->u.r.zs_code) = 0;
272 zs->zs_clear_flg = 1;
273 zs->zs_free_ent = FIRST;
274 zs->u.r.zs_oldcode = -1;
277 zs->u.r.zs_incode = zs->u.r.zs_code;
279 /* Special case for KwKwK string. */
280 if (zs->u.r.zs_code >= zs->zs_free_ent) {
281 if (zs->u.r.zs_code > zs->zs_free_ent ||
282 zs->u.r.zs_oldcode == -1) {
287 *zs->u.r.zs_stackp++ = zs->u.r.zs_finchar;
288 zs->u.r.zs_code = zs->u.r.zs_oldcode;
291 * The above condition ensures that code < free_ent.
292 * The construction of tab_prefixof in turn guarantees that
293 * each iteration decreases code and therefore stack usage is
294 * bound by 1 << BITS - 256.
297 /* Generate output characters in reverse order. */
298 while (zs->u.r.zs_code >= 256) {
299 *zs->u.r.zs_stackp++ = tab_suffixof(zs->u.r.zs_code);
300 zs->u.r.zs_code = tab_prefixof(zs->u.r.zs_code);
302 *zs->u.r.zs_stackp++ = zs->u.r.zs_finchar = tab_suffixof(zs->u.r.zs_code);
304 /* And put them out in forward order. */
308 *bp++ = *--zs->u.r.zs_stackp;
309 } while (zs->u.r.zs_stackp > de_stack);
311 /* Generate the new entry. */
312 if ((zs->u.r.zs_code = zs->zs_free_ent) < zs->zs_maxmaxcode &&
313 zs->u.r.zs_oldcode != -1) {
314 tab_prefixof(zs->u.r.zs_code) = (u_short) zs->u.r.zs_oldcode;
315 tab_suffixof(zs->u.r.zs_code) = zs->u.r.zs_finchar;
316 zs->zs_free_ent = zs->u.r.zs_code + 1;
319 /* Remember previous code. */
320 zs->u.r.zs_oldcode = zs->u.r.zs_incode;
322 zs->zs_state = S_EOF;
323 eof: return (num - count);
327 * Read one code from the standard input. If EOF, return -1.
331 * code or -1 is returned.
334 getcode(struct s_zstate *zs)
340 bp = zs->u.r.zs_gbuf;
341 if (zs->zs_clear_flg > 0 || zs->u.r.zs_roffset >= zs->u.r.zs_size ||
342 zs->zs_free_ent > zs->zs_maxcode) {
344 * If the next entry will be too big for the current gcode
345 * size, then we must increase the size. This implies reading
346 * a new buffer full, too.
348 if (zs->zs_free_ent > zs->zs_maxcode) {
350 if (zs->zs_n_bits == zs->zs_maxbits) /* Won't get any bigger now. */
351 zs->zs_maxcode = zs->zs_maxmaxcode;
353 zs->zs_maxcode = MAXCODE(zs->zs_n_bits);
355 if (zs->zs_clear_flg > 0) {
356 zs->zs_maxcode = MAXCODE(zs->zs_n_bits = INIT_BITS);
357 zs->zs_clear_flg = 0;
360 for (i = 0; i < zs->zs_n_bits && compressed_prelen; i++, compressed_prelen--)
361 zs->u.r.zs_gbuf[i] = *compressed_pre++;
362 zs->u.r.zs_size = fread(zs->u.r.zs_gbuf + i, 1, zs->zs_n_bits - i, zs->zs_fp);
363 zs->u.r.zs_size += i;
364 if (zs->u.r.zs_size <= 0) /* End of file. */
366 zs->u.r.zs_roffset = 0;
368 total_compressed_bytes += zs->u.r.zs_size;
370 /* Round size down to integral number of codes. */
371 zs->u.r.zs_size = (zs->u.r.zs_size << 3) - (zs->zs_n_bits - 1);
373 r_off = zs->u.r.zs_roffset;
374 bits = zs->zs_n_bits;
376 /* Get to the first byte. */
380 /* Get first part (low order bits). */
381 gcode = (*bp++ >> r_off);
383 r_off = 8 - r_off; /* Now, roffset into gcode word. */
385 /* Get any 8 bit parts in the middle (<=1 for up to 16 bits). */
387 gcode |= *bp++ << r_off;
392 /* High order bits. */
393 gcode |= (*bp & rmask[bits]) << r_off;
394 zs->u.r.zs_roffset += zs->zs_n_bits;