Home | History | Annotate | Line # | Download | only in compress
zopen.c revision 1.1
      1  1.1  cgd /*-
      2  1.1  cgd  * Copyright (c) 1985, 1986, 1992, 1993
      3  1.1  cgd  *	The Regents of the University of California.  All rights reserved.
      4  1.1  cgd  *
      5  1.1  cgd  * This code is derived from software contributed to Berkeley by
      6  1.1  cgd  * Diomidis Spinellis and James A. Woods, derived from original
      7  1.1  cgd  * work by Spencer Thomas and Joseph Orost.
      8  1.1  cgd  *
      9  1.1  cgd  * Redistribution and use in source and binary forms, with or without
     10  1.1  cgd  * modification, are permitted provided that the following conditions
     11  1.1  cgd  * are met:
     12  1.1  cgd  * 1. Redistributions of source code must retain the above copyright
     13  1.1  cgd  *    notice, this list of conditions and the following disclaimer.
     14  1.1  cgd  * 2. Redistributions in binary form must reproduce the above copyright
     15  1.1  cgd  *    notice, this list of conditions and the following disclaimer in the
     16  1.1  cgd  *    documentation and/or other materials provided with the distribution.
     17  1.1  cgd  * 3. All advertising materials mentioning features or use of this software
     18  1.1  cgd  *    must display the following acknowledgement:
     19  1.1  cgd  *	This product includes software developed by the University of
     20  1.1  cgd  *	California, Berkeley and its contributors.
     21  1.1  cgd  * 4. Neither the name of the University nor the names of its contributors
     22  1.1  cgd  *    may be used to endorse or promote products derived from this software
     23  1.1  cgd  *    without specific prior written permission.
     24  1.1  cgd  *
     25  1.1  cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     26  1.1  cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     27  1.1  cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     28  1.1  cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     29  1.1  cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     30  1.1  cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     31  1.1  cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     32  1.1  cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     33  1.1  cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     34  1.1  cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     35  1.1  cgd  * SUCH DAMAGE.
     36  1.1  cgd  */
     37  1.1  cgd 
     38  1.1  cgd #if defined(LIBC_SCCS) && !defined(lint)
     39  1.1  cgd static char sccsid[] = "@(#)zopen.c	8.1 (Berkeley) 6/27/93";
     40  1.1  cgd #endif /* LIBC_SCCS and not lint */
     41  1.1  cgd 
     42  1.1  cgd /*-
     43  1.1  cgd  * fcompress.c - File compression ala IEEE Computer, June 1984.
     44  1.1  cgd  *
     45  1.1  cgd  * Compress authors:
     46  1.1  cgd  *		Spencer W. Thomas	(decvax!utah-cs!thomas)
     47  1.1  cgd  *		Jim McKie		(decvax!mcvax!jim)
     48  1.1  cgd  *		Steve Davies		(decvax!vax135!petsd!peora!srd)
     49  1.1  cgd  *		Ken Turkowski		(decvax!decwrl!turtlevax!ken)
     50  1.1  cgd  *		James A. Woods		(decvax!ihnp4!ames!jaw)
     51  1.1  cgd  *		Joe Orost		(decvax!vax135!petsd!joe)
     52  1.1  cgd  *
     53  1.1  cgd  * Cleaned up and converted to library returning I/O streams by
     54  1.1  cgd  * Diomidis Spinellis <dds (at) doc.ic.ac.uk>.
     55  1.1  cgd  *
     56  1.1  cgd  * zopen(filename, mode, bits)
     57  1.1  cgd  *	Returns a FILE * that can be used for read or write.  The modes
     58  1.1  cgd  *	supported are only "r" and "w".  Seeking is not allowed.  On
     59  1.1  cgd  *	reading the file is decompressed, on writing it is compressed.
     60  1.1  cgd  *	The output is compatible with compress(1) with 16 bit tables.
     61  1.1  cgd  *	Any file produced by compress(1) can be read.
     62  1.1  cgd  */
     63  1.1  cgd 
     64  1.1  cgd #include <sys/param.h>
     65  1.1  cgd #include <sys/stat.h>
     66  1.1  cgd 
     67  1.1  cgd #include <ctype.h>
     68  1.1  cgd #include <errno.h>
     69  1.1  cgd #include <signal.h>
     70  1.1  cgd #include <stdio.h>
     71  1.1  cgd #include <stdlib.h>
     72  1.1  cgd #include <string.h>
     73  1.1  cgd #include <unistd.h>
     74  1.1  cgd 
     75  1.1  cgd #define	BITS		16		/* Default bits. */
     76  1.1  cgd #define	HSIZE		69001		/* 95% occupancy */
     77  1.1  cgd 
     78  1.1  cgd /* A code_int must be able to hold 2**BITS values of type int, and also -1. */
     79  1.1  cgd typedef long code_int;
     80  1.1  cgd typedef long count_int;
     81  1.1  cgd 
     82  1.1  cgd typedef u_char char_type;
     83  1.1  cgd static char_type magic_header[] =
     84  1.1  cgd 	{'\037', '\235'};		/* 1F 9D */
     85  1.1  cgd 
     86  1.1  cgd #define	BIT_MASK	0x1f		/* Defines for third byte of header. */
     87  1.1  cgd #define	BLOCK_MASK	0x80
     88  1.1  cgd 
     89  1.1  cgd /*
     90  1.1  cgd  * Masks 0x40 and 0x20 are free.  I think 0x20 should mean that there is
     91  1.1  cgd  * a fourth header byte (for expansion).
     92  1.1  cgd  */
     93  1.1  cgd #define	INIT_BITS 9			/* Initial number of bits/code. */
     94  1.1  cgd 
     95  1.1  cgd #define	MAXCODE(n_bits)	((1 << (n_bits)) - 1)
     96  1.1  cgd 
     97  1.1  cgd struct s_zstate {
     98  1.1  cgd 	FILE *zs_fp;			/* File stream for I/O */
     99  1.1  cgd 	char zs_mode;			/* r or w */
    100  1.1  cgd 	enum {
    101  1.1  cgd 		S_START, S_MIDDLE, S_EOF
    102  1.1  cgd 	} zs_state;			/* State of computation */
    103  1.1  cgd 	int zs_n_bits;			/* Number of bits/code. */
    104  1.1  cgd 	int zs_maxbits;			/* User settable max # bits/code. */
    105  1.1  cgd 	code_int zs_maxcode;		/* Maximum code, given n_bits. */
    106  1.1  cgd 	code_int zs_maxmaxcode;		/* Should NEVER generate this code. */
    107  1.1  cgd 	count_int zs_htab [HSIZE];
    108  1.1  cgd 	u_short zs_codetab [HSIZE];
    109  1.1  cgd 	code_int zs_hsize;		/* For dynamic table sizing. */
    110  1.1  cgd 	code_int zs_free_ent;		/* First unused entry. */
    111  1.1  cgd 	/*
    112  1.1  cgd 	 * Block compression parameters -- after all codes are used up,
    113  1.1  cgd 	 * and compression rate changes, start over.
    114  1.1  cgd 	 */
    115  1.1  cgd 	int zs_block_compress;
    116  1.1  cgd 	int zs_clear_flg;
    117  1.1  cgd 	long zs_ratio;
    118  1.1  cgd 	count_int zs_checkpoint;
    119  1.1  cgd 	int zs_offset;
    120  1.1  cgd 	long zs_in_count;		/* Length of input. */
    121  1.1  cgd 	long zs_bytes_out;		/* Length of compressed output. */
    122  1.1  cgd 	long zs_out_count;		/* # of codes output (for debugging). */
    123  1.1  cgd 	char_type zs_buf[BITS];
    124  1.1  cgd 	union {
    125  1.1  cgd 		struct {
    126  1.1  cgd 			long zs_fcode;
    127  1.1  cgd 			code_int zs_ent;
    128  1.1  cgd 			code_int zs_hsize_reg;
    129  1.1  cgd 			int zs_hshift;
    130  1.1  cgd 		} w;			/* Write paramenters */
    131  1.1  cgd 		struct {
    132  1.1  cgd 			char_type *zs_stackp;
    133  1.1  cgd 			int zs_finchar;
    134  1.1  cgd 			code_int zs_code, zs_oldcode, zs_incode;
    135  1.1  cgd 			int zs_roffset, zs_size;
    136  1.1  cgd 			char_type zs_gbuf[BITS];
    137  1.1  cgd 		} r;			/* Read parameters */
    138  1.1  cgd 	} u;
    139  1.1  cgd };
    140  1.1  cgd 
    141  1.1  cgd /* Definitions to retain old variable names */
    142  1.1  cgd #define	fp		zs->zs_fp
    143  1.1  cgd #define	zmode		zs->zs_mode
    144  1.1  cgd #define	state		zs->zs_state
    145  1.1  cgd #define	n_bits		zs->zs_n_bits
    146  1.1  cgd #define	maxbits		zs->zs_maxbits
    147  1.1  cgd #define	maxcode		zs->zs_maxcode
    148  1.1  cgd #define	maxmaxcode	zs->zs_maxmaxcode
    149  1.1  cgd #define	htab		zs->zs_htab
    150  1.1  cgd #define	codetab		zs->zs_codetab
    151  1.1  cgd #define	hsize		zs->zs_hsize
    152  1.1  cgd #define	free_ent	zs->zs_free_ent
    153  1.1  cgd #define	block_compress	zs->zs_block_compress
    154  1.1  cgd #define	clear_flg	zs->zs_clear_flg
    155  1.1  cgd #define	ratio		zs->zs_ratio
    156  1.1  cgd #define	checkpoint	zs->zs_checkpoint
    157  1.1  cgd #define	offset		zs->zs_offset
    158  1.1  cgd #define	in_count	zs->zs_in_count
    159  1.1  cgd #define	bytes_out	zs->zs_bytes_out
    160  1.1  cgd #define	out_count	zs->zs_out_count
    161  1.1  cgd #define	buf		zs->zs_buf
    162  1.1  cgd #define	fcode		zs->u.w.zs_fcode
    163  1.1  cgd #define	hsize_reg	zs->u.w.zs_hsize_reg
    164  1.1  cgd #define	ent		zs->u.w.zs_ent
    165  1.1  cgd #define	hshift		zs->u.w.zs_hshift
    166  1.1  cgd #define	stackp		zs->u.r.zs_stackp
    167  1.1  cgd #define	finchar		zs->u.r.zs_finchar
    168  1.1  cgd #define	code		zs->u.r.zs_code
    169  1.1  cgd #define	oldcode		zs->u.r.zs_oldcode
    170  1.1  cgd #define	incode		zs->u.r.zs_incode
    171  1.1  cgd #define	roffset		zs->u.r.zs_roffset
    172  1.1  cgd #define	size		zs->u.r.zs_size
    173  1.1  cgd #define	gbuf		zs->u.r.zs_gbuf
    174  1.1  cgd 
    175  1.1  cgd /*
    176  1.1  cgd  * To save much memory, we overlay the table used by compress() with those
    177  1.1  cgd  * used by decompress().  The tab_prefix table is the same size and type as
    178  1.1  cgd  * the codetab.  The tab_suffix table needs 2**BITS characters.  We get this
    179  1.1  cgd  * from the beginning of htab.  The output stack uses the rest of htab, and
    180  1.1  cgd  * contains characters.  There is plenty of room for any possible stack
    181  1.1  cgd  * (stack used to be 8000 characters).
    182  1.1  cgd  */
    183  1.1  cgd 
    184  1.1  cgd #define	htabof(i)	htab[i]
    185  1.1  cgd #define	codetabof(i)	codetab[i]
    186  1.1  cgd 
    187  1.1  cgd #define	tab_prefixof(i)	codetabof(i)
    188  1.1  cgd #define	tab_suffixof(i)	((char_type *)(htab))[i]
    189  1.1  cgd #define	de_stack	((char_type *)&tab_suffixof(1 << BITS))
    190  1.1  cgd 
    191  1.1  cgd #define	CHECK_GAP 10000		/* Ratio check interval. */
    192  1.1  cgd 
    193  1.1  cgd /*
    194  1.1  cgd  * the next two codes should not be changed lightly, as they must not
    195  1.1  cgd  * lie within the contiguous general code space.
    196  1.1  cgd  */
    197  1.1  cgd #define	FIRST	257		/* First free entry. */
    198  1.1  cgd #define	CLEAR	256		/* Table clear output code. */
    199  1.1  cgd 
    200  1.1  cgd static int	cl_block __P((struct s_zstate *));
    201  1.1  cgd static void	cl_hash __P((struct s_zstate *, count_int));
    202  1.1  cgd static code_int	getcode __P((struct s_zstate *));
    203  1.1  cgd static int	output __P((struct s_zstate *, code_int));
    204  1.1  cgd static int	zclose __P((void *));
    205  1.1  cgd static int	zread __P((void *, char *, int));
    206  1.1  cgd static int	zwrite __P((void *, const char *, int));
    207  1.1  cgd 
    208  1.1  cgd /*-
    209  1.1  cgd  * Algorithm from "A Technique for High Performance Data Compression",
    210  1.1  cgd  * Terry A. Welch, IEEE Computer Vol 17, No 6 (June 1984), pp 8-19.
    211  1.1  cgd  *
    212  1.1  cgd  * Algorithm:
    213  1.1  cgd  * 	Modified Lempel-Ziv method (LZW).  Basically finds common
    214  1.1  cgd  * substrings and replaces them with a variable size code.  This is
    215  1.1  cgd  * deterministic, and can be done on the fly.  Thus, the decompression
    216  1.1  cgd  * procedure needs no input table, but tracks the way the table was built.
    217  1.1  cgd  */
    218  1.1  cgd 
    219  1.1  cgd /*-
    220  1.1  cgd  * compress write
    221  1.1  cgd  *
    222  1.1  cgd  * Algorithm:  use open addressing double hashing (no chaining) on the
    223  1.1  cgd  * prefix code / next character combination.  We do a variant of Knuth's
    224  1.1  cgd  * algorithm D (vol. 3, sec. 6.4) along with G. Knott's relatively-prime
    225  1.1  cgd  * secondary probe.  Here, the modular division first probe is gives way
    226  1.1  cgd  * to a faster exclusive-or manipulation.  Also do block compression with
    227  1.1  cgd  * an adaptive reset, whereby the code table is cleared when the compression
    228  1.1  cgd  * ratio decreases, but after the table fills.  The variable-length output
    229  1.1  cgd  * codes are re-sized at this point, and a special CLEAR code is generated
    230  1.1  cgd  * for the decompressor.  Late addition:  construct the table according to
    231  1.1  cgd  * file size for noticeable speed improvement on small files.  Please direct
    232  1.1  cgd  * questions about this implementation to ames!jaw.
    233  1.1  cgd  */
    234  1.1  cgd static int
    235  1.1  cgd zwrite(cookie, wbp, num)
    236  1.1  cgd 	void *cookie;
    237  1.1  cgd 	const char *wbp;
    238  1.1  cgd 	int num;
    239  1.1  cgd {
    240  1.1  cgd 	register code_int i;
    241  1.1  cgd 	register int c, disp;
    242  1.1  cgd 	struct s_zstate *zs;
    243  1.1  cgd 	const u_char *bp;
    244  1.1  cgd 	u_char tmp;
    245  1.1  cgd 	int count;
    246  1.1  cgd 
    247  1.1  cgd 	if (num == 0)
    248  1.1  cgd 		return (0);
    249  1.1  cgd 
    250  1.1  cgd 	zs = cookie;
    251  1.1  cgd 	count = num;
    252  1.1  cgd 	bp = (u_char *)wbp;
    253  1.1  cgd 	if (state == S_MIDDLE)
    254  1.1  cgd 		goto middle;
    255  1.1  cgd 	state = S_MIDDLE;
    256  1.1  cgd 
    257  1.1  cgd 	maxmaxcode = 1L << BITS;
    258  1.1  cgd 	if (fwrite(magic_header,
    259  1.1  cgd 	    sizeof(char), sizeof(magic_header), fp) != sizeof(magic_header))
    260  1.1  cgd 		return (-1);
    261  1.1  cgd 	tmp = (u_char)(BITS | block_compress);
    262  1.1  cgd 	if (fwrite(&tmp, sizeof(char), sizeof(tmp), fp) != sizeof(tmp))
    263  1.1  cgd 		return (-1);
    264  1.1  cgd 
    265  1.1  cgd 	offset = 0;
    266  1.1  cgd 	bytes_out = 3;		/* Includes 3-byte header mojo. */
    267  1.1  cgd 	out_count = 0;
    268  1.1  cgd 	clear_flg = 0;
    269  1.1  cgd 	ratio = 0;
    270  1.1  cgd 	in_count = 1;
    271  1.1  cgd 	checkpoint = CHECK_GAP;
    272  1.1  cgd 	maxcode = MAXCODE(n_bits = INIT_BITS);
    273  1.1  cgd 	free_ent = ((block_compress) ? FIRST : 256);
    274  1.1  cgd 
    275  1.1  cgd 	ent = *bp++;
    276  1.1  cgd 	--count;
    277  1.1  cgd 
    278  1.1  cgd 	hshift = 0;
    279  1.1  cgd 	for (fcode = (long)hsize; fcode < 65536L; fcode *= 2L)
    280  1.1  cgd 		hshift++;
    281  1.1  cgd 	hshift = 8 - hshift;	/* Set hash code range bound. */
    282  1.1  cgd 
    283  1.1  cgd 	hsize_reg = hsize;
    284  1.1  cgd 	cl_hash(zs, (count_int)hsize_reg);	/* Clear hash table. */
    285  1.1  cgd 
    286  1.1  cgd middle:	for (i = 0; count--;) {
    287  1.1  cgd 		c = *bp++;
    288  1.1  cgd 		in_count++;
    289  1.1  cgd 		fcode = (long)(((long)c << maxbits) + ent);
    290  1.1  cgd 		i = ((c << hshift) ^ ent);	/* Xor hashing. */
    291  1.1  cgd 
    292  1.1  cgd 		if (htabof(i) == fcode) {
    293  1.1  cgd 			ent = codetabof(i);
    294  1.1  cgd 			continue;
    295  1.1  cgd 		} else if ((long)htabof(i) < 0)	/* Empty slot. */
    296  1.1  cgd 			goto nomatch;
    297  1.1  cgd 		disp = hsize_reg - i;	/* Secondary hash (after G. Knott). */
    298  1.1  cgd 		if (i == 0)
    299  1.1  cgd 			disp = 1;
    300  1.1  cgd probe:		if ((i -= disp) < 0)
    301  1.1  cgd 			i += hsize_reg;
    302  1.1  cgd 
    303  1.1  cgd 		if (htabof(i) == fcode) {
    304  1.1  cgd 			ent = codetabof(i);
    305  1.1  cgd 			continue;
    306  1.1  cgd 		}
    307  1.1  cgd 		if ((long)htabof(i) >= 0)
    308  1.1  cgd 			goto probe;
    309  1.1  cgd nomatch:	if (output(zs, (code_int) ent) == -1)
    310  1.1  cgd 			return (-1);
    311  1.1  cgd 		out_count++;
    312  1.1  cgd 		ent = c;
    313  1.1  cgd 		if (free_ent < maxmaxcode) {
    314  1.1  cgd 			codetabof(i) = free_ent++;	/* code -> hashtable */
    315  1.1  cgd 			htabof(i) = fcode;
    316  1.1  cgd 		} else if ((count_int)in_count >=
    317  1.1  cgd 		    checkpoint && block_compress) {
    318  1.1  cgd 			if (cl_block(zs) == -1)
    319  1.1  cgd 				return (-1);
    320  1.1  cgd 		}
    321  1.1  cgd 	}
    322  1.1  cgd 	return (num);
    323  1.1  cgd }
    324  1.1  cgd 
    325  1.1  cgd static int
    326  1.1  cgd zclose(cookie)
    327  1.1  cgd 	void *cookie;
    328  1.1  cgd {
    329  1.1  cgd 	struct s_zstate *zs;
    330  1.1  cgd 	int rval;
    331  1.1  cgd 
    332  1.1  cgd 	zs = cookie;
    333  1.1  cgd 	if (zmode == 'w') {		/* Put out the final code. */
    334  1.1  cgd 		if (output(zs, (code_int) ent) == -1) {
    335  1.1  cgd 			(void)fclose(fp);
    336  1.1  cgd 			free(zs);
    337  1.1  cgd 			return (-1);
    338  1.1  cgd 		}
    339  1.1  cgd 		out_count++;
    340  1.1  cgd 		if (output(zs, (code_int) - 1) == -1) {
    341  1.1  cgd 			(void)fclose(fp);
    342  1.1  cgd 			free(zs);
    343  1.1  cgd 			return (-1);
    344  1.1  cgd 		}
    345  1.1  cgd 	}
    346  1.1  cgd 	rval = fclose(fp) == EOF ? -1 : 0;
    347  1.1  cgd 	free(zs);
    348  1.1  cgd 	return (rval);
    349  1.1  cgd }
    350  1.1  cgd 
    351  1.1  cgd /*-
    352  1.1  cgd  * Output the given code.
    353  1.1  cgd  * Inputs:
    354  1.1  cgd  * 	code:	A n_bits-bit integer.  If == -1, then EOF.  This assumes
    355  1.1  cgd  *		that n_bits =< (long)wordsize - 1.
    356  1.1  cgd  * Outputs:
    357  1.1  cgd  * 	Outputs code to the file.
    358  1.1  cgd  * Assumptions:
    359  1.1  cgd  *	Chars are 8 bits long.
    360  1.1  cgd  * Algorithm:
    361  1.1  cgd  * 	Maintain a BITS character long buffer (so that 8 codes will
    362  1.1  cgd  * fit in it exactly).  Use the VAX insv instruction to insert each
    363  1.1  cgd  * code in turn.  When the buffer fills up empty it and start over.
    364  1.1  cgd  */
    365  1.1  cgd 
    366  1.1  cgd static char_type lmask[9] =
    367  1.1  cgd 	{0xff, 0xfe, 0xfc, 0xf8, 0xf0, 0xe0, 0xc0, 0x80, 0x00};
    368  1.1  cgd static char_type rmask[9] =
    369  1.1  cgd 	{0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff};
    370  1.1  cgd 
    371  1.1  cgd static int
    372  1.1  cgd output(zs, ocode)
    373  1.1  cgd 	struct s_zstate *zs;
    374  1.1  cgd 	code_int ocode;
    375  1.1  cgd {
    376  1.1  cgd 	register int bits, r_off;
    377  1.1  cgd 	register char_type *bp;
    378  1.1  cgd 
    379  1.1  cgd 	r_off = offset;
    380  1.1  cgd 	bits = n_bits;
    381  1.1  cgd 	bp = buf;
    382  1.1  cgd 	if (ocode >= 0) {
    383  1.1  cgd 		/* Get to the first byte. */
    384  1.1  cgd 		bp += (r_off >> 3);
    385  1.1  cgd 		r_off &= 7;
    386  1.1  cgd 		/*
    387  1.1  cgd 		 * Since ocode is always >= 8 bits, only need to mask the first
    388  1.1  cgd 		 * hunk on the left.
    389  1.1  cgd 		 */
    390  1.1  cgd 		*bp = (*bp & rmask[r_off]) | (ocode << r_off) & lmask[r_off];
    391  1.1  cgd 		bp++;
    392  1.1  cgd 		bits -= (8 - r_off);
    393  1.1  cgd 		ocode >>= 8 - r_off;
    394  1.1  cgd 		/* Get any 8 bit parts in the middle (<=1 for up to 16 bits). */
    395  1.1  cgd 		if (bits >= 8) {
    396  1.1  cgd 			*bp++ = ocode;
    397  1.1  cgd 			ocode >>= 8;
    398  1.1  cgd 			bits -= 8;
    399  1.1  cgd 		}
    400  1.1  cgd 		/* Last bits. */
    401  1.1  cgd 		if (bits)
    402  1.1  cgd 			*bp = ocode;
    403  1.1  cgd 		offset += n_bits;
    404  1.1  cgd 		if (offset == (n_bits << 3)) {
    405  1.1  cgd 			bp = buf;
    406  1.1  cgd 			bits = n_bits;
    407  1.1  cgd 			bytes_out += bits;
    408  1.1  cgd 			if (fwrite(bp, sizeof(char), bits, fp) != bits)
    409  1.1  cgd 				return (-1);
    410  1.1  cgd 			bp += bits;
    411  1.1  cgd 			bits = 0;
    412  1.1  cgd 			offset = 0;
    413  1.1  cgd 		}
    414  1.1  cgd 		/*
    415  1.1  cgd 		 * If the next entry is going to be too big for the ocode size,
    416  1.1  cgd 		 * then increase it, if possible.
    417  1.1  cgd 		 */
    418  1.1  cgd 		if (free_ent > maxcode || (clear_flg > 0)) {
    419  1.1  cgd 		       /*
    420  1.1  cgd 			* Write the whole buffer, because the input side won't
    421  1.1  cgd 			* discover the size increase until after it has read it.
    422  1.1  cgd 			*/
    423  1.1  cgd 			if (offset > 0) {
    424  1.1  cgd 				if (fwrite(buf, 1, n_bits, fp) != n_bits)
    425  1.1  cgd 					return (-1);
    426  1.1  cgd 				bytes_out += n_bits;
    427  1.1  cgd 			}
    428  1.1  cgd 			offset = 0;
    429  1.1  cgd 
    430  1.1  cgd 			if (clear_flg) {
    431  1.1  cgd 				maxcode = MAXCODE(n_bits = INIT_BITS);
    432  1.1  cgd 				clear_flg = 0;
    433  1.1  cgd 			} else {
    434  1.1  cgd 				n_bits++;
    435  1.1  cgd 				if (n_bits == maxbits)
    436  1.1  cgd 					maxcode = maxmaxcode;
    437  1.1  cgd 				else
    438  1.1  cgd 					maxcode = MAXCODE(n_bits);
    439  1.1  cgd 			}
    440  1.1  cgd 		}
    441  1.1  cgd 	} else {
    442  1.1  cgd 		/* At EOF, write the rest of the buffer. */
    443  1.1  cgd 		if (offset > 0) {
    444  1.1  cgd 			offset = (offset + 7) / 8;
    445  1.1  cgd 			if (fwrite(buf, 1, offset, fp) != offset)
    446  1.1  cgd 				return (-1);
    447  1.1  cgd 			bytes_out += offset;
    448  1.1  cgd 		}
    449  1.1  cgd 		offset = 0;
    450  1.1  cgd 	}
    451  1.1  cgd 	return (0);
    452  1.1  cgd }
    453  1.1  cgd 
    454  1.1  cgd /*
    455  1.1  cgd  * Decompress read.  This routine adapts to the codes in the file building
    456  1.1  cgd  * the "string" table on-the-fly; requiring no table to be stored in the
    457  1.1  cgd  * compressed file.  The tables used herein are shared with those of the
    458  1.1  cgd  * compress() routine.  See the definitions above.
    459  1.1  cgd  */
    460  1.1  cgd static int
    461  1.1  cgd zread(cookie, rbp, num)
    462  1.1  cgd 	void *cookie;
    463  1.1  cgd 	char *rbp;
    464  1.1  cgd 	int num;
    465  1.1  cgd {
    466  1.1  cgd 	register u_int count;
    467  1.1  cgd 	struct s_zstate *zs;
    468  1.1  cgd 	u_char *bp, header[3];
    469  1.1  cgd 
    470  1.1  cgd 	if (num == 0)
    471  1.1  cgd 		return (0);
    472  1.1  cgd 
    473  1.1  cgd 	zs = cookie;
    474  1.1  cgd 	count = num;
    475  1.1  cgd 	bp = (u_char *)rbp;
    476  1.1  cgd 	switch (state) {
    477  1.1  cgd 	case S_START:
    478  1.1  cgd 		state = S_MIDDLE;
    479  1.1  cgd 		break;
    480  1.1  cgd 	case S_MIDDLE:
    481  1.1  cgd 		goto middle;
    482  1.1  cgd 	case S_EOF:
    483  1.1  cgd 		goto eof;
    484  1.1  cgd 	}
    485  1.1  cgd 
    486  1.1  cgd 	/* Check the magic number */
    487  1.1  cgd 	if (fread(header,
    488  1.1  cgd 	    sizeof(char), sizeof(header), fp) != sizeof(header) ||
    489  1.1  cgd 	    memcmp(header, magic_header, sizeof(magic_header)) != 0) {
    490  1.1  cgd 		errno = EFTYPE;
    491  1.1  cgd 		return (-1);
    492  1.1  cgd 	}
    493  1.1  cgd 	maxbits = header[2];	/* Set -b from file. */
    494  1.1  cgd 	block_compress = maxbits & BLOCK_MASK;
    495  1.1  cgd 	maxbits &= BIT_MASK;
    496  1.1  cgd 	maxmaxcode = 1L << maxbits;
    497  1.1  cgd 	if (maxbits > BITS) {
    498  1.1  cgd 		errno = EFTYPE;
    499  1.1  cgd 		return (-1);
    500  1.1  cgd 	}
    501  1.1  cgd 	/* As above, initialize the first 256 entries in the table. */
    502  1.1  cgd 	maxcode = MAXCODE(n_bits = INIT_BITS);
    503  1.1  cgd 	for (code = 255; code >= 0; code--) {
    504  1.1  cgd 		tab_prefixof(code) = 0;
    505  1.1  cgd 		tab_suffixof(code) = (char_type) code;
    506  1.1  cgd 	}
    507  1.1  cgd 	free_ent = block_compress ? FIRST : 256;
    508  1.1  cgd 
    509  1.1  cgd 	finchar = oldcode = getcode(zs);
    510  1.1  cgd 	if (oldcode == -1)	/* EOF already? */
    511  1.1  cgd 		return (0);	/* Get out of here */
    512  1.1  cgd 
    513  1.1  cgd 	/* First code must be 8 bits = char. */
    514  1.1  cgd 	*bp++ = (u_char)finchar;
    515  1.1  cgd 	count--;
    516  1.1  cgd 	stackp = de_stack;
    517  1.1  cgd 
    518  1.1  cgd 	while ((code = getcode(zs)) > -1) {
    519  1.1  cgd 
    520  1.1  cgd 		if ((code == CLEAR) && block_compress) {
    521  1.1  cgd 			for (code = 255; code >= 0; code--)
    522  1.1  cgd 				tab_prefixof(code) = 0;
    523  1.1  cgd 			clear_flg = 1;
    524  1.1  cgd 			free_ent = FIRST - 1;
    525  1.1  cgd 			if ((code = getcode(zs)) == -1)	/* O, untimely death! */
    526  1.1  cgd 				break;
    527  1.1  cgd 		}
    528  1.1  cgd 		incode = code;
    529  1.1  cgd 
    530  1.1  cgd 		/* Special case for KwKwK string. */
    531  1.1  cgd 		if (code >= free_ent) {
    532  1.1  cgd 			*stackp++ = finchar;
    533  1.1  cgd 			code = oldcode;
    534  1.1  cgd 		}
    535  1.1  cgd 
    536  1.1  cgd 		/* Generate output characters in reverse order. */
    537  1.1  cgd 		while (code >= 256) {
    538  1.1  cgd 			*stackp++ = tab_suffixof(code);
    539  1.1  cgd 			code = tab_prefixof(code);
    540  1.1  cgd 		}
    541  1.1  cgd 		*stackp++ = finchar = tab_suffixof(code);
    542  1.1  cgd 
    543  1.1  cgd 		/* And put them out in forward order.  */
    544  1.1  cgd middle:		do {
    545  1.1  cgd 			if (count-- == 0)
    546  1.1  cgd 				return (num);
    547  1.1  cgd 			*bp++ = *--stackp;
    548  1.1  cgd 		} while (stackp > de_stack);
    549  1.1  cgd 
    550  1.1  cgd 		/* Generate the new entry. */
    551  1.1  cgd 		if ((code = free_ent) < maxmaxcode) {
    552  1.1  cgd 			tab_prefixof(code) = (u_short) oldcode;
    553  1.1  cgd 			tab_suffixof(code) = finchar;
    554  1.1  cgd 			free_ent = code + 1;
    555  1.1  cgd 		}
    556  1.1  cgd 
    557  1.1  cgd 		/* Remember previous code. */
    558  1.1  cgd 		oldcode = incode;
    559  1.1  cgd 	}
    560  1.1  cgd 	state = S_EOF;
    561  1.1  cgd eof:	return (num - count);
    562  1.1  cgd }
    563  1.1  cgd 
    564  1.1  cgd /*-
    565  1.1  cgd  * Read one code from the standard input.  If EOF, return -1.
    566  1.1  cgd  * Inputs:
    567  1.1  cgd  * 	stdin
    568  1.1  cgd  * Outputs:
    569  1.1  cgd  * 	code or -1 is returned.
    570  1.1  cgd  */
    571  1.1  cgd static code_int
    572  1.1  cgd getcode(zs)
    573  1.1  cgd 	struct s_zstate *zs;
    574  1.1  cgd {
    575  1.1  cgd 	register code_int gcode;
    576  1.1  cgd 	register int r_off, bits;
    577  1.1  cgd 	register char_type *bp;
    578  1.1  cgd 
    579  1.1  cgd 	bp = gbuf;
    580  1.1  cgd 	if (clear_flg > 0 || roffset >= size || free_ent > maxcode) {
    581  1.1  cgd 		/*
    582  1.1  cgd 		 * If the next entry will be too big for the current gcode
    583  1.1  cgd 		 * size, then we must increase the size.  This implies reading
    584  1.1  cgd 		 * a new buffer full, too.
    585  1.1  cgd 		 */
    586  1.1  cgd 		if (free_ent > maxcode) {
    587  1.1  cgd 			n_bits++;
    588  1.1  cgd 			if (n_bits == maxbits)	/* Won't get any bigger now. */
    589  1.1  cgd 				maxcode = maxmaxcode;
    590  1.1  cgd 			else
    591  1.1  cgd 				maxcode = MAXCODE(n_bits);
    592  1.1  cgd 		}
    593  1.1  cgd 		if (clear_flg > 0) {
    594  1.1  cgd 			maxcode = MAXCODE(n_bits = INIT_BITS);
    595  1.1  cgd 			clear_flg = 0;
    596  1.1  cgd 		}
    597  1.1  cgd 		size = fread(gbuf, 1, n_bits, fp);
    598  1.1  cgd 		if (size <= 0)			/* End of file. */
    599  1.1  cgd 			return (-1);
    600  1.1  cgd 		roffset = 0;
    601  1.1  cgd 		/* Round size down to integral number of codes. */
    602  1.1  cgd 		size = (size << 3) - (n_bits - 1);
    603  1.1  cgd 	}
    604  1.1  cgd 	r_off = roffset;
    605  1.1  cgd 	bits = n_bits;
    606  1.1  cgd 
    607  1.1  cgd 	/* Get to the first byte. */
    608  1.1  cgd 	bp += (r_off >> 3);
    609  1.1  cgd 	r_off &= 7;
    610  1.1  cgd 
    611  1.1  cgd 	/* Get first part (low order bits). */
    612  1.1  cgd 	gcode = (*bp++ >> r_off);
    613  1.1  cgd 	bits -= (8 - r_off);
    614  1.1  cgd 	r_off = 8 - r_off;	/* Now, roffset into gcode word. */
    615  1.1  cgd 
    616  1.1  cgd 	/* Get any 8 bit parts in the middle (<=1 for up to 16 bits). */
    617  1.1  cgd 	if (bits >= 8) {
    618  1.1  cgd 		gcode |= *bp++ << r_off;
    619  1.1  cgd 		r_off += 8;
    620  1.1  cgd 		bits -= 8;
    621  1.1  cgd 	}
    622  1.1  cgd 
    623  1.1  cgd 	/* High order bits. */
    624  1.1  cgd 	gcode |= (*bp & rmask[bits]) << r_off;
    625  1.1  cgd 	roffset += n_bits;
    626  1.1  cgd 
    627  1.1  cgd 	return (gcode);
    628  1.1  cgd }
    629  1.1  cgd 
    630  1.1  cgd static int
    631  1.1  cgd cl_block(zs)			/* Table clear for block compress. */
    632  1.1  cgd 	struct s_zstate *zs;
    633  1.1  cgd {
    634  1.1  cgd 	register long rat;
    635  1.1  cgd 
    636  1.1  cgd 	checkpoint = in_count + CHECK_GAP;
    637  1.1  cgd 
    638  1.1  cgd 	if (in_count > 0x007fffff) {	/* Shift will overflow. */
    639  1.1  cgd 		rat = bytes_out >> 8;
    640  1.1  cgd 		if (rat == 0)		/* Don't divide by zero. */
    641  1.1  cgd 			rat = 0x7fffffff;
    642  1.1  cgd 		else
    643  1.1  cgd 			rat = in_count / rat;
    644  1.1  cgd 	} else
    645  1.1  cgd 		rat = (in_count << 8) / bytes_out;	/* 8 fractional bits. */
    646  1.1  cgd 	if (rat > ratio)
    647  1.1  cgd 		ratio = rat;
    648  1.1  cgd 	else {
    649  1.1  cgd 		ratio = 0;
    650  1.1  cgd 		cl_hash(zs, (count_int) hsize);
    651  1.1  cgd 		free_ent = FIRST;
    652  1.1  cgd 		clear_flg = 1;
    653  1.1  cgd 		if (output(zs, (code_int) CLEAR) == -1)
    654  1.1  cgd 			return (-1);
    655  1.1  cgd 	}
    656  1.1  cgd 	return (0);
    657  1.1  cgd }
    658  1.1  cgd 
    659  1.1  cgd static void
    660  1.1  cgd cl_hash(zs, cl_hsize)			/* Reset code table. */
    661  1.1  cgd 	struct s_zstate *zs;
    662  1.1  cgd 	register count_int cl_hsize;
    663  1.1  cgd {
    664  1.1  cgd 	register count_int *htab_p;
    665  1.1  cgd 	register long i, m1;
    666  1.1  cgd 
    667  1.1  cgd 	m1 = -1;
    668  1.1  cgd 	htab_p = htab + cl_hsize;
    669  1.1  cgd 	i = cl_hsize - 16;
    670  1.1  cgd 	do {			/* Might use Sys V memset(3) here. */
    671  1.1  cgd 		*(htab_p - 16) = m1;
    672  1.1  cgd 		*(htab_p - 15) = m1;
    673  1.1  cgd 		*(htab_p - 14) = m1;
    674  1.1  cgd 		*(htab_p - 13) = m1;
    675  1.1  cgd 		*(htab_p - 12) = m1;
    676  1.1  cgd 		*(htab_p - 11) = m1;
    677  1.1  cgd 		*(htab_p - 10) = m1;
    678  1.1  cgd 		*(htab_p - 9) = m1;
    679  1.1  cgd 		*(htab_p - 8) = m1;
    680  1.1  cgd 		*(htab_p - 7) = m1;
    681  1.1  cgd 		*(htab_p - 6) = m1;
    682  1.1  cgd 		*(htab_p - 5) = m1;
    683  1.1  cgd 		*(htab_p - 4) = m1;
    684  1.1  cgd 		*(htab_p - 3) = m1;
    685  1.1  cgd 		*(htab_p - 2) = m1;
    686  1.1  cgd 		*(htab_p - 1) = m1;
    687  1.1  cgd 		htab_p -= 16;
    688  1.1  cgd 	} while ((i -= 16) >= 0);
    689  1.1  cgd 	for (i += 16; i > 0; i--)
    690  1.1  cgd 		*--htab_p = m1;
    691  1.1  cgd }
    692  1.1  cgd 
    693  1.1  cgd FILE *
    694  1.1  cgd zopen(fname, mode, bits)
    695  1.1  cgd 	const char *fname, *mode;
    696  1.1  cgd 	int bits;
    697  1.1  cgd {
    698  1.1  cgd 	struct s_zstate *zs;
    699  1.1  cgd 
    700  1.1  cgd 	if (mode[0] != 'r' && mode[0] != 'w' || mode[1] != '\0' ||
    701  1.1  cgd 	    bits < 0 || bits > BITS) {
    702  1.1  cgd 		errno = EINVAL;
    703  1.1  cgd 		return (NULL);
    704  1.1  cgd 	}
    705  1.1  cgd 
    706  1.1  cgd 	if ((zs = calloc(1, sizeof(struct s_zstate))) == NULL)
    707  1.1  cgd 		return (NULL);
    708  1.1  cgd 
    709  1.1  cgd 	maxbits = bits ? bits : BITS;	/* User settable max # bits/code. */
    710  1.1  cgd 	maxmaxcode = 1 << BITS;		/* Should NEVER generate this code. */
    711  1.1  cgd 	hsize = HSIZE;			/* For dynamic table sizing. */
    712  1.1  cgd 	free_ent = 0;			/* First unused entry. */
    713  1.1  cgd 	block_compress = BLOCK_MASK;
    714  1.1  cgd 	clear_flg = 0;
    715  1.1  cgd 	ratio = 0;
    716  1.1  cgd 	checkpoint = CHECK_GAP;
    717  1.1  cgd 	in_count = 1;			/* Length of input. */
    718  1.1  cgd 	out_count = 0;			/* # of codes output (for debugging). */
    719  1.1  cgd 	state = S_START;
    720  1.1  cgd 	roffset = 0;
    721  1.1  cgd 	size = 0;
    722  1.1  cgd 
    723  1.1  cgd 	/*
    724  1.1  cgd 	 * Layering compress on top of stdio in order to provide buffering,
    725  1.1  cgd 	 * and ensure that reads and write work with the data specified.
    726  1.1  cgd 	 */
    727  1.1  cgd 	if ((fp = fopen(fname, mode)) == NULL) {
    728  1.1  cgd 		free(zs);
    729  1.1  cgd 		return (NULL);
    730  1.1  cgd 	}
    731  1.1  cgd 	switch (*mode) {
    732  1.1  cgd 	case 'r':
    733  1.1  cgd 		zmode = 'r';
    734  1.1  cgd 		return (funopen(zs, zread, NULL, NULL, zclose));
    735  1.1  cgd 	case 'w':
    736  1.1  cgd 		zmode = 'w';
    737  1.1  cgd 		return (funopen(zs, NULL, zwrite, NULL, zclose));
    738  1.1  cgd 	}
    739  1.1  cgd 	/* NOTREACHED */
    740  1.1  cgd }
    741