1/*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 1985, 1986, 1992, 1993
5 *	The Regents of the University of California.  All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Diomidis Spinellis and James A. Woods, derived from original
9 * work by Spencer Thomas and Joseph Orost.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in the
18 *    documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 *    may be used to endorse or promote products derived from this software
21 *    without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36
37#include <sys/cdefs.h>
38/*-
39 * fcompress.c - File compression ala IEEE Computer, June 1984.
40 *
41 * Compress authors:
42 *		Spencer W. Thomas	(decvax!utah-cs!thomas)
43 *		Jim McKie		(decvax!mcvax!jim)
44 *		Steve Davies		(decvax!vax135!petsd!peora!srd)
45 *		Ken Turkowski		(decvax!decwrl!turtlevax!ken)
46 *		James A. Woods		(decvax!ihnp4!ames!jaw)
47 *		Joe Orost		(decvax!vax135!petsd!joe)
48 *
49 * Cleaned up and converted to library returning I/O streams by
50 * Diomidis Spinellis <dds@doc.ic.ac.uk>.
51 *
52 * zopen(filename, mode, bits)
53 *	Returns a FILE * that can be used for read or write.  The modes
54 *	supported are only "r" and "w".  Seeking is not allowed.  On
55 *	reading the file is decompressed, on writing it is compressed.
56 *	The output is compatible with compress(1) with 16 bit tables.
57 *	Any file produced by compress(1) can be read.
58 */
59
60#include <sys/param.h>
61#include <sys/stat.h>
62
63#include <ctype.h>
64#include <errno.h>
65#include <signal.h>
66#include <stdio.h>
67#include <stdlib.h>
68#include <string.h>
69#include <unistd.h>
70#include "zopen.h"
71
72#define	BITS		16		/* Default bits. */
73#define	HSIZE		69001		/* 95% occupancy */
74
75/* A code_int must be able to hold 2**BITS values of type int, and also -1. */
76typedef long code_int;
77typedef long count_int;
78
79typedef u_char char_type;
80static char_type magic_header[] =
81	{'\037', '\235'};		/* 1F 9D */
82
83#define	BIT_MASK	0x1f		/* Defines for third byte of header. */
84#define	BLOCK_MASK	0x80
85
86/*
87 * Masks 0x40 and 0x20 are free.  I think 0x20 should mean that there is
88 * a fourth header byte (for expansion).
89 */
90#define	INIT_BITS 9			/* Initial number of bits/code. */
91
92#define	MAXCODE(n_bits)	((1 << (n_bits)) - 1)
93
94struct s_zstate {
95	FILE *zs_fp;			/* File stream for I/O */
96	char zs_mode;			/* r or w */
97	enum {
98		S_START, S_MIDDLE, S_EOF
99	} zs_state;			/* State of computation */
100	u_int zs_n_bits;		/* Number of bits/code. */
101	u_int zs_maxbits;		/* User settable max # bits/code. */
102	code_int zs_maxcode;		/* Maximum code, given n_bits. */
103	code_int zs_maxmaxcode;		/* Should NEVER generate this code. */
104	count_int zs_htab [HSIZE];
105	u_short zs_codetab [HSIZE];
106	code_int zs_hsize;		/* For dynamic table sizing. */
107	code_int zs_free_ent;		/* First unused entry. */
108	/*
109	 * Block compression parameters -- after all codes are used up,
110	 * and compression rate changes, start over.
111	 */
112	int zs_block_compress;
113	int zs_clear_flg;
114	long zs_ratio;
115	count_int zs_checkpoint;
116	u_int zs_offset;
117	long zs_in_count;		/* Length of input. */
118	long zs_bytes_out;		/* Length of compressed output. */
119	long zs_out_count;		/* # of codes output (for debugging). */
120	char_type zs_buf[BITS];
121	union {
122		struct {
123			long zs_fcode;
124			code_int zs_ent;
125			code_int zs_hsize_reg;
126			int zs_hshift;
127		} w;			/* Write parameters */
128		struct {
129			char_type *zs_stackp;
130			int zs_finchar;
131			code_int zs_code, zs_oldcode, zs_incode;
132			int zs_roffset, zs_size;
133			char_type zs_gbuf[BITS];
134		} r;			/* Read parameters */
135	} u;
136};
137
138/* Definitions to retain old variable names */
139#define	fp		zs->zs_fp
140#define	zmode		zs->zs_mode
141#define	state		zs->zs_state
142#define	n_bits		zs->zs_n_bits
143#define	maxbits		zs->zs_maxbits
144#define	maxcode		zs->zs_maxcode
145#define	maxmaxcode	zs->zs_maxmaxcode
146#define	htab		zs->zs_htab
147#define	codetab		zs->zs_codetab
148#define	hsize		zs->zs_hsize
149#define	free_ent	zs->zs_free_ent
150#define	block_compress	zs->zs_block_compress
151#define	clear_flg	zs->zs_clear_flg
152#define	ratio		zs->zs_ratio
153#define	checkpoint	zs->zs_checkpoint
154#define	offset		zs->zs_offset
155#define	in_count	zs->zs_in_count
156#define	bytes_out	zs->zs_bytes_out
157#define	out_count	zs->zs_out_count
158#define	buf		zs->zs_buf
159#define	fcode		zs->u.w.zs_fcode
160#define	hsize_reg	zs->u.w.zs_hsize_reg
161#define	ent		zs->u.w.zs_ent
162#define	hshift		zs->u.w.zs_hshift
163#define	stackp		zs->u.r.zs_stackp
164#define	finchar		zs->u.r.zs_finchar
165#define	code		zs->u.r.zs_code
166#define	oldcode		zs->u.r.zs_oldcode
167#define	incode		zs->u.r.zs_incode
168#define	roffset		zs->u.r.zs_roffset
169#define	size		zs->u.r.zs_size
170#define	gbuf		zs->u.r.zs_gbuf
171
172/*
173 * To save much memory, we overlay the table used by compress() with those
174 * used by decompress().  The tab_prefix table is the same size and type as
175 * the codetab.  The tab_suffix table needs 2**BITS characters.  We get this
176 * from the beginning of htab.  The output stack uses the rest of htab, and
177 * contains characters.  There is plenty of room for any possible stack
178 * (stack used to be 8000 characters).
179 */
180
181#define	htabof(i)	htab[i]
182#define	codetabof(i)	codetab[i]
183
184#define	tab_prefixof(i)	codetabof(i)
185#define	tab_suffixof(i)	((char_type *)(htab))[i]
186#define	de_stack	((char_type *)&tab_suffixof(1 << BITS))
187
188#define	CHECK_GAP 10000		/* Ratio check interval. */
189
190/*
191 * the next two codes should not be changed lightly, as they must not
192 * lie within the contiguous general code space.
193 */
194#define	FIRST	257		/* First free entry. */
195#define	CLEAR	256		/* Table clear output code. */
196
197static int	cl_block(struct s_zstate *);
198static void	cl_hash(struct s_zstate *, count_int);
199static code_int	getcode(struct s_zstate *);
200static int	output(struct s_zstate *, code_int);
201static int	zclose(void *);
202static int	zread(void *, char *, int);
203static int	zwrite(void *, const char *, int);
204
205/*-
206 * Algorithm from "A Technique for High Performance Data Compression",
207 * Terry A. Welch, IEEE Computer Vol 17, No 6 (June 1984), pp 8-19.
208 *
209 * Algorithm:
210 * 	Modified Lempel-Ziv method (LZW).  Basically finds common
211 * substrings and replaces them with a variable size code.  This is
212 * deterministic, and can be done on the fly.  Thus, the decompression
213 * procedure needs no input table, but tracks the way the table was built.
214 */
215
216/*-
217 * compress write
218 *
219 * Algorithm:  use open addressing double hashing (no chaining) on the
220 * prefix code / next character combination.  We do a variant of Knuth's
221 * algorithm D (vol. 3, sec. 6.4) along with G. Knott's relatively-prime
222 * secondary probe.  Here, the modular division first probe is gives way
223 * to a faster exclusive-or manipulation.  Also do block compression with
224 * an adaptive reset, whereby the code table is cleared when the compression
225 * ratio decreases, but after the table fills.  The variable-length output
226 * codes are re-sized at this point, and a special CLEAR code is generated
227 * for the decompressor.  Late addition:  construct the table according to
228 * file size for noticeable speed improvement on small files.  Please direct
229 * questions about this implementation to ames!jaw.
230 */
231static int
232zwrite(void *cookie, const char *wbp, int num)
233{
234	code_int i;
235	int c, disp;
236	struct s_zstate *zs;
237	const u_char *bp;
238	u_char tmp;
239	int count;
240
241	if (num == 0)
242		return (0);
243
244	zs = cookie;
245	count = num;
246	bp = (const u_char *)wbp;
247	if (state == S_MIDDLE)
248		goto middle;
249	state = S_MIDDLE;
250
251	maxmaxcode = 1L << maxbits;
252	if (fwrite(magic_header,
253	    sizeof(char), sizeof(magic_header), fp) != sizeof(magic_header))
254		return (-1);
255	tmp = (u_char)((maxbits) | block_compress);
256	if (fwrite(&tmp, sizeof(char), sizeof(tmp), fp) != sizeof(tmp))
257		return (-1);
258
259	offset = 0;
260	bytes_out = 3;		/* Includes 3-byte header mojo. */
261	out_count = 0;
262	clear_flg = 0;
263	ratio = 0;
264	in_count = 1;
265	checkpoint = CHECK_GAP;
266	maxcode = MAXCODE(n_bits = INIT_BITS);
267	free_ent = ((block_compress) ? FIRST : 256);
268
269	ent = *bp++;
270	--count;
271
272	hshift = 0;
273	for (fcode = (long)hsize; fcode < 65536L; fcode *= 2L)
274		hshift++;
275	hshift = 8 - hshift;	/* Set hash code range bound. */
276
277	hsize_reg = hsize;
278	cl_hash(zs, (count_int)hsize_reg);	/* Clear hash table. */
279
280middle:	for (i = 0; count--;) {
281		c = *bp++;
282		in_count++;
283		fcode = (long)(((long)c << maxbits) + ent);
284		i = ((c << hshift) ^ ent);	/* Xor hashing. */
285
286		if (htabof(i) == fcode) {
287			ent = codetabof(i);
288			continue;
289		} else if ((long)htabof(i) < 0)	/* Empty slot. */
290			goto nomatch;
291		disp = hsize_reg - i;	/* Secondary hash (after G. Knott). */
292		if (i == 0)
293			disp = 1;
294probe:		if ((i -= disp) < 0)
295			i += hsize_reg;
296
297		if (htabof(i) == fcode) {
298			ent = codetabof(i);
299			continue;
300		}
301		if ((long)htabof(i) >= 0)
302			goto probe;
303nomatch:	if (output(zs, (code_int) ent) == -1)
304			return (-1);
305		out_count++;
306		ent = c;
307		if (free_ent < maxmaxcode) {
308			codetabof(i) = free_ent++;	/* code -> hashtable */
309			htabof(i) = fcode;
310		} else if ((count_int)in_count >=
311		    checkpoint && block_compress) {
312			if (cl_block(zs) == -1)
313				return (-1);
314		}
315	}
316	return (num);
317}
318
319static int
320zclose(void *cookie)
321{
322	struct s_zstate *zs;
323	int rval;
324
325	zs = cookie;
326	if (zmode == 'w') {		/* Put out the final code. */
327		if (output(zs, (code_int) ent) == -1) {
328			(void)fclose(fp);
329			free(zs);
330			return (-1);
331		}
332		out_count++;
333		if (output(zs, (code_int) - 1) == -1) {
334			(void)fclose(fp);
335			free(zs);
336			return (-1);
337		}
338	}
339	rval = fclose(fp) == EOF ? -1 : 0;
340	free(zs);
341	return (rval);
342}
343
344/*-
345 * Output the given code.
346 * Inputs:
347 * 	code:	A n_bits-bit integer.  If == -1, then EOF.  This assumes
348 *		that n_bits =< (long)wordsize - 1.
349 * Outputs:
350 * 	Outputs code to the file.
351 * Assumptions:
352 *	Chars are 8 bits long.
353 * Algorithm:
354 * 	Maintain a BITS character long buffer (so that 8 codes will
355 * fit in it exactly).  Use the VAX insv instruction to insert each
356 * code in turn.  When the buffer fills up empty it and start over.
357 */
358
359static char_type lmask[9] =
360	{0xff, 0xfe, 0xfc, 0xf8, 0xf0, 0xe0, 0xc0, 0x80, 0x00};
361static char_type rmask[9] =
362	{0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff};
363
364static int
365output(struct s_zstate *zs, code_int ocode)
366{
367	int r_off;
368	u_int bits;
369	char_type *bp;
370
371	r_off = offset;
372	bits = n_bits;
373	bp = buf;
374	if (ocode >= 0) {
375		/* Get to the first byte. */
376		bp += (r_off >> 3);
377		r_off &= 7;
378		/*
379		 * Since ocode is always >= 8 bits, only need to mask the first
380		 * hunk on the left.
381		 */
382		*bp = (*bp & rmask[r_off]) | ((ocode << r_off) & lmask[r_off]);
383		bp++;
384		bits -= (8 - r_off);
385		ocode >>= 8 - r_off;
386		/* Get any 8 bit parts in the middle (<=1 for up to 16 bits). */
387		if (bits >= 8) {
388			*bp++ = ocode;
389			ocode >>= 8;
390			bits -= 8;
391		}
392		/* Last bits. */
393		if (bits)
394			*bp = ocode;
395		offset += n_bits;
396		if (offset == (n_bits << 3)) {
397			bp = buf;
398			bits = n_bits;
399			bytes_out += bits;
400			if (fwrite(bp, sizeof(char), bits, fp) != bits)
401				return (-1);
402			bp += bits;
403			bits = 0;
404			offset = 0;
405		}
406		/*
407		 * If the next entry is going to be too big for the ocode size,
408		 * then increase it, if possible.
409		 */
410		if (free_ent > maxcode || (clear_flg > 0)) {
411		       /*
412			* Write the whole buffer, because the input side won't
413			* discover the size increase until after it has read it.
414			*/
415			if (offset > 0) {
416				if (fwrite(buf, 1, n_bits, fp) != n_bits)
417					return (-1);
418				bytes_out += n_bits;
419			}
420			offset = 0;
421
422			if (clear_flg) {
423				maxcode = MAXCODE(n_bits = INIT_BITS);
424				clear_flg = 0;
425			} else {
426				n_bits++;
427				if (n_bits == maxbits)
428					maxcode = maxmaxcode;
429				else
430					maxcode = MAXCODE(n_bits);
431			}
432		}
433	} else {
434		/* At EOF, write the rest of the buffer. */
435		if (offset > 0) {
436			offset = (offset + 7) / 8;
437			if (fwrite(buf, 1, offset, fp) != offset)
438				return (-1);
439			bytes_out += offset;
440		}
441		offset = 0;
442	}
443	return (0);
444}
445
446/*
447 * Decompress read.  This routine adapts to the codes in the file building
448 * the "string" table on-the-fly; requiring no table to be stored in the
449 * compressed file.  The tables used herein are shared with those of the
450 * compress() routine.  See the definitions above.
451 */
452static int
453zread(void *cookie, char *rbp, int num)
454{
455	u_int count;
456	struct s_zstate *zs;
457	u_char *bp, header[3];
458
459	if (num == 0)
460		return (0);
461
462	zs = cookie;
463	count = num;
464	bp = (u_char *)rbp;
465	switch (state) {
466	case S_START:
467		state = S_MIDDLE;
468		break;
469	case S_MIDDLE:
470		goto middle;
471	case S_EOF:
472		goto eof;
473	}
474
475	/* Check the magic number */
476	if (fread(header,
477	    sizeof(char), sizeof(header), fp) != sizeof(header) ||
478	    memcmp(header, magic_header, sizeof(magic_header)) != 0) {
479		errno = EFTYPE;
480		return (-1);
481	}
482	maxbits = header[2];	/* Set -b from file. */
483	block_compress = maxbits & BLOCK_MASK;
484	maxbits &= BIT_MASK;
485	maxmaxcode = 1L << maxbits;
486	if (maxbits > BITS || maxbits < 12) {
487		errno = EFTYPE;
488		return (-1);
489	}
490	/* As above, initialize the first 256 entries in the table. */
491	maxcode = MAXCODE(n_bits = INIT_BITS);
492	for (code = 255; code >= 0; code--) {
493		tab_prefixof(code) = 0;
494		tab_suffixof(code) = (char_type) code;
495	}
496	free_ent = block_compress ? FIRST : 256;
497
498	finchar = oldcode = getcode(zs);
499	if (oldcode == -1)	/* EOF already? */
500		return (0);	/* Get out of here */
501
502	/* First code must be 8 bits = char. */
503	*bp++ = (u_char)finchar;
504	count--;
505	stackp = de_stack;
506
507	while ((code = getcode(zs)) > -1) {
508
509		if ((code == CLEAR) && block_compress) {
510			for (code = 255; code >= 0; code--)
511				tab_prefixof(code) = 0;
512			clear_flg = 1;
513			free_ent = FIRST;
514			oldcode = -1;
515			continue;
516		}
517		incode = code;
518
519		/* Special case for kWkWk string. */
520		if (code >= free_ent) {
521			if (code > free_ent || oldcode == -1) {
522				/* Bad stream. */
523				errno = EINVAL;
524				return (-1);
525			}
526			*stackp++ = finchar;
527			code = oldcode;
528		}
529		/*
530		 * The above condition ensures that code < free_ent.
531		 * The construction of tab_prefixof in turn guarantees that
532		 * each iteration decreases code and therefore stack usage is
533		 * bound by 1 << BITS - 256.
534		 */
535
536		/* Generate output characters in reverse order. */
537		while (code >= 256) {
538			*stackp++ = tab_suffixof(code);
539			code = tab_prefixof(code);
540		}
541		*stackp++ = finchar = tab_suffixof(code);
542
543		/* And put them out in forward order.  */
544middle:		do {
545			if (count-- == 0)
546				return (num);
547			*bp++ = *--stackp;
548		} while (stackp > de_stack);
549
550		/* Generate the new entry. */
551		if ((code = free_ent) < maxmaxcode && oldcode != -1) {
552			tab_prefixof(code) = (u_short) oldcode;
553			tab_suffixof(code) = finchar;
554			free_ent = code + 1;
555		}
556
557		/* Remember previous code. */
558		oldcode = incode;
559	}
560	state = S_EOF;
561eof:	return (num - count);
562}
563
564/*-
565 * Read one code from the standard input.  If EOF, return -1.
566 * Inputs:
567 * 	stdin
568 * Outputs:
569 * 	code or -1 is returned.
570 */
571static code_int
572getcode(struct s_zstate *zs)
573{
574	code_int gcode;
575	int r_off, bits;
576	char_type *bp;
577
578	bp = gbuf;
579	if (clear_flg > 0 || roffset >= size || free_ent > maxcode) {
580		/*
581		 * If the next entry will be too big for the current gcode
582		 * size, then we must increase the size.  This implies reading
583		 * a new buffer full, too.
584		 */
585		if (free_ent > maxcode) {
586			n_bits++;
587			if (n_bits == maxbits)	/* Won't get any bigger now. */
588				maxcode = maxmaxcode;
589			else
590				maxcode = MAXCODE(n_bits);
591		}
592		if (clear_flg > 0) {
593			maxcode = MAXCODE(n_bits = INIT_BITS);
594			clear_flg = 0;
595		}
596		size = fread(gbuf, 1, n_bits, fp);
597		if (size <= 0)			/* End of file. */
598			return (-1);
599		roffset = 0;
600		/* Round size down to integral number of codes. */
601		size = (size << 3) - (n_bits - 1);
602	}
603	r_off = roffset;
604	bits = n_bits;
605
606	/* Get to the first byte. */
607	bp += (r_off >> 3);
608	r_off &= 7;
609
610	/* Get first part (low order bits). */
611	gcode = (*bp++ >> r_off);
612	bits -= (8 - r_off);
613	r_off = 8 - r_off;	/* Now, roffset into gcode word. */
614
615	/* Get any 8 bit parts in the middle (<=1 for up to 16 bits). */
616	if (bits >= 8) {
617		gcode |= *bp++ << r_off;
618		r_off += 8;
619		bits -= 8;
620	}
621
622	/* High order bits. */
623	gcode |= (*bp & rmask[bits]) << r_off;
624	roffset += n_bits;
625
626	return (gcode);
627}
628
629static int
630cl_block(struct s_zstate *zs)		/* Table clear for block compress. */
631{
632	long rat;
633
634	checkpoint = in_count + CHECK_GAP;
635
636	if (in_count > 0x007fffff) {	/* Shift will overflow. */
637		rat = bytes_out >> 8;
638		if (rat == 0)		/* Don't divide by zero. */
639			rat = 0x7fffffff;
640		else
641			rat = in_count / rat;
642	} else
643		rat = (in_count << 8) / bytes_out;	/* 8 fractional bits. */
644	if (rat > ratio)
645		ratio = rat;
646	else {
647		ratio = 0;
648		cl_hash(zs, (count_int) hsize);
649		free_ent = FIRST;
650		clear_flg = 1;
651		if (output(zs, (code_int) CLEAR) == -1)
652			return (-1);
653	}
654	return (0);
655}
656
657static void
658cl_hash(struct s_zstate *zs, count_int cl_hsize)	/* Reset code table. */
659{
660	count_int *htab_p;
661	long i, m1;
662
663	m1 = -1;
664	htab_p = htab + cl_hsize;
665	i = cl_hsize - 16;
666	do {			/* Might use Sys V memset(3) here. */
667		*(htab_p - 16) = m1;
668		*(htab_p - 15) = m1;
669		*(htab_p - 14) = m1;
670		*(htab_p - 13) = m1;
671		*(htab_p - 12) = m1;
672		*(htab_p - 11) = m1;
673		*(htab_p - 10) = m1;
674		*(htab_p - 9) = m1;
675		*(htab_p - 8) = m1;
676		*(htab_p - 7) = m1;
677		*(htab_p - 6) = m1;
678		*(htab_p - 5) = m1;
679		*(htab_p - 4) = m1;
680		*(htab_p - 3) = m1;
681		*(htab_p - 2) = m1;
682		*(htab_p - 1) = m1;
683		htab_p -= 16;
684	} while ((i -= 16) >= 0);
685	for (i += 16; i > 0; i--)
686		*--htab_p = m1;
687}
688
689FILE *
690zopen(const char *fname, const char *mode, int bits)
691{
692	struct s_zstate *zs;
693
694	if ((mode[0] != 'r' && mode[0] != 'w') || mode[1] != '\0' ||
695	    bits < 0 || bits > BITS) {
696		errno = EINVAL;
697		return (NULL);
698	}
699
700	if ((zs = calloc(1, sizeof(struct s_zstate))) == NULL)
701		return (NULL);
702
703	maxbits = bits ? bits : BITS;	/* User settable max # bits/code. */
704	maxmaxcode = 1L << maxbits;	/* Should NEVER generate this code. */
705	hsize = HSIZE;			/* For dynamic table sizing. */
706	free_ent = 0;			/* First unused entry. */
707	block_compress = BLOCK_MASK;
708	clear_flg = 0;
709	ratio = 0;
710	checkpoint = CHECK_GAP;
711	in_count = 1;			/* Length of input. */
712	out_count = 0;			/* # of codes output (for debugging). */
713	state = S_START;
714	roffset = 0;
715	size = 0;
716
717	/*
718	 * Layering compress on top of stdio in order to provide buffering,
719	 * and ensure that reads and write work with the data specified.
720	 */
721	if ((fp = fopen(fname, mode)) == NULL) {
722		free(zs);
723		return (NULL);
724	}
725	switch (*mode) {
726	case 'r':
727		zmode = 'r';
728		return (funopen(zs, zread, NULL, NULL, zclose));
729	case 'w':
730		zmode = 'w';
731		return (funopen(zs, NULL, zwrite, NULL, zclose));
732	}
733	/* NOTREACHED */
734	return (NULL);
735}
736