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