zopen.c revision 87628
1236884Smm/*-
2236884Smm * Copyright (c) 1985, 1986, 1992, 1993
3236884Smm *	The Regents of the University of California.  All rights reserved.
4236884Smm *
5236884Smm * This code is derived from software contributed to Berkeley by
6236884Smm * Diomidis Spinellis and James A. Woods, derived from original
7236884Smm * work by Spencer Thomas and Joseph Orost.
8236884Smm *
9236884Smm * Redistribution and use in source and binary forms, with or without
10236884Smm * modification, are permitted provided that the following conditions
11236884Smm * are met:
12236884Smm * 1. Redistributions of source code must retain the above copyright
13236884Smm *    notice, this list of conditions and the following disclaimer.
14236884Smm * 2. Redistributions in binary form must reproduce the above copyright
15236884Smm *    notice, this list of conditions and the following disclaimer in the
16236884Smm *    documentation and/or other materials provided with the distribution.
17236884Smm * 3. All advertising materials mentioning features or use of this software
18236884Smm *    must display the following acknowledgement:
19236884Smm *	This product includes software developed by the University of
20236884Smm *	California, Berkeley and its contributors.
21236884Smm * 4. Neither the name of the University nor the names of its contributors
22236884Smm *    may be used to endorse or promote products derived from this software
23268657Sdelphij *    without specific prior written permission.
24236884Smm *
25236884Smm * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26236884Smm * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27236884Smm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28236884Smm * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29236884Smm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30236884Smm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31236884Smm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32236884Smm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33236884Smm * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34236884Smm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35236884Smm * SUCH DAMAGE.
36236884Smm */
37236884Smm
38236884Smm#if defined(LIBC_SCCS) && !defined(lint)
39236884Smmstatic char sccsid[] = "@(#)zopen.c	8.1 (Berkeley) 6/27/93";
40236884Smm#endif /* LIBC_SCCS and not lint */
41236884Smm
42236884Smm#include <sys/cdefs.h>
43236884Smm__FBSDID("$FreeBSD: head/usr.bin/compress/zopen.c 87628 2001-12-10 21:13:08Z dwmalone $");
44236884Smm
45236884Smm/*-
46251631Sdelphij * fcompress.c - File compression ala IEEE Computer, June 1984.
47236884Smm *
48236884Smm * Compress authors:
49236884Smm *		Spencer W. Thomas	(decvax!utah-cs!thomas)
50236884Smm *		Jim McKie		(decvax!mcvax!jim)
51236884Smm *		Steve Davies		(decvax!vax135!petsd!peora!srd)
52236884Smm *		Ken Turkowski		(decvax!decwrl!turtlevax!ken)
53236884Smm *		James A. Woods		(decvax!ihnp4!ames!jaw)
54236884Smm *		Joe Orost		(decvax!vax135!petsd!joe)
55236884Smm *
56236884Smm * Cleaned up and converted to library returning I/O streams by
57236884Smm * Diomidis Spinellis <dds@doc.ic.ac.uk>.
58236884Smm *
59236884Smm * zopen(filename, mode, bits)
60236884Smm *	Returns a FILE * that can be used for read or write.  The modes
61236884Smm *	supported are only "r" and "w".  Seeking is not allowed.  On
62236884Smm *	reading the file is decompressed, on writing it is compressed.
63236884Smm *	The output is compatible with compress(1) with 16 bit tables.
64236884Smm *	Any file produced by compress(1) can be read.
65236884Smm */
66236884Smm
67236884Smm#include <sys/param.h>
68276081Sdelphij#include <sys/stat.h>
69236884Smm
70236884Smm#include <ctype.h>
71236884Smm#include <errno.h>
72236884Smm#include <signal.h>
73236884Smm#include <stdio.h>
74236884Smm#include <stdlib.h>
75236884Smm#include <string.h>
76236884Smm#include <unistd.h>
77236884Smm#include "zopen.h"
78236884Smm
79236884Smm#define	BITS		16		/* Default bits. */
80236884Smm#define	HSIZE		69001		/* 95% occupancy */
81236884Smm
82236884Smm/* A code_int must be able to hold 2**BITS values of type int, and also -1. */
83236884Smmtypedef long code_int;
84236884Smmtypedef long count_int;
85236884Smm
86236884Smmtypedef u_char char_type;
87236884Smmstatic char_type magic_header[] =
88236884Smm	{'\037', '\235'};		/* 1F 9D */
89236884Smm
90236884Smm#define	BIT_MASK	0x1f		/* Defines for third byte of header. */
91236884Smm#define	BLOCK_MASK	0x80
92236884Smm
93236884Smm/*
94236884Smm * Masks 0x40 and 0x20 are free.  I think 0x20 should mean that there is
95236884Smm * a fourth header byte (for expansion).
96236884Smm */
97240415Smm#define	INIT_BITS 9			/* Initial number of bits/code. */
98240415Smm
99240415Smm#define	MAXCODE(n_bits)	((1 << (n_bits)) - 1)
100236884Smm
101236884Smmstruct s_zstate {
102236884Smm	FILE *zs_fp;			/* File stream for I/O */
103236884Smm	char zs_mode;			/* r or w */
104236884Smm	enum {
105268650Sdelphij		S_START, S_MIDDLE, S_EOF
106268650Sdelphij	} zs_state;			/* State of computation */
107268650Sdelphij	u_int zs_n_bits;		/* Number of bits/code. */
108268650Sdelphij	u_int zs_maxbits;		/* User settable max # bits/code. */
109268650Sdelphij	code_int zs_maxcode;		/* Maximum code, given n_bits. */
110268650Sdelphij	code_int zs_maxmaxcode;		/* Should NEVER generate this code. */
111268650Sdelphij	count_int zs_htab [HSIZE];
112268650Sdelphij	u_short zs_codetab [HSIZE];
113268650Sdelphij	code_int zs_hsize;		/* For dynamic table sizing. */
114268650Sdelphij	code_int zs_free_ent;		/* First unused entry. */
115268650Sdelphij	/*
116268650Sdelphij	 * Block compression parameters -- after all codes are used up,
117268650Sdelphij	 * and compression rate changes, start over.
118268650Sdelphij	 */
119236884Smm	int zs_block_compress;
120236884Smm	int zs_clear_flg;
121236884Smm	long zs_ratio;
122236884Smm	count_int zs_checkpoint;
123236884Smm	u_int zs_offset;
124236884Smm	long zs_in_count;		/* Length of input. */
125268650Sdelphij	long zs_bytes_out;		/* Length of compressed output. */
126236884Smm	long zs_out_count;		/* # of codes output (for debugging). */
127236884Smm	char_type zs_buf[BITS];
128236884Smm	union {
129236884Smm		struct {
130236884Smm			long zs_fcode;
131236884Smm			code_int zs_ent;
132236884Smm			code_int zs_hsize_reg;
133236884Smm			int zs_hshift;
134236884Smm		} w;			/* Write paramenters */
135236884Smm		struct {
136236884Smm			char_type *zs_stackp;
137236884Smm			int zs_finchar;
138236884Smm			code_int zs_code, zs_oldcode, zs_incode;
139236884Smm			int zs_roffset, zs_size;
140236884Smm			char_type zs_gbuf[BITS];
141236884Smm		} r;			/* Read parameters */
142236884Smm	} u;
143236884Smm};
144236884Smm
145236884Smm/* Definitions to retain old variable names */
146236884Smm#define	fp		zs->zs_fp
147236884Smm#define	zmode		zs->zs_mode
148236884Smm#define	state		zs->zs_state
149236884Smm#define	n_bits		zs->zs_n_bits
150236884Smm#define	maxbits		zs->zs_maxbits
151246666Smm#define	maxcode		zs->zs_maxcode
152268657Sdelphij#define	maxmaxcode	zs->zs_maxmaxcode
153236884Smm#define	htab		zs->zs_htab
154236884Smm#define	codetab		zs->zs_codetab
155236884Smm#define	hsize		zs->zs_hsize
156236884Smm#define	free_ent	zs->zs_free_ent
157263397Sdelphij#define	block_compress	zs->zs_block_compress
158236884Smm#define	clear_flg	zs->zs_clear_flg
159236884Smm#define	ratio		zs->zs_ratio
160236884Smm#define	checkpoint	zs->zs_checkpoint
161236884Smm#define	offset		zs->zs_offset
162236884Smm#define	in_count	zs->zs_in_count
163236884Smm#define	bytes_out	zs->zs_bytes_out
164236884Smm#define	out_count	zs->zs_out_count
165236884Smm#define	buf		zs->zs_buf
166236884Smm#define	fcode		zs->u.w.zs_fcode
167236884Smm#define	hsize_reg	zs->u.w.zs_hsize_reg
168236884Smm#define	ent		zs->u.w.zs_ent
169268650Sdelphij#define	hshift		zs->u.w.zs_hshift
170268650Sdelphij#define	stackp		zs->u.r.zs_stackp
171268650Sdelphij#define	finchar		zs->u.r.zs_finchar
172268650Sdelphij#define	code		zs->u.r.zs_code
173268650Sdelphij#define	oldcode		zs->u.r.zs_oldcode
174268650Sdelphij#define	incode		zs->u.r.zs_incode
175268650Sdelphij#define	roffset		zs->u.r.zs_roffset
176268650Sdelphij#define	size		zs->u.r.zs_size
177268650Sdelphij#define	gbuf		zs->u.r.zs_gbuf
178268650Sdelphij
179268650Sdelphij/*
180268650Sdelphij * To save much memory, we overlay the table used by compress() with those
181268650Sdelphij * used by decompress().  The tab_prefix table is the same size and type as
182268650Sdelphij * the codetab.  The tab_suffix table needs 2**BITS characters.  We get this
183268650Sdelphij * from the beginning of htab.  The output stack uses the rest of htab, and
184268650Sdelphij * contains characters.  There is plenty of room for any possible stack
185236884Smm * (stack used to be 8000 characters).
186236884Smm */
187236884Smm
188236884Smm#define	htabof(i)	htab[i]
189268650Sdelphij#define	codetabof(i)	codetab[i]
190236884Smm
191236884Smm#define	tab_prefixof(i)	codetabof(i)
192236884Smm#define	tab_suffixof(i)	((char_type *)(htab))[i]
193236884Smm#define	de_stack	((char_type *)&tab_suffixof(1 << BITS))
194236884Smm
195236884Smm#define	CHECK_GAP 10000		/* Ratio check interval. */
196236884Smm
197236884Smm/*
198236884Smm * the next two codes should not be changed lightly, as they must not
199236884Smm * lie within the contiguous general code space.
200236884Smm */
201236884Smm#define	FIRST	257		/* First free entry. */
202236884Smm#define	CLEAR	256		/* Table clear output code. */
203236884Smm
204236884Smmstatic int	cl_block __P((struct s_zstate *));
205236884Smmstatic void	cl_hash __P((struct s_zstate *, count_int));
206236884Smmstatic code_int	getcode __P((struct s_zstate *));
207236884Smmstatic int	output __P((struct s_zstate *, code_int));
208236884Smmstatic int	zclose __P((void *));
209236884Smmstatic int	zread __P((void *, char *, int));
210236884Smmstatic int	zwrite __P((void *, const char *, int));
211236884Smm
212236884Smm/*-
213262120Savg * Algorithm from "A Technique for High Performance Data Compression",
214236884Smm * Terry A. Welch, IEEE Computer Vol 17, No 6 (June 1984), pp 8-19.
215236884Smm *
216236884Smm * Algorithm:
217236884Smm * 	Modified Lempel-Ziv method (LZW).  Basically finds common
218236884Smm * substrings and replaces them with a variable size code.  This is
219236884Smm * deterministic, and can be done on the fly.  Thus, the decompression
220268650Sdelphij * procedure needs no input table, but tracks the way the table was built.
221262120Savg */
222268650Sdelphij
223268650Sdelphij/*-
224268650Sdelphij * compress write
225268650Sdelphij *
226268650Sdelphij * Algorithm:  use open addressing double hashing (no chaining) on the
227268650Sdelphij * prefix code / next character combination.  We do a variant of Knuth's
228268650Sdelphij * algorithm D (vol. 3, sec. 6.4) along with G. Knott's relatively-prime
229236884Smm * secondary probe.  Here, the modular division first probe is gives way
230262120Savg * to a faster exclusive-or manipulation.  Also do block compression with
231236884Smm * an adaptive reset, whereby the code table is cleared when the compression
232236884Smm * ratio decreases, but after the table fills.  The variable-length output
233268650Sdelphij * codes are re-sized at this point, and a special CLEAR code is generated
234268650Sdelphij * for the decompressor.  Late addition:  construct the table according to
235268650Sdelphij * file size for noticeable speed improvement on small files.  Please direct
236268650Sdelphij * questions about this implementation to ames!jaw.
237268650Sdelphij */
238268650Sdelphijstatic int
239268650Sdelphijzwrite(cookie, wbp, num)
240236884Smm	void *cookie;
241236884Smm	const char *wbp;
242236884Smm	int num;
243240415Smm{
244236884Smm	code_int i;
245236884Smm	int c, disp;
246268650Sdelphij	struct s_zstate *zs;
247268650Sdelphij	const u_char *bp;
248268650Sdelphij	u_char tmp;
249268650Sdelphij	int count;
250268650Sdelphij
251268650Sdelphij	if (num == 0)
252268650Sdelphij		return (0);
253268650Sdelphij
254268650Sdelphij	zs = cookie;
255268650Sdelphij	count = num;
256268650Sdelphij	bp = wbp;
257268650Sdelphij	if (state == S_MIDDLE)
258262120Savg		goto middle;
259268650Sdelphij	state = S_MIDDLE;
260268650Sdelphij
261268650Sdelphij	maxmaxcode = 1L << maxbits;
262268650Sdelphij	if (fwrite(magic_header,
263268650Sdelphij	    sizeof(char), sizeof(magic_header), fp) != sizeof(magic_header))
264262120Savg		return (-1);
265268650Sdelphij	tmp = (u_char)((maxbits) | block_compress);
266268650Sdelphij	if (fwrite(&tmp, sizeof(char), sizeof(tmp), fp) != sizeof(tmp))
267268650Sdelphij		return (-1);
268262120Savg
269262120Savg	offset = 0;
270268650Sdelphij	bytes_out = 3;		/* Includes 3-byte header mojo. */
271268650Sdelphij	out_count = 0;
272268650Sdelphij	clear_flg = 0;
273268650Sdelphij	ratio = 0;
274268650Sdelphij	in_count = 1;
275268650Sdelphij	checkpoint = CHECK_GAP;
276268650Sdelphij	maxcode = MAXCODE(n_bits = INIT_BITS);
277236884Smm	free_ent = ((block_compress) ? FIRST : 256);
278236884Smm
279236884Smm	ent = *bp++;
280268650Sdelphij	--count;
281268650Sdelphij
282236884Smm	hshift = 0;
283236884Smm	for (fcode = (long)hsize; fcode < 65536L; fcode *= 2L)
284236884Smm		hshift++;
285268650Sdelphij	hshift = 8 - hshift;	/* Set hash code range bound. */
286268650Sdelphij
287268650Sdelphij	hsize_reg = hsize;
288268650Sdelphij	cl_hash(zs, (count_int)hsize_reg);	/* Clear hash table. */
289268650Sdelphij
290268650Sdelphijmiddle:	for (i = 0; count--;) {
291240415Smm		c = *bp++;
292240415Smm		in_count++;
293240415Smm		fcode = (long)(((long)c << maxbits) + ent);
294236884Smm		i = ((c << hshift) ^ ent);	/* Xor hashing. */
295236884Smm
296236884Smm		if (htabof(i) == fcode) {
297236884Smm			ent = codetabof(i);
298236884Smm			continue;
299236884Smm		} else if ((long)htabof(i) < 0)	/* Empty slot. */
300			goto nomatch;
301		disp = hsize_reg - i;	/* Secondary hash (after G. Knott). */
302		if (i == 0)
303			disp = 1;
304probe:		if ((i -= disp) < 0)
305			i += hsize_reg;
306
307		if (htabof(i) == fcode) {
308			ent = codetabof(i);
309			continue;
310		}
311		if ((long)htabof(i) >= 0)
312			goto probe;
313nomatch:	if (output(zs, (code_int) ent) == -1)
314			return (-1);
315		out_count++;
316		ent = c;
317		if (free_ent < maxmaxcode) {
318			codetabof(i) = free_ent++;	/* code -> hashtable */
319			htabof(i) = fcode;
320		} else if ((count_int)in_count >=
321		    checkpoint && block_compress) {
322			if (cl_block(zs) == -1)
323				return (-1);
324		}
325	}
326	return (num);
327}
328
329static int
330zclose(cookie)
331	void *cookie;
332{
333	struct s_zstate *zs;
334	int rval;
335
336	zs = cookie;
337	if (zmode == 'w') {		/* Put out the final code. */
338		if (output(zs, (code_int) ent) == -1) {
339			(void)fclose(fp);
340			free(zs);
341			return (-1);
342		}
343		out_count++;
344		if (output(zs, (code_int) - 1) == -1) {
345			(void)fclose(fp);
346			free(zs);
347			return (-1);
348		}
349	}
350	rval = fclose(fp) == EOF ? -1 : 0;
351	free(zs);
352	return (rval);
353}
354
355/*-
356 * Output the given code.
357 * Inputs:
358 * 	code:	A n_bits-bit integer.  If == -1, then EOF.  This assumes
359 *		that n_bits =< (long)wordsize - 1.
360 * Outputs:
361 * 	Outputs code to the file.
362 * Assumptions:
363 *	Chars are 8 bits long.
364 * Algorithm:
365 * 	Maintain a BITS character long buffer (so that 8 codes will
366 * fit in it exactly).  Use the VAX insv instruction to insert each
367 * code in turn.  When the buffer fills up empty it and start over.
368 */
369
370static char_type lmask[9] =
371	{0xff, 0xfe, 0xfc, 0xf8, 0xf0, 0xe0, 0xc0, 0x80, 0x00};
372static char_type rmask[9] =
373	{0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff};
374
375static int
376output(zs, ocode)
377	struct s_zstate *zs;
378	code_int ocode;
379{
380	int r_off;
381	u_int bits;
382	char_type *bp;
383
384	r_off = offset;
385	bits = n_bits;
386	bp = buf;
387	if (ocode >= 0) {
388		/* Get to the first byte. */
389		bp += (r_off >> 3);
390		r_off &= 7;
391		/*
392		 * Since ocode is always >= 8 bits, only need to mask the first
393		 * hunk on the left.
394		 */
395		*bp = (*bp & rmask[r_off]) | ((ocode << r_off) & lmask[r_off]);
396		bp++;
397		bits -= (8 - r_off);
398		ocode >>= 8 - r_off;
399		/* Get any 8 bit parts in the middle (<=1 for up to 16 bits). */
400		if (bits >= 8) {
401			*bp++ = ocode;
402			ocode >>= 8;
403			bits -= 8;
404		}
405		/* Last bits. */
406		if (bits)
407			*bp = ocode;
408		offset += n_bits;
409		if (offset == (n_bits << 3)) {
410			bp = buf;
411			bits = n_bits;
412			bytes_out += bits;
413			if (fwrite(bp, sizeof(char), bits, fp) != bits)
414				return (-1);
415			bp += bits;
416			bits = 0;
417			offset = 0;
418		}
419		/*
420		 * If the next entry is going to be too big for the ocode size,
421		 * then increase it, if possible.
422		 */
423		if (free_ent > maxcode || (clear_flg > 0)) {
424		       /*
425			* Write the whole buffer, because the input side won't
426			* discover the size increase until after it has read it.
427			*/
428			if (offset > 0) {
429				if (fwrite(buf, 1, n_bits, fp) != n_bits)
430					return (-1);
431				bytes_out += n_bits;
432			}
433			offset = 0;
434
435			if (clear_flg) {
436				maxcode = MAXCODE(n_bits = INIT_BITS);
437				clear_flg = 0;
438			} else {
439				n_bits++;
440				if (n_bits == maxbits)
441					maxcode = maxmaxcode;
442				else
443					maxcode = MAXCODE(n_bits);
444			}
445		}
446	} else {
447		/* At EOF, write the rest of the buffer. */
448		if (offset > 0) {
449			offset = (offset + 7) / 8;
450			if (fwrite(buf, 1, offset, fp) != offset)
451				return (-1);
452			bytes_out += offset;
453		}
454		offset = 0;
455	}
456	return (0);
457}
458
459/*
460 * Decompress read.  This routine adapts to the codes in the file building
461 * the "string" table on-the-fly; requiring no table to be stored in the
462 * compressed file.  The tables used herein are shared with those of the
463 * compress() routine.  See the definitions above.
464 */
465static int
466zread(cookie, rbp, num)
467	void *cookie;
468	char *rbp;
469	int num;
470{
471	u_int count;
472	struct s_zstate *zs;
473	u_char *bp, header[3];
474
475	if (num == 0)
476		return (0);
477
478	zs = cookie;
479	count = num;
480	bp = (u_char *)rbp;
481	switch (state) {
482	case S_START:
483		state = S_MIDDLE;
484		break;
485	case S_MIDDLE:
486		goto middle;
487	case S_EOF:
488		goto eof;
489	}
490
491	/* Check the magic number */
492	if (fread(header,
493	    sizeof(char), sizeof(header), fp) != sizeof(header) ||
494	    memcmp(header, magic_header, sizeof(magic_header)) != 0) {
495		errno = EFTYPE;
496		return (-1);
497	}
498	maxbits = header[2];	/* Set -b from file. */
499	block_compress = maxbits & BLOCK_MASK;
500	maxbits &= BIT_MASK;
501	maxmaxcode = 1L << maxbits;
502	if (maxbits > BITS) {
503		errno = EFTYPE;
504		return (-1);
505	}
506	/* As above, initialize the first 256 entries in the table. */
507	maxcode = MAXCODE(n_bits = INIT_BITS);
508	for (code = 255; code >= 0; code--) {
509		tab_prefixof(code) = 0;
510		tab_suffixof(code) = (char_type) code;
511	}
512	free_ent = block_compress ? FIRST : 256;
513
514	finchar = oldcode = getcode(zs);
515	if (oldcode == -1)	/* EOF already? */
516		return (0);	/* Get out of here */
517
518	/* First code must be 8 bits = char. */
519	*bp++ = (u_char)finchar;
520	count--;
521	stackp = de_stack;
522
523	while ((code = getcode(zs)) > -1) {
524
525		if ((code == CLEAR) && block_compress) {
526			for (code = 255; code >= 0; code--)
527				tab_prefixof(code) = 0;
528			clear_flg = 1;
529			free_ent = FIRST - 1;
530			if ((code = getcode(zs)) == -1)	/* O, untimely death! */
531				break;
532		}
533		incode = code;
534
535		/* Special case for KwKwK string. */
536		if (code >= free_ent) {
537			*stackp++ = finchar;
538			code = oldcode;
539		}
540
541		/* Generate output characters in reverse order. */
542		while (code >= 256) {
543			*stackp++ = tab_suffixof(code);
544			code = tab_prefixof(code);
545		}
546		*stackp++ = finchar = tab_suffixof(code);
547
548		/* And put them out in forward order.  */
549middle:		do {
550			if (count-- == 0)
551				return (num);
552			*bp++ = *--stackp;
553		} while (stackp > de_stack);
554
555		/* Generate the new entry. */
556		if ((code = free_ent) < maxmaxcode) {
557			tab_prefixof(code) = (u_short) oldcode;
558			tab_suffixof(code) = finchar;
559			free_ent = code + 1;
560		}
561
562		/* Remember previous code. */
563		oldcode = incode;
564	}
565	state = S_EOF;
566eof:	return (num - count);
567}
568
569/*-
570 * Read one code from the standard input.  If EOF, return -1.
571 * Inputs:
572 * 	stdin
573 * Outputs:
574 * 	code or -1 is returned.
575 */
576static code_int
577getcode(zs)
578	struct s_zstate *zs;
579{
580	code_int gcode;
581	int r_off, bits;
582	char_type *bp;
583
584	bp = gbuf;
585	if (clear_flg > 0 || roffset >= size || free_ent > maxcode) {
586		/*
587		 * If the next entry will be too big for the current gcode
588		 * size, then we must increase the size.  This implies reading
589		 * a new buffer full, too.
590		 */
591		if (free_ent > maxcode) {
592			n_bits++;
593			if (n_bits == maxbits)	/* Won't get any bigger now. */
594				maxcode = maxmaxcode;
595			else
596				maxcode = MAXCODE(n_bits);
597		}
598		if (clear_flg > 0) {
599			maxcode = MAXCODE(n_bits = INIT_BITS);
600			clear_flg = 0;
601		}
602		size = fread(gbuf, 1, n_bits, fp);
603		if (size <= 0)			/* End of file. */
604			return (-1);
605		roffset = 0;
606		/* Round size down to integral number of codes. */
607		size = (size << 3) - (n_bits - 1);
608	}
609	r_off = roffset;
610	bits = n_bits;
611
612	/* Get to the first byte. */
613	bp += (r_off >> 3);
614	r_off &= 7;
615
616	/* Get first part (low order bits). */
617	gcode = (*bp++ >> r_off);
618	bits -= (8 - r_off);
619	r_off = 8 - r_off;	/* Now, roffset into gcode word. */
620
621	/* Get any 8 bit parts in the middle (<=1 for up to 16 bits). */
622	if (bits >= 8) {
623		gcode |= *bp++ << r_off;
624		r_off += 8;
625		bits -= 8;
626	}
627
628	/* High order bits. */
629	gcode |= (*bp & rmask[bits]) << r_off;
630	roffset += n_bits;
631
632	return (gcode);
633}
634
635static int
636cl_block(zs)			/* Table clear for block compress. */
637	struct s_zstate *zs;
638{
639	long rat;
640
641	checkpoint = in_count + CHECK_GAP;
642
643	if (in_count > 0x007fffff) {	/* Shift will overflow. */
644		rat = bytes_out >> 8;
645		if (rat == 0)		/* Don't divide by zero. */
646			rat = 0x7fffffff;
647		else
648			rat = in_count / rat;
649	} else
650		rat = (in_count << 8) / bytes_out;	/* 8 fractional bits. */
651	if (rat > ratio)
652		ratio = rat;
653	else {
654		ratio = 0;
655		cl_hash(zs, (count_int) hsize);
656		free_ent = FIRST;
657		clear_flg = 1;
658		if (output(zs, (code_int) CLEAR) == -1)
659			return (-1);
660	}
661	return (0);
662}
663
664static void
665cl_hash(zs, cl_hsize)			/* Reset code table. */
666	struct s_zstate *zs;
667	count_int cl_hsize;
668{
669	count_int *htab_p;
670	long i, m1;
671
672	m1 = -1;
673	htab_p = htab + cl_hsize;
674	i = cl_hsize - 16;
675	do {			/* Might use Sys V memset(3) here. */
676		*(htab_p - 16) = m1;
677		*(htab_p - 15) = m1;
678		*(htab_p - 14) = m1;
679		*(htab_p - 13) = m1;
680		*(htab_p - 12) = m1;
681		*(htab_p - 11) = m1;
682		*(htab_p - 10) = m1;
683		*(htab_p - 9) = m1;
684		*(htab_p - 8) = m1;
685		*(htab_p - 7) = m1;
686		*(htab_p - 6) = m1;
687		*(htab_p - 5) = m1;
688		*(htab_p - 4) = m1;
689		*(htab_p - 3) = m1;
690		*(htab_p - 2) = m1;
691		*(htab_p - 1) = m1;
692		htab_p -= 16;
693	} while ((i -= 16) >= 0);
694	for (i += 16; i > 0; i--)
695		*--htab_p = m1;
696}
697
698FILE *
699zopen(fname, mode, bits)
700	const char *fname, *mode;
701	int bits;
702{
703	struct s_zstate *zs;
704
705	if ((mode[0] != 'r' && mode[0] != 'w') || mode[1] != '\0' ||
706	    bits < 0 || bits > BITS) {
707		errno = EINVAL;
708		return (NULL);
709	}
710
711	if ((zs = calloc(1, sizeof(struct s_zstate))) == NULL)
712		return (NULL);
713
714	maxbits = bits ? bits : BITS;	/* User settable max # bits/code. */
715	maxmaxcode = 1L << maxbits;	/* Should NEVER generate this code. */
716	hsize = HSIZE;			/* For dynamic table sizing. */
717	free_ent = 0;			/* First unused entry. */
718	block_compress = BLOCK_MASK;
719	clear_flg = 0;
720	ratio = 0;
721	checkpoint = CHECK_GAP;
722	in_count = 1;			/* Length of input. */
723	out_count = 0;			/* # of codes output (for debugging). */
724	state = S_START;
725	roffset = 0;
726	size = 0;
727
728	/*
729	 * Layering compress on top of stdio in order to provide buffering,
730	 * and ensure that reads and write work with the data specified.
731	 */
732	if ((fp = fopen(fname, mode)) == NULL) {
733		free(zs);
734		return (NULL);
735	}
736	switch (*mode) {
737	case 'r':
738		zmode = 'r';
739		return (funopen(zs, zread, NULL, NULL, zclose));
740	case 'w':
741		zmode = 'w';
742		return (funopen(zs, NULL, zwrite, NULL, zclose));
743	}
744	/* NOTREACHED */
745	return (NULL);
746}
747