xref: /csrg-svn/usr.bin/compress/zopen.c (revision 69049)
156972Sbostic /*-
263588Sbostic  * Copyright (c) 1985, 1986, 1992, 1993
363588Sbostic  *	The Regents of the University of California.  All rights reserved.
456972Sbostic  *
556972Sbostic  * This code is derived from software contributed to Berkeley by
656972Sbostic  * Diomidis Spinellis and James A. Woods, derived from original
756972Sbostic  * work by Spencer Thomas and Joseph Orost.
856972Sbostic  *
956972Sbostic  * %sccs.include.redist.c%
1056972Sbostic  */
1156972Sbostic 
1256972Sbostic #if defined(LIBC_SCCS) && !defined(lint)
13*69049Sbostic static char sccsid[] = "@(#)zopen.c	8.2 (Berkeley) 04/28/95";
1456972Sbostic #endif /* LIBC_SCCS and not lint */
1556972Sbostic 
1656972Sbostic /*-
1756972Sbostic  * fcompress.c - File compression ala IEEE Computer, June 1984.
1856972Sbostic  *
1956972Sbostic  * Compress authors:
2056972Sbostic  *		Spencer W. Thomas	(decvax!utah-cs!thomas)
2156972Sbostic  *		Jim McKie		(decvax!mcvax!jim)
2256972Sbostic  *		Steve Davies		(decvax!vax135!petsd!peora!srd)
2356972Sbostic  *		Ken Turkowski		(decvax!decwrl!turtlevax!ken)
2456972Sbostic  *		James A. Woods		(decvax!ihnp4!ames!jaw)
2556972Sbostic  *		Joe Orost		(decvax!vax135!petsd!joe)
2656972Sbostic  *
2756972Sbostic  * Cleaned up and converted to library returning I/O streams by
2856972Sbostic  * Diomidis Spinellis <dds@doc.ic.ac.uk>.
2956972Sbostic  *
3056972Sbostic  * zopen(filename, mode, bits)
3156972Sbostic  *	Returns a FILE * that can be used for read or write.  The modes
3256972Sbostic  *	supported are only "r" and "w".  Seeking is not allowed.  On
3356972Sbostic  *	reading the file is decompressed, on writing it is compressed.
3456972Sbostic  *	The output is compatible with compress(1) with 16 bit tables.
3556972Sbostic  *	Any file produced by compress(1) can be read.
3656972Sbostic  */
3756972Sbostic 
3856972Sbostic #include <sys/param.h>
3956972Sbostic #include <sys/stat.h>
4056972Sbostic 
4156972Sbostic #include <ctype.h>
4256972Sbostic #include <errno.h>
4356972Sbostic #include <signal.h>
4456972Sbostic #include <stdio.h>
4556972Sbostic #include <stdlib.h>
4656972Sbostic #include <string.h>
4756972Sbostic #include <unistd.h>
4856972Sbostic 
4956972Sbostic #define	BITS		16		/* Default bits. */
5056972Sbostic #define	HSIZE		69001		/* 95% occupancy */
5156972Sbostic 
5256972Sbostic /* A code_int must be able to hold 2**BITS values of type int, and also -1. */
5356972Sbostic typedef long code_int;
5456972Sbostic typedef long count_int;
5556972Sbostic 
5656972Sbostic typedef u_char char_type;
5756972Sbostic static char_type magic_header[] =
5856972Sbostic 	{'\037', '\235'};		/* 1F 9D */
5956972Sbostic 
6056972Sbostic #define	BIT_MASK	0x1f		/* Defines for third byte of header. */
6156972Sbostic #define	BLOCK_MASK	0x80
6256972Sbostic 
6356972Sbostic /*
6456972Sbostic  * Masks 0x40 and 0x20 are free.  I think 0x20 should mean that there is
6556972Sbostic  * a fourth header byte (for expansion).
6656972Sbostic  */
6756972Sbostic #define	INIT_BITS 9			/* Initial number of bits/code. */
6856972Sbostic 
6956972Sbostic #define	MAXCODE(n_bits)	((1 << (n_bits)) - 1)
7056972Sbostic 
7156972Sbostic struct s_zstate {
7256972Sbostic 	FILE *zs_fp;			/* File stream for I/O */
7356972Sbostic 	char zs_mode;			/* r or w */
7456972Sbostic 	enum {
7556972Sbostic 		S_START, S_MIDDLE, S_EOF
7656972Sbostic 	} zs_state;			/* State of computation */
7756972Sbostic 	int zs_n_bits;			/* Number of bits/code. */
7856972Sbostic 	int zs_maxbits;			/* User settable max # bits/code. */
7956972Sbostic 	code_int zs_maxcode;		/* Maximum code, given n_bits. */
8056972Sbostic 	code_int zs_maxmaxcode;		/* Should NEVER generate this code. */
8156972Sbostic 	count_int zs_htab [HSIZE];
8256972Sbostic 	u_short zs_codetab [HSIZE];
8356972Sbostic 	code_int zs_hsize;		/* For dynamic table sizing. */
8456972Sbostic 	code_int zs_free_ent;		/* First unused entry. */
8556972Sbostic 	/*
8656972Sbostic 	 * Block compression parameters -- after all codes are used up,
8756972Sbostic 	 * and compression rate changes, start over.
8856972Sbostic 	 */
8956972Sbostic 	int zs_block_compress;
9056972Sbostic 	int zs_clear_flg;
9156972Sbostic 	long zs_ratio;
9256972Sbostic 	count_int zs_checkpoint;
9356972Sbostic 	int zs_offset;
9456972Sbostic 	long zs_in_count;		/* Length of input. */
9556972Sbostic 	long zs_bytes_out;		/* Length of compressed output. */
9656972Sbostic 	long zs_out_count;		/* # of codes output (for debugging). */
9756972Sbostic 	char_type zs_buf[BITS];
9856972Sbostic 	union {
9956972Sbostic 		struct {
10056972Sbostic 			long zs_fcode;
10156972Sbostic 			code_int zs_ent;
10256972Sbostic 			code_int zs_hsize_reg;
10356972Sbostic 			int zs_hshift;
10456972Sbostic 		} w;			/* Write paramenters */
10556972Sbostic 		struct {
10656972Sbostic 			char_type *zs_stackp;
10756972Sbostic 			int zs_finchar;
10856972Sbostic 			code_int zs_code, zs_oldcode, zs_incode;
10956972Sbostic 			int zs_roffset, zs_size;
11056972Sbostic 			char_type zs_gbuf[BITS];
11156972Sbostic 		} r;			/* Read parameters */
11256972Sbostic 	} u;
11356972Sbostic };
11456972Sbostic 
11556972Sbostic /* Definitions to retain old variable names */
11656972Sbostic #define	fp		zs->zs_fp
11756972Sbostic #define	zmode		zs->zs_mode
11856972Sbostic #define	state		zs->zs_state
11956972Sbostic #define	n_bits		zs->zs_n_bits
12056972Sbostic #define	maxbits		zs->zs_maxbits
12156972Sbostic #define	maxcode		zs->zs_maxcode
12256972Sbostic #define	maxmaxcode	zs->zs_maxmaxcode
12356972Sbostic #define	htab		zs->zs_htab
12456972Sbostic #define	codetab		zs->zs_codetab
12556972Sbostic #define	hsize		zs->zs_hsize
12656972Sbostic #define	free_ent	zs->zs_free_ent
12756972Sbostic #define	block_compress	zs->zs_block_compress
12856972Sbostic #define	clear_flg	zs->zs_clear_flg
12956972Sbostic #define	ratio		zs->zs_ratio
13056972Sbostic #define	checkpoint	zs->zs_checkpoint
13156972Sbostic #define	offset		zs->zs_offset
13256972Sbostic #define	in_count	zs->zs_in_count
13356972Sbostic #define	bytes_out	zs->zs_bytes_out
13456972Sbostic #define	out_count	zs->zs_out_count
13556972Sbostic #define	buf		zs->zs_buf
13656972Sbostic #define	fcode		zs->u.w.zs_fcode
13756972Sbostic #define	hsize_reg	zs->u.w.zs_hsize_reg
13856972Sbostic #define	ent		zs->u.w.zs_ent
13956972Sbostic #define	hshift		zs->u.w.zs_hshift
14056972Sbostic #define	stackp		zs->u.r.zs_stackp
14156972Sbostic #define	finchar		zs->u.r.zs_finchar
14256972Sbostic #define	code		zs->u.r.zs_code
14356972Sbostic #define	oldcode		zs->u.r.zs_oldcode
14456972Sbostic #define	incode		zs->u.r.zs_incode
14556972Sbostic #define	roffset		zs->u.r.zs_roffset
14656972Sbostic #define	size		zs->u.r.zs_size
14756972Sbostic #define	gbuf		zs->u.r.zs_gbuf
14856972Sbostic 
14956972Sbostic /*
15056972Sbostic  * To save much memory, we overlay the table used by compress() with those
15156972Sbostic  * used by decompress().  The tab_prefix table is the same size and type as
15256972Sbostic  * the codetab.  The tab_suffix table needs 2**BITS characters.  We get this
15356972Sbostic  * from the beginning of htab.  The output stack uses the rest of htab, and
15456972Sbostic  * contains characters.  There is plenty of room for any possible stack
15556972Sbostic  * (stack used to be 8000 characters).
15656972Sbostic  */
15756972Sbostic 
15856972Sbostic #define	htabof(i)	htab[i]
15956972Sbostic #define	codetabof(i)	codetab[i]
16056972Sbostic 
16156972Sbostic #define	tab_prefixof(i)	codetabof(i)
16256972Sbostic #define	tab_suffixof(i)	((char_type *)(htab))[i]
16356972Sbostic #define	de_stack	((char_type *)&tab_suffixof(1 << BITS))
16456972Sbostic 
16556972Sbostic #define	CHECK_GAP 10000		/* Ratio check interval. */
16656972Sbostic 
16756972Sbostic /*
16856972Sbostic  * the next two codes should not be changed lightly, as they must not
16956972Sbostic  * lie within the contiguous general code space.
17056972Sbostic  */
17156972Sbostic #define	FIRST	257		/* First free entry. */
17256972Sbostic #define	CLEAR	256		/* Table clear output code. */
17356972Sbostic 
17456972Sbostic static int	cl_block __P((struct s_zstate *));
17556972Sbostic static void	cl_hash __P((struct s_zstate *, count_int));
17656972Sbostic static code_int	getcode __P((struct s_zstate *));
17756972Sbostic static int	output __P((struct s_zstate *, code_int));
17856972Sbostic static int	zclose __P((void *));
17956972Sbostic static int	zread __P((void *, char *, int));
18056972Sbostic static int	zwrite __P((void *, const char *, int));
18156972Sbostic 
18256972Sbostic /*-
18356972Sbostic  * Algorithm from "A Technique for High Performance Data Compression",
18456972Sbostic  * Terry A. Welch, IEEE Computer Vol 17, No 6 (June 1984), pp 8-19.
18556972Sbostic  *
18656972Sbostic  * Algorithm:
18756972Sbostic  * 	Modified Lempel-Ziv method (LZW).  Basically finds common
18856972Sbostic  * substrings and replaces them with a variable size code.  This is
18956972Sbostic  * deterministic, and can be done on the fly.  Thus, the decompression
19056972Sbostic  * procedure needs no input table, but tracks the way the table was built.
19156972Sbostic  */
19256972Sbostic 
19356972Sbostic /*-
19456972Sbostic  * compress write
19556972Sbostic  *
19656972Sbostic  * Algorithm:  use open addressing double hashing (no chaining) on the
19756972Sbostic  * prefix code / next character combination.  We do a variant of Knuth's
19856972Sbostic  * algorithm D (vol. 3, sec. 6.4) along with G. Knott's relatively-prime
19956972Sbostic  * secondary probe.  Here, the modular division first probe is gives way
20056972Sbostic  * to a faster exclusive-or manipulation.  Also do block compression with
20156972Sbostic  * an adaptive reset, whereby the code table is cleared when the compression
20256972Sbostic  * ratio decreases, but after the table fills.  The variable-length output
20356972Sbostic  * codes are re-sized at this point, and a special CLEAR code is generated
20456972Sbostic  * for the decompressor.  Late addition:  construct the table according to
20556972Sbostic  * file size for noticeable speed improvement on small files.  Please direct
20656972Sbostic  * questions about this implementation to ames!jaw.
20756972Sbostic  */
20856972Sbostic static int
zwrite(cookie,wbp,num)20956972Sbostic zwrite(cookie, wbp, num)
21056972Sbostic 	void *cookie;
21156972Sbostic 	const char *wbp;
21256972Sbostic 	int num;
21356972Sbostic {
21456972Sbostic 	register code_int i;
21556972Sbostic 	register int c, disp;
21656972Sbostic 	struct s_zstate *zs;
21756972Sbostic 	const u_char *bp;
21856972Sbostic 	u_char tmp;
21956972Sbostic 	int count;
22056972Sbostic 
22156972Sbostic 	if (num == 0)
22256972Sbostic 		return (0);
22356972Sbostic 
22456972Sbostic 	zs = cookie;
22556972Sbostic 	count = num;
22656972Sbostic 	bp = (u_char *)wbp;
22756972Sbostic 	if (state == S_MIDDLE)
22856972Sbostic 		goto middle;
22956972Sbostic 	state = S_MIDDLE;
23056972Sbostic 
231*69049Sbostic 	maxmaxcode = 1L << maxbits;
23256972Sbostic 	if (fwrite(magic_header,
23356972Sbostic 	    sizeof(char), sizeof(magic_header), fp) != sizeof(magic_header))
23456972Sbostic 		return (-1);
235*69049Sbostic 	tmp = (u_char)(maxbits | block_compress);
23656972Sbostic 	if (fwrite(&tmp, sizeof(char), sizeof(tmp), fp) != sizeof(tmp))
23756972Sbostic 		return (-1);
23856972Sbostic 
23956972Sbostic 	offset = 0;
24056972Sbostic 	bytes_out = 3;		/* Includes 3-byte header mojo. */
24156972Sbostic 	out_count = 0;
24256972Sbostic 	clear_flg = 0;
24356972Sbostic 	ratio = 0;
24456972Sbostic 	in_count = 1;
24556972Sbostic 	checkpoint = CHECK_GAP;
24656972Sbostic 	maxcode = MAXCODE(n_bits = INIT_BITS);
24756972Sbostic 	free_ent = ((block_compress) ? FIRST : 256);
24856972Sbostic 
24956972Sbostic 	ent = *bp++;
25056972Sbostic 	--count;
25156972Sbostic 
25256972Sbostic 	hshift = 0;
25356972Sbostic 	for (fcode = (long)hsize; fcode < 65536L; fcode *= 2L)
25456972Sbostic 		hshift++;
25556972Sbostic 	hshift = 8 - hshift;	/* Set hash code range bound. */
25656972Sbostic 
25756972Sbostic 	hsize_reg = hsize;
25856972Sbostic 	cl_hash(zs, (count_int)hsize_reg);	/* Clear hash table. */
25956972Sbostic 
26056972Sbostic middle:	for (i = 0; count--;) {
26156972Sbostic 		c = *bp++;
26256972Sbostic 		in_count++;
26356972Sbostic 		fcode = (long)(((long)c << maxbits) + ent);
26456972Sbostic 		i = ((c << hshift) ^ ent);	/* Xor hashing. */
26556972Sbostic 
26656972Sbostic 		if (htabof(i) == fcode) {
26756972Sbostic 			ent = codetabof(i);
26856972Sbostic 			continue;
26956972Sbostic 		} else if ((long)htabof(i) < 0)	/* Empty slot. */
27056972Sbostic 			goto nomatch;
27156972Sbostic 		disp = hsize_reg - i;	/* Secondary hash (after G. Knott). */
27256972Sbostic 		if (i == 0)
27356972Sbostic 			disp = 1;
27456972Sbostic probe:		if ((i -= disp) < 0)
27556972Sbostic 			i += hsize_reg;
27656972Sbostic 
27756972Sbostic 		if (htabof(i) == fcode) {
27856972Sbostic 			ent = codetabof(i);
27956972Sbostic 			continue;
28056972Sbostic 		}
28157187Sbostic 		if ((long)htabof(i) >= 0)
28256972Sbostic 			goto probe;
28356972Sbostic nomatch:	if (output(zs, (code_int) ent) == -1)
28456972Sbostic 			return (-1);
28556972Sbostic 		out_count++;
28656972Sbostic 		ent = c;
28756972Sbostic 		if (free_ent < maxmaxcode) {
28856972Sbostic 			codetabof(i) = free_ent++;	/* code -> hashtable */
28956972Sbostic 			htabof(i) = fcode;
29056972Sbostic 		} else if ((count_int)in_count >=
29156972Sbostic 		    checkpoint && block_compress) {
29256972Sbostic 			if (cl_block(zs) == -1)
29356972Sbostic 				return (-1);
29456972Sbostic 		}
29556972Sbostic 	}
29656972Sbostic 	return (num);
29756972Sbostic }
29856972Sbostic 
29956972Sbostic static int
zclose(cookie)30056972Sbostic zclose(cookie)
30156972Sbostic 	void *cookie;
30256972Sbostic {
30356972Sbostic 	struct s_zstate *zs;
30463587Sbostic 	int rval;
30556972Sbostic 
30656972Sbostic 	zs = cookie;
30756972Sbostic 	if (zmode == 'w') {		/* Put out the final code. */
30856972Sbostic 		if (output(zs, (code_int) ent) == -1) {
30963587Sbostic 			(void)fclose(fp);
31056972Sbostic 			free(zs);
31156972Sbostic 			return (-1);
31256972Sbostic 		}
31356972Sbostic 		out_count++;
31456972Sbostic 		if (output(zs, (code_int) - 1) == -1) {
31563587Sbostic 			(void)fclose(fp);
31656972Sbostic 			free(zs);
31756972Sbostic 			return (-1);
31856972Sbostic 		}
31956972Sbostic 	}
32063587Sbostic 	rval = fclose(fp) == EOF ? -1 : 0;
32156972Sbostic 	free(zs);
32263587Sbostic 	return (rval);
32356972Sbostic }
32456972Sbostic 
32556972Sbostic /*-
32656972Sbostic  * Output the given code.
32756972Sbostic  * Inputs:
32856972Sbostic  * 	code:	A n_bits-bit integer.  If == -1, then EOF.  This assumes
32956972Sbostic  *		that n_bits =< (long)wordsize - 1.
33056972Sbostic  * Outputs:
33156972Sbostic  * 	Outputs code to the file.
33256972Sbostic  * Assumptions:
33356972Sbostic  *	Chars are 8 bits long.
33456972Sbostic  * Algorithm:
33556972Sbostic  * 	Maintain a BITS character long buffer (so that 8 codes will
33656972Sbostic  * fit in it exactly).  Use the VAX insv instruction to insert each
33756972Sbostic  * code in turn.  When the buffer fills up empty it and start over.
33856972Sbostic  */
33956972Sbostic 
34056972Sbostic static char_type lmask[9] =
34156972Sbostic 	{0xff, 0xfe, 0xfc, 0xf8, 0xf0, 0xe0, 0xc0, 0x80, 0x00};
34256972Sbostic static char_type rmask[9] =
34356972Sbostic 	{0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff};
34456972Sbostic 
34556972Sbostic static int
output(zs,ocode)34656972Sbostic output(zs, ocode)
34756972Sbostic 	struct s_zstate *zs;
34856972Sbostic 	code_int ocode;
34956972Sbostic {
35056972Sbostic 	register int bits, r_off;
35156972Sbostic 	register char_type *bp;
35256972Sbostic 
35356972Sbostic 	r_off = offset;
35456972Sbostic 	bits = n_bits;
35556972Sbostic 	bp = buf;
35656972Sbostic 	if (ocode >= 0) {
35756972Sbostic 		/* Get to the first byte. */
35856972Sbostic 		bp += (r_off >> 3);
35956972Sbostic 		r_off &= 7;
36056972Sbostic 		/*
36156972Sbostic 		 * Since ocode is always >= 8 bits, only need to mask the first
36256972Sbostic 		 * hunk on the left.
36356972Sbostic 		 */
36456972Sbostic 		*bp = (*bp & rmask[r_off]) | (ocode << r_off) & lmask[r_off];
36556972Sbostic 		bp++;
36656972Sbostic 		bits -= (8 - r_off);
36756972Sbostic 		ocode >>= 8 - r_off;
36856972Sbostic 		/* Get any 8 bit parts in the middle (<=1 for up to 16 bits). */
36956972Sbostic 		if (bits >= 8) {
37056972Sbostic 			*bp++ = ocode;
37156972Sbostic 			ocode >>= 8;
37256972Sbostic 			bits -= 8;
37356972Sbostic 		}
37456972Sbostic 		/* Last bits. */
37556972Sbostic 		if (bits)
37656972Sbostic 			*bp = ocode;
37756972Sbostic 		offset += n_bits;
37856972Sbostic 		if (offset == (n_bits << 3)) {
37956972Sbostic 			bp = buf;
38056972Sbostic 			bits = n_bits;
38156972Sbostic 			bytes_out += bits;
38256972Sbostic 			if (fwrite(bp, sizeof(char), bits, fp) != bits)
38356972Sbostic 				return (-1);
38456972Sbostic 			bp += bits;
38556972Sbostic 			bits = 0;
38656972Sbostic 			offset = 0;
38756972Sbostic 		}
38856972Sbostic 		/*
38956972Sbostic 		 * If the next entry is going to be too big for the ocode size,
39056972Sbostic 		 * then increase it, if possible.
39156972Sbostic 		 */
39256972Sbostic 		if (free_ent > maxcode || (clear_flg > 0)) {
39356972Sbostic 		       /*
39456972Sbostic 			* Write the whole buffer, because the input side won't
39556972Sbostic 			* discover the size increase until after it has read it.
39656972Sbostic 			*/
39756972Sbostic 			if (offset > 0) {
39856972Sbostic 				if (fwrite(buf, 1, n_bits, fp) != n_bits)
39956972Sbostic 					return (-1);
40056972Sbostic 				bytes_out += n_bits;
40156972Sbostic 			}
40256972Sbostic 			offset = 0;
40356972Sbostic 
40456972Sbostic 			if (clear_flg) {
40556972Sbostic 				maxcode = MAXCODE(n_bits = INIT_BITS);
40656972Sbostic 				clear_flg = 0;
40756972Sbostic 			} else {
40856972Sbostic 				n_bits++;
40956972Sbostic 				if (n_bits == maxbits)
41056972Sbostic 					maxcode = maxmaxcode;
41156972Sbostic 				else
41256972Sbostic 					maxcode = MAXCODE(n_bits);
41356972Sbostic 			}
41456972Sbostic 		}
41556972Sbostic 	} else {
41656972Sbostic 		/* At EOF, write the rest of the buffer. */
41756972Sbostic 		if (offset > 0) {
41856972Sbostic 			offset = (offset + 7) / 8;
41956972Sbostic 			if (fwrite(buf, 1, offset, fp) != offset)
42056972Sbostic 				return (-1);
42156972Sbostic 			bytes_out += offset;
42256972Sbostic 		}
42356972Sbostic 		offset = 0;
42456972Sbostic 	}
42556972Sbostic 	return (0);
42656972Sbostic }
42756972Sbostic 
42856972Sbostic /*
42956972Sbostic  * Decompress read.  This routine adapts to the codes in the file building
43056972Sbostic  * the "string" table on-the-fly; requiring no table to be stored in the
43156972Sbostic  * compressed file.  The tables used herein are shared with those of the
43256972Sbostic  * compress() routine.  See the definitions above.
43356972Sbostic  */
43456972Sbostic static int
zread(cookie,rbp,num)43556972Sbostic zread(cookie, rbp, num)
43656972Sbostic 	void *cookie;
43756972Sbostic 	char *rbp;
43856972Sbostic 	int num;
43956972Sbostic {
44056972Sbostic 	register u_int count;
44156972Sbostic 	struct s_zstate *zs;
44256972Sbostic 	u_char *bp, header[3];
44356972Sbostic 
44456972Sbostic 	if (num == 0)
44556972Sbostic 		return (0);
44656972Sbostic 
44756972Sbostic 	zs = cookie;
44856972Sbostic 	count = num;
44956972Sbostic 	bp = (u_char *)rbp;
45056972Sbostic 	switch (state) {
45156972Sbostic 	case S_START:
45256972Sbostic 		state = S_MIDDLE;
45356972Sbostic 		break;
45456972Sbostic 	case S_MIDDLE:
45556972Sbostic 		goto middle;
45656972Sbostic 	case S_EOF:
45756972Sbostic 		goto eof;
45856972Sbostic 	}
45956972Sbostic 
46056972Sbostic 	/* Check the magic number */
46156972Sbostic 	if (fread(header,
46256972Sbostic 	    sizeof(char), sizeof(header), fp) != sizeof(header) ||
46356972Sbostic 	    memcmp(header, magic_header, sizeof(magic_header)) != 0) {
46456972Sbostic 		errno = EFTYPE;
46556972Sbostic 		return (-1);
46656972Sbostic 	}
46756972Sbostic 	maxbits = header[2];	/* Set -b from file. */
46856972Sbostic 	block_compress = maxbits & BLOCK_MASK;
46956972Sbostic 	maxbits &= BIT_MASK;
47056972Sbostic 	maxmaxcode = 1L << maxbits;
47156972Sbostic 	if (maxbits > BITS) {
47256972Sbostic 		errno = EFTYPE;
47356972Sbostic 		return (-1);
47456972Sbostic 	}
47556972Sbostic 	/* As above, initialize the first 256 entries in the table. */
47656972Sbostic 	maxcode = MAXCODE(n_bits = INIT_BITS);
47756972Sbostic 	for (code = 255; code >= 0; code--) {
47856972Sbostic 		tab_prefixof(code) = 0;
47956972Sbostic 		tab_suffixof(code) = (char_type) code;
48056972Sbostic 	}
48156972Sbostic 	free_ent = block_compress ? FIRST : 256;
48256972Sbostic 
48356972Sbostic 	finchar = oldcode = getcode(zs);
48456972Sbostic 	if (oldcode == -1)	/* EOF already? */
48556972Sbostic 		return (0);	/* Get out of here */
48656972Sbostic 
48756972Sbostic 	/* First code must be 8 bits = char. */
48856972Sbostic 	*bp++ = (u_char)finchar;
48956972Sbostic 	count--;
49056972Sbostic 	stackp = de_stack;
49156972Sbostic 
49256972Sbostic 	while ((code = getcode(zs)) > -1) {
49356972Sbostic 
49456972Sbostic 		if ((code == CLEAR) && block_compress) {
49556972Sbostic 			for (code = 255; code >= 0; code--)
49656972Sbostic 				tab_prefixof(code) = 0;
49756972Sbostic 			clear_flg = 1;
49856972Sbostic 			free_ent = FIRST - 1;
49956972Sbostic 			if ((code = getcode(zs)) == -1)	/* O, untimely death! */
50056972Sbostic 				break;
50156972Sbostic 		}
50256972Sbostic 		incode = code;
50356972Sbostic 
50456972Sbostic 		/* Special case for KwKwK string. */
50556972Sbostic 		if (code >= free_ent) {
50656972Sbostic 			*stackp++ = finchar;
50756972Sbostic 			code = oldcode;
50856972Sbostic 		}
50956972Sbostic 
51056972Sbostic 		/* Generate output characters in reverse order. */
51156972Sbostic 		while (code >= 256) {
51256972Sbostic 			*stackp++ = tab_suffixof(code);
51356972Sbostic 			code = tab_prefixof(code);
51456972Sbostic 		}
51556972Sbostic 		*stackp++ = finchar = tab_suffixof(code);
51656972Sbostic 
51756972Sbostic 		/* And put them out in forward order.  */
51856972Sbostic middle:		do {
51956972Sbostic 			if (count-- == 0)
52056972Sbostic 				return (num);
52156972Sbostic 			*bp++ = *--stackp;
52256972Sbostic 		} while (stackp > de_stack);
52356972Sbostic 
52456972Sbostic 		/* Generate the new entry. */
52556972Sbostic 		if ((code = free_ent) < maxmaxcode) {
52656972Sbostic 			tab_prefixof(code) = (u_short) oldcode;
52756972Sbostic 			tab_suffixof(code) = finchar;
52856972Sbostic 			free_ent = code + 1;
52956972Sbostic 		}
53056972Sbostic 
53156972Sbostic 		/* Remember previous code. */
53256972Sbostic 		oldcode = incode;
53356972Sbostic 	}
53456972Sbostic 	state = S_EOF;
53556972Sbostic eof:	return (num - count);
53656972Sbostic }
53756972Sbostic 
53856972Sbostic /*-
53956972Sbostic  * Read one code from the standard input.  If EOF, return -1.
54056972Sbostic  * Inputs:
54156972Sbostic  * 	stdin
54256972Sbostic  * Outputs:
54356972Sbostic  * 	code or -1 is returned.
54456972Sbostic  */
54556972Sbostic static code_int
getcode(zs)54656972Sbostic getcode(zs)
54756972Sbostic 	struct s_zstate *zs;
54856972Sbostic {
54956972Sbostic 	register code_int gcode;
55056972Sbostic 	register int r_off, bits;
55156972Sbostic 	register char_type *bp;
55256972Sbostic 
55356972Sbostic 	bp = gbuf;
55456972Sbostic 	if (clear_flg > 0 || roffset >= size || free_ent > maxcode) {
55556972Sbostic 		/*
55656972Sbostic 		 * If the next entry will be too big for the current gcode
55756972Sbostic 		 * size, then we must increase the size.  This implies reading
55856972Sbostic 		 * a new buffer full, too.
55956972Sbostic 		 */
56056972Sbostic 		if (free_ent > maxcode) {
56156972Sbostic 			n_bits++;
56256972Sbostic 			if (n_bits == maxbits)	/* Won't get any bigger now. */
56356972Sbostic 				maxcode = maxmaxcode;
56456972Sbostic 			else
56556972Sbostic 				maxcode = MAXCODE(n_bits);
56656972Sbostic 		}
56756972Sbostic 		if (clear_flg > 0) {
56856972Sbostic 			maxcode = MAXCODE(n_bits = INIT_BITS);
56956972Sbostic 			clear_flg = 0;
57056972Sbostic 		}
57156972Sbostic 		size = fread(gbuf, 1, n_bits, fp);
57256972Sbostic 		if (size <= 0)			/* End of file. */
57356972Sbostic 			return (-1);
57456972Sbostic 		roffset = 0;
57556972Sbostic 		/* Round size down to integral number of codes. */
57656972Sbostic 		size = (size << 3) - (n_bits - 1);
57756972Sbostic 	}
57856972Sbostic 	r_off = roffset;
57956972Sbostic 	bits = n_bits;
58056972Sbostic 
58156972Sbostic 	/* Get to the first byte. */
58256972Sbostic 	bp += (r_off >> 3);
58356972Sbostic 	r_off &= 7;
58456972Sbostic 
58556972Sbostic 	/* Get first part (low order bits). */
58656972Sbostic 	gcode = (*bp++ >> r_off);
58756972Sbostic 	bits -= (8 - r_off);
58856972Sbostic 	r_off = 8 - r_off;	/* Now, roffset into gcode word. */
58956972Sbostic 
59056972Sbostic 	/* Get any 8 bit parts in the middle (<=1 for up to 16 bits). */
59156972Sbostic 	if (bits >= 8) {
59256972Sbostic 		gcode |= *bp++ << r_off;
59356972Sbostic 		r_off += 8;
59456972Sbostic 		bits -= 8;
59556972Sbostic 	}
59656972Sbostic 
59756972Sbostic 	/* High order bits. */
59856972Sbostic 	gcode |= (*bp & rmask[bits]) << r_off;
59956972Sbostic 	roffset += n_bits;
60056972Sbostic 
60156972Sbostic 	return (gcode);
60256972Sbostic }
60356972Sbostic 
60456972Sbostic static int
cl_block(zs)60556972Sbostic cl_block(zs)			/* Table clear for block compress. */
60656972Sbostic 	struct s_zstate *zs;
60756972Sbostic {
60856972Sbostic 	register long rat;
60956972Sbostic 
61056972Sbostic 	checkpoint = in_count + CHECK_GAP;
61156972Sbostic 
61256972Sbostic 	if (in_count > 0x007fffff) {	/* Shift will overflow. */
61356972Sbostic 		rat = bytes_out >> 8;
61456972Sbostic 		if (rat == 0)		/* Don't divide by zero. */
61556972Sbostic 			rat = 0x7fffffff;
61656972Sbostic 		else
61756972Sbostic 			rat = in_count / rat;
61856972Sbostic 	} else
61956972Sbostic 		rat = (in_count << 8) / bytes_out;	/* 8 fractional bits. */
62056972Sbostic 	if (rat > ratio)
62156972Sbostic 		ratio = rat;
62256972Sbostic 	else {
62356972Sbostic 		ratio = 0;
62456972Sbostic 		cl_hash(zs, (count_int) hsize);
62556972Sbostic 		free_ent = FIRST;
62656972Sbostic 		clear_flg = 1;
62756972Sbostic 		if (output(zs, (code_int) CLEAR) == -1)
62856972Sbostic 			return (-1);
62956972Sbostic 	}
63056972Sbostic 	return (0);
63156972Sbostic }
63256972Sbostic 
63356972Sbostic static void
cl_hash(zs,cl_hsize)63456972Sbostic cl_hash(zs, cl_hsize)			/* Reset code table. */
63556972Sbostic 	struct s_zstate *zs;
63656972Sbostic 	register count_int cl_hsize;
63756972Sbostic {
63856972Sbostic 	register count_int *htab_p;
63956972Sbostic 	register long i, m1;
64056972Sbostic 
64156972Sbostic 	m1 = -1;
64256972Sbostic 	htab_p = htab + cl_hsize;
64356972Sbostic 	i = cl_hsize - 16;
64456972Sbostic 	do {			/* Might use Sys V memset(3) here. */
64556972Sbostic 		*(htab_p - 16) = m1;
64656972Sbostic 		*(htab_p - 15) = m1;
64756972Sbostic 		*(htab_p - 14) = m1;
64856972Sbostic 		*(htab_p - 13) = m1;
64956972Sbostic 		*(htab_p - 12) = m1;
65056972Sbostic 		*(htab_p - 11) = m1;
65156972Sbostic 		*(htab_p - 10) = m1;
65256972Sbostic 		*(htab_p - 9) = m1;
65356972Sbostic 		*(htab_p - 8) = m1;
65456972Sbostic 		*(htab_p - 7) = m1;
65556972Sbostic 		*(htab_p - 6) = m1;
65656972Sbostic 		*(htab_p - 5) = m1;
65756972Sbostic 		*(htab_p - 4) = m1;
65856972Sbostic 		*(htab_p - 3) = m1;
65956972Sbostic 		*(htab_p - 2) = m1;
66056972Sbostic 		*(htab_p - 1) = m1;
66156972Sbostic 		htab_p -= 16;
66256972Sbostic 	} while ((i -= 16) >= 0);
66356972Sbostic 	for (i += 16; i > 0; i--)
66456972Sbostic 		*--htab_p = m1;
66556972Sbostic }
66656972Sbostic 
66756972Sbostic FILE *
zopen(fname,mode,bits)66856972Sbostic zopen(fname, mode, bits)
66956972Sbostic 	const char *fname, *mode;
67056972Sbostic 	int bits;
67156972Sbostic {
67256972Sbostic 	struct s_zstate *zs;
67356972Sbostic 
67456972Sbostic 	if (mode[0] != 'r' && mode[0] != 'w' || mode[1] != '\0' ||
67556972Sbostic 	    bits < 0 || bits > BITS) {
67656972Sbostic 		errno = EINVAL;
67756972Sbostic 		return (NULL);
67856972Sbostic 	}
67956972Sbostic 
68056972Sbostic 	if ((zs = calloc(1, sizeof(struct s_zstate))) == NULL)
68156972Sbostic 		return (NULL);
68256972Sbostic 
68356972Sbostic 	maxbits = bits ? bits : BITS;	/* User settable max # bits/code. */
684*69049Sbostic 	maxmaxcode = 1 << maxbits;	/* Should NEVER generate this code. */
68556972Sbostic 	hsize = HSIZE;			/* For dynamic table sizing. */
68656972Sbostic 	free_ent = 0;			/* First unused entry. */
68756972Sbostic 	block_compress = BLOCK_MASK;
68856972Sbostic 	clear_flg = 0;
68956972Sbostic 	ratio = 0;
69056972Sbostic 	checkpoint = CHECK_GAP;
69156972Sbostic 	in_count = 1;			/* Length of input. */
69256972Sbostic 	out_count = 0;			/* # of codes output (for debugging). */
69356972Sbostic 	state = S_START;
69456972Sbostic 	roffset = 0;
69556972Sbostic 	size = 0;
69656972Sbostic 
69756972Sbostic 	/*
69856972Sbostic 	 * Layering compress on top of stdio in order to provide buffering,
69956972Sbostic 	 * and ensure that reads and write work with the data specified.
70056972Sbostic 	 */
70156972Sbostic 	if ((fp = fopen(fname, mode)) == NULL) {
70256972Sbostic 		free(zs);
70356972Sbostic 		return (NULL);
70456972Sbostic 	}
70556972Sbostic 	switch (*mode) {
70656972Sbostic 	case 'r':
70756972Sbostic 		zmode = 'r';
70856972Sbostic 		return (funopen(zs, zread, NULL, NULL, zclose));
70956972Sbostic 	case 'w':
71056972Sbostic 		zmode = 'w';
71156972Sbostic 		return (funopen(zs, NULL, zwrite, NULL, zclose));
71256972Sbostic 	}
71356972Sbostic 	/* NOTREACHED */
71456972Sbostic }
715