1/*	$NetBSD: zuncompress.c,v 1.11 2011/08/16 13:55:02 joerg Exp $ */
2
3/*-
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 * Copyright (c) 1985, 1986, 1992, 1993
7 *	The Regents of the University of California.  All rights reserved.
8 *
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.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
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.
24 *
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
35 * SUCH DAMAGE.
36 *
37 * from: NetBSD: zopen.c,v 1.8 2003/08/07 11:13:29 agc Exp
38 */
39
40/* This file is #included by gzip.c */
41
42static int	zread(void *, char *, int);
43
44#define	tab_prefixof(i)	(zs->zs_codetab[i])
45#define	tab_suffixof(i)	((char_type *)(zs->zs_htab))[i]
46#define	de_stack	((char_type *)&tab_suffixof(1 << BITS))
47
48#define BITS		16		/* Default bits. */
49#define HSIZE		69001		/* 95% occupancy */ /* XXX may not need HSIZE */
50#define BIT_MASK	0x1f		/* Defines for third byte of header. */
51#define BLOCK_MASK	0x80
52#define CHECK_GAP	10000		/* Ratio check interval. */
53#define BUFSIZE		(64 * 1024)
54
55/*
56 * Masks 0x40 and 0x20 are free.  I think 0x20 should mean that there is
57 * a fourth header byte (for expansion).
58 */
59#define INIT_BITS	9	/* Initial number of bits/code. */
60
61/*
62 * the next two codes should not be changed lightly, as they must not
63 * lie within the contiguous general code space.
64 */
65#define	FIRST	257		/* First free entry. */
66#define	CLEAR	256		/* Table clear output code. */
67
68
69#define MAXCODE(n_bits)	((1 << (n_bits)) - 1)
70
71typedef long	code_int;
72typedef long	count_int;
73typedef u_char	char_type;
74
75static char_type magic_header[] =
76	{'\037', '\235'};	/* 1F 9D */
77
78static char_type rmask[9] =
79	{0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff};
80
81static off_t total_compressed_bytes;
82static size_t compressed_prelen;
83static char *compressed_pre;
84
85struct s_zstate {
86	FILE *zs_fp;			/* File stream for I/O */
87	char zs_mode;			/* r or w */
88	enum {
89		S_START, S_MIDDLE, S_EOF
90	} zs_state;			/* State of computation */
91	int zs_n_bits;			/* Number of bits/code. */
92	int zs_maxbits;			/* User settable max # bits/code. */
93	code_int zs_maxcode;		/* Maximum code, given n_bits. */
94	code_int zs_maxmaxcode;		/* Should NEVER generate this code. */
95	count_int zs_htab [HSIZE];
96	u_short zs_codetab [HSIZE];
97	code_int zs_hsize;		/* For dynamic table sizing. */
98	code_int zs_free_ent;		/* First unused entry. */
99	/*
100	 * Block compression parameters -- after all codes are used up,
101	 * and compression rate changes, start over.
102	 */
103	int zs_block_compress;
104	int zs_clear_flg;
105	long zs_ratio;
106	count_int zs_checkpoint;
107	int zs_offset;
108	long zs_in_count;		/* Length of input. */
109	long zs_bytes_out;		/* Length of compressed output. */
110	long zs_out_count;		/* # of codes output (for debugging). */
111	char_type zs_buf[BITS];
112	union {
113		struct {
114			long zs_fcode;
115			code_int zs_ent;
116			code_int zs_hsize_reg;
117			int zs_hshift;
118		} w;			/* Write parameters */
119		struct {
120			char_type *zs_stackp;
121			int zs_finchar;
122			code_int zs_code, zs_oldcode, zs_incode;
123			int zs_roffset, zs_size;
124			char_type zs_gbuf[BITS];
125		} r;			/* Read parameters */
126	} u;
127};
128
129static code_int	getcode(struct s_zstate *zs);
130
131static off_t
132zuncompress(FILE *in, FILE *out, char *pre, size_t prelen,
133	    off_t *compressed_bytes)
134{
135	off_t bin, bout = 0;
136	char *buf;
137
138	buf = malloc(BUFSIZE);
139	if (buf == NULL)
140		return -1;
141
142	/* XXX */
143	compressed_prelen = prelen;
144	if (prelen != 0)
145		compressed_pre = pre;
146	else
147		compressed_pre = NULL;
148
149	while ((bin = fread(buf, 1, BUFSIZE, in)) != 0) {
150		if (tflag == 0 && (off_t)fwrite(buf, 1, bin, out) != bin) {
151			free(buf);
152			return -1;
153		}
154		bout += bin;
155	}
156
157	if (compressed_bytes)
158		*compressed_bytes = total_compressed_bytes;
159
160	free(buf);
161	return bout;
162}
163
164static int
165zclose(void *zs)
166{
167	free(zs);
168	/* We leave the caller to close the fd passed to zdopen() */
169	return 0;
170}
171
172FILE *
173zdopen(int fd)
174{
175	struct s_zstate *zs;
176
177	if ((zs = calloc(1, sizeof(struct s_zstate))) == NULL)
178		return (NULL);
179
180	zs->zs_state = S_START;
181
182	/* XXX we can get rid of some of these */
183	zs->zs_hsize = HSIZE;			/* For dynamic table sizing. */
184	zs->zs_free_ent = 0;			/* First unused entry. */
185	zs->zs_block_compress = BLOCK_MASK;
186	zs->zs_clear_flg = 0;			/* XXX we calloc()'d this structure why = 0? */
187	zs->zs_ratio = 0;
188	zs->zs_checkpoint = CHECK_GAP;
189	zs->zs_in_count = 1;			/* Length of input. */
190	zs->zs_out_count = 0;			/* # of codes output (for debugging). */
191	zs->u.r.zs_roffset = 0;
192	zs->u.r.zs_size = 0;
193
194	/*
195	 * Layering compress on top of stdio in order to provide buffering,
196	 * and ensure that reads and write work with the data specified.
197	 */
198	if ((zs->zs_fp = fdopen(fd, "r")) == NULL) {
199		free(zs);
200		return NULL;
201	}
202
203	return funopen(zs, zread, NULL, NULL, zclose);
204}
205
206/*
207 * Decompress read.  This routine adapts to the codes in the file building
208 * the "string" table on-the-fly; requiring no table to be stored in the
209 * compressed file.  The tables used herein are shared with those of the
210 * compress() routine.  See the definitions above.
211 */
212static int
213zread(void *cookie, char *rbp, int num)
214{
215	u_int count, i;
216	struct s_zstate *zs;
217	u_char *bp, header[3];
218
219	if (num == 0)
220		return (0);
221
222	zs = cookie;
223	count = num;
224	bp = (u_char *)rbp;
225	switch (zs->zs_state) {
226	case S_START:
227		zs->zs_state = S_MIDDLE;
228		break;
229	case S_MIDDLE:
230		goto middle;
231	case S_EOF:
232		goto eof;
233	}
234
235	/* Check the magic number */
236	for (i = 0; i < 3 && compressed_prelen; i++, compressed_prelen--)
237		header[i] = *compressed_pre++;
238
239	if (fread(header + i, 1, sizeof(header) - i, zs->zs_fp) !=
240		  sizeof(header) - i ||
241	    memcmp(header, magic_header, sizeof(magic_header)) != 0) {
242		errno = EFTYPE;
243		return (-1);
244	}
245	total_compressed_bytes = 0;
246	zs->zs_maxbits = header[2];	/* Set -b from file. */
247	zs->zs_block_compress = zs->zs_maxbits & BLOCK_MASK;
248	zs->zs_maxbits &= BIT_MASK;
249	zs->zs_maxmaxcode = 1L << zs->zs_maxbits;
250	if (zs->zs_maxbits > BITS || zs->zs_maxbits < 12) {
251		errno = EFTYPE;
252		return (-1);
253	}
254	/* As above, initialize the first 256 entries in the table. */
255	zs->zs_maxcode = MAXCODE(zs->zs_n_bits = INIT_BITS);
256	for (zs->u.r.zs_code = 255; zs->u.r.zs_code >= 0; zs->u.r.zs_code--) {
257		tab_prefixof(zs->u.r.zs_code) = 0;
258		tab_suffixof(zs->u.r.zs_code) = (char_type) zs->u.r.zs_code;
259	}
260	zs->zs_free_ent = zs->zs_block_compress ? FIRST : 256;
261
262	zs->u.r.zs_oldcode = -1;
263	zs->u.r.zs_stackp = de_stack;
264
265	while ((zs->u.r.zs_code = getcode(zs)) > -1) {
266
267		if ((zs->u.r.zs_code == CLEAR) && zs->zs_block_compress) {
268			for (zs->u.r.zs_code = 255; zs->u.r.zs_code >= 0;
269			    zs->u.r.zs_code--)
270				tab_prefixof(zs->u.r.zs_code) = 0;
271			zs->zs_clear_flg = 1;
272			zs->zs_free_ent = FIRST;
273			zs->u.r.zs_oldcode = -1;
274			continue;
275		}
276		zs->u.r.zs_incode = zs->u.r.zs_code;
277
278		/* Special case for KwKwK string. */
279		if (zs->u.r.zs_code >= zs->zs_free_ent) {
280			if (zs->u.r.zs_code > zs->zs_free_ent ||
281			    zs->u.r.zs_oldcode == -1) {
282				/* Bad stream. */
283				errno = EFTYPE;
284				return (-1);
285			}
286			*zs->u.r.zs_stackp++ = zs->u.r.zs_finchar;
287			zs->u.r.zs_code = zs->u.r.zs_oldcode;
288		}
289		/*
290		 * The above condition ensures that code < free_ent.
291		 * The construction of tab_prefixof in turn guarantees that
292		 * each iteration decreases code and therefore stack usage is
293		 * bound by 1 << BITS - 256.
294		 */
295
296		/* Generate output characters in reverse order. */
297		while (zs->u.r.zs_code >= 256) {
298			*zs->u.r.zs_stackp++ = tab_suffixof(zs->u.r.zs_code);
299			zs->u.r.zs_code = tab_prefixof(zs->u.r.zs_code);
300		}
301		*zs->u.r.zs_stackp++ = zs->u.r.zs_finchar = tab_suffixof(zs->u.r.zs_code);
302
303		/* And put them out in forward order.  */
304middle:		do {
305			if (count-- == 0)
306				return (num);
307			*bp++ = *--zs->u.r.zs_stackp;
308		} while (zs->u.r.zs_stackp > de_stack);
309
310		/* Generate the new entry. */
311		if ((zs->u.r.zs_code = zs->zs_free_ent) < zs->zs_maxmaxcode &&
312		    zs->u.r.zs_oldcode != -1) {
313			tab_prefixof(zs->u.r.zs_code) = (u_short) zs->u.r.zs_oldcode;
314			tab_suffixof(zs->u.r.zs_code) = zs->u.r.zs_finchar;
315			zs->zs_free_ent = zs->u.r.zs_code + 1;
316		}
317
318		/* Remember previous code. */
319		zs->u.r.zs_oldcode = zs->u.r.zs_incode;
320	}
321	zs->zs_state = S_EOF;
322eof:	return (num - count);
323}
324
325/*-
326 * Read one code from the standard input.  If EOF, return -1.
327 * Inputs:
328 * 	stdin
329 * Outputs:
330 * 	code or -1 is returned.
331 */
332static code_int
333getcode(struct s_zstate *zs)
334{
335	code_int gcode;
336	int r_off, bits, i;
337	char_type *bp;
338
339	bp = zs->u.r.zs_gbuf;
340	if (zs->zs_clear_flg > 0 || zs->u.r.zs_roffset >= zs->u.r.zs_size ||
341	    zs->zs_free_ent > zs->zs_maxcode) {
342		/*
343		 * If the next entry will be too big for the current gcode
344		 * size, then we must increase the size.  This implies reading
345		 * a new buffer full, too.
346		 */
347		if (zs->zs_free_ent > zs->zs_maxcode) {
348			zs->zs_n_bits++;
349			if (zs->zs_n_bits == zs->zs_maxbits)	/* Won't get any bigger now. */
350				zs->zs_maxcode = zs->zs_maxmaxcode;
351			else
352				zs->zs_maxcode = MAXCODE(zs->zs_n_bits);
353		}
354		if (zs->zs_clear_flg > 0) {
355			zs->zs_maxcode = MAXCODE(zs->zs_n_bits = INIT_BITS);
356			zs->zs_clear_flg = 0;
357		}
358		/* XXX */
359		for (i = 0; i < zs->zs_n_bits && compressed_prelen; i++, compressed_prelen--)
360			zs->u.r.zs_gbuf[i] = *compressed_pre++;
361		zs->u.r.zs_size = fread(zs->u.r.zs_gbuf + i, 1, zs->zs_n_bits - i, zs->zs_fp);
362		zs->u.r.zs_size += i;
363		if (zs->u.r.zs_size <= 0)			/* End of file. */
364			return (-1);
365		zs->u.r.zs_roffset = 0;
366
367		total_compressed_bytes += zs->u.r.zs_size;
368
369		/* Round size down to integral number of codes. */
370		zs->u.r.zs_size = (zs->u.r.zs_size << 3) - (zs->zs_n_bits - 1);
371	}
372	r_off = zs->u.r.zs_roffset;
373	bits = zs->zs_n_bits;
374
375	/* Get to the first byte. */
376	bp += (r_off >> 3);
377	r_off &= 7;
378
379	/* Get first part (low order bits). */
380	gcode = (*bp++ >> r_off);
381	bits -= (8 - r_off);
382	r_off = 8 - r_off;	/* Now, roffset into gcode word. */
383
384	/* Get any 8 bit parts in the middle (<=1 for up to 16 bits). */
385	if (bits >= 8) {
386		gcode |= *bp++ << r_off;
387		r_off += 8;
388		bits -= 8;
389	}
390
391	/* High order bits. */
392	gcode |= (*bp & rmask[bits]) << r_off;
393	zs->u.r.zs_roffset += zs->zs_n_bits;
394
395	return (gcode);
396}
397
398