xref: /freebsd/usr.bin/compress/zopen.c (revision c4fadc2b7845fd39b1660e68a48c5c8239759ca1)
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 
70821df508SXin LI #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>
76821df508SXin LI #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;
134*c4fadc2bSBenedict Reuschling 		} w;			/* Write parameters */
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 
204f1bb2cd2SWarner Losh static int	cl_block(struct s_zstate *);
205f1bb2cd2SWarner Losh static void	cl_hash(struct s_zstate *, count_int);
206f1bb2cd2SWarner Losh static code_int	getcode(struct s_zstate *);
207f1bb2cd2SWarner Losh static int	output(struct s_zstate *, code_int);
208f1bb2cd2SWarner Losh static int	zclose(void *);
209f1bb2cd2SWarner Losh static int	zread(void *, char *, int);
210f1bb2cd2SWarner Losh static int	zwrite(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
23947e9e6a1SDavid Malone zwrite(void *cookie, const char *wbp, int num)
2409b50d902SRodney W. Grimes {
241cb08795bSMark Murray 	code_int i;
242cb08795bSMark Murray 	int c, disp;
2439b50d902SRodney W. Grimes 	struct s_zstate *zs;
24439be1552SAlexander Kabaev 	const u_char *bp;
2459b50d902SRodney W. Grimes 	u_char tmp;
2469b50d902SRodney W. Grimes 	int count;
2479b50d902SRodney W. Grimes 
2489b50d902SRodney W. Grimes 	if (num == 0)
2499b50d902SRodney W. Grimes 		return (0);
2509b50d902SRodney W. Grimes 
2519b50d902SRodney W. Grimes 	zs = cookie;
2529b50d902SRodney W. Grimes 	count = num;
25339be1552SAlexander Kabaev 	bp = (const u_char *)wbp;
2549b50d902SRodney W. Grimes 	if (state == S_MIDDLE)
2559b50d902SRodney W. Grimes 		goto middle;
2569b50d902SRodney W. Grimes 	state = S_MIDDLE;
2579b50d902SRodney W. Grimes 
2585cf1ec50SAndreas Schulz 	maxmaxcode = 1L << maxbits;
2599b50d902SRodney W. Grimes 	if (fwrite(magic_header,
2609b50d902SRodney W. Grimes 	    sizeof(char), sizeof(magic_header), fp) != sizeof(magic_header))
2619b50d902SRodney W. Grimes 		return (-1);
262556751c1SJordan K. Hubbard 	tmp = (u_char)((maxbits) | block_compress);
2639b50d902SRodney W. Grimes 	if (fwrite(&tmp, sizeof(char), sizeof(tmp), fp) != sizeof(tmp))
2649b50d902SRodney W. Grimes 		return (-1);
2659b50d902SRodney W. Grimes 
2669b50d902SRodney W. Grimes 	offset = 0;
2679b50d902SRodney W. Grimes 	bytes_out = 3;		/* Includes 3-byte header mojo. */
2689b50d902SRodney W. Grimes 	out_count = 0;
2699b50d902SRodney W. Grimes 	clear_flg = 0;
2709b50d902SRodney W. Grimes 	ratio = 0;
2719b50d902SRodney W. Grimes 	in_count = 1;
2729b50d902SRodney W. Grimes 	checkpoint = CHECK_GAP;
2739b50d902SRodney W. Grimes 	maxcode = MAXCODE(n_bits = INIT_BITS);
2749b50d902SRodney W. Grimes 	free_ent = ((block_compress) ? FIRST : 256);
2759b50d902SRodney W. Grimes 
2769b50d902SRodney W. Grimes 	ent = *bp++;
2779b50d902SRodney W. Grimes 	--count;
2789b50d902SRodney W. Grimes 
2799b50d902SRodney W. Grimes 	hshift = 0;
2809b50d902SRodney W. Grimes 	for (fcode = (long)hsize; fcode < 65536L; fcode *= 2L)
2819b50d902SRodney W. Grimes 		hshift++;
2829b50d902SRodney W. Grimes 	hshift = 8 - hshift;	/* Set hash code range bound. */
2839b50d902SRodney W. Grimes 
2849b50d902SRodney W. Grimes 	hsize_reg = hsize;
2859b50d902SRodney W. Grimes 	cl_hash(zs, (count_int)hsize_reg);	/* Clear hash table. */
2869b50d902SRodney W. Grimes 
2879b50d902SRodney W. Grimes middle:	for (i = 0; count--;) {
2889b50d902SRodney W. Grimes 		c = *bp++;
2899b50d902SRodney W. Grimes 		in_count++;
2909b50d902SRodney W. Grimes 		fcode = (long)(((long)c << maxbits) + ent);
2919b50d902SRodney W. Grimes 		i = ((c << hshift) ^ ent);	/* Xor hashing. */
2929b50d902SRodney W. Grimes 
2939b50d902SRodney W. Grimes 		if (htabof(i) == fcode) {
2949b50d902SRodney W. Grimes 			ent = codetabof(i);
2959b50d902SRodney W. Grimes 			continue;
2969b50d902SRodney W. Grimes 		} else if ((long)htabof(i) < 0)	/* Empty slot. */
2979b50d902SRodney W. Grimes 			goto nomatch;
2989b50d902SRodney W. Grimes 		disp = hsize_reg - i;	/* Secondary hash (after G. Knott). */
2999b50d902SRodney W. Grimes 		if (i == 0)
3009b50d902SRodney W. Grimes 			disp = 1;
3019b50d902SRodney W. Grimes probe:		if ((i -= disp) < 0)
3029b50d902SRodney W. Grimes 			i += hsize_reg;
3039b50d902SRodney W. Grimes 
3049b50d902SRodney W. Grimes 		if (htabof(i) == fcode) {
3059b50d902SRodney W. Grimes 			ent = codetabof(i);
3069b50d902SRodney W. Grimes 			continue;
3079b50d902SRodney W. Grimes 		}
3089b50d902SRodney W. Grimes 		if ((long)htabof(i) >= 0)
3099b50d902SRodney W. Grimes 			goto probe;
3109b50d902SRodney W. Grimes nomatch:	if (output(zs, (code_int) ent) == -1)
3119b50d902SRodney W. Grimes 			return (-1);
3129b50d902SRodney W. Grimes 		out_count++;
3139b50d902SRodney W. Grimes 		ent = c;
3149b50d902SRodney W. Grimes 		if (free_ent < maxmaxcode) {
3159b50d902SRodney W. Grimes 			codetabof(i) = free_ent++;	/* code -> hashtable */
3169b50d902SRodney W. Grimes 			htabof(i) = fcode;
3179b50d902SRodney W. Grimes 		} else if ((count_int)in_count >=
3189b50d902SRodney W. Grimes 		    checkpoint && block_compress) {
3199b50d902SRodney W. Grimes 			if (cl_block(zs) == -1)
3209b50d902SRodney W. Grimes 				return (-1);
3219b50d902SRodney W. Grimes 		}
3229b50d902SRodney W. Grimes 	}
3239b50d902SRodney W. Grimes 	return (num);
3249b50d902SRodney W. Grimes }
3259b50d902SRodney W. Grimes 
3269b50d902SRodney W. Grimes static int
32747e9e6a1SDavid Malone zclose(void *cookie)
3289b50d902SRodney W. Grimes {
3299b50d902SRodney W. Grimes 	struct s_zstate *zs;
3309b50d902SRodney W. Grimes 	int rval;
3319b50d902SRodney W. Grimes 
3329b50d902SRodney W. Grimes 	zs = cookie;
3339b50d902SRodney W. Grimes 	if (zmode == 'w') {		/* Put out the final code. */
3349b50d902SRodney W. Grimes 		if (output(zs, (code_int) ent) == -1) {
3359b50d902SRodney W. Grimes 			(void)fclose(fp);
3369b50d902SRodney W. Grimes 			free(zs);
3379b50d902SRodney W. Grimes 			return (-1);
3389b50d902SRodney W. Grimes 		}
3399b50d902SRodney W. Grimes 		out_count++;
3409b50d902SRodney W. Grimes 		if (output(zs, (code_int) - 1) == -1) {
3419b50d902SRodney W. Grimes 			(void)fclose(fp);
3429b50d902SRodney W. Grimes 			free(zs);
3439b50d902SRodney W. Grimes 			return (-1);
3449b50d902SRodney W. Grimes 		}
3459b50d902SRodney W. Grimes 	}
3469b50d902SRodney W. Grimes 	rval = fclose(fp) == EOF ? -1 : 0;
3479b50d902SRodney W. Grimes 	free(zs);
3489b50d902SRodney W. Grimes 	return (rval);
3499b50d902SRodney W. Grimes }
3509b50d902SRodney W. Grimes 
3519b50d902SRodney W. Grimes /*-
3529b50d902SRodney W. Grimes  * Output the given code.
3539b50d902SRodney W. Grimes  * Inputs:
3549b50d902SRodney W. Grimes  * 	code:	A n_bits-bit integer.  If == -1, then EOF.  This assumes
3559b50d902SRodney W. Grimes  *		that n_bits =< (long)wordsize - 1.
3569b50d902SRodney W. Grimes  * Outputs:
3579b50d902SRodney W. Grimes  * 	Outputs code to the file.
3589b50d902SRodney W. Grimes  * Assumptions:
3599b50d902SRodney W. Grimes  *	Chars are 8 bits long.
3609b50d902SRodney W. Grimes  * Algorithm:
3619b50d902SRodney W. Grimes  * 	Maintain a BITS character long buffer (so that 8 codes will
3629b50d902SRodney W. Grimes  * fit in it exactly).  Use the VAX insv instruction to insert each
3639b50d902SRodney W. Grimes  * code in turn.  When the buffer fills up empty it and start over.
3649b50d902SRodney W. Grimes  */
3659b50d902SRodney W. Grimes 
3669b50d902SRodney W. Grimes static char_type lmask[9] =
3679b50d902SRodney W. Grimes 	{0xff, 0xfe, 0xfc, 0xf8, 0xf0, 0xe0, 0xc0, 0x80, 0x00};
3689b50d902SRodney W. Grimes static char_type rmask[9] =
3699b50d902SRodney W. Grimes 	{0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff};
3709b50d902SRodney W. Grimes 
3719b50d902SRodney W. Grimes static int
37247e9e6a1SDavid Malone output(struct s_zstate *zs, code_int ocode)
3739b50d902SRodney W. Grimes {
374cb08795bSMark Murray 	int r_off;
375546c7f66SMark Murray 	u_int bits;
376cb08795bSMark Murray 	char_type *bp;
3779b50d902SRodney W. Grimes 
3789b50d902SRodney W. Grimes 	r_off = offset;
3799b50d902SRodney W. Grimes 	bits = n_bits;
3809b50d902SRodney W. Grimes 	bp = buf;
3819b50d902SRodney W. Grimes 	if (ocode >= 0) {
3829b50d902SRodney W. Grimes 		/* Get to the first byte. */
3839b50d902SRodney W. Grimes 		bp += (r_off >> 3);
3849b50d902SRodney W. Grimes 		r_off &= 7;
3859b50d902SRodney W. Grimes 		/*
3869b50d902SRodney W. Grimes 		 * Since ocode is always >= 8 bits, only need to mask the first
3879b50d902SRodney W. Grimes 		 * hunk on the left.
3889b50d902SRodney W. Grimes 		 */
389fa146c53SArchie Cobbs 		*bp = (*bp & rmask[r_off]) | ((ocode << r_off) & lmask[r_off]);
3909b50d902SRodney W. Grimes 		bp++;
3919b50d902SRodney W. Grimes 		bits -= (8 - r_off);
3929b50d902SRodney W. Grimes 		ocode >>= 8 - r_off;
3939b50d902SRodney W. Grimes 		/* Get any 8 bit parts in the middle (<=1 for up to 16 bits). */
3949b50d902SRodney W. Grimes 		if (bits >= 8) {
3959b50d902SRodney W. Grimes 			*bp++ = ocode;
3969b50d902SRodney W. Grimes 			ocode >>= 8;
3979b50d902SRodney W. Grimes 			bits -= 8;
3989b50d902SRodney W. Grimes 		}
3999b50d902SRodney W. Grimes 		/* Last bits. */
4009b50d902SRodney W. Grimes 		if (bits)
4019b50d902SRodney W. Grimes 			*bp = ocode;
4029b50d902SRodney W. Grimes 		offset += n_bits;
4039b50d902SRodney W. Grimes 		if (offset == (n_bits << 3)) {
4049b50d902SRodney W. Grimes 			bp = buf;
4059b50d902SRodney W. Grimes 			bits = n_bits;
4069b50d902SRodney W. Grimes 			bytes_out += bits;
4079b50d902SRodney W. Grimes 			if (fwrite(bp, sizeof(char), bits, fp) != bits)
4089b50d902SRodney W. Grimes 				return (-1);
4099b50d902SRodney W. Grimes 			bp += bits;
4109b50d902SRodney W. Grimes 			bits = 0;
4119b50d902SRodney W. Grimes 			offset = 0;
4129b50d902SRodney W. Grimes 		}
4139b50d902SRodney W. Grimes 		/*
4149b50d902SRodney W. Grimes 		 * If the next entry is going to be too big for the ocode size,
4159b50d902SRodney W. Grimes 		 * then increase it, if possible.
4169b50d902SRodney W. Grimes 		 */
4179b50d902SRodney W. Grimes 		if (free_ent > maxcode || (clear_flg > 0)) {
4189b50d902SRodney W. Grimes 		       /*
4199b50d902SRodney W. Grimes 			* Write the whole buffer, because the input side won't
4209b50d902SRodney W. Grimes 			* discover the size increase until after it has read it.
4219b50d902SRodney W. Grimes 			*/
4229b50d902SRodney W. Grimes 			if (offset > 0) {
4239b50d902SRodney W. Grimes 				if (fwrite(buf, 1, n_bits, fp) != n_bits)
4249b50d902SRodney W. Grimes 					return (-1);
4259b50d902SRodney W. Grimes 				bytes_out += n_bits;
4269b50d902SRodney W. Grimes 			}
4279b50d902SRodney W. Grimes 			offset = 0;
4289b50d902SRodney W. Grimes 
4299b50d902SRodney W. Grimes 			if (clear_flg) {
4309b50d902SRodney W. Grimes 				maxcode = MAXCODE(n_bits = INIT_BITS);
4319b50d902SRodney W. Grimes 				clear_flg = 0;
4329b50d902SRodney W. Grimes 			} else {
4339b50d902SRodney W. Grimes 				n_bits++;
4349b50d902SRodney W. Grimes 				if (n_bits == maxbits)
4359b50d902SRodney W. Grimes 					maxcode = maxmaxcode;
4369b50d902SRodney W. Grimes 				else
4379b50d902SRodney W. Grimes 					maxcode = MAXCODE(n_bits);
4389b50d902SRodney W. Grimes 			}
4399b50d902SRodney W. Grimes 		}
4409b50d902SRodney W. Grimes 	} else {
4419b50d902SRodney W. Grimes 		/* At EOF, write the rest of the buffer. */
4429b50d902SRodney W. Grimes 		if (offset > 0) {
4439b50d902SRodney W. Grimes 			offset = (offset + 7) / 8;
4449b50d902SRodney W. Grimes 			if (fwrite(buf, 1, offset, fp) != offset)
4459b50d902SRodney W. Grimes 				return (-1);
4469b50d902SRodney W. Grimes 			bytes_out += offset;
4479b50d902SRodney W. Grimes 		}
4489b50d902SRodney W. Grimes 		offset = 0;
4499b50d902SRodney W. Grimes 	}
4509b50d902SRodney W. Grimes 	return (0);
4519b50d902SRodney W. Grimes }
4529b50d902SRodney W. Grimes 
4539b50d902SRodney W. Grimes /*
4549b50d902SRodney W. Grimes  * Decompress read.  This routine adapts to the codes in the file building
4559b50d902SRodney W. Grimes  * the "string" table on-the-fly; requiring no table to be stored in the
4569b50d902SRodney W. Grimes  * compressed file.  The tables used herein are shared with those of the
4579b50d902SRodney W. Grimes  * compress() routine.  See the definitions above.
4589b50d902SRodney W. Grimes  */
4599b50d902SRodney W. Grimes static int
46047e9e6a1SDavid Malone zread(void *cookie, char *rbp, int num)
4619b50d902SRodney W. Grimes {
462cb08795bSMark Murray 	u_int count;
4639b50d902SRodney W. Grimes 	struct s_zstate *zs;
4649b50d902SRodney W. Grimes 	u_char *bp, header[3];
4659b50d902SRodney W. Grimes 
4669b50d902SRodney W. Grimes 	if (num == 0)
4679b50d902SRodney W. Grimes 		return (0);
4689b50d902SRodney W. Grimes 
4699b50d902SRodney W. Grimes 	zs = cookie;
4709b50d902SRodney W. Grimes 	count = num;
4719b50d902SRodney W. Grimes 	bp = (u_char *)rbp;
4729b50d902SRodney W. Grimes 	switch (state) {
4739b50d902SRodney W. Grimes 	case S_START:
4749b50d902SRodney W. Grimes 		state = S_MIDDLE;
4759b50d902SRodney W. Grimes 		break;
4769b50d902SRodney W. Grimes 	case S_MIDDLE:
4779b50d902SRodney W. Grimes 		goto middle;
4789b50d902SRodney W. Grimes 	case S_EOF:
4799b50d902SRodney W. Grimes 		goto eof;
4809b50d902SRodney W. Grimes 	}
4819b50d902SRodney W. Grimes 
4829b50d902SRodney W. Grimes 	/* Check the magic number */
4839b50d902SRodney W. Grimes 	if (fread(header,
4849b50d902SRodney W. Grimes 	    sizeof(char), sizeof(header), fp) != sizeof(header) ||
4859b50d902SRodney W. Grimes 	    memcmp(header, magic_header, sizeof(magic_header)) != 0) {
4869b50d902SRodney W. Grimes 		errno = EFTYPE;
4879b50d902SRodney W. Grimes 		return (-1);
4889b50d902SRodney W. Grimes 	}
4899b50d902SRodney W. Grimes 	maxbits = header[2];	/* Set -b from file. */
4909b50d902SRodney W. Grimes 	block_compress = maxbits & BLOCK_MASK;
4919b50d902SRodney W. Grimes 	maxbits &= BIT_MASK;
4929b50d902SRodney W. Grimes 	maxmaxcode = 1L << maxbits;
4939b50d902SRodney W. Grimes 	if (maxbits > BITS) {
4949b50d902SRodney W. Grimes 		errno = EFTYPE;
4959b50d902SRodney W. Grimes 		return (-1);
4969b50d902SRodney W. Grimes 	}
4979b50d902SRodney W. Grimes 	/* As above, initialize the first 256 entries in the table. */
4989b50d902SRodney W. Grimes 	maxcode = MAXCODE(n_bits = INIT_BITS);
4999b50d902SRodney W. Grimes 	for (code = 255; code >= 0; code--) {
5009b50d902SRodney W. Grimes 		tab_prefixof(code) = 0;
5019b50d902SRodney W. Grimes 		tab_suffixof(code) = (char_type) code;
5029b50d902SRodney W. Grimes 	}
5039b50d902SRodney W. Grimes 	free_ent = block_compress ? FIRST : 256;
5049b50d902SRodney W. Grimes 
5059b50d902SRodney W. Grimes 	finchar = oldcode = getcode(zs);
5069b50d902SRodney W. Grimes 	if (oldcode == -1)	/* EOF already? */
5079b50d902SRodney W. Grimes 		return (0);	/* Get out of here */
5089b50d902SRodney W. Grimes 
5099b50d902SRodney W. Grimes 	/* First code must be 8 bits = char. */
5109b50d902SRodney W. Grimes 	*bp++ = (u_char)finchar;
5119b50d902SRodney W. Grimes 	count--;
5129b50d902SRodney W. Grimes 	stackp = de_stack;
5139b50d902SRodney W. Grimes 
5149b50d902SRodney W. Grimes 	while ((code = getcode(zs)) > -1) {
5159b50d902SRodney W. Grimes 
5169b50d902SRodney W. Grimes 		if ((code == CLEAR) && block_compress) {
5179b50d902SRodney W. Grimes 			for (code = 255; code >= 0; code--)
5189b50d902SRodney W. Grimes 				tab_prefixof(code) = 0;
5199b50d902SRodney W. Grimes 			clear_flg = 1;
5209b50d902SRodney W. Grimes 			free_ent = FIRST - 1;
5219b50d902SRodney W. Grimes 			if ((code = getcode(zs)) == -1)	/* O, untimely death! */
5229b50d902SRodney W. Grimes 				break;
5239b50d902SRodney W. Grimes 		}
5249b50d902SRodney W. Grimes 		incode = code;
5259b50d902SRodney W. Grimes 
5269b50d902SRodney W. Grimes 		/* Special case for KwKwK string. */
5279b50d902SRodney W. Grimes 		if (code >= free_ent) {
5289b50d902SRodney W. Grimes 			*stackp++ = finchar;
5299b50d902SRodney W. Grimes 			code = oldcode;
5309b50d902SRodney W. Grimes 		}
5319b50d902SRodney W. Grimes 
5329b50d902SRodney W. Grimes 		/* Generate output characters in reverse order. */
5339b50d902SRodney W. Grimes 		while (code >= 256) {
5349b50d902SRodney W. Grimes 			*stackp++ = tab_suffixof(code);
5359b50d902SRodney W. Grimes 			code = tab_prefixof(code);
5369b50d902SRodney W. Grimes 		}
5379b50d902SRodney W. Grimes 		*stackp++ = finchar = tab_suffixof(code);
5389b50d902SRodney W. Grimes 
5399b50d902SRodney W. Grimes 		/* And put them out in forward order.  */
5409b50d902SRodney W. Grimes middle:		do {
5419b50d902SRodney W. Grimes 			if (count-- == 0)
5429b50d902SRodney W. Grimes 				return (num);
5439b50d902SRodney W. Grimes 			*bp++ = *--stackp;
5449b50d902SRodney W. Grimes 		} while (stackp > de_stack);
5459b50d902SRodney W. Grimes 
5469b50d902SRodney W. Grimes 		/* Generate the new entry. */
5479b50d902SRodney W. Grimes 		if ((code = free_ent) < maxmaxcode) {
5489b50d902SRodney W. Grimes 			tab_prefixof(code) = (u_short) oldcode;
5499b50d902SRodney W. Grimes 			tab_suffixof(code) = finchar;
5509b50d902SRodney W. Grimes 			free_ent = code + 1;
5519b50d902SRodney W. Grimes 		}
5529b50d902SRodney W. Grimes 
5539b50d902SRodney W. Grimes 		/* Remember previous code. */
5549b50d902SRodney W. Grimes 		oldcode = incode;
5559b50d902SRodney W. Grimes 	}
5569b50d902SRodney W. Grimes 	state = S_EOF;
5579b50d902SRodney W. Grimes eof:	return (num - count);
5589b50d902SRodney W. Grimes }
5599b50d902SRodney W. Grimes 
5609b50d902SRodney W. Grimes /*-
5619b50d902SRodney W. Grimes  * Read one code from the standard input.  If EOF, return -1.
5629b50d902SRodney W. Grimes  * Inputs:
5639b50d902SRodney W. Grimes  * 	stdin
5649b50d902SRodney W. Grimes  * Outputs:
5659b50d902SRodney W. Grimes  * 	code or -1 is returned.
5669b50d902SRodney W. Grimes  */
5679b50d902SRodney W. Grimes static code_int
56847e9e6a1SDavid Malone getcode(struct s_zstate *zs)
5699b50d902SRodney W. Grimes {
570cb08795bSMark Murray 	code_int gcode;
571cb08795bSMark Murray 	int r_off, bits;
572cb08795bSMark Murray 	char_type *bp;
5739b50d902SRodney W. Grimes 
5749b50d902SRodney W. Grimes 	bp = gbuf;
5759b50d902SRodney W. Grimes 	if (clear_flg > 0 || roffset >= size || free_ent > maxcode) {
5769b50d902SRodney W. Grimes 		/*
5779b50d902SRodney W. Grimes 		 * If the next entry will be too big for the current gcode
5789b50d902SRodney W. Grimes 		 * size, then we must increase the size.  This implies reading
5799b50d902SRodney W. Grimes 		 * a new buffer full, too.
5809b50d902SRodney W. Grimes 		 */
5819b50d902SRodney W. Grimes 		if (free_ent > maxcode) {
5829b50d902SRodney W. Grimes 			n_bits++;
5839b50d902SRodney W. Grimes 			if (n_bits == maxbits)	/* Won't get any bigger now. */
5849b50d902SRodney W. Grimes 				maxcode = maxmaxcode;
5859b50d902SRodney W. Grimes 			else
5869b50d902SRodney W. Grimes 				maxcode = MAXCODE(n_bits);
5879b50d902SRodney W. Grimes 		}
5889b50d902SRodney W. Grimes 		if (clear_flg > 0) {
5899b50d902SRodney W. Grimes 			maxcode = MAXCODE(n_bits = INIT_BITS);
5909b50d902SRodney W. Grimes 			clear_flg = 0;
5919b50d902SRodney W. Grimes 		}
5929b50d902SRodney W. Grimes 		size = fread(gbuf, 1, n_bits, fp);
5939b50d902SRodney W. Grimes 		if (size <= 0)			/* End of file. */
5949b50d902SRodney W. Grimes 			return (-1);
5959b50d902SRodney W. Grimes 		roffset = 0;
5969b50d902SRodney W. Grimes 		/* Round size down to integral number of codes. */
5979b50d902SRodney W. Grimes 		size = (size << 3) - (n_bits - 1);
5989b50d902SRodney W. Grimes 	}
5999b50d902SRodney W. Grimes 	r_off = roffset;
6009b50d902SRodney W. Grimes 	bits = n_bits;
6019b50d902SRodney W. Grimes 
6029b50d902SRodney W. Grimes 	/* Get to the first byte. */
6039b50d902SRodney W. Grimes 	bp += (r_off >> 3);
6049b50d902SRodney W. Grimes 	r_off &= 7;
6059b50d902SRodney W. Grimes 
6069b50d902SRodney W. Grimes 	/* Get first part (low order bits). */
6079b50d902SRodney W. Grimes 	gcode = (*bp++ >> r_off);
6089b50d902SRodney W. Grimes 	bits -= (8 - r_off);
6099b50d902SRodney W. Grimes 	r_off = 8 - r_off;	/* Now, roffset into gcode word. */
6109b50d902SRodney W. Grimes 
6119b50d902SRodney W. Grimes 	/* Get any 8 bit parts in the middle (<=1 for up to 16 bits). */
6129b50d902SRodney W. Grimes 	if (bits >= 8) {
6139b50d902SRodney W. Grimes 		gcode |= *bp++ << r_off;
6149b50d902SRodney W. Grimes 		r_off += 8;
6159b50d902SRodney W. Grimes 		bits -= 8;
6169b50d902SRodney W. Grimes 	}
6179b50d902SRodney W. Grimes 
6189b50d902SRodney W. Grimes 	/* High order bits. */
6199b50d902SRodney W. Grimes 	gcode |= (*bp & rmask[bits]) << r_off;
6209b50d902SRodney W. Grimes 	roffset += n_bits;
6219b50d902SRodney W. Grimes 
6229b50d902SRodney W. Grimes 	return (gcode);
6239b50d902SRodney W. Grimes }
6249b50d902SRodney W. Grimes 
6259b50d902SRodney W. Grimes static int
62647e9e6a1SDavid Malone cl_block(struct s_zstate *zs)		/* Table clear for block compress. */
6279b50d902SRodney W. Grimes {
628cb08795bSMark Murray 	long rat;
6299b50d902SRodney W. Grimes 
6309b50d902SRodney W. Grimes 	checkpoint = in_count + CHECK_GAP;
6319b50d902SRodney W. Grimes 
6329b50d902SRodney W. Grimes 	if (in_count > 0x007fffff) {	/* Shift will overflow. */
6339b50d902SRodney W. Grimes 		rat = bytes_out >> 8;
6349b50d902SRodney W. Grimes 		if (rat == 0)		/* Don't divide by zero. */
6359b50d902SRodney W. Grimes 			rat = 0x7fffffff;
6369b50d902SRodney W. Grimes 		else
6379b50d902SRodney W. Grimes 			rat = in_count / rat;
6389b50d902SRodney W. Grimes 	} else
6399b50d902SRodney W. Grimes 		rat = (in_count << 8) / bytes_out;	/* 8 fractional bits. */
6409b50d902SRodney W. Grimes 	if (rat > ratio)
6419b50d902SRodney W. Grimes 		ratio = rat;
6429b50d902SRodney W. Grimes 	else {
6439b50d902SRodney W. Grimes 		ratio = 0;
6449b50d902SRodney W. Grimes 		cl_hash(zs, (count_int) hsize);
6459b50d902SRodney W. Grimes 		free_ent = FIRST;
6469b50d902SRodney W. Grimes 		clear_flg = 1;
6479b50d902SRodney W. Grimes 		if (output(zs, (code_int) CLEAR) == -1)
6489b50d902SRodney W. Grimes 			return (-1);
6499b50d902SRodney W. Grimes 	}
6509b50d902SRodney W. Grimes 	return (0);
6519b50d902SRodney W. Grimes }
6529b50d902SRodney W. Grimes 
6539b50d902SRodney W. Grimes static void
65447e9e6a1SDavid Malone cl_hash(struct s_zstate *zs, count_int cl_hsize)	/* Reset code table. */
6559b50d902SRodney W. Grimes {
656cb08795bSMark Murray 	count_int *htab_p;
657cb08795bSMark Murray 	long i, m1;
6589b50d902SRodney W. Grimes 
6599b50d902SRodney W. Grimes 	m1 = -1;
6609b50d902SRodney W. Grimes 	htab_p = htab + cl_hsize;
6619b50d902SRodney W. Grimes 	i = cl_hsize - 16;
6629b50d902SRodney W. Grimes 	do {			/* Might use Sys V memset(3) here. */
6639b50d902SRodney W. Grimes 		*(htab_p - 16) = m1;
6649b50d902SRodney W. Grimes 		*(htab_p - 15) = m1;
6659b50d902SRodney W. Grimes 		*(htab_p - 14) = m1;
6669b50d902SRodney W. Grimes 		*(htab_p - 13) = m1;
6679b50d902SRodney W. Grimes 		*(htab_p - 12) = m1;
6689b50d902SRodney W. Grimes 		*(htab_p - 11) = m1;
6699b50d902SRodney W. Grimes 		*(htab_p - 10) = m1;
6709b50d902SRodney W. Grimes 		*(htab_p - 9) = m1;
6719b50d902SRodney W. Grimes 		*(htab_p - 8) = m1;
6729b50d902SRodney W. Grimes 		*(htab_p - 7) = m1;
6739b50d902SRodney W. Grimes 		*(htab_p - 6) = m1;
6749b50d902SRodney W. Grimes 		*(htab_p - 5) = m1;
6759b50d902SRodney W. Grimes 		*(htab_p - 4) = m1;
6769b50d902SRodney W. Grimes 		*(htab_p - 3) = m1;
6779b50d902SRodney W. Grimes 		*(htab_p - 2) = m1;
6789b50d902SRodney W. Grimes 		*(htab_p - 1) = m1;
6799b50d902SRodney W. Grimes 		htab_p -= 16;
6809b50d902SRodney W. Grimes 	} while ((i -= 16) >= 0);
6819b50d902SRodney W. Grimes 	for (i += 16; i > 0; i--)
6829b50d902SRodney W. Grimes 		*--htab_p = m1;
6839b50d902SRodney W. Grimes }
6849b50d902SRodney W. Grimes 
6859b50d902SRodney W. Grimes FILE *
68647e9e6a1SDavid Malone zopen(const char *fname, const char *mode, int bits)
6879b50d902SRodney W. Grimes {
6889b50d902SRodney W. Grimes 	struct s_zstate *zs;
6899b50d902SRodney W. Grimes 
690fa146c53SArchie Cobbs 	if ((mode[0] != 'r' && mode[0] != 'w') || mode[1] != '\0' ||
6919b50d902SRodney W. Grimes 	    bits < 0 || bits > BITS) {
6929b50d902SRodney W. Grimes 		errno = EINVAL;
6939b50d902SRodney W. Grimes 		return (NULL);
6949b50d902SRodney W. Grimes 	}
6959b50d902SRodney W. Grimes 
6969b50d902SRodney W. Grimes 	if ((zs = calloc(1, sizeof(struct s_zstate))) == NULL)
6979b50d902SRodney W. Grimes 		return (NULL);
6989b50d902SRodney W. Grimes 
6999b50d902SRodney W. Grimes 	maxbits = bits ? bits : BITS;	/* User settable max # bits/code. */
7005cf1ec50SAndreas Schulz 	maxmaxcode = 1L << maxbits;	/* Should NEVER generate this code. */
7019b50d902SRodney W. Grimes 	hsize = HSIZE;			/* For dynamic table sizing. */
7029b50d902SRodney W. Grimes 	free_ent = 0;			/* First unused entry. */
7039b50d902SRodney W. Grimes 	block_compress = BLOCK_MASK;
7049b50d902SRodney W. Grimes 	clear_flg = 0;
7059b50d902SRodney W. Grimes 	ratio = 0;
7069b50d902SRodney W. Grimes 	checkpoint = CHECK_GAP;
7079b50d902SRodney W. Grimes 	in_count = 1;			/* Length of input. */
7089b50d902SRodney W. Grimes 	out_count = 0;			/* # of codes output (for debugging). */
7099b50d902SRodney W. Grimes 	state = S_START;
7109b50d902SRodney W. Grimes 	roffset = 0;
7119b50d902SRodney W. Grimes 	size = 0;
7129b50d902SRodney W. Grimes 
7139b50d902SRodney W. Grimes 	/*
7149b50d902SRodney W. Grimes 	 * Layering compress on top of stdio in order to provide buffering,
7159b50d902SRodney W. Grimes 	 * and ensure that reads and write work with the data specified.
7169b50d902SRodney W. Grimes 	 */
7179b50d902SRodney W. Grimes 	if ((fp = fopen(fname, mode)) == NULL) {
7189b50d902SRodney W. Grimes 		free(zs);
7199b50d902SRodney W. Grimes 		return (NULL);
7209b50d902SRodney W. Grimes 	}
7219b50d902SRodney W. Grimes 	switch (*mode) {
7229b50d902SRodney W. Grimes 	case 'r':
7239b50d902SRodney W. Grimes 		zmode = 'r';
7249b50d902SRodney W. Grimes 		return (funopen(zs, zread, NULL, NULL, zclose));
7259b50d902SRodney W. Grimes 	case 'w':
7269b50d902SRodney W. Grimes 		zmode = 'w';
7279b50d902SRodney W. Grimes 		return (funopen(zs, NULL, zwrite, NULL, zclose));
7289b50d902SRodney W. Grimes 	}
7299b50d902SRodney W. Grimes 	/* NOTREACHED */
730fa146c53SArchie Cobbs 	return (NULL);
7319b50d902SRodney W. Grimes }
732