xref: /freebsd/usr.bin/compress/zopen.c (revision 9f5b04e925d068b053cc41bae3d3c25a5325062c)
19b50d902SRodney W. Grimes /*-
29b50d902SRodney W. Grimes  * Copyright (c) 1985, 1986, 1992, 1993
39b50d902SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
49b50d902SRodney W. Grimes  *
59b50d902SRodney W. Grimes  * This code is derived from software contributed to Berkeley by
69b50d902SRodney W. Grimes  * Diomidis Spinellis and James A. Woods, derived from original
79b50d902SRodney W. Grimes  * work by Spencer Thomas and Joseph Orost.
89b50d902SRodney W. Grimes  *
99b50d902SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
109b50d902SRodney W. Grimes  * modification, are permitted provided that the following conditions
119b50d902SRodney W. Grimes  * are met:
129b50d902SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
139b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
149b50d902SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
159b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
169b50d902SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
179b50d902SRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
189b50d902SRodney W. Grimes  *    must display the following acknowledgement:
199b50d902SRodney W. Grimes  *	This product includes software developed by the University of
209b50d902SRodney W. Grimes  *	California, Berkeley and its contributors.
219b50d902SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
229b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
239b50d902SRodney W. Grimes  *    without specific prior written permission.
249b50d902SRodney W. Grimes  *
259b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
269b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
279b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
289b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
299b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
309b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
319b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
329b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
339b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
349b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
359b50d902SRodney W. Grimes  * SUCH DAMAGE.
369b50d902SRodney W. Grimes  */
379b50d902SRodney W. Grimes 
389b50d902SRodney W. Grimes #if defined(LIBC_SCCS) && !defined(lint)
399b50d902SRodney W. Grimes static char sccsid[] = "@(#)zopen.c	8.1 (Berkeley) 6/27/93";
409b50d902SRodney W. Grimes #endif /* LIBC_SCCS and not lint */
419b50d902SRodney W. Grimes 
429f5b04e9SDavid Malone #include <sys/cdefs.h>
439f5b04e9SDavid Malone __FBSDID("$FreeBSD$");
449f5b04e9SDavid Malone 
459b50d902SRodney W. Grimes /*-
469b50d902SRodney W. Grimes  * fcompress.c - File compression ala IEEE Computer, June 1984.
479b50d902SRodney W. Grimes  *
489b50d902SRodney W. Grimes  * Compress authors:
499b50d902SRodney W. Grimes  *		Spencer W. Thomas	(decvax!utah-cs!thomas)
509b50d902SRodney W. Grimes  *		Jim McKie		(decvax!mcvax!jim)
519b50d902SRodney W. Grimes  *		Steve Davies		(decvax!vax135!petsd!peora!srd)
529b50d902SRodney W. Grimes  *		Ken Turkowski		(decvax!decwrl!turtlevax!ken)
539b50d902SRodney W. Grimes  *		James A. Woods		(decvax!ihnp4!ames!jaw)
549b50d902SRodney W. Grimes  *		Joe Orost		(decvax!vax135!petsd!joe)
559b50d902SRodney W. Grimes  *
569b50d902SRodney W. Grimes  * Cleaned up and converted to library returning I/O streams by
579b50d902SRodney W. Grimes  * Diomidis Spinellis <dds@doc.ic.ac.uk>.
589b50d902SRodney W. Grimes  *
599b50d902SRodney W. Grimes  * zopen(filename, mode, bits)
609b50d902SRodney W. Grimes  *	Returns a FILE * that can be used for read or write.  The modes
619b50d902SRodney W. Grimes  *	supported are only "r" and "w".  Seeking is not allowed.  On
629b50d902SRodney W. Grimes  *	reading the file is decompressed, on writing it is compressed.
639b50d902SRodney W. Grimes  *	The output is compatible with compress(1) with 16 bit tables.
649b50d902SRodney W. Grimes  *	Any file produced by compress(1) can be read.
659b50d902SRodney W. Grimes  */
669b50d902SRodney W. Grimes 
679b50d902SRodney W. Grimes #include <sys/param.h>
689b50d902SRodney W. Grimes #include <sys/stat.h>
699b50d902SRodney W. Grimes 
709b50d902SRodney W. Grimes #include <ctype.h>
719b50d902SRodney W. Grimes #include <errno.h>
729b50d902SRodney W. Grimes #include <signal.h>
739b50d902SRodney W. Grimes #include <stdio.h>
749b50d902SRodney W. Grimes #include <stdlib.h>
759b50d902SRodney W. Grimes #include <string.h>
769b50d902SRodney W. Grimes #include <unistd.h>
776127186dSWolfram Schneider #include "zopen.h"
789b50d902SRodney W. Grimes 
799b50d902SRodney W. Grimes #define	BITS		16		/* Default bits. */
809b50d902SRodney W. Grimes #define	HSIZE		69001		/* 95% occupancy */
819b50d902SRodney W. Grimes 
829b50d902SRodney W. Grimes /* A code_int must be able to hold 2**BITS values of type int, and also -1. */
839b50d902SRodney W. Grimes typedef long code_int;
849b50d902SRodney W. Grimes typedef long count_int;
859b50d902SRodney W. Grimes 
869b50d902SRodney W. Grimes typedef u_char char_type;
879b50d902SRodney W. Grimes static char_type magic_header[] =
889b50d902SRodney W. Grimes 	{'\037', '\235'};		/* 1F 9D */
899b50d902SRodney W. Grimes 
909b50d902SRodney W. Grimes #define	BIT_MASK	0x1f		/* Defines for third byte of header. */
919b50d902SRodney W. Grimes #define	BLOCK_MASK	0x80
929b50d902SRodney W. Grimes 
939b50d902SRodney W. Grimes /*
949b50d902SRodney W. Grimes  * Masks 0x40 and 0x20 are free.  I think 0x20 should mean that there is
959b50d902SRodney W. Grimes  * a fourth header byte (for expansion).
969b50d902SRodney W. Grimes  */
979b50d902SRodney W. Grimes #define	INIT_BITS 9			/* Initial number of bits/code. */
989b50d902SRodney W. Grimes 
999b50d902SRodney W. Grimes #define	MAXCODE(n_bits)	((1 << (n_bits)) - 1)
1009b50d902SRodney W. Grimes 
1019b50d902SRodney W. Grimes struct s_zstate {
1029b50d902SRodney W. Grimes 	FILE *zs_fp;			/* File stream for I/O */
1039b50d902SRodney W. Grimes 	char zs_mode;			/* r or w */
1049b50d902SRodney W. Grimes 	enum {
1059b50d902SRodney W. Grimes 		S_START, S_MIDDLE, S_EOF
1069b50d902SRodney W. Grimes 	} zs_state;			/* State of computation */
107546c7f66SMark Murray 	u_int zs_n_bits;		/* Number of bits/code. */
108546c7f66SMark Murray 	u_int zs_maxbits;		/* User settable max # bits/code. */
1099b50d902SRodney W. Grimes 	code_int zs_maxcode;		/* Maximum code, given n_bits. */
1109b50d902SRodney W. Grimes 	code_int zs_maxmaxcode;		/* Should NEVER generate this code. */
1119b50d902SRodney W. Grimes 	count_int zs_htab [HSIZE];
1129b50d902SRodney W. Grimes 	u_short zs_codetab [HSIZE];
1139b50d902SRodney W. Grimes 	code_int zs_hsize;		/* For dynamic table sizing. */
1149b50d902SRodney W. Grimes 	code_int zs_free_ent;		/* First unused entry. */
1159b50d902SRodney W. Grimes 	/*
1169b50d902SRodney W. Grimes 	 * Block compression parameters -- after all codes are used up,
1179b50d902SRodney W. Grimes 	 * and compression rate changes, start over.
1189b50d902SRodney W. Grimes 	 */
1199b50d902SRodney W. Grimes 	int zs_block_compress;
1209b50d902SRodney W. Grimes 	int zs_clear_flg;
1219b50d902SRodney W. Grimes 	long zs_ratio;
1229b50d902SRodney W. Grimes 	count_int zs_checkpoint;
123546c7f66SMark Murray 	u_int zs_offset;
1249b50d902SRodney W. Grimes 	long zs_in_count;		/* Length of input. */
1259b50d902SRodney W. Grimes 	long zs_bytes_out;		/* Length of compressed output. */
1269b50d902SRodney W. Grimes 	long zs_out_count;		/* # of codes output (for debugging). */
1279b50d902SRodney W. Grimes 	char_type zs_buf[BITS];
1289b50d902SRodney W. Grimes 	union {
1299b50d902SRodney W. Grimes 		struct {
1309b50d902SRodney W. Grimes 			long zs_fcode;
1319b50d902SRodney W. Grimes 			code_int zs_ent;
1329b50d902SRodney W. Grimes 			code_int zs_hsize_reg;
1339b50d902SRodney W. Grimes 			int zs_hshift;
1349b50d902SRodney W. Grimes 		} w;			/* Write paramenters */
1359b50d902SRodney W. Grimes 		struct {
1369b50d902SRodney W. Grimes 			char_type *zs_stackp;
1379b50d902SRodney W. Grimes 			int zs_finchar;
1389b50d902SRodney W. Grimes 			code_int zs_code, zs_oldcode, zs_incode;
1399b50d902SRodney W. Grimes 			int zs_roffset, zs_size;
1409b50d902SRodney W. Grimes 			char_type zs_gbuf[BITS];
1419b50d902SRodney W. Grimes 		} r;			/* Read parameters */
1429b50d902SRodney W. Grimes 	} u;
1439b50d902SRodney W. Grimes };
1449b50d902SRodney W. Grimes 
1459b50d902SRodney W. Grimes /* Definitions to retain old variable names */
1469b50d902SRodney W. Grimes #define	fp		zs->zs_fp
1479b50d902SRodney W. Grimes #define	zmode		zs->zs_mode
1489b50d902SRodney W. Grimes #define	state		zs->zs_state
1499b50d902SRodney W. Grimes #define	n_bits		zs->zs_n_bits
1509b50d902SRodney W. Grimes #define	maxbits		zs->zs_maxbits
1519b50d902SRodney W. Grimes #define	maxcode		zs->zs_maxcode
1529b50d902SRodney W. Grimes #define	maxmaxcode	zs->zs_maxmaxcode
1539b50d902SRodney W. Grimes #define	htab		zs->zs_htab
1549b50d902SRodney W. Grimes #define	codetab		zs->zs_codetab
1559b50d902SRodney W. Grimes #define	hsize		zs->zs_hsize
1569b50d902SRodney W. Grimes #define	free_ent	zs->zs_free_ent
1579b50d902SRodney W. Grimes #define	block_compress	zs->zs_block_compress
1589b50d902SRodney W. Grimes #define	clear_flg	zs->zs_clear_flg
1599b50d902SRodney W. Grimes #define	ratio		zs->zs_ratio
1609b50d902SRodney W. Grimes #define	checkpoint	zs->zs_checkpoint
1619b50d902SRodney W. Grimes #define	offset		zs->zs_offset
1629b50d902SRodney W. Grimes #define	in_count	zs->zs_in_count
1639b50d902SRodney W. Grimes #define	bytes_out	zs->zs_bytes_out
1649b50d902SRodney W. Grimes #define	out_count	zs->zs_out_count
1659b50d902SRodney W. Grimes #define	buf		zs->zs_buf
1669b50d902SRodney W. Grimes #define	fcode		zs->u.w.zs_fcode
1679b50d902SRodney W. Grimes #define	hsize_reg	zs->u.w.zs_hsize_reg
1689b50d902SRodney W. Grimes #define	ent		zs->u.w.zs_ent
1699b50d902SRodney W. Grimes #define	hshift		zs->u.w.zs_hshift
1709b50d902SRodney W. Grimes #define	stackp		zs->u.r.zs_stackp
1719b50d902SRodney W. Grimes #define	finchar		zs->u.r.zs_finchar
1729b50d902SRodney W. Grimes #define	code		zs->u.r.zs_code
1739b50d902SRodney W. Grimes #define	oldcode		zs->u.r.zs_oldcode
1749b50d902SRodney W. Grimes #define	incode		zs->u.r.zs_incode
1759b50d902SRodney W. Grimes #define	roffset		zs->u.r.zs_roffset
1769b50d902SRodney W. Grimes #define	size		zs->u.r.zs_size
1779b50d902SRodney W. Grimes #define	gbuf		zs->u.r.zs_gbuf
1789b50d902SRodney W. Grimes 
1799b50d902SRodney W. Grimes /*
1809b50d902SRodney W. Grimes  * To save much memory, we overlay the table used by compress() with those
1819b50d902SRodney W. Grimes  * used by decompress().  The tab_prefix table is the same size and type as
1829b50d902SRodney W. Grimes  * the codetab.  The tab_suffix table needs 2**BITS characters.  We get this
1839b50d902SRodney W. Grimes  * from the beginning of htab.  The output stack uses the rest of htab, and
1849b50d902SRodney W. Grimes  * contains characters.  There is plenty of room for any possible stack
1859b50d902SRodney W. Grimes  * (stack used to be 8000 characters).
1869b50d902SRodney W. Grimes  */
1879b50d902SRodney W. Grimes 
1889b50d902SRodney W. Grimes #define	htabof(i)	htab[i]
1899b50d902SRodney W. Grimes #define	codetabof(i)	codetab[i]
1909b50d902SRodney W. Grimes 
1919b50d902SRodney W. Grimes #define	tab_prefixof(i)	codetabof(i)
1929b50d902SRodney W. Grimes #define	tab_suffixof(i)	((char_type *)(htab))[i]
1939b50d902SRodney W. Grimes #define	de_stack	((char_type *)&tab_suffixof(1 << BITS))
1949b50d902SRodney W. Grimes 
1959b50d902SRodney W. Grimes #define	CHECK_GAP 10000		/* Ratio check interval. */
1969b50d902SRodney W. Grimes 
1979b50d902SRodney W. Grimes /*
1989b50d902SRodney W. Grimes  * the next two codes should not be changed lightly, as they must not
1999b50d902SRodney W. Grimes  * lie within the contiguous general code space.
2009b50d902SRodney W. Grimes  */
2019b50d902SRodney W. Grimes #define	FIRST	257		/* First free entry. */
2029b50d902SRodney W. Grimes #define	CLEAR	256		/* Table clear output code. */
2039b50d902SRodney W. Grimes 
2049b50d902SRodney W. Grimes static int	cl_block __P((struct s_zstate *));
2059b50d902SRodney W. Grimes static void	cl_hash __P((struct s_zstate *, count_int));
2069b50d902SRodney W. Grimes static code_int	getcode __P((struct s_zstate *));
2079b50d902SRodney W. Grimes static int	output __P((struct s_zstate *, code_int));
2089b50d902SRodney W. Grimes static int	zclose __P((void *));
2099b50d902SRodney W. Grimes static int	zread __P((void *, char *, int));
2109b50d902SRodney W. Grimes static int	zwrite __P((void *, const char *, int));
2119b50d902SRodney W. Grimes 
2129b50d902SRodney W. Grimes /*-
2139b50d902SRodney W. Grimes  * Algorithm from "A Technique for High Performance Data Compression",
2149b50d902SRodney W. Grimes  * Terry A. Welch, IEEE Computer Vol 17, No 6 (June 1984), pp 8-19.
2159b50d902SRodney W. Grimes  *
2169b50d902SRodney W. Grimes  * Algorithm:
2179b50d902SRodney W. Grimes  * 	Modified Lempel-Ziv method (LZW).  Basically finds common
2189b50d902SRodney W. Grimes  * substrings and replaces them with a variable size code.  This is
2199b50d902SRodney W. Grimes  * deterministic, and can be done on the fly.  Thus, the decompression
2209b50d902SRodney W. Grimes  * procedure needs no input table, but tracks the way the table was built.
2219b50d902SRodney W. Grimes  */
2229b50d902SRodney W. Grimes 
2239b50d902SRodney W. Grimes /*-
2249b50d902SRodney W. Grimes  * compress write
2259b50d902SRodney W. Grimes  *
2269b50d902SRodney W. Grimes  * Algorithm:  use open addressing double hashing (no chaining) on the
2279b50d902SRodney W. Grimes  * prefix code / next character combination.  We do a variant of Knuth's
2289b50d902SRodney W. Grimes  * algorithm D (vol. 3, sec. 6.4) along with G. Knott's relatively-prime
2299b50d902SRodney W. Grimes  * secondary probe.  Here, the modular division first probe is gives way
2309b50d902SRodney W. Grimes  * to a faster exclusive-or manipulation.  Also do block compression with
2319b50d902SRodney W. Grimes  * an adaptive reset, whereby the code table is cleared when the compression
2329b50d902SRodney W. Grimes  * ratio decreases, but after the table fills.  The variable-length output
2339b50d902SRodney W. Grimes  * codes are re-sized at this point, and a special CLEAR code is generated
2349b50d902SRodney W. Grimes  * for the decompressor.  Late addition:  construct the table according to
2359b50d902SRodney W. Grimes  * file size for noticeable speed improvement on small files.  Please direct
2369b50d902SRodney W. Grimes  * questions about this implementation to ames!jaw.
2379b50d902SRodney W. Grimes  */
2389b50d902SRodney W. Grimes static int
2399b50d902SRodney W. Grimes zwrite(cookie, wbp, num)
2409b50d902SRodney W. Grimes 	void *cookie;
2419b50d902SRodney W. Grimes 	const char *wbp;
2429b50d902SRodney W. Grimes 	int num;
2439b50d902SRodney W. Grimes {
244cb08795bSMark Murray 	code_int i;
245cb08795bSMark Murray 	int c, disp;
2469b50d902SRodney W. Grimes 	struct s_zstate *zs;
2479b50d902SRodney W. Grimes 	const u_char *bp;
2489b50d902SRodney W. Grimes 	u_char tmp;
2499b50d902SRodney W. Grimes 	int count;
2509b50d902SRodney W. Grimes 
2519b50d902SRodney W. Grimes 	if (num == 0)
2529b50d902SRodney W. Grimes 		return (0);
2539b50d902SRodney W. Grimes 
2549b50d902SRodney W. Grimes 	zs = cookie;
2559b50d902SRodney W. Grimes 	count = num;
256cb08795bSMark Murray 	bp = wbp;
2579b50d902SRodney W. Grimes 	if (state == S_MIDDLE)
2589b50d902SRodney W. Grimes 		goto middle;
2599b50d902SRodney W. Grimes 	state = S_MIDDLE;
2609b50d902SRodney W. Grimes 
2615cf1ec50SAndreas Schulz 	maxmaxcode = 1L << maxbits;
2629b50d902SRodney W. Grimes 	if (fwrite(magic_header,
2639b50d902SRodney W. Grimes 	    sizeof(char), sizeof(magic_header), fp) != sizeof(magic_header))
2649b50d902SRodney W. Grimes 		return (-1);
265556751c1SJordan K. Hubbard 	tmp = (u_char)((maxbits) | block_compress);
2669b50d902SRodney W. Grimes 	if (fwrite(&tmp, sizeof(char), sizeof(tmp), fp) != sizeof(tmp))
2679b50d902SRodney W. Grimes 		return (-1);
2689b50d902SRodney W. Grimes 
2699b50d902SRodney W. Grimes 	offset = 0;
2709b50d902SRodney W. Grimes 	bytes_out = 3;		/* Includes 3-byte header mojo. */
2719b50d902SRodney W. Grimes 	out_count = 0;
2729b50d902SRodney W. Grimes 	clear_flg = 0;
2739b50d902SRodney W. Grimes 	ratio = 0;
2749b50d902SRodney W. Grimes 	in_count = 1;
2759b50d902SRodney W. Grimes 	checkpoint = CHECK_GAP;
2769b50d902SRodney W. Grimes 	maxcode = MAXCODE(n_bits = INIT_BITS);
2779b50d902SRodney W. Grimes 	free_ent = ((block_compress) ? FIRST : 256);
2789b50d902SRodney W. Grimes 
2799b50d902SRodney W. Grimes 	ent = *bp++;
2809b50d902SRodney W. Grimes 	--count;
2819b50d902SRodney W. Grimes 
2829b50d902SRodney W. Grimes 	hshift = 0;
2839b50d902SRodney W. Grimes 	for (fcode = (long)hsize; fcode < 65536L; fcode *= 2L)
2849b50d902SRodney W. Grimes 		hshift++;
2859b50d902SRodney W. Grimes 	hshift = 8 - hshift;	/* Set hash code range bound. */
2869b50d902SRodney W. Grimes 
2879b50d902SRodney W. Grimes 	hsize_reg = hsize;
2889b50d902SRodney W. Grimes 	cl_hash(zs, (count_int)hsize_reg);	/* Clear hash table. */
2899b50d902SRodney W. Grimes 
2909b50d902SRodney W. Grimes middle:	for (i = 0; count--;) {
2919b50d902SRodney W. Grimes 		c = *bp++;
2929b50d902SRodney W. Grimes 		in_count++;
2939b50d902SRodney W. Grimes 		fcode = (long)(((long)c << maxbits) + ent);
2949b50d902SRodney W. Grimes 		i = ((c << hshift) ^ ent);	/* Xor hashing. */
2959b50d902SRodney W. Grimes 
2969b50d902SRodney W. Grimes 		if (htabof(i) == fcode) {
2979b50d902SRodney W. Grimes 			ent = codetabof(i);
2989b50d902SRodney W. Grimes 			continue;
2999b50d902SRodney W. Grimes 		} else if ((long)htabof(i) < 0)	/* Empty slot. */
3009b50d902SRodney W. Grimes 			goto nomatch;
3019b50d902SRodney W. Grimes 		disp = hsize_reg - i;	/* Secondary hash (after G. Knott). */
3029b50d902SRodney W. Grimes 		if (i == 0)
3039b50d902SRodney W. Grimes 			disp = 1;
3049b50d902SRodney W. Grimes probe:		if ((i -= disp) < 0)
3059b50d902SRodney W. Grimes 			i += hsize_reg;
3069b50d902SRodney W. Grimes 
3079b50d902SRodney W. Grimes 		if (htabof(i) == fcode) {
3089b50d902SRodney W. Grimes 			ent = codetabof(i);
3099b50d902SRodney W. Grimes 			continue;
3109b50d902SRodney W. Grimes 		}
3119b50d902SRodney W. Grimes 		if ((long)htabof(i) >= 0)
3129b50d902SRodney W. Grimes 			goto probe;
3139b50d902SRodney W. Grimes nomatch:	if (output(zs, (code_int) ent) == -1)
3149b50d902SRodney W. Grimes 			return (-1);
3159b50d902SRodney W. Grimes 		out_count++;
3169b50d902SRodney W. Grimes 		ent = c;
3179b50d902SRodney W. Grimes 		if (free_ent < maxmaxcode) {
3189b50d902SRodney W. Grimes 			codetabof(i) = free_ent++;	/* code -> hashtable */
3199b50d902SRodney W. Grimes 			htabof(i) = fcode;
3209b50d902SRodney W. Grimes 		} else if ((count_int)in_count >=
3219b50d902SRodney W. Grimes 		    checkpoint && block_compress) {
3229b50d902SRodney W. Grimes 			if (cl_block(zs) == -1)
3239b50d902SRodney W. Grimes 				return (-1);
3249b50d902SRodney W. Grimes 		}
3259b50d902SRodney W. Grimes 	}
3269b50d902SRodney W. Grimes 	return (num);
3279b50d902SRodney W. Grimes }
3289b50d902SRodney W. Grimes 
3299b50d902SRodney W. Grimes static int
3309b50d902SRodney W. Grimes zclose(cookie)
3319b50d902SRodney W. Grimes 	void *cookie;
3329b50d902SRodney W. Grimes {
3339b50d902SRodney W. Grimes 	struct s_zstate *zs;
3349b50d902SRodney W. Grimes 	int rval;
3359b50d902SRodney W. Grimes 
3369b50d902SRodney W. Grimes 	zs = cookie;
3379b50d902SRodney W. Grimes 	if (zmode == 'w') {		/* Put out the final code. */
3389b50d902SRodney W. Grimes 		if (output(zs, (code_int) ent) == -1) {
3399b50d902SRodney W. Grimes 			(void)fclose(fp);
3409b50d902SRodney W. Grimes 			free(zs);
3419b50d902SRodney W. Grimes 			return (-1);
3429b50d902SRodney W. Grimes 		}
3439b50d902SRodney W. Grimes 		out_count++;
3449b50d902SRodney W. Grimes 		if (output(zs, (code_int) - 1) == -1) {
3459b50d902SRodney W. Grimes 			(void)fclose(fp);
3469b50d902SRodney W. Grimes 			free(zs);
3479b50d902SRodney W. Grimes 			return (-1);
3489b50d902SRodney W. Grimes 		}
3499b50d902SRodney W. Grimes 	}
3509b50d902SRodney W. Grimes 	rval = fclose(fp) == EOF ? -1 : 0;
3519b50d902SRodney W. Grimes 	free(zs);
3529b50d902SRodney W. Grimes 	return (rval);
3539b50d902SRodney W. Grimes }
3549b50d902SRodney W. Grimes 
3559b50d902SRodney W. Grimes /*-
3569b50d902SRodney W. Grimes  * Output the given code.
3579b50d902SRodney W. Grimes  * Inputs:
3589b50d902SRodney W. Grimes  * 	code:	A n_bits-bit integer.  If == -1, then EOF.  This assumes
3599b50d902SRodney W. Grimes  *		that n_bits =< (long)wordsize - 1.
3609b50d902SRodney W. Grimes  * Outputs:
3619b50d902SRodney W. Grimes  * 	Outputs code to the file.
3629b50d902SRodney W. Grimes  * Assumptions:
3639b50d902SRodney W. Grimes  *	Chars are 8 bits long.
3649b50d902SRodney W. Grimes  * Algorithm:
3659b50d902SRodney W. Grimes  * 	Maintain a BITS character long buffer (so that 8 codes will
3669b50d902SRodney W. Grimes  * fit in it exactly).  Use the VAX insv instruction to insert each
3679b50d902SRodney W. Grimes  * code in turn.  When the buffer fills up empty it and start over.
3689b50d902SRodney W. Grimes  */
3699b50d902SRodney W. Grimes 
3709b50d902SRodney W. Grimes static char_type lmask[9] =
3719b50d902SRodney W. Grimes 	{0xff, 0xfe, 0xfc, 0xf8, 0xf0, 0xe0, 0xc0, 0x80, 0x00};
3729b50d902SRodney W. Grimes static char_type rmask[9] =
3739b50d902SRodney W. Grimes 	{0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff};
3749b50d902SRodney W. Grimes 
3759b50d902SRodney W. Grimes static int
3769b50d902SRodney W. Grimes output(zs, ocode)
3779b50d902SRodney W. Grimes 	struct s_zstate *zs;
3789b50d902SRodney W. Grimes 	code_int ocode;
3799b50d902SRodney W. Grimes {
380cb08795bSMark Murray 	int r_off;
381546c7f66SMark Murray 	u_int bits;
382cb08795bSMark Murray 	char_type *bp;
3839b50d902SRodney W. Grimes 
3849b50d902SRodney W. Grimes 	r_off = offset;
3859b50d902SRodney W. Grimes 	bits = n_bits;
3869b50d902SRodney W. Grimes 	bp = buf;
3879b50d902SRodney W. Grimes 	if (ocode >= 0) {
3889b50d902SRodney W. Grimes 		/* Get to the first byte. */
3899b50d902SRodney W. Grimes 		bp += (r_off >> 3);
3909b50d902SRodney W. Grimes 		r_off &= 7;
3919b50d902SRodney W. Grimes 		/*
3929b50d902SRodney W. Grimes 		 * Since ocode is always >= 8 bits, only need to mask the first
3939b50d902SRodney W. Grimes 		 * hunk on the left.
3949b50d902SRodney W. Grimes 		 */
395fa146c53SArchie Cobbs 		*bp = (*bp & rmask[r_off]) | ((ocode << r_off) & lmask[r_off]);
3969b50d902SRodney W. Grimes 		bp++;
3979b50d902SRodney W. Grimes 		bits -= (8 - r_off);
3989b50d902SRodney W. Grimes 		ocode >>= 8 - r_off;
3999b50d902SRodney W. Grimes 		/* Get any 8 bit parts in the middle (<=1 for up to 16 bits). */
4009b50d902SRodney W. Grimes 		if (bits >= 8) {
4019b50d902SRodney W. Grimes 			*bp++ = ocode;
4029b50d902SRodney W. Grimes 			ocode >>= 8;
4039b50d902SRodney W. Grimes 			bits -= 8;
4049b50d902SRodney W. Grimes 		}
4059b50d902SRodney W. Grimes 		/* Last bits. */
4069b50d902SRodney W. Grimes 		if (bits)
4079b50d902SRodney W. Grimes 			*bp = ocode;
4089b50d902SRodney W. Grimes 		offset += n_bits;
4099b50d902SRodney W. Grimes 		if (offset == (n_bits << 3)) {
4109b50d902SRodney W. Grimes 			bp = buf;
4119b50d902SRodney W. Grimes 			bits = n_bits;
4129b50d902SRodney W. Grimes 			bytes_out += bits;
4139b50d902SRodney W. Grimes 			if (fwrite(bp, sizeof(char), bits, fp) != bits)
4149b50d902SRodney W. Grimes 				return (-1);
4159b50d902SRodney W. Grimes 			bp += bits;
4169b50d902SRodney W. Grimes 			bits = 0;
4179b50d902SRodney W. Grimes 			offset = 0;
4189b50d902SRodney W. Grimes 		}
4199b50d902SRodney W. Grimes 		/*
4209b50d902SRodney W. Grimes 		 * If the next entry is going to be too big for the ocode size,
4219b50d902SRodney W. Grimes 		 * then increase it, if possible.
4229b50d902SRodney W. Grimes 		 */
4239b50d902SRodney W. Grimes 		if (free_ent > maxcode || (clear_flg > 0)) {
4249b50d902SRodney W. Grimes 		       /*
4259b50d902SRodney W. Grimes 			* Write the whole buffer, because the input side won't
4269b50d902SRodney W. Grimes 			* discover the size increase until after it has read it.
4279b50d902SRodney W. Grimes 			*/
4289b50d902SRodney W. Grimes 			if (offset > 0) {
4299b50d902SRodney W. Grimes 				if (fwrite(buf, 1, n_bits, fp) != n_bits)
4309b50d902SRodney W. Grimes 					return (-1);
4319b50d902SRodney W. Grimes 				bytes_out += n_bits;
4329b50d902SRodney W. Grimes 			}
4339b50d902SRodney W. Grimes 			offset = 0;
4349b50d902SRodney W. Grimes 
4359b50d902SRodney W. Grimes 			if (clear_flg) {
4369b50d902SRodney W. Grimes 				maxcode = MAXCODE(n_bits = INIT_BITS);
4379b50d902SRodney W. Grimes 				clear_flg = 0;
4389b50d902SRodney W. Grimes 			} else {
4399b50d902SRodney W. Grimes 				n_bits++;
4409b50d902SRodney W. Grimes 				if (n_bits == maxbits)
4419b50d902SRodney W. Grimes 					maxcode = maxmaxcode;
4429b50d902SRodney W. Grimes 				else
4439b50d902SRodney W. Grimes 					maxcode = MAXCODE(n_bits);
4449b50d902SRodney W. Grimes 			}
4459b50d902SRodney W. Grimes 		}
4469b50d902SRodney W. Grimes 	} else {
4479b50d902SRodney W. Grimes 		/* At EOF, write the rest of the buffer. */
4489b50d902SRodney W. Grimes 		if (offset > 0) {
4499b50d902SRodney W. Grimes 			offset = (offset + 7) / 8;
4509b50d902SRodney W. Grimes 			if (fwrite(buf, 1, offset, fp) != offset)
4519b50d902SRodney W. Grimes 				return (-1);
4529b50d902SRodney W. Grimes 			bytes_out += offset;
4539b50d902SRodney W. Grimes 		}
4549b50d902SRodney W. Grimes 		offset = 0;
4559b50d902SRodney W. Grimes 	}
4569b50d902SRodney W. Grimes 	return (0);
4579b50d902SRodney W. Grimes }
4589b50d902SRodney W. Grimes 
4599b50d902SRodney W. Grimes /*
4609b50d902SRodney W. Grimes  * Decompress read.  This routine adapts to the codes in the file building
4619b50d902SRodney W. Grimes  * the "string" table on-the-fly; requiring no table to be stored in the
4629b50d902SRodney W. Grimes  * compressed file.  The tables used herein are shared with those of the
4639b50d902SRodney W. Grimes  * compress() routine.  See the definitions above.
4649b50d902SRodney W. Grimes  */
4659b50d902SRodney W. Grimes static int
4669b50d902SRodney W. Grimes zread(cookie, rbp, num)
4679b50d902SRodney W. Grimes 	void *cookie;
4689b50d902SRodney W. Grimes 	char *rbp;
4699b50d902SRodney W. Grimes 	int num;
4709b50d902SRodney W. Grimes {
471cb08795bSMark Murray 	u_int count;
4729b50d902SRodney W. Grimes 	struct s_zstate *zs;
4739b50d902SRodney W. Grimes 	u_char *bp, header[3];
4749b50d902SRodney W. Grimes 
4759b50d902SRodney W. Grimes 	if (num == 0)
4769b50d902SRodney W. Grimes 		return (0);
4779b50d902SRodney W. Grimes 
4789b50d902SRodney W. Grimes 	zs = cookie;
4799b50d902SRodney W. Grimes 	count = num;
4809b50d902SRodney W. Grimes 	bp = (u_char *)rbp;
4819b50d902SRodney W. Grimes 	switch (state) {
4829b50d902SRodney W. Grimes 	case S_START:
4839b50d902SRodney W. Grimes 		state = S_MIDDLE;
4849b50d902SRodney W. Grimes 		break;
4859b50d902SRodney W. Grimes 	case S_MIDDLE:
4869b50d902SRodney W. Grimes 		goto middle;
4879b50d902SRodney W. Grimes 	case S_EOF:
4889b50d902SRodney W. Grimes 		goto eof;
4899b50d902SRodney W. Grimes 	}
4909b50d902SRodney W. Grimes 
4919b50d902SRodney W. Grimes 	/* Check the magic number */
4929b50d902SRodney W. Grimes 	if (fread(header,
4939b50d902SRodney W. Grimes 	    sizeof(char), sizeof(header), fp) != sizeof(header) ||
4949b50d902SRodney W. Grimes 	    memcmp(header, magic_header, sizeof(magic_header)) != 0) {
4959b50d902SRodney W. Grimes 		errno = EFTYPE;
4969b50d902SRodney W. Grimes 		return (-1);
4979b50d902SRodney W. Grimes 	}
4989b50d902SRodney W. Grimes 	maxbits = header[2];	/* Set -b from file. */
4999b50d902SRodney W. Grimes 	block_compress = maxbits & BLOCK_MASK;
5009b50d902SRodney W. Grimes 	maxbits &= BIT_MASK;
5019b50d902SRodney W. Grimes 	maxmaxcode = 1L << maxbits;
5029b50d902SRodney W. Grimes 	if (maxbits > BITS) {
5039b50d902SRodney W. Grimes 		errno = EFTYPE;
5049b50d902SRodney W. Grimes 		return (-1);
5059b50d902SRodney W. Grimes 	}
5069b50d902SRodney W. Grimes 	/* As above, initialize the first 256 entries in the table. */
5079b50d902SRodney W. Grimes 	maxcode = MAXCODE(n_bits = INIT_BITS);
5089b50d902SRodney W. Grimes 	for (code = 255; code >= 0; code--) {
5099b50d902SRodney W. Grimes 		tab_prefixof(code) = 0;
5109b50d902SRodney W. Grimes 		tab_suffixof(code) = (char_type) code;
5119b50d902SRodney W. Grimes 	}
5129b50d902SRodney W. Grimes 	free_ent = block_compress ? FIRST : 256;
5139b50d902SRodney W. Grimes 
5149b50d902SRodney W. Grimes 	finchar = oldcode = getcode(zs);
5159b50d902SRodney W. Grimes 	if (oldcode == -1)	/* EOF already? */
5169b50d902SRodney W. Grimes 		return (0);	/* Get out of here */
5179b50d902SRodney W. Grimes 
5189b50d902SRodney W. Grimes 	/* First code must be 8 bits = char. */
5199b50d902SRodney W. Grimes 	*bp++ = (u_char)finchar;
5209b50d902SRodney W. Grimes 	count--;
5219b50d902SRodney W. Grimes 	stackp = de_stack;
5229b50d902SRodney W. Grimes 
5239b50d902SRodney W. Grimes 	while ((code = getcode(zs)) > -1) {
5249b50d902SRodney W. Grimes 
5259b50d902SRodney W. Grimes 		if ((code == CLEAR) && block_compress) {
5269b50d902SRodney W. Grimes 			for (code = 255; code >= 0; code--)
5279b50d902SRodney W. Grimes 				tab_prefixof(code) = 0;
5289b50d902SRodney W. Grimes 			clear_flg = 1;
5299b50d902SRodney W. Grimes 			free_ent = FIRST - 1;
5309b50d902SRodney W. Grimes 			if ((code = getcode(zs)) == -1)	/* O, untimely death! */
5319b50d902SRodney W. Grimes 				break;
5329b50d902SRodney W. Grimes 		}
5339b50d902SRodney W. Grimes 		incode = code;
5349b50d902SRodney W. Grimes 
5359b50d902SRodney W. Grimes 		/* Special case for KwKwK string. */
5369b50d902SRodney W. Grimes 		if (code >= free_ent) {
5379b50d902SRodney W. Grimes 			*stackp++ = finchar;
5389b50d902SRodney W. Grimes 			code = oldcode;
5399b50d902SRodney W. Grimes 		}
5409b50d902SRodney W. Grimes 
5419b50d902SRodney W. Grimes 		/* Generate output characters in reverse order. */
5429b50d902SRodney W. Grimes 		while (code >= 256) {
5439b50d902SRodney W. Grimes 			*stackp++ = tab_suffixof(code);
5449b50d902SRodney W. Grimes 			code = tab_prefixof(code);
5459b50d902SRodney W. Grimes 		}
5469b50d902SRodney W. Grimes 		*stackp++ = finchar = tab_suffixof(code);
5479b50d902SRodney W. Grimes 
5489b50d902SRodney W. Grimes 		/* And put them out in forward order.  */
5499b50d902SRodney W. Grimes middle:		do {
5509b50d902SRodney W. Grimes 			if (count-- == 0)
5519b50d902SRodney W. Grimes 				return (num);
5529b50d902SRodney W. Grimes 			*bp++ = *--stackp;
5539b50d902SRodney W. Grimes 		} while (stackp > de_stack);
5549b50d902SRodney W. Grimes 
5559b50d902SRodney W. Grimes 		/* Generate the new entry. */
5569b50d902SRodney W. Grimes 		if ((code = free_ent) < maxmaxcode) {
5579b50d902SRodney W. Grimes 			tab_prefixof(code) = (u_short) oldcode;
5589b50d902SRodney W. Grimes 			tab_suffixof(code) = finchar;
5599b50d902SRodney W. Grimes 			free_ent = code + 1;
5609b50d902SRodney W. Grimes 		}
5619b50d902SRodney W. Grimes 
5629b50d902SRodney W. Grimes 		/* Remember previous code. */
5639b50d902SRodney W. Grimes 		oldcode = incode;
5649b50d902SRodney W. Grimes 	}
5659b50d902SRodney W. Grimes 	state = S_EOF;
5669b50d902SRodney W. Grimes eof:	return (num - count);
5679b50d902SRodney W. Grimes }
5689b50d902SRodney W. Grimes 
5699b50d902SRodney W. Grimes /*-
5709b50d902SRodney W. Grimes  * Read one code from the standard input.  If EOF, return -1.
5719b50d902SRodney W. Grimes  * Inputs:
5729b50d902SRodney W. Grimes  * 	stdin
5739b50d902SRodney W. Grimes  * Outputs:
5749b50d902SRodney W. Grimes  * 	code or -1 is returned.
5759b50d902SRodney W. Grimes  */
5769b50d902SRodney W. Grimes static code_int
5779b50d902SRodney W. Grimes getcode(zs)
5789b50d902SRodney W. Grimes 	struct s_zstate *zs;
5799b50d902SRodney W. Grimes {
580cb08795bSMark Murray 	code_int gcode;
581cb08795bSMark Murray 	int r_off, bits;
582cb08795bSMark Murray 	char_type *bp;
5839b50d902SRodney W. Grimes 
5849b50d902SRodney W. Grimes 	bp = gbuf;
5859b50d902SRodney W. Grimes 	if (clear_flg > 0 || roffset >= size || free_ent > maxcode) {
5869b50d902SRodney W. Grimes 		/*
5879b50d902SRodney W. Grimes 		 * If the next entry will be too big for the current gcode
5889b50d902SRodney W. Grimes 		 * size, then we must increase the size.  This implies reading
5899b50d902SRodney W. Grimes 		 * a new buffer full, too.
5909b50d902SRodney W. Grimes 		 */
5919b50d902SRodney W. Grimes 		if (free_ent > maxcode) {
5929b50d902SRodney W. Grimes 			n_bits++;
5939b50d902SRodney W. Grimes 			if (n_bits == maxbits)	/* Won't get any bigger now. */
5949b50d902SRodney W. Grimes 				maxcode = maxmaxcode;
5959b50d902SRodney W. Grimes 			else
5969b50d902SRodney W. Grimes 				maxcode = MAXCODE(n_bits);
5979b50d902SRodney W. Grimes 		}
5989b50d902SRodney W. Grimes 		if (clear_flg > 0) {
5999b50d902SRodney W. Grimes 			maxcode = MAXCODE(n_bits = INIT_BITS);
6009b50d902SRodney W. Grimes 			clear_flg = 0;
6019b50d902SRodney W. Grimes 		}
6029b50d902SRodney W. Grimes 		size = fread(gbuf, 1, n_bits, fp);
6039b50d902SRodney W. Grimes 		if (size <= 0)			/* End of file. */
6049b50d902SRodney W. Grimes 			return (-1);
6059b50d902SRodney W. Grimes 		roffset = 0;
6069b50d902SRodney W. Grimes 		/* Round size down to integral number of codes. */
6079b50d902SRodney W. Grimes 		size = (size << 3) - (n_bits - 1);
6089b50d902SRodney W. Grimes 	}
6099b50d902SRodney W. Grimes 	r_off = roffset;
6109b50d902SRodney W. Grimes 	bits = n_bits;
6119b50d902SRodney W. Grimes 
6129b50d902SRodney W. Grimes 	/* Get to the first byte. */
6139b50d902SRodney W. Grimes 	bp += (r_off >> 3);
6149b50d902SRodney W. Grimes 	r_off &= 7;
6159b50d902SRodney W. Grimes 
6169b50d902SRodney W. Grimes 	/* Get first part (low order bits). */
6179b50d902SRodney W. Grimes 	gcode = (*bp++ >> r_off);
6189b50d902SRodney W. Grimes 	bits -= (8 - r_off);
6199b50d902SRodney W. Grimes 	r_off = 8 - r_off;	/* Now, roffset into gcode word. */
6209b50d902SRodney W. Grimes 
6219b50d902SRodney W. Grimes 	/* Get any 8 bit parts in the middle (<=1 for up to 16 bits). */
6229b50d902SRodney W. Grimes 	if (bits >= 8) {
6239b50d902SRodney W. Grimes 		gcode |= *bp++ << r_off;
6249b50d902SRodney W. Grimes 		r_off += 8;
6259b50d902SRodney W. Grimes 		bits -= 8;
6269b50d902SRodney W. Grimes 	}
6279b50d902SRodney W. Grimes 
6289b50d902SRodney W. Grimes 	/* High order bits. */
6299b50d902SRodney W. Grimes 	gcode |= (*bp & rmask[bits]) << r_off;
6309b50d902SRodney W. Grimes 	roffset += n_bits;
6319b50d902SRodney W. Grimes 
6329b50d902SRodney W. Grimes 	return (gcode);
6339b50d902SRodney W. Grimes }
6349b50d902SRodney W. Grimes 
6359b50d902SRodney W. Grimes static int
6369b50d902SRodney W. Grimes cl_block(zs)			/* Table clear for block compress. */
6379b50d902SRodney W. Grimes 	struct s_zstate *zs;
6389b50d902SRodney W. Grimes {
639cb08795bSMark Murray 	long rat;
6409b50d902SRodney W. Grimes 
6419b50d902SRodney W. Grimes 	checkpoint = in_count + CHECK_GAP;
6429b50d902SRodney W. Grimes 
6439b50d902SRodney W. Grimes 	if (in_count > 0x007fffff) {	/* Shift will overflow. */
6449b50d902SRodney W. Grimes 		rat = bytes_out >> 8;
6459b50d902SRodney W. Grimes 		if (rat == 0)		/* Don't divide by zero. */
6469b50d902SRodney W. Grimes 			rat = 0x7fffffff;
6479b50d902SRodney W. Grimes 		else
6489b50d902SRodney W. Grimes 			rat = in_count / rat;
6499b50d902SRodney W. Grimes 	} else
6509b50d902SRodney W. Grimes 		rat = (in_count << 8) / bytes_out;	/* 8 fractional bits. */
6519b50d902SRodney W. Grimes 	if (rat > ratio)
6529b50d902SRodney W. Grimes 		ratio = rat;
6539b50d902SRodney W. Grimes 	else {
6549b50d902SRodney W. Grimes 		ratio = 0;
6559b50d902SRodney W. Grimes 		cl_hash(zs, (count_int) hsize);
6569b50d902SRodney W. Grimes 		free_ent = FIRST;
6579b50d902SRodney W. Grimes 		clear_flg = 1;
6589b50d902SRodney W. Grimes 		if (output(zs, (code_int) CLEAR) == -1)
6599b50d902SRodney W. Grimes 			return (-1);
6609b50d902SRodney W. Grimes 	}
6619b50d902SRodney W. Grimes 	return (0);
6629b50d902SRodney W. Grimes }
6639b50d902SRodney W. Grimes 
6649b50d902SRodney W. Grimes static void
6659b50d902SRodney W. Grimes cl_hash(zs, cl_hsize)			/* Reset code table. */
6669b50d902SRodney W. Grimes 	struct s_zstate *zs;
667cb08795bSMark Murray 	count_int cl_hsize;
6689b50d902SRodney W. Grimes {
669cb08795bSMark Murray 	count_int *htab_p;
670cb08795bSMark Murray 	long i, m1;
6719b50d902SRodney W. Grimes 
6729b50d902SRodney W. Grimes 	m1 = -1;
6739b50d902SRodney W. Grimes 	htab_p = htab + cl_hsize;
6749b50d902SRodney W. Grimes 	i = cl_hsize - 16;
6759b50d902SRodney W. Grimes 	do {			/* Might use Sys V memset(3) here. */
6769b50d902SRodney W. Grimes 		*(htab_p - 16) = m1;
6779b50d902SRodney W. Grimes 		*(htab_p - 15) = m1;
6789b50d902SRodney W. Grimes 		*(htab_p - 14) = m1;
6799b50d902SRodney W. Grimes 		*(htab_p - 13) = m1;
6809b50d902SRodney W. Grimes 		*(htab_p - 12) = m1;
6819b50d902SRodney W. Grimes 		*(htab_p - 11) = m1;
6829b50d902SRodney W. Grimes 		*(htab_p - 10) = m1;
6839b50d902SRodney W. Grimes 		*(htab_p - 9) = m1;
6849b50d902SRodney W. Grimes 		*(htab_p - 8) = m1;
6859b50d902SRodney W. Grimes 		*(htab_p - 7) = m1;
6869b50d902SRodney W. Grimes 		*(htab_p - 6) = m1;
6879b50d902SRodney W. Grimes 		*(htab_p - 5) = m1;
6889b50d902SRodney W. Grimes 		*(htab_p - 4) = m1;
6899b50d902SRodney W. Grimes 		*(htab_p - 3) = m1;
6909b50d902SRodney W. Grimes 		*(htab_p - 2) = m1;
6919b50d902SRodney W. Grimes 		*(htab_p - 1) = m1;
6929b50d902SRodney W. Grimes 		htab_p -= 16;
6939b50d902SRodney W. Grimes 	} while ((i -= 16) >= 0);
6949b50d902SRodney W. Grimes 	for (i += 16; i > 0; i--)
6959b50d902SRodney W. Grimes 		*--htab_p = m1;
6969b50d902SRodney W. Grimes }
6979b50d902SRodney W. Grimes 
6989b50d902SRodney W. Grimes FILE *
6999b50d902SRodney W. Grimes zopen(fname, mode, bits)
7009b50d902SRodney W. Grimes 	const char *fname, *mode;
7019b50d902SRodney W. Grimes 	int bits;
7029b50d902SRodney W. Grimes {
7039b50d902SRodney W. Grimes 	struct s_zstate *zs;
7049b50d902SRodney W. Grimes 
705fa146c53SArchie Cobbs 	if ((mode[0] != 'r' && mode[0] != 'w') || mode[1] != '\0' ||
7069b50d902SRodney W. Grimes 	    bits < 0 || bits > BITS) {
7079b50d902SRodney W. Grimes 		errno = EINVAL;
7089b50d902SRodney W. Grimes 		return (NULL);
7099b50d902SRodney W. Grimes 	}
7109b50d902SRodney W. Grimes 
7119b50d902SRodney W. Grimes 	if ((zs = calloc(1, sizeof(struct s_zstate))) == NULL)
7129b50d902SRodney W. Grimes 		return (NULL);
7139b50d902SRodney W. Grimes 
7149b50d902SRodney W. Grimes 	maxbits = bits ? bits : BITS;	/* User settable max # bits/code. */
7155cf1ec50SAndreas Schulz 	maxmaxcode = 1L << maxbits;	/* Should NEVER generate this code. */
7169b50d902SRodney W. Grimes 	hsize = HSIZE;			/* For dynamic table sizing. */
7179b50d902SRodney W. Grimes 	free_ent = 0;			/* First unused entry. */
7189b50d902SRodney W. Grimes 	block_compress = BLOCK_MASK;
7199b50d902SRodney W. Grimes 	clear_flg = 0;
7209b50d902SRodney W. Grimes 	ratio = 0;
7219b50d902SRodney W. Grimes 	checkpoint = CHECK_GAP;
7229b50d902SRodney W. Grimes 	in_count = 1;			/* Length of input. */
7239b50d902SRodney W. Grimes 	out_count = 0;			/* # of codes output (for debugging). */
7249b50d902SRodney W. Grimes 	state = S_START;
7259b50d902SRodney W. Grimes 	roffset = 0;
7269b50d902SRodney W. Grimes 	size = 0;
7279b50d902SRodney W. Grimes 
7289b50d902SRodney W. Grimes 	/*
7299b50d902SRodney W. Grimes 	 * Layering compress on top of stdio in order to provide buffering,
7309b50d902SRodney W. Grimes 	 * and ensure that reads and write work with the data specified.
7319b50d902SRodney W. Grimes 	 */
7329b50d902SRodney W. Grimes 	if ((fp = fopen(fname, mode)) == NULL) {
7339b50d902SRodney W. Grimes 		free(zs);
7349b50d902SRodney W. Grimes 		return (NULL);
7359b50d902SRodney W. Grimes 	}
7369b50d902SRodney W. Grimes 	switch (*mode) {
7379b50d902SRodney W. Grimes 	case 'r':
7389b50d902SRodney W. Grimes 		zmode = 'r';
7399b50d902SRodney W. Grimes 		return (funopen(zs, zread, NULL, NULL, zclose));
7409b50d902SRodney W. Grimes 	case 'w':
7419b50d902SRodney W. Grimes 		zmode = 'w';
7429b50d902SRodney W. Grimes 		return (funopen(zs, NULL, zwrite, NULL, zclose));
7439b50d902SRodney W. Grimes 	}
7449b50d902SRodney W. Grimes 	/* NOTREACHED */
745fa146c53SArchie Cobbs 	return (NULL);
7469b50d902SRodney W. Grimes }
747