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