xref: /openbsd-src/sbin/savecore/zopen.c (revision 85b33b0351f6b0361c8d7c719330412e7e45be64)
1*85b33b03Skrw /*	$OpenBSD: zopen.c,v 1.4 2017/01/22 01:55:08 krw Exp $	*/
296dc1bb7Stedu /*	$NetBSD: zopen.c,v 1.5 1995/03/26 09:44:53 glass Exp $	*/
396dc1bb7Stedu 
496dc1bb7Stedu /*-
596dc1bb7Stedu  * Copyright (c) 1985, 1986, 1992, 1993
696dc1bb7Stedu  *	The Regents of the University of California.  All rights reserved.
796dc1bb7Stedu  *
896dc1bb7Stedu  * This code is derived from software contributed to Berkeley by
996dc1bb7Stedu  * Diomidis Spinellis and James A. Woods, derived from original
1096dc1bb7Stedu  * work by Spencer Thomas and Joseph Orost.
1196dc1bb7Stedu  *
1296dc1bb7Stedu  * Redistribution and use in source and binary forms, with or without
1396dc1bb7Stedu  * modification, are permitted provided that the following conditions
1496dc1bb7Stedu  * are met:
1596dc1bb7Stedu  * 1. Redistributions of source code must retain the above copyright
1696dc1bb7Stedu  *    notice, this list of conditions and the following disclaimer.
1796dc1bb7Stedu  * 2. Redistributions in binary form must reproduce the above copyright
1896dc1bb7Stedu  *    notice, this list of conditions and the following disclaimer in the
1996dc1bb7Stedu  *    documentation and/or other materials provided with the distribution.
2096dc1bb7Stedu  * 3. Neither the name of the University nor the names of its contributors
2196dc1bb7Stedu  *    may be used to endorse or promote products derived from this software
2296dc1bb7Stedu  *    without specific prior written permission.
2396dc1bb7Stedu  *
2496dc1bb7Stedu  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2596dc1bb7Stedu  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2696dc1bb7Stedu  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2796dc1bb7Stedu  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2896dc1bb7Stedu  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2996dc1bb7Stedu  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3096dc1bb7Stedu  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3196dc1bb7Stedu  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3296dc1bb7Stedu  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3396dc1bb7Stedu  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3496dc1bb7Stedu  * SUCH DAMAGE.
3596dc1bb7Stedu  *
3696dc1bb7Stedu  *	From: @(#)zopen.c	8.1 (Berkeley) 6/27/93
3796dc1bb7Stedu  */
3896dc1bb7Stedu 
3996dc1bb7Stedu /*-
4096dc1bb7Stedu  * fcompress.c - File compression ala IEEE Computer, June 1984.
4196dc1bb7Stedu  *
4296dc1bb7Stedu  * Compress authors:
4396dc1bb7Stedu  *		Spencer W. Thomas	(decvax!utah-cs!thomas)
4496dc1bb7Stedu  *		Jim McKie		(decvax!mcvax!jim)
4596dc1bb7Stedu  *		Steve Davies		(decvax!vax135!petsd!peora!srd)
4696dc1bb7Stedu  *		Ken Turkowski		(decvax!decwrl!turtlevax!ken)
4796dc1bb7Stedu  *		James A. Woods		(decvax!ihnp4!ames!jaw)
4896dc1bb7Stedu  *		Joe Orost		(decvax!vax135!petsd!joe)
4996dc1bb7Stedu  *
5096dc1bb7Stedu  * Cleaned up and converted to library returning I/O streams by
5196dc1bb7Stedu  * Diomidis Spinellis <dds@doc.ic.ac.uk>.
5296dc1bb7Stedu  *
5396dc1bb7Stedu  * zopen(filename, mode, bits)
5496dc1bb7Stedu  *	Returns a FILE * that can be used for read or write.  The modes
5596dc1bb7Stedu  *	supported are only "r" and "w".  Seeking is not allowed.  On
5696dc1bb7Stedu  *	reading the file is decompressed, on writing it is compressed.
5796dc1bb7Stedu  *	The output is compatible with compress(1) with 16 bit tables.
5896dc1bb7Stedu  *	Any file produced by compress(1) can be read.
5996dc1bb7Stedu  */
6096dc1bb7Stedu 
6196dc1bb7Stedu #include <sys/stat.h>
6296dc1bb7Stedu 
6396dc1bb7Stedu #include <ctype.h>
6496dc1bb7Stedu #include <errno.h>
6596dc1bb7Stedu #include <signal.h>
6696dc1bb7Stedu #include <stdio.h>
6796dc1bb7Stedu #include <stdlib.h>
6896dc1bb7Stedu #include <string.h>
6996dc1bb7Stedu #include <unistd.h>
7096dc1bb7Stedu #include <fcntl.h>
7196dc1bb7Stedu #include "compress.h"
7296dc1bb7Stedu 
7396dc1bb7Stedu #define MINIMUM(a, b)	(((a) < (b)) ? (a) : (b))
7496dc1bb7Stedu 
7596dc1bb7Stedu #define	BITS		16		/* Default bits. */
7696dc1bb7Stedu #define	HSIZE		69001		/* 95% occupancy */
7796dc1bb7Stedu #define	ZBUFSIZ		8192		/* I/O buffer size */
7896dc1bb7Stedu 
7996dc1bb7Stedu /* A code_int must be able to hold 2**BITS values of type int, and also -1. */
8096dc1bb7Stedu typedef long code_int;
8196dc1bb7Stedu typedef long count_int;
8296dc1bb7Stedu 
8396dc1bb7Stedu static const u_char z_magic[] =
8496dc1bb7Stedu 	{'\037', '\235'};		/* 1F 9D */
8596dc1bb7Stedu 
8696dc1bb7Stedu #define	BIT_MASK	0x1f		/* Defines for third byte of header. */
8796dc1bb7Stedu #define	BLOCK_MASK	0x80
8896dc1bb7Stedu 
8996dc1bb7Stedu /*
9096dc1bb7Stedu  * Masks 0x40 and 0x20 are free.  I think 0x20 should mean that there is
9196dc1bb7Stedu  * a fourth header byte (for expansion).
9296dc1bb7Stedu  */
9396dc1bb7Stedu #define	INIT_BITS 9			/* Initial number of bits/code. */
9496dc1bb7Stedu 
9596dc1bb7Stedu #define	MAXCODE(n_bits)	((1 << (n_bits)) - 1)
9696dc1bb7Stedu 
9796dc1bb7Stedu struct s_zstate {
9896dc1bb7Stedu 	int zs_fd;			/* File stream for I/O */
9996dc1bb7Stedu 	char zs_mode;			/* r or w */
10096dc1bb7Stedu 	enum {
10196dc1bb7Stedu 		S_START, S_MAGIC, S_MIDDLE, S_EOF
10296dc1bb7Stedu 	} zs_state;			/* State of computation */
10396dc1bb7Stedu 	int zs_n_bits;			/* Number of bits/code. */
10496dc1bb7Stedu 	int zs_maxbits;			/* User settable max # bits/code. */
10596dc1bb7Stedu 	code_int zs_maxcode;		/* Maximum code, given n_bits. */
10696dc1bb7Stedu 	code_int zs_maxmaxcode;		/* Should NEVER generate this code. */
10796dc1bb7Stedu 	count_int zs_htab[HSIZE];
10896dc1bb7Stedu 	u_short zs_codetab[HSIZE];
10996dc1bb7Stedu 	code_int zs_hsize;		/* For dynamic table sizing. */
11096dc1bb7Stedu 	code_int zs_free_ent;		/* First unused entry. */
11196dc1bb7Stedu 	/*
11296dc1bb7Stedu 	 * Block compression parameters -- after all codes are used up,
11396dc1bb7Stedu 	 * and compression rate changes, start over.
11496dc1bb7Stedu 	 */
11596dc1bb7Stedu 	int zs_block_compress;
11696dc1bb7Stedu 	int zs_clear_flg;
11796dc1bb7Stedu 	long zs_ratio;
11896dc1bb7Stedu 	count_int zs_checkpoint;
11996dc1bb7Stedu 	long zs_in_count;		/* Length of input. */
12096dc1bb7Stedu 	long zs_bytes_out;		/* Length of output. */
12196dc1bb7Stedu 	long zs_out_count;		/* # of codes output (for debugging).*/
12296dc1bb7Stedu 	u_char zs_buf[ZBUFSIZ];		/* I/O buffer */
12396dc1bb7Stedu 	u_char *zs_bp;			/* Current I/O window in the zs_buf */
12496dc1bb7Stedu 	int zs_offset;			/* Number of bits in the zs_buf */
12596dc1bb7Stedu 	union {
12696dc1bb7Stedu 		struct {
12796dc1bb7Stedu 			long zs_fcode;
12896dc1bb7Stedu 			code_int zs_ent;
12996dc1bb7Stedu 			code_int zs_hsize_reg;
13096dc1bb7Stedu 			int zs_hshift;
13196dc1bb7Stedu 		} w;			/* Write parameters */
13296dc1bb7Stedu 		struct {
13396dc1bb7Stedu 			u_char *zs_stackp, *zs_ebp;
13496dc1bb7Stedu 			int zs_finchar;
13596dc1bb7Stedu 			code_int zs_code, zs_oldcode, zs_incode;
13696dc1bb7Stedu 			int zs_size;
13796dc1bb7Stedu 		} r;			/* Read parameters */
13896dc1bb7Stedu 	} u;
13996dc1bb7Stedu };
14096dc1bb7Stedu 
14196dc1bb7Stedu /* Definitions to retain old variable names */
14296dc1bb7Stedu #define zs_fcode	u.w.zs_fcode
14396dc1bb7Stedu #define zs_ent		u.w.zs_ent
14496dc1bb7Stedu #define zs_hsize_reg	u.w.zs_hsize_reg
14596dc1bb7Stedu #define zs_hshift	u.w.zs_hshift
14696dc1bb7Stedu #define zs_stackp	u.r.zs_stackp
14796dc1bb7Stedu #define zs_finchar	u.r.zs_finchar
14896dc1bb7Stedu #define zs_code		u.r.zs_code
14996dc1bb7Stedu #define zs_oldcode	u.r.zs_oldcode
15096dc1bb7Stedu #define zs_incode	u.r.zs_incode
15196dc1bb7Stedu #define zs_size		u.r.zs_size
15296dc1bb7Stedu #define zs_ebp		u.r.zs_ebp
15396dc1bb7Stedu 
15496dc1bb7Stedu /*
15596dc1bb7Stedu  * To save much memory, we overlay the table used by compress() with those
15696dc1bb7Stedu  * used by decompress().  The tab_prefix table is the same size and type as
15796dc1bb7Stedu  * the codetab.  The tab_suffix table needs 2**BITS characters.  We get this
15896dc1bb7Stedu  * from the beginning of htab.  The output stack uses the rest of htab, and
15996dc1bb7Stedu  * contains characters.  There is plenty of room for any possible stack
16096dc1bb7Stedu  * (stack used to be 8000 characters).
16196dc1bb7Stedu  */
16296dc1bb7Stedu 
16396dc1bb7Stedu #define	htabof(i)	zs->zs_htab[i]
16496dc1bb7Stedu #define	codetabof(i)	zs->zs_codetab[i]
16596dc1bb7Stedu 
16696dc1bb7Stedu #define	tab_prefixof(i)	codetabof(i)
16796dc1bb7Stedu #define	tab_suffixof(i)	((u_char *)(zs->zs_htab))[i]
16896dc1bb7Stedu #define	de_stack	((u_char *)&tab_suffixof(1 << BITS))
16996dc1bb7Stedu 
17096dc1bb7Stedu #define	CHECK_GAP 10000		/* Ratio check interval. */
17196dc1bb7Stedu 
17296dc1bb7Stedu /*
17396dc1bb7Stedu  * the next two codes should not be changed lightly, as they must not
17496dc1bb7Stedu  * lie within the contiguous general code space.
17596dc1bb7Stedu  */
17696dc1bb7Stedu #define	FIRST	257		/* First free entry. */
17796dc1bb7Stedu #define	CLEAR	256		/* Table clear output code. */
17896dc1bb7Stedu 
17996dc1bb7Stedu static int	cl_block(struct s_zstate *);
18096dc1bb7Stedu static void	cl_hash(struct s_zstate *, count_int);
18196dc1bb7Stedu static int	output(struct s_zstate *, code_int);
18296dc1bb7Stedu 
18396dc1bb7Stedu /*-
18496dc1bb7Stedu  * Algorithm from "A Technique for High Performance Data Compression",
18596dc1bb7Stedu  * Terry A. Welch, IEEE Computer Vol 17, No 6 (June 1984), pp 8-19.
18696dc1bb7Stedu  *
18796dc1bb7Stedu  * Algorithm:
18896dc1bb7Stedu  *	Modified Lempel-Ziv method (LZW).  Basically finds common
18996dc1bb7Stedu  * substrings and replaces them with a variable size code.  This is
19096dc1bb7Stedu  * deterministic, and can be done on the fly.  Thus, the decompression
19196dc1bb7Stedu  * procedure needs no input table, but tracks the way the table was built.
19296dc1bb7Stedu  */
19396dc1bb7Stedu 
19496dc1bb7Stedu /*-
19596dc1bb7Stedu  * compress write
19696dc1bb7Stedu  *
19796dc1bb7Stedu  * Algorithm:  use open addressing double hashing (no chaining) on the
19896dc1bb7Stedu  * prefix code / next character combination.  We do a variant of Knuth's
19996dc1bb7Stedu  * algorithm D (vol. 3, sec. 6.4) along with G. Knott's relatively-prime
20096dc1bb7Stedu  * secondary probe.  Here, the modular division first probe is gives way
20196dc1bb7Stedu  * to a faster exclusive-or manipulation.  Also do block compression with
20296dc1bb7Stedu  * an adaptive reset, whereby the code table is cleared when the compression
20396dc1bb7Stedu  * ratio decreases, but after the table fills.  The variable-length output
20496dc1bb7Stedu  * codes are re-sized at this point, and a special CLEAR code is generated
20596dc1bb7Stedu  * for the decompressor.  Late addition:  construct the table according to
20696dc1bb7Stedu  * file size for noticeable speed improvement on small files.  Please direct
20796dc1bb7Stedu  * questions about this implementation to ames!jaw.
20896dc1bb7Stedu  */
20996dc1bb7Stedu int
zwrite(void * cookie,const char * wbp,int num)21096dc1bb7Stedu zwrite(void *cookie, const char *wbp, int num)
21196dc1bb7Stedu {
21296dc1bb7Stedu 	code_int i;
21396dc1bb7Stedu 	int c, disp;
21496dc1bb7Stedu 	struct s_zstate *zs;
21596dc1bb7Stedu 	const u_char *bp;
21696dc1bb7Stedu 	u_char tmp;
21796dc1bb7Stedu 	int count;
21896dc1bb7Stedu 
21996dc1bb7Stedu 	zs = cookie;
22096dc1bb7Stedu 	count = num;
22196dc1bb7Stedu 	bp = (u_char *)wbp;
22296dc1bb7Stedu 	switch (zs->zs_state) {
22396dc1bb7Stedu 	case S_MAGIC:
22496dc1bb7Stedu 		return -1;
22596dc1bb7Stedu 	case S_EOF:
22696dc1bb7Stedu 		return 0;
22796dc1bb7Stedu 	case S_START:
22896dc1bb7Stedu 		zs->zs_state = S_MIDDLE;
22996dc1bb7Stedu 
23096dc1bb7Stedu 		zs->zs_maxmaxcode = 1L << zs->zs_maxbits;
23196dc1bb7Stedu 		if (write(zs->zs_fd, z_magic, sizeof(z_magic)) !=
23296dc1bb7Stedu 		    sizeof(z_magic))
23396dc1bb7Stedu 			return (-1);
23496dc1bb7Stedu 		tmp = (u_char)(zs->zs_maxbits | zs->zs_block_compress);
23596dc1bb7Stedu 		if (write(zs->zs_fd, &tmp, sizeof(tmp)) != sizeof(tmp))
23696dc1bb7Stedu 			return (-1);
23796dc1bb7Stedu 
23896dc1bb7Stedu 		zs->zs_bp = zs->zs_buf;
23996dc1bb7Stedu 		zs->zs_offset = 0;
24096dc1bb7Stedu 		zs->zs_bytes_out = 3;	/* Includes 3-byte header mojo. */
24196dc1bb7Stedu 		zs->zs_out_count = 0;
24296dc1bb7Stedu 		zs->zs_clear_flg = 0;
24396dc1bb7Stedu 		zs->zs_ratio = 0;
24496dc1bb7Stedu 		zs->zs_in_count = 1;
24596dc1bb7Stedu 		zs->zs_checkpoint = CHECK_GAP;
24696dc1bb7Stedu 		zs->zs_maxcode = MAXCODE(zs->zs_n_bits = INIT_BITS);
24796dc1bb7Stedu 		zs->zs_free_ent = ((zs->zs_block_compress) ? FIRST : 256);
24896dc1bb7Stedu 
24996dc1bb7Stedu 		zs->zs_ent = *bp++;
25096dc1bb7Stedu 		--count;
25196dc1bb7Stedu 
25296dc1bb7Stedu 		zs->zs_hshift = 0;
25396dc1bb7Stedu 		for (zs->zs_fcode = (long)zs->zs_hsize; zs->zs_fcode < 65536L;
25496dc1bb7Stedu 		    zs->zs_fcode *= 2L)
25596dc1bb7Stedu 			zs->zs_hshift++;
25696dc1bb7Stedu 		/* Set hash code range bound. */
25796dc1bb7Stedu 		zs->zs_hshift = 8 - zs->zs_hshift;
25896dc1bb7Stedu 
25996dc1bb7Stedu 		zs->zs_hsize_reg = zs->zs_hsize;
26096dc1bb7Stedu 		/* Clear hash table. */
26196dc1bb7Stedu 		cl_hash(zs, (count_int)zs->zs_hsize_reg);
26296dc1bb7Stedu 
26396dc1bb7Stedu 	case S_MIDDLE:
26496dc1bb7Stedu 		for (i = 0; count-- > 0;) {
26596dc1bb7Stedu 			c = *bp++;
26696dc1bb7Stedu 			zs->zs_in_count++;
26796dc1bb7Stedu 			zs->zs_fcode = (long)(((long)c << zs->zs_maxbits) +
26896dc1bb7Stedu 			    zs->zs_ent);
26996dc1bb7Stedu 			/* Xor hashing. */
27096dc1bb7Stedu 			i = ((c << zs->zs_hshift) ^ zs->zs_ent);
27196dc1bb7Stedu 
27296dc1bb7Stedu 			if (htabof(i) == zs->zs_fcode) {
27396dc1bb7Stedu 				zs->zs_ent = codetabof(i);
27496dc1bb7Stedu 				continue;
27596dc1bb7Stedu 			} else if ((long)htabof(i) < 0)	/* Empty slot. */
27696dc1bb7Stedu 				goto nomatch;
27796dc1bb7Stedu 			/* Secondary hash (after G. Knott). */
27896dc1bb7Stedu 			disp = zs->zs_hsize_reg - i;
27996dc1bb7Stedu 			if (i == 0)
28096dc1bb7Stedu 				disp = 1;
28196dc1bb7Stedu probe:			if ((i -= disp) < 0)
28296dc1bb7Stedu 				i += zs->zs_hsize_reg;
28396dc1bb7Stedu 
28496dc1bb7Stedu 			if (htabof(i) == zs->zs_fcode) {
28596dc1bb7Stedu 				zs->zs_ent = codetabof(i);
28696dc1bb7Stedu 				continue;
28796dc1bb7Stedu 			}
28896dc1bb7Stedu 			if ((long)htabof(i) >= 0)
28996dc1bb7Stedu 				goto probe;
29096dc1bb7Stedu nomatch:		if (output(zs, (code_int) zs->zs_ent) == -1)
29196dc1bb7Stedu 				return (-1);
29296dc1bb7Stedu 			zs->zs_out_count++;
29396dc1bb7Stedu 			zs->zs_ent = c;
29496dc1bb7Stedu 			if (zs->zs_free_ent < zs->zs_maxmaxcode) {
29596dc1bb7Stedu 				/* code -> hashtable */
29696dc1bb7Stedu 				codetabof(i) = zs->zs_free_ent++;
29796dc1bb7Stedu 				htabof(i) = zs->zs_fcode;
29896dc1bb7Stedu 			} else if ((count_int)zs->zs_in_count >=
29996dc1bb7Stedu 			    zs->zs_checkpoint && zs->zs_block_compress) {
30096dc1bb7Stedu 				if (cl_block(zs) == -1)
30196dc1bb7Stedu 					return (-1);
30296dc1bb7Stedu 			}
30396dc1bb7Stedu 		}
30496dc1bb7Stedu 	}
30596dc1bb7Stedu 	return (num);
30696dc1bb7Stedu }
30796dc1bb7Stedu 
30896dc1bb7Stedu int
z_close(void * cookie,struct z_info * info,const char * name,struct stat * sb)30996dc1bb7Stedu z_close(void *cookie, struct z_info *info, const char *name, struct stat *sb)
31096dc1bb7Stedu {
31196dc1bb7Stedu 	struct s_zstate *zs;
31296dc1bb7Stedu 	int rval;
31396dc1bb7Stedu 
31496dc1bb7Stedu 	zs = cookie;
31596dc1bb7Stedu 	if (zs->zs_mode == 'w') {		/* Put out the final code. */
31696dc1bb7Stedu 		if (output(zs, (code_int) zs->zs_ent) == -1) {
31796dc1bb7Stedu 			(void)close(zs->zs_fd);
31896dc1bb7Stedu 			free(zs);
31996dc1bb7Stedu 			return (-1);
32096dc1bb7Stedu 		}
32196dc1bb7Stedu 		zs->zs_out_count++;
32296dc1bb7Stedu 		if (output(zs, (code_int) - 1) == -1) {
32396dc1bb7Stedu 			(void)close(zs->zs_fd);
32496dc1bb7Stedu 			free(zs);
32596dc1bb7Stedu 			return (-1);
32696dc1bb7Stedu 		}
32796dc1bb7Stedu 	}
32896dc1bb7Stedu 
32996dc1bb7Stedu 	if (info != NULL) {
33096dc1bb7Stedu 		info->mtime = 0;
33196dc1bb7Stedu 		info->crc = (u_int32_t)-1;
33296dc1bb7Stedu 		info->hlen = 0;
33396dc1bb7Stedu 		info->total_in = (off_t)zs->zs_in_count;
33496dc1bb7Stedu 		info->total_out = (off_t)zs->zs_bytes_out;
33596dc1bb7Stedu 	}
33696dc1bb7Stedu 
33796dc1bb7Stedu 	rval = close(zs->zs_fd);
33896dc1bb7Stedu 	free(zs);
33996dc1bb7Stedu 	return (rval);
34096dc1bb7Stedu }
34196dc1bb7Stedu 
34296dc1bb7Stedu static int
zclose(void * cookie)34396dc1bb7Stedu zclose(void *cookie)
34496dc1bb7Stedu {
34596dc1bb7Stedu 	return z_close(cookie, NULL, NULL, NULL);
34696dc1bb7Stedu }
34796dc1bb7Stedu 
34896dc1bb7Stedu /*-
34996dc1bb7Stedu  * Output the given code.
35096dc1bb7Stedu  * Inputs:
35196dc1bb7Stedu  *	code:	A n_bits-bit integer.  If == -1, then EOF.  This assumes
35296dc1bb7Stedu  *		that n_bits =< (long)wordsize - 1.
35396dc1bb7Stedu  * Outputs:
35496dc1bb7Stedu  *	Outputs code to the file.
35596dc1bb7Stedu  * Assumptions:
35696dc1bb7Stedu  *	Chars are 8 bits long.
35796dc1bb7Stedu  * Algorithm:
35896dc1bb7Stedu  *	Maintain a BITS character long buffer (so that 8 codes will
35996dc1bb7Stedu  * fit in it exactly).  Use the VAX insv instruction to insert each
36096dc1bb7Stedu  * code in turn.  When the buffer fills up empty it and start over.
36196dc1bb7Stedu  */
36296dc1bb7Stedu 
36396dc1bb7Stedu static const u_char lmask[9] =
36496dc1bb7Stedu 	{0xff, 0xfe, 0xfc, 0xf8, 0xf0, 0xe0, 0xc0, 0x80, 0x00};
36596dc1bb7Stedu static const u_char rmask[9] =
36696dc1bb7Stedu 	{0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff};
36796dc1bb7Stedu 
36896dc1bb7Stedu static int
output(struct s_zstate * zs,code_int ocode)36996dc1bb7Stedu output(struct s_zstate *zs, code_int ocode)
37096dc1bb7Stedu {
37196dc1bb7Stedu 	int bits;
37296dc1bb7Stedu 
37396dc1bb7Stedu 	if (ocode >= 0) {
37496dc1bb7Stedu 		int r_off;
37596dc1bb7Stedu 		u_char *bp;
37696dc1bb7Stedu 
37796dc1bb7Stedu 		/* Get to the first byte. */
37896dc1bb7Stedu 		bp = zs->zs_bp + (zs->zs_offset >> 3);
37996dc1bb7Stedu 		r_off = zs->zs_offset & 7;
38096dc1bb7Stedu 		bits = zs->zs_n_bits;
38196dc1bb7Stedu 
38296dc1bb7Stedu 		/*
38396dc1bb7Stedu 		 * Since ocode is always >= 8 bits, only need to mask the first
38496dc1bb7Stedu 		 * hunk on the left.
38596dc1bb7Stedu 		 */
38696dc1bb7Stedu 		*bp = (*bp & rmask[r_off]) | ((ocode << r_off) & lmask[r_off]);
38796dc1bb7Stedu 		bp++;
38896dc1bb7Stedu 		bits -= (8 - r_off);
38996dc1bb7Stedu 		ocode >>= 8 - r_off;
39096dc1bb7Stedu 		/* Get any 8 bit parts in the middle (<=1 for up to 16 bits) */
39196dc1bb7Stedu 		if (bits >= 8) {
39296dc1bb7Stedu 			*bp++ = ocode;
39396dc1bb7Stedu 			ocode >>= 8;
39496dc1bb7Stedu 			bits -= 8;
39596dc1bb7Stedu 		}
39696dc1bb7Stedu 		/* Last bits. */
39796dc1bb7Stedu 		if (bits)
39896dc1bb7Stedu 			*bp = ocode;
39996dc1bb7Stedu 		zs->zs_offset += zs->zs_n_bits;
40096dc1bb7Stedu 		if (zs->zs_offset == (zs->zs_n_bits << 3)) {
40196dc1bb7Stedu 			zs->zs_bp += zs->zs_n_bits;
40296dc1bb7Stedu 			zs->zs_offset = 0;
40396dc1bb7Stedu 		}
40496dc1bb7Stedu 		/*
40596dc1bb7Stedu 		 * If the next entry is going to be too big for the ocode size,
40696dc1bb7Stedu 		 * then increase it, if possible.
40796dc1bb7Stedu 		 */
40896dc1bb7Stedu 		if (zs->zs_free_ent > zs->zs_maxcode ||
40996dc1bb7Stedu 		    (zs->zs_clear_flg > 0)) {
41096dc1bb7Stedu 			/*
41196dc1bb7Stedu 			 * Write the whole buffer, because the input side won't
41296dc1bb7Stedu 			 * discover the size increase until after it has read it
41396dc1bb7Stedu 			 */
41496dc1bb7Stedu 			if (zs->zs_offset > 0) {
41596dc1bb7Stedu 				zs->zs_bp += zs->zs_n_bits;
41696dc1bb7Stedu 				zs->zs_offset = 0;
41796dc1bb7Stedu 			}
41896dc1bb7Stedu 
41996dc1bb7Stedu 			if (zs->zs_clear_flg) {
42096dc1bb7Stedu 				zs->zs_maxcode =
42196dc1bb7Stedu 					MAXCODE(zs->zs_n_bits = INIT_BITS);
42296dc1bb7Stedu 				zs->zs_clear_flg = 0;
42396dc1bb7Stedu 			} else {
42496dc1bb7Stedu 				zs->zs_n_bits++;
42596dc1bb7Stedu 				if (zs->zs_n_bits == zs->zs_maxbits)
42696dc1bb7Stedu 					zs->zs_maxcode = zs->zs_maxmaxcode;
42796dc1bb7Stedu 				else
42896dc1bb7Stedu 					zs->zs_maxcode =
42996dc1bb7Stedu 					    MAXCODE(zs->zs_n_bits);
43096dc1bb7Stedu 			}
43196dc1bb7Stedu 		}
43296dc1bb7Stedu 
43396dc1bb7Stedu 		if (zs->zs_bp + zs->zs_n_bits > &zs->zs_buf[ZBUFSIZ]) {
43496dc1bb7Stedu 			bits = zs->zs_bp - zs->zs_buf;
43596dc1bb7Stedu 			if (write(zs->zs_fd, zs->zs_buf, bits) != bits)
43696dc1bb7Stedu 				return (-1);
43796dc1bb7Stedu 			zs->zs_bytes_out += bits;
43896dc1bb7Stedu 			if (zs->zs_offset > 0)
43996dc1bb7Stedu 				fprintf (stderr, "zs_offset != 0\n");
44096dc1bb7Stedu 			zs->zs_bp = zs->zs_buf;
44196dc1bb7Stedu 		}
44296dc1bb7Stedu 	} else {
44396dc1bb7Stedu 		/* At EOF, write the rest of the buffer. */
44496dc1bb7Stedu 		if (zs->zs_offset > 0)
44596dc1bb7Stedu 			zs->zs_bp += (zs->zs_offset + 7) / 8;
44696dc1bb7Stedu 		if (zs->zs_bp > zs->zs_buf) {
44796dc1bb7Stedu 			bits = zs->zs_bp - zs->zs_buf;
44896dc1bb7Stedu 			if (write(zs->zs_fd, zs->zs_buf, bits) != bits)
44996dc1bb7Stedu 				return (-1);
45096dc1bb7Stedu 			zs->zs_bytes_out += bits;
45196dc1bb7Stedu 		}
45296dc1bb7Stedu 		zs->zs_offset = 0;
45396dc1bb7Stedu 		zs->zs_bp = zs->zs_buf;
45496dc1bb7Stedu 	}
45596dc1bb7Stedu 	return (0);
45696dc1bb7Stedu }
45796dc1bb7Stedu 
45896dc1bb7Stedu /* Table clear for block compress. */
45996dc1bb7Stedu static int
cl_block(struct s_zstate * zs)46096dc1bb7Stedu cl_block(struct s_zstate *zs)
46196dc1bb7Stedu {
46296dc1bb7Stedu 	long rat;
46396dc1bb7Stedu 
46496dc1bb7Stedu 	zs->zs_checkpoint = zs->zs_in_count + CHECK_GAP;
46596dc1bb7Stedu 
46696dc1bb7Stedu 	if (zs->zs_in_count > 0x007fffff) {	/* Shift will overflow. */
46796dc1bb7Stedu 		rat = zs->zs_bytes_out >> 8;
46896dc1bb7Stedu 		if (rat == 0)		/* Don't divide by zero. */
46996dc1bb7Stedu 			rat = 0x7fffffff;
47096dc1bb7Stedu 		else
47196dc1bb7Stedu 			rat = zs->zs_in_count / rat;
47296dc1bb7Stedu 	} else {
47396dc1bb7Stedu 		/* 8 fractional bits. */
47496dc1bb7Stedu 		rat = (zs->zs_in_count << 8) / zs->zs_bytes_out;
47596dc1bb7Stedu 	}
47696dc1bb7Stedu 	if (rat > zs->zs_ratio)
47796dc1bb7Stedu 		zs->zs_ratio = rat;
47896dc1bb7Stedu 	else {
47996dc1bb7Stedu 		zs->zs_ratio = 0;
48096dc1bb7Stedu 		cl_hash(zs, (count_int) zs->zs_hsize);
48196dc1bb7Stedu 		zs->zs_free_ent = FIRST;
48296dc1bb7Stedu 		zs->zs_clear_flg = 1;
48396dc1bb7Stedu 		if (output(zs, (code_int) CLEAR) == -1)
48496dc1bb7Stedu 			return (-1);
48596dc1bb7Stedu 	}
48696dc1bb7Stedu 	return (0);
48796dc1bb7Stedu }
48896dc1bb7Stedu 
48996dc1bb7Stedu /* Reset code table. */
49096dc1bb7Stedu static void
cl_hash(struct s_zstate * zs,count_int cl_hsize)49196dc1bb7Stedu cl_hash(struct s_zstate *zs, count_int cl_hsize)
49296dc1bb7Stedu {
49396dc1bb7Stedu 	count_int *htab_p;
49496dc1bb7Stedu 	long i, m1;
49596dc1bb7Stedu 
49696dc1bb7Stedu 	m1 = -1;
49796dc1bb7Stedu 	htab_p = zs->zs_htab + cl_hsize;
49896dc1bb7Stedu 	i = cl_hsize - 16;
49996dc1bb7Stedu 	do {			/* Might use Sys V memset(3) here. */
50096dc1bb7Stedu 		*(htab_p - 16) = m1;
50196dc1bb7Stedu 		*(htab_p - 15) = m1;
50296dc1bb7Stedu 		*(htab_p - 14) = m1;
50396dc1bb7Stedu 		*(htab_p - 13) = m1;
50496dc1bb7Stedu 		*(htab_p - 12) = m1;
50596dc1bb7Stedu 		*(htab_p - 11) = m1;
50696dc1bb7Stedu 		*(htab_p - 10) = m1;
50796dc1bb7Stedu 		*(htab_p - 9) = m1;
50896dc1bb7Stedu 		*(htab_p - 8) = m1;
50996dc1bb7Stedu 		*(htab_p - 7) = m1;
51096dc1bb7Stedu 		*(htab_p - 6) = m1;
51196dc1bb7Stedu 		*(htab_p - 5) = m1;
51296dc1bb7Stedu 		*(htab_p - 4) = m1;
51396dc1bb7Stedu 		*(htab_p - 3) = m1;
51496dc1bb7Stedu 		*(htab_p - 2) = m1;
51596dc1bb7Stedu 		*(htab_p - 1) = m1;
51696dc1bb7Stedu 		htab_p -= 16;
51796dc1bb7Stedu 	} while ((i -= 16) >= 0);
51896dc1bb7Stedu 	for (i += 16; i > 0; i--)
51996dc1bb7Stedu 		*--htab_p = m1;
52096dc1bb7Stedu }
52196dc1bb7Stedu 
52296dc1bb7Stedu FILE *
zopen(const char * name,const char * mode,int bits)52396dc1bb7Stedu zopen(const char *name, const char *mode, int bits)
52496dc1bb7Stedu {
52596dc1bb7Stedu 	FILE *fp;
52696dc1bb7Stedu 	int fd;
52796dc1bb7Stedu 	void *cookie;
52896dc1bb7Stedu 	if ((fd = open(name, (*mode=='r'? O_RDONLY:O_WRONLY|O_CREAT),
52996dc1bb7Stedu 	    S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) == -1)
53096dc1bb7Stedu 		return NULL;
53196dc1bb7Stedu 	if ((cookie = z_open(fd, mode, NULL, bits, 0, 0)) == NULL) {
53296dc1bb7Stedu 		close(fd);
53396dc1bb7Stedu 		return NULL;
53496dc1bb7Stedu 	}
535141245ffStedu 	if ((fp = funopen(cookie, NULL,
53696dc1bb7Stedu 	    (*mode == 'w'?zwrite:NULL), NULL, zclose)) == NULL) {
53796dc1bb7Stedu 		close(fd);
53896dc1bb7Stedu 		free(cookie);
53996dc1bb7Stedu 		return NULL;
54096dc1bb7Stedu 	}
54196dc1bb7Stedu 	return fp;
54296dc1bb7Stedu }
54396dc1bb7Stedu 
54496dc1bb7Stedu void *
z_open(int fd,const char * mode,char * name,int bits,u_int32_t mtime,int gotmagic)54596dc1bb7Stedu z_open(int fd, const char *mode, char *name, int bits,
54696dc1bb7Stedu     u_int32_t mtime, int gotmagic)
54796dc1bb7Stedu {
54896dc1bb7Stedu 	struct s_zstate *zs;
54996dc1bb7Stedu 
55096dc1bb7Stedu 	if ((mode[0] != 'r' && mode[0] != 'w') || mode[1] != '\0' ||
55196dc1bb7Stedu 	    bits < 0 || bits > BITS) {
55296dc1bb7Stedu 		errno = EINVAL;
55396dc1bb7Stedu 		return (NULL);
55496dc1bb7Stedu 	}
55596dc1bb7Stedu 
55696dc1bb7Stedu 	if ((zs = calloc(1, sizeof(struct s_zstate))) == NULL)
55796dc1bb7Stedu 		return (NULL);
55896dc1bb7Stedu 
55996dc1bb7Stedu 	/* User settable max # bits/code. */
56096dc1bb7Stedu 	zs->zs_maxbits = bits ? bits : BITS;
56196dc1bb7Stedu 	/* Should NEVER generate this code. */
56296dc1bb7Stedu 	zs->zs_maxmaxcode = 1 << zs->zs_maxbits;
56396dc1bb7Stedu 	zs->zs_hsize = HSIZE;		/* For dynamic table sizing. */
56496dc1bb7Stedu 	zs->zs_free_ent = 0;		/* First unused entry. */
56596dc1bb7Stedu 	zs->zs_block_compress = BLOCK_MASK;
56696dc1bb7Stedu 	zs->zs_clear_flg = 0;
56796dc1bb7Stedu 	zs->zs_ratio = 0;
56896dc1bb7Stedu 	zs->zs_checkpoint = CHECK_GAP;
56996dc1bb7Stedu 	zs->zs_in_count = 0;		/* Length of input. */
57096dc1bb7Stedu 	zs->zs_out_count = 0;		/* # of codes output (for debugging).*/
57196dc1bb7Stedu 	zs->zs_state = gotmagic ? S_MAGIC : S_START;
57296dc1bb7Stedu 	zs->zs_offset = 0;
57396dc1bb7Stedu 	zs->zs_size = 0;
57496dc1bb7Stedu 	zs->zs_mode = mode[0];
57596dc1bb7Stedu 	zs->zs_bp = zs->zs_ebp = zs->zs_buf;
57696dc1bb7Stedu 
57796dc1bb7Stedu 	zs->zs_fd = fd;
57896dc1bb7Stedu 	return zs;
57996dc1bb7Stedu }
580