xref: /onnv-gate/usr/src/uts/common/io/ppp/spppcomp/zlib.c (revision 5084:7d838c5c0eed)
10Sstevel@tonic-gate /*
2*3886Sahl  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
30Sstevel@tonic-gate  * Use is subject to license terms.
40Sstevel@tonic-gate  */
50Sstevel@tonic-gate 
60Sstevel@tonic-gate /*
70Sstevel@tonic-gate  * Updated from zlib-1.0.4 to zlib-1.1.3 by James Carlson.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * This file is derived from various .h and .c files from the zlib-1.0.4
100Sstevel@tonic-gate  * distribution by Jean-loup Gailly and Mark Adler, with some additions
110Sstevel@tonic-gate  * by Paul Mackerras to aid in implementing Deflate compression and
120Sstevel@tonic-gate  * decompression for PPP packets.  See zlib.h for conditions of
130Sstevel@tonic-gate  * distribution and use.
140Sstevel@tonic-gate  *
150Sstevel@tonic-gate  * Changes that have been made include:
160Sstevel@tonic-gate  * - added Z_PACKET_FLUSH (see zlib.h for details)
170Sstevel@tonic-gate  * - added inflateIncomp and deflateOutputPending
180Sstevel@tonic-gate  * - allow strm->next_out to be NULL, meaning discard the output
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * $Id: zlib.c,v 1.11 1998/09/13 23:37:12 paulus Exp $
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate 
230Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
240Sstevel@tonic-gate 
250Sstevel@tonic-gate /*
260Sstevel@tonic-gate  *  ==FILEVERSION 971210==
270Sstevel@tonic-gate  *
280Sstevel@tonic-gate  * This marker is used by the Linux installation script to determine
290Sstevel@tonic-gate  * whether an up-to-date version of this file is already installed.
300Sstevel@tonic-gate  */
310Sstevel@tonic-gate 
320Sstevel@tonic-gate #define	NO_DUMMY_DECL
330Sstevel@tonic-gate #define	NO_ZCFUNCS
340Sstevel@tonic-gate #define	MY_ZCALLOC
350Sstevel@tonic-gate 
360Sstevel@tonic-gate #if defined(__FreeBSD__) && (defined(KERNEL) || defined(_KERNEL))
370Sstevel@tonic-gate #define	inflate	inflate_ppp	/* FreeBSD already has an inflate :-( */
380Sstevel@tonic-gate #endif
390Sstevel@tonic-gate 
400Sstevel@tonic-gate 
410Sstevel@tonic-gate /* +++ zutil.h */
420Sstevel@tonic-gate /*
430Sstevel@tonic-gate  *
440Sstevel@tonic-gate  * zutil.h -- internal interface and configuration of the compression library
450Sstevel@tonic-gate  * Copyright (C) 1995-1998 Jean-loup Gailly.
460Sstevel@tonic-gate  * For conditions of distribution and use, see copyright notice in zlib.h
470Sstevel@tonic-gate  */
480Sstevel@tonic-gate 
490Sstevel@tonic-gate /*
500Sstevel@tonic-gate  * WARNING: this file should *not* be used by applications. It is part
510Sstevel@tonic-gate  * of the implementation of the compression library and is subject to
520Sstevel@tonic-gate  * change. Applications should only use zlib.h.
530Sstevel@tonic-gate  */
540Sstevel@tonic-gate 
550Sstevel@tonic-gate /* From: zutil.h,v 1.16 1996/07/24 13:41:13 me Exp $ */
560Sstevel@tonic-gate 
570Sstevel@tonic-gate #ifndef _Z_UTIL_H
580Sstevel@tonic-gate #define	_Z_UTIL_H
590Sstevel@tonic-gate 
600Sstevel@tonic-gate #include "zlib.h"
610Sstevel@tonic-gate 
620Sstevel@tonic-gate #if defined(KERNEL) || defined(_KERNEL)
630Sstevel@tonic-gate /* Assume this is a *BSD or SVR4 kernel */
640Sstevel@tonic-gate #include <sys/types.h>
650Sstevel@tonic-gate #include <sys/time.h>
660Sstevel@tonic-gate #include <sys/systm.h>
670Sstevel@tonic-gate #ifdef SOL2
680Sstevel@tonic-gate #include <sys/cmn_err.h>
690Sstevel@tonic-gate #endif
700Sstevel@tonic-gate #define	HAVE_MEMCPY
710Sstevel@tonic-gate #define	memcmp		bcmp
720Sstevel@tonic-gate 
730Sstevel@tonic-gate #else
740Sstevel@tonic-gate #if defined(__KERNEL__)
750Sstevel@tonic-gate /* Assume this is a Linux kernel */
760Sstevel@tonic-gate #include <linux/string.h>
770Sstevel@tonic-gate #define	HAVE_MEMCPY
780Sstevel@tonic-gate 
790Sstevel@tonic-gate #else /* not kernel */
800Sstevel@tonic-gate 
810Sstevel@tonic-gate #include <stddef.h>
820Sstevel@tonic-gate #ifdef NO_ERRNO_H
830Sstevel@tonic-gate extern int errno;
840Sstevel@tonic-gate #else
850Sstevel@tonic-gate #include <errno.h>
860Sstevel@tonic-gate #endif
870Sstevel@tonic-gate #ifdef STDC
880Sstevel@tonic-gate #include <string.h>
890Sstevel@tonic-gate #include <stdlib.h>
900Sstevel@tonic-gate #endif
910Sstevel@tonic-gate #endif /* __KERNEL__ */
920Sstevel@tonic-gate #endif /* _KERNEL || KERNEL */
930Sstevel@tonic-gate 
940Sstevel@tonic-gate #ifndef local
950Sstevel@tonic-gate #define	local static
960Sstevel@tonic-gate #endif
970Sstevel@tonic-gate /* compile with -Dlocal if your debugger can't find static symbols */
980Sstevel@tonic-gate 
990Sstevel@tonic-gate typedef unsigned char  uch;
1000Sstevel@tonic-gate typedef uch FAR uchf;
1010Sstevel@tonic-gate typedef unsigned short ush;
1020Sstevel@tonic-gate typedef ush FAR ushf;
1030Sstevel@tonic-gate typedef unsigned long  ulg;
1040Sstevel@tonic-gate 
105*3886Sahl static const char *z_errmsg[10]; /* indexed by 2-zlib_error */
1060Sstevel@tonic-gate /* (size given to avoid silly warnings with Visual C++) */
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate #define	ERR_MSG(err) z_errmsg[Z_NEED_DICT-(err)]
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate #define	ERR_RETURN(strm, err) \
1110Sstevel@tonic-gate 	return (strm->msg = ERR_MSG(err), (err))
1120Sstevel@tonic-gate /* To be used only when the state is known to be valid */
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate 	/* common constants */
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate #ifndef DEF_WBITS
1170Sstevel@tonic-gate #define	DEF_WBITS MAX_WBITS
1180Sstevel@tonic-gate #endif
1190Sstevel@tonic-gate /* default windowBits for decompression. MAX_WBITS is for compression only */
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate #if MAX_MEM_LEVEL >= 8
1220Sstevel@tonic-gate #define	DEF_MEM_LEVEL 8
1230Sstevel@tonic-gate #else
1240Sstevel@tonic-gate #define	DEF_MEM_LEVEL  MAX_MEM_LEVEL
1250Sstevel@tonic-gate #endif
1260Sstevel@tonic-gate /* default memLevel */
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate #define	STORED_BLOCK 0
1290Sstevel@tonic-gate #define	STATIC_TREES 1
1300Sstevel@tonic-gate #define	DYN_TREES    2
1310Sstevel@tonic-gate /* The three kinds of block type */
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate #define	MIN_MATCH  3
1340Sstevel@tonic-gate #define	MAX_MATCH  258
1350Sstevel@tonic-gate /* The minimum and maximum match lengths */
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate #define	PRESET_DICT 0x20 /* preset dictionary flag in zlib header */
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate 	/* target dependencies */
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate #ifdef MSDOS
1420Sstevel@tonic-gate #define	OS_CODE  0x00
1430Sstevel@tonic-gate #ifdef __TURBOC__
1440Sstevel@tonic-gate #include <alloc.h>
1450Sstevel@tonic-gate #else /* MSC or DJGPP */
1460Sstevel@tonic-gate #include <malloc.h>
1470Sstevel@tonic-gate #endif
1480Sstevel@tonic-gate #endif
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate #ifdef OS2
1510Sstevel@tonic-gate #define	OS_CODE  0x06
1520Sstevel@tonic-gate #endif
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate #ifdef WIN32 /* Window 95 & Windows NT */
1550Sstevel@tonic-gate #define	OS_CODE  0x0b
1560Sstevel@tonic-gate #endif
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate #if defined(VAXC) || defined(VMS)
1590Sstevel@tonic-gate #define	OS_CODE  0x02
1600Sstevel@tonic-gate #define	F_OPEN(name, mode) \
1610Sstevel@tonic-gate 	fopen((name), (mode), "mbc=60", "ctx=stm", "rfm=fix", "mrs=512")
1620Sstevel@tonic-gate #endif
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate #ifdef AMIGA
1650Sstevel@tonic-gate #define	OS_CODE  0x01
1660Sstevel@tonic-gate #endif
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate #if defined(ATARI) || defined(atarist)
1690Sstevel@tonic-gate #define	OS_CODE  0x05
1700Sstevel@tonic-gate #endif
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate #ifdef MACOS
1730Sstevel@tonic-gate #define	OS_CODE  0x07
1740Sstevel@tonic-gate #endif
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate #ifdef __50SERIES /* Prime/PRIMOS */
1770Sstevel@tonic-gate #define	OS_CODE  0x0F
1780Sstevel@tonic-gate #endif
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate #ifdef TOPS20
1810Sstevel@tonic-gate #define	OS_CODE  0x0a
1820Sstevel@tonic-gate #endif
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate #if defined(_BEOS_) || defined(RISCOS)
1850Sstevel@tonic-gate #define	fdopen(fd, mode) NULL /* No fdopen() */
1860Sstevel@tonic-gate #endif
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate 	/* Common defaults */
1890Sstevel@tonic-gate 
1900Sstevel@tonic-gate #ifndef OS_CODE
1910Sstevel@tonic-gate #define	OS_CODE  0x03  /* assume Unix */
1920Sstevel@tonic-gate #endif
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate #ifndef F_OPEN
1950Sstevel@tonic-gate #define	F_OPEN(name, mode) fopen((name), (mode))
1960Sstevel@tonic-gate #endif
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate 	/* functions */
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate #ifdef HAVE_STRERROR
2010Sstevel@tonic-gate extern char *strerror OF((int));
2020Sstevel@tonic-gate #define	zstrerror(errnum) strerror(errnum)
2030Sstevel@tonic-gate #else
2040Sstevel@tonic-gate #define	zstrerror(errnum) ""
2050Sstevel@tonic-gate #endif
2060Sstevel@tonic-gate 
2070Sstevel@tonic-gate #if defined(pyr)
2080Sstevel@tonic-gate #define	NO_MEMCPY
2090Sstevel@tonic-gate #endif
2100Sstevel@tonic-gate #if (defined(M_I86SM) || defined(M_I86MM)) && !defined(_MSC_VER)
2110Sstevel@tonic-gate /*
2120Sstevel@tonic-gate  * Use our own functions for small and medium model with MSC <= 5.0.
2130Sstevel@tonic-gate  * You may have to use the same strategy for Borland C (untested).
2140Sstevel@tonic-gate  */
2150Sstevel@tonic-gate #define	NO_MEMCPY
2160Sstevel@tonic-gate #endif
2170Sstevel@tonic-gate #if defined(STDC) && !defined(HAVE_MEMCPY) && !defined(NO_MEMCPY)
2180Sstevel@tonic-gate #define	HAVE_MEMCPY
2190Sstevel@tonic-gate #endif
2200Sstevel@tonic-gate #ifdef HAVE_MEMCPY
2210Sstevel@tonic-gate #ifdef SMALL_MEDIUM /* MSDOS small or medium model */
2220Sstevel@tonic-gate #define	zmemcpy _fmemcpy
2230Sstevel@tonic-gate #define	zmemcmp _fmemcmp
2240Sstevel@tonic-gate #define	zmemzero(dest, len) _fmemset(dest, 0, len)
2250Sstevel@tonic-gate #else
2260Sstevel@tonic-gate #define	zmemcpy (void) memcpy
2270Sstevel@tonic-gate #define	zmemcmp memcmp
2280Sstevel@tonic-gate #define	zmemzero(dest, len) (void) memset(dest, 0, len)
2290Sstevel@tonic-gate #endif
2300Sstevel@tonic-gate #else
2310Sstevel@tonic-gate extern void zmemcpy  OF((Bytef* dest, const Bytef* source, uInt len));
2320Sstevel@tonic-gate extern int  zmemcmp  OF((const Bytef* s1, const Bytef* s2, uInt len));
2330Sstevel@tonic-gate extern void zmemzero OF((Bytef* dest, uInt len));
2340Sstevel@tonic-gate #endif
2350Sstevel@tonic-gate 
2360Sstevel@tonic-gate /* Diagnostic functions */
2370Sstevel@tonic-gate #ifdef DEBUG_ZLIB
2380Sstevel@tonic-gate #include <stdio.h>
2390Sstevel@tonic-gate #ifndef verbose
2400Sstevel@tonic-gate #define	verbose 0
2410Sstevel@tonic-gate #endif
2420Sstevel@tonic-gate extern void z_error    OF((char *m));
2430Sstevel@tonic-gate #define	Assert(cond, msg) { if (!(cond)) z_error(msg); }
2440Sstevel@tonic-gate #define	Trace(x) {if (z_verbose >= 0) fprintf x; }
2450Sstevel@tonic-gate #define	Tracev(x) {if (z_verbose > 0) fprintf x; }
2460Sstevel@tonic-gate #define	Tracevv(x) {if (z_verbose > 1) fprintf x; }
2470Sstevel@tonic-gate #define	Tracec(c, x) {if (z_verbose > 0 && (c)) fprintf x; }
2480Sstevel@tonic-gate #define	Tracecv(c, x) {if (z_verbose > 1 && (c)) fprintf x; }
2490Sstevel@tonic-gate #else
2500Sstevel@tonic-gate #if defined(SOL2) && defined(DEBUG)
2510Sstevel@tonic-gate #define	Assert(cond, msg)	((cond) ? ((void)0) : panic(msg))
2520Sstevel@tonic-gate #else
2530Sstevel@tonic-gate #define	Assert(cond, msg)	((void)0)
2540Sstevel@tonic-gate #endif
2550Sstevel@tonic-gate #define	Trace(x)	((void)0)
2560Sstevel@tonic-gate #define	Tracev(x)	((void)0)
2570Sstevel@tonic-gate #define	Tracevv(x)	((void)0)
2580Sstevel@tonic-gate #define	Tracec(c, x)	((void)0)
2590Sstevel@tonic-gate #define	Tracecv(c, x)	((void)0)
2600Sstevel@tonic-gate #endif
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate 
2630Sstevel@tonic-gate typedef uLong (*check_func) OF((uLong check, const Bytef *buf, uInt len));
2640Sstevel@tonic-gate 
2650Sstevel@tonic-gate /* voidpf zcalloc OF((voidpf opaque, unsigned items, unsigned size)); */
2660Sstevel@tonic-gate /* void   zcfree  OF((voidpf opaque, voidpf ptr)); */
2670Sstevel@tonic-gate 
2680Sstevel@tonic-gate #define	ZALLOC(strm, items, size) \
2690Sstevel@tonic-gate 	(*((strm)->zalloc))((strm)->opaque, (items), (size))
2700Sstevel@tonic-gate #define	ZFREE(strm, addr)  (*((strm)->zfree))((strm)->opaque, (voidpf)(addr))
2710Sstevel@tonic-gate #define	TRY_FREE(s, p) {if (p) ZFREE(s, p); }
2720Sstevel@tonic-gate 
2730Sstevel@tonic-gate #endif /* _Z_UTIL_H */
2740Sstevel@tonic-gate /* --- zutil.h */
2750Sstevel@tonic-gate 
2760Sstevel@tonic-gate /* +++ deflate.h */
2770Sstevel@tonic-gate /*
2780Sstevel@tonic-gate  * deflate.h -- internal compression state
2790Sstevel@tonic-gate  * Copyright (C) 1995-1998 Jean-loup Gailly
2800Sstevel@tonic-gate  * For conditions of distribution and use, see copyright notice in zlib.h
2810Sstevel@tonic-gate  */
2820Sstevel@tonic-gate 
2830Sstevel@tonic-gate /*
2840Sstevel@tonic-gate  * WARNING: this file should *not* be used by applications. It is part
2850Sstevel@tonic-gate  * of the implementation of the compression library and is subject to
2860Sstevel@tonic-gate  * change. Applications should only use zlib.h.
2870Sstevel@tonic-gate  */
2880Sstevel@tonic-gate 
2890Sstevel@tonic-gate /* From: deflate.h,v 1.10 1996/07/02 12:41:00 me Exp $ */
2900Sstevel@tonic-gate 
2910Sstevel@tonic-gate #ifndef _DEFLATE_H
2920Sstevel@tonic-gate #define	_DEFLATE_H
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate /* #include "zutil.h" */
2950Sstevel@tonic-gate 
2960Sstevel@tonic-gate /*
2970Sstevel@tonic-gate  * ===========================================================================
2980Sstevel@tonic-gate  * Internal compression state.
2990Sstevel@tonic-gate  */
3000Sstevel@tonic-gate 
3010Sstevel@tonic-gate #define	LENGTH_CODES 29
3020Sstevel@tonic-gate /* number of length codes, not counting the special END_BLOCK code */
3030Sstevel@tonic-gate 
3040Sstevel@tonic-gate #define	LITERALS  256
3050Sstevel@tonic-gate /* number of literal bytes 0..255 */
3060Sstevel@tonic-gate 
3070Sstevel@tonic-gate #define	L_CODES (LITERALS+1+LENGTH_CODES)
3080Sstevel@tonic-gate /* number of Literal or Length codes, including the END_BLOCK code */
3090Sstevel@tonic-gate 
3100Sstevel@tonic-gate #define	D_CODES   30
3110Sstevel@tonic-gate /* number of distance codes */
3120Sstevel@tonic-gate 
3130Sstevel@tonic-gate #define	BL_CODES  19
3140Sstevel@tonic-gate /* number of codes used to transfer the bit lengths */
3150Sstevel@tonic-gate 
3160Sstevel@tonic-gate #define	HEAP_SIZE (2*L_CODES+1)
3170Sstevel@tonic-gate /* maximum heap size */
3180Sstevel@tonic-gate 
3190Sstevel@tonic-gate #define	MAX_BITS 15
3200Sstevel@tonic-gate /* All codes must not exceed MAX_BITS bits */
3210Sstevel@tonic-gate 
3220Sstevel@tonic-gate #define	INIT_STATE    42
3230Sstevel@tonic-gate #define	BUSY_STATE   113
3240Sstevel@tonic-gate #define	FINISH_STATE 666
3250Sstevel@tonic-gate /* Stream status */
3260Sstevel@tonic-gate 
3270Sstevel@tonic-gate 
3280Sstevel@tonic-gate /* Data structure describing a single value and its code string. */
3290Sstevel@tonic-gate typedef struct ct_data_s {
3300Sstevel@tonic-gate 	union {
3310Sstevel@tonic-gate 		ush freq;	/* frequency count */
3320Sstevel@tonic-gate 		ush code;	/* bit string */
3330Sstevel@tonic-gate 	} fc;
3340Sstevel@tonic-gate 	union {
3350Sstevel@tonic-gate 		ush dad;	/* father node in Huffman tree */
3360Sstevel@tonic-gate 		ush len;	/* length of bit string */
3370Sstevel@tonic-gate 	} dl;
3380Sstevel@tonic-gate } FAR ct_data;
3390Sstevel@tonic-gate 
3400Sstevel@tonic-gate #define	Freq fc.freq
3410Sstevel@tonic-gate #define	Code fc.code
3420Sstevel@tonic-gate #define	Dad  dl.dad
3430Sstevel@tonic-gate #define	Len  dl.len
3440Sstevel@tonic-gate 
3450Sstevel@tonic-gate typedef struct static_tree_desc_s  static_tree_desc;
3460Sstevel@tonic-gate 
3470Sstevel@tonic-gate typedef struct tree_desc_s {
3480Sstevel@tonic-gate 	ct_data *dyn_tree;	/* the dynamic tree */
3490Sstevel@tonic-gate 	int	max_code;	/* largest code with non zero frequency */
3500Sstevel@tonic-gate 	static_tree_desc *stat_desc;	/* the corresponding static tree */
3510Sstevel@tonic-gate } FAR tree_desc;
3520Sstevel@tonic-gate 
3530Sstevel@tonic-gate typedef ush Pos;
3540Sstevel@tonic-gate typedef Pos FAR Posf;
3550Sstevel@tonic-gate typedef unsigned IPos;
3560Sstevel@tonic-gate 
3570Sstevel@tonic-gate /*
3580Sstevel@tonic-gate  * A Pos is an index in the character window. We use short instead of
3590Sstevel@tonic-gate  * int to save space in the various tables. IPos is used only for
3600Sstevel@tonic-gate  * parameter passing.
3610Sstevel@tonic-gate  */
3620Sstevel@tonic-gate 
3630Sstevel@tonic-gate typedef struct deflate_state {
3640Sstevel@tonic-gate 	z_streamp strm;	/* pointer back to this zlib stream */
3650Sstevel@tonic-gate 	int   status;	/* as the name implies */
3660Sstevel@tonic-gate 	Bytef *pending_buf;	/* output still pending */
3670Sstevel@tonic-gate 	ulg   pending_buf_size;	/* size of pending_buf */
3680Sstevel@tonic-gate 	Bytef *pending_out;	/* next pending byte to output to the stream */
3690Sstevel@tonic-gate 	int   pending;	/* nb of bytes in the pending buffer */
3700Sstevel@tonic-gate 	int   noheader;	/* suppress zlib header and adler32 */
3710Sstevel@tonic-gate 	Byte  data_type;	/* UNKNOWN, BINARY or ASCII */
3720Sstevel@tonic-gate 	Byte  method;	/* STORED (for zip only) or DEFLATED */
3730Sstevel@tonic-gate 	/* value of flush param for previous deflate call */
3740Sstevel@tonic-gate 	int   last_flush;
3750Sstevel@tonic-gate 
3760Sstevel@tonic-gate 	/* used by deflate.c: */
3770Sstevel@tonic-gate 
3780Sstevel@tonic-gate 	uInt  w_size;	/* LZ77 window size (32K by default) */
3790Sstevel@tonic-gate 	uInt  w_bits;	/* log2(w_size)  (8..16) */
3800Sstevel@tonic-gate 	uInt  w_mask;	/* w_size - 1 */
3810Sstevel@tonic-gate 
3820Sstevel@tonic-gate 	Bytef *window;
3830Sstevel@tonic-gate 	/*
3840Sstevel@tonic-gate 	 * Sliding window. Input bytes are read into the second half
3850Sstevel@tonic-gate 	 * of the window, and move to the first half later to keep a
3860Sstevel@tonic-gate 	 * dictionary of at least wSize bytes. With this organization,
3870Sstevel@tonic-gate 	 * matches are limited to a distance of wSize-MAX_MATCH bytes,
3880Sstevel@tonic-gate 	 * but this ensures that IO is always performed with a length
3890Sstevel@tonic-gate 	 * multiple of the block size. Also, it limits the window size
3900Sstevel@tonic-gate 	 * to 64K, which is quite useful on MSDOS.  To do: use the
3910Sstevel@tonic-gate 	 * user input buffer as sliding window.
3920Sstevel@tonic-gate 	 */
3930Sstevel@tonic-gate 
3940Sstevel@tonic-gate 	ulg window_size;
3950Sstevel@tonic-gate 	/*
3960Sstevel@tonic-gate 	 * Actual size of window: 2*wSize, except when the user input
3970Sstevel@tonic-gate 	 * buffer is directly used as sliding window.
3980Sstevel@tonic-gate 	 */
3990Sstevel@tonic-gate 
4000Sstevel@tonic-gate 	Posf *prev;
4010Sstevel@tonic-gate 	/*
4020Sstevel@tonic-gate 	 * Link to older string with same hash index. To limit the
4030Sstevel@tonic-gate 	 * size of this array to 64K, this link is maintained only for
4040Sstevel@tonic-gate 	 * the last 32K strings.  An index in this array is thus a
4050Sstevel@tonic-gate 	 * window index modulo 32K.
4060Sstevel@tonic-gate 	 */
4070Sstevel@tonic-gate 
4080Sstevel@tonic-gate 	Posf *head;	/* Heads of the hash chains or NIL. */
4090Sstevel@tonic-gate 
4100Sstevel@tonic-gate 	uInt  ins_h;	/* hash index of string to be inserted */
4110Sstevel@tonic-gate 	uInt  hash_size;	/* number of elements in hash table */
4120Sstevel@tonic-gate 	uInt  hash_bits;	/* log2(hash_size) */
4130Sstevel@tonic-gate 	uInt  hash_mask;	/* hash_size-1 */
4140Sstevel@tonic-gate 
4150Sstevel@tonic-gate 	uInt  hash_shift;
4160Sstevel@tonic-gate 	/*
4170Sstevel@tonic-gate 	 * Number of bits by which ins_h must be shifted at each input
4180Sstevel@tonic-gate 	 * step. It must be such that after MIN_MATCH steps, the
4190Sstevel@tonic-gate 	 * oldest byte no longer takes part in the hash key, that is:
4200Sstevel@tonic-gate 	 * hash_shift * MIN_MATCH >= hash_bits
4210Sstevel@tonic-gate 	 */
4220Sstevel@tonic-gate 
4230Sstevel@tonic-gate 	long block_start;
4240Sstevel@tonic-gate 	/*
4250Sstevel@tonic-gate 	 * Window position at the beginning of the current output
4260Sstevel@tonic-gate 	 * block. Gets negative when the window is moved backwards.
4270Sstevel@tonic-gate 	 */
4280Sstevel@tonic-gate 
4290Sstevel@tonic-gate 	uInt match_length;	/* length of best match */
4300Sstevel@tonic-gate 	IPos prev_match;	/* previous match */
4310Sstevel@tonic-gate 	int match_available;	/* set if previous match exists */
4320Sstevel@tonic-gate 	uInt strstart;	/* start of string to insert */
4330Sstevel@tonic-gate 	uInt match_start;	/* start of matching string */
4340Sstevel@tonic-gate 	uInt lookahead;	/* number of valid bytes ahead in window */
4350Sstevel@tonic-gate 
4360Sstevel@tonic-gate 	uInt prev_length;
4370Sstevel@tonic-gate 	/*
4380Sstevel@tonic-gate 	 * Length of the best match at previous step. Matches not
4390Sstevel@tonic-gate 	 * greater than this are discarded. This is used in the lazy
4400Sstevel@tonic-gate 	 * match evaluation.
4410Sstevel@tonic-gate 	 */
4420Sstevel@tonic-gate 
4430Sstevel@tonic-gate 	uInt max_chain_length;
4440Sstevel@tonic-gate 	/*
4450Sstevel@tonic-gate 	 * To speed up deflation, hash chains are never searched
4460Sstevel@tonic-gate 	 * beyond *this length.  A higher limit improves compression
4470Sstevel@tonic-gate 	 * ratio but *degrades the speed.
4480Sstevel@tonic-gate 	 */
4490Sstevel@tonic-gate 
4500Sstevel@tonic-gate 	uInt max_lazy_match;
4510Sstevel@tonic-gate 	/*
4520Sstevel@tonic-gate 	 * Attempt to find a better match only when the current match
4530Sstevel@tonic-gate 	 * is strictly smaller than this value. This mechanism is used
4540Sstevel@tonic-gate 	 * only for compression levels >= 4.
4550Sstevel@tonic-gate 	 */
4560Sstevel@tonic-gate #define	max_insert_length  max_lazy_match
4570Sstevel@tonic-gate 	/*
4580Sstevel@tonic-gate 	 * Insert new strings in the hash table only if the match
4590Sstevel@tonic-gate 	 * length is not greater than this length. This saves time but
4600Sstevel@tonic-gate 	 * degrades compression.  max_insert_length is used only for
4610Sstevel@tonic-gate 	 * compression levels <= 3.
4620Sstevel@tonic-gate 	 */
4630Sstevel@tonic-gate 
4640Sstevel@tonic-gate 	int level;	/* compression level (1..9) */
4650Sstevel@tonic-gate 	int strategy;	/* favor or force Huffman coding */
4660Sstevel@tonic-gate 
4670Sstevel@tonic-gate 	uInt good_match;
4680Sstevel@tonic-gate 	/* Use a faster search when the previous match is longer than this */
4690Sstevel@tonic-gate 
4700Sstevel@tonic-gate 	int nice_match;	/* Stop searching when current match exceeds this */
4710Sstevel@tonic-gate 
4720Sstevel@tonic-gate 	/* used by trees.c: */
4730Sstevel@tonic-gate 	/* Didn't use ct_data typedef below to supress compiler warning */
4740Sstevel@tonic-gate 	struct ct_data_s dyn_ltree[HEAP_SIZE];	/* literal and length tree */
4750Sstevel@tonic-gate 	struct ct_data_s dyn_dtree[2*D_CODES+1];	/* distance tree */
4760Sstevel@tonic-gate 	/* Huffman tree for bit lengths */
4770Sstevel@tonic-gate 	struct ct_data_s bl_tree[2*BL_CODES+1];
4780Sstevel@tonic-gate 
4790Sstevel@tonic-gate 	struct tree_desc_s l_desc;	/* desc. for literal tree */
4800Sstevel@tonic-gate 	struct tree_desc_s d_desc;	/* desc. for distance tree */
4810Sstevel@tonic-gate 	struct tree_desc_s bl_desc;	/* desc. for bit length tree */
4820Sstevel@tonic-gate 
4830Sstevel@tonic-gate 	ush bl_count[MAX_BITS+1];
4840Sstevel@tonic-gate 	/* number of codes at each bit length for an optimal tree */
4850Sstevel@tonic-gate 
4860Sstevel@tonic-gate 	int heap[2*L_CODES+1];	/* heap used to build the Huffman trees */
4870Sstevel@tonic-gate 	int heap_len;	/* number of elements in the heap */
4880Sstevel@tonic-gate 	int heap_max;	/* element of largest frequency */
4890Sstevel@tonic-gate 	/*
4900Sstevel@tonic-gate 	 * The sons of heap[n] are heap[2*n] and heap[2*n+1]. heap[0]
4910Sstevel@tonic-gate 	 * is not used.  The same heap array is used to build all
4920Sstevel@tonic-gate 	 * trees.
4930Sstevel@tonic-gate 	 */
4940Sstevel@tonic-gate 
4950Sstevel@tonic-gate 	uch depth[2*L_CODES+1];
4960Sstevel@tonic-gate 	/*
4970Sstevel@tonic-gate 	 * Depth of each subtree used as tie breaker for trees of
4980Sstevel@tonic-gate 	 * equal frequency
4990Sstevel@tonic-gate 	 */
5000Sstevel@tonic-gate 
5010Sstevel@tonic-gate 	uchf *l_buf;	/* buffer for literals or lengths */
5020Sstevel@tonic-gate 
5030Sstevel@tonic-gate 	uInt lit_bufsize;
5040Sstevel@tonic-gate 	/*
5050Sstevel@tonic-gate 	 * Size of match buffer for literals/lengths.  There are 4
5060Sstevel@tonic-gate 	 * reasons for limiting lit_bufsize to 64K:
5070Sstevel@tonic-gate 	 *
5080Sstevel@tonic-gate 	 *   - frequencies can be kept in 16 bit counters
5090Sstevel@tonic-gate 	 *
5100Sstevel@tonic-gate 	 *   - if compression is not successful for the first block,
5110Sstevel@tonic-gate 	 *   all input data is still in the window so we can still
5120Sstevel@tonic-gate 	 *   emit a stored block even when input comes from standard
5130Sstevel@tonic-gate 	 *   input.  (This can also be done for all blocks if
5140Sstevel@tonic-gate 	 *   lit_bufsize is not greater than 32K.)
5150Sstevel@tonic-gate 	 *
5160Sstevel@tonic-gate 	 *   - if compression is not successful for a file smaller
5170Sstevel@tonic-gate 	 *   than 64K, we can even emit a stored file instead of a
5180Sstevel@tonic-gate 	 *   stored block (saving 5 bytes).  This is applicable only
5190Sstevel@tonic-gate 	 *   for zip (not gzip or zlib).
5200Sstevel@tonic-gate 	 *
5210Sstevel@tonic-gate 	 *   - creating new Huffman trees less frequently may not
5220Sstevel@tonic-gate 	 *   provide fast adaptation to changes in the input data
5230Sstevel@tonic-gate 	 *   statistics. (Take for example a binary file with poorly
5240Sstevel@tonic-gate 	 *   compressible code followed by a highly compressible
5250Sstevel@tonic-gate 	 *   string table.) Smaller buffer sizes give fast adaptation
5260Sstevel@tonic-gate 	 *   but have of course the overhead of transmitting trees
5270Sstevel@tonic-gate 	 *   more frequently.
5280Sstevel@tonic-gate 	 *
5290Sstevel@tonic-gate 	 *   - I can't count above 4
5300Sstevel@tonic-gate 	 */
5310Sstevel@tonic-gate 
5320Sstevel@tonic-gate 	uInt last_lit;	/* running index in l_buf */
5330Sstevel@tonic-gate 
5340Sstevel@tonic-gate 	ushf *d_buf;
5350Sstevel@tonic-gate 	/*
5360Sstevel@tonic-gate 	 * Buffer for distances. To simplify the code, d_buf and l_buf
5370Sstevel@tonic-gate 	 * have the same number of elements. To use different lengths,
5380Sstevel@tonic-gate 	 * an extra flag array would be necessary.
5390Sstevel@tonic-gate 	 */
5400Sstevel@tonic-gate 
5410Sstevel@tonic-gate 	ulg opt_len;	/* bit length of current block with optimal trees */
5420Sstevel@tonic-gate 	ulg static_len;	/* bit length of current block with static trees */
5430Sstevel@tonic-gate 	uInt matches;	/* number of string matches in current block */
5440Sstevel@tonic-gate 	int last_eob_len;	/* bit length of EOB code for last block */
5450Sstevel@tonic-gate 
5460Sstevel@tonic-gate 	ulg compressed_len;	/* total bit length of compressed file PPP */
5470Sstevel@tonic-gate #ifdef DEBUG_ZLIB
5480Sstevel@tonic-gate 	ulg bits_sent;	/* bit length of the compressed data */
5490Sstevel@tonic-gate #endif
5500Sstevel@tonic-gate 
5510Sstevel@tonic-gate 	ush bi_buf;
5520Sstevel@tonic-gate 	/*
5530Sstevel@tonic-gate 	 * Output buffer. bits are inserted starting at the bottom
5540Sstevel@tonic-gate 	 * (least significant bits).
5550Sstevel@tonic-gate 	 */
5560Sstevel@tonic-gate 	int bi_valid;
5570Sstevel@tonic-gate 	/*
5580Sstevel@tonic-gate 	 * Number of valid bits in bi_buf.  All bits above the last
5590Sstevel@tonic-gate 	 * valid bit are always zero.
5600Sstevel@tonic-gate 	 */
5610Sstevel@tonic-gate 
5620Sstevel@tonic-gate } FAR deflate_state;
5630Sstevel@tonic-gate 
5640Sstevel@tonic-gate /*
5650Sstevel@tonic-gate  * Output a byte on the stream.  IN assertion: there is enough room in
5660Sstevel@tonic-gate  * pending_buf.
5670Sstevel@tonic-gate  */
5680Sstevel@tonic-gate #define	put_byte(s, c) {s->pending_buf[s->pending++] = (c); }
5690Sstevel@tonic-gate 
5700Sstevel@tonic-gate 
5710Sstevel@tonic-gate #define	MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1)
5720Sstevel@tonic-gate /*
5730Sstevel@tonic-gate  * Minimum amount of lookahead, except at the end of the input file.
5740Sstevel@tonic-gate  * See deflate.c for comments about the MIN_MATCH+1.
5750Sstevel@tonic-gate  */
5760Sstevel@tonic-gate 
5770Sstevel@tonic-gate #define	MAX_DIST(s)  ((s)->w_size-MIN_LOOKAHEAD)
5780Sstevel@tonic-gate /*
5790Sstevel@tonic-gate  * In order to simplify the code, particularly on 16 bit machines,
5800Sstevel@tonic-gate  * match distances are limited to MAX_DIST instead of WSIZE.
5810Sstevel@tonic-gate  */
5820Sstevel@tonic-gate 
5830Sstevel@tonic-gate 	/* in trees.c */
5840Sstevel@tonic-gate void _tr_init		OF((deflate_state *s));
5850Sstevel@tonic-gate int  _tr_tally		OF((deflate_state *s, unsigned dist, unsigned lc));
5860Sstevel@tonic-gate void  _tr_flush_block	OF((deflate_state *s, charf *buf, ulg stored_len,
5870Sstevel@tonic-gate     int eof));
5880Sstevel@tonic-gate void _tr_align		OF((deflate_state *s));
5890Sstevel@tonic-gate void _tr_stored_block	OF((deflate_state *s, charf *buf, ulg stored_len,
5900Sstevel@tonic-gate     int eof));
5910Sstevel@tonic-gate void _tr_stored_type_only OF((deflate_state *));	/* PPP */
5920Sstevel@tonic-gate 
5930Sstevel@tonic-gate #define	d_code(dist) \
5940Sstevel@tonic-gate 	((dist) < 256 ? _dist_code[dist] : _dist_code[256+((dist)>>7)])
5950Sstevel@tonic-gate /*
5960Sstevel@tonic-gate  * Mapping from a distance to a distance code. dist is the distance - 1 and
5970Sstevel@tonic-gate  * must not have side effects. _dist_code[256] and _dist_code[257] are never
5980Sstevel@tonic-gate  * used.
5990Sstevel@tonic-gate  */
6000Sstevel@tonic-gate 
6010Sstevel@tonic-gate #ifndef DEBUG_ZLIB
6020Sstevel@tonic-gate /* Inline versions of _tr_tally for speed: */
6030Sstevel@tonic-gate 
6040Sstevel@tonic-gate local uch _length_code[];
6050Sstevel@tonic-gate local uch _dist_code[];
6060Sstevel@tonic-gate 
6070Sstevel@tonic-gate #define	_tr_tally_lit(s, c, flush) \
6080Sstevel@tonic-gate 	{	uch cc = (c); \
6090Sstevel@tonic-gate 		s->d_buf[s->last_lit] = 0; \
6100Sstevel@tonic-gate 		s->l_buf[s->last_lit++] = cc; \
6110Sstevel@tonic-gate 		s->dyn_ltree[cc].Freq++; \
6120Sstevel@tonic-gate 		flush = (s->last_lit == s->lit_bufsize-1); \
6130Sstevel@tonic-gate 	}
6140Sstevel@tonic-gate #define	_tr_tally_dist(s, distance, length, flush) \
6150Sstevel@tonic-gate 	{	uch len = (length); \
6160Sstevel@tonic-gate 		ush dist = (distance); \
6170Sstevel@tonic-gate 		s->d_buf[s->last_lit] = dist; \
6180Sstevel@tonic-gate 		s->l_buf[s->last_lit++] = len; \
6190Sstevel@tonic-gate 		dist--; \
6200Sstevel@tonic-gate 		s->dyn_ltree[_length_code[len]+LITERALS+1].Freq++; \
6210Sstevel@tonic-gate 		s->dyn_dtree[d_code(dist)].Freq++; \
6220Sstevel@tonic-gate 		flush = (s->last_lit == s->lit_bufsize-1); \
6230Sstevel@tonic-gate 	}
6240Sstevel@tonic-gate #else
6250Sstevel@tonic-gate #define	_tr_tally_lit(s, c, flush) flush = _tr_tally(s, 0, c)
6260Sstevel@tonic-gate #define	_tr_tally_dist(s, distance, length, flush) \
6270Sstevel@tonic-gate 		flush = _tr_tally(s, distance, length)
6280Sstevel@tonic-gate #endif
6290Sstevel@tonic-gate 
6300Sstevel@tonic-gate #endif
6310Sstevel@tonic-gate /* --- deflate.h */
6320Sstevel@tonic-gate 
6330Sstevel@tonic-gate /* +++ deflate.c */
6340Sstevel@tonic-gate /*
6350Sstevel@tonic-gate  * deflate.c -- compress data using the deflation algorithm
6360Sstevel@tonic-gate  * Copyright (C) 1995-1998 Jean-loup Gailly.
6370Sstevel@tonic-gate  * For conditions of distribution and use, see copyright notice in zlib.h
6380Sstevel@tonic-gate  */
6390Sstevel@tonic-gate 
6400Sstevel@tonic-gate /*
6410Sstevel@tonic-gate  *  ALGORITHM
6420Sstevel@tonic-gate  *
6430Sstevel@tonic-gate  *      The "deflation" process depends on being able to identify portions
6440Sstevel@tonic-gate  *      of the input text which are identical to earlier input (within a
6450Sstevel@tonic-gate  *      sliding window trailing behind the input currently being processed).
6460Sstevel@tonic-gate  *
6470Sstevel@tonic-gate  *      The most straightforward technique turns out to be the fastest for
6480Sstevel@tonic-gate  *      most input files: try all possible matches and select the longest.
6490Sstevel@tonic-gate  *      The key feature of this algorithm is that insertions into the string
6500Sstevel@tonic-gate  *      dictionary are very simple and thus fast, and deletions are avoided
6510Sstevel@tonic-gate  *      completely. Insertions are performed at each input character, whereas
6520Sstevel@tonic-gate  *      string matches are performed only when the previous match ends. So it
6530Sstevel@tonic-gate  *      is preferable to spend more time in matches to allow very fast string
6540Sstevel@tonic-gate  *      insertions and avoid deletions. The matching algorithm for small
6550Sstevel@tonic-gate  *      strings is inspired from that of Rabin & Karp. A brute force approach
6560Sstevel@tonic-gate  *      is used to find longer strings when a small match has been found.
6570Sstevel@tonic-gate  *      A similar algorithm is used in comic (by Jan-Mark Wams) and freeze
6580Sstevel@tonic-gate  *      (by Leonid Broukhis).
6590Sstevel@tonic-gate  *         A previous version of this file used a more sophisticated algorithm
6600Sstevel@tonic-gate  *      (by Fiala and Greene) which is guaranteed to run in linear amortized
6610Sstevel@tonic-gate  *      time, but has a larger average cost, uses more memory and is patented.
6620Sstevel@tonic-gate  *      However the F&G algorithm may be faster for some highly redundant
6630Sstevel@tonic-gate  *      files if the parameter max_chain_length (described below) is too large.
6640Sstevel@tonic-gate  *
6650Sstevel@tonic-gate  *  ACKNOWLEDGEMENTS
6660Sstevel@tonic-gate  *
6670Sstevel@tonic-gate  *      The idea of lazy evaluation of matches is due to Jan-Mark Wams, and
6680Sstevel@tonic-gate  *      I found it in 'freeze' written by Leonid Broukhis.
6690Sstevel@tonic-gate  *      Thanks to many people for bug reports and testing.
6700Sstevel@tonic-gate  *
6710Sstevel@tonic-gate  *  REFERENCES
6720Sstevel@tonic-gate  *
6730Sstevel@tonic-gate  *      Deutsch, L.P.,"DEFLATE Compressed Data Format Specification".
6740Sstevel@tonic-gate  *      Available in ftp://ds.internic.net/rfc/rfc1951.txt
6750Sstevel@tonic-gate  *
6760Sstevel@tonic-gate  *      A description of the Rabin and Karp algorithm is given in the book
6770Sstevel@tonic-gate  *         "Algorithms" by R. Sedgewick, Addison-Wesley, p252.
6780Sstevel@tonic-gate  *
6790Sstevel@tonic-gate  *      Fiala,E.R., and Greene,D.H.
6800Sstevel@tonic-gate  *         Data Compression with Finite Windows, Comm.ACM, 32,4 (1989) 490-595
6810Sstevel@tonic-gate  *
6820Sstevel@tonic-gate  */
6830Sstevel@tonic-gate 
6840Sstevel@tonic-gate /* From: deflate.c,v 1.15 1996/07/24 13:40:58 me Exp $ */
6850Sstevel@tonic-gate 
6860Sstevel@tonic-gate /* #include "deflate.h" */
6870Sstevel@tonic-gate 
6880Sstevel@tonic-gate const char deflate_copyright[] =
6890Sstevel@tonic-gate " deflate 1.1.3 Copyright 1995-1998 Jean-loup Gailly ";
6900Sstevel@tonic-gate /*
6910Sstevel@tonic-gate  * If you use the zlib library in a product, an acknowledgment is
6920Sstevel@tonic-gate  * welcome in the documentation of your product. If for some reason
6930Sstevel@tonic-gate  * you cannot include such an acknowledgment, I would appreciate that
6940Sstevel@tonic-gate  * you keep this copyright string in the executable of your product.
6950Sstevel@tonic-gate  */
6960Sstevel@tonic-gate 
6970Sstevel@tonic-gate /*
6980Sstevel@tonic-gate  * ===========================================================================
6990Sstevel@tonic-gate  *  Function prototypes.
7000Sstevel@tonic-gate  */
7010Sstevel@tonic-gate typedef enum {
7020Sstevel@tonic-gate 	/* block not completed, need more input or more output */
7030Sstevel@tonic-gate 	need_more,
7040Sstevel@tonic-gate 	block_done,	/* block flush performed */
7050Sstevel@tonic-gate 	/* finish started, need only more output at next deflate */
7060Sstevel@tonic-gate 	finish_started,
7070Sstevel@tonic-gate 	finish_done	/* finish done, accept no more input or output */
7080Sstevel@tonic-gate } block_state;
7090Sstevel@tonic-gate 
7100Sstevel@tonic-gate typedef block_state (*compress_func) OF((deflate_state *s, int flush));
7110Sstevel@tonic-gate /* Compression function. Returns the block state after the call. */
7120Sstevel@tonic-gate 
7130Sstevel@tonic-gate local void fill_window	OF((deflate_state *s));
7140Sstevel@tonic-gate local block_state deflate_stored OF((deflate_state *s, int flush));
7150Sstevel@tonic-gate local block_state deflate_fast	OF((deflate_state *s, int flush));
7160Sstevel@tonic-gate local block_state deflate_slow	OF((deflate_state *s, int flush));
7170Sstevel@tonic-gate local void lm_init	OF((deflate_state *s));
7180Sstevel@tonic-gate local void putShortMSB	OF((deflate_state *s, uInt b));
7190Sstevel@tonic-gate local void flush_pending	OF((z_streamp strm));
7200Sstevel@tonic-gate local int read_buf	OF((z_streamp strm, Bytef *buf, unsigned size));
7210Sstevel@tonic-gate #ifdef ASMV
7220Sstevel@tonic-gate void match_init	OF((void));	/* asm code initialization */
7230Sstevel@tonic-gate uInt longest_match	OF((deflate_state *s, IPos cur_match));
7240Sstevel@tonic-gate #else
7250Sstevel@tonic-gate local uInt longest_match	OF((deflate_state *s, IPos cur_match));
7260Sstevel@tonic-gate #endif
7270Sstevel@tonic-gate 
7280Sstevel@tonic-gate #ifdef DEBUG_ZLIB
7290Sstevel@tonic-gate local void check_match OF((deflate_state *s, IPos start, IPos match,
7300Sstevel@tonic-gate     int length));
7310Sstevel@tonic-gate #endif
7320Sstevel@tonic-gate 
7330Sstevel@tonic-gate /*
7340Sstevel@tonic-gate  * ===========================================================================
7350Sstevel@tonic-gate  * Local data
7360Sstevel@tonic-gate  */
7370Sstevel@tonic-gate 
7380Sstevel@tonic-gate #define	NIL 0
7390Sstevel@tonic-gate /* Tail of hash chains */
7400Sstevel@tonic-gate 
7410Sstevel@tonic-gate #ifndef TOO_FAR
7420Sstevel@tonic-gate #define	TOO_FAR 4096
7430Sstevel@tonic-gate #endif
7440Sstevel@tonic-gate /* Matches of length 3 are discarded if their distance exceeds TOO_FAR */
7450Sstevel@tonic-gate 
7460Sstevel@tonic-gate #define	MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1)
7470Sstevel@tonic-gate /*
7480Sstevel@tonic-gate  * Minimum amount of lookahead, except at the end of the input file.
7490Sstevel@tonic-gate  * See deflate.c for comments about the MIN_MATCH+1.
7500Sstevel@tonic-gate  */
7510Sstevel@tonic-gate 
7520Sstevel@tonic-gate /*
7530Sstevel@tonic-gate  * Values for max_lazy_match, good_match and max_chain_length,
7540Sstevel@tonic-gate  * depending on the desired pack level (0..9). The values given below
7550Sstevel@tonic-gate  * have been tuned to exclude worst case performance for pathological
7560Sstevel@tonic-gate  * files. Better values may be found for specific files.
7570Sstevel@tonic-gate  */
7580Sstevel@tonic-gate typedef struct config_s {
7590Sstevel@tonic-gate 	ush good_length;	/* reduce lazy search above this match length */
7600Sstevel@tonic-gate 	ush max_lazy;	/* do not perform lazy search above this match length */
7610Sstevel@tonic-gate 	ush nice_length;	/* quit search above this match length */
7620Sstevel@tonic-gate 	ush max_chain;
7630Sstevel@tonic-gate 	compress_func func;
7640Sstevel@tonic-gate } config;
7650Sstevel@tonic-gate 
7660Sstevel@tonic-gate local const config configuration_table[10] = {
7670Sstevel@tonic-gate /*	good lazy nice chain */
7680Sstevel@tonic-gate /* 0 */ {0,    0,  0,    0, deflate_stored},  /* store only */
7690Sstevel@tonic-gate /* 1 */ {4,    4,  8,    4, deflate_fast}, /* maximum speed, no lazy matches */
7700Sstevel@tonic-gate /* 2 */ {4,    5, 16,    8, deflate_fast},
7710Sstevel@tonic-gate /* 3 */ {4,    6, 32,   32, deflate_fast},
7720Sstevel@tonic-gate 
7730Sstevel@tonic-gate /* 4 */ {4,    4, 16,   16, deflate_slow},  /* lazy matches */
7740Sstevel@tonic-gate /* 5 */ {8,   16, 32,   32, deflate_slow},
7750Sstevel@tonic-gate /* 6 */ {8,   16, 128, 128, deflate_slow},
7760Sstevel@tonic-gate /* 7 */ {8,   32, 128, 256, deflate_slow},
7770Sstevel@tonic-gate /* 8 */ {32, 128, 258, 1024, deflate_slow},
7780Sstevel@tonic-gate /* 9 */ {32, 258, 258, 4096, deflate_slow}};	/* maximum compression */
7790Sstevel@tonic-gate 
7800Sstevel@tonic-gate /*
7810Sstevel@tonic-gate  * Note: the deflate() code requires max_lazy >= MIN_MATCH and max_chain >= 4
7820Sstevel@tonic-gate  * For deflate_fast() (levels <= 3) good is ignored and lazy has a different
7830Sstevel@tonic-gate  * meaning.
7840Sstevel@tonic-gate  */
7850Sstevel@tonic-gate 
7860Sstevel@tonic-gate #define	EQUAL 0
7870Sstevel@tonic-gate /* result of memcmp for equal strings */
7880Sstevel@tonic-gate 
7890Sstevel@tonic-gate #ifndef NO_DUMMY_DECL
7900Sstevel@tonic-gate struct static_tree_desc_s {int dummy; };	/* for buggy compilers */
7910Sstevel@tonic-gate #endif
7920Sstevel@tonic-gate 
7930Sstevel@tonic-gate /*
7940Sstevel@tonic-gate  * ===========================================================================
7950Sstevel@tonic-gate  * Update a hash value with the given input byte
7960Sstevel@tonic-gate  * IN  assertion: all calls to to UPDATE_HASH are made with consecutive
7970Sstevel@tonic-gate  *    input characters, so that a running hash key can be computed from the
7980Sstevel@tonic-gate  *    previous key instead of complete recalculation each time.
7990Sstevel@tonic-gate  */
8000Sstevel@tonic-gate #define	UPDATE_HASH(s, h, c) (h = (((h)<<s->hash_shift) ^ (c)) & s->hash_mask)
8010Sstevel@tonic-gate 
8020Sstevel@tonic-gate 
8030Sstevel@tonic-gate /*
8040Sstevel@tonic-gate  * ===========================================================================
8050Sstevel@tonic-gate  * Insert string str in the dictionary and set match_head to the previous head
8060Sstevel@tonic-gate  * of the hash chain (the most recent string with same hash key). Return
8070Sstevel@tonic-gate  * the previous length of the hash chain.
8080Sstevel@tonic-gate  * If this file is compiled with -DFASTEST, the compression level is forced
8090Sstevel@tonic-gate  * to 1, and no hash chains are maintained.
8100Sstevel@tonic-gate  * IN  assertion: all calls to to INSERT_STRING are made with consecutive
8110Sstevel@tonic-gate  *    input characters and the first MIN_MATCH bytes of str are valid
8120Sstevel@tonic-gate  *    (except for the last MIN_MATCH-1 bytes of the input file).
8130Sstevel@tonic-gate  */
8140Sstevel@tonic-gate #ifdef FASTEST
8150Sstevel@tonic-gate #define	INSERT_STRING(s, str, match_head) \
8160Sstevel@tonic-gate 	(UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \
8170Sstevel@tonic-gate 	match_head = s->head[s->ins_h], \
8180Sstevel@tonic-gate 	s->head[s->ins_h] = (Pos)(str))
8190Sstevel@tonic-gate #else
8200Sstevel@tonic-gate #define	INSERT_STRING(s, str, match_head) \
8210Sstevel@tonic-gate 	(UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \
8220Sstevel@tonic-gate 	s->prev[(str) & s->w_mask] = match_head = s->head[s->ins_h], \
8230Sstevel@tonic-gate 	s->head[s->ins_h] = (Pos)(str))
8240Sstevel@tonic-gate #endif
8250Sstevel@tonic-gate 
8260Sstevel@tonic-gate /*
8270Sstevel@tonic-gate  * ===========================================================================
8280Sstevel@tonic-gate  * Initialize the hash table (avoiding 64K overflow for 16 bit systems).
8290Sstevel@tonic-gate  * prev[] will be initialized on the fly.
8300Sstevel@tonic-gate  */
8310Sstevel@tonic-gate #define	CLEAR_HASH(s) \
8320Sstevel@tonic-gate     s->head[s->hash_size-1] = NIL; \
8330Sstevel@tonic-gate     zmemzero((Bytef *)s->head, (unsigned)(s->hash_size-1)*sizeof (*s->head));
8340Sstevel@tonic-gate 
8350Sstevel@tonic-gate /* ========================================================================= */
8360Sstevel@tonic-gate int
deflateInit_(strm,level,version,stream_size)8370Sstevel@tonic-gate deflateInit_(strm, level, version, stream_size)
8380Sstevel@tonic-gate     z_streamp strm;
8390Sstevel@tonic-gate     int level;
8400Sstevel@tonic-gate     const char *version;
8410Sstevel@tonic-gate     int stream_size;
8420Sstevel@tonic-gate {
8430Sstevel@tonic-gate 	(void) deflate_copyright;
8440Sstevel@tonic-gate 	return deflateInit2_(strm, level, Z_DEFLATED, MAX_WBITS, DEF_MEM_LEVEL,
8450Sstevel@tonic-gate 	    Z_DEFAULT_STRATEGY, version, stream_size);
8460Sstevel@tonic-gate 	/* To do: ignore strm->next_in if we use it as window */
8470Sstevel@tonic-gate }
8480Sstevel@tonic-gate 
8490Sstevel@tonic-gate /* ========================================================================= */
deflateInit2_(strm,level,method,windowBits,memLevel,strategy,version,stream_size)8500Sstevel@tonic-gate int deflateInit2_(strm, level, method, windowBits, memLevel, strategy,
8510Sstevel@tonic-gate     version, stream_size)
8520Sstevel@tonic-gate     z_streamp strm;
8530Sstevel@tonic-gate     int  level;
8540Sstevel@tonic-gate     int  method;
8550Sstevel@tonic-gate     int  windowBits;
8560Sstevel@tonic-gate     int  memLevel;
8570Sstevel@tonic-gate     int  strategy;
8580Sstevel@tonic-gate     const char *version;
8590Sstevel@tonic-gate     int stream_size;
8600Sstevel@tonic-gate {
8610Sstevel@tonic-gate 	deflate_state *s;
8620Sstevel@tonic-gate 	int noheader = 0;
8630Sstevel@tonic-gate 	static const char *my_version = ZLIB_VERSION;
8640Sstevel@tonic-gate 
8650Sstevel@tonic-gate 	ushf *overlay;
8660Sstevel@tonic-gate 	/*
8670Sstevel@tonic-gate 	 * We overlay pending_buf and d_buf+l_buf. This works since
8680Sstevel@tonic-gate 	 * the average output size for (length, distance) codes is <=
8690Sstevel@tonic-gate 	 * 24 bits.
8700Sstevel@tonic-gate 	 */
8710Sstevel@tonic-gate 
8720Sstevel@tonic-gate 	if (version == Z_NULL || version[0] != my_version[0] ||
8730Sstevel@tonic-gate 	    stream_size != sizeof (z_stream)) {
8740Sstevel@tonic-gate 		return (Z_VERSION_ERROR);
8750Sstevel@tonic-gate 	}
8760Sstevel@tonic-gate 	if (strm == Z_NULL)
8770Sstevel@tonic-gate 		return (Z_STREAM_ERROR);
8780Sstevel@tonic-gate 
8790Sstevel@tonic-gate 	strm->msg = Z_NULL;
8800Sstevel@tonic-gate #ifndef NO_ZCFUNCS
8810Sstevel@tonic-gate 	if (strm->zalloc == Z_NULL) {
8820Sstevel@tonic-gate 		strm->zalloc = zcalloc;
8830Sstevel@tonic-gate 		strm->opaque = (voidpf)0;
8840Sstevel@tonic-gate 	}
8850Sstevel@tonic-gate 	if (strm->zfree == Z_NULL) strm->zfree = zcfree;
8860Sstevel@tonic-gate #endif
8870Sstevel@tonic-gate 
8880Sstevel@tonic-gate 	if (level == Z_DEFAULT_COMPRESSION) level = 6;
8890Sstevel@tonic-gate #ifdef FASTEST
8900Sstevel@tonic-gate 	level = 1;
8910Sstevel@tonic-gate #endif
8920Sstevel@tonic-gate 
8930Sstevel@tonic-gate 	if (windowBits < 0) { /* undocumented feature: suppress zlib header */
8940Sstevel@tonic-gate 		noheader = 1;
8950Sstevel@tonic-gate 		windowBits = -windowBits;
8960Sstevel@tonic-gate 	}
8970Sstevel@tonic-gate 	if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method != Z_DEFLATED ||
8980Sstevel@tonic-gate 	    windowBits <= 8 || windowBits > 15 || level < 0 || level > 9 ||
8990Sstevel@tonic-gate 	    strategy < 0 || strategy > Z_HUFFMAN_ONLY) {
9000Sstevel@tonic-gate 		return (Z_STREAM_ERROR);
9010Sstevel@tonic-gate 	}
9020Sstevel@tonic-gate 	s = (deflate_state *) ZALLOC(strm, 1, sizeof (deflate_state));
9030Sstevel@tonic-gate 	if (s == Z_NULL)
9040Sstevel@tonic-gate 		return (Z_MEM_ERROR);
9050Sstevel@tonic-gate 	strm->state = (struct internal_state FAR *)s;
9060Sstevel@tonic-gate 	s->strm = strm;
9070Sstevel@tonic-gate 
9080Sstevel@tonic-gate 	s->noheader = noheader;
9090Sstevel@tonic-gate 	s->w_bits = windowBits;
9100Sstevel@tonic-gate 	s->w_size = 1 << s->w_bits;
9110Sstevel@tonic-gate 	s->w_mask = s->w_size - 1;
9120Sstevel@tonic-gate 
9130Sstevel@tonic-gate 	s->hash_bits = memLevel + 7;
9140Sstevel@tonic-gate 	s->hash_size = 1 << s->hash_bits;
9150Sstevel@tonic-gate 	s->hash_mask = s->hash_size - 1;
9160Sstevel@tonic-gate 	s->hash_shift =  ((s->hash_bits+MIN_MATCH-1)/MIN_MATCH);
9170Sstevel@tonic-gate 
9180Sstevel@tonic-gate 	s->window = (Bytef *) ZALLOC(strm, s->w_size, 2*sizeof (Byte));
9190Sstevel@tonic-gate 	s->prev   = (Posf *)  ZALLOC(strm, s->w_size, sizeof (Pos));
9200Sstevel@tonic-gate 	s->head   = (Posf *)  ZALLOC(strm, s->hash_size, sizeof (Pos));
9210Sstevel@tonic-gate 
9220Sstevel@tonic-gate 	s->lit_bufsize = 1 << (memLevel + 6);	/* 16K elements by default */
9230Sstevel@tonic-gate 
9240Sstevel@tonic-gate 	overlay = (ushf *) ZALLOC(strm, s->lit_bufsize, sizeof (ush)+2);
9250Sstevel@tonic-gate 	s->pending_buf = (uchf *) overlay;
9260Sstevel@tonic-gate 	s->pending_buf_size = (ulg)s->lit_bufsize * (sizeof (ush)+2L);
9270Sstevel@tonic-gate 
9280Sstevel@tonic-gate 	if (s->window == Z_NULL || s->prev == Z_NULL || s->head == Z_NULL ||
9290Sstevel@tonic-gate 	    s->pending_buf == Z_NULL) {
9300Sstevel@tonic-gate 		strm->msg = ERR_MSG(Z_MEM_ERROR);
9310Sstevel@tonic-gate 		s->status = INIT_STATE;
9320Sstevel@tonic-gate 		(void) deflateEnd(strm);
9330Sstevel@tonic-gate 		return (Z_MEM_ERROR);
9340Sstevel@tonic-gate 	}
9350Sstevel@tonic-gate 	s->d_buf = overlay + s->lit_bufsize/sizeof (ush);
9360Sstevel@tonic-gate 	s->l_buf = s->pending_buf + (1+sizeof (ush))*s->lit_bufsize;
9370Sstevel@tonic-gate 
9380Sstevel@tonic-gate 	s->level = level;
9390Sstevel@tonic-gate 	s->strategy = strategy;
9400Sstevel@tonic-gate 	s->method = (Byte)method;
9410Sstevel@tonic-gate 
9420Sstevel@tonic-gate 	return (deflateReset(strm));
9430Sstevel@tonic-gate }
9440Sstevel@tonic-gate 
9450Sstevel@tonic-gate /* ========================================================================= */
9460Sstevel@tonic-gate int
deflateSetDictionary(strm,dictionary,dictLength)9470Sstevel@tonic-gate deflateSetDictionary(strm, dictionary, dictLength)
9480Sstevel@tonic-gate     z_streamp strm;
9490Sstevel@tonic-gate     const Bytef *dictionary;
9500Sstevel@tonic-gate     uInt  dictLength;
9510Sstevel@tonic-gate {
9520Sstevel@tonic-gate 	deflate_state *s;
9530Sstevel@tonic-gate 	uInt length = dictLength;
9540Sstevel@tonic-gate 	uInt n;
9550Sstevel@tonic-gate 	IPos hash_head = 0;
9560Sstevel@tonic-gate 
9570Sstevel@tonic-gate 	if (strm == Z_NULL || strm->state == Z_NULL || dictionary == Z_NULL)
9580Sstevel@tonic-gate 		return (Z_STREAM_ERROR);
9590Sstevel@tonic-gate 
9600Sstevel@tonic-gate 	s = (deflate_state *) strm->state;
9610Sstevel@tonic-gate 	if (s->status != INIT_STATE)
9620Sstevel@tonic-gate 		return (Z_STREAM_ERROR);
9630Sstevel@tonic-gate 
9640Sstevel@tonic-gate 	strm->adler = adler32(strm->adler, dictionary, dictLength);
9650Sstevel@tonic-gate 
9660Sstevel@tonic-gate 	if (length < MIN_MATCH)
9670Sstevel@tonic-gate 		return (Z_OK);
9680Sstevel@tonic-gate 	if (length > MAX_DIST(s)) {
9690Sstevel@tonic-gate 		length = MAX_DIST(s);
9700Sstevel@tonic-gate #ifndef USE_DICT_HEAD
9710Sstevel@tonic-gate 		/* use the tail of the dictionary */
9720Sstevel@tonic-gate 		dictionary += dictLength - length;
9730Sstevel@tonic-gate #endif
9740Sstevel@tonic-gate 	}
9750Sstevel@tonic-gate 	Assert(length <= s->window_size, "dict copy");
9760Sstevel@tonic-gate 	zmemcpy(s->window, dictionary, length);
9770Sstevel@tonic-gate 	s->strstart = length;
9780Sstevel@tonic-gate 	s->block_start = (long)length;
9790Sstevel@tonic-gate 
9800Sstevel@tonic-gate 	/*
9810Sstevel@tonic-gate 	 * Insert all strings in the hash table (except for the last
9820Sstevel@tonic-gate 	 * two bytes).  s->lookahead stays null, so s->ins_h will be
9830Sstevel@tonic-gate 	 * recomputed at the next call of fill_window.
9840Sstevel@tonic-gate 	 */
9850Sstevel@tonic-gate 	s->ins_h = s->window[0];
9860Sstevel@tonic-gate 	UPDATE_HASH(s, s->ins_h, s->window[1]);
9870Sstevel@tonic-gate 	for (n = 0; n <= length - MIN_MATCH; n++) {
9880Sstevel@tonic-gate 		INSERT_STRING(s, n, hash_head);
9890Sstevel@tonic-gate 	}
9900Sstevel@tonic-gate 	if (hash_head) hash_head = 0;	/* to make compiler happy */
9910Sstevel@tonic-gate 	return (Z_OK);
9920Sstevel@tonic-gate }
9930Sstevel@tonic-gate 
9940Sstevel@tonic-gate /* ========================================================================= */
9950Sstevel@tonic-gate int
deflateReset(strm)9960Sstevel@tonic-gate deflateReset(strm)
9970Sstevel@tonic-gate     z_streamp strm;
9980Sstevel@tonic-gate {
9990Sstevel@tonic-gate 	deflate_state *s;
10000Sstevel@tonic-gate 
10010Sstevel@tonic-gate 	if (strm == Z_NULL || strm->state == Z_NULL ||
10020Sstevel@tonic-gate 	    strm->zalloc == Z_NULL || strm->zfree == Z_NULL)
10030Sstevel@tonic-gate 		return (Z_STREAM_ERROR);
10040Sstevel@tonic-gate 
10050Sstevel@tonic-gate 	strm->total_in = strm->total_out = 0;
10060Sstevel@tonic-gate 	/* use zfree if we ever allocate msg dynamically */
10070Sstevel@tonic-gate 	strm->msg = Z_NULL;
10080Sstevel@tonic-gate 	strm->data_type = Z_UNKNOWN;
10090Sstevel@tonic-gate 
10100Sstevel@tonic-gate 	s = (deflate_state *)strm->state;
10110Sstevel@tonic-gate 	s->pending = 0;
10120Sstevel@tonic-gate 	s->pending_out = s->pending_buf;
10130Sstevel@tonic-gate 
10140Sstevel@tonic-gate 	if (s->noheader < 0) {
10150Sstevel@tonic-gate 		/* was set to -1 by deflate(..., Z_FINISH); */
10160Sstevel@tonic-gate 		s->noheader = 0;
10170Sstevel@tonic-gate 	}
10180Sstevel@tonic-gate 	s->status = s->noheader ? BUSY_STATE : INIT_STATE;
10190Sstevel@tonic-gate 	strm->adler = 1;
10200Sstevel@tonic-gate 	s->last_flush = Z_NO_FLUSH;
10210Sstevel@tonic-gate 
10220Sstevel@tonic-gate 	_tr_init(s);
10230Sstevel@tonic-gate 	lm_init(s);
10240Sstevel@tonic-gate 
10250Sstevel@tonic-gate 	return (Z_OK);
10260Sstevel@tonic-gate }
10270Sstevel@tonic-gate 
10280Sstevel@tonic-gate /* ========================================================================= */
10290Sstevel@tonic-gate int
deflateParams(strm,level,strategy)10300Sstevel@tonic-gate deflateParams(strm, level, strategy)
10310Sstevel@tonic-gate     z_streamp strm;
10320Sstevel@tonic-gate     int level;
10330Sstevel@tonic-gate     int strategy;
10340Sstevel@tonic-gate {
10350Sstevel@tonic-gate 	deflate_state *s;
10360Sstevel@tonic-gate 	compress_func func;
10370Sstevel@tonic-gate 	int err = Z_OK;
10380Sstevel@tonic-gate 
10390Sstevel@tonic-gate 	if (strm == Z_NULL || strm->state == Z_NULL)
10400Sstevel@tonic-gate 		return (Z_STREAM_ERROR);
10410Sstevel@tonic-gate 	s = (deflate_state *) strm->state;
10420Sstevel@tonic-gate 
10430Sstevel@tonic-gate 	if (level == Z_DEFAULT_COMPRESSION) {
10440Sstevel@tonic-gate 		level = 6;
10450Sstevel@tonic-gate 	}
10460Sstevel@tonic-gate 	if (level < 0 || level > 9 || strategy < 0 ||
10470Sstevel@tonic-gate 	    strategy > Z_HUFFMAN_ONLY) {
10480Sstevel@tonic-gate 		return (Z_STREAM_ERROR);
10490Sstevel@tonic-gate 	}
10500Sstevel@tonic-gate 	func = configuration_table[s->level].func;
10510Sstevel@tonic-gate 
10520Sstevel@tonic-gate 	if (func != configuration_table[level].func && strm->total_in != 0) {
10530Sstevel@tonic-gate 		/* Flush the last buffer: */
10540Sstevel@tonic-gate 		err = deflate(strm, Z_PARTIAL_FLUSH);
10550Sstevel@tonic-gate 	}
10560Sstevel@tonic-gate 	if (s->level != level) {
10570Sstevel@tonic-gate 		s->level = level;
10580Sstevel@tonic-gate 		s->max_lazy_match   = configuration_table[level].max_lazy;
10590Sstevel@tonic-gate 		s->good_match	= configuration_table[level].good_length;
10600Sstevel@tonic-gate 		s->nice_match	= configuration_table[level].nice_length;
10610Sstevel@tonic-gate 		s->max_chain_length = configuration_table[level].max_chain;
10620Sstevel@tonic-gate 	}
10630Sstevel@tonic-gate 	s->strategy = strategy;
10640Sstevel@tonic-gate 	return (err);
10650Sstevel@tonic-gate }
10660Sstevel@tonic-gate 
10670Sstevel@tonic-gate /*
10680Sstevel@tonic-gate  * =========================================================================
10690Sstevel@tonic-gate  * Put a short in the pending buffer. The 16-bit value is put in MSB order.
10700Sstevel@tonic-gate  * IN assertion: the stream state is correct and there is enough room in
10710Sstevel@tonic-gate  * pending_buf.
10720Sstevel@tonic-gate  */
10730Sstevel@tonic-gate local void
putShortMSB(s,b)10740Sstevel@tonic-gate putShortMSB(s, b)
10750Sstevel@tonic-gate     deflate_state *s;
10760Sstevel@tonic-gate     uInt b;
10770Sstevel@tonic-gate {
10780Sstevel@tonic-gate 	put_byte(s, (Byte)(b >> 8));
10790Sstevel@tonic-gate 	put_byte(s, (Byte)(b & 0xff));
10800Sstevel@tonic-gate }
10810Sstevel@tonic-gate 
10820Sstevel@tonic-gate /*
10830Sstevel@tonic-gate  * =========================================================================
10840Sstevel@tonic-gate  * Flush as much pending output as possible. All deflate() output goes
10850Sstevel@tonic-gate  * through this function so some applications may wish to modify it
10860Sstevel@tonic-gate  * to avoid allocating a large strm->next_out buffer and copying into it.
10870Sstevel@tonic-gate  * (See also read_buf()).
10880Sstevel@tonic-gate  */
10890Sstevel@tonic-gate local void
flush_pending(strm)10900Sstevel@tonic-gate flush_pending(strm)
10910Sstevel@tonic-gate     z_streamp strm;
10920Sstevel@tonic-gate {
10930Sstevel@tonic-gate 	deflate_state *s = (deflate_state *) strm->state;
10940Sstevel@tonic-gate 	unsigned len = s->pending;
10950Sstevel@tonic-gate 
10960Sstevel@tonic-gate 	if (len > strm->avail_out) len = strm->avail_out;
10970Sstevel@tonic-gate 	if (len == 0)
10980Sstevel@tonic-gate 		return;
10990Sstevel@tonic-gate 
11000Sstevel@tonic-gate 	if (strm->next_out != Z_NULL) {		/* PPP */
11010Sstevel@tonic-gate 		zmemcpy(strm->next_out, s->pending_out, len);
11020Sstevel@tonic-gate 		strm->next_out += len;
11030Sstevel@tonic-gate 	}					/* PPP */
11040Sstevel@tonic-gate 	s->pending_out += len;
11050Sstevel@tonic-gate 	strm->total_out += len;
11060Sstevel@tonic-gate 	strm->avail_out  -= len;
11070Sstevel@tonic-gate 	s->pending -= len;
11080Sstevel@tonic-gate 	if (s->pending == 0) {
11090Sstevel@tonic-gate 		s->pending_out = s->pending_buf;
11100Sstevel@tonic-gate 	}
11110Sstevel@tonic-gate }
11120Sstevel@tonic-gate 
11130Sstevel@tonic-gate /* ========================================================================= */
11140Sstevel@tonic-gate int
deflate(strm,flush)11150Sstevel@tonic-gate deflate(strm, flush)
11160Sstevel@tonic-gate     z_streamp strm;
11170Sstevel@tonic-gate     int flush;
11180Sstevel@tonic-gate {
11190Sstevel@tonic-gate 	int old_flush;	/* value of flush param for previous deflate call */
11200Sstevel@tonic-gate 	deflate_state *s;
11210Sstevel@tonic-gate 
11220Sstevel@tonic-gate 	if (strm == Z_NULL || strm->state == Z_NULL ||
11230Sstevel@tonic-gate 	    flush > Z_FINISH || flush < 0) {
11240Sstevel@tonic-gate 		return (Z_STREAM_ERROR);
11250Sstevel@tonic-gate 	}
11260Sstevel@tonic-gate 	s = (deflate_state *) strm->state;
11270Sstevel@tonic-gate 
11280Sstevel@tonic-gate 	if (/* strm->next_out == Z_NULL || --- we allow null --- PPP */
11290Sstevel@tonic-gate 		(strm->next_in == Z_NULL && strm->avail_in != 0) ||
11300Sstevel@tonic-gate 	    (s->status == FINISH_STATE && flush != Z_FINISH)) {
11310Sstevel@tonic-gate 		ERR_RETURN(strm, Z_STREAM_ERROR);
11320Sstevel@tonic-gate 	}
11330Sstevel@tonic-gate 	if (strm->avail_out == 0) ERR_RETURN(strm, Z_BUF_ERROR);
11340Sstevel@tonic-gate 
11350Sstevel@tonic-gate 	s->strm = strm;	/* just in case */
11360Sstevel@tonic-gate 	old_flush = s->last_flush;
11370Sstevel@tonic-gate 	s->last_flush = flush;
11380Sstevel@tonic-gate 
11390Sstevel@tonic-gate 	/* Write the zlib header */
11400Sstevel@tonic-gate 	if (s->status == INIT_STATE) {
11410Sstevel@tonic-gate 
11420Sstevel@tonic-gate 		uInt header = (Z_DEFLATED + ((s->w_bits-8)<<4)) << 8;
11430Sstevel@tonic-gate 		uInt level_flags = (s->level-1) >> 1;
11440Sstevel@tonic-gate 
11450Sstevel@tonic-gate 		if (level_flags > 3) level_flags = 3;
11460Sstevel@tonic-gate 		header |= (level_flags << 6);
11470Sstevel@tonic-gate 		if (s->strstart != 0) header |= PRESET_DICT;
11480Sstevel@tonic-gate 		header += 31 - (header % 31);
11490Sstevel@tonic-gate 
11500Sstevel@tonic-gate 		s->status = BUSY_STATE;
11510Sstevel@tonic-gate 		putShortMSB(s, header);
11520Sstevel@tonic-gate 
11530Sstevel@tonic-gate 		/* Save the adler32 of the preset dictionary: */
11540Sstevel@tonic-gate 		if (s->strstart != 0) {
11550Sstevel@tonic-gate 			putShortMSB(s, (uInt)(strm->adler >> 16));
11560Sstevel@tonic-gate 			putShortMSB(s, (uInt)(strm->adler & 0xffff));
11570Sstevel@tonic-gate 		}
11580Sstevel@tonic-gate 		strm->adler = 1L;
11590Sstevel@tonic-gate 	}
11600Sstevel@tonic-gate 
11610Sstevel@tonic-gate 	/* Flush as much pending output as possible */
11620Sstevel@tonic-gate 	if (s->pending != 0) {
11630Sstevel@tonic-gate 		flush_pending(strm);
11640Sstevel@tonic-gate 		if (strm->avail_out == 0) {
11650Sstevel@tonic-gate 			/*
11660Sstevel@tonic-gate 			 * Since avail_out is 0, deflate will be
11670Sstevel@tonic-gate 			 * called again with more output space, but
11680Sstevel@tonic-gate 			 * possibly with both pending and avail_in
11690Sstevel@tonic-gate 			 * equal to zero. There won't be anything to
11700Sstevel@tonic-gate 			 * do, but this is not an error situation so
11710Sstevel@tonic-gate 			 * make sure we return OK instead of BUF_ERROR
11720Sstevel@tonic-gate 			 * at next call of deflate:
11730Sstevel@tonic-gate 			 */
11740Sstevel@tonic-gate 			s->last_flush = -1;
11750Sstevel@tonic-gate 			return (Z_OK);
11760Sstevel@tonic-gate 		}
11770Sstevel@tonic-gate 
11780Sstevel@tonic-gate 		/*
11790Sstevel@tonic-gate 		 * Make sure there is something to do and avoid
11800Sstevel@tonic-gate 		 * duplicate consecutive flushes. For repeated and
11810Sstevel@tonic-gate 		 * useless calls with Z_FINISH, we keep returning
11820Sstevel@tonic-gate 		 * Z_STREAM_END instead of Z_BUFF_ERROR.
11830Sstevel@tonic-gate 		 */
11840Sstevel@tonic-gate 	} else if (strm->avail_in == 0 && flush <= old_flush &&
11850Sstevel@tonic-gate 	    flush != Z_FINISH) {
11860Sstevel@tonic-gate 		ERR_RETURN(strm, Z_BUF_ERROR);
11870Sstevel@tonic-gate 	}
11880Sstevel@tonic-gate 
11890Sstevel@tonic-gate 	/* User must not provide more input after the first FINISH: */
11900Sstevel@tonic-gate 	if (s->status == FINISH_STATE && strm->avail_in != 0) {
11910Sstevel@tonic-gate 		ERR_RETURN(strm, Z_BUF_ERROR);
11920Sstevel@tonic-gate 	}
11930Sstevel@tonic-gate 
11940Sstevel@tonic-gate 	/* Start a new block or continue the current one. */
11950Sstevel@tonic-gate 	if (strm->avail_in != 0 || s->lookahead != 0 ||
11960Sstevel@tonic-gate 	    (flush != Z_NO_FLUSH && s->status != FINISH_STATE)) {
11970Sstevel@tonic-gate 		block_state bstate;
11980Sstevel@tonic-gate 
11990Sstevel@tonic-gate 		bstate = (*(configuration_table[s->level].func))(s, flush);
12000Sstevel@tonic-gate 
12010Sstevel@tonic-gate 		if (bstate == finish_started || bstate == finish_done) {
12020Sstevel@tonic-gate 			s->status = FINISH_STATE;
12030Sstevel@tonic-gate 		}
12040Sstevel@tonic-gate 		if (bstate == need_more || bstate == finish_started) {
12050Sstevel@tonic-gate 			if (strm->avail_out == 0) {
12060Sstevel@tonic-gate 				/* avoid BUF_ERROR next call, see above */
12070Sstevel@tonic-gate 				s->last_flush = -1;
12080Sstevel@tonic-gate 			}
12090Sstevel@tonic-gate 			return (Z_OK);
12100Sstevel@tonic-gate 			/*
12110Sstevel@tonic-gate 			 * If flush != Z_NO_FLUSH && avail_out == 0,
12120Sstevel@tonic-gate 			 * the next call of deflate should use the
12130Sstevel@tonic-gate 			 * same flush parameter to make sure that the
12140Sstevel@tonic-gate 			 * flush is complete. So we don't have to
12150Sstevel@tonic-gate 			 * output an empty block here, this will be
12160Sstevel@tonic-gate 			 * done at next call. This also ensures that
12170Sstevel@tonic-gate 			 * for a very small output buffer, we emit at
12180Sstevel@tonic-gate 			 * most one empty block.
12190Sstevel@tonic-gate 			 */
12200Sstevel@tonic-gate 		}
12210Sstevel@tonic-gate 		if (bstate == block_done) {
12220Sstevel@tonic-gate 			if (flush == Z_PARTIAL_FLUSH) {
12230Sstevel@tonic-gate 				_tr_align(s);
12240Sstevel@tonic-gate 			} else if (flush == Z_PACKET_FLUSH) {	/* PPP */
12250Sstevel@tonic-gate 				/*
12260Sstevel@tonic-gate 				 * Output just the 3-bit `stored'
12270Sstevel@tonic-gate 				 * block type value, but not a zero
12280Sstevel@tonic-gate 				 * length.  Added for PPP.
12290Sstevel@tonic-gate 				 */
12300Sstevel@tonic-gate 				_tr_stored_type_only(s);	/* PPP */
12310Sstevel@tonic-gate 			} else { /* FULL_FLUSH or SYNC_FLUSH */
12320Sstevel@tonic-gate 				_tr_stored_block(s, (char *)0, 0L, 0);
12330Sstevel@tonic-gate 				/*
12340Sstevel@tonic-gate 				 * For a full flush, this empty block
12350Sstevel@tonic-gate 				 * will be recognized as a special
12360Sstevel@tonic-gate 				 * marker by inflate_sync().
12370Sstevel@tonic-gate 				 */
12380Sstevel@tonic-gate 				if (flush == Z_FULL_FLUSH) {
12390Sstevel@tonic-gate 					CLEAR_HASH(s);	/* forget history */
12400Sstevel@tonic-gate 				}
12410Sstevel@tonic-gate 			}
12420Sstevel@tonic-gate 			flush_pending(strm);
12430Sstevel@tonic-gate 			if (strm->avail_out == 0) {
12440Sstevel@tonic-gate 				/* avoid BUF_ERROR at next call, see above */
12450Sstevel@tonic-gate 				s->last_flush = -1;
12460Sstevel@tonic-gate 				return (Z_OK);
12470Sstevel@tonic-gate 			}
12480Sstevel@tonic-gate 		}
12490Sstevel@tonic-gate 	}
12500Sstevel@tonic-gate 	Assert(strm->avail_out > 0, "bug2");
12510Sstevel@tonic-gate 
12520Sstevel@tonic-gate 	if (flush != Z_FINISH)
12530Sstevel@tonic-gate 		return (Z_OK);
12540Sstevel@tonic-gate 	if (s->noheader)
12550Sstevel@tonic-gate 		return (Z_STREAM_END);
12560Sstevel@tonic-gate 
12570Sstevel@tonic-gate 	/* Write the zlib trailer (adler32) */
12580Sstevel@tonic-gate 	putShortMSB(s, (uInt)(strm->adler >> 16));
12590Sstevel@tonic-gate 	putShortMSB(s, (uInt)(strm->adler & 0xffff));
12600Sstevel@tonic-gate 	flush_pending(strm);
12610Sstevel@tonic-gate 	/*
12620Sstevel@tonic-gate 	 * If avail_out is zero, the application will call deflate
12630Sstevel@tonic-gate 	 * again to flush the rest.
12640Sstevel@tonic-gate 	 */
12650Sstevel@tonic-gate 	s->noheader = -1;	/* write the trailer only once! */
12660Sstevel@tonic-gate 	return (s->pending != 0 ? Z_OK : Z_STREAM_END);
12670Sstevel@tonic-gate }
12680Sstevel@tonic-gate 
12690Sstevel@tonic-gate /* ========================================================================= */
12700Sstevel@tonic-gate int
deflateEnd(strm)12710Sstevel@tonic-gate deflateEnd(strm)
12720Sstevel@tonic-gate     z_streamp strm;
12730Sstevel@tonic-gate {
12740Sstevel@tonic-gate 	int status;
12750Sstevel@tonic-gate 	deflate_state *s;
12760Sstevel@tonic-gate 
12770Sstevel@tonic-gate 	if (strm == Z_NULL || strm->state == Z_NULL)
12780Sstevel@tonic-gate 		return (Z_STREAM_ERROR);
12790Sstevel@tonic-gate 	s = (deflate_state *) strm->state;
12800Sstevel@tonic-gate 
12810Sstevel@tonic-gate 	status = s->status;
12820Sstevel@tonic-gate 	if (status != INIT_STATE && status != BUSY_STATE &&
12830Sstevel@tonic-gate 	    status != FINISH_STATE) {
12840Sstevel@tonic-gate 		return (Z_STREAM_ERROR);
12850Sstevel@tonic-gate 	}
12860Sstevel@tonic-gate 
12870Sstevel@tonic-gate 	/* Deallocate in reverse order of allocations: */
12880Sstevel@tonic-gate 	TRY_FREE(strm, s->pending_buf);
12890Sstevel@tonic-gate 	TRY_FREE(strm, s->head);
12900Sstevel@tonic-gate 	TRY_FREE(strm, s->prev);
12910Sstevel@tonic-gate 	TRY_FREE(strm, s->window);
12920Sstevel@tonic-gate 
12930Sstevel@tonic-gate 	ZFREE(strm, s);
12940Sstevel@tonic-gate 	strm->state = Z_NULL;
12950Sstevel@tonic-gate 
12960Sstevel@tonic-gate 	return (status == BUSY_STATE ? Z_DATA_ERROR : Z_OK);
12970Sstevel@tonic-gate }
12980Sstevel@tonic-gate 
12990Sstevel@tonic-gate /*
13000Sstevel@tonic-gate  * =========================================================================
13010Sstevel@tonic-gate  * Copy the source state to the destination state.
13020Sstevel@tonic-gate  * To simplify the source, this is not supported for 16-bit MSDOS (which
13030Sstevel@tonic-gate  * doesn't have enough memory anyway to duplicate compression states).
13040Sstevel@tonic-gate  */
13050Sstevel@tonic-gate int
deflateCopy(dest,source)13060Sstevel@tonic-gate deflateCopy(dest, source)
13070Sstevel@tonic-gate     z_streamp dest;
13080Sstevel@tonic-gate     z_streamp source;
13090Sstevel@tonic-gate {
13100Sstevel@tonic-gate #ifdef MAXSEG_64K
13110Sstevel@tonic-gate 	return (Z_STREAM_ERROR);
13120Sstevel@tonic-gate #else
13130Sstevel@tonic-gate 	deflate_state *ds;
13140Sstevel@tonic-gate 	deflate_state *ss;
13150Sstevel@tonic-gate 	ushf *overlay;
13160Sstevel@tonic-gate 
13170Sstevel@tonic-gate 	if (source == Z_NULL || dest == Z_NULL || source->state == Z_NULL)
13180Sstevel@tonic-gate 		return (Z_STREAM_ERROR);
13190Sstevel@tonic-gate 	ss = (deflate_state *) source->state;
13200Sstevel@tonic-gate 
13210Sstevel@tonic-gate 	zmemcpy(dest, source, sizeof (*dest));
13220Sstevel@tonic-gate 
13230Sstevel@tonic-gate 	ds = (deflate_state *) ZALLOC(dest, 1, sizeof (deflate_state));
13240Sstevel@tonic-gate 	if (ds == Z_NULL)
13250Sstevel@tonic-gate 		return (Z_MEM_ERROR);
13260Sstevel@tonic-gate 	dest->state = (struct internal_state FAR *) ds;
13270Sstevel@tonic-gate 	zmemcpy(ds, ss, sizeof (*ds));
13280Sstevel@tonic-gate 	ds->strm = dest;
13290Sstevel@tonic-gate 
13300Sstevel@tonic-gate 	ds->window = (Bytef *) ZALLOC(dest, ds->w_size, 2*sizeof (Byte));
13310Sstevel@tonic-gate 	ds->prev   = (Posf *)  ZALLOC(dest, ds->w_size, sizeof (Pos));
13320Sstevel@tonic-gate 	ds->head   = (Posf *)  ZALLOC(dest, ds->hash_size, sizeof (Pos));
13330Sstevel@tonic-gate 	overlay = (ushf *) ZALLOC(dest, ds->lit_bufsize, sizeof (ush)+2);
13340Sstevel@tonic-gate 	ds->pending_buf = (uchf *) overlay;
13350Sstevel@tonic-gate 
13360Sstevel@tonic-gate 	if (ds->window == Z_NULL || ds->prev == Z_NULL || ds->head == Z_NULL ||
13370Sstevel@tonic-gate 	    ds->pending_buf == Z_NULL) {
13380Sstevel@tonic-gate 		ds->status = INIT_STATE;
13390Sstevel@tonic-gate 		(void) deflateEnd(dest);
13400Sstevel@tonic-gate 		return (Z_MEM_ERROR);
13410Sstevel@tonic-gate 	}
13420Sstevel@tonic-gate 	/* following zmemcpy doesn't work for 16-bit MSDOS */
13430Sstevel@tonic-gate 	zmemcpy(ds->window, ss->window, ds->w_size * 2 * sizeof (Byte));
13440Sstevel@tonic-gate 	zmemcpy(ds->prev, ss->prev, ds->w_size * sizeof (Pos));
13450Sstevel@tonic-gate 	zmemcpy(ds->head, ss->head, ds->hash_size * sizeof (Pos));
13460Sstevel@tonic-gate 	zmemcpy(ds->pending_buf, ss->pending_buf, (uInt)ds->pending_buf_size);
13470Sstevel@tonic-gate 
13480Sstevel@tonic-gate 	ds->pending_out = ds->pending_buf + (ss->pending_out - ss->pending_buf);
13490Sstevel@tonic-gate 	ds->d_buf = overlay + ds->lit_bufsize/sizeof (ush);
13500Sstevel@tonic-gate 	ds->l_buf = ds->pending_buf + (1+sizeof (ush))*ds->lit_bufsize;
13510Sstevel@tonic-gate 
13520Sstevel@tonic-gate 	ds->l_desc.dyn_tree = ds->dyn_ltree;
13530Sstevel@tonic-gate 	ds->d_desc.dyn_tree = ds->dyn_dtree;
13540Sstevel@tonic-gate 	ds->bl_desc.dyn_tree = ds->bl_tree;
13550Sstevel@tonic-gate 
13560Sstevel@tonic-gate 	return (Z_OK);
13570Sstevel@tonic-gate #endif
13580Sstevel@tonic-gate }
13590Sstevel@tonic-gate 
13600Sstevel@tonic-gate /*
13610Sstevel@tonic-gate  * ===========================================================================
13620Sstevel@tonic-gate  * Return the number of bytes of output which are immediately available
13630Sstevel@tonic-gate  * for output from the decompressor.		---PPP---
13640Sstevel@tonic-gate  */
13650Sstevel@tonic-gate int
deflateOutputPending(strm)13660Sstevel@tonic-gate deflateOutputPending(strm)
13670Sstevel@tonic-gate     z_streamp strm;
13680Sstevel@tonic-gate {
13690Sstevel@tonic-gate 	if (strm == Z_NULL || strm->state == Z_NULL)
13700Sstevel@tonic-gate 		return (0);
13710Sstevel@tonic-gate 
13720Sstevel@tonic-gate 	return (((deflate_state *)(strm->state))->pending);
13730Sstevel@tonic-gate }
13740Sstevel@tonic-gate 
13750Sstevel@tonic-gate /*
13760Sstevel@tonic-gate  * ===========================================================================
13770Sstevel@tonic-gate  * Read a new buffer from the current input stream, update the adler32
13780Sstevel@tonic-gate  * and total number of bytes read.  All deflate() input goes through
13790Sstevel@tonic-gate  * this function so some applications may wish to modify it to avoid
13800Sstevel@tonic-gate  * allocating a large strm->next_in buffer and copying from it.
13810Sstevel@tonic-gate  * (See also flush_pending()).
13820Sstevel@tonic-gate  */
13830Sstevel@tonic-gate local int
read_buf(strm,buf,size)13840Sstevel@tonic-gate read_buf(strm, buf, size)
13850Sstevel@tonic-gate     z_streamp strm;
13860Sstevel@tonic-gate     Bytef *buf;
13870Sstevel@tonic-gate     unsigned size;
13880Sstevel@tonic-gate {
13890Sstevel@tonic-gate 	unsigned len = strm->avail_in;
13900Sstevel@tonic-gate 
13910Sstevel@tonic-gate 	if (len > size) len = size;
13920Sstevel@tonic-gate 	if (len == 0)
13930Sstevel@tonic-gate 		return (0);
13940Sstevel@tonic-gate 
13950Sstevel@tonic-gate 	strm->avail_in  -= len;
13960Sstevel@tonic-gate 
13970Sstevel@tonic-gate 	if (!((deflate_state *)(strm->state))->noheader) {
13980Sstevel@tonic-gate 		strm->adler = adler32(strm->adler, strm->next_in, len);
13990Sstevel@tonic-gate 	}
14000Sstevel@tonic-gate 	zmemcpy(buf, strm->next_in, len);
14010Sstevel@tonic-gate 	strm->next_in  += len;
14020Sstevel@tonic-gate 	strm->total_in += len;
14030Sstevel@tonic-gate 
14040Sstevel@tonic-gate 	return ((int)len);
14050Sstevel@tonic-gate }
14060Sstevel@tonic-gate 
14070Sstevel@tonic-gate /*
14080Sstevel@tonic-gate  * ===========================================================================
14090Sstevel@tonic-gate  * Initialize the "longest match" routines for a new zlib stream
14100Sstevel@tonic-gate  */
14110Sstevel@tonic-gate local void
lm_init(s)14120Sstevel@tonic-gate lm_init(s)
14130Sstevel@tonic-gate     deflate_state *s;
14140Sstevel@tonic-gate {
14150Sstevel@tonic-gate 	s->window_size = (ulg)2L*s->w_size;
14160Sstevel@tonic-gate 
14170Sstevel@tonic-gate 	CLEAR_HASH(s);
14180Sstevel@tonic-gate 
14190Sstevel@tonic-gate 	/* Set the default configuration parameters: */
14200Sstevel@tonic-gate 	s->max_lazy_match   = configuration_table[s->level].max_lazy;
14210Sstevel@tonic-gate 	s->good_match	= configuration_table[s->level].good_length;
14220Sstevel@tonic-gate 	s->nice_match	= configuration_table[s->level].nice_length;
14230Sstevel@tonic-gate 	s->max_chain_length = configuration_table[s->level].max_chain;
14240Sstevel@tonic-gate 
14250Sstevel@tonic-gate 	s->strstart = 0;
14260Sstevel@tonic-gate 	s->block_start = 0L;
14270Sstevel@tonic-gate 	s->lookahead = 0;
14280Sstevel@tonic-gate 	s->match_length = s->prev_length = MIN_MATCH-1;
14290Sstevel@tonic-gate 	s->match_available = 0;
14300Sstevel@tonic-gate 	s->ins_h = 0;
14310Sstevel@tonic-gate #ifdef ASMV
14320Sstevel@tonic-gate 	match_init();	/* initialize the asm code */
14330Sstevel@tonic-gate #endif
14340Sstevel@tonic-gate }
14350Sstevel@tonic-gate 
14360Sstevel@tonic-gate /*
14370Sstevel@tonic-gate  * ===========================================================================
14380Sstevel@tonic-gate  * Set match_start to the longest match starting at the given string and
14390Sstevel@tonic-gate  * return its length. Matches shorter or equal to prev_length are discarded,
14400Sstevel@tonic-gate  * in which case the result is equal to prev_length and match_start is
14410Sstevel@tonic-gate  * garbage.
14420Sstevel@tonic-gate  * IN assertions: cur_match is the head of the hash chain for the current
14430Sstevel@tonic-gate  *   string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1
14440Sstevel@tonic-gate  * OUT assertion: the match length is not greater than s->lookahead.
14450Sstevel@tonic-gate  */
14460Sstevel@tonic-gate #ifndef ASMV
14470Sstevel@tonic-gate /*
14480Sstevel@tonic-gate  * For 80x86 and 680x0, an optimized version will be provided in
14490Sstevel@tonic-gate  * match.asm or match.S. The code will be functionally equivalent.
14500Sstevel@tonic-gate  */
14510Sstevel@tonic-gate #ifndef FASTEST
14520Sstevel@tonic-gate local uInt
longest_match(s,cur_match)14530Sstevel@tonic-gate longest_match(s, cur_match)
14540Sstevel@tonic-gate     deflate_state *s;
14550Sstevel@tonic-gate     IPos cur_match;	/* current match */
14560Sstevel@tonic-gate {
14570Sstevel@tonic-gate 	/* max hash chain length */
14580Sstevel@tonic-gate 	unsigned chain_length = s->max_chain_length;
14590Sstevel@tonic-gate 	register Bytef *scan = s->window + s->strstart;	/* current string */
14600Sstevel@tonic-gate 	register Bytef *match;	/* matched string */
14610Sstevel@tonic-gate 	register int len;	/* length of current match */
14620Sstevel@tonic-gate 	int best_len = s->prev_length;	/* best match length so far */
14630Sstevel@tonic-gate 	int nice_match = s->nice_match;	/* stop if match long enough */
14640Sstevel@tonic-gate 	IPos limit = s->strstart > (IPos)MAX_DIST(s) ?
14650Sstevel@tonic-gate 	    s->strstart - (IPos)MAX_DIST(s) : NIL;
14660Sstevel@tonic-gate 	/*
14670Sstevel@tonic-gate 	 * Stop when cur_match becomes <= limit. To simplify the code,
14680Sstevel@tonic-gate 	 * we prevent matches with the string of window index 0.
14690Sstevel@tonic-gate 	 */
14700Sstevel@tonic-gate 	Posf *prev = s->prev;
14710Sstevel@tonic-gate 	uInt wmask = s->w_mask;
14720Sstevel@tonic-gate 
14730Sstevel@tonic-gate #ifdef UNALIGNED_OK
14740Sstevel@tonic-gate 	/*
14750Sstevel@tonic-gate 	 * Compare two bytes at a time. Note: this is not always
14760Sstevel@tonic-gate 	 * beneficial.  Try with and without -DUNALIGNED_OK to check.
14770Sstevel@tonic-gate 	 */
14780Sstevel@tonic-gate 	register Bytef *strend = s->window + s->strstart + MAX_MATCH - 1;
14790Sstevel@tonic-gate 	register ush scan_start = *(ushf*)scan;
14800Sstevel@tonic-gate 	register ush scan_end   = *(ushf*)(scan+best_len-1);
14810Sstevel@tonic-gate #else
14820Sstevel@tonic-gate 	register Bytef *strend = s->window + s->strstart + MAX_MATCH;
14830Sstevel@tonic-gate 	register Byte scan_end1  = scan[best_len-1];
14840Sstevel@tonic-gate 	register Byte scan_end   = scan[best_len];
14850Sstevel@tonic-gate #endif
14860Sstevel@tonic-gate 
14870Sstevel@tonic-gate 	/*
14880Sstevel@tonic-gate 	 * The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2
14890Sstevel@tonic-gate 	 * multiple of 16.  It is easy to get rid of this optimization
14900Sstevel@tonic-gate 	 * if necessary.
14910Sstevel@tonic-gate 	 */
14920Sstevel@tonic-gate 	Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever");
14930Sstevel@tonic-gate 
14940Sstevel@tonic-gate 	/* Do not waste too much time if we already have a good match: */
14950Sstevel@tonic-gate 	if (s->prev_length >= s->good_match) {
14960Sstevel@tonic-gate 		chain_length >>= 2;
14970Sstevel@tonic-gate 	}
14980Sstevel@tonic-gate 	/*
14990Sstevel@tonic-gate 	 * Do not look for matches beyond the end of the input. This
15000Sstevel@tonic-gate 	 * is necessary to make deflate deterministic.
15010Sstevel@tonic-gate 	 */
15020Sstevel@tonic-gate 	if ((uInt)nice_match > s->lookahead) nice_match = s->lookahead;
15030Sstevel@tonic-gate 
15040Sstevel@tonic-gate 	Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD,
15050Sstevel@tonic-gate 	    "need lookahead");
15060Sstevel@tonic-gate 
15070Sstevel@tonic-gate 	do {
15080Sstevel@tonic-gate 		Assert(cur_match <= s->strstart, "no future");
15090Sstevel@tonic-gate 		match = s->window + cur_match;
15100Sstevel@tonic-gate 
15110Sstevel@tonic-gate 		/*
15120Sstevel@tonic-gate 		 * Skip to next match if the match length cannot
15130Sstevel@tonic-gate 		 * increase or if the match length is less than 2:
15140Sstevel@tonic-gate 		 */
15150Sstevel@tonic-gate #if (defined(UNALIGNED_OK) && MAX_MATCH == 258)
15160Sstevel@tonic-gate 		/*
15170Sstevel@tonic-gate 		 * This code assumes sizeof (unsigned short) == 2. Do
15180Sstevel@tonic-gate 		 * not use UNALIGNED_OK if your compiler uses a
15190Sstevel@tonic-gate 		 * different size.
15200Sstevel@tonic-gate 		 */
15210Sstevel@tonic-gate 		if (*(ushf*)(match+best_len-1) != scan_end ||
15220Sstevel@tonic-gate 		    *(ushf*)match != scan_start) continue;
15230Sstevel@tonic-gate 
15240Sstevel@tonic-gate 		/*
15250Sstevel@tonic-gate 		 * It is not necessary to compare scan[2] and match[2]
15260Sstevel@tonic-gate 		 * since they are always equal when the other bytes
15270Sstevel@tonic-gate 		 * match, given that the hash keys are equal and that
15280Sstevel@tonic-gate 		 * HASH_BITS >= 8. Compare 2 bytes at a time at
15290Sstevel@tonic-gate 		 * strstart+3, +5, ... up to strstart+257. We check
15300Sstevel@tonic-gate 		 * for insufficient lookahead only every 4th
15310Sstevel@tonic-gate 		 * comparison; the 128th check will be made at
15320Sstevel@tonic-gate 		 * strstart+257. If MAX_MATCH-2 is not a multiple of
15330Sstevel@tonic-gate 		 * 8, it is necessary to put more guard bytes at the
15340Sstevel@tonic-gate 		 * end of the window, or to check more often for
15350Sstevel@tonic-gate 		 * insufficient lookahead.
15360Sstevel@tonic-gate 		 */
15370Sstevel@tonic-gate 		Assert(scan[2] == match[2], "scan[2]?");
15380Sstevel@tonic-gate 		scan++, match++;
15390Sstevel@tonic-gate 		do {
15400Sstevel@tonic-gate 		} while (*(ushf *)(scan += 2) == *(ushf *)(match += 2) &&
15410Sstevel@tonic-gate 		    *(ushf *)(scan += 2) == *(ushf *)(match += 2) &&
15420Sstevel@tonic-gate 		    *(ushf *)(scan += 2) == *(ushf *)(match += 2) &&
15430Sstevel@tonic-gate 		    *(ushf *)(scan += 2) == *(ushf *)(match += 2) &&
15440Sstevel@tonic-gate 		    scan < strend);
15450Sstevel@tonic-gate 		/* The funny "do {}" generates better code on most compilers */
15460Sstevel@tonic-gate 
15470Sstevel@tonic-gate 		/* Here, scan <= window+strstart+257 */
15480Sstevel@tonic-gate 		Assert(scan <= s->window+(unsigned)(s->window_size-1),
15490Sstevel@tonic-gate 		    "wild scan");
15500Sstevel@tonic-gate 		if (*scan == *match) scan++;
15510Sstevel@tonic-gate 
15520Sstevel@tonic-gate 		len = (MAX_MATCH - 1) - (int)(strend-scan);
15530Sstevel@tonic-gate 		scan = strend - (MAX_MATCH-1);
15540Sstevel@tonic-gate 
15550Sstevel@tonic-gate #else /* UNALIGNED_OK */
15560Sstevel@tonic-gate 
15570Sstevel@tonic-gate 		if (match[best_len]	!= scan_end	||
15580Sstevel@tonic-gate 		    match[best_len-1]	!= scan_end1	||
15590Sstevel@tonic-gate 		    *match		!= *scan	||
15600Sstevel@tonic-gate 		    *++match		!= scan[1])
15610Sstevel@tonic-gate 			continue;
15620Sstevel@tonic-gate 
15630Sstevel@tonic-gate 		/*
15640Sstevel@tonic-gate 		 * The check at best_len-1 can be removed because it
15650Sstevel@tonic-gate 		 * will be made again later. (This heuristic is not
15660Sstevel@tonic-gate 		 * always a win.)  It is not necessary to compare
15670Sstevel@tonic-gate 		 * scan[2] and match[2] since they are always equal
15680Sstevel@tonic-gate 		 * when the other bytes match, given that the hash
15690Sstevel@tonic-gate 		 * keys are equal and that HASH_BITS >= 8.
15700Sstevel@tonic-gate 		 */
15710Sstevel@tonic-gate 		scan += 2, match++;
15720Sstevel@tonic-gate 		Assert(*scan == *match, "match[2]?");
15730Sstevel@tonic-gate 
15740Sstevel@tonic-gate 		/*
15750Sstevel@tonic-gate 		 * We check for insufficient lookahead only every 8th
15760Sstevel@tonic-gate 		 * comparison; the 256th check will be made at
15770Sstevel@tonic-gate 		 * strstart+258.
15780Sstevel@tonic-gate 		 */
15790Sstevel@tonic-gate 		do {
15800Sstevel@tonic-gate 		} while (*++scan == *++match && *++scan == *++match &&
15810Sstevel@tonic-gate 		    *++scan == *++match && *++scan == *++match &&
15820Sstevel@tonic-gate 		    *++scan == *++match && *++scan == *++match &&
15830Sstevel@tonic-gate 		    *++scan == *++match && *++scan == *++match &&
15840Sstevel@tonic-gate 		    scan < strend);
15850Sstevel@tonic-gate 
15860Sstevel@tonic-gate 		Assert(scan <= s->window+(unsigned)(s->window_size-1),
15870Sstevel@tonic-gate 		    "wild scan");
15880Sstevel@tonic-gate 
15890Sstevel@tonic-gate 		len = MAX_MATCH - (int)(strend - scan);
15900Sstevel@tonic-gate 		scan = strend - MAX_MATCH;
15910Sstevel@tonic-gate 
15920Sstevel@tonic-gate #endif /* UNALIGNED_OK */
15930Sstevel@tonic-gate 
15940Sstevel@tonic-gate 		if (len > best_len) {
15950Sstevel@tonic-gate 			s->match_start = cur_match;
15960Sstevel@tonic-gate 			best_len = len;
15970Sstevel@tonic-gate 			if (len >= nice_match) break;
15980Sstevel@tonic-gate #ifdef UNALIGNED_OK
15990Sstevel@tonic-gate 			scan_end = *(ushf*)(scan+best_len-1);
16000Sstevel@tonic-gate #else
16010Sstevel@tonic-gate 			scan_end1  = scan[best_len-1];
16020Sstevel@tonic-gate 			scan_end   = scan[best_len];
16030Sstevel@tonic-gate #endif
16040Sstevel@tonic-gate 		}
16050Sstevel@tonic-gate 	} while ((cur_match = prev[cur_match & wmask]) > limit &&
16060Sstevel@tonic-gate 	    --chain_length != 0);
16070Sstevel@tonic-gate 
16080Sstevel@tonic-gate 	if ((uInt)best_len <= s->lookahead)
16090Sstevel@tonic-gate 		return (best_len);
16100Sstevel@tonic-gate 	return (s->lookahead);
16110Sstevel@tonic-gate }
16120Sstevel@tonic-gate #else /* FASTEST */
16130Sstevel@tonic-gate /*
16140Sstevel@tonic-gate  * ---------------------------------------------------------------------------
16150Sstevel@tonic-gate  * Optimized version for level == 1 only
16160Sstevel@tonic-gate  */
16170Sstevel@tonic-gate local uInt
longest_match(s,cur_match)16180Sstevel@tonic-gate longest_match(s, cur_match)
16190Sstevel@tonic-gate deflate_state *s;
16200Sstevel@tonic-gate IPos cur_match;		/* current match */
16210Sstevel@tonic-gate {
16220Sstevel@tonic-gate 	register Bytef *scan = s->window + s->strstart; /* current string */
16230Sstevel@tonic-gate 	register Bytef *match;		/* matched string */
16240Sstevel@tonic-gate 	register int len;			/* length of current match */
16250Sstevel@tonic-gate 	register Bytef *strend = s->window + s->strstart + MAX_MATCH;
16260Sstevel@tonic-gate 
16270Sstevel@tonic-gate 	/*
16280Sstevel@tonic-gate 	 * The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2
16290Sstevel@tonic-gate 	 * multiple of 16.  It is easy to get rid of this optimization
16300Sstevel@tonic-gate 	 * if necessary.
16310Sstevel@tonic-gate 	 */
16320Sstevel@tonic-gate 	Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever");
16330Sstevel@tonic-gate 
16340Sstevel@tonic-gate 	Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD,
16350Sstevel@tonic-gate 	    "need lookahead");
16360Sstevel@tonic-gate 
16370Sstevel@tonic-gate 	Assert(cur_match <= s->strstart, "no future");
16380Sstevel@tonic-gate 
16390Sstevel@tonic-gate 	match = s->window + cur_match;
16400Sstevel@tonic-gate 
16410Sstevel@tonic-gate 	/* Return failure if the match length is less than 2: */
16420Sstevel@tonic-gate 	if (match[0] != scan[0] || match[1] != scan[1])
16430Sstevel@tonic-gate 		return (MIN_MATCH-1);
16440Sstevel@tonic-gate 
16450Sstevel@tonic-gate 	/*
16460Sstevel@tonic-gate 	 * The check at best_len-1 can be removed because it will be
16470Sstevel@tonic-gate 	 * made again later. (This heuristic is not always a win.)  It
16480Sstevel@tonic-gate 	 * is not necessary to compare scan[2] and match[2] since they
16490Sstevel@tonic-gate 	 * are always equal when the other bytes match, given that the
16500Sstevel@tonic-gate 	 * hash keys are equal and that HASH_BITS >= 8.
16510Sstevel@tonic-gate 	 */
16520Sstevel@tonic-gate 	scan += 2, match += 2;
16530Sstevel@tonic-gate 	Assert(*scan == *match, "match[2]?");
16540Sstevel@tonic-gate 
16550Sstevel@tonic-gate 	/*
16560Sstevel@tonic-gate 	 * We check for insufficient lookahead only every 8th comparison;
16570Sstevel@tonic-gate 	 * the 256th check will be made at strstart+258.
16580Sstevel@tonic-gate 	 */
16590Sstevel@tonic-gate 	do {
16600Sstevel@tonic-gate 	} while (*++scan == *++match && *++scan == *++match &&
16610Sstevel@tonic-gate 	    *++scan == *++match && *++scan == *++match &&
16620Sstevel@tonic-gate 	    *++scan == *++match && *++scan == *++match &&
16630Sstevel@tonic-gate 	    *++scan == *++match && *++scan == *++match &&
16640Sstevel@tonic-gate 	    scan < strend);
16650Sstevel@tonic-gate 
16660Sstevel@tonic-gate 	Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
16670Sstevel@tonic-gate 
16680Sstevel@tonic-gate 	len = MAX_MATCH - (int)(strend - scan);
16690Sstevel@tonic-gate 
16700Sstevel@tonic-gate 	if (len < MIN_MATCH)
16710Sstevel@tonic-gate 		return (MIN_MATCH - 1);
16720Sstevel@tonic-gate 
16730Sstevel@tonic-gate 	s->match_start = cur_match;
16740Sstevel@tonic-gate 	return (len <= s->lookahead ? len : s->lookahead);
16750Sstevel@tonic-gate }
16760Sstevel@tonic-gate #endif /* FASTEST */
16770Sstevel@tonic-gate #endif /* ASMV */
16780Sstevel@tonic-gate 
16790Sstevel@tonic-gate #ifdef DEBUG_ZLIB
16800Sstevel@tonic-gate /*
16810Sstevel@tonic-gate  * ===========================================================================
16820Sstevel@tonic-gate  * Check that the match at match_start is indeed a match.
16830Sstevel@tonic-gate  */
16840Sstevel@tonic-gate local void
check_match(s,start,match,length)16850Sstevel@tonic-gate check_match(s, start, match, length)
16860Sstevel@tonic-gate     deflate_state *s;
16870Sstevel@tonic-gate     IPos start, match;
16880Sstevel@tonic-gate     int length;
16890Sstevel@tonic-gate {
16900Sstevel@tonic-gate 	/* check that the match is indeed a match */
16910Sstevel@tonic-gate 	if (zmemcmp(s->window + match, s->window + start, length) != EQUAL) {
16920Sstevel@tonic-gate 		fprintf(stderr, " start %u, match %u, length %d\n",
16930Sstevel@tonic-gate 		    start, match, length);
16940Sstevel@tonic-gate 		do {
16950Sstevel@tonic-gate 			fprintf(stderr, "%c%c", s->window[match++],
16960Sstevel@tonic-gate 			    s->window[start++]);
16970Sstevel@tonic-gate 		} while (--length != 0);
16980Sstevel@tonic-gate 		z_error("invalid match");
16990Sstevel@tonic-gate 	}
17000Sstevel@tonic-gate 	if (z_verbose > 1) {
17010Sstevel@tonic-gate 		fprintf(stderr, "\\[%d,%d]", start-match, length);
17020Sstevel@tonic-gate 		do { putc(s->window[start++], stderr); } while (--length != 0);
17030Sstevel@tonic-gate 	}
17040Sstevel@tonic-gate }
17050Sstevel@tonic-gate #else
17060Sstevel@tonic-gate #define	check_match(s, start, match, length)
17070Sstevel@tonic-gate #endif
17080Sstevel@tonic-gate 
17090Sstevel@tonic-gate /*
17100Sstevel@tonic-gate  * ===========================================================================
17110Sstevel@tonic-gate  * Fill the window when the lookahead becomes insufficient.
17120Sstevel@tonic-gate  * Updates strstart and lookahead.
17130Sstevel@tonic-gate  *
17140Sstevel@tonic-gate  * IN assertion: lookahead < MIN_LOOKAHEAD
17150Sstevel@tonic-gate  * OUT assertions: strstart <= window_size-MIN_LOOKAHEAD
17160Sstevel@tonic-gate  *    At least one byte has been read, or avail_in == 0; reads are
17170Sstevel@tonic-gate  *    performed for at least two bytes (required for the zip translate_eol
17180Sstevel@tonic-gate  *    option -- not supported here).
17190Sstevel@tonic-gate  */
17200Sstevel@tonic-gate local void
fill_window(s)17210Sstevel@tonic-gate fill_window(s)
17220Sstevel@tonic-gate     deflate_state *s;
17230Sstevel@tonic-gate {
17240Sstevel@tonic-gate 	register unsigned n, m;
17250Sstevel@tonic-gate 	register Posf *p;
17260Sstevel@tonic-gate 	unsigned more;	/* Amount of free space at the end of the window. */
17270Sstevel@tonic-gate 	uInt wsize = s->w_size;
17280Sstevel@tonic-gate 
17290Sstevel@tonic-gate 	do {
17300Sstevel@tonic-gate 		more = (unsigned)(s->window_size -(ulg)s->lookahead -
17310Sstevel@tonic-gate 		    (ulg)s->strstart);
17320Sstevel@tonic-gate 
17330Sstevel@tonic-gate 		/* Deal with !@#$% 64K limit: */
17340Sstevel@tonic-gate 		if (more == 0 && s->strstart == 0 && s->lookahead == 0) {
17350Sstevel@tonic-gate 			more = wsize;
17360Sstevel@tonic-gate 
17370Sstevel@tonic-gate 		} else if (more == (unsigned)(-1)) {
17380Sstevel@tonic-gate 			/*
17390Sstevel@tonic-gate 			 * Very unlikely, but possible on 16 bit
17400Sstevel@tonic-gate 			 * machine if strstart == 0 and lookahead == 1
17410Sstevel@tonic-gate 			 * (input done one byte at time)
17420Sstevel@tonic-gate 			 */
17430Sstevel@tonic-gate 			more--;
17440Sstevel@tonic-gate 
17450Sstevel@tonic-gate 			/*
17460Sstevel@tonic-gate 			 * If the window is almost full and there is
17470Sstevel@tonic-gate 			 * insufficient lookahead, move the upper half
17480Sstevel@tonic-gate 			 * to the lower one to make room in the upper
17490Sstevel@tonic-gate 			 * half.
17500Sstevel@tonic-gate 			 */
17510Sstevel@tonic-gate 		} else if (s->strstart >= wsize+MAX_DIST(s)) {
17520Sstevel@tonic-gate 
17530Sstevel@tonic-gate 			Assert(wsize+wsize <= s->window_size, "wsize*2");
17540Sstevel@tonic-gate 			zmemcpy(s->window, s->window+wsize, (unsigned)wsize);
17550Sstevel@tonic-gate 			s->match_start -= wsize;
17560Sstevel@tonic-gate 			/* we now have strstart >= MAX_DIST */
17570Sstevel@tonic-gate 			s->strstart    -= wsize;
17580Sstevel@tonic-gate 			s->block_start -= (long)wsize;
17590Sstevel@tonic-gate 
17600Sstevel@tonic-gate 			/*
17610Sstevel@tonic-gate 			 * Slide the hash table (could be avoided with
17620Sstevel@tonic-gate 			 * 32 bit values at the expense of memory
17630Sstevel@tonic-gate 			 * usage). We slide even when level == 0 to
17640Sstevel@tonic-gate 			 * keep the hash table consistent if we switch
17650Sstevel@tonic-gate 			 * back to level > 0 later. (Using level 0
17660Sstevel@tonic-gate 			 * permanently is not an optimal usage of
17670Sstevel@tonic-gate 			 * zlib, so we don't care about this
17680Sstevel@tonic-gate 			 * pathological case.)
17690Sstevel@tonic-gate 			 */
17700Sstevel@tonic-gate 			n = s->hash_size;
17710Sstevel@tonic-gate 			p = &s->head[n];
17720Sstevel@tonic-gate 			do {
17730Sstevel@tonic-gate 				m = *--p;
17740Sstevel@tonic-gate 				*p = (Pos)(m >= wsize ? m-wsize : NIL);
17750Sstevel@tonic-gate 			} while (--n);
17760Sstevel@tonic-gate 
17770Sstevel@tonic-gate 			n = wsize;
17780Sstevel@tonic-gate #ifndef FASTEST
17790Sstevel@tonic-gate 			p = &s->prev[n];
17800Sstevel@tonic-gate 			do {
17810Sstevel@tonic-gate 				m = *--p;
17820Sstevel@tonic-gate 				*p = (Pos)(m >= wsize ? m-wsize : NIL);
17830Sstevel@tonic-gate 				/*
17840Sstevel@tonic-gate 				 * If n is not on any hash chain,
17850Sstevel@tonic-gate 				 * prev[n] is garbage but its value
17860Sstevel@tonic-gate 				 * will never be used.
17870Sstevel@tonic-gate 				 */
17880Sstevel@tonic-gate 			} while (--n);
17890Sstevel@tonic-gate #endif
17900Sstevel@tonic-gate 			more += wsize;
17910Sstevel@tonic-gate 		}
17920Sstevel@tonic-gate 		if (s->strm->avail_in == 0)
17930Sstevel@tonic-gate 			return;
17940Sstevel@tonic-gate 
17950Sstevel@tonic-gate 		/*
17960Sstevel@tonic-gate 		 * If there was no sliding:
17970Sstevel@tonic-gate 		 *    strstart <= WSIZE+MAX_DIST-1 &&
17980Sstevel@tonic-gate 		 *	lookahead <= MIN_LOOKAHEAD - 1 &&
17990Sstevel@tonic-gate 		 *    more == window_size - lookahead - strstart
18000Sstevel@tonic-gate 		 * => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE +
18010Sstevel@tonic-gate 		 *	MAX_DIST-1)
18020Sstevel@tonic-gate 		 * => more >= window_size - 2*WSIZE + 2
18030Sstevel@tonic-gate 		 * In the BIG_MEM or MMAP case (not yet supported),
18040Sstevel@tonic-gate 		 *   window_size == input_size + MIN_LOOKAHEAD  &&
18050Sstevel@tonic-gate 		 *   strstart + s->lookahead <= input_size =>
18060Sstevel@tonic-gate 		 *	more >= MIN_LOOKAHEAD.
18070Sstevel@tonic-gate 		 * Otherwise, window_size == 2*WSIZE so more >= 2.
18080Sstevel@tonic-gate 		 * If there was sliding, more >= WSIZE. So in all cases,
18090Sstevel@tonic-gate 		 * more >= 2.
18100Sstevel@tonic-gate 		 */
18110Sstevel@tonic-gate 		Assert(more >= 2, "more < 2");
18120Sstevel@tonic-gate 		Assert(s->strstart + s->lookahead + more <= s->window_size,
18130Sstevel@tonic-gate 		    "read too much");
18140Sstevel@tonic-gate 
18150Sstevel@tonic-gate 		n = read_buf(s->strm, s->window + s->strstart + s->lookahead,
18160Sstevel@tonic-gate 		    more);
18170Sstevel@tonic-gate 		s->lookahead += n;
18180Sstevel@tonic-gate 
18190Sstevel@tonic-gate 		/* Initialize the hash value now that we have some input: */
18200Sstevel@tonic-gate 		if (s->lookahead >= MIN_MATCH) {
18210Sstevel@tonic-gate 			s->ins_h = s->window[s->strstart];
18220Sstevel@tonic-gate 			UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]);
18230Sstevel@tonic-gate #if MIN_MATCH != 3
18240Sstevel@tonic-gate 			Call UPDATE_HASH() MIN_MATCH-3 more times
18250Sstevel@tonic-gate #endif
18260Sstevel@tonic-gate 			    }
18270Sstevel@tonic-gate 		/*
18280Sstevel@tonic-gate 		 * If the whole input has less than MIN_MATCH bytes,
18290Sstevel@tonic-gate 		 * ins_h is garbage, but this is not important since
18300Sstevel@tonic-gate 		 * only literal bytes will be emitted.
18310Sstevel@tonic-gate 		 */
18320Sstevel@tonic-gate 
18330Sstevel@tonic-gate 	} while (s->lookahead < MIN_LOOKAHEAD && s->strm->avail_in != 0);
18340Sstevel@tonic-gate }
18350Sstevel@tonic-gate 
18360Sstevel@tonic-gate /*
18370Sstevel@tonic-gate  * ===========================================================================
18380Sstevel@tonic-gate  * Flush the current block, with given end-of-file flag.
18390Sstevel@tonic-gate  * IN assertion: strstart is set to the end of the current match.
18400Sstevel@tonic-gate  */
18410Sstevel@tonic-gate #define	FLUSH_BLOCK_ONLY(s, eof) { \
18420Sstevel@tonic-gate 	_tr_flush_block(s, (s->block_start >= 0L ? \
18430Sstevel@tonic-gate 		(charf *)&s->window[(unsigned)s->block_start] : \
18440Sstevel@tonic-gate 		(charf *)Z_NULL), \
18450Sstevel@tonic-gate 		(ulg)((long)s->strstart - s->block_start), \
18460Sstevel@tonic-gate 		(eof)); \
18470Sstevel@tonic-gate 	s->block_start = s->strstart; \
18480Sstevel@tonic-gate 	flush_pending(s->strm); \
18490Sstevel@tonic-gate 	Tracev((stderr, "[FLUSH]")); \
18500Sstevel@tonic-gate }
18510Sstevel@tonic-gate 
18520Sstevel@tonic-gate /* Same but force premature exit if necessary. */
18530Sstevel@tonic-gate #define	FLUSH_BLOCK(s, eof) { \
18540Sstevel@tonic-gate 	FLUSH_BLOCK_ONLY(s, eof); \
18550Sstevel@tonic-gate 	if (s->strm->avail_out == 0) \
18560Sstevel@tonic-gate 		return ((eof) ? finish_started : need_more); \
18570Sstevel@tonic-gate }
18580Sstevel@tonic-gate 
18590Sstevel@tonic-gate /*
18600Sstevel@tonic-gate  * ===========================================================================
18610Sstevel@tonic-gate  * Copy without compression as much as possible from the input stream, return
18620Sstevel@tonic-gate  * the current block state.
18630Sstevel@tonic-gate  * This function does not insert new strings in the dictionary since
18640Sstevel@tonic-gate  * uncompressible data is probably not useful. This function is used
18650Sstevel@tonic-gate  * only for the level=0 compression option.
18660Sstevel@tonic-gate  * NOTE: this function should be optimized to avoid extra copying from
18670Sstevel@tonic-gate  * window to pending_buf.
18680Sstevel@tonic-gate  */
18690Sstevel@tonic-gate local block_state
deflate_stored(s,flush)18700Sstevel@tonic-gate deflate_stored(s, flush)
18710Sstevel@tonic-gate     deflate_state *s;
18720Sstevel@tonic-gate     int flush;
18730Sstevel@tonic-gate {
18740Sstevel@tonic-gate 	/*
18750Sstevel@tonic-gate 	 * Stored blocks are limited to 0xffff bytes, pending_buf is
18760Sstevel@tonic-gate 	 * limited to pending_buf_size, and each stored block has a 5
18770Sstevel@tonic-gate 	 * byte header:
18780Sstevel@tonic-gate 	 */
18790Sstevel@tonic-gate 	ulg max_block_size = 0xffff;
18800Sstevel@tonic-gate 	ulg max_start;
18810Sstevel@tonic-gate 
18820Sstevel@tonic-gate 	if (max_block_size > s->pending_buf_size - 5) {
18830Sstevel@tonic-gate 		max_block_size = s->pending_buf_size - 5;
18840Sstevel@tonic-gate 	}
18850Sstevel@tonic-gate 
18860Sstevel@tonic-gate 	/* Copy as much as possible from input to output: */
18870Sstevel@tonic-gate 	for (;;) {
18880Sstevel@tonic-gate 		/* Fill the window as much as possible: */
18890Sstevel@tonic-gate 		if (s->lookahead <= 1) {
18900Sstevel@tonic-gate 
18910Sstevel@tonic-gate 			Assert(s->strstart < s->w_size+MAX_DIST(s) ||
18920Sstevel@tonic-gate 			    s->block_start >= (long)s->w_size,
18930Sstevel@tonic-gate 			    "slide too late");
18940Sstevel@tonic-gate 
18950Sstevel@tonic-gate 			fill_window(s);
18960Sstevel@tonic-gate 			if (s->lookahead == 0 && flush == Z_NO_FLUSH)
18970Sstevel@tonic-gate 				return (need_more);
18980Sstevel@tonic-gate 
18990Sstevel@tonic-gate 			if (s->lookahead == 0)
19000Sstevel@tonic-gate 				break;	/* flush the current block */
19010Sstevel@tonic-gate 		}
19020Sstevel@tonic-gate 		Assert(s->block_start >= 0L, "block gone");
19030Sstevel@tonic-gate 
19040Sstevel@tonic-gate 		s->strstart += s->lookahead;
19050Sstevel@tonic-gate 		s->lookahead = 0;
19060Sstevel@tonic-gate 
19070Sstevel@tonic-gate 		/* Emit a stored block if pending_buf will be full: */
19080Sstevel@tonic-gate 		max_start = s->block_start + max_block_size;
19090Sstevel@tonic-gate 		if (s->strstart == 0 || (ulg)s->strstart >= max_start) {
19100Sstevel@tonic-gate 			/*
19110Sstevel@tonic-gate 			 * strstart == 0 is possible when wraparound
19120Sstevel@tonic-gate 			 * on 16-bit machine
19130Sstevel@tonic-gate 			 */
19140Sstevel@tonic-gate 			s->lookahead = (uInt)(s->strstart - max_start);
19150Sstevel@tonic-gate 			s->strstart = (uInt)max_start;
19160Sstevel@tonic-gate 			FLUSH_BLOCK(s, 0);
19170Sstevel@tonic-gate 		}
19180Sstevel@tonic-gate 		/*
19190Sstevel@tonic-gate 		 * Flush if we may have to slide, otherwise
19200Sstevel@tonic-gate 		 * block_start may become negative and the data will
19210Sstevel@tonic-gate 		 * be gone:
19220Sstevel@tonic-gate 		 */
19230Sstevel@tonic-gate 		if (s->strstart - (uInt)s->block_start >= MAX_DIST(s)) {
19240Sstevel@tonic-gate 			FLUSH_BLOCK(s, 0);
19250Sstevel@tonic-gate 		}
19260Sstevel@tonic-gate 	}
19270Sstevel@tonic-gate 	FLUSH_BLOCK(s, flush == Z_FINISH);
19280Sstevel@tonic-gate 	return (flush == Z_FINISH ? finish_done : block_done);
19290Sstevel@tonic-gate }
19300Sstevel@tonic-gate 
19310Sstevel@tonic-gate /*
19320Sstevel@tonic-gate  * ===========================================================================
19330Sstevel@tonic-gate  * Compress as much as possible from the input stream, return the current
19340Sstevel@tonic-gate  * block state.
19350Sstevel@tonic-gate  * This function does not perform lazy evaluation of matches and inserts
19360Sstevel@tonic-gate  * new strings in the dictionary only for unmatched strings or for short
19370Sstevel@tonic-gate  * matches. It is used only for the fast compression options.
19380Sstevel@tonic-gate  */
19390Sstevel@tonic-gate local block_state
deflate_fast(s,flush)19400Sstevel@tonic-gate deflate_fast(s, flush)
19410Sstevel@tonic-gate     deflate_state *s;
19420Sstevel@tonic-gate     int flush;
19430Sstevel@tonic-gate {
19440Sstevel@tonic-gate 	IPos hash_head = NIL;	/* head of the hash chain */
19450Sstevel@tonic-gate 	int bflush;	/* set if current block must be flushed */
19460Sstevel@tonic-gate 
19470Sstevel@tonic-gate 	for (;;) {
19480Sstevel@tonic-gate 		/*
19490Sstevel@tonic-gate 		 * Make sure that we always have enough lookahead,
19500Sstevel@tonic-gate 		 * except at the end of the input file. We need
19510Sstevel@tonic-gate 		 * MAX_MATCH bytes for the next match, plus MIN_MATCH
19520Sstevel@tonic-gate 		 * bytes to insert the string following the next
19530Sstevel@tonic-gate 		 * match.
19540Sstevel@tonic-gate 		 */
19550Sstevel@tonic-gate 		if (s->lookahead < MIN_LOOKAHEAD) {
19560Sstevel@tonic-gate 			fill_window(s);
19570Sstevel@tonic-gate 			if (s->lookahead < MIN_LOOKAHEAD &&
19580Sstevel@tonic-gate 			    flush == Z_NO_FLUSH) {
19590Sstevel@tonic-gate 				return (need_more);
19600Sstevel@tonic-gate 			}
19610Sstevel@tonic-gate 			if (s->lookahead == 0)
19620Sstevel@tonic-gate 				break;	/* flush the current block */
19630Sstevel@tonic-gate 		}
19640Sstevel@tonic-gate 
19650Sstevel@tonic-gate 		/*
19660Sstevel@tonic-gate 		 * Insert the string window[strstart .. strstart+2] in
19670Sstevel@tonic-gate 		 * the dictionary, and set hash_head to the head of
19680Sstevel@tonic-gate 		 * the hash chain:
19690Sstevel@tonic-gate 		 */
19700Sstevel@tonic-gate 		if (s->lookahead >= MIN_MATCH) {
19710Sstevel@tonic-gate 			INSERT_STRING(s, s->strstart, hash_head);
19720Sstevel@tonic-gate 		}
19730Sstevel@tonic-gate 
19740Sstevel@tonic-gate 		/*
19750Sstevel@tonic-gate 		 * Find the longest match, discarding those <=
19760Sstevel@tonic-gate 		 * prev_length.  At this point we have always
19770Sstevel@tonic-gate 		 * match_length < MIN_MATCH
19780Sstevel@tonic-gate 		 */
19790Sstevel@tonic-gate 		if (hash_head != NIL && s->strstart - hash_head <=
19800Sstevel@tonic-gate 		    MAX_DIST(s)) {
19810Sstevel@tonic-gate 			/*
19820Sstevel@tonic-gate 			 * To simplify the code, we prevent matches
19830Sstevel@tonic-gate 			 * with the string of window index 0 (in
19840Sstevel@tonic-gate 			 * particular we have to avoid a match of the
19850Sstevel@tonic-gate 			 * string with itself at the start of the
19860Sstevel@tonic-gate 			 * input file).
19870Sstevel@tonic-gate 			 */
19880Sstevel@tonic-gate 			if (s->strategy != Z_HUFFMAN_ONLY) {
19890Sstevel@tonic-gate 				s->match_length = longest_match(s, hash_head);
19900Sstevel@tonic-gate 			}
19910Sstevel@tonic-gate 			/* longest_match() sets match_start */
19920Sstevel@tonic-gate 		}
19930Sstevel@tonic-gate 		if (s->match_length >= MIN_MATCH) {
19940Sstevel@tonic-gate 			check_match(s, s->strstart, s->match_start,
19950Sstevel@tonic-gate 			    s->match_length);
19960Sstevel@tonic-gate 
19970Sstevel@tonic-gate 			_tr_tally_dist(s, s->strstart - s->match_start,
19980Sstevel@tonic-gate 			    s->match_length - MIN_MATCH, bflush);
19990Sstevel@tonic-gate 
20000Sstevel@tonic-gate 			s->lookahead -= s->match_length;
20010Sstevel@tonic-gate 
20020Sstevel@tonic-gate 			/*
20030Sstevel@tonic-gate 			 * Insert new strings in the hash table only
20040Sstevel@tonic-gate 			 * if the match length is not too large. This
20050Sstevel@tonic-gate 			 * saves time but degrades compression.
20060Sstevel@tonic-gate 			 */
20070Sstevel@tonic-gate #ifndef FASTEST
20080Sstevel@tonic-gate 			if (s->match_length <= s->max_insert_length &&
20090Sstevel@tonic-gate 			    s->lookahead >= MIN_MATCH) {
20100Sstevel@tonic-gate 				/* string at strstart already in hash table */
20110Sstevel@tonic-gate 				s->match_length--;
20120Sstevel@tonic-gate 				do {
20130Sstevel@tonic-gate 					s->strstart++;
20140Sstevel@tonic-gate 					INSERT_STRING(s, s->strstart,
20150Sstevel@tonic-gate 					    hash_head);
20160Sstevel@tonic-gate 					/*
20170Sstevel@tonic-gate 					 * strstart never exceeds
20180Sstevel@tonic-gate 					 * WSIZE-MAX_MATCH, so there
20190Sstevel@tonic-gate 					 * are always MIN_MATCH bytes
20200Sstevel@tonic-gate 					 * ahead.
20210Sstevel@tonic-gate 					 */
20220Sstevel@tonic-gate 				} while (--s->match_length != 0);
20230Sstevel@tonic-gate 				s->strstart++;
20240Sstevel@tonic-gate 			} else
20250Sstevel@tonic-gate #endif
20260Sstevel@tonic-gate 			{
20270Sstevel@tonic-gate 				s->strstart += s->match_length;
20280Sstevel@tonic-gate 				s->match_length = 0;
20290Sstevel@tonic-gate 				s->ins_h = s->window[s->strstart];
20300Sstevel@tonic-gate 				UPDATE_HASH(s, s->ins_h,
20310Sstevel@tonic-gate 				    s->window[s->strstart+1]);
20320Sstevel@tonic-gate #if MIN_MATCH != 3
20330Sstevel@tonic-gate 				Call UPDATE_HASH() MIN_MATCH-3 more times
20340Sstevel@tonic-gate #endif
20350Sstevel@tonic-gate 				/*
20360Sstevel@tonic-gate 				 * If lookahead < MIN_MATCH, ins_h is
20370Sstevel@tonic-gate 				 * garbage, but it does not matter
20380Sstevel@tonic-gate 				 * since it will be recomputed at next
20390Sstevel@tonic-gate 				 * deflate call.
20400Sstevel@tonic-gate 				 */
20410Sstevel@tonic-gate 			}
20420Sstevel@tonic-gate 		} else {
20430Sstevel@tonic-gate 			/* No match, output a literal byte */
20440Sstevel@tonic-gate 			Tracevv((stderr, "%c", s->window[s->strstart]));
20450Sstevel@tonic-gate 			_tr_tally_lit(s, s->window[s->strstart], bflush);
20460Sstevel@tonic-gate 			s->lookahead--;
20470Sstevel@tonic-gate 			s->strstart++;
20480Sstevel@tonic-gate 		}
20490Sstevel@tonic-gate 		if (bflush) FLUSH_BLOCK(s, 0);
20500Sstevel@tonic-gate 	}
20510Sstevel@tonic-gate 	FLUSH_BLOCK(s, flush == Z_FINISH);
20520Sstevel@tonic-gate 	return (flush == Z_FINISH ? finish_done : block_done);
20530Sstevel@tonic-gate }
20540Sstevel@tonic-gate 
20550Sstevel@tonic-gate /*
20560Sstevel@tonic-gate  * ===========================================================================
20570Sstevel@tonic-gate  * Same as above, but achieves better compression. We use a lazy
20580Sstevel@tonic-gate  * evaluation for matches: a match is finally adopted only if there is
20590Sstevel@tonic-gate  * no better match at the next window position.
20600Sstevel@tonic-gate  */
20610Sstevel@tonic-gate local block_state
deflate_slow(s,flush)20620Sstevel@tonic-gate deflate_slow(s, flush)
20630Sstevel@tonic-gate     deflate_state *s;
20640Sstevel@tonic-gate     int flush;
20650Sstevel@tonic-gate {
20660Sstevel@tonic-gate 	IPos hash_head = NIL;	/* head of hash chain */
20670Sstevel@tonic-gate 	int bflush;	/* set if current block must be flushed */
20680Sstevel@tonic-gate 
20690Sstevel@tonic-gate 	/* Process the input block. */
20700Sstevel@tonic-gate 	for (;;) {
20710Sstevel@tonic-gate 		/*
20720Sstevel@tonic-gate 		 * Make sure that we always have enough lookahead,
20730Sstevel@tonic-gate 		 * except at the end of the input file. We need
20740Sstevel@tonic-gate 		 * MAX_MATCH bytes for the next match, plus MIN_MATCH
20750Sstevel@tonic-gate 		 * bytes to insert the string following the next
20760Sstevel@tonic-gate 		 * match.
20770Sstevel@tonic-gate 		 */
20780Sstevel@tonic-gate 		if (s->lookahead < MIN_LOOKAHEAD) {
20790Sstevel@tonic-gate 			fill_window(s);
20800Sstevel@tonic-gate 			if (s->lookahead < MIN_LOOKAHEAD &&
20810Sstevel@tonic-gate 			    flush == Z_NO_FLUSH) {
20820Sstevel@tonic-gate 				return (need_more);
20830Sstevel@tonic-gate 			}
20840Sstevel@tonic-gate 			/* flush the current block */
20850Sstevel@tonic-gate 			if (s->lookahead == 0)
20860Sstevel@tonic-gate 				break;
20870Sstevel@tonic-gate 		}
20880Sstevel@tonic-gate 
20890Sstevel@tonic-gate 		/*
20900Sstevel@tonic-gate 		 * Insert the string window[strstart .. strstart+2] in
20910Sstevel@tonic-gate 		 * the dictionary, and set hash_head to the head of
20920Sstevel@tonic-gate 		 * the hash chain:
20930Sstevel@tonic-gate 		 */
20940Sstevel@tonic-gate 		if (s->lookahead >= MIN_MATCH) {
20950Sstevel@tonic-gate 			INSERT_STRING(s, s->strstart, hash_head);
20960Sstevel@tonic-gate 		}
20970Sstevel@tonic-gate 
20980Sstevel@tonic-gate 		/*
20990Sstevel@tonic-gate 		 * Find the longest match, discarding those <=
21000Sstevel@tonic-gate 		 * prev_length.
21010Sstevel@tonic-gate 		 */
21020Sstevel@tonic-gate 		s->prev_length = s->match_length;
21030Sstevel@tonic-gate 		s->prev_match = s->match_start;
21040Sstevel@tonic-gate 		s->match_length = MIN_MATCH-1;
21050Sstevel@tonic-gate 
21060Sstevel@tonic-gate 		if (hash_head != NIL && s->prev_length < s->max_lazy_match &&
21070Sstevel@tonic-gate 		    s->strstart - hash_head <= MAX_DIST(s)) {
21080Sstevel@tonic-gate 			/*
21090Sstevel@tonic-gate 			 * To simplify the code, we prevent matches
21100Sstevel@tonic-gate 			 * with the string of window index 0 (in
21110Sstevel@tonic-gate 			 * particular we have to avoid a match of the
21120Sstevel@tonic-gate 			 * string with itself at the start of the
21130Sstevel@tonic-gate 			 * input file).
21140Sstevel@tonic-gate 			 */
21150Sstevel@tonic-gate 			if (s->strategy != Z_HUFFMAN_ONLY) {
21160Sstevel@tonic-gate 				s->match_length = longest_match(s, hash_head);
21170Sstevel@tonic-gate 			}
21180Sstevel@tonic-gate 			/* longest_match() sets match_start */
21190Sstevel@tonic-gate 
21200Sstevel@tonic-gate 			if (s->match_length <= 5 &&
21210Sstevel@tonic-gate 			    (s->strategy == Z_FILTERED ||
21220Sstevel@tonic-gate 				(s->match_length == MIN_MATCH &&
21230Sstevel@tonic-gate 				    s->strstart - s->match_start > TOO_FAR))) {
21240Sstevel@tonic-gate 
21250Sstevel@tonic-gate 				/*
21260Sstevel@tonic-gate 				 * If prev_match is also MIN_MATCH,
21270Sstevel@tonic-gate 				 * match_start is garbage but we will
21280Sstevel@tonic-gate 				 * ignore the current match anyway.
21290Sstevel@tonic-gate 				 */
21300Sstevel@tonic-gate 				s->match_length = MIN_MATCH-1;
21310Sstevel@tonic-gate 			}
21320Sstevel@tonic-gate 		}
21330Sstevel@tonic-gate 		/*
21340Sstevel@tonic-gate 		 * If there was a match at the previous step and the
21350Sstevel@tonic-gate 		 * current match is not better, output the previous
21360Sstevel@tonic-gate 		 * match:
21370Sstevel@tonic-gate 		 */
21380Sstevel@tonic-gate 		if (s->prev_length >= MIN_MATCH &&
21390Sstevel@tonic-gate 		    s->match_length <= s->prev_length) {
21400Sstevel@tonic-gate 			uInt max_insert = s->strstart + s->lookahead -
21410Sstevel@tonic-gate 			    MIN_MATCH;
21420Sstevel@tonic-gate 			/* Do not insert strings in hash table beyond this. */
21430Sstevel@tonic-gate 
21440Sstevel@tonic-gate 			check_match(s, s->strstart-1, s->prev_match,
21450Sstevel@tonic-gate 			    s->prev_length);
21460Sstevel@tonic-gate 
21470Sstevel@tonic-gate 			_tr_tally_dist(s, s->strstart -1 - s->prev_match,
21480Sstevel@tonic-gate 			    s->prev_length - MIN_MATCH, bflush);
21490Sstevel@tonic-gate 
21500Sstevel@tonic-gate 			/*
21510Sstevel@tonic-gate 			 * Insert in hash table all strings up to the
21520Sstevel@tonic-gate 			 * end of the match.  strstart-1 and strstart
21530Sstevel@tonic-gate 			 * are already inserted. If there is not
21540Sstevel@tonic-gate 			 * enough lookahead, the last two strings are
21550Sstevel@tonic-gate 			 * not inserted in the hash table.
21560Sstevel@tonic-gate 			 */
21570Sstevel@tonic-gate 			s->lookahead -= s->prev_length-1;
21580Sstevel@tonic-gate 			s->prev_length -= 2;
21590Sstevel@tonic-gate 			do {
21600Sstevel@tonic-gate 				if (++s->strstart <= max_insert) {
21610Sstevel@tonic-gate 					INSERT_STRING(s, s->strstart,
21620Sstevel@tonic-gate 					    hash_head);
21630Sstevel@tonic-gate 				}
21640Sstevel@tonic-gate 			} while (--s->prev_length != 0);
21650Sstevel@tonic-gate 			s->match_available = 0;
21660Sstevel@tonic-gate 			s->match_length = MIN_MATCH-1;
21670Sstevel@tonic-gate 			s->strstart++;
21680Sstevel@tonic-gate 
21690Sstevel@tonic-gate 			if (bflush) FLUSH_BLOCK(s, 0);
21700Sstevel@tonic-gate 
21710Sstevel@tonic-gate 		} else if (s->match_available) {
21720Sstevel@tonic-gate 			/*
21730Sstevel@tonic-gate 			 * If there was no match at the previous
21740Sstevel@tonic-gate 			 * position, output a single literal. If there
21750Sstevel@tonic-gate 			 * was a match but the current match is
21760Sstevel@tonic-gate 			 * longer, truncate the previous match to a
21770Sstevel@tonic-gate 			 * single literal.
21780Sstevel@tonic-gate 			 */
21790Sstevel@tonic-gate 			Tracevv((stderr, "%c", s->window[s->strstart-1]));
21800Sstevel@tonic-gate 			_tr_tally_lit(s, s->window[s->strstart-1], bflush);
21810Sstevel@tonic-gate 			if (bflush) {
21820Sstevel@tonic-gate 				FLUSH_BLOCK_ONLY(s, 0);
21830Sstevel@tonic-gate 			}
21840Sstevel@tonic-gate 			s->strstart++;
21850Sstevel@tonic-gate 			s->lookahead--;
21860Sstevel@tonic-gate 			if (s->strm->avail_out == 0)
21870Sstevel@tonic-gate 				return (need_more);
21880Sstevel@tonic-gate 		} else {
21890Sstevel@tonic-gate 			/*
21900Sstevel@tonic-gate 			 * There is no previous match to compare with,
21910Sstevel@tonic-gate 			 * wait for the next step to decide.
21920Sstevel@tonic-gate 			 */
21930Sstevel@tonic-gate 			s->match_available = 1;
21940Sstevel@tonic-gate 			s->strstart++;
21950Sstevel@tonic-gate 			s->lookahead--;
21960Sstevel@tonic-gate 		}
21970Sstevel@tonic-gate 	}
21980Sstevel@tonic-gate 	Assert(flush != Z_NO_FLUSH, "no flush?");
21990Sstevel@tonic-gate 	if (s->match_available) {
22000Sstevel@tonic-gate 		Tracevv((stderr, "%c", s->window[s->strstart-1]));
22010Sstevel@tonic-gate 		_tr_tally_lit(s, s->window[s->strstart-1], bflush);
22020Sstevel@tonic-gate 		s->match_available = 0;
22030Sstevel@tonic-gate 	}
22040Sstevel@tonic-gate 	FLUSH_BLOCK(s, flush == Z_FINISH);
22050Sstevel@tonic-gate 	return (flush == Z_FINISH ? finish_done : block_done);
22060Sstevel@tonic-gate }
22070Sstevel@tonic-gate /* --- deflate.c */
22080Sstevel@tonic-gate 
22090Sstevel@tonic-gate /* +++ trees.c */
22100Sstevel@tonic-gate /*
22110Sstevel@tonic-gate  * trees.c -- output deflated data using Huffman coding
22120Sstevel@tonic-gate  * Copyright (C) 1995-1998 Jean-loup Gailly
22130Sstevel@tonic-gate  * For conditions of distribution and use, see copyright notice in zlib.h
22140Sstevel@tonic-gate  */
22150Sstevel@tonic-gate 
22160Sstevel@tonic-gate /*
22170Sstevel@tonic-gate  *  ALGORITHM
22180Sstevel@tonic-gate  *
22190Sstevel@tonic-gate  *      The "deflation" process uses several Huffman trees. The more
22200Sstevel@tonic-gate  *      common source values are represented by shorter bit sequences.
22210Sstevel@tonic-gate  *
22220Sstevel@tonic-gate  *      Each code tree is stored in a compressed form which is itself
22230Sstevel@tonic-gate  * a Huffman encoding of the lengths of all the code strings (in
22240Sstevel@tonic-gate  * ascending order by source values).  The actual code strings are
22250Sstevel@tonic-gate  * reconstructed from the lengths in the inflate process, as described
22260Sstevel@tonic-gate  * in the deflate specification.
22270Sstevel@tonic-gate  *
22280Sstevel@tonic-gate  *  REFERENCES
22290Sstevel@tonic-gate  *
22300Sstevel@tonic-gate  *      Deutsch, L.P.,"'Deflate' Compressed Data Format Specification".
22310Sstevel@tonic-gate  *      Available in ftp.uu.net:/pub/archiving/zip/doc/deflate-1.1.doc
22320Sstevel@tonic-gate  *
22330Sstevel@tonic-gate  *      Storer, James A.
22340Sstevel@tonic-gate  *          Data Compression:  Methods and Theory, pp. 49-50.
22350Sstevel@tonic-gate  *          Computer Science Press, 1988.  ISBN 0-7167-8156-5.
22360Sstevel@tonic-gate  *
22370Sstevel@tonic-gate  *      Sedgewick, R.
22380Sstevel@tonic-gate  *          Algorithms, p290.
22390Sstevel@tonic-gate  *          Addison-Wesley, 1983. ISBN 0-201-06672-6.
22400Sstevel@tonic-gate  */
22410Sstevel@tonic-gate 
22420Sstevel@tonic-gate /* From: trees.c,v 1.11 1996/07/24 13:41:06 me Exp $ */
22430Sstevel@tonic-gate 
22440Sstevel@tonic-gate /* #include "deflate.h" */
22450Sstevel@tonic-gate 
22460Sstevel@tonic-gate #ifdef DEBUG_ZLIB
22470Sstevel@tonic-gate #include <ctype.h>
22480Sstevel@tonic-gate #endif
22490Sstevel@tonic-gate 
22500Sstevel@tonic-gate /*
22510Sstevel@tonic-gate  * ===========================================================================
22520Sstevel@tonic-gate  * Constants
22530Sstevel@tonic-gate  */
22540Sstevel@tonic-gate 
22550Sstevel@tonic-gate #define	MAX_BL_BITS 7
22560Sstevel@tonic-gate /* Bit length codes must not exceed MAX_BL_BITS bits */
22570Sstevel@tonic-gate 
22580Sstevel@tonic-gate #define	END_BLOCK 256
22590Sstevel@tonic-gate /* end of block literal code */
22600Sstevel@tonic-gate 
22610Sstevel@tonic-gate #define	REP_3_6		16
22620Sstevel@tonic-gate /* repeat previous bit length 3-6 times (2 bits of repeat count) */
22630Sstevel@tonic-gate 
22640Sstevel@tonic-gate #define	REPZ_3_10	17
22650Sstevel@tonic-gate /* repeat a zero length 3-10 times  (3 bits of repeat count) */
22660Sstevel@tonic-gate 
22670Sstevel@tonic-gate #define	REPZ_11_138	18
22680Sstevel@tonic-gate /* repeat a zero length 11-138 times  (7 bits of repeat count) */
22690Sstevel@tonic-gate 
22700Sstevel@tonic-gate /* extra bits for each length code */
22710Sstevel@tonic-gate local const int extra_lbits[LENGTH_CODES] = {
22720Sstevel@tonic-gate 	0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4,
22730Sstevel@tonic-gate 	4, 4, 4, 5, 5, 5, 5, 0};
22740Sstevel@tonic-gate 
22750Sstevel@tonic-gate /* extra bits for each distance code */
22760Sstevel@tonic-gate local const int extra_dbits[D_CODES] = {
22770Sstevel@tonic-gate 	0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9,
22780Sstevel@tonic-gate 	9, 10, 10, 11, 11, 12, 12, 13, 13};
22790Sstevel@tonic-gate 
22800Sstevel@tonic-gate /* extra bits for each bit length code */
22810Sstevel@tonic-gate local const int extra_blbits[BL_CODES] = {
22820Sstevel@tonic-gate 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 3, 7};
22830Sstevel@tonic-gate 
22840Sstevel@tonic-gate local const uch bl_order[BL_CODES] = {
22850Sstevel@tonic-gate 	16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
22860Sstevel@tonic-gate 
22870Sstevel@tonic-gate /*
22880Sstevel@tonic-gate  * The lengths of the bit length codes are sent in order of decreasing
22890Sstevel@tonic-gate  * probability, to avoid transmitting the lengths for unused bit
22900Sstevel@tonic-gate  * length codes.
22910Sstevel@tonic-gate  */
22920Sstevel@tonic-gate 
22930Sstevel@tonic-gate #define	Buf_size (8 * 2*sizeof (char))
22940Sstevel@tonic-gate /*
22950Sstevel@tonic-gate  * Number of bits used within bi_buf. (bi_buf might be implemented on
22960Sstevel@tonic-gate  * more than 16 bits on some systems.)
22970Sstevel@tonic-gate  */
22980Sstevel@tonic-gate 
22990Sstevel@tonic-gate /*
23000Sstevel@tonic-gate  * ===========================================================================
23010Sstevel@tonic-gate  * Local data. These are initialized only once.
23020Sstevel@tonic-gate  */
23030Sstevel@tonic-gate #define	DIST_CODE_LEN  512 /* see definition of array dist_code below */
23040Sstevel@tonic-gate 
23050Sstevel@tonic-gate local ct_data static_ltree[L_CODES+2];
23060Sstevel@tonic-gate /*
23070Sstevel@tonic-gate  * The static literal tree. Since the bit lengths are imposed, there
23080Sstevel@tonic-gate  * is no need for the L_CODES extra codes used during heap
23090Sstevel@tonic-gate  * construction. However The codes 286 and 287 are needed to build a
23100Sstevel@tonic-gate  * canonical tree (see _tr_init below).
23110Sstevel@tonic-gate  */
23120Sstevel@tonic-gate 
23130Sstevel@tonic-gate local ct_data static_dtree[D_CODES];
23140Sstevel@tonic-gate /*
23150Sstevel@tonic-gate  * The static distance tree. (Actually a trivial tree since all codes
23160Sstevel@tonic-gate  * use 5 bits.)
23170Sstevel@tonic-gate  */
23180Sstevel@tonic-gate 
23190Sstevel@tonic-gate local uch _dist_code[512];
23200Sstevel@tonic-gate /*
23210Sstevel@tonic-gate  * distance codes. The first 256 values correspond to the distances 3
23220Sstevel@tonic-gate  * .. 258, the last 256 values correspond to the top 8 bits of the 15
23230Sstevel@tonic-gate  * bit distances.
23240Sstevel@tonic-gate  */
23250Sstevel@tonic-gate 
23260Sstevel@tonic-gate local uch _length_code[MAX_MATCH-MIN_MATCH+1];
23270Sstevel@tonic-gate /* length code for each normalized match length (0 == MIN_MATCH) */
23280Sstevel@tonic-gate 
23290Sstevel@tonic-gate local int base_length[LENGTH_CODES];
23300Sstevel@tonic-gate /* First normalized length for each code (0 = MIN_MATCH) */
23310Sstevel@tonic-gate 
23320Sstevel@tonic-gate local int base_dist[D_CODES];
23330Sstevel@tonic-gate /* First normalized distance for each code (0 = distance of 1) */
23340Sstevel@tonic-gate 
23350Sstevel@tonic-gate struct static_tree_desc_s {
23360Sstevel@tonic-gate 	const ct_data *static_tree;	/* static tree or NULL */
23370Sstevel@tonic-gate 	const intf    *extra_bits;	/* extra bits for each code or NULL */
23380Sstevel@tonic-gate 	int	extra_base;	/* base index for extra_bits */
23390Sstevel@tonic-gate 	int	elems;	/* max number of elements in the tree */
23400Sstevel@tonic-gate 	int	max_length;	/* max bit length for the codes */
23410Sstevel@tonic-gate };
23420Sstevel@tonic-gate 
23430Sstevel@tonic-gate local static_tree_desc  static_l_desc = {
23440Sstevel@tonic-gate 	static_ltree, extra_lbits, LITERALS+1,	L_CODES, MAX_BITS};
23450Sstevel@tonic-gate 
23460Sstevel@tonic-gate local static_tree_desc  static_d_desc = {
23470Sstevel@tonic-gate 	static_dtree, extra_dbits, 0,		D_CODES, MAX_BITS};
23480Sstevel@tonic-gate 
23490Sstevel@tonic-gate local static_tree_desc  static_bl_desc = {
23500Sstevel@tonic-gate 	(const ct_data *)0, extra_blbits, 0,		BL_CODES, MAX_BL_BITS};
23510Sstevel@tonic-gate 
23520Sstevel@tonic-gate /*
23530Sstevel@tonic-gate  * ===========================================================================
23540Sstevel@tonic-gate  * Local (static) routines in this file.
23550Sstevel@tonic-gate  */
23560Sstevel@tonic-gate 
23570Sstevel@tonic-gate local void tr_static_init OF((void));
23580Sstevel@tonic-gate local void init_block	OF((deflate_state *s));
23590Sstevel@tonic-gate local void pqdownheap	OF((deflate_state *s, ct_data *tree, int k));
23600Sstevel@tonic-gate local void gen_bitlen	OF((deflate_state *s, tree_desc *desc));
23610Sstevel@tonic-gate local void gen_codes	OF((ct_data *tree, int max_code, ushf *bl_count));
23620Sstevel@tonic-gate local void build_tree	OF((deflate_state *s, tree_desc *desc));
23630Sstevel@tonic-gate local void scan_tree	OF((deflate_state *s, ct_data *tree, int max_code));
23640Sstevel@tonic-gate local void send_tree	OF((deflate_state *s, ct_data *tree, int max_code));
23650Sstevel@tonic-gate local int  build_bl_tree	OF((deflate_state *s));
23660Sstevel@tonic-gate local void send_all_trees	OF((deflate_state *s, int lcodes, int dcodes,
23670Sstevel@tonic-gate     int blcodes));
23680Sstevel@tonic-gate local void compress_block OF((deflate_state *s, ct_data *ltree,
23690Sstevel@tonic-gate     ct_data *dtree));
23700Sstevel@tonic-gate local void set_data_type	OF((deflate_state *s));
23710Sstevel@tonic-gate local unsigned bi_reverse	OF((unsigned value, int length));
23720Sstevel@tonic-gate local void bi_windup	OF((deflate_state *s));
23730Sstevel@tonic-gate local void bi_flush	OF((deflate_state *s));
23740Sstevel@tonic-gate local void copy_block	OF((deflate_state *s, charf *buf, unsigned len,
23750Sstevel@tonic-gate     int header));
23760Sstevel@tonic-gate 
23770Sstevel@tonic-gate #ifndef DEBUG_ZLIB
23780Sstevel@tonic-gate #define	send_code(s, c, tree) send_bits(s, tree[c].Code, tree[c].Len)
23790Sstevel@tonic-gate /* Send a code of the given tree. c and tree must not have side effects */
23800Sstevel@tonic-gate 
23810Sstevel@tonic-gate #else /* DEBUG_ZLIB */
23820Sstevel@tonic-gate #define	send_code(s, c, tree) \
23830Sstevel@tonic-gate 	{ if (z_verbose > 2) fprintf(stderr, "\ncd %3d ", (c)); \
23840Sstevel@tonic-gate 	send_bits(s, tree[c].Code, tree[c].Len); }
23850Sstevel@tonic-gate #endif
23860Sstevel@tonic-gate 
23870Sstevel@tonic-gate /*
23880Sstevel@tonic-gate  * ===========================================================================
23890Sstevel@tonic-gate  * Output a short LSB first on the stream.
23900Sstevel@tonic-gate  * IN assertion: there is enough room in pendingBuf.
23910Sstevel@tonic-gate  */
23920Sstevel@tonic-gate #define	put_short(s, w) { \
23930Sstevel@tonic-gate 	put_byte(s, (uch)((w) & 0xff)); \
23940Sstevel@tonic-gate 	put_byte(s, (uch)((ush)(w) >> 8)); \
23950Sstevel@tonic-gate }
23960Sstevel@tonic-gate 
23970Sstevel@tonic-gate /*
23980Sstevel@tonic-gate  * ===========================================================================
23990Sstevel@tonic-gate  * Send a value on a given number of bits.
24000Sstevel@tonic-gate  * IN assertion: length <= 16 and value fits in length bits.
24010Sstevel@tonic-gate  */
24020Sstevel@tonic-gate #ifdef DEBUG_ZLIB
24030Sstevel@tonic-gate local void send_bits	OF((deflate_state *s, int value, int length));
24040Sstevel@tonic-gate 
24050Sstevel@tonic-gate local void
send_bits(s,value,length)24060Sstevel@tonic-gate send_bits(s, value, length)
24070Sstevel@tonic-gate     deflate_state *s;
24080Sstevel@tonic-gate     int value;	/* value to send */
24090Sstevel@tonic-gate     int length;	/* number of bits */
24100Sstevel@tonic-gate {
24110Sstevel@tonic-gate 	Tracevv((stderr, " l %2d v %4x ", length, value));
24120Sstevel@tonic-gate 	Assert(length > 0 && length <= 15, "invalid length");
24130Sstevel@tonic-gate 	s->bits_sent += (ulg)length;
24140Sstevel@tonic-gate 
24150Sstevel@tonic-gate 	/*
24160Sstevel@tonic-gate 	 * If not enough room in bi_buf, use (valid) bits from bi_buf
24170Sstevel@tonic-gate 	 * and (16 - bi_valid) bits from value, leaving (width -
24180Sstevel@tonic-gate 	 * (16-bi_valid)) unused bits in value.
24190Sstevel@tonic-gate 	 */
24200Sstevel@tonic-gate 	if (s->bi_valid > (int)Buf_size - length) {
24210Sstevel@tonic-gate 		s->bi_buf |= (value << s->bi_valid);
24220Sstevel@tonic-gate 		put_short(s, s->bi_buf);
24230Sstevel@tonic-gate 		s->bi_buf = (ush)value >> (Buf_size - s->bi_valid);
24240Sstevel@tonic-gate 		s->bi_valid += length - Buf_size;
24250Sstevel@tonic-gate 	} else {
24260Sstevel@tonic-gate 		s->bi_buf |= value << s->bi_valid;
24270Sstevel@tonic-gate 		s->bi_valid += length;
24280Sstevel@tonic-gate 	}
24290Sstevel@tonic-gate }
24300Sstevel@tonic-gate #else /* !DEBUG_ZLIB */
24310Sstevel@tonic-gate 
24320Sstevel@tonic-gate #define	send_bits(s, value, length) \
24330Sstevel@tonic-gate {	int len = length; \
24340Sstevel@tonic-gate 	if (s->bi_valid > (int)Buf_size - len) {\
24350Sstevel@tonic-gate 		int val = value; \
24360Sstevel@tonic-gate 		s->bi_buf |= (val << s->bi_valid); \
24370Sstevel@tonic-gate 		put_short(s, s->bi_buf); \
24380Sstevel@tonic-gate 		s->bi_buf = (ush)val >> (Buf_size - s->bi_valid); \
24390Sstevel@tonic-gate 		s->bi_valid += len - Buf_size; \
24400Sstevel@tonic-gate 	} else {\
24410Sstevel@tonic-gate 		s->bi_buf |= (value) << s->bi_valid; \
24420Sstevel@tonic-gate 		s->bi_valid += len; \
24430Sstevel@tonic-gate 	}\
24440Sstevel@tonic-gate }
24450Sstevel@tonic-gate #endif /* DEBUG_ZLIB */
24460Sstevel@tonic-gate 
24470Sstevel@tonic-gate 
24480Sstevel@tonic-gate #define	MAX(a, b) (a >= b ? a : b)
24490Sstevel@tonic-gate /* the arguments must not have side effects */
24500Sstevel@tonic-gate 
24510Sstevel@tonic-gate /*
24520Sstevel@tonic-gate  * ===========================================================================
24530Sstevel@tonic-gate  * Initialize the various 'constant' tables. In a multi-threaded environment,
24540Sstevel@tonic-gate  * this function may be called by two threads concurrently, but this is
24550Sstevel@tonic-gate  * harmless since both invocations do exactly the same thing.
24560Sstevel@tonic-gate  */
24570Sstevel@tonic-gate local void
tr_static_init()24580Sstevel@tonic-gate tr_static_init()
24590Sstevel@tonic-gate {
24600Sstevel@tonic-gate 	static int static_init_done = 0;
24610Sstevel@tonic-gate 	int n;	/* iterates over tree elements */
24620Sstevel@tonic-gate 	int bits;	/* bit counter */
24630Sstevel@tonic-gate 	int length;	/* length value */
24640Sstevel@tonic-gate 	int code;	/* code value */
24650Sstevel@tonic-gate 	int dist;	/* distance index */
24660Sstevel@tonic-gate 	ush bl_count[MAX_BITS+1];
24670Sstevel@tonic-gate 	/* number of codes at each bit length for an optimal tree */
24680Sstevel@tonic-gate 
24690Sstevel@tonic-gate 	if (static_init_done)
24700Sstevel@tonic-gate 		return;
24710Sstevel@tonic-gate 
24720Sstevel@tonic-gate 	/* For some embedded targets, global variables are not initialized: */
24730Sstevel@tonic-gate 	static_l_desc.static_tree = static_ltree;
24740Sstevel@tonic-gate 	static_l_desc.extra_bits = extra_lbits;
24750Sstevel@tonic-gate 	static_d_desc.static_tree = static_dtree;
24760Sstevel@tonic-gate 	static_d_desc.extra_bits = extra_dbits;
24770Sstevel@tonic-gate 	static_bl_desc.extra_bits = extra_blbits;
24780Sstevel@tonic-gate 
24790Sstevel@tonic-gate 	/* Initialize the mapping length (0..255) -> length code (0..28) */
24800Sstevel@tonic-gate 	length = 0;
24810Sstevel@tonic-gate 	for (code = 0; code < LENGTH_CODES-1; code++) {
24820Sstevel@tonic-gate 		base_length[code] = length;
24830Sstevel@tonic-gate 		for (n = 0; n < (1<<extra_lbits[code]); n++) {
24840Sstevel@tonic-gate 			_length_code[length++] = (uch)code;
24850Sstevel@tonic-gate 		}
24860Sstevel@tonic-gate 	}
24870Sstevel@tonic-gate 	Assert(length == 256, "tr_static_init: length != 256");
24880Sstevel@tonic-gate 	/*
24890Sstevel@tonic-gate 	 * Note that the length 255 (match length 258) can be
24900Sstevel@tonic-gate 	 * represented in two different ways: code 284 + 5 bits or
24910Sstevel@tonic-gate 	 * code 285, so we overwrite _length_code[255] to use the best
24920Sstevel@tonic-gate 	 * encoding:
24930Sstevel@tonic-gate 	 */
24940Sstevel@tonic-gate 	_length_code[length-1] = (uch)code;
24950Sstevel@tonic-gate 
24960Sstevel@tonic-gate 	/* Initialize the mapping dist (0..32K) -> dist code (0..29) */
24970Sstevel@tonic-gate 	dist = 0;
24980Sstevel@tonic-gate 	for (code = 0; code < 16; code++) {
24990Sstevel@tonic-gate 		base_dist[code] = dist;
25000Sstevel@tonic-gate 		for (n = 0; n < (1<<extra_dbits[code]); n++) {
25010Sstevel@tonic-gate 			_dist_code[dist++] = (uch)code;
25020Sstevel@tonic-gate 		}
25030Sstevel@tonic-gate 	}
25040Sstevel@tonic-gate 	Assert(dist == 256, "tr_static_init: dist != 256");
25050Sstevel@tonic-gate 	dist >>= 7;	/* from now on, all distances are divided by 128 */
25060Sstevel@tonic-gate 	for (; code < D_CODES; code++) {
25070Sstevel@tonic-gate 		base_dist[code] = dist << 7;
25080Sstevel@tonic-gate 		for (n = 0; n < (1<<(extra_dbits[code]-7)); n++) {
25090Sstevel@tonic-gate 			_dist_code[256 + dist++] = (uch)code;
25100Sstevel@tonic-gate 		}
25110Sstevel@tonic-gate 	}
25120Sstevel@tonic-gate 	Assert(dist == 256, "tr_static_init: 256+dist != 512");
25130Sstevel@tonic-gate 
25140Sstevel@tonic-gate 	/* Construct the codes of the static literal tree */
25150Sstevel@tonic-gate 	for (bits = 0; bits <= MAX_BITS; bits++) bl_count[bits] = 0;
25160Sstevel@tonic-gate 	n = 0;
25170Sstevel@tonic-gate 	while (n <= 143) static_ltree[n++].Len = 8, bl_count[8]++;
25180Sstevel@tonic-gate 	while (n <= 255) static_ltree[n++].Len = 9, bl_count[9]++;
25190Sstevel@tonic-gate 	while (n <= 279) static_ltree[n++].Len = 7, bl_count[7]++;
25200Sstevel@tonic-gate 	while (n <= 287) static_ltree[n++].Len = 8, bl_count[8]++;
25210Sstevel@tonic-gate 	/*
25220Sstevel@tonic-gate 	 * Codes 286 and 287 do not exist, but we must include them in the
25230Sstevel@tonic-gate 	 * tree construction to get a canonical Huffman tree (longest code
25240Sstevel@tonic-gate 	 * all ones)
25250Sstevel@tonic-gate 	 */
25260Sstevel@tonic-gate 	gen_codes((ct_data *)static_ltree, L_CODES+1, bl_count);
25270Sstevel@tonic-gate 
25280Sstevel@tonic-gate 	/* The static distance tree is trivial: */
25290Sstevel@tonic-gate 	for (n = 0; n < D_CODES; n++) {
25300Sstevel@tonic-gate 		static_dtree[n].Len = 5;
25310Sstevel@tonic-gate 		static_dtree[n].Code = bi_reverse((unsigned)n, 5);
25320Sstevel@tonic-gate 	}
25330Sstevel@tonic-gate 	static_init_done = 1;
25340Sstevel@tonic-gate }
25350Sstevel@tonic-gate 
25360Sstevel@tonic-gate /*
25370Sstevel@tonic-gate  * ===========================================================================
25380Sstevel@tonic-gate  * Initialize the tree data structures for a new zlib stream.
25390Sstevel@tonic-gate  */
25400Sstevel@tonic-gate void
_tr_init(s)25410Sstevel@tonic-gate _tr_init(s)
25420Sstevel@tonic-gate     deflate_state *s;
25430Sstevel@tonic-gate {
25440Sstevel@tonic-gate 	tr_static_init();
25450Sstevel@tonic-gate 
25460Sstevel@tonic-gate 	s->l_desc.dyn_tree = s->dyn_ltree;
25470Sstevel@tonic-gate 	s->l_desc.stat_desc = &static_l_desc;
25480Sstevel@tonic-gate 
25490Sstevel@tonic-gate 	s->d_desc.dyn_tree = s->dyn_dtree;
25500Sstevel@tonic-gate 	s->d_desc.stat_desc = &static_d_desc;
25510Sstevel@tonic-gate 
25520Sstevel@tonic-gate 	s->bl_desc.dyn_tree = s->bl_tree;
25530Sstevel@tonic-gate 	s->bl_desc.stat_desc = &static_bl_desc;
25540Sstevel@tonic-gate 
25550Sstevel@tonic-gate 	s->bi_buf = 0;
25560Sstevel@tonic-gate 	s->bi_valid = 0;
25570Sstevel@tonic-gate 	s->last_eob_len = 8;	/* enough lookahead for inflate */
25580Sstevel@tonic-gate 	s->compressed_len = 0L;		/* PPP */
25590Sstevel@tonic-gate #ifdef DEBUG_ZLIB
25600Sstevel@tonic-gate 	s->bits_sent = 0L;
25610Sstevel@tonic-gate #endif
25620Sstevel@tonic-gate 
25630Sstevel@tonic-gate 	/* Initialize the first block of the first file: */
25640Sstevel@tonic-gate 	init_block(s);
25650Sstevel@tonic-gate }
25660Sstevel@tonic-gate 
25670Sstevel@tonic-gate /*
25680Sstevel@tonic-gate  * ===========================================================================
25690Sstevel@tonic-gate  * Initialize a new block.
25700Sstevel@tonic-gate  */
25710Sstevel@tonic-gate local void
init_block(s)25720Sstevel@tonic-gate init_block(s)
25730Sstevel@tonic-gate     deflate_state *s;
25740Sstevel@tonic-gate {
25750Sstevel@tonic-gate 	int n;	/* iterates over tree elements */
25760Sstevel@tonic-gate 
25770Sstevel@tonic-gate 	/* Initialize the trees. */
25780Sstevel@tonic-gate 	for (n = 0; n < L_CODES;  n++) s->dyn_ltree[n].Freq = 0;
25790Sstevel@tonic-gate 	for (n = 0; n < D_CODES;  n++) s->dyn_dtree[n].Freq = 0;
25800Sstevel@tonic-gate 	for (n = 0; n < BL_CODES; n++) s->bl_tree[n].Freq = 0;
25810Sstevel@tonic-gate 
25820Sstevel@tonic-gate 	s->dyn_ltree[END_BLOCK].Freq = 1;
25830Sstevel@tonic-gate 	s->opt_len = s->static_len = 0L;
25840Sstevel@tonic-gate 	s->last_lit = s->matches = 0;
25850Sstevel@tonic-gate }
25860Sstevel@tonic-gate 
25870Sstevel@tonic-gate #define	SMALLEST 1
25880Sstevel@tonic-gate /* Index within the heap array of least frequent node in the Huffman tree */
25890Sstevel@tonic-gate 
25900Sstevel@tonic-gate 
25910Sstevel@tonic-gate /*
25920Sstevel@tonic-gate  * ===========================================================================
25930Sstevel@tonic-gate  * Remove the smallest element from the heap and recreate the heap with
25940Sstevel@tonic-gate  * one less element. Updates heap and heap_len.
25950Sstevel@tonic-gate  */
25960Sstevel@tonic-gate #define	pqremove(s, tree, top) \
25970Sstevel@tonic-gate {\
25980Sstevel@tonic-gate 	top = s->heap[SMALLEST]; \
25990Sstevel@tonic-gate 	s->heap[SMALLEST] = s->heap[s->heap_len--]; \
26000Sstevel@tonic-gate 	pqdownheap(s, tree, SMALLEST); \
26010Sstevel@tonic-gate }
26020Sstevel@tonic-gate 
26030Sstevel@tonic-gate /*
26040Sstevel@tonic-gate  * ===========================================================================
26050Sstevel@tonic-gate  * Compares to subtrees, using the tree depth as tie breaker when
26060Sstevel@tonic-gate  * the subtrees have equal frequency. This minimizes the worst case length.
26070Sstevel@tonic-gate  */
26080Sstevel@tonic-gate #define	smaller(tree, n, m, depth) \
26090Sstevel@tonic-gate 	(tree[n].Freq < tree[m].Freq || \
26100Sstevel@tonic-gate 	(tree[n].Freq == tree[m].Freq && depth[n] <= depth[m]))
26110Sstevel@tonic-gate /*
26120Sstevel@tonic-gate  * ===========================================================================
26130Sstevel@tonic-gate  * Restore the heap property by moving down the tree starting at node k,
26140Sstevel@tonic-gate  * exchanging a node with the smallest of its two sons if necessary, stopping
26150Sstevel@tonic-gate  * when the heap property is re-established (each father smaller than its
26160Sstevel@tonic-gate  * two sons).
26170Sstevel@tonic-gate  */
26180Sstevel@tonic-gate local void
pqdownheap(s,tree,k)26190Sstevel@tonic-gate pqdownheap(s, tree, k)
26200Sstevel@tonic-gate     deflate_state *s;
26210Sstevel@tonic-gate     ct_data *tree;	/* the tree to restore */
26220Sstevel@tonic-gate     int k;	/* node to move down */
26230Sstevel@tonic-gate {
26240Sstevel@tonic-gate 	int v = s->heap[k];
26250Sstevel@tonic-gate 	int j = k << 1;	/* left son of k */
26260Sstevel@tonic-gate 	while (j <= s->heap_len) {
26270Sstevel@tonic-gate 		/* Set j to the smallest of the two sons: */
26280Sstevel@tonic-gate 		if (j < s->heap_len &&
26290Sstevel@tonic-gate 		    smaller(tree, s->heap[j+1], s->heap[j], s->depth)) {
26300Sstevel@tonic-gate 			j++;
26310Sstevel@tonic-gate 		}
26320Sstevel@tonic-gate 		/* Exit if v is smaller than both sons */
26330Sstevel@tonic-gate 		if (smaller(tree, v, s->heap[j], s->depth)) break;
26340Sstevel@tonic-gate 
26350Sstevel@tonic-gate 		/* Exchange v with the smallest son */
26360Sstevel@tonic-gate 		s->heap[k] = s->heap[j];  k = j;
26370Sstevel@tonic-gate 
26380Sstevel@tonic-gate 		/* And continue down the tree, setting j to the left son of k */
26390Sstevel@tonic-gate 		j <<= 1;
26400Sstevel@tonic-gate 	}
26410Sstevel@tonic-gate 	s->heap[k] = v;
26420Sstevel@tonic-gate }
26430Sstevel@tonic-gate 
26440Sstevel@tonic-gate /*
26450Sstevel@tonic-gate  * ===========================================================================
26460Sstevel@tonic-gate  * Compute the optimal bit lengths for a tree and update the total bit length
26470Sstevel@tonic-gate  * for the current block.
26480Sstevel@tonic-gate  * IN assertion: the fields freq and dad are set, heap[heap_max] and
26490Sstevel@tonic-gate  *    above are the tree nodes sorted by increasing frequency.
26500Sstevel@tonic-gate  * OUT assertions: the field len is set to the optimal bit length, the
26510Sstevel@tonic-gate  *     array bl_count contains the frequencies for each bit length.
26520Sstevel@tonic-gate  *     The length opt_len is updated; static_len is also updated if stree is
26530Sstevel@tonic-gate  *     not null.
26540Sstevel@tonic-gate  */
26550Sstevel@tonic-gate local void
gen_bitlen(s,desc)26560Sstevel@tonic-gate gen_bitlen(s, desc)
26570Sstevel@tonic-gate     deflate_state *s;
26580Sstevel@tonic-gate     tree_desc *desc;	/* the tree descriptor */
26590Sstevel@tonic-gate {
26600Sstevel@tonic-gate 	ct_data *tree  = desc->dyn_tree;
26610Sstevel@tonic-gate 	int max_code   = desc->max_code;
26620Sstevel@tonic-gate 	const ct_data *stree = desc->stat_desc->static_tree;
26630Sstevel@tonic-gate 	const intf *extra    = desc->stat_desc->extra_bits;
26640Sstevel@tonic-gate 	int base	= desc->stat_desc->extra_base;
26650Sstevel@tonic-gate 	int max_length = desc->stat_desc->max_length;
26660Sstevel@tonic-gate 	int h;	/* heap index */
26670Sstevel@tonic-gate 	int n, m;	/* iterate over the tree elements */
26680Sstevel@tonic-gate 	int bits;	/* bit length */
26690Sstevel@tonic-gate 	int xbits;	/* extra bits */
26700Sstevel@tonic-gate 	ush f;	/* frequency */
26710Sstevel@tonic-gate 	/* number of elements with bit length too large */
26720Sstevel@tonic-gate 	int overflow = 0;
26730Sstevel@tonic-gate 
26740Sstevel@tonic-gate 	for (bits = 0; bits <= MAX_BITS; bits++) s->bl_count[bits] = 0;
26750Sstevel@tonic-gate 
26760Sstevel@tonic-gate 	/*
26770Sstevel@tonic-gate 	 * In a first pass, compute the optimal bit lengths (which may
26780Sstevel@tonic-gate 	 * overflow in the case of the bit length tree).
26790Sstevel@tonic-gate 	 */
26800Sstevel@tonic-gate 	tree[s->heap[s->heap_max]].Len = 0;	/* root of the heap */
26810Sstevel@tonic-gate 
26820Sstevel@tonic-gate 	for (h = s->heap_max+1; h < HEAP_SIZE; h++) {
26830Sstevel@tonic-gate 		n = s->heap[h];
26840Sstevel@tonic-gate 		bits = tree[tree[n].Dad].Len + 1;
26850Sstevel@tonic-gate 		if (bits > max_length) bits = max_length, overflow++;
26860Sstevel@tonic-gate 		tree[n].Len = (ush)bits;
26870Sstevel@tonic-gate 		/* We overwrite tree[n].Dad which is no longer needed */
26880Sstevel@tonic-gate 
26890Sstevel@tonic-gate 		if (n > max_code) continue;	/* not a leaf node */
26900Sstevel@tonic-gate 
26910Sstevel@tonic-gate 		s->bl_count[bits]++;
26920Sstevel@tonic-gate 		xbits = 0;
26930Sstevel@tonic-gate 		if (n >= base) xbits = extra[n-base];
26940Sstevel@tonic-gate 		f = tree[n].Freq;
26950Sstevel@tonic-gate 		s->opt_len += (ulg)f * (bits + xbits);
26960Sstevel@tonic-gate 		if (stree) s->static_len += (ulg)f * (stree[n].Len + xbits);
26970Sstevel@tonic-gate 	}
26980Sstevel@tonic-gate 	if (overflow == 0)
26990Sstevel@tonic-gate 		return;
27000Sstevel@tonic-gate 
27010Sstevel@tonic-gate 	Trace((stderr, "\nbit length overflow\n"));
27020Sstevel@tonic-gate 	/* This happens for example on obj2 and pic of the Calgary corpus */
27030Sstevel@tonic-gate 
27040Sstevel@tonic-gate 	/* Find the first bit length which could increase: */
27050Sstevel@tonic-gate 	do {
27060Sstevel@tonic-gate 		bits = max_length-1;
27070Sstevel@tonic-gate 		while (s->bl_count[bits] == 0) bits--;
27080Sstevel@tonic-gate 		s->bl_count[bits]--;	/* move one leaf down the tree */
27090Sstevel@tonic-gate 		/* move one overflow item as its brother */
27100Sstevel@tonic-gate 		s->bl_count[bits+1] += 2;
27110Sstevel@tonic-gate 		s->bl_count[max_length]--;
27120Sstevel@tonic-gate 		/*
27130Sstevel@tonic-gate 		 * The brother of the overflow item also moves one
27140Sstevel@tonic-gate 		 * step up, but this does not affect
27150Sstevel@tonic-gate 		 * bl_count[max_length]
27160Sstevel@tonic-gate 		 */
27170Sstevel@tonic-gate 		overflow -= 2;
27180Sstevel@tonic-gate 	} while (overflow > 0);
27190Sstevel@tonic-gate 
27200Sstevel@tonic-gate 	/*
27210Sstevel@tonic-gate 	 * Now recompute all bit lengths, scanning in increasing
27220Sstevel@tonic-gate 	 * frequency.  h is still equal to HEAP_SIZE. (It is simpler
27230Sstevel@tonic-gate 	 * to reconstruct all lengths instead of fixing only the wrong
27240Sstevel@tonic-gate 	 * ones. This idea is taken from 'ar' written by Haruhiko
27250Sstevel@tonic-gate 	 * Okumura.)
27260Sstevel@tonic-gate 	 */
27270Sstevel@tonic-gate 	for (bits = max_length; bits != 0; bits--) {
27280Sstevel@tonic-gate 		n = s->bl_count[bits];
27290Sstevel@tonic-gate 		while (n != 0) {
27300Sstevel@tonic-gate 			m = s->heap[--h];
27310Sstevel@tonic-gate 			if (m > max_code) continue;
27320Sstevel@tonic-gate 			if (tree[m].Len != (unsigned)bits) {
27330Sstevel@tonic-gate 				Trace((stderr, "code %d bits %d->%d\n", m,
27340Sstevel@tonic-gate 				    tree[m].Len, bits));
27350Sstevel@tonic-gate 				s->opt_len += ((long)bits - (long)tree[m].Len)
27360Sstevel@tonic-gate 				    *(long)tree[m].Freq;
27370Sstevel@tonic-gate 				tree[m].Len = (ush)bits;
27380Sstevel@tonic-gate 			}
27390Sstevel@tonic-gate 			n--;
27400Sstevel@tonic-gate 		}
27410Sstevel@tonic-gate 	}
27420Sstevel@tonic-gate }
27430Sstevel@tonic-gate 
27440Sstevel@tonic-gate /*
27450Sstevel@tonic-gate  * ===========================================================================
27460Sstevel@tonic-gate  * Generate the codes for a given tree and bit counts (which need not be
27470Sstevel@tonic-gate  * optimal).
27480Sstevel@tonic-gate  * IN assertion: the array bl_count contains the bit length statistics for
27490Sstevel@tonic-gate  * the given tree and the field len is set for all tree elements.
27500Sstevel@tonic-gate  * OUT assertion: the field code is set for all tree elements of non
27510Sstevel@tonic-gate  *     zero code length.
27520Sstevel@tonic-gate  */
27530Sstevel@tonic-gate local void
gen_codes(tree,max_code,bl_count)27540Sstevel@tonic-gate gen_codes(tree, max_code, bl_count)
27550Sstevel@tonic-gate     ct_data *tree;	/* the tree to decorate */
27560Sstevel@tonic-gate     int max_code;	/* largest code with non zero frequency */
27570Sstevel@tonic-gate     ushf *bl_count;	/* number of codes at each bit length */
27580Sstevel@tonic-gate {
27590Sstevel@tonic-gate 	/* next code value for each bit length */
27600Sstevel@tonic-gate 	ush next_code[MAX_BITS+1];
27610Sstevel@tonic-gate 	ush code = 0;	/* running code value */
27620Sstevel@tonic-gate 	int bits;	/* bit index */
27630Sstevel@tonic-gate 	int n;	/* code index */
27640Sstevel@tonic-gate 
27650Sstevel@tonic-gate 	/*
27660Sstevel@tonic-gate 	 * The distribution counts are first used to generate the code
27670Sstevel@tonic-gate 	 * values without bit reversal.
27680Sstevel@tonic-gate 	 */
27690Sstevel@tonic-gate 	for (bits = 1; bits <= MAX_BITS; bits++) {
27700Sstevel@tonic-gate 		next_code[bits] = code = (code + bl_count[bits-1]) << 1;
27710Sstevel@tonic-gate 	}
27720Sstevel@tonic-gate 	/*
27730Sstevel@tonic-gate 	 * Check that the bit counts in bl_count are consistent. The
27740Sstevel@tonic-gate 	 * last code must be all ones.
27750Sstevel@tonic-gate 	 */
27760Sstevel@tonic-gate 	Assert(code + bl_count[MAX_BITS]-1 == (1<<MAX_BITS)-1,
27770Sstevel@tonic-gate 	    "inconsistent bit counts");
27780Sstevel@tonic-gate 	Tracev((stderr, "\ngen_codes: max_code %d ", max_code));
27790Sstevel@tonic-gate 
27800Sstevel@tonic-gate 	for (n = 0;  n <= max_code; n++) {
27810Sstevel@tonic-gate 		int len = tree[n].Len;
27820Sstevel@tonic-gate 		if (len == 0) continue;
27830Sstevel@tonic-gate 		/* Now reverse the bits */
27840Sstevel@tonic-gate 		tree[n].Code = bi_reverse(next_code[len]++, len);
27850Sstevel@tonic-gate 
27860Sstevel@tonic-gate 		Tracecv(tree != static_ltree,
27870Sstevel@tonic-gate 		    (stderr, "\nn %3d %c l %2d c %4x (%x) ",
27880Sstevel@tonic-gate 		    n, (isgraph(n) ? n : ' '), len, tree[n].Code,
27890Sstevel@tonic-gate 			next_code[len]-1));
27900Sstevel@tonic-gate 	}
27910Sstevel@tonic-gate }
27920Sstevel@tonic-gate 
27930Sstevel@tonic-gate /*
27940Sstevel@tonic-gate  * ===========================================================================
27950Sstevel@tonic-gate  * Construct one Huffman tree and assigns the code bit strings and lengths.
27960Sstevel@tonic-gate  * Update the total bit length for the current block.
27970Sstevel@tonic-gate  * IN assertion: the field freq is set for all tree elements.
27980Sstevel@tonic-gate  * OUT assertions: the fields len and code are set to the optimal bit length
27990Sstevel@tonic-gate  *     and corresponding code. The length opt_len is updated; static_len is
28000Sstevel@tonic-gate  *     also updated if stree is not null. The field max_code is set.
28010Sstevel@tonic-gate  */
28020Sstevel@tonic-gate local void
build_tree(s,desc)28030Sstevel@tonic-gate build_tree(s, desc)
28040Sstevel@tonic-gate     deflate_state *s;
28050Sstevel@tonic-gate     tree_desc *desc;	/* the tree descriptor */
28060Sstevel@tonic-gate {
28070Sstevel@tonic-gate 	ct_data *tree   = desc->dyn_tree;
28080Sstevel@tonic-gate 	const ct_data *stree  = desc->stat_desc->static_tree;
28090Sstevel@tonic-gate 	int elems	= desc->stat_desc->elems;
28100Sstevel@tonic-gate 	int n, m;	/* iterate over heap elements */
28110Sstevel@tonic-gate 	int max_code = -1;	/* largest code with non zero frequency */
28120Sstevel@tonic-gate 	int node;	/* new node being created */
28130Sstevel@tonic-gate 
28140Sstevel@tonic-gate 	/*
28150Sstevel@tonic-gate 	 * Construct the initial heap, with least frequent element in
28160Sstevel@tonic-gate 	 * heap[SMALLEST]. The sons of heap[n] are heap[2*n] and
28170Sstevel@tonic-gate 	 * heap[2*n+1].  heap[0] is not used.
28180Sstevel@tonic-gate 	 */
28190Sstevel@tonic-gate 	s->heap_len = 0, s->heap_max = HEAP_SIZE;
28200Sstevel@tonic-gate 
28210Sstevel@tonic-gate 	for (n = 0; n < elems; n++) {
28220Sstevel@tonic-gate 		if (tree[n].Freq != 0) {
28230Sstevel@tonic-gate 			s->heap[++(s->heap_len)] = max_code = n;
28240Sstevel@tonic-gate 			s->depth[n] = 0;
28250Sstevel@tonic-gate 		} else {
28260Sstevel@tonic-gate 			tree[n].Len = 0;
28270Sstevel@tonic-gate 		}
28280Sstevel@tonic-gate 	}
28290Sstevel@tonic-gate 
28300Sstevel@tonic-gate 	/*
28310Sstevel@tonic-gate 	 * The pkzip format requires that at least one distance code
28320Sstevel@tonic-gate 	 * exists, and that at least one bit should be sent even if
28330Sstevel@tonic-gate 	 * there is only one possible code. So to avoid special checks
28340Sstevel@tonic-gate 	 * later on we force at least two codes of non zero frequency.
28350Sstevel@tonic-gate 	 */
28360Sstevel@tonic-gate 	while (s->heap_len < 2) {
28370Sstevel@tonic-gate 		node = s->heap[++(s->heap_len)] = (max_code < 2 ?
28380Sstevel@tonic-gate 		    ++max_code : 0);
28390Sstevel@tonic-gate 		tree[node].Freq = 1;
28400Sstevel@tonic-gate 		s->depth[node] = 0;
28410Sstevel@tonic-gate 		s->opt_len--; if (stree) s->static_len -= stree[node].Len;
28420Sstevel@tonic-gate 		/* node is 0 or 1 so it does not have extra bits */
28430Sstevel@tonic-gate 	}
28440Sstevel@tonic-gate 	desc->max_code = max_code;
28450Sstevel@tonic-gate 
28460Sstevel@tonic-gate 	/*
28470Sstevel@tonic-gate 	 * The elements heap[heap_len/2+1 .. heap_len] are leaves of
28480Sstevel@tonic-gate 	 * the tree, establish sub-heaps of increasing lengths:
28490Sstevel@tonic-gate 	 */
28500Sstevel@tonic-gate 	for (n = s->heap_len/2; n >= 1; n--) pqdownheap(s, tree, n);
28510Sstevel@tonic-gate 
28520Sstevel@tonic-gate 	/*
28530Sstevel@tonic-gate 	 * Construct the Huffman tree by repeatedly combining the
28540Sstevel@tonic-gate 	 * least two frequent nodes.
28550Sstevel@tonic-gate 	 */
28560Sstevel@tonic-gate 	node = elems;	/* next internal node of the tree */
28570Sstevel@tonic-gate 	do {
28580Sstevel@tonic-gate 		pqremove(s, tree, n);	/* n = node of least frequency */
28590Sstevel@tonic-gate 		m = s->heap[SMALLEST];	/* m = node of next least frequency */
28600Sstevel@tonic-gate 
28610Sstevel@tonic-gate 		/* keep the nodes sorted by frequency */
28620Sstevel@tonic-gate 		s->heap[--(s->heap_max)] = n;
28630Sstevel@tonic-gate 		s->heap[--(s->heap_max)] = m;
28640Sstevel@tonic-gate 
28650Sstevel@tonic-gate 		/* Create a new node father of n and m */
28660Sstevel@tonic-gate 		tree[node].Freq = tree[n].Freq + tree[m].Freq;
28670Sstevel@tonic-gate 		s->depth[node] = (uch) (MAX(s->depth[n], s->depth[m]) + 1);
28680Sstevel@tonic-gate 		tree[n].Dad = tree[m].Dad = (ush)node;
28690Sstevel@tonic-gate #ifdef DUMP_BL_TREE
28700Sstevel@tonic-gate 		if (tree == s->bl_tree) {
28710Sstevel@tonic-gate 			fprintf(stderr, "\nnode %d(%d), sons %d(%d) %d(%d)",
28720Sstevel@tonic-gate 			    node, tree[node].Freq, n, tree[n].Freq, m,
28730Sstevel@tonic-gate 			    tree[m].Freq);
28740Sstevel@tonic-gate 		}
28750Sstevel@tonic-gate #endif
28760Sstevel@tonic-gate 		/* and insert the new node in the heap */
28770Sstevel@tonic-gate 		s->heap[SMALLEST] = node++;
28780Sstevel@tonic-gate 		pqdownheap(s, tree, SMALLEST);
28790Sstevel@tonic-gate 
28800Sstevel@tonic-gate 	} while (s->heap_len >= 2);
28810Sstevel@tonic-gate 
28820Sstevel@tonic-gate 	s->heap[--(s->heap_max)] = s->heap[SMALLEST];
28830Sstevel@tonic-gate 
28840Sstevel@tonic-gate 	/*
28850Sstevel@tonic-gate 	 * At this point, the fields freq and dad are set. We can now
28860Sstevel@tonic-gate 	 * generate the bit lengths.
28870Sstevel@tonic-gate 	 */
28880Sstevel@tonic-gate 	gen_bitlen(s, (tree_desc *)desc);
28890Sstevel@tonic-gate 
28900Sstevel@tonic-gate 	/* The field len is now set, we can generate the bit codes */
28910Sstevel@tonic-gate 	gen_codes((ct_data *)tree, max_code, s->bl_count);
28920Sstevel@tonic-gate }
28930Sstevel@tonic-gate 
28940Sstevel@tonic-gate /*
28950Sstevel@tonic-gate  * ===========================================================================
28960Sstevel@tonic-gate  * Scan a literal or distance tree to determine the frequencies of the codes
28970Sstevel@tonic-gate  * in the bit length tree.
28980Sstevel@tonic-gate  */
28990Sstevel@tonic-gate local void
scan_tree(s,tree,max_code)29000Sstevel@tonic-gate scan_tree(s, tree, max_code)
29010Sstevel@tonic-gate     deflate_state *s;
29020Sstevel@tonic-gate     ct_data *tree;	/* the tree to be scanned */
29030Sstevel@tonic-gate     int max_code;	/* and its largest code of non zero frequency */
29040Sstevel@tonic-gate {
29050Sstevel@tonic-gate 	int n;	/* iterates over all tree elements */
29060Sstevel@tonic-gate 	int prevlen = -1;	/* last emitted length */
29070Sstevel@tonic-gate 	int curlen;	/* length of current code */
29080Sstevel@tonic-gate 	int nextlen = tree[0].Len;	/* length of next code */
29090Sstevel@tonic-gate 	int count = 0;	/* repeat count of the current code */
29100Sstevel@tonic-gate 	int max_count = 7;	/* max repeat count */
29110Sstevel@tonic-gate 	int min_count = 4;	/* min repeat count */
29120Sstevel@tonic-gate 
29130Sstevel@tonic-gate 	if (nextlen == 0) max_count = 138, min_count = 3;
29140Sstevel@tonic-gate 	tree[max_code+1].Len = (ush)0xffff;	/* guard */
29150Sstevel@tonic-gate 
29160Sstevel@tonic-gate 	for (n = 0; n <= max_code; n++) {
29170Sstevel@tonic-gate 		curlen = nextlen; nextlen = tree[n+1].Len;
29180Sstevel@tonic-gate 		if (++count < max_count && curlen == nextlen) {
29190Sstevel@tonic-gate 			continue;
29200Sstevel@tonic-gate 		} else if (count < min_count) {
29210Sstevel@tonic-gate 			s->bl_tree[curlen].Freq += count;
29220Sstevel@tonic-gate 		} else if (curlen != 0) {
29230Sstevel@tonic-gate 			if (curlen != prevlen) s->bl_tree[curlen].Freq++;
29240Sstevel@tonic-gate 			s->bl_tree[REP_3_6].Freq++;
29250Sstevel@tonic-gate 		} else if (count <= 10) {
29260Sstevel@tonic-gate 			s->bl_tree[REPZ_3_10].Freq++;
29270Sstevel@tonic-gate 		} else {
29280Sstevel@tonic-gate 			s->bl_tree[REPZ_11_138].Freq++;
29290Sstevel@tonic-gate 		}
29300Sstevel@tonic-gate 		count = 0; prevlen = curlen;
29310Sstevel@tonic-gate 		if (nextlen == 0) {
29320Sstevel@tonic-gate 			max_count = 138, min_count = 3;
29330Sstevel@tonic-gate 		} else if (curlen == nextlen) {
29340Sstevel@tonic-gate 			max_count = 6, min_count = 3;
29350Sstevel@tonic-gate 		} else {
29360Sstevel@tonic-gate 			max_count = 7, min_count = 4;
29370Sstevel@tonic-gate 		}
29380Sstevel@tonic-gate 	}
29390Sstevel@tonic-gate }
29400Sstevel@tonic-gate 
29410Sstevel@tonic-gate /*
29420Sstevel@tonic-gate  * ===========================================================================
29430Sstevel@tonic-gate  * Send a literal or distance tree in compressed form, using the codes in
29440Sstevel@tonic-gate  * bl_tree.
29450Sstevel@tonic-gate  */
29460Sstevel@tonic-gate local void
send_tree(s,tree,max_code)29470Sstevel@tonic-gate send_tree(s, tree, max_code)
29480Sstevel@tonic-gate     deflate_state *s;
29490Sstevel@tonic-gate     ct_data *tree;	/* the tree to be scanned */
29500Sstevel@tonic-gate     int max_code;	/* and its largest code of non zero frequency */
29510Sstevel@tonic-gate {
29520Sstevel@tonic-gate 	int n;	/* iterates over all tree elements */
29530Sstevel@tonic-gate 	int prevlen = -1;	/* last emitted length */
29540Sstevel@tonic-gate 	int curlen;	/* length of current code */
29550Sstevel@tonic-gate 	int nextlen = tree[0].Len;	/* length of next code */
29560Sstevel@tonic-gate 	int count = 0;	/* repeat count of the current code */
29570Sstevel@tonic-gate 	int max_count = 7;	/* max repeat count */
29580Sstevel@tonic-gate 	int min_count = 4;	/* min repeat count */
29590Sstevel@tonic-gate 
29600Sstevel@tonic-gate 	/* tree[max_code+1].Len = -1; */  /* guard already set */
29610Sstevel@tonic-gate 	if (nextlen == 0) max_count = 138, min_count = 3;
29620Sstevel@tonic-gate 
29630Sstevel@tonic-gate 	for (n = 0; n <= max_code; n++) {
29640Sstevel@tonic-gate 		curlen = nextlen; nextlen = tree[n+1].Len;
29650Sstevel@tonic-gate 		if (++count < max_count && curlen == nextlen) {
29660Sstevel@tonic-gate 			continue;
29670Sstevel@tonic-gate 		} else if (count < min_count) {
29680Sstevel@tonic-gate 			do { send_code(s, curlen, s->bl_tree); }
29690Sstevel@tonic-gate 			while (--count != 0);
29700Sstevel@tonic-gate 
29710Sstevel@tonic-gate 		} else if (curlen != 0) {
29720Sstevel@tonic-gate 			if (curlen != prevlen) {
29730Sstevel@tonic-gate 				send_code(s, curlen, s->bl_tree); count--;
29740Sstevel@tonic-gate 			}
29750Sstevel@tonic-gate 			Assert(count >= 3 && count <= 6, " 3_6?");
29760Sstevel@tonic-gate 			send_code(s, REP_3_6, s->bl_tree);
29770Sstevel@tonic-gate 			send_bits(s, count-3, 2);
29780Sstevel@tonic-gate 
29790Sstevel@tonic-gate 		} else if (count <= 10) {
29800Sstevel@tonic-gate 			send_code(s, REPZ_3_10, s->bl_tree);
29810Sstevel@tonic-gate 			send_bits(s, count-3, 3);
29820Sstevel@tonic-gate 
29830Sstevel@tonic-gate 		} else {
29840Sstevel@tonic-gate 			send_code(s, REPZ_11_138, s->bl_tree);
29850Sstevel@tonic-gate 			send_bits(s, count-11, 7);
29860Sstevel@tonic-gate 		}
29870Sstevel@tonic-gate 		count = 0; prevlen = curlen;
29880Sstevel@tonic-gate 		if (nextlen == 0) {
29890Sstevel@tonic-gate 			max_count = 138, min_count = 3;
29900Sstevel@tonic-gate 		} else if (curlen == nextlen) {
29910Sstevel@tonic-gate 			max_count = 6, min_count = 3;
29920Sstevel@tonic-gate 		} else {
29930Sstevel@tonic-gate 			max_count = 7, min_count = 4;
29940Sstevel@tonic-gate 		}
29950Sstevel@tonic-gate 	}
29960Sstevel@tonic-gate }
29970Sstevel@tonic-gate 
29980Sstevel@tonic-gate /*
29990Sstevel@tonic-gate  * ===========================================================================
30000Sstevel@tonic-gate  * Construct the Huffman tree for the bit lengths and return the index in
30010Sstevel@tonic-gate  * bl_order of the last bit length code to send.
30020Sstevel@tonic-gate  */
30030Sstevel@tonic-gate local int
build_bl_tree(s)30040Sstevel@tonic-gate build_bl_tree(s)
30050Sstevel@tonic-gate     deflate_state *s;
30060Sstevel@tonic-gate {
30070Sstevel@tonic-gate 	/* index of last bit length code of non zero freq */
30080Sstevel@tonic-gate 	int max_blindex;
30090Sstevel@tonic-gate 
30100Sstevel@tonic-gate 	/*
30110Sstevel@tonic-gate 	 * Determine the bit length frequencies for literal and
30120Sstevel@tonic-gate 	 * distance trees
30130Sstevel@tonic-gate 	 */
30140Sstevel@tonic-gate 	scan_tree(s, (ct_data *)s->dyn_ltree, s->l_desc.max_code);
30150Sstevel@tonic-gate 	scan_tree(s, (ct_data *)s->dyn_dtree, s->d_desc.max_code);
30160Sstevel@tonic-gate 
30170Sstevel@tonic-gate 	/* Build the bit length tree: */
30180Sstevel@tonic-gate 	build_tree(s, (tree_desc *)(&(s->bl_desc)));
30190Sstevel@tonic-gate 	/*
30200Sstevel@tonic-gate 	 * opt_len now includes the length of the tree
30210Sstevel@tonic-gate 	 * representations, except the lengths of the bit lengths
30220Sstevel@tonic-gate 	 * codes and the 5+5+4 bits for the counts.
30230Sstevel@tonic-gate 	 */
30240Sstevel@tonic-gate 
30250Sstevel@tonic-gate 	/*
30260Sstevel@tonic-gate 	 * Determine the number of bit length codes to send. The pkzip
30270Sstevel@tonic-gate 	 * format requires that at least 4 bit length codes be
30280Sstevel@tonic-gate 	 * sent. (appnote.txt says 3 but the actual value used is 4.)
30290Sstevel@tonic-gate 	 */
30300Sstevel@tonic-gate 	for (max_blindex = BL_CODES-1; max_blindex >= 3; max_blindex--) {
30310Sstevel@tonic-gate 		if (s->bl_tree[bl_order[max_blindex]].Len != 0) break;
30320Sstevel@tonic-gate 	}
30330Sstevel@tonic-gate 	/* Update opt_len to include the bit length tree and counts */
30340Sstevel@tonic-gate 	s->opt_len += 3*(max_blindex+1) + 5+5+4;
30350Sstevel@tonic-gate 	Tracev((stderr, "\ndyn trees: dyn %ld, stat %ld",
30360Sstevel@tonic-gate 	    s->opt_len, s->static_len));
30370Sstevel@tonic-gate 
30380Sstevel@tonic-gate 	return (max_blindex);
30390Sstevel@tonic-gate }
30400Sstevel@tonic-gate 
30410Sstevel@tonic-gate /*
30420Sstevel@tonic-gate  * ===========================================================================
30430Sstevel@tonic-gate  * Send the header for a block using dynamic Huffman trees: the counts, the
30440Sstevel@tonic-gate  * lengths of the bit length codes, the literal tree and the distance tree.
30450Sstevel@tonic-gate  * IN assertion: lcodes >= 257, dcodes >= 1, blcodes >= 4.
30460Sstevel@tonic-gate  */
30470Sstevel@tonic-gate local void
send_all_trees(s,lcodes,dcodes,blcodes)30480Sstevel@tonic-gate send_all_trees(s, lcodes, dcodes, blcodes)
30490Sstevel@tonic-gate     deflate_state *s;
30500Sstevel@tonic-gate     int lcodes, dcodes, blcodes;	/* number of codes for each tree */
30510Sstevel@tonic-gate {
30520Sstevel@tonic-gate 	int rank;	/* index in bl_order */
30530Sstevel@tonic-gate 
30540Sstevel@tonic-gate 	Assert(lcodes >= 257 && dcodes >= 1 && blcodes >= 4,
30550Sstevel@tonic-gate 	    "not enough codes");
30560Sstevel@tonic-gate 	Assert(lcodes <= L_CODES && dcodes <= D_CODES && blcodes <= BL_CODES,
30570Sstevel@tonic-gate 	    "too many codes");
30580Sstevel@tonic-gate 	Tracev((stderr, "\nbl counts: "));
30590Sstevel@tonic-gate 	send_bits(s, lcodes-257, 5);	/* not +255 as stated in appnote.txt */
30600Sstevel@tonic-gate 	send_bits(s, dcodes-1,   5);
30610Sstevel@tonic-gate 	send_bits(s, blcodes-4,  4);	/* not -3 as stated in appnote.txt */
30620Sstevel@tonic-gate 	for (rank = 0; rank < blcodes; rank++) {
30630Sstevel@tonic-gate 		Tracev((stderr, "\nbl code %2d ", bl_order[rank]));
30640Sstevel@tonic-gate 		send_bits(s, s->bl_tree[bl_order[rank]].Len, 3);
30650Sstevel@tonic-gate 	}
30660Sstevel@tonic-gate #ifdef DEBUG_ZLIB
30670Sstevel@tonic-gate 	Tracev((stderr, "\nbl tree: sent %ld", s->bits_sent));
30680Sstevel@tonic-gate #endif
30690Sstevel@tonic-gate 
30700Sstevel@tonic-gate 	/* literal tree */
30710Sstevel@tonic-gate 	send_tree(s, (ct_data *)s->dyn_ltree, lcodes-1);
30720Sstevel@tonic-gate #ifdef DEBUG_ZLIB
30730Sstevel@tonic-gate 	Tracev((stderr, "\nlit tree: sent %ld", s->bits_sent));
30740Sstevel@tonic-gate #endif
30750Sstevel@tonic-gate 
30760Sstevel@tonic-gate 	/* distance tree */
30770Sstevel@tonic-gate 	send_tree(s, (ct_data *)s->dyn_dtree, dcodes-1);
30780Sstevel@tonic-gate #ifdef DEBUG_ZLIB
30790Sstevel@tonic-gate 	Tracev((stderr, "\ndist tree: sent %ld", s->bits_sent));
30800Sstevel@tonic-gate #endif
30810Sstevel@tonic-gate }
30820Sstevel@tonic-gate 
30830Sstevel@tonic-gate /*
30840Sstevel@tonic-gate  * ===========================================================================
30850Sstevel@tonic-gate  * Send a stored block
30860Sstevel@tonic-gate  */
30870Sstevel@tonic-gate void
_tr_stored_block(s,buf,stored_len,eof)30880Sstevel@tonic-gate _tr_stored_block(s, buf, stored_len, eof)
30890Sstevel@tonic-gate     deflate_state *s;
30900Sstevel@tonic-gate     charf *buf;	/* input block */
30910Sstevel@tonic-gate     ulg stored_len;	/* length of input block */
30920Sstevel@tonic-gate     int eof;	/* true if this is the last block for a file */
30930Sstevel@tonic-gate {
30940Sstevel@tonic-gate 	send_bits(s, (STORED_BLOCK<<1)+eof, 3);	/* send block type */
30950Sstevel@tonic-gate 	s->compressed_len = (s->compressed_len + 3 + 7) & (ulg)~7L; /* PPP */
30960Sstevel@tonic-gate 	s->compressed_len += (stored_len + 4) << 3;	/* PPP */
30970Sstevel@tonic-gate 
30980Sstevel@tonic-gate 	copy_block(s, buf, (unsigned)stored_len, 1);	/* with header */
30990Sstevel@tonic-gate }
31000Sstevel@tonic-gate 
31010Sstevel@tonic-gate /*
31020Sstevel@tonic-gate  * Send just the `stored block' type code without any length bytes or data.
31030Sstevel@tonic-gate  * ---PPP---
31040Sstevel@tonic-gate  */
31050Sstevel@tonic-gate void
_tr_stored_type_only(s)31060Sstevel@tonic-gate _tr_stored_type_only(s)
31070Sstevel@tonic-gate     deflate_state *s;
31080Sstevel@tonic-gate {
31090Sstevel@tonic-gate 	send_bits(s, (STORED_BLOCK << 1), 3);
31100Sstevel@tonic-gate 	bi_windup(s);
31110Sstevel@tonic-gate 	s->compressed_len = (s->compressed_len + 3) & ~7L;	/* PPP */
31120Sstevel@tonic-gate }
31130Sstevel@tonic-gate 
31140Sstevel@tonic-gate 
31150Sstevel@tonic-gate /*
31160Sstevel@tonic-gate  * ===========================================================================
31170Sstevel@tonic-gate  * Send one empty static block to give enough lookahead for inflate.
31180Sstevel@tonic-gate  * This takes 10 bits, of which 7 may remain in the bit buffer.
31190Sstevel@tonic-gate  * The current inflate code requires 9 bits of lookahead. If the
31200Sstevel@tonic-gate  * last two codes for the previous block (real code plus EOB) were coded
31210Sstevel@tonic-gate  * on 5 bits or less, inflate may have only 5+3 bits of lookahead to decode
31220Sstevel@tonic-gate  * the last real code. In this case we send two empty static blocks instead
31230Sstevel@tonic-gate  * of one. (There are no problems if the previous block is stored or fixed.)
31240Sstevel@tonic-gate  * To simplify the code, we assume the worst case of last real code encoded
31250Sstevel@tonic-gate  * on one bit only.
31260Sstevel@tonic-gate  */
31270Sstevel@tonic-gate void
_tr_align(s)31280Sstevel@tonic-gate _tr_align(s)
31290Sstevel@tonic-gate     deflate_state *s;
31300Sstevel@tonic-gate {
31310Sstevel@tonic-gate 	send_bits(s, STATIC_TREES<<1, 3);
31320Sstevel@tonic-gate 	send_code(s, END_BLOCK, static_ltree);
31330Sstevel@tonic-gate 	s->compressed_len += 10L;	/* 3 for block type, 7 for EOB */
31340Sstevel@tonic-gate 	bi_flush(s);
31350Sstevel@tonic-gate 	/*
31360Sstevel@tonic-gate 	 * Of the 10 bits for the empty block, we have already sent
31370Sstevel@tonic-gate 	 * (10 - bi_valid) bits. The lookahead for the last real code
31380Sstevel@tonic-gate 	 * (before the EOB of the previous block) was thus at least
31390Sstevel@tonic-gate 	 * one plus the length of the EOB plus what we have just sent
31400Sstevel@tonic-gate 	 * of the empty static block.
31410Sstevel@tonic-gate 	 */
31420Sstevel@tonic-gate 	if (1 + s->last_eob_len + 10 - s->bi_valid < 9) {
31430Sstevel@tonic-gate 		send_bits(s, STATIC_TREES<<1, 3);
31440Sstevel@tonic-gate 		send_code(s, END_BLOCK, static_ltree);
31450Sstevel@tonic-gate 		s->compressed_len += 10L;
31460Sstevel@tonic-gate 		bi_flush(s);
31470Sstevel@tonic-gate 	}
31480Sstevel@tonic-gate 	s->last_eob_len = 7;
31490Sstevel@tonic-gate }
31500Sstevel@tonic-gate 
31510Sstevel@tonic-gate /*
31520Sstevel@tonic-gate  * ===========================================================================
31530Sstevel@tonic-gate  * Determine the best encoding for the current block: dynamic trees, static
31540Sstevel@tonic-gate  * trees or store, and output the encoded block to the zip file.
31550Sstevel@tonic-gate  */
31560Sstevel@tonic-gate void
_tr_flush_block(s,buf,stored_len,eof)31570Sstevel@tonic-gate _tr_flush_block(s, buf, stored_len, eof)
31580Sstevel@tonic-gate     deflate_state *s;
31590Sstevel@tonic-gate     charf *buf;	/* input block, or NULL if too old */
31600Sstevel@tonic-gate     ulg stored_len;	/* length of input block */
31610Sstevel@tonic-gate     int eof;	/* true if this is the last block for a file */
31620Sstevel@tonic-gate {
31630Sstevel@tonic-gate 	ulg opt_lenb, static_lenb;	/* opt_len and static_len in bytes */
31640Sstevel@tonic-gate 	/* index of last bit length code of non zero freq */
31650Sstevel@tonic-gate 	int max_blindex = 0;
31660Sstevel@tonic-gate 
31670Sstevel@tonic-gate 	/* Build the Huffman trees unless a stored block is forced */
31680Sstevel@tonic-gate 	if (s->level > 0) {
31690Sstevel@tonic-gate 
31700Sstevel@tonic-gate 		/* Check if the file is ascii or binary */
31710Sstevel@tonic-gate 		if (s->data_type == Z_UNKNOWN) set_data_type(s);
31720Sstevel@tonic-gate 
31730Sstevel@tonic-gate 		/* Construct the literal and distance trees */
31740Sstevel@tonic-gate 		build_tree(s, (tree_desc *)(&(s->l_desc)));
31750Sstevel@tonic-gate 		Tracev((stderr, "\nlit data: dyn %ld, stat %ld", s->opt_len,
31760Sstevel@tonic-gate 		    s->static_len));
31770Sstevel@tonic-gate 
31780Sstevel@tonic-gate 		build_tree(s, (tree_desc *)(&(s->d_desc)));
31790Sstevel@tonic-gate 		Tracev((stderr, "\ndist data: dyn %ld, stat %ld", s->opt_len,
31800Sstevel@tonic-gate 		    s->static_len));
31810Sstevel@tonic-gate 		/*
31820Sstevel@tonic-gate 		 * At this point, opt_len and static_len are the total
31830Sstevel@tonic-gate 		 * bit lengths of the compressed block data, excluding
31840Sstevel@tonic-gate 		 * the tree representations.
31850Sstevel@tonic-gate 		 */
31860Sstevel@tonic-gate 
31870Sstevel@tonic-gate 		/*
31880Sstevel@tonic-gate 		 * Build the bit length tree for the above two trees,
31890Sstevel@tonic-gate 		 * and get the index in bl_order of the last bit
31900Sstevel@tonic-gate 		 * length code to send.
31910Sstevel@tonic-gate 		 */
31920Sstevel@tonic-gate 		max_blindex = build_bl_tree(s);
31930Sstevel@tonic-gate 
31940Sstevel@tonic-gate 		/*
31950Sstevel@tonic-gate 		 * Determine the best encoding. Compute first the
31960Sstevel@tonic-gate 		 * block length in bytes
31970Sstevel@tonic-gate 		 */
31980Sstevel@tonic-gate 		opt_lenb = (s->opt_len+3+7)>>3;
31990Sstevel@tonic-gate 		static_lenb = (s->static_len+3+7)>>3;
32000Sstevel@tonic-gate 
32010Sstevel@tonic-gate 		Tracev((stderr,
32020Sstevel@tonic-gate 		    "\nopt %lu(%lu) stat %lu(%lu) stored %lu lit %u ",
32030Sstevel@tonic-gate 		    opt_lenb, s->opt_len, static_lenb, s->static_len,
32040Sstevel@tonic-gate 		    stored_len, s->last_lit));
32050Sstevel@tonic-gate 
32060Sstevel@tonic-gate 		if (static_lenb <= opt_lenb) opt_lenb = static_lenb;
32070Sstevel@tonic-gate 
32080Sstevel@tonic-gate 	} else {
32090Sstevel@tonic-gate 		Assert(buf != (char *)0, "lost buf");
32100Sstevel@tonic-gate 		/* force a stored block */
32110Sstevel@tonic-gate 		opt_lenb = static_lenb = stored_len + 5;
32120Sstevel@tonic-gate 	}
32130Sstevel@tonic-gate 
32140Sstevel@tonic-gate 	/*
32150Sstevel@tonic-gate 	 * If compression failed and this is the first and last block,
32160Sstevel@tonic-gate 	 * and if the .zip file can be seeked (to rewrite the local
32170Sstevel@tonic-gate 	 * header), the whole file is transformed into a stored file:
32180Sstevel@tonic-gate 	 */
32190Sstevel@tonic-gate #ifdef STORED_FILE_OK
32200Sstevel@tonic-gate #ifdef FORCE_STORED_FILE
32210Sstevel@tonic-gate #define	FRC_STR_COND	eof && s->compressed_len == 0L /* force stored file */
32220Sstevel@tonic-gate #else
32230Sstevel@tonic-gate #define	FRC_STR_COND	stored_len <= opt_lenb && eof && \
32240Sstevel@tonic-gate 			s->compressed_len == 0L && seekable()
32250Sstevel@tonic-gate #endif
32260Sstevel@tonic-gate 	if (FRC_STR_COND) {
32270Sstevel@tonic-gate #undef FRC_STR_COND
32280Sstevel@tonic-gate 		/*
32290Sstevel@tonic-gate 		 * Since LIT_BUFSIZE <= 2*WSIZE, the input data must
32300Sstevel@tonic-gate 		 * be there:
32310Sstevel@tonic-gate 		 */
32320Sstevel@tonic-gate 		if (buf == (charf*)0) error("block vanished");
32330Sstevel@tonic-gate 
32340Sstevel@tonic-gate 		/* without header */
32350Sstevel@tonic-gate 		copy_block(s, buf, (unsigned)stored_len, 0);
32360Sstevel@tonic-gate 		s->compressed_len = stored_len << 3;
32370Sstevel@tonic-gate 		s->method = STORED;
32380Sstevel@tonic-gate 	} else
32390Sstevel@tonic-gate #endif /* STORED_FILE_OK */
32400Sstevel@tonic-gate 
32410Sstevel@tonic-gate #ifdef FORCE_STORED
32420Sstevel@tonic-gate #define	FRC_STR_COND	buf != (char *)0	/* force stored block */
32430Sstevel@tonic-gate #else
32440Sstevel@tonic-gate 			/* 4: two words for the lengths */
32450Sstevel@tonic-gate #define	FRC_STR_COND	stored_len+4 <= opt_lenb && buf != (char *)0
32460Sstevel@tonic-gate #endif
32470Sstevel@tonic-gate 		if (FRC_STR_COND) {
32480Sstevel@tonic-gate #undef FRC_STR_COND
32490Sstevel@tonic-gate 			/*
32500Sstevel@tonic-gate 			 * The test buf != NULL is only necessary if
32510Sstevel@tonic-gate 			 * LIT_BUFSIZE > WSIZE.  Otherwise we can't
32520Sstevel@tonic-gate 			 * have processed more than WSIZE input bytes
32530Sstevel@tonic-gate 			 * since the last block flush, because
32540Sstevel@tonic-gate 			 * compression would have been successful. If
32550Sstevel@tonic-gate 			 * LIT_BUFSIZE <= WSIZE, it is never too late
32560Sstevel@tonic-gate 			 * to transform a block into a stored block.
32570Sstevel@tonic-gate 			 */
32580Sstevel@tonic-gate 			_tr_stored_block(s, buf, stored_len, eof);
32590Sstevel@tonic-gate #ifdef FORCE_STATIC
32600Sstevel@tonic-gate #define	FRC_STAT_COND	static_lenb >= 0 /* force static trees */
32610Sstevel@tonic-gate #else
32620Sstevel@tonic-gate #define	FRC_STAT_COND	static_lenb == opt_lenb
32630Sstevel@tonic-gate #endif
32640Sstevel@tonic-gate 		} else if (FRC_STAT_COND) {
32650Sstevel@tonic-gate #undef FRC_STAT_COND
32660Sstevel@tonic-gate 			send_bits(s, (STATIC_TREES<<1)+eof, 3);
32670Sstevel@tonic-gate 			compress_block(s, (ct_data *)static_ltree,
32680Sstevel@tonic-gate 			    (ct_data *)static_dtree);
32690Sstevel@tonic-gate 			s->compressed_len += 3 + s->static_len;	/* PPP */
32700Sstevel@tonic-gate 		} else {
32710Sstevel@tonic-gate 			send_bits(s, (DYN_TREES<<1)+eof, 3);
32720Sstevel@tonic-gate 			send_all_trees(s, s->l_desc.max_code+1,
32730Sstevel@tonic-gate 			    s->d_desc.max_code+1,
32740Sstevel@tonic-gate 			    max_blindex+1);
32750Sstevel@tonic-gate 			compress_block(s, (ct_data *)s->dyn_ltree,
32760Sstevel@tonic-gate 			    (ct_data *)s->dyn_dtree);
32770Sstevel@tonic-gate 			s->compressed_len += 3 + s->opt_len;	/* PPP */
32780Sstevel@tonic-gate 		}
32790Sstevel@tonic-gate #ifdef DEBUG_ZLIB
32800Sstevel@tonic-gate 	Assert(s->compressed_len == s->bits_sent, "bad compressed size");
32810Sstevel@tonic-gate #endif
32820Sstevel@tonic-gate 	/*
32830Sstevel@tonic-gate 	 * The above check is made mod 2^32, for files larger than 512
32840Sstevel@tonic-gate 	 * MB and uLong implemented on 32 bits.
32850Sstevel@tonic-gate 	 */
32860Sstevel@tonic-gate 	init_block(s);
32870Sstevel@tonic-gate 
32880Sstevel@tonic-gate 	if (eof) {
32890Sstevel@tonic-gate 		bi_windup(s);
32900Sstevel@tonic-gate 		s->compressed_len += 7;	/* align on byte boundary PPP */
32910Sstevel@tonic-gate 	}
32920Sstevel@tonic-gate 	Tracev((stderr, "\ncomprlen %lu(%lu) ", s->compressed_len>>3,
32930Sstevel@tonic-gate 	    s->compressed_len-7*eof));
32940Sstevel@tonic-gate 
32950Sstevel@tonic-gate 	/* return (s->compressed_len >> 3); */
32960Sstevel@tonic-gate }
32970Sstevel@tonic-gate 
32980Sstevel@tonic-gate /*
32990Sstevel@tonic-gate  * ===========================================================================
33000Sstevel@tonic-gate  * Save the match info and tally the frequency counts. Return true if
33010Sstevel@tonic-gate  * the current block must be flushed.
33020Sstevel@tonic-gate  */
33030Sstevel@tonic-gate int
_tr_tally(s,dist,lc)33040Sstevel@tonic-gate _tr_tally(s, dist, lc)
33050Sstevel@tonic-gate     deflate_state *s;
33060Sstevel@tonic-gate     unsigned dist;	/* distance of matched string */
33070Sstevel@tonic-gate 	/* match length-MIN_MATCH or unmatched char (if dist==0) */
33080Sstevel@tonic-gate     unsigned lc;
33090Sstevel@tonic-gate {
33100Sstevel@tonic-gate 	s->d_buf[s->last_lit] = (ush)dist;
33110Sstevel@tonic-gate 	s->l_buf[s->last_lit++] = (uch)lc;
33120Sstevel@tonic-gate 	if (dist == 0) {
33130Sstevel@tonic-gate 		/* lc is the unmatched char */
33140Sstevel@tonic-gate 		s->dyn_ltree[lc].Freq++;
33150Sstevel@tonic-gate 	} else {
33160Sstevel@tonic-gate 		s->matches++;
33170Sstevel@tonic-gate 		/* Here, lc is the match length - MIN_MATCH */
33180Sstevel@tonic-gate 		dist--;	/* dist = match distance - 1 */
33190Sstevel@tonic-gate 		Assert((ush)dist < (ush)MAX_DIST(s) &&
33200Sstevel@tonic-gate 		    (ush)lc <= (ush)(MAX_MATCH-MIN_MATCH) &&
33210Sstevel@tonic-gate 		    (ush)d_code(dist) < (ush)D_CODES,  "_tr_tally: bad match");
33220Sstevel@tonic-gate 
33230Sstevel@tonic-gate 		s->dyn_ltree[_length_code[lc]+LITERALS+1].Freq++;
33240Sstevel@tonic-gate 		s->dyn_dtree[d_code(dist)].Freq++;
33250Sstevel@tonic-gate 	}
33260Sstevel@tonic-gate 
33270Sstevel@tonic-gate #ifdef TRUNCATE_BLOCK
33280Sstevel@tonic-gate 	/* Try to guess if it is profitable to stop the current block here */
33290Sstevel@tonic-gate 	if ((s->last_lit & 0x1fff) == 0 && s->level > 2) {
33300Sstevel@tonic-gate 		/* Compute an upper bound for the compressed length */
33310Sstevel@tonic-gate 		ulg out_length = (ulg)s->last_lit*8L;
33320Sstevel@tonic-gate 		ulg in_length = (ulg)((long)s->strstart - s->block_start);
33330Sstevel@tonic-gate 		int dcode;
33340Sstevel@tonic-gate 		for (dcode = 0; dcode < D_CODES; dcode++) {
33350Sstevel@tonic-gate 			out_length += (ulg)s->dyn_dtree[dcode].Freq *
33360Sstevel@tonic-gate 			    (5L+extra_dbits[dcode]);
33370Sstevel@tonic-gate 		}
33380Sstevel@tonic-gate 		out_length >>= 3;
33390Sstevel@tonic-gate 		Tracev((stderr, "\nlast_lit %u, in %ld, out ~%ld(%ld%%) ",
33400Sstevel@tonic-gate 		    s->last_lit, in_length, out_length,
33410Sstevel@tonic-gate 		    100L - out_length*100L/in_length));
33420Sstevel@tonic-gate 		if (s->matches < s->last_lit/2 && out_length < in_length/2)
33430Sstevel@tonic-gate 			return (1);
33440Sstevel@tonic-gate 	}
33450Sstevel@tonic-gate #endif
33460Sstevel@tonic-gate 	return (s->last_lit == s->lit_bufsize-1);
33470Sstevel@tonic-gate 	/*
33480Sstevel@tonic-gate 	 * We avoid equality with lit_bufsize because of wraparound at 64K
33490Sstevel@tonic-gate 	 * on 16 bit machines and because stored blocks are restricted to
33500Sstevel@tonic-gate 	 * 64K-1 bytes.
33510Sstevel@tonic-gate 	 */
33520Sstevel@tonic-gate }
33530Sstevel@tonic-gate 
33540Sstevel@tonic-gate /*
33550Sstevel@tonic-gate  * ===========================================================================
33560Sstevel@tonic-gate  * Send the block data compressed using the given Huffman trees
33570Sstevel@tonic-gate  */
33580Sstevel@tonic-gate local void
compress_block(s,ltree,dtree)33590Sstevel@tonic-gate compress_block(s, ltree, dtree)
33600Sstevel@tonic-gate     deflate_state *s;
33610Sstevel@tonic-gate     ct_data *ltree;	/* literal tree */
33620Sstevel@tonic-gate     ct_data *dtree;	/* distance tree */
33630Sstevel@tonic-gate {
33640Sstevel@tonic-gate 	unsigned dist;	/* distance of matched string */
33650Sstevel@tonic-gate 	int lc;	/* match length or unmatched char (if dist == 0) */
33660Sstevel@tonic-gate 	unsigned lx = 0;	/* running index in l_buf */
33670Sstevel@tonic-gate 	unsigned code;	/* the code to send */
33680Sstevel@tonic-gate 	int extra;	/* number of extra bits to send */
33690Sstevel@tonic-gate 
33700Sstevel@tonic-gate 	if (s->last_lit != 0) do {
33710Sstevel@tonic-gate 		dist = s->d_buf[lx];
33720Sstevel@tonic-gate 		lc = s->l_buf[lx++];
33730Sstevel@tonic-gate 		if (dist == 0) {
33740Sstevel@tonic-gate 			/* send a literal byte */
33750Sstevel@tonic-gate 			send_code(s, lc, ltree);
33760Sstevel@tonic-gate 			Tracecv(isgraph(lc), (stderr, " '%c' ", lc));
33770Sstevel@tonic-gate 		} else {
33780Sstevel@tonic-gate 			/* Here, lc is the match length - MIN_MATCH */
33790Sstevel@tonic-gate 			code = _length_code[lc];
33800Sstevel@tonic-gate 			/* send the length code */
33810Sstevel@tonic-gate 			send_code(s, code+LITERALS+1, ltree);
33820Sstevel@tonic-gate 			extra = extra_lbits[code];
33830Sstevel@tonic-gate 			if (extra != 0) {
33840Sstevel@tonic-gate 				lc -= base_length[code];
33850Sstevel@tonic-gate 				/* send the extra length bits */
33860Sstevel@tonic-gate 				send_bits(s, lc, extra);
33870Sstevel@tonic-gate 			}
33880Sstevel@tonic-gate 			/* dist is now the match distance - 1 */
33890Sstevel@tonic-gate 			dist--;
33900Sstevel@tonic-gate 			code = d_code(dist);
33910Sstevel@tonic-gate 			Assert(code < D_CODES, "bad d_code");
33920Sstevel@tonic-gate 
33930Sstevel@tonic-gate 			/* send the distance code */
33940Sstevel@tonic-gate 			send_code(s, code, dtree);
33950Sstevel@tonic-gate 			extra = extra_dbits[code];
33960Sstevel@tonic-gate 			if (extra != 0) {
33970Sstevel@tonic-gate 				dist -= base_dist[code];
33980Sstevel@tonic-gate 				/* send the extra distance bits */
33990Sstevel@tonic-gate 				send_bits(s, dist, extra);
34000Sstevel@tonic-gate 			}
34010Sstevel@tonic-gate 		} /* literal or match pair ? */
34020Sstevel@tonic-gate 
34030Sstevel@tonic-gate 		/*
34040Sstevel@tonic-gate 		 * Check that the overlay between pending_buf and
34050Sstevel@tonic-gate 		 * d_buf+l_buf is ok:
34060Sstevel@tonic-gate 		 */
34070Sstevel@tonic-gate 		Assert(s->pending < s->lit_bufsize + 2*lx,
34080Sstevel@tonic-gate 		    "pendingBuf overflow");
34090Sstevel@tonic-gate 
34100Sstevel@tonic-gate 	} while (lx < s->last_lit);
34110Sstevel@tonic-gate 
34120Sstevel@tonic-gate 	send_code(s, END_BLOCK, ltree);
34130Sstevel@tonic-gate 	s->last_eob_len = ltree[END_BLOCK].Len;
34140Sstevel@tonic-gate }
34150Sstevel@tonic-gate 
34160Sstevel@tonic-gate /*
34170Sstevel@tonic-gate  * ===========================================================================
34180Sstevel@tonic-gate  * Set the data type to ASCII or BINARY, using a crude approximation:
34190Sstevel@tonic-gate  * binary if more than 20% of the bytes are <= 6 or >= 128, ascii otherwise.
34200Sstevel@tonic-gate  * IN assertion: the fields freq of dyn_ltree are set and the total of all
34210Sstevel@tonic-gate  * frequencies does not exceed 64K (to fit in an int on 16 bit machines).
34220Sstevel@tonic-gate  */
34230Sstevel@tonic-gate local void
set_data_type(s)34240Sstevel@tonic-gate set_data_type(s)
34250Sstevel@tonic-gate     deflate_state *s;
34260Sstevel@tonic-gate {
34270Sstevel@tonic-gate 	int n = 0;
34280Sstevel@tonic-gate 	unsigned ascii_freq = 0;
34290Sstevel@tonic-gate 	unsigned bin_freq = 0;
34300Sstevel@tonic-gate 	while (n < 7)	bin_freq	+= s->dyn_ltree[n++].Freq;
34310Sstevel@tonic-gate 	while (n < 128)	ascii_freq	+= s->dyn_ltree[n++].Freq;
34320Sstevel@tonic-gate 	while (n < LITERALS) bin_freq	+= s->dyn_ltree[n++].Freq;
34330Sstevel@tonic-gate 	s->data_type = (Byte)(bin_freq > (ascii_freq >> 2) ?
34340Sstevel@tonic-gate 	    Z_BINARY : Z_ASCII);
34350Sstevel@tonic-gate }
34360Sstevel@tonic-gate 
34370Sstevel@tonic-gate /*
34380Sstevel@tonic-gate  * ===========================================================================
34390Sstevel@tonic-gate  * Reverse the first len bits of a code, using straightforward code (a faster
34400Sstevel@tonic-gate  * method would use a table)
34410Sstevel@tonic-gate  * IN assertion: 1 <= len <= 15
34420Sstevel@tonic-gate  */
34430Sstevel@tonic-gate local unsigned
bi_reverse(code,len)34440Sstevel@tonic-gate bi_reverse(code, len)
34450Sstevel@tonic-gate     unsigned code;	/* the value to invert */
34460Sstevel@tonic-gate     int len;	/* its bit length */
34470Sstevel@tonic-gate {
34480Sstevel@tonic-gate 	register unsigned res = 0;
34490Sstevel@tonic-gate 	do {
34500Sstevel@tonic-gate 		res |= code & 1;
34510Sstevel@tonic-gate 		code >>= 1, res <<= 1;
34520Sstevel@tonic-gate 	} while (--len > 0);
34530Sstevel@tonic-gate 	return (res >> 1);
34540Sstevel@tonic-gate }
34550Sstevel@tonic-gate 
34560Sstevel@tonic-gate /*
34570Sstevel@tonic-gate  * ===========================================================================
34580Sstevel@tonic-gate  * Flush the bit buffer, keeping at most 7 bits in it.
34590Sstevel@tonic-gate  */
34600Sstevel@tonic-gate local void
bi_flush(s)34610Sstevel@tonic-gate bi_flush(s)
34620Sstevel@tonic-gate     deflate_state *s;
34630Sstevel@tonic-gate {
34640Sstevel@tonic-gate 	if (s->bi_valid == 16) {
34650Sstevel@tonic-gate 		put_short(s, s->bi_buf);
34660Sstevel@tonic-gate 		s->bi_buf = 0;
34670Sstevel@tonic-gate 		s->bi_valid = 0;
34680Sstevel@tonic-gate 	} else if (s->bi_valid >= 8) {
34690Sstevel@tonic-gate 		put_byte(s, (Byte)s->bi_buf);
34700Sstevel@tonic-gate 		s->bi_buf >>= 8;
34710Sstevel@tonic-gate 		s->bi_valid -= 8;
34720Sstevel@tonic-gate 	}
34730Sstevel@tonic-gate }
34740Sstevel@tonic-gate 
34750Sstevel@tonic-gate /*
34760Sstevel@tonic-gate  * ===========================================================================
34770Sstevel@tonic-gate  * Flush the bit buffer and align the output on a byte boundary
34780Sstevel@tonic-gate  */
34790Sstevel@tonic-gate local void
bi_windup(s)34800Sstevel@tonic-gate bi_windup(s)
34810Sstevel@tonic-gate     deflate_state *s;
34820Sstevel@tonic-gate {
34830Sstevel@tonic-gate 	if (s->bi_valid > 8) {
34840Sstevel@tonic-gate 		put_short(s, s->bi_buf);
34850Sstevel@tonic-gate 	} else if (s->bi_valid > 0) {
34860Sstevel@tonic-gate 		put_byte(s, (Byte)s->bi_buf);
34870Sstevel@tonic-gate 	}
34880Sstevel@tonic-gate 	s->bi_buf = 0;
34890Sstevel@tonic-gate 	s->bi_valid = 0;
34900Sstevel@tonic-gate #ifdef DEBUG_ZLIB
34910Sstevel@tonic-gate 	s->bits_sent = (s->bits_sent+7) & ~7;
34920Sstevel@tonic-gate #endif
34930Sstevel@tonic-gate }
34940Sstevel@tonic-gate 
34950Sstevel@tonic-gate /*
34960Sstevel@tonic-gate  * ===========================================================================
34970Sstevel@tonic-gate  * Copy a stored block, storing first the length and its
34980Sstevel@tonic-gate  * one's complement if requested.
34990Sstevel@tonic-gate  */
35000Sstevel@tonic-gate local void
copy_block(s,buf,len,header)35010Sstevel@tonic-gate copy_block(s, buf, len, header)
35020Sstevel@tonic-gate     deflate_state *s;
35030Sstevel@tonic-gate     charf    *buf;	/* the input data */
35040Sstevel@tonic-gate     unsigned len;	/* its length */
35050Sstevel@tonic-gate     int	header;	/* true if block header must be written */
35060Sstevel@tonic-gate {
35070Sstevel@tonic-gate 	bi_windup(s);	/* align on byte boundary */
35080Sstevel@tonic-gate 	s->last_eob_len = 8;	/* enough lookahead for inflate */
35090Sstevel@tonic-gate 
35100Sstevel@tonic-gate 	if (header) {
35110Sstevel@tonic-gate 		put_short(s, (ush)len);
35120Sstevel@tonic-gate 		put_short(s, (ush)~len);
35130Sstevel@tonic-gate #ifdef DEBUG_ZLIB
35140Sstevel@tonic-gate 		s->bits_sent += 2*16;
35150Sstevel@tonic-gate #endif
35160Sstevel@tonic-gate 	}
35170Sstevel@tonic-gate #ifdef DEBUG_ZLIB
35180Sstevel@tonic-gate 	s->bits_sent += (ulg)len<<3;
35190Sstevel@tonic-gate #endif
35200Sstevel@tonic-gate 	/* bundle up the put_byte(s, *buf++) calls PPP */
35210Sstevel@tonic-gate 	Assert(s->pending + len < s->pending_buf_size, "pending_buf overrun");
35220Sstevel@tonic-gate 	zmemcpy(&s->pending_buf[s->pending], buf, len);	/* PPP */
35230Sstevel@tonic-gate 	s->pending += len;				/* PPP */
35240Sstevel@tonic-gate }
35250Sstevel@tonic-gate /* --- trees.c */
35260Sstevel@tonic-gate 
35270Sstevel@tonic-gate /* +++ inflate.c */
35280Sstevel@tonic-gate /*
35290Sstevel@tonic-gate  * inflate.c -- zlib interface to inflate modules
35300Sstevel@tonic-gate  * Copyright (C) 1995-1998 Mark Adler
35310Sstevel@tonic-gate  * For conditions of distribution and use, see copyright notice in zlib.h
35320Sstevel@tonic-gate  */
35330Sstevel@tonic-gate 
35340Sstevel@tonic-gate /* #include "zutil.h" */
35350Sstevel@tonic-gate 
35360Sstevel@tonic-gate /* +++ infblock.h */
35370Sstevel@tonic-gate /*
35380Sstevel@tonic-gate  * infblock.h -- header to use infblock.c
35390Sstevel@tonic-gate  * Copyright (C) 1995-1998 Mark Adler
35400Sstevel@tonic-gate  * For conditions of distribution and use, see copyright notice in zlib.h
35410Sstevel@tonic-gate  */
35420Sstevel@tonic-gate 
35430Sstevel@tonic-gate /*
35440Sstevel@tonic-gate  * WARNING: this file should *not* be used by applications. It is part
35450Sstevel@tonic-gate  * of the implementation of the compression library and is subject to
35460Sstevel@tonic-gate  * change. Applications should only use zlib.h.
35470Sstevel@tonic-gate  */
35480Sstevel@tonic-gate 
35490Sstevel@tonic-gate struct inflate_blocks_state;
35500Sstevel@tonic-gate typedef struct inflate_blocks_state FAR inflate_blocks_statef;
35510Sstevel@tonic-gate 
35520Sstevel@tonic-gate extern inflate_blocks_statef * inflate_blocks_new OF((
35530Sstevel@tonic-gate     z_streamp z,
35540Sstevel@tonic-gate     check_func c,	/* check function */
35550Sstevel@tonic-gate     uInt w));	/* window size */
35560Sstevel@tonic-gate 
35570Sstevel@tonic-gate extern int inflate_blocks OF((
35580Sstevel@tonic-gate     inflate_blocks_statef *,
35590Sstevel@tonic-gate     z_streamp,
35600Sstevel@tonic-gate     int));	/* initial return code */
35610Sstevel@tonic-gate 
35620Sstevel@tonic-gate extern void inflate_blocks_reset OF((
35630Sstevel@tonic-gate     inflate_blocks_statef *,
35640Sstevel@tonic-gate     z_streamp,
35650Sstevel@tonic-gate     uLongf *));	/* check value on output */
35660Sstevel@tonic-gate 
35670Sstevel@tonic-gate extern int inflate_blocks_free OF((
35680Sstevel@tonic-gate     inflate_blocks_statef *,
35690Sstevel@tonic-gate     z_streamp));
35700Sstevel@tonic-gate 
35710Sstevel@tonic-gate extern void inflate_set_dictionary OF((
35720Sstevel@tonic-gate     inflate_blocks_statef *s,
35730Sstevel@tonic-gate     const Bytef *d,  /* dictionary */
35740Sstevel@tonic-gate     uInt  n));	/* dictionary length */
35750Sstevel@tonic-gate 
35760Sstevel@tonic-gate extern int inflate_blocks_sync_point OF((
35770Sstevel@tonic-gate     inflate_blocks_statef *s));
35780Sstevel@tonic-gate 
35790Sstevel@tonic-gate /* PPP -- added function */
35800Sstevel@tonic-gate extern int inflate_addhistory OF((
35810Sstevel@tonic-gate     inflate_blocks_statef *,
35820Sstevel@tonic-gate     z_streamp));
35830Sstevel@tonic-gate 
35840Sstevel@tonic-gate /* PPP -- added function */
35850Sstevel@tonic-gate extern int inflate_packet_flush OF((
35860Sstevel@tonic-gate     inflate_blocks_statef *));
35870Sstevel@tonic-gate /* --- infblock.h */
35880Sstevel@tonic-gate 
35890Sstevel@tonic-gate #ifndef NO_DUMMY_DECL
35900Sstevel@tonic-gate struct inflate_blocks_state {int dummy; };	/* for buggy compilers */
35910Sstevel@tonic-gate #endif
35920Sstevel@tonic-gate 
35930Sstevel@tonic-gate /* inflate private state */
35940Sstevel@tonic-gate struct internal_state {
35950Sstevel@tonic-gate 
35960Sstevel@tonic-gate 	/* mode */
35970Sstevel@tonic-gate 	enum {
35980Sstevel@tonic-gate 		METHOD,	/* waiting for method byte */
35990Sstevel@tonic-gate 		FLAG,	/* waiting for flag byte */
36000Sstevel@tonic-gate 		DICT4,	/* four dictionary check bytes to go */
36010Sstevel@tonic-gate 		DICT3,	/* three dictionary check bytes to go */
36020Sstevel@tonic-gate 		DICT2,	/* two dictionary check bytes to go */
36030Sstevel@tonic-gate 		DICT1,	/* one dictionary check byte to go */
36040Sstevel@tonic-gate 		DICT0,	/* waiting for inflateSetDictionary */
36050Sstevel@tonic-gate 		BLOCKS,	/* decompressing blocks */
36060Sstevel@tonic-gate 		CHECK4,	/* four check bytes to go */
36070Sstevel@tonic-gate 		CHECK3,	/* three check bytes to go */
36080Sstevel@tonic-gate 		CHECK2,	/* two check bytes to go */
36090Sstevel@tonic-gate 		CHECK1,	/* one check byte to go */
36100Sstevel@tonic-gate 		DONE,	/* finished check, done */
36110Sstevel@tonic-gate 		BAD}	/* got an error--stay here */
36120Sstevel@tonic-gate 	mode;	/* current inflate mode */
36130Sstevel@tonic-gate 
36140Sstevel@tonic-gate 	/* mode dependent information */
36150Sstevel@tonic-gate 	union {
36160Sstevel@tonic-gate 		uInt method;	/* if FLAGS, method byte */
36170Sstevel@tonic-gate 		struct {
36180Sstevel@tonic-gate 			uLong was;	/* computed check value */
36190Sstevel@tonic-gate 			uLong need;	/* stream check value */
36200Sstevel@tonic-gate 		} check;	/* if CHECK, check values to compare */
36210Sstevel@tonic-gate 		uInt marker;	/* if BAD, inflateSync's marker bytes count */
36220Sstevel@tonic-gate 	} sub;	/* submode */
36230Sstevel@tonic-gate 
36240Sstevel@tonic-gate 	/* mode independent information */
36250Sstevel@tonic-gate 	int  nowrap;	/* flag for no wrapper */
36260Sstevel@tonic-gate 	uInt wbits;	/* log2(window size)  (8..15, defaults to 15) */
36270Sstevel@tonic-gate 	/* current inflate_blocks state */
36280Sstevel@tonic-gate 	inflate_blocks_statef *blocks;
36290Sstevel@tonic-gate };
36300Sstevel@tonic-gate 
36310Sstevel@tonic-gate 
36320Sstevel@tonic-gate int
inflateReset(z)36330Sstevel@tonic-gate inflateReset(z)
36340Sstevel@tonic-gate z_streamp z;
36350Sstevel@tonic-gate {
36360Sstevel@tonic-gate 	if (z == Z_NULL || z->state == Z_NULL)
36370Sstevel@tonic-gate 		return (Z_STREAM_ERROR);
36380Sstevel@tonic-gate 	z->total_in = z->total_out = 0;
36390Sstevel@tonic-gate 	z->msg = Z_NULL;
36400Sstevel@tonic-gate 	z->state->mode = z->state->nowrap ? BLOCKS : METHOD;
36410Sstevel@tonic-gate 	inflate_blocks_reset(z->state->blocks, z, Z_NULL);
36420Sstevel@tonic-gate 	Trace((stderr, "inflate: reset\n"));
36430Sstevel@tonic-gate 	return (Z_OK);
36440Sstevel@tonic-gate }
36450Sstevel@tonic-gate 
36460Sstevel@tonic-gate 
36470Sstevel@tonic-gate int
inflateEnd(z)36480Sstevel@tonic-gate inflateEnd(z)
36490Sstevel@tonic-gate z_streamp z;
36500Sstevel@tonic-gate {
36510Sstevel@tonic-gate 	if (z == Z_NULL || z->state == Z_NULL || z->zfree == Z_NULL)
36520Sstevel@tonic-gate 		return (Z_STREAM_ERROR);
36530Sstevel@tonic-gate 	if (z->state->blocks != Z_NULL) {
36540Sstevel@tonic-gate 		(void) inflate_blocks_free(z->state->blocks, z);
36550Sstevel@tonic-gate 		z->state->blocks = Z_NULL;
36560Sstevel@tonic-gate 	}
36570Sstevel@tonic-gate 	ZFREE(z, z->state);
36580Sstevel@tonic-gate 	z->state = Z_NULL;
36590Sstevel@tonic-gate 	Trace((stderr, "inflate: end\n"));
36600Sstevel@tonic-gate 	return (Z_OK);
36610Sstevel@tonic-gate }
36620Sstevel@tonic-gate 
36630Sstevel@tonic-gate 
36640Sstevel@tonic-gate int
inflateInit2_(z,w,version,stream_size)36650Sstevel@tonic-gate inflateInit2_(z, w, version, stream_size)
36660Sstevel@tonic-gate z_streamp z;
36670Sstevel@tonic-gate int w;
36680Sstevel@tonic-gate const char *version;
36690Sstevel@tonic-gate int stream_size;
36700Sstevel@tonic-gate {
36710Sstevel@tonic-gate 	if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
36720Sstevel@tonic-gate 	    stream_size != sizeof (z_stream))
36730Sstevel@tonic-gate 		return (Z_VERSION_ERROR);
36740Sstevel@tonic-gate 
36750Sstevel@tonic-gate 	/* initialize state */
36760Sstevel@tonic-gate 	if (z == Z_NULL)
36770Sstevel@tonic-gate 		return (Z_STREAM_ERROR);
36780Sstevel@tonic-gate 	z->msg = Z_NULL;
36790Sstevel@tonic-gate #ifndef NO_ZCFUNCS
36800Sstevel@tonic-gate 	if (z->zalloc == Z_NULL)
36810Sstevel@tonic-gate 	{
36820Sstevel@tonic-gate 		z->zalloc = zcalloc;
36830Sstevel@tonic-gate 		z->opaque = (voidpf)0;
36840Sstevel@tonic-gate 	}
36850Sstevel@tonic-gate 	if (z->zfree == Z_NULL) z->zfree = zcfree;
36860Sstevel@tonic-gate #endif
36870Sstevel@tonic-gate 	if ((z->state = (struct internal_state FAR *)
36880Sstevel@tonic-gate 	    ZALLOC(z, 1, sizeof (struct internal_state))) == Z_NULL)
36890Sstevel@tonic-gate 		return (Z_MEM_ERROR);
36900Sstevel@tonic-gate 	z->state->blocks = Z_NULL;
36910Sstevel@tonic-gate 
36920Sstevel@tonic-gate 	/* handle undocumented nowrap option (no zlib header or check) */
36930Sstevel@tonic-gate 	z->state->nowrap = 0;
36940Sstevel@tonic-gate 	if (w < 0)
36950Sstevel@tonic-gate 	{
36960Sstevel@tonic-gate 		w = - w;
36970Sstevel@tonic-gate 		z->state->nowrap = 1;
36980Sstevel@tonic-gate 	}
36990Sstevel@tonic-gate 
37000Sstevel@tonic-gate 	/* set window size */
37010Sstevel@tonic-gate 	if (w < 8 || w > 15)
37020Sstevel@tonic-gate 	{
37030Sstevel@tonic-gate 		(void) inflateEnd(z);
37040Sstevel@tonic-gate 		return (Z_STREAM_ERROR);
37050Sstevel@tonic-gate 	}
37060Sstevel@tonic-gate 	z->state->wbits = (uInt)w;
37070Sstevel@tonic-gate 
37080Sstevel@tonic-gate 	/* create inflate_blocks state */
37090Sstevel@tonic-gate 	if ((z->state->blocks =
37100Sstevel@tonic-gate 	    inflate_blocks_new(z, z->state->nowrap ?
37110Sstevel@tonic-gate 		Z_NULL : adler32, (uInt)1 << w))
37120Sstevel@tonic-gate 	    == Z_NULL)
37130Sstevel@tonic-gate 	{
37140Sstevel@tonic-gate 		(void) inflateEnd(z);
37150Sstevel@tonic-gate 		return (Z_MEM_ERROR);
37160Sstevel@tonic-gate 	}
37170Sstevel@tonic-gate 	Trace((stderr, "inflate: allocated\n"));
37180Sstevel@tonic-gate 
37190Sstevel@tonic-gate 	/* reset state */
37200Sstevel@tonic-gate 	(void) inflateReset(z);
37210Sstevel@tonic-gate 	return (Z_OK);
37220Sstevel@tonic-gate }
37230Sstevel@tonic-gate 
37240Sstevel@tonic-gate 
37250Sstevel@tonic-gate int
inflateInit_(z,version,stream_size)37260Sstevel@tonic-gate inflateInit_(z, version, stream_size)
37270Sstevel@tonic-gate z_streamp z;
37280Sstevel@tonic-gate const char *version;
37290Sstevel@tonic-gate int stream_size;
37300Sstevel@tonic-gate {
37310Sstevel@tonic-gate 	return (inflateInit2_(z, DEF_WBITS, version, stream_size));
37320Sstevel@tonic-gate }
37330Sstevel@tonic-gate 
37340Sstevel@tonic-gate /* PPP -- added "empty" label and changed f to Z_OK */
37350Sstevel@tonic-gate #define	NEEDBYTE {if (z->avail_in == 0) goto empty; r = Z_OK; } ((void)0)
37360Sstevel@tonic-gate #define	NEXTBYTE (z->avail_in--, z->total_in++, *z->next_in++)
37370Sstevel@tonic-gate 
37380Sstevel@tonic-gate int
inflate(z,f)37390Sstevel@tonic-gate inflate(z, f)
37400Sstevel@tonic-gate z_streamp z;
37410Sstevel@tonic-gate int f;
37420Sstevel@tonic-gate {
37430Sstevel@tonic-gate 	int r;
37440Sstevel@tonic-gate 	uInt b;
37450Sstevel@tonic-gate 
37460Sstevel@tonic-gate 	if (z == Z_NULL || z->state == Z_NULL || z->next_in == Z_NULL)
37470Sstevel@tonic-gate 		return (Z_STREAM_ERROR);
37480Sstevel@tonic-gate 	/* f = f == Z_FINISH ? Z_BUF_ERROR : Z_OK; -- PPP; Z_FINISH unused */
37490Sstevel@tonic-gate 	r = Z_BUF_ERROR;
37500Sstevel@tonic-gate 	/* CONSTCOND */
37510Sstevel@tonic-gate 	while (1)
37520Sstevel@tonic-gate 		switch (z->state->mode)
37530Sstevel@tonic-gate 	{
37540Sstevel@tonic-gate 	case METHOD:
37550Sstevel@tonic-gate 		NEEDBYTE;
37560Sstevel@tonic-gate 		if (((z->state->sub.method = NEXTBYTE) & 0xf) != Z_DEFLATED)
37570Sstevel@tonic-gate 		{
37580Sstevel@tonic-gate 			z->state->mode = BAD;
37590Sstevel@tonic-gate 			z->msg = "unknown compression method";
37600Sstevel@tonic-gate 			/* can't try inflateSync */
37610Sstevel@tonic-gate 			z->state->sub.marker = 5;
37620Sstevel@tonic-gate 			break;
37630Sstevel@tonic-gate 		}
37640Sstevel@tonic-gate 		if ((z->state->sub.method >> 4) + 8 > z->state->wbits)
37650Sstevel@tonic-gate 		{
37660Sstevel@tonic-gate 			z->state->mode = BAD;
37670Sstevel@tonic-gate 			z->msg = "invalid window size";
37680Sstevel@tonic-gate 			/* can't try inflateSync */
37690Sstevel@tonic-gate 			z->state->sub.marker = 5;
37700Sstevel@tonic-gate 			break;
37710Sstevel@tonic-gate 		}
37720Sstevel@tonic-gate 		z->state->mode = FLAG;
37730Sstevel@tonic-gate 		/* FALLTHRU */
37740Sstevel@tonic-gate 	case FLAG:
37750Sstevel@tonic-gate 		NEEDBYTE;
37760Sstevel@tonic-gate 		b = NEXTBYTE;
37770Sstevel@tonic-gate 		if (((z->state->sub.method << 8) + b) % 31)
37780Sstevel@tonic-gate 		{
37790Sstevel@tonic-gate 			z->state->mode = BAD;
37800Sstevel@tonic-gate 			z->msg = "incorrect header check";
37810Sstevel@tonic-gate 			/* can't try inflateSync */
37820Sstevel@tonic-gate 			z->state->sub.marker = 5;
37830Sstevel@tonic-gate 			break;
37840Sstevel@tonic-gate 		}
37850Sstevel@tonic-gate 		Trace((stderr, "inflate: zlib header ok\n"));
37860Sstevel@tonic-gate 		if (!(b & PRESET_DICT))
37870Sstevel@tonic-gate 		{
37880Sstevel@tonic-gate 			z->state->mode = BLOCKS;
37890Sstevel@tonic-gate 			break;
37900Sstevel@tonic-gate 		}
37910Sstevel@tonic-gate 		z->state->mode = DICT4;
37920Sstevel@tonic-gate 		/* FALLTHRU */
37930Sstevel@tonic-gate 	case DICT4:
37940Sstevel@tonic-gate 		NEEDBYTE;
37950Sstevel@tonic-gate 		z->state->sub.check.need = (uLong)NEXTBYTE << 24;
37960Sstevel@tonic-gate 		z->state->mode = DICT3;
37970Sstevel@tonic-gate 		/* FALLTHRU */
37980Sstevel@tonic-gate 	case DICT3:
37990Sstevel@tonic-gate 		NEEDBYTE;
38000Sstevel@tonic-gate 		z->state->sub.check.need += (uLong)NEXTBYTE << 16;
38010Sstevel@tonic-gate 		z->state->mode = DICT2;
38020Sstevel@tonic-gate 		/* FALLTHRU */
38030Sstevel@tonic-gate 	case DICT2:
38040Sstevel@tonic-gate 		NEEDBYTE;
38050Sstevel@tonic-gate 		z->state->sub.check.need += (uLong)NEXTBYTE << 8;
38060Sstevel@tonic-gate 		z->state->mode = DICT1;
38070Sstevel@tonic-gate 		/* FALLTHRU */
38080Sstevel@tonic-gate 	case DICT1:
38090Sstevel@tonic-gate 		NEEDBYTE;
38100Sstevel@tonic-gate 		z->state->sub.check.need += (uLong)NEXTBYTE;
38110Sstevel@tonic-gate 		z->adler = z->state->sub.check.need;
38120Sstevel@tonic-gate 		z->state->mode = DICT0;
38130Sstevel@tonic-gate 		return (Z_NEED_DICT);
38140Sstevel@tonic-gate 	case DICT0:
38150Sstevel@tonic-gate 		z->state->mode = BAD;
38160Sstevel@tonic-gate 		z->msg = "need dictionary";
38170Sstevel@tonic-gate 		z->state->sub.marker = 0;	/* can try inflateSync */
38180Sstevel@tonic-gate 		return (Z_STREAM_ERROR);
38190Sstevel@tonic-gate 	case BLOCKS:
38200Sstevel@tonic-gate 		r = inflate_blocks(z->state->blocks, z, r);
38210Sstevel@tonic-gate 		if (f == Z_PACKET_FLUSH && z->avail_in == 0 &&	/* PPP */
38220Sstevel@tonic-gate 		    z->avail_out != 0)				/* PPP */
38230Sstevel@tonic-gate 			r = inflate_packet_flush(z->state->blocks); /* PPP */
38240Sstevel@tonic-gate 		if (r == Z_DATA_ERROR)
38250Sstevel@tonic-gate 		{
38260Sstevel@tonic-gate 			z->state->mode = BAD;
38270Sstevel@tonic-gate 			/* can try inflateSync */
38280Sstevel@tonic-gate 			z->state->sub.marker = 0;
38290Sstevel@tonic-gate 			break;
38300Sstevel@tonic-gate 		}
38310Sstevel@tonic-gate 		/* PPP */
38320Sstevel@tonic-gate 		if (r != Z_STREAM_END)
38330Sstevel@tonic-gate 			return (r);
38340Sstevel@tonic-gate 		r = Z_OK;	/* PPP */
38350Sstevel@tonic-gate 		inflate_blocks_reset(z->state->blocks, z,
38360Sstevel@tonic-gate 		    &z->state->sub.check.was);
38370Sstevel@tonic-gate 		if (z->state->nowrap)
38380Sstevel@tonic-gate 		{
38390Sstevel@tonic-gate 			z->state->mode = DONE;
38400Sstevel@tonic-gate 			break;
38410Sstevel@tonic-gate 		}
38420Sstevel@tonic-gate 		z->state->mode = CHECK4;
38430Sstevel@tonic-gate 		/* FALLTHRU */
38440Sstevel@tonic-gate 	case CHECK4:
38450Sstevel@tonic-gate 		NEEDBYTE;
38460Sstevel@tonic-gate 		z->state->sub.check.need = (uLong)NEXTBYTE << 24;
38470Sstevel@tonic-gate 		z->state->mode = CHECK3;
38480Sstevel@tonic-gate 		/* FALLTHRU */
38490Sstevel@tonic-gate 	case CHECK3:
38500Sstevel@tonic-gate 		NEEDBYTE;
38510Sstevel@tonic-gate 		z->state->sub.check.need += (uLong)NEXTBYTE << 16;
38520Sstevel@tonic-gate 		z->state->mode = CHECK2;
38530Sstevel@tonic-gate 		/* FALLTHRU */
38540Sstevel@tonic-gate 	case CHECK2:
38550Sstevel@tonic-gate 		NEEDBYTE;
38560Sstevel@tonic-gate 		z->state->sub.check.need += (uLong)NEXTBYTE << 8;
38570Sstevel@tonic-gate 		z->state->mode = CHECK1;
38580Sstevel@tonic-gate 		/* FALLTHRU */
38590Sstevel@tonic-gate 	case CHECK1:
38600Sstevel@tonic-gate 		NEEDBYTE;
38610Sstevel@tonic-gate 		z->state->sub.check.need += (uLong)NEXTBYTE;
38620Sstevel@tonic-gate 
38630Sstevel@tonic-gate 		if (z->state->sub.check.was != z->state->sub.check.need)
38640Sstevel@tonic-gate 		{
38650Sstevel@tonic-gate 			z->state->mode = BAD;
38660Sstevel@tonic-gate 			z->msg = "incorrect data check";
38670Sstevel@tonic-gate 			/* can't try inflateSync */
38680Sstevel@tonic-gate 			z->state->sub.marker = 5;
38690Sstevel@tonic-gate 			break;
38700Sstevel@tonic-gate 		}
38710Sstevel@tonic-gate 		Trace((stderr, "inflate: zlib check ok\n"));
38720Sstevel@tonic-gate 		z->state->mode = DONE;
38730Sstevel@tonic-gate 		/* FALLTHRU */
38740Sstevel@tonic-gate 	case DONE:
38750Sstevel@tonic-gate 		return (Z_STREAM_END);
38760Sstevel@tonic-gate 	case BAD:
38770Sstevel@tonic-gate 		return (Z_DATA_ERROR);
38780Sstevel@tonic-gate 	default:
38790Sstevel@tonic-gate 		return (Z_STREAM_ERROR);
38800Sstevel@tonic-gate 	}
38810Sstevel@tonic-gate 
38820Sstevel@tonic-gate /* PPP -- packet flush handling */
38830Sstevel@tonic-gate empty:
38840Sstevel@tonic-gate 	if (f != Z_PACKET_FLUSH)
38850Sstevel@tonic-gate 		return (r);
38860Sstevel@tonic-gate 	z->state->mode = BAD;
38870Sstevel@tonic-gate 	z->msg = "need more for packet flush";
38880Sstevel@tonic-gate 	z->state->sub.marker = 0;	/* can try inflateSync */
38890Sstevel@tonic-gate 	return (Z_DATA_ERROR);
38900Sstevel@tonic-gate }
38910Sstevel@tonic-gate 
38920Sstevel@tonic-gate 
38930Sstevel@tonic-gate int
inflateSetDictionary(z,dictionary,dictLength)38940Sstevel@tonic-gate inflateSetDictionary(z, dictionary, dictLength)
38950Sstevel@tonic-gate z_streamp z;
38960Sstevel@tonic-gate const Bytef *dictionary;
38970Sstevel@tonic-gate uInt  dictLength;
38980Sstevel@tonic-gate {
38990Sstevel@tonic-gate 	uInt length = dictLength;
39000Sstevel@tonic-gate 
39010Sstevel@tonic-gate 	if (z == Z_NULL || z->state == Z_NULL || z->state->mode != DICT0)
39020Sstevel@tonic-gate 		return (Z_STREAM_ERROR);
39030Sstevel@tonic-gate 
39040Sstevel@tonic-gate 	if (adler32(1L, dictionary, dictLength) != z->adler)
39050Sstevel@tonic-gate 		return (Z_DATA_ERROR);
39060Sstevel@tonic-gate 	z->adler = 1L;
39070Sstevel@tonic-gate 
39080Sstevel@tonic-gate 	if (length >= ((uInt)1<<z->state->wbits))
39090Sstevel@tonic-gate 	{
39100Sstevel@tonic-gate 		length = (1<<z->state->wbits)-1;
39110Sstevel@tonic-gate 		dictionary += dictLength - length;
39120Sstevel@tonic-gate 	}
39130Sstevel@tonic-gate 	inflate_set_dictionary(z->state->blocks, dictionary, length);
39140Sstevel@tonic-gate 	z->state->mode = BLOCKS;
39150Sstevel@tonic-gate 	return (Z_OK);
39160Sstevel@tonic-gate }
39170Sstevel@tonic-gate 
39180Sstevel@tonic-gate /*
39190Sstevel@tonic-gate  * This subroutine adds the data at next_in/avail_in to the output history
39200Sstevel@tonic-gate  * without performing any output.  The output buffer must be "caught up";
39210Sstevel@tonic-gate  * i.e. no pending output (hence s->read equals s->write), and the state must
39220Sstevel@tonic-gate  * be BLOCKS (i.e. we should be willing to see the start of a series of
39230Sstevel@tonic-gate  * BLOCKS).  On exit, the output will also be caught up, and the checksum
39240Sstevel@tonic-gate  * will have been updated if need be.
39250Sstevel@tonic-gate  *
39260Sstevel@tonic-gate  * Added for PPP.
39270Sstevel@tonic-gate  */
39280Sstevel@tonic-gate 
39290Sstevel@tonic-gate int
inflateIncomp(z)39300Sstevel@tonic-gate inflateIncomp(z)
39310Sstevel@tonic-gate z_stream *z;
39320Sstevel@tonic-gate {
39330Sstevel@tonic-gate 	if (z->state->mode != BLOCKS)
39340Sstevel@tonic-gate 		return (Z_DATA_ERROR);
39350Sstevel@tonic-gate 	return (inflate_addhistory(z->state->blocks, z));
39360Sstevel@tonic-gate }
39370Sstevel@tonic-gate 
39380Sstevel@tonic-gate 
39390Sstevel@tonic-gate int
inflateSync(z)39400Sstevel@tonic-gate inflateSync(z)
39410Sstevel@tonic-gate z_streamp z;
39420Sstevel@tonic-gate {
39430Sstevel@tonic-gate 	uInt n;	/* number of bytes to look at */
39440Sstevel@tonic-gate 	Bytef *p;	/* pointer to bytes */
39450Sstevel@tonic-gate 	uInt m;	/* number of marker bytes found in a row */
39460Sstevel@tonic-gate 	uLong r, w;	/* temporaries to save total_in and total_out */
39470Sstevel@tonic-gate 
39480Sstevel@tonic-gate 	/* set up */
39490Sstevel@tonic-gate 	if (z == Z_NULL || z->state == Z_NULL)
39500Sstevel@tonic-gate 		return (Z_STREAM_ERROR);
39510Sstevel@tonic-gate 	if (z->state->mode != BAD)
39520Sstevel@tonic-gate 	{
39530Sstevel@tonic-gate 		z->state->mode = BAD;
39540Sstevel@tonic-gate 		z->state->sub.marker = 0;
39550Sstevel@tonic-gate 	}
39560Sstevel@tonic-gate 	if ((n = z->avail_in) == 0)
39570Sstevel@tonic-gate 		return (Z_BUF_ERROR);
39580Sstevel@tonic-gate 	p = z->next_in;
39590Sstevel@tonic-gate 	m = z->state->sub.marker;
39600Sstevel@tonic-gate 
39610Sstevel@tonic-gate 	/* search */
39620Sstevel@tonic-gate 	while (n && m < 4)
39630Sstevel@tonic-gate 	{
39640Sstevel@tonic-gate 		static const Byte mark[4] = { 0, 0, 0xff, 0xff };
39650Sstevel@tonic-gate 		if (*p == mark[m])
39660Sstevel@tonic-gate 			m++;
39670Sstevel@tonic-gate 		else if (*p)
39680Sstevel@tonic-gate 			m = 0;
39690Sstevel@tonic-gate 		else
39700Sstevel@tonic-gate 			/*
39710Sstevel@tonic-gate 			 * This statement maps 2->2 and 3->1 because a
39720Sstevel@tonic-gate 			 * mismatch with input byte 0x00 on the first
39730Sstevel@tonic-gate 			 * 0xFF in the pattern means that we still
39740Sstevel@tonic-gate 			 * have two contiguous zeros matched (thus
39750Sstevel@tonic-gate 			 * offset 2 is kept), but a mismatch on the
39760Sstevel@tonic-gate 			 * second 0xFF means that only one 0x00 byte
39770Sstevel@tonic-gate 			 * has been matched.  (Boyer-Moore like
39780Sstevel@tonic-gate 			 * search.)
39790Sstevel@tonic-gate 			 */
39800Sstevel@tonic-gate 			m = 4 - m;
39810Sstevel@tonic-gate 		p++, n--;
39820Sstevel@tonic-gate 	}
39830Sstevel@tonic-gate 
39840Sstevel@tonic-gate 	/* restore */
39850Sstevel@tonic-gate 	z->total_in += p - z->next_in;
39860Sstevel@tonic-gate 	z->next_in = p;
39870Sstevel@tonic-gate 	z->avail_in = n;
39880Sstevel@tonic-gate 	z->state->sub.marker = m;
39890Sstevel@tonic-gate 
39900Sstevel@tonic-gate 	/* return no joy or set up to restart on a new block */
39910Sstevel@tonic-gate 	if (m != 4)
39920Sstevel@tonic-gate 		return (Z_DATA_ERROR);
39930Sstevel@tonic-gate 	r = z->total_in;  w = z->total_out;
39940Sstevel@tonic-gate 	(void) inflateReset(z);
39950Sstevel@tonic-gate 	z->total_in = r;  z->total_out = w;
39960Sstevel@tonic-gate 	z->state->mode = BLOCKS;
39970Sstevel@tonic-gate 	return (Z_OK);
39980Sstevel@tonic-gate }
39990Sstevel@tonic-gate 
40000Sstevel@tonic-gate /*
40010Sstevel@tonic-gate  * Returns true if inflate is currently at the end of a block
40020Sstevel@tonic-gate  * generated by Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by
40030Sstevel@tonic-gate  * one PPP implementation to provide an additional safety check. PPP
40040Sstevel@tonic-gate  * uses Z_SYNC_FLUSH but removes the length bytes of the resulting
40050Sstevel@tonic-gate  * empty stored block. When decompressing, PPP checks that at the end
40060Sstevel@tonic-gate  * of input packet, inflate is waiting for these length bytes.
40070Sstevel@tonic-gate  */
40080Sstevel@tonic-gate int
inflateSyncPoint(z)40090Sstevel@tonic-gate inflateSyncPoint(z)
40100Sstevel@tonic-gate z_streamp z;
40110Sstevel@tonic-gate {
40120Sstevel@tonic-gate 	if (z == Z_NULL || z->state == Z_NULL || z->state->blocks == Z_NULL)
40130Sstevel@tonic-gate 		return (Z_STREAM_ERROR);
40140Sstevel@tonic-gate 	return (inflate_blocks_sync_point(z->state->blocks));
40150Sstevel@tonic-gate }
40160Sstevel@tonic-gate 
40170Sstevel@tonic-gate #undef NEEDBYTE
40180Sstevel@tonic-gate #undef NEXTBYTE
40190Sstevel@tonic-gate /* --- inflate.c */
40200Sstevel@tonic-gate 
40210Sstevel@tonic-gate /* +++ infblock.c */
40220Sstevel@tonic-gate /*
40230Sstevel@tonic-gate  * infblock.c -- interpret and process block types to last block
40240Sstevel@tonic-gate  * Copyright (C) 1995-1998 Mark Adler
40250Sstevel@tonic-gate  * For conditions of distribution and use, see copyright notice in zlib.h
40260Sstevel@tonic-gate  */
40270Sstevel@tonic-gate 
40280Sstevel@tonic-gate /* #include "zutil.h" */
40290Sstevel@tonic-gate /* #include "infblock.h" */
40300Sstevel@tonic-gate 
40310Sstevel@tonic-gate /* +++ inftrees.h */
40320Sstevel@tonic-gate /*
40330Sstevel@tonic-gate  * inftrees.h -- header to use inftrees.c
40340Sstevel@tonic-gate  * Copyright (C) 1995-1998 Mark Adler
40350Sstevel@tonic-gate  * For conditions of distribution and use, see copyright notice in zlib.h
40360Sstevel@tonic-gate  */
40370Sstevel@tonic-gate 
40380Sstevel@tonic-gate /*
40390Sstevel@tonic-gate  * WARNING: this file should *not* be used by applications. It is part
40400Sstevel@tonic-gate  * of the implementation of the compression library and is subject to
40410Sstevel@tonic-gate  * change. Applications should only use zlib.h.
40420Sstevel@tonic-gate  */
40430Sstevel@tonic-gate 
40440Sstevel@tonic-gate /*
40450Sstevel@tonic-gate  * Huffman code lookup table entry--this entry is four bytes for
40460Sstevel@tonic-gate  * machines that have 16-bit pointers (e.g. PC's in the small or
40470Sstevel@tonic-gate  * medium model).
40480Sstevel@tonic-gate  */
40490Sstevel@tonic-gate 
40500Sstevel@tonic-gate typedef struct inflate_huft_s FAR inflate_huft;
40510Sstevel@tonic-gate 
40520Sstevel@tonic-gate struct inflate_huft_s {
40530Sstevel@tonic-gate 	union {
40540Sstevel@tonic-gate 		struct {
40550Sstevel@tonic-gate 			Byte Exop;	/* number of extra bits or operation */
40560Sstevel@tonic-gate 			/* number of bits in this code or subcode */
40570Sstevel@tonic-gate 			Byte Bits;
40580Sstevel@tonic-gate 		} what;
40590Sstevel@tonic-gate 		Bytef *pad;	/* pad structure to a power of 2 (4 bytes for */
40600Sstevel@tonic-gate 	} word;	/*  16-bit, 8 bytes for 32-bit machines) */
40610Sstevel@tonic-gate 	/* literal, length base, distance base, or table offset */
40620Sstevel@tonic-gate 	uInt base;
40630Sstevel@tonic-gate };
40640Sstevel@tonic-gate 
40650Sstevel@tonic-gate /*
40660Sstevel@tonic-gate  * Maximum size of dynamic tree.  The maximum found in a long but non-
40670Sstevel@tonic-gate  * exhaustive search was 1004 huft structures (850 for length/literals
40680Sstevel@tonic-gate  * and 154 for distances, the latter actually the result of an
40690Sstevel@tonic-gate  * exhaustive search).  The actual maximum is not known, but the value
40700Sstevel@tonic-gate  * below is more than safe.
40710Sstevel@tonic-gate  */
40720Sstevel@tonic-gate #define	MANY 1440
40730Sstevel@tonic-gate 
40740Sstevel@tonic-gate extern int inflate_trees_bits OF((
40750Sstevel@tonic-gate     uIntf *,			/* 19 code lengths */
40760Sstevel@tonic-gate     uIntf *,			/* bits tree desired/actual depth */
40770Sstevel@tonic-gate     inflate_huft * FAR *,	/* bits tree result */
40780Sstevel@tonic-gate     inflate_huft *,		/* space for trees */
40790Sstevel@tonic-gate     z_streamp));	/* for zalloc, zfree functions */
40800Sstevel@tonic-gate 
40810Sstevel@tonic-gate extern int inflate_trees_dynamic OF((
40820Sstevel@tonic-gate     uInt,	/* number of literal/length codes */
40830Sstevel@tonic-gate     uInt,	/* number of distance codes */
40840Sstevel@tonic-gate     uIntf *,	/* that many (total) code lengths */
40850Sstevel@tonic-gate     uIntf *,	/* literal desired/actual bit depth */
40860Sstevel@tonic-gate     uIntf *,	/* distance desired/actual bit depth */
40870Sstevel@tonic-gate     inflate_huft * FAR *,	/* literal/length tree result */
40880Sstevel@tonic-gate     inflate_huft * FAR *,	/* distance tree result */
40890Sstevel@tonic-gate     inflate_huft *,		/* space for trees */
40900Sstevel@tonic-gate     z_streamp));	/* for zalloc, zfree functions */
40910Sstevel@tonic-gate 
40920Sstevel@tonic-gate extern int inflate_trees_fixed OF((
40930Sstevel@tonic-gate     uIntf *,	/* literal desired/actual bit depth */
40940Sstevel@tonic-gate     uIntf *,	/* distance desired/actual bit depth */
40950Sstevel@tonic-gate     const inflate_huft * FAR *,	/* literal/length tree result */
40960Sstevel@tonic-gate     const inflate_huft * FAR *,	/* distance tree result */
40970Sstevel@tonic-gate     z_streamp));
40980Sstevel@tonic-gate 
40990Sstevel@tonic-gate /* --- inftrees.h */
41000Sstevel@tonic-gate 
41010Sstevel@tonic-gate /* +++ infcodes.h */
41020Sstevel@tonic-gate /*
41030Sstevel@tonic-gate  * infcodes.h -- header to use infcodes.c
41040Sstevel@tonic-gate  * Copyright (C) 1995-1998 Mark Adler
41050Sstevel@tonic-gate  * For conditions of distribution and use, see copyright notice in zlib.h
41060Sstevel@tonic-gate  */
41070Sstevel@tonic-gate 
41080Sstevel@tonic-gate /*
41090Sstevel@tonic-gate  * WARNING: this file should *not* be used by applications. It is part
41100Sstevel@tonic-gate  * of the implementation of the compression library and is subject to
41110Sstevel@tonic-gate  * change. Applications should only use zlib.h.
41120Sstevel@tonic-gate  */
41130Sstevel@tonic-gate 
41140Sstevel@tonic-gate struct inflate_codes_state;
41150Sstevel@tonic-gate typedef struct inflate_codes_state FAR inflate_codes_statef;
41160Sstevel@tonic-gate 
41170Sstevel@tonic-gate extern inflate_codes_statef *inflate_codes_new OF((
41180Sstevel@tonic-gate     uInt, uInt,
41190Sstevel@tonic-gate     const inflate_huft *, const inflate_huft *,
41200Sstevel@tonic-gate     z_streamp));
41210Sstevel@tonic-gate 
41220Sstevel@tonic-gate extern int inflate_codes OF((
41230Sstevel@tonic-gate     inflate_blocks_statef *,
41240Sstevel@tonic-gate     z_streamp,
41250Sstevel@tonic-gate     int));
41260Sstevel@tonic-gate 
41270Sstevel@tonic-gate extern void inflate_codes_free OF((
41280Sstevel@tonic-gate     inflate_codes_statef *,
41290Sstevel@tonic-gate     z_streamp));
41300Sstevel@tonic-gate 
41310Sstevel@tonic-gate /* --- infcodes.h */
41320Sstevel@tonic-gate 
41330Sstevel@tonic-gate /* +++ infutil.h */
41340Sstevel@tonic-gate /*
41350Sstevel@tonic-gate  * infutil.h -- types and macros common to blocks and codes
41360Sstevel@tonic-gate  * Copyright (C) 1995-1998 Mark Adler
41370Sstevel@tonic-gate  * For conditions of distribution and use, see copyright notice in zlib.h
41380Sstevel@tonic-gate  */
41390Sstevel@tonic-gate 
41400Sstevel@tonic-gate /*
41410Sstevel@tonic-gate  * WARNING: this file should *not* be used by applications. It is part
41420Sstevel@tonic-gate  * of the implementation of the compression library and is subject to
41430Sstevel@tonic-gate  * change. Applications should only use zlib.h.
41440Sstevel@tonic-gate  */
41450Sstevel@tonic-gate 
41460Sstevel@tonic-gate #ifndef _INFUTIL_H
41470Sstevel@tonic-gate #define	_INFUTIL_H
41480Sstevel@tonic-gate 
41490Sstevel@tonic-gate typedef enum {
41500Sstevel@tonic-gate 	TYPE,	/* get type bits (3, including end bit) */
41510Sstevel@tonic-gate 	LENS,	/* get lengths for stored */
41520Sstevel@tonic-gate 	STORED,	/* processing stored block */
41530Sstevel@tonic-gate 	TABLE,	/* get table lengths */
41540Sstevel@tonic-gate 	BTREE,	/* get bit lengths tree for a dynamic block */
41550Sstevel@tonic-gate 	DTREE,	/* get length, distance trees for a dynamic block */
41560Sstevel@tonic-gate 	CODES,	/* processing fixed or dynamic block */
41570Sstevel@tonic-gate 	DRY,	/* output remaining window bytes */
41580Sstevel@tonic-gate 	DONEB,	/* finished last block, done */
41590Sstevel@tonic-gate 	BADB}	/* got a data error--stuck here */
41600Sstevel@tonic-gate inflate_block_mode;
41610Sstevel@tonic-gate 
41620Sstevel@tonic-gate /* inflate blocks semi-private state */
41630Sstevel@tonic-gate struct inflate_blocks_state {
41640Sstevel@tonic-gate 
41650Sstevel@tonic-gate 	/* mode */
41660Sstevel@tonic-gate 	inflate_block_mode  mode;	/* current inflate_block mode */
41670Sstevel@tonic-gate 
41680Sstevel@tonic-gate 	/* mode dependent information */
41690Sstevel@tonic-gate 	union {
41700Sstevel@tonic-gate 		uInt left;	/* if STORED, bytes left to copy */
41710Sstevel@tonic-gate 		struct {
41720Sstevel@tonic-gate 			uInt table;	/* table lengths (14 bits) */
41730Sstevel@tonic-gate 			uInt index;	/* index into blens (or border) */
41740Sstevel@tonic-gate 			uIntf *blens;	/* bit lengths of codes */
41750Sstevel@tonic-gate 			uInt bb;	/* bit length tree depth */
41760Sstevel@tonic-gate 			inflate_huft *tb;	/* bit length decoding tree */
41770Sstevel@tonic-gate 		} trees;	/* if DTREE, decoding info for trees */
41780Sstevel@tonic-gate 		struct {
41790Sstevel@tonic-gate 			inflate_codes_statef *codes;
41800Sstevel@tonic-gate 		} decode;	/* if CODES, current state */
41810Sstevel@tonic-gate 	} sub;	/* submode */
41820Sstevel@tonic-gate 	uInt last;	/* true if this block is the last block */
41830Sstevel@tonic-gate 
41840Sstevel@tonic-gate 	/* mode independent information */
41850Sstevel@tonic-gate 	uInt bitk;	/* bits in bit buffer */
41860Sstevel@tonic-gate 	uLong bitb;	/* bit buffer */
41870Sstevel@tonic-gate 	inflate_huft *hufts;  /* single malloc for tree space */
41880Sstevel@tonic-gate 	Bytef *window;	/* sliding window */
41890Sstevel@tonic-gate 	Bytef *end;	/* one byte after sliding window */
41900Sstevel@tonic-gate 	Bytef *read;	/* window read pointer */
41910Sstevel@tonic-gate 	Bytef *write;	/* window write pointer */
41920Sstevel@tonic-gate 	check_func checkfn;	/* check function */
41930Sstevel@tonic-gate 	uLong check;	/* check on output */
41940Sstevel@tonic-gate 
41950Sstevel@tonic-gate };
41960Sstevel@tonic-gate 
41970Sstevel@tonic-gate 
41980Sstevel@tonic-gate /* defines for inflate input/output */
41990Sstevel@tonic-gate /*   update pointers and return */
42000Sstevel@tonic-gate #define	UPDBITS {s->bitb = b; s->bitk = k; }
42010Sstevel@tonic-gate #define	UPDIN {z->avail_in = n; z->total_in += p-z->next_in; z->next_in = p; }
42020Sstevel@tonic-gate #define	UPDOUT {s->write = q; }
42030Sstevel@tonic-gate #define	UPDATE {UPDBITS UPDIN UPDOUT}
42040Sstevel@tonic-gate #define	LEAVE {UPDATE return (inflate_flush(s, z, r)); }
42050Sstevel@tonic-gate /*   get bytes and bits */
42060Sstevel@tonic-gate #define	LOADIN {p = z->next_in; n = z->avail_in; b = s->bitb; k = s->bitk; }
42070Sstevel@tonic-gate #define	NEEDBYTE { if (n) r = Z_OK; else LEAVE }
42080Sstevel@tonic-gate #define	NEXTBYTE (n--, *p++)
42090Sstevel@tonic-gate #define	NEEDBITS(j) { while (k < (j)) { NEEDBYTE; b |= ((uLong)NEXTBYTE)<<k; \
42100Sstevel@tonic-gate 	k += 8; }}
42110Sstevel@tonic-gate #define	DUMPBITS(j) {b >>= (j); k -= (j); }
42120Sstevel@tonic-gate /*   output bytes */
42130Sstevel@tonic-gate #define	WAVAIL (uInt)(q < s->read ? s->read-q-1 : s->end-q)
42140Sstevel@tonic-gate #define	LOADOUT {q = s->write; m = (uInt)WAVAIL; }
42150Sstevel@tonic-gate #define	WWRAP {if (q == s->end && s->read != s->window) {q = s->window; \
42160Sstevel@tonic-gate 	m = (uInt)WAVAIL; }}
42170Sstevel@tonic-gate #define	FLUSH {UPDOUT r = inflate_flush(s, z, r); LOADOUT}
42180Sstevel@tonic-gate #define	NEEDOUT {if (m == 0) {WWRAP if (m == 0) { FLUSH WWRAP \
42190Sstevel@tonic-gate 	if (m == 0) LEAVE }} r = Z_OK; }
42200Sstevel@tonic-gate #define	OUTBYTE(a) {*q++ = (Byte)(a); m--; }
42210Sstevel@tonic-gate /*   load local pointers */
42220Sstevel@tonic-gate #define	LOAD {LOADIN LOADOUT}
42230Sstevel@tonic-gate 
42240Sstevel@tonic-gate /* masks for lower bits (size given to avoid silly warnings with Visual C++) */
42250Sstevel@tonic-gate extern uInt inflate_mask[17];
42260Sstevel@tonic-gate 
42270Sstevel@tonic-gate /* copy as much as possible from the sliding window to the output area */
42280Sstevel@tonic-gate extern int inflate_flush OF((
42290Sstevel@tonic-gate     inflate_blocks_statef *,
42300Sstevel@tonic-gate     z_streamp,
42310Sstevel@tonic-gate     int));
42320Sstevel@tonic-gate 
42330Sstevel@tonic-gate #ifndef NO_DUMMY_DECL
42340Sstevel@tonic-gate struct internal_state {int dummy; };	/* for buggy compilers */
42350Sstevel@tonic-gate #endif
42360Sstevel@tonic-gate 
42370Sstevel@tonic-gate #endif
42380Sstevel@tonic-gate /* --- infutil.h */
42390Sstevel@tonic-gate 
42400Sstevel@tonic-gate #ifndef NO_DUMMY_DECL
42410Sstevel@tonic-gate struct inflate_codes_state {int dummy; };	/* for buggy compilers */
42420Sstevel@tonic-gate #endif
42430Sstevel@tonic-gate 
42440Sstevel@tonic-gate /* Table for deflate from PKZIP's appnote.txt. */
42450Sstevel@tonic-gate local const uInt border[] = { /* Order of the bit length code lengths */
42460Sstevel@tonic-gate 	16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
42470Sstevel@tonic-gate 
42480Sstevel@tonic-gate /*
42490Sstevel@tonic-gate  * Notes beyond the 1.93a appnote.txt:
42500Sstevel@tonic-gate  *
42510Sstevel@tonic-gate  *   1. Distance pointers never point before the beginning of the output
42520Sstevel@tonic-gate  *      stream.
42530Sstevel@tonic-gate  *   2. Distance pointers can point back across blocks, up to 32k away.
42540Sstevel@tonic-gate  *   3. There is an implied maximum of 7 bits for the bit length table and
42550Sstevel@tonic-gate  *      15 bits for the actual data.
42560Sstevel@tonic-gate  *   4. If only one code exists, then it is encoded using one bit.  (Zero
42570Sstevel@tonic-gate  *      would be more efficient, but perhaps a little confusing.)  If two
42580Sstevel@tonic-gate  *      codes exist, they are coded using one bit each (0 and 1).
42590Sstevel@tonic-gate  *   5. There is no way of sending zero distance codes--a dummy must be
42600Sstevel@tonic-gate  *      sent if there are none.  (History: a pre 2.0 version of PKZIP would
42610Sstevel@tonic-gate  *      store blocks with no distance codes, but this was discovered to be
42620Sstevel@tonic-gate  *      too harsh a criterion.)  Valid only for 1.93a.  2.04c does allow
42630Sstevel@tonic-gate  *      zero distance codes, which is sent as one code of zero bits in
42640Sstevel@tonic-gate  *      length.
42650Sstevel@tonic-gate  *   6. There are up to 286 literal/length codes.  Code 256 represents the
42660Sstevel@tonic-gate  *      end-of-block.  Note however that the static length tree defines
42670Sstevel@tonic-gate  *      288 codes just to fill out the Huffman codes.  Codes 286 and 287
42680Sstevel@tonic-gate  *      cannot be used though, since there is no length base or extra bits
42690Sstevel@tonic-gate  *      defined for them.  Similarily, there are up to 30 distance codes.
42700Sstevel@tonic-gate  *      However, static trees define 32 codes (all 5 bits) to fill out the
42710Sstevel@tonic-gate  *      Huffman codes, but the last two had better not show up in the data.
42720Sstevel@tonic-gate  *   7. Unzip can check dynamic Huffman blocks for complete code sets.
42730Sstevel@tonic-gate  *      The exception is that a single code would not be complete (see #4).
42740Sstevel@tonic-gate  *   8. The five bits following the block type is really the number of
42750Sstevel@tonic-gate  *      literal codes sent minus 257.
42760Sstevel@tonic-gate  *   9. Length codes 8,16,16 are interpreted as 13 length codes of 8 bits
42770Sstevel@tonic-gate  *      (1+6+6).  Therefore, to output three times the length, you output
42780Sstevel@tonic-gate  *      three codes (1+1+1), whereas to output four times the same length,
42790Sstevel@tonic-gate  *      you only need two codes (1+3).  Hmm.
42800Sstevel@tonic-gate  *  10. In the tree reconstruction algorithm, Code = Code + Increment
42810Sstevel@tonic-gate  *      only if BitLength(i) is not zero.  (Pretty obvious.)
42820Sstevel@tonic-gate  *  11. Correction: 4 Bits: #of Bit Length codes - 4     (4 - 19)
42830Sstevel@tonic-gate  *  12. Note: length code 284 can represent 227-258, but length code 285
42840Sstevel@tonic-gate  *      really is 258.  The last length deserves its own, short code
42850Sstevel@tonic-gate  *      since it gets used a lot in very redundant files.  The length
42860Sstevel@tonic-gate  *      258 is special since 258 - 3 (the min match length) is 255.
42870Sstevel@tonic-gate  *  13. The literal/length and distance code bit lengths are read as a
42880Sstevel@tonic-gate  *      single stream of lengths.  It is possible (and advantageous) for
42890Sstevel@tonic-gate  *      a repeat code (16, 17, or 18) to go across the boundary between
42900Sstevel@tonic-gate  *      the two sets of lengths.
42910Sstevel@tonic-gate  */
42920Sstevel@tonic-gate 
42930Sstevel@tonic-gate 
42940Sstevel@tonic-gate void
inflate_blocks_reset(s,z,c)42950Sstevel@tonic-gate inflate_blocks_reset(s, z, c)
42960Sstevel@tonic-gate inflate_blocks_statef *s;
42970Sstevel@tonic-gate z_streamp z;
42980Sstevel@tonic-gate uLongf *c;
42990Sstevel@tonic-gate {
43000Sstevel@tonic-gate 	if (c != Z_NULL)
43010Sstevel@tonic-gate 		*c = s->check;
43020Sstevel@tonic-gate 	if ((s->mode == BTREE || s->mode == DTREE) &&
43030Sstevel@tonic-gate 	    s->sub.trees.blens != Z_NULL) {
43040Sstevel@tonic-gate 		ZFREE(z, s->sub.trees.blens);
43050Sstevel@tonic-gate 		s->sub.trees.blens = Z_NULL;
43060Sstevel@tonic-gate 	}
43070Sstevel@tonic-gate 	if (s->mode == CODES && s->sub.decode.codes != Z_NULL) {
43080Sstevel@tonic-gate 		(void) inflate_codes_free(s->sub.decode.codes, z);
43090Sstevel@tonic-gate 		s->sub.decode.codes = Z_NULL;
43100Sstevel@tonic-gate 	}
43110Sstevel@tonic-gate 	s->mode = TYPE;
43120Sstevel@tonic-gate 	s->bitk = 0;
43130Sstevel@tonic-gate 	s->bitb = 0;
43140Sstevel@tonic-gate 	s->read = s->write = s->window;
43150Sstevel@tonic-gate 	if (s->checkfn != Z_NULL)
43160Sstevel@tonic-gate 		z->adler = s->check = (*s->checkfn)(0L, Z_NULL, 0);
43170Sstevel@tonic-gate 	Trace((stderr, "inflate:   blocks reset\n"));
43180Sstevel@tonic-gate }
43190Sstevel@tonic-gate 
43200Sstevel@tonic-gate inflate_blocks_statef *
inflate_blocks_new(z,c,w)43210Sstevel@tonic-gate inflate_blocks_new(z, c, w)
43220Sstevel@tonic-gate z_streamp z;
43230Sstevel@tonic-gate check_func c;
43240Sstevel@tonic-gate uInt w;
43250Sstevel@tonic-gate {
43260Sstevel@tonic-gate 	inflate_blocks_statef *s;
43270Sstevel@tonic-gate 
43280Sstevel@tonic-gate 	if ((s = (inflate_blocks_statef *)ZALLOC
43290Sstevel@tonic-gate 	    (z, 1, sizeof (struct inflate_blocks_state))) == Z_NULL)
43300Sstevel@tonic-gate 		return (s);
43310Sstevel@tonic-gate 	s->hufts = (inflate_huft *)ZALLOC(z, MANY, sizeof (inflate_huft));
43320Sstevel@tonic-gate 	if (s->hufts == Z_NULL) {
43330Sstevel@tonic-gate 		ZFREE(z, s);
43340Sstevel@tonic-gate 		return (Z_NULL);
43350Sstevel@tonic-gate 	}
43360Sstevel@tonic-gate 	if ((s->window = (Bytef *)ZALLOC(z, 1, w)) == Z_NULL)
43370Sstevel@tonic-gate 	{
43380Sstevel@tonic-gate 		ZFREE(z, s->hufts);
43390Sstevel@tonic-gate 		ZFREE(z, s);
43400Sstevel@tonic-gate 		return (Z_NULL);
43410Sstevel@tonic-gate 	}
43420Sstevel@tonic-gate 	s->end = s->window + w;
43430Sstevel@tonic-gate 	s->checkfn = c;
43440Sstevel@tonic-gate 	s->mode = TYPE;
43450Sstevel@tonic-gate 	Trace((stderr, "inflate:   blocks allocated\n"));
43460Sstevel@tonic-gate 	inflate_blocks_reset(s, z, Z_NULL);
43470Sstevel@tonic-gate 	return (s);
43480Sstevel@tonic-gate }
43490Sstevel@tonic-gate 
43500Sstevel@tonic-gate 
43510Sstevel@tonic-gate int
inflate_blocks(s,z,r)43520Sstevel@tonic-gate inflate_blocks(s, z, r)
43530Sstevel@tonic-gate inflate_blocks_statef *s;
43540Sstevel@tonic-gate z_streamp z;
43550Sstevel@tonic-gate int r;
43560Sstevel@tonic-gate {
43570Sstevel@tonic-gate 	uInt t;	/* temporary storage */
43580Sstevel@tonic-gate 	uLong b;	/* bit buffer */
43590Sstevel@tonic-gate 	uInt k;	/* bits in bit buffer */
43600Sstevel@tonic-gate 	Bytef *p;	/* input data pointer */
43610Sstevel@tonic-gate 	uInt n;	/* bytes available there */
43620Sstevel@tonic-gate 	Bytef *q;	/* output window write pointer */
43630Sstevel@tonic-gate 	uInt m;	/* bytes to end of window or read pointer */
43640Sstevel@tonic-gate 
43650Sstevel@tonic-gate 	/* copy input/output information to locals (UPDATE macro restores) */
43660Sstevel@tonic-gate 	LOAD;
43670Sstevel@tonic-gate 
43680Sstevel@tonic-gate 	/* process input based on current state */
43690Sstevel@tonic-gate 	/* CONSTCOND */
43700Sstevel@tonic-gate 	while (1)
43710Sstevel@tonic-gate 		switch (s->mode)
43720Sstevel@tonic-gate 	{
43730Sstevel@tonic-gate 	case TYPE:
43740Sstevel@tonic-gate 		NEEDBITS(3);
43750Sstevel@tonic-gate 		t = (uInt)b & 7;
43760Sstevel@tonic-gate 		s->last = t & 1;
43770Sstevel@tonic-gate 		switch (t >> 1)
43780Sstevel@tonic-gate 		{
43790Sstevel@tonic-gate 		case 0:			/* stored */
43800Sstevel@tonic-gate 			Trace((stderr, "inflate:     stored block%s\n",
43810Sstevel@tonic-gate 			    s->last ? " (last)" : ""));
43820Sstevel@tonic-gate 			DUMPBITS(3);
43830Sstevel@tonic-gate 			t = k & 7;	/* go to byte boundary */
43840Sstevel@tonic-gate 			DUMPBITS(t);
43850Sstevel@tonic-gate 			s->mode = LENS;	/* get length of stored block */
43860Sstevel@tonic-gate 			break;
43870Sstevel@tonic-gate 		case 1:			/* fixed */
43880Sstevel@tonic-gate 			Trace((stderr, "inflate:     fixed codes block%s\n",
43890Sstevel@tonic-gate 			    s->last ? " (last)" : ""));
43900Sstevel@tonic-gate 			{
43910Sstevel@tonic-gate 				uInt bl, bd;
43920Sstevel@tonic-gate 				const inflate_huft *tl, *td;
43930Sstevel@tonic-gate 
43940Sstevel@tonic-gate 				(void) inflate_trees_fixed(&bl, &bd, &tl, &td,
43950Sstevel@tonic-gate 				    z);
43960Sstevel@tonic-gate 				s->sub.decode.codes = inflate_codes_new(bl,
43970Sstevel@tonic-gate 				    bd, tl, td, z);
43980Sstevel@tonic-gate 				if (s->sub.decode.codes == Z_NULL)
43990Sstevel@tonic-gate 				{
44000Sstevel@tonic-gate 					r = Z_MEM_ERROR;
44010Sstevel@tonic-gate 					LEAVE
44020Sstevel@tonic-gate 				}
44030Sstevel@tonic-gate 			}
44040Sstevel@tonic-gate 			DUMPBITS(3);
44050Sstevel@tonic-gate 			s->mode = CODES;
44060Sstevel@tonic-gate 			break;
44070Sstevel@tonic-gate 		case 2:			/* dynamic */
44080Sstevel@tonic-gate 			Trace((stderr, "inflate:     dynamic codes block%s\n",
44090Sstevel@tonic-gate 			    s->last ? " (last)" : ""));
44100Sstevel@tonic-gate 			DUMPBITS(3);
44110Sstevel@tonic-gate 			s->mode = TABLE;
44120Sstevel@tonic-gate 			break;
44130Sstevel@tonic-gate 		case 3:			/* illegal */
44140Sstevel@tonic-gate 			DUMPBITS(3);
44150Sstevel@tonic-gate 			s->mode = BADB;
44160Sstevel@tonic-gate 			z->msg = "invalid block type";
44170Sstevel@tonic-gate 			r = Z_DATA_ERROR;
44180Sstevel@tonic-gate 			LEAVE
44190Sstevel@tonic-gate 		}
44200Sstevel@tonic-gate 		break;
44210Sstevel@tonic-gate 	case LENS:
44220Sstevel@tonic-gate 		NEEDBITS(32);
44230Sstevel@tonic-gate 		if ((((~b) >> 16) & 0xffff) != (b & 0xffff))
44240Sstevel@tonic-gate 		{
44250Sstevel@tonic-gate 			s->mode = BADB;
44260Sstevel@tonic-gate 			z->msg = "invalid stored block lengths";
44270Sstevel@tonic-gate 			r = Z_DATA_ERROR;
44280Sstevel@tonic-gate 			LEAVE
44290Sstevel@tonic-gate 		}
44300Sstevel@tonic-gate 		s->sub.left = (uInt)b & 0xffff;
44310Sstevel@tonic-gate 		b = k = 0;	/* dump bits */
44320Sstevel@tonic-gate 		Tracev((stderr, "inflate:       stored length %u\n",
44330Sstevel@tonic-gate 		    s->sub.left));
44340Sstevel@tonic-gate 		s->mode = s->sub.left ? STORED : (s->last ? DRY : TYPE);
44350Sstevel@tonic-gate 		break;
44360Sstevel@tonic-gate 	case STORED:
44370Sstevel@tonic-gate 		if (n == 0)
44380Sstevel@tonic-gate 			LEAVE
44390Sstevel@tonic-gate 		NEEDOUT;
44400Sstevel@tonic-gate 		t = s->sub.left;
44410Sstevel@tonic-gate 		if (t > n) t = n;
44420Sstevel@tonic-gate 		if (t > m) t = m;
44430Sstevel@tonic-gate 		zmemcpy(q, p, t);
44440Sstevel@tonic-gate 		p += t;  n -= t;
44450Sstevel@tonic-gate 		q += t;  m -= t;
44460Sstevel@tonic-gate 		if ((s->sub.left -= t) != 0)
44470Sstevel@tonic-gate 			break;
44480Sstevel@tonic-gate 		Tracev((stderr,
44490Sstevel@tonic-gate 		    "inflate:       stored end, %lu total out\n",
44500Sstevel@tonic-gate 		    z->total_out + (q >= s->read ? q - s->read :
44510Sstevel@tonic-gate 			(s->end - s->read) + (q - s->window))));
44520Sstevel@tonic-gate 		s->mode = s->last ? DRY : TYPE;
44530Sstevel@tonic-gate 		break;
44540Sstevel@tonic-gate 	case TABLE:
44550Sstevel@tonic-gate 		NEEDBITS(14);
44560Sstevel@tonic-gate 		s->sub.trees.table = t = (uInt)b & 0x3fff;
44570Sstevel@tonic-gate #ifndef PKZIP_BUG_WORKAROUND
44580Sstevel@tonic-gate 		if ((t & 0x1f) > 29 || ((t >> 5) & 0x1f) > 29)
44590Sstevel@tonic-gate 		{
44600Sstevel@tonic-gate 			s->mode = BADB;
44610Sstevel@tonic-gate 			z->msg =
44620Sstevel@tonic-gate 			    (char *)"too many length or distance symbols";
44630Sstevel@tonic-gate 			r = Z_DATA_ERROR;
44640Sstevel@tonic-gate 			LEAVE
44650Sstevel@tonic-gate 		}
44660Sstevel@tonic-gate #endif
44670Sstevel@tonic-gate 		t = 258 + (t & 0x1f) + ((t >> 5) & 0x1f);
44680Sstevel@tonic-gate 		/* if (t < 19) t = 19; */
44690Sstevel@tonic-gate 		if ((s->sub.trees.blens = (uIntf*)ZALLOC(z, t,
44700Sstevel@tonic-gate 		    sizeof (uInt))) == Z_NULL)
44710Sstevel@tonic-gate 		{
44720Sstevel@tonic-gate 			r = Z_MEM_ERROR;
44730Sstevel@tonic-gate 			LEAVE
44740Sstevel@tonic-gate 		}
44750Sstevel@tonic-gate 		DUMPBITS(14);
44760Sstevel@tonic-gate 		s->sub.trees.index = 0;
44770Sstevel@tonic-gate 		Tracev((stderr, "inflate:       table sizes ok\n"));
44780Sstevel@tonic-gate 		s->mode = BTREE;
44790Sstevel@tonic-gate 		/* FALLTHRU */
44800Sstevel@tonic-gate 	case BTREE:
44810Sstevel@tonic-gate 		while (s->sub.trees.index < 4 + (s->sub.trees.table >> 10))
44820Sstevel@tonic-gate 		{
44830Sstevel@tonic-gate 			NEEDBITS(3);
44840Sstevel@tonic-gate 			s->sub.trees.blens[border[s->sub.trees.index++]] =
44850Sstevel@tonic-gate 			    (uInt)b & 7;
44860Sstevel@tonic-gate 			DUMPBITS(3);
44870Sstevel@tonic-gate 		}
44880Sstevel@tonic-gate 		while (s->sub.trees.index < 19)
44890Sstevel@tonic-gate 			s->sub.trees.blens[border[s->sub.trees.index++]] =
44900Sstevel@tonic-gate 			    0;
44910Sstevel@tonic-gate 		s->sub.trees.bb = 7;
44920Sstevel@tonic-gate 		t = inflate_trees_bits(s->sub.trees.blens, &s->sub.trees.bb,
44930Sstevel@tonic-gate 		    &s->sub.trees.tb, s->hufts, z);
44940Sstevel@tonic-gate 		if (t != Z_OK)
44950Sstevel@tonic-gate 		{
44960Sstevel@tonic-gate 			ZFREE(z, s->sub.trees.blens);
44970Sstevel@tonic-gate 			s->sub.trees.blens = Z_NULL;
44980Sstevel@tonic-gate 			r = t;
44990Sstevel@tonic-gate 			if (r == Z_DATA_ERROR)
45000Sstevel@tonic-gate 				s->mode = BADB;
45010Sstevel@tonic-gate 			LEAVE
45020Sstevel@tonic-gate 		}
45030Sstevel@tonic-gate 		s->sub.trees.index = 0;
45040Sstevel@tonic-gate 		Tracev((stderr, "inflate:       bits tree ok\n"));
45050Sstevel@tonic-gate 		s->mode = DTREE;
45060Sstevel@tonic-gate 		/* FALLTHRU */
45070Sstevel@tonic-gate 	case DTREE:
45080Sstevel@tonic-gate 		while (t = s->sub.trees.table,
45090Sstevel@tonic-gate 		    s->sub.trees.index < 258 + (t & 0x1f) +
45100Sstevel@tonic-gate 		    ((t >> 5) & 0x1f))
45110Sstevel@tonic-gate 		{
45120Sstevel@tonic-gate 			inflate_huft *h;
45130Sstevel@tonic-gate 			uInt i, j, c;
45140Sstevel@tonic-gate 
45150Sstevel@tonic-gate 			t = s->sub.trees.bb;
45160Sstevel@tonic-gate 			NEEDBITS(t);
45170Sstevel@tonic-gate 			h = s->sub.trees.tb + ((uInt)b & inflate_mask[t]);
45180Sstevel@tonic-gate 			t = h->word.what.Bits;
45190Sstevel@tonic-gate 			c = h->base;
45200Sstevel@tonic-gate 			if (c < 16)
45210Sstevel@tonic-gate 			{
45220Sstevel@tonic-gate 				DUMPBITS(t);
45230Sstevel@tonic-gate 				s->sub.trees.blens[s->sub.trees.index++] =
45240Sstevel@tonic-gate 				    c;
45250Sstevel@tonic-gate 			} else { /* c == 16..18 */
45260Sstevel@tonic-gate 				i = c == 18 ? 7 : c - 14;
45270Sstevel@tonic-gate 				j = c == 18 ? 11 : 3;
45280Sstevel@tonic-gate 				NEEDBITS(t + i);
45290Sstevel@tonic-gate 				DUMPBITS(t);
45300Sstevel@tonic-gate 				j += (uInt)b & inflate_mask[i];
45310Sstevel@tonic-gate 				DUMPBITS(i);
45320Sstevel@tonic-gate 				i = s->sub.trees.index;
45330Sstevel@tonic-gate 				t = s->sub.trees.table;
45340Sstevel@tonic-gate 				if (i + j > 258 + (t & 0x1f) +
45350Sstevel@tonic-gate 				    ((t >> 5) & 0x1f) ||
45360Sstevel@tonic-gate 				    (c == 16 && i < 1))
45370Sstevel@tonic-gate 				{
45380Sstevel@tonic-gate 					ZFREE(z, s->sub.trees.blens);
45390Sstevel@tonic-gate 					s->sub.trees.blens = Z_NULL;
45400Sstevel@tonic-gate 					s->mode = BADB;
45410Sstevel@tonic-gate 					z->msg = "invalid bit length repeat";
45420Sstevel@tonic-gate 					r = Z_DATA_ERROR;
45430Sstevel@tonic-gate 					LEAVE
45440Sstevel@tonic-gate 				}
45450Sstevel@tonic-gate 				c = c == 16 ? s->sub.trees.blens[i - 1] : 0;
45460Sstevel@tonic-gate 				do {
45470Sstevel@tonic-gate 					s->sub.trees.blens[i++] = c;
45480Sstevel@tonic-gate 				} while (--j);
45490Sstevel@tonic-gate 				s->sub.trees.index = i;
45500Sstevel@tonic-gate 			}
45510Sstevel@tonic-gate 		}
45520Sstevel@tonic-gate 		s->sub.trees.tb = Z_NULL;
45530Sstevel@tonic-gate 		{
45540Sstevel@tonic-gate 			uInt bl, bd;
45550Sstevel@tonic-gate 			inflate_huft *tl, *td;
45560Sstevel@tonic-gate 			inflate_codes_statef *c;
45570Sstevel@tonic-gate 
45580Sstevel@tonic-gate 				/* must be <= 9 for lookahead assumptions */
45590Sstevel@tonic-gate 			bl = 9;
45600Sstevel@tonic-gate 				/* must be <= 9 for lookahead assumptions */
45610Sstevel@tonic-gate 			bd = 6;
45620Sstevel@tonic-gate 			t = s->sub.trees.table;
45630Sstevel@tonic-gate 			t = inflate_trees_dynamic(257 + (t & 0x1f),
45640Sstevel@tonic-gate 			    1 + ((t >> 5) & 0x1f),
45650Sstevel@tonic-gate 			    s->sub.trees.blens, &bl, &bd, &tl, &td,
45660Sstevel@tonic-gate 			    s->hufts, z);
45670Sstevel@tonic-gate 			ZFREE(z, s->sub.trees.blens);
45680Sstevel@tonic-gate 			s->sub.trees.blens = Z_NULL;
45690Sstevel@tonic-gate 			if (t != Z_OK)
45700Sstevel@tonic-gate 			{
45710Sstevel@tonic-gate 				if (t == (uInt)Z_DATA_ERROR)
45720Sstevel@tonic-gate 					s->mode = BADB;
45730Sstevel@tonic-gate 				r = t;
45740Sstevel@tonic-gate 				LEAVE
45750Sstevel@tonic-gate 			}
45760Sstevel@tonic-gate 			Tracev((stderr, "inflate:       trees ok\n"));
45770Sstevel@tonic-gate 			if ((c = inflate_codes_new(bl, bd, tl, td, z)) ==
45780Sstevel@tonic-gate 			    Z_NULL)
45790Sstevel@tonic-gate 			{
45800Sstevel@tonic-gate 				r = Z_MEM_ERROR;
45810Sstevel@tonic-gate 				LEAVE
45820Sstevel@tonic-gate 			}
45830Sstevel@tonic-gate 			s->sub.decode.codes = c;
45840Sstevel@tonic-gate 		}
45850Sstevel@tonic-gate 		s->mode = CODES;
45860Sstevel@tonic-gate 		/* FALLTHRU */
45870Sstevel@tonic-gate 	case CODES:
45880Sstevel@tonic-gate 		UPDATE;
45890Sstevel@tonic-gate 		if ((r = inflate_codes(s, z, r)) != Z_STREAM_END)
45900Sstevel@tonic-gate 			return (inflate_flush(s, z, r));
45910Sstevel@tonic-gate 		r = Z_OK;
45920Sstevel@tonic-gate 		(void) inflate_codes_free(s->sub.decode.codes, z);
45930Sstevel@tonic-gate 		LOAD;
45940Sstevel@tonic-gate 		Tracev((stderr, "inflate:       codes end, %lu total out\n",
45950Sstevel@tonic-gate 		    z->total_out + (q >= s->read ? q - s->read :
45960Sstevel@tonic-gate 			(s->end - s->read) + (q - s->window))));
45970Sstevel@tonic-gate 		if (!s->last)
45980Sstevel@tonic-gate 		{
45990Sstevel@tonic-gate 			s->mode = TYPE;
46000Sstevel@tonic-gate 			break;
46010Sstevel@tonic-gate 		}
46020Sstevel@tonic-gate 		s->mode = DRY;
46030Sstevel@tonic-gate 		/* FALLTHRU */
46040Sstevel@tonic-gate 	case DRY:
46050Sstevel@tonic-gate 		FLUSH;
46060Sstevel@tonic-gate 		if (s->read != s->write)
46070Sstevel@tonic-gate 			LEAVE
46080Sstevel@tonic-gate 		s->mode = DONEB;
46090Sstevel@tonic-gate 		/* FALLTHRU */
46100Sstevel@tonic-gate 	case DONEB:
46110Sstevel@tonic-gate 		r = Z_STREAM_END;
46120Sstevel@tonic-gate 		LEAVE
46130Sstevel@tonic-gate 	case BADB:
46140Sstevel@tonic-gate 		r = Z_DATA_ERROR;
46150Sstevel@tonic-gate 		LEAVE
46160Sstevel@tonic-gate 	default:
46170Sstevel@tonic-gate 		r = Z_STREAM_ERROR;
46180Sstevel@tonic-gate 		LEAVE
46190Sstevel@tonic-gate 	}
46200Sstevel@tonic-gate 	/* NOTREACHED */
46210Sstevel@tonic-gate 	/* otherwise lint complains */
46220Sstevel@tonic-gate }
46230Sstevel@tonic-gate 
46240Sstevel@tonic-gate 
46250Sstevel@tonic-gate int
inflate_blocks_free(s,z)46260Sstevel@tonic-gate inflate_blocks_free(s, z)
46270Sstevel@tonic-gate inflate_blocks_statef *s;
46280Sstevel@tonic-gate z_streamp z;
46290Sstevel@tonic-gate {
46300Sstevel@tonic-gate 	inflate_blocks_reset(s, z, Z_NULL);
46310Sstevel@tonic-gate 	ZFREE(z, s->window);
46320Sstevel@tonic-gate 	s->window = Z_NULL;
46330Sstevel@tonic-gate 	ZFREE(z, s->hufts);
46340Sstevel@tonic-gate 	s->hufts = Z_NULL;
46350Sstevel@tonic-gate 	ZFREE(z, s);
46360Sstevel@tonic-gate 	Trace((stderr, "inflate:   blocks freed\n"));
46370Sstevel@tonic-gate 	return (Z_OK);
46380Sstevel@tonic-gate }
46390Sstevel@tonic-gate 
46400Sstevel@tonic-gate 
46410Sstevel@tonic-gate void
inflate_set_dictionary(s,d,n)46420Sstevel@tonic-gate inflate_set_dictionary(s, d, n)
46430Sstevel@tonic-gate inflate_blocks_statef *s;
46440Sstevel@tonic-gate const Bytef *d;
46450Sstevel@tonic-gate uInt  n;
46460Sstevel@tonic-gate {
46470Sstevel@tonic-gate 	Assert(s->window + n <= s->end, "set dict");
46480Sstevel@tonic-gate 	zmemcpy((charf *)s->window, d, n);
46490Sstevel@tonic-gate 	s->read = s->write = s->window + n;
46500Sstevel@tonic-gate }
46510Sstevel@tonic-gate 
46520Sstevel@tonic-gate /*
46530Sstevel@tonic-gate  * Returns true if inflate is currently at the end of a block
46540Sstevel@tonic-gate  * generated by Z_SYNC_FLUSH or Z_FULL_FLUSH.
46550Sstevel@tonic-gate  * IN assertion: s != Z_NULL
46560Sstevel@tonic-gate  */
46570Sstevel@tonic-gate int
inflate_blocks_sync_point(s)46580Sstevel@tonic-gate inflate_blocks_sync_point(s)
46590Sstevel@tonic-gate inflate_blocks_statef *s;
46600Sstevel@tonic-gate {
46610Sstevel@tonic-gate 	return (s->mode == LENS);
46620Sstevel@tonic-gate }
46630Sstevel@tonic-gate 
46640Sstevel@tonic-gate /*
46650Sstevel@tonic-gate  * This subroutine adds the data at next_in/avail_in to the output history
46660Sstevel@tonic-gate  * without performing any output.  The output buffer must be "caught up";
46670Sstevel@tonic-gate  * i.e. no pending output (hence s->read equals s->write), and the state must
46680Sstevel@tonic-gate  * be BLOCKS (i.e. we should be willing to see the start of a series of
46690Sstevel@tonic-gate  * BLOCKS).  On exit, the output will also be caught up, and the checksum
46700Sstevel@tonic-gate  * will have been updated if need be.
46710Sstevel@tonic-gate  */
46720Sstevel@tonic-gate int
inflate_addhistory(s,z)46730Sstevel@tonic-gate inflate_addhistory(s, z)
46740Sstevel@tonic-gate inflate_blocks_statef *s;
46750Sstevel@tonic-gate z_stream *z;
46760Sstevel@tonic-gate {
46770Sstevel@tonic-gate 	uLong b;	/* bit buffer */  /* NOT USED HERE */
46780Sstevel@tonic-gate 	uInt k;	/* bits in bit buffer */ /* NOT USED HERE */
46790Sstevel@tonic-gate 	uInt t;	/* temporary storage */
46800Sstevel@tonic-gate 	Bytef *p;	/* input data pointer */
46810Sstevel@tonic-gate 	uInt n;	/* bytes available there */
46820Sstevel@tonic-gate 	Bytef *q;	/* output window write pointer */
46830Sstevel@tonic-gate 	uInt m;	/* bytes to end of window or read pointer */
46840Sstevel@tonic-gate 
46850Sstevel@tonic-gate 	if (s->read != s->write)
46860Sstevel@tonic-gate 		return (Z_STREAM_ERROR);
46870Sstevel@tonic-gate 	if (s->mode != TYPE)
46880Sstevel@tonic-gate 		return (Z_DATA_ERROR);
46890Sstevel@tonic-gate 
46900Sstevel@tonic-gate 	/* we're ready to rock */
46910Sstevel@tonic-gate 	LOAD;
46920Sstevel@tonic-gate 	/*
46930Sstevel@tonic-gate 	 * while there is input ready, copy to output buffer, moving
46940Sstevel@tonic-gate 	 * pointers as needed.
46950Sstevel@tonic-gate 	 */
46960Sstevel@tonic-gate 	while (n) {
46970Sstevel@tonic-gate 		t = n;	/* how many to do */
46980Sstevel@tonic-gate 		/* is there room until end of buffer? */
46990Sstevel@tonic-gate 		if (t > m) t = m;
47000Sstevel@tonic-gate 		/* update check information */
47010Sstevel@tonic-gate 		if (s->checkfn != Z_NULL)
47020Sstevel@tonic-gate 			s->check = (*s->checkfn)(s->check, q, t);
47030Sstevel@tonic-gate 		zmemcpy(q, p, t);
47040Sstevel@tonic-gate 		q += t;
47050Sstevel@tonic-gate 		p += t;
47060Sstevel@tonic-gate 		n -= t;
47070Sstevel@tonic-gate 		z->total_out += t;
47080Sstevel@tonic-gate 		s->read = q;	/* drag read pointer forward */
47090Sstevel@tonic-gate /* WWRAP */	/* expand WWRAP macro by hand to handle s->read */
47100Sstevel@tonic-gate 		if (q == s->end) {
47110Sstevel@tonic-gate 			s->read = q = s->window;
47120Sstevel@tonic-gate 			m = WAVAIL;
47130Sstevel@tonic-gate 		}
47140Sstevel@tonic-gate 	}
47150Sstevel@tonic-gate 	UPDATE;
47160Sstevel@tonic-gate 	return (Z_OK);
47170Sstevel@tonic-gate }
47180Sstevel@tonic-gate 
47190Sstevel@tonic-gate 
47200Sstevel@tonic-gate /*
47210Sstevel@tonic-gate  * At the end of a Deflate-compressed PPP packet, we expect to have seen
47220Sstevel@tonic-gate  * a `stored' block type value but not the (zero) length bytes.
47230Sstevel@tonic-gate  */
47240Sstevel@tonic-gate int
inflate_packet_flush(s)47250Sstevel@tonic-gate inflate_packet_flush(s)
47260Sstevel@tonic-gate     inflate_blocks_statef *s;
47270Sstevel@tonic-gate {
47280Sstevel@tonic-gate 	if (s->mode != LENS)
47290Sstevel@tonic-gate 		return (Z_DATA_ERROR);
47300Sstevel@tonic-gate 	s->mode = TYPE;
47310Sstevel@tonic-gate 	return (Z_OK);
47320Sstevel@tonic-gate }
47330Sstevel@tonic-gate /* --- infblock.c */
47340Sstevel@tonic-gate 
47350Sstevel@tonic-gate /* +++ inftrees.c */
47360Sstevel@tonic-gate /*
47370Sstevel@tonic-gate  * inftrees.c -- generate Huffman trees for efficient decoding
47380Sstevel@tonic-gate  * Copyright (C) 1995-1998 Mark Adler
47390Sstevel@tonic-gate  * For conditions of distribution and use, see copyright notice in zlib.h
47400Sstevel@tonic-gate  */
47410Sstevel@tonic-gate 
47420Sstevel@tonic-gate /* #include "zutil.h" */
47430Sstevel@tonic-gate /* #include "inftrees.h" */
47440Sstevel@tonic-gate 
47450Sstevel@tonic-gate const char inflate_copyright[] =
47460Sstevel@tonic-gate " inflate 1.1.3 Copyright 1995-1998 Mark Adler ";
47470Sstevel@tonic-gate /*
47480Sstevel@tonic-gate  * If you use the zlib library in a product, an acknowledgment is
47490Sstevel@tonic-gate  * welcome in the documentation of your product. If for some reason
47500Sstevel@tonic-gate  * you cannot include such an acknowledgment, I would appreciate that
47510Sstevel@tonic-gate  * you keep this copyright string in the executable of your product.
47520Sstevel@tonic-gate  */
47530Sstevel@tonic-gate 
47540Sstevel@tonic-gate #ifndef NO_DUMMY_DECL
47550Sstevel@tonic-gate struct internal_state  {int dummy; };	/* for buggy compilers */
47560Sstevel@tonic-gate #endif
47570Sstevel@tonic-gate 
47580Sstevel@tonic-gate /* simplify the use of the inflate_huft type with some defines */
47590Sstevel@tonic-gate #define	exop word.what.Exop
47600Sstevel@tonic-gate #define	bits word.what.Bits
47610Sstevel@tonic-gate 
47620Sstevel@tonic-gate 
47630Sstevel@tonic-gate local int huft_build OF((
47640Sstevel@tonic-gate 	uIntf *,	/* code lengths in bits */
47650Sstevel@tonic-gate 	uInt,		/* number of codes */
47660Sstevel@tonic-gate 	uInt,		/* number of "simple" codes */
47670Sstevel@tonic-gate 	const uIntf *,	/* list of base values for non-simple codes */
47680Sstevel@tonic-gate 	const uIntf *,	/* list of extra bits for non-simple codes */
47690Sstevel@tonic-gate 	inflate_huft * FAR*, /* result: starting table */
47700Sstevel@tonic-gate 	uIntf *,	/* maximum lookup bits (returns actual) */
47710Sstevel@tonic-gate 	inflate_huft *hp,	/* space for trees */
47720Sstevel@tonic-gate 	uInt *hn,	/* hufts used in space */
47730Sstevel@tonic-gate 	uIntf *v));	/* working area: values in order of bit length */
47740Sstevel@tonic-gate 
47750Sstevel@tonic-gate /* Tables for deflate from PKZIP's appnote.txt. */
47760Sstevel@tonic-gate local const uInt cplens[31] = { /* Copy lengths for literal codes 257..285 */
47770Sstevel@tonic-gate 	3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
47780Sstevel@tonic-gate 	35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
47790Sstevel@tonic-gate 	/* see note #13 above about 258 */
47800Sstevel@tonic-gate local const uInt cplext[31] = { /* Extra bits for literal codes 257..285 */
47810Sstevel@tonic-gate 	0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
47820Sstevel@tonic-gate 	3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 112, 112};
47830Sstevel@tonic-gate 	/* 112==invalid */
47840Sstevel@tonic-gate local const uInt cpdist[30] = { /* Copy offsets for distance codes 0..29 */
47850Sstevel@tonic-gate 	1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
47860Sstevel@tonic-gate 	257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
47870Sstevel@tonic-gate 	8193, 12289, 16385, 24577};
47880Sstevel@tonic-gate local const uInt cpdext[30] = { /* Extra bits for distance codes */
47890Sstevel@tonic-gate 	0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
47900Sstevel@tonic-gate 	7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
47910Sstevel@tonic-gate 	12, 12, 13, 13};
47920Sstevel@tonic-gate 
47930Sstevel@tonic-gate /*
47940Sstevel@tonic-gate  * Huffman code decoding is performed using a multi-level table
47950Sstevel@tonic-gate  * lookup.  The fastest way to decode is to simply build a lookup
47960Sstevel@tonic-gate  * table whose size is determined by the longest code.  However, the
47970Sstevel@tonic-gate  * time it takes to build this table can also be a factor if the data
47980Sstevel@tonic-gate  * being decoded is not very long.  The most common codes are
47990Sstevel@tonic-gate  * necessarily the shortest codes, so those codes dominate the
48000Sstevel@tonic-gate  * decoding time, and hence the speed.  The idea is you can have a
48010Sstevel@tonic-gate  * shorter table that decodes the shorter, more probable codes, and
48020Sstevel@tonic-gate  * then point to subsidiary tables for the longer codes.  The time it
48030Sstevel@tonic-gate  * costs to decode the longer codes is then traded against the time it
48040Sstevel@tonic-gate  * takes to make longer tables.
48050Sstevel@tonic-gate  *
48060Sstevel@tonic-gate  * This results of this trade are in the variables lbits and dbits
48070Sstevel@tonic-gate  * below.  lbits is the number of bits the first level table for
48080Sstevel@tonic-gate  * literal/ length codes can decode in one step, and dbits is the same
48090Sstevel@tonic-gate  * thing for the distance codes.  Subsequent tables are also less than
48100Sstevel@tonic-gate  * or equal to those sizes.  These values may be adjusted either when
48110Sstevel@tonic-gate  * all of the codes are shorter than that, in which case the longest
48120Sstevel@tonic-gate  * code length in bits is used, or when the shortest code is *longer*
48130Sstevel@tonic-gate  * than the requested table size, in which case the length of the
48140Sstevel@tonic-gate  * shortest code in bits is used.
48150Sstevel@tonic-gate  *
48160Sstevel@tonic-gate  * There are two different values for the two tables, since they code
48170Sstevel@tonic-gate  * a different number of possibilities each.  The literal/length table
48180Sstevel@tonic-gate  * codes 286 possible values, or in a flat code, a little over eight
48190Sstevel@tonic-gate  * bits.  The distance table codes 30 possible values, or a little
48200Sstevel@tonic-gate  * less than five bits, flat.  The optimum values for speed end up
48210Sstevel@tonic-gate  * being about one bit more than those, so lbits is 8+1 and dbits is
48220Sstevel@tonic-gate  * 5+1.  The optimum values may differ though from machine to machine,
48230Sstevel@tonic-gate  * and possibly even between compilers.  Your mileage may vary.
48240Sstevel@tonic-gate  */
48250Sstevel@tonic-gate 
48260Sstevel@tonic-gate 
48270Sstevel@tonic-gate /* If BMAX needs to be larger than 16, then h and x[] should be uLong. */
48280Sstevel@tonic-gate #define	BMAX 15		/* maximum bit length of any code */
48290Sstevel@tonic-gate 
48300Sstevel@tonic-gate 
48310Sstevel@tonic-gate local int
huft_build(b,n,s,d,e,t,m,hp,hn,v)48320Sstevel@tonic-gate huft_build(b, n, s, d, e, t, m, hp, hn, v)
48330Sstevel@tonic-gate uIntf *b;	/* code lengths in bits (all assumed <= BMAX) */
48340Sstevel@tonic-gate uInt n;	/* number of codes (assumed <= 288) */
48350Sstevel@tonic-gate uInt s;	/* number of simple-valued codes (0..s-1) */
48360Sstevel@tonic-gate const uIntf *d;	/* list of base values for non-simple codes */
48370Sstevel@tonic-gate const uIntf *e;	/* list of extra bits for non-simple codes */
48380Sstevel@tonic-gate inflate_huft * FAR *t;	/* result: starting table */
48390Sstevel@tonic-gate uIntf *m;	/* maximum lookup bits, returns actual */
48400Sstevel@tonic-gate inflate_huft *hp;	/* space for trees */
48410Sstevel@tonic-gate uInt *hn;		/* hufts used in space */
48420Sstevel@tonic-gate uIntf *v;		/* working area: values in order of bit length */
48430Sstevel@tonic-gate /*
48440Sstevel@tonic-gate  * Given a list of code lengths and a maximum table size, make a set
48450Sstevel@tonic-gate  * of tables to decode that set of codes.  Return Z_OK on success,
48460Sstevel@tonic-gate  * Z_BUF_ERROR if the given code set is incomplete (the tables are
48470Sstevel@tonic-gate  * still built in this case), Z_DATA_ERROR if the input is invalid (an
48480Sstevel@tonic-gate  * over-subscribed set of lengths), or Z_MEM_ERROR if not enough
48490Sstevel@tonic-gate  * memory.
48500Sstevel@tonic-gate  */
48510Sstevel@tonic-gate {
48520Sstevel@tonic-gate 
48530Sstevel@tonic-gate 	uInt a;	/* counter for codes of length k */
48540Sstevel@tonic-gate 	uInt c[BMAX+1];	/* bit length count table */
48550Sstevel@tonic-gate 	uInt f;	/* i repeats in table every f entries */
48560Sstevel@tonic-gate 	int g;	/* maximum code length */
48570Sstevel@tonic-gate 	int h;	/* table level */
48580Sstevel@tonic-gate 	register uInt i;	/* counter, current code */
48590Sstevel@tonic-gate 	register uInt j;	/* counter */
48600Sstevel@tonic-gate 	register int k;	/* number of bits in current code */
48610Sstevel@tonic-gate 	int l;	/* bits per table (returned in m) */
48620Sstevel@tonic-gate 	register uIntf *p;	/* pointer into c[], b[], or v[] */
48630Sstevel@tonic-gate 	inflate_huft *q;	/* points to current table */
48640Sstevel@tonic-gate 	struct inflate_huft_s r; /* table entry for structure assignment */
48650Sstevel@tonic-gate 	inflate_huft *u[BMAX];	/* table stack */
48660Sstevel@tonic-gate 	uInt mask;	/* (1 << w) - 1, to avoid cc -O bug on HP */
48670Sstevel@tonic-gate 	register int w;	/* bits before this table == (l * h) */
48680Sstevel@tonic-gate 	uInt x[BMAX+1];	/* bit offsets, then code stack */
48690Sstevel@tonic-gate 	uIntf *xp;	/* pointer into x */
48700Sstevel@tonic-gate 	int y;	/* number of dummy codes added */
48710Sstevel@tonic-gate 	uInt z;	/* number of entries in current table */
48720Sstevel@tonic-gate 
48730Sstevel@tonic-gate 	(void) inflate_copyright;
48740Sstevel@tonic-gate 	/* Generate counts for each bit length */
48750Sstevel@tonic-gate 	p = c;
48760Sstevel@tonic-gate #define	C0 *p++ = 0;
48770Sstevel@tonic-gate #define	C2 C0 C0 C0 C0
48780Sstevel@tonic-gate #define	C4 C2 C2 C2 C2
48790Sstevel@tonic-gate 	C4	/* clear c[]--assume BMAX+1 is 16 */
48800Sstevel@tonic-gate 	    p = b;  i = n;
48810Sstevel@tonic-gate 	do {
48820Sstevel@tonic-gate 		c[*p++]++;	/* assume all entries <= BMAX */
48830Sstevel@tonic-gate 	} while (--i);
48840Sstevel@tonic-gate 	if (c[0] == n)		/* null input--all zero length codes */
48850Sstevel@tonic-gate 	{
48860Sstevel@tonic-gate 		*t = (inflate_huft *)Z_NULL;
48870Sstevel@tonic-gate 		*m = 0;
48880Sstevel@tonic-gate 		return (Z_OK);
48890Sstevel@tonic-gate 	}
48900Sstevel@tonic-gate 
48910Sstevel@tonic-gate 
48920Sstevel@tonic-gate 	/* Find minimum and maximum length, bound *m by those */
48930Sstevel@tonic-gate 	l = *m;
48940Sstevel@tonic-gate 	for (j = 1; j <= BMAX; j++)
48950Sstevel@tonic-gate 		if (c[j])
48960Sstevel@tonic-gate 			break;
48970Sstevel@tonic-gate 	k = j;	/* minimum code length */
48980Sstevel@tonic-gate 	if ((uInt)l < j)
48990Sstevel@tonic-gate 		l = j;
49000Sstevel@tonic-gate 	for (i = BMAX; i; i--)
49010Sstevel@tonic-gate 		if (c[i])
49020Sstevel@tonic-gate 			break;
49030Sstevel@tonic-gate 	g = i;	/* maximum code length */
49040Sstevel@tonic-gate 	if ((uInt)l > i)
49050Sstevel@tonic-gate 		l = i;
49060Sstevel@tonic-gate 	*m = l;
49070Sstevel@tonic-gate 
49080Sstevel@tonic-gate 
49090Sstevel@tonic-gate 	/* Adjust last length count to fill out codes, if needed */
49100Sstevel@tonic-gate 	for (y = 1 << j; j < i; j++, y <<= 1)
49110Sstevel@tonic-gate 		if ((y -= c[j]) < 0)
49120Sstevel@tonic-gate 			return (Z_DATA_ERROR);
49130Sstevel@tonic-gate 	if ((y -= c[i]) < 0)
49140Sstevel@tonic-gate 		return (Z_DATA_ERROR);
49150Sstevel@tonic-gate 	c[i] += y;
49160Sstevel@tonic-gate 
49170Sstevel@tonic-gate 
49180Sstevel@tonic-gate 	/* Generate starting offsets into the value table for each length */
49190Sstevel@tonic-gate 	x[1] = j = 0;
49200Sstevel@tonic-gate 	p = c + 1;  xp = x + 2;
49210Sstevel@tonic-gate 	while (--i) {		/* note that i == g from above */
49220Sstevel@tonic-gate 		*xp++ = (j += *p++);
49230Sstevel@tonic-gate 	}
49240Sstevel@tonic-gate 
49250Sstevel@tonic-gate 
49260Sstevel@tonic-gate 	/* Make a table of values in order of bit lengths */
49270Sstevel@tonic-gate 	p = b;  i = 0;
49280Sstevel@tonic-gate 	do {
49290Sstevel@tonic-gate 		if ((j = *p++) != 0)
49300Sstevel@tonic-gate 			v[x[j]++] = i;
49310Sstevel@tonic-gate 	} while (++i < n);
49320Sstevel@tonic-gate 	n = x[g];	/* set n to length of v */
49330Sstevel@tonic-gate 
49340Sstevel@tonic-gate 
49350Sstevel@tonic-gate 	/* Generate the Huffman codes and for each, make the table entries */
49360Sstevel@tonic-gate 	x[0] = i = 0;	/* first Huffman code is zero */
49370Sstevel@tonic-gate 	p = v;	/* grab values in bit order */
49380Sstevel@tonic-gate 	h = -1;	/* no tables yet--level -1 */
49390Sstevel@tonic-gate 	w = -l;	/* bits decoded == (l * h) */
49400Sstevel@tonic-gate 	u[0] = (inflate_huft *)Z_NULL;	/* just to keep compilers happy */
49410Sstevel@tonic-gate 	q = (inflate_huft *)Z_NULL;	/* ditto */
49420Sstevel@tonic-gate 	z = 0;	/* ditto */
49430Sstevel@tonic-gate 
49440Sstevel@tonic-gate 	/* go through the bit lengths (k already is bits in shortest code) */
49450Sstevel@tonic-gate 	for (; k <= g; k++) {
49460Sstevel@tonic-gate 		a = c[k];
49470Sstevel@tonic-gate 		while (a--) {
49480Sstevel@tonic-gate 			/*
49490Sstevel@tonic-gate 			 * here i is the Huffman code of length k bits
49500Sstevel@tonic-gate 			 * for value *p.  make tables up to required
49510Sstevel@tonic-gate 			 * level.
49520Sstevel@tonic-gate 			 */
49530Sstevel@tonic-gate 			while (k > w + l) {
49540Sstevel@tonic-gate 				h++;
49550Sstevel@tonic-gate 				w += l;	/* previous table always l bits */
49560Sstevel@tonic-gate 
49570Sstevel@tonic-gate 				/*
49580Sstevel@tonic-gate 				 * compute minimum size table less
49590Sstevel@tonic-gate 				 * than or equal to l bits
49600Sstevel@tonic-gate 				 */
49610Sstevel@tonic-gate 				z = g - w;
49620Sstevel@tonic-gate 				/* table size upper limit */
49630Sstevel@tonic-gate 				z = z > (uInt)l ? l : z;
49640Sstevel@tonic-gate 				/* try a k-w bit table */
49650Sstevel@tonic-gate 				if ((f = 1 << (j = k - w)) > a + 1) {
49660Sstevel@tonic-gate 					/* too few codes for k-w bit table */
49670Sstevel@tonic-gate 					/* deduct codes from patterns left */
49680Sstevel@tonic-gate 					f -= a + 1;
49690Sstevel@tonic-gate 					xp = c + k;
49700Sstevel@tonic-gate 					if (j < z)
49710Sstevel@tonic-gate 						/*
49720Sstevel@tonic-gate 						 * try smaller tables
49730Sstevel@tonic-gate 						 * up to z bits
49740Sstevel@tonic-gate 						 */
49750Sstevel@tonic-gate 						while (++j < z) {
49760Sstevel@tonic-gate 							/*
49770Sstevel@tonic-gate 							 * enough
49780Sstevel@tonic-gate 							 * codes to
49790Sstevel@tonic-gate 							 * use up j
49800Sstevel@tonic-gate 							 * bits
49810Sstevel@tonic-gate 							 */
49820Sstevel@tonic-gate 							if ((f <<= 1) <= *++xp)
49830Sstevel@tonic-gate 								break;
49840Sstevel@tonic-gate 							f -= *xp;
49850Sstevel@tonic-gate 							/*
49860Sstevel@tonic-gate 							 * else deduct
49870Sstevel@tonic-gate 							 * codes from
49880Sstevel@tonic-gate 							 * patterns
49890Sstevel@tonic-gate 							 */
49900Sstevel@tonic-gate 						}
49910Sstevel@tonic-gate 				}
49920Sstevel@tonic-gate 				/* table entries for j-bit table */
49930Sstevel@tonic-gate 				z = 1 << j;
49940Sstevel@tonic-gate 
49950Sstevel@tonic-gate 				/* allocate new table */
49960Sstevel@tonic-gate 				/* (note: doesn't matter for fixed) */
49970Sstevel@tonic-gate 				/* not enough memory */
49980Sstevel@tonic-gate 				if (*hn + z > MANY)
49990Sstevel@tonic-gate 					return (Z_MEM_ERROR);
50000Sstevel@tonic-gate 				u[h] = q = hp + *hn;
50010Sstevel@tonic-gate 				*hn += z;
50020Sstevel@tonic-gate 
50030Sstevel@tonic-gate 				/* connect to last table, if there is one */
50040Sstevel@tonic-gate 				if (h) {
50050Sstevel@tonic-gate 					/* save pattern for backing up */
50060Sstevel@tonic-gate 					x[h] = i;
50070Sstevel@tonic-gate 					/* bits to dump before this table */
50080Sstevel@tonic-gate 					r.bits = (Byte)l;
50090Sstevel@tonic-gate 					/* bits in this table */
50100Sstevel@tonic-gate 					r.exop = (Byte)j;
50110Sstevel@tonic-gate 					j = i >> (w - l);
50120Sstevel@tonic-gate 					/* offset to this table */
50130Sstevel@tonic-gate 					r.base = (uInt)(q - u[h-1] - j);
50140Sstevel@tonic-gate 					/* connect to last table */
50150Sstevel@tonic-gate 					u[h-1][j] = r;
50160Sstevel@tonic-gate 				} else
50170Sstevel@tonic-gate 					/* first table is returned result */
50180Sstevel@tonic-gate 					*t = q;
50190Sstevel@tonic-gate 			}
50200Sstevel@tonic-gate 
50210Sstevel@tonic-gate 			/* set up table entry in r */
50220Sstevel@tonic-gate 			r.bits = (Byte)(k - w);
50230Sstevel@tonic-gate 			if (p >= v + n)
50240Sstevel@tonic-gate 				/* out of values--invalid code */
50250Sstevel@tonic-gate 				r.exop = 128 + 64;
50260Sstevel@tonic-gate 			else if (*p < s)
50270Sstevel@tonic-gate 			{
50280Sstevel@tonic-gate 				/* 256 is end-of-block */
50290Sstevel@tonic-gate 				r.exop = (Byte)(*p < 256 ? 0 : 32 + 64);
50300Sstevel@tonic-gate 				/* simple code is just the value */
50310Sstevel@tonic-gate 				r.base = *p++;
50320Sstevel@tonic-gate 			}
50330Sstevel@tonic-gate 			else
50340Sstevel@tonic-gate 			{
50350Sstevel@tonic-gate 				/* non-simple--look up in lists */
50360Sstevel@tonic-gate 				r.exop = (Byte)(e[*p - s] + 16 + 64);
50370Sstevel@tonic-gate 				r.base = d[*p++ - s];
50380Sstevel@tonic-gate 			}
50390Sstevel@tonic-gate 
50400Sstevel@tonic-gate 			/* fill code-like entries with r */
50410Sstevel@tonic-gate 			f = 1 << (k - w);
50420Sstevel@tonic-gate 			for (j = i >> w; j < z; j += f)
50430Sstevel@tonic-gate 				q[j] = r;
50440Sstevel@tonic-gate 
50450Sstevel@tonic-gate 			/* backwards increment the k-bit code i */
50460Sstevel@tonic-gate 			for (j = 1 << (k - 1); i & j; j >>= 1)
50470Sstevel@tonic-gate 				i ^= j;
50480Sstevel@tonic-gate 			i ^= j;
50490Sstevel@tonic-gate 
50500Sstevel@tonic-gate 			/* backup over finished tables */
50510Sstevel@tonic-gate 			mask = (1 << w) - 1;	/* needed on HP, cc -O bug */
50520Sstevel@tonic-gate 			while ((i & mask) != x[h])
50530Sstevel@tonic-gate 			{
50540Sstevel@tonic-gate 				h--;	/* don't need to update q */
50550Sstevel@tonic-gate 				w -= l;
50560Sstevel@tonic-gate 				mask = (1 << w) - 1;
50570Sstevel@tonic-gate 			}
50580Sstevel@tonic-gate 		}
50590Sstevel@tonic-gate 	}
50600Sstevel@tonic-gate 
50610Sstevel@tonic-gate 
50620Sstevel@tonic-gate 	/* Return Z_BUF_ERROR if we were given an incomplete table */
50630Sstevel@tonic-gate 	return (y != 0 && g != 1 ? Z_BUF_ERROR : Z_OK);
50640Sstevel@tonic-gate }
50650Sstevel@tonic-gate 
50660Sstevel@tonic-gate 
50670Sstevel@tonic-gate int
inflate_trees_bits(c,bb,tb,hp,z)50680Sstevel@tonic-gate inflate_trees_bits(c, bb, tb, hp, z)
50690Sstevel@tonic-gate uIntf *c;	/* 19 code lengths */
50700Sstevel@tonic-gate uIntf *bb;	/* bits tree desired/actual depth */
50710Sstevel@tonic-gate inflate_huft * FAR *tb;	/* bits tree result */
50720Sstevel@tonic-gate inflate_huft *hp;	/* space for trees */
50730Sstevel@tonic-gate z_streamp z;	/* for zfree function */
50740Sstevel@tonic-gate {
50750Sstevel@tonic-gate 	int r;
50760Sstevel@tonic-gate 	uInt hn = 0;		/* hufts used in space */
50770Sstevel@tonic-gate 	uIntf v[19];		/* work area for huft_build */
50780Sstevel@tonic-gate 
50790Sstevel@tonic-gate 	r = huft_build(c, 19, 19, (uIntf*)Z_NULL, (uIntf*)Z_NULL, tb, bb,
50800Sstevel@tonic-gate 	    hp, &hn, v);
50810Sstevel@tonic-gate 	if (r == Z_DATA_ERROR)
50820Sstevel@tonic-gate 		z->msg = "oversubscribed dynamic bit lengths tree";
50830Sstevel@tonic-gate 	else if (r == Z_BUF_ERROR || *bb == 0)
50840Sstevel@tonic-gate 	{
50850Sstevel@tonic-gate 		z->msg = "incomplete dynamic bit lengths tree";
50860Sstevel@tonic-gate 		r = Z_DATA_ERROR;
50870Sstevel@tonic-gate 	}
50880Sstevel@tonic-gate 	return (r);
50890Sstevel@tonic-gate }
50900Sstevel@tonic-gate 
50910Sstevel@tonic-gate 
50920Sstevel@tonic-gate int
inflate_trees_dynamic(nl,nd,c,bl,bd,tl,td,hp,z)50930Sstevel@tonic-gate inflate_trees_dynamic(nl, nd, c, bl, bd, tl, td, hp, z)
50940Sstevel@tonic-gate uInt nl;	/* number of literal/length codes */
50950Sstevel@tonic-gate uInt nd;	/* number of distance codes */
50960Sstevel@tonic-gate uIntf *c;	/* that many (total) code lengths */
50970Sstevel@tonic-gate uIntf *bl;	/* literal desired/actual bit depth */
50980Sstevel@tonic-gate uIntf *bd;	/* distance desired/actual bit depth */
50990Sstevel@tonic-gate inflate_huft * FAR *tl;	/* literal/length tree result */
51000Sstevel@tonic-gate inflate_huft * FAR *td;	/* distance tree result */
51010Sstevel@tonic-gate inflate_huft *hp;	/* space for trees */
51020Sstevel@tonic-gate z_streamp z;	/* for zfree function */
51030Sstevel@tonic-gate {
51040Sstevel@tonic-gate 	int r;
51050Sstevel@tonic-gate 	uInt hn = 0;		/* hufts used in space */
51060Sstevel@tonic-gate 	uIntf v[288];		/* work area for huft_build */
51070Sstevel@tonic-gate 
51080Sstevel@tonic-gate 	/* build literal/length tree */
51090Sstevel@tonic-gate 	r = huft_build(c, nl, 257, cplens, cplext, tl, bl, hp, &hn, v);
51100Sstevel@tonic-gate 	if (r != Z_OK || *bl == 0)
51110Sstevel@tonic-gate 	{
51120Sstevel@tonic-gate 		if (r == Z_DATA_ERROR)
51130Sstevel@tonic-gate 			z->msg = "oversubscribed literal/length tree";
51140Sstevel@tonic-gate 		else if (r != Z_MEM_ERROR)
51150Sstevel@tonic-gate 		{
51160Sstevel@tonic-gate 			z->msg = "incomplete literal/length tree";
51170Sstevel@tonic-gate 			r = Z_DATA_ERROR;
51180Sstevel@tonic-gate 		}
51190Sstevel@tonic-gate 		return (r);
51200Sstevel@tonic-gate 	}
51210Sstevel@tonic-gate 
51220Sstevel@tonic-gate 	/* build distance tree */
51230Sstevel@tonic-gate 	r = huft_build(c + nl, nd, 0, cpdist, cpdext, td, bd, hp, &hn, v);
51240Sstevel@tonic-gate 	if (r != Z_OK || (*bd == 0 && nl > 257))
51250Sstevel@tonic-gate 	{
51260Sstevel@tonic-gate 		if (r == Z_DATA_ERROR)
51270Sstevel@tonic-gate 			z->msg = "oversubscribed distance tree";
51280Sstevel@tonic-gate 		else if (r == Z_BUF_ERROR) {
51290Sstevel@tonic-gate #ifdef PKZIP_BUG_WORKAROUND
51300Sstevel@tonic-gate 			r = Z_OK;
51310Sstevel@tonic-gate #else
51320Sstevel@tonic-gate 			z->msg = "incomplete distance tree";
51330Sstevel@tonic-gate 			r = Z_DATA_ERROR;
51340Sstevel@tonic-gate 		} else if (r != Z_MEM_ERROR) {
51350Sstevel@tonic-gate 			z->msg = "empty distance tree with lengths";
51360Sstevel@tonic-gate 			r = Z_DATA_ERROR;
51370Sstevel@tonic-gate #endif
51380Sstevel@tonic-gate 		}
51390Sstevel@tonic-gate 		return (r);
51400Sstevel@tonic-gate 	}
51410Sstevel@tonic-gate 
51420Sstevel@tonic-gate /* done */
51430Sstevel@tonic-gate 	return (Z_OK);
51440Sstevel@tonic-gate }
51450Sstevel@tonic-gate 
51460Sstevel@tonic-gate 
51470Sstevel@tonic-gate /* build fixed tables only once--keep them here */
51480Sstevel@tonic-gate /* #define	BUILDFIXED */
51490Sstevel@tonic-gate #ifdef BUILDFIXED
51500Sstevel@tonic-gate local int fixed_built = 0;
51510Sstevel@tonic-gate #define	FIXEDH 544	/* number of hufts used by fixed tables */
51520Sstevel@tonic-gate local inflate_huft fixed_mem[FIXEDH];
51530Sstevel@tonic-gate local uInt fixed_bl;
51540Sstevel@tonic-gate local uInt fixed_bd;
51550Sstevel@tonic-gate local inflate_huft *fixed_tl;
51560Sstevel@tonic-gate local inflate_huft *fixed_td;
51570Sstevel@tonic-gate #else
51580Sstevel@tonic-gate #include "inffixed.h"
51590Sstevel@tonic-gate #endif
51600Sstevel@tonic-gate 
51610Sstevel@tonic-gate /*ARGSUSED*/
51620Sstevel@tonic-gate int
inflate_trees_fixed(bl,bd,tl,td,z)51630Sstevel@tonic-gate inflate_trees_fixed(bl, bd, tl, td, z)
51640Sstevel@tonic-gate uIntf *bl;	/* literal desired/actual bit depth */
51650Sstevel@tonic-gate uIntf *bd;	/* distance desired/actual bit depth */
51660Sstevel@tonic-gate const inflate_huft * FAR *tl;	/* literal/length tree result */
51670Sstevel@tonic-gate const inflate_huft * FAR *td;	/* distance tree result */
51680Sstevel@tonic-gate z_streamp z;	/* for memory allocation */
51690Sstevel@tonic-gate {
51700Sstevel@tonic-gate #ifdef BUILDFIXED
51710Sstevel@tonic-gate 	/*
51720Sstevel@tonic-gate 	 * build fixed tables if not already (multiple overlapped
51730Sstevel@tonic-gate 	 * executions ok)
51740Sstevel@tonic-gate 	 */
51750Sstevel@tonic-gate 	if (!fixed_built)
51760Sstevel@tonic-gate 	{
51770Sstevel@tonic-gate 		int k;	/* temporary variable */
51780Sstevel@tonic-gate 		uInt f = 0;	/* number of hufts used in fixed_mem */
51790Sstevel@tonic-gate 		uIntf *c;	/* length list for huft_build */
51800Sstevel@tonic-gate 		uIntf *v;
51810Sstevel@tonic-gate 
51820Sstevel@tonic-gate 		/* allocate memory */
51830Sstevel@tonic-gate 		if ((c = (uIntf*)ZALLOC(z, 288, sizeof (uInt))) == Z_NULL)
51840Sstevel@tonic-gate 			return (Z_MEM_ERROR);
51850Sstevel@tonic-gate 		if ((v = (uIntf*)ZALLOC(z, 288, sizeof (uInt))) == Z_NULL)
51860Sstevel@tonic-gate 		{
51870Sstevel@tonic-gate 			ZFREE(z, c);
51880Sstevel@tonic-gate 			return (Z_MEM_ERROR);
51890Sstevel@tonic-gate 		}
51900Sstevel@tonic-gate 		/* literal table */
51910Sstevel@tonic-gate 		for (k = 0; k < 144; k++)
51920Sstevel@tonic-gate 			c[k] = 8;
51930Sstevel@tonic-gate 		for (; k < 256; k++)
51940Sstevel@tonic-gate 			c[k] = 9;
51950Sstevel@tonic-gate 		for (; k < 280; k++)
51960Sstevel@tonic-gate 			c[k] = 7;
51970Sstevel@tonic-gate 		for (; k < 288; k++)
51980Sstevel@tonic-gate 			c[k] = 8;
51990Sstevel@tonic-gate 		fixed_bl = 9;
52000Sstevel@tonic-gate 		(void) huft_build(c, 288, 257, cplens, cplext, &fixed_tl,
52010Sstevel@tonic-gate 		    &fixed_bl, fixed_mem, &f, v);
52020Sstevel@tonic-gate 
52030Sstevel@tonic-gate 		/* distance table */
52040Sstevel@tonic-gate 		for (k = 0; k < 30; k++)
52050Sstevel@tonic-gate 			c[k] = 5;
52060Sstevel@tonic-gate 		fixed_bd = 5;
52070Sstevel@tonic-gate 		(void) huft_build(c, 30, 0, cpdist, cpdext, &fixed_td,
52080Sstevel@tonic-gate 		    &fixed_bd, fixed_mem, &f, v);
52090Sstevel@tonic-gate 
52100Sstevel@tonic-gate 		/* done */
52110Sstevel@tonic-gate 		ZFREE(z, v);
52120Sstevel@tonic-gate 		ZFREE(z, c);
52130Sstevel@tonic-gate 		fixed_built = 1;
52140Sstevel@tonic-gate 	}
52150Sstevel@tonic-gate #endif
52160Sstevel@tonic-gate 	*bl = fixed_bl;
52170Sstevel@tonic-gate 	*bd = fixed_bd;
52180Sstevel@tonic-gate 	*tl = fixed_tl;
52190Sstevel@tonic-gate 	*td = fixed_td;
52200Sstevel@tonic-gate 	return (Z_OK);
52210Sstevel@tonic-gate }
52220Sstevel@tonic-gate /* --- inftrees.c */
52230Sstevel@tonic-gate 
52240Sstevel@tonic-gate /* +++ infcodes.c */
52250Sstevel@tonic-gate /*
52260Sstevel@tonic-gate  * infcodes.c -- process literals and length/distance pairs
52270Sstevel@tonic-gate  * Copyright (C) 1995-1998 Mark Adler
52280Sstevel@tonic-gate  * For conditions of distribution and use, see copyright notice in zlib.h
52290Sstevel@tonic-gate  */
52300Sstevel@tonic-gate 
52310Sstevel@tonic-gate /* #include "zutil.h" */
52320Sstevel@tonic-gate /* #include "inftrees.h" */
52330Sstevel@tonic-gate /* #include "infblock.h" */
52340Sstevel@tonic-gate /* #include "infcodes.h" */
52350Sstevel@tonic-gate /* #include "infutil.h" */
52360Sstevel@tonic-gate 
52370Sstevel@tonic-gate /* +++ inffast.h */
52380Sstevel@tonic-gate /*
52390Sstevel@tonic-gate  * inffast.h -- header to use inffast.c
52400Sstevel@tonic-gate  * Copyright (C) 1995-1998 Mark Adler
52410Sstevel@tonic-gate  * For conditions of distribution and use, see copyright notice in zlib.h
52420Sstevel@tonic-gate  */
52430Sstevel@tonic-gate 
52440Sstevel@tonic-gate /*
52450Sstevel@tonic-gate  * WARNING: this file should *not* be used by applications. It is part
52460Sstevel@tonic-gate  * of the implementation of the compression library and is subject to
52470Sstevel@tonic-gate  * change. Applications should only use zlib.h.
52480Sstevel@tonic-gate  */
52490Sstevel@tonic-gate 
52500Sstevel@tonic-gate extern int inflate_fast OF((
52510Sstevel@tonic-gate     uInt,
52520Sstevel@tonic-gate     uInt,
52530Sstevel@tonic-gate     const inflate_huft *,
52540Sstevel@tonic-gate     const inflate_huft *,
52550Sstevel@tonic-gate     inflate_blocks_statef *,
52560Sstevel@tonic-gate     z_streamp));
52570Sstevel@tonic-gate /* --- inffast.h */
52580Sstevel@tonic-gate 
52590Sstevel@tonic-gate /* simplify the use of the inflate_huft type with some defines */
52600Sstevel@tonic-gate #define	exop word.what.Exop
52610Sstevel@tonic-gate #define	bits word.what.Bits
52620Sstevel@tonic-gate 
52630Sstevel@tonic-gate /* inflate codes private state */
52640Sstevel@tonic-gate struct inflate_codes_state {
52650Sstevel@tonic-gate 
52660Sstevel@tonic-gate 	/* mode */
52670Sstevel@tonic-gate 	enum {	/* waiting for "i:"=input, "o:"=output, "x:"=nothing */
52680Sstevel@tonic-gate 		START,	/* x: set up for LEN */
52690Sstevel@tonic-gate 		LEN,	/* i: get length/literal/eob next */
52700Sstevel@tonic-gate 		LENEXT,	/* i: getting length extra (have base) */
52710Sstevel@tonic-gate 		DIST,	/* i: get distance next */
52720Sstevel@tonic-gate 		DISTEXT,	/* i: getting distance extra */
52730Sstevel@tonic-gate 		COPY,	/* o: copying bytes in window, waiting for space */
52740Sstevel@tonic-gate 		LIT,	/* o: got literal, waiting for output space */
52750Sstevel@tonic-gate 		WASH,	/* o: got eob, possibly still output waiting */
52760Sstevel@tonic-gate 		END,	/* x: got eob and all data flushed */
52770Sstevel@tonic-gate 		BADCODE}	/* x: got error */
52780Sstevel@tonic-gate 	mode;	/* current inflate_codes mode */
52790Sstevel@tonic-gate 
52800Sstevel@tonic-gate 	/* mode dependent information */
52810Sstevel@tonic-gate 	uInt len;
52820Sstevel@tonic-gate 	union {
52830Sstevel@tonic-gate 		struct {
52840Sstevel@tonic-gate 			const inflate_huft *tree;	/* pointer into tree */
52850Sstevel@tonic-gate 			uInt need;	/* bits needed */
52860Sstevel@tonic-gate 		} code;	/* if LEN or DIST, where in tree */
52870Sstevel@tonic-gate 		uInt lit;	/* if LIT, literal */
52880Sstevel@tonic-gate 		struct {
52890Sstevel@tonic-gate 			uInt get;	/* bits to get for extra */
52900Sstevel@tonic-gate 			uInt dist;	/* distance back to copy from */
52910Sstevel@tonic-gate 		} copy;	/* if EXT or COPY, where and how much */
52920Sstevel@tonic-gate 	} sub;	/* submode */
52930Sstevel@tonic-gate 
52940Sstevel@tonic-gate 	/* mode independent information */
52950Sstevel@tonic-gate 	Byte lbits;	/* ltree bits decoded per branch */
52960Sstevel@tonic-gate 	Byte dbits;	/* dtree bits decoder per branch */
52970Sstevel@tonic-gate 	const inflate_huft *ltree;	/* literal/length/eob tree */
52980Sstevel@tonic-gate 	const inflate_huft *dtree;	/* distance tree */
52990Sstevel@tonic-gate 
53000Sstevel@tonic-gate };
53010Sstevel@tonic-gate 
53020Sstevel@tonic-gate 
53030Sstevel@tonic-gate inflate_codes_statef *
inflate_codes_new(bl,bd,tl,td,z)53040Sstevel@tonic-gate inflate_codes_new(bl, bd, tl, td, z)
53050Sstevel@tonic-gate uInt bl, bd;
53060Sstevel@tonic-gate const inflate_huft *tl;
53070Sstevel@tonic-gate const inflate_huft *td;	/* need separate declaration for Borland C++ */
53080Sstevel@tonic-gate z_streamp z;
53090Sstevel@tonic-gate {
53100Sstevel@tonic-gate 	inflate_codes_statef *c;
53110Sstevel@tonic-gate 
53120Sstevel@tonic-gate 	if ((c = (inflate_codes_statef *)
53130Sstevel@tonic-gate 	    ZALLOC(z, 1, sizeof (struct inflate_codes_state))) != Z_NULL)
53140Sstevel@tonic-gate 	{
53150Sstevel@tonic-gate 		c->mode = START;
53160Sstevel@tonic-gate 		c->lbits = (Byte)bl;
53170Sstevel@tonic-gate 		c->dbits = (Byte)bd;
53180Sstevel@tonic-gate 		c->ltree = tl;
53190Sstevel@tonic-gate 		c->dtree = td;
53200Sstevel@tonic-gate 		Tracev((stderr, "inflate:       codes new\n"));
53210Sstevel@tonic-gate 	}
53220Sstevel@tonic-gate 	return (c);
53230Sstevel@tonic-gate }
53240Sstevel@tonic-gate 
53250Sstevel@tonic-gate 
53260Sstevel@tonic-gate int
inflate_codes(s,z,r)53270Sstevel@tonic-gate inflate_codes(s, z, r)
53280Sstevel@tonic-gate inflate_blocks_statef *s;
53290Sstevel@tonic-gate z_streamp z;
53300Sstevel@tonic-gate int r;
53310Sstevel@tonic-gate {
53320Sstevel@tonic-gate 	uInt j;	/* temporary storage */
53330Sstevel@tonic-gate 	const inflate_huft *t;	/* temporary pointer */
53340Sstevel@tonic-gate 	uInt e;	/* extra bits or operation */
53350Sstevel@tonic-gate 	uLong b;	/* bit buffer */
53360Sstevel@tonic-gate 	uInt k;	/* bits in bit buffer */
53370Sstevel@tonic-gate 	Bytef *p;	/* input data pointer */
53380Sstevel@tonic-gate 	uInt n;	/* bytes available there */
53390Sstevel@tonic-gate 	Bytef *q;	/* output window write pointer */
53400Sstevel@tonic-gate 	uInt m;	/* bytes to end of window or read pointer */
53410Sstevel@tonic-gate 	Bytef *f;	/* pointer to copy strings from */
53420Sstevel@tonic-gate 	inflate_codes_statef *c = s->sub.decode.codes;	/* codes state */
53430Sstevel@tonic-gate 
53440Sstevel@tonic-gate 	/* copy input/output information to locals (UPDATE macro restores) */
53450Sstevel@tonic-gate 	LOAD;
53460Sstevel@tonic-gate 
53470Sstevel@tonic-gate 	/* process input and output based on current state */
53480Sstevel@tonic-gate 	/* CONSTCOND */
53490Sstevel@tonic-gate 	while (1)
53500Sstevel@tonic-gate 		/* waiting for "i:"=input, "o:"=output, "x:"=nothing */
53510Sstevel@tonic-gate 		switch (c->mode) {
53520Sstevel@tonic-gate 		case START:	/* x: set up for LEN */
53530Sstevel@tonic-gate #ifndef SLOW
53540Sstevel@tonic-gate 			if (m >= 258 && n >= 10)
53550Sstevel@tonic-gate 			{
53560Sstevel@tonic-gate 				UPDATE;
53570Sstevel@tonic-gate 				r = inflate_fast(c->lbits, c->dbits,
53580Sstevel@tonic-gate 				    c->ltree, c->dtree, s, z);
53590Sstevel@tonic-gate 				LOAD;
53600Sstevel@tonic-gate 				if (r != Z_OK) {
53610Sstevel@tonic-gate 					c->mode = r == Z_STREAM_END ?
53620Sstevel@tonic-gate 					    WASH : BADCODE;
53630Sstevel@tonic-gate 					break;
53640Sstevel@tonic-gate 				}
53650Sstevel@tonic-gate 			}
53660Sstevel@tonic-gate #endif /* !SLOW */
53670Sstevel@tonic-gate 			c->sub.code.need = c->lbits;
53680Sstevel@tonic-gate 			c->sub.code.tree = c->ltree;
53690Sstevel@tonic-gate 			c->mode = LEN;
53700Sstevel@tonic-gate 			/* FALLTHRU */
53710Sstevel@tonic-gate 		case LEN:	/* i: get length/literal/eob next */
53720Sstevel@tonic-gate 			j = c->sub.code.need;
53730Sstevel@tonic-gate 			NEEDBITS(j);
53740Sstevel@tonic-gate 			t = c->sub.code.tree +
53750Sstevel@tonic-gate 			    ((uInt)b & inflate_mask[j]);
53760Sstevel@tonic-gate 			DUMPBITS(t->bits);
53770Sstevel@tonic-gate 			e = (uInt)(t->exop);
53780Sstevel@tonic-gate 			if (e == 0) {	/* literal */
53790Sstevel@tonic-gate 				c->sub.lit = t->base;
53800Sstevel@tonic-gate 				Tracevv((stderr, t->base >= 0x20 &&
53810Sstevel@tonic-gate 				    t->base < 0x7f ?
53820Sstevel@tonic-gate 				    "inflate:         literal '%c'\n" :
53830Sstevel@tonic-gate 				    "inflate:         literal 0x%02x\n",
53840Sstevel@tonic-gate 				    t->base));
53850Sstevel@tonic-gate 				c->mode = LIT;
53860Sstevel@tonic-gate 				break;
53870Sstevel@tonic-gate 			}
53880Sstevel@tonic-gate 			if (e & 16) {	/* length */
53890Sstevel@tonic-gate 				c->sub.copy.get = e & 15;
53900Sstevel@tonic-gate 				c->len = t->base;
53910Sstevel@tonic-gate 				c->mode = LENEXT;
53920Sstevel@tonic-gate 				break;
53930Sstevel@tonic-gate 			}
53940Sstevel@tonic-gate 			if ((e & 64) == 0) {	/* next table */
53950Sstevel@tonic-gate 				c->sub.code.need = e;
53960Sstevel@tonic-gate 				c->sub.code.tree = t + t->base;
53970Sstevel@tonic-gate 				break;
53980Sstevel@tonic-gate 			}
53990Sstevel@tonic-gate 			if (e & 32) {	/* end of block */
54000Sstevel@tonic-gate 				Tracevv((stderr,
54010Sstevel@tonic-gate 				    "inflate:         end of block\n"));
54020Sstevel@tonic-gate 				c->mode = WASH;
54030Sstevel@tonic-gate 				break;
54040Sstevel@tonic-gate 			}
54050Sstevel@tonic-gate 			c->mode = BADCODE;	/* invalid code */
54060Sstevel@tonic-gate 			z->msg = "invalid literal/length code";
54070Sstevel@tonic-gate 			r = Z_DATA_ERROR;
54080Sstevel@tonic-gate 			LEAVE
54090Sstevel@tonic-gate 		case LENEXT:	/* i: getting length extra (have base) */
54100Sstevel@tonic-gate 			j = c->sub.copy.get;
54110Sstevel@tonic-gate 			NEEDBITS(j);
54120Sstevel@tonic-gate 			c->len += (uInt)b & inflate_mask[j];
54130Sstevel@tonic-gate 			DUMPBITS(j);
54140Sstevel@tonic-gate 			c->sub.code.need = c->dbits;
54150Sstevel@tonic-gate 			c->sub.code.tree = c->dtree;
54160Sstevel@tonic-gate 			Tracevv((stderr,
54170Sstevel@tonic-gate 			    "inflate:         length %u\n", c->len));
54180Sstevel@tonic-gate 			c->mode = DIST;
54190Sstevel@tonic-gate 			/* FALLTHRU */
54200Sstevel@tonic-gate 		case DIST:	/* i: get distance next */
54210Sstevel@tonic-gate 			j = c->sub.code.need;
54220Sstevel@tonic-gate 			NEEDBITS(j);
54230Sstevel@tonic-gate 			t = c->sub.code.tree + ((uInt)b & inflate_mask[j]);
54240Sstevel@tonic-gate 			DUMPBITS(t->bits);
54250Sstevel@tonic-gate 			e = (uInt)(t->exop);
54260Sstevel@tonic-gate 			if (e & 16) {	/* distance */
54270Sstevel@tonic-gate 				c->sub.copy.get = e & 15;
54280Sstevel@tonic-gate 				c->sub.copy.dist = t->base;
54290Sstevel@tonic-gate 				c->mode = DISTEXT;
54300Sstevel@tonic-gate 				break;
54310Sstevel@tonic-gate 			}
54320Sstevel@tonic-gate 			if ((e & 64) == 0) {	/* next table */
54330Sstevel@tonic-gate 				c->sub.code.need = e;
54340Sstevel@tonic-gate 				c->sub.code.tree = t + t->base;
54350Sstevel@tonic-gate 				break;
54360Sstevel@tonic-gate 			}
54370Sstevel@tonic-gate 			c->mode = BADCODE;	/* invalid code */
54380Sstevel@tonic-gate 			z->msg = "invalid distance code";
54390Sstevel@tonic-gate 			r = Z_DATA_ERROR;
54400Sstevel@tonic-gate 			LEAVE
54410Sstevel@tonic-gate 		case DISTEXT:	/* i: getting distance extra */
54420Sstevel@tonic-gate 			j = c->sub.copy.get;
54430Sstevel@tonic-gate 			NEEDBITS(j);
54440Sstevel@tonic-gate 			c->sub.copy.dist += (uInt)b & inflate_mask[j];
54450Sstevel@tonic-gate 			DUMPBITS(j);
54460Sstevel@tonic-gate 			Tracevv((stderr,
54470Sstevel@tonic-gate 			    "inflate:         distance %u\n",
54480Sstevel@tonic-gate 			    c->sub.copy.dist));
54490Sstevel@tonic-gate 			c->mode = COPY;
54500Sstevel@tonic-gate 			/* FALLTHRU */
54510Sstevel@tonic-gate 		case COPY:
54520Sstevel@tonic-gate 			/* o: copying bytes in window, waiting for space */
54530Sstevel@tonic-gate #ifndef __TURBOC__ /* Turbo C bug for following expression */
54540Sstevel@tonic-gate 			f = (uInt)(q - s->window) < c->sub.copy.dist ?
54550Sstevel@tonic-gate 			    s->end - (c->sub.copy.dist - (q - s->window)) :
54560Sstevel@tonic-gate 				q - c->sub.copy.dist;
54570Sstevel@tonic-gate #else
54580Sstevel@tonic-gate 			f = q - c->sub.copy.dist;
54590Sstevel@tonic-gate 			if ((uInt)(q - s->window) < c->sub.copy.dist)
54600Sstevel@tonic-gate 				f = s->end - (c->sub.copy.dist -
54610Sstevel@tonic-gate 				    (uInt)(q - s->window));
54620Sstevel@tonic-gate #endif
54630Sstevel@tonic-gate 			while (c->len)
54640Sstevel@tonic-gate 			{
54650Sstevel@tonic-gate 				NEEDOUT;
54660Sstevel@tonic-gate 				OUTBYTE(*f++);
54670Sstevel@tonic-gate 				if (f == s->end)
54680Sstevel@tonic-gate 					f = s->window;
54690Sstevel@tonic-gate 				c->len--;
54700Sstevel@tonic-gate 			}
54710Sstevel@tonic-gate 			c->mode = START;
54720Sstevel@tonic-gate 			break;
54730Sstevel@tonic-gate 		case LIT:	/* o: got literal, waiting for output space */
54740Sstevel@tonic-gate 			NEEDOUT;
54750Sstevel@tonic-gate 			OUTBYTE(c->sub.lit);
54760Sstevel@tonic-gate 			c->mode = START;
54770Sstevel@tonic-gate 			break;
54780Sstevel@tonic-gate 		case WASH:	/* o: got eob, possibly more output */
54790Sstevel@tonic-gate 			if (k > 7) {	/* return unused byte, if any */
54800Sstevel@tonic-gate 				Assert(k < 16,
54810Sstevel@tonic-gate 				    "inflate_codes grabbed too many bytes");
54820Sstevel@tonic-gate 				k -= 8;
54830Sstevel@tonic-gate 				n++;
54840Sstevel@tonic-gate 				p--;	/* can always return one */
54850Sstevel@tonic-gate 			}
54860Sstevel@tonic-gate 			FLUSH;
54870Sstevel@tonic-gate 			if (s->read != s->write)
54880Sstevel@tonic-gate 				LEAVE
54890Sstevel@tonic-gate 			c->mode = END;
54900Sstevel@tonic-gate 			/* FALLTHRU */
54910Sstevel@tonic-gate 		case END:
54920Sstevel@tonic-gate 			r = Z_STREAM_END;
54930Sstevel@tonic-gate 			LEAVE
54940Sstevel@tonic-gate 		case BADCODE:	/* x: got error */
54950Sstevel@tonic-gate 			r = Z_DATA_ERROR;
54960Sstevel@tonic-gate 			LEAVE
54970Sstevel@tonic-gate 		default:
54980Sstevel@tonic-gate 			r = Z_STREAM_ERROR;
54990Sstevel@tonic-gate 			LEAVE
55000Sstevel@tonic-gate 		}
55010Sstevel@tonic-gate 	/* NOTREACHED */
55020Sstevel@tonic-gate 	/* otherwise lint complains */
55030Sstevel@tonic-gate }
55040Sstevel@tonic-gate 
55050Sstevel@tonic-gate 
55060Sstevel@tonic-gate void
inflate_codes_free(c,z)55070Sstevel@tonic-gate inflate_codes_free(c, z)
55080Sstevel@tonic-gate inflate_codes_statef *c;
55090Sstevel@tonic-gate z_streamp z;
55100Sstevel@tonic-gate {
55110Sstevel@tonic-gate 	ZFREE(z, c);
55120Sstevel@tonic-gate 	Tracev((stderr, "inflate:       codes free\n"));
55130Sstevel@tonic-gate }
55140Sstevel@tonic-gate /* --- infcodes.c */
55150Sstevel@tonic-gate 
55160Sstevel@tonic-gate /* +++ infutil.c */
55170Sstevel@tonic-gate /*
55180Sstevel@tonic-gate  * inflate_util.c -- data and routines common to blocks and codes
55190Sstevel@tonic-gate  * Copyright (C) 1995-1998 Mark Adler
55200Sstevel@tonic-gate  * For conditions of distribution and use, see copyright notice in zlib.h
55210Sstevel@tonic-gate  */
55220Sstevel@tonic-gate 
55230Sstevel@tonic-gate /* #include "zutil.h" */
55240Sstevel@tonic-gate /* #include "infblock.h" */
55250Sstevel@tonic-gate /* #include "inftrees.h" */
55260Sstevel@tonic-gate /* #include "infcodes.h" */
55270Sstevel@tonic-gate /* #include "infutil.h" */
55280Sstevel@tonic-gate 
55290Sstevel@tonic-gate #ifndef NO_DUMMY_DECL
55300Sstevel@tonic-gate struct inflate_codes_state {int dummy; };	/* for buggy compilers */
55310Sstevel@tonic-gate #endif
55320Sstevel@tonic-gate 
55330Sstevel@tonic-gate /* And'ing with mask[n] masks the lower n bits */
55340Sstevel@tonic-gate uInt inflate_mask[17] = {
55350Sstevel@tonic-gate 	0x0000,
55360Sstevel@tonic-gate 	0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,
55370Sstevel@tonic-gate 	0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff
55380Sstevel@tonic-gate };
55390Sstevel@tonic-gate 
55400Sstevel@tonic-gate 
55410Sstevel@tonic-gate /* copy as much as possible from the sliding window to the output area */
55420Sstevel@tonic-gate int
inflate_flush(s,z,r)55430Sstevel@tonic-gate inflate_flush(s, z, r)
55440Sstevel@tonic-gate inflate_blocks_statef *s;
55450Sstevel@tonic-gate z_streamp z;
55460Sstevel@tonic-gate int r;
55470Sstevel@tonic-gate {
55480Sstevel@tonic-gate 	uInt n;
55490Sstevel@tonic-gate 	Bytef *p;
55500Sstevel@tonic-gate 	Bytef *q;
55510Sstevel@tonic-gate 
55520Sstevel@tonic-gate 	/* local copies of source and destination pointers */
55530Sstevel@tonic-gate 	p = z->next_out;
55540Sstevel@tonic-gate 	q = s->read;
55550Sstevel@tonic-gate 
55560Sstevel@tonic-gate 	/* compute number of bytes to copy as far as end of window */
55570Sstevel@tonic-gate 	n = (uInt)((q <= s->write ? s->write : s->end) - q);
55580Sstevel@tonic-gate 	if (n > z->avail_out) n = z->avail_out;
55590Sstevel@tonic-gate 	if (n && r == Z_BUF_ERROR) r = Z_OK;
55600Sstevel@tonic-gate 
55610Sstevel@tonic-gate 	/* update counters */
55620Sstevel@tonic-gate 	z->avail_out -= n;
55630Sstevel@tonic-gate 	z->total_out += n;
55640Sstevel@tonic-gate 
55650Sstevel@tonic-gate 	/* update check information */
55660Sstevel@tonic-gate 	if (s->checkfn != Z_NULL)
55670Sstevel@tonic-gate 		z->adler = s->check = (*s->checkfn)(s->check, q, n);
55680Sstevel@tonic-gate 
55690Sstevel@tonic-gate 	/* copy as far as end of window */
55700Sstevel@tonic-gate 	if (p != Z_NULL) {	/* PPP */
55710Sstevel@tonic-gate 		zmemcpy(p, q, n);
55720Sstevel@tonic-gate 		p += n;
55730Sstevel@tonic-gate 	}	/* PPP */
55740Sstevel@tonic-gate 	q += n;
55750Sstevel@tonic-gate 
55760Sstevel@tonic-gate 	/* see if more to copy at beginning of window */
55770Sstevel@tonic-gate 	if (q == s->end)
55780Sstevel@tonic-gate 	{
55790Sstevel@tonic-gate 		/* wrap pointers */
55800Sstevel@tonic-gate 		q = s->window;
55810Sstevel@tonic-gate 		if (s->write == s->end)
55820Sstevel@tonic-gate 			s->write = s->window;
55830Sstevel@tonic-gate 
55840Sstevel@tonic-gate 		/* compute bytes to copy */
55850Sstevel@tonic-gate 		n = (uInt)(s->write - q);
55860Sstevel@tonic-gate 		if (n > z->avail_out) n = z->avail_out;
55870Sstevel@tonic-gate 		if (n && r == Z_BUF_ERROR) r = Z_OK;
55880Sstevel@tonic-gate 
55890Sstevel@tonic-gate 		/* update counters */
55900Sstevel@tonic-gate 		z->avail_out -= n;
55910Sstevel@tonic-gate 		z->total_out += n;
55920Sstevel@tonic-gate 
55930Sstevel@tonic-gate 		/* update check information */
55940Sstevel@tonic-gate 		if (s->checkfn != Z_NULL)
55950Sstevel@tonic-gate 			z->adler = s->check = (*s->checkfn)(s->check, q, n);
55960Sstevel@tonic-gate 
55970Sstevel@tonic-gate 		/* copy */
55980Sstevel@tonic-gate 		if (p != Z_NULL) {	/* PPP */
55990Sstevel@tonic-gate 			zmemcpy(p, q, n);
56000Sstevel@tonic-gate 			p += n;
56010Sstevel@tonic-gate 		}	/* PPP */
56020Sstevel@tonic-gate 		q += n;
56030Sstevel@tonic-gate 	}
56040Sstevel@tonic-gate 
56050Sstevel@tonic-gate 	/* update pointers */
56060Sstevel@tonic-gate 	z->next_out = p;
56070Sstevel@tonic-gate 	s->read = q;
56080Sstevel@tonic-gate 
56090Sstevel@tonic-gate 	/* done */
56100Sstevel@tonic-gate 	return (r);
56110Sstevel@tonic-gate }
56120Sstevel@tonic-gate /* --- infutil.c */
56130Sstevel@tonic-gate 
56140Sstevel@tonic-gate /* +++ inffast.c */
56150Sstevel@tonic-gate /*
56160Sstevel@tonic-gate  * inffast.c -- process literals and length/distance pairs fast
56170Sstevel@tonic-gate  * Copyright (C) 1995-1998 Mark Adler
56180Sstevel@tonic-gate  * For conditions of distribution and use, see copyright notice in zlib.h
56190Sstevel@tonic-gate  */
56200Sstevel@tonic-gate 
56210Sstevel@tonic-gate /* #include "zutil.h" */
56220Sstevel@tonic-gate /* #include "inftrees.h" */
56230Sstevel@tonic-gate /* #include "infblock.h" */
56240Sstevel@tonic-gate /* #include "infcodes.h" */
56250Sstevel@tonic-gate /* #include "infutil.h" */
56260Sstevel@tonic-gate /* #include "inffast.h" */
56270Sstevel@tonic-gate 
56280Sstevel@tonic-gate #ifndef NO_DUMMY_DECL
56290Sstevel@tonic-gate struct inflate_codes_state {int dummy; };	/* for buggy compilers */
56300Sstevel@tonic-gate #endif
56310Sstevel@tonic-gate 
56320Sstevel@tonic-gate /* simplify the use of the inflate_huft type with some defines */
56330Sstevel@tonic-gate #define	exop word.what.Exop
56340Sstevel@tonic-gate #define	bits word.what.Bits
56350Sstevel@tonic-gate 
56360Sstevel@tonic-gate /* macros for bit input with no checking and for returning unused bytes */
56370Sstevel@tonic-gate #define	GRABBITS(j) { while (k < (j)) {b |= ((uLong)NEXTBYTE)<<k; k += 8; }}
56380Sstevel@tonic-gate #define	UNGRAB {c = z->avail_in-n; c = (k>>3) < c?k>>3:c; n += c; p -= c; \
56390Sstevel@tonic-gate 	k -= c<<3; }
56400Sstevel@tonic-gate 
56410Sstevel@tonic-gate /*
56420Sstevel@tonic-gate  * Called with number of bytes left to write in window at least 258
56430Sstevel@tonic-gate  * (the maximum string length) and number of input bytes available at
56440Sstevel@tonic-gate  * least ten.  The ten bytes are six bytes for the longest length/
56450Sstevel@tonic-gate  * distance pair plus four bytes for overloading the bit buffer.
56460Sstevel@tonic-gate  */
56470Sstevel@tonic-gate 
56480Sstevel@tonic-gate int
inflate_fast(bl,bd,tl,td,s,z)56490Sstevel@tonic-gate inflate_fast(bl, bd, tl, td, s, z)
56500Sstevel@tonic-gate uInt bl, bd;
56510Sstevel@tonic-gate const inflate_huft *tl;
56520Sstevel@tonic-gate const inflate_huft *td;	/* need separate declaration for Borland C++ */
56530Sstevel@tonic-gate inflate_blocks_statef *s;
56540Sstevel@tonic-gate z_streamp z;
56550Sstevel@tonic-gate {
56560Sstevel@tonic-gate 	const inflate_huft *t;	/* temporary pointer */
56570Sstevel@tonic-gate 	uInt e;	/* extra bits or operation */
56580Sstevel@tonic-gate 	uLong b;	/* bit buffer */
56590Sstevel@tonic-gate 	uInt k;	/* bits in bit buffer */
56600Sstevel@tonic-gate 	Bytef *p;	/* input data pointer */
56610Sstevel@tonic-gate 	uInt n;	/* bytes available there */
56620Sstevel@tonic-gate 	Bytef *q;	/* output window write pointer */
56630Sstevel@tonic-gate 	uInt m;	/* bytes to end of window or read pointer */
56640Sstevel@tonic-gate 	uInt ml;	/* mask for literal/length tree */
56650Sstevel@tonic-gate 	uInt md;	/* mask for distance tree */
56660Sstevel@tonic-gate 	uInt c;	/* bytes to copy */
56670Sstevel@tonic-gate 	uInt d;	/* distance back to copy from */
56680Sstevel@tonic-gate 	Bytef *r;	/* copy source pointer */
56690Sstevel@tonic-gate 
56700Sstevel@tonic-gate 	/* load input, output, bit values */
56710Sstevel@tonic-gate 	LOAD;
56720Sstevel@tonic-gate 
56730Sstevel@tonic-gate 	/* initialize masks */
56740Sstevel@tonic-gate 	ml = inflate_mask[bl];
56750Sstevel@tonic-gate 	md = inflate_mask[bd];
56760Sstevel@tonic-gate 
56770Sstevel@tonic-gate 	/* do until not enough input or output space for fast loop */
56780Sstevel@tonic-gate 	do {	/* assume called with m >= 258 && n >= 10 */
56790Sstevel@tonic-gate 		/* get literal/length code */
56800Sstevel@tonic-gate 		/* max bits for literal/length code */
56810Sstevel@tonic-gate 		GRABBITS(20);
56820Sstevel@tonic-gate 		if ((e = (t = tl + ((uInt)b & ml))->exop) == 0) {
56830Sstevel@tonic-gate 			DUMPBITS(t->bits);
56840Sstevel@tonic-gate 			Tracevv((stderr, t->base >= 0x20 && t->base < 0x7f ?
56850Sstevel@tonic-gate 			    "inflate:         * literal '%c'\n" :
56860Sstevel@tonic-gate 			    "inflate:         * literal 0x%02x\n", t->base));
56870Sstevel@tonic-gate 			*q++ = (Byte)t->base;
56880Sstevel@tonic-gate 			m--;
56890Sstevel@tonic-gate 			continue;
56900Sstevel@tonic-gate 		}
56910Sstevel@tonic-gate 		do {
56920Sstevel@tonic-gate 			DUMPBITS(t->bits);
56930Sstevel@tonic-gate 			if (e & 16) {
56940Sstevel@tonic-gate 				/* get extra bits for length */
56950Sstevel@tonic-gate 				e &= 15;
56960Sstevel@tonic-gate 				c = t->base + ((uInt)b & inflate_mask[e]);
56970Sstevel@tonic-gate 				DUMPBITS(e);
56980Sstevel@tonic-gate 				Tracevv((stderr,
56990Sstevel@tonic-gate 				    "inflate:         * length %u\n", c));
57000Sstevel@tonic-gate 
57010Sstevel@tonic-gate 				/* decode distance base of block to copy */
57020Sstevel@tonic-gate 				GRABBITS(15);	/* max bits for distance code */
57030Sstevel@tonic-gate 				e = (t = td + ((uInt)b & md))->exop;
57040Sstevel@tonic-gate 				do {
57050Sstevel@tonic-gate 					DUMPBITS(t->bits);
57060Sstevel@tonic-gate 					if (e & 16) {
57070Sstevel@tonic-gate 						/*
57080Sstevel@tonic-gate 						 * get extra bits to
57090Sstevel@tonic-gate 						 * add to distance
57100Sstevel@tonic-gate 						 * base
57110Sstevel@tonic-gate 						 */
57120Sstevel@tonic-gate 						e &= 15;
57130Sstevel@tonic-gate 						/* get extra bits (up to 13) */
57140Sstevel@tonic-gate 						GRABBITS(e);
57150Sstevel@tonic-gate 						d = t->base + ((uInt)b &
57160Sstevel@tonic-gate 						    inflate_mask[e]);
57170Sstevel@tonic-gate 						DUMPBITS(e);
57180Sstevel@tonic-gate 						Tracevv((stderr,
57190Sstevel@tonic-gate 						    "inflate:         * "
57200Sstevel@tonic-gate 						    "distance %u\n", d));
57210Sstevel@tonic-gate 
57220Sstevel@tonic-gate 						/* do the copy */
57230Sstevel@tonic-gate 						m -= c;
57240Sstevel@tonic-gate 						/* offset before dest */
57250Sstevel@tonic-gate 						if ((uInt)(q - s->window) >= d)
57260Sstevel@tonic-gate 							/*  just copy */
57270Sstevel@tonic-gate 						{
57280Sstevel@tonic-gate 							r = q - d;
57290Sstevel@tonic-gate 							/*
57300Sstevel@tonic-gate 							 * minimum
57310Sstevel@tonic-gate 							 * count is
57320Sstevel@tonic-gate 							 * three, so
57330Sstevel@tonic-gate 							 * unroll loop
57340Sstevel@tonic-gate 							 * a little
57350Sstevel@tonic-gate 							 */
57360Sstevel@tonic-gate 							*q++ = *r++;  c--;
57370Sstevel@tonic-gate 							*q++ = *r++;  c--;
57380Sstevel@tonic-gate 						}
57390Sstevel@tonic-gate 					/* else offset after destination */
57400Sstevel@tonic-gate 						else {
57410Sstevel@tonic-gate 	/* bytes from offset to end */
57420Sstevel@tonic-gate 							e = d - (uInt)(q -
57430Sstevel@tonic-gate 							    s->window);
57440Sstevel@tonic-gate 	/* pointer to offset */
57450Sstevel@tonic-gate 							r = s->end - e;
57460Sstevel@tonic-gate 							/* if source crosses */
57470Sstevel@tonic-gate 							if (c > e) {
57480Sstevel@tonic-gate 	/* copy to end of window */
57490Sstevel@tonic-gate 								c -= e;
57500Sstevel@tonic-gate 								do {
57510Sstevel@tonic-gate 									*q++ =
57520Sstevel@tonic-gate 									    *r
57530Sstevel@tonic-gate 									    ++;
57540Sstevel@tonic-gate 								} while (--e);
57550Sstevel@tonic-gate 	/* copy rest from start of window */
57560Sstevel@tonic-gate 								r = s->window;
57570Sstevel@tonic-gate 							}
57580Sstevel@tonic-gate 						}
57590Sstevel@tonic-gate 						/* copy all or what's left */
57600Sstevel@tonic-gate 						do {
57610Sstevel@tonic-gate 							*q++ = *r++;
57620Sstevel@tonic-gate 						} while (--c);
57630Sstevel@tonic-gate 						break;
57640Sstevel@tonic-gate 					} else if ((e & 64) == 0) {
57650Sstevel@tonic-gate 						t += t->base;
57660Sstevel@tonic-gate 						e = (t += ((uInt)b &
57670Sstevel@tonic-gate 						    inflate_mask[e]))->exop;
57680Sstevel@tonic-gate 					} else {
57690Sstevel@tonic-gate 						z->msg =
57700Sstevel@tonic-gate 						    "invalid distance code";
57710Sstevel@tonic-gate 						UNGRAB;
57720Sstevel@tonic-gate 						UPDATE;
57730Sstevel@tonic-gate 						return (Z_DATA_ERROR);
57740Sstevel@tonic-gate 					}
57750Sstevel@tonic-gate 					/* CONSTCOND */
57760Sstevel@tonic-gate 				} while (1);
57770Sstevel@tonic-gate 				break;
57780Sstevel@tonic-gate 			}
57790Sstevel@tonic-gate 			if ((e & 64) == 0)
57800Sstevel@tonic-gate 			{
57810Sstevel@tonic-gate 				t += t->base;
57820Sstevel@tonic-gate 				if ((e = (t += ((uInt)b &
57830Sstevel@tonic-gate 				    inflate_mask[e]))->exop) == 0)
57840Sstevel@tonic-gate 				{
57850Sstevel@tonic-gate 					DUMPBITS(t->bits);
57860Sstevel@tonic-gate 					Tracevv((stderr, t->base >= 0x20 &&
57870Sstevel@tonic-gate 					    t->base < 0x7f ?
57880Sstevel@tonic-gate 					    "inflate:         * literal '%c'\n"
57890Sstevel@tonic-gate 					    :
57900Sstevel@tonic-gate 					    "inflate:         * literal "
57910Sstevel@tonic-gate 					    "0x%02x\n", t->base));
57920Sstevel@tonic-gate 					*q++ = (Byte)t->base;
57930Sstevel@tonic-gate 					m--;
57940Sstevel@tonic-gate 					break;
57950Sstevel@tonic-gate 				}
57960Sstevel@tonic-gate 			} else if (e & 32) {
57970Sstevel@tonic-gate 				Tracevv((stderr,
57980Sstevel@tonic-gate 				    "inflate:         * end of block\n"));
57990Sstevel@tonic-gate 				UNGRAB;
58000Sstevel@tonic-gate 				UPDATE;
58010Sstevel@tonic-gate 				return (Z_STREAM_END);
58020Sstevel@tonic-gate 			} else {
58030Sstevel@tonic-gate 				z->msg = "invalid literal/length code";
58040Sstevel@tonic-gate 				UNGRAB;
58050Sstevel@tonic-gate 				UPDATE;
58060Sstevel@tonic-gate 				return (Z_DATA_ERROR);
58070Sstevel@tonic-gate 			}
58080Sstevel@tonic-gate 			/* CONSTCOND */
58090Sstevel@tonic-gate 		} while (1);
58100Sstevel@tonic-gate 	} while (m >= 258 && n >= 10);
58110Sstevel@tonic-gate 
58120Sstevel@tonic-gate 	/* not enough input or output--restore pointers and return */
58130Sstevel@tonic-gate 	UNGRAB;
58140Sstevel@tonic-gate 	UPDATE;
58150Sstevel@tonic-gate 	return (Z_OK);
58160Sstevel@tonic-gate }
58170Sstevel@tonic-gate /* --- inffast.c */
58180Sstevel@tonic-gate 
58190Sstevel@tonic-gate /* +++ zutil.c */
58200Sstevel@tonic-gate /*
58210Sstevel@tonic-gate  * zutil.c -- target dependent utility functions for the compression library
58220Sstevel@tonic-gate  * Copyright (C) 1995-1998 Jean-loup Gailly.
58230Sstevel@tonic-gate  * For conditions of distribution and use, see copyright notice in zlib.h
58240Sstevel@tonic-gate  */
58250Sstevel@tonic-gate 
58260Sstevel@tonic-gate /* From: zutil.c,v 1.17 1996/07/24 13:41:12 me Exp $ */
58270Sstevel@tonic-gate 
58280Sstevel@tonic-gate #ifdef DEBUG_ZLIB
58290Sstevel@tonic-gate #include <stdio.h>
58300Sstevel@tonic-gate #endif
58310Sstevel@tonic-gate 
58320Sstevel@tonic-gate /* #include "zutil.h" */
58330Sstevel@tonic-gate 
58340Sstevel@tonic-gate #ifndef NO_DUMMY_DECL
58350Sstevel@tonic-gate struct internal_state	{int dummy; };	/* for buggy compilers */
58360Sstevel@tonic-gate #endif
58370Sstevel@tonic-gate 
58380Sstevel@tonic-gate #ifndef STDC
58390Sstevel@tonic-gate extern void exit OF((int));
58400Sstevel@tonic-gate #endif
58410Sstevel@tonic-gate 
5842*3886Sahl static const char *z_errmsg[10] = {
58430Sstevel@tonic-gate "need dictionary",	/* Z_NEED_DICT		2 */
58440Sstevel@tonic-gate "stream end",		/* Z_STREAM_END		1 */
58450Sstevel@tonic-gate "",			/* Z_OK			0 */
58460Sstevel@tonic-gate "file error",		/* Z_ERRNO		(-1) */
58470Sstevel@tonic-gate "stream error",		/* Z_STREAM_ERROR	(-2) */
58480Sstevel@tonic-gate "data error",		/* Z_DATA_ERROR		(-3) */
58490Sstevel@tonic-gate "insufficient memory",	/* Z_MEM_ERROR		(-4) */
58500Sstevel@tonic-gate "buffer error",		/* Z_BUF_ERROR		(-5) */
58510Sstevel@tonic-gate "incompatible version",	/* Z_VERSION_ERROR	(-6) */
58520Sstevel@tonic-gate ""};
58530Sstevel@tonic-gate 
58540Sstevel@tonic-gate 
58550Sstevel@tonic-gate const char *
zlibVersion()58560Sstevel@tonic-gate zlibVersion()
58570Sstevel@tonic-gate {
58580Sstevel@tonic-gate 	return (ZLIB_VERSION);
58590Sstevel@tonic-gate }
58600Sstevel@tonic-gate 
58610Sstevel@tonic-gate #ifdef DEBUG_ZLIB
58620Sstevel@tonic-gate void
z_error(m)58630Sstevel@tonic-gate z_error(m)
58640Sstevel@tonic-gate     char *m;
58650Sstevel@tonic-gate {
58660Sstevel@tonic-gate 	fprintf(stderr, "%s\n", m);
58670Sstevel@tonic-gate 	exit(1);
58680Sstevel@tonic-gate }
58690Sstevel@tonic-gate #endif
58700Sstevel@tonic-gate 
58710Sstevel@tonic-gate #ifndef HAVE_MEMCPY
58720Sstevel@tonic-gate 
58730Sstevel@tonic-gate void
zmemcpy(dest,source,len)58740Sstevel@tonic-gate zmemcpy(dest, source, len)
58750Sstevel@tonic-gate     Bytef* dest;
58760Sstevel@tonic-gate     const Bytef* source;
58770Sstevel@tonic-gate     uInt  len;
58780Sstevel@tonic-gate {
58790Sstevel@tonic-gate 	if (len == 0)
58800Sstevel@tonic-gate 		return;
58810Sstevel@tonic-gate 	do {
58820Sstevel@tonic-gate 		*dest++ = *source++;	/* ??? to be unrolled */
58830Sstevel@tonic-gate 	} while (--len != 0);
58840Sstevel@tonic-gate }
58850Sstevel@tonic-gate 
58860Sstevel@tonic-gate int
zmemcmp(s1,s2,len)58870Sstevel@tonic-gate zmemcmp(s1, s2, len)
58880Sstevel@tonic-gate const Bytef* s1;
58890Sstevel@tonic-gate const Bytef* s2;
58900Sstevel@tonic-gate uInt  len;
58910Sstevel@tonic-gate {
58920Sstevel@tonic-gate 	uInt j;
58930Sstevel@tonic-gate 
58940Sstevel@tonic-gate 	for (j = 0; j < len; j++) {
58950Sstevel@tonic-gate 		if (s1[j] != s2[j])
58960Sstevel@tonic-gate 			return (2*(s1[j] > s2[j])-1);
58970Sstevel@tonic-gate 	}
58980Sstevel@tonic-gate 	return (0);
58990Sstevel@tonic-gate }
59000Sstevel@tonic-gate 
59010Sstevel@tonic-gate void
zmemzero(dest,len)59020Sstevel@tonic-gate zmemzero(dest, len)
59030Sstevel@tonic-gate     Bytef* dest;
59040Sstevel@tonic-gate     uInt  len;
59050Sstevel@tonic-gate {
59060Sstevel@tonic-gate 	if (len == 0)
59070Sstevel@tonic-gate 		return;
59080Sstevel@tonic-gate 	do {
59090Sstevel@tonic-gate 		*dest++ = 0;	/* ??? to be unrolled */
59100Sstevel@tonic-gate 	} while (--len != 0);
59110Sstevel@tonic-gate }
59120Sstevel@tonic-gate #endif
59130Sstevel@tonic-gate 
59140Sstevel@tonic-gate #ifdef __TURBOC__
59150Sstevel@tonic-gate #if (defined(__BORLANDC__) || !defined(SMALL_MEDIUM)) && !defined(__32BIT__)
59160Sstevel@tonic-gate /*
59170Sstevel@tonic-gate  * Small and medium model in Turbo C are for now limited to near
59180Sstevel@tonic-gate  * allocation with reduced MAX_WBITS and MAX_MEM_LEVEL
59190Sstevel@tonic-gate  */
59200Sstevel@tonic-gate #define	MY_ZCALLOC
59210Sstevel@tonic-gate 
59220Sstevel@tonic-gate /*
59230Sstevel@tonic-gate  * Turbo C malloc() does not allow dynamic allocation of 64K bytes and
59240Sstevel@tonic-gate  * farmalloc(64K) returns a pointer with an offset of 8, so we must
59250Sstevel@tonic-gate  * fix the pointer. Warning: the pointer must be put back to its
59260Sstevel@tonic-gate  * original form in order to free it, use zcfree().
59270Sstevel@tonic-gate  */
59280Sstevel@tonic-gate 
59290Sstevel@tonic-gate #define	MAX_PTR 10
59300Sstevel@tonic-gate /* 10*64K = 640K */
59310Sstevel@tonic-gate 
59320Sstevel@tonic-gate local int next_ptr = 0;
59330Sstevel@tonic-gate 
59340Sstevel@tonic-gate typedef struct ptr_table_s {
59350Sstevel@tonic-gate 	voidpf org_ptr;
59360Sstevel@tonic-gate 	voidpf new_ptr;
59370Sstevel@tonic-gate } ptr_table;
59380Sstevel@tonic-gate 
59390Sstevel@tonic-gate local ptr_table table[MAX_PTR];
59400Sstevel@tonic-gate /*
59410Sstevel@tonic-gate  * This table is used to remember the original form of pointers to
59420Sstevel@tonic-gate  * large buffers (64K). Such pointers are normalized with a zero
59430Sstevel@tonic-gate  * offset.  Since MSDOS is not a preemptive multitasking OS, this
59440Sstevel@tonic-gate  * table is not protected from concurrent access. This hack doesn't
59450Sstevel@tonic-gate  * work anyway on a protected system like OS/2. Use Microsoft C
59460Sstevel@tonic-gate  * instead.
59470Sstevel@tonic-gate  */
59480Sstevel@tonic-gate 
59490Sstevel@tonic-gate voidpf
zcalloc(voidpf opaque,unsigned items,unsigned size)59500Sstevel@tonic-gate zcalloc(voidpf opaque, unsigned items, unsigned size)
59510Sstevel@tonic-gate {
59520Sstevel@tonic-gate 	voidpf buf = opaque;	/* just to make some compilers happy */
59530Sstevel@tonic-gate 	ulg bsize = (ulg)items*size;
59540Sstevel@tonic-gate 
59550Sstevel@tonic-gate 	/*
59560Sstevel@tonic-gate 	 * If we allocate less than 65520 bytes, we assume that
59570Sstevel@tonic-gate 	 * farmalloc will return a usable pointer which doesn't have
59580Sstevel@tonic-gate 	 * to be normalized.
59590Sstevel@tonic-gate 	 */
59600Sstevel@tonic-gate 	if (bsize < 65520L) {
59610Sstevel@tonic-gate 		buf = farmalloc(bsize);
59620Sstevel@tonic-gate 		if (*(ush *)&buf != 0)
59630Sstevel@tonic-gate 			return (buf);
59640Sstevel@tonic-gate 	} else {
59650Sstevel@tonic-gate 		buf = farmalloc(bsize + 16L);
59660Sstevel@tonic-gate 	}
59670Sstevel@tonic-gate 	if (buf == NULL || next_ptr >= MAX_PTR)
59680Sstevel@tonic-gate 		return (NULL);
59690Sstevel@tonic-gate 	table[next_ptr].org_ptr = buf;
59700Sstevel@tonic-gate 
59710Sstevel@tonic-gate 	/* Normalize the pointer to seg:0 */
59720Sstevel@tonic-gate 	*((ush *)&buf+1) += ((ush)((uch *)buf-0) + 15) >> 4;
59730Sstevel@tonic-gate 	*(ush *)&buf = 0;
59740Sstevel@tonic-gate 	table[next_ptr++].new_ptr = buf;
59750Sstevel@tonic-gate 	return (buf);
59760Sstevel@tonic-gate }
59770Sstevel@tonic-gate 
59780Sstevel@tonic-gate void
zcfree(voidpf opaque,voidpf ptr)59790Sstevel@tonic-gate zcfree(voidpf opaque, voidpf ptr)
59800Sstevel@tonic-gate {
59810Sstevel@tonic-gate 	int n;
59820Sstevel@tonic-gate 	if (*(ush*)&ptr != 0) { /* object < 64K */
59830Sstevel@tonic-gate 		farfree(ptr);
59840Sstevel@tonic-gate 		return;
59850Sstevel@tonic-gate 	}
59860Sstevel@tonic-gate 	/* Find the original pointer */
59870Sstevel@tonic-gate 	for (n = 0; n < next_ptr; n++) {
59880Sstevel@tonic-gate 		if (ptr != table[n].new_ptr)
59890Sstevel@tonic-gate 			continue;
59900Sstevel@tonic-gate 
59910Sstevel@tonic-gate 		farfree(table[n].org_ptr);
59920Sstevel@tonic-gate 		while (++n < next_ptr) {
59930Sstevel@tonic-gate 			table[n-1] = table[n];
59940Sstevel@tonic-gate 		}
59950Sstevel@tonic-gate 		next_ptr--;
59960Sstevel@tonic-gate 		return;
59970Sstevel@tonic-gate 	}
59980Sstevel@tonic-gate 	ptr = opaque;	/* just to make some compilers happy */
59990Sstevel@tonic-gate 	Assert(0, "zcfree: ptr not found");
60000Sstevel@tonic-gate }
60010Sstevel@tonic-gate #endif
60020Sstevel@tonic-gate #endif /* __TURBOC__ */
60030Sstevel@tonic-gate 
60040Sstevel@tonic-gate 
60050Sstevel@tonic-gate #if defined(M_I86) && !defined(__32BIT__)
60060Sstevel@tonic-gate /* Microsoft C in 16-bit mode */
60070Sstevel@tonic-gate 
60080Sstevel@tonic-gate #define	MY_ZCALLOC
60090Sstevel@tonic-gate 
60100Sstevel@tonic-gate #if (!defined(_MSC_VER) || (_MSC_VER <= 600))
60110Sstevel@tonic-gate #define	_halloc  halloc
60120Sstevel@tonic-gate #define	_hfree   hfree
60130Sstevel@tonic-gate #endif
60140Sstevel@tonic-gate 
60150Sstevel@tonic-gate voidpf
zcalloc(voidpf opaque,unsigned items,unsigned size)60160Sstevel@tonic-gate zcalloc(voidpf opaque, unsigned items, unsigned size)
60170Sstevel@tonic-gate {
60180Sstevel@tonic-gate 	if (opaque) opaque = 0;	/* to make compiler happy */
60190Sstevel@tonic-gate 	return (_halloc((long)items, size));
60200Sstevel@tonic-gate }
60210Sstevel@tonic-gate 
60220Sstevel@tonic-gate void
zcfree(voidpf opaque,voidpf ptr)60230Sstevel@tonic-gate zcfree(voidpf opaque, voidpf ptr)
60240Sstevel@tonic-gate {
60250Sstevel@tonic-gate 	if (opaque) opaque = 0;	/* to make compiler happy */
60260Sstevel@tonic-gate 	_hfree(ptr);
60270Sstevel@tonic-gate }
60280Sstevel@tonic-gate 
60290Sstevel@tonic-gate #endif /* MSC */
60300Sstevel@tonic-gate 
60310Sstevel@tonic-gate 
60320Sstevel@tonic-gate #ifndef MY_ZCALLOC /* Any system without a special alloc function */
60330Sstevel@tonic-gate 
60340Sstevel@tonic-gate #ifndef STDC
60350Sstevel@tonic-gate extern voidp  calloc OF((uInt items, uInt size));
60360Sstevel@tonic-gate extern void   free   OF((voidpf ptr));
60370Sstevel@tonic-gate #endif
60380Sstevel@tonic-gate 
60390Sstevel@tonic-gate voidpf
zcalloc(opaque,items,size)60400Sstevel@tonic-gate zcalloc(opaque, items, size)
60410Sstevel@tonic-gate     voidpf opaque;
60420Sstevel@tonic-gate     unsigned items;
60430Sstevel@tonic-gate     unsigned size;
60440Sstevel@tonic-gate {
60450Sstevel@tonic-gate 	if (opaque) items += size - size;	/* make compiler happy */
60460Sstevel@tonic-gate 	return ((voidpf)calloc(items, size));
60470Sstevel@tonic-gate }
60480Sstevel@tonic-gate 
60490Sstevel@tonic-gate /*ARGSUSED*/
60500Sstevel@tonic-gate void
zcfree(opaque,ptr)60510Sstevel@tonic-gate zcfree(opaque, ptr)
60520Sstevel@tonic-gate     voidpf opaque;
60530Sstevel@tonic-gate     voidpf ptr;
60540Sstevel@tonic-gate {
60550Sstevel@tonic-gate 	free(ptr);
60560Sstevel@tonic-gate }
60570Sstevel@tonic-gate 
60580Sstevel@tonic-gate #endif /* MY_ZCALLOC */
60590Sstevel@tonic-gate /* --- zutil.c */
60600Sstevel@tonic-gate 
60610Sstevel@tonic-gate /* +++ adler32.c */
60620Sstevel@tonic-gate /*
60630Sstevel@tonic-gate  * adler32.c -- compute the Adler-32 checksum of a data stream
60640Sstevel@tonic-gate  * Copyright (C) 1995-1998 Mark Adler
60650Sstevel@tonic-gate  * For conditions of distribution and use, see copyright notice in zlib.h
60660Sstevel@tonic-gate  */
60670Sstevel@tonic-gate 
60680Sstevel@tonic-gate /* From: adler32.c,v 1.10 1996/05/22 11:52:18 me Exp $ */
60690Sstevel@tonic-gate 
60700Sstevel@tonic-gate /* #include "zlib.h" */
60710Sstevel@tonic-gate 
60720Sstevel@tonic-gate #define	BASE 65521L /* largest prime smaller than 65536 */
60730Sstevel@tonic-gate #define	NMAX 5552
60740Sstevel@tonic-gate /* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */
60750Sstevel@tonic-gate 
60760Sstevel@tonic-gate #define	DO1(buf, i)  {s1 += buf[i]; s2 += s1; }
60770Sstevel@tonic-gate #define	DO2(buf, i)  DO1(buf, i); DO1(buf, i+1);
60780Sstevel@tonic-gate #define	DO4(buf, i)  DO2(buf, i); DO2(buf, i+2);
60790Sstevel@tonic-gate #define	DO8(buf, i)  DO4(buf, i); DO4(buf, i+4);
60800Sstevel@tonic-gate #define	DO16(buf)   DO8(buf, 0); DO8(buf, 8);
60810Sstevel@tonic-gate 
60820Sstevel@tonic-gate /* ========================================================================= */
60830Sstevel@tonic-gate uLong
adler32(adler,buf,len)60840Sstevel@tonic-gate adler32(adler, buf, len)
60850Sstevel@tonic-gate     uLong adler;
60860Sstevel@tonic-gate     const Bytef *buf;
60870Sstevel@tonic-gate     uInt len;
60880Sstevel@tonic-gate {
60890Sstevel@tonic-gate 	unsigned long s1 = adler & 0xffff;
60900Sstevel@tonic-gate 	unsigned long s2 = (adler >> 16) & 0xffff;
60910Sstevel@tonic-gate 	int k;
60920Sstevel@tonic-gate 
60930Sstevel@tonic-gate 	if (buf == Z_NULL)
60940Sstevel@tonic-gate 		return (1L);
60950Sstevel@tonic-gate 
60960Sstevel@tonic-gate 	while (len > 0) {
60970Sstevel@tonic-gate 		k = len < NMAX ? len : NMAX;
60980Sstevel@tonic-gate 		len -= k;
60990Sstevel@tonic-gate 		while (k >= 16) {
61000Sstevel@tonic-gate 			DO16(buf);
61010Sstevel@tonic-gate 			buf += 16;
61020Sstevel@tonic-gate 			k -= 16;
61030Sstevel@tonic-gate 		}
61040Sstevel@tonic-gate 		if (k != 0) do {
61050Sstevel@tonic-gate 			s1 += *buf++;
61060Sstevel@tonic-gate 			s2 += s1;
61070Sstevel@tonic-gate 		} while (--k);
61080Sstevel@tonic-gate 		s1 %= BASE;
61090Sstevel@tonic-gate 		s2 %= BASE;
61100Sstevel@tonic-gate 	}
61110Sstevel@tonic-gate 	return ((s2 << 16) | s1);
61120Sstevel@tonic-gate }
61130Sstevel@tonic-gate /* --- adler32.c */
6114