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